Completed
Pull Request — master (#1545)
by Naveen
01:50 queued 28s
created
src/modules/linked_data/wordlift_linked_data.php 2 patches
Indentation   +424 added lines, -424 removed lines patch added patch discarded remove patch
@@ -22,62 +22,62 @@  discard block
 block discarded – undo
22 22
  */
23 23
 function wl_linked_data_save_post( $post_id ) {
24 24
 
25
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
26
-
27
-	$log->trace( "Saving post $post_id to Linked Data..." );
28
-
29
-	// If it's not numeric exit from here.
30
-	if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
31
-		$log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
32
-
33
-		return;
34
-	}
35
-
36
-	// Get the post type and check whether it supports the editor.
37
-	//
38
-	// @see https://github.com/insideout10/wordlift-plugin/issues/659.
39
-	$post_type = get_post_type( $post_id );
40
-	/**
41
-	 * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
42
-	 *
43
-	 * @since 3.19.4
44
-	 *
45
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
46
-	 */
47
-	$is_editor_supported = wl_post_type_supports_editor( $post_type );
48
-
49
-	$is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id );
50
-	// Bail out if it's not an entity.
51
-	if ( ! $is_editor_supported
52
-	     && ! $is_no_editor_analysis_enabled ) {
53
-		$log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
54
-
55
-		return;
56
-	}
57
-
58
-	/**
59
-	 * Only process valid post types
60
-	 *
61
-	 * @since 3.25.6
62
-	 *
63
-	 */
64
-	$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
65
-
66
-	// Bail out if it's not a valid entity.
67
-	if ( ! in_array( $post_type, $supported_types ) && ! $is_no_editor_analysis_enabled ) {
68
-		$log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
69
-
70
-		return;
71
-	}
72
-
73
-	// Unhook this function so it doesn't loop infinitely.
74
-	remove_action( 'save_post', 'wl_linked_data_save_post' );
75
-
76
-	// raise the *wl_linked_data_save_post* event.
77
-	do_action( 'wl_linked_data_save_post', $post_id );
78
-
79
-	// Re-hook this function.
80
-	add_action( 'save_post', 'wl_linked_data_save_post' );
25
+    $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
26
+
27
+    $log->trace( "Saving post $post_id to Linked Data..." );
28
+
29
+    // If it's not numeric exit from here.
30
+    if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
31
+        $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
32
+
33
+        return;
34
+    }
35
+
36
+    // Get the post type and check whether it supports the editor.
37
+    //
38
+    // @see https://github.com/insideout10/wordlift-plugin/issues/659.
39
+    $post_type = get_post_type( $post_id );
40
+    /**
41
+     * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
42
+     *
43
+     * @since 3.19.4
44
+     *
45
+     * @see https://github.com/insideout10/wordlift-plugin/issues/847.
46
+     */
47
+    $is_editor_supported = wl_post_type_supports_editor( $post_type );
48
+
49
+    $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id );
50
+    // Bail out if it's not an entity.
51
+    if ( ! $is_editor_supported
52
+         && ! $is_no_editor_analysis_enabled ) {
53
+        $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
54
+
55
+        return;
56
+    }
57
+
58
+    /**
59
+     * Only process valid post types
60
+     *
61
+     * @since 3.25.6
62
+     *
63
+     */
64
+    $supported_types = Wordlift_Entity_Service::valid_entity_post_types();
65
+
66
+    // Bail out if it's not a valid entity.
67
+    if ( ! in_array( $post_type, $supported_types ) && ! $is_no_editor_analysis_enabled ) {
68
+        $log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
69
+
70
+        return;
71
+    }
72
+
73
+    // Unhook this function so it doesn't loop infinitely.
74
+    remove_action( 'save_post', 'wl_linked_data_save_post' );
75
+
76
+    // raise the *wl_linked_data_save_post* event.
77
+    do_action( 'wl_linked_data_save_post', $post_id );
78
+
79
+    // Re-hook this function.
80
+    add_action( 'save_post', 'wl_linked_data_save_post' );
81 81
 }
82 82
 
83 83
 add_action( 'save_post', 'wl_linked_data_save_post' );
@@ -92,182 +92,182 @@  discard block
 block discarded – undo
92 92
  */
93 93
 function wl_linked_data_save_post_and_related_entities( $post_id ) {
94 94
 
95
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
96
-
97
-	$log->trace( "Saving $post_id to Linked Data along with related entities..." );
98
-
99
-	// Ignore auto-saves
100
-	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
101
-		$log->trace( 'Doing autosave, skipping...' );
102
-
103
-		return;
104
-	}
105
-
106
-	// get the current post.
107
-	$post = get_post( $post_id );
108
-
109
-	remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
110
-
111
-	// wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
112
-
113
-	// Get the entity service instance.
114
-	$entity_service  = Wordlift_Entity_Service::get_instance();
115
-	$uri_service     = Wordlift_Entity_Uri_Service::get_instance();
116
-	$content_service = Wordpress_Content_Service::get_instance();
117
-
118
-	// Store mapping between tmp new entities uris and real new entities uri
119
-	$entities_uri_mapping = array();
120
-
121
-	// Save all the selected internal entity uris to this variable.
122
-	$internal_entity_uris = array();
123
-
124
-	// Save the entities coming with POST data.
125
-	if ( isset( $_POST['wl_entities'] ) ) {
126
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
127
-		wl_write_log( json_encode( $_POST['wl_entities'] ) );
128
-		wl_write_log( "]" );
129
-
130
-		$entities_via_post = $_POST['wl_entities'];
131
-
132
-		foreach ( $entities_via_post as $entity_uri => $entity ) {
133
-
134
-			if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) {
135
-				$existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() );
136
-				if ( isset( $existing_entity ) ) {
137
-					$existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID );
138
-					// Type doesn't match, continue to create a new entity.
139
-					if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) {
140
-						$existing_entity = null;
141
-					}
142
-				}
143
-			} else {
144
-				// Look if current entity uri matches an internal existing entity, meaning:
145
-				// 1. when $entity_uri is an internal uri
146
-				// 2. when $entity_uri is an external uri used as sameAs of an internal entity
147
-				$existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri );
148
-			}
149
-
150
-			// Don't save the entities which are not found, but also local.
151
-			if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) {
152
-				$internal_entity_uris[] = $entity_uri;
153
-				continue;
154
-			}
155
-
156
-			if ( ! isset( $existing_entity ) ) {
157
-				// Update entity data with related post
158
-				$entity['related_post_id'] = $post_id;
159
-				// New entity, save it.
160
-				$existing_entity = wl_save_entity( $entity );
161
-			} else {
162
-				// Existing entity, update post status.
163
-				if ( $existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish' ) {
164
-					$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
165
-						? $post->post_status : 'draft';
166
-					wl_update_post_status( $existing_entity->ID, $post_status );
167
-				}
168
-			}
169
-
170
-			$uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
171
-
172
-			$internal_entity_uris[] = $uri;
173
-			wl_write_log( "Map $entity_uri on $uri" );
174
-			$entities_uri_mapping[ $entity_uri ] = $uri;
175
-
176
-		}
177
-
178
-	}
179
-
180
-	// Replace tmp uris in content post if needed
181
-	$updated_post_content = $post->post_content;
182
-
183
-	// Update the post content if we found mappings of new entities.
184
-	if ( ! empty( $entities_uri_mapping ) ) {
185
-		// Save each entity and store the post id.
186
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
187
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
188
-				continue;
189
-			}
190
-
191
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
192
-		}
193
-
194
-		// Update the post content.
195
-		/**
196
-		 * Note: wp_update_post do stripslashes before saving the
197
-		 * content, so add the slashes to prevent back slash getting
198
-		 * removed.
199
-		 */
200
-		wp_update_post( array(
201
-			'ID'           => $post->ID,
202
-			'post_content' => addslashes( $updated_post_content ),
203
-		) );
204
-	}
205
-
206
-	// Reset previously saved instances.
207
-	wl_core_delete_relation_instances( $post_id );
208
-
209
-	$relations = Object_Relation_Factory::get_instance( $post_id )
210
-	                                    ->get_relations_from_content(
211
-		                                    $updated_post_content,
212
-		                                    Object_Type_Enum::POST,
213
-		                                    $internal_entity_uris
214
-	                                    );
215
-
216
-	// Save relation instances
217
-	foreach ( $relations as $relation ) {
218
-
219
-		wl_core_add_relation_instance(
220
-		// subject id.
221
-			$post_id,
222
-			// what, where, when, who
223
-			$relation->get_relation_type(),
224
-			// object id.
225
-			$relation->get_object_id(),
226
-			// Subject type.
227
-			$relation->get_subject_type(),
228
-			// Object type.
229
-			$relation->get_object_type()
230
-		);
231
-
232
-	}
233
-
234
-	if ( isset( $_POST['wl_entities'] ) ) {
235
-		// Save post metadata if available
236
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
237
-			$_POST['wl_metadata'] : array();
238
-
239
-		$fields = array(
240
-			Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
241
-			Wordlift_Schema_Service::FIELD_TOPIC,
242
-		);
243
-
244
-		// Unlink topic taxonomy terms
245
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
246
-
247
-		foreach ( $fields as $field ) {
248
-
249
-			// Delete current values
250
-			delete_post_meta( $post->ID, $field );
251
-			// Retrieve the entity uri
252
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
253
-				stripslashes( $metadata_via_post[ $field ] ) : '';
254
-
255
-			if ( empty( $uri ) ) {
256
-				continue;
257
-			}
258
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
259
-
260
-			if ( $entity ) {
261
-				add_post_meta( $post->ID, $field, $entity->ID, true );
262
-				// Set also the topic taxonomy
263
-				if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
264
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
265
-				}
266
-			}
267
-		}
268
-	}
269
-
270
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
95
+    $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
96
+
97
+    $log->trace( "Saving $post_id to Linked Data along with related entities..." );
98
+
99
+    // Ignore auto-saves
100
+    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
101
+        $log->trace( 'Doing autosave, skipping...' );
102
+
103
+        return;
104
+    }
105
+
106
+    // get the current post.
107
+    $post = get_post( $post_id );
108
+
109
+    remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
110
+
111
+    // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
112
+
113
+    // Get the entity service instance.
114
+    $entity_service  = Wordlift_Entity_Service::get_instance();
115
+    $uri_service     = Wordlift_Entity_Uri_Service::get_instance();
116
+    $content_service = Wordpress_Content_Service::get_instance();
117
+
118
+    // Store mapping between tmp new entities uris and real new entities uri
119
+    $entities_uri_mapping = array();
120
+
121
+    // Save all the selected internal entity uris to this variable.
122
+    $internal_entity_uris = array();
123
+
124
+    // Save the entities coming with POST data.
125
+    if ( isset( $_POST['wl_entities'] ) ) {
126
+        wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
127
+        wl_write_log( json_encode( $_POST['wl_entities'] ) );
128
+        wl_write_log( "]" );
129
+
130
+        $entities_via_post = $_POST['wl_entities'];
131
+
132
+        foreach ( $entities_via_post as $entity_uri => $entity ) {
133
+
134
+            if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) {
135
+                $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() );
136
+                if ( isset( $existing_entity ) ) {
137
+                    $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID );
138
+                    // Type doesn't match, continue to create a new entity.
139
+                    if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) {
140
+                        $existing_entity = null;
141
+                    }
142
+                }
143
+            } else {
144
+                // Look if current entity uri matches an internal existing entity, meaning:
145
+                // 1. when $entity_uri is an internal uri
146
+                // 2. when $entity_uri is an external uri used as sameAs of an internal entity
147
+                $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri );
148
+            }
149
+
150
+            // Don't save the entities which are not found, but also local.
151
+            if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) {
152
+                $internal_entity_uris[] = $entity_uri;
153
+                continue;
154
+            }
155
+
156
+            if ( ! isset( $existing_entity ) ) {
157
+                // Update entity data with related post
158
+                $entity['related_post_id'] = $post_id;
159
+                // New entity, save it.
160
+                $existing_entity = wl_save_entity( $entity );
161
+            } else {
162
+                // Existing entity, update post status.
163
+                if ( $existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish' ) {
164
+                    $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
165
+                        ? $post->post_status : 'draft';
166
+                    wl_update_post_status( $existing_entity->ID, $post_status );
167
+                }
168
+            }
169
+
170
+            $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
171
+
172
+            $internal_entity_uris[] = $uri;
173
+            wl_write_log( "Map $entity_uri on $uri" );
174
+            $entities_uri_mapping[ $entity_uri ] = $uri;
175
+
176
+        }
177
+
178
+    }
179
+
180
+    // Replace tmp uris in content post if needed
181
+    $updated_post_content = $post->post_content;
182
+
183
+    // Update the post content if we found mappings of new entities.
184
+    if ( ! empty( $entities_uri_mapping ) ) {
185
+        // Save each entity and store the post id.
186
+        foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
187
+            if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
188
+                continue;
189
+            }
190
+
191
+            $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
192
+        }
193
+
194
+        // Update the post content.
195
+        /**
196
+         * Note: wp_update_post do stripslashes before saving the
197
+         * content, so add the slashes to prevent back slash getting
198
+         * removed.
199
+         */
200
+        wp_update_post( array(
201
+            'ID'           => $post->ID,
202
+            'post_content' => addslashes( $updated_post_content ),
203
+        ) );
204
+    }
205
+
206
+    // Reset previously saved instances.
207
+    wl_core_delete_relation_instances( $post_id );
208
+
209
+    $relations = Object_Relation_Factory::get_instance( $post_id )
210
+                                        ->get_relations_from_content(
211
+                                            $updated_post_content,
212
+                                            Object_Type_Enum::POST,
213
+                                            $internal_entity_uris
214
+                                        );
215
+
216
+    // Save relation instances
217
+    foreach ( $relations as $relation ) {
218
+
219
+        wl_core_add_relation_instance(
220
+        // subject id.
221
+            $post_id,
222
+            // what, where, when, who
223
+            $relation->get_relation_type(),
224
+            // object id.
225
+            $relation->get_object_id(),
226
+            // Subject type.
227
+            $relation->get_subject_type(),
228
+            // Object type.
229
+            $relation->get_object_type()
230
+        );
231
+
232
+    }
233
+
234
+    if ( isset( $_POST['wl_entities'] ) ) {
235
+        // Save post metadata if available
236
+        $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
237
+            $_POST['wl_metadata'] : array();
238
+
239
+        $fields = array(
240
+            Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
241
+            Wordlift_Schema_Service::FIELD_TOPIC,
242
+        );
243
+
244
+        // Unlink topic taxonomy terms
245
+        Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
246
+
247
+        foreach ( $fields as $field ) {
248
+
249
+            // Delete current values
250
+            delete_post_meta( $post->ID, $field );
251
+            // Retrieve the entity uri
252
+            $uri = ( isset( $metadata_via_post[ $field ] ) ) ?
253
+                stripslashes( $metadata_via_post[ $field ] ) : '';
254
+
255
+            if ( empty( $uri ) ) {
256
+                continue;
257
+            }
258
+            $entity = $entity_service->get_entity_post_by_uri( $uri );
259
+
260
+            if ( $entity ) {
261
+                add_post_meta( $post->ID, $field, $entity->ID, true );
262
+                // Set also the topic taxonomy
263
+                if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
264
+                    Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
265
+                }
266
+            }
267
+        }
268
+    }
269
+
270
+    add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
271 271
 }
272 272
 
273 273
 add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
@@ -290,204 +290,204 @@  discard block
 block discarded – undo
290 290
  */
291 291
 function wl_save_entity( $entity_data ) {
292 292
 
293
-	// Required for REST API calls
294
-	if ( ! function_exists( 'wp_crop_image' ) ) {
295
-		require_once( ABSPATH . 'wp-admin/includes/image.php' );
296
-	}
293
+    // Required for REST API calls
294
+    if ( ! function_exists( 'wp_crop_image' ) ) {
295
+        require_once( ABSPATH . 'wp-admin/includes/image.php' );
296
+    }
297 297
 
298
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
298
+    $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
299 299
 
300
-	/*
300
+    /*
301 301
 	 * Data is coming from a $_POST, sanitize it.
302 302
 	 *
303 303
 	 * @since 3.19.4
304 304
 	 *
305 305
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
306 306
 	 */
307
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
308
-	$type_uri         = $entity_data['main_type'];
309
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
310
-	$description      = $entity_data['description'];
311
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
312
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
313
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
314
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
315
-	// Get the synonyms.
316
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
317
-
318
-	// Check whether an entity already exists with the provided URI.
319
-	$uri = $entity_data['uri'];
320
-	if ( isset ( $uri ) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
321
-		$log->debug( "Post already exists for URI $uri." );
322
-
323
-		return $post;
324
-	}
325
-
326
-	// Prepare properties of the new entity.
327
-	$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
328
-		? get_post_status( $related_post_id ) : 'draft';
329
-
330
-	$params = array(
331
-		// @@todo: we don't want an entity to be automatically published.
332
-		'post_status'  => $post_status,
333
-		'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
334
-		'post_title'   => $label,
335
-		'post_content' => $description,
336
-		'post_excerpt' => '',
337
-		// Ensure we're using a valid slug. We're not overwriting an existing
338
-		// entity with a post_name already set, since this call is made only for
339
-		// new entities.
340
-		//
341
-		// See https://github.com/insideout10/wordlift-plugin/issues/282
342
-		'post_name'    => sanitize_title( $label ),
343
-	);
344
-
345
-	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
346
-	// the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
347
-	// Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
348
-	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
349
-	// is created when saving a post.
350
-	global $wpseo_metabox, $seo_ultimate;
351
-	if ( isset( $wpseo_metabox ) ) {
352
-		remove_action( 'wp_insert_post', array(
353
-			$wpseo_metabox,
354
-			'save_postdata',
355
-		) );
356
-	}
357
-
358
-	if ( isset( $seo_ultimate ) ) {
359
-		remove_action( 'save_post', array(
360
-			$seo_ultimate,
361
-			'save_postmeta_box',
362
-		) );
363
-	}
364
-
365
-	// The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
366
-	// save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
367
-	// to the save_post and restore them after we saved the entity.
368
-	// see https://github.com/insideout10/wordlift-plugin/issues/203
369
-	// see https://github.com/insideout10/wordlift-plugin/issues/156
370
-	// see https://github.com/insideout10/wordlift-plugin/issues/148
371
-	global $wp_filter;
372
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
373
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
374
-
375
-
376
-	$log->trace( 'Going to insert new post...' );
377
-
378
-	// create or update the post.
379
-	$post_id = wp_insert_post( $params, true );
380
-
381
-	// Setting the alternative labels for this entity.
382
-	Wordlift_Entity_Service::get_instance()
383
-	                       ->set_alternative_labels( $post_id, $synonyms );
384
-
385
-	// Restore all the existing filters.
386
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
387
-
388
-	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
389
-	if ( isset( $wpseo_metabox ) ) {
390
-		add_action( 'wp_insert_post', array(
391
-			$wpseo_metabox,
392
-			'save_postdata',
393
-		) );
394
-	}
395
-
396
-	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
397
-	if ( isset( $seo_ultimate ) ) {
398
-		add_action( 'save_post', array(
399
-			$seo_ultimate,
400
-			'save_postmeta_box',
401
-		), 10, 2 );
402
-	}
403
-
404
-	// TODO: handle errors.
405
-	if ( is_wp_error( $post_id ) ) {
406
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
407
-
408
-		// inform an error occurred.
409
-		return null;
410
-	}
411
-
412
-	wl_set_entity_main_type( $post_id, $type_uri );
413
-
414
-	// Save the entity types.
415
-	wl_set_entity_rdf_types( $post_id, $entity_types );
416
-
417
-	// Get a dataset URI for the entity.
418
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
419
-
420
-	// Add the uri to the sameAs data if it's not a local URI.
421
-	if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
422
-	     $wl_uri !== $uri &&
423
-	     // Only remote entities
424
-	     0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
425
-	) {
426
-		array_push( $same_as, $uri );
427
-	}
428
-
429
-	// Save the sameAs data for the entity.
430
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
431
-
432
-	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
433
-	foreach ( $other_properties as $property_name => $property_value ) {
434
-		wl_schema_set_value( $post_id, $property_name, $property_value );
435
-	}
436
-
437
-	// Call hooks.
438
-	do_action( 'wl_save_entity', $post_id );
439
-
440
-	foreach ( $images as $image_remote_url ) {
441
-
442
-		// Check if image is already present in local DB
443
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
444
-			// Do nothing.
445
-			continue;
446
-		}
447
-
448
-		// Check if there is an existing attachment for this post ID and source URL.
449
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
450
-
451
-		// Skip if an existing image is found.
452
-		if ( null !== $existing_image ) {
453
-			continue;
454
-		}
455
-
456
-		// Save the image and get the local path.
457
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
458
-
459
-		if ( false === $image || is_wp_error( $image ) ) {
460
-			continue;
461
-		}
462
-
463
-		// Get the local URL.
464
-		$filename     = $image['path'];
465
-		$url          = $image['url'];
466
-		$content_type = $image['content_type'];
467
-
468
-		$attachment = array(
469
-			'guid'           => $url,
470
-			// post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
471
-			'post_title'     => $label,
472
-			// Set the title to the post title.
473
-			'post_content'   => '',
474
-			'post_status'    => 'inherit',
475
-			'post_mime_type' => $content_type,
476
-		);
477
-
478
-		// Create the attachment in WordPress and generate the related metadata.
479
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
480
-
481
-		// Set the source URL for the image.
482
-		wl_set_source_url( $attachment_id, $image_remote_url );
483
-
484
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
485
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
486
-
487
-		// Set it as the featured image.
488
-		set_post_thumbnail( $post_id, $attachment_id );
489
-	}
490
-
491
-	// finally return the entity post.
492
-	return get_post( $post_id );
307
+    $label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
308
+    $type_uri         = $entity_data['main_type'];
309
+    $entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
310
+    $description      = $entity_data['description'];
311
+    $images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
312
+    $same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
313
+    $related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
314
+    $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
315
+    // Get the synonyms.
316
+    $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
317
+
318
+    // Check whether an entity already exists with the provided URI.
319
+    $uri = $entity_data['uri'];
320
+    if ( isset ( $uri ) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
321
+        $log->debug( "Post already exists for URI $uri." );
322
+
323
+        return $post;
324
+    }
325
+
326
+    // Prepare properties of the new entity.
327
+    $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
328
+        ? get_post_status( $related_post_id ) : 'draft';
329
+
330
+    $params = array(
331
+        // @@todo: we don't want an entity to be automatically published.
332
+        'post_status'  => $post_status,
333
+        'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
334
+        'post_title'   => $label,
335
+        'post_content' => $description,
336
+        'post_excerpt' => '',
337
+        // Ensure we're using a valid slug. We're not overwriting an existing
338
+        // entity with a post_name already set, since this call is made only for
339
+        // new entities.
340
+        //
341
+        // See https://github.com/insideout10/wordlift-plugin/issues/282
342
+        'post_name'    => sanitize_title( $label ),
343
+    );
344
+
345
+    // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
346
+    // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
347
+    // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
348
+    // This does NOT affect saving an entity from the entity admin page since this function is called when an entity
349
+    // is created when saving a post.
350
+    global $wpseo_metabox, $seo_ultimate;
351
+    if ( isset( $wpseo_metabox ) ) {
352
+        remove_action( 'wp_insert_post', array(
353
+            $wpseo_metabox,
354
+            'save_postdata',
355
+        ) );
356
+    }
357
+
358
+    if ( isset( $seo_ultimate ) ) {
359
+        remove_action( 'save_post', array(
360
+            $seo_ultimate,
361
+            'save_postmeta_box',
362
+        ) );
363
+    }
364
+
365
+    // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
366
+    // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
367
+    // to the save_post and restore them after we saved the entity.
368
+    // see https://github.com/insideout10/wordlift-plugin/issues/203
369
+    // see https://github.com/insideout10/wordlift-plugin/issues/156
370
+    // see https://github.com/insideout10/wordlift-plugin/issues/148
371
+    global $wp_filter;
372
+    $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
373
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
374
+
375
+
376
+    $log->trace( 'Going to insert new post...' );
377
+
378
+    // create or update the post.
379
+    $post_id = wp_insert_post( $params, true );
380
+
381
+    // Setting the alternative labels for this entity.
382
+    Wordlift_Entity_Service::get_instance()
383
+                            ->set_alternative_labels( $post_id, $synonyms );
384
+
385
+    // Restore all the existing filters.
386
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
387
+
388
+    // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
389
+    if ( isset( $wpseo_metabox ) ) {
390
+        add_action( 'wp_insert_post', array(
391
+            $wpseo_metabox,
392
+            'save_postdata',
393
+        ) );
394
+    }
395
+
396
+    // If SEO Ultimate is installed, add back the hook we removed a few lines above.
397
+    if ( isset( $seo_ultimate ) ) {
398
+        add_action( 'save_post', array(
399
+            $seo_ultimate,
400
+            'save_postmeta_box',
401
+        ), 10, 2 );
402
+    }
403
+
404
+    // TODO: handle errors.
405
+    if ( is_wp_error( $post_id ) ) {
406
+        $log->error( 'An error occurred: ' . $post_id->get_error_message() );
407
+
408
+        // inform an error occurred.
409
+        return null;
410
+    }
411
+
412
+    wl_set_entity_main_type( $post_id, $type_uri );
413
+
414
+    // Save the entity types.
415
+    wl_set_entity_rdf_types( $post_id, $entity_types );
416
+
417
+    // Get a dataset URI for the entity.
418
+    $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
419
+
420
+    // Add the uri to the sameAs data if it's not a local URI.
421
+    if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
422
+         $wl_uri !== $uri &&
423
+         // Only remote entities
424
+         0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
425
+    ) {
426
+        array_push( $same_as, $uri );
427
+    }
428
+
429
+    // Save the sameAs data for the entity.
430
+    wl_schema_set_value( $post_id, 'sameAs', $same_as );
431
+
432
+    // Save the other properties (latitude, langitude, startDate, endDate, etc.)
433
+    foreach ( $other_properties as $property_name => $property_value ) {
434
+        wl_schema_set_value( $post_id, $property_name, $property_value );
435
+    }
436
+
437
+    // Call hooks.
438
+    do_action( 'wl_save_entity', $post_id );
439
+
440
+    foreach ( $images as $image_remote_url ) {
441
+
442
+        // Check if image is already present in local DB
443
+        if ( strpos( $image_remote_url, site_url() ) !== false ) {
444
+            // Do nothing.
445
+            continue;
446
+        }
447
+
448
+        // Check if there is an existing attachment for this post ID and source URL.
449
+        $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
450
+
451
+        // Skip if an existing image is found.
452
+        if ( null !== $existing_image ) {
453
+            continue;
454
+        }
455
+
456
+        // Save the image and get the local path.
457
+        $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
458
+
459
+        if ( false === $image || is_wp_error( $image ) ) {
460
+            continue;
461
+        }
462
+
463
+        // Get the local URL.
464
+        $filename     = $image['path'];
465
+        $url          = $image['url'];
466
+        $content_type = $image['content_type'];
467
+
468
+        $attachment = array(
469
+            'guid'           => $url,
470
+            // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
471
+            'post_title'     => $label,
472
+            // Set the title to the post title.
473
+            'post_content'   => '',
474
+            'post_status'    => 'inherit',
475
+            'post_mime_type' => $content_type,
476
+        );
477
+
478
+        // Create the attachment in WordPress and generate the related metadata.
479
+        $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
480
+
481
+        // Set the source URL for the image.
482
+        wl_set_source_url( $attachment_id, $image_remote_url );
483
+
484
+        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
485
+        wp_update_attachment_metadata( $attachment_id, $attachment_data );
486
+
487
+        // Set it as the featured image.
488
+        set_post_thumbnail( $post_id, $attachment_id );
489
+    }
490
+
491
+    // finally return the entity post.
492
+    return get_post( $post_id );
493 493
 }
Please login to merge, or discard this patch.
Spacing   +126 added lines, -126 removed lines patch added patch discarded remove patch
@@ -20,15 +20,15 @@  discard block
 block discarded – undo
20 20
  * @since 3.0.0
21 21
  *
22 22
  */
23
-function wl_linked_data_save_post( $post_id ) {
23
+function wl_linked_data_save_post($post_id) {
24 24
 
25
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' );
25
+	$log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post');
26 26
 
27
-	$log->trace( "Saving post $post_id to Linked Data..." );
27
+	$log->trace("Saving post $post_id to Linked Data...");
28 28
 
29 29
 	// If it's not numeric exit from here.
30
-	if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) {
31
-		$log->debug( "Skipping $post_id, because id is not numeric or is a post revision." );
30
+	if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) {
31
+		$log->debug("Skipping $post_id, because id is not numeric or is a post revision.");
32 32
 
33 33
 		return;
34 34
 	}
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
 	// Get the post type and check whether it supports the editor.
37 37
 	//
38 38
 	// @see https://github.com/insideout10/wordlift-plugin/issues/659.
39
-	$post_type = get_post_type( $post_id );
39
+	$post_type = get_post_type($post_id);
40 40
 	/**
41 41
 	 * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting.
42 42
 	 *
@@ -44,13 +44,13 @@  discard block
 block discarded – undo
44 44
 	 *
45 45
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
46 46
 	 */
47
-	$is_editor_supported = wl_post_type_supports_editor( $post_type );
47
+	$is_editor_supported = wl_post_type_supports_editor($post_type);
48 48
 
49
-	$is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id );
49
+	$is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id);
50 50
 	// Bail out if it's not an entity.
51 51
 	if ( ! $is_editor_supported
52
-	     && ! $is_no_editor_analysis_enabled ) {
53
-		$log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." );
52
+	     && ! $is_no_editor_analysis_enabled) {
53
+		$log->debug("Skipping $post_id, because $post_type doesn't support the editor (content).");
54 54
 
55 55
 		return;
56 56
 	}
@@ -64,23 +64,23 @@  discard block
 block discarded – undo
64 64
 	$supported_types = Wordlift_Entity_Service::valid_entity_post_types();
65 65
 
66 66
 	// Bail out if it's not a valid entity.
67
-	if ( ! in_array( $post_type, $supported_types ) && ! $is_no_editor_analysis_enabled ) {
68
-		$log->debug( "Skipping $post_id, because $post_type is not a valid entity." );
67
+	if ( ! in_array($post_type, $supported_types) && ! $is_no_editor_analysis_enabled) {
68
+		$log->debug("Skipping $post_id, because $post_type is not a valid entity.");
69 69
 
70 70
 		return;
71 71
 	}
72 72
 
73 73
 	// Unhook this function so it doesn't loop infinitely.
74
-	remove_action( 'save_post', 'wl_linked_data_save_post' );
74
+	remove_action('save_post', 'wl_linked_data_save_post');
75 75
 
76 76
 	// raise the *wl_linked_data_save_post* event.
77
-	do_action( 'wl_linked_data_save_post', $post_id );
77
+	do_action('wl_linked_data_save_post', $post_id);
78 78
 
79 79
 	// Re-hook this function.
80
-	add_action( 'save_post', 'wl_linked_data_save_post' );
80
+	add_action('save_post', 'wl_linked_data_save_post');
81 81
 }
82 82
 
83
-add_action( 'save_post', 'wl_linked_data_save_post' );
83
+add_action('save_post', 'wl_linked_data_save_post');
84 84
 
85 85
 /**
86 86
  * Save the post to the triple store. Also saves the entities locally and on the triple store.
@@ -90,23 +90,23 @@  discard block
 block discarded – undo
90 90
  * @since 3.0.0
91 91
  *
92 92
  */
93
-function wl_linked_data_save_post_and_related_entities( $post_id ) {
93
+function wl_linked_data_save_post_and_related_entities($post_id) {
94 94
 
95
-	$log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' );
95
+	$log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities');
96 96
 
97
-	$log->trace( "Saving $post_id to Linked Data along with related entities..." );
97
+	$log->trace("Saving $post_id to Linked Data along with related entities...");
98 98
 
99 99
 	// Ignore auto-saves
100
-	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
101
-		$log->trace( 'Doing autosave, skipping...' );
100
+	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
101
+		$log->trace('Doing autosave, skipping...');
102 102
 
103 103
 		return;
104 104
 	}
105 105
 
106 106
 	// get the current post.
107
-	$post = get_post( $post_id );
107
+	$post = get_post($post_id);
108 108
 
109
-	remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
109
+	remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
110 110
 
111 111
 	// wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" );
112 112
 
@@ -122,21 +122,21 @@  discard block
 block discarded – undo
122 122
 	$internal_entity_uris = array();
123 123
 
124 124
 	// Save the entities coming with POST data.
125
-	if ( isset( $_POST['wl_entities'] ) ) {
126
-		wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " );
127
-		wl_write_log( json_encode( $_POST['wl_entities'] ) );
128
-		wl_write_log( "]" );
125
+	if (isset($_POST['wl_entities'])) {
126
+		wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: ");
127
+		wl_write_log(json_encode($_POST['wl_entities']));
128
+		wl_write_log("]");
129 129
 
130 130
 		$entities_via_post = $_POST['wl_entities'];
131 131
 
132
-		foreach ( $entities_via_post as $entity_uri => $entity ) {
132
+		foreach ($entities_via_post as $entity_uri => $entity) {
133 133
 
134
-			if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) {
135
-				$existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() );
136
-				if ( isset( $existing_entity ) ) {
137
-					$existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID );
134
+			if (preg_match('/^local-entity-.+/', $entity_uri)) {
135
+				$existing_entity = get_page_by_title($entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types());
136
+				if (isset($existing_entity)) {
137
+					$existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get($existing_entity->ID);
138 138
 					// Type doesn't match, continue to create a new entity.
139
-					if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) {
139
+					if ( ! isset($existing_entity_type) || $existing_entity_type['css_class'] !== $entity['main_type']) {
140 140
 						$existing_entity = null;
141 141
 					}
142 142
 				}
@@ -144,34 +144,34 @@  discard block
 block discarded – undo
144 144
 				// Look if current entity uri matches an internal existing entity, meaning:
145 145
 				// 1. when $entity_uri is an internal uri
146 146
 				// 2. when $entity_uri is an external uri used as sameAs of an internal entity
147
-				$existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri );
147
+				$existing_entity = $entity_service->get_entity_post_by_uri($entity_uri);
148 148
 			}
149 149
 
150 150
 			// Don't save the entities which are not found, but also local.
151
-			if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) {
151
+			if ( ! isset($existing_entity) && $uri_service->is_internal($entity_uri)) {
152 152
 				$internal_entity_uris[] = $entity_uri;
153 153
 				continue;
154 154
 			}
155 155
 
156
-			if ( ! isset( $existing_entity ) ) {
156
+			if ( ! isset($existing_entity)) {
157 157
 				// Update entity data with related post
158 158
 				$entity['related_post_id'] = $post_id;
159 159
 				// New entity, save it.
160
-				$existing_entity = wl_save_entity( $entity );
160
+				$existing_entity = wl_save_entity($entity);
161 161
 			} else {
162 162
 				// Existing entity, update post status.
163
-				if ( $existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish' ) {
164
-					$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
163
+				if ($existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish') {
164
+					$post_status = apply_filters('wl_feature__enable__entity-auto-publish', true)
165 165
 						? $post->post_status : 'draft';
166
-					wl_update_post_status( $existing_entity->ID, $post_status );
166
+					wl_update_post_status($existing_entity->ID, $post_status);
167 167
 				}
168 168
 			}
169 169
 
170
-			$uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
170
+			$uri = $content_service->get_entity_id(Wordpress_Content_Id::create_post($existing_entity->ID));
171 171
 
172 172
 			$internal_entity_uris[] = $uri;
173
-			wl_write_log( "Map $entity_uri on $uri" );
174
-			$entities_uri_mapping[ $entity_uri ] = $uri;
173
+			wl_write_log("Map $entity_uri on $uri");
174
+			$entities_uri_mapping[$entity_uri] = $uri;
175 175
 
176 176
 		}
177 177
 
@@ -181,14 +181,14 @@  discard block
 block discarded – undo
181 181
 	$updated_post_content = $post->post_content;
182 182
 
183 183
 	// Update the post content if we found mappings of new entities.
184
-	if ( ! empty( $entities_uri_mapping ) ) {
184
+	if ( ! empty($entities_uri_mapping)) {
185 185
 		// Save each entity and store the post id.
186
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
187
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
186
+		foreach ($entities_uri_mapping as $tmp_uri => $uri) {
187
+			if (1 !== preg_match('@^(https?://|local-entity-)@', $tmp_uri)) {
188 188
 				continue;
189 189
 			}
190 190
 
191
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
191
+			$updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content);
192 192
 		}
193 193
 
194 194
 		// Update the post content.
@@ -197,16 +197,16 @@  discard block
 block discarded – undo
197 197
 		 * content, so add the slashes to prevent back slash getting
198 198
 		 * removed.
199 199
 		 */
200
-		wp_update_post( array(
200
+		wp_update_post(array(
201 201
 			'ID'           => $post->ID,
202
-			'post_content' => addslashes( $updated_post_content ),
203
-		) );
202
+			'post_content' => addslashes($updated_post_content),
203
+		));
204 204
 	}
205 205
 
206 206
 	// Reset previously saved instances.
207
-	wl_core_delete_relation_instances( $post_id );
207
+	wl_core_delete_relation_instances($post_id);
208 208
 
209
-	$relations = Object_Relation_Factory::get_instance( $post_id )
209
+	$relations = Object_Relation_Factory::get_instance($post_id)
210 210
 	                                    ->get_relations_from_content(
211 211
 		                                    $updated_post_content,
212 212
 		                                    Object_Type_Enum::POST,
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 	                                    );
215 215
 
216 216
 	// Save relation instances
217
-	foreach ( $relations as $relation ) {
217
+	foreach ($relations as $relation) {
218 218
 
219 219
 		wl_core_add_relation_instance(
220 220
 		// subject id.
@@ -231,9 +231,9 @@  discard block
 block discarded – undo
231 231
 
232 232
 	}
233 233
 
234
-	if ( isset( $_POST['wl_entities'] ) ) {
234
+	if (isset($_POST['wl_entities'])) {
235 235
 		// Save post metadata if available
236
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
236
+		$metadata_via_post = (isset($_POST['wl_metadata'])) ?
237 237
 			$_POST['wl_metadata'] : array();
238 238
 
239 239
 		$fields = array(
@@ -242,35 +242,35 @@  discard block
 block discarded – undo
242 242
 		);
243 243
 
244 244
 		// Unlink topic taxonomy terms
245
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
245
+		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID);
246 246
 
247
-		foreach ( $fields as $field ) {
247
+		foreach ($fields as $field) {
248 248
 
249 249
 			// Delete current values
250
-			delete_post_meta( $post->ID, $field );
250
+			delete_post_meta($post->ID, $field);
251 251
 			// Retrieve the entity uri
252
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
253
-				stripslashes( $metadata_via_post[ $field ] ) : '';
252
+			$uri = (isset($metadata_via_post[$field])) ?
253
+				stripslashes($metadata_via_post[$field]) : '';
254 254
 
255
-			if ( empty( $uri ) ) {
255
+			if (empty($uri)) {
256 256
 				continue;
257 257
 			}
258
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
258
+			$entity = $entity_service->get_entity_post_by_uri($uri);
259 259
 
260
-			if ( $entity ) {
261
-				add_post_meta( $post->ID, $field, $entity->ID, true );
260
+			if ($entity) {
261
+				add_post_meta($post->ID, $field, $entity->ID, true);
262 262
 				// Set also the topic taxonomy
263
-				if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
264
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
263
+				if ($field === Wordlift_Schema_Service::FIELD_TOPIC) {
264
+					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity);
265 265
 				}
266 266
 			}
267 267
 		}
268 268
 	}
269 269
 
270
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
270
+	add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
271 271
 }
272 272
 
273
-add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
273
+add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
274 274
 
275 275
 /**
276 276
  * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is
@@ -288,14 +288,14 @@  discard block
 block discarded – undo
288 288
  *
289 289
  * @return null|WP_Post A post instance or null in case of failure.
290 290
  */
291
-function wl_save_entity( $entity_data ) {
291
+function wl_save_entity($entity_data) {
292 292
 
293 293
 	// Required for REST API calls
294
-	if ( ! function_exists( 'wp_crop_image' ) ) {
295
-		require_once( ABSPATH . 'wp-admin/includes/image.php' );
294
+	if ( ! function_exists('wp_crop_image')) {
295
+		require_once(ABSPATH.'wp-admin/includes/image.php');
296 296
 	}
297 297
 
298
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
298
+	$log = Wordlift_Log_Service::get_logger('wl_save_entity');
299 299
 
300 300
 	/*
301 301
 	 * Data is coming from a $_POST, sanitize it.
@@ -304,28 +304,28 @@  discard block
 block discarded – undo
304 304
 	 *
305 305
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
306 306
 	 */
307
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
307
+	$label            = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label']));
308 308
 	$type_uri         = $entity_data['main_type'];
309
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
309
+	$entity_types     = isset($entity_data['type']) ? $entity_data['type'] : array();
310 310
 	$description      = $entity_data['description'];
311
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
312
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
313
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
314
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
311
+	$images           = isset($entity_data['image']) ? (array) $entity_data['image'] : array();
312
+	$same_as          = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array();
313
+	$related_post_id  = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null;
314
+	$other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array();
315 315
 	// Get the synonyms.
316
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
316
+	$synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array();
317 317
 
318 318
 	// Check whether an entity already exists with the provided URI.
319 319
 	$uri = $entity_data['uri'];
320
-	if ( isset ( $uri ) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
321
-		$log->debug( "Post already exists for URI $uri." );
320
+	if (isset ($uri) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) {
321
+		$log->debug("Post already exists for URI $uri.");
322 322
 
323 323
 		return $post;
324 324
 	}
325 325
 
326 326
 	// Prepare properties of the new entity.
327
-	$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
328
-		? get_post_status( $related_post_id ) : 'draft';
327
+	$post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) && is_numeric($related_post_id)
328
+		? get_post_status($related_post_id) : 'draft';
329 329
 
330 330
 	$params = array(
331 331
 		// @@todo: we don't want an entity to be automatically published.
@@ -339,7 +339,7 @@  discard block
 block discarded – undo
339 339
 		// new entities.
340 340
 		//
341 341
 		// See https://github.com/insideout10/wordlift-plugin/issues/282
342
-		'post_name'    => sanitize_title( $label ),
342
+		'post_name'    => sanitize_title($label),
343 343
 	);
344 344
 
345 345
 	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
@@ -348,18 +348,18 @@  discard block
 block discarded – undo
348 348
 	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
349 349
 	// is created when saving a post.
350 350
 	global $wpseo_metabox, $seo_ultimate;
351
-	if ( isset( $wpseo_metabox ) ) {
352
-		remove_action( 'wp_insert_post', array(
351
+	if (isset($wpseo_metabox)) {
352
+		remove_action('wp_insert_post', array(
353 353
 			$wpseo_metabox,
354 354
 			'save_postdata',
355
-		) );
355
+		));
356 356
 	}
357 357
 
358
-	if ( isset( $seo_ultimate ) ) {
359
-		remove_action( 'save_post', array(
358
+	if (isset($seo_ultimate)) {
359
+		remove_action('save_post', array(
360 360
 			$seo_ultimate,
361 361
 			'save_postmeta_box',
362
-		) );
362
+		));
363 363
 	}
364 364
 
365 365
 	// The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
@@ -369,94 +369,94 @@  discard block
 block discarded – undo
369 369
 	// see https://github.com/insideout10/wordlift-plugin/issues/156
370 370
 	// see https://github.com/insideout10/wordlift-plugin/issues/148
371 371
 	global $wp_filter;
372
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
373
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
372
+	$save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
373
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
374 374
 
375 375
 
376
-	$log->trace( 'Going to insert new post...' );
376
+	$log->trace('Going to insert new post...');
377 377
 
378 378
 	// create or update the post.
379
-	$post_id = wp_insert_post( $params, true );
379
+	$post_id = wp_insert_post($params, true);
380 380
 
381 381
 	// Setting the alternative labels for this entity.
382 382
 	Wordlift_Entity_Service::get_instance()
383
-	                       ->set_alternative_labels( $post_id, $synonyms );
383
+	                       ->set_alternative_labels($post_id, $synonyms);
384 384
 
385 385
 	// Restore all the existing filters.
386
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
386
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
387 387
 
388 388
 	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
389
-	if ( isset( $wpseo_metabox ) ) {
390
-		add_action( 'wp_insert_post', array(
389
+	if (isset($wpseo_metabox)) {
390
+		add_action('wp_insert_post', array(
391 391
 			$wpseo_metabox,
392 392
 			'save_postdata',
393
-		) );
393
+		));
394 394
 	}
395 395
 
396 396
 	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
397
-	if ( isset( $seo_ultimate ) ) {
398
-		add_action( 'save_post', array(
397
+	if (isset($seo_ultimate)) {
398
+		add_action('save_post', array(
399 399
 			$seo_ultimate,
400 400
 			'save_postmeta_box',
401
-		), 10, 2 );
401
+		), 10, 2);
402 402
 	}
403 403
 
404 404
 	// TODO: handle errors.
405
-	if ( is_wp_error( $post_id ) ) {
406
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
405
+	if (is_wp_error($post_id)) {
406
+		$log->error('An error occurred: '.$post_id->get_error_message());
407 407
 
408 408
 		// inform an error occurred.
409 409
 		return null;
410 410
 	}
411 411
 
412
-	wl_set_entity_main_type( $post_id, $type_uri );
412
+	wl_set_entity_main_type($post_id, $type_uri);
413 413
 
414 414
 	// Save the entity types.
415
-	wl_set_entity_rdf_types( $post_id, $entity_types );
415
+	wl_set_entity_rdf_types($post_id, $entity_types);
416 416
 
417 417
 	// Get a dataset URI for the entity.
418
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
418
+	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri($post_id);
419 419
 
420 420
 	// Add the uri to the sameAs data if it's not a local URI.
421
-	if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
421
+	if (isset($uri) && preg_match('@https?://.*@', $uri) &&
422 422
 	     $wl_uri !== $uri &&
423 423
 	     // Only remote entities
424
-	     0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
424
+	     0 !== strpos($uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri())
425 425
 	) {
426
-		array_push( $same_as, $uri );
426
+		array_push($same_as, $uri);
427 427
 	}
428 428
 
429 429
 	// Save the sameAs data for the entity.
430
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
430
+	wl_schema_set_value($post_id, 'sameAs', $same_as);
431 431
 
432 432
 	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
433
-	foreach ( $other_properties as $property_name => $property_value ) {
434
-		wl_schema_set_value( $post_id, $property_name, $property_value );
433
+	foreach ($other_properties as $property_name => $property_value) {
434
+		wl_schema_set_value($post_id, $property_name, $property_value);
435 435
 	}
436 436
 
437 437
 	// Call hooks.
438
-	do_action( 'wl_save_entity', $post_id );
438
+	do_action('wl_save_entity', $post_id);
439 439
 
440
-	foreach ( $images as $image_remote_url ) {
440
+	foreach ($images as $image_remote_url) {
441 441
 
442 442
 		// Check if image is already present in local DB
443
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
443
+		if (strpos($image_remote_url, site_url()) !== false) {
444 444
 			// Do nothing.
445 445
 			continue;
446 446
 		}
447 447
 
448 448
 		// Check if there is an existing attachment for this post ID and source URL.
449
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
449
+		$existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url);
450 450
 
451 451
 		// Skip if an existing image is found.
452
-		if ( null !== $existing_image ) {
452
+		if (null !== $existing_image) {
453 453
 			continue;
454 454
 		}
455 455
 
456 456
 		// Save the image and get the local path.
457
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
457
+		$image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url);
458 458
 
459
-		if ( false === $image || is_wp_error( $image ) ) {
459
+		if (false === $image || is_wp_error($image)) {
460 460
 			continue;
461 461
 		}
462 462
 
@@ -476,18 +476,18 @@  discard block
 block discarded – undo
476 476
 		);
477 477
 
478 478
 		// Create the attachment in WordPress and generate the related metadata.
479
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
479
+		$attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
480 480
 
481 481
 		// Set the source URL for the image.
482
-		wl_set_source_url( $attachment_id, $image_remote_url );
482
+		wl_set_source_url($attachment_id, $image_remote_url);
483 483
 
484
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
485
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
484
+		$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
485
+		wp_update_attachment_metadata($attachment_id, $attachment_data);
486 486
 
487 487
 		// Set it as the featured image.
488
-		set_post_thumbnail( $post_id, $attachment_id );
488
+		set_post_thumbnail($post_id, $attachment_id);
489 489
 	}
490 490
 
491 491
 	// finally return the entity post.
492
-	return get_post( $post_id );
492
+	return get_post($post_id);
493 493
 }
Please login to merge, or discard this patch.