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