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