@@ -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,111 +63,111 @@ 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()['meta']; |
|
| 83 | + $metas = $request->get_json_params()['meta']; |
|
| 84 | 84 | |
| 85 | - // Ignore if no change in meta |
|
| 86 | - if ( empty($metas) ) { |
|
| 87 | - $log->trace( 'No meta data, skipping...' ); |
|
| 88 | - return; |
|
| 89 | - } |
|
| 85 | + // Ignore if no change in meta |
|
| 86 | + if ( empty($metas) ) { |
|
| 87 | + $log->trace( 'No meta data, skipping...' ); |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - $entities_via_meta = json_decode($metas['wl_entities_gutenberg'], true); |
|
| 91 | + $entities_via_meta = json_decode($metas['wl_entities_gutenberg'], true); |
|
| 92 | 92 | |
| 93 | - //file_put_contents( WP_PLUGIN_DIR . '/wordlift/debug.txt', serialize($entities_via_meta) ); |
|
| 93 | + //file_put_contents( WP_PLUGIN_DIR . '/wordlift/debug.txt', serialize($entities_via_meta) ); |
|
| 94 | 94 | |
| 95 | - foreach ( $entities_via_meta as $entity_uri => $entity ) { |
|
| 95 | + foreach ( $entities_via_meta as $entity_uri => $entity ) { |
|
| 96 | 96 | |
| 97 | - // Only if the current entity is created from scratch let's avoid to |
|
| 98 | - // create more than one entity with same label & entity type. |
|
| 99 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 100 | - $entity['main_type'] : null; |
|
| 97 | + // Only if the current entity is created from scratch let's avoid to |
|
| 98 | + // create more than one entity with same label & entity type. |
|
| 99 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 100 | + $entity['main_type'] : null; |
|
| 101 | 101 | |
| 102 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 103 | - // 1. when $entity_uri is an internal uri |
|
| 104 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 105 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 102 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 103 | + // 1. when $entity_uri is an internal uri |
|
| 104 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 105 | + $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 106 | 106 | |
| 107 | - // Detect the uri depending if is an existing or a new entity |
|
| 108 | - $uri = ( null === $ie ) ? |
|
| 109 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 110 | - $entity['label'], |
|
| 111 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 112 | - $entity_type |
|
| 113 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 107 | + // Detect the uri depending if is an existing or a new entity |
|
| 108 | + $uri = ( null === $ie ) ? |
|
| 109 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 110 | + $entity['label'], |
|
| 111 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 112 | + $entity_type |
|
| 113 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 114 | 114 | |
| 115 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 116 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 115 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 116 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 117 | 117 | |
| 118 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 119 | - // These uris need to be rewritten here and replaced in the content |
|
| 120 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 121 | - // Override the entity obj |
|
| 122 | - $entity['uri'] = $uri; |
|
| 123 | - } |
|
| 118 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 119 | + // These uris need to be rewritten here and replaced in the content |
|
| 120 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 121 | + // Override the entity obj |
|
| 122 | + $entity['uri'] = $uri; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - // Update entity data with related post |
|
| 126 | - $entity['related_post_id'] = $post_id; |
|
| 125 | + // Update entity data with related post |
|
| 126 | + $entity['related_post_id'] = $post_id; |
|
| 127 | 127 | |
| 128 | - // Save the entity if is a new entity |
|
| 129 | - if ( null === $ie ) { |
|
| 130 | - wl_save_entity( $entity ); |
|
| 131 | - } |
|
| 128 | + // Save the entity if is a new entity |
|
| 129 | + if ( null === $ie ) { |
|
| 130 | + wl_save_entity( $entity ); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - } |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - // Replace tmp uris in content post if needed |
|
| 136 | - $updated_post_content = $post->post_content; |
|
| 135 | + // Replace tmp uris in content post if needed |
|
| 136 | + $updated_post_content = $post->post_content; |
|
| 137 | 137 | |
| 138 | - // Update the post content if we found mappings of new entities. |
|
| 139 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 140 | - // Save each entity and store the post id. |
|
| 141 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 142 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 143 | - } |
|
| 138 | + // Update the post content if we found mappings of new entities. |
|
| 139 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 140 | + // Save each entity and store the post id. |
|
| 141 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 142 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - // Update the post content. |
|
| 146 | - wp_update_post( array( |
|
| 147 | - 'ID' => $post->ID, |
|
| 148 | - 'post_content' => $updated_post_content, |
|
| 149 | - ) ); |
|
| 150 | - } |
|
| 145 | + // Update the post content. |
|
| 146 | + wp_update_post( array( |
|
| 147 | + 'ID' => $post->ID, |
|
| 148 | + 'post_content' => $updated_post_content, |
|
| 149 | + ) ); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - // Extract related/referenced entities from text. |
|
| 153 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 152 | + // Extract related/referenced entities from text. |
|
| 153 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 154 | 154 | |
| 155 | - // Reset previously saved instances. |
|
| 156 | - wl_core_delete_relation_instances( $post_id ); |
|
| 155 | + // Reset previously saved instances. |
|
| 156 | + wl_core_delete_relation_instances( $post_id ); |
|
| 157 | 157 | |
| 158 | - // Save relation instances |
|
| 159 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 158 | + // Save relation instances |
|
| 159 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 160 | 160 | |
| 161 | - wl_core_add_relation_instance( |
|
| 162 | - $post_id, |
|
| 163 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 164 | - $referenced_entity_id |
|
| 165 | - ); |
|
| 161 | + wl_core_add_relation_instance( |
|
| 162 | + $post_id, |
|
| 163 | + $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 164 | + $referenced_entity_id |
|
| 165 | + ); |
|
| 166 | 166 | |
| 167 | - } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - // Push the post to Redlink. |
|
| 170 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 169 | + // Push the post to Redlink. |
|
| 170 | + Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -179,156 +179,156 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 181 | 181 | |
| 182 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 182 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 183 | 183 | |
| 184 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 184 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 185 | 185 | |
| 186 | - // Ignore auto-saves |
|
| 187 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 188 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 186 | + // Ignore auto-saves |
|
| 187 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 188 | + $log->trace( 'Doing autosave, skipping...' ); |
|
| 189 | 189 | |
| 190 | - return; |
|
| 191 | - } |
|
| 190 | + return; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - // get the current post. |
|
| 194 | - $post = get_post( $post_id ); |
|
| 193 | + // get the current post. |
|
| 194 | + $post = get_post( $post_id ); |
|
| 195 | 195 | |
| 196 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 196 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 197 | 197 | |
| 198 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 198 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 199 | 199 | |
| 200 | - // Get the entity service instance. |
|
| 201 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 200 | + // Get the entity service instance. |
|
| 201 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 202 | 202 | |
| 203 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 204 | - $entities_uri_mapping = array(); |
|
| 203 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 204 | + $entities_uri_mapping = array(); |
|
| 205 | 205 | |
| 206 | - // Save the entities coming with POST data. |
|
| 207 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 206 | + // Save the entities coming with POST data. |
|
| 207 | + if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 208 | 208 | |
| 209 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 210 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 211 | - wl_write_log( "]" ); |
|
| 212 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 213 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 214 | - wl_write_log( "]" ); |
|
| 209 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 210 | + wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 211 | + wl_write_log( "]" ); |
|
| 212 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 213 | + wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 214 | + wl_write_log( "]" ); |
|
| 215 | 215 | |
| 216 | - $entities_via_post = $_POST['wl_entities']; |
|
| 217 | - $boxes_via_post = $_POST['wl_boxes']; |
|
| 216 | + $entities_via_post = $_POST['wl_entities']; |
|
| 217 | + $boxes_via_post = $_POST['wl_boxes']; |
|
| 218 | 218 | |
| 219 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 219 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 220 | 220 | |
| 221 | - // Only if the current entity is created from scratch let's avoid to |
|
| 222 | - // create more than one entity with same label & entity type. |
|
| 223 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 224 | - $entity['main_type'] : null; |
|
| 221 | + // Only if the current entity is created from scratch let's avoid to |
|
| 222 | + // create more than one entity with same label & entity type. |
|
| 223 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 224 | + $entity['main_type'] : null; |
|
| 225 | 225 | |
| 226 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 227 | - // 1. when $entity_uri is an internal uri |
|
| 228 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 229 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 226 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 227 | + // 1. when $entity_uri is an internal uri |
|
| 228 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 229 | + $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 230 | 230 | |
| 231 | - // Detect the uri depending if is an existing or a new entity |
|
| 232 | - $uri = ( null === $ie ) ? |
|
| 233 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 234 | - $entity['label'], |
|
| 235 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 236 | - $entity_type |
|
| 237 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 231 | + // Detect the uri depending if is an existing or a new entity |
|
| 232 | + $uri = ( null === $ie ) ? |
|
| 233 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 234 | + $entity['label'], |
|
| 235 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 236 | + $entity_type |
|
| 237 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 238 | 238 | |
| 239 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 240 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 239 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 240 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 241 | 241 | |
| 242 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 243 | - // These uris need to be rewritten here and replaced in the content |
|
| 244 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 245 | - // Override the entity obj |
|
| 246 | - $entity['uri'] = $uri; |
|
| 247 | - } |
|
| 242 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 243 | + // These uris need to be rewritten here and replaced in the content |
|
| 244 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 245 | + // Override the entity obj |
|
| 246 | + $entity['uri'] = $uri; |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - // Update entity data with related post |
|
| 250 | - $entity['related_post_id'] = $post_id; |
|
| 249 | + // Update entity data with related post |
|
| 250 | + $entity['related_post_id'] = $post_id; |
|
| 251 | 251 | |
| 252 | - // Save the entity if is a new entity |
|
| 253 | - if ( null === $ie ) { |
|
| 254 | - wl_save_entity( $entity ); |
|
| 255 | - } |
|
| 252 | + // Save the entity if is a new entity |
|
| 253 | + if ( null === $ie ) { |
|
| 254 | + wl_save_entity( $entity ); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - } |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - } |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - // Replace tmp uris in content post if needed |
|
| 262 | - $updated_post_content = $post->post_content; |
|
| 261 | + // Replace tmp uris in content post if needed |
|
| 262 | + $updated_post_content = $post->post_content; |
|
| 263 | 263 | |
| 264 | - // Update the post content if we found mappings of new entities. |
|
| 265 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 266 | - // Save each entity and store the post id. |
|
| 267 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 268 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 269 | - } |
|
| 264 | + // Update the post content if we found mappings of new entities. |
|
| 265 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 266 | + // Save each entity and store the post id. |
|
| 267 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 268 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - // Update the post content. |
|
| 272 | - wp_update_post( array( |
|
| 273 | - 'ID' => $post->ID, |
|
| 274 | - 'post_content' => $updated_post_content, |
|
| 275 | - ) ); |
|
| 276 | - } |
|
| 271 | + // Update the post content. |
|
| 272 | + wp_update_post( array( |
|
| 273 | + 'ID' => $post->ID, |
|
| 274 | + 'post_content' => $updated_post_content, |
|
| 275 | + ) ); |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | - // Extract related/referenced entities from text. |
|
| 279 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 278 | + // Extract related/referenced entities from text. |
|
| 279 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 280 | 280 | |
| 281 | - // Reset previously saved instances. |
|
| 282 | - wl_core_delete_relation_instances( $post_id ); |
|
| 281 | + // Reset previously saved instances. |
|
| 282 | + wl_core_delete_relation_instances( $post_id ); |
|
| 283 | 283 | |
| 284 | - // Save relation instances |
|
| 285 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 284 | + // Save relation instances |
|
| 285 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 286 | 286 | |
| 287 | - wl_core_add_relation_instance( |
|
| 288 | - $post_id, |
|
| 289 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 290 | - $referenced_entity_id |
|
| 291 | - ); |
|
| 287 | + wl_core_add_relation_instance( |
|
| 288 | + $post_id, |
|
| 289 | + $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 290 | + $referenced_entity_id |
|
| 291 | + ); |
|
| 292 | 292 | |
| 293 | - } |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 296 | - // Save post metadata if available |
|
| 297 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 298 | - $_POST['wl_metadata'] : array(); |
|
| 299 | - |
|
| 300 | - $fields = array( |
|
| 301 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 302 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 303 | - ); |
|
| 304 | - |
|
| 305 | - // Unlink topic taxonomy terms |
|
| 306 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 307 | - |
|
| 308 | - foreach ( $fields as $field ) { |
|
| 309 | - |
|
| 310 | - // Delete current values |
|
| 311 | - delete_post_meta( $post->ID, $field ); |
|
| 312 | - // Retrieve the entity uri |
|
| 313 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 314 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 315 | - |
|
| 316 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 317 | - |
|
| 318 | - if ( $entity ) { |
|
| 319 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 320 | - // Set also the topic taxonomy |
|
| 321 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 322 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - // Push the post to Redlink. |
|
| 329 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 330 | - |
|
| 331 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 295 | + if ( isset( $_POST['wl_entities'] ) ) { |
|
| 296 | + // Save post metadata if available |
|
| 297 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 298 | + $_POST['wl_metadata'] : array(); |
|
| 299 | + |
|
| 300 | + $fields = array( |
|
| 301 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 302 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 303 | + ); |
|
| 304 | + |
|
| 305 | + // Unlink topic taxonomy terms |
|
| 306 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 307 | + |
|
| 308 | + foreach ( $fields as $field ) { |
|
| 309 | + |
|
| 310 | + // Delete current values |
|
| 311 | + delete_post_meta( $post->ID, $field ); |
|
| 312 | + // Retrieve the entity uri |
|
| 313 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 314 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 315 | + |
|
| 316 | + $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 317 | + |
|
| 318 | + if ( $entity ) { |
|
| 319 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 320 | + // Set also the topic taxonomy |
|
| 321 | + if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 322 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + // Push the post to Redlink. |
|
| 329 | + Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 330 | + |
|
| 331 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -351,208 +351,208 @@ discard block |
||
| 351 | 351 | */ |
| 352 | 352 | function wl_save_entity( $entity_data ) { |
| 353 | 353 | |
| 354 | - // Required for REST API calls |
|
| 355 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 356 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 357 | - } |
|
| 354 | + // Required for REST API calls |
|
| 355 | + if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 356 | + require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 359 | + $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 360 | 360 | |
| 361 | - $uri = $entity_data['uri']; |
|
| 362 | - /* |
|
| 361 | + $uri = $entity_data['uri']; |
|
| 362 | + /* |
|
| 363 | 363 | * Data is coming from a $_POST, sanitize it. |
| 364 | 364 | * |
| 365 | 365 | * @since 3.19.4 |
| 366 | 366 | * |
| 367 | 367 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 368 | 368 | */ |
| 369 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 370 | - $type_uri = $entity_data['main_type']; |
|
| 371 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 372 | - $description = $entity_data['description']; |
|
| 373 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 374 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 375 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 376 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 377 | - |
|
| 378 | - // Get the synonyms. |
|
| 379 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 380 | - |
|
| 381 | - // Check whether an entity already exists with the provided URI. |
|
| 382 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 383 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 384 | - |
|
| 385 | - return $post; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - // Prepare properties of the new entity. |
|
| 389 | - $params = array( |
|
| 390 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 391 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 392 | - 'post_title' => $label, |
|
| 393 | - 'post_content' => $description, |
|
| 394 | - 'post_excerpt' => '', |
|
| 395 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 396 | - // entity with a post_name already set, since this call is made only for |
|
| 397 | - // new entities. |
|
| 398 | - // |
|
| 399 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 400 | - 'post_name' => sanitize_title( $label ), |
|
| 401 | - ); |
|
| 402 | - |
|
| 403 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 404 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 405 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 406 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 407 | - // is created when saving a post. |
|
| 408 | - global $wpseo_metabox, $seo_ultimate; |
|
| 409 | - if ( isset( $wpseo_metabox ) ) { |
|
| 410 | - remove_action( 'wp_insert_post', array( |
|
| 411 | - $wpseo_metabox, |
|
| 412 | - 'save_postdata', |
|
| 413 | - ) ); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - if ( isset( $seo_ultimate ) ) { |
|
| 417 | - remove_action( 'save_post', array( |
|
| 418 | - $seo_ultimate, |
|
| 419 | - 'save_postmeta_box', |
|
| 420 | - ) ); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 424 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 425 | - // to the save_post and restore them after we saved the entity. |
|
| 426 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 427 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 428 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 429 | - global $wp_filter; |
|
| 430 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 431 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 432 | - |
|
| 433 | - |
|
| 434 | - $log->trace( 'Going to insert new post...' ); |
|
| 435 | - |
|
| 436 | - // create or update the post. |
|
| 437 | - $post_id = wp_insert_post( $params, true ); |
|
| 438 | - |
|
| 439 | - // Setting the alternative labels for this entity. |
|
| 440 | - Wordlift_Entity_Service::get_instance() |
|
| 441 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 442 | - |
|
| 443 | - // Restore all the existing filters. |
|
| 444 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 445 | - |
|
| 446 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 447 | - if ( isset( $wpseo_metabox ) ) { |
|
| 448 | - add_action( 'wp_insert_post', array( |
|
| 449 | - $wpseo_metabox, |
|
| 450 | - 'save_postdata', |
|
| 451 | - ) ); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 455 | - if ( isset( $seo_ultimate ) ) { |
|
| 456 | - add_action( 'save_post', array( |
|
| 457 | - $seo_ultimate, |
|
| 458 | - 'save_postmeta_box', |
|
| 459 | - ), 10, 2 ); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - // TODO: handle errors. |
|
| 463 | - if ( is_wp_error( $post_id ) ) { |
|
| 464 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 465 | - |
|
| 466 | - // inform an error occurred. |
|
| 467 | - return null; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 471 | - |
|
| 472 | - // Save the entity types. |
|
| 473 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 474 | - |
|
| 475 | - // Get a dataset URI for the entity. |
|
| 476 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 477 | - |
|
| 478 | - // Save the entity URI. |
|
| 479 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 480 | - |
|
| 481 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 482 | - if ( $wl_uri !== $uri ) { |
|
| 483 | - array_push( $same_as, $uri ); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 487 | - |
|
| 488 | - // Save the sameAs data for the entity. |
|
| 489 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 490 | - |
|
| 491 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 492 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 493 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - // Call hooks. |
|
| 497 | - do_action( 'wl_save_entity', $post_id ); |
|
| 498 | - |
|
| 499 | - foreach ( $images as $image_remote_url ) { |
|
| 500 | - |
|
| 501 | - // Check if image is already present in local DB |
|
| 502 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 503 | - // Do nothing. |
|
| 504 | - continue; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 508 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 509 | - |
|
| 510 | - // Skip if an existing image is found. |
|
| 511 | - if ( null !== $existing_image ) { |
|
| 512 | - continue; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - // Save the image and get the local path. |
|
| 516 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 517 | - |
|
| 518 | - if ( false === $image ) { |
|
| 519 | - continue; |
|
| 520 | - } |
|
| 369 | + $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 370 | + $type_uri = $entity_data['main_type']; |
|
| 371 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 372 | + $description = $entity_data['description']; |
|
| 373 | + $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 374 | + $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 375 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 376 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 377 | + |
|
| 378 | + // Get the synonyms. |
|
| 379 | + $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 380 | + |
|
| 381 | + // Check whether an entity already exists with the provided URI. |
|
| 382 | + if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 383 | + $log->debug( "Post already exists for URI $uri." ); |
|
| 384 | + |
|
| 385 | + return $post; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + // Prepare properties of the new entity. |
|
| 389 | + $params = array( |
|
| 390 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 391 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 392 | + 'post_title' => $label, |
|
| 393 | + 'post_content' => $description, |
|
| 394 | + 'post_excerpt' => '', |
|
| 395 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 396 | + // entity with a post_name already set, since this call is made only for |
|
| 397 | + // new entities. |
|
| 398 | + // |
|
| 399 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 400 | + 'post_name' => sanitize_title( $label ), |
|
| 401 | + ); |
|
| 402 | + |
|
| 403 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 404 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 405 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 406 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 407 | + // is created when saving a post. |
|
| 408 | + global $wpseo_metabox, $seo_ultimate; |
|
| 409 | + if ( isset( $wpseo_metabox ) ) { |
|
| 410 | + remove_action( 'wp_insert_post', array( |
|
| 411 | + $wpseo_metabox, |
|
| 412 | + 'save_postdata', |
|
| 413 | + ) ); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + if ( isset( $seo_ultimate ) ) { |
|
| 417 | + remove_action( 'save_post', array( |
|
| 418 | + $seo_ultimate, |
|
| 419 | + 'save_postmeta_box', |
|
| 420 | + ) ); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 424 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 425 | + // to the save_post and restore them after we saved the entity. |
|
| 426 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 427 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 428 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 429 | + global $wp_filter; |
|
| 430 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 431 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 432 | + |
|
| 433 | + |
|
| 434 | + $log->trace( 'Going to insert new post...' ); |
|
| 435 | + |
|
| 436 | + // create or update the post. |
|
| 437 | + $post_id = wp_insert_post( $params, true ); |
|
| 438 | + |
|
| 439 | + // Setting the alternative labels for this entity. |
|
| 440 | + Wordlift_Entity_Service::get_instance() |
|
| 441 | + ->set_alternative_labels( $post_id, $synonyms ); |
|
| 442 | + |
|
| 443 | + // Restore all the existing filters. |
|
| 444 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 445 | + |
|
| 446 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 447 | + if ( isset( $wpseo_metabox ) ) { |
|
| 448 | + add_action( 'wp_insert_post', array( |
|
| 449 | + $wpseo_metabox, |
|
| 450 | + 'save_postdata', |
|
| 451 | + ) ); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 455 | + if ( isset( $seo_ultimate ) ) { |
|
| 456 | + add_action( 'save_post', array( |
|
| 457 | + $seo_ultimate, |
|
| 458 | + 'save_postmeta_box', |
|
| 459 | + ), 10, 2 ); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + // TODO: handle errors. |
|
| 463 | + if ( is_wp_error( $post_id ) ) { |
|
| 464 | + $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 465 | + |
|
| 466 | + // inform an error occurred. |
|
| 467 | + return null; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 471 | + |
|
| 472 | + // Save the entity types. |
|
| 473 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 474 | + |
|
| 475 | + // Get a dataset URI for the entity. |
|
| 476 | + $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 477 | + |
|
| 478 | + // Save the entity URI. |
|
| 479 | + wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 480 | + |
|
| 481 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 482 | + if ( $wl_uri !== $uri ) { |
|
| 483 | + array_push( $same_as, $uri ); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + $new_uri = wl_get_entity_uri( $post_id ); |
|
| 487 | + |
|
| 488 | + // Save the sameAs data for the entity. |
|
| 489 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 490 | + |
|
| 491 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 492 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 493 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + // Call hooks. |
|
| 497 | + do_action( 'wl_save_entity', $post_id ); |
|
| 498 | + |
|
| 499 | + foreach ( $images as $image_remote_url ) { |
|
| 500 | + |
|
| 501 | + // Check if image is already present in local DB |
|
| 502 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 503 | + // Do nothing. |
|
| 504 | + continue; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 508 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 509 | + |
|
| 510 | + // Skip if an existing image is found. |
|
| 511 | + if ( null !== $existing_image ) { |
|
| 512 | + continue; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + // Save the image and get the local path. |
|
| 516 | + $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 517 | + |
|
| 518 | + if ( false === $image ) { |
|
| 519 | + continue; |
|
| 520 | + } |
|
| 521 | 521 | |
| 522 | - // Get the local URL. |
|
| 523 | - $filename = $image['path']; |
|
| 524 | - $url = $image['url']; |
|
| 525 | - $content_type = $image['content_type']; |
|
| 522 | + // Get the local URL. |
|
| 523 | + $filename = $image['path']; |
|
| 524 | + $url = $image['url']; |
|
| 525 | + $content_type = $image['content_type']; |
|
| 526 | 526 | |
| 527 | - $attachment = array( |
|
| 528 | - 'guid' => $url, |
|
| 529 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 530 | - 'post_title' => $label, |
|
| 531 | - // Set the title to the post title. |
|
| 532 | - 'post_content' => '', |
|
| 533 | - 'post_status' => 'inherit', |
|
| 534 | - 'post_mime_type' => $content_type, |
|
| 535 | - ); |
|
| 536 | - |
|
| 537 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 538 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 539 | - |
|
| 540 | - // Set the source URL for the image. |
|
| 541 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 527 | + $attachment = array( |
|
| 528 | + 'guid' => $url, |
|
| 529 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 530 | + 'post_title' => $label, |
|
| 531 | + // Set the title to the post title. |
|
| 532 | + 'post_content' => '', |
|
| 533 | + 'post_status' => 'inherit', |
|
| 534 | + 'post_mime_type' => $content_type, |
|
| 535 | + ); |
|
| 536 | + |
|
| 537 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 538 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 539 | + |
|
| 540 | + // Set the source URL for the image. |
|
| 541 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 542 | 542 | |
| 543 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 544 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 543 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 544 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 545 | 545 | |
| 546 | - // Set it as the featured image. |
|
| 547 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 548 | - } |
|
| 546 | + // Set it as the featured image. |
|
| 547 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 548 | + } |
|
| 549 | 549 | |
| 550 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 551 | - // save the entity in the triple store. |
|
| 552 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 550 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 551 | + // save the entity in the triple store. |
|
| 552 | + Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 553 | 553 | |
| 554 | - // finally return the entity post. |
|
| 555 | - return get_post( $post_id ); |
|
| 554 | + // finally return the entity post. |
|
| 555 | + return get_post( $post_id ); |
|
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | /** |
@@ -566,40 +566,40 @@ discard block |
||
| 566 | 566 | */ |
| 567 | 567 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 568 | 568 | |
| 569 | - // Remove quote escapes. |
|
| 570 | - $content = str_replace( '\\"', '"', $content ); |
|
| 569 | + // Remove quote escapes. |
|
| 570 | + $content = str_replace( '\\"', '"', $content ); |
|
| 571 | 571 | |
| 572 | - // Match all itemid attributes. |
|
| 573 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 572 | + // Match all itemid attributes. |
|
| 573 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 574 | 574 | |
| 575 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 575 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 576 | 576 | |
| 577 | - // Remove the pattern while it is found (match nested annotations). |
|
| 578 | - $matches = array(); |
|
| 577 | + // Remove the pattern while it is found (match nested annotations). |
|
| 578 | + $matches = array(); |
|
| 579 | 579 | |
| 580 | - // In case of errors, return an empty array. |
|
| 581 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 582 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 580 | + // In case of errors, return an empty array. |
|
| 581 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 582 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 583 | 583 | |
| 584 | - return array(); |
|
| 585 | - } |
|
| 584 | + return array(); |
|
| 585 | + } |
|
| 586 | 586 | |
| 587 | 587 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 588 | 588 | |
| 589 | - // Collect the entities. |
|
| 590 | - $entities = array(); |
|
| 591 | - foreach ( $matches[1] as $uri ) { |
|
| 592 | - $uri_d = html_entity_decode( $uri ); |
|
| 589 | + // Collect the entities. |
|
| 590 | + $entities = array(); |
|
| 591 | + foreach ( $matches[1] as $uri ) { |
|
| 592 | + $uri_d = html_entity_decode( $uri ); |
|
| 593 | 593 | |
| 594 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 594 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 595 | 595 | |
| 596 | - if ( null !== $entity ) { |
|
| 597 | - array_push( $entities, $entity->ID ); |
|
| 598 | - } |
|
| 599 | - } |
|
| 596 | + if ( null !== $entity ) { |
|
| 597 | + array_push( $entities, $entity->ID ); |
|
| 598 | + } |
|
| 599 | + } |
|
| 600 | 600 | |
| 601 | - // $count = sizeof( $entities ); |
|
| 602 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 601 | + // $count = sizeof( $entities ); |
|
| 602 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 603 | 603 | |
| 604 | - return $entities; |
|
| 604 | + return $entities; |
|
| 605 | 605 | } |
@@ -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 | |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | $metas = $request->get_json_params()['meta']; |
| 84 | 84 | |
| 85 | 85 | // Ignore if no change in meta |
| 86 | - if ( empty($metas) ) { |
|
| 87 | - $log->trace( 'No meta data, skipping...' ); |
|
| 86 | + if (empty($metas)) { |
|
| 87 | + $log->trace('No meta data, skipping...'); |
|
| 88 | 88 | return; |
| 89 | 89 | } |
| 90 | 90 | |
@@ -92,32 +92,32 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | //file_put_contents( WP_PLUGIN_DIR . '/wordlift/debug.txt', serialize($entities_via_meta) ); |
| 94 | 94 | |
| 95 | - foreach ( $entities_via_meta as $entity_uri => $entity ) { |
|
| 95 | + foreach ($entities_via_meta as $entity_uri => $entity) { |
|
| 96 | 96 | |
| 97 | 97 | // Only if the current entity is created from scratch let's avoid to |
| 98 | 98 | // create more than one entity with same label & entity type. |
| 99 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 99 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 100 | 100 | $entity['main_type'] : null; |
| 101 | 101 | |
| 102 | 102 | // Look if current entity uri matches an internal existing entity, meaning: |
| 103 | 103 | // 1. when $entity_uri is an internal uri |
| 104 | 104 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 105 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 105 | + $ie = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 106 | 106 | |
| 107 | 107 | // Detect the uri depending if is an existing or a new entity |
| 108 | - $uri = ( null === $ie ) ? |
|
| 108 | + $uri = (null === $ie) ? |
|
| 109 | 109 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 110 | 110 | $entity['label'], |
| 111 | 111 | Wordlift_Entity_Service::TYPE_NAME, |
| 112 | 112 | $entity_type |
| 113 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 113 | + ) : wl_get_entity_uri($ie->ID); |
|
| 114 | 114 | |
| 115 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 116 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 115 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 116 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 117 | 117 | |
| 118 | 118 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 119 | 119 | // These uris need to be rewritten here and replaced in the content |
| 120 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 120 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 121 | 121 | // Override the entity obj |
| 122 | 122 | $entity['uri'] = $uri; |
| 123 | 123 | } |
@@ -126,8 +126,8 @@ discard block |
||
| 126 | 126 | $entity['related_post_id'] = $post_id; |
| 127 | 127 | |
| 128 | 128 | // Save the entity if is a new entity |
| 129 | - if ( null === $ie ) { |
|
| 130 | - wl_save_entity( $entity ); |
|
| 129 | + if (null === $ie) { |
|
| 130 | + wl_save_entity($entity); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | } |
@@ -136,38 +136,38 @@ discard block |
||
| 136 | 136 | $updated_post_content = $post->post_content; |
| 137 | 137 | |
| 138 | 138 | // Update the post content if we found mappings of new entities. |
| 139 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 139 | + if ( ! empty($entities_uri_mapping)) { |
|
| 140 | 140 | // Save each entity and store the post id. |
| 141 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 142 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 141 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 142 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | // Update the post content. |
| 146 | - wp_update_post( array( |
|
| 146 | + wp_update_post(array( |
|
| 147 | 147 | 'ID' => $post->ID, |
| 148 | 148 | 'post_content' => $updated_post_content, |
| 149 | - ) ); |
|
| 149 | + )); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // Extract related/referenced entities from text. |
| 153 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 153 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 154 | 154 | |
| 155 | 155 | // Reset previously saved instances. |
| 156 | - wl_core_delete_relation_instances( $post_id ); |
|
| 156 | + wl_core_delete_relation_instances($post_id); |
|
| 157 | 157 | |
| 158 | 158 | // Save relation instances |
| 159 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 159 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 160 | 160 | |
| 161 | 161 | wl_core_add_relation_instance( |
| 162 | 162 | $post_id, |
| 163 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 163 | + $entity_service->get_classification_scope_for($referenced_entity_id), |
|
| 164 | 164 | $referenced_entity_id |
| 165 | 165 | ); |
| 166 | 166 | |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | // Push the post to Redlink. |
| 170 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 170 | + Wordlift_Linked_Data_Service::get_instance()->push($post->ID); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -177,23 +177,23 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @param int $post_id The post id being saved. |
| 179 | 179 | */ |
| 180 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 180 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 181 | 181 | |
| 182 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 182 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
| 183 | 183 | |
| 184 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 184 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
| 185 | 185 | |
| 186 | 186 | // Ignore auto-saves |
| 187 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 188 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 187 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 188 | + $log->trace('Doing autosave, skipping...'); |
|
| 189 | 189 | |
| 190 | 190 | return; |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | // get the current post. |
| 194 | - $post = get_post( $post_id ); |
|
| 194 | + $post = get_post($post_id); |
|
| 195 | 195 | |
| 196 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 196 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 197 | 197 | |
| 198 | 198 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 199 | 199 | |
@@ -204,44 +204,44 @@ discard block |
||
| 204 | 204 | $entities_uri_mapping = array(); |
| 205 | 205 | |
| 206 | 206 | // Save the entities coming with POST data. |
| 207 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 207 | + if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { |
|
| 208 | 208 | |
| 209 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 210 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 211 | - wl_write_log( "]" ); |
|
| 212 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 213 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 214 | - wl_write_log( "]" ); |
|
| 209 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 210 | + wl_write_log(json_encode($_POST['wl_entities'])); |
|
| 211 | + wl_write_log("]"); |
|
| 212 | + wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: "); |
|
| 213 | + wl_write_log(json_encode($_POST['wl_boxes'], true)); |
|
| 214 | + wl_write_log("]"); |
|
| 215 | 215 | |
| 216 | 216 | $entities_via_post = $_POST['wl_entities']; |
| 217 | 217 | $boxes_via_post = $_POST['wl_boxes']; |
| 218 | 218 | |
| 219 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 219 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 220 | 220 | |
| 221 | 221 | // Only if the current entity is created from scratch let's avoid to |
| 222 | 222 | // create more than one entity with same label & entity type. |
| 223 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 223 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 224 | 224 | $entity['main_type'] : null; |
| 225 | 225 | |
| 226 | 226 | // Look if current entity uri matches an internal existing entity, meaning: |
| 227 | 227 | // 1. when $entity_uri is an internal uri |
| 228 | 228 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 229 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 229 | + $ie = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 230 | 230 | |
| 231 | 231 | // Detect the uri depending if is an existing or a new entity |
| 232 | - $uri = ( null === $ie ) ? |
|
| 232 | + $uri = (null === $ie) ? |
|
| 233 | 233 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 234 | 234 | $entity['label'], |
| 235 | 235 | Wordlift_Entity_Service::TYPE_NAME, |
| 236 | 236 | $entity_type |
| 237 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 237 | + ) : wl_get_entity_uri($ie->ID); |
|
| 238 | 238 | |
| 239 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 240 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 239 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 240 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 241 | 241 | |
| 242 | 242 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 243 | 243 | // These uris need to be rewritten here and replaced in the content |
| 244 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 244 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 245 | 245 | // Override the entity obj |
| 246 | 246 | $entity['uri'] = $uri; |
| 247 | 247 | } |
@@ -250,8 +250,8 @@ discard block |
||
| 250 | 250 | $entity['related_post_id'] = $post_id; |
| 251 | 251 | |
| 252 | 252 | // Save the entity if is a new entity |
| 253 | - if ( null === $ie ) { |
|
| 254 | - wl_save_entity( $entity ); |
|
| 253 | + if (null === $ie) { |
|
| 254 | + wl_save_entity($entity); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | } |
@@ -262,39 +262,39 @@ discard block |
||
| 262 | 262 | $updated_post_content = $post->post_content; |
| 263 | 263 | |
| 264 | 264 | // Update the post content if we found mappings of new entities. |
| 265 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 265 | + if ( ! empty($entities_uri_mapping)) { |
|
| 266 | 266 | // Save each entity and store the post id. |
| 267 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 268 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 267 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 268 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | // Update the post content. |
| 272 | - wp_update_post( array( |
|
| 272 | + wp_update_post(array( |
|
| 273 | 273 | 'ID' => $post->ID, |
| 274 | 274 | 'post_content' => $updated_post_content, |
| 275 | - ) ); |
|
| 275 | + )); |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | // Extract related/referenced entities from text. |
| 279 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 279 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 280 | 280 | |
| 281 | 281 | // Reset previously saved instances. |
| 282 | - wl_core_delete_relation_instances( $post_id ); |
|
| 282 | + wl_core_delete_relation_instances($post_id); |
|
| 283 | 283 | |
| 284 | 284 | // Save relation instances |
| 285 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 285 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 286 | 286 | |
| 287 | 287 | wl_core_add_relation_instance( |
| 288 | 288 | $post_id, |
| 289 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 289 | + $entity_service->get_classification_scope_for($referenced_entity_id), |
|
| 290 | 290 | $referenced_entity_id |
| 291 | 291 | ); |
| 292 | 292 | |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 295 | + if (isset($_POST['wl_entities'])) { |
|
| 296 | 296 | // Save post metadata if available |
| 297 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 297 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? |
|
| 298 | 298 | $_POST['wl_metadata'] : array(); |
| 299 | 299 | |
| 300 | 300 | $fields = array( |
@@ -303,35 +303,35 @@ discard block |
||
| 303 | 303 | ); |
| 304 | 304 | |
| 305 | 305 | // Unlink topic taxonomy terms |
| 306 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 306 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
| 307 | 307 | |
| 308 | - foreach ( $fields as $field ) { |
|
| 308 | + foreach ($fields as $field) { |
|
| 309 | 309 | |
| 310 | 310 | // Delete current values |
| 311 | - delete_post_meta( $post->ID, $field ); |
|
| 311 | + delete_post_meta($post->ID, $field); |
|
| 312 | 312 | // Retrieve the entity uri |
| 313 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 314 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 313 | + $uri = (isset($metadata_via_post[$field])) ? |
|
| 314 | + stripslashes($metadata_via_post[$field]) : ''; |
|
| 315 | 315 | |
| 316 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 316 | + $entity = $entity_service->get_entity_post_by_uri($uri); |
|
| 317 | 317 | |
| 318 | - if ( $entity ) { |
|
| 319 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 318 | + if ($entity) { |
|
| 319 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
| 320 | 320 | // Set also the topic taxonomy |
| 321 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 322 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 321 | + if ($field === Wordlift_Schema_Service::FIELD_TOPIC) { |
|
| 322 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
| 323 | 323 | } |
| 324 | 324 | } |
| 325 | 325 | } |
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | // Push the post to Redlink. |
| 329 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 329 | + Wordlift_Linked_Data_Service::get_instance()->push($post->ID); |
|
| 330 | 330 | |
| 331 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 331 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 334 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 335 | 335 | |
| 336 | 336 | /** |
| 337 | 337 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -349,14 +349,14 @@ discard block |
||
| 349 | 349 | * |
| 350 | 350 | * @return null|WP_Post A post instance or null in case of failure. |
| 351 | 351 | */ |
| 352 | -function wl_save_entity( $entity_data ) { |
|
| 352 | +function wl_save_entity($entity_data) { |
|
| 353 | 353 | |
| 354 | 354 | // Required for REST API calls |
| 355 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 356 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 355 | + if ( ! function_exists('wp_crop_image')) { |
|
| 356 | + require_once(ABSPATH.'wp-admin/includes/image.php'); |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 359 | + $log = Wordlift_Log_Service::get_logger('wl_save_entity'); |
|
| 360 | 360 | |
| 361 | 361 | $uri = $entity_data['uri']; |
| 362 | 362 | /* |
@@ -366,28 +366,28 @@ discard block |
||
| 366 | 366 | * |
| 367 | 367 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 368 | 368 | */ |
| 369 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 369 | + $label = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label'])); |
|
| 370 | 370 | $type_uri = $entity_data['main_type']; |
| 371 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 371 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 372 | 372 | $description = $entity_data['description']; |
| 373 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 374 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 375 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 376 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 373 | + $images = isset($entity_data['image']) ? (array) $entity_data['image'] : array(); |
|
| 374 | + $same_as = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array(); |
|
| 375 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 376 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 377 | 377 | |
| 378 | 378 | // Get the synonyms. |
| 379 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 379 | + $synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array(); |
|
| 380 | 380 | |
| 381 | 381 | // Check whether an entity already exists with the provided URI. |
| 382 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 383 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 382 | + if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) { |
|
| 383 | + $log->debug("Post already exists for URI $uri."); |
|
| 384 | 384 | |
| 385 | 385 | return $post; |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | // Prepare properties of the new entity. |
| 389 | 389 | $params = array( |
| 390 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 390 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 391 | 391 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 392 | 392 | 'post_title' => $label, |
| 393 | 393 | 'post_content' => $description, |
@@ -397,7 +397,7 @@ discard block |
||
| 397 | 397 | // new entities. |
| 398 | 398 | // |
| 399 | 399 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 400 | - 'post_name' => sanitize_title( $label ), |
|
| 400 | + 'post_name' => sanitize_title($label), |
|
| 401 | 401 | ); |
| 402 | 402 | |
| 403 | 403 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -406,18 +406,18 @@ discard block |
||
| 406 | 406 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 407 | 407 | // is created when saving a post. |
| 408 | 408 | global $wpseo_metabox, $seo_ultimate; |
| 409 | - if ( isset( $wpseo_metabox ) ) { |
|
| 410 | - remove_action( 'wp_insert_post', array( |
|
| 409 | + if (isset($wpseo_metabox)) { |
|
| 410 | + remove_action('wp_insert_post', array( |
|
| 411 | 411 | $wpseo_metabox, |
| 412 | 412 | 'save_postdata', |
| 413 | - ) ); |
|
| 413 | + )); |
|
| 414 | 414 | } |
| 415 | 415 | |
| 416 | - if ( isset( $seo_ultimate ) ) { |
|
| 417 | - remove_action( 'save_post', array( |
|
| 416 | + if (isset($seo_ultimate)) { |
|
| 417 | + remove_action('save_post', array( |
|
| 418 | 418 | $seo_ultimate, |
| 419 | 419 | 'save_postmeta_box', |
| 420 | - ) ); |
|
| 420 | + )); |
|
| 421 | 421 | } |
| 422 | 422 | |
| 423 | 423 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -427,95 +427,95 @@ discard block |
||
| 427 | 427 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 428 | 428 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 429 | 429 | global $wp_filter; |
| 430 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 431 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 430 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 431 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 432 | 432 | |
| 433 | 433 | |
| 434 | - $log->trace( 'Going to insert new post...' ); |
|
| 434 | + $log->trace('Going to insert new post...'); |
|
| 435 | 435 | |
| 436 | 436 | // create or update the post. |
| 437 | - $post_id = wp_insert_post( $params, true ); |
|
| 437 | + $post_id = wp_insert_post($params, true); |
|
| 438 | 438 | |
| 439 | 439 | // Setting the alternative labels for this entity. |
| 440 | 440 | Wordlift_Entity_Service::get_instance() |
| 441 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 441 | + ->set_alternative_labels($post_id, $synonyms); |
|
| 442 | 442 | |
| 443 | 443 | // Restore all the existing filters. |
| 444 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 444 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 445 | 445 | |
| 446 | 446 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 447 | - if ( isset( $wpseo_metabox ) ) { |
|
| 448 | - add_action( 'wp_insert_post', array( |
|
| 447 | + if (isset($wpseo_metabox)) { |
|
| 448 | + add_action('wp_insert_post', array( |
|
| 449 | 449 | $wpseo_metabox, |
| 450 | 450 | 'save_postdata', |
| 451 | - ) ); |
|
| 451 | + )); |
|
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 455 | - if ( isset( $seo_ultimate ) ) { |
|
| 456 | - add_action( 'save_post', array( |
|
| 455 | + if (isset($seo_ultimate)) { |
|
| 456 | + add_action('save_post', array( |
|
| 457 | 457 | $seo_ultimate, |
| 458 | 458 | 'save_postmeta_box', |
| 459 | - ), 10, 2 ); |
|
| 459 | + ), 10, 2); |
|
| 460 | 460 | } |
| 461 | 461 | |
| 462 | 462 | // TODO: handle errors. |
| 463 | - if ( is_wp_error( $post_id ) ) { |
|
| 464 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 463 | + if (is_wp_error($post_id)) { |
|
| 464 | + $log->error('An error occurred: '.$post_id->get_error_message()); |
|
| 465 | 465 | |
| 466 | 466 | // inform an error occurred. |
| 467 | 467 | return null; |
| 468 | 468 | } |
| 469 | 469 | |
| 470 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 470 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 471 | 471 | |
| 472 | 472 | // Save the entity types. |
| 473 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 473 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 474 | 474 | |
| 475 | 475 | // Get a dataset URI for the entity. |
| 476 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 476 | + $wl_uri = wl_build_entity_uri($post_id); |
|
| 477 | 477 | |
| 478 | 478 | // Save the entity URI. |
| 479 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 479 | + wl_set_entity_uri($post_id, $wl_uri); |
|
| 480 | 480 | |
| 481 | 481 | // Add the uri to the sameAs data if it's not a local URI. |
| 482 | - if ( $wl_uri !== $uri ) { |
|
| 483 | - array_push( $same_as, $uri ); |
|
| 482 | + if ($wl_uri !== $uri) { |
|
| 483 | + array_push($same_as, $uri); |
|
| 484 | 484 | } |
| 485 | 485 | |
| 486 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 486 | + $new_uri = wl_get_entity_uri($post_id); |
|
| 487 | 487 | |
| 488 | 488 | // Save the sameAs data for the entity. |
| 489 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 489 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 490 | 490 | |
| 491 | 491 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 492 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 493 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 492 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 493 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | // Call hooks. |
| 497 | - do_action( 'wl_save_entity', $post_id ); |
|
| 497 | + do_action('wl_save_entity', $post_id); |
|
| 498 | 498 | |
| 499 | - foreach ( $images as $image_remote_url ) { |
|
| 499 | + foreach ($images as $image_remote_url) { |
|
| 500 | 500 | |
| 501 | 501 | // Check if image is already present in local DB |
| 502 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 502 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 503 | 503 | // Do nothing. |
| 504 | 504 | continue; |
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | // Check if there is an existing attachment for this post ID and source URL. |
| 508 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 508 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 509 | 509 | |
| 510 | 510 | // Skip if an existing image is found. |
| 511 | - if ( null !== $existing_image ) { |
|
| 511 | + if (null !== $existing_image) { |
|
| 512 | 512 | continue; |
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | // Save the image and get the local path. |
| 516 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 516 | + $image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url); |
|
| 517 | 517 | |
| 518 | - if ( false === $image ) { |
|
| 518 | + if (false === $image) { |
|
| 519 | 519 | continue; |
| 520 | 520 | } |
| 521 | 521 | |
@@ -535,24 +535,24 @@ discard block |
||
| 535 | 535 | ); |
| 536 | 536 | |
| 537 | 537 | // Create the attachment in WordPress and generate the related metadata. |
| 538 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 538 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 539 | 539 | |
| 540 | 540 | // Set the source URL for the image. |
| 541 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 541 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 542 | 542 | |
| 543 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 544 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 543 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 544 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 545 | 545 | |
| 546 | 546 | // Set it as the featured image. |
| 547 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 547 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 551 | 551 | // save the entity in the triple store. |
| 552 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 552 | + Wordlift_Linked_Data_Service::get_instance()->push($post_id); |
|
| 553 | 553 | |
| 554 | 554 | // finally return the entity post. |
| 555 | - return get_post( $post_id ); |
|
| 555 | + return get_post($post_id); |
|
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | /** |
@@ -564,10 +564,10 @@ discard block |
||
| 564 | 564 | * |
| 565 | 565 | * @return array An array of entity posts. |
| 566 | 566 | */ |
| 567 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 567 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 568 | 568 | |
| 569 | 569 | // Remove quote escapes. |
| 570 | - $content = str_replace( '\\"', '"', $content ); |
|
| 570 | + $content = str_replace('\\"', '"', $content); |
|
| 571 | 571 | |
| 572 | 572 | // Match all itemid attributes. |
| 573 | 573 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -578,8 +578,8 @@ discard block |
||
| 578 | 578 | $matches = array(); |
| 579 | 579 | |
| 580 | 580 | // In case of errors, return an empty array. |
| 581 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 582 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 581 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 582 | + wl_write_log("Found no entities embedded in content"); |
|
| 583 | 583 | |
| 584 | 584 | return array(); |
| 585 | 585 | } |
@@ -588,13 +588,13 @@ discard block |
||
| 588 | 588 | |
| 589 | 589 | // Collect the entities. |
| 590 | 590 | $entities = array(); |
| 591 | - foreach ( $matches[1] as $uri ) { |
|
| 592 | - $uri_d = html_entity_decode( $uri ); |
|
| 591 | + foreach ($matches[1] as $uri) { |
|
| 592 | + $uri_d = html_entity_decode($uri); |
|
| 593 | 593 | |
| 594 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 594 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 595 | 595 | |
| 596 | - if ( null !== $entity ) { |
|
| 597 | - array_push( $entities, $entity->ID ); |
|
| 596 | + if (null !== $entity) { |
|
| 597 | + array_push($entities, $entity->ID); |
|
| 598 | 598 | } |
| 599 | 599 | } |
| 600 | 600 | |
@@ -10,15 +10,15 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | function wl_register_metaboxes() { |
| 12 | 12 | |
| 13 | - // Load metabox classes. |
|
| 14 | - require_once( 'WL_Metabox/class-wl-metabox.php' ); |
|
| 13 | + // Load metabox classes. |
|
| 14 | + require_once( 'WL_Metabox/class-wl-metabox.php' ); |
|
| 15 | 15 | |
| 16 | - $wl_metabox = new WL_Metabox(); // Everything is done inside here with the correct timing. |
|
| 16 | + $wl_metabox = new WL_Metabox(); // Everything is done inside here with the correct timing. |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | if ( is_admin() ) { |
| 20 | - add_action( 'load-post.php', 'wl_register_metaboxes' ); |
|
| 21 | - add_action( 'load-post-new.php', 'wl_register_metaboxes' ); |
|
| 20 | + add_action( 'load-post.php', 'wl_register_metaboxes' ); |
|
| 21 | + add_action( 'load-post-new.php', 'wl_register_metaboxes' ); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | |
@@ -29,20 +29,20 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | function wl_admin_add_entities_meta_box( $post_type ) { |
| 31 | 31 | |
| 32 | - // Bail out if the post type doesn't support a TinyMCE editor. |
|
| 33 | - if ( ! wl_post_type_supports_editor( $post_type ) ) { |
|
| 34 | - return; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - if (!Wordlift_Admin::is_gutenberg()) { |
|
| 38 | - // Add main meta box for related entities and 4W only if not Gutenberg |
|
| 39 | - add_meta_box( |
|
| 40 | - 'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high' |
|
| 41 | - ); |
|
| 42 | - } else { |
|
| 43 | - // Call wl_entities_box_content for the other things that it does. |
|
| 44 | - wl_entities_box_content(get_post(), false); |
|
| 45 | - } |
|
| 32 | + // Bail out if the post type doesn't support a TinyMCE editor. |
|
| 33 | + if ( ! wl_post_type_supports_editor( $post_type ) ) { |
|
| 34 | + return; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + if (!Wordlift_Admin::is_gutenberg()) { |
|
| 38 | + // Add main meta box for related entities and 4W only if not Gutenberg |
|
| 39 | + add_meta_box( |
|
| 40 | + 'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high' |
|
| 41 | + ); |
|
| 42 | + } else { |
|
| 43 | + // Call wl_entities_box_content for the other things that it does. |
|
| 44 | + wl_entities_box_content(get_post(), false); |
|
| 45 | + } |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | add_action( 'add_meta_boxes', 'wl_admin_add_entities_meta_box' ); |
@@ -58,18 +58,18 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | function wl_post_type_supports_editor( $post_type ) { |
| 60 | 60 | |
| 61 | - $default = post_type_supports( $post_type, 'editor' ); |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Allow 3rd parties to force the classification to load. |
|
| 65 | - * |
|
| 66 | - * @since 3.19.4 |
|
| 67 | - * |
|
| 68 | - * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 69 | - * |
|
| 70 | - * @param bool $default The preset value as gathered by the `post_type_supports` call. |
|
| 71 | - */ |
|
| 72 | - return apply_filters( 'wl_post_type_supports_editor', $default, $post_type ); |
|
| 61 | + $default = post_type_supports( $post_type, 'editor' ); |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Allow 3rd parties to force the classification to load. |
|
| 65 | + * |
|
| 66 | + * @since 3.19.4 |
|
| 67 | + * |
|
| 68 | + * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 69 | + * |
|
| 70 | + * @param bool $default The preset value as gathered by the `post_type_supports` call. |
|
| 71 | + */ |
|
| 72 | + return apply_filters( 'wl_post_type_supports_editor', $default, $post_type ); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -79,100 +79,100 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | function wl_entities_box_content( $post, $wrapper = true ) { |
| 81 | 81 | |
| 82 | - // Angularjs edit-post widget wrapper. |
|
| 83 | - if($wrapper) echo '<div id="wordlift-edit-post-outer-wrapper"></div>'; |
|
| 82 | + // Angularjs edit-post widget wrapper. |
|
| 83 | + if($wrapper) echo '<div id="wordlift-edit-post-outer-wrapper"></div>'; |
|
| 84 | 84 | |
| 85 | - // Angularjs edit-post widget classification boxes configuration. |
|
| 86 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 85 | + // Angularjs edit-post widget classification boxes configuration. |
|
| 86 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 87 | 87 | |
| 88 | - // Array to store all related entities ids. |
|
| 89 | - $all_referenced_entities_ids = array(); |
|
| 88 | + // Array to store all related entities ids. |
|
| 89 | + $all_referenced_entities_ids = array(); |
|
| 90 | 90 | |
| 91 | - // Add selected entities to classification_boxes. |
|
| 92 | - foreach ( $classification_boxes as $i => $box ) { |
|
| 93 | - // Build the proper relation name. |
|
| 94 | - $relation_name = $box['id']; |
|
| 91 | + // Add selected entities to classification_boxes. |
|
| 92 | + foreach ( $classification_boxes as $i => $box ) { |
|
| 93 | + // Build the proper relation name. |
|
| 94 | + $relation_name = $box['id']; |
|
| 95 | 95 | |
| 96 | - // Get the entity referenced from the post content. |
|
| 97 | - /* |
|
| 96 | + // Get the entity referenced from the post content. |
|
| 97 | + /* |
|
| 98 | 98 | * Allow 3rd parties to provide another post content. |
| 99 | 99 | * |
| 100 | 100 | * @since 3.20.0 |
| 101 | 101 | */ |
| 102 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 103 | - $entity_uris = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content ); |
|
| 104 | - |
|
| 105 | - // Enhance current box selected entities. |
|
| 106 | - $classification_boxes[ $i ]['selectedEntities'] = $entity_uris; |
|
| 107 | - |
|
| 108 | - // Maps the URIs to entity posts. |
|
| 109 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 110 | - |
|
| 111 | - // Replace all entity URI's with post ID's if found or null if there is no related post. |
|
| 112 | - $entity_ids = array_map( function ( $item ) use ( $entity_service ) { |
|
| 113 | - // Return entity post by the entity URI or null. |
|
| 114 | - $post = $entity_service->get_entity_post_by_uri( $item ); |
|
| 115 | - |
|
| 116 | - // Check that the post object is not null. |
|
| 117 | - if ( ! empty( $post ) ) { |
|
| 118 | - return $post->ID; |
|
| 119 | - } |
|
| 120 | - }, $entity_uris ); |
|
| 121 | - // Store the entity ids for all the 4W. |
|
| 122 | - $all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids ); |
|
| 123 | - |
|
| 124 | - } |
|
| 125 | - // Json encoding for classification boxes structure. |
|
| 126 | - $classification_boxes = wp_json_encode( $classification_boxes ); |
|
| 127 | - |
|
| 128 | - // Ensure there are no repetitions of the referenced entities. |
|
| 129 | - $all_referenced_entities_ids = array_unique( $all_referenced_entities_ids ); |
|
| 130 | - |
|
| 131 | - // Remove all null, false and empty strings. |
|
| 132 | - // NULL is being returned in some cases, when there is not related post, so we need to remove it. |
|
| 133 | - $all_referenced_entities_ids = array_filter( $all_referenced_entities_ids ); |
|
| 134 | - |
|
| 135 | - // Build the entity storage object. |
|
| 136 | - $referenced_entities_obj = array(); |
|
| 137 | - foreach ( $all_referenced_entities_ids as $referenced_entity ) { |
|
| 138 | - $entity = wl_serialize_entity( $referenced_entity ); |
|
| 139 | - // Set a default confidence of `PHP_INT_MAX` for already annotated entities. |
|
| 140 | - $referenced_entities_obj[ $entity['id'] ] = $entity |
|
| 141 | - + array( 'confidence' => PHP_INT_MAX ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $referenced_entities_obj = empty( $referenced_entities_obj ) ? |
|
| 145 | - '{}' : wp_json_encode( $referenced_entities_obj ); |
|
| 146 | - |
|
| 147 | - $published_place_id = get_post_meta( |
|
| 148 | - $post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true |
|
| 149 | - ); |
|
| 150 | - $published_place_obj = ( $published_place_id ) ? |
|
| 151 | - wp_json_encode( wl_serialize_entity( $published_place_id ) ) : |
|
| 152 | - 'undefined'; |
|
| 153 | - |
|
| 154 | - $topic_id = get_post_meta( |
|
| 155 | - $post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true |
|
| 156 | - ); |
|
| 157 | - $topic_obj = ( $topic_id ) ? |
|
| 158 | - wp_json_encode( wl_serialize_entity( $topic_id ) ) : |
|
| 159 | - 'undefined'; |
|
| 160 | - |
|
| 161 | - $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
| 162 | - |
|
| 163 | - $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH; |
|
| 164 | - $default_path = WL_DEFAULT_PATH; |
|
| 165 | - $dataset_uri = $configuration_service->get_dataset_uri(); |
|
| 166 | - $current_post_uri = wl_get_entity_uri( $post->ID ); |
|
| 167 | - |
|
| 168 | - // Retrieve the current post author. |
|
| 169 | - $post_author = get_userdata( $post->post_author )->display_name; |
|
| 170 | - // Retrive the published date. |
|
| 171 | - $published_date = get_the_time( 'Y-m-d', $post->ID ); |
|
| 172 | - // Current language. |
|
| 173 | - $current_language = $configuration_service->get_language_code(); |
|
| 174 | - |
|
| 175 | - $js_code = <<<JS |
|
| 102 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 103 | + $entity_uris = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content ); |
|
| 104 | + |
|
| 105 | + // Enhance current box selected entities. |
|
| 106 | + $classification_boxes[ $i ]['selectedEntities'] = $entity_uris; |
|
| 107 | + |
|
| 108 | + // Maps the URIs to entity posts. |
|
| 109 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 110 | + |
|
| 111 | + // Replace all entity URI's with post ID's if found or null if there is no related post. |
|
| 112 | + $entity_ids = array_map( function ( $item ) use ( $entity_service ) { |
|
| 113 | + // Return entity post by the entity URI or null. |
|
| 114 | + $post = $entity_service->get_entity_post_by_uri( $item ); |
|
| 115 | + |
|
| 116 | + // Check that the post object is not null. |
|
| 117 | + if ( ! empty( $post ) ) { |
|
| 118 | + return $post->ID; |
|
| 119 | + } |
|
| 120 | + }, $entity_uris ); |
|
| 121 | + // Store the entity ids for all the 4W. |
|
| 122 | + $all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids ); |
|
| 123 | + |
|
| 124 | + } |
|
| 125 | + // Json encoding for classification boxes structure. |
|
| 126 | + $classification_boxes = wp_json_encode( $classification_boxes ); |
|
| 127 | + |
|
| 128 | + // Ensure there are no repetitions of the referenced entities. |
|
| 129 | + $all_referenced_entities_ids = array_unique( $all_referenced_entities_ids ); |
|
| 130 | + |
|
| 131 | + // Remove all null, false and empty strings. |
|
| 132 | + // NULL is being returned in some cases, when there is not related post, so we need to remove it. |
|
| 133 | + $all_referenced_entities_ids = array_filter( $all_referenced_entities_ids ); |
|
| 134 | + |
|
| 135 | + // Build the entity storage object. |
|
| 136 | + $referenced_entities_obj = array(); |
|
| 137 | + foreach ( $all_referenced_entities_ids as $referenced_entity ) { |
|
| 138 | + $entity = wl_serialize_entity( $referenced_entity ); |
|
| 139 | + // Set a default confidence of `PHP_INT_MAX` for already annotated entities. |
|
| 140 | + $referenced_entities_obj[ $entity['id'] ] = $entity |
|
| 141 | + + array( 'confidence' => PHP_INT_MAX ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $referenced_entities_obj = empty( $referenced_entities_obj ) ? |
|
| 145 | + '{}' : wp_json_encode( $referenced_entities_obj ); |
|
| 146 | + |
|
| 147 | + $published_place_id = get_post_meta( |
|
| 148 | + $post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true |
|
| 149 | + ); |
|
| 150 | + $published_place_obj = ( $published_place_id ) ? |
|
| 151 | + wp_json_encode( wl_serialize_entity( $published_place_id ) ) : |
|
| 152 | + 'undefined'; |
|
| 153 | + |
|
| 154 | + $topic_id = get_post_meta( |
|
| 155 | + $post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true |
|
| 156 | + ); |
|
| 157 | + $topic_obj = ( $topic_id ) ? |
|
| 158 | + wp_json_encode( wl_serialize_entity( $topic_id ) ) : |
|
| 159 | + 'undefined'; |
|
| 160 | + |
|
| 161 | + $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
| 162 | + |
|
| 163 | + $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH; |
|
| 164 | + $default_path = WL_DEFAULT_PATH; |
|
| 165 | + $dataset_uri = $configuration_service->get_dataset_uri(); |
|
| 166 | + $current_post_uri = wl_get_entity_uri( $post->ID ); |
|
| 167 | + |
|
| 168 | + // Retrieve the current post author. |
|
| 169 | + $post_author = get_userdata( $post->post_author )->display_name; |
|
| 170 | + // Retrive the published date. |
|
| 171 | + $published_date = get_the_time( 'Y-m-d', $post->ID ); |
|
| 172 | + // Current language. |
|
| 173 | + $current_language = $configuration_service->get_language_code(); |
|
| 174 | + |
|
| 175 | + $js_code = <<<JS |
|
| 176 | 176 | if ('undefined' == typeof window.wordlift) { |
| 177 | 177 | window.wordlift = {}; |
| 178 | 178 | window.wordlift.entities = {}; |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | window.wordlift.currentLanguage = '$current_language'; |
| 194 | 194 | JS; |
| 195 | 195 | |
| 196 | - if($wrapper) echo '<script type="text/javascript">'.PHP_EOL.$js_code.PHP_EOL.'</script>'; |
|
| 197 | - wp_add_inline_script('wl-entity-metabox-utility', $js_code); |
|
| 196 | + if($wrapper) echo '<script type="text/javascript">'.PHP_EOL.$js_code.PHP_EOL.'</script>'; |
|
| 197 | + wp_add_inline_script('wl-entity-metabox-utility', $js_code); |
|
| 198 | 198 | } |
| 199 | 199 | |
@@ -11,14 +11,14 @@ discard block |
||
| 11 | 11 | function wl_register_metaboxes() { |
| 12 | 12 | |
| 13 | 13 | // Load metabox classes. |
| 14 | - require_once( 'WL_Metabox/class-wl-metabox.php' ); |
|
| 14 | + require_once('WL_Metabox/class-wl-metabox.php'); |
|
| 15 | 15 | |
| 16 | - $wl_metabox = new WL_Metabox(); // Everything is done inside here with the correct timing. |
|
| 16 | + $wl_metabox = new WL_Metabox(); // Everything is done inside here with the correct timing. |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | -if ( is_admin() ) { |
|
| 20 | - add_action( 'load-post.php', 'wl_register_metaboxes' ); |
|
| 21 | - add_action( 'load-post-new.php', 'wl_register_metaboxes' ); |
|
| 19 | +if (is_admin()) { |
|
| 20 | + add_action('load-post.php', 'wl_register_metaboxes'); |
|
| 21 | + add_action('load-post-new.php', 'wl_register_metaboxes'); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | |
@@ -27,17 +27,17 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @param string $post_type The type of the current open post. |
| 29 | 29 | */ |
| 30 | -function wl_admin_add_entities_meta_box( $post_type ) { |
|
| 30 | +function wl_admin_add_entities_meta_box($post_type) { |
|
| 31 | 31 | |
| 32 | 32 | // Bail out if the post type doesn't support a TinyMCE editor. |
| 33 | - if ( ! wl_post_type_supports_editor( $post_type ) ) { |
|
| 33 | + if ( ! wl_post_type_supports_editor($post_type)) { |
|
| 34 | 34 | return; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - if (!Wordlift_Admin::is_gutenberg()) { |
|
| 37 | + if ( ! Wordlift_Admin::is_gutenberg()) { |
|
| 38 | 38 | // Add main meta box for related entities and 4W only if not Gutenberg |
| 39 | 39 | add_meta_box( |
| 40 | - 'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high' |
|
| 40 | + 'wordlift_entities_box', __('WordLift', 'wordlift'), 'wl_entities_box_content', $post_type, 'side', 'high' |
|
| 41 | 41 | ); |
| 42 | 42 | } else { |
| 43 | 43 | // Call wl_entities_box_content for the other things that it does. |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | -add_action( 'add_meta_boxes', 'wl_admin_add_entities_meta_box' ); |
|
| 48 | +add_action('add_meta_boxes', 'wl_admin_add_entities_meta_box'); |
|
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * Whether the post type supports the editor UI. |
@@ -56,9 +56,9 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @return bool True if the editor UI is supported otherwise false. |
| 58 | 58 | */ |
| 59 | -function wl_post_type_supports_editor( $post_type ) { |
|
| 59 | +function wl_post_type_supports_editor($post_type) { |
|
| 60 | 60 | |
| 61 | - $default = post_type_supports( $post_type, 'editor' ); |
|
| 61 | + $default = post_type_supports($post_type, 'editor'); |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Allow 3rd parties to force the classification to load. |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @param bool $default The preset value as gathered by the `post_type_supports` call. |
| 71 | 71 | */ |
| 72 | - return apply_filters( 'wl_post_type_supports_editor', $default, $post_type ); |
|
| 72 | + return apply_filters('wl_post_type_supports_editor', $default, $post_type); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -77,19 +77,19 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @param WP_Post $post The current post. |
| 79 | 79 | */ |
| 80 | -function wl_entities_box_content( $post, $wrapper = true ) { |
|
| 80 | +function wl_entities_box_content($post, $wrapper = true) { |
|
| 81 | 81 | |
| 82 | 82 | // Angularjs edit-post widget wrapper. |
| 83 | - if($wrapper) echo '<div id="wordlift-edit-post-outer-wrapper"></div>'; |
|
| 83 | + if ($wrapper) echo '<div id="wordlift-edit-post-outer-wrapper"></div>'; |
|
| 84 | 84 | |
| 85 | 85 | // Angularjs edit-post widget classification boxes configuration. |
| 86 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 86 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 87 | 87 | |
| 88 | 88 | // Array to store all related entities ids. |
| 89 | 89 | $all_referenced_entities_ids = array(); |
| 90 | 90 | |
| 91 | 91 | // Add selected entities to classification_boxes. |
| 92 | - foreach ( $classification_boxes as $i => $box ) { |
|
| 92 | + foreach ($classification_boxes as $i => $box) { |
|
| 93 | 93 | // Build the proper relation name. |
| 94 | 94 | $relation_name = $box['id']; |
| 95 | 95 | |
@@ -99,80 +99,78 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @since 3.20.0 |
| 101 | 101 | */ |
| 102 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 103 | - $entity_uris = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content ); |
|
| 102 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 103 | + $entity_uris = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris($post_content); |
|
| 104 | 104 | |
| 105 | 105 | // Enhance current box selected entities. |
| 106 | - $classification_boxes[ $i ]['selectedEntities'] = $entity_uris; |
|
| 106 | + $classification_boxes[$i]['selectedEntities'] = $entity_uris; |
|
| 107 | 107 | |
| 108 | 108 | // Maps the URIs to entity posts. |
| 109 | 109 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 110 | 110 | |
| 111 | 111 | // Replace all entity URI's with post ID's if found or null if there is no related post. |
| 112 | - $entity_ids = array_map( function ( $item ) use ( $entity_service ) { |
|
| 112 | + $entity_ids = array_map(function($item) use ($entity_service) { |
|
| 113 | 113 | // Return entity post by the entity URI or null. |
| 114 | - $post = $entity_service->get_entity_post_by_uri( $item ); |
|
| 114 | + $post = $entity_service->get_entity_post_by_uri($item); |
|
| 115 | 115 | |
| 116 | 116 | // Check that the post object is not null. |
| 117 | - if ( ! empty( $post ) ) { |
|
| 117 | + if ( ! empty($post)) { |
|
| 118 | 118 | return $post->ID; |
| 119 | 119 | } |
| 120 | - }, $entity_uris ); |
|
| 120 | + }, $entity_uris); |
|
| 121 | 121 | // Store the entity ids for all the 4W. |
| 122 | - $all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids ); |
|
| 122 | + $all_referenced_entities_ids = array_merge($all_referenced_entities_ids, $entity_ids); |
|
| 123 | 123 | |
| 124 | 124 | } |
| 125 | 125 | // Json encoding for classification boxes structure. |
| 126 | - $classification_boxes = wp_json_encode( $classification_boxes ); |
|
| 126 | + $classification_boxes = wp_json_encode($classification_boxes); |
|
| 127 | 127 | |
| 128 | 128 | // Ensure there are no repetitions of the referenced entities. |
| 129 | - $all_referenced_entities_ids = array_unique( $all_referenced_entities_ids ); |
|
| 129 | + $all_referenced_entities_ids = array_unique($all_referenced_entities_ids); |
|
| 130 | 130 | |
| 131 | 131 | // Remove all null, false and empty strings. |
| 132 | 132 | // NULL is being returned in some cases, when there is not related post, so we need to remove it. |
| 133 | - $all_referenced_entities_ids = array_filter( $all_referenced_entities_ids ); |
|
| 133 | + $all_referenced_entities_ids = array_filter($all_referenced_entities_ids); |
|
| 134 | 134 | |
| 135 | 135 | // Build the entity storage object. |
| 136 | 136 | $referenced_entities_obj = array(); |
| 137 | - foreach ( $all_referenced_entities_ids as $referenced_entity ) { |
|
| 138 | - $entity = wl_serialize_entity( $referenced_entity ); |
|
| 137 | + foreach ($all_referenced_entities_ids as $referenced_entity) { |
|
| 138 | + $entity = wl_serialize_entity($referenced_entity); |
|
| 139 | 139 | // Set a default confidence of `PHP_INT_MAX` for already annotated entities. |
| 140 | - $referenced_entities_obj[ $entity['id'] ] = $entity |
|
| 141 | - + array( 'confidence' => PHP_INT_MAX ); |
|
| 140 | + $referenced_entities_obj[$entity['id']] = $entity |
|
| 141 | + + array('confidence' => PHP_INT_MAX); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - $referenced_entities_obj = empty( $referenced_entities_obj ) ? |
|
| 145 | - '{}' : wp_json_encode( $referenced_entities_obj ); |
|
| 144 | + $referenced_entities_obj = empty($referenced_entities_obj) ? |
|
| 145 | + '{}' : wp_json_encode($referenced_entities_obj); |
|
| 146 | 146 | |
| 147 | 147 | $published_place_id = get_post_meta( |
| 148 | 148 | $post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true |
| 149 | 149 | ); |
| 150 | - $published_place_obj = ( $published_place_id ) ? |
|
| 151 | - wp_json_encode( wl_serialize_entity( $published_place_id ) ) : |
|
| 152 | - 'undefined'; |
|
| 150 | + $published_place_obj = ($published_place_id) ? |
|
| 151 | + wp_json_encode(wl_serialize_entity($published_place_id)) : 'undefined'; |
|
| 153 | 152 | |
| 154 | 153 | $topic_id = get_post_meta( |
| 155 | 154 | $post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true |
| 156 | 155 | ); |
| 157 | - $topic_obj = ( $topic_id ) ? |
|
| 158 | - wp_json_encode( wl_serialize_entity( $topic_id ) ) : |
|
| 159 | - 'undefined'; |
|
| 156 | + $topic_obj = ($topic_id) ? |
|
| 157 | + wp_json_encode(wl_serialize_entity($topic_id)) : 'undefined'; |
|
| 160 | 158 | |
| 161 | 159 | $configuration_service = Wordlift_Configuration_Service::get_instance(); |
| 162 | 160 | |
| 163 | 161 | $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH; |
| 164 | 162 | $default_path = WL_DEFAULT_PATH; |
| 165 | 163 | $dataset_uri = $configuration_service->get_dataset_uri(); |
| 166 | - $current_post_uri = wl_get_entity_uri( $post->ID ); |
|
| 164 | + $current_post_uri = wl_get_entity_uri($post->ID); |
|
| 167 | 165 | |
| 168 | 166 | // Retrieve the current post author. |
| 169 | - $post_author = get_userdata( $post->post_author )->display_name; |
|
| 167 | + $post_author = get_userdata($post->post_author)->display_name; |
|
| 170 | 168 | // Retrive the published date. |
| 171 | - $published_date = get_the_time( 'Y-m-d', $post->ID ); |
|
| 169 | + $published_date = get_the_time('Y-m-d', $post->ID); |
|
| 172 | 170 | // Current language. |
| 173 | 171 | $current_language = $configuration_service->get_language_code(); |
| 174 | 172 | |
| 175 | - $js_code = <<<JS |
|
| 173 | + $js_code = <<<JS |
|
| 176 | 174 | if ('undefined' == typeof window.wordlift) { |
| 177 | 175 | window.wordlift = {}; |
| 178 | 176 | window.wordlift.entities = {}; |
@@ -193,7 +191,7 @@ discard block |
||
| 193 | 191 | window.wordlift.currentLanguage = '$current_language'; |
| 194 | 192 | JS; |
| 195 | 193 | |
| 196 | - if($wrapper) echo '<script type="text/javascript">'.PHP_EOL.$js_code.PHP_EOL.'</script>'; |
|
| 194 | + if ($wrapper) echo '<script type="text/javascript">'.PHP_EOL.$js_code.PHP_EOL.'</script>'; |
|
| 197 | 195 | wp_add_inline_script('wl-entity-metabox-utility', $js_code); |
| 198 | 196 | } |
| 199 | 197 | |
@@ -80,7 +80,9 @@ discard block |
||
| 80 | 80 | function wl_entities_box_content( $post, $wrapper = true ) { |
| 81 | 81 | |
| 82 | 82 | // Angularjs edit-post widget wrapper. |
| 83 | - if($wrapper) echo '<div id="wordlift-edit-post-outer-wrapper"></div>'; |
|
| 83 | + if($wrapper) { |
|
| 84 | + echo '<div id="wordlift-edit-post-outer-wrapper"></div>'; |
|
| 85 | + } |
|
| 84 | 86 | |
| 85 | 87 | // Angularjs edit-post widget classification boxes configuration. |
| 86 | 88 | $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
@@ -193,7 +195,9 @@ discard block |
||
| 193 | 195 | window.wordlift.currentLanguage = '$current_language'; |
| 194 | 196 | JS; |
| 195 | 197 | |
| 196 | - if($wrapper) echo '<script type="text/javascript">'.PHP_EOL.$js_code.PHP_EOL.'</script>'; |
|
| 198 | + if($wrapper) { |
|
| 199 | + echo '<script type="text/javascript">'.PHP_EOL.$js_code.PHP_EOL.'</script>'; |
|
| 200 | + } |
|
| 197 | 201 | wp_add_inline_script('wl-entity-metabox-utility', $js_code); |
| 198 | 202 | } |
| 199 | 203 | |
@@ -8,138 +8,138 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Wordlift_Remote_Image_Service { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Save the image with the specified URL locally. |
|
| 13 | - * |
|
| 14 | - * @param string $url The image remote URL. |
|
| 15 | - * |
|
| 16 | - * @since 3.18.0 |
|
| 17 | - * |
|
| 18 | - * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 19 | - * url, *content_type*: the image content type) or false on error. |
|
| 20 | - */ |
|
| 21 | - public static function save_from_url( $url ) { |
|
| 22 | - |
|
| 23 | - // Required for REST API calls |
|
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | - } |
|
| 27 | - // Load `WP_Filesystem`. |
|
| 28 | - WP_Filesystem(); |
|
| 29 | - global $wp_filesystem; |
|
| 30 | - |
|
| 31 | - // Parse the url. |
|
| 32 | - $parts = wp_parse_url( $url ); |
|
| 33 | - |
|
| 34 | - // Get the bare filename (filename w/o the extension). |
|
| 35 | - $basename = pathinfo( $parts['path'], PATHINFO_FILENAME ); |
|
| 36 | - |
|
| 37 | - // Get the base dir. |
|
| 38 | - $wp_upload_dir = wp_upload_dir(); |
|
| 39 | - |
|
| 40 | - // Set the upload directory and URL. |
|
| 41 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 42 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 43 | - |
|
| 44 | - // Get the full path to the local filename. |
|
| 45 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 46 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 47 | - |
|
| 48 | - // Create custom directory and bail on failure. |
|
| 49 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 50 | - wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 51 | - |
|
| 52 | - return false; |
|
| 53 | - }; |
|
| 54 | - |
|
| 55 | - $response = self::get_response( $url ); |
|
| 56 | - |
|
| 57 | - // Bail if the response is not set. |
|
| 58 | - if ( false === $response ) { |
|
| 59 | - wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 60 | - |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Get the content type of response. |
|
| 65 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 66 | - |
|
| 67 | - // Get the file extension. |
|
| 68 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 69 | - |
|
| 70 | - // Bail if the content type is not supported. |
|
| 71 | - if ( empty( $extension ) ) { |
|
| 72 | - return false; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - // Complete the local filename. |
|
| 76 | - $image_full_path .= $extension; |
|
| 77 | - $image_full_url .= $extension; |
|
| 78 | - |
|
| 79 | - // Store the data locally. |
|
| 80 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 81 | - |
|
| 82 | - // Return the path. |
|
| 83 | - return array( |
|
| 84 | - 'path' => $image_full_path, |
|
| 85 | - 'url' => $image_full_url, |
|
| 86 | - 'content_type' => $content_type, |
|
| 87 | - ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Returns the file extension using the content type. |
|
| 92 | - * |
|
| 93 | - * @param string $content_type File content type. |
|
| 94 | - * |
|
| 95 | - * @since 3.18.0 |
|
| 96 | - * |
|
| 97 | - * @return string|bool The file extension on success and |
|
| 98 | - * false on fail or if the content type is not supported. |
|
| 99 | - */ |
|
| 100 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 101 | - |
|
| 102 | - // Return the extension if match. |
|
| 103 | - switch ( $content_type ) { |
|
| 104 | - case 'image/jpeg': |
|
| 105 | - case 'image/jpg': |
|
| 106 | - return '.jpg'; |
|
| 107 | - case 'image/gif': |
|
| 108 | - return '.gif'; |
|
| 109 | - case 'image/png': |
|
| 110 | - return '.png'; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // Otherwise return false. |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Retrieve the response from url and sets the response. |
|
| 119 | - * |
|
| 120 | - * @param string $url The url to retrieve. |
|
| 121 | - * |
|
| 122 | - * @since 3.18.0 |
|
| 123 | - * |
|
| 124 | - * @return false|array True on success and false on failure. |
|
| 125 | - */ |
|
| 126 | - private static function get_response( $url ) { |
|
| 127 | - // Request the remote file. |
|
| 128 | - $response = wp_remote_get( $url ); |
|
| 129 | - |
|
| 130 | - // Bail out if the response is not ok. |
|
| 131 | - if ( |
|
| 132 | - is_wp_error( $response ) |
|
| 133 | - || 200 !== (int) $response['response']['code'] |
|
| 134 | - || ! isset( $response['body'] ) |
|
| 135 | - ) { |
|
| 136 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 137 | - |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // Set the response. |
|
| 142 | - return $response; |
|
| 143 | - } |
|
| 11 | + /** |
|
| 12 | + * Save the image with the specified URL locally. |
|
| 13 | + * |
|
| 14 | + * @param string $url The image remote URL. |
|
| 15 | + * |
|
| 16 | + * @since 3.18.0 |
|
| 17 | + * |
|
| 18 | + * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 19 | + * url, *content_type*: the image content type) or false on error. |
|
| 20 | + */ |
|
| 21 | + public static function save_from_url( $url ) { |
|
| 22 | + |
|
| 23 | + // Required for REST API calls |
|
| 24 | + if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | + } |
|
| 27 | + // Load `WP_Filesystem`. |
|
| 28 | + WP_Filesystem(); |
|
| 29 | + global $wp_filesystem; |
|
| 30 | + |
|
| 31 | + // Parse the url. |
|
| 32 | + $parts = wp_parse_url( $url ); |
|
| 33 | + |
|
| 34 | + // Get the bare filename (filename w/o the extension). |
|
| 35 | + $basename = pathinfo( $parts['path'], PATHINFO_FILENAME ); |
|
| 36 | + |
|
| 37 | + // Get the base dir. |
|
| 38 | + $wp_upload_dir = wp_upload_dir(); |
|
| 39 | + |
|
| 40 | + // Set the upload directory and URL. |
|
| 41 | + $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 42 | + $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 43 | + |
|
| 44 | + // Get the full path to the local filename. |
|
| 45 | + $image_full_path = $upload_dir . '/' . $basename; |
|
| 46 | + $image_full_url = $upload_url . '/' . $basename; |
|
| 47 | + |
|
| 48 | + // Create custom directory and bail on failure. |
|
| 49 | + if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 50 | + wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 51 | + |
|
| 52 | + return false; |
|
| 53 | + }; |
|
| 54 | + |
|
| 55 | + $response = self::get_response( $url ); |
|
| 56 | + |
|
| 57 | + // Bail if the response is not set. |
|
| 58 | + if ( false === $response ) { |
|
| 59 | + wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 60 | + |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Get the content type of response. |
|
| 65 | + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 66 | + |
|
| 67 | + // Get the file extension. |
|
| 68 | + $extension = self::get_extension_from_content_type( $content_type ); |
|
| 69 | + |
|
| 70 | + // Bail if the content type is not supported. |
|
| 71 | + if ( empty( $extension ) ) { |
|
| 72 | + return false; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + // Complete the local filename. |
|
| 76 | + $image_full_path .= $extension; |
|
| 77 | + $image_full_url .= $extension; |
|
| 78 | + |
|
| 79 | + // Store the data locally. |
|
| 80 | + $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 81 | + |
|
| 82 | + // Return the path. |
|
| 83 | + return array( |
|
| 84 | + 'path' => $image_full_path, |
|
| 85 | + 'url' => $image_full_url, |
|
| 86 | + 'content_type' => $content_type, |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Returns the file extension using the content type. |
|
| 92 | + * |
|
| 93 | + * @param string $content_type File content type. |
|
| 94 | + * |
|
| 95 | + * @since 3.18.0 |
|
| 96 | + * |
|
| 97 | + * @return string|bool The file extension on success and |
|
| 98 | + * false on fail or if the content type is not supported. |
|
| 99 | + */ |
|
| 100 | + private static function get_extension_from_content_type( $content_type ) { |
|
| 101 | + |
|
| 102 | + // Return the extension if match. |
|
| 103 | + switch ( $content_type ) { |
|
| 104 | + case 'image/jpeg': |
|
| 105 | + case 'image/jpg': |
|
| 106 | + return '.jpg'; |
|
| 107 | + case 'image/gif': |
|
| 108 | + return '.gif'; |
|
| 109 | + case 'image/png': |
|
| 110 | + return '.png'; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // Otherwise return false. |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Retrieve the response from url and sets the response. |
|
| 119 | + * |
|
| 120 | + * @param string $url The url to retrieve. |
|
| 121 | + * |
|
| 122 | + * @since 3.18.0 |
|
| 123 | + * |
|
| 124 | + * @return false|array True on success and false on failure. |
|
| 125 | + */ |
|
| 126 | + private static function get_response( $url ) { |
|
| 127 | + // Request the remote file. |
|
| 128 | + $response = wp_remote_get( $url ); |
|
| 129 | + |
|
| 130 | + // Bail out if the response is not ok. |
|
| 131 | + if ( |
|
| 132 | + is_wp_error( $response ) |
|
| 133 | + || 200 !== (int) $response['response']['code'] |
|
| 134 | + || ! isset( $response['body'] ) |
|
| 135 | + ) { |
|
| 136 | + wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 137 | + |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // Set the response. |
|
| 142 | + return $response; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | } |
@@ -18,57 +18,57 @@ discard block |
||
| 18 | 18 | * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
| 19 | 19 | * url, *content_type*: the image content type) or false on error. |
| 20 | 20 | */ |
| 21 | - public static function save_from_url( $url ) { |
|
| 21 | + public static function save_from_url($url) { |
|
| 22 | 22 | |
| 23 | 23 | // Required for REST API calls |
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 24 | + if ( ! function_exists('WP_Filesystem')) { |
|
| 25 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
| 26 | 26 | } |
| 27 | 27 | // Load `WP_Filesystem`. |
| 28 | 28 | WP_Filesystem(); |
| 29 | 29 | global $wp_filesystem; |
| 30 | 30 | |
| 31 | 31 | // Parse the url. |
| 32 | - $parts = wp_parse_url( $url ); |
|
| 32 | + $parts = wp_parse_url($url); |
|
| 33 | 33 | |
| 34 | 34 | // Get the bare filename (filename w/o the extension). |
| 35 | - $basename = pathinfo( $parts['path'], PATHINFO_FILENAME ); |
|
| 35 | + $basename = pathinfo($parts['path'], PATHINFO_FILENAME); |
|
| 36 | 36 | |
| 37 | 37 | // Get the base dir. |
| 38 | 38 | $wp_upload_dir = wp_upload_dir(); |
| 39 | 39 | |
| 40 | 40 | // Set the upload directory and URL. |
| 41 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 42 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 41 | + $upload_dir = $wp_upload_dir['basedir'].'/wl'.$wp_upload_dir['subdir']; |
|
| 42 | + $upload_url = $wp_upload_dir['baseurl'].'/wl'.$wp_upload_dir['subdir']; |
|
| 43 | 43 | |
| 44 | 44 | // Get the full path to the local filename. |
| 45 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 46 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 45 | + $image_full_path = $upload_dir.'/'.$basename; |
|
| 46 | + $image_full_url = $upload_url.'/'.$basename; |
|
| 47 | 47 | |
| 48 | 48 | // Create custom directory and bail on failure. |
| 49 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 50 | - wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 49 | + if ( ! wp_mkdir_p($upload_dir)) { |
|
| 50 | + wl_write_log("save_image_from_url : failed creating upload dir $upload_dir \n"); |
|
| 51 | 51 | |
| 52 | 52 | return false; |
| 53 | 53 | }; |
| 54 | 54 | |
| 55 | - $response = self::get_response( $url ); |
|
| 55 | + $response = self::get_response($url); |
|
| 56 | 56 | |
| 57 | 57 | // Bail if the response is not set. |
| 58 | - if ( false === $response ) { |
|
| 59 | - wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 58 | + if (false === $response) { |
|
| 59 | + wl_write_log("save_image_from_url : failed to fetch the response from: $url \n"); |
|
| 60 | 60 | |
| 61 | 61 | return false; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | // Get the content type of response. |
| 65 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 65 | + $content_type = wp_remote_retrieve_header($response, 'content-type'); |
|
| 66 | 66 | |
| 67 | 67 | // Get the file extension. |
| 68 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 68 | + $extension = self::get_extension_from_content_type($content_type); |
|
| 69 | 69 | |
| 70 | 70 | // Bail if the content type is not supported. |
| 71 | - if ( empty( $extension ) ) { |
|
| 71 | + if (empty($extension)) { |
|
| 72 | 72 | return false; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | $image_full_url .= $extension; |
| 78 | 78 | |
| 79 | 79 | // Store the data locally. |
| 80 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 80 | + $wp_filesystem->put_contents($image_full_path, wp_remote_retrieve_body($response)); |
|
| 81 | 81 | |
| 82 | 82 | // Return the path. |
| 83 | 83 | return array( |
@@ -97,10 +97,10 @@ discard block |
||
| 97 | 97 | * @return string|bool The file extension on success and |
| 98 | 98 | * false on fail or if the content type is not supported. |
| 99 | 99 | */ |
| 100 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 100 | + private static function get_extension_from_content_type($content_type) { |
|
| 101 | 101 | |
| 102 | 102 | // Return the extension if match. |
| 103 | - switch ( $content_type ) { |
|
| 103 | + switch ($content_type) { |
|
| 104 | 104 | case 'image/jpeg': |
| 105 | 105 | case 'image/jpg': |
| 106 | 106 | return '.jpg'; |
@@ -123,17 +123,17 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @return false|array True on success and false on failure. |
| 125 | 125 | */ |
| 126 | - private static function get_response( $url ) { |
|
| 126 | + private static function get_response($url) { |
|
| 127 | 127 | // Request the remote file. |
| 128 | - $response = wp_remote_get( $url ); |
|
| 128 | + $response = wp_remote_get($url); |
|
| 129 | 129 | |
| 130 | 130 | // Bail out if the response is not ok. |
| 131 | 131 | if ( |
| 132 | - is_wp_error( $response ) |
|
| 132 | + is_wp_error($response) |
|
| 133 | 133 | || 200 !== (int) $response['response']['code'] |
| 134 | - || ! isset( $response['body'] ) |
|
| 134 | + || ! isset($response['body']) |
|
| 135 | 135 | ) { |
| 136 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 136 | + wl_write_log("save_image_from_url : error fetching image $url \n"); |
|
| 137 | 137 | |
| 138 | 138 | return false; |
| 139 | 139 | } |
@@ -14,83 +14,83 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Entity_Type_Taxonomy_Service { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The WordPress taxonomy name. |
|
| 19 | - * |
|
| 20 | - * @since 1.0.0 |
|
| 21 | - */ |
|
| 22 | - const TAXONOMY_NAME = 'wl_entity_type'; |
|
| 17 | + /** |
|
| 18 | + * The WordPress taxonomy name. |
|
| 19 | + * |
|
| 20 | + * @since 1.0.0 |
|
| 21 | + */ |
|
| 22 | + const TAXONOMY_NAME = 'wl_entity_type'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Register the taxonomies. |
|
| 26 | - * |
|
| 27 | - * @since 3.18.0 |
|
| 28 | - */ |
|
| 29 | - public function init() { |
|
| 24 | + /** |
|
| 25 | + * Register the taxonomies. |
|
| 26 | + * |
|
| 27 | + * @since 3.18.0 |
|
| 28 | + */ |
|
| 29 | + public function init() { |
|
| 30 | 30 | |
| 31 | - $labels = array( |
|
| 32 | - 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 33 | - 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 34 | - 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 35 | - 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 36 | - 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 37 | - 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 38 | - 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 39 | - 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 40 | - 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 41 | - 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 42 | - 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 43 | - ); |
|
| 31 | + $labels = array( |
|
| 32 | + 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 33 | + 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 34 | + 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 35 | + 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 36 | + 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 37 | + 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 38 | + 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 39 | + 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 40 | + 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 41 | + 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 42 | + 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 43 | + ); |
|
| 44 | 44 | |
| 45 | - // Take away GUI for taxonomy editing. |
|
| 46 | - // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
| 47 | - $capabilities = array( |
|
| 48 | - // We enable editors to change the title/description of terms: |
|
| 49 | - // |
|
| 50 | - // @see https://github.com/insideout10/wordlift-plugin/issues/398. |
|
| 51 | - 'manage_terms' => 'manage_options', |
|
| 52 | - 'edit_terms' => 'wl_entity_type_edit_term', |
|
| 53 | - 'delete_terms' => 'wl_entity_type_delete_term', |
|
| 54 | - 'assign_terms' => 'edit_posts', |
|
| 55 | - ); |
|
| 45 | + // Take away GUI for taxonomy editing. |
|
| 46 | + // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
| 47 | + $capabilities = array( |
|
| 48 | + // We enable editors to change the title/description of terms: |
|
| 49 | + // |
|
| 50 | + // @see https://github.com/insideout10/wordlift-plugin/issues/398. |
|
| 51 | + 'manage_terms' => 'manage_options', |
|
| 52 | + 'edit_terms' => 'wl_entity_type_edit_term', |
|
| 53 | + 'delete_terms' => 'wl_entity_type_delete_term', |
|
| 54 | + 'assign_terms' => 'edit_posts', |
|
| 55 | + ); |
|
| 56 | 56 | |
| 57 | - $args = array( |
|
| 58 | - 'labels' => $labels, |
|
| 59 | - 'capabilities' => $capabilities, |
|
| 60 | - 'hierarchical' => true, |
|
| 61 | - 'show_admin_column' => true, |
|
| 62 | - 'show_in_rest' => true, |
|
| 63 | - 'show_in_quick_edit' => false, |
|
| 64 | - ); |
|
| 57 | + $args = array( |
|
| 58 | + 'labels' => $labels, |
|
| 59 | + 'capabilities' => $capabilities, |
|
| 60 | + 'hierarchical' => true, |
|
| 61 | + 'show_admin_column' => true, |
|
| 62 | + 'show_in_rest' => true, |
|
| 63 | + 'show_in_quick_edit' => false, |
|
| 64 | + ); |
|
| 65 | 65 | |
| 66 | - /* |
|
| 66 | + /* |
|
| 67 | 67 | * If `All Entity Types` is enabled, use the new metabox. |
| 68 | 68 | * |
| 69 | 69 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 70 | 70 | * @since 3.20.0 |
| 71 | 71 | */ |
| 72 | - if ( WL_ALL_ENTITY_TYPES ) { |
|
| 73 | - $args['meta_box_cb'] = array( 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render' ); |
|
| 74 | - } |
|
| 72 | + if ( WL_ALL_ENTITY_TYPES ) { |
|
| 73 | + $args['meta_box_cb'] = array( 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render' ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - register_taxonomy( |
|
| 77 | - self::TAXONOMY_NAME, // Taxonomy name. |
|
| 78 | - Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
| 79 | - $args // Taxonomy args. |
|
| 80 | - ); |
|
| 76 | + register_taxonomy( |
|
| 77 | + self::TAXONOMY_NAME, // Taxonomy name. |
|
| 78 | + Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
| 79 | + $args // Taxonomy args. |
|
| 80 | + ); |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Register meta wl_entities_gutenberg for use in Gutenberg |
|
| 84 | - */ |
|
| 85 | - register_meta( 'post', 'wl_entities_gutenberg', array( |
|
| 86 | - 'show_in_rest' => true, |
|
| 87 | - 'single' => true, |
|
| 88 | - 'type' => 'string', |
|
| 89 | - ) ); |
|
| 82 | + /** |
|
| 83 | + * Register meta wl_entities_gutenberg for use in Gutenberg |
|
| 84 | + */ |
|
| 85 | + register_meta( 'post', 'wl_entities_gutenberg', array( |
|
| 86 | + 'show_in_rest' => true, |
|
| 87 | + 'single' => true, |
|
| 88 | + 'type' => 'string', |
|
| 89 | + ) ); |
|
| 90 | 90 | |
| 91 | - // Add filter to change the metabox CSS class. |
|
| 92 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 91 | + // Add filter to change the metabox CSS class. |
|
| 92 | + add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 93 | 93 | |
| 94 | - } |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | 96 | } |
@@ -29,17 +29,17 @@ discard block |
||
| 29 | 29 | public function init() { |
| 30 | 30 | |
| 31 | 31 | $labels = array( |
| 32 | - 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 33 | - 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 34 | - 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 35 | - 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 36 | - 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 37 | - 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 38 | - 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 39 | - 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 40 | - 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 41 | - 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 42 | - 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 32 | + 'name' => _x('Entity Types', 'taxonomy general name', 'wordlift'), |
|
| 33 | + 'singular_name' => _x('Entity Type', 'taxonomy singular name', 'wordlift'), |
|
| 34 | + 'search_items' => __('Search Entity Types', 'wordlift'), |
|
| 35 | + 'all_items' => __('All Entity Types', 'wordlift'), |
|
| 36 | + 'parent_item' => __('Parent Entity Type', 'wordlift'), |
|
| 37 | + 'parent_item_colon' => __('Parent Entity Type:', 'wordlift'), |
|
| 38 | + 'edit_item' => __('Edit Entity Type', 'wordlift'), |
|
| 39 | + 'update_item' => __('Update Entity Type', 'wordlift'), |
|
| 40 | + 'add_new_item' => __('Add New Entity Type', 'wordlift'), |
|
| 41 | + 'new_item_name' => __('New Entity Type', 'wordlift'), |
|
| 42 | + 'menu_name' => __('Entity Types', 'wordlift'), |
|
| 43 | 43 | ); |
| 44 | 44 | |
| 45 | 45 | // Take away GUI for taxonomy editing. |
@@ -69,8 +69,8 @@ discard block |
||
| 69 | 69 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 70 | 70 | * @since 3.20.0 |
| 71 | 71 | */ |
| 72 | - if ( WL_ALL_ENTITY_TYPES ) { |
|
| 73 | - $args['meta_box_cb'] = array( 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render' ); |
|
| 72 | + if (WL_ALL_ENTITY_TYPES) { |
|
| 73 | + $args['meta_box_cb'] = array('Wordlift_Admin_Schemaorg_Taxonomy_Metabox', 'render'); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | register_taxonomy( |
@@ -82,14 +82,14 @@ discard block |
||
| 82 | 82 | /** |
| 83 | 83 | * Register meta wl_entities_gutenberg for use in Gutenberg |
| 84 | 84 | */ |
| 85 | - register_meta( 'post', 'wl_entities_gutenberg', array( |
|
| 85 | + register_meta('post', 'wl_entities_gutenberg', array( |
|
| 86 | 86 | 'show_in_rest' => true, |
| 87 | 87 | 'single' => true, |
| 88 | 88 | 'type' => 'string', |
| 89 | - ) ); |
|
| 89 | + )); |
|
| 90 | 90 | |
| 91 | 91 | // Add filter to change the metabox CSS class. |
| 92 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 92 | + add_filter('postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class'); |
|
| 93 | 93 | |
| 94 | 94 | } |
| 95 | 95 | |