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