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