Completed
Push — develop ( 1ffa16...5fba5b )
by David
03:05
created

Wordlift_Entity_Service::add_criterias()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 14

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 29
loc 29
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Provide entity-related services.
5
 *
6
 * @since 3.1.0
7
 */
8
class Wordlift_Entity_Service {
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
	 * The entity post type name.
39
	 *
40
	 * @since 3.1.0
41
	 */
42
	const TYPE_NAME = 'entity';
43
44
	/**
45
	 * The alternative label meta key.
46
	 *
47
	 * @since 3.2.0
48
	 */
49
	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
50
51
	/**
52
	 * The alternative label input template.
53
	 *
54
	 * @since 3.2.0
55
	 */
56
	// TODO: this should be moved to a class that deals with HTML code.
57
	const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
58
                <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label>
59
                <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text">
60
                <button class="button wl-delete-button">%s</button>
61
                </div>';
62
63
	/**
64
	 * A singleton instance of the Entity service.
65
	 *
66
	 * @since  3.2.0
67
	 * @access private
68
	 * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
69
	 */
70
	private static $instance;
71
72
	/**
73
	 * Create a Wordlift_Entity_Service instance.
74
	 *
75
	 * @since 3.2.0
76
	 *
77
	 * @param \Wordlift_UI_Service       $ui_service       The UI service.
78
	 * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
79
	 */
80
	public function __construct( $ui_service, $relation_service ) {
81
82
		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
83
84
		$this->ui_service       = $ui_service;
85
		$this->relation_service = $relation_service;
86
87
		// Set the singleton instance.
88
		self::$instance = $this;
89
	}
90
91
	/**
92
	 * Get the singleton instance of the Entity service.
93
	 *
94
	 * @since 3.2.0
95
	 * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
96
	 */
97
	public static function get_instance() {
98
99
		return self::$instance;
100
	}
101
102
	/**
103
	 * Determines whether a post is an entity or not. Entity is in this context
104
	 * something which is not an article.
105
	 *
106
	 * @since 3.1.0
107
	 *
108
	 * @param int $post_id A post id.
109
	 *
110
	 * @return bool Return true if the post is an entity otherwise false.
111
	 */
112
	public function is_entity( $post_id ) {
113
114
		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME );
115
116
		if ( 0 === count( $terms ) ) {
117
			return false;
118
		}
119
120
		// We don't consider an `article` to be an entity.
121
		if ( 'article' !== $terms[0]->slug ) {
122
			return true;
123
		}
124
125
		return false;
126
	}
127
128
	/**
129
	 * Get the proper classification scope for a given entity post
130
	 *
131
	 * @since 3.5.0
132
	 *
133
	 * @param integer $post_id An entity post id.
134
	 *
135
	 * @param string  $default The default classification scope, `what` if not
136
	 *                         provided.
137
	 *
138
	 * @return string Returns a classification scope (e.g. 'what').
139
	 */
140
	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
141
142
		if ( false === $this->is_entity( $post_id ) ) {
143
			return $default;
144
		}
145
146
		// Retrieve the entity type
147
		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
148
		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
149
		// Retrieve classification boxes configuration
150
		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
151
		foreach ( $classification_boxes as $cb ) {
152
			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
153
				return $cb['id'];
154
			}
155
		}
156
157
		return $default;
158
	}
159
160
	/**
161
	 * Check whether a {@link WP_Post} is used.
162
	 *
163
	 * @param int $post_id The {@link WP_Post}'s id.
164
	 *
165
	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
166
	 */
167
	public function is_used( $post_id ) {
168
169
		if ( false === $this->is_entity( $post_id ) ) {
170
			return null;
171
		}
172
		// Retrieve the post
173
		$entity = get_post( $post_id );
174
175
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
176
		// Retrieve Wordlift relation instances table name
177
		$table_name = wl_core_get_relation_instances_table_name();
178
179
		// Check is it's referenced / related to another post / entity
180
		$stmt = $wpdb->prepare(
181
			"SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
182
			$entity->ID
183
		);
184
185
		// Perform the query
186
		$relation_instances = (int) $wpdb->get_var( $stmt );
187
		// If there is at least one relation instance for the current entity, then it's used
188
		if ( 0 < $relation_instances ) {
189
			return true;
190
		}
191
192
		// Check if the entity uri is used as meta_value
193
		$stmt = $wpdb->prepare(
194
			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
195
			$entity->ID,
196
			wl_get_entity_uri( $entity->ID )
0 ignored issues
show
Deprecated Code introduced by
The function wl_get_entity_uri() has been deprecated with message: use Wordlift_Entity_Service::get_instance()->get_uri( $post_id )

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
197
		);
198
		// Perform the query
199
		$meta_instances = (int) $wpdb->get_var( $stmt );
200
201
		// If there is at least one meta that refers the current entity uri, then current entity is used
202
		if ( 0 < $meta_instances ) {
203
			return true;
204
		}
205
206
		// If we are here, it means the current entity is not used at the moment
207
		return false;
208
	}
209
210
	/**
211
	 * Determines whether a given uri is an internal uri or not.
212
	 *
213
	 * @since 3.3.2
214
	 *
215
	 * @param int $uri An uri.
216
	 *
217
	 * @return true if the uri internal to the current dataset otherwise false.
218
	 */
219
	public function is_internal_uri( $uri ) {
220
221
		return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) );
0 ignored issues
show
Deprecated Code introduced by
The function wl_configuration_get_redlink_dataset_uri() has been deprecated with message: use Wordlift_Configuration_Service::get_instance()->get_dataset_uri();

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
222
	}
223
224
	/**
225
	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
226
	 *
227
	 * @since 3.2.0
228
	 *
229
	 * @param string $uri The entity URI.
230
	 *
231
	 * @return WP_Post|null A WP_Post instance or null if not found.
232
	 */
233
	public function get_entity_post_by_uri( $uri ) {
234
235
		$this->log->trace( "Getting an entity post for URI $uri..." );
236
237
		// Check if we've been provided with a value otherwise return null.
238
		if ( empty( $uri ) ) {
239
			return null;
240
		}
241
242
		$query_args = array(
243
			// See https://github.com/insideout10/wordlift-plugin/issues/654.
244
			'ignore_sticky_posts' => 1,
245
			'posts_per_page'      => 1,
246
			'post_status'         => 'any',
247
			'post_type'           => Wordlift_Entity_Service::valid_entity_post_types(),
248
			'meta_query'          => array(
249
				array(
250
					'key'     => WL_ENTITY_URL_META_NAME,
251
					'value'   => $uri,
252
					'compare' => '=',
253
				),
254
			),
255
			// The following query can be avoided, it's superfluous since
256
			// we're looking for a post with a specific Entity URI. This query
257
			// may in fact consume up to 50% of the timing necessary to load a
258
			// page.
259
			//
260
			// See https://github.com/insideout10/wordlift-plugin/issues/674.
261
			//			'tax_query'           => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
262
			//				'relation' => 'AND',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
263
			//				array(
264
			//					'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
265
			//					'operator' => 'EXISTS',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
266
			//				),
267
			//				array(
268
			//					'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
269
			//					'field'    => 'slug',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
270
			//					'terms'    => 'article',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
271
			//					'operator' => 'NOT IN',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
272
			//				),
273
			//			),
274
		);
275
276
		// Only if the current uri is not an internal uri, entity search is
277
		// performed also looking at sameAs values.
278
		//
279
		// This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237
280
		if ( ! $this->is_internal_uri( $uri ) ) {
281
282
			$query_args['meta_query']['relation'] = 'OR';
283
			$query_args['meta_query'][]           = array(
284
				'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
285
				'value'   => $uri,
286
				'compare' => '=',
287
			);
288
		}
289
290
		$posts = get_posts( $query_args );
291
292
//		$query = new WP_Query( $query_args );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
293
//
294
//		// Get the matching entity posts.
295
//		$posts = $query->get_posts();
296
297
		// Return null if no post is found.
298
		if ( 0 === count( $posts ) ) {
299
			$this->log->warn( "No post for URI $uri." );
300
301
			return null;
302
		}
303
304
		// Return the found post.
305
		return current( $posts );
306
	}
307
308
	/**
309
	 * Fires once a post has been saved. This function uses the $_REQUEST, therefore
310
	 * we check that the post we're saving is the current post.
311
	 *
312
	 * @see   https://github.com/insideout10/wordlift-plugin/issues/363
313
	 *
314
	 * @since 3.2.0
315
	 *
316
	 * @param int     $post_id Post ID.
317
	 * @param WP_Post $post    Post object.
318
	 * @param bool    $update  Whether this is an existing post being updated or not.
319
	 */
320
	public function save_post( $post_id, $post, $update ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
save_post uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
321
322
		// Avoid doing anything if post is autosave or a revision.
323
		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
324
			return;
325
		}
326
327
		// We're setting the alternative label that have been provided via the UI
328
		// (in fact we're using $_REQUEST), while save_post may be also called
329
		// programmatically by some other function: we need to check therefore if
330
		// the $post_id in the save_post call matches the post id set in the request.
331
		//
332
		// If this is not the current post being saved or if it's not an entity, return.
333
		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
334
			return;
335
		}
336
337
		// Get the alt labels from the request (or empty array).
338
		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
339
340
		// Set the alternative labels.
341
		$this->set_alternative_labels( $post_id, $alt_labels );
342
343
	}
344
345
	/**
346
	 * Set the alternative labels.
347
	 *
348
	 * @since 3.2.0
349
	 *
350
	 * @param int   $post_id    The post id.
351
	 * @param array $alt_labels An array of labels.
352
	 */
353
	public function set_alternative_labels( $post_id, $alt_labels ) {
354
355
		// Force $alt_labels to be an array
356
		if ( ! is_array( $alt_labels ) ) {
357
			$alt_labels = array( $alt_labels );
358
		}
359
360
		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
361
362
		// Delete all the existing alternate labels.
363
		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
364
365
		// Set the alternative labels.
366
		foreach ( $alt_labels as $alt_label ) {
367
			if ( ! empty( $alt_label ) ) {
368
				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
369
			}
370
		}
371
372
	}
373
374
	/**
375
	 * Retrieve the alternate labels.
376
	 *
377
	 * @since 3.2.0
378
	 *
379
	 * @param int $post_id Post id.
380
	 *
381
	 * @return mixed An array  of alternative labels.
382
	 */
383
	public function get_alternative_labels( $post_id ) {
384
385
		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
386
	}
387
388
	/**
389
	 * Retrieve the labels for an entity, i.e. the title + the synonyms.
390
	 *
391
	 * @since 3.12.0
392
	 *
393
	 * @param int $post_id The entity {@link WP_Post} id.
394
	 *
395
	 * @return array An array with the entity title and labels.
396
	 */
397
	public function get_labels( $post_id ) {
398
399
		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
400
	}
401
402
	/**
403
	 * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
404
	 *
405
	 * @since 3.2.0
406
	 *
407
	 * @param WP_Post $post Post object.
408
	 */
409
	public function edit_form_before_permalink( $post ) {
410
411
		// If it's not an entity, return.
412
		if ( ! $this->is_entity( $post->ID ) ) {
413
			return;
414
		}
415
416
		// Print the input template.
417
		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
418
419
		// Print all the currently set alternative labels.
420
		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
421
422
			echo $this->get_alternative_label_input( $alt_label );
423
424
		};
425
426
		// Print the button.
427
		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
428
429
	}
430
431
	/**
432
	 * Get the URI for the entity with the specified post id.
433
	 *
434
	 * @since 3.6.0
435
	 *
436
	 * @param int $post_id The entity post id.
437
	 *
438
	 * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
439
	 */
440
	public function get_uri( $post_id ) {
441
442
		// If a null is given, nothing to do
443
		if ( null == $post_id ) {
444
			return null;
445
		}
446
447
		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
448
449
		// If the dataset uri is not properly configured, null is returned
450
		if ( '' === wl_configuration_get_redlink_dataset_uri() ) {
0 ignored issues
show
Deprecated Code introduced by
The function wl_configuration_get_redlink_dataset_uri() has been deprecated with message: use Wordlift_Configuration_Service::get_instance()->get_dataset_uri();

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
451
			return null;
452
		}
453
454
		// Set the URI if it isn't set yet.
455
		$post_status = get_post_status( $post_id );
456
		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
457
			$uri = wl_build_entity_uri( $post_id );
458
			wl_set_entity_uri( $post_id, $uri );
459
		}
460
461
		return $uri;
462
	}
463
464
465
	/**
466
	 * Get the alternative label input HTML code.
467
	 *
468
	 * @since 3.2.0
469
	 *
470
	 * @param string $value The input value.
471
	 *
472
	 * @return string The input HTML code.
473
	 */
474
	private function get_alternative_label_input( $value = '' ) {
475
476
		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
477
	}
478
479
	/**
480
	 * Get the number of entity posts published in this blog.
481
	 *
482
	 * @since 3.6.0
483
	 *
484
	 * @return int The number of published entity posts.
485
	 */
486
	public function count() {
487
488
		$posts = get_posts( $this->add_criterias( array(
489
			'post_status' => 'any',
490
			'numberposts' => - 1,
491
		) ) );
492
493
		return count( $posts );
494
	}
495
496
	/**
497
	 * Add the entity filtering criterias to the arguments for a `get_posts`
498
	 * call.
499
	 *
500
	 * @since 3.15.0
501
	 *
502
	 * @param array $args The arguments for a `get_posts` call.
503
	 *
504
	 * @return array The arguments for a `get_posts` call.
505
	 */
506 View Code Duplication
	public static function add_criterias( $args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
507
508
		// Build an optimal tax-query.
509
		$tax_query = array(
510
			'relation' => 'AND',
511
			array(
512
				'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
513
				'operator' => 'EXISTS',
514
			),
515
			array(
516
				'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
517
				'field'    => 'slug',
518
				'terms'    => 'article',
519
				'operator' => 'NOT IN',
520
			),
521
		);
522
523
		return $args + array(
524
				'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
525
				// Since 3.17.0: should this be faster?
526
				'tax_query' => $tax_query,
527
				//				'tax_query' => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
528
				//					array(
529
				//						'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
530
				//						'terms'    => self::get_entity_terms(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
531
				//					),
532
				//				),
533
			);
534
	}
535
536
//	/**
537
//	 * Get the entity terms IDs which represent an entity.
538
//	 *
539
//	 * @since 3.17.0 deprecated.
540
//	 * @since 3.15.0
541
//	 *
542
//	 * @deprecated
543
//	 * @return array An array of terms' ids.
544
//	 */
545
//	public static function get_entity_terms() {
546
//
547
//		$terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array(
548
//			'hide_empty' => false,
549
//			// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
550
//			// An issue has been opened with the AAM plugin author as well.
551
//			//
552
//			// see https://github.com/insideout10/wordlift-plugin/issues/334
553
//			// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
554
//			'fields'     => 'all',
555
//		) );
556
//
557
//		return array_map( function ( $term ) {
558
//			return $term->term_id;
559
//		}, array_filter( $terms, function ( $term ) {
560
//			return 'article' !== $term->slug;
561
//		} ) );
562
//	}
563
564
	/**
565
	 * Create a new entity.
566
	 *
567
	 * @since 3.9.0
568
	 *
569
	 * @param string $name     The entity name.
570
	 * @param string $type_uri The entity's type URI.
571
	 * @param null   $logo     The entity logo id (or NULL if none).
572
	 * @param string $status   The post status, by default 'publish'.
573
	 *
574
	 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
575
	 */
576
	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
577
578
		// Create an entity for the publisher.
579
		$post_id = wp_insert_post( array(
580
			'post_type'    => self::TYPE_NAME,
581
			'post_title'   => $name,
582
			'post_status'  => $status,
583
			'post_content' => '',
584
		) );
585
586
		// Return the error if any.
587
		if ( is_wp_error( $post_id ) ) {
588
			return $post_id;
589
		}
590
591
		// Set the entity logo.
592
		if ( $logo && is_numeric( $logo ) ) {
593
			set_post_thumbnail( $post_id, $logo );
594
		}
595
596
		// Set the entity type.
597
		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
598
599
		return $post_id;
600
	}
601
602
	/**
603
	 * Get the entities related to the one with the specified id. By default only
604
	 * published entities will be returned.
605
	 *
606
	 * @since 3.10.0
607
	 *
608
	 * @param int    $id          The post id.
609
	 * @param string $post_status The target post status (default = publish).
610
	 *
611
	 * @return array An array of post ids.
612
	 */
613
	public function get_related_entities( $id, $post_status = 'publish' ) {
614
615
		return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
616
	}
617
618
	/**
619
	 * Get the list of entities.
620
	 *
621
	 * @since 3.12.2
622
	 *
623
	 * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
624
	 *
625
	 * @return array An array of entity posts.
626
	 */
627
	public function get( $params = array() ) {
628
629
		// Set the defaults.
630
		$defaults = array( 'post_type' => 'entity' );
631
632
		// Merge the defaults with the provided parameters.
633
		$args = wp_parse_args( $params, $defaults );
634
635
		// Call the `get_posts` function.
636
		return get_posts( $args );
637
	}
638
639
	/**
640
	 * The list of post type names which can be used for entities
641
	 *
642
	 * Criteria is that the post type is public. The list of valid post types
643
	 * can be overridden with a filter.
644
	 *
645
	 * @since 3.15.0
646
	 *
647
	 * @return array Array containing the names of the valid post types.
648
	 */
649
	static function valid_entity_post_types() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
650
651
		// Ignore builtins in the call to avoid getting attachments.
652
		$post_types = array( 'post', 'page', self::TYPE_NAME );
653
654
		return apply_filters( 'wl_valid_entity_post_types', $post_types );
655
	}
656
657
}
658