Completed
Push — master ( 6a2a3a...b548ab )
by David
02:46
created
src/includes/class-wordlift-entity-service.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -514,7 +514,7 @@
 block discarded – undo
514 514
 		 * @param null $logo The entity logo id (or NULL if none).
515 515
 		 * @param string $status The post status, by default 'publish'.
516 516
 		 *
517
-		 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
517
+		 * @return integer The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
518 518
 		 * @since 3.9.0
519 519
 		 *
520 520
 		 */
Please login to merge, or discard this patch.
Indentation   +565 added lines, -565 removed lines patch added patch discarded remove patch
@@ -16,484 +16,484 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Entity_Service {
18 18
 
19
-	/**
20
-	 * The Log service.
21
-	 *
22
-	 * @since  3.2.0
23
-	 * @access private
24
-	 * @var \Wordlift_Log_Service $log The Log service.
25
-	 */
26
-	private $log;
27
-
28
-	/**
29
-	 * The UI service.
30
-	 *
31
-	 * @since  3.2.0
32
-	 * @access private
33
-	 * @var \Wordlift_UI_Service $ui_service The UI service.
34
-	 */
35
-	private $ui_service;
36
-
37
-	/**
38
-	 * The {@link Wordlift_Relation_Service} instance.
39
-	 *
40
-	 * @since  3.15.0
41
-	 * @access private
42
-	 * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
43
-	 */
44
-	private $relation_service;
45
-
46
-	/**
47
-	 * The {@link Wordlift_Entity_Uri_Service} instance.
48
-	 *
49
-	 * @since  3.16.3
50
-	 * @access private
51
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
52
-	 */
53
-	private $entity_uri_service;
54
-
55
-	/**
56
-	 * The entity post type name.
57
-	 *
58
-	 * @since 3.1.0
59
-	 */
60
-	const TYPE_NAME = 'entity';
61
-
62
-	/**
63
-	 * The alternative label meta key.
64
-	 *
65
-	 * @since 3.2.0
66
-	 */
67
-	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
68
-
69
-	/**
70
-	 * The alternative label input template.
71
-	 *
72
-	 * @since 3.2.0
73
-	 */
74
-	// TODO: this should be moved to a class that deals with HTML code.
75
-	const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
19
+    /**
20
+     * The Log service.
21
+     *
22
+     * @since  3.2.0
23
+     * @access private
24
+     * @var \Wordlift_Log_Service $log The Log service.
25
+     */
26
+    private $log;
27
+
28
+    /**
29
+     * The UI service.
30
+     *
31
+     * @since  3.2.0
32
+     * @access private
33
+     * @var \Wordlift_UI_Service $ui_service The UI service.
34
+     */
35
+    private $ui_service;
36
+
37
+    /**
38
+     * The {@link Wordlift_Relation_Service} instance.
39
+     *
40
+     * @since  3.15.0
41
+     * @access private
42
+     * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
43
+     */
44
+    private $relation_service;
45
+
46
+    /**
47
+     * The {@link Wordlift_Entity_Uri_Service} instance.
48
+     *
49
+     * @since  3.16.3
50
+     * @access private
51
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
52
+     */
53
+    private $entity_uri_service;
54
+
55
+    /**
56
+     * The entity post type name.
57
+     *
58
+     * @since 3.1.0
59
+     */
60
+    const TYPE_NAME = 'entity';
61
+
62
+    /**
63
+     * The alternative label meta key.
64
+     *
65
+     * @since 3.2.0
66
+     */
67
+    const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
68
+
69
+    /**
70
+     * The alternative label input template.
71
+     *
72
+     * @since 3.2.0
73
+     */
74
+    // TODO: this should be moved to a class that deals with HTML code.
75
+    const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
76 76
                 <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label>
77 77
                 <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text">
78 78
                 <button class="button wl-delete-button">%s</button>
79 79
                 </div>';
80 80
 
81
-	/**
82
-	 * A singleton instance of the Entity service.
83
-	 *
84
-	 * @since  3.2.0
85
-	 * @access private
86
-	 * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
87
-	 */
88
-	private static $instance;
89
-
90
-	/**
91
-	 * Create a Wordlift_Entity_Service instance.
92
-	 *
93
-	 * @param \Wordlift_UI_Service $ui_service The UI service.
94
-	 * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
95
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
96
-	 *
97
-	 * @since 3.2.0
98
-	 *
99
-	 */
100
-	public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
101
-
102
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
103
-
104
-		$this->ui_service         = $ui_service;
105
-		$this->relation_service   = $relation_service;
106
-		$this->entity_uri_service = $entity_uri_service;
107
-
108
-		// Set the singleton instance.
109
-		self::$instance = $this;
110
-	}
111
-
112
-	/**
113
-	 * Get the singleton instance of the Entity service.
114
-	 *
115
-	 * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
116
-	 * @since 3.2.0
117
-	 */
118
-	public static function get_instance() {
119
-
120
-		return self::$instance;
121
-	}
122
-
123
-	/**
124
-	 * Determines whether a post is an entity or not. Entity is in this context
125
-	 * something which is not an article.
126
-	 *
127
-	 * @param int $post_id A post id.
128
-	 *
129
-	 * @return bool Return true if the post is an entity otherwise false.
130
-	 * @since 3.1.0
131
-	 *
132
-	 */
133
-	public function is_entity( $post_id ) {
134
-
135
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
136
-
137
-		if ( is_wp_error( $terms ) ) {
138
-			$this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
139
-
140
-			return false;
141
-		}
142
-
143
-		if ( empty( $terms ) ) {
144
-			return false;
145
-		}
146
-
147
-		/*
81
+    /**
82
+     * A singleton instance of the Entity service.
83
+     *
84
+     * @since  3.2.0
85
+     * @access private
86
+     * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
87
+     */
88
+    private static $instance;
89
+
90
+    /**
91
+     * Create a Wordlift_Entity_Service instance.
92
+     *
93
+     * @param \Wordlift_UI_Service $ui_service The UI service.
94
+     * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
95
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
96
+     *
97
+     * @since 3.2.0
98
+     *
99
+     */
100
+    public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
101
+
102
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
103
+
104
+        $this->ui_service         = $ui_service;
105
+        $this->relation_service   = $relation_service;
106
+        $this->entity_uri_service = $entity_uri_service;
107
+
108
+        // Set the singleton instance.
109
+        self::$instance = $this;
110
+    }
111
+
112
+    /**
113
+     * Get the singleton instance of the Entity service.
114
+     *
115
+     * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
116
+     * @since 3.2.0
117
+     */
118
+    public static function get_instance() {
119
+
120
+        return self::$instance;
121
+    }
122
+
123
+    /**
124
+     * Determines whether a post is an entity or not. Entity is in this context
125
+     * something which is not an article.
126
+     *
127
+     * @param int $post_id A post id.
128
+     *
129
+     * @return bool Return true if the post is an entity otherwise false.
130
+     * @since 3.1.0
131
+     *
132
+     */
133
+    public function is_entity( $post_id ) {
134
+
135
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
136
+
137
+        if ( is_wp_error( $terms ) ) {
138
+            $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
139
+
140
+            return false;
141
+        }
142
+
143
+        if ( empty( $terms ) ) {
144
+            return false;
145
+        }
146
+
147
+        /*
148 148
 		 * We don't consider an `article` to be an entity.
149 149
 		 *
150 150
 		 * @since 3.20.0 At least one associated mustn't be an `article`.
151 151
 		 *
152 152
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
153 153
 		 */
154
-		foreach ( $terms as $term ) {
155
-			if ( 'article' !== $term->slug ) {
156
-				return true;
157
-			}
158
-		}
159
-
160
-		return false;
161
-	}
162
-
163
-	/**
164
-	 * Get the proper classification scope for a given entity post
165
-	 *
166
-	 * @param integer $post_id An entity post id.
167
-	 *
168
-	 * @param string $default The default classification scope, `what` if not
169
-	 *                         provided.
170
-	 *
171
-	 * @return string Returns a classification scope (e.g. 'what').
172
-	 * @since 3.5.0
173
-	 *
174
-	 */
175
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
176
-
177
-		if ( false === $this->is_entity( $post_id ) ) {
178
-			return $default;
179
-		}
180
-
181
-		// Retrieve the entity type
182
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
183
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
184
-		// Retrieve classification boxes configuration
185
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
186
-		foreach ( $classification_boxes as $cb ) {
187
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
188
-				return $cb['id'];
189
-			}
190
-		}
191
-
192
-		return $default;
193
-	}
194
-
195
-	/**
196
-	 * Check whether a {@link WP_Post} is used.
197
-	 *
198
-	 * @param int $post_id The {@link WP_Post}'s id.
199
-	 *
200
-	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
201
-	 */
202
-	public function is_used( $post_id ) {
203
-
204
-		if ( false === $this->is_entity( $post_id ) ) {
205
-			return null;
206
-		}
207
-		// Retrieve the post
208
-		$entity = get_post( $post_id );
209
-
210
-		global $wpdb;
211
-		// Retrieve Wordlift relation instances table name
212
-		$table_name = wl_core_get_relation_instances_table_name();
213
-
214
-		// Check is it's referenced / related to another post / entity
215
-		$stmt = $wpdb->prepare(
216
-			"SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
217
-			$entity->ID
218
-		);
219
-
220
-		// Perform the query
221
-		$relation_instances = (int) $wpdb->get_var( $stmt );
222
-		// If there is at least one relation instance for the current entity, then it's used
223
-		if ( 0 < $relation_instances ) {
224
-			return true;
225
-		}
226
-
227
-		// Check if the entity uri is used as meta_value
228
-		$stmt = $wpdb->prepare(
229
-			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
230
-			$entity->ID,
231
-			wl_get_entity_uri( $entity->ID )
232
-		);
233
-		// Perform the query
234
-		$meta_instances = (int) $wpdb->get_var( $stmt );
235
-
236
-		// If there is at least one meta that refers the current entity uri, then current entity is used
237
-		if ( 0 < $meta_instances ) {
238
-			return true;
239
-		}
240
-
241
-		// If we are here, it means the current entity is not used at the moment
242
-		return false;
243
-	}
244
-
245
-	/**
246
-	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
247
-	 *
248
-	 * @param string $uri The entity URI.
249
-	 *
250
-	 * @return WP_Post|null A WP_Post instance or null if not found.
251
-	 * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
252
-	 *
253
-	 * @since      3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
254
-	 * @since      3.2.0
255
-	 *
256
-	 */
257
-	public function get_entity_post_by_uri( $uri ) {
258
-
259
-		return $this->entity_uri_service->get_entity( $uri );
260
-	}
261
-
262
-	/**
263
-	 * Fires once a post has been saved. This function uses the $_REQUEST, therefore
264
-	 * we check that the post we're saving is the current post.
265
-	 *
266
-	 * @see   https://github.com/insideout10/wordlift-plugin/issues/363
267
-	 *
268
-	 * @since 3.2.0
269
-	 *
270
-	 * @param int $post_id Post ID.
271
-	 * @param WP_Post $post Post object.
272
-	 * @param bool $update Whether this is an existing post being updated or not.
273
-	 */
274
-	public function save_post( $post_id, $post, $update ) {
275
-
276
-		// Avoid doing anything if post is autosave or a revision.
277
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
278
-			return;
279
-		}
280
-
281
-		// We're setting the alternative label that have been provided via the UI
282
-		// (in fact we're using $_REQUEST), while save_post may be also called
283
-		// programmatically by some other function: we need to check therefore if
284
-		// the $post_id in the save_post call matches the post id set in the request.
285
-		//
286
-		// If this is not the current post being saved or if it's not an entity, return.
287
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
288
-			return;
289
-		}
290
-
291
-		// Get the alt labels from the request (or empty array).
292
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
293
-
294
-		// Set the alternative labels.
295
-		$this->set_alternative_labels( $post_id, $alt_labels );
296
-
297
-	}
298
-
299
-	/**
300
-	 * Set the alternative labels.
301
-	 *
302
-	 * @param int $post_id The post id.
303
-	 * @param array $alt_labels An array of labels.
304
-	 *
305
-	 * @since 3.2.0
306
-	 *
307
-	 */
308
-	public function set_alternative_labels( $post_id, $alt_labels ) {
309
-
310
-		// Force $alt_labels to be an array
311
-		if ( ! is_array( $alt_labels ) ) {
312
-			$alt_labels = array( $alt_labels );
313
-		}
314
-
315
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
316
-
317
-		// Delete all the existing alternate labels.
318
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
319
-
320
-		// Set the alternative labels.
321
-		foreach ( $alt_labels as $alt_label ) {
322
-			if ( ! empty( $alt_label ) ) {
323
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
324
-			}
325
-		}
326
-
327
-	}
328
-
329
-	/**
330
-	 * Retrieve the alternate labels.
331
-	 *
332
-	 * @param int $post_id Post id.
333
-	 *
334
-	 * @return mixed An array  of alternative labels.
335
-	 * @since 3.2.0
336
-	 *
337
-	 */
338
-	public function get_alternative_labels( $post_id ) {
339
-
340
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
341
-	}
342
-
343
-	/**
344
-	 * Retrieve the labels for an entity, i.e. the title + the synonyms.
345
-	 *
346
-	 * @param int $post_id The entity {@link WP_Post} id.
347
-	 *
348
-	 * @return array An array with the entity title and labels.
349
-	 * @since 3.12.0
350
-	 *
351
-	 */
352
-	public function get_labels( $post_id ) {
353
-
354
-		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
355
-	}
356
-
357
-	/**
358
-	 * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
359
-	 *
360
-	 * @param WP_Post $post Post object.
361
-	 *
362
-	 * @since 3.2.0
363
-	 *
364
-	 */
365
-	public function edit_form_before_permalink( $post ) {
366
-
367
-		// If it's not an entity, return.
368
-		if ( ! $this->is_entity( $post->ID ) ) {
369
-			return;
370
-		}
371
-
372
-		// Print the input template.
373
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
374
-
375
-		// Print all the currently set alternative labels.
376
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
377
-
378
-			echo $this->get_alternative_label_input( $alt_label );
379
-
380
-		};
381
-
382
-		// Print the button.
383
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
384
-
385
-	}
386
-
387
-	/**
388
-	 * Get the URI for the entity with the specified post id.
389
-	 *
390
-	 * @param int $post_id The entity post id.
391
-	 *
392
-	 * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
393
-	 * @since 3.6.0
394
-	 *
395
-	 */
396
-	public function get_uri( $post_id ) {
397
-
398
-		// If a null is given, nothing to do
399
-		if ( null == $post_id ) {
400
-			return null;
401
-		}
402
-
403
-		$dataset_uri = wl_configuration_get_redlink_dataset_uri();
404
-
405
-		// If the dataset uri is not properly configured, null is returned
406
-		if ( empty( $dataset_uri ) ) {
407
-			return null;
408
-		}
409
-
410
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
411
-
412
-		/*
154
+        foreach ( $terms as $term ) {
155
+            if ( 'article' !== $term->slug ) {
156
+                return true;
157
+            }
158
+        }
159
+
160
+        return false;
161
+    }
162
+
163
+    /**
164
+     * Get the proper classification scope for a given entity post
165
+     *
166
+     * @param integer $post_id An entity post id.
167
+     *
168
+     * @param string $default The default classification scope, `what` if not
169
+     *                         provided.
170
+     *
171
+     * @return string Returns a classification scope (e.g. 'what').
172
+     * @since 3.5.0
173
+     *
174
+     */
175
+    public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
176
+
177
+        if ( false === $this->is_entity( $post_id ) ) {
178
+            return $default;
179
+        }
180
+
181
+        // Retrieve the entity type
182
+        $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
183
+        $entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
184
+        // Retrieve classification boxes configuration
185
+        $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
186
+        foreach ( $classification_boxes as $cb ) {
187
+            if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
188
+                return $cb['id'];
189
+            }
190
+        }
191
+
192
+        return $default;
193
+    }
194
+
195
+    /**
196
+     * Check whether a {@link WP_Post} is used.
197
+     *
198
+     * @param int $post_id The {@link WP_Post}'s id.
199
+     *
200
+     * @return bool|null Null if it's not an entity, otherwise true if it's used.
201
+     */
202
+    public function is_used( $post_id ) {
203
+
204
+        if ( false === $this->is_entity( $post_id ) ) {
205
+            return null;
206
+        }
207
+        // Retrieve the post
208
+        $entity = get_post( $post_id );
209
+
210
+        global $wpdb;
211
+        // Retrieve Wordlift relation instances table name
212
+        $table_name = wl_core_get_relation_instances_table_name();
213
+
214
+        // Check is it's referenced / related to another post / entity
215
+        $stmt = $wpdb->prepare(
216
+            "SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
217
+            $entity->ID
218
+        );
219
+
220
+        // Perform the query
221
+        $relation_instances = (int) $wpdb->get_var( $stmt );
222
+        // If there is at least one relation instance for the current entity, then it's used
223
+        if ( 0 < $relation_instances ) {
224
+            return true;
225
+        }
226
+
227
+        // Check if the entity uri is used as meta_value
228
+        $stmt = $wpdb->prepare(
229
+            "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
230
+            $entity->ID,
231
+            wl_get_entity_uri( $entity->ID )
232
+        );
233
+        // Perform the query
234
+        $meta_instances = (int) $wpdb->get_var( $stmt );
235
+
236
+        // If there is at least one meta that refers the current entity uri, then current entity is used
237
+        if ( 0 < $meta_instances ) {
238
+            return true;
239
+        }
240
+
241
+        // If we are here, it means the current entity is not used at the moment
242
+        return false;
243
+    }
244
+
245
+    /**
246
+     * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
247
+     *
248
+     * @param string $uri The entity URI.
249
+     *
250
+     * @return WP_Post|null A WP_Post instance or null if not found.
251
+     * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
252
+     *
253
+     * @since      3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
254
+     * @since      3.2.0
255
+     *
256
+     */
257
+    public function get_entity_post_by_uri( $uri ) {
258
+
259
+        return $this->entity_uri_service->get_entity( $uri );
260
+    }
261
+
262
+    /**
263
+     * Fires once a post has been saved. This function uses the $_REQUEST, therefore
264
+     * we check that the post we're saving is the current post.
265
+     *
266
+     * @see   https://github.com/insideout10/wordlift-plugin/issues/363
267
+     *
268
+     * @since 3.2.0
269
+     *
270
+     * @param int $post_id Post ID.
271
+     * @param WP_Post $post Post object.
272
+     * @param bool $update Whether this is an existing post being updated or not.
273
+     */
274
+    public function save_post( $post_id, $post, $update ) {
275
+
276
+        // Avoid doing anything if post is autosave or a revision.
277
+        if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
278
+            return;
279
+        }
280
+
281
+        // We're setting the alternative label that have been provided via the UI
282
+        // (in fact we're using $_REQUEST), while save_post may be also called
283
+        // programmatically by some other function: we need to check therefore if
284
+        // the $post_id in the save_post call matches the post id set in the request.
285
+        //
286
+        // If this is not the current post being saved or if it's not an entity, return.
287
+        if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
288
+            return;
289
+        }
290
+
291
+        // Get the alt labels from the request (or empty array).
292
+        $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
293
+
294
+        // Set the alternative labels.
295
+        $this->set_alternative_labels( $post_id, $alt_labels );
296
+
297
+    }
298
+
299
+    /**
300
+     * Set the alternative labels.
301
+     *
302
+     * @param int $post_id The post id.
303
+     * @param array $alt_labels An array of labels.
304
+     *
305
+     * @since 3.2.0
306
+     *
307
+     */
308
+    public function set_alternative_labels( $post_id, $alt_labels ) {
309
+
310
+        // Force $alt_labels to be an array
311
+        if ( ! is_array( $alt_labels ) ) {
312
+            $alt_labels = array( $alt_labels );
313
+        }
314
+
315
+        $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
316
+
317
+        // Delete all the existing alternate labels.
318
+        delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
319
+
320
+        // Set the alternative labels.
321
+        foreach ( $alt_labels as $alt_label ) {
322
+            if ( ! empty( $alt_label ) ) {
323
+                add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
324
+            }
325
+        }
326
+
327
+    }
328
+
329
+    /**
330
+     * Retrieve the alternate labels.
331
+     *
332
+     * @param int $post_id Post id.
333
+     *
334
+     * @return mixed An array  of alternative labels.
335
+     * @since 3.2.0
336
+     *
337
+     */
338
+    public function get_alternative_labels( $post_id ) {
339
+
340
+        return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
341
+    }
342
+
343
+    /**
344
+     * Retrieve the labels for an entity, i.e. the title + the synonyms.
345
+     *
346
+     * @param int $post_id The entity {@link WP_Post} id.
347
+     *
348
+     * @return array An array with the entity title and labels.
349
+     * @since 3.12.0
350
+     *
351
+     */
352
+    public function get_labels( $post_id ) {
353
+
354
+        return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
355
+    }
356
+
357
+    /**
358
+     * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
359
+     *
360
+     * @param WP_Post $post Post object.
361
+     *
362
+     * @since 3.2.0
363
+     *
364
+     */
365
+    public function edit_form_before_permalink( $post ) {
366
+
367
+        // If it's not an entity, return.
368
+        if ( ! $this->is_entity( $post->ID ) ) {
369
+            return;
370
+        }
371
+
372
+        // Print the input template.
373
+        $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
374
+
375
+        // Print all the currently set alternative labels.
376
+        foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
377
+
378
+            echo $this->get_alternative_label_input( $alt_label );
379
+
380
+        };
381
+
382
+        // Print the button.
383
+        $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
384
+
385
+    }
386
+
387
+    /**
388
+     * Get the URI for the entity with the specified post id.
389
+     *
390
+     * @param int $post_id The entity post id.
391
+     *
392
+     * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
393
+     * @since 3.6.0
394
+     *
395
+     */
396
+    public function get_uri( $post_id ) {
397
+
398
+        // If a null is given, nothing to do
399
+        if ( null == $post_id ) {
400
+            return null;
401
+        }
402
+
403
+        $dataset_uri = wl_configuration_get_redlink_dataset_uri();
404
+
405
+        // If the dataset uri is not properly configured, null is returned
406
+        if ( empty( $dataset_uri ) ) {
407
+            return null;
408
+        }
409
+
410
+        $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
411
+
412
+        /*
413 413
 		 * Consider the URI invalid if it doesn't start with the dataset URI.
414 414
 		 *
415 415
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/996
416 416
 		 */
417
-		if ( 0 !== strpos( $uri, $dataset_uri ) ) {
418
-			$uri = null;
419
-		}
420
-
421
-		// Set the URI if it isn't set yet.
422
-		$post_status = get_post_status( $post_id );
423
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
424
-			$uri = wl_build_entity_uri( $post_id );
425
-			wl_set_entity_uri( $post_id, $uri );
426
-		}
427
-
428
-		return $uri;
429
-	}
430
-
431
-
432
-	/**
433
-	 * Get the alternative label input HTML code.
434
-	 *
435
-	 * @param string $value The input value.
436
-	 *
437
-	 * @return string The input HTML code.
438
-	 * @since 3.2.0
439
-	 *
440
-	 */
441
-		private
442
-		function get_alternative_label_input( $value = '' ) {
443
-
444
-			return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
445
-		}
446
-
447
-		/**
448
-		 * Get the number of entity posts published in this blog.
449
-		 *
450
-		 * @return int The number of published entity posts.
451
-		 * @since 3.6.0
452
-		 *
453
-		 */
454
-		public
455
-		function count() {
456
-
457
-			$posts = get_posts( $this->add_criterias( array(
458
-				'fields'      => 'ids',
459
-				'post_status' => 'any',
460
-				'numberposts' => - 1,
461
-			) ) );
462
-
463
-			return count( $posts );
464
-		}
465
-
466
-		/**
467
-		 * Add the entity filtering criterias to the arguments for a `get_posts`
468
-		 * call.
469
-		 *
470
-		 * @param array $args The arguments for a `get_posts` call.
471
-		 *
472
-		 * @return array The arguments for a `get_posts` call.
473
-		 * @since 3.15.0
474
-		 *
475
-		 */
476
-		public
477
-		static function add_criterias( $args ) {
478
-
479
-			// Build an optimal tax-query.
480
-			$tax_query = array(
481
-				'relation' => 'AND',
482
-				array(
483
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
484
-					'operator' => 'EXISTS',
485
-				),
486
-				array(
487
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
488
-					'field'    => 'slug',
489
-					'terms'    => 'article',
490
-					'operator' => 'NOT IN',
491
-				),
492
-			);
493
-
494
-			return $args + array(
495
-					'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
496
-					/*
417
+        if ( 0 !== strpos( $uri, $dataset_uri ) ) {
418
+            $uri = null;
419
+        }
420
+
421
+        // Set the URI if it isn't set yet.
422
+        $post_status = get_post_status( $post_id );
423
+        if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
424
+            $uri = wl_build_entity_uri( $post_id );
425
+            wl_set_entity_uri( $post_id, $uri );
426
+        }
427
+
428
+        return $uri;
429
+    }
430
+
431
+
432
+    /**
433
+     * Get the alternative label input HTML code.
434
+     *
435
+     * @param string $value The input value.
436
+     *
437
+     * @return string The input HTML code.
438
+     * @since 3.2.0
439
+     *
440
+     */
441
+        private
442
+        function get_alternative_label_input( $value = '' ) {
443
+
444
+            return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
445
+        }
446
+
447
+        /**
448
+         * Get the number of entity posts published in this blog.
449
+         *
450
+         * @return int The number of published entity posts.
451
+         * @since 3.6.0
452
+         *
453
+         */
454
+        public
455
+        function count() {
456
+
457
+            $posts = get_posts( $this->add_criterias( array(
458
+                'fields'      => 'ids',
459
+                'post_status' => 'any',
460
+                'numberposts' => - 1,
461
+            ) ) );
462
+
463
+            return count( $posts );
464
+        }
465
+
466
+        /**
467
+         * Add the entity filtering criterias to the arguments for a `get_posts`
468
+         * call.
469
+         *
470
+         * @param array $args The arguments for a `get_posts` call.
471
+         *
472
+         * @return array The arguments for a `get_posts` call.
473
+         * @since 3.15.0
474
+         *
475
+         */
476
+        public
477
+        static function add_criterias( $args ) {
478
+
479
+            // Build an optimal tax-query.
480
+            $tax_query = array(
481
+                'relation' => 'AND',
482
+                array(
483
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
484
+                    'operator' => 'EXISTS',
485
+                ),
486
+                array(
487
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
488
+                    'field'    => 'slug',
489
+                    'terms'    => 'article',
490
+                    'operator' => 'NOT IN',
491
+                ),
492
+            );
493
+
494
+            return $args + array(
495
+                    'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
496
+                    /*
497 497
 					 * Ensure compatibility with Polylang.
498 498
 					 *
499 499
 					 * @see https://github.com/insideout10/wordlift-plugin/issues/855.
@@ -501,105 +501,105 @@  discard block
 block discarded – undo
501 501
 					 *
502 502
 					 * @since 3.19.5
503 503
 					 */
504
-					'lang'      => '',
505
-					'tax_query' => $tax_query,
506
-				);
507
-		}
508
-
509
-		/**
510
-		 * Create a new entity.
511
-		 *
512
-		 * @param string $name The entity name.
513
-		 * @param string $type_uri The entity's type URI.
514
-		 * @param null $logo The entity logo id (or NULL if none).
515
-		 * @param string $status The post status, by default 'publish'.
516
-		 *
517
-		 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
518
-		 * @since 3.9.0
519
-		 *
520
-		 */
521
-		public
522
-		function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
523
-
524
-			// Create an entity for the publisher.
525
-			$post_id = @wp_insert_post( array(
526
-				'post_type'    => self::TYPE_NAME,
527
-				'post_title'   => $name,
528
-				'post_status'  => $status,
529
-				'post_content' => '',
530
-			) );
531
-
532
-			// Return the error if any.
533
-			if ( is_wp_error( $post_id ) ) {
534
-				return $post_id;
535
-			}
536
-
537
-			// Set the entity logo.
538
-			if ( $logo && is_numeric( $logo ) ) {
539
-				set_post_thumbnail( $post_id, $logo );
540
-			}
541
-
542
-			// Set the entity type.
543
-			Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
544
-
545
-			return $post_id;
546
-		}
547
-
548
-		/**
549
-		 * Get the entities related to the one with the specified id. By default only
550
-		 * published entities will be returned.
551
-		 *
552
-		 * @param int $id The post id.
553
-		 * @param string $post_status The target post status (default = publish).
554
-		 *
555
-		 * @return array An array of post ids.
556
-		 * @since 3.10.0
557
-		 *
558
-		 */
559
-		public
560
-		function get_related_entities( $id, $post_status = 'publish' ) {
561
-
562
-			return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
563
-		}
564
-
565
-		/**
566
-		 * Get the list of entities.
567
-		 *
568
-		 * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
569
-		 *
570
-		 * @return array An array of entity posts.
571
-		 * @since 3.12.2
572
-		 *
573
-		 */
574
-		public
575
-		function get( $params = array() ) {
576
-
577
-			// Set the defaults.
578
-			$defaults = array( 'post_type' => 'entity' );
579
-
580
-			// Merge the defaults with the provided parameters.
581
-			$args = wp_parse_args( $params, $defaults );
582
-
583
-			// Call the `get_posts` function.
584
-			return get_posts( $args );
585
-		}
586
-
587
-		/**
588
-		 * The list of post type names which can be used for entities
589
-		 *
590
-		 * Criteria is that the post type is public. The list of valid post types
591
-		 * can be overridden with a filter.
592
-		 *
593
-		 * @return array Array containing the names of the valid post types.
594
-		 * @since 3.15.0
595
-		 *
596
-		 */
597
-		static function valid_entity_post_types() {
598
-
599
-			// Ignore builtins in the call to avoid getting attachments.
600
-			$post_types = array( 'post', 'page', self::TYPE_NAME );
601
-
602
-			return apply_filters( 'wl_valid_entity_post_types', $post_types );
603
-		}
604
-
605
-	}
504
+                    'lang'      => '',
505
+                    'tax_query' => $tax_query,
506
+                );
507
+        }
508
+
509
+        /**
510
+         * Create a new entity.
511
+         *
512
+         * @param string $name The entity name.
513
+         * @param string $type_uri The entity's type URI.
514
+         * @param null $logo The entity logo id (or NULL if none).
515
+         * @param string $status The post status, by default 'publish'.
516
+         *
517
+         * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
518
+         * @since 3.9.0
519
+         *
520
+         */
521
+        public
522
+        function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
523
+
524
+            // Create an entity for the publisher.
525
+            $post_id = @wp_insert_post( array(
526
+                'post_type'    => self::TYPE_NAME,
527
+                'post_title'   => $name,
528
+                'post_status'  => $status,
529
+                'post_content' => '',
530
+            ) );
531
+
532
+            // Return the error if any.
533
+            if ( is_wp_error( $post_id ) ) {
534
+                return $post_id;
535
+            }
536
+
537
+            // Set the entity logo.
538
+            if ( $logo && is_numeric( $logo ) ) {
539
+                set_post_thumbnail( $post_id, $logo );
540
+            }
541
+
542
+            // Set the entity type.
543
+            Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
544
+
545
+            return $post_id;
546
+        }
547
+
548
+        /**
549
+         * Get the entities related to the one with the specified id. By default only
550
+         * published entities will be returned.
551
+         *
552
+         * @param int $id The post id.
553
+         * @param string $post_status The target post status (default = publish).
554
+         *
555
+         * @return array An array of post ids.
556
+         * @since 3.10.0
557
+         *
558
+         */
559
+        public
560
+        function get_related_entities( $id, $post_status = 'publish' ) {
561
+
562
+            return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
563
+        }
564
+
565
+        /**
566
+         * Get the list of entities.
567
+         *
568
+         * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
569
+         *
570
+         * @return array An array of entity posts.
571
+         * @since 3.12.2
572
+         *
573
+         */
574
+        public
575
+        function get( $params = array() ) {
576
+
577
+            // Set the defaults.
578
+            $defaults = array( 'post_type' => 'entity' );
579
+
580
+            // Merge the defaults with the provided parameters.
581
+            $args = wp_parse_args( $params, $defaults );
582
+
583
+            // Call the `get_posts` function.
584
+            return get_posts( $args );
585
+        }
586
+
587
+        /**
588
+         * The list of post type names which can be used for entities
589
+         *
590
+         * Criteria is that the post type is public. The list of valid post types
591
+         * can be overridden with a filter.
592
+         *
593
+         * @return array Array containing the names of the valid post types.
594
+         * @since 3.15.0
595
+         *
596
+         */
597
+        static function valid_entity_post_types() {
598
+
599
+            // Ignore builtins in the call to avoid getting attachments.
600
+            $post_types = array( 'post', 'page', self::TYPE_NAME );
601
+
602
+            return apply_filters( 'wl_valid_entity_post_types', $post_types );
603
+        }
604
+
605
+    }
Please login to merge, or discard this patch.
Spacing   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -97,9 +97,9 @@  discard block
 block discarded – undo
97 97
 	 * @since 3.2.0
98 98
 	 *
99 99
 	 */
100
-	public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
100
+	public function __construct($ui_service, $relation_service, $entity_uri_service) {
101 101
 
102
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
102
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service');
103 103
 
104 104
 		$this->ui_service         = $ui_service;
105 105
 		$this->relation_service   = $relation_service;
@@ -130,17 +130,17 @@  discard block
 block discarded – undo
130 130
 	 * @since 3.1.0
131 131
 	 *
132 132
 	 */
133
-	public function is_entity( $post_id ) {
133
+	public function is_entity($post_id) {
134 134
 
135
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
135
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
136 136
 
137
-		if ( is_wp_error( $terms ) ) {
138
-			$this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
137
+		if (is_wp_error($terms)) {
138
+			$this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message());
139 139
 
140 140
 			return false;
141 141
 		}
142 142
 
143
-		if ( empty( $terms ) ) {
143
+		if (empty($terms)) {
144 144
 			return false;
145 145
 		}
146 146
 
@@ -151,8 +151,8 @@  discard block
 block discarded – undo
151 151
 		 *
152 152
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
153 153
 		 */
154
-		foreach ( $terms as $term ) {
155
-			if ( 'article' !== $term->slug ) {
154
+		foreach ($terms as $term) {
155
+			if ('article' !== $term->slug) {
156 156
 				return true;
157 157
 			}
158 158
 		}
@@ -172,19 +172,19 @@  discard block
 block discarded – undo
172 172
 	 * @since 3.5.0
173 173
 	 *
174 174
 	 */
175
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
175
+	public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) {
176 176
 
177
-		if ( false === $this->is_entity( $post_id ) ) {
177
+		if (false === $this->is_entity($post_id)) {
178 178
 			return $default;
179 179
 		}
180 180
 
181 181
 		// Retrieve the entity type
182
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
183
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
182
+		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id);
183
+		$entity_type     = str_replace('wl-', '', $entity_type_arr['css_class']);
184 184
 		// Retrieve classification boxes configuration
185
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
186
-		foreach ( $classification_boxes as $cb ) {
187
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
185
+		$classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
186
+		foreach ($classification_boxes as $cb) {
187
+			if (in_array($entity_type, $cb['registeredTypes'])) {
188 188
 				return $cb['id'];
189 189
 			}
190 190
 		}
@@ -199,13 +199,13 @@  discard block
 block discarded – undo
199 199
 	 *
200 200
 	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
201 201
 	 */
202
-	public function is_used( $post_id ) {
202
+	public function is_used($post_id) {
203 203
 
204
-		if ( false === $this->is_entity( $post_id ) ) {
204
+		if (false === $this->is_entity($post_id)) {
205 205
 			return null;
206 206
 		}
207 207
 		// Retrieve the post
208
-		$entity = get_post( $post_id );
208
+		$entity = get_post($post_id);
209 209
 
210 210
 		global $wpdb;
211 211
 		// Retrieve Wordlift relation instances table name
@@ -218,9 +218,9 @@  discard block
 block discarded – undo
218 218
 		);
219 219
 
220 220
 		// Perform the query
221
-		$relation_instances = (int) $wpdb->get_var( $stmt );
221
+		$relation_instances = (int) $wpdb->get_var($stmt);
222 222
 		// If there is at least one relation instance for the current entity, then it's used
223
-		if ( 0 < $relation_instances ) {
223
+		if (0 < $relation_instances) {
224 224
 			return true;
225 225
 		}
226 226
 
@@ -228,13 +228,13 @@  discard block
 block discarded – undo
228 228
 		$stmt = $wpdb->prepare(
229 229
 			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
230 230
 			$entity->ID,
231
-			wl_get_entity_uri( $entity->ID )
231
+			wl_get_entity_uri($entity->ID)
232 232
 		);
233 233
 		// Perform the query
234
-		$meta_instances = (int) $wpdb->get_var( $stmt );
234
+		$meta_instances = (int) $wpdb->get_var($stmt);
235 235
 
236 236
 		// If there is at least one meta that refers the current entity uri, then current entity is used
237
-		if ( 0 < $meta_instances ) {
237
+		if (0 < $meta_instances) {
238 238
 			return true;
239 239
 		}
240 240
 
@@ -254,9 +254,9 @@  discard block
 block discarded – undo
254 254
 	 * @since      3.2.0
255 255
 	 *
256 256
 	 */
257
-	public function get_entity_post_by_uri( $uri ) {
257
+	public function get_entity_post_by_uri($uri) {
258 258
 
259
-		return $this->entity_uri_service->get_entity( $uri );
259
+		return $this->entity_uri_service->get_entity($uri);
260 260
 	}
261 261
 
262 262
 	/**
@@ -271,10 +271,10 @@  discard block
 block discarded – undo
271 271
 	 * @param WP_Post $post Post object.
272 272
 	 * @param bool $update Whether this is an existing post being updated or not.
273 273
 	 */
274
-	public function save_post( $post_id, $post, $update ) {
274
+	public function save_post($post_id, $post, $update) {
275 275
 
276 276
 		// Avoid doing anything if post is autosave or a revision.
277
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
277
+		if (wp_is_post_autosave($post) || wp_is_post_revision($post)) {
278 278
 			return;
279 279
 		}
280 280
 
@@ -284,15 +284,15 @@  discard block
 block discarded – undo
284 284
 		// the $post_id in the save_post call matches the post id set in the request.
285 285
 		//
286 286
 		// If this is not the current post being saved or if it's not an entity, return.
287
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
287
+		if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) {
288 288
 			return;
289 289
 		}
290 290
 
291 291
 		// Get the alt labels from the request (or empty array).
292
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
292
+		$alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array();
293 293
 
294 294
 		// Set the alternative labels.
295
-		$this->set_alternative_labels( $post_id, $alt_labels );
295
+		$this->set_alternative_labels($post_id, $alt_labels);
296 296
 
297 297
 	}
298 298
 
@@ -305,22 +305,22 @@  discard block
 block discarded – undo
305 305
 	 * @since 3.2.0
306 306
 	 *
307 307
 	 */
308
-	public function set_alternative_labels( $post_id, $alt_labels ) {
308
+	public function set_alternative_labels($post_id, $alt_labels) {
309 309
 
310 310
 		// Force $alt_labels to be an array
311
-		if ( ! is_array( $alt_labels ) ) {
312
-			$alt_labels = array( $alt_labels );
311
+		if ( ! is_array($alt_labels)) {
312
+			$alt_labels = array($alt_labels);
313 313
 		}
314 314
 
315
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
315
+		$this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]");
316 316
 
317 317
 		// Delete all the existing alternate labels.
318
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
318
+		delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
319 319
 
320 320
 		// Set the alternative labels.
321
-		foreach ( $alt_labels as $alt_label ) {
322
-			if ( ! empty( $alt_label ) ) {
323
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
321
+		foreach ($alt_labels as $alt_label) {
322
+			if ( ! empty($alt_label)) {
323
+				add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label);
324 324
 			}
325 325
 		}
326 326
 
@@ -335,9 +335,9 @@  discard block
 block discarded – undo
335 335
 	 * @since 3.2.0
336 336
 	 *
337 337
 	 */
338
-	public function get_alternative_labels( $post_id ) {
338
+	public function get_alternative_labels($post_id) {
339 339
 
340
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
340
+		return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
341 341
 	}
342 342
 
343 343
 	/**
@@ -349,9 +349,9 @@  discard block
 block discarded – undo
349 349
 	 * @since 3.12.0
350 350
 	 *
351 351
 	 */
352
-	public function get_labels( $post_id ) {
352
+	public function get_labels($post_id) {
353 353
 
354
-		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
354
+		return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id));
355 355
 	}
356 356
 
357 357
 	/**
@@ -362,25 +362,25 @@  discard block
 block discarded – undo
362 362
 	 * @since 3.2.0
363 363
 	 *
364 364
 	 */
365
-	public function edit_form_before_permalink( $post ) {
365
+	public function edit_form_before_permalink($post) {
366 366
 
367 367
 		// If it's not an entity, return.
368
-		if ( ! $this->is_entity( $post->ID ) ) {
368
+		if ( ! $this->is_entity($post->ID)) {
369 369
 			return;
370 370
 		}
371 371
 
372 372
 		// Print the input template.
373
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
373
+		$this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input());
374 374
 
375 375
 		// Print all the currently set alternative labels.
376
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
376
+		foreach ($this->get_alternative_labels($post->ID) as $alt_label) {
377 377
 
378
-			echo $this->get_alternative_label_input( $alt_label );
378
+			echo $this->get_alternative_label_input($alt_label);
379 379
 
380 380
 		};
381 381
 
382 382
 		// Print the button.
383
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
383
+		$this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift'));
384 384
 
385 385
 	}
386 386
 
@@ -393,36 +393,36 @@  discard block
 block discarded – undo
393 393
 	 * @since 3.6.0
394 394
 	 *
395 395
 	 */
396
-	public function get_uri( $post_id ) {
396
+	public function get_uri($post_id) {
397 397
 
398 398
 		// If a null is given, nothing to do
399
-		if ( null == $post_id ) {
399
+		if (null == $post_id) {
400 400
 			return null;
401 401
 		}
402 402
 
403 403
 		$dataset_uri = wl_configuration_get_redlink_dataset_uri();
404 404
 
405 405
 		// If the dataset uri is not properly configured, null is returned
406
-		if ( empty( $dataset_uri ) ) {
406
+		if (empty($dataset_uri)) {
407 407
 			return null;
408 408
 		}
409 409
 
410
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
410
+		$uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true);
411 411
 
412 412
 		/*
413 413
 		 * Consider the URI invalid if it doesn't start with the dataset URI.
414 414
 		 *
415 415
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/996
416 416
 		 */
417
-		if ( 0 !== strpos( $uri, $dataset_uri ) ) {
417
+		if (0 !== strpos($uri, $dataset_uri)) {
418 418
 			$uri = null;
419 419
 		}
420 420
 
421 421
 		// Set the URI if it isn't set yet.
422
-		$post_status = get_post_status( $post_id );
423
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
424
-			$uri = wl_build_entity_uri( $post_id );
425
-			wl_set_entity_uri( $post_id, $uri );
422
+		$post_status = get_post_status($post_id);
423
+		if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) {
424
+			$uri = wl_build_entity_uri($post_id);
425
+			wl_set_entity_uri($post_id, $uri);
426 426
 		}
427 427
 
428 428
 		return $uri;
@@ -439,9 +439,9 @@  discard block
 block discarded – undo
439 439
 	 *
440 440
 	 */
441 441
 		private
442
-		function get_alternative_label_input( $value = '' ) {
442
+		function get_alternative_label_input($value = '') {
443 443
 
444
-			return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
444
+			return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift'));
445 445
 		}
446 446
 
447 447
 		/**
@@ -454,13 +454,13 @@  discard block
 block discarded – undo
454 454
 		public
455 455
 		function count() {
456 456
 
457
-			$posts = get_posts( $this->add_criterias( array(
457
+			$posts = get_posts($this->add_criterias(array(
458 458
 				'fields'      => 'ids',
459 459
 				'post_status' => 'any',
460
-				'numberposts' => - 1,
461
-			) ) );
460
+				'numberposts' => -1,
461
+			)));
462 462
 
463
-			return count( $posts );
463
+			return count($posts);
464 464
 		}
465 465
 
466 466
 		/**
@@ -474,7 +474,7 @@  discard block
 block discarded – undo
474 474
 		 *
475 475
 		 */
476 476
 		public
477
-		static function add_criterias( $args ) {
477
+		static function add_criterias($args) {
478 478
 
479 479
 			// Build an optimal tax-query.
480 480
 			$tax_query = array(
@@ -519,28 +519,28 @@  discard block
 block discarded – undo
519 519
 		 *
520 520
 		 */
521 521
 		public
522
-		function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
522
+		function create($name, $type_uri, $logo = null, $status = 'publish') {
523 523
 
524 524
 			// Create an entity for the publisher.
525
-			$post_id = @wp_insert_post( array(
525
+			$post_id = @wp_insert_post(array(
526 526
 				'post_type'    => self::TYPE_NAME,
527 527
 				'post_title'   => $name,
528 528
 				'post_status'  => $status,
529 529
 				'post_content' => '',
530
-			) );
530
+			));
531 531
 
532 532
 			// Return the error if any.
533
-			if ( is_wp_error( $post_id ) ) {
533
+			if (is_wp_error($post_id)) {
534 534
 				return $post_id;
535 535
 			}
536 536
 
537 537
 			// Set the entity logo.
538
-			if ( $logo && is_numeric( $logo ) ) {
539
-				set_post_thumbnail( $post_id, $logo );
538
+			if ($logo && is_numeric($logo)) {
539
+				set_post_thumbnail($post_id, $logo);
540 540
 			}
541 541
 
542 542
 			// Set the entity type.
543
-			Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
543
+			Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri);
544 544
 
545 545
 			return $post_id;
546 546
 		}
@@ -557,9 +557,9 @@  discard block
 block discarded – undo
557 557
 		 *
558 558
 		 */
559 559
 		public
560
-		function get_related_entities( $id, $post_status = 'publish' ) {
560
+		function get_related_entities($id, $post_status = 'publish') {
561 561
 
562
-			return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
562
+			return $this->relation_service->get_objects($id, 'ids', null, $post_status);
563 563
 		}
564 564
 
565 565
 		/**
@@ -572,16 +572,16 @@  discard block
 block discarded – undo
572 572
 		 *
573 573
 		 */
574 574
 		public
575
-		function get( $params = array() ) {
575
+		function get($params = array()) {
576 576
 
577 577
 			// Set the defaults.
578
-			$defaults = array( 'post_type' => 'entity' );
578
+			$defaults = array('post_type' => 'entity');
579 579
 
580 580
 			// Merge the defaults with the provided parameters.
581
-			$args = wp_parse_args( $params, $defaults );
581
+			$args = wp_parse_args($params, $defaults);
582 582
 
583 583
 			// Call the `get_posts` function.
584
-			return get_posts( $args );
584
+			return get_posts($args);
585 585
 		}
586 586
 
587 587
 		/**
@@ -597,9 +597,9 @@  discard block
 block discarded – undo
597 597
 		static function valid_entity_post_types() {
598 598
 
599 599
 			// Ignore builtins in the call to avoid getting attachments.
600
-			$post_types = array( 'post', 'page', self::TYPE_NAME );
600
+			$post_types = array('post', 'page', self::TYPE_NAME);
601 601
 
602
-			return apply_filters( 'wl_valid_entity_post_types', $post_types );
602
+			return apply_filters('wl_valid_entity_post_types', $post_types);
603 603
 		}
604 604
 
605 605
 	}
Please login to merge, or discard this patch.
src/admin/wordlift_admin_meta_box_entities.php 2 patches
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -10,15 +10,15 @@  discard block
 block discarded – undo
10 10
  */
11 11
 function wl_register_metaboxes() {
12 12
 
13
-	// Load metabox classes.
14
-	require_once( 'WL_Metabox/class-wl-metabox.php' );
13
+    // Load metabox classes.
14
+    require_once( 'WL_Metabox/class-wl-metabox.php' );
15 15
 
16
-	$wl_metabox = new WL_Metabox();     // Everything is done inside here with the correct timing.
16
+    $wl_metabox = new WL_Metabox();     // Everything is done inside here with the correct timing.
17 17
 }
18 18
 
19 19
 if ( is_admin() ) {
20
-	add_action( 'load-post.php', 'wl_register_metaboxes' );
21
-	add_action( 'load-post-new.php', 'wl_register_metaboxes' );
20
+    add_action( 'load-post.php', 'wl_register_metaboxes' );
21
+    add_action( 'load-post-new.php', 'wl_register_metaboxes' );
22 22
 }
23 23
 
24 24
 
@@ -29,31 +29,31 @@  discard block
 block discarded – undo
29 29
  */
30 30
 function wl_admin_add_entities_meta_box( $post_type ) {
31 31
 
32
-	/*
32
+    /*
33 33
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
34 34
 	 *
35 35
 	 * @since 3.20.3
36 36
 	 *
37 37
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
38 38
 	 */
39
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
40
-		return;
41
-	}
42
-
43
-	// Bail out if the post type doesn't support a TinyMCE editor.
44
-	if ( ! wl_post_type_supports_editor( $post_type ) ) {
45
-		return;
46
-	}
47
-
48
-	if ( ! Wordlift_Admin::is_gutenberg() ) {
49
-		// Add main meta box for related entities and 4W only if not Gutenberg
50
-		add_meta_box(
51
-			'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
52
-		);
53
-	} else {
54
-		// Call wl_entities_box_content for the other things that it does.
55
-		wl_entities_box_content( get_post(), false );
56
-	}
39
+    if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
40
+        return;
41
+    }
42
+
43
+    // Bail out if the post type doesn't support a TinyMCE editor.
44
+    if ( ! wl_post_type_supports_editor( $post_type ) ) {
45
+        return;
46
+    }
47
+
48
+    if ( ! Wordlift_Admin::is_gutenberg() ) {
49
+        // Add main meta box for related entities and 4W only if not Gutenberg
50
+        add_meta_box(
51
+            'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
52
+        );
53
+    } else {
54
+        // Call wl_entities_box_content for the other things that it does.
55
+        wl_entities_box_content( get_post(), false );
56
+    }
57 57
 }
58 58
 
59 59
 add_action( 'add_meta_boxes', 'wl_admin_add_entities_meta_box' );
@@ -69,19 +69,19 @@  discard block
 block discarded – undo
69 69
  */
70 70
 function wl_post_type_supports_editor( $post_type ) {
71 71
 
72
-	$default = post_type_supports( $post_type, 'editor' );
73
-
74
-	/**
75
-	 * Allow 3rd parties to force the classification to load.
76
-	 *
77
-	 * @param bool $default The preset value as gathered by the `post_type_supports` call.
78
-	 *
79
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
80
-	 *
81
-	 * @since 3.19.4
82
-	 *
83
-	 */
84
-	return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
72
+    $default = post_type_supports( $post_type, 'editor' );
73
+
74
+    /**
75
+     * Allow 3rd parties to force the classification to load.
76
+     *
77
+     * @param bool $default The preset value as gathered by the `post_type_supports` call.
78
+     *
79
+     * @see https://github.com/insideout10/wordlift-plugin/issues/847.
80
+     *
81
+     * @since 3.19.4
82
+     *
83
+     */
84
+    return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
85 85
 }
86 86
 
87 87
 /**
@@ -91,106 +91,106 @@  discard block
 block discarded – undo
91 91
  */
92 92
 function wl_entities_box_content( $post, $wrapper = true ) {
93 93
 
94
-	// Angularjs edit-post widget wrapper.
95
-	if ( $wrapper ) {
96
-		echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
97
-	}
94
+    // Angularjs edit-post widget wrapper.
95
+    if ( $wrapper ) {
96
+        echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
97
+    }
98 98
 
99
-	// Angularjs edit-post widget classification boxes configuration.
100
-	$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
99
+    // Angularjs edit-post widget classification boxes configuration.
100
+    $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
101 101
 
102
-	// Array to store all related entities ids.
103
-	$all_referenced_entities_ids = array();
102
+    // Array to store all related entities ids.
103
+    $all_referenced_entities_ids = array();
104 104
 
105
-	// Add selected entities to classification_boxes.
106
-	foreach ( $classification_boxes as $i => $box ) {
107
-		// Build the proper relation name.
108
-		$relation_name = $box['id'];
105
+    // Add selected entities to classification_boxes.
106
+    foreach ( $classification_boxes as $i => $box ) {
107
+        // Build the proper relation name.
108
+        $relation_name = $box['id'];
109 109
 
110
-		// Get the entity referenced from the post content.
111
-		/*
110
+        // Get the entity referenced from the post content.
111
+        /*
112 112
 		 * Allow 3rd parties to provide another post content.
113 113
 		 *
114 114
 		 * @since 3.20.0
115 115
 		 */
116
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
117
-		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
118
-
119
-		// Enhance current box selected entities.
120
-		$classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
121
-
122
-		// Maps the URIs to entity posts.
123
-		$entity_service = Wordlift_Entity_Service::get_instance();
124
-
125
-		// Replace all entity URI's with post ID's if found or null if there is no related post.
126
-		$entity_ids = array_map( function ( $item ) use ( $entity_service ) {
127
-			// Return entity post by the entity URI or null.
128
-			$post = $entity_service->get_entity_post_by_uri( $item );
129
-
130
-			// Check that the post object is not null.
131
-			if ( ! empty( $post ) ) {
132
-				return $post->ID;
133
-			}
134
-		}, $entity_uris );
135
-		// Store the entity ids for all the 4W.
136
-		$all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
137
-
138
-	}
139
-
140
-	// Json encoding for classification boxes structure.
141
-	$classification_boxes = wp_json_encode( $classification_boxes );
142
-
143
-	// Ensure there are no repetitions of the referenced entities.
144
-	$all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
145
-
146
-	// Remove all null, false and empty strings.
147
-	// NULL is being returned in some cases, when there is not related post, so we need to remove it.
148
-	$all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
149
-
150
-	// Build the entity storage object.
151
-	$referenced_entities_obj = array();
152
-	foreach ( $all_referenced_entities_ids as $referenced_entity ) {
153
-		$entity = wl_serialize_entity( $referenced_entity );
154
-		// Set a default confidence of `PHP_INT_MAX` for already annotated entities.
155
-		$referenced_entities_obj[ $entity['id'] ] = $entity
156
-		                                            + array( 'confidence' => PHP_INT_MAX );
157
-	}
158
-
159
-	$referenced_entities_obj = empty( $referenced_entities_obj ) ?
160
-		'{}' : wp_json_encode( $referenced_entities_obj );
161
-
162
-	$published_place_id  = get_post_meta(
163
-		$post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
164
-	);
165
-	$published_place_obj = ( $published_place_id ) ?
166
-		wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
167
-		'undefined';
168
-
169
-	$topic_id  = get_post_meta(
170
-		$post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
171
-	);
172
-	$topic_obj = ( $topic_id ) ?
173
-		wp_json_encode( wl_serialize_entity( $topic_id ) ) :
174
-		'undefined';
175
-
176
-	$configuration_service = Wordlift_Configuration_Service::get_instance();
177
-
178
-	$default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
179
-	$default_path           = WL_DEFAULT_PATH;
180
-	$dataset_uri            = $configuration_service->get_dataset_uri();
181
-	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
182
-
183
-	// Retrieve the current post author.
184
-	$post_author = get_userdata( $post->post_author )->display_name;
185
-	// Retrive the published date.
186
-	$published_date = get_the_time( 'Y-m-d', $post->ID );
187
-	// Current language.
188
-	$current_language            = $configuration_service->get_language_code();
189
-	$wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
190
-	$timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
191
-	$addslashes_post_author      = addslashes( $post_author );
192
-
193
-	$js_code = <<<JS
116
+        $post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
117
+        $entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
118
+
119
+        // Enhance current box selected entities.
120
+        $classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
121
+
122
+        // Maps the URIs to entity posts.
123
+        $entity_service = Wordlift_Entity_Service::get_instance();
124
+
125
+        // Replace all entity URI's with post ID's if found or null if there is no related post.
126
+        $entity_ids = array_map( function ( $item ) use ( $entity_service ) {
127
+            // Return entity post by the entity URI or null.
128
+            $post = $entity_service->get_entity_post_by_uri( $item );
129
+
130
+            // Check that the post object is not null.
131
+            if ( ! empty( $post ) ) {
132
+                return $post->ID;
133
+            }
134
+        }, $entity_uris );
135
+        // Store the entity ids for all the 4W.
136
+        $all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
137
+
138
+    }
139
+
140
+    // Json encoding for classification boxes structure.
141
+    $classification_boxes = wp_json_encode( $classification_boxes );
142
+
143
+    // Ensure there are no repetitions of the referenced entities.
144
+    $all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
145
+
146
+    // Remove all null, false and empty strings.
147
+    // NULL is being returned in some cases, when there is not related post, so we need to remove it.
148
+    $all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
149
+
150
+    // Build the entity storage object.
151
+    $referenced_entities_obj = array();
152
+    foreach ( $all_referenced_entities_ids as $referenced_entity ) {
153
+        $entity = wl_serialize_entity( $referenced_entity );
154
+        // Set a default confidence of `PHP_INT_MAX` for already annotated entities.
155
+        $referenced_entities_obj[ $entity['id'] ] = $entity
156
+                                                    + array( 'confidence' => PHP_INT_MAX );
157
+    }
158
+
159
+    $referenced_entities_obj = empty( $referenced_entities_obj ) ?
160
+        '{}' : wp_json_encode( $referenced_entities_obj );
161
+
162
+    $published_place_id  = get_post_meta(
163
+        $post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
164
+    );
165
+    $published_place_obj = ( $published_place_id ) ?
166
+        wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
167
+        'undefined';
168
+
169
+    $topic_id  = get_post_meta(
170
+        $post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
171
+    );
172
+    $topic_obj = ( $topic_id ) ?
173
+        wp_json_encode( wl_serialize_entity( $topic_id ) ) :
174
+        'undefined';
175
+
176
+    $configuration_service = Wordlift_Configuration_Service::get_instance();
177
+
178
+    $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
179
+    $default_path           = WL_DEFAULT_PATH;
180
+    $dataset_uri            = $configuration_service->get_dataset_uri();
181
+    $current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
182
+
183
+    // Retrieve the current post author.
184
+    $post_author = get_userdata( $post->post_author )->display_name;
185
+    // Retrive the published date.
186
+    $published_date = get_the_time( 'Y-m-d', $post->ID );
187
+    // Current language.
188
+    $current_language            = $configuration_service->get_language_code();
189
+    $wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
190
+    $timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
191
+    $addslashes_post_author      = addslashes( $post_author );
192
+
193
+    $js_code = <<<JS
194 194
 		if ('undefined' == typeof window.wordlift) {
195 195
 			window.wordlift = {};
196 196
 			window.wordlift.entities = {};  		
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
 		window.wordlift.timelinejsDefaultOptions = $timelinejs_default_options;
213 213
 JS;
214 214
 	
215
-	echo '<script type="text/javascript">' . PHP_EOL . $js_code . PHP_EOL . '</script>';
215
+    echo '<script type="text/javascript">' . PHP_EOL . $js_code . PHP_EOL . '</script>';
216 216
 
217 217
 }
218 218
 
Please login to merge, or discard this patch.
Spacing   +46 added lines, -48 removed lines patch added patch discarded remove patch
@@ -11,14 +11,14 @@  discard block
 block discarded – undo
11 11
 function wl_register_metaboxes() {
12 12
 
13 13
 	// Load metabox classes.
14
-	require_once( 'WL_Metabox/class-wl-metabox.php' );
14
+	require_once('WL_Metabox/class-wl-metabox.php');
15 15
 
16
-	$wl_metabox = new WL_Metabox();     // Everything is done inside here with the correct timing.
16
+	$wl_metabox = new WL_Metabox(); // Everything is done inside here with the correct timing.
17 17
 }
18 18
 
19
-if ( is_admin() ) {
20
-	add_action( 'load-post.php', 'wl_register_metaboxes' );
21
-	add_action( 'load-post-new.php', 'wl_register_metaboxes' );
19
+if (is_admin()) {
20
+	add_action('load-post.php', 'wl_register_metaboxes');
21
+	add_action('load-post-new.php', 'wl_register_metaboxes');
22 22
 }
23 23
 
24 24
 
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
  *
28 28
  * @param string $post_type The type of the current open post.
29 29
  */
30
-function wl_admin_add_entities_meta_box( $post_type ) {
30
+function wl_admin_add_entities_meta_box($post_type) {
31 31
 
32 32
 	/*
33 33
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
@@ -36,27 +36,27 @@  discard block
 block discarded – undo
36 36
 	 *
37 37
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
38 38
 	 */
39
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
39
+	if ( ! apply_filters('wl_can_see_classification_box', true)) {
40 40
 		return;
41 41
 	}
42 42
 
43 43
 	// Bail out if the post type doesn't support a TinyMCE editor.
44
-	if ( ! wl_post_type_supports_editor( $post_type ) ) {
44
+	if ( ! wl_post_type_supports_editor($post_type)) {
45 45
 		return;
46 46
 	}
47 47
 
48
-	if ( ! Wordlift_Admin::is_gutenberg() ) {
48
+	if ( ! Wordlift_Admin::is_gutenberg()) {
49 49
 		// Add main meta box for related entities and 4W only if not Gutenberg
50 50
 		add_meta_box(
51
-			'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
51
+			'wordlift_entities_box', __('WordLift', 'wordlift'), 'wl_entities_box_content', $post_type, 'side', 'high'
52 52
 		);
53 53
 	} else {
54 54
 		// Call wl_entities_box_content for the other things that it does.
55
-		wl_entities_box_content( get_post(), false );
55
+		wl_entities_box_content(get_post(), false);
56 56
 	}
57 57
 }
58 58
 
59
-add_action( 'add_meta_boxes', 'wl_admin_add_entities_meta_box' );
59
+add_action('add_meta_boxes', 'wl_admin_add_entities_meta_box');
60 60
 
61 61
 /**
62 62
  * Whether the post type supports the editor UI.
@@ -67,9 +67,9 @@  discard block
 block discarded – undo
67 67
  *
68 68
  * @return bool True if the editor UI is supported otherwise false.
69 69
  */
70
-function wl_post_type_supports_editor( $post_type ) {
70
+function wl_post_type_supports_editor($post_type) {
71 71
 
72
-	$default = post_type_supports( $post_type, 'editor' );
72
+	$default = post_type_supports($post_type, 'editor');
73 73
 
74 74
 	/**
75 75
 	 * Allow 3rd parties to force the classification to load.
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
 	 * @since 3.19.4
82 82
 	 *
83 83
 	 */
84
-	return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
84
+	return apply_filters('wl_post_type_supports_editor', $default, $post_type);
85 85
 }
86 86
 
87 87
 /**
@@ -89,21 +89,21 @@  discard block
 block discarded – undo
89 89
  *
90 90
  * @param WP_Post $post The current post.
91 91
  */
92
-function wl_entities_box_content( $post, $wrapper = true ) {
92
+function wl_entities_box_content($post, $wrapper = true) {
93 93
 
94 94
 	// Angularjs edit-post widget wrapper.
95
-	if ( $wrapper ) {
95
+	if ($wrapper) {
96 96
 		echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
97 97
 	}
98 98
 
99 99
 	// Angularjs edit-post widget classification boxes configuration.
100
-	$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
100
+	$classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
101 101
 
102 102
 	// Array to store all related entities ids.
103 103
 	$all_referenced_entities_ids = array();
104 104
 
105 105
 	// Add selected entities to classification_boxes.
106
-	foreach ( $classification_boxes as $i => $box ) {
106
+	foreach ($classification_boxes as $i => $box) {
107 107
 		// Build the proper relation name.
108 108
 		$relation_name = $box['id'];
109 109
 
@@ -113,82 +113,80 @@  discard block
 block discarded – undo
113 113
 		 *
114 114
 		 * @since 3.20.0
115 115
 		 */
116
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
117
-		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
116
+		$post_content = apply_filters('wl_post_content', $post->post_content, $post);
117
+		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris($post_content);
118 118
 
119 119
 		// Enhance current box selected entities.
120
-		$classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
120
+		$classification_boxes[$i]['selectedEntities'] = $entity_uris;
121 121
 
122 122
 		// Maps the URIs to entity posts.
123 123
 		$entity_service = Wordlift_Entity_Service::get_instance();
124 124
 
125 125
 		// Replace all entity URI's with post ID's if found or null if there is no related post.
126
-		$entity_ids = array_map( function ( $item ) use ( $entity_service ) {
126
+		$entity_ids = array_map(function($item) use ($entity_service) {
127 127
 			// Return entity post by the entity URI or null.
128
-			$post = $entity_service->get_entity_post_by_uri( $item );
128
+			$post = $entity_service->get_entity_post_by_uri($item);
129 129
 
130 130
 			// Check that the post object is not null.
131
-			if ( ! empty( $post ) ) {
131
+			if ( ! empty($post)) {
132 132
 				return $post->ID;
133 133
 			}
134
-		}, $entity_uris );
134
+		}, $entity_uris);
135 135
 		// Store the entity ids for all the 4W.
136
-		$all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
136
+		$all_referenced_entities_ids = array_merge($all_referenced_entities_ids, $entity_ids);
137 137
 
138 138
 	}
139 139
 
140 140
 	// Json encoding for classification boxes structure.
141
-	$classification_boxes = wp_json_encode( $classification_boxes );
141
+	$classification_boxes = wp_json_encode($classification_boxes);
142 142
 
143 143
 	// Ensure there are no repetitions of the referenced entities.
144
-	$all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
144
+	$all_referenced_entities_ids = array_unique($all_referenced_entities_ids);
145 145
 
146 146
 	// Remove all null, false and empty strings.
147 147
 	// NULL is being returned in some cases, when there is not related post, so we need to remove it.
148
-	$all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
148
+	$all_referenced_entities_ids = array_filter($all_referenced_entities_ids);
149 149
 
150 150
 	// Build the entity storage object.
151 151
 	$referenced_entities_obj = array();
152
-	foreach ( $all_referenced_entities_ids as $referenced_entity ) {
153
-		$entity = wl_serialize_entity( $referenced_entity );
152
+	foreach ($all_referenced_entities_ids as $referenced_entity) {
153
+		$entity = wl_serialize_entity($referenced_entity);
154 154
 		// Set a default confidence of `PHP_INT_MAX` for already annotated entities.
155
-		$referenced_entities_obj[ $entity['id'] ] = $entity
156
-		                                            + array( 'confidence' => PHP_INT_MAX );
155
+		$referenced_entities_obj[$entity['id']] = $entity
156
+		                                            + array('confidence' => PHP_INT_MAX);
157 157
 	}
158 158
 
159
-	$referenced_entities_obj = empty( $referenced_entities_obj ) ?
160
-		'{}' : wp_json_encode( $referenced_entities_obj );
159
+	$referenced_entities_obj = empty($referenced_entities_obj) ?
160
+		'{}' : wp_json_encode($referenced_entities_obj);
161 161
 
162 162
 	$published_place_id  = get_post_meta(
163 163
 		$post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
164 164
 	);
165
-	$published_place_obj = ( $published_place_id ) ?
166
-		wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
167
-		'undefined';
165
+	$published_place_obj = ($published_place_id) ?
166
+		wp_json_encode(wl_serialize_entity($published_place_id)) : 'undefined';
168 167
 
169 168
 	$topic_id  = get_post_meta(
170 169
 		$post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
171 170
 	);
172
-	$topic_obj = ( $topic_id ) ?
173
-		wp_json_encode( wl_serialize_entity( $topic_id ) ) :
174
-		'undefined';
171
+	$topic_obj = ($topic_id) ?
172
+		wp_json_encode(wl_serialize_entity($topic_id)) : 'undefined';
175 173
 
176 174
 	$configuration_service = Wordlift_Configuration_Service::get_instance();
177 175
 
178 176
 	$default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
179 177
 	$default_path           = WL_DEFAULT_PATH;
180 178
 	$dataset_uri            = $configuration_service->get_dataset_uri();
181
-	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
179
+	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri($post->ID);
182 180
 
183 181
 	// Retrieve the current post author.
184
-	$post_author = get_userdata( $post->post_author )->display_name;
182
+	$post_author = get_userdata($post->post_author)->display_name;
185 183
 	// Retrive the published date.
186
-	$published_date = get_the_time( 'Y-m-d', $post->ID );
184
+	$published_date = get_the_time('Y-m-d', $post->ID);
187 185
 	// Current language.
188 186
 	$current_language            = $configuration_service->get_language_code();
189 187
 	$wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
190
-	$timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
191
-	$addslashes_post_author      = addslashes( $post_author );
188
+	$timelinejs_default_options  = json_encode($wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT);
189
+	$addslashes_post_author      = addslashes($post_author);
192 190
 
193 191
 	$js_code = <<<JS
194 192
 		if ('undefined' == typeof window.wordlift) {
@@ -212,7 +210,7 @@  discard block
 block discarded – undo
212 210
 		window.wordlift.timelinejsDefaultOptions = $timelinejs_default_options;
213 211
 JS;
214 212
 	
215
-	echo '<script type="text/javascript">' . PHP_EOL . $js_code . PHP_EOL . '</script>';
213
+	echo '<script type="text/javascript">'.PHP_EOL.$js_code.PHP_EOL.'</script>';
216 214
 
217 215
 }
218 216
 
Please login to merge, or discard this patch.