@@ -20,359 +20,359 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Post_Adapter { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Log_Service} logging instance. |
|
| 25 | - * |
|
| 26 | - * @access private |
|
| 27 | - * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
| 28 | - */ |
|
| 29 | - private $log; |
|
| 30 | - |
|
| 31 | - public function __construct() { |
|
| 32 | - |
|
| 33 | - // Bail out if block editor's functions aren't available. |
|
| 34 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 35 | - return; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 39 | - |
|
| 40 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 41 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 42 | - |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
| 47 | - * correctly. |
|
| 48 | - */ |
|
| 49 | - public function init() { |
|
| 50 | - |
|
| 51 | - register_block_type( 'wordlift/classification', array( |
|
| 52 | - 'editor_script' => 'wl-block-editor', |
|
| 53 | - 'attributes' => array( |
|
| 54 | - 'entities' => array( 'type' => 'array' ), |
|
| 55 | - ), |
|
| 56 | - ) ); |
|
| 57 | - |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * A sample structure: |
|
| 62 | - * |
|
| 63 | - * { |
|
| 64 | - * "entities": [ |
|
| 65 | - * { |
|
| 66 | - * "annotations": { |
|
| 67 | - * "urn:enhancement-7e8e66fc": { |
|
| 68 | - * "start": 3480, |
|
| 69 | - * "end": 3486, |
|
| 70 | - * "text": "libero" |
|
| 71 | - * } |
|
| 72 | - * }, |
|
| 73 | - * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
| 74 | - * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
| 75 | - * "label": "Libero (volley-ball)", |
|
| 76 | - * "mainType": "other", |
|
| 77 | - * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
| 78 | - * "sameAs": null, |
|
| 79 | - * "synonyms": [], |
|
| 80 | - * "types": ["other"] |
|
| 81 | - * } |
|
| 82 | - * ] |
|
| 83 | - * } |
|
| 84 | - * |
|
| 85 | - * @param array $data An array of slashed post data. |
|
| 86 | - * @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|
| 87 | - * |
|
| 88 | - * @return array The data array. |
|
| 89 | - */ |
|
| 90 | - public function wp_insert_post_data( $data, $postarr ) { |
|
| 91 | - |
|
| 92 | - $post_status = $data['post_status']; |
|
| 93 | - if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
| 94 | - return $data; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 98 | - . var_export( $data, true ) ); |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - try { |
|
| 102 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 103 | - |
|
| 104 | - foreach ( $entities as $entity ) { |
|
| 105 | - |
|
| 106 | - $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
| 107 | - |
|
| 108 | - if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
| 109 | - continue; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $entity_uris = $this->get_entity_uris( $entity ); |
|
| 113 | - |
|
| 114 | - if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
| 115 | - Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
| 116 | - // Skip the entity |
|
| 117 | - continue; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // If 'entity auto publish' is false, we set the status to `draft` by default. |
|
| 121 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
| 122 | - ? $data['post_status'] : 'draft'; |
|
| 123 | - $this->create_or_update_entity( $entity, $post_status ); |
|
| 124 | - |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - } catch ( \Exception $e ) { |
|
| 128 | - $this->log->error( $e->getMessage() ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - return $data; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
| 137 | - * |
|
| 138 | - * @param string $post_content The post content. |
|
| 139 | - * |
|
| 140 | - * @return array An array of entities' structures. |
|
| 141 | - * @throws \Exception |
|
| 142 | - */ |
|
| 143 | - private function parse_content( $post_content ) { |
|
| 144 | - |
|
| 145 | - $all_blocks = parse_blocks( $post_content ); |
|
| 146 | - $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 147 | - . var_export( $all_blocks, true ) ); |
|
| 148 | - |
|
| 149 | - $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 150 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 151 | - } ); |
|
| 152 | - |
|
| 153 | - // Bail out if the blocks' array is empty. |
|
| 154 | - if ( empty( $blocks ) ) { |
|
| 155 | - return array(); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $block = current( $blocks ); |
|
| 159 | - $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 160 | - . var_export( $block, true ) ); |
|
| 161 | - |
|
| 162 | - // Bail out if the entities array is empty. |
|
| 163 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 164 | - return array(); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $block['attrs']['entities']; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Collect entity labels from the entity array. |
|
| 172 | - * |
|
| 173 | - * This function expects an array with the following keys: |
|
| 174 | - * |
|
| 175 | - * array( |
|
| 176 | - * 'label' => ..., |
|
| 177 | - * 'synonyms' => array( ... ), |
|
| 178 | - * 'annotations' => array( |
|
| 179 | - * ...id... => array( text => ... ), |
|
| 180 | - * ), |
|
| 181 | - * 'occurrences' => array( ... ), |
|
| 182 | - * ) |
|
| 183 | - * |
|
| 184 | - * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
| 185 | - * |
|
| 186 | - * array( |
|
| 187 | - * ...label..., |
|
| 188 | - * ...synonyms..., |
|
| 189 | - * ...texts..., |
|
| 190 | - * ) |
|
| 191 | - * |
|
| 192 | - * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
| 193 | - * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
| 194 | - * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
| 195 | - * `occurrences`. |
|
| 196 | - * |
|
| 197 | - * @param array $entity { |
|
| 198 | - * The entity data. |
|
| 199 | - * |
|
| 200 | - * @type string $label The entity label. |
|
| 201 | - * @type array $synonyms The entity synonyms. |
|
| 202 | - * @type array $occurrences The selected occurrences. |
|
| 203 | - * @type array $annotations The annotations. |
|
| 204 | - * } |
|
| 205 | - * |
|
| 206 | - * @return array An array of labels. |
|
| 207 | - */ |
|
| 208 | - public function get_labels( $entity ) { |
|
| 209 | - |
|
| 210 | - $args = wp_parse_args( $entity, array( |
|
| 211 | - 'label' => array(), |
|
| 212 | - 'synonyms' => array(), |
|
| 213 | - 'annotations' => array(), |
|
| 214 | - 'occurrences' => array(), |
|
| 215 | - ) ); |
|
| 216 | - |
|
| 217 | - // We gather all the labels, occurrences texts and synonyms into one array. |
|
| 218 | - $initial = array_merge( |
|
| 219 | - (array) $args['label'], |
|
| 220 | - (array) $args['synonyms'] |
|
| 221 | - ); |
|
| 222 | - |
|
| 223 | - $annotations = $args['annotations']; |
|
| 224 | - |
|
| 225 | - return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 226 | - |
|
| 227 | - // Bail out if occurrences->$item->text isn't set or its contents are already |
|
| 228 | - // in `$carry`. |
|
| 229 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 230 | - || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 231 | - return $carry; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Push the label. |
|
| 235 | - $carry[] = $annotations[ $item ]['text']; |
|
| 236 | - |
|
| 237 | - return $carry; |
|
| 238 | - }, $initial ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Create or update the entity. |
|
| 243 | - * |
|
| 244 | - * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
| 245 | - * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
| 246 | - * |
|
| 247 | - * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
| 248 | - * |
|
| 249 | - * @param array $entity { |
|
| 250 | - * The entity parameters. |
|
| 251 | - * |
|
| 252 | - * @type string The entity item id URI. |
|
| 253 | - * @type string|array The entity sameAs URI(s). |
|
| 254 | - * @type string $description The entity description. |
|
| 255 | - * } |
|
| 256 | - * |
|
| 257 | - * @param $string $post_status The post status, default 'draft'. |
|
| 258 | - * |
|
| 259 | - * @return int|\WP_Error |
|
| 260 | - * @throws \Exception |
|
| 261 | - */ |
|
| 262 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 263 | - |
|
| 264 | - // Get only valid IDs. |
|
| 265 | - $uris = $this->get_entity_uris( $entity ); |
|
| 266 | - |
|
| 267 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 268 | - |
|
| 269 | - $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 270 | - . var_export( $uris, true ) ); |
|
| 271 | - |
|
| 272 | - // Get the labels. |
|
| 273 | - $labels = $this->get_labels( $entity ); |
|
| 274 | - |
|
| 275 | - if ( empty( $post ) ) { |
|
| 276 | - // Create the entity if it doesn't exist. |
|
| 277 | - $post_id = Entity_Store::get_instance()->create( array( |
|
| 278 | - 'labels' => $labels, |
|
| 279 | - 'description' => $entity['description'], |
|
| 280 | - 'same_as' => $uris, |
|
| 281 | - ), $post_status ); |
|
| 282 | - |
|
| 283 | - // Return the WP_Error if we got one. |
|
| 284 | - if ( is_wp_error( $post_id ) ) { |
|
| 285 | - return $post_id; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - // Add the entity type. |
|
| 289 | - if ( isset( $entity['mainType'] ) ) { |
|
| 290 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 294 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 295 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 296 | - } |
|
| 297 | - } else { |
|
| 298 | - // Update the entity otherwise. |
|
| 299 | - $post_id = Entity_Store::get_instance()->update( array( |
|
| 300 | - 'ID' => $post->ID, |
|
| 301 | - 'labels' => $labels, |
|
| 302 | - 'same_as' => $uris, |
|
| 303 | - ) ); |
|
| 304 | - |
|
| 305 | - // Add the entity type. |
|
| 306 | - if ( isset( $entity['mainType'] ) ) { |
|
| 307 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
|
| 311 | - // Set the post status, we need to set that in order to support entities |
|
| 312 | - // created using rest endpoint on block editor, so that they get published |
|
| 313 | - // when the post is published. |
|
| 314 | - // Once the entity is published dont update the post status. |
|
| 315 | - if ( $post->post_status !== 'publish' ) { |
|
| 316 | - wp_update_post( array( |
|
| 317 | - 'ID' => $post->ID, |
|
| 318 | - 'post_status' => $post_status |
|
| 319 | - ) ); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - return $post_id; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Get the first matching entity for the provided URI array. |
|
| 328 | - * |
|
| 329 | - * Entities IDs and sameAs are searched. |
|
| 330 | - * |
|
| 331 | - * @param array $uris An array of URIs. |
|
| 332 | - * |
|
| 333 | - * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
| 334 | - */ |
|
| 335 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
| 336 | - |
|
| 337 | - foreach ( $uris as $uri ) { |
|
| 338 | - $existing_entity = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri )->get_bag(); |
|
| 339 | - if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
| 340 | - return $existing_entity; |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - return null; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * @param $entity |
|
| 349 | - * |
|
| 350 | - * @return array |
|
| 351 | - */ |
|
| 352 | - private function filter_valid_entity_ids( $entity ) { |
|
| 353 | - $id = $entity['id']; |
|
| 354 | - |
|
| 355 | - return array_filter( (array) $id, function ( $id ) { |
|
| 356 | - return preg_match( '|^https?://|', $id ); |
|
| 357 | - } ); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @param array $entity |
|
| 362 | - * |
|
| 363 | - * @return array |
|
| 364 | - */ |
|
| 365 | - private function get_entity_uris( $entity ) { |
|
| 366 | - $ids = $this->filter_valid_entity_ids( $entity ); |
|
| 367 | - |
|
| 368 | - return array_merge( |
|
| 369 | - (array) $ids, |
|
| 370 | - (array) $entity['sameAs'] |
|
| 371 | - ); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - private function entity_id_valid( $entity_id ) { |
|
| 375 | - return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
| 376 | - } |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Log_Service} logging instance. |
|
| 25 | + * |
|
| 26 | + * @access private |
|
| 27 | + * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
| 28 | + */ |
|
| 29 | + private $log; |
|
| 30 | + |
|
| 31 | + public function __construct() { |
|
| 32 | + |
|
| 33 | + // Bail out if block editor's functions aren't available. |
|
| 34 | + if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 35 | + return; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 39 | + |
|
| 40 | + add_action( 'init', array( $this, 'init' ) ); |
|
| 41 | + add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 42 | + |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
| 47 | + * correctly. |
|
| 48 | + */ |
|
| 49 | + public function init() { |
|
| 50 | + |
|
| 51 | + register_block_type( 'wordlift/classification', array( |
|
| 52 | + 'editor_script' => 'wl-block-editor', |
|
| 53 | + 'attributes' => array( |
|
| 54 | + 'entities' => array( 'type' => 'array' ), |
|
| 55 | + ), |
|
| 56 | + ) ); |
|
| 57 | + |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * A sample structure: |
|
| 62 | + * |
|
| 63 | + * { |
|
| 64 | + * "entities": [ |
|
| 65 | + * { |
|
| 66 | + * "annotations": { |
|
| 67 | + * "urn:enhancement-7e8e66fc": { |
|
| 68 | + * "start": 3480, |
|
| 69 | + * "end": 3486, |
|
| 70 | + * "text": "libero" |
|
| 71 | + * } |
|
| 72 | + * }, |
|
| 73 | + * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
| 74 | + * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
| 75 | + * "label": "Libero (volley-ball)", |
|
| 76 | + * "mainType": "other", |
|
| 77 | + * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
| 78 | + * "sameAs": null, |
|
| 79 | + * "synonyms": [], |
|
| 80 | + * "types": ["other"] |
|
| 81 | + * } |
|
| 82 | + * ] |
|
| 83 | + * } |
|
| 84 | + * |
|
| 85 | + * @param array $data An array of slashed post data. |
|
| 86 | + * @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|
| 87 | + * |
|
| 88 | + * @return array The data array. |
|
| 89 | + */ |
|
| 90 | + public function wp_insert_post_data( $data, $postarr ) { |
|
| 91 | + |
|
| 92 | + $post_status = $data['post_status']; |
|
| 93 | + if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
| 94 | + return $data; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 98 | + . var_export( $data, true ) ); |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + try { |
|
| 102 | + $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 103 | + |
|
| 104 | + foreach ( $entities as $entity ) { |
|
| 105 | + |
|
| 106 | + $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
| 107 | + |
|
| 108 | + if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
| 109 | + continue; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $entity_uris = $this->get_entity_uris( $entity ); |
|
| 113 | + |
|
| 114 | + if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
| 115 | + Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
| 116 | + // Skip the entity |
|
| 117 | + continue; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // If 'entity auto publish' is false, we set the status to `draft` by default. |
|
| 121 | + $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
| 122 | + ? $data['post_status'] : 'draft'; |
|
| 123 | + $this->create_or_update_entity( $entity, $post_status ); |
|
| 124 | + |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + } catch ( \Exception $e ) { |
|
| 128 | + $this->log->error( $e->getMessage() ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + return $data; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
| 137 | + * |
|
| 138 | + * @param string $post_content The post content. |
|
| 139 | + * |
|
| 140 | + * @return array An array of entities' structures. |
|
| 141 | + * @throws \Exception |
|
| 142 | + */ |
|
| 143 | + private function parse_content( $post_content ) { |
|
| 144 | + |
|
| 145 | + $all_blocks = parse_blocks( $post_content ); |
|
| 146 | + $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 147 | + . var_export( $all_blocks, true ) ); |
|
| 148 | + |
|
| 149 | + $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 150 | + return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 151 | + } ); |
|
| 152 | + |
|
| 153 | + // Bail out if the blocks' array is empty. |
|
| 154 | + if ( empty( $blocks ) ) { |
|
| 155 | + return array(); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $block = current( $blocks ); |
|
| 159 | + $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 160 | + . var_export( $block, true ) ); |
|
| 161 | + |
|
| 162 | + // Bail out if the entities array is empty. |
|
| 163 | + if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 164 | + return array(); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $block['attrs']['entities']; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Collect entity labels from the entity array. |
|
| 172 | + * |
|
| 173 | + * This function expects an array with the following keys: |
|
| 174 | + * |
|
| 175 | + * array( |
|
| 176 | + * 'label' => ..., |
|
| 177 | + * 'synonyms' => array( ... ), |
|
| 178 | + * 'annotations' => array( |
|
| 179 | + * ...id... => array( text => ... ), |
|
| 180 | + * ), |
|
| 181 | + * 'occurrences' => array( ... ), |
|
| 182 | + * ) |
|
| 183 | + * |
|
| 184 | + * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
| 185 | + * |
|
| 186 | + * array( |
|
| 187 | + * ...label..., |
|
| 188 | + * ...synonyms..., |
|
| 189 | + * ...texts..., |
|
| 190 | + * ) |
|
| 191 | + * |
|
| 192 | + * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
| 193 | + * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
| 194 | + * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
| 195 | + * `occurrences`. |
|
| 196 | + * |
|
| 197 | + * @param array $entity { |
|
| 198 | + * The entity data. |
|
| 199 | + * |
|
| 200 | + * @type string $label The entity label. |
|
| 201 | + * @type array $synonyms The entity synonyms. |
|
| 202 | + * @type array $occurrences The selected occurrences. |
|
| 203 | + * @type array $annotations The annotations. |
|
| 204 | + * } |
|
| 205 | + * |
|
| 206 | + * @return array An array of labels. |
|
| 207 | + */ |
|
| 208 | + public function get_labels( $entity ) { |
|
| 209 | + |
|
| 210 | + $args = wp_parse_args( $entity, array( |
|
| 211 | + 'label' => array(), |
|
| 212 | + 'synonyms' => array(), |
|
| 213 | + 'annotations' => array(), |
|
| 214 | + 'occurrences' => array(), |
|
| 215 | + ) ); |
|
| 216 | + |
|
| 217 | + // We gather all the labels, occurrences texts and synonyms into one array. |
|
| 218 | + $initial = array_merge( |
|
| 219 | + (array) $args['label'], |
|
| 220 | + (array) $args['synonyms'] |
|
| 221 | + ); |
|
| 222 | + |
|
| 223 | + $annotations = $args['annotations']; |
|
| 224 | + |
|
| 225 | + return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 226 | + |
|
| 227 | + // Bail out if occurrences->$item->text isn't set or its contents are already |
|
| 228 | + // in `$carry`. |
|
| 229 | + if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 230 | + || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 231 | + return $carry; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Push the label. |
|
| 235 | + $carry[] = $annotations[ $item ]['text']; |
|
| 236 | + |
|
| 237 | + return $carry; |
|
| 238 | + }, $initial ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Create or update the entity. |
|
| 243 | + * |
|
| 244 | + * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
| 245 | + * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
| 246 | + * |
|
| 247 | + * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
| 248 | + * |
|
| 249 | + * @param array $entity { |
|
| 250 | + * The entity parameters. |
|
| 251 | + * |
|
| 252 | + * @type string The entity item id URI. |
|
| 253 | + * @type string|array The entity sameAs URI(s). |
|
| 254 | + * @type string $description The entity description. |
|
| 255 | + * } |
|
| 256 | + * |
|
| 257 | + * @param $string $post_status The post status, default 'draft'. |
|
| 258 | + * |
|
| 259 | + * @return int|\WP_Error |
|
| 260 | + * @throws \Exception |
|
| 261 | + */ |
|
| 262 | + private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 263 | + |
|
| 264 | + // Get only valid IDs. |
|
| 265 | + $uris = $this->get_entity_uris( $entity ); |
|
| 266 | + |
|
| 267 | + $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 268 | + |
|
| 269 | + $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 270 | + . var_export( $uris, true ) ); |
|
| 271 | + |
|
| 272 | + // Get the labels. |
|
| 273 | + $labels = $this->get_labels( $entity ); |
|
| 274 | + |
|
| 275 | + if ( empty( $post ) ) { |
|
| 276 | + // Create the entity if it doesn't exist. |
|
| 277 | + $post_id = Entity_Store::get_instance()->create( array( |
|
| 278 | + 'labels' => $labels, |
|
| 279 | + 'description' => $entity['description'], |
|
| 280 | + 'same_as' => $uris, |
|
| 281 | + ), $post_status ); |
|
| 282 | + |
|
| 283 | + // Return the WP_Error if we got one. |
|
| 284 | + if ( is_wp_error( $post_id ) ) { |
|
| 285 | + return $post_id; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + // Add the entity type. |
|
| 289 | + if ( isset( $entity['mainType'] ) ) { |
|
| 290 | + wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 294 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 295 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 296 | + } |
|
| 297 | + } else { |
|
| 298 | + // Update the entity otherwise. |
|
| 299 | + $post_id = Entity_Store::get_instance()->update( array( |
|
| 300 | + 'ID' => $post->ID, |
|
| 301 | + 'labels' => $labels, |
|
| 302 | + 'same_as' => $uris, |
|
| 303 | + ) ); |
|
| 304 | + |
|
| 305 | + // Add the entity type. |
|
| 306 | + if ( isset( $entity['mainType'] ) ) { |
|
| 307 | + wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
|
| 311 | + // Set the post status, we need to set that in order to support entities |
|
| 312 | + // created using rest endpoint on block editor, so that they get published |
|
| 313 | + // when the post is published. |
|
| 314 | + // Once the entity is published dont update the post status. |
|
| 315 | + if ( $post->post_status !== 'publish' ) { |
|
| 316 | + wp_update_post( array( |
|
| 317 | + 'ID' => $post->ID, |
|
| 318 | + 'post_status' => $post_status |
|
| 319 | + ) ); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + return $post_id; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Get the first matching entity for the provided URI array. |
|
| 328 | + * |
|
| 329 | + * Entities IDs and sameAs are searched. |
|
| 330 | + * |
|
| 331 | + * @param array $uris An array of URIs. |
|
| 332 | + * |
|
| 333 | + * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
| 334 | + */ |
|
| 335 | + private function get_first_matching_entity_by_uri( $uris ) { |
|
| 336 | + |
|
| 337 | + foreach ( $uris as $uri ) { |
|
| 338 | + $existing_entity = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri )->get_bag(); |
|
| 339 | + if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
| 340 | + return $existing_entity; |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + return null; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * @param $entity |
|
| 349 | + * |
|
| 350 | + * @return array |
|
| 351 | + */ |
|
| 352 | + private function filter_valid_entity_ids( $entity ) { |
|
| 353 | + $id = $entity['id']; |
|
| 354 | + |
|
| 355 | + return array_filter( (array) $id, function ( $id ) { |
|
| 356 | + return preg_match( '|^https?://|', $id ); |
|
| 357 | + } ); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @param array $entity |
|
| 362 | + * |
|
| 363 | + * @return array |
|
| 364 | + */ |
|
| 365 | + private function get_entity_uris( $entity ) { |
|
| 366 | + $ids = $this->filter_valid_entity_ids( $entity ); |
|
| 367 | + |
|
| 368 | + return array_merge( |
|
| 369 | + (array) $ids, |
|
| 370 | + (array) $entity['sameAs'] |
|
| 371 | + ); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + private function entity_id_valid( $entity_id ) { |
|
| 375 | + return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | 378 | } |
@@ -31,14 +31,14 @@ discard block |
||
| 31 | 31 | public function __construct() { |
| 32 | 32 | |
| 33 | 33 | // Bail out if block editor's functions aren't available. |
| 34 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 34 | + if ( ! function_exists('register_block_type') || ! function_exists('parse_blocks')) { |
|
| 35 | 35 | return; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 38 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 39 | 39 | |
| 40 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 41 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 40 | + add_action('init', array($this, 'init')); |
|
| 41 | + add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10, 2); |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | |
@@ -48,12 +48,12 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | public function init() { |
| 50 | 50 | |
| 51 | - register_block_type( 'wordlift/classification', array( |
|
| 51 | + register_block_type('wordlift/classification', array( |
|
| 52 | 52 | 'editor_script' => 'wl-block-editor', |
| 53 | 53 | 'attributes' => array( |
| 54 | - 'entities' => array( 'type' => 'array' ), |
|
| 54 | + 'entities' => array('type' => 'array'), |
|
| 55 | 55 | ), |
| 56 | - ) ); |
|
| 56 | + )); |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |
@@ -87,45 +87,45 @@ discard block |
||
| 87 | 87 | * |
| 88 | 88 | * @return array The data array. |
| 89 | 89 | */ |
| 90 | - public function wp_insert_post_data( $data, $postarr ) { |
|
| 90 | + public function wp_insert_post_data($data, $postarr) { |
|
| 91 | 91 | |
| 92 | 92 | $post_status = $data['post_status']; |
| 93 | - if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
| 93 | + if ('auto-draft' === $post_status || 'inherit' === $post_status) { |
|
| 94 | 94 | return $data; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 98 | - . var_export( $data, true ) ); |
|
| 97 | + $this->log->trace("The following data has been received by `wp_insert_post_data`:\n" |
|
| 98 | + . var_export($data, true)); |
|
| 99 | 99 | |
| 100 | 100 | |
| 101 | 101 | try { |
| 102 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 102 | + $entities = $this->parse_content(wp_unslash($data['post_content'])); |
|
| 103 | 103 | |
| 104 | - foreach ( $entities as $entity ) { |
|
| 104 | + foreach ($entities as $entity) { |
|
| 105 | 105 | |
| 106 | - $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
| 106 | + $entity_id = array_key_exists('id', $entity) ? $entity['id'] : ''; |
|
| 107 | 107 | |
| 108 | - if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
| 108 | + if ( ! $this->entity_id_valid($entity_id)) { |
|
| 109 | 109 | continue; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - $entity_uris = $this->get_entity_uris( $entity ); |
|
| 112 | + $entity_uris = $this->get_entity_uris($entity); |
|
| 113 | 113 | |
| 114 | - if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
| 115 | - Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
| 114 | + if ($this->get_first_matching_entity_by_uri($entity_uris) === null && |
|
| 115 | + Post_Entities_Validator::is_local_entity_uri_exist(Wordlift_Entity_Uri_Service::get_instance(), $entity_uris)) { |
|
| 116 | 116 | // Skip the entity |
| 117 | 117 | continue; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // If 'entity auto publish' is false, we set the status to `draft` by default. |
| 121 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
| 121 | + $post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) |
|
| 122 | 122 | ? $data['post_status'] : 'draft'; |
| 123 | - $this->create_or_update_entity( $entity, $post_status ); |
|
| 123 | + $this->create_or_update_entity($entity, $post_status); |
|
| 124 | 124 | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - } catch ( \Exception $e ) { |
|
| 128 | - $this->log->error( $e->getMessage() ); |
|
| 127 | + } catch (\Exception $e) { |
|
| 128 | + $this->log->error($e->getMessage()); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | |
@@ -140,27 +140,27 @@ discard block |
||
| 140 | 140 | * @return array An array of entities' structures. |
| 141 | 141 | * @throws \Exception |
| 142 | 142 | */ |
| 143 | - private function parse_content( $post_content ) { |
|
| 143 | + private function parse_content($post_content) { |
|
| 144 | 144 | |
| 145 | - $all_blocks = parse_blocks( $post_content ); |
|
| 146 | - $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 147 | - . var_export( $all_blocks, true ) ); |
|
| 145 | + $all_blocks = parse_blocks($post_content); |
|
| 146 | + $this->log->trace("The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 147 | + . var_export($all_blocks, true)); |
|
| 148 | 148 | |
| 149 | - $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 150 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 149 | + $blocks = array_filter($all_blocks, function($item) { |
|
| 150 | + return ! empty($item['blockName']) && 'wordlift/classification' === $item['blockName']; |
|
| 151 | 151 | } ); |
| 152 | 152 | |
| 153 | 153 | // Bail out if the blocks' array is empty. |
| 154 | - if ( empty( $blocks ) ) { |
|
| 154 | + if (empty($blocks)) { |
|
| 155 | 155 | return array(); |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - $block = current( $blocks ); |
|
| 159 | - $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 160 | - . var_export( $block, true ) ); |
|
| 158 | + $block = current($blocks); |
|
| 159 | + $this->log->trace("The following block has been found while in `wp_insert_post`:\n" |
|
| 160 | + . var_export($block, true)); |
|
| 161 | 161 | |
| 162 | 162 | // Bail out if the entities array is empty. |
| 163 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 163 | + if (empty($block['attrs']) && empty($block['attrs']['entities'])) { |
|
| 164 | 164 | return array(); |
| 165 | 165 | } |
| 166 | 166 | |
@@ -205,14 +205,14 @@ discard block |
||
| 205 | 205 | * |
| 206 | 206 | * @return array An array of labels. |
| 207 | 207 | */ |
| 208 | - public function get_labels( $entity ) { |
|
| 208 | + public function get_labels($entity) { |
|
| 209 | 209 | |
| 210 | - $args = wp_parse_args( $entity, array( |
|
| 210 | + $args = wp_parse_args($entity, array( |
|
| 211 | 211 | 'label' => array(), |
| 212 | 212 | 'synonyms' => array(), |
| 213 | 213 | 'annotations' => array(), |
| 214 | 214 | 'occurrences' => array(), |
| 215 | - ) ); |
|
| 215 | + )); |
|
| 216 | 216 | |
| 217 | 217 | // We gather all the labels, occurrences texts and synonyms into one array. |
| 218 | 218 | $initial = array_merge( |
@@ -222,20 +222,20 @@ discard block |
||
| 222 | 222 | |
| 223 | 223 | $annotations = $args['annotations']; |
| 224 | 224 | |
| 225 | - return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 225 | + return array_reduce($args['occurrences'], function($carry, $item) use ($annotations) { |
|
| 226 | 226 | |
| 227 | 227 | // Bail out if occurrences->$item->text isn't set or its contents are already |
| 228 | 228 | // in `$carry`. |
| 229 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 230 | - || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 229 | + if ( ! isset($annotations[$item]['text']) |
|
| 230 | + || in_array($annotations[$item]['text'], $carry)) { |
|
| 231 | 231 | return $carry; |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | // Push the label. |
| 235 | - $carry[] = $annotations[ $item ]['text']; |
|
| 235 | + $carry[] = $annotations[$item]['text']; |
|
| 236 | 236 | |
| 237 | 237 | return $carry; |
| 238 | - }, $initial ); |
|
| 238 | + }, $initial); |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | /** |
@@ -259,52 +259,52 @@ discard block |
||
| 259 | 259 | * @return int|\WP_Error |
| 260 | 260 | * @throws \Exception |
| 261 | 261 | */ |
| 262 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 262 | + private function create_or_update_entity($entity, $post_status = 'draft') { |
|
| 263 | 263 | |
| 264 | 264 | // Get only valid IDs. |
| 265 | - $uris = $this->get_entity_uris( $entity ); |
|
| 265 | + $uris = $this->get_entity_uris($entity); |
|
| 266 | 266 | |
| 267 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 267 | + $post = $this->get_first_matching_entity_by_uri($uris); |
|
| 268 | 268 | |
| 269 | - $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 270 | - . var_export( $uris, true ) ); |
|
| 269 | + $this->log->trace('Entity'.(empty($post) ? ' not' : '')." found with the following URIs:\n" |
|
| 270 | + . var_export($uris, true)); |
|
| 271 | 271 | |
| 272 | 272 | // Get the labels. |
| 273 | - $labels = $this->get_labels( $entity ); |
|
| 273 | + $labels = $this->get_labels($entity); |
|
| 274 | 274 | |
| 275 | - if ( empty( $post ) ) { |
|
| 275 | + if (empty($post)) { |
|
| 276 | 276 | // Create the entity if it doesn't exist. |
| 277 | - $post_id = Entity_Store::get_instance()->create( array( |
|
| 277 | + $post_id = Entity_Store::get_instance()->create(array( |
|
| 278 | 278 | 'labels' => $labels, |
| 279 | 279 | 'description' => $entity['description'], |
| 280 | 280 | 'same_as' => $uris, |
| 281 | - ), $post_status ); |
|
| 281 | + ), $post_status); |
|
| 282 | 282 | |
| 283 | 283 | // Return the WP_Error if we got one. |
| 284 | - if ( is_wp_error( $post_id ) ) { |
|
| 284 | + if (is_wp_error($post_id)) { |
|
| 285 | 285 | return $post_id; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | // Add the entity type. |
| 289 | - if ( isset( $entity['mainType'] ) ) { |
|
| 290 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 289 | + if (isset($entity['mainType'])) { |
|
| 290 | + wp_set_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 294 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 295 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 293 | + if (isset($entity['properties']) && isset($entity['properties']['latitude']) && isset($entity['properties']['longitude'])) { |
|
| 294 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude']); |
|
| 295 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude']); |
|
| 296 | 296 | } |
| 297 | 297 | } else { |
| 298 | 298 | // Update the entity otherwise. |
| 299 | - $post_id = Entity_Store::get_instance()->update( array( |
|
| 299 | + $post_id = Entity_Store::get_instance()->update(array( |
|
| 300 | 300 | 'ID' => $post->ID, |
| 301 | 301 | 'labels' => $labels, |
| 302 | 302 | 'same_as' => $uris, |
| 303 | - ) ); |
|
| 303 | + )); |
|
| 304 | 304 | |
| 305 | 305 | // Add the entity type. |
| 306 | - if ( isset( $entity['mainType'] ) ) { |
|
| 307 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 306 | + if (isset($entity['mainType'])) { |
|
| 307 | + wp_add_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
@@ -312,11 +312,11 @@ discard block |
||
| 312 | 312 | // created using rest endpoint on block editor, so that they get published |
| 313 | 313 | // when the post is published. |
| 314 | 314 | // Once the entity is published dont update the post status. |
| 315 | - if ( $post->post_status !== 'publish' ) { |
|
| 316 | - wp_update_post( array( |
|
| 315 | + if ($post->post_status !== 'publish') { |
|
| 316 | + wp_update_post(array( |
|
| 317 | 317 | 'ID' => $post->ID, |
| 318 | 318 | 'post_status' => $post_status |
| 319 | - ) ); |
|
| 319 | + )); |
|
| 320 | 320 | } |
| 321 | 321 | } |
| 322 | 322 | |
@@ -332,11 +332,11 @@ discard block |
||
| 332 | 332 | * |
| 333 | 333 | * @return \WP_Post|null The entity WP_Post if found or null if not found. |
| 334 | 334 | */ |
| 335 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
| 335 | + private function get_first_matching_entity_by_uri($uris) { |
|
| 336 | 336 | |
| 337 | - foreach ( $uris as $uri ) { |
|
| 338 | - $existing_entity = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri )->get_bag(); |
|
| 339 | - if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
| 337 | + foreach ($uris as $uri) { |
|
| 338 | + $existing_entity = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri)->get_bag(); |
|
| 339 | + if (is_a($existing_entity, 'WP_Post')) { |
|
| 340 | 340 | return $existing_entity; |
| 341 | 341 | } |
| 342 | 342 | } |
@@ -349,11 +349,11 @@ discard block |
||
| 349 | 349 | * |
| 350 | 350 | * @return array |
| 351 | 351 | */ |
| 352 | - private function filter_valid_entity_ids( $entity ) { |
|
| 352 | + private function filter_valid_entity_ids($entity) { |
|
| 353 | 353 | $id = $entity['id']; |
| 354 | 354 | |
| 355 | - return array_filter( (array) $id, function ( $id ) { |
|
| 356 | - return preg_match( '|^https?://|', $id ); |
|
| 355 | + return array_filter((array) $id, function($id) { |
|
| 356 | + return preg_match('|^https?://|', $id); |
|
| 357 | 357 | } ); |
| 358 | 358 | } |
| 359 | 359 | |
@@ -362,8 +362,8 @@ discard block |
||
| 362 | 362 | * |
| 363 | 363 | * @return array |
| 364 | 364 | */ |
| 365 | - private function get_entity_uris( $entity ) { |
|
| 366 | - $ids = $this->filter_valid_entity_ids( $entity ); |
|
| 365 | + private function get_entity_uris($entity) { |
|
| 366 | + $ids = $this->filter_valid_entity_ids($entity); |
|
| 367 | 367 | |
| 368 | 368 | return array_merge( |
| 369 | 369 | (array) $ids, |
@@ -371,8 +371,8 @@ discard block |
||
| 371 | 371 | ); |
| 372 | 372 | } |
| 373 | 373 | |
| 374 | - private function entity_id_valid( $entity_id ) { |
|
| 375 | - return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
| 374 | + private function entity_id_valid($entity_id) { |
|
| 375 | + return preg_match('#^https?://#i', $entity_id) === 1; |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | } |