Completed
Pull Request — develop (#1494)
by Naveen
56s
created
src/includes/class-wordlift-entity-service.php 2 patches
Indentation   +618 added lines, -618 removed lines patch added patch discarded remove patch
@@ -18,542 +18,542 @@  discard block
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Entity_Service {
20 20
 
21
-	/**
22
-	 * The Log service.
23
-	 *
24
-	 * @since  3.2.0
25
-	 * @access private
26
-	 * @var \Wordlift_Log_Service $log The Log service.
27
-	 */
28
-	private $log;
29
-
30
-	/**
31
-	 * The UI service.
32
-	 *
33
-	 * @since  3.2.0
34
-	 * @access private
35
-	 * @var \Wordlift_UI_Service $ui_service The UI service.
36
-	 */
37
-	private $ui_service;
38
-
39
-	/**
40
-	 * The {@link Wordlift_Relation_Service} instance.
41
-	 *
42
-	 * @since  3.15.0
43
-	 * @access private
44
-	 * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
45
-	 */
46
-	private $relation_service;
47
-
48
-	/**
49
-	 * The {@link Wordlift_Entity_Uri_Service} instance.
50
-	 *
51
-	 * @since  3.16.3
52
-	 * @access private
53
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
54
-	 */
55
-	private $entity_uri_service;
56
-
57
-	/**
58
-	 * The entity post type name.
59
-	 *
60
-	 * @since 3.1.0
61
-	 */
62
-	const TYPE_NAME = 'entity';
63
-
64
-	/**
65
-	 * The alternative label meta key.
66
-	 *
67
-	 * @since 3.2.0
68
-	 */
69
-	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
70
-
71
-	/**
72
-	 * The alternative label input template.
73
-	 *
74
-	 * @since 3.2.0
75
-	 */
76
-	// TODO: this should be moved to a class that deals with HTML code.
77
-	const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
21
+    /**
22
+     * The Log service.
23
+     *
24
+     * @since  3.2.0
25
+     * @access private
26
+     * @var \Wordlift_Log_Service $log The Log service.
27
+     */
28
+    private $log;
29
+
30
+    /**
31
+     * The UI service.
32
+     *
33
+     * @since  3.2.0
34
+     * @access private
35
+     * @var \Wordlift_UI_Service $ui_service The UI service.
36
+     */
37
+    private $ui_service;
38
+
39
+    /**
40
+     * The {@link Wordlift_Relation_Service} instance.
41
+     *
42
+     * @since  3.15.0
43
+     * @access private
44
+     * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
45
+     */
46
+    private $relation_service;
47
+
48
+    /**
49
+     * The {@link Wordlift_Entity_Uri_Service} instance.
50
+     *
51
+     * @since  3.16.3
52
+     * @access private
53
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
54
+     */
55
+    private $entity_uri_service;
56
+
57
+    /**
58
+     * The entity post type name.
59
+     *
60
+     * @since 3.1.0
61
+     */
62
+    const TYPE_NAME = 'entity';
63
+
64
+    /**
65
+     * The alternative label meta key.
66
+     *
67
+     * @since 3.2.0
68
+     */
69
+    const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
70
+
71
+    /**
72
+     * The alternative label input template.
73
+     *
74
+     * @since 3.2.0
75
+     */
76
+    // TODO: this should be moved to a class that deals with HTML code.
77
+    const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
78 78
                 <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label>
79 79
                 <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text">
80 80
                 <button class="button wl-delete-button">%s</button>
81 81
                 </div>';
82 82
 
83
-	/**
84
-	 * A singleton instance of the Entity service.
85
-	 *
86
-	 * @since  3.2.0
87
-	 * @access private
88
-	 * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
89
-	 */
90
-	private static $instance;
91
-
92
-	/**
93
-	 * Create a Wordlift_Entity_Service instance.
94
-	 *
95
-	 * @param \Wordlift_UI_Service $ui_service The UI service.
96
-	 * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
97
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
98
-	 *
99
-	 * @since 3.2.0
100
-	 *
101
-	 */
102
-	public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
103
-
104
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
105
-
106
-		$this->ui_service         = $ui_service;
107
-		$this->relation_service   = $relation_service;
108
-		$this->entity_uri_service = $entity_uri_service;
109
-
110
-		// Set the singleton instance.
111
-		self::$instance = $this;
112
-	}
113
-
114
-	/**
115
-	 * Get the singleton instance of the Entity service.
116
-	 *
117
-	 * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
118
-	 * @since 3.2.0
119
-	 */
120
-	public static function get_instance() {
121
-
122
-		return self::$instance;
123
-	}
124
-
125
-	/**
126
-	 * Determines whether a post is an entity or not. Entity is in this context
127
-	 * something which is not an article.
128
-	 *
129
-	 * @param int $post_id A post id.
130
-	 *
131
-	 * @return bool Return true if the post is an entity otherwise false.
132
-	 * @since 3.1.0
133
-	 *
134
-	 */
135
-	public function is_entity( $post_id ) {
136
-
137
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
138
-
139
-		if ( is_wp_error( $terms ) ) {
140
-			$this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
141
-
142
-			return false;
143
-		}
144
-
145
-		if ( empty( $terms ) ) {
146
-			return false;
147
-		}
148
-
149
-		/*
83
+    /**
84
+     * A singleton instance of the Entity service.
85
+     *
86
+     * @since  3.2.0
87
+     * @access private
88
+     * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
89
+     */
90
+    private static $instance;
91
+
92
+    /**
93
+     * Create a Wordlift_Entity_Service instance.
94
+     *
95
+     * @param \Wordlift_UI_Service $ui_service The UI service.
96
+     * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
97
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
98
+     *
99
+     * @since 3.2.0
100
+     *
101
+     */
102
+    public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
103
+
104
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
105
+
106
+        $this->ui_service         = $ui_service;
107
+        $this->relation_service   = $relation_service;
108
+        $this->entity_uri_service = $entity_uri_service;
109
+
110
+        // Set the singleton instance.
111
+        self::$instance = $this;
112
+    }
113
+
114
+    /**
115
+     * Get the singleton instance of the Entity service.
116
+     *
117
+     * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
118
+     * @since 3.2.0
119
+     */
120
+    public static function get_instance() {
121
+
122
+        return self::$instance;
123
+    }
124
+
125
+    /**
126
+     * Determines whether a post is an entity or not. Entity is in this context
127
+     * something which is not an article.
128
+     *
129
+     * @param int $post_id A post id.
130
+     *
131
+     * @return bool Return true if the post is an entity otherwise false.
132
+     * @since 3.1.0
133
+     *
134
+     */
135
+    public function is_entity( $post_id ) {
136
+
137
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
138
+
139
+        if ( is_wp_error( $terms ) ) {
140
+            $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
141
+
142
+            return false;
143
+        }
144
+
145
+        if ( empty( $terms ) ) {
146
+            return false;
147
+        }
148
+
149
+        /*
150 150
 		 * We don't consider an `article` to be an entity.
151 151
 		 *
152 152
 		 * @since 3.20.0 At least one associated mustn't be an `article`.
153 153
 		 *
154 154
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
155 155
 		 */
156
-		foreach ( $terms as $term ) {
157
-			if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) {
158
-				return true;
159
-			}
160
-		}
161
-
162
-		return false;
163
-	}
164
-
165
-	/**
166
-	 * Get the proper classification scope for a given entity post
167
-	 *
168
-	 * @param integer $post_id An entity post id.
169
-	 *
170
-	 * @param string $default The default classification scope, `what` if not
171
-	 *                         provided.
172
-	 *
173
-	 * @return string Returns a classification scope (e.g. 'what').
174
-	 * @since 3.5.0
175
-	 *
176
-	 */
177
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
178
-
179
-		if ( false === $this->is_entity( $post_id ) ) {
180
-			return $default;
181
-		}
182
-
183
-		// Retrieve the entity type
184
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
185
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
186
-		// Retrieve classification boxes configuration
187
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
188
-		foreach ( $classification_boxes as $cb ) {
189
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
190
-				return $cb['id'];
191
-			}
192
-		}
193
-
194
-		return $default;
195
-	}
196
-
197
-	/**
198
-	 * Check whether a {@link WP_Post} is used.
199
-	 *
200
-	 * @param int $post_id The {@link WP_Post}'s id.
201
-	 *
202
-	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
203
-	 */
204
-	public function is_used( $post_id ) {
205
-
206
-		if ( false === $this->is_entity( $post_id ) ) {
207
-			return null;
208
-		}
209
-		// Retrieve the post
210
-		$entity = get_post( $post_id );
211
-
212
-		global $wpdb;
213
-		// Retrieve Wordlift relation instances table name
214
-		$table_name = wl_core_get_relation_instances_table_name();
215
-
216
-		// Check is it's referenced / related to another post / entity
217
-		$stmt = $wpdb->prepare(
218
-			"SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
219
-			$entity->ID
220
-		);
221
-
222
-		// Perform the query
223
-		$relation_instances = (int) $wpdb->get_var( $stmt );
224
-		// If there is at least one relation instance for the current entity, then it's used
225
-		if ( 0 < $relation_instances ) {
226
-			return true;
227
-		}
228
-
229
-		// Check if the entity uri is used as meta_value
230
-		$stmt = $wpdb->prepare(
231
-			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
232
-			$entity->ID,
233
-			wl_get_entity_uri( $entity->ID )
234
-		);
235
-		// Perform the query
236
-		$meta_instances = (int) $wpdb->get_var( $stmt );
237
-
238
-		// If there is at least one meta that refers the current entity uri, then current entity is used
239
-		if ( 0 < $meta_instances ) {
240
-			return true;
241
-		}
242
-
243
-		// If we are here, it means the current entity is not used at the moment
244
-		return false;
245
-	}
246
-
247
-	/**
248
-	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
249
-	 *
250
-	 * @param string $uri The entity URI.
251
-	 *
252
-	 * @return WP_Post|null A WP_Post instance or null if not found.
253
-	 * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
254
-	 *
255
-	 * @since      3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
256
-	 * @since      3.2.0
257
-	 *
258
-	 */
259
-	public function get_entity_post_by_uri( $uri ) {
260
-
261
-		return $this->entity_uri_service->get_entity( $uri );
262
-	}
263
-
264
-	/**
265
-	 * Fires once a post has been saved. This function uses the $_REQUEST, therefore
266
-	 * we check that the post we're saving is the current post.
267
-	 *
268
-	 * @see   https://github.com/insideout10/wordlift-plugin/issues/363
269
-	 *
270
-	 * @since 3.2.0
271
-	 *
272
-	 * @param int $post_id Post ID.
273
-	 * @param WP_Post $post Post object.
274
-	 * @param bool $update Whether this is an existing post being updated or not.
275
-	 */
276
-	public function save_post( $post_id, $post, $update ) {
277
-
278
-		// Avoid doing anything if post is autosave or a revision.
279
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
280
-			return;
281
-		}
282
-
283
-		// We're setting the alternative label that have been provided via the UI
284
-		// (in fact we're using $_REQUEST), while save_post may be also called
285
-		// programmatically by some other function: we need to check therefore if
286
-		// the $post_id in the save_post call matches the post id set in the request.
287
-		//
288
-		// If this is not the current post being saved or if it's not an entity, return.
289
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
290
-			return;
291
-		}
292
-
293
-		// Get the alt labels from the request (or empty array).
294
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array();
295
-
296
-		if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) {
297
-			// This is via classic editor, so set the alternative labels.
298
-			$this->set_alternative_labels( $post_id, $alt_labels );
299
-		}
300
-
301
-
302
-	}
303
-
304
-	/**
305
-	 * Set the alternative labels.
306
-	 *
307
-	 * @param int $post_id The post id.
308
-	 * @param array $alt_labels An array of labels.
309
-	 *
310
-	 * @since 3.2.0
311
-	 *
312
-	 */
313
-	public function set_alternative_labels( $post_id, $alt_labels ) {
314
-
315
-		// Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error
316
-		// instead of post id.
317
-		if ( ! is_numeric( $post_id ) ) {
318
-			return;
319
-		}
320
-
321
-		// Force $alt_labels to be an array
322
-		if ( ! is_array( $alt_labels ) ) {
323
-			$alt_labels = array( $alt_labels );
324
-		}
325
-
326
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
327
-
328
-		// Delete all the existing alternate labels.
329
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
330
-
331
-		// Save only unique synonymns.
332
-		$alt_labels = array_unique( $alt_labels );
333
-
334
-		// Set the alternative labels.
335
-		foreach ( $alt_labels as $alt_label ) {
336
-
337
-			// Strip html code from synonym.
338
-			$alt_label = wp_strip_all_tags( $alt_label );
339
-
340
-			if ( ! empty( $alt_label ) ) {
341
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label );
342
-			}
343
-		}
344
-
345
-	}
346
-
347
-	/**
348
-	 * Retrieve the alternate labels.
349
-	 *
350
-	 * @param int $post_id Post id.
351
-	 *
352
-	 * @return mixed An array  of alternative labels.
353
-	 * @since 3.2.0
354
-	 *
355
-	 */
356
-	public function get_alternative_labels( $post_id ) {
357
-
358
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
359
-	}
360
-
361
-	/**
362
-	 * Retrieve the labels for an entity, i.e. the title + the synonyms.
363
-	 *
364
-	 * @param int $post_id The entity {@link WP_Post} id.
365
-	 * @param int $object_type The object type {@link Object_Type_Enum}
366
-	 *
367
-	 * @return array An array with the entity title and labels.
368
-	 * @since 3.12.0
369
-	 */
370
-	public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) {
371
-		if ( $object_type === Object_Type_Enum::POST ) {
372
-			return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
373
-		}
374
-
375
-		// Term Reference dont have synonyms yet.
376
-		return array();
377
-	}
378
-
379
-	/**
380
-	 * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
381
-	 *
382
-	 * @param WP_Post $post Post object.
383
-	 *
384
-	 * @since 3.2.0
385
-	 *
386
-	 */
387
-	public function edit_form_before_permalink( $post ) {
388
-
389
-		// If it's not an entity, return.
390
-		if ( ! $this->is_entity( $post->ID ) ) {
391
-			return;
392
-		}
393
-
394
-		// If disabled by filter, return.
395
-		if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) {
396
-			return;
397
-		}
398
-
399
-		// Print the input template.
400
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
401
-
402
-		// Print all the currently set alternative labels.
403
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
404
-
405
-			echo $this->get_alternative_label_input( $alt_label );
406
-
407
-		};
408
-
409
-		// Print the button.
410
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
411
-
412
-	}
413
-
414
-	/**
415
-	 * Get the URI for the entity with the specified post id.
416
-	 *
417
-	 * @param int $post_id The entity post id.
418
-	 *
419
-	 * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
420
-	 * @since 3.6.0
421
-	 *
422
-	 */
423
-	private function get_uri_for_post( $post_id ) {
424
-
425
-		$log = Wordlift_Log_Service::get_logger( get_class() );
426
-
427
-		// If a null is given, nothing to do
428
-		if ( is_null( $post_id ) ) {
429
-			return null;
430
-		}
431
-
432
-		$dataset_uri = wl_configuration_get_redlink_dataset_uri();
433
-
434
-		if ( empty( $dataset_uri ) ) {
435
-			// Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri()
436
-			$log->debug( 'Continuing, dataset uri not configured...' );
437
-		}
438
-
439
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
440
-
441
-		/*
156
+        foreach ( $terms as $term ) {
157
+            if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) {
158
+                return true;
159
+            }
160
+        }
161
+
162
+        return false;
163
+    }
164
+
165
+    /**
166
+     * Get the proper classification scope for a given entity post
167
+     *
168
+     * @param integer $post_id An entity post id.
169
+     *
170
+     * @param string $default The default classification scope, `what` if not
171
+     *                         provided.
172
+     *
173
+     * @return string Returns a classification scope (e.g. 'what').
174
+     * @since 3.5.0
175
+     *
176
+     */
177
+    public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
178
+
179
+        if ( false === $this->is_entity( $post_id ) ) {
180
+            return $default;
181
+        }
182
+
183
+        // Retrieve the entity type
184
+        $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
185
+        $entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
186
+        // Retrieve classification boxes configuration
187
+        $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
188
+        foreach ( $classification_boxes as $cb ) {
189
+            if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
190
+                return $cb['id'];
191
+            }
192
+        }
193
+
194
+        return $default;
195
+    }
196
+
197
+    /**
198
+     * Check whether a {@link WP_Post} is used.
199
+     *
200
+     * @param int $post_id The {@link WP_Post}'s id.
201
+     *
202
+     * @return bool|null Null if it's not an entity, otherwise true if it's used.
203
+     */
204
+    public function is_used( $post_id ) {
205
+
206
+        if ( false === $this->is_entity( $post_id ) ) {
207
+            return null;
208
+        }
209
+        // Retrieve the post
210
+        $entity = get_post( $post_id );
211
+
212
+        global $wpdb;
213
+        // Retrieve Wordlift relation instances table name
214
+        $table_name = wl_core_get_relation_instances_table_name();
215
+
216
+        // Check is it's referenced / related to another post / entity
217
+        $stmt = $wpdb->prepare(
218
+            "SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
219
+            $entity->ID
220
+        );
221
+
222
+        // Perform the query
223
+        $relation_instances = (int) $wpdb->get_var( $stmt );
224
+        // If there is at least one relation instance for the current entity, then it's used
225
+        if ( 0 < $relation_instances ) {
226
+            return true;
227
+        }
228
+
229
+        // Check if the entity uri is used as meta_value
230
+        $stmt = $wpdb->prepare(
231
+            "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
232
+            $entity->ID,
233
+            wl_get_entity_uri( $entity->ID )
234
+        );
235
+        // Perform the query
236
+        $meta_instances = (int) $wpdb->get_var( $stmt );
237
+
238
+        // If there is at least one meta that refers the current entity uri, then current entity is used
239
+        if ( 0 < $meta_instances ) {
240
+            return true;
241
+        }
242
+
243
+        // If we are here, it means the current entity is not used at the moment
244
+        return false;
245
+    }
246
+
247
+    /**
248
+     * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
249
+     *
250
+     * @param string $uri The entity URI.
251
+     *
252
+     * @return WP_Post|null A WP_Post instance or null if not found.
253
+     * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
254
+     *
255
+     * @since      3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
256
+     * @since      3.2.0
257
+     *
258
+     */
259
+    public function get_entity_post_by_uri( $uri ) {
260
+
261
+        return $this->entity_uri_service->get_entity( $uri );
262
+    }
263
+
264
+    /**
265
+     * Fires once a post has been saved. This function uses the $_REQUEST, therefore
266
+     * we check that the post we're saving is the current post.
267
+     *
268
+     * @see   https://github.com/insideout10/wordlift-plugin/issues/363
269
+     *
270
+     * @since 3.2.0
271
+     *
272
+     * @param int $post_id Post ID.
273
+     * @param WP_Post $post Post object.
274
+     * @param bool $update Whether this is an existing post being updated or not.
275
+     */
276
+    public function save_post( $post_id, $post, $update ) {
277
+
278
+        // Avoid doing anything if post is autosave or a revision.
279
+        if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
280
+            return;
281
+        }
282
+
283
+        // We're setting the alternative label that have been provided via the UI
284
+        // (in fact we're using $_REQUEST), while save_post may be also called
285
+        // programmatically by some other function: we need to check therefore if
286
+        // the $post_id in the save_post call matches the post id set in the request.
287
+        //
288
+        // If this is not the current post being saved or if it's not an entity, return.
289
+        if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
290
+            return;
291
+        }
292
+
293
+        // Get the alt labels from the request (or empty array).
294
+        $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array();
295
+
296
+        if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) {
297
+            // This is via classic editor, so set the alternative labels.
298
+            $this->set_alternative_labels( $post_id, $alt_labels );
299
+        }
300
+
301
+
302
+    }
303
+
304
+    /**
305
+     * Set the alternative labels.
306
+     *
307
+     * @param int $post_id The post id.
308
+     * @param array $alt_labels An array of labels.
309
+     *
310
+     * @since 3.2.0
311
+     *
312
+     */
313
+    public function set_alternative_labels( $post_id, $alt_labels ) {
314
+
315
+        // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error
316
+        // instead of post id.
317
+        if ( ! is_numeric( $post_id ) ) {
318
+            return;
319
+        }
320
+
321
+        // Force $alt_labels to be an array
322
+        if ( ! is_array( $alt_labels ) ) {
323
+            $alt_labels = array( $alt_labels );
324
+        }
325
+
326
+        $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
327
+
328
+        // Delete all the existing alternate labels.
329
+        delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
330
+
331
+        // Save only unique synonymns.
332
+        $alt_labels = array_unique( $alt_labels );
333
+
334
+        // Set the alternative labels.
335
+        foreach ( $alt_labels as $alt_label ) {
336
+
337
+            // Strip html code from synonym.
338
+            $alt_label = wp_strip_all_tags( $alt_label );
339
+
340
+            if ( ! empty( $alt_label ) ) {
341
+                add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label );
342
+            }
343
+        }
344
+
345
+    }
346
+
347
+    /**
348
+     * Retrieve the alternate labels.
349
+     *
350
+     * @param int $post_id Post id.
351
+     *
352
+     * @return mixed An array  of alternative labels.
353
+     * @since 3.2.0
354
+     *
355
+     */
356
+    public function get_alternative_labels( $post_id ) {
357
+
358
+        return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
359
+    }
360
+
361
+    /**
362
+     * Retrieve the labels for an entity, i.e. the title + the synonyms.
363
+     *
364
+     * @param int $post_id The entity {@link WP_Post} id.
365
+     * @param int $object_type The object type {@link Object_Type_Enum}
366
+     *
367
+     * @return array An array with the entity title and labels.
368
+     * @since 3.12.0
369
+     */
370
+    public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) {
371
+        if ( $object_type === Object_Type_Enum::POST ) {
372
+            return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
373
+        }
374
+
375
+        // Term Reference dont have synonyms yet.
376
+        return array();
377
+    }
378
+
379
+    /**
380
+     * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
381
+     *
382
+     * @param WP_Post $post Post object.
383
+     *
384
+     * @since 3.2.0
385
+     *
386
+     */
387
+    public function edit_form_before_permalink( $post ) {
388
+
389
+        // If it's not an entity, return.
390
+        if ( ! $this->is_entity( $post->ID ) ) {
391
+            return;
392
+        }
393
+
394
+        // If disabled by filter, return.
395
+        if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) {
396
+            return;
397
+        }
398
+
399
+        // Print the input template.
400
+        $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
401
+
402
+        // Print all the currently set alternative labels.
403
+        foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
404
+
405
+            echo $this->get_alternative_label_input( $alt_label );
406
+
407
+        };
408
+
409
+        // Print the button.
410
+        $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
411
+
412
+    }
413
+
414
+    /**
415
+     * Get the URI for the entity with the specified post id.
416
+     *
417
+     * @param int $post_id The entity post id.
418
+     *
419
+     * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
420
+     * @since 3.6.0
421
+     *
422
+     */
423
+    private function get_uri_for_post( $post_id ) {
424
+
425
+        $log = Wordlift_Log_Service::get_logger( get_class() );
426
+
427
+        // If a null is given, nothing to do
428
+        if ( is_null( $post_id ) ) {
429
+            return null;
430
+        }
431
+
432
+        $dataset_uri = wl_configuration_get_redlink_dataset_uri();
433
+
434
+        if ( empty( $dataset_uri ) ) {
435
+            // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri()
436
+            $log->debug( 'Continuing, dataset uri not configured...' );
437
+        }
438
+
439
+        $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
440
+
441
+        /*
442 442
 		 * Consider the URI invalid if it doesn't start with the dataset URI.
443 443
 		 *
444 444
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/996
445 445
 		 */
446
-		if ( empty( $dataset_uri ) || 0 !== strpos( $uri, $dataset_uri ) ) {
447
-			$uri = null;
448
-		}
449
-
450
-		// Set the URI if it isn't set yet.
451
-		$post_status = get_post_status( $post_id );
452
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'inherit' !== $post_status ) {
453
-			$uri = wl_build_entity_uri( $post_id );
454
-			wl_set_entity_uri( $post_id, $uri );
455
-		}
456
-
457
-		return $uri;
458
-	}
459
-
460
-	public function get_uri( $object_id, $type = Object_Type_Enum::POST ) {
461
-
462
-		if ( Object_Type_Enum::POST === $type ) {
463
-			return $this->get_uri_for_post( $object_id );
464
-		}
465
-
466
-		if ( Object_Type_Enum::USER === $type ) {
467
-			$uri = Wordlift_User_Service::get_instance()->get_uri( $object_id );
468
-
469
-			return ( false === $uri ? null : $uri );
470
-		}
471
-
472
-		if ( Object_Type_Enum::TERM === $type ) {
473
-			return wl_get_term_entity_uri( $object_id );
474
-		}
475
-
476
-		return null;
477
-	}
478
-
479
-	/**
480
-	 * Get the alternative label input HTML code.
481
-	 *
482
-	 * @param string $value The input value.
483
-	 *
484
-	 * @return string The input HTML code.
485
-	 * @since 3.2.0
486
-	 *
487
-	 */
488
-	private function get_alternative_label_input( $value = '' ) {
489
-
490
-		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
491
-	}
492
-
493
-	/**
494
-	 * Get the number of entity posts published in this blog.
495
-	 *
496
-	 * @return int The number of published entity posts.
497
-	 * @since 3.6.0
498
-	 *
499
-	 */
500
-	public function count() {
501
-		global $wpdb;
502
-
503
-		// Try to get the count from the transient.
504
-		$count = get_transient( '_wl_entity_service__count' );
505
-		if ( false !== $count ) {
506
-			return $count;
507
-		}
508
-
509
-		// Query the count.
510
-		$count = $wpdb->get_var( $wpdb->prepare(
511
-			"SELECT COUNT( DISTINCT( tr.object_id ) )"
512
-			. " FROM {$wpdb->term_relationships} tr"
513
-			. " INNER JOIN {$wpdb->term_taxonomy} tt"
514
-			. "  ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id"
515
-			. " INNER JOIN {$wpdb->terms} t"
516
-			. "  ON t.term_id = tt.term_id AND t.name != %s",
517
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
518
-			'article'
519
-		) );
520
-
521
-		// Store the count in cache.
522
-		set_transient( '_wl_entity_service__count', $count, 900 );
523
-
524
-		return $count;
525
-	}
526
-
527
-	/**
528
-	 * Add the entity filtering criterias to the arguments for a `get_posts`
529
-	 * call.
530
-	 *
531
-	 * @param array $args The arguments for a `get_posts` call.
532
-	 *
533
-	 * @return array The arguments for a `get_posts` call.
534
-	 * @since 3.15.0
535
-	 *
536
-	 */
537
-	public static function add_criterias( $args ) {
538
-
539
-		// Build an optimal tax-query.
540
-		$tax_query = array(
541
-			'relation' => 'AND',
542
-			array(
543
-				'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
544
-				'operator' => 'EXISTS',
545
-			),
546
-			array(
547
-				'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
548
-				'field'    => 'slug',
549
-				'terms'    => 'article',
550
-				'operator' => 'NOT IN',
551
-			),
552
-		);
553
-
554
-		return $args + array(
555
-				'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
556
-				/*
446
+        if ( empty( $dataset_uri ) || 0 !== strpos( $uri, $dataset_uri ) ) {
447
+            $uri = null;
448
+        }
449
+
450
+        // Set the URI if it isn't set yet.
451
+        $post_status = get_post_status( $post_id );
452
+        if ( empty( $uri ) && 'auto-draft' !== $post_status && 'inherit' !== $post_status ) {
453
+            $uri = wl_build_entity_uri( $post_id );
454
+            wl_set_entity_uri( $post_id, $uri );
455
+        }
456
+
457
+        return $uri;
458
+    }
459
+
460
+    public function get_uri( $object_id, $type = Object_Type_Enum::POST ) {
461
+
462
+        if ( Object_Type_Enum::POST === $type ) {
463
+            return $this->get_uri_for_post( $object_id );
464
+        }
465
+
466
+        if ( Object_Type_Enum::USER === $type ) {
467
+            $uri = Wordlift_User_Service::get_instance()->get_uri( $object_id );
468
+
469
+            return ( false === $uri ? null : $uri );
470
+        }
471
+
472
+        if ( Object_Type_Enum::TERM === $type ) {
473
+            return wl_get_term_entity_uri( $object_id );
474
+        }
475
+
476
+        return null;
477
+    }
478
+
479
+    /**
480
+     * Get the alternative label input HTML code.
481
+     *
482
+     * @param string $value The input value.
483
+     *
484
+     * @return string The input HTML code.
485
+     * @since 3.2.0
486
+     *
487
+     */
488
+    private function get_alternative_label_input( $value = '' ) {
489
+
490
+        return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
491
+    }
492
+
493
+    /**
494
+     * Get the number of entity posts published in this blog.
495
+     *
496
+     * @return int The number of published entity posts.
497
+     * @since 3.6.0
498
+     *
499
+     */
500
+    public function count() {
501
+        global $wpdb;
502
+
503
+        // Try to get the count from the transient.
504
+        $count = get_transient( '_wl_entity_service__count' );
505
+        if ( false !== $count ) {
506
+            return $count;
507
+        }
508
+
509
+        // Query the count.
510
+        $count = $wpdb->get_var( $wpdb->prepare(
511
+            "SELECT COUNT( DISTINCT( tr.object_id ) )"
512
+            . " FROM {$wpdb->term_relationships} tr"
513
+            . " INNER JOIN {$wpdb->term_taxonomy} tt"
514
+            . "  ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id"
515
+            . " INNER JOIN {$wpdb->terms} t"
516
+            . "  ON t.term_id = tt.term_id AND t.name != %s",
517
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
518
+            'article'
519
+        ) );
520
+
521
+        // Store the count in cache.
522
+        set_transient( '_wl_entity_service__count', $count, 900 );
523
+
524
+        return $count;
525
+    }
526
+
527
+    /**
528
+     * Add the entity filtering criterias to the arguments for a `get_posts`
529
+     * call.
530
+     *
531
+     * @param array $args The arguments for a `get_posts` call.
532
+     *
533
+     * @return array The arguments for a `get_posts` call.
534
+     * @since 3.15.0
535
+     *
536
+     */
537
+    public static function add_criterias( $args ) {
538
+
539
+        // Build an optimal tax-query.
540
+        $tax_query = array(
541
+            'relation' => 'AND',
542
+            array(
543
+                'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
544
+                'operator' => 'EXISTS',
545
+            ),
546
+            array(
547
+                'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
548
+                'field'    => 'slug',
549
+                'terms'    => 'article',
550
+                'operator' => 'NOT IN',
551
+            ),
552
+        );
553
+
554
+        return $args + array(
555
+                'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
556
+                /*
557 557
 				 * Ensure compatibility with Polylang.
558 558
 				 *
559 559
 				 * @see https://github.com/insideout10/wordlift-plugin/issues/855.
@@ -561,102 +561,102 @@  discard block
 block discarded – undo
561 561
 				 *
562 562
 				 * @since 3.19.5
563 563
 				 */
564
-				'lang'      => '',
565
-				'tax_query' => $tax_query,
566
-			);
567
-	}
568
-
569
-	/**
570
-	 * Create a new entity.
571
-	 *
572
-	 * @param string $name The entity name.
573
-	 * @param string $type_uri The entity's type URI.
574
-	 * @param null $logo The entity logo id (or NULL if none).
575
-	 * @param string $status The post status, by default 'publish'.
576
-	 *
577
-	 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
578
-	 * @since 3.9.0
579
-	 *
580
-	 */
581
-	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
582
-
583
-		// Create an entity for the publisher.
584
-		$post_id = @wp_insert_post( array(
585
-			'post_type'    => self::TYPE_NAME,
586
-			'post_title'   => $name,
587
-			'post_status'  => $status,
588
-			'post_content' => '',
589
-		) );
590
-
591
-		// Return the error if any.
592
-		if ( is_wp_error( $post_id ) ) {
593
-			return $post_id;
594
-		}
595
-
596
-		// Set the entity logo.
597
-		if ( $logo && is_numeric( $logo ) ) {
598
-			set_post_thumbnail( $post_id, $logo );
599
-		}
600
-
601
-		// Set the entity type.
602
-		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
603
-
604
-		return $post_id;
605
-	}
606
-
607
-	/**
608
-	 * Get the entities related to the one with the specified id. By default only
609
-	 * published entities will be returned.
610
-	 *
611
-	 * @param int $id The post id.
612
-	 * @param string $post_status The target post status (default = publish).
613
-	 *
614
-	 * @return array An array of post ids.
615
-	 * @since 3.10.0
616
-	 *
617
-	 */
618
-	public function get_related_entities( $id, $post_status = 'publish' ) {
619
-
620
-		return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
621
-	}
622
-
623
-	/**
624
-	 * Get the list of entities.
625
-	 *
626
-	 * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
627
-	 *
628
-	 * @return array An array of entity posts.
629
-	 * @since 3.12.2
630
-	 *
631
-	 */
632
-	public function get( $params = array() ) {
633
-
634
-		// Set the defaults.
635
-		$defaults = array( 'post_type' => 'entity' );
636
-
637
-		// Merge the defaults with the provided parameters.
638
-		$args = wp_parse_args( $params, $defaults );
639
-
640
-		// Call the `get_posts` function.
641
-		return get_posts( $args );
642
-	}
643
-
644
-	/**
645
-	 * The list of post type names which can be used for entities
646
-	 *
647
-	 * Criteria is that the post type is public. The list of valid post types
648
-	 * can be overridden with a filter.
649
-	 *
650
-	 * @return array Array containing the names of the valid post types.
651
-	 * @since 3.15.0
652
-	 *
653
-	 */
654
-	static function valid_entity_post_types() {
655
-
656
-		// Ignore builtins in the call to avoid getting attachments.
657
-		$post_types = array( 'post', 'page', self::TYPE_NAME, 'product' );
658
-
659
-		return apply_filters( 'wl_valid_entity_post_types', $post_types );
660
-	}
564
+                'lang'      => '',
565
+                'tax_query' => $tax_query,
566
+            );
567
+    }
568
+
569
+    /**
570
+     * Create a new entity.
571
+     *
572
+     * @param string $name The entity name.
573
+     * @param string $type_uri The entity's type URI.
574
+     * @param null $logo The entity logo id (or NULL if none).
575
+     * @param string $status The post status, by default 'publish'.
576
+     *
577
+     * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
578
+     * @since 3.9.0
579
+     *
580
+     */
581
+    public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
582
+
583
+        // Create an entity for the publisher.
584
+        $post_id = @wp_insert_post( array(
585
+            'post_type'    => self::TYPE_NAME,
586
+            'post_title'   => $name,
587
+            'post_status'  => $status,
588
+            'post_content' => '',
589
+        ) );
590
+
591
+        // Return the error if any.
592
+        if ( is_wp_error( $post_id ) ) {
593
+            return $post_id;
594
+        }
595
+
596
+        // Set the entity logo.
597
+        if ( $logo && is_numeric( $logo ) ) {
598
+            set_post_thumbnail( $post_id, $logo );
599
+        }
600
+
601
+        // Set the entity type.
602
+        Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
603
+
604
+        return $post_id;
605
+    }
606
+
607
+    /**
608
+     * Get the entities related to the one with the specified id. By default only
609
+     * published entities will be returned.
610
+     *
611
+     * @param int $id The post id.
612
+     * @param string $post_status The target post status (default = publish).
613
+     *
614
+     * @return array An array of post ids.
615
+     * @since 3.10.0
616
+     *
617
+     */
618
+    public function get_related_entities( $id, $post_status = 'publish' ) {
619
+
620
+        return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
621
+    }
622
+
623
+    /**
624
+     * Get the list of entities.
625
+     *
626
+     * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
627
+     *
628
+     * @return array An array of entity posts.
629
+     * @since 3.12.2
630
+     *
631
+     */
632
+    public function get( $params = array() ) {
633
+
634
+        // Set the defaults.
635
+        $defaults = array( 'post_type' => 'entity' );
636
+
637
+        // Merge the defaults with the provided parameters.
638
+        $args = wp_parse_args( $params, $defaults );
639
+
640
+        // Call the `get_posts` function.
641
+        return get_posts( $args );
642
+    }
643
+
644
+    /**
645
+     * The list of post type names which can be used for entities
646
+     *
647
+     * Criteria is that the post type is public. The list of valid post types
648
+     * can be overridden with a filter.
649
+     *
650
+     * @return array Array containing the names of the valid post types.
651
+     * @since 3.15.0
652
+     *
653
+     */
654
+    static function valid_entity_post_types() {
655
+
656
+        // Ignore builtins in the call to avoid getting attachments.
657
+        $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' );
658
+
659
+        return apply_filters( 'wl_valid_entity_post_types', $post_types );
660
+    }
661 661
 
662 662
 }
Please login to merge, or discard this patch.
Spacing   +97 added lines, -97 removed lines patch added patch discarded remove patch
@@ -99,9 +99,9 @@  discard block
 block discarded – undo
99 99
 	 * @since 3.2.0
100 100
 	 *
101 101
 	 */
102
-	public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
102
+	public function __construct($ui_service, $relation_service, $entity_uri_service) {
103 103
 
104
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
104
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service');
105 105
 
106 106
 		$this->ui_service         = $ui_service;
107 107
 		$this->relation_service   = $relation_service;
@@ -132,17 +132,17 @@  discard block
 block discarded – undo
132 132
 	 * @since 3.1.0
133 133
 	 *
134 134
 	 */
135
-	public function is_entity( $post_id ) {
135
+	public function is_entity($post_id) {
136 136
 
137
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
137
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
138 138
 
139
-		if ( is_wp_error( $terms ) ) {
140
-			$this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
139
+		if (is_wp_error($terms)) {
140
+			$this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message());
141 141
 
142 142
 			return false;
143 143
 		}
144 144
 
145
-		if ( empty( $terms ) ) {
145
+		if (empty($terms)) {
146 146
 			return false;
147 147
 		}
148 148
 
@@ -153,8 +153,8 @@  discard block
 block discarded – undo
153 153
 		 *
154 154
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
155 155
 		 */
156
-		foreach ( $terms as $term ) {
157
-			if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) {
156
+		foreach ($terms as $term) {
157
+			if (1 !== preg_match('~(^|-)article$~', $term->slug)) {
158 158
 				return true;
159 159
 			}
160 160
 		}
@@ -174,19 +174,19 @@  discard block
 block discarded – undo
174 174
 	 * @since 3.5.0
175 175
 	 *
176 176
 	 */
177
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
177
+	public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) {
178 178
 
179
-		if ( false === $this->is_entity( $post_id ) ) {
179
+		if (false === $this->is_entity($post_id)) {
180 180
 			return $default;
181 181
 		}
182 182
 
183 183
 		// Retrieve the entity type
184
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
185
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
184
+		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id);
185
+		$entity_type     = str_replace('wl-', '', $entity_type_arr['css_class']);
186 186
 		// Retrieve classification boxes configuration
187
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
188
-		foreach ( $classification_boxes as $cb ) {
189
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
187
+		$classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
188
+		foreach ($classification_boxes as $cb) {
189
+			if (in_array($entity_type, $cb['registeredTypes'])) {
190 190
 				return $cb['id'];
191 191
 			}
192 192
 		}
@@ -201,13 +201,13 @@  discard block
 block discarded – undo
201 201
 	 *
202 202
 	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
203 203
 	 */
204
-	public function is_used( $post_id ) {
204
+	public function is_used($post_id) {
205 205
 
206
-		if ( false === $this->is_entity( $post_id ) ) {
206
+		if (false === $this->is_entity($post_id)) {
207 207
 			return null;
208 208
 		}
209 209
 		// Retrieve the post
210
-		$entity = get_post( $post_id );
210
+		$entity = get_post($post_id);
211 211
 
212 212
 		global $wpdb;
213 213
 		// Retrieve Wordlift relation instances table name
@@ -220,9 +220,9 @@  discard block
 block discarded – undo
220 220
 		);
221 221
 
222 222
 		// Perform the query
223
-		$relation_instances = (int) $wpdb->get_var( $stmt );
223
+		$relation_instances = (int) $wpdb->get_var($stmt);
224 224
 		// If there is at least one relation instance for the current entity, then it's used
225
-		if ( 0 < $relation_instances ) {
225
+		if (0 < $relation_instances) {
226 226
 			return true;
227 227
 		}
228 228
 
@@ -230,13 +230,13 @@  discard block
 block discarded – undo
230 230
 		$stmt = $wpdb->prepare(
231 231
 			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
232 232
 			$entity->ID,
233
-			wl_get_entity_uri( $entity->ID )
233
+			wl_get_entity_uri($entity->ID)
234 234
 		);
235 235
 		// Perform the query
236
-		$meta_instances = (int) $wpdb->get_var( $stmt );
236
+		$meta_instances = (int) $wpdb->get_var($stmt);
237 237
 
238 238
 		// If there is at least one meta that refers the current entity uri, then current entity is used
239
-		if ( 0 < $meta_instances ) {
239
+		if (0 < $meta_instances) {
240 240
 			return true;
241 241
 		}
242 242
 
@@ -256,9 +256,9 @@  discard block
 block discarded – undo
256 256
 	 * @since      3.2.0
257 257
 	 *
258 258
 	 */
259
-	public function get_entity_post_by_uri( $uri ) {
259
+	public function get_entity_post_by_uri($uri) {
260 260
 
261
-		return $this->entity_uri_service->get_entity( $uri );
261
+		return $this->entity_uri_service->get_entity($uri);
262 262
 	}
263 263
 
264 264
 	/**
@@ -273,10 +273,10 @@  discard block
 block discarded – undo
273 273
 	 * @param WP_Post $post Post object.
274 274
 	 * @param bool $update Whether this is an existing post being updated or not.
275 275
 	 */
276
-	public function save_post( $post_id, $post, $update ) {
276
+	public function save_post($post_id, $post, $update) {
277 277
 
278 278
 		// Avoid doing anything if post is autosave or a revision.
279
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
279
+		if (wp_is_post_autosave($post) || wp_is_post_revision($post)) {
280 280
 			return;
281 281
 		}
282 282
 
@@ -286,16 +286,16 @@  discard block
 block discarded – undo
286 286
 		// the $post_id in the save_post call matches the post id set in the request.
287 287
 		//
288 288
 		// If this is not the current post being saved or if it's not an entity, return.
289
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
289
+		if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) {
290 290
 			return;
291 291
 		}
292 292
 
293 293
 		// Get the alt labels from the request (or empty array).
294
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array();
294
+		$alt_labels = isset($_REQUEST['wl_alternative_label']) ? (array) $_REQUEST['wl_alternative_label'] : array();
295 295
 
296
-		if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) {
296
+		if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) {
297 297
 			// This is via classic editor, so set the alternative labels.
298
-			$this->set_alternative_labels( $post_id, $alt_labels );
298
+			$this->set_alternative_labels($post_id, $alt_labels);
299 299
 		}
300 300
 
301 301
 
@@ -310,35 +310,35 @@  discard block
 block discarded – undo
310 310
 	 * @since 3.2.0
311 311
 	 *
312 312
 	 */
313
-	public function set_alternative_labels( $post_id, $alt_labels ) {
313
+	public function set_alternative_labels($post_id, $alt_labels) {
314 314
 
315 315
 		// Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error
316 316
 		// instead of post id.
317
-		if ( ! is_numeric( $post_id ) ) {
317
+		if ( ! is_numeric($post_id)) {
318 318
 			return;
319 319
 		}
320 320
 
321 321
 		// Force $alt_labels to be an array
322
-		if ( ! is_array( $alt_labels ) ) {
323
-			$alt_labels = array( $alt_labels );
322
+		if ( ! is_array($alt_labels)) {
323
+			$alt_labels = array($alt_labels);
324 324
 		}
325 325
 
326
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
326
+		$this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]");
327 327
 
328 328
 		// Delete all the existing alternate labels.
329
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
329
+		delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
330 330
 
331 331
 		// Save only unique synonymns.
332
-		$alt_labels = array_unique( $alt_labels );
332
+		$alt_labels = array_unique($alt_labels);
333 333
 
334 334
 		// Set the alternative labels.
335
-		foreach ( $alt_labels as $alt_label ) {
335
+		foreach ($alt_labels as $alt_label) {
336 336
 
337 337
 			// Strip html code from synonym.
338
-			$alt_label = wp_strip_all_tags( $alt_label );
338
+			$alt_label = wp_strip_all_tags($alt_label);
339 339
 
340
-			if ( ! empty( $alt_label ) ) {
341
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label );
340
+			if ( ! empty($alt_label)) {
341
+				add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label);
342 342
 			}
343 343
 		}
344 344
 
@@ -353,9 +353,9 @@  discard block
 block discarded – undo
353 353
 	 * @since 3.2.0
354 354
 	 *
355 355
 	 */
356
-	public function get_alternative_labels( $post_id ) {
356
+	public function get_alternative_labels($post_id) {
357 357
 
358
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
358
+		return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
359 359
 	}
360 360
 
361 361
 	/**
@@ -367,9 +367,9 @@  discard block
 block discarded – undo
367 367
 	 * @return array An array with the entity title and labels.
368 368
 	 * @since 3.12.0
369 369
 	 */
370
-	public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) {
371
-		if ( $object_type === Object_Type_Enum::POST ) {
372
-			return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
370
+	public function get_labels($post_id, $object_type = Object_Type_Enum::POST) {
371
+		if ($object_type === Object_Type_Enum::POST) {
372
+			return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id));
373 373
 		}
374 374
 
375 375
 		// Term Reference dont have synonyms yet.
@@ -384,30 +384,30 @@  discard block
 block discarded – undo
384 384
 	 * @since 3.2.0
385 385
 	 *
386 386
 	 */
387
-	public function edit_form_before_permalink( $post ) {
387
+	public function edit_form_before_permalink($post) {
388 388
 
389 389
 		// If it's not an entity, return.
390
-		if ( ! $this->is_entity( $post->ID ) ) {
390
+		if ( ! $this->is_entity($post->ID)) {
391 391
 			return;
392 392
 		}
393 393
 
394 394
 		// If disabled by filter, return.
395
-		if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) {
395
+		if ( ! apply_filters('wl_feature__enable__add-synonyms', true)) {
396 396
 			return;
397 397
 		}
398 398
 
399 399
 		// Print the input template.
400
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
400
+		$this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input());
401 401
 
402 402
 		// Print all the currently set alternative labels.
403
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
403
+		foreach ($this->get_alternative_labels($post->ID) as $alt_label) {
404 404
 
405
-			echo $this->get_alternative_label_input( $alt_label );
405
+			echo $this->get_alternative_label_input($alt_label);
406 406
 
407 407
 		};
408 408
 
409 409
 		// Print the button.
410
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
410
+		$this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift'));
411 411
 
412 412
 	}
413 413
 
@@ -420,57 +420,57 @@  discard block
 block discarded – undo
420 420
 	 * @since 3.6.0
421 421
 	 *
422 422
 	 */
423
-	private function get_uri_for_post( $post_id ) {
423
+	private function get_uri_for_post($post_id) {
424 424
 
425
-		$log = Wordlift_Log_Service::get_logger( get_class() );
425
+		$log = Wordlift_Log_Service::get_logger(get_class());
426 426
 
427 427
 		// If a null is given, nothing to do
428
-		if ( is_null( $post_id ) ) {
428
+		if (is_null($post_id)) {
429 429
 			return null;
430 430
 		}
431 431
 
432 432
 		$dataset_uri = wl_configuration_get_redlink_dataset_uri();
433 433
 
434
-		if ( empty( $dataset_uri ) ) {
434
+		if (empty($dataset_uri)) {
435 435
 			// Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri()
436
-			$log->debug( 'Continuing, dataset uri not configured...' );
436
+			$log->debug('Continuing, dataset uri not configured...');
437 437
 		}
438 438
 
439
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
439
+		$uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true);
440 440
 
441 441
 		/*
442 442
 		 * Consider the URI invalid if it doesn't start with the dataset URI.
443 443
 		 *
444 444
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/996
445 445
 		 */
446
-		if ( empty( $dataset_uri ) || 0 !== strpos( $uri, $dataset_uri ) ) {
446
+		if (empty($dataset_uri) || 0 !== strpos($uri, $dataset_uri)) {
447 447
 			$uri = null;
448 448
 		}
449 449
 
450 450
 		// Set the URI if it isn't set yet.
451
-		$post_status = get_post_status( $post_id );
452
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'inherit' !== $post_status ) {
453
-			$uri = wl_build_entity_uri( $post_id );
454
-			wl_set_entity_uri( $post_id, $uri );
451
+		$post_status = get_post_status($post_id);
452
+		if (empty($uri) && 'auto-draft' !== $post_status && 'inherit' !== $post_status) {
453
+			$uri = wl_build_entity_uri($post_id);
454
+			wl_set_entity_uri($post_id, $uri);
455 455
 		}
456 456
 
457 457
 		return $uri;
458 458
 	}
459 459
 
460
-	public function get_uri( $object_id, $type = Object_Type_Enum::POST ) {
460
+	public function get_uri($object_id, $type = Object_Type_Enum::POST) {
461 461
 
462
-		if ( Object_Type_Enum::POST === $type ) {
463
-			return $this->get_uri_for_post( $object_id );
462
+		if (Object_Type_Enum::POST === $type) {
463
+			return $this->get_uri_for_post($object_id);
464 464
 		}
465 465
 
466
-		if ( Object_Type_Enum::USER === $type ) {
467
-			$uri = Wordlift_User_Service::get_instance()->get_uri( $object_id );
466
+		if (Object_Type_Enum::USER === $type) {
467
+			$uri = Wordlift_User_Service::get_instance()->get_uri($object_id);
468 468
 
469
-			return ( false === $uri ? null : $uri );
469
+			return (false === $uri ? null : $uri);
470 470
 		}
471 471
 
472
-		if ( Object_Type_Enum::TERM === $type ) {
473
-			return wl_get_term_entity_uri( $object_id );
472
+		if (Object_Type_Enum::TERM === $type) {
473
+			return wl_get_term_entity_uri($object_id);
474 474
 		}
475 475
 
476 476
 		return null;
@@ -485,9 +485,9 @@  discard block
 block discarded – undo
485 485
 	 * @since 3.2.0
486 486
 	 *
487 487
 	 */
488
-	private function get_alternative_label_input( $value = '' ) {
488
+	private function get_alternative_label_input($value = '') {
489 489
 
490
-		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
490
+		return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift'));
491 491
 	}
492 492
 
493 493
 	/**
@@ -501,13 +501,13 @@  discard block
 block discarded – undo
501 501
 		global $wpdb;
502 502
 
503 503
 		// Try to get the count from the transient.
504
-		$count = get_transient( '_wl_entity_service__count' );
505
-		if ( false !== $count ) {
504
+		$count = get_transient('_wl_entity_service__count');
505
+		if (false !== $count) {
506 506
 			return $count;
507 507
 		}
508 508
 
509 509
 		// Query the count.
510
-		$count = $wpdb->get_var( $wpdb->prepare(
510
+		$count = $wpdb->get_var($wpdb->prepare(
511 511
 			"SELECT COUNT( DISTINCT( tr.object_id ) )"
512 512
 			. " FROM {$wpdb->term_relationships} tr"
513 513
 			. " INNER JOIN {$wpdb->term_taxonomy} tt"
@@ -516,10 +516,10 @@  discard block
 block discarded – undo
516 516
 			. "  ON t.term_id = tt.term_id AND t.name != %s",
517 517
 			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
518 518
 			'article'
519
-		) );
519
+		));
520 520
 
521 521
 		// Store the count in cache.
522
-		set_transient( '_wl_entity_service__count', $count, 900 );
522
+		set_transient('_wl_entity_service__count', $count, 900);
523 523
 
524 524
 		return $count;
525 525
 	}
@@ -534,7 +534,7 @@  discard block
 block discarded – undo
534 534
 	 * @since 3.15.0
535 535
 	 *
536 536
 	 */
537
-	public static function add_criterias( $args ) {
537
+	public static function add_criterias($args) {
538 538
 
539 539
 		// Build an optimal tax-query.
540 540
 		$tax_query = array(
@@ -578,28 +578,28 @@  discard block
 block discarded – undo
578 578
 	 * @since 3.9.0
579 579
 	 *
580 580
 	 */
581
-	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
581
+	public function create($name, $type_uri, $logo = null, $status = 'publish') {
582 582
 
583 583
 		// Create an entity for the publisher.
584
-		$post_id = @wp_insert_post( array(
584
+		$post_id = @wp_insert_post(array(
585 585
 			'post_type'    => self::TYPE_NAME,
586 586
 			'post_title'   => $name,
587 587
 			'post_status'  => $status,
588 588
 			'post_content' => '',
589
-		) );
589
+		));
590 590
 
591 591
 		// Return the error if any.
592
-		if ( is_wp_error( $post_id ) ) {
592
+		if (is_wp_error($post_id)) {
593 593
 			return $post_id;
594 594
 		}
595 595
 
596 596
 		// Set the entity logo.
597
-		if ( $logo && is_numeric( $logo ) ) {
598
-			set_post_thumbnail( $post_id, $logo );
597
+		if ($logo && is_numeric($logo)) {
598
+			set_post_thumbnail($post_id, $logo);
599 599
 		}
600 600
 
601 601
 		// Set the entity type.
602
-		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
602
+		Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri);
603 603
 
604 604
 		return $post_id;
605 605
 	}
@@ -615,9 +615,9 @@  discard block
 block discarded – undo
615 615
 	 * @since 3.10.0
616 616
 	 *
617 617
 	 */
618
-	public function get_related_entities( $id, $post_status = 'publish' ) {
618
+	public function get_related_entities($id, $post_status = 'publish') {
619 619
 
620
-		return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
620
+		return $this->relation_service->get_objects($id, 'ids', null, $post_status);
621 621
 	}
622 622
 
623 623
 	/**
@@ -629,16 +629,16 @@  discard block
 block discarded – undo
629 629
 	 * @since 3.12.2
630 630
 	 *
631 631
 	 */
632
-	public function get( $params = array() ) {
632
+	public function get($params = array()) {
633 633
 
634 634
 		// Set the defaults.
635
-		$defaults = array( 'post_type' => 'entity' );
635
+		$defaults = array('post_type' => 'entity');
636 636
 
637 637
 		// Merge the defaults with the provided parameters.
638
-		$args = wp_parse_args( $params, $defaults );
638
+		$args = wp_parse_args($params, $defaults);
639 639
 
640 640
 		// Call the `get_posts` function.
641
-		return get_posts( $args );
641
+		return get_posts($args);
642 642
 	}
643 643
 
644 644
 	/**
@@ -654,9 +654,9 @@  discard block
 block discarded – undo
654 654
 	static function valid_entity_post_types() {
655 655
 
656 656
 		// Ignore builtins in the call to avoid getting attachments.
657
-		$post_types = array( 'post', 'page', self::TYPE_NAME, 'product' );
657
+		$post_types = array('post', 'page', self::TYPE_NAME, 'product');
658 658
 
659
-		return apply_filters( 'wl_valid_entity_post_types', $post_types );
659
+		return apply_filters('wl_valid_entity_post_types', $post_types);
660 660
 	}
661 661
 
662 662
 }
Please login to merge, or discard this patch.