@@ -16,45 +16,45 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | function wl_linked_data_save_post( $post_id ) { |
| 18 | 18 | |
| 19 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 19 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 20 | 20 | |
| 21 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 21 | + $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 22 | 22 | |
| 23 | - // If it's not numeric exit from here. |
|
| 24 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 25 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 23 | + // If it's not numeric exit from here. |
|
| 24 | + if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 25 | + $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 26 | 26 | |
| 27 | - return; |
|
| 28 | - } |
|
| 27 | + return; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - // Get the post type and check whether it supports the editor. |
|
| 31 | - // |
|
| 32 | - // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 33 | - $post_type = get_post_type( $post_id ); |
|
| 34 | - /** |
|
| 35 | - * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 36 | - * |
|
| 37 | - * @since 3.19.4 |
|
| 38 | - * |
|
| 39 | - * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 40 | - */ |
|
| 41 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 30 | + // Get the post type and check whether it supports the editor. |
|
| 31 | + // |
|
| 32 | + // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 33 | + $post_type = get_post_type( $post_id ); |
|
| 34 | + /** |
|
| 35 | + * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 36 | + * |
|
| 37 | + * @since 3.19.4 |
|
| 38 | + * |
|
| 39 | + * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 40 | + */ |
|
| 41 | + $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 42 | 42 | |
| 43 | - // Bail out if it's not an entity. |
|
| 44 | - if ( ! $is_editor_supported ) { |
|
| 45 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 43 | + // Bail out if it's not an entity. |
|
| 44 | + if ( ! $is_editor_supported ) { |
|
| 45 | + $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 46 | 46 | |
| 47 | - return; |
|
| 48 | - } |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - // Unhook this function so it doesn't loop infinitely. |
|
| 51 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 50 | + // Unhook this function so it doesn't loop infinitely. |
|
| 51 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 52 | 52 | |
| 53 | - // raise the *wl_linked_data_save_post* event. |
|
| 54 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 53 | + // raise the *wl_linked_data_save_post* event. |
|
| 54 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 55 | 55 | |
| 56 | - // Re-hook this function. |
|
| 57 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 56 | + // Re-hook this function. |
|
| 57 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -63,112 +63,112 @@ discard block |
||
| 63 | 63 | |
| 64 | 64 | function wl_linked_data_rest_insert_post( $post, $request, $creating ){ |
| 65 | 65 | |
| 66 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 66 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 67 | 67 | |
| 68 | - $post_id = $post->ID; |
|
| 69 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 68 | + $post_id = $post->ID; |
|
| 69 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 70 | 70 | |
| 71 | - // Ignore auto-saves |
|
| 72 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 73 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 74 | - return; |
|
| 75 | - } |
|
| 71 | + // Ignore auto-saves |
|
| 72 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 73 | + $log->trace( 'Doing autosave, skipping...' ); |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - // Get the entity service instance. |
|
| 78 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 77 | + // Get the entity service instance. |
|
| 78 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 79 | 79 | |
| 80 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 81 | - $entities_uri_mapping = array(); |
|
| 80 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 81 | + $entities_uri_mapping = array(); |
|
| 82 | 82 | |
| 83 | - $metas = $request->get_json_params(); |
|
| 84 | - $metas = $metas['meta']; |
|
| 83 | + $metas = $request->get_json_params(); |
|
| 84 | + $metas = $metas['meta']; |
|
| 85 | 85 | |
| 86 | - // Ignore if no change in meta |
|
| 87 | - if ( empty($metas) ) { |
|
| 88 | - $log->trace( 'No meta data, skipping...' ); |
|
| 89 | - return; |
|
| 90 | - } |
|
| 86 | + // Ignore if no change in meta |
|
| 87 | + if ( empty($metas) ) { |
|
| 88 | + $log->trace( 'No meta data, skipping...' ); |
|
| 89 | + return; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $entities_via_meta = json_decode($metas['wl_entities_gutenberg'], true); |
|
| 92 | + $entities_via_meta = json_decode($metas['wl_entities_gutenberg'], true); |
|
| 93 | 93 | |
| 94 | - //file_put_contents( WP_PLUGIN_DIR . '/wordlift/debug.txt', serialize($entities_via_meta) ); |
|
| 94 | + //file_put_contents( WP_PLUGIN_DIR . '/wordlift/debug.txt', serialize($entities_via_meta) ); |
|
| 95 | 95 | |
| 96 | - foreach ( $entities_via_meta as $entity_uri => $entity ) { |
|
| 96 | + foreach ( $entities_via_meta as $entity_uri => $entity ) { |
|
| 97 | 97 | |
| 98 | - // Only if the current entity is created from scratch let's avoid to |
|
| 99 | - // create more than one entity with same label & entity type. |
|
| 100 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 101 | - $entity['main_type'] : null; |
|
| 98 | + // Only if the current entity is created from scratch let's avoid to |
|
| 99 | + // create more than one entity with same label & entity type. |
|
| 100 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 101 | + $entity['main_type'] : null; |
|
| 102 | 102 | |
| 103 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 104 | - // 1. when $entity_uri is an internal uri |
|
| 105 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 106 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 103 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 104 | + // 1. when $entity_uri is an internal uri |
|
| 105 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 106 | + $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 107 | 107 | |
| 108 | - // Detect the uri depending if is an existing or a new entity |
|
| 109 | - $uri = ( null === $ie ) ? |
|
| 110 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 111 | - $entity['label'], |
|
| 112 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 113 | - $entity_type |
|
| 114 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 108 | + // Detect the uri depending if is an existing or a new entity |
|
| 109 | + $uri = ( null === $ie ) ? |
|
| 110 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 111 | + $entity['label'], |
|
| 112 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 113 | + $entity_type |
|
| 114 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 115 | 115 | |
| 116 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 117 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 116 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 117 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 118 | 118 | |
| 119 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 120 | - // These uris need to be rewritten here and replaced in the content |
|
| 121 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 122 | - // Override the entity obj |
|
| 123 | - $entity['uri'] = $uri; |
|
| 124 | - } |
|
| 119 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 120 | + // These uris need to be rewritten here and replaced in the content |
|
| 121 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 122 | + // Override the entity obj |
|
| 123 | + $entity['uri'] = $uri; |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - // Update entity data with related post |
|
| 127 | - $entity['related_post_id'] = $post_id; |
|
| 126 | + // Update entity data with related post |
|
| 127 | + $entity['related_post_id'] = $post_id; |
|
| 128 | 128 | |
| 129 | - // Save the entity if is a new entity |
|
| 130 | - if ( null === $ie ) { |
|
| 131 | - wl_save_entity( $entity ); |
|
| 132 | - } |
|
| 129 | + // Save the entity if is a new entity |
|
| 130 | + if ( null === $ie ) { |
|
| 131 | + wl_save_entity( $entity ); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - } |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - // Replace tmp uris in content post if needed |
|
| 137 | - $updated_post_content = $post->post_content; |
|
| 136 | + // Replace tmp uris in content post if needed |
|
| 137 | + $updated_post_content = $post->post_content; |
|
| 138 | 138 | |
| 139 | - // Update the post content if we found mappings of new entities. |
|
| 140 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 141 | - // Save each entity and store the post id. |
|
| 142 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 143 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 144 | - } |
|
| 139 | + // Update the post content if we found mappings of new entities. |
|
| 140 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 141 | + // Save each entity and store the post id. |
|
| 142 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 143 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - // Update the post content. |
|
| 147 | - wp_update_post( array( |
|
| 148 | - 'ID' => $post->ID, |
|
| 149 | - 'post_content' => $updated_post_content, |
|
| 150 | - ) ); |
|
| 151 | - } |
|
| 146 | + // Update the post content. |
|
| 147 | + wp_update_post( array( |
|
| 148 | + 'ID' => $post->ID, |
|
| 149 | + 'post_content' => $updated_post_content, |
|
| 150 | + ) ); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - // Extract related/referenced entities from text. |
|
| 154 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 153 | + // Extract related/referenced entities from text. |
|
| 154 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 155 | 155 | |
| 156 | - // Reset previously saved instances. |
|
| 157 | - wl_core_delete_relation_instances( $post_id ); |
|
| 156 | + // Reset previously saved instances. |
|
| 157 | + wl_core_delete_relation_instances( $post_id ); |
|
| 158 | 158 | |
| 159 | - // Save relation instances |
|
| 160 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 159 | + // Save relation instances |
|
| 160 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 161 | 161 | |
| 162 | - wl_core_add_relation_instance( |
|
| 163 | - $post_id, |
|
| 164 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 165 | - $referenced_entity_id |
|
| 166 | - ); |
|
| 162 | + wl_core_add_relation_instance( |
|
| 163 | + $post_id, |
|
| 164 | + $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 165 | + $referenced_entity_id |
|
| 166 | + ); |
|
| 167 | 167 | |
| 168 | - } |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - // Push the post to Redlink. |
|
| 171 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 170 | + // Push the post to Redlink. |
|
| 171 | + Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -180,156 +180,156 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 182 | 182 | |
| 183 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 183 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 184 | 184 | |
| 185 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 185 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 186 | 186 | |
| 187 | - // Ignore auto-saves |
|
| 188 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 189 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 187 | + // Ignore auto-saves |
|
| 188 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 189 | + $log->trace( 'Doing autosave, skipping...' ); |
|
| 190 | 190 | |
| 191 | - return; |
|
| 192 | - } |
|
| 191 | + return; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - // get the current post. |
|
| 195 | - $post = get_post( $post_id ); |
|
| 194 | + // get the current post. |
|
| 195 | + $post = get_post( $post_id ); |
|
| 196 | 196 | |
| 197 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 197 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 198 | 198 | |
| 199 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 199 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 200 | 200 | |
| 201 | - // Get the entity service instance. |
|
| 202 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 201 | + // Get the entity service instance. |
|
| 202 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 203 | 203 | |
| 204 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 205 | - $entities_uri_mapping = array(); |
|
| 204 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 205 | + $entities_uri_mapping = array(); |
|
| 206 | 206 | |
| 207 | - // Save the entities coming with POST data. |
|
| 208 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 207 | + // Save the entities coming with POST data. |
|
| 208 | + if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 209 | 209 | |
| 210 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 211 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 212 | - wl_write_log( "]" ); |
|
| 213 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 214 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 215 | - wl_write_log( "]" ); |
|
| 210 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 211 | + wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 212 | + wl_write_log( "]" ); |
|
| 213 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 214 | + wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 215 | + wl_write_log( "]" ); |
|
| 216 | 216 | |
| 217 | - $entities_via_post = $_POST['wl_entities']; |
|
| 218 | - $boxes_via_post = $_POST['wl_boxes']; |
|
| 217 | + $entities_via_post = $_POST['wl_entities']; |
|
| 218 | + $boxes_via_post = $_POST['wl_boxes']; |
|
| 219 | 219 | |
| 220 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 220 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 221 | 221 | |
| 222 | - // Only if the current entity is created from scratch let's avoid to |
|
| 223 | - // create more than one entity with same label & entity type. |
|
| 224 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 225 | - $entity['main_type'] : null; |
|
| 222 | + // Only if the current entity is created from scratch let's avoid to |
|
| 223 | + // create more than one entity with same label & entity type. |
|
| 224 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 225 | + $entity['main_type'] : null; |
|
| 226 | 226 | |
| 227 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 228 | - // 1. when $entity_uri is an internal uri |
|
| 229 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 230 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 227 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 228 | + // 1. when $entity_uri is an internal uri |
|
| 229 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 230 | + $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 231 | 231 | |
| 232 | - // Detect the uri depending if is an existing or a new entity |
|
| 233 | - $uri = ( null === $ie ) ? |
|
| 234 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 235 | - $entity['label'], |
|
| 236 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 237 | - $entity_type |
|
| 238 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 232 | + // Detect the uri depending if is an existing or a new entity |
|
| 233 | + $uri = ( null === $ie ) ? |
|
| 234 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 235 | + $entity['label'], |
|
| 236 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 237 | + $entity_type |
|
| 238 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 239 | 239 | |
| 240 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 241 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 240 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 241 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 242 | 242 | |
| 243 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 244 | - // These uris need to be rewritten here and replaced in the content |
|
| 245 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 246 | - // Override the entity obj |
|
| 247 | - $entity['uri'] = $uri; |
|
| 248 | - } |
|
| 243 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 244 | + // These uris need to be rewritten here and replaced in the content |
|
| 245 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 246 | + // Override the entity obj |
|
| 247 | + $entity['uri'] = $uri; |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - // Update entity data with related post |
|
| 251 | - $entity['related_post_id'] = $post_id; |
|
| 250 | + // Update entity data with related post |
|
| 251 | + $entity['related_post_id'] = $post_id; |
|
| 252 | 252 | |
| 253 | - // Save the entity if is a new entity |
|
| 254 | - if ( null === $ie ) { |
|
| 255 | - wl_save_entity( $entity ); |
|
| 256 | - } |
|
| 253 | + // Save the entity if is a new entity |
|
| 254 | + if ( null === $ie ) { |
|
| 255 | + wl_save_entity( $entity ); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - } |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - } |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - // Replace tmp uris in content post if needed |
|
| 263 | - $updated_post_content = $post->post_content; |
|
| 262 | + // Replace tmp uris in content post if needed |
|
| 263 | + $updated_post_content = $post->post_content; |
|
| 264 | 264 | |
| 265 | - // Update the post content if we found mappings of new entities. |
|
| 266 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 267 | - // Save each entity and store the post id. |
|
| 268 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 269 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 270 | - } |
|
| 265 | + // Update the post content if we found mappings of new entities. |
|
| 266 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 267 | + // Save each entity and store the post id. |
|
| 268 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 269 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - // Update the post content. |
|
| 273 | - wp_update_post( array( |
|
| 274 | - 'ID' => $post->ID, |
|
| 275 | - 'post_content' => $updated_post_content, |
|
| 276 | - ) ); |
|
| 277 | - } |
|
| 272 | + // Update the post content. |
|
| 273 | + wp_update_post( array( |
|
| 274 | + 'ID' => $post->ID, |
|
| 275 | + 'post_content' => $updated_post_content, |
|
| 276 | + ) ); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - // Extract related/referenced entities from text. |
|
| 280 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 279 | + // Extract related/referenced entities from text. |
|
| 280 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 281 | 281 | |
| 282 | - // Reset previously saved instances. |
|
| 283 | - wl_core_delete_relation_instances( $post_id ); |
|
| 282 | + // Reset previously saved instances. |
|
| 283 | + wl_core_delete_relation_instances( $post_id ); |
|
| 284 | 284 | |
| 285 | - // Save relation instances |
|
| 286 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 285 | + // Save relation instances |
|
| 286 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 287 | 287 | |
| 288 | - wl_core_add_relation_instance( |
|
| 289 | - $post_id, |
|
| 290 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 291 | - $referenced_entity_id |
|
| 292 | - ); |
|
| 288 | + wl_core_add_relation_instance( |
|
| 289 | + $post_id, |
|
| 290 | + $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 291 | + $referenced_entity_id |
|
| 292 | + ); |
|
| 293 | 293 | |
| 294 | - } |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 297 | - // Save post metadata if available |
|
| 298 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 299 | - $_POST['wl_metadata'] : array(); |
|
| 300 | - |
|
| 301 | - $fields = array( |
|
| 302 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 303 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 304 | - ); |
|
| 305 | - |
|
| 306 | - // Unlink topic taxonomy terms |
|
| 307 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 308 | - |
|
| 309 | - foreach ( $fields as $field ) { |
|
| 310 | - |
|
| 311 | - // Delete current values |
|
| 312 | - delete_post_meta( $post->ID, $field ); |
|
| 313 | - // Retrieve the entity uri |
|
| 314 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 315 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 316 | - |
|
| 317 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 318 | - |
|
| 319 | - if ( $entity ) { |
|
| 320 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 321 | - // Set also the topic taxonomy |
|
| 322 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 323 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - // Push the post to Redlink. |
|
| 330 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 331 | - |
|
| 332 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 296 | + if ( isset( $_POST['wl_entities'] ) ) { |
|
| 297 | + // Save post metadata if available |
|
| 298 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 299 | + $_POST['wl_metadata'] : array(); |
|
| 300 | + |
|
| 301 | + $fields = array( |
|
| 302 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 303 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 304 | + ); |
|
| 305 | + |
|
| 306 | + // Unlink topic taxonomy terms |
|
| 307 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 308 | + |
|
| 309 | + foreach ( $fields as $field ) { |
|
| 310 | + |
|
| 311 | + // Delete current values |
|
| 312 | + delete_post_meta( $post->ID, $field ); |
|
| 313 | + // Retrieve the entity uri |
|
| 314 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 315 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 316 | + |
|
| 317 | + $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 318 | + |
|
| 319 | + if ( $entity ) { |
|
| 320 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 321 | + // Set also the topic taxonomy |
|
| 322 | + if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 323 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + // Push the post to Redlink. |
|
| 330 | + Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 331 | + |
|
| 332 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -352,208 +352,208 @@ discard block |
||
| 352 | 352 | */ |
| 353 | 353 | function wl_save_entity( $entity_data ) { |
| 354 | 354 | |
| 355 | - // Required for REST API calls |
|
| 356 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 357 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 358 | - } |
|
| 355 | + // Required for REST API calls |
|
| 356 | + if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 357 | + require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 358 | + } |
|
| 359 | 359 | |
| 360 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 360 | + $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 361 | 361 | |
| 362 | - $uri = $entity_data['uri']; |
|
| 363 | - /* |
|
| 362 | + $uri = $entity_data['uri']; |
|
| 363 | + /* |
|
| 364 | 364 | * Data is coming from a $_POST, sanitize it. |
| 365 | 365 | * |
| 366 | 366 | * @since 3.19.4 |
| 367 | 367 | * |
| 368 | 368 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 369 | 369 | */ |
| 370 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 371 | - $type_uri = $entity_data['main_type']; |
|
| 372 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 373 | - $description = $entity_data['description']; |
|
| 374 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 375 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 376 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 377 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 378 | - |
|
| 379 | - // Get the synonyms. |
|
| 380 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 381 | - |
|
| 382 | - // Check whether an entity already exists with the provided URI. |
|
| 383 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 384 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 385 | - |
|
| 386 | - return $post; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - // Prepare properties of the new entity. |
|
| 390 | - $params = array( |
|
| 391 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 392 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 393 | - 'post_title' => $label, |
|
| 394 | - 'post_content' => $description, |
|
| 395 | - 'post_excerpt' => '', |
|
| 396 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 397 | - // entity with a post_name already set, since this call is made only for |
|
| 398 | - // new entities. |
|
| 399 | - // |
|
| 400 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 401 | - 'post_name' => sanitize_title( $label ), |
|
| 402 | - ); |
|
| 403 | - |
|
| 404 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 405 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 406 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 407 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 408 | - // is created when saving a post. |
|
| 409 | - global $wpseo_metabox, $seo_ultimate; |
|
| 410 | - if ( isset( $wpseo_metabox ) ) { |
|
| 411 | - remove_action( 'wp_insert_post', array( |
|
| 412 | - $wpseo_metabox, |
|
| 413 | - 'save_postdata', |
|
| 414 | - ) ); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - if ( isset( $seo_ultimate ) ) { |
|
| 418 | - remove_action( 'save_post', array( |
|
| 419 | - $seo_ultimate, |
|
| 420 | - 'save_postmeta_box', |
|
| 421 | - ) ); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 425 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 426 | - // to the save_post and restore them after we saved the entity. |
|
| 427 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 428 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 429 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 430 | - global $wp_filter; |
|
| 431 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 432 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 433 | - |
|
| 434 | - |
|
| 435 | - $log->trace( 'Going to insert new post...' ); |
|
| 436 | - |
|
| 437 | - // create or update the post. |
|
| 438 | - $post_id = wp_insert_post( $params, true ); |
|
| 439 | - |
|
| 440 | - // Setting the alternative labels for this entity. |
|
| 441 | - Wordlift_Entity_Service::get_instance() |
|
| 442 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 443 | - |
|
| 444 | - // Restore all the existing filters. |
|
| 445 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 446 | - |
|
| 447 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 448 | - if ( isset( $wpseo_metabox ) ) { |
|
| 449 | - add_action( 'wp_insert_post', array( |
|
| 450 | - $wpseo_metabox, |
|
| 451 | - 'save_postdata', |
|
| 452 | - ) ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 456 | - if ( isset( $seo_ultimate ) ) { |
|
| 457 | - add_action( 'save_post', array( |
|
| 458 | - $seo_ultimate, |
|
| 459 | - 'save_postmeta_box', |
|
| 460 | - ), 10, 2 ); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - // TODO: handle errors. |
|
| 464 | - if ( is_wp_error( $post_id ) ) { |
|
| 465 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 466 | - |
|
| 467 | - // inform an error occurred. |
|
| 468 | - return null; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 472 | - |
|
| 473 | - // Save the entity types. |
|
| 474 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 475 | - |
|
| 476 | - // Get a dataset URI for the entity. |
|
| 477 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 478 | - |
|
| 479 | - // Save the entity URI. |
|
| 480 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 481 | - |
|
| 482 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 483 | - if ( $wl_uri !== $uri ) { |
|
| 484 | - array_push( $same_as, $uri ); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 488 | - |
|
| 489 | - // Save the sameAs data for the entity. |
|
| 490 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 491 | - |
|
| 492 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 493 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 494 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - // Call hooks. |
|
| 498 | - do_action( 'wl_save_entity', $post_id ); |
|
| 499 | - |
|
| 500 | - foreach ( $images as $image_remote_url ) { |
|
| 501 | - |
|
| 502 | - // Check if image is already present in local DB |
|
| 503 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 504 | - // Do nothing. |
|
| 505 | - continue; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 509 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 510 | - |
|
| 511 | - // Skip if an existing image is found. |
|
| 512 | - if ( null !== $existing_image ) { |
|
| 513 | - continue; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - // Save the image and get the local path. |
|
| 517 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 518 | - |
|
| 519 | - if ( false === $image ) { |
|
| 520 | - continue; |
|
| 521 | - } |
|
| 370 | + $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 371 | + $type_uri = $entity_data['main_type']; |
|
| 372 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 373 | + $description = $entity_data['description']; |
|
| 374 | + $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 375 | + $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 376 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 377 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 378 | + |
|
| 379 | + // Get the synonyms. |
|
| 380 | + $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 381 | + |
|
| 382 | + // Check whether an entity already exists with the provided URI. |
|
| 383 | + if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 384 | + $log->debug( "Post already exists for URI $uri." ); |
|
| 385 | + |
|
| 386 | + return $post; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + // Prepare properties of the new entity. |
|
| 390 | + $params = array( |
|
| 391 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 392 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 393 | + 'post_title' => $label, |
|
| 394 | + 'post_content' => $description, |
|
| 395 | + 'post_excerpt' => '', |
|
| 396 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 397 | + // entity with a post_name already set, since this call is made only for |
|
| 398 | + // new entities. |
|
| 399 | + // |
|
| 400 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 401 | + 'post_name' => sanitize_title( $label ), |
|
| 402 | + ); |
|
| 403 | + |
|
| 404 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 405 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 406 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 407 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 408 | + // is created when saving a post. |
|
| 409 | + global $wpseo_metabox, $seo_ultimate; |
|
| 410 | + if ( isset( $wpseo_metabox ) ) { |
|
| 411 | + remove_action( 'wp_insert_post', array( |
|
| 412 | + $wpseo_metabox, |
|
| 413 | + 'save_postdata', |
|
| 414 | + ) ); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + if ( isset( $seo_ultimate ) ) { |
|
| 418 | + remove_action( 'save_post', array( |
|
| 419 | + $seo_ultimate, |
|
| 420 | + 'save_postmeta_box', |
|
| 421 | + ) ); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 425 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 426 | + // to the save_post and restore them after we saved the entity. |
|
| 427 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 428 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 429 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 430 | + global $wp_filter; |
|
| 431 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 432 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 433 | + |
|
| 434 | + |
|
| 435 | + $log->trace( 'Going to insert new post...' ); |
|
| 436 | + |
|
| 437 | + // create or update the post. |
|
| 438 | + $post_id = wp_insert_post( $params, true ); |
|
| 439 | + |
|
| 440 | + // Setting the alternative labels for this entity. |
|
| 441 | + Wordlift_Entity_Service::get_instance() |
|
| 442 | + ->set_alternative_labels( $post_id, $synonyms ); |
|
| 443 | + |
|
| 444 | + // Restore all the existing filters. |
|
| 445 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 446 | + |
|
| 447 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 448 | + if ( isset( $wpseo_metabox ) ) { |
|
| 449 | + add_action( 'wp_insert_post', array( |
|
| 450 | + $wpseo_metabox, |
|
| 451 | + 'save_postdata', |
|
| 452 | + ) ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 456 | + if ( isset( $seo_ultimate ) ) { |
|
| 457 | + add_action( 'save_post', array( |
|
| 458 | + $seo_ultimate, |
|
| 459 | + 'save_postmeta_box', |
|
| 460 | + ), 10, 2 ); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + // TODO: handle errors. |
|
| 464 | + if ( is_wp_error( $post_id ) ) { |
|
| 465 | + $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 466 | + |
|
| 467 | + // inform an error occurred. |
|
| 468 | + return null; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 472 | + |
|
| 473 | + // Save the entity types. |
|
| 474 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 475 | + |
|
| 476 | + // Get a dataset URI for the entity. |
|
| 477 | + $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 478 | + |
|
| 479 | + // Save the entity URI. |
|
| 480 | + wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 481 | + |
|
| 482 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 483 | + if ( $wl_uri !== $uri ) { |
|
| 484 | + array_push( $same_as, $uri ); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + $new_uri = wl_get_entity_uri( $post_id ); |
|
| 488 | + |
|
| 489 | + // Save the sameAs data for the entity. |
|
| 490 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 491 | + |
|
| 492 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 493 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 494 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + // Call hooks. |
|
| 498 | + do_action( 'wl_save_entity', $post_id ); |
|
| 499 | + |
|
| 500 | + foreach ( $images as $image_remote_url ) { |
|
| 501 | + |
|
| 502 | + // Check if image is already present in local DB |
|
| 503 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 504 | + // Do nothing. |
|
| 505 | + continue; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 509 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 510 | + |
|
| 511 | + // Skip if an existing image is found. |
|
| 512 | + if ( null !== $existing_image ) { |
|
| 513 | + continue; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + // Save the image and get the local path. |
|
| 517 | + $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 518 | + |
|
| 519 | + if ( false === $image ) { |
|
| 520 | + continue; |
|
| 521 | + } |
|
| 522 | 522 | |
| 523 | - // Get the local URL. |
|
| 524 | - $filename = $image['path']; |
|
| 525 | - $url = $image['url']; |
|
| 526 | - $content_type = $image['content_type']; |
|
| 523 | + // Get the local URL. |
|
| 524 | + $filename = $image['path']; |
|
| 525 | + $url = $image['url']; |
|
| 526 | + $content_type = $image['content_type']; |
|
| 527 | 527 | |
| 528 | - $attachment = array( |
|
| 529 | - 'guid' => $url, |
|
| 530 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 531 | - 'post_title' => $label, |
|
| 532 | - // Set the title to the post title. |
|
| 533 | - 'post_content' => '', |
|
| 534 | - 'post_status' => 'inherit', |
|
| 535 | - 'post_mime_type' => $content_type, |
|
| 536 | - ); |
|
| 537 | - |
|
| 538 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 539 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 540 | - |
|
| 541 | - // Set the source URL for the image. |
|
| 542 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 528 | + $attachment = array( |
|
| 529 | + 'guid' => $url, |
|
| 530 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 531 | + 'post_title' => $label, |
|
| 532 | + // Set the title to the post title. |
|
| 533 | + 'post_content' => '', |
|
| 534 | + 'post_status' => 'inherit', |
|
| 535 | + 'post_mime_type' => $content_type, |
|
| 536 | + ); |
|
| 537 | + |
|
| 538 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 539 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 540 | + |
|
| 541 | + // Set the source URL for the image. |
|
| 542 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 543 | 543 | |
| 544 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 545 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 544 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 545 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 546 | 546 | |
| 547 | - // Set it as the featured image. |
|
| 548 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 549 | - } |
|
| 547 | + // Set it as the featured image. |
|
| 548 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 549 | + } |
|
| 550 | 550 | |
| 551 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 552 | - // save the entity in the triple store. |
|
| 553 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 551 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 552 | + // save the entity in the triple store. |
|
| 553 | + Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 554 | 554 | |
| 555 | - // finally return the entity post. |
|
| 556 | - return get_post( $post_id ); |
|
| 555 | + // finally return the entity post. |
|
| 556 | + return get_post( $post_id ); |
|
| 557 | 557 | } |
| 558 | 558 | |
| 559 | 559 | /** |
@@ -567,40 +567,40 @@ discard block |
||
| 567 | 567 | */ |
| 568 | 568 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 569 | 569 | |
| 570 | - // Remove quote escapes. |
|
| 571 | - $content = str_replace( '\\"', '"', $content ); |
|
| 570 | + // Remove quote escapes. |
|
| 571 | + $content = str_replace( '\\"', '"', $content ); |
|
| 572 | 572 | |
| 573 | - // Match all itemid attributes. |
|
| 574 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 573 | + // Match all itemid attributes. |
|
| 574 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 575 | 575 | |
| 576 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 576 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 577 | 577 | |
| 578 | - // Remove the pattern while it is found (match nested annotations). |
|
| 579 | - $matches = array(); |
|
| 578 | + // Remove the pattern while it is found (match nested annotations). |
|
| 579 | + $matches = array(); |
|
| 580 | 580 | |
| 581 | - // In case of errors, return an empty array. |
|
| 582 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 583 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 581 | + // In case of errors, return an empty array. |
|
| 582 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 583 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 584 | 584 | |
| 585 | - return array(); |
|
| 586 | - } |
|
| 585 | + return array(); |
|
| 586 | + } |
|
| 587 | 587 | |
| 588 | 588 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 589 | 589 | |
| 590 | - // Collect the entities. |
|
| 591 | - $entities = array(); |
|
| 592 | - foreach ( $matches[1] as $uri ) { |
|
| 593 | - $uri_d = html_entity_decode( $uri ); |
|
| 590 | + // Collect the entities. |
|
| 591 | + $entities = array(); |
|
| 592 | + foreach ( $matches[1] as $uri ) { |
|
| 593 | + $uri_d = html_entity_decode( $uri ); |
|
| 594 | 594 | |
| 595 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 595 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 596 | 596 | |
| 597 | - if ( null !== $entity ) { |
|
| 598 | - array_push( $entities, $entity->ID ); |
|
| 599 | - } |
|
| 600 | - } |
|
| 597 | + if ( null !== $entity ) { |
|
| 598 | + array_push( $entities, $entity->ID ); |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | 601 | |
| 602 | - // $count = sizeof( $entities ); |
|
| 603 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 602 | + // $count = sizeof( $entities ); |
|
| 603 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 604 | 604 | |
| 605 | - return $entities; |
|
| 605 | + return $entities; |
|
| 606 | 606 | } |
@@ -14,15 +14,15 @@ discard block |
||
| 14 | 14 | * |
| 15 | 15 | * @param int $post_id The post id. |
| 16 | 16 | */ |
| 17 | -function wl_linked_data_save_post( $post_id ) { |
|
| 17 | +function wl_linked_data_save_post($post_id) { |
|
| 18 | 18 | |
| 19 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 19 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post'); |
|
| 20 | 20 | |
| 21 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 21 | + $log->trace("Saving post $post_id to Linked Data..."); |
|
| 22 | 22 | |
| 23 | 23 | // If it's not numeric exit from here. |
| 24 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 25 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 24 | + if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) { |
|
| 25 | + $log->debug("Skipping $post_id, because id is not numeric or is a post revision."); |
|
| 26 | 26 | |
| 27 | 27 | return; |
| 28 | 28 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | // Get the post type and check whether it supports the editor. |
| 31 | 31 | // |
| 32 | 32 | // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
| 33 | - $post_type = get_post_type( $post_id ); |
|
| 33 | + $post_type = get_post_type($post_id); |
|
| 34 | 34 | /** |
| 35 | 35 | * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
| 36 | 36 | * |
@@ -38,39 +38,39 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
| 40 | 40 | */ |
| 41 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 41 | + $is_editor_supported = wl_post_type_supports_editor($post_type); |
|
| 42 | 42 | |
| 43 | 43 | // Bail out if it's not an entity. |
| 44 | - if ( ! $is_editor_supported ) { |
|
| 45 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 44 | + if ( ! $is_editor_supported) { |
|
| 45 | + $log->debug("Skipping $post_id, because $post_type doesn't support the editor (content)."); |
|
| 46 | 46 | |
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // Unhook this function so it doesn't loop infinitely. |
| 51 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 51 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
| 52 | 52 | |
| 53 | 53 | // raise the *wl_linked_data_save_post* event. |
| 54 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 54 | + do_action('wl_linked_data_save_post', $post_id); |
|
| 55 | 55 | |
| 56 | 56 | // Re-hook this function. |
| 57 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 57 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 60 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
| 61 | 61 | |
| 62 | -add_action( 'rest_insert_post', 'wl_linked_data_rest_insert_post', 10, 3 ); |
|
| 62 | +add_action('rest_insert_post', 'wl_linked_data_rest_insert_post', 10, 3); |
|
| 63 | 63 | |
| 64 | -function wl_linked_data_rest_insert_post( $post, $request, $creating ){ |
|
| 64 | +function wl_linked_data_rest_insert_post($post, $request, $creating) { |
|
| 65 | 65 | |
| 66 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 66 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
| 67 | 67 | |
| 68 | 68 | $post_id = $post->ID; |
| 69 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 69 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
| 70 | 70 | |
| 71 | 71 | // Ignore auto-saves |
| 72 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 73 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 72 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 73 | + $log->trace('Doing autosave, skipping...'); |
|
| 74 | 74 | return; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -84,8 +84,8 @@ discard block |
||
| 84 | 84 | $metas = $metas['meta']; |
| 85 | 85 | |
| 86 | 86 | // Ignore if no change in meta |
| 87 | - if ( empty($metas) ) { |
|
| 88 | - $log->trace( 'No meta data, skipping...' ); |
|
| 87 | + if (empty($metas)) { |
|
| 88 | + $log->trace('No meta data, skipping...'); |
|
| 89 | 89 | return; |
| 90 | 90 | } |
| 91 | 91 | |
@@ -93,32 +93,32 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | //file_put_contents( WP_PLUGIN_DIR . '/wordlift/debug.txt', serialize($entities_via_meta) ); |
| 95 | 95 | |
| 96 | - foreach ( $entities_via_meta as $entity_uri => $entity ) { |
|
| 96 | + foreach ($entities_via_meta as $entity_uri => $entity) { |
|
| 97 | 97 | |
| 98 | 98 | // Only if the current entity is created from scratch let's avoid to |
| 99 | 99 | // create more than one entity with same label & entity type. |
| 100 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 100 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 101 | 101 | $entity['main_type'] : null; |
| 102 | 102 | |
| 103 | 103 | // Look if current entity uri matches an internal existing entity, meaning: |
| 104 | 104 | // 1. when $entity_uri is an internal uri |
| 105 | 105 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 106 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 106 | + $ie = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 107 | 107 | |
| 108 | 108 | // Detect the uri depending if is an existing or a new entity |
| 109 | - $uri = ( null === $ie ) ? |
|
| 109 | + $uri = (null === $ie) ? |
|
| 110 | 110 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 111 | 111 | $entity['label'], |
| 112 | 112 | Wordlift_Entity_Service::TYPE_NAME, |
| 113 | 113 | $entity_type |
| 114 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 114 | + ) : wl_get_entity_uri($ie->ID); |
|
| 115 | 115 | |
| 116 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 117 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 116 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 117 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 118 | 118 | |
| 119 | 119 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 120 | 120 | // These uris need to be rewritten here and replaced in the content |
| 121 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 121 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 122 | 122 | // Override the entity obj |
| 123 | 123 | $entity['uri'] = $uri; |
| 124 | 124 | } |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | $entity['related_post_id'] = $post_id; |
| 128 | 128 | |
| 129 | 129 | // Save the entity if is a new entity |
| 130 | - if ( null === $ie ) { |
|
| 131 | - wl_save_entity( $entity ); |
|
| 130 | + if (null === $ie) { |
|
| 131 | + wl_save_entity($entity); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | } |
@@ -137,38 +137,38 @@ discard block |
||
| 137 | 137 | $updated_post_content = $post->post_content; |
| 138 | 138 | |
| 139 | 139 | // Update the post content if we found mappings of new entities. |
| 140 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 140 | + if ( ! empty($entities_uri_mapping)) { |
|
| 141 | 141 | // Save each entity and store the post id. |
| 142 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 143 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 142 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 143 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Update the post content. |
| 147 | - wp_update_post( array( |
|
| 147 | + wp_update_post(array( |
|
| 148 | 148 | 'ID' => $post->ID, |
| 149 | 149 | 'post_content' => $updated_post_content, |
| 150 | - ) ); |
|
| 150 | + )); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | // Extract related/referenced entities from text. |
| 154 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 154 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 155 | 155 | |
| 156 | 156 | // Reset previously saved instances. |
| 157 | - wl_core_delete_relation_instances( $post_id ); |
|
| 157 | + wl_core_delete_relation_instances($post_id); |
|
| 158 | 158 | |
| 159 | 159 | // Save relation instances |
| 160 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 160 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 161 | 161 | |
| 162 | 162 | wl_core_add_relation_instance( |
| 163 | 163 | $post_id, |
| 164 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 164 | + $entity_service->get_classification_scope_for($referenced_entity_id), |
|
| 165 | 165 | $referenced_entity_id |
| 166 | 166 | ); |
| 167 | 167 | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | // Push the post to Redlink. |
| 171 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 171 | + Wordlift_Linked_Data_Service::get_instance()->push($post->ID); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -178,23 +178,23 @@ discard block |
||
| 178 | 178 | * |
| 179 | 179 | * @param int $post_id The post id being saved. |
| 180 | 180 | */ |
| 181 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 181 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 182 | 182 | |
| 183 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 183 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
| 184 | 184 | |
| 185 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 185 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
| 186 | 186 | |
| 187 | 187 | // Ignore auto-saves |
| 188 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 189 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 188 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 189 | + $log->trace('Doing autosave, skipping...'); |
|
| 190 | 190 | |
| 191 | 191 | return; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // get the current post. |
| 195 | - $post = get_post( $post_id ); |
|
| 195 | + $post = get_post($post_id); |
|
| 196 | 196 | |
| 197 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 197 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 198 | 198 | |
| 199 | 199 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 200 | 200 | |
@@ -205,44 +205,44 @@ discard block |
||
| 205 | 205 | $entities_uri_mapping = array(); |
| 206 | 206 | |
| 207 | 207 | // Save the entities coming with POST data. |
| 208 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 208 | + if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { |
|
| 209 | 209 | |
| 210 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 211 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 212 | - wl_write_log( "]" ); |
|
| 213 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 214 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 215 | - wl_write_log( "]" ); |
|
| 210 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 211 | + wl_write_log(json_encode($_POST['wl_entities'])); |
|
| 212 | + wl_write_log("]"); |
|
| 213 | + wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: "); |
|
| 214 | + wl_write_log(json_encode($_POST['wl_boxes'], true)); |
|
| 215 | + wl_write_log("]"); |
|
| 216 | 216 | |
| 217 | 217 | $entities_via_post = $_POST['wl_entities']; |
| 218 | 218 | $boxes_via_post = $_POST['wl_boxes']; |
| 219 | 219 | |
| 220 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 220 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 221 | 221 | |
| 222 | 222 | // Only if the current entity is created from scratch let's avoid to |
| 223 | 223 | // create more than one entity with same label & entity type. |
| 224 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 224 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 225 | 225 | $entity['main_type'] : null; |
| 226 | 226 | |
| 227 | 227 | // Look if current entity uri matches an internal existing entity, meaning: |
| 228 | 228 | // 1. when $entity_uri is an internal uri |
| 229 | 229 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 230 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 230 | + $ie = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 231 | 231 | |
| 232 | 232 | // Detect the uri depending if is an existing or a new entity |
| 233 | - $uri = ( null === $ie ) ? |
|
| 233 | + $uri = (null === $ie) ? |
|
| 234 | 234 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 235 | 235 | $entity['label'], |
| 236 | 236 | Wordlift_Entity_Service::TYPE_NAME, |
| 237 | 237 | $entity_type |
| 238 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 238 | + ) : wl_get_entity_uri($ie->ID); |
|
| 239 | 239 | |
| 240 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 241 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 240 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 241 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 242 | 242 | |
| 243 | 243 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 244 | 244 | // These uris need to be rewritten here and replaced in the content |
| 245 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 245 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 246 | 246 | // Override the entity obj |
| 247 | 247 | $entity['uri'] = $uri; |
| 248 | 248 | } |
@@ -251,8 +251,8 @@ discard block |
||
| 251 | 251 | $entity['related_post_id'] = $post_id; |
| 252 | 252 | |
| 253 | 253 | // Save the entity if is a new entity |
| 254 | - if ( null === $ie ) { |
|
| 255 | - wl_save_entity( $entity ); |
|
| 254 | + if (null === $ie) { |
|
| 255 | + wl_save_entity($entity); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | } |
@@ -263,39 +263,39 @@ discard block |
||
| 263 | 263 | $updated_post_content = $post->post_content; |
| 264 | 264 | |
| 265 | 265 | // Update the post content if we found mappings of new entities. |
| 266 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 266 | + if ( ! empty($entities_uri_mapping)) { |
|
| 267 | 267 | // Save each entity and store the post id. |
| 268 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 269 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 268 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 269 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | // Update the post content. |
| 273 | - wp_update_post( array( |
|
| 273 | + wp_update_post(array( |
|
| 274 | 274 | 'ID' => $post->ID, |
| 275 | 275 | 'post_content' => $updated_post_content, |
| 276 | - ) ); |
|
| 276 | + )); |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | // Extract related/referenced entities from text. |
| 280 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 280 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 281 | 281 | |
| 282 | 282 | // Reset previously saved instances. |
| 283 | - wl_core_delete_relation_instances( $post_id ); |
|
| 283 | + wl_core_delete_relation_instances($post_id); |
|
| 284 | 284 | |
| 285 | 285 | // Save relation instances |
| 286 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 286 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 287 | 287 | |
| 288 | 288 | wl_core_add_relation_instance( |
| 289 | 289 | $post_id, |
| 290 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 290 | + $entity_service->get_classification_scope_for($referenced_entity_id), |
|
| 291 | 291 | $referenced_entity_id |
| 292 | 292 | ); |
| 293 | 293 | |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 296 | + if (isset($_POST['wl_entities'])) { |
|
| 297 | 297 | // Save post metadata if available |
| 298 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 298 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? |
|
| 299 | 299 | $_POST['wl_metadata'] : array(); |
| 300 | 300 | |
| 301 | 301 | $fields = array( |
@@ -304,35 +304,35 @@ discard block |
||
| 304 | 304 | ); |
| 305 | 305 | |
| 306 | 306 | // Unlink topic taxonomy terms |
| 307 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 307 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
| 308 | 308 | |
| 309 | - foreach ( $fields as $field ) { |
|
| 309 | + foreach ($fields as $field) { |
|
| 310 | 310 | |
| 311 | 311 | // Delete current values |
| 312 | - delete_post_meta( $post->ID, $field ); |
|
| 312 | + delete_post_meta($post->ID, $field); |
|
| 313 | 313 | // Retrieve the entity uri |
| 314 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 315 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 314 | + $uri = (isset($metadata_via_post[$field])) ? |
|
| 315 | + stripslashes($metadata_via_post[$field]) : ''; |
|
| 316 | 316 | |
| 317 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 317 | + $entity = $entity_service->get_entity_post_by_uri($uri); |
|
| 318 | 318 | |
| 319 | - if ( $entity ) { |
|
| 320 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 319 | + if ($entity) { |
|
| 320 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
| 321 | 321 | // Set also the topic taxonomy |
| 322 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 323 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 322 | + if ($field === Wordlift_Schema_Service::FIELD_TOPIC) { |
|
| 323 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
| 324 | 324 | } |
| 325 | 325 | } |
| 326 | 326 | } |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | // Push the post to Redlink. |
| 330 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 330 | + Wordlift_Linked_Data_Service::get_instance()->push($post->ID); |
|
| 331 | 331 | |
| 332 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 332 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 335 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 336 | 336 | |
| 337 | 337 | /** |
| 338 | 338 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -350,14 +350,14 @@ discard block |
||
| 350 | 350 | * |
| 351 | 351 | * @return null|WP_Post A post instance or null in case of failure. |
| 352 | 352 | */ |
| 353 | -function wl_save_entity( $entity_data ) { |
|
| 353 | +function wl_save_entity($entity_data) { |
|
| 354 | 354 | |
| 355 | 355 | // Required for REST API calls |
| 356 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 357 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 356 | + if ( ! function_exists('wp_crop_image')) { |
|
| 357 | + require_once(ABSPATH.'wp-admin/includes/image.php'); |
|
| 358 | 358 | } |
| 359 | 359 | |
| 360 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 360 | + $log = Wordlift_Log_Service::get_logger('wl_save_entity'); |
|
| 361 | 361 | |
| 362 | 362 | $uri = $entity_data['uri']; |
| 363 | 363 | /* |
@@ -367,28 +367,28 @@ discard block |
||
| 367 | 367 | * |
| 368 | 368 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 369 | 369 | */ |
| 370 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 370 | + $label = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label'])); |
|
| 371 | 371 | $type_uri = $entity_data['main_type']; |
| 372 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 372 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 373 | 373 | $description = $entity_data['description']; |
| 374 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 375 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 376 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 377 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 374 | + $images = isset($entity_data['image']) ? (array) $entity_data['image'] : array(); |
|
| 375 | + $same_as = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array(); |
|
| 376 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 377 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 378 | 378 | |
| 379 | 379 | // Get the synonyms. |
| 380 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 380 | + $synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array(); |
|
| 381 | 381 | |
| 382 | 382 | // Check whether an entity already exists with the provided URI. |
| 383 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 384 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 383 | + if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) { |
|
| 384 | + $log->debug("Post already exists for URI $uri."); |
|
| 385 | 385 | |
| 386 | 386 | return $post; |
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | // Prepare properties of the new entity. |
| 390 | 390 | $params = array( |
| 391 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 391 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 392 | 392 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 393 | 393 | 'post_title' => $label, |
| 394 | 394 | 'post_content' => $description, |
@@ -398,7 +398,7 @@ discard block |
||
| 398 | 398 | // new entities. |
| 399 | 399 | // |
| 400 | 400 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 401 | - 'post_name' => sanitize_title( $label ), |
|
| 401 | + 'post_name' => sanitize_title($label), |
|
| 402 | 402 | ); |
| 403 | 403 | |
| 404 | 404 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -407,18 +407,18 @@ discard block |
||
| 407 | 407 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 408 | 408 | // is created when saving a post. |
| 409 | 409 | global $wpseo_metabox, $seo_ultimate; |
| 410 | - if ( isset( $wpseo_metabox ) ) { |
|
| 411 | - remove_action( 'wp_insert_post', array( |
|
| 410 | + if (isset($wpseo_metabox)) { |
|
| 411 | + remove_action('wp_insert_post', array( |
|
| 412 | 412 | $wpseo_metabox, |
| 413 | 413 | 'save_postdata', |
| 414 | - ) ); |
|
| 414 | + )); |
|
| 415 | 415 | } |
| 416 | 416 | |
| 417 | - if ( isset( $seo_ultimate ) ) { |
|
| 418 | - remove_action( 'save_post', array( |
|
| 417 | + if (isset($seo_ultimate)) { |
|
| 418 | + remove_action('save_post', array( |
|
| 419 | 419 | $seo_ultimate, |
| 420 | 420 | 'save_postmeta_box', |
| 421 | - ) ); |
|
| 421 | + )); |
|
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -428,95 +428,95 @@ discard block |
||
| 428 | 428 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 429 | 429 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 430 | 430 | global $wp_filter; |
| 431 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 432 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 431 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 432 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 433 | 433 | |
| 434 | 434 | |
| 435 | - $log->trace( 'Going to insert new post...' ); |
|
| 435 | + $log->trace('Going to insert new post...'); |
|
| 436 | 436 | |
| 437 | 437 | // create or update the post. |
| 438 | - $post_id = wp_insert_post( $params, true ); |
|
| 438 | + $post_id = wp_insert_post($params, true); |
|
| 439 | 439 | |
| 440 | 440 | // Setting the alternative labels for this entity. |
| 441 | 441 | Wordlift_Entity_Service::get_instance() |
| 442 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 442 | + ->set_alternative_labels($post_id, $synonyms); |
|
| 443 | 443 | |
| 444 | 444 | // Restore all the existing filters. |
| 445 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 445 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 446 | 446 | |
| 447 | 447 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 448 | - if ( isset( $wpseo_metabox ) ) { |
|
| 449 | - add_action( 'wp_insert_post', array( |
|
| 448 | + if (isset($wpseo_metabox)) { |
|
| 449 | + add_action('wp_insert_post', array( |
|
| 450 | 450 | $wpseo_metabox, |
| 451 | 451 | 'save_postdata', |
| 452 | - ) ); |
|
| 452 | + )); |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 456 | - if ( isset( $seo_ultimate ) ) { |
|
| 457 | - add_action( 'save_post', array( |
|
| 456 | + if (isset($seo_ultimate)) { |
|
| 457 | + add_action('save_post', array( |
|
| 458 | 458 | $seo_ultimate, |
| 459 | 459 | 'save_postmeta_box', |
| 460 | - ), 10, 2 ); |
|
| 460 | + ), 10, 2); |
|
| 461 | 461 | } |
| 462 | 462 | |
| 463 | 463 | // TODO: handle errors. |
| 464 | - if ( is_wp_error( $post_id ) ) { |
|
| 465 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 464 | + if (is_wp_error($post_id)) { |
|
| 465 | + $log->error('An error occurred: '.$post_id->get_error_message()); |
|
| 466 | 466 | |
| 467 | 467 | // inform an error occurred. |
| 468 | 468 | return null; |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 471 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 472 | 472 | |
| 473 | 473 | // Save the entity types. |
| 474 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 474 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 475 | 475 | |
| 476 | 476 | // Get a dataset URI for the entity. |
| 477 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 477 | + $wl_uri = wl_build_entity_uri($post_id); |
|
| 478 | 478 | |
| 479 | 479 | // Save the entity URI. |
| 480 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 480 | + wl_set_entity_uri($post_id, $wl_uri); |
|
| 481 | 481 | |
| 482 | 482 | // Add the uri to the sameAs data if it's not a local URI. |
| 483 | - if ( $wl_uri !== $uri ) { |
|
| 484 | - array_push( $same_as, $uri ); |
|
| 483 | + if ($wl_uri !== $uri) { |
|
| 484 | + array_push($same_as, $uri); |
|
| 485 | 485 | } |
| 486 | 486 | |
| 487 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 487 | + $new_uri = wl_get_entity_uri($post_id); |
|
| 488 | 488 | |
| 489 | 489 | // Save the sameAs data for the entity. |
| 490 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 490 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 491 | 491 | |
| 492 | 492 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 493 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 494 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 493 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 494 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | // Call hooks. |
| 498 | - do_action( 'wl_save_entity', $post_id ); |
|
| 498 | + do_action('wl_save_entity', $post_id); |
|
| 499 | 499 | |
| 500 | - foreach ( $images as $image_remote_url ) { |
|
| 500 | + foreach ($images as $image_remote_url) { |
|
| 501 | 501 | |
| 502 | 502 | // Check if image is already present in local DB |
| 503 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 503 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 504 | 504 | // Do nothing. |
| 505 | 505 | continue; |
| 506 | 506 | } |
| 507 | 507 | |
| 508 | 508 | // Check if there is an existing attachment for this post ID and source URL. |
| 509 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 509 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 510 | 510 | |
| 511 | 511 | // Skip if an existing image is found. |
| 512 | - if ( null !== $existing_image ) { |
|
| 512 | + if (null !== $existing_image) { |
|
| 513 | 513 | continue; |
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | // Save the image and get the local path. |
| 517 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 517 | + $image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url); |
|
| 518 | 518 | |
| 519 | - if ( false === $image ) { |
|
| 519 | + if (false === $image) { |
|
| 520 | 520 | continue; |
| 521 | 521 | } |
| 522 | 522 | |
@@ -536,24 +536,24 @@ discard block |
||
| 536 | 536 | ); |
| 537 | 537 | |
| 538 | 538 | // Create the attachment in WordPress and generate the related metadata. |
| 539 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 539 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 540 | 540 | |
| 541 | 541 | // Set the source URL for the image. |
| 542 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 542 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 543 | 543 | |
| 544 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 545 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 544 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 545 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 546 | 546 | |
| 547 | 547 | // Set it as the featured image. |
| 548 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 548 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 552 | 552 | // save the entity in the triple store. |
| 553 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 553 | + Wordlift_Linked_Data_Service::get_instance()->push($post_id); |
|
| 554 | 554 | |
| 555 | 555 | // finally return the entity post. |
| 556 | - return get_post( $post_id ); |
|
| 556 | + return get_post($post_id); |
|
| 557 | 557 | } |
| 558 | 558 | |
| 559 | 559 | /** |
@@ -565,10 +565,10 @@ discard block |
||
| 565 | 565 | * |
| 566 | 566 | * @return array An array of entity posts. |
| 567 | 567 | */ |
| 568 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 568 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 569 | 569 | |
| 570 | 570 | // Remove quote escapes. |
| 571 | - $content = str_replace( '\\"', '"', $content ); |
|
| 571 | + $content = str_replace('\\"', '"', $content); |
|
| 572 | 572 | |
| 573 | 573 | // Match all itemid attributes. |
| 574 | 574 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -579,8 +579,8 @@ discard block |
||
| 579 | 579 | $matches = array(); |
| 580 | 580 | |
| 581 | 581 | // In case of errors, return an empty array. |
| 582 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 583 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 582 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 583 | + wl_write_log("Found no entities embedded in content"); |
|
| 584 | 584 | |
| 585 | 585 | return array(); |
| 586 | 586 | } |
@@ -589,13 +589,13 @@ discard block |
||
| 589 | 589 | |
| 590 | 590 | // Collect the entities. |
| 591 | 591 | $entities = array(); |
| 592 | - foreach ( $matches[1] as $uri ) { |
|
| 593 | - $uri_d = html_entity_decode( $uri ); |
|
| 592 | + foreach ($matches[1] as $uri) { |
|
| 593 | + $uri_d = html_entity_decode($uri); |
|
| 594 | 594 | |
| 595 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 595 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 596 | 596 | |
| 597 | - if ( null !== $entity ) { |
|
| 598 | - array_push( $entities, $entity->ID ); |
|
| 597 | + if (null !== $entity) { |
|
| 598 | + array_push($entities, $entity->ID); |
|
| 599 | 599 | } |
| 600 | 600 | } |
| 601 | 601 | |