Completed
Push — develop ( 581605...baada6 )
by Naveen
01:59 queued 51s
created
src/includes/class-wordlift-post-to-jsonld-converter.php 2 patches
Indentation   +518 added lines, -518 removed lines patch added patch discarded remove patch
@@ -19,542 +19,542 @@
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter {
21 21
 
22
-	/**
23
-	 * @var Wordlift_Post_To_Jsonld_Converter
24
-	 */
25
-	private static $instance;
26
-
27
-	/**
28
-	 * A {@link Wordlift_Log_Service} instance.
29
-	 *
30
-	 * @since  3.10.0
31
-	 * @access private
32
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
33
-	 */
34
-	private $log;
35
-
36
-	/**
37
-	 * @var false
38
-	 */
39
-	private $disable_convert_filters;
40
-	/**
41
-	 * @var Object_Relation_Service
42
-	 */
43
-	private $object_relation_service;
44
-
45
-	/**
46
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
47
-	 *
48
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
49
-	 * @param \Wordlift_User_Service        $user_service A {@link Wordlift_User_Service} instance.
50
-	 * @param \Wordlift_Attachment_Service  $attachment_service A {@link Wordlift_Attachment_Service} instance.
51
-	 *
52
-	 * @since 3.10.0
53
-	 */
54
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) {
55
-		parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() );
56
-		$this->disable_convert_filters = $disable_convert_filters;
57
-		$this->object_relation_service = Object_Relation_Service::get_instance();
58
-		// Set a reference to the logger.
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
60
-
61
-		self::$instance = $this;
62
-
63
-	}
64
-
65
-	public static function get_instance() {
66
-
67
-		return self::$instance;
68
-	}
69
-
70
-	public function new_instance_with_filters_disabled() {
71
-		return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true );
72
-	}
73
-
74
-	/**
75
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
76
-	 * found while processing the post is set in the $references array.
77
-	 *
78
-	 * @param int              $post_id The post id.
79
-	 * @param array<Reference> $references An array of entity references.
80
-	 * @param array            $references_infos
81
-	 *
82
-	 * @return array A JSON-LD array.
83
-	 * @since 3.10.0
84
-	 */
85
-	public function convert( $post_id, &$references = array(), &$references_infos = array() ) {
86
-
87
-		// Get the post instance.
88
-		$post = get_post( $post_id );
89
-		if ( null === $post ) {
90
-			// Post not found.
91
-			return null;
92
-		}
93
-
94
-		// Get the base JSON-LD and the list of entities referenced by this entity.
95
-		$jsonld = parent::convert( $post_id, $references, $references_infos );
96
-
97
-		// Set WebPage by default.
98
-		if ( empty( $jsonld['@type'] ) ) {
99
-			$jsonld['@type'] = 'WebPage';
100
-		}
101
-
102
-		// Get the entity name.
103
-		$jsonld['headline'] = $post->post_title;
104
-
105
-		$custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id );
106
-
107
-		if ( isset( $custom_fields ) ) {
108
-			$this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos );
109
-		}
110
-
111
-		// Set the published and modified dates.
112
-		/*
22
+    /**
23
+     * @var Wordlift_Post_To_Jsonld_Converter
24
+     */
25
+    private static $instance;
26
+
27
+    /**
28
+     * A {@link Wordlift_Log_Service} instance.
29
+     *
30
+     * @since  3.10.0
31
+     * @access private
32
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
33
+     */
34
+    private $log;
35
+
36
+    /**
37
+     * @var false
38
+     */
39
+    private $disable_convert_filters;
40
+    /**
41
+     * @var Object_Relation_Service
42
+     */
43
+    private $object_relation_service;
44
+
45
+    /**
46
+     * Wordlift_Post_To_Jsonld_Converter constructor.
47
+     *
48
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
49
+     * @param \Wordlift_User_Service        $user_service A {@link Wordlift_User_Service} instance.
50
+     * @param \Wordlift_Attachment_Service  $attachment_service A {@link Wordlift_Attachment_Service} instance.
51
+     *
52
+     * @since 3.10.0
53
+     */
54
+    public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) {
55
+        parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() );
56
+        $this->disable_convert_filters = $disable_convert_filters;
57
+        $this->object_relation_service = Object_Relation_Service::get_instance();
58
+        // Set a reference to the logger.
59
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
60
+
61
+        self::$instance = $this;
62
+
63
+    }
64
+
65
+    public static function get_instance() {
66
+
67
+        return self::$instance;
68
+    }
69
+
70
+    public function new_instance_with_filters_disabled() {
71
+        return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true );
72
+    }
73
+
74
+    /**
75
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
76
+     * found while processing the post is set in the $references array.
77
+     *
78
+     * @param int              $post_id The post id.
79
+     * @param array<Reference> $references An array of entity references.
80
+     * @param array            $references_infos
81
+     *
82
+     * @return array A JSON-LD array.
83
+     * @since 3.10.0
84
+     */
85
+    public function convert( $post_id, &$references = array(), &$references_infos = array() ) {
86
+
87
+        // Get the post instance.
88
+        $post = get_post( $post_id );
89
+        if ( null === $post ) {
90
+            // Post not found.
91
+            return null;
92
+        }
93
+
94
+        // Get the base JSON-LD and the list of entities referenced by this entity.
95
+        $jsonld = parent::convert( $post_id, $references, $references_infos );
96
+
97
+        // Set WebPage by default.
98
+        if ( empty( $jsonld['@type'] ) ) {
99
+            $jsonld['@type'] = 'WebPage';
100
+        }
101
+
102
+        // Get the entity name.
103
+        $jsonld['headline'] = $post->post_title;
104
+
105
+        $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id );
106
+
107
+        if ( isset( $custom_fields ) ) {
108
+            $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos );
109
+        }
110
+
111
+        // Set the published and modified dates.
112
+        /*
113 113
 		 * Set the `datePublished` and `dateModified` using the local timezone.
114 114
 		 *
115 115
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/887
116 116
 		 *
117 117
 		 * @since 3.20.0
118 118
 		 */
119
-		try {
120
-			$default_timezone = date_default_timezone_get();
121
-			$timezone         = get_option( 'timezone_string' );
122
-			if ( ! empty( $timezone ) ) {
123
-				date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
124
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
125
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
126
-				date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
127
-			} else {
128
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
129
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
130
-			}
131
-		} catch ( Exception $e ) {
132
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
133
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
134
-		}
135
-
136
-		// Get the word count for the post.
137
-		/*
119
+        try {
120
+            $default_timezone = date_default_timezone_get();
121
+            $timezone         = get_option( 'timezone_string' );
122
+            if ( ! empty( $timezone ) ) {
123
+                date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
124
+                $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
125
+                $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
126
+                date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
127
+            } else {
128
+                $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
129
+                $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
130
+            }
131
+        } catch ( Exception $e ) {
132
+            $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
133
+            $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
134
+        }
135
+
136
+        // Get the word count for the post.
137
+        /*
138 138
 		 * Do not display the `wordCount` on a `WebPage`.
139 139
 		 *
140 140
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/888
141 141
 		 *
142 142
 		 * @since 3.20.0
143 143
 		 */
144
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
145
-			$post_adapter        = new Wordlift_Post_Adapter( $post_id );
146
-			$jsonld['wordCount'] = $post_adapter->word_count();
147
-		}
144
+        if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
145
+            $post_adapter        = new Wordlift_Post_Adapter( $post_id );
146
+            $jsonld['wordCount'] = $post_adapter->word_count();
147
+        }
148 148
 
149
-		/*
149
+        /*
150 150
 		 * Add keywords, articleSection, commentCount and inLanguage properties to `Article` JSON-LD
151 151
 		 *
152 152
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/1140
153 153
 		 *
154 154
 		 * @since 3.27.2
155 155
 		 */
156
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
157
-			$post_adapter    = new Wordlift_Post_Adapter( $post_id );
158
-			$keywords        = $post_adapter->keywords();
159
-			$article_section = $post_adapter->article_section();
160
-			$comment_count   = $post_adapter->comment_count();
161
-			$locale          = $post_adapter->locale();
162
-
163
-			if ( isset( $keywords ) ) {
164
-				$jsonld['keywords'] = $keywords;
165
-			}
166
-			if ( ! empty( $article_section ) ) {
167
-				$jsonld['articleSection'] = $article_section;
168
-			}
169
-			$jsonld['commentCount'] = $comment_count;
170
-			$jsonld['inLanguage']   = $locale;
171
-			$post_adapter->add_references( $post_id, $references );
172
-		}
173
-
174
-		// Set the publisher.
175
-		$this->set_publisher( $jsonld );
176
-
177
-		$references = $this->convert_references( $references );
178
-
179
-		// Process the references if any.
180
-		$this->set_mentions_and_about( $references, $post, $jsonld );
181
-
182
-		// Finally set the author.
183
-		$jsonld['author'] = $this->get_author( $post->post_author, $references );
184
-
185
-		// Return the JSON-LD if filters are disabled by the client.
186
-		if ( $this->disable_convert_filters ) {
187
-			return $jsonld;
188
-		}
189
-
190
-		/**
191
-		 * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
192
-		 *
193
-		 * @param array $value {
194
-		 *
195
-		 * @type array $jsonld The JSON-LD structure.
196
-		 * @type int[] $references An array of post IDs.
197
-		 * }
198
-		 * @since 3.25.0
199
-		 *
200
-		 * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
201
-		 *
202
-		 * @api
203
-		 */
204
-		$ret_val = apply_filters(
205
-			'wl_post_jsonld_array',
206
-			array(
207
-				'jsonld'     => $jsonld,
208
-				'references' => $references,
209
-			),
210
-			$post_id
211
-		);
212
-
213
-		$jsonld     = $ret_val['jsonld'];
214
-		$references = $ret_val['references'];
215
-
216
-		/**
217
-		 * Call the `wl_post_jsonld` filter.
218
-		 *
219
-		 * @param array $jsonld The JSON-LD structure.
220
-		 * @param int $post_id The {@link WP_Post} `id`.
221
-		 * @param array $references The array of referenced entities.
222
-		 *
223
-		 * @since 3.14.0
224
-		 *
225
-		 * @api
226
-		 */
227
-		return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
228
-	}
229
-
230
-	/**
231
-	 * Get the author's JSON-LD fragment.
232
-	 *
233
-	 * The JSON-LD fragment is generated using the {@link WP_User}'s data or
234
-	 * the referenced entity if configured for the {@link WP_User}.
235
-	 *
236
-	 * @param int   $author_id The author {@link WP_User}'s `id`.
237
-	 * @param array $references An array of referenced entities.
238
-	 *
239
-	 * @return string|array A JSON-LD structure.
240
-	 * @since 3.14.0
241
-	 */
242
-	public function get_author( $author_id, &$references ) {
243
-
244
-		// Get the entity bound to this user.
245
-		$entity_id = $this->user_service->get_entity( $author_id );
246
-
247
-		// If there's no entity bound return a simple author structure.
248
-		if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
249
-
250
-			$author            = get_the_author_meta( 'display_name', $author_id );
251
-			$author_first_name = get_the_author_meta( 'first_name', $author_id );
252
-			$author_last_name  = get_the_author_meta( 'last_name', $author_id );
253
-			$author_uri        = $this->user_service->get_uri( $author_id );
254
-
255
-			return array(
256
-				'@type'      => 'Person',
257
-				'@id'        => $author_uri,
258
-				'name'       => $author,
259
-				'givenName'  => $author_first_name,
260
-				'familyName' => $author_last_name,
261
-				'url'        => get_author_posts_url( $author_id ),
262
-			);
263
-		}
264
-
265
-		// Add the author to the references.
266
-		$author_uri   = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
267
-		$references[] = $entity_id;
268
-
269
-		// Return the JSON-LD for the referenced entity.
270
-		return array(
271
-			'@id' => $author_uri,
272
-		);
273
-	}
274
-
275
-	/**
276
-	 * Enrich the provided params array with publisher data, if available.
277
-	 *
278
-	 * @param array $params The parameters array.
279
-	 *
280
-	 * @since 3.10.0
281
-	 */
282
-	protected function set_publisher( &$params ) {
283
-
284
-		// If the publisher id isn't set don't do anything.
285
-		$publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
286
-		if ( null === $publisher_id ) {
287
-			return;
288
-		}
289
-
290
-		// Get the post instance.
291
-		$post = get_post( $publisher_id );
292
-		if ( null === $post ) {
293
-			// Publisher not found.
294
-			return;
295
-		}
296
-
297
-		// Get the item id.
298
-		$id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id );
299
-
300
-		// Get the type.
301
-		$type = $this->entity_type_service->get( $publisher_id );
302
-
303
-		// Get the name.
304
-		$name = $post->post_title;
305
-
306
-		// Set the publisher data.
307
-		$params['publisher'] = array(
308
-			'@type' => $this->relative_to_context( $type['uri'] ),
309
-			'@id'   => $id,
310
-			'name'  => $name,
311
-		);
312
-
313
-		// Add the sameAs values associated with the publisher.
314
-		$storage_factory = Wordlift_Storage_Factory::get_instance();
315
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
316
-		if ( ! empty( $sameas ) ) {
317
-			$params['publisher']['sameAs'] = $sameas;
318
-		}
319
-
320
-		// Set the logo, only for http://schema.org/Organization as Person doesn't
321
-		// support the logo property.
322
-		//
323
-		// See http://schema.org/logo.
324
-		if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) {
325
-			return;
326
-		}
327
-
328
-		// Get the publisher logo.
329
-		$publisher_logo = $this->get_publisher_logo( $post->ID );
330
-
331
-		// Bail out if the publisher logo isn't set.
332
-		if ( false === $publisher_logo ) {
333
-			return;
334
-		}
335
-
336
-		// Copy over some useful properties.
337
-		//
338
-		// See https://developers.google.com/search/docs/data-types/articles.
339
-		$params['publisher']['logo']['@type'] = 'ImageObject';
340
-		$params['publisher']['logo']['url']   = $publisher_logo['url'];
341
-
342
-		// If you specify a "width" or "height" value you should leave out
343
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
344
-		//
345
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
346
-		$params['publisher']['logo']['width']  = $publisher_logo['width'];
347
-		$params['publisher']['logo']['height'] = $publisher_logo['height'];
348
-
349
-	}
350
-
351
-	/**
352
-	 * Get the publisher logo structure.
353
-	 *
354
-	 * The function returns false when the publisher logo cannot be determined, i.e.:
355
-	 *  - the post has no featured image.
356
-	 *  - the featured image has no file.
357
-	 *  - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file.
358
-	 *
359
-	 * @param int $post_id The post id.
360
-	 *
361
-	 * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case
362
-	 *  of errors.
363
-	 * @since 3.19.2
364
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
365
-	 */
366
-	private function get_publisher_logo( $post_id ) {
367
-
368
-		// Get the featured image for the post.
369
-		$thumbnail_id = get_post_thumbnail_id( $post_id );
370
-
371
-		// Bail out if thumbnail not available.
372
-		if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
373
-			$this->log->info( "Featured image not set for post $post_id." );
374
-
375
-			return false;
376
-		}
377
-
378
-		// Get the uploads base URL.
379
-		$uploads_dir = wp_upload_dir();
380
-
381
-		// Get the attachment metadata.
382
-		$metadata = wp_get_attachment_metadata( $thumbnail_id );
383
-
384
-		// Bail out if the file isn't set.
385
-		if ( ! isset( $metadata['file'] ) ) {
386
-			$this->log->warn( "Featured image file not found for post $post_id." );
387
-
388
-			return false;
389
-		}
390
-
391
-		// Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
392
-		$path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
393
-
394
-		// Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149
395
-		if ( ! file_exists( $path ) ) {
396
-			$this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
397
-
398
-			$attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' );
399
-			if ( $attachment_image_src ) {
400
-				return array(
401
-					'url'    => $attachment_image_src[0],
402
-					'width'  => $attachment_image_src[1],
403
-					'height' => $attachment_image_src[2],
404
-				);
405
-			}
406
-
407
-			// Bail out if we cant fetch wp_get_attachment_image_src
408
-			return false;
409
-
410
-		}
411
-
412
-		// Try to get the image editor and bail out if the editor cannot be instantiated.
413
-		$original_file_editor = wp_get_image_editor( $path );
414
-		if ( is_wp_error( $original_file_editor ) ) {
415
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
416
-
417
-			return false;
418
-		}
419
-
420
-		// Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
421
-		// and we don't actually know the end values.
422
-		$publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
423
-
424
-		// If the file doesn't exist yet, create it.
425
-		if ( ! file_exists( $publisher_logo_path ) ) {
426
-			$original_file_editor->resize( 600, 60 );
427
-			$original_file_editor->save( $publisher_logo_path );
428
-		}
429
-
430
-		// Try to get the image editor and bail out if the editor cannot be instantiated.
431
-		$publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
432
-		if ( is_wp_error( $publisher_logo_editor ) ) {
433
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
434
-
435
-			return false;
436
-		}
437
-
438
-		// Get the actual size.
439
-		$size = $publisher_logo_editor->get_size();
440
-
441
-		// Finally return the array with data.
442
-		return array(
443
-			'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
444
-			'width'  => $size['width'],
445
-			'height' => $size['height'],
446
-		);
447
-	}
448
-
449
-	/**
450
-	 * @param $references
451
-	 * @param $post
452
-	 * @param $jsonld
453
-	 *
454
-	 * @return void
455
-	 */
456
-	private function set_mentions_and_about( $references, $post, &$jsonld ) {
457
-
458
-		if ( count( $references ) === 0 ) {
459
-			return;
460
-		}
461
-
462
-		// Prepare the `about` and `mentions` array.
463
-		$mentions = array();
464
-		$about    = array();
465
-
466
-		// If the entity is in the title, then it should be an `about`.
467
-		foreach ( $references as $reference ) {
468
-
469
-			if ( ! $reference instanceof Reference ) {
470
-				// This condition should never be reached.
471
-				continue;
472
-			}
473
-
474
-			// Get the entity labels.
475
-			$labels = Wordlift_Entity_Service::get_instance()->get_labels( $reference->get_id(), $reference->get_type() );
476
-			// Get the entity URI.
477
-			$item = array(
478
-				'@id' => Wordlift_Entity_Service::get_instance()->get_uri( $reference->get_id(), $reference->get_type() ),
479
-			);
480
-
481
-			$escaped_labels = array_map(
482
-				function ( $value ) {
483
-					return preg_quote( $value, '/' );
484
-				},
485
-				$labels
486
-			);
487
-
488
-			$matches = false;
489
-
490
-			// When the title is empty, then we shouldn't yield a match to about section.
491
-			if ( array_filter( $escaped_labels ) ) {
492
-				// Check if the labels match any part of the title.
493
-				$matches = $this->check_title_match( $escaped_labels, $post->post_title );
494
-			}
495
-
496
-			// If the title matches, assign the entity to the about, otherwise to the mentions.
497
-			if ( $matches ) {
498
-				$about[] = $item;
499
-			} else {
500
-				$mentions[] = $item;
501
-			}
502
-		}
503
-
504
-		// If we have abouts, assign them to the JSON-LD.
505
-		if ( 0 < count( $about ) ) {
506
-			$jsonld['about'] = $about;
507
-		}
508
-
509
-		// If we have mentions, assign them to the JSON-LD.
510
-		if ( 0 < count( $mentions ) ) {
511
-			$jsonld['mentions'] = $mentions;
512
-		}
513
-
514
-		return $jsonld;
515
-	}
516
-
517
-	/**
518
-	 * Check if the labels match any part of the title.
519
-	 *
520
-	 * @param $labels array The labels to check.
521
-	 * @param $title string The title to check.
522
-	 *
523
-	 * @return boolean
524
-	 */
525
-	public function check_title_match( $labels, $title ) {
526
-
527
-		// If the title is empty, then we shouldn't yield a match to about section.
528
-		if ( empty( $title ) ) {
529
-			return false;
530
-		}
531
-
532
-		// Check if the labels match any part of the title.
533
-		return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title );
534
-
535
-	}
536
-
537
-	/**
538
-	 * Convert references to abstract data type if we find any.
539
-	 *
540
-	 * @param $references array<int|Reference>
541
-	 *
542
-	 * @return Reference[]
543
-	 */
544
-	private function convert_references( $references ) {
545
-		return array_map(
546
-			function ( $reference ) {
547
-				// Legacy code may still push numerical references to this
548
-				// $references variable, so convert it to post references.
549
-				if ( is_numeric( $reference ) ) {
550
-					  return new Post_Reference( $reference );
551
-				}
552
-
553
-				return $reference;
554
-
555
-			},
556
-			$references
557
-		);
558
-	}
156
+        if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
157
+            $post_adapter    = new Wordlift_Post_Adapter( $post_id );
158
+            $keywords        = $post_adapter->keywords();
159
+            $article_section = $post_adapter->article_section();
160
+            $comment_count   = $post_adapter->comment_count();
161
+            $locale          = $post_adapter->locale();
162
+
163
+            if ( isset( $keywords ) ) {
164
+                $jsonld['keywords'] = $keywords;
165
+            }
166
+            if ( ! empty( $article_section ) ) {
167
+                $jsonld['articleSection'] = $article_section;
168
+            }
169
+            $jsonld['commentCount'] = $comment_count;
170
+            $jsonld['inLanguage']   = $locale;
171
+            $post_adapter->add_references( $post_id, $references );
172
+        }
173
+
174
+        // Set the publisher.
175
+        $this->set_publisher( $jsonld );
176
+
177
+        $references = $this->convert_references( $references );
178
+
179
+        // Process the references if any.
180
+        $this->set_mentions_and_about( $references, $post, $jsonld );
181
+
182
+        // Finally set the author.
183
+        $jsonld['author'] = $this->get_author( $post->post_author, $references );
184
+
185
+        // Return the JSON-LD if filters are disabled by the client.
186
+        if ( $this->disable_convert_filters ) {
187
+            return $jsonld;
188
+        }
189
+
190
+        /**
191
+         * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
192
+         *
193
+         * @param array $value {
194
+         *
195
+         * @type array $jsonld The JSON-LD structure.
196
+         * @type int[] $references An array of post IDs.
197
+         * }
198
+         * @since 3.25.0
199
+         *
200
+         * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
201
+         *
202
+         * @api
203
+         */
204
+        $ret_val = apply_filters(
205
+            'wl_post_jsonld_array',
206
+            array(
207
+                'jsonld'     => $jsonld,
208
+                'references' => $references,
209
+            ),
210
+            $post_id
211
+        );
212
+
213
+        $jsonld     = $ret_val['jsonld'];
214
+        $references = $ret_val['references'];
215
+
216
+        /**
217
+         * Call the `wl_post_jsonld` filter.
218
+         *
219
+         * @param array $jsonld The JSON-LD structure.
220
+         * @param int $post_id The {@link WP_Post} `id`.
221
+         * @param array $references The array of referenced entities.
222
+         *
223
+         * @since 3.14.0
224
+         *
225
+         * @api
226
+         */
227
+        return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
228
+    }
229
+
230
+    /**
231
+     * Get the author's JSON-LD fragment.
232
+     *
233
+     * The JSON-LD fragment is generated using the {@link WP_User}'s data or
234
+     * the referenced entity if configured for the {@link WP_User}.
235
+     *
236
+     * @param int   $author_id The author {@link WP_User}'s `id`.
237
+     * @param array $references An array of referenced entities.
238
+     *
239
+     * @return string|array A JSON-LD structure.
240
+     * @since 3.14.0
241
+     */
242
+    public function get_author( $author_id, &$references ) {
243
+
244
+        // Get the entity bound to this user.
245
+        $entity_id = $this->user_service->get_entity( $author_id );
246
+
247
+        // If there's no entity bound return a simple author structure.
248
+        if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
249
+
250
+            $author            = get_the_author_meta( 'display_name', $author_id );
251
+            $author_first_name = get_the_author_meta( 'first_name', $author_id );
252
+            $author_last_name  = get_the_author_meta( 'last_name', $author_id );
253
+            $author_uri        = $this->user_service->get_uri( $author_id );
254
+
255
+            return array(
256
+                '@type'      => 'Person',
257
+                '@id'        => $author_uri,
258
+                'name'       => $author,
259
+                'givenName'  => $author_first_name,
260
+                'familyName' => $author_last_name,
261
+                'url'        => get_author_posts_url( $author_id ),
262
+            );
263
+        }
264
+
265
+        // Add the author to the references.
266
+        $author_uri   = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
267
+        $references[] = $entity_id;
268
+
269
+        // Return the JSON-LD for the referenced entity.
270
+        return array(
271
+            '@id' => $author_uri,
272
+        );
273
+    }
274
+
275
+    /**
276
+     * Enrich the provided params array with publisher data, if available.
277
+     *
278
+     * @param array $params The parameters array.
279
+     *
280
+     * @since 3.10.0
281
+     */
282
+    protected function set_publisher( &$params ) {
283
+
284
+        // If the publisher id isn't set don't do anything.
285
+        $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
286
+        if ( null === $publisher_id ) {
287
+            return;
288
+        }
289
+
290
+        // Get the post instance.
291
+        $post = get_post( $publisher_id );
292
+        if ( null === $post ) {
293
+            // Publisher not found.
294
+            return;
295
+        }
296
+
297
+        // Get the item id.
298
+        $id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id );
299
+
300
+        // Get the type.
301
+        $type = $this->entity_type_service->get( $publisher_id );
302
+
303
+        // Get the name.
304
+        $name = $post->post_title;
305
+
306
+        // Set the publisher data.
307
+        $params['publisher'] = array(
308
+            '@type' => $this->relative_to_context( $type['uri'] ),
309
+            '@id'   => $id,
310
+            'name'  => $name,
311
+        );
312
+
313
+        // Add the sameAs values associated with the publisher.
314
+        $storage_factory = Wordlift_Storage_Factory::get_instance();
315
+        $sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
316
+        if ( ! empty( $sameas ) ) {
317
+            $params['publisher']['sameAs'] = $sameas;
318
+        }
319
+
320
+        // Set the logo, only for http://schema.org/Organization as Person doesn't
321
+        // support the logo property.
322
+        //
323
+        // See http://schema.org/logo.
324
+        if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) {
325
+            return;
326
+        }
327
+
328
+        // Get the publisher logo.
329
+        $publisher_logo = $this->get_publisher_logo( $post->ID );
330
+
331
+        // Bail out if the publisher logo isn't set.
332
+        if ( false === $publisher_logo ) {
333
+            return;
334
+        }
335
+
336
+        // Copy over some useful properties.
337
+        //
338
+        // See https://developers.google.com/search/docs/data-types/articles.
339
+        $params['publisher']['logo']['@type'] = 'ImageObject';
340
+        $params['publisher']['logo']['url']   = $publisher_logo['url'];
341
+
342
+        // If you specify a "width" or "height" value you should leave out
343
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
344
+        //
345
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
346
+        $params['publisher']['logo']['width']  = $publisher_logo['width'];
347
+        $params['publisher']['logo']['height'] = $publisher_logo['height'];
348
+
349
+    }
350
+
351
+    /**
352
+     * Get the publisher logo structure.
353
+     *
354
+     * The function returns false when the publisher logo cannot be determined, i.e.:
355
+     *  - the post has no featured image.
356
+     *  - the featured image has no file.
357
+     *  - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file.
358
+     *
359
+     * @param int $post_id The post id.
360
+     *
361
+     * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case
362
+     *  of errors.
363
+     * @since 3.19.2
364
+     * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
365
+     */
366
+    private function get_publisher_logo( $post_id ) {
367
+
368
+        // Get the featured image for the post.
369
+        $thumbnail_id = get_post_thumbnail_id( $post_id );
370
+
371
+        // Bail out if thumbnail not available.
372
+        if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
373
+            $this->log->info( "Featured image not set for post $post_id." );
374
+
375
+            return false;
376
+        }
377
+
378
+        // Get the uploads base URL.
379
+        $uploads_dir = wp_upload_dir();
380
+
381
+        // Get the attachment metadata.
382
+        $metadata = wp_get_attachment_metadata( $thumbnail_id );
383
+
384
+        // Bail out if the file isn't set.
385
+        if ( ! isset( $metadata['file'] ) ) {
386
+            $this->log->warn( "Featured image file not found for post $post_id." );
387
+
388
+            return false;
389
+        }
390
+
391
+        // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
392
+        $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
393
+
394
+        // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149
395
+        if ( ! file_exists( $path ) ) {
396
+            $this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
397
+
398
+            $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' );
399
+            if ( $attachment_image_src ) {
400
+                return array(
401
+                    'url'    => $attachment_image_src[0],
402
+                    'width'  => $attachment_image_src[1],
403
+                    'height' => $attachment_image_src[2],
404
+                );
405
+            }
406
+
407
+            // Bail out if we cant fetch wp_get_attachment_image_src
408
+            return false;
409
+
410
+        }
411
+
412
+        // Try to get the image editor and bail out if the editor cannot be instantiated.
413
+        $original_file_editor = wp_get_image_editor( $path );
414
+        if ( is_wp_error( $original_file_editor ) ) {
415
+            $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
416
+
417
+            return false;
418
+        }
419
+
420
+        // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
421
+        // and we don't actually know the end values.
422
+        $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
423
+
424
+        // If the file doesn't exist yet, create it.
425
+        if ( ! file_exists( $publisher_logo_path ) ) {
426
+            $original_file_editor->resize( 600, 60 );
427
+            $original_file_editor->save( $publisher_logo_path );
428
+        }
429
+
430
+        // Try to get the image editor and bail out if the editor cannot be instantiated.
431
+        $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
432
+        if ( is_wp_error( $publisher_logo_editor ) ) {
433
+            $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
434
+
435
+            return false;
436
+        }
437
+
438
+        // Get the actual size.
439
+        $size = $publisher_logo_editor->get_size();
440
+
441
+        // Finally return the array with data.
442
+        return array(
443
+            'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
444
+            'width'  => $size['width'],
445
+            'height' => $size['height'],
446
+        );
447
+    }
448
+
449
+    /**
450
+     * @param $references
451
+     * @param $post
452
+     * @param $jsonld
453
+     *
454
+     * @return void
455
+     */
456
+    private function set_mentions_and_about( $references, $post, &$jsonld ) {
457
+
458
+        if ( count( $references ) === 0 ) {
459
+            return;
460
+        }
461
+
462
+        // Prepare the `about` and `mentions` array.
463
+        $mentions = array();
464
+        $about    = array();
465
+
466
+        // If the entity is in the title, then it should be an `about`.
467
+        foreach ( $references as $reference ) {
468
+
469
+            if ( ! $reference instanceof Reference ) {
470
+                // This condition should never be reached.
471
+                continue;
472
+            }
473
+
474
+            // Get the entity labels.
475
+            $labels = Wordlift_Entity_Service::get_instance()->get_labels( $reference->get_id(), $reference->get_type() );
476
+            // Get the entity URI.
477
+            $item = array(
478
+                '@id' => Wordlift_Entity_Service::get_instance()->get_uri( $reference->get_id(), $reference->get_type() ),
479
+            );
480
+
481
+            $escaped_labels = array_map(
482
+                function ( $value ) {
483
+                    return preg_quote( $value, '/' );
484
+                },
485
+                $labels
486
+            );
487
+
488
+            $matches = false;
489
+
490
+            // When the title is empty, then we shouldn't yield a match to about section.
491
+            if ( array_filter( $escaped_labels ) ) {
492
+                // Check if the labels match any part of the title.
493
+                $matches = $this->check_title_match( $escaped_labels, $post->post_title );
494
+            }
495
+
496
+            // If the title matches, assign the entity to the about, otherwise to the mentions.
497
+            if ( $matches ) {
498
+                $about[] = $item;
499
+            } else {
500
+                $mentions[] = $item;
501
+            }
502
+        }
503
+
504
+        // If we have abouts, assign them to the JSON-LD.
505
+        if ( 0 < count( $about ) ) {
506
+            $jsonld['about'] = $about;
507
+        }
508
+
509
+        // If we have mentions, assign them to the JSON-LD.
510
+        if ( 0 < count( $mentions ) ) {
511
+            $jsonld['mentions'] = $mentions;
512
+        }
513
+
514
+        return $jsonld;
515
+    }
516
+
517
+    /**
518
+     * Check if the labels match any part of the title.
519
+     *
520
+     * @param $labels array The labels to check.
521
+     * @param $title string The title to check.
522
+     *
523
+     * @return boolean
524
+     */
525
+    public function check_title_match( $labels, $title ) {
526
+
527
+        // If the title is empty, then we shouldn't yield a match to about section.
528
+        if ( empty( $title ) ) {
529
+            return false;
530
+        }
531
+
532
+        // Check if the labels match any part of the title.
533
+        return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title );
534
+
535
+    }
536
+
537
+    /**
538
+     * Convert references to abstract data type if we find any.
539
+     *
540
+     * @param $references array<int|Reference>
541
+     *
542
+     * @return Reference[]
543
+     */
544
+    private function convert_references( $references ) {
545
+        return array_map(
546
+            function ( $reference ) {
547
+                // Legacy code may still push numerical references to this
548
+                // $references variable, so convert it to post references.
549
+                if ( is_numeric( $reference ) ) {
550
+                        return new Post_Reference( $reference );
551
+                }
552
+
553
+                return $reference;
554
+
555
+            },
556
+            $references
557
+        );
558
+    }
559 559
 
560 560
 }
Please login to merge, or discard this patch.
Spacing   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -51,12 +51,12 @@  discard block
 block discarded – undo
51 51
 	 *
52 52
 	 * @since 3.10.0
53 53
 	 */
54
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) {
55
-		parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() );
54
+	public function __construct($entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false) {
55
+		parent::__construct($entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create());
56 56
 		$this->disable_convert_filters = $disable_convert_filters;
57 57
 		$this->object_relation_service = Object_Relation_Service::get_instance();
58 58
 		// Set a reference to the logger.
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
59
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter');
60 60
 
61 61
 		self::$instance = $this;
62 62
 
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 	}
69 69
 
70 70
 	public function new_instance_with_filters_disabled() {
71
-		return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true );
71
+		return new static($this->entity_type_service, $this->user_service, $this->attachment_service, true);
72 72
 	}
73 73
 
74 74
 	/**
@@ -82,30 +82,30 @@  discard block
 block discarded – undo
82 82
 	 * @return array A JSON-LD array.
83 83
 	 * @since 3.10.0
84 84
 	 */
85
-	public function convert( $post_id, &$references = array(), &$references_infos = array() ) {
85
+	public function convert($post_id, &$references = array(), &$references_infos = array()) {
86 86
 
87 87
 		// Get the post instance.
88
-		$post = get_post( $post_id );
89
-		if ( null === $post ) {
88
+		$post = get_post($post_id);
89
+		if (null === $post) {
90 90
 			// Post not found.
91 91
 			return null;
92 92
 		}
93 93
 
94 94
 		// Get the base JSON-LD and the list of entities referenced by this entity.
95
-		$jsonld = parent::convert( $post_id, $references, $references_infos );
95
+		$jsonld = parent::convert($post_id, $references, $references_infos);
96 96
 
97 97
 		// Set WebPage by default.
98
-		if ( empty( $jsonld['@type'] ) ) {
98
+		if (empty($jsonld['@type'])) {
99 99
 			$jsonld['@type'] = 'WebPage';
100 100
 		}
101 101
 
102 102
 		// Get the entity name.
103 103
 		$jsonld['headline'] = $post->post_title;
104 104
 
105
-		$custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id );
105
+		$custom_fields = $this->entity_type_service->get_custom_fields_for_post($post_id);
106 106
 
107
-		if ( isset( $custom_fields ) ) {
108
-			$this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos );
107
+		if (isset($custom_fields)) {
108
+			$this->process_type_custom_fields($jsonld, $custom_fields, $post, $references, $references_infos);
109 109
 		}
110 110
 
111 111
 		// Set the published and modified dates.
@@ -118,19 +118,19 @@  discard block
 block discarded – undo
118 118
 		 */
119 119
 		try {
120 120
 			$default_timezone = date_default_timezone_get();
121
-			$timezone         = get_option( 'timezone_string' );
122
-			if ( ! empty( $timezone ) ) {
123
-				date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
124
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
125
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
126
-				date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
121
+			$timezone         = get_option('timezone_string');
122
+			if ( ! empty($timezone)) {
123
+				date_default_timezone_set($timezone); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
124
+				$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post);
125
+				$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i:sP', false, $post);
126
+				date_default_timezone_set($default_timezone); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
127 127
 			} else {
128
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
129
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
128
+				$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false);
129
+				$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i', true, $post, false);
130 130
 			}
131
-		} catch ( Exception $e ) {
132
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
133
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
131
+		} catch (Exception $e) {
132
+			$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false);
133
+			$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i', true, $post, false);
134 134
 		}
135 135
 
136 136
 		// Get the word count for the post.
@@ -141,8 +141,8 @@  discard block
 block discarded – undo
141 141
 		 *
142 142
 		 * @since 3.20.0
143 143
 		 */
144
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
145
-			$post_adapter        = new Wordlift_Post_Adapter( $post_id );
144
+		if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) {
145
+			$post_adapter        = new Wordlift_Post_Adapter($post_id);
146 146
 			$jsonld['wordCount'] = $post_adapter->word_count();
147 147
 		}
148 148
 
@@ -153,37 +153,37 @@  discard block
 block discarded – undo
153 153
 		 *
154 154
 		 * @since 3.27.2
155 155
 		 */
156
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
157
-			$post_adapter    = new Wordlift_Post_Adapter( $post_id );
156
+		if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) {
157
+			$post_adapter    = new Wordlift_Post_Adapter($post_id);
158 158
 			$keywords        = $post_adapter->keywords();
159 159
 			$article_section = $post_adapter->article_section();
160 160
 			$comment_count   = $post_adapter->comment_count();
161 161
 			$locale          = $post_adapter->locale();
162 162
 
163
-			if ( isset( $keywords ) ) {
163
+			if (isset($keywords)) {
164 164
 				$jsonld['keywords'] = $keywords;
165 165
 			}
166
-			if ( ! empty( $article_section ) ) {
166
+			if ( ! empty($article_section)) {
167 167
 				$jsonld['articleSection'] = $article_section;
168 168
 			}
169 169
 			$jsonld['commentCount'] = $comment_count;
170 170
 			$jsonld['inLanguage']   = $locale;
171
-			$post_adapter->add_references( $post_id, $references );
171
+			$post_adapter->add_references($post_id, $references);
172 172
 		}
173 173
 
174 174
 		// Set the publisher.
175
-		$this->set_publisher( $jsonld );
175
+		$this->set_publisher($jsonld);
176 176
 
177
-		$references = $this->convert_references( $references );
177
+		$references = $this->convert_references($references);
178 178
 
179 179
 		// Process the references if any.
180
-		$this->set_mentions_and_about( $references, $post, $jsonld );
180
+		$this->set_mentions_and_about($references, $post, $jsonld);
181 181
 
182 182
 		// Finally set the author.
183
-		$jsonld['author'] = $this->get_author( $post->post_author, $references );
183
+		$jsonld['author'] = $this->get_author($post->post_author, $references);
184 184
 
185 185
 		// Return the JSON-LD if filters are disabled by the client.
186
-		if ( $this->disable_convert_filters ) {
186
+		if ($this->disable_convert_filters) {
187 187
 			return $jsonld;
188 188
 		}
189 189
 
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
 		 *
225 225
 		 * @api
226 226
 		 */
227
-		return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
227
+		return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references);
228 228
 	}
229 229
 
230 230
 	/**
@@ -239,18 +239,18 @@  discard block
 block discarded – undo
239 239
 	 * @return string|array A JSON-LD structure.
240 240
 	 * @since 3.14.0
241 241
 	 */
242
-	public function get_author( $author_id, &$references ) {
242
+	public function get_author($author_id, &$references) {
243 243
 
244 244
 		// Get the entity bound to this user.
245
-		$entity_id = $this->user_service->get_entity( $author_id );
245
+		$entity_id = $this->user_service->get_entity($author_id);
246 246
 
247 247
 		// If there's no entity bound return a simple author structure.
248
-		if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
248
+		if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) {
249 249
 
250
-			$author            = get_the_author_meta( 'display_name', $author_id );
251
-			$author_first_name = get_the_author_meta( 'first_name', $author_id );
252
-			$author_last_name  = get_the_author_meta( 'last_name', $author_id );
253
-			$author_uri        = $this->user_service->get_uri( $author_id );
250
+			$author            = get_the_author_meta('display_name', $author_id);
251
+			$author_first_name = get_the_author_meta('first_name', $author_id);
252
+			$author_last_name  = get_the_author_meta('last_name', $author_id);
253
+			$author_uri        = $this->user_service->get_uri($author_id);
254 254
 
255 255
 			return array(
256 256
 				'@type'      => 'Person',
@@ -258,12 +258,12 @@  discard block
 block discarded – undo
258 258
 				'name'       => $author,
259 259
 				'givenName'  => $author_first_name,
260 260
 				'familyName' => $author_last_name,
261
-				'url'        => get_author_posts_url( $author_id ),
261
+				'url'        => get_author_posts_url($author_id),
262 262
 			);
263 263
 		}
264 264
 
265 265
 		// Add the author to the references.
266
-		$author_uri   = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
266
+		$author_uri   = Wordlift_Entity_Service::get_instance()->get_uri($entity_id);
267 267
 		$references[] = $entity_id;
268 268
 
269 269
 		// Return the JSON-LD for the referenced entity.
@@ -279,41 +279,41 @@  discard block
 block discarded – undo
279 279
 	 *
280 280
 	 * @since 3.10.0
281 281
 	 */
282
-	protected function set_publisher( &$params ) {
282
+	protected function set_publisher(&$params) {
283 283
 
284 284
 		// If the publisher id isn't set don't do anything.
285 285
 		$publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
286
-		if ( null === $publisher_id ) {
286
+		if (null === $publisher_id) {
287 287
 			return;
288 288
 		}
289 289
 
290 290
 		// Get the post instance.
291
-		$post = get_post( $publisher_id );
292
-		if ( null === $post ) {
291
+		$post = get_post($publisher_id);
292
+		if (null === $post) {
293 293
 			// Publisher not found.
294 294
 			return;
295 295
 		}
296 296
 
297 297
 		// Get the item id.
298
-		$id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id );
298
+		$id = Wordlift_Entity_Service::get_instance()->get_uri($publisher_id);
299 299
 
300 300
 		// Get the type.
301
-		$type = $this->entity_type_service->get( $publisher_id );
301
+		$type = $this->entity_type_service->get($publisher_id);
302 302
 
303 303
 		// Get the name.
304 304
 		$name = $post->post_title;
305 305
 
306 306
 		// Set the publisher data.
307 307
 		$params['publisher'] = array(
308
-			'@type' => $this->relative_to_context( $type['uri'] ),
308
+			'@type' => $this->relative_to_context($type['uri']),
309 309
 			'@id'   => $id,
310 310
 			'name'  => $name,
311 311
 		);
312 312
 
313 313
 		// Add the sameAs values associated with the publisher.
314 314
 		$storage_factory = Wordlift_Storage_Factory::get_instance();
315
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
316
-		if ( ! empty( $sameas ) ) {
315
+		$sameas          = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id);
316
+		if ( ! empty($sameas)) {
317 317
 			$params['publisher']['sameAs'] = $sameas;
318 318
 		}
319 319
 
@@ -321,15 +321,15 @@  discard block
 block discarded – undo
321 321
 		// support the logo property.
322 322
 		//
323 323
 		// See http://schema.org/logo.
324
-		if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) {
324
+		if (1 !== preg_match('~Organization$~', $type['uri'])) {
325 325
 			return;
326 326
 		}
327 327
 
328 328
 		// Get the publisher logo.
329
-		$publisher_logo = $this->get_publisher_logo( $post->ID );
329
+		$publisher_logo = $this->get_publisher_logo($post->ID);
330 330
 
331 331
 		// Bail out if the publisher logo isn't set.
332
-		if ( false === $publisher_logo ) {
332
+		if (false === $publisher_logo) {
333 333
 			return;
334 334
 		}
335 335
 
@@ -363,14 +363,14 @@  discard block
 block discarded – undo
363 363
 	 * @since 3.19.2
364 364
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
365 365
 	 */
366
-	private function get_publisher_logo( $post_id ) {
366
+	private function get_publisher_logo($post_id) {
367 367
 
368 368
 		// Get the featured image for the post.
369
-		$thumbnail_id = get_post_thumbnail_id( $post_id );
369
+		$thumbnail_id = get_post_thumbnail_id($post_id);
370 370
 
371 371
 		// Bail out if thumbnail not available.
372
-		if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
373
-			$this->log->info( "Featured image not set for post $post_id." );
372
+		if (empty($thumbnail_id) || 0 === $thumbnail_id) {
373
+			$this->log->info("Featured image not set for post $post_id.");
374 374
 
375 375
 			return false;
376 376
 		}
@@ -379,24 +379,24 @@  discard block
 block discarded – undo
379 379
 		$uploads_dir = wp_upload_dir();
380 380
 
381 381
 		// Get the attachment metadata.
382
-		$metadata = wp_get_attachment_metadata( $thumbnail_id );
382
+		$metadata = wp_get_attachment_metadata($thumbnail_id);
383 383
 
384 384
 		// Bail out if the file isn't set.
385
-		if ( ! isset( $metadata['file'] ) ) {
386
-			$this->log->warn( "Featured image file not found for post $post_id." );
385
+		if ( ! isset($metadata['file'])) {
386
+			$this->log->warn("Featured image file not found for post $post_id.");
387 387
 
388 388
 			return false;
389 389
 		}
390 390
 
391 391
 		// Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
392
-		$path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
392
+		$path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file'];
393 393
 
394 394
 		// Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149
395
-		if ( ! file_exists( $path ) ) {
396
-			$this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
395
+		if ( ! file_exists($path)) {
396
+			$this->log->warn("Featured image file $path doesn't exist for post $post_id.");
397 397
 
398
-			$attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' );
399
-			if ( $attachment_image_src ) {
398
+			$attachment_image_src = wp_get_attachment_image_src($thumbnail_id, '');
399
+			if ($attachment_image_src) {
400 400
 				return array(
401 401
 					'url'    => $attachment_image_src[0],
402 402
 					'width'  => $attachment_image_src[1],
@@ -410,27 +410,27 @@  discard block
 block discarded – undo
410 410
 		}
411 411
 
412 412
 		// Try to get the image editor and bail out if the editor cannot be instantiated.
413
-		$original_file_editor = wp_get_image_editor( $path );
414
-		if ( is_wp_error( $original_file_editor ) ) {
415
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
413
+		$original_file_editor = wp_get_image_editor($path);
414
+		if (is_wp_error($original_file_editor)) {
415
+			$this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id.");
416 416
 
417 417
 			return false;
418 418
 		}
419 419
 
420 420
 		// Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
421 421
 		// and we don't actually know the end values.
422
-		$publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
422
+		$publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo');
423 423
 
424 424
 		// If the file doesn't exist yet, create it.
425
-		if ( ! file_exists( $publisher_logo_path ) ) {
426
-			$original_file_editor->resize( 600, 60 );
427
-			$original_file_editor->save( $publisher_logo_path );
425
+		if ( ! file_exists($publisher_logo_path)) {
426
+			$original_file_editor->resize(600, 60);
427
+			$original_file_editor->save($publisher_logo_path);
428 428
 		}
429 429
 
430 430
 		// Try to get the image editor and bail out if the editor cannot be instantiated.
431
-		$publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
432
-		if ( is_wp_error( $publisher_logo_editor ) ) {
433
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
431
+		$publisher_logo_editor = wp_get_image_editor($publisher_logo_path);
432
+		if (is_wp_error($publisher_logo_editor)) {
433
+			$this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id.");
434 434
 
435 435
 			return false;
436 436
 		}
@@ -440,7 +440,7 @@  discard block
 block discarded – undo
440 440
 
441 441
 		// Finally return the array with data.
442 442
 		return array(
443
-			'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
443
+			'url'    => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])),
444 444
 			'width'  => $size['width'],
445 445
 			'height' => $size['height'],
446 446
 		);
@@ -453,9 +453,9 @@  discard block
 block discarded – undo
453 453
 	 *
454 454
 	 * @return void
455 455
 	 */
456
-	private function set_mentions_and_about( $references, $post, &$jsonld ) {
456
+	private function set_mentions_and_about($references, $post, &$jsonld) {
457 457
 
458
-		if ( count( $references ) === 0 ) {
458
+		if (count($references) === 0) {
459 459
 			return;
460 460
 		}
461 461
 
@@ -464,23 +464,23 @@  discard block
 block discarded – undo
464 464
 		$about    = array();
465 465
 
466 466
 		// If the entity is in the title, then it should be an `about`.
467
-		foreach ( $references as $reference ) {
467
+		foreach ($references as $reference) {
468 468
 
469
-			if ( ! $reference instanceof Reference ) {
469
+			if ( ! $reference instanceof Reference) {
470 470
 				// This condition should never be reached.
471 471
 				continue;
472 472
 			}
473 473
 
474 474
 			// Get the entity labels.
475
-			$labels = Wordlift_Entity_Service::get_instance()->get_labels( $reference->get_id(), $reference->get_type() );
475
+			$labels = Wordlift_Entity_Service::get_instance()->get_labels($reference->get_id(), $reference->get_type());
476 476
 			// Get the entity URI.
477 477
 			$item = array(
478
-				'@id' => Wordlift_Entity_Service::get_instance()->get_uri( $reference->get_id(), $reference->get_type() ),
478
+				'@id' => Wordlift_Entity_Service::get_instance()->get_uri($reference->get_id(), $reference->get_type()),
479 479
 			);
480 480
 
481 481
 			$escaped_labels = array_map(
482
-				function ( $value ) {
483
-					return preg_quote( $value, '/' );
482
+				function($value) {
483
+					return preg_quote($value, '/');
484 484
 				},
485 485
 				$labels
486 486
 			);
@@ -488,13 +488,13 @@  discard block
 block discarded – undo
488 488
 			$matches = false;
489 489
 
490 490
 			// When the title is empty, then we shouldn't yield a match to about section.
491
-			if ( array_filter( $escaped_labels ) ) {
491
+			if (array_filter($escaped_labels)) {
492 492
 				// Check if the labels match any part of the title.
493
-				$matches = $this->check_title_match( $escaped_labels, $post->post_title );
493
+				$matches = $this->check_title_match($escaped_labels, $post->post_title);
494 494
 			}
495 495
 
496 496
 			// If the title matches, assign the entity to the about, otherwise to the mentions.
497
-			if ( $matches ) {
497
+			if ($matches) {
498 498
 				$about[] = $item;
499 499
 			} else {
500 500
 				$mentions[] = $item;
@@ -502,12 +502,12 @@  discard block
 block discarded – undo
502 502
 		}
503 503
 
504 504
 		// If we have abouts, assign them to the JSON-LD.
505
-		if ( 0 < count( $about ) ) {
505
+		if (0 < count($about)) {
506 506
 			$jsonld['about'] = $about;
507 507
 		}
508 508
 
509 509
 		// If we have mentions, assign them to the JSON-LD.
510
-		if ( 0 < count( $mentions ) ) {
510
+		if (0 < count($mentions)) {
511 511
 			$jsonld['mentions'] = $mentions;
512 512
 		}
513 513
 
@@ -522,15 +522,15 @@  discard block
 block discarded – undo
522 522
 	 *
523 523
 	 * @return boolean
524 524
 	 */
525
-	public function check_title_match( $labels, $title ) {
525
+	public function check_title_match($labels, $title) {
526 526
 
527 527
 		// If the title is empty, then we shouldn't yield a match to about section.
528
-		if ( empty( $title ) ) {
528
+		if (empty($title)) {
529 529
 			return false;
530 530
 		}
531 531
 
532 532
 		// Check if the labels match any part of the title.
533
-		return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title );
533
+		return 1 === preg_match('/\b('.implode('|', $labels).')\b/iu', $title);
534 534
 
535 535
 	}
536 536
 
@@ -541,13 +541,13 @@  discard block
 block discarded – undo
541 541
 	 *
542 542
 	 * @return Reference[]
543 543
 	 */
544
-	private function convert_references( $references ) {
544
+	private function convert_references($references) {
545 545
 		return array_map(
546
-			function ( $reference ) {
546
+			function($reference) {
547 547
 				// Legacy code may still push numerical references to this
548 548
 				// $references variable, so convert it to post references.
549
-				if ( is_numeric( $reference ) ) {
550
-					  return new Post_Reference( $reference );
549
+				if (is_numeric($reference)) {
550
+					  return new Post_Reference($reference);
551 551
 				}
552 552
 
553 553
 				return $reference;
Please login to merge, or discard this patch.