Completed
Pull Request — develop (#1540)
by Naveen
01:04
created
src/modules/linked_data/wordlift_linked_data.php 2 patches
Indentation   +430 added lines, -430 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,188 +92,188 @@  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' );
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..." );
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
-				// New entity, save it.
158
-				$existing_entity = wl_save_entity( $entity );
159
-			} else {
160
-				// Existing entity, update post status.
161
-				if ( $existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish' ) {
162
-					$parent_post_status = $post->post_status;
163
-					$post_status        = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
164
-						? $parent_post_status : 'draft';
165
-					wp_update_post( array(
166
-						'ID'          => $existing_entity->ID,
167
-						'post_status' => $post_status
168
-					) );
169
-				}
170
-			}
171
-
172
-			$uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
173
-
174
-
175
-			// Update entity data with related post
176
-			$entity['related_post_id'] = $post_id;
177
-
178
-			$internal_entity_uris[] = $uri;
179
-			wl_write_log( "Map $entity_uri on $uri" );
180
-			$entities_uri_mapping[ $entity_uri ] = $uri;
181
-
182
-		}
183
-
184
-	}
185
-
186
-	// Replace tmp uris in content post if needed
187
-	$updated_post_content = $post->post_content;
188
-
189
-	// Update the post content if we found mappings of new entities.
190
-	if ( ! empty( $entities_uri_mapping ) ) {
191
-		// Save each entity and store the post id.
192
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
193
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
194
-				continue;
195
-			}
196
-
197
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
198
-		}
199
-
200
-		// Update the post content.
201
-		/**
202
-		 * Note: wp_update_post do stripslashes before saving the
203
-		 * content, so add the slashes to prevent back slash getting
204
-		 * removed.
205
-		 */
206
-		wp_update_post( array(
207
-			'ID'           => $post->ID,
208
-			'post_content' => addslashes( $updated_post_content ),
209
-		) );
210
-	}
211
-
212
-	// Reset previously saved instances.
213
-	wl_core_delete_relation_instances( $post_id );
214
-
215
-	$relations = Object_Relation_Factory::get_instance( $post_id )
216
-	                                    ->get_relations_from_content(
217
-		                                    $updated_post_content,
218
-		                                    Object_Type_Enum::POST,
219
-		                                    $internal_entity_uris
220
-	                                    );
221
-
222
-	// Save relation instances
223
-	foreach ( $relations as $relation ) {
224
-
225
-		wl_core_add_relation_instance(
226
-		// subject id.
227
-			$post_id,
228
-			// what, where, when, who
229
-			$relation->get_relation_type(),
230
-			// object id.
231
-			$relation->get_object_id(),
232
-			// Subject type.
233
-			$relation->get_subject_type(),
234
-			// Object type.
235
-			$relation->get_object_type()
236
-		);
237
-
238
-	}
239
-
240
-	if ( isset( $_POST['wl_entities'] ) ) {
241
-		// Save post metadata if available
242
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
243
-			$_POST['wl_metadata'] : array();
244
-
245
-		$fields = array(
246
-			Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
247
-			Wordlift_Schema_Service::FIELD_TOPIC,
248
-		);
249
-
250
-		// Unlink topic taxonomy terms
251
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
252
-
253
-		foreach ( $fields as $field ) {
254
-
255
-			// Delete current values
256
-			delete_post_meta( $post->ID, $field );
257
-			// Retrieve the entity uri
258
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
259
-				stripslashes( $metadata_via_post[ $field ] ) : '';
260
-
261
-			if ( empty( $uri ) ) {
262
-				continue;
263
-			}
264
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
265
-
266
-			if ( $entity ) {
267
-				add_post_meta( $post->ID, $field, $entity->ID, true );
268
-				// Set also the topic taxonomy
269
-				if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
270
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
271
-				}
272
-			}
273
-		}
274
-	}
275
-
276
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
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
+                // New entity, save it.
158
+                $existing_entity = wl_save_entity( $entity );
159
+            } else {
160
+                // Existing entity, update post status.
161
+                if ( $existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish' ) {
162
+                    $parent_post_status = $post->post_status;
163
+                    $post_status        = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
164
+                        ? $parent_post_status : 'draft';
165
+                    wp_update_post( array(
166
+                        'ID'          => $existing_entity->ID,
167
+                        'post_status' => $post_status
168
+                    ) );
169
+                }
170
+            }
171
+
172
+            $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
173
+
174
+
175
+            // Update entity data with related post
176
+            $entity['related_post_id'] = $post_id;
177
+
178
+            $internal_entity_uris[] = $uri;
179
+            wl_write_log( "Map $entity_uri on $uri" );
180
+            $entities_uri_mapping[ $entity_uri ] = $uri;
181
+
182
+        }
183
+
184
+    }
185
+
186
+    // Replace tmp uris in content post if needed
187
+    $updated_post_content = $post->post_content;
188
+
189
+    // Update the post content if we found mappings of new entities.
190
+    if ( ! empty( $entities_uri_mapping ) ) {
191
+        // Save each entity and store the post id.
192
+        foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
193
+            if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
194
+                continue;
195
+            }
196
+
197
+            $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
198
+        }
199
+
200
+        // Update the post content.
201
+        /**
202
+         * Note: wp_update_post do stripslashes before saving the
203
+         * content, so add the slashes to prevent back slash getting
204
+         * removed.
205
+         */
206
+        wp_update_post( array(
207
+            'ID'           => $post->ID,
208
+            'post_content' => addslashes( $updated_post_content ),
209
+        ) );
210
+    }
211
+
212
+    // Reset previously saved instances.
213
+    wl_core_delete_relation_instances( $post_id );
214
+
215
+    $relations = Object_Relation_Factory::get_instance( $post_id )
216
+                                        ->get_relations_from_content(
217
+                                            $updated_post_content,
218
+                                            Object_Type_Enum::POST,
219
+                                            $internal_entity_uris
220
+                                        );
221
+
222
+    // Save relation instances
223
+    foreach ( $relations as $relation ) {
224
+
225
+        wl_core_add_relation_instance(
226
+        // subject id.
227
+            $post_id,
228
+            // what, where, when, who
229
+            $relation->get_relation_type(),
230
+            // object id.
231
+            $relation->get_object_id(),
232
+            // Subject type.
233
+            $relation->get_subject_type(),
234
+            // Object type.
235
+            $relation->get_object_type()
236
+        );
237
+
238
+    }
239
+
240
+    if ( isset( $_POST['wl_entities'] ) ) {
241
+        // Save post metadata if available
242
+        $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
243
+            $_POST['wl_metadata'] : array();
244
+
245
+        $fields = array(
246
+            Wordlift_Schema_Service::FIELD_LOCATION_CREATED,
247
+            Wordlift_Schema_Service::FIELD_TOPIC,
248
+        );
249
+
250
+        // Unlink topic taxonomy terms
251
+        Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
252
+
253
+        foreach ( $fields as $field ) {
254
+
255
+            // Delete current values
256
+            delete_post_meta( $post->ID, $field );
257
+            // Retrieve the entity uri
258
+            $uri = ( isset( $metadata_via_post[ $field ] ) ) ?
259
+                stripslashes( $metadata_via_post[ $field ] ) : '';
260
+
261
+            if ( empty( $uri ) ) {
262
+                continue;
263
+            }
264
+            $entity = $entity_service->get_entity_post_by_uri( $uri );
265
+
266
+            if ( $entity ) {
267
+                add_post_meta( $post->ID, $field, $entity->ID, true );
268
+                // Set also the topic taxonomy
269
+                if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
270
+                    Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
271
+                }
272
+            }
273
+        }
274
+    }
275
+
276
+    add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
277 277
 }
278 278
 
279 279
 add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
@@ -296,205 +296,205 @@  discard block
 block discarded – undo
296 296
  */
297 297
 function wl_save_entity( $entity_data ) {
298 298
 
299
-	// Required for REST API calls
300
-	if ( ! function_exists( 'wp_crop_image' ) ) {
301
-		require_once( ABSPATH . 'wp-admin/includes/image.php' );
302
-	}
299
+    // Required for REST API calls
300
+    if ( ! function_exists( 'wp_crop_image' ) ) {
301
+        require_once( ABSPATH . 'wp-admin/includes/image.php' );
302
+    }
303 303
 
304
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
304
+    $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
305 305
 
306
-	/*
306
+    /*
307 307
 	 * Data is coming from a $_POST, sanitize it.
308 308
 	 *
309 309
 	 * @since 3.19.4
310 310
 	 *
311 311
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
312 312
 	 */
313
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
314
-	$type_uri         = $entity_data['main_type'];
315
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
316
-	$description      = $entity_data['description'];
317
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
318
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
319
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
320
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
321
-
322
-	// Get the synonyms.
323
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
324
-
325
-	// Check whether an entity already exists with the provided URI.
326
-	$uri = $entity_data['uri'];
327
-	if ( isset ( $uri ) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
328
-		$log->debug( "Post already exists for URI $uri." );
329
-
330
-		return $post;
331
-	}
332
-
333
-	// Prepare properties of the new entity.
334
-	$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
335
-		? get_post_status( $related_post_id ) : 'draft';
336
-
337
-	$params = array(
338
-		// @@todo: we don't want an entity to be automatically published.
339
-		'post_status'  => $post_status,
340
-		'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
341
-		'post_title'   => $label,
342
-		'post_content' => $description,
343
-		'post_excerpt' => '',
344
-		// Ensure we're using a valid slug. We're not overwriting an existing
345
-		// entity with a post_name already set, since this call is made only for
346
-		// new entities.
347
-		//
348
-		// See https://github.com/insideout10/wordlift-plugin/issues/282
349
-		'post_name'    => sanitize_title( $label ),
350
-	);
351
-
352
-	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
353
-	// the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
354
-	// Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
355
-	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
356
-	// is created when saving a post.
357
-	global $wpseo_metabox, $seo_ultimate;
358
-	if ( isset( $wpseo_metabox ) ) {
359
-		remove_action( 'wp_insert_post', array(
360
-			$wpseo_metabox,
361
-			'save_postdata',
362
-		) );
363
-	}
364
-
365
-	if ( isset( $seo_ultimate ) ) {
366
-		remove_action( 'save_post', array(
367
-			$seo_ultimate,
368
-			'save_postmeta_box',
369
-		) );
370
-	}
371
-
372
-	// The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
373
-	// save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
374
-	// to the save_post and restore them after we saved the entity.
375
-	// see https://github.com/insideout10/wordlift-plugin/issues/203
376
-	// see https://github.com/insideout10/wordlift-plugin/issues/156
377
-	// see https://github.com/insideout10/wordlift-plugin/issues/148
378
-	global $wp_filter;
379
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
380
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
381
-
382
-
383
-	$log->trace( 'Going to insert new post...' );
384
-
385
-	// create or update the post.
386
-	$post_id = wp_insert_post( $params, true );
387
-
388
-	// Setting the alternative labels for this entity.
389
-	Wordlift_Entity_Service::get_instance()
390
-	                       ->set_alternative_labels( $post_id, $synonyms );
391
-
392
-	// Restore all the existing filters.
393
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
394
-
395
-	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
396
-	if ( isset( $wpseo_metabox ) ) {
397
-		add_action( 'wp_insert_post', array(
398
-			$wpseo_metabox,
399
-			'save_postdata',
400
-		) );
401
-	}
402
-
403
-	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
404
-	if ( isset( $seo_ultimate ) ) {
405
-		add_action( 'save_post', array(
406
-			$seo_ultimate,
407
-			'save_postmeta_box',
408
-		), 10, 2 );
409
-	}
410
-
411
-	// TODO: handle errors.
412
-	if ( is_wp_error( $post_id ) ) {
413
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
414
-
415
-		// inform an error occurred.
416
-		return null;
417
-	}
418
-
419
-	wl_set_entity_main_type( $post_id, $type_uri );
420
-
421
-	// Save the entity types.
422
-	wl_set_entity_rdf_types( $post_id, $entity_types );
423
-
424
-	// Get a dataset URI for the entity.
425
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
426
-
427
-	// Add the uri to the sameAs data if it's not a local URI.
428
-	if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
429
-	     $wl_uri !== $uri &&
430
-	     // Only remote entities
431
-	     0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
432
-	) {
433
-		array_push( $same_as, $uri );
434
-	}
435
-
436
-	// Save the sameAs data for the entity.
437
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
438
-
439
-	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
440
-	foreach ( $other_properties as $property_name => $property_value ) {
441
-		wl_schema_set_value( $post_id, $property_name, $property_value );
442
-	}
443
-
444
-	// Call hooks.
445
-	do_action( 'wl_save_entity', $post_id );
446
-
447
-	foreach ( $images as $image_remote_url ) {
448
-
449
-		// Check if image is already present in local DB
450
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
451
-			// Do nothing.
452
-			continue;
453
-		}
454
-
455
-		// Check if there is an existing attachment for this post ID and source URL.
456
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
457
-
458
-		// Skip if an existing image is found.
459
-		if ( null !== $existing_image ) {
460
-			continue;
461
-		}
462
-
463
-		// Save the image and get the local path.
464
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
465
-
466
-		if ( false === $image || is_wp_error( $image ) ) {
467
-			continue;
468
-		}
469
-
470
-		// Get the local URL.
471
-		$filename     = $image['path'];
472
-		$url          = $image['url'];
473
-		$content_type = $image['content_type'];
474
-
475
-		$attachment = array(
476
-			'guid'           => $url,
477
-			// post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
478
-			'post_title'     => $label,
479
-			// Set the title to the post title.
480
-			'post_content'   => '',
481
-			'post_status'    => 'inherit',
482
-			'post_mime_type' => $content_type,
483
-		);
484
-
485
-		// Create the attachment in WordPress and generate the related metadata.
486
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
487
-
488
-		// Set the source URL for the image.
489
-		wl_set_source_url( $attachment_id, $image_remote_url );
490
-
491
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
492
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
493
-
494
-		// Set it as the featured image.
495
-		set_post_thumbnail( $post_id, $attachment_id );
496
-	}
497
-
498
-	// finally return the entity post.
499
-	return get_post( $post_id );
313
+    $label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
314
+    $type_uri         = $entity_data['main_type'];
315
+    $entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
316
+    $description      = $entity_data['description'];
317
+    $images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
318
+    $same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
319
+    $related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
320
+    $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
321
+
322
+    // Get the synonyms.
323
+    $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
324
+
325
+    // Check whether an entity already exists with the provided URI.
326
+    $uri = $entity_data['uri'];
327
+    if ( isset ( $uri ) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
328
+        $log->debug( "Post already exists for URI $uri." );
329
+
330
+        return $post;
331
+    }
332
+
333
+    // Prepare properties of the new entity.
334
+    $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
335
+        ? get_post_status( $related_post_id ) : 'draft';
336
+
337
+    $params = array(
338
+        // @@todo: we don't want an entity to be automatically published.
339
+        'post_status'  => $post_status,
340
+        'post_type'    => Wordlift_Entity_Service::TYPE_NAME,
341
+        'post_title'   => $label,
342
+        'post_content' => $description,
343
+        'post_excerpt' => '',
344
+        // Ensure we're using a valid slug. We're not overwriting an existing
345
+        // entity with a post_name already set, since this call is made only for
346
+        // new entities.
347
+        //
348
+        // See https://github.com/insideout10/wordlift-plugin/issues/282
349
+        'post_name'    => sanitize_title( $label ),
350
+    );
351
+
352
+    // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
353
+    // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
354
+    // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
355
+    // This does NOT affect saving an entity from the entity admin page since this function is called when an entity
356
+    // is created when saving a post.
357
+    global $wpseo_metabox, $seo_ultimate;
358
+    if ( isset( $wpseo_metabox ) ) {
359
+        remove_action( 'wp_insert_post', array(
360
+            $wpseo_metabox,
361
+            'save_postdata',
362
+        ) );
363
+    }
364
+
365
+    if ( isset( $seo_ultimate ) ) {
366
+        remove_action( 'save_post', array(
367
+            $seo_ultimate,
368
+            'save_postmeta_box',
369
+        ) );
370
+    }
371
+
372
+    // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
373
+    // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
374
+    // to the save_post and restore them after we saved the entity.
375
+    // see https://github.com/insideout10/wordlift-plugin/issues/203
376
+    // see https://github.com/insideout10/wordlift-plugin/issues/156
377
+    // see https://github.com/insideout10/wordlift-plugin/issues/148
378
+    global $wp_filter;
379
+    $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
380
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
381
+
382
+
383
+    $log->trace( 'Going to insert new post...' );
384
+
385
+    // create or update the post.
386
+    $post_id = wp_insert_post( $params, true );
387
+
388
+    // Setting the alternative labels for this entity.
389
+    Wordlift_Entity_Service::get_instance()
390
+                            ->set_alternative_labels( $post_id, $synonyms );
391
+
392
+    // Restore all the existing filters.
393
+    is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
394
+
395
+    // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
396
+    if ( isset( $wpseo_metabox ) ) {
397
+        add_action( 'wp_insert_post', array(
398
+            $wpseo_metabox,
399
+            'save_postdata',
400
+        ) );
401
+    }
402
+
403
+    // If SEO Ultimate is installed, add back the hook we removed a few lines above.
404
+    if ( isset( $seo_ultimate ) ) {
405
+        add_action( 'save_post', array(
406
+            $seo_ultimate,
407
+            'save_postmeta_box',
408
+        ), 10, 2 );
409
+    }
410
+
411
+    // TODO: handle errors.
412
+    if ( is_wp_error( $post_id ) ) {
413
+        $log->error( 'An error occurred: ' . $post_id->get_error_message() );
414
+
415
+        // inform an error occurred.
416
+        return null;
417
+    }
418
+
419
+    wl_set_entity_main_type( $post_id, $type_uri );
420
+
421
+    // Save the entity types.
422
+    wl_set_entity_rdf_types( $post_id, $entity_types );
423
+
424
+    // Get a dataset URI for the entity.
425
+    $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
426
+
427
+    // Add the uri to the sameAs data if it's not a local URI.
428
+    if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
429
+         $wl_uri !== $uri &&
430
+         // Only remote entities
431
+         0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
432
+    ) {
433
+        array_push( $same_as, $uri );
434
+    }
435
+
436
+    // Save the sameAs data for the entity.
437
+    wl_schema_set_value( $post_id, 'sameAs', $same_as );
438
+
439
+    // Save the other properties (latitude, langitude, startDate, endDate, etc.)
440
+    foreach ( $other_properties as $property_name => $property_value ) {
441
+        wl_schema_set_value( $post_id, $property_name, $property_value );
442
+    }
443
+
444
+    // Call hooks.
445
+    do_action( 'wl_save_entity', $post_id );
446
+
447
+    foreach ( $images as $image_remote_url ) {
448
+
449
+        // Check if image is already present in local DB
450
+        if ( strpos( $image_remote_url, site_url() ) !== false ) {
451
+            // Do nothing.
452
+            continue;
453
+        }
454
+
455
+        // Check if there is an existing attachment for this post ID and source URL.
456
+        $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
457
+
458
+        // Skip if an existing image is found.
459
+        if ( null !== $existing_image ) {
460
+            continue;
461
+        }
462
+
463
+        // Save the image and get the local path.
464
+        $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
465
+
466
+        if ( false === $image || is_wp_error( $image ) ) {
467
+            continue;
468
+        }
469
+
470
+        // Get the local URL.
471
+        $filename     = $image['path'];
472
+        $url          = $image['url'];
473
+        $content_type = $image['content_type'];
474
+
475
+        $attachment = array(
476
+            'guid'           => $url,
477
+            // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type
478
+            'post_title'     => $label,
479
+            // Set the title to the post title.
480
+            'post_content'   => '',
481
+            'post_status'    => 'inherit',
482
+            'post_mime_type' => $content_type,
483
+        );
484
+
485
+        // Create the attachment in WordPress and generate the related metadata.
486
+        $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
487
+
488
+        // Set the source URL for the image.
489
+        wl_set_source_url( $attachment_id, $image_remote_url );
490
+
491
+        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
492
+        wp_update_attachment_metadata( $attachment_id, $attachment_data );
493
+
494
+        // Set it as the featured image.
495
+        set_post_thumbnail( $post_id, $attachment_id );
496
+    }
497
+
498
+    // finally return the entity post.
499
+    return get_post( $post_id );
500 500
 }
Please login to merge, or discard this patch.
Spacing   +127 added lines, -127 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,40 +144,40 @@  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
 				// New entity, save it.
158
-				$existing_entity = wl_save_entity( $entity );
158
+				$existing_entity = wl_save_entity($entity);
159 159
 			} else {
160 160
 				// Existing entity, update post status.
161
-				if ( $existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish' ) {
161
+				if ($existing_entity instanceof WP_Post && $existing_entity->post_status !== 'publish') {
162 162
 					$parent_post_status = $post->post_status;
163
-					$post_status        = apply_filters( 'wl_feature__enable__entity-auto-publish', true )
163
+					$post_status        = apply_filters('wl_feature__enable__entity-auto-publish', true)
164 164
 						? $parent_post_status : 'draft';
165
-					wp_update_post( array(
165
+					wp_update_post(array(
166 166
 						'ID'          => $existing_entity->ID,
167 167
 						'post_status' => $post_status
168
-					) );
168
+					));
169 169
 				}
170 170
 			}
171 171
 
172
-			$uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) );
172
+			$uri = $content_service->get_entity_id(Wordpress_Content_Id::create_post($existing_entity->ID));
173 173
 
174 174
 
175 175
 			// Update entity data with related post
176 176
 			$entity['related_post_id'] = $post_id;
177 177
 
178 178
 			$internal_entity_uris[] = $uri;
179
-			wl_write_log( "Map $entity_uri on $uri" );
180
-			$entities_uri_mapping[ $entity_uri ] = $uri;
179
+			wl_write_log("Map $entity_uri on $uri");
180
+			$entities_uri_mapping[$entity_uri] = $uri;
181 181
 
182 182
 		}
183 183
 
@@ -187,14 +187,14 @@  discard block
 block discarded – undo
187 187
 	$updated_post_content = $post->post_content;
188 188
 
189 189
 	// Update the post content if we found mappings of new entities.
190
-	if ( ! empty( $entities_uri_mapping ) ) {
190
+	if ( ! empty($entities_uri_mapping)) {
191 191
 		// Save each entity and store the post id.
192
-		foreach ( $entities_uri_mapping as $tmp_uri => $uri ) {
193
-			if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) {
192
+		foreach ($entities_uri_mapping as $tmp_uri => $uri) {
193
+			if (1 !== preg_match('@^(https?://|local-entity-)@', $tmp_uri)) {
194 194
 				continue;
195 195
 			}
196 196
 
197
-			$updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content );
197
+			$updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content);
198 198
 		}
199 199
 
200 200
 		// Update the post content.
@@ -203,16 +203,16 @@  discard block
 block discarded – undo
203 203
 		 * content, so add the slashes to prevent back slash getting
204 204
 		 * removed.
205 205
 		 */
206
-		wp_update_post( array(
206
+		wp_update_post(array(
207 207
 			'ID'           => $post->ID,
208
-			'post_content' => addslashes( $updated_post_content ),
209
-		) );
208
+			'post_content' => addslashes($updated_post_content),
209
+		));
210 210
 	}
211 211
 
212 212
 	// Reset previously saved instances.
213
-	wl_core_delete_relation_instances( $post_id );
213
+	wl_core_delete_relation_instances($post_id);
214 214
 
215
-	$relations = Object_Relation_Factory::get_instance( $post_id )
215
+	$relations = Object_Relation_Factory::get_instance($post_id)
216 216
 	                                    ->get_relations_from_content(
217 217
 		                                    $updated_post_content,
218 218
 		                                    Object_Type_Enum::POST,
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
 	                                    );
221 221
 
222 222
 	// Save relation instances
223
-	foreach ( $relations as $relation ) {
223
+	foreach ($relations as $relation) {
224 224
 
225 225
 		wl_core_add_relation_instance(
226 226
 		// subject id.
@@ -237,9 +237,9 @@  discard block
 block discarded – undo
237 237
 
238 238
 	}
239 239
 
240
-	if ( isset( $_POST['wl_entities'] ) ) {
240
+	if (isset($_POST['wl_entities'])) {
241 241
 		// Save post metadata if available
242
-		$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
242
+		$metadata_via_post = (isset($_POST['wl_metadata'])) ?
243 243
 			$_POST['wl_metadata'] : array();
244 244
 
245 245
 		$fields = array(
@@ -248,35 +248,35 @@  discard block
 block discarded – undo
248 248
 		);
249 249
 
250 250
 		// Unlink topic taxonomy terms
251
-		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID );
251
+		Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID);
252 252
 
253
-		foreach ( $fields as $field ) {
253
+		foreach ($fields as $field) {
254 254
 
255 255
 			// Delete current values
256
-			delete_post_meta( $post->ID, $field );
256
+			delete_post_meta($post->ID, $field);
257 257
 			// Retrieve the entity uri
258
-			$uri = ( isset( $metadata_via_post[ $field ] ) ) ?
259
-				stripslashes( $metadata_via_post[ $field ] ) : '';
258
+			$uri = (isset($metadata_via_post[$field])) ?
259
+				stripslashes($metadata_via_post[$field]) : '';
260 260
 
261
-			if ( empty( $uri ) ) {
261
+			if (empty($uri)) {
262 262
 				continue;
263 263
 			}
264
-			$entity = $entity_service->get_entity_post_by_uri( $uri );
264
+			$entity = $entity_service->get_entity_post_by_uri($uri);
265 265
 
266
-			if ( $entity ) {
267
-				add_post_meta( $post->ID, $field, $entity->ID, true );
266
+			if ($entity) {
267
+				add_post_meta($post->ID, $field, $entity->ID, true);
268 268
 				// Set also the topic taxonomy
269
-				if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) {
270
-					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity );
269
+				if ($field === Wordlift_Schema_Service::FIELD_TOPIC) {
270
+					Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity);
271 271
 				}
272 272
 			}
273 273
 		}
274 274
 	}
275 275
 
276
-	add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
276
+	add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
277 277
 }
278 278
 
279
-add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' );
279
+add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
280 280
 
281 281
 /**
282 282
  * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is
@@ -294,14 +294,14 @@  discard block
 block discarded – undo
294 294
  *
295 295
  * @return null|WP_Post A post instance or null in case of failure.
296 296
  */
297
-function wl_save_entity( $entity_data ) {
297
+function wl_save_entity($entity_data) {
298 298
 
299 299
 	// Required for REST API calls
300
-	if ( ! function_exists( 'wp_crop_image' ) ) {
301
-		require_once( ABSPATH . 'wp-admin/includes/image.php' );
300
+	if ( ! function_exists('wp_crop_image')) {
301
+		require_once(ABSPATH.'wp-admin/includes/image.php');
302 302
 	}
303 303
 
304
-	$log = Wordlift_Log_Service::get_logger( 'wl_save_entity' );
304
+	$log = Wordlift_Log_Service::get_logger('wl_save_entity');
305 305
 
306 306
 	/*
307 307
 	 * Data is coming from a $_POST, sanitize it.
@@ -310,29 +310,29 @@  discard block
 block discarded – undo
310 310
 	 *
311 311
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/841
312 312
 	 */
313
-	$label            = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) );
313
+	$label            = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label']));
314 314
 	$type_uri         = $entity_data['main_type'];
315
-	$entity_types     = isset( $entity_data['type'] ) ? $entity_data['type'] : array();
315
+	$entity_types     = isset($entity_data['type']) ? $entity_data['type'] : array();
316 316
 	$description      = $entity_data['description'];
317
-	$images           = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array();
318
-	$same_as          = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array();
319
-	$related_post_id  = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null;
320
-	$other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array();
317
+	$images           = isset($entity_data['image']) ? (array) $entity_data['image'] : array();
318
+	$same_as          = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array();
319
+	$related_post_id  = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null;
320
+	$other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array();
321 321
 
322 322
 	// Get the synonyms.
323
-	$synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array();
323
+	$synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array();
324 324
 
325 325
 	// Check whether an entity already exists with the provided URI.
326 326
 	$uri = $entity_data['uri'];
327
-	if ( isset ( $uri ) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) {
328
-		$log->debug( "Post already exists for URI $uri." );
327
+	if (isset ($uri) && null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) {
328
+		$log->debug("Post already exists for URI $uri.");
329 329
 
330 330
 		return $post;
331 331
 	}
332 332
 
333 333
 	// Prepare properties of the new entity.
334
-	$post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id )
335
-		? get_post_status( $related_post_id ) : 'draft';
334
+	$post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) && is_numeric($related_post_id)
335
+		? get_post_status($related_post_id) : 'draft';
336 336
 
337 337
 	$params = array(
338 338
 		// @@todo: we don't want an entity to be automatically published.
@@ -346,7 +346,7 @@  discard block
 block discarded – undo
346 346
 		// new entities.
347 347
 		//
348 348
 		// See https://github.com/insideout10/wordlift-plugin/issues/282
349
-		'post_name'    => sanitize_title( $label ),
349
+		'post_name'    => sanitize_title($label),
350 350
 	);
351 351
 
352 352
 	// If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
@@ -355,18 +355,18 @@  discard block
 block discarded – undo
355 355
 	// This does NOT affect saving an entity from the entity admin page since this function is called when an entity
356 356
 	// is created when saving a post.
357 357
 	global $wpseo_metabox, $seo_ultimate;
358
-	if ( isset( $wpseo_metabox ) ) {
359
-		remove_action( 'wp_insert_post', array(
358
+	if (isset($wpseo_metabox)) {
359
+		remove_action('wp_insert_post', array(
360 360
 			$wpseo_metabox,
361 361
 			'save_postdata',
362
-		) );
362
+		));
363 363
 	}
364 364
 
365
-	if ( isset( $seo_ultimate ) ) {
366
-		remove_action( 'save_post', array(
365
+	if (isset($seo_ultimate)) {
366
+		remove_action('save_post', array(
367 367
 			$seo_ultimate,
368 368
 			'save_postmeta_box',
369
-		) );
369
+		));
370 370
 	}
371 371
 
372 372
 	// The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
@@ -376,94 +376,94 @@  discard block
 block discarded – undo
376 376
 	// see https://github.com/insideout10/wordlift-plugin/issues/156
377 377
 	// see https://github.com/insideout10/wordlift-plugin/issues/148
378 378
 	global $wp_filter;
379
-	$save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
380
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
379
+	$save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks;
380
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters();
381 381
 
382 382
 
383
-	$log->trace( 'Going to insert new post...' );
383
+	$log->trace('Going to insert new post...');
384 384
 
385 385
 	// create or update the post.
386
-	$post_id = wp_insert_post( $params, true );
386
+	$post_id = wp_insert_post($params, true);
387 387
 
388 388
 	// Setting the alternative labels for this entity.
389 389
 	Wordlift_Entity_Service::get_instance()
390
-	                       ->set_alternative_labels( $post_id, $synonyms );
390
+	                       ->set_alternative_labels($post_id, $synonyms);
391 391
 
392 392
 	// Restore all the existing filters.
393
-	is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
393
+	is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters;
394 394
 
395 395
 	// If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
396
-	if ( isset( $wpseo_metabox ) ) {
397
-		add_action( 'wp_insert_post', array(
396
+	if (isset($wpseo_metabox)) {
397
+		add_action('wp_insert_post', array(
398 398
 			$wpseo_metabox,
399 399
 			'save_postdata',
400
-		) );
400
+		));
401 401
 	}
402 402
 
403 403
 	// If SEO Ultimate is installed, add back the hook we removed a few lines above.
404
-	if ( isset( $seo_ultimate ) ) {
405
-		add_action( 'save_post', array(
404
+	if (isset($seo_ultimate)) {
405
+		add_action('save_post', array(
406 406
 			$seo_ultimate,
407 407
 			'save_postmeta_box',
408
-		), 10, 2 );
408
+		), 10, 2);
409 409
 	}
410 410
 
411 411
 	// TODO: handle errors.
412
-	if ( is_wp_error( $post_id ) ) {
413
-		$log->error( 'An error occurred: ' . $post_id->get_error_message() );
412
+	if (is_wp_error($post_id)) {
413
+		$log->error('An error occurred: '.$post_id->get_error_message());
414 414
 
415 415
 		// inform an error occurred.
416 416
 		return null;
417 417
 	}
418 418
 
419
-	wl_set_entity_main_type( $post_id, $type_uri );
419
+	wl_set_entity_main_type($post_id, $type_uri);
420 420
 
421 421
 	// Save the entity types.
422
-	wl_set_entity_rdf_types( $post_id, $entity_types );
422
+	wl_set_entity_rdf_types($post_id, $entity_types);
423 423
 
424 424
 	// Get a dataset URI for the entity.
425
-	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
425
+	$wl_uri = Wordlift_Entity_Service::get_instance()->get_uri($post_id);
426 426
 
427 427
 	// Add the uri to the sameAs data if it's not a local URI.
428
-	if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) &&
428
+	if (isset($uri) && preg_match('@https?://.*@', $uri) &&
429 429
 	     $wl_uri !== $uri &&
430 430
 	     // Only remote entities
431
-	     0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() )
431
+	     0 !== strpos($uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri())
432 432
 	) {
433
-		array_push( $same_as, $uri );
433
+		array_push($same_as, $uri);
434 434
 	}
435 435
 
436 436
 	// Save the sameAs data for the entity.
437
-	wl_schema_set_value( $post_id, 'sameAs', $same_as );
437
+	wl_schema_set_value($post_id, 'sameAs', $same_as);
438 438
 
439 439
 	// Save the other properties (latitude, langitude, startDate, endDate, etc.)
440
-	foreach ( $other_properties as $property_name => $property_value ) {
441
-		wl_schema_set_value( $post_id, $property_name, $property_value );
440
+	foreach ($other_properties as $property_name => $property_value) {
441
+		wl_schema_set_value($post_id, $property_name, $property_value);
442 442
 	}
443 443
 
444 444
 	// Call hooks.
445
-	do_action( 'wl_save_entity', $post_id );
445
+	do_action('wl_save_entity', $post_id);
446 446
 
447
-	foreach ( $images as $image_remote_url ) {
447
+	foreach ($images as $image_remote_url) {
448 448
 
449 449
 		// Check if image is already present in local DB
450
-		if ( strpos( $image_remote_url, site_url() ) !== false ) {
450
+		if (strpos($image_remote_url, site_url()) !== false) {
451 451
 			// Do nothing.
452 452
 			continue;
453 453
 		}
454 454
 
455 455
 		// Check if there is an existing attachment for this post ID and source URL.
456
-		$existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url );
456
+		$existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url);
457 457
 
458 458
 		// Skip if an existing image is found.
459
-		if ( null !== $existing_image ) {
459
+		if (null !== $existing_image) {
460 460
 			continue;
461 461
 		}
462 462
 
463 463
 		// Save the image and get the local path.
464
-		$image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url );
464
+		$image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url);
465 465
 
466
-		if ( false === $image || is_wp_error( $image ) ) {
466
+		if (false === $image || is_wp_error($image)) {
467 467
 			continue;
468 468
 		}
469 469
 
@@ -483,18 +483,18 @@  discard block
 block discarded – undo
483 483
 		);
484 484
 
485 485
 		// Create the attachment in WordPress and generate the related metadata.
486
-		$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
486
+		$attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
487 487
 
488 488
 		// Set the source URL for the image.
489
-		wl_set_source_url( $attachment_id, $image_remote_url );
489
+		wl_set_source_url($attachment_id, $image_remote_url);
490 490
 
491
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
492
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
491
+		$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
492
+		wp_update_attachment_metadata($attachment_id, $attachment_data);
493 493
 
494 494
 		// Set it as the featured image.
495
-		set_post_thumbnail( $post_id, $attachment_id );
495
+		set_post_thumbnail($post_id, $attachment_id);
496 496
 	}
497 497
 
498 498
 	// finally return the entity post.
499
-	return get_post( $post_id );
499
+	return get_post($post_id);
500 500
 }
Please login to merge, or discard this patch.