@@ -18,320 +18,320 @@ |
||
| 18 | 18 | |
| 19 | 19 | class Post_Adapter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A {@link Wordlift_Log_Service} logging instance. |
|
| 23 | - * |
|
| 24 | - * @access private |
|
| 25 | - * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
| 26 | - */ |
|
| 27 | - private $log; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 31 | - * |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Entity_Store} instance. |
|
| 39 | - * |
|
| 40 | - * @access private |
|
| 41 | - * @var Entity_Store $entity_store A {@link Entity_Store} instance. |
|
| 42 | - */ |
|
| 43 | - private $entity_store; |
|
| 44 | - |
|
| 45 | - public function __construct() { |
|
| 46 | - |
|
| 47 | - // Bail out if block editor's functions aren't available. |
|
| 48 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 49 | - return; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 53 | - |
|
| 54 | - $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 55 | - $this->entity_store = Entity_Store::get_instance(); |
|
| 56 | - |
|
| 57 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
| 64 | - * correctly. |
|
| 65 | - */ |
|
| 66 | - public function init() { |
|
| 67 | - |
|
| 68 | - register_block_type( 'wordlift/classification', array( |
|
| 69 | - 'editor_script' => 'wl-block-editor', |
|
| 70 | - 'attributes' => array( |
|
| 71 | - 'entities' => array( 'type' => 'array' ), |
|
| 72 | - ), |
|
| 73 | - ) ); |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * A sample structure: |
|
| 79 | - * |
|
| 80 | - * { |
|
| 81 | - * "entities": [ |
|
| 82 | - * { |
|
| 83 | - * "annotations": { |
|
| 84 | - * "urn:enhancement-7e8e66fc": { |
|
| 85 | - * "start": 3480, |
|
| 86 | - * "end": 3486, |
|
| 87 | - * "text": "libero" |
|
| 88 | - * } |
|
| 89 | - * }, |
|
| 90 | - * "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.", |
|
| 91 | - * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
| 92 | - * "label": "Libero (volley-ball)", |
|
| 93 | - * "mainType": "other", |
|
| 94 | - * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
| 95 | - * "sameAs": null, |
|
| 96 | - * "synonyms": [], |
|
| 97 | - * "types": ["other"] |
|
| 98 | - * } |
|
| 99 | - * ] |
|
| 100 | - * } |
|
| 101 | - * |
|
| 102 | - * @param array $data An array of slashed post data. |
|
| 103 | - * @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|
| 104 | - * |
|
| 105 | - * @return array The data array. |
|
| 106 | - * @throws \Exception |
|
| 107 | - */ |
|
| 108 | - public function wp_insert_post_data( $data, $postarr ) { |
|
| 109 | - |
|
| 110 | - $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | - . var_export( $data, true ) ); |
|
| 112 | - |
|
| 113 | - try { |
|
| 114 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 115 | - |
|
| 116 | - foreach ( $entities as $entity ) { |
|
| 117 | - $this->create_or_update_entity( $entity, $data['post_status'] ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - } catch ( \Exception $e ) { |
|
| 121 | - $this->log->error( $e->getMessage() ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $data; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
| 129 | - * |
|
| 130 | - * @param string $post_content The post content. |
|
| 131 | - * |
|
| 132 | - * @return array An array of entities' structures. |
|
| 133 | - * @throws \Exception |
|
| 134 | - */ |
|
| 135 | - private function parse_content( $post_content ) { |
|
| 136 | - |
|
| 137 | - $all_blocks = parse_blocks( $post_content ); |
|
| 138 | - $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | - . var_export( $all_blocks, true ) ); |
|
| 140 | - |
|
| 141 | - $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 142 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 143 | - } ); |
|
| 144 | - |
|
| 145 | - // Bail out if the blocks' array is empty. |
|
| 146 | - if ( empty( $blocks ) ) { |
|
| 147 | - return array(); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $block = current( $blocks ); |
|
| 151 | - $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | - . var_export( $block, true ) ); |
|
| 153 | - |
|
| 154 | - // Bail out if the entities array is empty. |
|
| 155 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 156 | - return array(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $block['attrs']['entities']; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Collect entity labels from the entity array. |
|
| 164 | - * |
|
| 165 | - * This function expects an array with the following keys: |
|
| 166 | - * |
|
| 167 | - * array( |
|
| 168 | - * 'label' => ..., |
|
| 169 | - * 'synonyms' => array( ... ), |
|
| 170 | - * 'annotations' => array( |
|
| 171 | - * ...id... => array( text => ... ), |
|
| 172 | - * ), |
|
| 173 | - * 'occurrences' => array( ... ), |
|
| 174 | - * ) |
|
| 175 | - * |
|
| 176 | - * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
| 177 | - * |
|
| 178 | - * array( |
|
| 179 | - * ...label..., |
|
| 180 | - * ...synonyms..., |
|
| 181 | - * ...texts..., |
|
| 182 | - * ) |
|
| 183 | - * |
|
| 184 | - * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
| 185 | - * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
| 186 | - * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
| 187 | - * `occurrences`. |
|
| 188 | - * |
|
| 189 | - * @param array $entity { |
|
| 190 | - * The entity data. |
|
| 191 | - * |
|
| 192 | - * @type string $label The entity label. |
|
| 193 | - * @type array $synonyms The entity synonyms. |
|
| 194 | - * @type array $occurrences The selected occurrences. |
|
| 195 | - * @type array $annotations The annotations. |
|
| 196 | - * } |
|
| 197 | - * |
|
| 198 | - * @return array An array of labels. |
|
| 199 | - */ |
|
| 200 | - public function get_labels( $entity ) { |
|
| 201 | - |
|
| 202 | - $args = wp_parse_args( $entity, array( |
|
| 203 | - 'label' => array(), |
|
| 204 | - 'synonyms' => array(), |
|
| 205 | - 'annotations' => array(), |
|
| 206 | - 'occurrences' => array(), |
|
| 207 | - ) ); |
|
| 208 | - |
|
| 209 | - // We gather all the labels, occurrences texts and synonyms into one array. |
|
| 210 | - $initial = array_merge( |
|
| 211 | - (array) $args['label'], |
|
| 212 | - (array) $args['synonyms'] |
|
| 213 | - ); |
|
| 214 | - |
|
| 215 | - $annotations = $args['annotations']; |
|
| 216 | - |
|
| 217 | - return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 218 | - |
|
| 219 | - // Bail out if occurrences->$item->text isn't set or its contents are already |
|
| 220 | - // in `$carry`. |
|
| 221 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 222 | - || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 223 | - return $carry; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Push the label. |
|
| 227 | - $carry[] = $annotations[ $item ]['text']; |
|
| 228 | - |
|
| 229 | - return $carry; |
|
| 230 | - }, $initial ); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Create or update the entity. |
|
| 235 | - * |
|
| 236 | - * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
| 237 | - * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
| 238 | - * |
|
| 239 | - * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
| 240 | - * |
|
| 241 | - * @param array $entity { |
|
| 242 | - * The entity parameters. |
|
| 243 | - * |
|
| 244 | - * @type string The entity item id URI. |
|
| 245 | - * @type string|array The entity sameAs URI(s). |
|
| 246 | - * @type string $description The entity description. |
|
| 247 | - * } |
|
| 248 | - * |
|
| 249 | - * @param $string $post_status The post status, default 'draft'. |
|
| 250 | - * |
|
| 251 | - * @return int|\WP_Error |
|
| 252 | - * @throws \Exception |
|
| 253 | - */ |
|
| 254 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 255 | - |
|
| 256 | - // Get only valid IDs. |
|
| 257 | - $ids = array_filter( (array) $entity['id'], function ( $id ) { |
|
| 258 | - return preg_match( '|^https?://|', $id ); |
|
| 259 | - } ); |
|
| 260 | - |
|
| 261 | - $uris = array_merge( |
|
| 262 | - (array) $ids, |
|
| 263 | - (array) $entity['sameAs'] |
|
| 264 | - ); |
|
| 265 | - |
|
| 266 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 267 | - |
|
| 268 | - $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 269 | - . var_export( $uris, true ) ); |
|
| 270 | - |
|
| 271 | - // Get the labels. |
|
| 272 | - $labels = $this->get_labels( $entity ); |
|
| 273 | - |
|
| 274 | - if ( empty( $post ) ) { |
|
| 275 | - // Create the entity if it doesn't exist. |
|
| 276 | - $post_id = $this->entity_store->create( array( |
|
| 277 | - 'labels' => $labels, |
|
| 278 | - 'description' => $entity['description'], |
|
| 279 | - 'same_as' => $uris, |
|
| 280 | - ), $post_status ); |
|
| 281 | - |
|
| 282 | - // Return the WP_Error if we got one. |
|
| 283 | - if ( is_wp_error( $post_id ) ) { |
|
| 284 | - return $post_id; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - // Add the entity type. |
|
| 288 | - if ( isset( $entity['mainType'] ) ) { |
|
| 289 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 293 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 294 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 295 | - } |
|
| 296 | - } else { |
|
| 297 | - // Update the entity otherwise. |
|
| 298 | - $post_id = $this->entity_store->update( array( |
|
| 299 | - 'ID' => $post->ID, |
|
| 300 | - 'labels' => $labels, |
|
| 301 | - 'same_as' => $uris, |
|
| 302 | - ) ); |
|
| 303 | - |
|
| 304 | - // Add the entity type. |
|
| 305 | - if ( isset( $entity['mainType'] ) ) { |
|
| 306 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return $post_id; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get the first matching entity for the provided URI array. |
|
| 315 | - * |
|
| 316 | - * Entities IDs and sameAs are searched. |
|
| 317 | - * |
|
| 318 | - * @param array $uris An array of URIs. |
|
| 319 | - * |
|
| 320 | - * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
| 321 | - */ |
|
| 322 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
| 323 | - |
|
| 324 | - foreach ( $uris as $uri ) { |
|
| 325 | - $existing_entity = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 326 | - if ( isset( $existing_entity ) ) { |
|
| 327 | - return $existing_entity; |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - return null; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - // /** |
|
| 21 | + /** |
|
| 22 | + * A {@link Wordlift_Log_Service} logging instance. |
|
| 23 | + * |
|
| 24 | + * @access private |
|
| 25 | + * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
| 26 | + */ |
|
| 27 | + private $log; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 31 | + * |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Entity_Store} instance. |
|
| 39 | + * |
|
| 40 | + * @access private |
|
| 41 | + * @var Entity_Store $entity_store A {@link Entity_Store} instance. |
|
| 42 | + */ |
|
| 43 | + private $entity_store; |
|
| 44 | + |
|
| 45 | + public function __construct() { |
|
| 46 | + |
|
| 47 | + // Bail out if block editor's functions aren't available. |
|
| 48 | + if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 49 | + return; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 53 | + |
|
| 54 | + $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 55 | + $this->entity_store = Entity_Store::get_instance(); |
|
| 56 | + |
|
| 57 | + add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | + add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
| 64 | + * correctly. |
|
| 65 | + */ |
|
| 66 | + public function init() { |
|
| 67 | + |
|
| 68 | + register_block_type( 'wordlift/classification', array( |
|
| 69 | + 'editor_script' => 'wl-block-editor', |
|
| 70 | + 'attributes' => array( |
|
| 71 | + 'entities' => array( 'type' => 'array' ), |
|
| 72 | + ), |
|
| 73 | + ) ); |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * A sample structure: |
|
| 79 | + * |
|
| 80 | + * { |
|
| 81 | + * "entities": [ |
|
| 82 | + * { |
|
| 83 | + * "annotations": { |
|
| 84 | + * "urn:enhancement-7e8e66fc": { |
|
| 85 | + * "start": 3480, |
|
| 86 | + * "end": 3486, |
|
| 87 | + * "text": "libero" |
|
| 88 | + * } |
|
| 89 | + * }, |
|
| 90 | + * "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.", |
|
| 91 | + * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
| 92 | + * "label": "Libero (volley-ball)", |
|
| 93 | + * "mainType": "other", |
|
| 94 | + * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
| 95 | + * "sameAs": null, |
|
| 96 | + * "synonyms": [], |
|
| 97 | + * "types": ["other"] |
|
| 98 | + * } |
|
| 99 | + * ] |
|
| 100 | + * } |
|
| 101 | + * |
|
| 102 | + * @param array $data An array of slashed post data. |
|
| 103 | + * @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|
| 104 | + * |
|
| 105 | + * @return array The data array. |
|
| 106 | + * @throws \Exception |
|
| 107 | + */ |
|
| 108 | + public function wp_insert_post_data( $data, $postarr ) { |
|
| 109 | + |
|
| 110 | + $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | + . var_export( $data, true ) ); |
|
| 112 | + |
|
| 113 | + try { |
|
| 114 | + $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 115 | + |
|
| 116 | + foreach ( $entities as $entity ) { |
|
| 117 | + $this->create_or_update_entity( $entity, $data['post_status'] ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + } catch ( \Exception $e ) { |
|
| 121 | + $this->log->error( $e->getMessage() ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $data; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
| 129 | + * |
|
| 130 | + * @param string $post_content The post content. |
|
| 131 | + * |
|
| 132 | + * @return array An array of entities' structures. |
|
| 133 | + * @throws \Exception |
|
| 134 | + */ |
|
| 135 | + private function parse_content( $post_content ) { |
|
| 136 | + |
|
| 137 | + $all_blocks = parse_blocks( $post_content ); |
|
| 138 | + $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | + . var_export( $all_blocks, true ) ); |
|
| 140 | + |
|
| 141 | + $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 142 | + return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 143 | + } ); |
|
| 144 | + |
|
| 145 | + // Bail out if the blocks' array is empty. |
|
| 146 | + if ( empty( $blocks ) ) { |
|
| 147 | + return array(); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $block = current( $blocks ); |
|
| 151 | + $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | + . var_export( $block, true ) ); |
|
| 153 | + |
|
| 154 | + // Bail out if the entities array is empty. |
|
| 155 | + if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 156 | + return array(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $block['attrs']['entities']; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Collect entity labels from the entity array. |
|
| 164 | + * |
|
| 165 | + * This function expects an array with the following keys: |
|
| 166 | + * |
|
| 167 | + * array( |
|
| 168 | + * 'label' => ..., |
|
| 169 | + * 'synonyms' => array( ... ), |
|
| 170 | + * 'annotations' => array( |
|
| 171 | + * ...id... => array( text => ... ), |
|
| 172 | + * ), |
|
| 173 | + * 'occurrences' => array( ... ), |
|
| 174 | + * ) |
|
| 175 | + * |
|
| 176 | + * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
| 177 | + * |
|
| 178 | + * array( |
|
| 179 | + * ...label..., |
|
| 180 | + * ...synonyms..., |
|
| 181 | + * ...texts..., |
|
| 182 | + * ) |
|
| 183 | + * |
|
| 184 | + * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
| 185 | + * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
| 186 | + * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
| 187 | + * `occurrences`. |
|
| 188 | + * |
|
| 189 | + * @param array $entity { |
|
| 190 | + * The entity data. |
|
| 191 | + * |
|
| 192 | + * @type string $label The entity label. |
|
| 193 | + * @type array $synonyms The entity synonyms. |
|
| 194 | + * @type array $occurrences The selected occurrences. |
|
| 195 | + * @type array $annotations The annotations. |
|
| 196 | + * } |
|
| 197 | + * |
|
| 198 | + * @return array An array of labels. |
|
| 199 | + */ |
|
| 200 | + public function get_labels( $entity ) { |
|
| 201 | + |
|
| 202 | + $args = wp_parse_args( $entity, array( |
|
| 203 | + 'label' => array(), |
|
| 204 | + 'synonyms' => array(), |
|
| 205 | + 'annotations' => array(), |
|
| 206 | + 'occurrences' => array(), |
|
| 207 | + ) ); |
|
| 208 | + |
|
| 209 | + // We gather all the labels, occurrences texts and synonyms into one array. |
|
| 210 | + $initial = array_merge( |
|
| 211 | + (array) $args['label'], |
|
| 212 | + (array) $args['synonyms'] |
|
| 213 | + ); |
|
| 214 | + |
|
| 215 | + $annotations = $args['annotations']; |
|
| 216 | + |
|
| 217 | + return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 218 | + |
|
| 219 | + // Bail out if occurrences->$item->text isn't set or its contents are already |
|
| 220 | + // in `$carry`. |
|
| 221 | + if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 222 | + || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 223 | + return $carry; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Push the label. |
|
| 227 | + $carry[] = $annotations[ $item ]['text']; |
|
| 228 | + |
|
| 229 | + return $carry; |
|
| 230 | + }, $initial ); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Create or update the entity. |
|
| 235 | + * |
|
| 236 | + * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
| 237 | + * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
| 238 | + * |
|
| 239 | + * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
| 240 | + * |
|
| 241 | + * @param array $entity { |
|
| 242 | + * The entity parameters. |
|
| 243 | + * |
|
| 244 | + * @type string The entity item id URI. |
|
| 245 | + * @type string|array The entity sameAs URI(s). |
|
| 246 | + * @type string $description The entity description. |
|
| 247 | + * } |
|
| 248 | + * |
|
| 249 | + * @param $string $post_status The post status, default 'draft'. |
|
| 250 | + * |
|
| 251 | + * @return int|\WP_Error |
|
| 252 | + * @throws \Exception |
|
| 253 | + */ |
|
| 254 | + private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 255 | + |
|
| 256 | + // Get only valid IDs. |
|
| 257 | + $ids = array_filter( (array) $entity['id'], function ( $id ) { |
|
| 258 | + return preg_match( '|^https?://|', $id ); |
|
| 259 | + } ); |
|
| 260 | + |
|
| 261 | + $uris = array_merge( |
|
| 262 | + (array) $ids, |
|
| 263 | + (array) $entity['sameAs'] |
|
| 264 | + ); |
|
| 265 | + |
|
| 266 | + $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 267 | + |
|
| 268 | + $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 269 | + . var_export( $uris, true ) ); |
|
| 270 | + |
|
| 271 | + // Get the labels. |
|
| 272 | + $labels = $this->get_labels( $entity ); |
|
| 273 | + |
|
| 274 | + if ( empty( $post ) ) { |
|
| 275 | + // Create the entity if it doesn't exist. |
|
| 276 | + $post_id = $this->entity_store->create( array( |
|
| 277 | + 'labels' => $labels, |
|
| 278 | + 'description' => $entity['description'], |
|
| 279 | + 'same_as' => $uris, |
|
| 280 | + ), $post_status ); |
|
| 281 | + |
|
| 282 | + // Return the WP_Error if we got one. |
|
| 283 | + if ( is_wp_error( $post_id ) ) { |
|
| 284 | + return $post_id; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + // Add the entity type. |
|
| 288 | + if ( isset( $entity['mainType'] ) ) { |
|
| 289 | + wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 293 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 294 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 295 | + } |
|
| 296 | + } else { |
|
| 297 | + // Update the entity otherwise. |
|
| 298 | + $post_id = $this->entity_store->update( array( |
|
| 299 | + 'ID' => $post->ID, |
|
| 300 | + 'labels' => $labels, |
|
| 301 | + 'same_as' => $uris, |
|
| 302 | + ) ); |
|
| 303 | + |
|
| 304 | + // Add the entity type. |
|
| 305 | + if ( isset( $entity['mainType'] ) ) { |
|
| 306 | + wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return $post_id; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get the first matching entity for the provided URI array. |
|
| 315 | + * |
|
| 316 | + * Entities IDs and sameAs are searched. |
|
| 317 | + * |
|
| 318 | + * @param array $uris An array of URIs. |
|
| 319 | + * |
|
| 320 | + * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
| 321 | + */ |
|
| 322 | + private function get_first_matching_entity_by_uri( $uris ) { |
|
| 323 | + |
|
| 324 | + foreach ( $uris as $uri ) { |
|
| 325 | + $existing_entity = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 326 | + if ( isset( $existing_entity ) ) { |
|
| 327 | + return $existing_entity; |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + return null; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + // /** |
|
| 335 | 335 | // * @param int $post_ID Post ID. |
| 336 | 336 | // * @param \WP_Post $post Post object. |
| 337 | 337 | // * @param bool $update Whether this is an existing post being updated or not. |
@@ -45,17 +45,17 @@ discard block |
||
| 45 | 45 | public function __construct() { |
| 46 | 46 | |
| 47 | 47 | // Bail out if block editor's functions aren't available. |
| 48 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 48 | + if ( ! function_exists('register_block_type') || ! function_exists('parse_blocks')) { |
|
| 49 | 49 | return; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 52 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 53 | 53 | |
| 54 | 54 | $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
| 55 | 55 | $this->entity_store = Entity_Store::get_instance(); |
| 56 | 56 | |
| 57 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 57 | + add_action('init', array($this, 'init')); |
|
| 58 | + add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10, 2); |
|
| 59 | 59 | |
| 60 | 60 | } |
| 61 | 61 | |
@@ -65,12 +65,12 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | public function init() { |
| 67 | 67 | |
| 68 | - register_block_type( 'wordlift/classification', array( |
|
| 68 | + register_block_type('wordlift/classification', array( |
|
| 69 | 69 | 'editor_script' => 'wl-block-editor', |
| 70 | 70 | 'attributes' => array( |
| 71 | - 'entities' => array( 'type' => 'array' ), |
|
| 71 | + 'entities' => array('type' => 'array'), |
|
| 72 | 72 | ), |
| 73 | - ) ); |
|
| 73 | + )); |
|
| 74 | 74 | |
| 75 | 75 | } |
| 76 | 76 | |
@@ -105,20 +105,20 @@ discard block |
||
| 105 | 105 | * @return array The data array. |
| 106 | 106 | * @throws \Exception |
| 107 | 107 | */ |
| 108 | - public function wp_insert_post_data( $data, $postarr ) { |
|
| 108 | + public function wp_insert_post_data($data, $postarr) { |
|
| 109 | 109 | |
| 110 | - $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | - . var_export( $data, true ) ); |
|
| 110 | + $this->log->trace("The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | + . var_export($data, true)); |
|
| 112 | 112 | |
| 113 | 113 | try { |
| 114 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 114 | + $entities = $this->parse_content(wp_unslash($data['post_content'])); |
|
| 115 | 115 | |
| 116 | - foreach ( $entities as $entity ) { |
|
| 117 | - $this->create_or_update_entity( $entity, $data['post_status'] ); |
|
| 116 | + foreach ($entities as $entity) { |
|
| 117 | + $this->create_or_update_entity($entity, $data['post_status']); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - } catch ( \Exception $e ) { |
|
| 121 | - $this->log->error( $e->getMessage() ); |
|
| 120 | + } catch (\Exception $e) { |
|
| 121 | + $this->log->error($e->getMessage()); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | return $data; |
@@ -132,27 +132,27 @@ discard block |
||
| 132 | 132 | * @return array An array of entities' structures. |
| 133 | 133 | * @throws \Exception |
| 134 | 134 | */ |
| 135 | - private function parse_content( $post_content ) { |
|
| 135 | + private function parse_content($post_content) { |
|
| 136 | 136 | |
| 137 | - $all_blocks = parse_blocks( $post_content ); |
|
| 138 | - $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | - . var_export( $all_blocks, true ) ); |
|
| 137 | + $all_blocks = parse_blocks($post_content); |
|
| 138 | + $this->log->trace("The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | + . var_export($all_blocks, true)); |
|
| 140 | 140 | |
| 141 | - $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 142 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 141 | + $blocks = array_filter($all_blocks, function($item) { |
|
| 142 | + return ! empty($item['blockName']) && 'wordlift/classification' === $item['blockName']; |
|
| 143 | 143 | } ); |
| 144 | 144 | |
| 145 | 145 | // Bail out if the blocks' array is empty. |
| 146 | - if ( empty( $blocks ) ) { |
|
| 146 | + if (empty($blocks)) { |
|
| 147 | 147 | return array(); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - $block = current( $blocks ); |
|
| 151 | - $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | - . var_export( $block, true ) ); |
|
| 150 | + $block = current($blocks); |
|
| 151 | + $this->log->trace("The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | + . var_export($block, true)); |
|
| 153 | 153 | |
| 154 | 154 | // Bail out if the entities array is empty. |
| 155 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 155 | + if (empty($block['attrs']) && empty($block['attrs']['entities'])) { |
|
| 156 | 156 | return array(); |
| 157 | 157 | } |
| 158 | 158 | |
@@ -197,14 +197,14 @@ discard block |
||
| 197 | 197 | * |
| 198 | 198 | * @return array An array of labels. |
| 199 | 199 | */ |
| 200 | - public function get_labels( $entity ) { |
|
| 200 | + public function get_labels($entity) { |
|
| 201 | 201 | |
| 202 | - $args = wp_parse_args( $entity, array( |
|
| 202 | + $args = wp_parse_args($entity, array( |
|
| 203 | 203 | 'label' => array(), |
| 204 | 204 | 'synonyms' => array(), |
| 205 | 205 | 'annotations' => array(), |
| 206 | 206 | 'occurrences' => array(), |
| 207 | - ) ); |
|
| 207 | + )); |
|
| 208 | 208 | |
| 209 | 209 | // We gather all the labels, occurrences texts and synonyms into one array. |
| 210 | 210 | $initial = array_merge( |
@@ -214,20 +214,20 @@ discard block |
||
| 214 | 214 | |
| 215 | 215 | $annotations = $args['annotations']; |
| 216 | 216 | |
| 217 | - return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 217 | + return array_reduce($args['occurrences'], function($carry, $item) use ($annotations) { |
|
| 218 | 218 | |
| 219 | 219 | // Bail out if occurrences->$item->text isn't set or its contents are already |
| 220 | 220 | // in `$carry`. |
| 221 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 222 | - || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 221 | + if ( ! isset($annotations[$item]['text']) |
|
| 222 | + || in_array($annotations[$item]['text'], $carry)) { |
|
| 223 | 223 | return $carry; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // Push the label. |
| 227 | - $carry[] = $annotations[ $item ]['text']; |
|
| 227 | + $carry[] = $annotations[$item]['text']; |
|
| 228 | 228 | |
| 229 | 229 | return $carry; |
| 230 | - }, $initial ); |
|
| 230 | + }, $initial); |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -251,11 +251,11 @@ discard block |
||
| 251 | 251 | * @return int|\WP_Error |
| 252 | 252 | * @throws \Exception |
| 253 | 253 | */ |
| 254 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 254 | + private function create_or_update_entity($entity, $post_status = 'draft') { |
|
| 255 | 255 | |
| 256 | 256 | // Get only valid IDs. |
| 257 | - $ids = array_filter( (array) $entity['id'], function ( $id ) { |
|
| 258 | - return preg_match( '|^https?://|', $id ); |
|
| 257 | + $ids = array_filter((array) $entity['id'], function($id) { |
|
| 258 | + return preg_match('|^https?://|', $id); |
|
| 259 | 259 | } ); |
| 260 | 260 | |
| 261 | 261 | $uris = array_merge( |
@@ -263,47 +263,47 @@ discard block |
||
| 263 | 263 | (array) $entity['sameAs'] |
| 264 | 264 | ); |
| 265 | 265 | |
| 266 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 266 | + $post = $this->get_first_matching_entity_by_uri($uris); |
|
| 267 | 267 | |
| 268 | - $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 269 | - . var_export( $uris, true ) ); |
|
| 268 | + $this->log->trace('Entity'.(empty($post) ? ' not' : '')." found with the following URIs:\n" |
|
| 269 | + . var_export($uris, true)); |
|
| 270 | 270 | |
| 271 | 271 | // Get the labels. |
| 272 | - $labels = $this->get_labels( $entity ); |
|
| 272 | + $labels = $this->get_labels($entity); |
|
| 273 | 273 | |
| 274 | - if ( empty( $post ) ) { |
|
| 274 | + if (empty($post)) { |
|
| 275 | 275 | // Create the entity if it doesn't exist. |
| 276 | - $post_id = $this->entity_store->create( array( |
|
| 276 | + $post_id = $this->entity_store->create(array( |
|
| 277 | 277 | 'labels' => $labels, |
| 278 | 278 | 'description' => $entity['description'], |
| 279 | 279 | 'same_as' => $uris, |
| 280 | - ), $post_status ); |
|
| 280 | + ), $post_status); |
|
| 281 | 281 | |
| 282 | 282 | // Return the WP_Error if we got one. |
| 283 | - if ( is_wp_error( $post_id ) ) { |
|
| 283 | + if (is_wp_error($post_id)) { |
|
| 284 | 284 | return $post_id; |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | // Add the entity type. |
| 288 | - if ( isset( $entity['mainType'] ) ) { |
|
| 289 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 288 | + if (isset($entity['mainType'])) { |
|
| 289 | + wp_set_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 293 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 294 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 292 | + if (isset($entity['properties']) && isset($entity['properties']['latitude']) && isset($entity['properties']['longitude'])) { |
|
| 293 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude']); |
|
| 294 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude']); |
|
| 295 | 295 | } |
| 296 | 296 | } else { |
| 297 | 297 | // Update the entity otherwise. |
| 298 | - $post_id = $this->entity_store->update( array( |
|
| 298 | + $post_id = $this->entity_store->update(array( |
|
| 299 | 299 | 'ID' => $post->ID, |
| 300 | 300 | 'labels' => $labels, |
| 301 | 301 | 'same_as' => $uris, |
| 302 | - ) ); |
|
| 302 | + )); |
|
| 303 | 303 | |
| 304 | 304 | // Add the entity type. |
| 305 | - if ( isset( $entity['mainType'] ) ) { |
|
| 306 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 305 | + if (isset($entity['mainType'])) { |
|
| 306 | + wp_add_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
@@ -319,11 +319,11 @@ discard block |
||
| 319 | 319 | * |
| 320 | 320 | * @return \WP_Post|null The entity WP_Post if found or null if not found. |
| 321 | 321 | */ |
| 322 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
| 322 | + private function get_first_matching_entity_by_uri($uris) { |
|
| 323 | 323 | |
| 324 | - foreach ( $uris as $uri ) { |
|
| 325 | - $existing_entity = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 326 | - if ( isset( $existing_entity ) ) { |
|
| 324 | + foreach ($uris as $uri) { |
|
| 325 | + $existing_entity = $this->entity_service->get_entity_post_by_uri($uri); |
|
| 326 | + if (isset($existing_entity)) { |
|
| 327 | 327 | return $existing_entity; |
| 328 | 328 | } |
| 329 | 329 | } |