Completed
Push — master ( 3f9e5a...70803a )
by David
02:16
created
src/includes/class-wordlift-entity-type-service.php 2 patches
Indentation   +376 added lines, -376 removed lines patch added patch discarded remove patch
@@ -19,129 +19,129 @@  discard block
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Entity_Type_Service {
21 21
 
22
-	/**
23
-	 * The {@link Wordlift_Schema_Service} instance.
24
-	 *
25
-	 * @since  3.7.0
26
-	 * @access private
27
-	 * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
-	 */
29
-	private $schema_service;
30
-
31
-	/**
32
-	 * A {@link Wordlift_Log_Service} instance.
33
-	 *
34
-	 * @since  3.8.0
35
-	 * @access private
36
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
-	 */
38
-	private $log;
39
-
40
-	/**
41
-	 * The {@link Wordlift_Entity_Type_Service} singleton instance.
42
-	 *
43
-	 * @since  3.7.0
44
-	 * @access private
45
-	 * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
46
-	 */
47
-	private static $instance;
48
-
49
-	/**
50
-	 * Wordlift_Entity_Type_Service constructor.
51
-	 *
52
-	 * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
53
-	 *
54
-	 * @since 3.7.0
55
-	 *
56
-	 */
57
-	public function __construct( $schema_service ) {
58
-
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
60
-
61
-		$this->schema_service = $schema_service;
62
-
63
-		self::$instance = $this;
64
-
65
-	}
66
-
67
-	/**
68
-	 * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
69
-	 *
70
-	 * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
71
-	 * @since 3.7.0
72
-	 */
73
-	public static function get_instance() {
74
-
75
-		return self::$instance;
76
-	}
77
-
78
-	/**
79
-	 * Get the types associated with the specified entity post id.
80
-	 *
81
-	 * We have a strategy to define the entity type, given that everything is
82
-	 * an entity, i.e. also posts/pages and custom post types.
83
-	 *
84
-	 * @param int $post_id The post id.
85
-	 *
86
-	 * @return array|null {
87
-	 * An array of type properties or null if no term is associated
88
-	 *
89
-	 * @type string css_class     The css class, e.g. `wl-thing`.
90
-	 * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
91
-	 * @type array  same_as       An array of same as attributes.
92
-	 * @type array  custom_fields An array of custom fields.
93
-	 * @type array  linked_data   An array of {@link Wordlift_Sparql_Tuple_Rendition}.
94
-	 * }
95
-	 * @since 3.7.0
96
-	 *
97
-	 * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
98
-	 *
99
-	 * @since 3.18.0 The cases are the following:
100
-	 *  1. the post has a term from the Entity Types Taxonomy: the term defines
101
-	 *     the entity type, e.g. Organization, Person, ...
102
-	 *  2. the post doesn't have a term from the Entity Types Taxonomy:
103
-	 *      a) the post is a `wl_entity` custom post type, then the post is
104
-	 *           assigned the `Thing` entity type by default.
105
-	 *      b) the post is a `post` post type, then the post is
106
-	 *           assigned the `Article` entity type by default.
107
-	 *      c) the post is a custom post type then it is
108
-	 *          assigned the `WebPage` entity type by default.
109
-	 *
110
-	 */
111
-	public function get( $post_id ) {
112
-
113
-		$this->log->trace( "Getting the post type for post $post_id..." );
114
-
115
-		// Get the post type.
116
-		$post_type = get_post_type( $post_id );
117
-
118
-		// Return `web-page` for non entities.
119
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
-			$this->log->info( "Returning `web-page` for post $post_id." );
121
-
122
-			return $this->schema_service->get_schema( 'web-page' );
123
-		}
124
-
125
-		// Get the type from the associated classification.
126
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
127
-
128
-		// Return the schema type if there is a term found.
129
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
130
-			// Cycle through the terms and return the first one with a valid schema.
131
-			foreach ( $terms as $term ) {
132
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
133
-
134
-				// Try to get the schema for the term.
135
-				$schema = $this->schema_service->get_schema( $term->slug );
136
-
137
-				// If found, return it, ignoring the other types.
138
-				if ( null !== $schema ) {
139
-					// Return the entity type with the specified id.
140
-					return $schema;
141
-				}
142
-			}
143
-
144
-			/*
22
+    /**
23
+     * The {@link Wordlift_Schema_Service} instance.
24
+     *
25
+     * @since  3.7.0
26
+     * @access private
27
+     * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
+     */
29
+    private $schema_service;
30
+
31
+    /**
32
+     * A {@link Wordlift_Log_Service} instance.
33
+     *
34
+     * @since  3.8.0
35
+     * @access private
36
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
+     */
38
+    private $log;
39
+
40
+    /**
41
+     * The {@link Wordlift_Entity_Type_Service} singleton instance.
42
+     *
43
+     * @since  3.7.0
44
+     * @access private
45
+     * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
46
+     */
47
+    private static $instance;
48
+
49
+    /**
50
+     * Wordlift_Entity_Type_Service constructor.
51
+     *
52
+     * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
53
+     *
54
+     * @since 3.7.0
55
+     *
56
+     */
57
+    public function __construct( $schema_service ) {
58
+
59
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
60
+
61
+        $this->schema_service = $schema_service;
62
+
63
+        self::$instance = $this;
64
+
65
+    }
66
+
67
+    /**
68
+     * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
69
+     *
70
+     * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
71
+     * @since 3.7.0
72
+     */
73
+    public static function get_instance() {
74
+
75
+        return self::$instance;
76
+    }
77
+
78
+    /**
79
+     * Get the types associated with the specified entity post id.
80
+     *
81
+     * We have a strategy to define the entity type, given that everything is
82
+     * an entity, i.e. also posts/pages and custom post types.
83
+     *
84
+     * @param int $post_id The post id.
85
+     *
86
+     * @return array|null {
87
+     * An array of type properties or null if no term is associated
88
+     *
89
+     * @type string css_class     The css class, e.g. `wl-thing`.
90
+     * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
91
+     * @type array  same_as       An array of same as attributes.
92
+     * @type array  custom_fields An array of custom fields.
93
+     * @type array  linked_data   An array of {@link Wordlift_Sparql_Tuple_Rendition}.
94
+     * }
95
+     * @since 3.7.0
96
+     *
97
+     * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
98
+     *
99
+     * @since 3.18.0 The cases are the following:
100
+     *  1. the post has a term from the Entity Types Taxonomy: the term defines
101
+     *     the entity type, e.g. Organization, Person, ...
102
+     *  2. the post doesn't have a term from the Entity Types Taxonomy:
103
+     *      a) the post is a `wl_entity` custom post type, then the post is
104
+     *           assigned the `Thing` entity type by default.
105
+     *      b) the post is a `post` post type, then the post is
106
+     *           assigned the `Article` entity type by default.
107
+     *      c) the post is a custom post type then it is
108
+     *          assigned the `WebPage` entity type by default.
109
+     *
110
+     */
111
+    public function get( $post_id ) {
112
+
113
+        $this->log->trace( "Getting the post type for post $post_id..." );
114
+
115
+        // Get the post type.
116
+        $post_type = get_post_type( $post_id );
117
+
118
+        // Return `web-page` for non entities.
119
+        if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
+            $this->log->info( "Returning `web-page` for post $post_id." );
121
+
122
+            return $this->schema_service->get_schema( 'web-page' );
123
+        }
124
+
125
+        // Get the type from the associated classification.
126
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
127
+
128
+        // Return the schema type if there is a term found.
129
+        if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
130
+            // Cycle through the terms and return the first one with a valid schema.
131
+            foreach ( $terms as $term ) {
132
+                $this->log->debug( "Found `{$term->slug}` term for post $post_id." );
133
+
134
+                // Try to get the schema for the term.
135
+                $schema = $this->schema_service->get_schema( $term->slug );
136
+
137
+                // If found, return it, ignoring the other types.
138
+                if ( null !== $schema ) {
139
+                    // Return the entity type with the specified id.
140
+                    return $schema;
141
+                }
142
+            }
143
+
144
+            /*
145 145
 			 * When a schema isn't found, we return `thing`. Schema may not be found because
146 146
 			 * the new schema classes that we support since #852 aren't configured in the schema
147 147
 			 * service.
@@ -151,90 +151,90 @@  discard block
 block discarded – undo
151 151
 			 * @since 3.20.0
152 152
 			 */
153 153
 
154
-			return $this->schema_service->get_schema( 'thing' );
155
-		}
156
-
157
-		// If it's a page or post return `Article`.
158
-		if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
160
-
161
-			// Return "Article" schema type for posts.
162
-			return $this->schema_service->get_schema( 'article' );
163
-		}
164
-
165
-		// Return "Thing" schema type for entities.
166
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
167
-
168
-		// Return the entity type with the specified id.
169
-		return $this->schema_service->get_schema( 'thing' );
170
-
171
-	}
172
-
173
-	/**
174
-	 * Get the term ids of the entity types associated to the specified post.
175
-	 *
176
-	 * @param int $post_id The post id.
177
-	 *
178
-	 * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
179
-	 * @since 3.20.0
180
-	 *
181
-	 */
182
-	public function get_ids( $post_id ) {
183
-
184
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
185
-	}
186
-
187
-	/**
188
-	 * Get the camel case names of the entity types associated to the specified post.
189
-	 *
190
-	 * @param int $post_id The post id.
191
-	 *
192
-	 * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
193
-	 * @since 3.20.0
194
-	 *
195
-	 */
196
-	public function get_names( $post_id ) {
197
-
198
-		$ids = $this->get_ids( $post_id );
199
-
200
-		$camel_case_names = array_map( function ( $id ) {
201
-			return get_term_meta( $id, '_wl_name', true );
202
-		}, $ids );
203
-
204
-		return $camel_case_names;
205
-	}
206
-
207
-	/**
208
-	 * Set the main type for the specified entity post, given the type URI.
209
-	 *
210
-	 * @param int $post_id The post id.
211
-	 * @param string $type_uri The type URI.
212
-	 * @param bool $replace Whether the provided type must replace the existing types, by default `true`.
213
-	 *
214
-	 * @since 3.8.0
215
-	 *
216
-	 */
217
-	public function set( $post_id, $type_uri, $replace = true ) {
218
-
219
-		// If the type URI is empty we remove the type.
220
-		if ( empty( $type_uri ) ) {
221
-			$this->log->debug( "Removing entity type for post $post_id..." );
222
-
223
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
224
-
225
-			return;
226
-		}
227
-
228
-		$this->log->debug( "Setting entity type for post $post_id..." );
229
-
230
-		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
231
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
232
-			// Get term by slug.
233
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
234
-			// Get term by URI.
235
-			: $this->get_term_by_uri( $type_uri );
236
-
237
-		/*
154
+            return $this->schema_service->get_schema( 'thing' );
155
+        }
156
+
157
+        // If it's a page or post return `Article`.
158
+        if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
+            $this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
160
+
161
+            // Return "Article" schema type for posts.
162
+            return $this->schema_service->get_schema( 'article' );
163
+        }
164
+
165
+        // Return "Thing" schema type for entities.
166
+        $this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
167
+
168
+        // Return the entity type with the specified id.
169
+        return $this->schema_service->get_schema( 'thing' );
170
+
171
+    }
172
+
173
+    /**
174
+     * Get the term ids of the entity types associated to the specified post.
175
+     *
176
+     * @param int $post_id The post id.
177
+     *
178
+     * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
179
+     * @since 3.20.0
180
+     *
181
+     */
182
+    public function get_ids( $post_id ) {
183
+
184
+        return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
185
+    }
186
+
187
+    /**
188
+     * Get the camel case names of the entity types associated to the specified post.
189
+     *
190
+     * @param int $post_id The post id.
191
+     *
192
+     * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
193
+     * @since 3.20.0
194
+     *
195
+     */
196
+    public function get_names( $post_id ) {
197
+
198
+        $ids = $this->get_ids( $post_id );
199
+
200
+        $camel_case_names = array_map( function ( $id ) {
201
+            return get_term_meta( $id, '_wl_name', true );
202
+        }, $ids );
203
+
204
+        return $camel_case_names;
205
+    }
206
+
207
+    /**
208
+     * Set the main type for the specified entity post, given the type URI.
209
+     *
210
+     * @param int $post_id The post id.
211
+     * @param string $type_uri The type URI.
212
+     * @param bool $replace Whether the provided type must replace the existing types, by default `true`.
213
+     *
214
+     * @since 3.8.0
215
+     *
216
+     */
217
+    public function set( $post_id, $type_uri, $replace = true ) {
218
+
219
+        // If the type URI is empty we remove the type.
220
+        if ( empty( $type_uri ) ) {
221
+            $this->log->debug( "Removing entity type for post $post_id..." );
222
+
223
+            wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
224
+
225
+            return;
226
+        }
227
+
228
+        $this->log->debug( "Setting entity type for post $post_id..." );
229
+
230
+        // if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
231
+        $term = ( 0 === strpos( $type_uri, 'wl-' ) )
232
+            // Get term by slug.
233
+            ? $this->get_term_by_slug( substr( $type_uri, 3 ) )
234
+            // Get term by URI.
235
+            : $this->get_term_by_uri( $type_uri );
236
+
237
+        /*
238 238
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
239 239
 		 * connected to Articles via mentions. We realized that the client JS code is passing `wl-other` when the
240 240
 		 * entity type isn't "notable". In which case we couldn't find an entity type.
@@ -245,174 +245,174 @@  discard block
 block discarded – undo
245 245
 		 *
246 246
 		 * @since 3.23.4
247 247
 		 */
248
-		if ( false === $term ) {
249
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
250
-
251
-			$term = $this->get_term_by_slug( 'thing' );
252
-
253
-			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
254
-			// is installed.
255
-			if ( false === $term ) {
256
-				return;
257
-			}
258
-		}
259
-
260
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
261
-
262
-		// `$replace` is passed to decide whether to replace or append the term.
263
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
264
-
265
-	}
266
-
267
-	/**
268
-	 * Get an entity type term given its slug.
269
-	 *
270
-	 * @param string $slug The slug.
271
-	 *
272
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
273
-	 *                             or `$term` was not found.
274
-	 * @since 3.20.0
275
-	 *
276
-	 */
277
-	private function get_term_by_slug( $slug ) {
278
-
279
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
280
-	}
281
-
282
-	/**
283
-	 * Get an entity type term given its URI.
284
-	 *
285
-	 * @param string $uri The uri.
286
-	 *
287
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
288
-	 *                             or `$term` was not found.
289
-	 * @since 3.20.0
290
-	 *
291
-	 */
292
-	public function get_term_by_uri( $uri ) {
293
-
294
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
295
-			'fields'     => 'all',
296
-			'get'        => 'all',
297
-			'number'     => 1,
298
-			'meta_query' => array(
299
-				array(
300
-					// Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
301
-					// `WL_ALL_ENTITY_TYPES` is set to true.
302
-					'key'   => '_wl_uri',
303
-					'value' => $uri,
304
-				),
305
-			),
306
-			'orderby'    => 'term_id',
307
-			'order'      => 'ASC',
308
-		) );
309
-
310
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
311
-	}
312
-
313
-	/**
314
-	 * Check whether an entity type is set for the {@link WP_Post} with the
315
-	 * specified id.
316
-	 *
317
-	 * @param int $post_id The {@link WP_Post}'s `id`.
318
-	 * @param string $uri The entity type URI.
319
-	 *
320
-	 * @return bool True if an entity type is set otherwise false.
321
-	 * @since 3.15.0
322
-	 *
323
-	 */
324
-	public function has_entity_type( $post_id, $uri = null ) {
325
-
326
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
327
-
328
-		// If an URI hasn't been specified just check whether we have at least
329
-		// one entity type.
330
-		if ( null === $uri ) {
331
-
332
-			// Get the post terms for the specified post ID.
333
-			$terms = $this->get_post_terms( $post_id );
334
-
335
-			$this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
336
-
337
-			// True if there's at least one term bound to the post.
338
-			return ( 0 < count( $terms ) );
339
-		}
340
-
341
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
342
-
343
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
344
-
345
-		// Check whether the post has an entity type with that URI.
346
-		return $has_entity_type;
347
-	}
348
-
349
-	/**
350
-	 * Get the list of entity types' terms for the specified {@link WP_Post}.
351
-	 *
352
-	 * @param int $post_id The {@link WP_Post} id.
353
-	 *
354
-	 * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
355
-	 * @since 3.15.0
356
-	 *
357
-	 */
358
-	private function get_post_terms( $post_id ) {
359
-
360
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
361
-			'hide_empty' => false,
362
-			// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
363
-			// An issue has been opened with the AAM plugin author as well.
364
-			//
365
-			// see https://github.com/insideout10/wordlift-plugin/issues/334
366
-			// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
367
-			'fields'     => 'all',
368
-		) );
369
-	}
370
-
371
-	/**
372
-	 * Get an entity type term given its URI.
373
-	 *
374
-	 * @param int $post_id The {@link WP_Post} id.
375
-	 * @param string $uri The entity type URI.
376
-	 *
377
-	 * @return bool True if the post has that type URI bound to it otherwise false.
378
-	 * @since 3.15.0
379
-	 *
380
-	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
381
-	 */
382
-	private function has_post_term_by_uri( $post_id, $uri ) {
383
-
384
-		// Get the post terms bound to the specified post.
385
-		$terms = $this->get_post_terms( $post_id );
386
-
387
-		// Look for a term if the specified URI.
388
-		foreach ( $terms as $term ) {
389
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
390
-
391
-			if ( $uri === $term_uri ) {
392
-				return true;
393
-			}
394
-		}
395
-
396
-		// Return null.
397
-		return false;
398
-	}
399
-
400
-
401
-	/**
402
-	 * Determines whether a post type can be used for entities.
403
-	 *
404
-	 * Criteria is that the post type is public. The list of valid post types
405
-	 * can be overridden with a filter.
406
-	 *
407
-	 * @param string $post_type A post type name.
408
-	 *
409
-	 * @return bool Return true if the post type can be used for entities, otherwise false.
410
-	 * @since 3.15.0
411
-	 *
412
-	 */
413
-	public static function is_valid_entity_post_type( $post_type ) {
414
-
415
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
416
-	}
248
+        if ( false === $term ) {
249
+            $this->log->warn( "No term found for URI $type_uri, will use Thing." );
250
+
251
+            $term = $this->get_term_by_slug( 'thing' );
252
+
253
+            // We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
254
+            // is installed.
255
+            if ( false === $term ) {
256
+                return;
257
+            }
258
+        }
259
+
260
+        $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
261
+
262
+        // `$replace` is passed to decide whether to replace or append the term.
263
+        wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
264
+
265
+    }
266
+
267
+    /**
268
+     * Get an entity type term given its slug.
269
+     *
270
+     * @param string $slug The slug.
271
+     *
272
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
273
+     *                             or `$term` was not found.
274
+     * @since 3.20.0
275
+     *
276
+     */
277
+    private function get_term_by_slug( $slug ) {
278
+
279
+        return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
280
+    }
281
+
282
+    /**
283
+     * Get an entity type term given its URI.
284
+     *
285
+     * @param string $uri The uri.
286
+     *
287
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
288
+     *                             or `$term` was not found.
289
+     * @since 3.20.0
290
+     *
291
+     */
292
+    public function get_term_by_uri( $uri ) {
293
+
294
+        $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
295
+            'fields'     => 'all',
296
+            'get'        => 'all',
297
+            'number'     => 1,
298
+            'meta_query' => array(
299
+                array(
300
+                    // Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
301
+                    // `WL_ALL_ENTITY_TYPES` is set to true.
302
+                    'key'   => '_wl_uri',
303
+                    'value' => $uri,
304
+                ),
305
+            ),
306
+            'orderby'    => 'term_id',
307
+            'order'      => 'ASC',
308
+        ) );
309
+
310
+        return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
311
+    }
312
+
313
+    /**
314
+     * Check whether an entity type is set for the {@link WP_Post} with the
315
+     * specified id.
316
+     *
317
+     * @param int $post_id The {@link WP_Post}'s `id`.
318
+     * @param string $uri The entity type URI.
319
+     *
320
+     * @return bool True if an entity type is set otherwise false.
321
+     * @since 3.15.0
322
+     *
323
+     */
324
+    public function has_entity_type( $post_id, $uri = null ) {
325
+
326
+        $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
327
+
328
+        // If an URI hasn't been specified just check whether we have at least
329
+        // one entity type.
330
+        if ( null === $uri ) {
331
+
332
+            // Get the post terms for the specified post ID.
333
+            $terms = $this->get_post_terms( $post_id );
334
+
335
+            $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
336
+
337
+            // True if there's at least one term bound to the post.
338
+            return ( 0 < count( $terms ) );
339
+        }
340
+
341
+        $has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
342
+
343
+        $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
344
+
345
+        // Check whether the post has an entity type with that URI.
346
+        return $has_entity_type;
347
+    }
348
+
349
+    /**
350
+     * Get the list of entity types' terms for the specified {@link WP_Post}.
351
+     *
352
+     * @param int $post_id The {@link WP_Post} id.
353
+     *
354
+     * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
355
+     * @since 3.15.0
356
+     *
357
+     */
358
+    private function get_post_terms( $post_id ) {
359
+
360
+        return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
361
+            'hide_empty' => false,
362
+            // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
363
+            // An issue has been opened with the AAM plugin author as well.
364
+            //
365
+            // see https://github.com/insideout10/wordlift-plugin/issues/334
366
+            // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
367
+            'fields'     => 'all',
368
+        ) );
369
+    }
370
+
371
+    /**
372
+     * Get an entity type term given its URI.
373
+     *
374
+     * @param int $post_id The {@link WP_Post} id.
375
+     * @param string $uri The entity type URI.
376
+     *
377
+     * @return bool True if the post has that type URI bound to it otherwise false.
378
+     * @since 3.15.0
379
+     *
380
+     * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
381
+     */
382
+    private function has_post_term_by_uri( $post_id, $uri ) {
383
+
384
+        // Get the post terms bound to the specified post.
385
+        $terms = $this->get_post_terms( $post_id );
386
+
387
+        // Look for a term if the specified URI.
388
+        foreach ( $terms as $term ) {
389
+            $term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
390
+
391
+            if ( $uri === $term_uri ) {
392
+                return true;
393
+            }
394
+        }
395
+
396
+        // Return null.
397
+        return false;
398
+    }
399
+
400
+
401
+    /**
402
+     * Determines whether a post type can be used for entities.
403
+     *
404
+     * Criteria is that the post type is public. The list of valid post types
405
+     * can be overridden with a filter.
406
+     *
407
+     * @param string $post_type A post type name.
408
+     *
409
+     * @return bool Return true if the post type can be used for entities, otherwise false.
410
+     * @since 3.15.0
411
+     *
412
+     */
413
+    public static function is_valid_entity_post_type( $post_type ) {
414
+
415
+        return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
416
+    }
417 417
 
418 418
 }
Please login to merge, or discard this patch.
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -54,9 +54,9 @@  discard block
 block discarded – undo
54 54
 	 * @since 3.7.0
55 55
 	 *
56 56
 	 */
57
-	public function __construct( $schema_service ) {
57
+	public function __construct($schema_service) {
58 58
 
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
59
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Service');
60 60
 
61 61
 		$this->schema_service = $schema_service;
62 62
 
@@ -108,34 +108,34 @@  discard block
 block discarded – undo
108 108
 	 *          assigned the `WebPage` entity type by default.
109 109
 	 *
110 110
 	 */
111
-	public function get( $post_id ) {
111
+	public function get($post_id) {
112 112
 
113
-		$this->log->trace( "Getting the post type for post $post_id..." );
113
+		$this->log->trace("Getting the post type for post $post_id...");
114 114
 
115 115
 		// Get the post type.
116
-		$post_type = get_post_type( $post_id );
116
+		$post_type = get_post_type($post_id);
117 117
 
118 118
 		// Return `web-page` for non entities.
119
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
-			$this->log->info( "Returning `web-page` for post $post_id." );
119
+		if ( ! self::is_valid_entity_post_type($post_type)) {
120
+			$this->log->info("Returning `web-page` for post $post_id.");
121 121
 
122
-			return $this->schema_service->get_schema( 'web-page' );
122
+			return $this->schema_service->get_schema('web-page');
123 123
 		}
124 124
 
125 125
 		// Get the type from the associated classification.
126
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
126
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
127 127
 
128 128
 		// Return the schema type if there is a term found.
129
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
129
+		if ( ! is_wp_error($terms) && ! empty($terms)) {
130 130
 			// Cycle through the terms and return the first one with a valid schema.
131
-			foreach ( $terms as $term ) {
132
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
131
+			foreach ($terms as $term) {
132
+				$this->log->debug("Found `{$term->slug}` term for post $post_id.");
133 133
 
134 134
 				// Try to get the schema for the term.
135
-				$schema = $this->schema_service->get_schema( $term->slug );
135
+				$schema = $this->schema_service->get_schema($term->slug);
136 136
 
137 137
 				// If found, return it, ignoring the other types.
138
-				if ( null !== $schema ) {
138
+				if (null !== $schema) {
139 139
 					// Return the entity type with the specified id.
140 140
 					return $schema;
141 141
 				}
@@ -151,22 +151,22 @@  discard block
 block discarded – undo
151 151
 			 * @since 3.20.0
152 152
 			 */
153 153
 
154
-			return $this->schema_service->get_schema( 'thing' );
154
+			return $this->schema_service->get_schema('thing');
155 155
 		}
156 156
 
157 157
 		// If it's a page or post return `Article`.
158
-		if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
158
+		if (in_array($post_type, array('post', 'page'))) {
159
+			$this->log->debug("Post $post_id has no terms, and it's a `post` type, returning `Article`.");
160 160
 
161 161
 			// Return "Article" schema type for posts.
162
-			return $this->schema_service->get_schema( 'article' );
162
+			return $this->schema_service->get_schema('article');
163 163
 		}
164 164
 
165 165
 		// Return "Thing" schema type for entities.
166
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
166
+		$this->log->debug("Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`.");
167 167
 
168 168
 		// Return the entity type with the specified id.
169
-		return $this->schema_service->get_schema( 'thing' );
169
+		return $this->schema_service->get_schema('thing');
170 170
 
171 171
 	}
172 172
 
@@ -179,9 +179,9 @@  discard block
 block discarded – undo
179 179
 	 * @since 3.20.0
180 180
 	 *
181 181
 	 */
182
-	public function get_ids( $post_id ) {
182
+	public function get_ids($post_id) {
183 183
 
184
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
184
+		return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('fields' => 'ids',));
185 185
 	}
186 186
 
187 187
 	/**
@@ -193,13 +193,13 @@  discard block
 block discarded – undo
193 193
 	 * @since 3.20.0
194 194
 	 *
195 195
 	 */
196
-	public function get_names( $post_id ) {
196
+	public function get_names($post_id) {
197 197
 
198
-		$ids = $this->get_ids( $post_id );
198
+		$ids = $this->get_ids($post_id);
199 199
 
200
-		$camel_case_names = array_map( function ( $id ) {
201
-			return get_term_meta( $id, '_wl_name', true );
202
-		}, $ids );
200
+		$camel_case_names = array_map(function($id) {
201
+			return get_term_meta($id, '_wl_name', true);
202
+		}, $ids);
203 203
 
204 204
 		return $camel_case_names;
205 205
 	}
@@ -214,25 +214,25 @@  discard block
 block discarded – undo
214 214
 	 * @since 3.8.0
215 215
 	 *
216 216
 	 */
217
-	public function set( $post_id, $type_uri, $replace = true ) {
217
+	public function set($post_id, $type_uri, $replace = true) {
218 218
 
219 219
 		// If the type URI is empty we remove the type.
220
-		if ( empty( $type_uri ) ) {
221
-			$this->log->debug( "Removing entity type for post $post_id..." );
220
+		if (empty($type_uri)) {
221
+			$this->log->debug("Removing entity type for post $post_id...");
222 222
 
223
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
223
+			wp_set_object_terms($post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
224 224
 
225 225
 			return;
226 226
 		}
227 227
 
228
-		$this->log->debug( "Setting entity type for post $post_id..." );
228
+		$this->log->debug("Setting entity type for post $post_id...");
229 229
 
230 230
 		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
231
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
231
+		$term = (0 === strpos($type_uri, 'wl-'))
232 232
 			// Get term by slug.
233
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
233
+			? $this->get_term_by_slug(substr($type_uri, 3))
234 234
 			// Get term by URI.
235
-			: $this->get_term_by_uri( $type_uri );
235
+			: $this->get_term_by_uri($type_uri);
236 236
 
237 237
 		/*
238 238
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
@@ -245,22 +245,22 @@  discard block
 block discarded – undo
245 245
 		 *
246 246
 		 * @since 3.23.4
247 247
 		 */
248
-		if ( false === $term ) {
249
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
248
+		if (false === $term) {
249
+			$this->log->warn("No term found for URI $type_uri, will use Thing.");
250 250
 
251
-			$term = $this->get_term_by_slug( 'thing' );
251
+			$term = $this->get_term_by_slug('thing');
252 252
 
253 253
 			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
254 254
 			// is installed.
255
-			if ( false === $term ) {
255
+			if (false === $term) {
256 256
 				return;
257 257
 			}
258 258
 		}
259 259
 
260
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
260
+		$this->log->debug("Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]...");
261 261
 
262 262
 		// `$replace` is passed to decide whether to replace or append the term.
263
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
263
+		wp_set_object_terms($post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace);
264 264
 
265 265
 	}
266 266
 
@@ -274,9 +274,9 @@  discard block
 block discarded – undo
274 274
 	 * @since 3.20.0
275 275
 	 *
276 276
 	 */
277
-	private function get_term_by_slug( $slug ) {
277
+	private function get_term_by_slug($slug) {
278 278
 
279
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
279
+		return get_term_by('slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
280 280
 	}
281 281
 
282 282
 	/**
@@ -289,9 +289,9 @@  discard block
 block discarded – undo
289 289
 	 * @since 3.20.0
290 290
 	 *
291 291
 	 */
292
-	public function get_term_by_uri( $uri ) {
292
+	public function get_term_by_uri($uri) {
293 293
 
294
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
294
+		$terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
295 295
 			'fields'     => 'all',
296 296
 			'get'        => 'all',
297 297
 			'number'     => 1,
@@ -305,9 +305,9 @@  discard block
 block discarded – undo
305 305
 			),
306 306
 			'orderby'    => 'term_id',
307 307
 			'order'      => 'ASC',
308
-		) );
308
+		));
309 309
 
310
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
310
+		return is_array($terms) && ! empty($terms) ? $terms[0] : false;
311 311
 	}
312 312
 
313 313
 	/**
@@ -321,26 +321,26 @@  discard block
 block discarded – undo
321 321
 	 * @since 3.15.0
322 322
 	 *
323 323
 	 */
324
-	public function has_entity_type( $post_id, $uri = null ) {
324
+	public function has_entity_type($post_id, $uri = null) {
325 325
 
326
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
326
+		$this->log->debug("Checking if post $post_id has an entity type [ $uri ]...");
327 327
 
328 328
 		// If an URI hasn't been specified just check whether we have at least
329 329
 		// one entity type.
330
-		if ( null === $uri ) {
330
+		if (null === $uri) {
331 331
 
332 332
 			// Get the post terms for the specified post ID.
333
-			$terms = $this->get_post_terms( $post_id );
333
+			$terms = $this->get_post_terms($post_id);
334 334
 
335
-			$this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
335
+			$this->log->debug("Post $post_id has ".count($terms).' type(s).');
336 336
 
337 337
 			// True if there's at least one term bound to the post.
338
-			return ( 0 < count( $terms ) );
338
+			return (0 < count($terms));
339 339
 		}
340 340
 
341
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
341
+		$has_entity_type = (null !== $this->has_post_term_by_uri($post_id, $uri));
342 342
 
343
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
343
+		$this->log->debug("Post $post_id has $uri type: ".($has_entity_type ? 'yes' : 'no'));
344 344
 
345 345
 		// Check whether the post has an entity type with that URI.
346 346
 		return $has_entity_type;
@@ -355,9 +355,9 @@  discard block
 block discarded – undo
355 355
 	 * @since 3.15.0
356 356
 	 *
357 357
 	 */
358
-	private function get_post_terms( $post_id ) {
358
+	private function get_post_terms($post_id) {
359 359
 
360
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
360
+		return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
361 361
 			'hide_empty' => false,
362 362
 			// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
363 363
 			// An issue has been opened with the AAM plugin author as well.
@@ -365,7 +365,7 @@  discard block
 block discarded – undo
365 365
 			// see https://github.com/insideout10/wordlift-plugin/issues/334
366 366
 			// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
367 367
 			'fields'     => 'all',
368
-		) );
368
+		));
369 369
 	}
370 370
 
371 371
 	/**
@@ -379,16 +379,16 @@  discard block
 block discarded – undo
379 379
 	 *
380 380
 	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
381 381
 	 */
382
-	private function has_post_term_by_uri( $post_id, $uri ) {
382
+	private function has_post_term_by_uri($post_id, $uri) {
383 383
 
384 384
 		// Get the post terms bound to the specified post.
385
-		$terms = $this->get_post_terms( $post_id );
385
+		$terms = $this->get_post_terms($post_id);
386 386
 
387 387
 		// Look for a term if the specified URI.
388
-		foreach ( $terms as $term ) {
389
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
388
+		foreach ($terms as $term) {
389
+			$term_uri = get_term_meta($term->term_id, '_wl_uri', true);
390 390
 
391
-			if ( $uri === $term_uri ) {
391
+			if ($uri === $term_uri) {
392 392
 				return true;
393 393
 			}
394 394
 		}
@@ -410,9 +410,9 @@  discard block
 block discarded – undo
410 410
 	 * @since 3.15.0
411 411
 	 *
412 412
 	 */
413
-	public static function is_valid_entity_post_type( $post_type ) {
413
+	public static function is_valid_entity_post_type($post_type) {
414 414
 
415
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
415
+		return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true);
416 416
 	}
417 417
 
418 418
 }
Please login to merge, or discard this patch.