Completed
Push — develop ( b03bf8...d7af47 )
by David
03:01
created
src/includes/class-wordlift-entity-service.php 2 patches
Indentation   +718 added lines, -718 removed lines patch added patch discarded remove patch
@@ -7,637 +7,637 @@  discard block
 block discarded – undo
7 7
  */
8 8
 class Wordlift_Entity_Service {
9 9
 
10
-	/**
11
-	 * The Log service.
12
-	 *
13
-	 * @since  3.2.0
14
-	 * @access private
15
-	 * @var \Wordlift_Log_Service $log The Log service.
16
-	 */
17
-	private $log;
18
-
19
-	/**
20
-	 * The UI service.
21
-	 *
22
-	 * @since  3.2.0
23
-	 * @access private
24
-	 * @var \Wordlift_UI_Service $ui_service The UI service.
25
-	 */
26
-	private $ui_service;
27
-
28
-	/**
29
-	 * The {@link Wordlift_Relation_Service} instance.
30
-	 *
31
-	 * @since  3.15.0
32
-	 * @access private
33
-	 * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
34
-	 */
35
-	private $relation_service;
36
-
37
-	/**
38
-	 * An array of URIs to post ID valid for the current request.
39
-	 *
40
-	 * @since 3.16.1
41
-	 * @var array $uri_to_post An array of URIs to post ID valid for the current request.
42
-	 */
43
-	private $uri_to_post;
44
-
45
-	/**
46
-	 * The entity post type name.
47
-	 *
48
-	 * @since 3.1.0
49
-	 */
50
-	const TYPE_NAME = 'entity';
51
-
52
-	/**
53
-	 * The alternative label meta key.
54
-	 *
55
-	 * @since 3.2.0
56
-	 */
57
-	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
58
-
59
-	/**
60
-	 * The alternative label input template.
61
-	 *
62
-	 * @since 3.2.0
63
-	 */
64
-	// TODO: this should be moved to a class that deals with HTML code.
65
-	const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
10
+    /**
11
+     * The Log service.
12
+     *
13
+     * @since  3.2.0
14
+     * @access private
15
+     * @var \Wordlift_Log_Service $log The Log service.
16
+     */
17
+    private $log;
18
+
19
+    /**
20
+     * The UI service.
21
+     *
22
+     * @since  3.2.0
23
+     * @access private
24
+     * @var \Wordlift_UI_Service $ui_service The UI service.
25
+     */
26
+    private $ui_service;
27
+
28
+    /**
29
+     * The {@link Wordlift_Relation_Service} instance.
30
+     *
31
+     * @since  3.15.0
32
+     * @access private
33
+     * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
34
+     */
35
+    private $relation_service;
36
+
37
+    /**
38
+     * An array of URIs to post ID valid for the current request.
39
+     *
40
+     * @since 3.16.1
41
+     * @var array $uri_to_post An array of URIs to post ID valid for the current request.
42
+     */
43
+    private $uri_to_post;
44
+
45
+    /**
46
+     * The entity post type name.
47
+     *
48
+     * @since 3.1.0
49
+     */
50
+    const TYPE_NAME = 'entity';
51
+
52
+    /**
53
+     * The alternative label meta key.
54
+     *
55
+     * @since 3.2.0
56
+     */
57
+    const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
58
+
59
+    /**
60
+     * The alternative label input template.
61
+     *
62
+     * @since 3.2.0
63
+     */
64
+    // TODO: this should be moved to a class that deals with HTML code.
65
+    const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
66 66
                 <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label>
67 67
                 <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text">
68 68
                 <button class="button wl-delete-button">%s</button>
69 69
                 </div>';
70 70
 
71
-	/**
72
-	 * A singleton instance of the Entity service.
73
-	 *
74
-	 * @since  3.2.0
75
-	 * @access private
76
-	 * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
77
-	 */
78
-	private static $instance;
79
-
80
-	/**
81
-	 * Create a Wordlift_Entity_Service instance.
82
-	 *
83
-	 * @since 3.2.0
84
-	 *
85
-	 * @param \Wordlift_UI_Service       $ui_service       The UI service.
86
-	 * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
87
-	 */
88
-	public function __construct( $ui_service, $relation_service ) {
89
-
90
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
91
-
92
-		$this->ui_service       = $ui_service;
93
-		$this->relation_service = $relation_service;
94
-
95
-		$this->reset_uris();
96
-
97
-		// Set the singleton instance.
98
-		self::$instance = $this;
99
-	}
100
-
101
-	/**
102
-	 * Get the singleton instance of the Entity service.
103
-	 *
104
-	 * @since 3.2.0
105
-	 * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
106
-	 */
107
-	public static function get_instance() {
108
-
109
-		return self::$instance;
110
-	}
111
-
112
-	/**
113
-	 * Determines whether a post is an entity or not. Entity is in this context
114
-	 * something which is not an article.
115
-	 *
116
-	 * @since 3.1.0
117
-	 *
118
-	 * @param int $post_id A post id.
119
-	 *
120
-	 * @return bool Return true if the post is an entity otherwise false.
121
-	 */
122
-	public function is_entity( $post_id ) {
123
-
124
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME );
125
-
126
-		if ( 0 === count( $terms ) ) {
127
-			return false;
128
-		}
129
-
130
-		// We don't consider an `article` to be an entity.
131
-		if ( 'article' !== $terms[0]->slug ) {
132
-			return true;
133
-		}
134
-
135
-		return false;
136
-	}
137
-
138
-	/**
139
-	 * Get the proper classification scope for a given entity post
140
-	 *
141
-	 * @since 3.5.0
142
-	 *
143
-	 * @param integer $post_id An entity post id.
144
-	 *
145
-	 * @param string  $default The default classification scope, `what` if not
146
-	 *                         provided.
147
-	 *
148
-	 * @return string Returns a classification scope (e.g. 'what').
149
-	 */
150
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
151
-
152
-		if ( false === $this->is_entity( $post_id ) ) {
153
-			return $default;
154
-		}
155
-
156
-		// Retrieve the entity type
157
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
158
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
159
-		// Retrieve classification boxes configuration
160
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
161
-		foreach ( $classification_boxes as $cb ) {
162
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
163
-				return $cb['id'];
164
-			}
165
-		}
166
-
167
-		return $default;
168
-	}
169
-
170
-	/**
171
-	 * Check whether a {@link WP_Post} is used.
172
-	 *
173
-	 * @param int $post_id The {@link WP_Post}'s id.
174
-	 *
175
-	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
176
-	 */
177
-	public function is_used( $post_id ) {
178
-
179
-		if ( false === $this->is_entity( $post_id ) ) {
180
-			return null;
181
-		}
182
-		// Retrieve the post
183
-		$entity = get_post( $post_id );
184
-
185
-		global $wpdb;
186
-		// Retrieve Wordlift relation instances table name
187
-		$table_name = wl_core_get_relation_instances_table_name();
188
-
189
-		// Check is it's referenced / related to another post / entity
190
-		$stmt = $wpdb->prepare(
191
-			"SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
192
-			$entity->ID
193
-		);
194
-
195
-		// Perform the query
196
-		$relation_instances = (int) $wpdb->get_var( $stmt );
197
-		// If there is at least one relation instance for the current entity, then it's used
198
-		if ( 0 < $relation_instances ) {
199
-			return true;
200
-		}
201
-
202
-		// Check if the entity uri is used as meta_value
203
-		$stmt = $wpdb->prepare(
204
-			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
205
-			$entity->ID,
206
-			wl_get_entity_uri( $entity->ID )
207
-		);
208
-		// Perform the query
209
-		$meta_instances = (int) $wpdb->get_var( $stmt );
210
-
211
-		// If there is at least one meta that refers the current entity uri, then current entity is used
212
-		if ( 0 < $meta_instances ) {
213
-			return true;
214
-		}
215
-
216
-		// If we are here, it means the current entity is not used at the moment
217
-		return false;
218
-	}
219
-
220
-	/**
221
-	 * Determines whether a given uri is an internal uri or not.
222
-	 *
223
-	 * @since 3.3.2
224
-	 *
225
-	 * @param int $uri An uri.
226
-	 *
227
-	 * @return true if the uri internal to the current dataset otherwise false.
228
-	 */
229
-	public function is_internal_uri( $uri ) {
230
-
231
-		return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) );
232
-	}
233
-
234
-	/**
235
-	 * Preload the provided URIs in the local cache.
236
-	 *
237
-	 * This function will populate the local `$uri_to_post` array by running a
238
-	 * single query with all the URIs and returning the mappings in the array.
239
-	 *
240
-	 * @since 3.16.1
241
-	 *
242
-	 * @param array $uris An array of URIs.
243
-	 */
244
-	public function preload_uris( $uris ) {
245
-
246
-		$that          = $this;
247
-		$external_uris = array_filter( $uris, function ( $item ) use ( $that ) {
248
-			return ! $that->is_internal_uri( $item );
249
-		} );
250
-
251
-		$query_args = array(
252
-			// See https://github.com/insideout10/wordlift-plugin/issues/654.
253
-			'ignore_sticky_posts' => 1,
254
-			'numberposts'         => - 1,
255
-			'post_status'         => 'any',
256
-			'post_type'           => Wordlift_Entity_Service::valid_entity_post_types(),
257
-			'meta_query'          => array(
258
-				array(
259
-					'key'     => WL_ENTITY_URL_META_NAME,
260
-					'value'   => $uris,
261
-					'compare' => 'IN',
262
-				),
263
-			),
264
-		);
265
-
266
-		// Only if the current uri is not an internal uri, entity search is
267
-		// performed also looking at sameAs values.
268
-		//
269
-		// This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
270
-		if ( 0 < count( $external_uris ) ) {
271
-
272
-			$query_args['meta_query']['relation'] = 'OR';
273
-			$query_args['meta_query'][]           = array(
274
-				'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
275
-				'value'   => $external_uris,
276
-				'compare' => 'IN',
277
-			);
278
-
279
-		}
280
-
281
-		// Get the posts.
282
-		$posts = get_posts( $query_args );
283
-
284
-		// Populate the array. We reinitialize the array on purpose because
285
-		// we don't want these data to long live.
286
-		$this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) {
287
-			return $carry
288
-				   // Get the URI related to the post and fill them with the item id.
289
-				   + array_fill_keys( $that->get_uris( $item->ID ), $item );
290
-		}, array() );
291
-
292
-		// Add the not found URIs.
293
-		$this->uri_to_post += array_fill_keys( $uris, null );
294
-
295
-		$this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." );
296
-
297
-	}
298
-
299
-	/**
300
-	 * Reset the URI to post local cache.
301
-	 *
302
-	 * @since 3.16.1
303
-	 */
304
-	public function reset_uris() {
305
-
306
-		$this->uri_to_post = array();
307
-
308
-	}
309
-
310
-	/**
311
-	 * Get all the URIs (item id and same as) related to a post.
312
-	 *
313
-	 * @since 3.16.1
314
-	 *
315
-	 * @param int $post_id The {@link WP_Post) id.
316
-	 *
317
-	 * @return array An array of URIs.
318
-	 */
319
-	public function get_uris( $post_id ) {
320
-
321
-		return get_post_meta( $post_id, WL_ENTITY_URL_META_NAME )
322
-			   + get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS );
323
-	}
324
-
325
-
326
-	/**
327
-	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
328
-	 *
329
-	 * @since 3.2.0
330
-	 *
331
-	 * @param string $uri The entity URI.
332
-	 *
333
-	 * @return WP_Post|null A WP_Post instance or null if not found.
334
-	 */
335
-	public function get_entity_post_by_uri( $uri ) {
336
-
337
-		$this->log->trace( "Getting an entity post for URI $uri..." );
338
-
339
-		// Check if we've been provided with a value otherwise return null.
340
-		if ( empty( $uri ) ) {
341
-			return null;
342
-		}
343
-
344
-		// If the URI is cached, return the cached post.
345
-		if ( array_key_exists( $uri, $this->uri_to_post ) ) {
346
-			$this->log->debug( "Returning cached post for $uri..." );
347
-
348
-			return $this->uri_to_post[ $uri ];
349
-		}
350
-
351
-		$this->log->debug( "Querying post for $uri..." );
352
-
353
-		$query_args = array(
354
-			// See https://github.com/insideout10/wordlift-plugin/issues/654.
355
-			'ignore_sticky_posts' => 1,
356
-			'posts_per_page'      => 1,
357
-			'post_status'         => 'any',
358
-			'post_type'           => Wordlift_Entity_Service::valid_entity_post_types(),
359
-			'meta_query'          => array(
360
-				array(
361
-					'key'     => WL_ENTITY_URL_META_NAME,
362
-					'value'   => $uri,
363
-					'compare' => '=',
364
-				),
365
-			),
366
-			// The following query can be avoided, it's superfluous since
367
-			// we're looking for a post with a specific Entity URI. This query
368
-			// may in fact consume up to 50% of the timing necessary to load a
369
-			// page.
370
-			//
371
-			// See https://github.com/insideout10/wordlift-plugin/issues/674.
372
-			//			'tax_query'           => array(
373
-			//				'relation' => 'AND',
374
-			//				array(
375
-			//					'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
376
-			//					'operator' => 'EXISTS',
377
-			//				),
378
-			//				array(
379
-			//					'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
380
-			//					'field'    => 'slug',
381
-			//					'terms'    => 'article',
382
-			//					'operator' => 'NOT IN',
383
-			//				),
384
-			//			),
385
-		);
386
-
387
-		// Only if the current uri is not an internal uri, entity search is
388
-		// performed also looking at sameAs values.
389
-		//
390
-		// This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
391
-		if ( ! $this->is_internal_uri( $uri ) ) {
392
-
393
-			$query_args['meta_query']['relation'] = 'OR';
394
-			$query_args['meta_query'][]           = array(
395
-				'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
396
-				'value'   => $uri,
397
-				'compare' => '=',
398
-			);
399
-		}
400
-
401
-		$posts = get_posts( $query_args );
402
-
403
-		// Return null if no post is found.
404
-		if ( 0 === count( $posts ) ) {
405
-			$this->log->warn( "No post for URI $uri." );
406
-
407
-			return null;
408
-		}
409
-
410
-		// Return the found post.
411
-		return current( $posts );
412
-	}
413
-
414
-	/**
415
-	 * Fires once a post has been saved. This function uses the $_REQUEST, therefore
416
-	 * we check that the post we're saving is the current post.
417
-	 *
418
-	 * @see   https://github.com/insideout10/wordlift-plugin/issues/363
419
-	 *
420
-	 * @since 3.2.0
421
-	 *
422
-	 * @param int     $post_id Post ID.
423
-	 * @param WP_Post $post    Post object.
424
-	 * @param bool    $update  Whether this is an existing post being updated or not.
425
-	 */
426
-	public function save_post( $post_id, $post, $update ) {
427
-
428
-		// Avoid doing anything if post is autosave or a revision.
429
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
430
-			return;
431
-		}
432
-
433
-		// We're setting the alternative label that have been provided via the UI
434
-		// (in fact we're using $_REQUEST), while save_post may be also called
435
-		// programmatically by some other function: we need to check therefore if
436
-		// the $post_id in the save_post call matches the post id set in the request.
437
-		//
438
-		// If this is not the current post being saved or if it's not an entity, return.
439
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
440
-			return;
441
-		}
442
-
443
-		// Get the alt labels from the request (or empty array).
444
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
445
-
446
-		// Set the alternative labels.
447
-		$this->set_alternative_labels( $post_id, $alt_labels );
448
-
449
-	}
450
-
451
-	/**
452
-	 * Set the alternative labels.
453
-	 *
454
-	 * @since 3.2.0
455
-	 *
456
-	 * @param int   $post_id    The post id.
457
-	 * @param array $alt_labels An array of labels.
458
-	 */
459
-	public function set_alternative_labels( $post_id, $alt_labels ) {
460
-
461
-		// Force $alt_labels to be an array
462
-		if ( ! is_array( $alt_labels ) ) {
463
-			$alt_labels = array( $alt_labels );
464
-		}
465
-
466
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
467
-
468
-		// Delete all the existing alternate labels.
469
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
470
-
471
-		// Set the alternative labels.
472
-		foreach ( $alt_labels as $alt_label ) {
473
-			if ( ! empty( $alt_label ) ) {
474
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
475
-			}
476
-		}
477
-
478
-	}
479
-
480
-	/**
481
-	 * Retrieve the alternate labels.
482
-	 *
483
-	 * @since 3.2.0
484
-	 *
485
-	 * @param int $post_id Post id.
486
-	 *
487
-	 * @return mixed An array  of alternative labels.
488
-	 */
489
-	public function get_alternative_labels( $post_id ) {
490
-
491
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
492
-	}
493
-
494
-	/**
495
-	 * Retrieve the labels for an entity, i.e. the title + the synonyms.
496
-	 *
497
-	 * @since 3.12.0
498
-	 *
499
-	 * @param int $post_id The entity {@link WP_Post} id.
500
-	 *
501
-	 * @return array An array with the entity title and labels.
502
-	 */
503
-	public function get_labels( $post_id ) {
504
-
505
-		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
506
-	}
507
-
508
-	/**
509
-	 * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
510
-	 *
511
-	 * @since 3.2.0
512
-	 *
513
-	 * @param WP_Post $post Post object.
514
-	 */
515
-	public function edit_form_before_permalink( $post ) {
516
-
517
-		// If it's not an entity, return.
518
-		if ( ! $this->is_entity( $post->ID ) ) {
519
-			return;
520
-		}
521
-
522
-		// Print the input template.
523
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
524
-
525
-		// Print all the currently set alternative labels.
526
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
527
-
528
-			echo $this->get_alternative_label_input( $alt_label );
529
-
530
-		};
531
-
532
-		// Print the button.
533
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
534
-
535
-	}
536
-
537
-	/**
538
-	 * Get the URI for the entity with the specified post id.
539
-	 *
540
-	 * @since 3.6.0
541
-	 *
542
-	 * @param int $post_id The entity post id.
543
-	 *
544
-	 * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
545
-	 */
546
-	public function get_uri( $post_id ) {
547
-
548
-		// If a null is given, nothing to do
549
-		if ( null == $post_id ) {
550
-			return null;
551
-		}
552
-
553
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
554
-
555
-		// If the dataset uri is not properly configured, null is returned
556
-		if ( '' === wl_configuration_get_redlink_dataset_uri() ) {
557
-			return null;
558
-		}
559
-
560
-		// Set the URI if it isn't set yet.
561
-		$post_status = get_post_status( $post_id );
562
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
563
-			$uri = wl_build_entity_uri( $post_id );
564
-			wl_set_entity_uri( $post_id, $uri );
565
-		}
566
-
567
-		return $uri;
568
-	}
569
-
570
-
571
-	/**
572
-	 * Get the alternative label input HTML code.
573
-	 *
574
-	 * @since 3.2.0
575
-	 *
576
-	 * @param string $value The input value.
577
-	 *
578
-	 * @return string The input HTML code.
579
-	 */
580
-	private function get_alternative_label_input( $value = '' ) {
581
-
582
-		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
583
-	}
584
-
585
-	/**
586
-	 * Get the number of entity posts published in this blog.
587
-	 *
588
-	 * @since 3.6.0
589
-	 *
590
-	 * @return int The number of published entity posts.
591
-	 */
592
-	public function count() {
593
-
594
-		$posts = get_posts( $this->add_criterias( array(
595
-			'post_status' => 'any',
596
-			'numberposts' => - 1,
597
-		) ) );
598
-
599
-		return count( $posts );
600
-	}
601
-
602
-	/**
603
-	 * Add the entity filtering criterias to the arguments for a `get_posts`
604
-	 * call.
605
-	 *
606
-	 * @since 3.15.0
607
-	 *
608
-	 * @param array $args The arguments for a `get_posts` call.
609
-	 *
610
-	 * @return array The arguments for a `get_posts` call.
611
-	 */
612
-	public static function add_criterias( $args ) {
613
-
614
-		// Build an optimal tax-query.
615
-		$tax_query = array(
616
-			'relation' => 'AND',
617
-			array(
618
-				'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
619
-				'operator' => 'EXISTS',
620
-			),
621
-			array(
622
-				'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
623
-				'field'    => 'slug',
624
-				'terms'    => 'article',
625
-				'operator' => 'NOT IN',
626
-			),
627
-		);
628
-
629
-		return $args + array(
630
-				'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
631
-				// Since 3.17.0: should this be faster?
632
-				'tax_query' => $tax_query,
633
-				//				'tax_query' => array(
634
-				//					array(
635
-				//						'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
636
-				//						'terms'    => self::get_entity_terms(),
637
-				//					),
638
-				//				),
639
-			);
640
-	}
71
+    /**
72
+     * A singleton instance of the Entity service.
73
+     *
74
+     * @since  3.2.0
75
+     * @access private
76
+     * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
77
+     */
78
+    private static $instance;
79
+
80
+    /**
81
+     * Create a Wordlift_Entity_Service instance.
82
+     *
83
+     * @since 3.2.0
84
+     *
85
+     * @param \Wordlift_UI_Service       $ui_service       The UI service.
86
+     * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
87
+     */
88
+    public function __construct( $ui_service, $relation_service ) {
89
+
90
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
91
+
92
+        $this->ui_service       = $ui_service;
93
+        $this->relation_service = $relation_service;
94
+
95
+        $this->reset_uris();
96
+
97
+        // Set the singleton instance.
98
+        self::$instance = $this;
99
+    }
100
+
101
+    /**
102
+     * Get the singleton instance of the Entity service.
103
+     *
104
+     * @since 3.2.0
105
+     * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
106
+     */
107
+    public static function get_instance() {
108
+
109
+        return self::$instance;
110
+    }
111
+
112
+    /**
113
+     * Determines whether a post is an entity or not. Entity is in this context
114
+     * something which is not an article.
115
+     *
116
+     * @since 3.1.0
117
+     *
118
+     * @param int $post_id A post id.
119
+     *
120
+     * @return bool Return true if the post is an entity otherwise false.
121
+     */
122
+    public function is_entity( $post_id ) {
123
+
124
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME );
125
+
126
+        if ( 0 === count( $terms ) ) {
127
+            return false;
128
+        }
129
+
130
+        // We don't consider an `article` to be an entity.
131
+        if ( 'article' !== $terms[0]->slug ) {
132
+            return true;
133
+        }
134
+
135
+        return false;
136
+    }
137
+
138
+    /**
139
+     * Get the proper classification scope for a given entity post
140
+     *
141
+     * @since 3.5.0
142
+     *
143
+     * @param integer $post_id An entity post id.
144
+     *
145
+     * @param string  $default The default classification scope, `what` if not
146
+     *                         provided.
147
+     *
148
+     * @return string Returns a classification scope (e.g. 'what').
149
+     */
150
+    public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
151
+
152
+        if ( false === $this->is_entity( $post_id ) ) {
153
+            return $default;
154
+        }
155
+
156
+        // Retrieve the entity type
157
+        $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
158
+        $entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
159
+        // Retrieve classification boxes configuration
160
+        $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
161
+        foreach ( $classification_boxes as $cb ) {
162
+            if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
163
+                return $cb['id'];
164
+            }
165
+        }
166
+
167
+        return $default;
168
+    }
169
+
170
+    /**
171
+     * Check whether a {@link WP_Post} is used.
172
+     *
173
+     * @param int $post_id The {@link WP_Post}'s id.
174
+     *
175
+     * @return bool|null Null if it's not an entity, otherwise true if it's used.
176
+     */
177
+    public function is_used( $post_id ) {
178
+
179
+        if ( false === $this->is_entity( $post_id ) ) {
180
+            return null;
181
+        }
182
+        // Retrieve the post
183
+        $entity = get_post( $post_id );
184
+
185
+        global $wpdb;
186
+        // Retrieve Wordlift relation instances table name
187
+        $table_name = wl_core_get_relation_instances_table_name();
188
+
189
+        // Check is it's referenced / related to another post / entity
190
+        $stmt = $wpdb->prepare(
191
+            "SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
192
+            $entity->ID
193
+        );
194
+
195
+        // Perform the query
196
+        $relation_instances = (int) $wpdb->get_var( $stmt );
197
+        // If there is at least one relation instance for the current entity, then it's used
198
+        if ( 0 < $relation_instances ) {
199
+            return true;
200
+        }
201
+
202
+        // Check if the entity uri is used as meta_value
203
+        $stmt = $wpdb->prepare(
204
+            "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
205
+            $entity->ID,
206
+            wl_get_entity_uri( $entity->ID )
207
+        );
208
+        // Perform the query
209
+        $meta_instances = (int) $wpdb->get_var( $stmt );
210
+
211
+        // If there is at least one meta that refers the current entity uri, then current entity is used
212
+        if ( 0 < $meta_instances ) {
213
+            return true;
214
+        }
215
+
216
+        // If we are here, it means the current entity is not used at the moment
217
+        return false;
218
+    }
219
+
220
+    /**
221
+     * Determines whether a given uri is an internal uri or not.
222
+     *
223
+     * @since 3.3.2
224
+     *
225
+     * @param int $uri An uri.
226
+     *
227
+     * @return true if the uri internal to the current dataset otherwise false.
228
+     */
229
+    public function is_internal_uri( $uri ) {
230
+
231
+        return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) );
232
+    }
233
+
234
+    /**
235
+     * Preload the provided URIs in the local cache.
236
+     *
237
+     * This function will populate the local `$uri_to_post` array by running a
238
+     * single query with all the URIs and returning the mappings in the array.
239
+     *
240
+     * @since 3.16.1
241
+     *
242
+     * @param array $uris An array of URIs.
243
+     */
244
+    public function preload_uris( $uris ) {
245
+
246
+        $that          = $this;
247
+        $external_uris = array_filter( $uris, function ( $item ) use ( $that ) {
248
+            return ! $that->is_internal_uri( $item );
249
+        } );
250
+
251
+        $query_args = array(
252
+            // See https://github.com/insideout10/wordlift-plugin/issues/654.
253
+            'ignore_sticky_posts' => 1,
254
+            'numberposts'         => - 1,
255
+            'post_status'         => 'any',
256
+            'post_type'           => Wordlift_Entity_Service::valid_entity_post_types(),
257
+            'meta_query'          => array(
258
+                array(
259
+                    'key'     => WL_ENTITY_URL_META_NAME,
260
+                    'value'   => $uris,
261
+                    'compare' => 'IN',
262
+                ),
263
+            ),
264
+        );
265
+
266
+        // Only if the current uri is not an internal uri, entity search is
267
+        // performed also looking at sameAs values.
268
+        //
269
+        // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
270
+        if ( 0 < count( $external_uris ) ) {
271
+
272
+            $query_args['meta_query']['relation'] = 'OR';
273
+            $query_args['meta_query'][]           = array(
274
+                'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
275
+                'value'   => $external_uris,
276
+                'compare' => 'IN',
277
+            );
278
+
279
+        }
280
+
281
+        // Get the posts.
282
+        $posts = get_posts( $query_args );
283
+
284
+        // Populate the array. We reinitialize the array on purpose because
285
+        // we don't want these data to long live.
286
+        $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) {
287
+            return $carry
288
+                    // Get the URI related to the post and fill them with the item id.
289
+                   + array_fill_keys( $that->get_uris( $item->ID ), $item );
290
+        }, array() );
291
+
292
+        // Add the not found URIs.
293
+        $this->uri_to_post += array_fill_keys( $uris, null );
294
+
295
+        $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." );
296
+
297
+    }
298
+
299
+    /**
300
+     * Reset the URI to post local cache.
301
+     *
302
+     * @since 3.16.1
303
+     */
304
+    public function reset_uris() {
305
+
306
+        $this->uri_to_post = array();
307
+
308
+    }
309
+
310
+    /**
311
+     * Get all the URIs (item id and same as) related to a post.
312
+     *
313
+     * @since 3.16.1
314
+     *
315
+     * @param int $post_id The {@link WP_Post) id.
316
+     *
317
+     * @return array An array of URIs.
318
+     */
319
+    public function get_uris( $post_id ) {
320
+
321
+        return get_post_meta( $post_id, WL_ENTITY_URL_META_NAME )
322
+               + get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS );
323
+    }
324
+
325
+
326
+    /**
327
+     * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
328
+     *
329
+     * @since 3.2.0
330
+     *
331
+     * @param string $uri The entity URI.
332
+     *
333
+     * @return WP_Post|null A WP_Post instance or null if not found.
334
+     */
335
+    public function get_entity_post_by_uri( $uri ) {
336
+
337
+        $this->log->trace( "Getting an entity post for URI $uri..." );
338
+
339
+        // Check if we've been provided with a value otherwise return null.
340
+        if ( empty( $uri ) ) {
341
+            return null;
342
+        }
343
+
344
+        // If the URI is cached, return the cached post.
345
+        if ( array_key_exists( $uri, $this->uri_to_post ) ) {
346
+            $this->log->debug( "Returning cached post for $uri..." );
347
+
348
+            return $this->uri_to_post[ $uri ];
349
+        }
350
+
351
+        $this->log->debug( "Querying post for $uri..." );
352
+
353
+        $query_args = array(
354
+            // See https://github.com/insideout10/wordlift-plugin/issues/654.
355
+            'ignore_sticky_posts' => 1,
356
+            'posts_per_page'      => 1,
357
+            'post_status'         => 'any',
358
+            'post_type'           => Wordlift_Entity_Service::valid_entity_post_types(),
359
+            'meta_query'          => array(
360
+                array(
361
+                    'key'     => WL_ENTITY_URL_META_NAME,
362
+                    'value'   => $uri,
363
+                    'compare' => '=',
364
+                ),
365
+            ),
366
+            // The following query can be avoided, it's superfluous since
367
+            // we're looking for a post with a specific Entity URI. This query
368
+            // may in fact consume up to 50% of the timing necessary to load a
369
+            // page.
370
+            //
371
+            // See https://github.com/insideout10/wordlift-plugin/issues/674.
372
+            //			'tax_query'           => array(
373
+            //				'relation' => 'AND',
374
+            //				array(
375
+            //					'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
376
+            //					'operator' => 'EXISTS',
377
+            //				),
378
+            //				array(
379
+            //					'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
380
+            //					'field'    => 'slug',
381
+            //					'terms'    => 'article',
382
+            //					'operator' => 'NOT IN',
383
+            //				),
384
+            //			),
385
+        );
386
+
387
+        // Only if the current uri is not an internal uri, entity search is
388
+        // performed also looking at sameAs values.
389
+        //
390
+        // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
391
+        if ( ! $this->is_internal_uri( $uri ) ) {
392
+
393
+            $query_args['meta_query']['relation'] = 'OR';
394
+            $query_args['meta_query'][]           = array(
395
+                'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
396
+                'value'   => $uri,
397
+                'compare' => '=',
398
+            );
399
+        }
400
+
401
+        $posts = get_posts( $query_args );
402
+
403
+        // Return null if no post is found.
404
+        if ( 0 === count( $posts ) ) {
405
+            $this->log->warn( "No post for URI $uri." );
406
+
407
+            return null;
408
+        }
409
+
410
+        // Return the found post.
411
+        return current( $posts );
412
+    }
413
+
414
+    /**
415
+     * Fires once a post has been saved. This function uses the $_REQUEST, therefore
416
+     * we check that the post we're saving is the current post.
417
+     *
418
+     * @see   https://github.com/insideout10/wordlift-plugin/issues/363
419
+     *
420
+     * @since 3.2.0
421
+     *
422
+     * @param int     $post_id Post ID.
423
+     * @param WP_Post $post    Post object.
424
+     * @param bool    $update  Whether this is an existing post being updated or not.
425
+     */
426
+    public function save_post( $post_id, $post, $update ) {
427
+
428
+        // Avoid doing anything if post is autosave or a revision.
429
+        if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
430
+            return;
431
+        }
432
+
433
+        // We're setting the alternative label that have been provided via the UI
434
+        // (in fact we're using $_REQUEST), while save_post may be also called
435
+        // programmatically by some other function: we need to check therefore if
436
+        // the $post_id in the save_post call matches the post id set in the request.
437
+        //
438
+        // If this is not the current post being saved or if it's not an entity, return.
439
+        if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
440
+            return;
441
+        }
442
+
443
+        // Get the alt labels from the request (or empty array).
444
+        $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
445
+
446
+        // Set the alternative labels.
447
+        $this->set_alternative_labels( $post_id, $alt_labels );
448
+
449
+    }
450
+
451
+    /**
452
+     * Set the alternative labels.
453
+     *
454
+     * @since 3.2.0
455
+     *
456
+     * @param int   $post_id    The post id.
457
+     * @param array $alt_labels An array of labels.
458
+     */
459
+    public function set_alternative_labels( $post_id, $alt_labels ) {
460
+
461
+        // Force $alt_labels to be an array
462
+        if ( ! is_array( $alt_labels ) ) {
463
+            $alt_labels = array( $alt_labels );
464
+        }
465
+
466
+        $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
467
+
468
+        // Delete all the existing alternate labels.
469
+        delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
470
+
471
+        // Set the alternative labels.
472
+        foreach ( $alt_labels as $alt_label ) {
473
+            if ( ! empty( $alt_label ) ) {
474
+                add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
475
+            }
476
+        }
477
+
478
+    }
479
+
480
+    /**
481
+     * Retrieve the alternate labels.
482
+     *
483
+     * @since 3.2.0
484
+     *
485
+     * @param int $post_id Post id.
486
+     *
487
+     * @return mixed An array  of alternative labels.
488
+     */
489
+    public function get_alternative_labels( $post_id ) {
490
+
491
+        return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
492
+    }
493
+
494
+    /**
495
+     * Retrieve the labels for an entity, i.e. the title + the synonyms.
496
+     *
497
+     * @since 3.12.0
498
+     *
499
+     * @param int $post_id The entity {@link WP_Post} id.
500
+     *
501
+     * @return array An array with the entity title and labels.
502
+     */
503
+    public function get_labels( $post_id ) {
504
+
505
+        return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
506
+    }
507
+
508
+    /**
509
+     * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
510
+     *
511
+     * @since 3.2.0
512
+     *
513
+     * @param WP_Post $post Post object.
514
+     */
515
+    public function edit_form_before_permalink( $post ) {
516
+
517
+        // If it's not an entity, return.
518
+        if ( ! $this->is_entity( $post->ID ) ) {
519
+            return;
520
+        }
521
+
522
+        // Print the input template.
523
+        $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
524
+
525
+        // Print all the currently set alternative labels.
526
+        foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
527
+
528
+            echo $this->get_alternative_label_input( $alt_label );
529
+
530
+        };
531
+
532
+        // Print the button.
533
+        $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
534
+
535
+    }
536
+
537
+    /**
538
+     * Get the URI for the entity with the specified post id.
539
+     *
540
+     * @since 3.6.0
541
+     *
542
+     * @param int $post_id The entity post id.
543
+     *
544
+     * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
545
+     */
546
+    public function get_uri( $post_id ) {
547
+
548
+        // If a null is given, nothing to do
549
+        if ( null == $post_id ) {
550
+            return null;
551
+        }
552
+
553
+        $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
554
+
555
+        // If the dataset uri is not properly configured, null is returned
556
+        if ( '' === wl_configuration_get_redlink_dataset_uri() ) {
557
+            return null;
558
+        }
559
+
560
+        // Set the URI if it isn't set yet.
561
+        $post_status = get_post_status( $post_id );
562
+        if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
563
+            $uri = wl_build_entity_uri( $post_id );
564
+            wl_set_entity_uri( $post_id, $uri );
565
+        }
566
+
567
+        return $uri;
568
+    }
569
+
570
+
571
+    /**
572
+     * Get the alternative label input HTML code.
573
+     *
574
+     * @since 3.2.0
575
+     *
576
+     * @param string $value The input value.
577
+     *
578
+     * @return string The input HTML code.
579
+     */
580
+    private function get_alternative_label_input( $value = '' ) {
581
+
582
+        return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
583
+    }
584
+
585
+    /**
586
+     * Get the number of entity posts published in this blog.
587
+     *
588
+     * @since 3.6.0
589
+     *
590
+     * @return int The number of published entity posts.
591
+     */
592
+    public function count() {
593
+
594
+        $posts = get_posts( $this->add_criterias( array(
595
+            'post_status' => 'any',
596
+            'numberposts' => - 1,
597
+        ) ) );
598
+
599
+        return count( $posts );
600
+    }
601
+
602
+    /**
603
+     * Add the entity filtering criterias to the arguments for a `get_posts`
604
+     * call.
605
+     *
606
+     * @since 3.15.0
607
+     *
608
+     * @param array $args The arguments for a `get_posts` call.
609
+     *
610
+     * @return array The arguments for a `get_posts` call.
611
+     */
612
+    public static function add_criterias( $args ) {
613
+
614
+        // Build an optimal tax-query.
615
+        $tax_query = array(
616
+            'relation' => 'AND',
617
+            array(
618
+                'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
619
+                'operator' => 'EXISTS',
620
+            ),
621
+            array(
622
+                'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
623
+                'field'    => 'slug',
624
+                'terms'    => 'article',
625
+                'operator' => 'NOT IN',
626
+            ),
627
+        );
628
+
629
+        return $args + array(
630
+                'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
631
+                // Since 3.17.0: should this be faster?
632
+                'tax_query' => $tax_query,
633
+                //				'tax_query' => array(
634
+                //					array(
635
+                //						'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
636
+                //						'terms'    => self::get_entity_terms(),
637
+                //					),
638
+                //				),
639
+            );
640
+    }
641 641
 
642 642
 //	/**
643 643
 //	 * Get the entity terms IDs which represent an entity.
@@ -667,97 +667,97 @@  discard block
 block discarded – undo
667 667
 //		} ) );
668 668
 //	}
669 669
 
670
-	/**
671
-	 * Create a new entity.
672
-	 *
673
-	 * @since 3.9.0
674
-	 *
675
-	 * @param string $name     The entity name.
676
-	 * @param string $type_uri The entity's type URI.
677
-	 * @param null   $logo     The entity logo id (or NULL if none).
678
-	 * @param string $status   The post status, by default 'publish'.
679
-	 *
680
-	 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
681
-	 */
682
-	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
683
-
684
-		// Create an entity for the publisher.
685
-		$post_id = wp_insert_post( array(
686
-			'post_type'    => self::TYPE_NAME,
687
-			'post_title'   => $name,
688
-			'post_status'  => $status,
689
-			'post_content' => '',
690
-		) );
691
-
692
-		// Return the error if any.
693
-		if ( is_wp_error( $post_id ) ) {
694
-			return $post_id;
695
-		}
696
-
697
-		// Set the entity logo.
698
-		if ( $logo && is_numeric( $logo ) ) {
699
-			set_post_thumbnail( $post_id, $logo );
700
-		}
701
-
702
-		// Set the entity type.
703
-		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
704
-
705
-		return $post_id;
706
-	}
707
-
708
-	/**
709
-	 * Get the entities related to the one with the specified id. By default only
710
-	 * published entities will be returned.
711
-	 *
712
-	 * @since 3.10.0
713
-	 *
714
-	 * @param int    $id          The post id.
715
-	 * @param string $post_status The target post status (default = publish).
716
-	 *
717
-	 * @return array An array of post ids.
718
-	 */
719
-	public function get_related_entities( $id, $post_status = 'publish' ) {
720
-
721
-		return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
722
-	}
723
-
724
-	/**
725
-	 * Get the list of entities.
726
-	 *
727
-	 * @since 3.12.2
728
-	 *
729
-	 * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
730
-	 *
731
-	 * @return array An array of entity posts.
732
-	 */
733
-	public function get( $params = array() ) {
734
-
735
-		// Set the defaults.
736
-		$defaults = array( 'post_type' => 'entity' );
737
-
738
-		// Merge the defaults with the provided parameters.
739
-		$args = wp_parse_args( $params, $defaults );
740
-
741
-		// Call the `get_posts` function.
742
-		return get_posts( $args );
743
-	}
744
-
745
-	/**
746
-	 * The list of post type names which can be used for entities
747
-	 *
748
-	 * Criteria is that the post type is public. The list of valid post types
749
-	 * can be overridden with a filter.
750
-	 *
751
-	 * @since 3.15.0
752
-	 *
753
-	 * @return array Array containing the names of the valid post types.
754
-	 */
755
-	static function valid_entity_post_types() {
756
-
757
-		// Ignore builtins in the call to avoid getting attachments.
758
-		$post_types = array( 'post', 'page', self::TYPE_NAME );
759
-
760
-		return apply_filters( 'wl_valid_entity_post_types', $post_types );
761
-	}
670
+    /**
671
+     * Create a new entity.
672
+     *
673
+     * @since 3.9.0
674
+     *
675
+     * @param string $name     The entity name.
676
+     * @param string $type_uri The entity's type URI.
677
+     * @param null   $logo     The entity logo id (or NULL if none).
678
+     * @param string $status   The post status, by default 'publish'.
679
+     *
680
+     * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
681
+     */
682
+    public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
683
+
684
+        // Create an entity for the publisher.
685
+        $post_id = wp_insert_post( array(
686
+            'post_type'    => self::TYPE_NAME,
687
+            'post_title'   => $name,
688
+            'post_status'  => $status,
689
+            'post_content' => '',
690
+        ) );
691
+
692
+        // Return the error if any.
693
+        if ( is_wp_error( $post_id ) ) {
694
+            return $post_id;
695
+        }
696
+
697
+        // Set the entity logo.
698
+        if ( $logo && is_numeric( $logo ) ) {
699
+            set_post_thumbnail( $post_id, $logo );
700
+        }
701
+
702
+        // Set the entity type.
703
+        Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
704
+
705
+        return $post_id;
706
+    }
707
+
708
+    /**
709
+     * Get the entities related to the one with the specified id. By default only
710
+     * published entities will be returned.
711
+     *
712
+     * @since 3.10.0
713
+     *
714
+     * @param int    $id          The post id.
715
+     * @param string $post_status The target post status (default = publish).
716
+     *
717
+     * @return array An array of post ids.
718
+     */
719
+    public function get_related_entities( $id, $post_status = 'publish' ) {
720
+
721
+        return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
722
+    }
723
+
724
+    /**
725
+     * Get the list of entities.
726
+     *
727
+     * @since 3.12.2
728
+     *
729
+     * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
730
+     *
731
+     * @return array An array of entity posts.
732
+     */
733
+    public function get( $params = array() ) {
734
+
735
+        // Set the defaults.
736
+        $defaults = array( 'post_type' => 'entity' );
737
+
738
+        // Merge the defaults with the provided parameters.
739
+        $args = wp_parse_args( $params, $defaults );
740
+
741
+        // Call the `get_posts` function.
742
+        return get_posts( $args );
743
+    }
744
+
745
+    /**
746
+     * The list of post type names which can be used for entities
747
+     *
748
+     * Criteria is that the post type is public. The list of valid post types
749
+     * can be overridden with a filter.
750
+     *
751
+     * @since 3.15.0
752
+     *
753
+     * @return array Array containing the names of the valid post types.
754
+     */
755
+    static function valid_entity_post_types() {
756
+
757
+        // Ignore builtins in the call to avoid getting attachments.
758
+        $post_types = array( 'post', 'page', self::TYPE_NAME );
759
+
760
+        return apply_filters( 'wl_valid_entity_post_types', $post_types );
761
+    }
762 762
 
763 763
 }
Please login to merge, or discard this patch.
Spacing   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -85,9 +85,9 @@  discard block
 block discarded – undo
85 85
 	 * @param \Wordlift_UI_Service       $ui_service       The UI service.
86 86
 	 * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
87 87
 	 */
88
-	public function __construct( $ui_service, $relation_service ) {
88
+	public function __construct($ui_service, $relation_service) {
89 89
 
90
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
90
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service');
91 91
 
92 92
 		$this->ui_service       = $ui_service;
93 93
 		$this->relation_service = $relation_service;
@@ -119,16 +119,16 @@  discard block
 block discarded – undo
119 119
 	 *
120 120
 	 * @return bool Return true if the post is an entity otherwise false.
121 121
 	 */
122
-	public function is_entity( $post_id ) {
122
+	public function is_entity($post_id) {
123 123
 
124
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME );
124
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME);
125 125
 
126
-		if ( 0 === count( $terms ) ) {
126
+		if (0 === count($terms)) {
127 127
 			return false;
128 128
 		}
129 129
 
130 130
 		// We don't consider an `article` to be an entity.
131
-		if ( 'article' !== $terms[0]->slug ) {
131
+		if ('article' !== $terms[0]->slug) {
132 132
 			return true;
133 133
 		}
134 134
 
@@ -147,19 +147,19 @@  discard block
 block discarded – undo
147 147
 	 *
148 148
 	 * @return string Returns a classification scope (e.g. 'what').
149 149
 	 */
150
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
150
+	public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) {
151 151
 
152
-		if ( false === $this->is_entity( $post_id ) ) {
152
+		if (false === $this->is_entity($post_id)) {
153 153
 			return $default;
154 154
 		}
155 155
 
156 156
 		// Retrieve the entity type
157
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
158
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
157
+		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id);
158
+		$entity_type     = str_replace('wl-', '', $entity_type_arr['css_class']);
159 159
 		// Retrieve classification boxes configuration
160
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
161
-		foreach ( $classification_boxes as $cb ) {
162
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
160
+		$classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
161
+		foreach ($classification_boxes as $cb) {
162
+			if (in_array($entity_type, $cb['registeredTypes'])) {
163 163
 				return $cb['id'];
164 164
 			}
165 165
 		}
@@ -174,13 +174,13 @@  discard block
 block discarded – undo
174 174
 	 *
175 175
 	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
176 176
 	 */
177
-	public function is_used( $post_id ) {
177
+	public function is_used($post_id) {
178 178
 
179
-		if ( false === $this->is_entity( $post_id ) ) {
179
+		if (false === $this->is_entity($post_id)) {
180 180
 			return null;
181 181
 		}
182 182
 		// Retrieve the post
183
-		$entity = get_post( $post_id );
183
+		$entity = get_post($post_id);
184 184
 
185 185
 		global $wpdb;
186 186
 		// Retrieve Wordlift relation instances table name
@@ -193,9 +193,9 @@  discard block
 block discarded – undo
193 193
 		);
194 194
 
195 195
 		// Perform the query
196
-		$relation_instances = (int) $wpdb->get_var( $stmt );
196
+		$relation_instances = (int) $wpdb->get_var($stmt);
197 197
 		// If there is at least one relation instance for the current entity, then it's used
198
-		if ( 0 < $relation_instances ) {
198
+		if (0 < $relation_instances) {
199 199
 			return true;
200 200
 		}
201 201
 
@@ -203,13 +203,13 @@  discard block
 block discarded – undo
203 203
 		$stmt = $wpdb->prepare(
204 204
 			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
205 205
 			$entity->ID,
206
-			wl_get_entity_uri( $entity->ID )
206
+			wl_get_entity_uri($entity->ID)
207 207
 		);
208 208
 		// Perform the query
209
-		$meta_instances = (int) $wpdb->get_var( $stmt );
209
+		$meta_instances = (int) $wpdb->get_var($stmt);
210 210
 
211 211
 		// If there is at least one meta that refers the current entity uri, then current entity is used
212
-		if ( 0 < $meta_instances ) {
212
+		if (0 < $meta_instances) {
213 213
 			return true;
214 214
 		}
215 215
 
@@ -226,9 +226,9 @@  discard block
 block discarded – undo
226 226
 	 *
227 227
 	 * @return true if the uri internal to the current dataset otherwise false.
228 228
 	 */
229
-	public function is_internal_uri( $uri ) {
229
+	public function is_internal_uri($uri) {
230 230
 
231
-		return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) );
231
+		return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri()));
232 232
 	}
233 233
 
234 234
 	/**
@@ -241,17 +241,17 @@  discard block
 block discarded – undo
241 241
 	 *
242 242
 	 * @param array $uris An array of URIs.
243 243
 	 */
244
-	public function preload_uris( $uris ) {
244
+	public function preload_uris($uris) {
245 245
 
246 246
 		$that          = $this;
247
-		$external_uris = array_filter( $uris, function ( $item ) use ( $that ) {
248
-			return ! $that->is_internal_uri( $item );
247
+		$external_uris = array_filter($uris, function($item) use ($that) {
248
+			return ! $that->is_internal_uri($item);
249 249
 		} );
250 250
 
251 251
 		$query_args = array(
252 252
 			// See https://github.com/insideout10/wordlift-plugin/issues/654.
253 253
 			'ignore_sticky_posts' => 1,
254
-			'numberposts'         => - 1,
254
+			'numberposts'         => -1,
255 255
 			'post_status'         => 'any',
256 256
 			'post_type'           => Wordlift_Entity_Service::valid_entity_post_types(),
257 257
 			'meta_query'          => array(
@@ -267,7 +267,7 @@  discard block
 block discarded – undo
267 267
 		// performed also looking at sameAs values.
268 268
 		//
269 269
 		// This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
270
-		if ( 0 < count( $external_uris ) ) {
270
+		if (0 < count($external_uris)) {
271 271
 
272 272
 			$query_args['meta_query']['relation'] = 'OR';
273 273
 			$query_args['meta_query'][]           = array(
@@ -279,20 +279,20 @@  discard block
 block discarded – undo
279 279
 		}
280 280
 
281 281
 		// Get the posts.
282
-		$posts = get_posts( $query_args );
282
+		$posts = get_posts($query_args);
283 283
 
284 284
 		// Populate the array. We reinitialize the array on purpose because
285 285
 		// we don't want these data to long live.
286
-		$this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) {
286
+		$this->uri_to_post = array_reduce($posts, function($carry, $item) use ($that) {
287 287
 			return $carry
288 288
 				   // Get the URI related to the post and fill them with the item id.
289
-				   + array_fill_keys( $that->get_uris( $item->ID ), $item );
290
-		}, array() );
289
+				   + array_fill_keys($that->get_uris($item->ID), $item);
290
+		}, array());
291 291
 
292 292
 		// Add the not found URIs.
293
-		$this->uri_to_post += array_fill_keys( $uris, null );
293
+		$this->uri_to_post += array_fill_keys($uris, null);
294 294
 
295
-		$this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." );
295
+		$this->log->debug(count($this->uri_to_post)." URI(s) preloaded.");
296 296
 
297 297
 	}
298 298
 
@@ -316,10 +316,10 @@  discard block
 block discarded – undo
316 316
 	 *
317 317
 	 * @return array An array of URIs.
318 318
 	 */
319
-	public function get_uris( $post_id ) {
319
+	public function get_uris($post_id) {
320 320
 
321
-		return get_post_meta( $post_id, WL_ENTITY_URL_META_NAME )
322
-			   + get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS );
321
+		return get_post_meta($post_id, WL_ENTITY_URL_META_NAME)
322
+			   + get_post_meta($post_id, Wordlift_Schema_Service::FIELD_SAME_AS);
323 323
 	}
324 324
 
325 325
 
@@ -332,23 +332,23 @@  discard block
 block discarded – undo
332 332
 	 *
333 333
 	 * @return WP_Post|null A WP_Post instance or null if not found.
334 334
 	 */
335
-	public function get_entity_post_by_uri( $uri ) {
335
+	public function get_entity_post_by_uri($uri) {
336 336
 
337
-		$this->log->trace( "Getting an entity post for URI $uri..." );
337
+		$this->log->trace("Getting an entity post for URI $uri...");
338 338
 
339 339
 		// Check if we've been provided with a value otherwise return null.
340
-		if ( empty( $uri ) ) {
340
+		if (empty($uri)) {
341 341
 			return null;
342 342
 		}
343 343
 
344 344
 		// If the URI is cached, return the cached post.
345
-		if ( array_key_exists( $uri, $this->uri_to_post ) ) {
346
-			$this->log->debug( "Returning cached post for $uri..." );
345
+		if (array_key_exists($uri, $this->uri_to_post)) {
346
+			$this->log->debug("Returning cached post for $uri...");
347 347
 
348
-			return $this->uri_to_post[ $uri ];
348
+			return $this->uri_to_post[$uri];
349 349
 		}
350 350
 
351
-		$this->log->debug( "Querying post for $uri..." );
351
+		$this->log->debug("Querying post for $uri...");
352 352
 
353 353
 		$query_args = array(
354 354
 			// See https://github.com/insideout10/wordlift-plugin/issues/654.
@@ -388,7 +388,7 @@  discard block
 block discarded – undo
388 388
 		// performed also looking at sameAs values.
389 389
 		//
390 390
 		// This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
391
-		if ( ! $this->is_internal_uri( $uri ) ) {
391
+		if ( ! $this->is_internal_uri($uri)) {
392 392
 
393 393
 			$query_args['meta_query']['relation'] = 'OR';
394 394
 			$query_args['meta_query'][]           = array(
@@ -398,17 +398,17 @@  discard block
 block discarded – undo
398 398
 			);
399 399
 		}
400 400
 
401
-		$posts = get_posts( $query_args );
401
+		$posts = get_posts($query_args);
402 402
 
403 403
 		// Return null if no post is found.
404
-		if ( 0 === count( $posts ) ) {
405
-			$this->log->warn( "No post for URI $uri." );
404
+		if (0 === count($posts)) {
405
+			$this->log->warn("No post for URI $uri.");
406 406
 
407 407
 			return null;
408 408
 		}
409 409
 
410 410
 		// Return the found post.
411
-		return current( $posts );
411
+		return current($posts);
412 412
 	}
413 413
 
414 414
 	/**
@@ -423,10 +423,10 @@  discard block
 block discarded – undo
423 423
 	 * @param WP_Post $post    Post object.
424 424
 	 * @param bool    $update  Whether this is an existing post being updated or not.
425 425
 	 */
426
-	public function save_post( $post_id, $post, $update ) {
426
+	public function save_post($post_id, $post, $update) {
427 427
 
428 428
 		// Avoid doing anything if post is autosave or a revision.
429
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
429
+		if (wp_is_post_autosave($post) || wp_is_post_revision($post)) {
430 430
 			return;
431 431
 		}
432 432
 
@@ -436,15 +436,15 @@  discard block
 block discarded – undo
436 436
 		// the $post_id in the save_post call matches the post id set in the request.
437 437
 		//
438 438
 		// If this is not the current post being saved or if it's not an entity, return.
439
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
439
+		if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) {
440 440
 			return;
441 441
 		}
442 442
 
443 443
 		// Get the alt labels from the request (or empty array).
444
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
444
+		$alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array();
445 445
 
446 446
 		// Set the alternative labels.
447
-		$this->set_alternative_labels( $post_id, $alt_labels );
447
+		$this->set_alternative_labels($post_id, $alt_labels);
448 448
 
449 449
 	}
450 450
 
@@ -456,22 +456,22 @@  discard block
 block discarded – undo
456 456
 	 * @param int   $post_id    The post id.
457 457
 	 * @param array $alt_labels An array of labels.
458 458
 	 */
459
-	public function set_alternative_labels( $post_id, $alt_labels ) {
459
+	public function set_alternative_labels($post_id, $alt_labels) {
460 460
 
461 461
 		// Force $alt_labels to be an array
462
-		if ( ! is_array( $alt_labels ) ) {
463
-			$alt_labels = array( $alt_labels );
462
+		if ( ! is_array($alt_labels)) {
463
+			$alt_labels = array($alt_labels);
464 464
 		}
465 465
 
466
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
466
+		$this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]");
467 467
 
468 468
 		// Delete all the existing alternate labels.
469
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
469
+		delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
470 470
 
471 471
 		// Set the alternative labels.
472
-		foreach ( $alt_labels as $alt_label ) {
473
-			if ( ! empty( $alt_label ) ) {
474
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
472
+		foreach ($alt_labels as $alt_label) {
473
+			if ( ! empty($alt_label)) {
474
+				add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label);
475 475
 			}
476 476
 		}
477 477
 
@@ -486,9 +486,9 @@  discard block
 block discarded – undo
486 486
 	 *
487 487
 	 * @return mixed An array  of alternative labels.
488 488
 	 */
489
-	public function get_alternative_labels( $post_id ) {
489
+	public function get_alternative_labels($post_id) {
490 490
 
491
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
491
+		return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
492 492
 	}
493 493
 
494 494
 	/**
@@ -500,9 +500,9 @@  discard block
 block discarded – undo
500 500
 	 *
501 501
 	 * @return array An array with the entity title and labels.
502 502
 	 */
503
-	public function get_labels( $post_id ) {
503
+	public function get_labels($post_id) {
504 504
 
505
-		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
505
+		return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id));
506 506
 	}
507 507
 
508 508
 	/**
@@ -512,25 +512,25 @@  discard block
 block discarded – undo
512 512
 	 *
513 513
 	 * @param WP_Post $post Post object.
514 514
 	 */
515
-	public function edit_form_before_permalink( $post ) {
515
+	public function edit_form_before_permalink($post) {
516 516
 
517 517
 		// If it's not an entity, return.
518
-		if ( ! $this->is_entity( $post->ID ) ) {
518
+		if ( ! $this->is_entity($post->ID)) {
519 519
 			return;
520 520
 		}
521 521
 
522 522
 		// Print the input template.
523
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
523
+		$this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input());
524 524
 
525 525
 		// Print all the currently set alternative labels.
526
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
526
+		foreach ($this->get_alternative_labels($post->ID) as $alt_label) {
527 527
 
528
-			echo $this->get_alternative_label_input( $alt_label );
528
+			echo $this->get_alternative_label_input($alt_label);
529 529
 
530 530
 		};
531 531
 
532 532
 		// Print the button.
533
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
533
+		$this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift'));
534 534
 
535 535
 	}
536 536
 
@@ -543,25 +543,25 @@  discard block
 block discarded – undo
543 543
 	 *
544 544
 	 * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
545 545
 	 */
546
-	public function get_uri( $post_id ) {
546
+	public function get_uri($post_id) {
547 547
 
548 548
 		// If a null is given, nothing to do
549
-		if ( null == $post_id ) {
549
+		if (null == $post_id) {
550 550
 			return null;
551 551
 		}
552 552
 
553
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
553
+		$uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true);
554 554
 
555 555
 		// If the dataset uri is not properly configured, null is returned
556
-		if ( '' === wl_configuration_get_redlink_dataset_uri() ) {
556
+		if ('' === wl_configuration_get_redlink_dataset_uri()) {
557 557
 			return null;
558 558
 		}
559 559
 
560 560
 		// Set the URI if it isn't set yet.
561
-		$post_status = get_post_status( $post_id );
562
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
563
-			$uri = wl_build_entity_uri( $post_id );
564
-			wl_set_entity_uri( $post_id, $uri );
561
+		$post_status = get_post_status($post_id);
562
+		if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) {
563
+			$uri = wl_build_entity_uri($post_id);
564
+			wl_set_entity_uri($post_id, $uri);
565 565
 		}
566 566
 
567 567
 		return $uri;
@@ -577,9 +577,9 @@  discard block
 block discarded – undo
577 577
 	 *
578 578
 	 * @return string The input HTML code.
579 579
 	 */
580
-	private function get_alternative_label_input( $value = '' ) {
580
+	private function get_alternative_label_input($value = '') {
581 581
 
582
-		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
582
+		return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift'));
583 583
 	}
584 584
 
585 585
 	/**
@@ -591,12 +591,12 @@  discard block
 block discarded – undo
591 591
 	 */
592 592
 	public function count() {
593 593
 
594
-		$posts = get_posts( $this->add_criterias( array(
594
+		$posts = get_posts($this->add_criterias(array(
595 595
 			'post_status' => 'any',
596
-			'numberposts' => - 1,
597
-		) ) );
596
+			'numberposts' => -1,
597
+		)));
598 598
 
599
-		return count( $posts );
599
+		return count($posts);
600 600
 	}
601 601
 
602 602
 	/**
@@ -609,7 +609,7 @@  discard block
 block discarded – undo
609 609
 	 *
610 610
 	 * @return array The arguments for a `get_posts` call.
611 611
 	 */
612
-	public static function add_criterias( $args ) {
612
+	public static function add_criterias($args) {
613 613
 
614 614
 		// Build an optimal tax-query.
615 615
 		$tax_query = array(
@@ -679,28 +679,28 @@  discard block
 block discarded – undo
679 679
 	 *
680 680
 	 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
681 681
 	 */
682
-	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
682
+	public function create($name, $type_uri, $logo = null, $status = 'publish') {
683 683
 
684 684
 		// Create an entity for the publisher.
685
-		$post_id = wp_insert_post( array(
685
+		$post_id = wp_insert_post(array(
686 686
 			'post_type'    => self::TYPE_NAME,
687 687
 			'post_title'   => $name,
688 688
 			'post_status'  => $status,
689 689
 			'post_content' => '',
690
-		) );
690
+		));
691 691
 
692 692
 		// Return the error if any.
693
-		if ( is_wp_error( $post_id ) ) {
693
+		if (is_wp_error($post_id)) {
694 694
 			return $post_id;
695 695
 		}
696 696
 
697 697
 		// Set the entity logo.
698
-		if ( $logo && is_numeric( $logo ) ) {
699
-			set_post_thumbnail( $post_id, $logo );
698
+		if ($logo && is_numeric($logo)) {
699
+			set_post_thumbnail($post_id, $logo);
700 700
 		}
701 701
 
702 702
 		// Set the entity type.
703
-		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
703
+		Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri);
704 704
 
705 705
 		return $post_id;
706 706
 	}
@@ -716,9 +716,9 @@  discard block
 block discarded – undo
716 716
 	 *
717 717
 	 * @return array An array of post ids.
718 718
 	 */
719
-	public function get_related_entities( $id, $post_status = 'publish' ) {
719
+	public function get_related_entities($id, $post_status = 'publish') {
720 720
 
721
-		return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
721
+		return $this->relation_service->get_objects($id, 'ids', null, $post_status);
722 722
 	}
723 723
 
724 724
 	/**
@@ -730,16 +730,16 @@  discard block
 block discarded – undo
730 730
 	 *
731 731
 	 * @return array An array of entity posts.
732 732
 	 */
733
-	public function get( $params = array() ) {
733
+	public function get($params = array()) {
734 734
 
735 735
 		// Set the defaults.
736
-		$defaults = array( 'post_type' => 'entity' );
736
+		$defaults = array('post_type' => 'entity');
737 737
 
738 738
 		// Merge the defaults with the provided parameters.
739
-		$args = wp_parse_args( $params, $defaults );
739
+		$args = wp_parse_args($params, $defaults);
740 740
 
741 741
 		// Call the `get_posts` function.
742
-		return get_posts( $args );
742
+		return get_posts($args);
743 743
 	}
744 744
 
745 745
 	/**
@@ -755,9 +755,9 @@  discard block
 block discarded – undo
755 755
 	static function valid_entity_post_types() {
756 756
 
757 757
 		// Ignore builtins in the call to avoid getting attachments.
758
-		$post_types = array( 'post', 'page', self::TYPE_NAME );
758
+		$post_types = array('post', 'page', self::TYPE_NAME);
759 759
 
760
-		return apply_filters( 'wl_valid_entity_post_types', $post_types );
760
+		return apply_filters('wl_valid_entity_post_types', $post_types);
761 761
 	}
762 762
 
763 763
 }
Please login to merge, or discard this patch.