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