Completed
Pull Request — develop (#1732)
by
unknown
01:29
created
src/includes/class-wordlift-post-to-jsonld-converter.php 1 patch
Spacing   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -46,11 +46,11 @@  discard block
 block discarded – undo
46 46
 	 *
47 47
 	 * @since 3.10.0
48 48
 	 */
49
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) {
50
-		parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() );
49
+	public function __construct($entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false) {
50
+		parent::__construct($entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create());
51 51
 		$this->disable_convert_filters = $disable_convert_filters;
52 52
 		// Set a reference to the logger.
53
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
53
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter');
54 54
 
55 55
 		self::$instance = $this;
56 56
 
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 	}
63 63
 
64 64
 	public function new_instance_with_filters_disabled() {
65
-		return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true );
65
+		return new static($this->entity_type_service, $this->user_service, $this->attachment_service, true);
66 66
 	}
67 67
 
68 68
 	/**
@@ -76,20 +76,20 @@  discard block
 block discarded – undo
76 76
 	 * @return array A JSON-LD array.
77 77
 	 * @since 3.10.0
78 78
 	 */
79
-	public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) {
79
+	public function convert($post_id, &$references = array(), &$references_infos = array(), $relations = null) {
80 80
 
81 81
 		// Get the post instance.
82
-		$post = get_post( $post_id );
83
-		if ( null === $post ) {
82
+		$post = get_post($post_id);
83
+		if (null === $post) {
84 84
 			// Post not found.
85 85
 			return null;
86 86
 		}
87 87
 
88 88
 		// Get the base JSON-LD and the list of entities referenced by this entity.
89
-		$jsonld = parent::convert( $post_id, $references, $references_infos, $relations );
89
+		$jsonld = parent::convert($post_id, $references, $references_infos, $relations);
90 90
 
91 91
 		// Set WebPage by default.
92
-		if ( empty( $jsonld['@type'] ) ) {
92
+		if (empty($jsonld['@type'])) {
93 93
 			$jsonld['@type'] = 'WebPage';
94 94
 		}
95 95
 
@@ -106,19 +106,19 @@  discard block
 block discarded – undo
106 106
 		 */
107 107
 		try {
108 108
 			$default_timezone = date_default_timezone_get();
109
-			$timezone         = get_option( 'timezone_string' );
110
-			if ( ! empty( $timezone ) ) {
111
-				date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
112
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
113
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
114
-				date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
109
+			$timezone         = get_option('timezone_string');
110
+			if ( ! empty($timezone)) {
111
+				date_default_timezone_set($timezone); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
112
+				$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post);
113
+				$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i:sP', false, $post);
114
+				date_default_timezone_set($default_timezone); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
115 115
 			} else {
116
-				$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
117
-				$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
116
+				$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false);
117
+				$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i', true, $post, false);
118 118
 			}
119
-		} catch ( Exception $e ) {
120
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
121
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
119
+		} catch (Exception $e) {
120
+			$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false);
121
+			$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i', true, $post, false);
122 122
 		}
123 123
 
124 124
 		// Get the word count for the post.
@@ -129,8 +129,8 @@  discard block
 block discarded – undo
129 129
 		 *
130 130
 		 * @since 3.20.0
131 131
 		 */
132
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
133
-			$post_adapter        = new Wordlift_Post_Adapter( $post_id );
132
+		if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) {
133
+			$post_adapter        = new Wordlift_Post_Adapter($post_id);
134 134
 			$jsonld['wordCount'] = $post_adapter->word_count();
135 135
 		}
136 136
 
@@ -141,17 +141,17 @@  discard block
 block discarded – undo
141 141
 		 *
142 142
 		 * @since 3.27.2
143 143
 		 */
144
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
145
-			$post_adapter    = new Wordlift_Post_Adapter( $post_id );
144
+		if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) {
145
+			$post_adapter    = new Wordlift_Post_Adapter($post_id);
146 146
 			$keywords        = $post_adapter->keywords();
147 147
 			$article_section = $post_adapter->article_section();
148 148
 			$comment_count   = $post_adapter->comment_count();
149 149
 			$locale          = $post_adapter->locale();
150 150
 
151
-			if ( isset( $keywords ) ) {
151
+			if (isset($keywords)) {
152 152
 				$jsonld['keywords'] = $keywords;
153 153
 			}
154
-			if ( ! empty( $article_section ) ) {
154
+			if ( ! empty($article_section)) {
155 155
 				$jsonld['articleSection'] = $article_section;
156 156
 			}
157 157
 			$jsonld['commentCount'] = $comment_count;
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
 		}
160 160
 
161 161
 		// Set the publisher.
162
-		$this->set_publisher( $jsonld );
162
+		$this->set_publisher($jsonld);
163 163
 
164 164
 		/**
165 165
 		 * Call the `wl_post_jsonld_author` filter.
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 		$ret_val = apply_filters(
183 183
 			'wl_jsonld_author',
184 184
 			array(
185
-				'author'     => $this->get_author( $post->post_author, $references ),
185
+				'author'     => $this->get_author($post->post_author, $references),
186 186
 				'references' => $references,
187 187
 			),
188 188
 			$post_id
@@ -196,13 +196,13 @@  discard block
 block discarded – undo
196 196
 		 *
197 197
 		 * @since 3.53.2
198 198
 		 */
199
-		if ( ! empty( $ret_val['author'] ) ) {
199
+		if ( ! empty($ret_val['author'])) {
200 200
 			$jsonld['author'] = $ret_val['author'];
201 201
 			$references       = $ret_val['references'];
202 202
 		}
203 203
 
204 204
 		// Return the JSON-LD if filters are disabled by the client.
205
-		if ( $this->disable_convert_filters ) {
205
+		if ($this->disable_convert_filters) {
206 206
 			return $jsonld;
207 207
 		}
208 208
 
@@ -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
 	/**
@@ -262,14 +262,14 @@  discard block
 block discarded – undo
262 262
 	 * @return string|array A JSON-LD structure.
263 263
 	 * @since 3.14.0
264 264
 	 */
265
-	public function get_author( $author_id, &$references ) {
265
+	public function get_author($author_id, &$references) {
266 266
 
267 267
 		// Get the entity bound to this user.
268
-		$entity_id = $this->user_service->get_entity( $author_id );
268
+		$entity_id = $this->user_service->get_entity($author_id);
269 269
 
270
-		if ( ! empty( $entity_id ) && 'publish' === get_post_status( $entity_id ) ) {
270
+		if ( ! empty($entity_id) && 'publish' === get_post_status($entity_id)) {
271 271
 			// Add the author to the references.
272
-			$author_uri   = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
272
+			$author_uri   = Wordlift_Entity_Service::get_instance()->get_uri($entity_id);
273 273
 			$references[] = $entity_id;
274 274
 
275 275
 			// Return the JSON-LD for the referenced entity.
@@ -279,11 +279,11 @@  discard block
 block discarded – undo
279 279
 		}
280 280
 
281 281
 		// If there's no entity bound return a simple author structure.
282
-		if ( false !== get_userdata( $author_id ) ) {
283
-			$author            = get_the_author_meta( 'display_name', $author_id );
284
-			$author_first_name = get_the_author_meta( 'first_name', $author_id );
285
-			$author_last_name  = get_the_author_meta( 'last_name', $author_id );
286
-			$author_uri        = $this->user_service->get_uri( $author_id );
282
+		if (false !== get_userdata($author_id)) {
283
+			$author            = get_the_author_meta('display_name', $author_id);
284
+			$author_first_name = get_the_author_meta('first_name', $author_id);
285
+			$author_last_name  = get_the_author_meta('last_name', $author_id);
286
+			$author_uri        = $this->user_service->get_uri($author_id);
287 287
 
288 288
 			return array(
289 289
 				'@type'      => 'Person',
@@ -291,7 +291,7 @@  discard block
 block discarded – undo
291 291
 				'name'       => $author,
292 292
 				'givenName'  => $author_first_name,
293 293
 				'familyName' => $author_last_name,
294
-				'url'        => get_author_posts_url( $author_id ),
294
+				'url'        => get_author_posts_url($author_id),
295 295
 			);
296 296
 		}
297 297
 
@@ -306,41 +306,41 @@  discard block
 block discarded – undo
306 306
 	 *
307 307
 	 * @since 3.10.0
308 308
 	 */
309
-	protected function set_publisher( &$params ) {
309
+	protected function set_publisher(&$params) {
310 310
 
311 311
 		// If the publisher id isn't set don't do anything.
312 312
 		$publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
313
-		if ( empty( $publisher_id ) ) {
313
+		if (empty($publisher_id)) {
314 314
 			return;
315 315
 		}
316 316
 
317 317
 		// Get the post instance.
318
-		$post = get_post( $publisher_id );
319
-		if ( ! is_a( $post, '\WP_Post' ) ) {
318
+		$post = get_post($publisher_id);
319
+		if ( ! is_a($post, '\WP_Post')) {
320 320
 			// Publisher not found.
321 321
 			return;
322 322
 		}
323 323
 
324 324
 		// Get the item id.
325
-		$id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id );
325
+		$id = Wordlift_Entity_Service::get_instance()->get_uri($publisher_id);
326 326
 
327 327
 		// Get the type.
328
-		$type = $this->entity_type_service->get( $publisher_id );
328
+		$type = $this->entity_type_service->get($publisher_id);
329 329
 
330 330
 		// Get the name.
331 331
 		$name = $post->post_title;
332 332
 
333 333
 		// Set the publisher data.
334 334
 		$params['publisher'] = array(
335
-			'@type' => $this->relative_to_context( $type['uri'] ),
335
+			'@type' => $this->relative_to_context($type['uri']),
336 336
 			'@id'   => $id,
337 337
 			'name'  => $name,
338 338
 		);
339 339
 
340 340
 		// Add the sameAs values associated with the publisher.
341 341
 		$storage_factory = Wordlift_Storage_Factory::get_instance();
342
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
343
-		if ( ! empty( $sameas ) ) {
342
+		$sameas          = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id);
343
+		if ( ! empty($sameas)) {
344 344
 			$params['publisher']['sameAs'] = $sameas;
345 345
 		}
346 346
 
@@ -348,15 +348,15 @@  discard block
 block discarded – undo
348 348
 		// support the logo property.
349 349
 		//
350 350
 		// See http://schema.org/logo.
351
-		if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) {
351
+		if (1 !== preg_match('~Organization$~', $type['uri'])) {
352 352
 			return;
353 353
 		}
354 354
 
355 355
 		// Get the publisher logo.
356
-		$publisher_logo = $this->get_publisher_logo( $post->ID );
356
+		$publisher_logo = $this->get_publisher_logo($post->ID);
357 357
 
358 358
 		// Bail out if the publisher logo isn't set.
359
-		if ( false === $publisher_logo ) {
359
+		if (false === $publisher_logo) {
360 360
 			return;
361 361
 		}
362 362
 
@@ -390,14 +390,14 @@  discard block
 block discarded – undo
390 390
 	 * @since 3.19.2
391 391
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
392 392
 	 */
393
-	private function get_publisher_logo( $post_id ) {
393
+	private function get_publisher_logo($post_id) {
394 394
 
395 395
 		// Get the featured image for the post.
396
-		$thumbnail_id = get_post_thumbnail_id( $post_id );
396
+		$thumbnail_id = get_post_thumbnail_id($post_id);
397 397
 
398 398
 		// Bail out if thumbnail not available.
399
-		if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
400
-			$this->log->info( "Featured image not set for post $post_id." );
399
+		if (empty($thumbnail_id) || 0 === $thumbnail_id) {
400
+			$this->log->info("Featured image not set for post $post_id.");
401 401
 
402 402
 			return false;
403 403
 		}
@@ -406,24 +406,24 @@  discard block
 block discarded – undo
406 406
 		$uploads_dir = wp_upload_dir();
407 407
 
408 408
 		// Get the attachment metadata.
409
-		$metadata = wp_get_attachment_metadata( $thumbnail_id );
409
+		$metadata = wp_get_attachment_metadata($thumbnail_id);
410 410
 
411 411
 		// Bail out if the file isn't set.
412
-		if ( ! isset( $metadata['file'] ) ) {
413
-			$this->log->warn( "Featured image file not found for post $post_id." );
412
+		if ( ! isset($metadata['file'])) {
413
+			$this->log->warn("Featured image file not found for post $post_id.");
414 414
 
415 415
 			return false;
416 416
 		}
417 417
 
418 418
 		// Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
419
-		$path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
419
+		$path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file'];
420 420
 
421 421
 		// Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149
422
-		if ( ! file_exists( $path ) ) {
423
-			$this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
422
+		if ( ! file_exists($path)) {
423
+			$this->log->warn("Featured image file $path doesn't exist for post $post_id.");
424 424
 
425
-			$attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' );
426
-			if ( $attachment_image_src ) {
425
+			$attachment_image_src = wp_get_attachment_image_src($thumbnail_id, '');
426
+			if ($attachment_image_src) {
427 427
 				return array(
428 428
 					'url'    => $attachment_image_src[0],
429 429
 					'width'  => $attachment_image_src[1],
@@ -437,27 +437,27 @@  discard block
 block discarded – undo
437 437
 		}
438 438
 
439 439
 		// Try to get the image editor and bail out if the editor cannot be instantiated.
440
-		$original_file_editor = wp_get_image_editor( $path );
441
-		if ( is_wp_error( $original_file_editor ) ) {
442
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
440
+		$original_file_editor = wp_get_image_editor($path);
441
+		if (is_wp_error($original_file_editor)) {
442
+			$this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id.");
443 443
 
444 444
 			return false;
445 445
 		}
446 446
 
447 447
 		// Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
448 448
 		// and we don't actually know the end values.
449
-		$publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
449
+		$publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo');
450 450
 
451 451
 		// If the file doesn't exist yet, create it.
452
-		if ( ! file_exists( $publisher_logo_path ) ) {
453
-			$original_file_editor->resize( 600, 60 );
454
-			$original_file_editor->save( $publisher_logo_path );
452
+		if ( ! file_exists($publisher_logo_path)) {
453
+			$original_file_editor->resize(600, 60);
454
+			$original_file_editor->save($publisher_logo_path);
455 455
 		}
456 456
 
457 457
 		// Try to get the image editor and bail out if the editor cannot be instantiated.
458
-		$publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
459
-		if ( is_wp_error( $publisher_logo_editor ) ) {
460
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
458
+		$publisher_logo_editor = wp_get_image_editor($publisher_logo_path);
459
+		if (is_wp_error($publisher_logo_editor)) {
460
+			$this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id.");
461 461
 
462 462
 			return false;
463 463
 		}
@@ -467,7 +467,7 @@  discard block
 block discarded – undo
467 467
 
468 468
 		// Finally return the array with data.
469 469
 		return array(
470
-			'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
470
+			'url'    => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])),
471 471
 			'width'  => $size['width'],
472 472
 			'height' => $size['height'],
473 473
 		);
Please login to merge, or discard this patch.