Completed
Push — master ( 960688...66a7fe )
by David
02:47
created
src/includes/class-wordlift-timeline-service.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -313,7 +313,7 @@
 block discarded – undo
313 313
 	/**
314 314
 	 * Convert the date to a date array.
315 315
 	 *
316
-	 * @param $value int A date value.
316
+	 * @param integer $value int A date value.
317 317
 	 *
318 318
 	 * @return array An array containing year, month and day values.
319 319
 	 * @since 3.7.0
Please login to merge, or discard this patch.
Indentation   +358 added lines, -358 removed lines patch added patch discarded remove patch
@@ -16,131 +16,131 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Timeline_Service {
18 18
 
19
-	/**
20
-	 * The Log service.
21
-	 *
22
-	 * @since  3.1.0
23
-	 * @access private
24
-	 * @var \Wordlift_Log_Service $log The Log service.
25
-	 */
26
-	private $log;
27
-
28
-	/**
29
-	 * The Entity service.
30
-	 *
31
-	 * @since  3.1.0
32
-	 * @access private
33
-	 * @var \Wordlift_Entity_Service $entity_service The Entity service.
34
-	 */
35
-	private $entity_service;
36
-
37
-	/**
38
-	 * The number of words to use for the excerpt, set in the `to_json` function
39
-	 * and used by a filter.
40
-	 *
41
-	 * @since  3.7.0
42
-	 * @access private
43
-	 * @var int $excerpt_length The number of words to use for the excerpt.
44
-	 */
45
-	private $excerpt_length;
46
-
47
-	/**
48
-	 * The {@link Wordlift_Entity_Type_Service} instance.
49
-	 *
50
-	 * @since  3.15.0
51
-	 * @access private
52
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
53
-	 */
54
-	private $entity_type_service;
55
-
56
-	/**
57
-	 * A singleton instance of the Timeline service (useful for unit tests).
58
-	 *
59
-	 * @since  3.1.0
60
-	 * @access private
61
-	 * @var \Wordlift_Timeline_Service $instance The singleton instance.
62
-	 */
63
-	private static $instance;
64
-
65
-	/**
66
-	 * Create a Wordlift_Timeline_Service instance.
67
-	 *
68
-	 * @param \Wordlift_Entity_Service $entity_service The Entity service.
69
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
70
-	 *
71
-	 * @since 3.1.0
72
-	 *
73
-	 */
74
-	public function __construct( $entity_service, $entity_type_service ) {
75
-
76
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
77
-
78
-		$this->entity_service      = $entity_service;
79
-		$this->entity_type_service = $entity_type_service;
80
-
81
-		self::$instance = $this;
82
-
83
-	}
84
-
85
-	/**
86
-	 * Get the singleton instance of the Wordlift_Timeline_Service
87
-	 *
88
-	 * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service.
89
-	 * @since 3.1.0
90
-	 *
91
-	 */
92
-	public static function get_instance() {
93
-
94
-		return self::$instance;
95
-	}
96
-
97
-	/**
98
-	 * Retrieve timeline events and output them in JSON.
99
-	 *
100
-	 * @since 3.1.0
101
-	 */
102
-	public function ajax_timeline() {
103
-
104
-		// Get the ID of the post who requested the timeline.
105
-		$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
106
-
107
-		// Get the events and transform them for the JSON response, then send them to the client.
108
-		wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
109
-
110
-	}
111
-
112
-	/**
113
-	 * Retrieve timeline events.
114
-	 *
115
-	 * @param int $post_id The post ID.
116
-	 *
117
-	 * @return array An array of event posts.
118
-	 * @since 3.1.0
119
-	 *
120
-	 * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
121
-	 *
122
-	 */
123
-	public function get_events( $post_id = null ) {
124
-
125
-		// Get the entity IDs either from the entities related to the specified post or from the last 50 published
126
-		// posts if no post has been specified.
127
-		$ids = ( is_numeric( $post_id )
128
-			? wl_core_get_related_entity_ids( $post_id )
129
-			: $this->get_all_related_to_last_50_published_posts() );
130
-
131
-		// Add the post itself if it's an entity.
132
-		if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) {
133
-			$ids[] = $post_id;
134
-		}
135
-
136
-		// If there's no entities, return an empty array right away.
137
-		if ( 0 === sizeof( $ids ) ) {
138
-			$this->log->trace( "No events found [ post id :: $post_id ]" );
139
-
140
-			return array();
141
-		}
142
-
143
-		$this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
19
+    /**
20
+     * The Log service.
21
+     *
22
+     * @since  3.1.0
23
+     * @access private
24
+     * @var \Wordlift_Log_Service $log The Log service.
25
+     */
26
+    private $log;
27
+
28
+    /**
29
+     * The Entity service.
30
+     *
31
+     * @since  3.1.0
32
+     * @access private
33
+     * @var \Wordlift_Entity_Service $entity_service The Entity service.
34
+     */
35
+    private $entity_service;
36
+
37
+    /**
38
+     * The number of words to use for the excerpt, set in the `to_json` function
39
+     * and used by a filter.
40
+     *
41
+     * @since  3.7.0
42
+     * @access private
43
+     * @var int $excerpt_length The number of words to use for the excerpt.
44
+     */
45
+    private $excerpt_length;
46
+
47
+    /**
48
+     * The {@link Wordlift_Entity_Type_Service} instance.
49
+     *
50
+     * @since  3.15.0
51
+     * @access private
52
+     * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
53
+     */
54
+    private $entity_type_service;
55
+
56
+    /**
57
+     * A singleton instance of the Timeline service (useful for unit tests).
58
+     *
59
+     * @since  3.1.0
60
+     * @access private
61
+     * @var \Wordlift_Timeline_Service $instance The singleton instance.
62
+     */
63
+    private static $instance;
64
+
65
+    /**
66
+     * Create a Wordlift_Timeline_Service instance.
67
+     *
68
+     * @param \Wordlift_Entity_Service $entity_service The Entity service.
69
+     * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
70
+     *
71
+     * @since 3.1.0
72
+     *
73
+     */
74
+    public function __construct( $entity_service, $entity_type_service ) {
75
+
76
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
77
+
78
+        $this->entity_service      = $entity_service;
79
+        $this->entity_type_service = $entity_type_service;
80
+
81
+        self::$instance = $this;
82
+
83
+    }
84
+
85
+    /**
86
+     * Get the singleton instance of the Wordlift_Timeline_Service
87
+     *
88
+     * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service.
89
+     * @since 3.1.0
90
+     *
91
+     */
92
+    public static function get_instance() {
93
+
94
+        return self::$instance;
95
+    }
96
+
97
+    /**
98
+     * Retrieve timeline events and output them in JSON.
99
+     *
100
+     * @since 3.1.0
101
+     */
102
+    public function ajax_timeline() {
103
+
104
+        // Get the ID of the post who requested the timeline.
105
+        $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
106
+
107
+        // Get the events and transform them for the JSON response, then send them to the client.
108
+        wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
109
+
110
+    }
111
+
112
+    /**
113
+     * Retrieve timeline events.
114
+     *
115
+     * @param int $post_id The post ID.
116
+     *
117
+     * @return array An array of event posts.
118
+     * @since 3.1.0
119
+     *
120
+     * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
121
+     *
122
+     */
123
+    public function get_events( $post_id = null ) {
124
+
125
+        // Get the entity IDs either from the entities related to the specified post or from the last 50 published
126
+        // posts if no post has been specified.
127
+        $ids = ( is_numeric( $post_id )
128
+            ? wl_core_get_related_entity_ids( $post_id )
129
+            : $this->get_all_related_to_last_50_published_posts() );
130
+
131
+        // Add the post itself if it's an entity.
132
+        if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) {
133
+            $ids[] = $post_id;
134
+        }
135
+
136
+        // If there's no entities, return an empty array right away.
137
+        if ( 0 === sizeof( $ids ) ) {
138
+            $this->log->trace( "No events found [ post id :: $post_id ]" );
139
+
140
+            return array();
141
+        }
142
+
143
+        $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
144 144
 
145 145
 //		$entity_type_service = $this->entity_type_service;
146 146
 
@@ -152,238 +152,238 @@  discard block
 block discarded – undo
152 152
 //			return $entity_type_service->has_entity_type( $item, 'http://schema.org/Event' );
153 153
 //		} ) );
154 154
 
155
-		$args = array(
156
-			'post__in'       => $ids,
157
-			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
158
-			'post_status'    => 'publish',
159
-			'posts_per_page' => - 1,
160
-			'meta_query'     => array(
161
-				'relation' => 'AND',
162
-				array(
163
-					'key'     => Wordlift_Schema_Service::FIELD_DATE_START,
164
-					'value'   => null,
165
-					'compare' => '!=',
166
-				),
167
-				array(
168
-					'key'     => Wordlift_Schema_Service::FIELD_DATE_END,
169
-					'value'   => null,
170
-					'compare' => '!=',
171
-				),
172
-			),
173
-			'tax_query'      => array(
174
-				array(
175
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
176
-					'field'    => 'slug',
177
-					'terms'    => 'event',
178
-				),
179
-			),
180
-		);
181
-
182
-		return get_posts( $args );
183
-	}
184
-
185
-	/**
186
-	 * Convert timeline events to JSON. This function sets the global post in order
187
-	 * to get an automatic excerpt. Since we're being called inside an AJAX request,
188
-	 * we're not taking care of restoring any previous post: there isn't any.
189
-	 *
190
-	 * @param array $posts An array of posts.
191
-	 *
192
-	 * @return array|string An array of timeline events or an empty string if no posts are provided.
193
-	 * @since 3.1.0
194
-	 *
195
-	 */
196
-	public function to_json( $posts ) {
197
-
198
-		// If there are no events, return empty JSON
199
-		if ( empty( $posts ) || is_null( $posts ) ) {
200
-			return '';
201
-		}
202
-
203
-		// {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
204
-		$display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media';
205
-
206
-		// The number of words for the excerpt (by default 55, as WordPress).
207
-		$this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55;
208
-		add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
209
-
210
-		// Add a filter to remove the [...] after excerpts, since we're adding
211
-		// a link to the post itself.
212
-		add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) );
213
-
214
-		// Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
215
-		$start_at_slide = 0;
216
-		$event_index    = - 1;
217
-		$now            = time();
218
-
219
-		// Prepare the timeline variable.
220
-		$timeline = array();
221
-
222
-		// Populate the arrays.
223
-		$timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
224
-
225
-			// Get the start and end dates.
226
-			$start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) );
227
-			$end_date   = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) );
228
-
229
-			// Set the starting slide.
230
-			$event_index ++;
231
-			if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
232
-				$start_at_slide = $event_index;
233
-			}
234
-
235
-			// Load thumbnail
236
-			$thumbnail_id = get_post_thumbnail_id( $item->ID );
237
-			if ( '' !== $thumbnail_id && 0 !== $thumbnail_id
238
-			     && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) )
239
-			) {
240
-
241
-				// Set the thumbnail URL.
242
-				if ( 'background' === $display_images_as ) {
243
-					$date['background'] = array( 'url' => $attachment[0] );
244
-					$date['media']      = array( 'thumbnail' => $attachment[0] );
245
-				} else {
246
-					$date['media'] = array(
247
-						'url'       => $attachment[0],
248
-						'thumbnail' => $attachment[0],
249
-					);
250
-				}
251
-			}
252
-
253
-			// Set the start/end dates by converting them to TimelineJS required format.
254
-			$date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
255
-			$date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
256
-
257
-			setup_postdata( $GLOBALS['post'] = $item );
258
-
259
-			$more_link_text = sprintf(
260
-				'<span aria-label="%1$s">%2$s</span>',
261
-				sprintf(
262
-				/* translators: %s: Name of current post */
263
-					__( 'Continue reading %s' ),
264
-					the_title_attribute( array( 'echo' => false ) )
265
-				),
266
-				__( '(more&hellip;)' )
267
-			);
268
-
269
-			// Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
270
-			$date['text'] = array(
271
-				'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
272
-			);
273
-
274
-			// If we have an excerpt, set it.
275
-			if ( 0 < $excerpt_length ) {
276
-				$date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
277
-			}
278
-
279
-			return $date;
280
-
281
-		}, $posts );
282
-
283
-		// Finally remove the excerpt filter.
284
-		remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
285
-
286
-		// The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
287
-		return array(
288
-			'timeline'       => $timeline,
289
-			'start_at_slide' => $start_at_slide,
290
-		);
291
-	}
292
-
293
-	/**
294
-	 * This function filters {@link excerpt_more} by removing it, since we're
295
-	 * adding the 'read more' link. This filter is set by {@see to_json}.
296
-	 *
297
-	 * @param string $excerpt_more The excerpt more preset.
298
-	 *
299
-	 * @return string An empty string.
300
-	 * @since 3.7.0
301
-	 *
302
-	 */
303
-	public function excerpt_more( $excerpt_more ) {
304
-
305
-		return '';
306
-	}
307
-
308
-	/**
309
-	 * A filter for the excerpt length, set by the `to_json` function, to tailor
310
-	 * how many words to return according to the client setting.
311
-	 *
312
-	 * @param int $length The preset number of words.
313
-	 *
314
-	 * @return int The number of words for the preset.
315
-	 * @since 3.7.0
316
-	 *
317
-	 */
318
-	public function excerpt_length( $length ) {
319
-
320
-		return $this->excerpt_length;
321
-	}
322
-
323
-
324
-	/**
325
-	 * Convert the date to a date array.
326
-	 *
327
-	 * @param $value int A date value.
328
-	 *
329
-	 * @return array An array containing year, month and day values.
330
-	 * @since 3.7.0
331
-	 *
332
-	 */
333
-	public static function date( $value ) {
334
-
335
-		return array(
336
-			'year'  => (int) date( 'Y', $value ),
337
-			'month' => (int) date( 'm', $value ),
338
-			'day'   => (int) date( 'd', $value ),
339
-
340
-		);
341
-	}
342
-
343
-	/**
344
-	 * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to
345
-	 * its specific function).
346
-	 *
347
-	 * @return array An array of post IDs.
348
-	 * @since 3.1.0
349
-	 *
350
-	 */
351
-	public function get_all_related_to_last_50_published_posts() {
352
-
353
-		// Global timeline. Get entities from the latest posts.
354
-		$latest_posts_ids = get_posts( array(
355
-			'numberposts' => 50,
356
-			'fields'      => 'ids', //only get post IDs
357
-			'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
358
-			'tax_query'   => array(
359
-				'relation' => 'OR',
360
-				array(
361
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
362
-					'operator' => 'NOT EXISTS',
363
-				),
364
-				array(
365
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
366
-					'field'    => 'slug',
367
-					'terms'    => 'article',
368
-				),
369
-			),
370
-			'post_status' => 'publish',
371
-		) );
372
-
373
-		if ( empty( $latest_posts_ids ) ) {
374
-			// There are no posts.
375
-			return array();
376
-		}
377
-
378
-		// Collect entities related to latest posts
379
-		$entity_ids = array();
380
-		foreach ( $latest_posts_ids as $id ) {
381
-			$entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array(
382
-				'status' => 'publish',
383
-			) ) );
384
-		}
385
-
386
-		return $entity_ids;
387
-	}
155
+        $args = array(
156
+            'post__in'       => $ids,
157
+            'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
158
+            'post_status'    => 'publish',
159
+            'posts_per_page' => - 1,
160
+            'meta_query'     => array(
161
+                'relation' => 'AND',
162
+                array(
163
+                    'key'     => Wordlift_Schema_Service::FIELD_DATE_START,
164
+                    'value'   => null,
165
+                    'compare' => '!=',
166
+                ),
167
+                array(
168
+                    'key'     => Wordlift_Schema_Service::FIELD_DATE_END,
169
+                    'value'   => null,
170
+                    'compare' => '!=',
171
+                ),
172
+            ),
173
+            'tax_query'      => array(
174
+                array(
175
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
176
+                    'field'    => 'slug',
177
+                    'terms'    => 'event',
178
+                ),
179
+            ),
180
+        );
181
+
182
+        return get_posts( $args );
183
+    }
184
+
185
+    /**
186
+     * Convert timeline events to JSON. This function sets the global post in order
187
+     * to get an automatic excerpt. Since we're being called inside an AJAX request,
188
+     * we're not taking care of restoring any previous post: there isn't any.
189
+     *
190
+     * @param array $posts An array of posts.
191
+     *
192
+     * @return array|string An array of timeline events or an empty string if no posts are provided.
193
+     * @since 3.1.0
194
+     *
195
+     */
196
+    public function to_json( $posts ) {
197
+
198
+        // If there are no events, return empty JSON
199
+        if ( empty( $posts ) || is_null( $posts ) ) {
200
+            return '';
201
+        }
202
+
203
+        // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
204
+        $display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media';
205
+
206
+        // The number of words for the excerpt (by default 55, as WordPress).
207
+        $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55;
208
+        add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
209
+
210
+        // Add a filter to remove the [...] after excerpts, since we're adding
211
+        // a link to the post itself.
212
+        add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) );
213
+
214
+        // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
215
+        $start_at_slide = 0;
216
+        $event_index    = - 1;
217
+        $now            = time();
218
+
219
+        // Prepare the timeline variable.
220
+        $timeline = array();
221
+
222
+        // Populate the arrays.
223
+        $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
224
+
225
+            // Get the start and end dates.
226
+            $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) );
227
+            $end_date   = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) );
228
+
229
+            // Set the starting slide.
230
+            $event_index ++;
231
+            if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
232
+                $start_at_slide = $event_index;
233
+            }
234
+
235
+            // Load thumbnail
236
+            $thumbnail_id = get_post_thumbnail_id( $item->ID );
237
+            if ( '' !== $thumbnail_id && 0 !== $thumbnail_id
238
+                 && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) )
239
+            ) {
240
+
241
+                // Set the thumbnail URL.
242
+                if ( 'background' === $display_images_as ) {
243
+                    $date['background'] = array( 'url' => $attachment[0] );
244
+                    $date['media']      = array( 'thumbnail' => $attachment[0] );
245
+                } else {
246
+                    $date['media'] = array(
247
+                        'url'       => $attachment[0],
248
+                        'thumbnail' => $attachment[0],
249
+                    );
250
+                }
251
+            }
252
+
253
+            // Set the start/end dates by converting them to TimelineJS required format.
254
+            $date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
255
+            $date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
256
+
257
+            setup_postdata( $GLOBALS['post'] = $item );
258
+
259
+            $more_link_text = sprintf(
260
+                '<span aria-label="%1$s">%2$s</span>',
261
+                sprintf(
262
+                /* translators: %s: Name of current post */
263
+                    __( 'Continue reading %s' ),
264
+                    the_title_attribute( array( 'echo' => false ) )
265
+                ),
266
+                __( '(more&hellip;)' )
267
+            );
268
+
269
+            // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
270
+            $date['text'] = array(
271
+                'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
272
+            );
273
+
274
+            // If we have an excerpt, set it.
275
+            if ( 0 < $excerpt_length ) {
276
+                $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
277
+            }
278
+
279
+            return $date;
280
+
281
+        }, $posts );
282
+
283
+        // Finally remove the excerpt filter.
284
+        remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
285
+
286
+        // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
287
+        return array(
288
+            'timeline'       => $timeline,
289
+            'start_at_slide' => $start_at_slide,
290
+        );
291
+    }
292
+
293
+    /**
294
+     * This function filters {@link excerpt_more} by removing it, since we're
295
+     * adding the 'read more' link. This filter is set by {@see to_json}.
296
+     *
297
+     * @param string $excerpt_more The excerpt more preset.
298
+     *
299
+     * @return string An empty string.
300
+     * @since 3.7.0
301
+     *
302
+     */
303
+    public function excerpt_more( $excerpt_more ) {
304
+
305
+        return '';
306
+    }
307
+
308
+    /**
309
+     * A filter for the excerpt length, set by the `to_json` function, to tailor
310
+     * how many words to return according to the client setting.
311
+     *
312
+     * @param int $length The preset number of words.
313
+     *
314
+     * @return int The number of words for the preset.
315
+     * @since 3.7.0
316
+     *
317
+     */
318
+    public function excerpt_length( $length ) {
319
+
320
+        return $this->excerpt_length;
321
+    }
322
+
323
+
324
+    /**
325
+     * Convert the date to a date array.
326
+     *
327
+     * @param $value int A date value.
328
+     *
329
+     * @return array An array containing year, month and day values.
330
+     * @since 3.7.0
331
+     *
332
+     */
333
+    public static function date( $value ) {
334
+
335
+        return array(
336
+            'year'  => (int) date( 'Y', $value ),
337
+            'month' => (int) date( 'm', $value ),
338
+            'day'   => (int) date( 'd', $value ),
339
+
340
+        );
341
+    }
342
+
343
+    /**
344
+     * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to
345
+     * its specific function).
346
+     *
347
+     * @return array An array of post IDs.
348
+     * @since 3.1.0
349
+     *
350
+     */
351
+    public function get_all_related_to_last_50_published_posts() {
352
+
353
+        // Global timeline. Get entities from the latest posts.
354
+        $latest_posts_ids = get_posts( array(
355
+            'numberposts' => 50,
356
+            'fields'      => 'ids', //only get post IDs
357
+            'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
358
+            'tax_query'   => array(
359
+                'relation' => 'OR',
360
+                array(
361
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
362
+                    'operator' => 'NOT EXISTS',
363
+                ),
364
+                array(
365
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
366
+                    'field'    => 'slug',
367
+                    'terms'    => 'article',
368
+                ),
369
+            ),
370
+            'post_status' => 'publish',
371
+        ) );
372
+
373
+        if ( empty( $latest_posts_ids ) ) {
374
+            // There are no posts.
375
+            return array();
376
+        }
377
+
378
+        // Collect entities related to latest posts
379
+        $entity_ids = array();
380
+        foreach ( $latest_posts_ids as $id ) {
381
+            $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array(
382
+                'status' => 'publish',
383
+            ) ) );
384
+        }
385
+
386
+        return $entity_ids;
387
+    }
388 388
 
389 389
 }
Please login to merge, or discard this patch.
Spacing   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -71,9 +71,9 @@  discard block
 block discarded – undo
71 71
 	 * @since 3.1.0
72 72
 	 *
73 73
 	 */
74
-	public function __construct( $entity_service, $entity_type_service ) {
74
+	public function __construct($entity_service, $entity_type_service) {
75 75
 
76
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
76
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Service');
77 77
 
78 78
 		$this->entity_service      = $entity_service;
79 79
 		$this->entity_type_service = $entity_type_service;
@@ -102,10 +102,10 @@  discard block
 block discarded – undo
102 102
 	public function ajax_timeline() {
103 103
 
104 104
 		// Get the ID of the post who requested the timeline.
105
-		$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
105
+		$post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : null);
106 106
 
107 107
 		// Get the events and transform them for the JSON response, then send them to the client.
108
-		wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
108
+		wp_send_json($this->to_json($this->get_events($post_id)));
109 109
 
110 110
 	}
111 111
 
@@ -120,27 +120,27 @@  discard block
 block discarded – undo
120 120
 	 * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
121 121
 	 *
122 122
 	 */
123
-	public function get_events( $post_id = null ) {
123
+	public function get_events($post_id = null) {
124 124
 
125 125
 		// Get the entity IDs either from the entities related to the specified post or from the last 50 published
126 126
 		// posts if no post has been specified.
127
-		$ids = ( is_numeric( $post_id )
128
-			? wl_core_get_related_entity_ids( $post_id )
129
-			: $this->get_all_related_to_last_50_published_posts() );
127
+		$ids = (is_numeric($post_id)
128
+			? wl_core_get_related_entity_ids($post_id)
129
+			: $this->get_all_related_to_last_50_published_posts());
130 130
 
131 131
 		// Add the post itself if it's an entity.
132
-		if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) {
132
+		if (is_numeric($post_id) && $this->entity_service->is_entity($post_id)) {
133 133
 			$ids[] = $post_id;
134 134
 		}
135 135
 
136 136
 		// If there's no entities, return an empty array right away.
137
-		if ( 0 === sizeof( $ids ) ) {
138
-			$this->log->trace( "No events found [ post id :: $post_id ]" );
137
+		if (0 === sizeof($ids)) {
138
+			$this->log->trace("No events found [ post id :: $post_id ]");
139 139
 
140 140
 			return array();
141 141
 		}
142 142
 
143
-		$this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
143
+		$this->log->trace('Getting events [ entity ids :: '.join(', ', $ids).' ]');
144 144
 
145 145
 //		$entity_type_service = $this->entity_type_service;
146 146
 
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
 			'post__in'       => $ids,
157 157
 			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
158 158
 			'post_status'    => 'publish',
159
-			'posts_per_page' => - 1,
159
+			'posts_per_page' => -1,
160 160
 			'meta_query'     => array(
161 161
 				'relation' => 'AND',
162 162
 				array(
@@ -179,7 +179,7 @@  discard block
 block discarded – undo
179 179
 			),
180 180
 		);
181 181
 
182
-		return get_posts( $args );
182
+		return get_posts($args);
183 183
 	}
184 184
 
185 185
 	/**
@@ -193,23 +193,23 @@  discard block
 block discarded – undo
193 193
 	 * @since 3.1.0
194 194
 	 *
195 195
 	 */
196
-	public function to_json( $posts ) {
196
+	public function to_json($posts) {
197 197
 
198 198
 		// If there are no events, return empty JSON
199
-		if ( empty( $posts ) || is_null( $posts ) ) {
199
+		if (empty($posts) || is_null($posts)) {
200 200
 			return '';
201 201
 		}
202 202
 
203 203
 		// {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
204
-		$display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media';
204
+		$display_images_as = isset($_REQUEST['display_images_as']) ? $_REQUEST['display_images_as'] : 'media';
205 205
 
206 206
 		// The number of words for the excerpt (by default 55, as WordPress).
207
-		$this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55;
208
-		add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
207
+		$this->excerpt_length = $excerpt_length = isset($_REQUEST['excerpt_length']) && is_numeric($_REQUEST['excerpt_length']) ? $_REQUEST['excerpt_length'] : 55;
208
+		add_filter('excerpt_length', array($this, 'excerpt_length'));
209 209
 
210 210
 		// Add a filter to remove the [...] after excerpts, since we're adding
211 211
 		// a link to the post itself.
212
-		add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) );
212
+		add_filter('excerpt_more', array($this, 'excerpt_more'));
213 213
 
214 214
 		// Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
215 215
 		$start_at_slide = 0;
@@ -220,28 +220,28 @@  discard block
 block discarded – undo
220 220
 		$timeline = array();
221 221
 
222 222
 		// Populate the arrays.
223
-		$timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
223
+		$timeline['events'] = array_map(function($item) use (&$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length) {
224 224
 
225 225
 			// Get the start and end dates.
226
-			$start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) );
227
-			$end_date   = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) );
226
+			$start_date = strtotime(get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true));
227
+			$end_date   = strtotime(get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true));
228 228
 
229 229
 			// Set the starting slide.
230
-			$event_index ++;
231
-			if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
230
+			$event_index++;
231
+			if (0 === $start_at_slide && $now >= $start_date && $now <= $end_date) {
232 232
 				$start_at_slide = $event_index;
233 233
 			}
234 234
 
235 235
 			// Load thumbnail
236
-			$thumbnail_id = get_post_thumbnail_id( $item->ID );
237
-			if ( '' !== $thumbnail_id && 0 !== $thumbnail_id
238
-			     && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) )
236
+			$thumbnail_id = get_post_thumbnail_id($item->ID);
237
+			if ('' !== $thumbnail_id && 0 !== $thumbnail_id
238
+			     && false !== ($attachment = wp_get_attachment_image_src($thumbnail_id))
239 239
 			) {
240 240
 
241 241
 				// Set the thumbnail URL.
242
-				if ( 'background' === $display_images_as ) {
243
-					$date['background'] = array( 'url' => $attachment[0] );
244
-					$date['media']      = array( 'thumbnail' => $attachment[0] );
242
+				if ('background' === $display_images_as) {
243
+					$date['background'] = array('url' => $attachment[0]);
244
+					$date['media']      = array('thumbnail' => $attachment[0]);
245 245
 				} else {
246 246
 					$date['media'] = array(
247 247
 						'url'       => $attachment[0],
@@ -251,37 +251,37 @@  discard block
 block discarded – undo
251 251
 			}
252 252
 
253 253
 			// Set the start/end dates by converting them to TimelineJS required format.
254
-			$date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
255
-			$date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
254
+			$date['start_date'] = Wordlift_Timeline_Service::date($start_date);
255
+			$date['end_date']   = Wordlift_Timeline_Service::date($end_date);
256 256
 
257
-			setup_postdata( $GLOBALS['post'] = $item );
257
+			setup_postdata($GLOBALS['post'] = $item);
258 258
 
259 259
 			$more_link_text = sprintf(
260 260
 				'<span aria-label="%1$s">%2$s</span>',
261 261
 				sprintf(
262 262
 				/* translators: %s: Name of current post */
263
-					__( 'Continue reading %s' ),
264
-					the_title_attribute( array( 'echo' => false ) )
263
+					__('Continue reading %s'),
264
+					the_title_attribute(array('echo' => false))
265 265
 				),
266
-				__( '(more&hellip;)' )
266
+				__('(more&hellip;)')
267 267
 			);
268 268
 
269 269
 			// Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
270 270
 			$date['text'] = array(
271
-				'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
271
+				'headline' => '<a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>',
272 272
 			);
273 273
 
274 274
 			// If we have an excerpt, set it.
275
-			if ( 0 < $excerpt_length ) {
276
-				$date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
275
+			if (0 < $excerpt_length) {
276
+				$date['text']['text'] = sprintf('%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text);
277 277
 			}
278 278
 
279 279
 			return $date;
280 280
 
281
-		}, $posts );
281
+		}, $posts);
282 282
 
283 283
 		// Finally remove the excerpt filter.
284
-		remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
284
+		remove_filter('excerpt_length', array($this, 'excerpt_length'));
285 285
 
286 286
 		// The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
287 287
 		return array(
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
 	 * @since 3.7.0
301 301
 	 *
302 302
 	 */
303
-	public function excerpt_more( $excerpt_more ) {
303
+	public function excerpt_more($excerpt_more) {
304 304
 
305 305
 		return '';
306 306
 	}
@@ -315,7 +315,7 @@  discard block
 block discarded – undo
315 315
 	 * @since 3.7.0
316 316
 	 *
317 317
 	 */
318
-	public function excerpt_length( $length ) {
318
+	public function excerpt_length($length) {
319 319
 
320 320
 		return $this->excerpt_length;
321 321
 	}
@@ -330,12 +330,12 @@  discard block
 block discarded – undo
330 330
 	 * @since 3.7.0
331 331
 	 *
332 332
 	 */
333
-	public static function date( $value ) {
333
+	public static function date($value) {
334 334
 
335 335
 		return array(
336
-			'year'  => (int) date( 'Y', $value ),
337
-			'month' => (int) date( 'm', $value ),
338
-			'day'   => (int) date( 'd', $value ),
336
+			'year'  => (int) date('Y', $value),
337
+			'month' => (int) date('m', $value),
338
+			'day'   => (int) date('d', $value),
339 339
 
340 340
 		);
341 341
 	}
@@ -351,7 +351,7 @@  discard block
 block discarded – undo
351 351
 	public function get_all_related_to_last_50_published_posts() {
352 352
 
353 353
 		// Global timeline. Get entities from the latest posts.
354
-		$latest_posts_ids = get_posts( array(
354
+		$latest_posts_ids = get_posts(array(
355 355
 			'numberposts' => 50,
356 356
 			'fields'      => 'ids', //only get post IDs
357 357
 			'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
@@ -368,19 +368,19 @@  discard block
 block discarded – undo
368 368
 				),
369 369
 			),
370 370
 			'post_status' => 'publish',
371
-		) );
371
+		));
372 372
 
373
-		if ( empty( $latest_posts_ids ) ) {
373
+		if (empty($latest_posts_ids)) {
374 374
 			// There are no posts.
375 375
 			return array();
376 376
 		}
377 377
 
378 378
 		// Collect entities related to latest posts
379 379
 		$entity_ids = array();
380
-		foreach ( $latest_posts_ids as $id ) {
381
-			$entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array(
380
+		foreach ($latest_posts_ids as $id) {
381
+			$entity_ids = array_merge($entity_ids, wl_core_get_related_entity_ids($id, array(
382 382
 				'status' => 'publish',
383
-			) ) );
383
+			)));
384 384
 		}
385 385
 
386 386
 		return $entity_ids;
Please login to merge, or discard this patch.
src/includes/linked-data/storage/class-wordlift-post-image-storage.php 2 patches
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -18,45 +18,45 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Post_Image_Storage extends Wordlift_Storage {
20 20
 
21
-	/**
22
-	 * Get the property value.
23
-	 *
24
-	 * @param int $post_id The {@link WP_Post}'s id.
25
-	 *
26
-	 * @return array|string|null A single string, or an array of values or null
27
-	 *                           if the property isn't recognized.
28
-	 * @since 3.15.0
29
-	 *
30
-	 */
31
-	public function get( $post_id ) {
32
-
33
-		// Prepare the return array.
34
-		$image_urls = array();
35
-
36
-		// If there is a featured image it has the priority.
37
-		$featured_image_id = get_post_thumbnail_id( $post_id );
38
-		if ( is_numeric( $featured_image_id ) && 0 < $featured_image_id ) {
39
-			$image_url = wp_get_attachment_url( $featured_image_id );
40
-
41
-			$image_urls[] = $image_url;
42
-		}
43
-
44
-		$images = get_children( array(
45
-			'post_parent'    => $post_id,
46
-			'post_type'      => 'attachment',
47
-			'post_mime_type' => 'image',
48
-		) );
49
-
50
-		// Collect the URLs.
51
-		foreach ( $images as $attachment_id => $attachment ) {
52
-			$image_url = wp_get_attachment_url( $attachment_id );
53
-			// Ensure the URL isn't collected already.
54
-			if ( ! in_array( $image_url, $image_urls ) ) {
55
-				array_push( $image_urls, $image_url );
56
-			}
57
-		}
58
-
59
-		return $image_urls;
60
-	}
21
+    /**
22
+     * Get the property value.
23
+     *
24
+     * @param int $post_id The {@link WP_Post}'s id.
25
+     *
26
+     * @return array|string|null A single string, or an array of values or null
27
+     *                           if the property isn't recognized.
28
+     * @since 3.15.0
29
+     *
30
+     */
31
+    public function get( $post_id ) {
32
+
33
+        // Prepare the return array.
34
+        $image_urls = array();
35
+
36
+        // If there is a featured image it has the priority.
37
+        $featured_image_id = get_post_thumbnail_id( $post_id );
38
+        if ( is_numeric( $featured_image_id ) && 0 < $featured_image_id ) {
39
+            $image_url = wp_get_attachment_url( $featured_image_id );
40
+
41
+            $image_urls[] = $image_url;
42
+        }
43
+
44
+        $images = get_children( array(
45
+            'post_parent'    => $post_id,
46
+            'post_type'      => 'attachment',
47
+            'post_mime_type' => 'image',
48
+        ) );
49
+
50
+        // Collect the URLs.
51
+        foreach ( $images as $attachment_id => $attachment ) {
52
+            $image_url = wp_get_attachment_url( $attachment_id );
53
+            // Ensure the URL isn't collected already.
54
+            if ( ! in_array( $image_url, $image_urls ) ) {
55
+                array_push( $image_urls, $image_url );
56
+            }
57
+        }
58
+
59
+        return $image_urls;
60
+    }
61 61
 
62 62
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -28,31 +28,31 @@
 block discarded – undo
28 28
 	 * @since 3.15.0
29 29
 	 *
30 30
 	 */
31
-	public function get( $post_id ) {
31
+	public function get($post_id) {
32 32
 
33 33
 		// Prepare the return array.
34 34
 		$image_urls = array();
35 35
 
36 36
 		// If there is a featured image it has the priority.
37
-		$featured_image_id = get_post_thumbnail_id( $post_id );
38
-		if ( is_numeric( $featured_image_id ) && 0 < $featured_image_id ) {
39
-			$image_url = wp_get_attachment_url( $featured_image_id );
37
+		$featured_image_id = get_post_thumbnail_id($post_id);
38
+		if (is_numeric($featured_image_id) && 0 < $featured_image_id) {
39
+			$image_url = wp_get_attachment_url($featured_image_id);
40 40
 
41 41
 			$image_urls[] = $image_url;
42 42
 		}
43 43
 
44
-		$images = get_children( array(
44
+		$images = get_children(array(
45 45
 			'post_parent'    => $post_id,
46 46
 			'post_type'      => 'attachment',
47 47
 			'post_mime_type' => 'image',
48
-		) );
48
+		));
49 49
 
50 50
 		// Collect the URLs.
51
-		foreach ( $images as $attachment_id => $attachment ) {
52
-			$image_url = wp_get_attachment_url( $attachment_id );
51
+		foreach ($images as $attachment_id => $attachment) {
52
+			$image_url = wp_get_attachment_url($attachment_id);
53 53
 			// Ensure the URL isn't collected already.
54
-			if ( ! in_array( $image_url, $image_urls ) ) {
55
-				array_push( $image_urls, $image_url );
54
+			if ( ! in_array($image_url, $image_urls)) {
55
+				array_push($image_urls, $image_url);
56 56
 			}
57 57
 		}
58 58
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-post-to-jsonld-converter.php 2 patches
Indentation   +376 added lines, -376 removed lines patch added patch discarded remove patch
@@ -15,393 +15,393 @@
 block discarded – undo
15 15
  */
16 16
 class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter {
17 17
 
18
-	/**
19
-	 * A {@link Wordlift_Configuration_Service} instance.
20
-	 *
21
-	 * @since  3.10.0
22
-	 * @access private
23
-	 * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
24
-	 */
25
-	private $configuration_service;
26
-
27
-	/**
28
-	 * A {@link Wordlift_Log_Service} instance.
29
-	 *
30
-	 * @since  3.10.0
31
-	 * @access private
32
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
33
-	 */
34
-	private $log;
35
-
36
-	/**
37
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
38
-	 *
39
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
40
-	 * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
41
-	 * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
42
-	 * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
43
-	 * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
44
-	 *
45
-	 * @since 3.10.0
46
-	 *
47
-	 */
48
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) {
49
-		parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
50
-
51
-		$this->configuration_service = $configuration_service;
52
-
53
-		// Set a reference to the logger.
54
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
55
-	}
56
-
57
-	/**
58
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
59
-	 * found while processing the post is set in the $references array.
60
-	 *
61
-	 * @param int $post_id The post id.
62
-	 * @param array $references An array of entity references.
63
-	 *
64
-	 * @return array A JSON-LD array.
65
-	 * @since 3.10.0
66
-	 *
67
-	 */
68
-	public function convert( $post_id, &$references = array() ) {
69
-
70
-		// Get the post instance.
71
-		if ( null === $post = get_post( $post_id ) ) {
72
-			// Post not found.
73
-			return null;
74
-		}
75
-
76
-		// Get the base JSON-LD and the list of entities referenced by this entity.
77
-		$jsonld = parent::convert( $post_id, $references );
78
-
79
-		// Set WebPage by default.
80
-		if ( empty( $jsonld['@type'] ) ) {
81
-			$jsonld['@type'] = 'WebPage';
82
-		}
83
-
84
-		// Get the entity name.
85
-		$jsonld['headline'] = $post->post_title;
86
-
87
-		// Set the published and modified dates.
88
-		/*
18
+    /**
19
+     * A {@link Wordlift_Configuration_Service} instance.
20
+     *
21
+     * @since  3.10.0
22
+     * @access private
23
+     * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
24
+     */
25
+    private $configuration_service;
26
+
27
+    /**
28
+     * A {@link Wordlift_Log_Service} instance.
29
+     *
30
+     * @since  3.10.0
31
+     * @access private
32
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
33
+     */
34
+    private $log;
35
+
36
+    /**
37
+     * Wordlift_Post_To_Jsonld_Converter constructor.
38
+     *
39
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
40
+     * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
41
+     * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
42
+     * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
43
+     * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
44
+     *
45
+     * @since 3.10.0
46
+     *
47
+     */
48
+    public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) {
49
+        parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
50
+
51
+        $this->configuration_service = $configuration_service;
52
+
53
+        // Set a reference to the logger.
54
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
55
+    }
56
+
57
+    /**
58
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
59
+     * found while processing the post is set in the $references array.
60
+     *
61
+     * @param int $post_id The post id.
62
+     * @param array $references An array of entity references.
63
+     *
64
+     * @return array A JSON-LD array.
65
+     * @since 3.10.0
66
+     *
67
+     */
68
+    public function convert( $post_id, &$references = array() ) {
69
+
70
+        // Get the post instance.
71
+        if ( null === $post = get_post( $post_id ) ) {
72
+            // Post not found.
73
+            return null;
74
+        }
75
+
76
+        // Get the base JSON-LD and the list of entities referenced by this entity.
77
+        $jsonld = parent::convert( $post_id, $references );
78
+
79
+        // Set WebPage by default.
80
+        if ( empty( $jsonld['@type'] ) ) {
81
+            $jsonld['@type'] = 'WebPage';
82
+        }
83
+
84
+        // Get the entity name.
85
+        $jsonld['headline'] = $post->post_title;
86
+
87
+        // Set the published and modified dates.
88
+        /*
89 89
 		 * Set the `datePublished` and `dateModified` using the local timezone.
90 90
 		 *
91 91
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/887
92 92
 		 *
93 93
 		 * @since 3.20.0
94 94
 		 */
95
-		try {
96
-			$default_timezone = date_default_timezone_get();
97
-			date_default_timezone_set( get_option( 'timezone_string' ) );
98
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
99
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
100
-			date_default_timezone_set( $default_timezone );
101
-		} catch ( Exception $e ) {
102
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
103
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
104
-		}
105
-
106
-		// Get the word count for the post.
107
-		/*
95
+        try {
96
+            $default_timezone = date_default_timezone_get();
97
+            date_default_timezone_set( get_option( 'timezone_string' ) );
98
+            $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
99
+            $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
100
+            date_default_timezone_set( $default_timezone );
101
+        } catch ( Exception $e ) {
102
+            $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
103
+            $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
104
+        }
105
+
106
+        // Get the word count for the post.
107
+        /*
108 108
 		 * Do not display the `wordCount` on a `WebPage`.
109 109
 		 *
110 110
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/888
111 111
 		 *
112 112
 		 * @since 3.20.0
113 113
 		 */
114
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
115
-			$post_adapter        = new Wordlift_Post_Adapter( $post_id );
116
-			$jsonld['wordCount'] = $post_adapter->word_count();
117
-		}
118
-
119
-		// Set the publisher.
120
-		$this->set_publisher( $jsonld );
121
-
122
-		// Process the references if any.
123
-		if ( 0 < count( $references ) ) {
124
-
125
-			// Prepare the `about` and `mentions` array.
126
-			$about = $mentions = array();
127
-
128
-			// If the entity is in the title, then it should be an `about`.
129
-			foreach ( $references as $reference ) {
130
-
131
-				// Get the entity labels.
132
-				$labels = $this->entity_service->get_labels( $reference );
133
-
134
-				// Get the entity URI.
135
-				$item = array(
136
-					'@id' => $this->entity_service->get_uri( $reference ),
137
-				);
138
-
139
-				$escaped_labels = array_map( function ( $value ) {
140
-					return preg_quote( $value, '/' );
141
-				}, $labels );
142
-
143
-				// Check if the labels match any part of the title.
144
-				$matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title );
145
-
146
-				// If the title matches, assign the entity to the about, otherwise to the mentions.
147
-				if ( $matches ) {
148
-					$about[] = $item;
149
-				} else {
150
-					$mentions[] = $item;
151
-				}
152
-			}
153
-
154
-			// If we have abouts, assign them to the JSON-LD.
155
-			if ( 0 < count( $about ) ) {
156
-				$jsonld['about'] = $about;
157
-			}
158
-
159
-			// If we have mentions, assign them to the JSON-LD.
160
-			if ( 0 < count( $mentions ) ) {
161
-				$jsonld['mentions'] = $mentions;
162
-			}
163
-		}
164
-
165
-		// Finally set the author.
166
-		$jsonld['author'] = $this->get_author( $post->post_author, $references );
167
-
168
-		/**
169
-		 * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
170
-		 *
171
-		 * @param array $value {
172
-		 *
173
-		 * @type array $jsonld The JSON-LD structure.
174
-		 * @type int[] $references An array of post IDs.
175
-		 * }
176
-		 * @since 3.25.0
177
-		 *
178
-		 * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
179
-		 *
180
-		 * @api
181
-		 */
182
-		$ret_val    = apply_filters( 'wl_post_jsonld_array', array(
183
-			'jsonld'     => $jsonld,
184
-			'references' => $references,
185
-		), $post_id );
186
-		$jsonld     = $ret_val['jsonld'];
187
-		$references = $ret_val['references'];
188
-
189
-		/**
190
-		 * Call the `wl_post_jsonld` filter.
191
-		 *
192
-		 * @param array $jsonld The JSON-LD structure.
193
-		 * @param int $post_id The {@link WP_Post} `id`.
194
-		 * @param array $references The array of referenced entities.
195
-		 *
196
-		 * @since 3.14.0
197
-		 *
198
-		 * @api
199
-		 */
200
-		return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
201
-	}
202
-
203
-	/**
204
-	 * Get the author's JSON-LD fragment.
205
-	 *
206
-	 * The JSON-LD fragment is generated using the {@link WP_User}'s data or
207
-	 * the referenced entity if configured for the {@link WP_User}.
208
-	 *
209
-	 * @param int $author_id The author {@link WP_User}'s `id`.
210
-	 * @param array $references An array of referenced entities.
211
-	 *
212
-	 * @return string|array A JSON-LD structure.
213
-	 * @since 3.14.0
214
-	 *
215
-	 */
216
-	private function get_author( $author_id, &$references ) {
217
-
218
-		// Get the entity bound to this user.
219
-		$entity_id = $this->user_service->get_entity( $author_id );
220
-
221
-		// If there's no entity bound return a simple author structure.
222
-		if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
223
-
224
-			$author     = get_the_author_meta( 'display_name', $author_id );
225
-			$author_uri = $this->user_service->get_uri( $author_id );
226
-
227
-			return array(
228
-				'@type' => 'Person',
229
-				'@id'   => $author_uri,
230
-				'name'  => $author,
231
-			);
232
-		}
233
-
234
-		// Add the author to the references.
235
-		$author_uri   = $this->entity_service->get_uri( $entity_id );
236
-		$references[] = $entity_id;
237
-
238
-		// Return the JSON-LD for the referenced entity.
239
-		return array(
240
-			'@id' => $author_uri,
241
-		);
242
-	}
243
-
244
-	/**
245
-	 * Enrich the provided params array with publisher data, if available.
246
-	 *
247
-	 * @param array $params The parameters array.
248
-	 *
249
-	 * @since 3.10.0
250
-	 *
251
-	 */
252
-	protected function set_publisher( &$params ) {
253
-
254
-		// If the publisher id isn't set don't do anything.
255
-		if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) {
256
-			return;
257
-		}
258
-
259
-		// Get the post instance.
260
-		if ( null === $post = get_post( $publisher_id ) ) {
261
-			// Publisher not found.
262
-			return;
263
-		}
264
-
265
-		// Get the item id.
266
-		$id = $this->entity_service->get_uri( $publisher_id );
267
-
268
-		// Get the type.
269
-		$type = $this->entity_type_service->get( $publisher_id );
270
-
271
-		// Get the name.
272
-		$name = $post->post_title;
273
-
274
-		// Set the publisher data.
275
-		$params['publisher'] = array(
276
-			'@type' => $this->relative_to_context( $type['uri'] ),
277
-			'@id'   => $id,
278
-			'name'  => $name,
279
-		);
280
-
281
-		// Add the sameAs values associated with the publisher.
282
-		$storage_factory = Wordlift_Storage_Factory::get_instance();
283
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
284
-		if ( ! empty( $sameas ) ) {
285
-			$params['publisher']['sameAs'] = $sameas;
286
-		}
287
-
288
-		// Set the logo, only for http://schema.org/Organization as Person doesn't
289
-		// support the logo property.
290
-		//
291
-		// See http://schema.org/logo.
292
-		if ( 'http://schema.org/Organization' !== $type['uri'] ) {
293
-			return;
294
-		}
295
-
296
-		// Get the publisher logo.
297
-		$publisher_logo = $this->get_publisher_logo( $post->ID );
298
-
299
-		// Bail out if the publisher logo isn't set.
300
-		if ( false === $publisher_logo ) {
301
-			return;
302
-		}
303
-
304
-		// Copy over some useful properties.
305
-		//
306
-		// See https://developers.google.com/search/docs/data-types/articles.
307
-		$params['publisher']['logo']['@type'] = 'ImageObject';
308
-		$params['publisher']['logo']['url']   = $publisher_logo['url'];
309
-
310
-		// If you specify a "width" or "height" value you should leave out
311
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
312
-		//
313
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
314
-		$params['publisher']['logo']['width']  = $publisher_logo['width'];
315
-		$params['publisher']['logo']['height'] = $publisher_logo['height'];
316
-
317
-	}
318
-
319
-	/**
320
-	 * Get the publisher logo structure.
321
-	 *
322
-	 * The function returns false when the publisher logo cannot be determined, i.e.:
323
-	 *  - the post has no featured image.
324
-	 *  - the featured image has no file.
325
-	 *  - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file.
326
-	 *
327
-	 * @param int $post_id The post id.
328
-	 *
329
-	 * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case
330
-	 *  of errors.
331
-	 * @since 3.19.2
332
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
333
-	 *
334
-	 */
335
-	private function get_publisher_logo( $post_id ) {
336
-
337
-		// Get the featured image for the post.
338
-		$thumbnail_id = get_post_thumbnail_id( $post_id );
339
-
340
-		// Bail out if thumbnail not available.
341
-		if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
342
-			$this->log->info( "Featured image not set for post $post_id." );
343
-
344
-			return false;
345
-		}
346
-
347
-		// Get the uploads base URL.
348
-		$uploads_dir = wp_upload_dir();
349
-
350
-		// Get the attachment metadata.
351
-		$metadata = wp_get_attachment_metadata( $thumbnail_id );
352
-
353
-		// Bail out if the file isn't set.
354
-		if ( ! isset( $metadata['file'] ) ) {
355
-			$this->log->warn( "Featured image file not found for post $post_id." );
356
-
357
-			return false;
358
-		}
359
-
360
-		// Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
361
-		$path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
362
-
363
-		// Bail out if the file isn't found.
364
-		if ( ! file_exists( $path ) ) {
365
-			$this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
366
-
367
-			return false;
368
-		}
369
-
370
-		// Try to get the image editor and bail out if the editor cannot be instantiated.
371
-		$original_file_editor = wp_get_image_editor( $path );
372
-		if ( is_wp_error( $original_file_editor ) ) {
373
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
374
-
375
-			return false;
376
-		}
377
-
378
-		// Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
379
-		// and we don't actually know the end values.
380
-		$publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
381
-
382
-		// If the file doesn't exist yet, create it.
383
-		if ( ! file_exists( $publisher_logo_path ) ) {
384
-			$original_file_editor->resize( 600, 60 );
385
-			$original_file_editor->save( $publisher_logo_path );
386
-		}
387
-
388
-		// Try to get the image editor and bail out if the editor cannot be instantiated.
389
-		$publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
390
-		if ( is_wp_error( $publisher_logo_editor ) ) {
391
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
392
-
393
-			return false;
394
-		}
395
-
396
-		// Get the actual size.
397
-		$size = $publisher_logo_editor->get_size();
398
-
399
-		// Finally return the array with data.
400
-		return array(
401
-			'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
402
-			'width'  => $size['width'],
403
-			'height' => $size['height'],
404
-		);
405
-	}
114
+        if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
115
+            $post_adapter        = new Wordlift_Post_Adapter( $post_id );
116
+            $jsonld['wordCount'] = $post_adapter->word_count();
117
+        }
118
+
119
+        // Set the publisher.
120
+        $this->set_publisher( $jsonld );
121
+
122
+        // Process the references if any.
123
+        if ( 0 < count( $references ) ) {
124
+
125
+            // Prepare the `about` and `mentions` array.
126
+            $about = $mentions = array();
127
+
128
+            // If the entity is in the title, then it should be an `about`.
129
+            foreach ( $references as $reference ) {
130
+
131
+                // Get the entity labels.
132
+                $labels = $this->entity_service->get_labels( $reference );
133
+
134
+                // Get the entity URI.
135
+                $item = array(
136
+                    '@id' => $this->entity_service->get_uri( $reference ),
137
+                );
138
+
139
+                $escaped_labels = array_map( function ( $value ) {
140
+                    return preg_quote( $value, '/' );
141
+                }, $labels );
142
+
143
+                // Check if the labels match any part of the title.
144
+                $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title );
145
+
146
+                // If the title matches, assign the entity to the about, otherwise to the mentions.
147
+                if ( $matches ) {
148
+                    $about[] = $item;
149
+                } else {
150
+                    $mentions[] = $item;
151
+                }
152
+            }
153
+
154
+            // If we have abouts, assign them to the JSON-LD.
155
+            if ( 0 < count( $about ) ) {
156
+                $jsonld['about'] = $about;
157
+            }
158
+
159
+            // If we have mentions, assign them to the JSON-LD.
160
+            if ( 0 < count( $mentions ) ) {
161
+                $jsonld['mentions'] = $mentions;
162
+            }
163
+        }
164
+
165
+        // Finally set the author.
166
+        $jsonld['author'] = $this->get_author( $post->post_author, $references );
167
+
168
+        /**
169
+         * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
170
+         *
171
+         * @param array $value {
172
+         *
173
+         * @type array $jsonld The JSON-LD structure.
174
+         * @type int[] $references An array of post IDs.
175
+         * }
176
+         * @since 3.25.0
177
+         *
178
+         * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
179
+         *
180
+         * @api
181
+         */
182
+        $ret_val    = apply_filters( 'wl_post_jsonld_array', array(
183
+            'jsonld'     => $jsonld,
184
+            'references' => $references,
185
+        ), $post_id );
186
+        $jsonld     = $ret_val['jsonld'];
187
+        $references = $ret_val['references'];
188
+
189
+        /**
190
+         * Call the `wl_post_jsonld` filter.
191
+         *
192
+         * @param array $jsonld The JSON-LD structure.
193
+         * @param int $post_id The {@link WP_Post} `id`.
194
+         * @param array $references The array of referenced entities.
195
+         *
196
+         * @since 3.14.0
197
+         *
198
+         * @api
199
+         */
200
+        return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
201
+    }
202
+
203
+    /**
204
+     * Get the author's JSON-LD fragment.
205
+     *
206
+     * The JSON-LD fragment is generated using the {@link WP_User}'s data or
207
+     * the referenced entity if configured for the {@link WP_User}.
208
+     *
209
+     * @param int $author_id The author {@link WP_User}'s `id`.
210
+     * @param array $references An array of referenced entities.
211
+     *
212
+     * @return string|array A JSON-LD structure.
213
+     * @since 3.14.0
214
+     *
215
+     */
216
+    private function get_author( $author_id, &$references ) {
217
+
218
+        // Get the entity bound to this user.
219
+        $entity_id = $this->user_service->get_entity( $author_id );
220
+
221
+        // If there's no entity bound return a simple author structure.
222
+        if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
223
+
224
+            $author     = get_the_author_meta( 'display_name', $author_id );
225
+            $author_uri = $this->user_service->get_uri( $author_id );
226
+
227
+            return array(
228
+                '@type' => 'Person',
229
+                '@id'   => $author_uri,
230
+                'name'  => $author,
231
+            );
232
+        }
233
+
234
+        // Add the author to the references.
235
+        $author_uri   = $this->entity_service->get_uri( $entity_id );
236
+        $references[] = $entity_id;
237
+
238
+        // Return the JSON-LD for the referenced entity.
239
+        return array(
240
+            '@id' => $author_uri,
241
+        );
242
+    }
243
+
244
+    /**
245
+     * Enrich the provided params array with publisher data, if available.
246
+     *
247
+     * @param array $params The parameters array.
248
+     *
249
+     * @since 3.10.0
250
+     *
251
+     */
252
+    protected function set_publisher( &$params ) {
253
+
254
+        // If the publisher id isn't set don't do anything.
255
+        if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) {
256
+            return;
257
+        }
258
+
259
+        // Get the post instance.
260
+        if ( null === $post = get_post( $publisher_id ) ) {
261
+            // Publisher not found.
262
+            return;
263
+        }
264
+
265
+        // Get the item id.
266
+        $id = $this->entity_service->get_uri( $publisher_id );
267
+
268
+        // Get the type.
269
+        $type = $this->entity_type_service->get( $publisher_id );
270
+
271
+        // Get the name.
272
+        $name = $post->post_title;
273
+
274
+        // Set the publisher data.
275
+        $params['publisher'] = array(
276
+            '@type' => $this->relative_to_context( $type['uri'] ),
277
+            '@id'   => $id,
278
+            'name'  => $name,
279
+        );
280
+
281
+        // Add the sameAs values associated with the publisher.
282
+        $storage_factory = Wordlift_Storage_Factory::get_instance();
283
+        $sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
284
+        if ( ! empty( $sameas ) ) {
285
+            $params['publisher']['sameAs'] = $sameas;
286
+        }
287
+
288
+        // Set the logo, only for http://schema.org/Organization as Person doesn't
289
+        // support the logo property.
290
+        //
291
+        // See http://schema.org/logo.
292
+        if ( 'http://schema.org/Organization' !== $type['uri'] ) {
293
+            return;
294
+        }
295
+
296
+        // Get the publisher logo.
297
+        $publisher_logo = $this->get_publisher_logo( $post->ID );
298
+
299
+        // Bail out if the publisher logo isn't set.
300
+        if ( false === $publisher_logo ) {
301
+            return;
302
+        }
303
+
304
+        // Copy over some useful properties.
305
+        //
306
+        // See https://developers.google.com/search/docs/data-types/articles.
307
+        $params['publisher']['logo']['@type'] = 'ImageObject';
308
+        $params['publisher']['logo']['url']   = $publisher_logo['url'];
309
+
310
+        // If you specify a "width" or "height" value you should leave out
311
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
312
+        //
313
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
314
+        $params['publisher']['logo']['width']  = $publisher_logo['width'];
315
+        $params['publisher']['logo']['height'] = $publisher_logo['height'];
316
+
317
+    }
318
+
319
+    /**
320
+     * Get the publisher logo structure.
321
+     *
322
+     * The function returns false when the publisher logo cannot be determined, i.e.:
323
+     *  - the post has no featured image.
324
+     *  - the featured image has no file.
325
+     *  - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file.
326
+     *
327
+     * @param int $post_id The post id.
328
+     *
329
+     * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case
330
+     *  of errors.
331
+     * @since 3.19.2
332
+     * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
333
+     *
334
+     */
335
+    private function get_publisher_logo( $post_id ) {
336
+
337
+        // Get the featured image for the post.
338
+        $thumbnail_id = get_post_thumbnail_id( $post_id );
339
+
340
+        // Bail out if thumbnail not available.
341
+        if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
342
+            $this->log->info( "Featured image not set for post $post_id." );
343
+
344
+            return false;
345
+        }
346
+
347
+        // Get the uploads base URL.
348
+        $uploads_dir = wp_upload_dir();
349
+
350
+        // Get the attachment metadata.
351
+        $metadata = wp_get_attachment_metadata( $thumbnail_id );
352
+
353
+        // Bail out if the file isn't set.
354
+        if ( ! isset( $metadata['file'] ) ) {
355
+            $this->log->warn( "Featured image file not found for post $post_id." );
356
+
357
+            return false;
358
+        }
359
+
360
+        // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
361
+        $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
362
+
363
+        // Bail out if the file isn't found.
364
+        if ( ! file_exists( $path ) ) {
365
+            $this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
366
+
367
+            return false;
368
+        }
369
+
370
+        // Try to get the image editor and bail out if the editor cannot be instantiated.
371
+        $original_file_editor = wp_get_image_editor( $path );
372
+        if ( is_wp_error( $original_file_editor ) ) {
373
+            $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
374
+
375
+            return false;
376
+        }
377
+
378
+        // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
379
+        // and we don't actually know the end values.
380
+        $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
381
+
382
+        // If the file doesn't exist yet, create it.
383
+        if ( ! file_exists( $publisher_logo_path ) ) {
384
+            $original_file_editor->resize( 600, 60 );
385
+            $original_file_editor->save( $publisher_logo_path );
386
+        }
387
+
388
+        // Try to get the image editor and bail out if the editor cannot be instantiated.
389
+        $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
390
+        if ( is_wp_error( $publisher_logo_editor ) ) {
391
+            $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
392
+
393
+            return false;
394
+        }
395
+
396
+        // Get the actual size.
397
+        $size = $publisher_logo_editor->get_size();
398
+
399
+        // Finally return the array with data.
400
+        return array(
401
+            'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
402
+            'width'  => $size['width'],
403
+            'height' => $size['height'],
404
+        );
405
+    }
406 406
 
407 407
 }
Please login to merge, or discard this patch.
Spacing   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -45,13 +45,13 @@  discard block
 block discarded – undo
45 45
 	 * @since 3.10.0
46 46
 	 *
47 47
 	 */
48
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) {
49
-		parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
48
+	public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service) {
49
+		parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service);
50 50
 
51 51
 		$this->configuration_service = $configuration_service;
52 52
 
53 53
 		// Set a reference to the logger.
54
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
54
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter');
55 55
 	}
56 56
 
57 57
 	/**
@@ -65,19 +65,19 @@  discard block
 block discarded – undo
65 65
 	 * @since 3.10.0
66 66
 	 *
67 67
 	 */
68
-	public function convert( $post_id, &$references = array() ) {
68
+	public function convert($post_id, &$references = array()) {
69 69
 
70 70
 		// Get the post instance.
71
-		if ( null === $post = get_post( $post_id ) ) {
71
+		if (null === $post = get_post($post_id)) {
72 72
 			// Post not found.
73 73
 			return null;
74 74
 		}
75 75
 
76 76
 		// Get the base JSON-LD and the list of entities referenced by this entity.
77
-		$jsonld = parent::convert( $post_id, $references );
77
+		$jsonld = parent::convert($post_id, $references);
78 78
 
79 79
 		// Set WebPage by default.
80
-		if ( empty( $jsonld['@type'] ) ) {
80
+		if (empty($jsonld['@type'])) {
81 81
 			$jsonld['@type'] = 'WebPage';
82 82
 		}
83 83
 
@@ -94,13 +94,13 @@  discard block
 block discarded – undo
94 94
 		 */
95 95
 		try {
96 96
 			$default_timezone = date_default_timezone_get();
97
-			date_default_timezone_set( get_option( 'timezone_string' ) );
98
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post );
99
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post );
100
-			date_default_timezone_set( $default_timezone );
101
-		} catch ( Exception $e ) {
102
-			$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
103
-			$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
97
+			date_default_timezone_set(get_option('timezone_string'));
98
+			$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post);
99
+			$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i:sP', false, $post);
100
+			date_default_timezone_set($default_timezone);
101
+		} catch (Exception $e) {
102
+			$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false);
103
+			$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i', true, $post, false);
104 104
 		}
105 105
 
106 106
 		// Get the word count for the post.
@@ -111,40 +111,40 @@  discard block
 block discarded – undo
111 111
 		 *
112 112
 		 * @since 3.20.0
113 113
 		 */
114
-		if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) {
115
-			$post_adapter        = new Wordlift_Post_Adapter( $post_id );
114
+		if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) {
115
+			$post_adapter        = new Wordlift_Post_Adapter($post_id);
116 116
 			$jsonld['wordCount'] = $post_adapter->word_count();
117 117
 		}
118 118
 
119 119
 		// Set the publisher.
120
-		$this->set_publisher( $jsonld );
120
+		$this->set_publisher($jsonld);
121 121
 
122 122
 		// Process the references if any.
123
-		if ( 0 < count( $references ) ) {
123
+		if (0 < count($references)) {
124 124
 
125 125
 			// Prepare the `about` and `mentions` array.
126 126
 			$about = $mentions = array();
127 127
 
128 128
 			// If the entity is in the title, then it should be an `about`.
129
-			foreach ( $references as $reference ) {
129
+			foreach ($references as $reference) {
130 130
 
131 131
 				// Get the entity labels.
132
-				$labels = $this->entity_service->get_labels( $reference );
132
+				$labels = $this->entity_service->get_labels($reference);
133 133
 
134 134
 				// Get the entity URI.
135 135
 				$item = array(
136
-					'@id' => $this->entity_service->get_uri( $reference ),
136
+					'@id' => $this->entity_service->get_uri($reference),
137 137
 				);
138 138
 
139
-				$escaped_labels = array_map( function ( $value ) {
140
-					return preg_quote( $value, '/' );
141
-				}, $labels );
139
+				$escaped_labels = array_map(function($value) {
140
+					return preg_quote($value, '/');
141
+				}, $labels);
142 142
 
143 143
 				// Check if the labels match any part of the title.
144
-				$matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title );
144
+				$matches = 1 === preg_match('/'.implode('|', $escaped_labels).'/', $post->post_title);
145 145
 
146 146
 				// If the title matches, assign the entity to the about, otherwise to the mentions.
147
-				if ( $matches ) {
147
+				if ($matches) {
148 148
 					$about[] = $item;
149 149
 				} else {
150 150
 					$mentions[] = $item;
@@ -152,18 +152,18 @@  discard block
 block discarded – undo
152 152
 			}
153 153
 
154 154
 			// If we have abouts, assign them to the JSON-LD.
155
-			if ( 0 < count( $about ) ) {
155
+			if (0 < count($about)) {
156 156
 				$jsonld['about'] = $about;
157 157
 			}
158 158
 
159 159
 			// If we have mentions, assign them to the JSON-LD.
160
-			if ( 0 < count( $mentions ) ) {
160
+			if (0 < count($mentions)) {
161 161
 				$jsonld['mentions'] = $mentions;
162 162
 			}
163 163
 		}
164 164
 
165 165
 		// Finally set the author.
166
-		$jsonld['author'] = $this->get_author( $post->post_author, $references );
166
+		$jsonld['author'] = $this->get_author($post->post_author, $references);
167 167
 
168 168
 		/**
169 169
 		 * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
@@ -179,10 +179,10 @@  discard block
 block discarded – undo
179 179
 		 *
180 180
 		 * @api
181 181
 		 */
182
-		$ret_val    = apply_filters( 'wl_post_jsonld_array', array(
182
+		$ret_val = apply_filters('wl_post_jsonld_array', array(
183 183
 			'jsonld'     => $jsonld,
184 184
 			'references' => $references,
185
-		), $post_id );
185
+		), $post_id);
186 186
 		$jsonld     = $ret_val['jsonld'];
187 187
 		$references = $ret_val['references'];
188 188
 
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 		 *
198 198
 		 * @api
199 199
 		 */
200
-		return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
200
+		return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references);
201 201
 	}
202 202
 
203 203
 	/**
@@ -213,16 +213,16 @@  discard block
 block discarded – undo
213 213
 	 * @since 3.14.0
214 214
 	 *
215 215
 	 */
216
-	private function get_author( $author_id, &$references ) {
216
+	private function get_author($author_id, &$references) {
217 217
 
218 218
 		// Get the entity bound to this user.
219
-		$entity_id = $this->user_service->get_entity( $author_id );
219
+		$entity_id = $this->user_service->get_entity($author_id);
220 220
 
221 221
 		// If there's no entity bound return a simple author structure.
222
-		if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
222
+		if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) {
223 223
 
224
-			$author     = get_the_author_meta( 'display_name', $author_id );
225
-			$author_uri = $this->user_service->get_uri( $author_id );
224
+			$author     = get_the_author_meta('display_name', $author_id);
225
+			$author_uri = $this->user_service->get_uri($author_id);
226 226
 
227 227
 			return array(
228 228
 				'@type' => 'Person',
@@ -232,7 +232,7 @@  discard block
 block discarded – undo
232 232
 		}
233 233
 
234 234
 		// Add the author to the references.
235
-		$author_uri   = $this->entity_service->get_uri( $entity_id );
235
+		$author_uri   = $this->entity_service->get_uri($entity_id);
236 236
 		$references[] = $entity_id;
237 237
 
238 238
 		// Return the JSON-LD for the referenced entity.
@@ -249,39 +249,39 @@  discard block
 block discarded – undo
249 249
 	 * @since 3.10.0
250 250
 	 *
251 251
 	 */
252
-	protected function set_publisher( &$params ) {
252
+	protected function set_publisher(&$params) {
253 253
 
254 254
 		// If the publisher id isn't set don't do anything.
255
-		if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) {
255
+		if (null === $publisher_id = $this->configuration_service->get_publisher_id()) {
256 256
 			return;
257 257
 		}
258 258
 
259 259
 		// Get the post instance.
260
-		if ( null === $post = get_post( $publisher_id ) ) {
260
+		if (null === $post = get_post($publisher_id)) {
261 261
 			// Publisher not found.
262 262
 			return;
263 263
 		}
264 264
 
265 265
 		// Get the item id.
266
-		$id = $this->entity_service->get_uri( $publisher_id );
266
+		$id = $this->entity_service->get_uri($publisher_id);
267 267
 
268 268
 		// Get the type.
269
-		$type = $this->entity_type_service->get( $publisher_id );
269
+		$type = $this->entity_type_service->get($publisher_id);
270 270
 
271 271
 		// Get the name.
272 272
 		$name = $post->post_title;
273 273
 
274 274
 		// Set the publisher data.
275 275
 		$params['publisher'] = array(
276
-			'@type' => $this->relative_to_context( $type['uri'] ),
276
+			'@type' => $this->relative_to_context($type['uri']),
277 277
 			'@id'   => $id,
278 278
 			'name'  => $name,
279 279
 		);
280 280
 
281 281
 		// Add the sameAs values associated with the publisher.
282 282
 		$storage_factory = Wordlift_Storage_Factory::get_instance();
283
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
284
-		if ( ! empty( $sameas ) ) {
283
+		$sameas          = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id);
284
+		if ( ! empty($sameas)) {
285 285
 			$params['publisher']['sameAs'] = $sameas;
286 286
 		}
287 287
 
@@ -289,15 +289,15 @@  discard block
 block discarded – undo
289 289
 		// support the logo property.
290 290
 		//
291 291
 		// See http://schema.org/logo.
292
-		if ( 'http://schema.org/Organization' !== $type['uri'] ) {
292
+		if ('http://schema.org/Organization' !== $type['uri']) {
293 293
 			return;
294 294
 		}
295 295
 
296 296
 		// Get the publisher logo.
297
-		$publisher_logo = $this->get_publisher_logo( $post->ID );
297
+		$publisher_logo = $this->get_publisher_logo($post->ID);
298 298
 
299 299
 		// Bail out if the publisher logo isn't set.
300
-		if ( false === $publisher_logo ) {
300
+		if (false === $publisher_logo) {
301 301
 			return;
302 302
 		}
303 303
 
@@ -332,14 +332,14 @@  discard block
 block discarded – undo
332 332
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue.
333 333
 	 *
334 334
 	 */
335
-	private function get_publisher_logo( $post_id ) {
335
+	private function get_publisher_logo($post_id) {
336 336
 
337 337
 		// Get the featured image for the post.
338
-		$thumbnail_id = get_post_thumbnail_id( $post_id );
338
+		$thumbnail_id = get_post_thumbnail_id($post_id);
339 339
 
340 340
 		// Bail out if thumbnail not available.
341
-		if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) {
342
-			$this->log->info( "Featured image not set for post $post_id." );
341
+		if (empty($thumbnail_id) || 0 === $thumbnail_id) {
342
+			$this->log->info("Featured image not set for post $post_id.");
343 343
 
344 344
 			return false;
345 345
 		}
@@ -348,47 +348,47 @@  discard block
 block discarded – undo
348 348
 		$uploads_dir = wp_upload_dir();
349 349
 
350 350
 		// Get the attachment metadata.
351
-		$metadata = wp_get_attachment_metadata( $thumbnail_id );
351
+		$metadata = wp_get_attachment_metadata($thumbnail_id);
352 352
 
353 353
 		// Bail out if the file isn't set.
354
-		if ( ! isset( $metadata['file'] ) ) {
355
-			$this->log->warn( "Featured image file not found for post $post_id." );
354
+		if ( ! isset($metadata['file'])) {
355
+			$this->log->warn("Featured image file not found for post $post_id.");
356 356
 
357 357
 			return false;
358 358
 		}
359 359
 
360 360
 		// Retrieve the relative filename, e.g. "2018/05/logo_publisher.png"
361
-		$path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file'];
361
+		$path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file'];
362 362
 
363 363
 		// Bail out if the file isn't found.
364
-		if ( ! file_exists( $path ) ) {
365
-			$this->log->warn( "Featured image file $path doesn't exist for post $post_id." );
364
+		if ( ! file_exists($path)) {
365
+			$this->log->warn("Featured image file $path doesn't exist for post $post_id.");
366 366
 
367 367
 			return false;
368 368
 		}
369 369
 
370 370
 		// Try to get the image editor and bail out if the editor cannot be instantiated.
371
-		$original_file_editor = wp_get_image_editor( $path );
372
-		if ( is_wp_error( $original_file_editor ) ) {
373
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." );
371
+		$original_file_editor = wp_get_image_editor($path);
372
+		if (is_wp_error($original_file_editor)) {
373
+			$this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id.");
374 374
 
375 375
 			return false;
376 376
 		}
377 377
 
378 378
 		// Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling
379 379
 		// and we don't actually know the end values.
380
-		$publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' );
380
+		$publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo');
381 381
 
382 382
 		// If the file doesn't exist yet, create it.
383
-		if ( ! file_exists( $publisher_logo_path ) ) {
384
-			$original_file_editor->resize( 600, 60 );
385
-			$original_file_editor->save( $publisher_logo_path );
383
+		if ( ! file_exists($publisher_logo_path)) {
384
+			$original_file_editor->resize(600, 60);
385
+			$original_file_editor->save($publisher_logo_path);
386 386
 		}
387 387
 
388 388
 		// Try to get the image editor and bail out if the editor cannot be instantiated.
389
-		$publisher_logo_editor = wp_get_image_editor( $publisher_logo_path );
390
-		if ( is_wp_error( $publisher_logo_editor ) ) {
391
-			$this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." );
389
+		$publisher_logo_editor = wp_get_image_editor($publisher_logo_path);
390
+		if (is_wp_error($publisher_logo_editor)) {
391
+			$this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id.");
392 392
 
393 393
 			return false;
394 394
 		}
@@ -398,7 +398,7 @@  discard block
 block discarded – undo
398 398
 
399 399
 		// Finally return the array with data.
400 400
 		return array(
401
-			'url'    => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ),
401
+			'url'    => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])),
402 402
 			'width'  => $size['width'],
403 403
 			'height' => $size['height'],
404 404
 		);
Please login to merge, or discard this patch.
src/includes/class-wordlift-abstract-post-to-jsonld-converter.php 2 patches
Indentation   +292 added lines, -292 removed lines patch added patch discarded remove patch
@@ -18,317 +18,317 @@
 block discarded – undo
18 18
  */
19 19
 abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter {
20 20
 
21
-	/**
22
-	 * The JSON-LD context.
23
-	 *
24
-	 * @since 3.10.0
25
-	 */
26
-	const CONTEXT = 'http://schema.org';
27
-
28
-	/**
29
-	 * A {@link Wordlift_Entity_Type_Service} instance.
30
-	 *
31
-	 * @since  3.10.0
32
-	 * @access protected
33
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
34
-	 */
35
-	protected $entity_type_service;
36
-
37
-	/**
38
-	 * A {@link Wordlift_Entity_Service} instance.
39
-	 *
40
-	 * @since  3.10.0
41
-	 * @access protected
42
-	 * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance.
43
-	 */
44
-	protected $entity_service;
45
-
46
-	/**
47
-	 * A {@link Wordlift_User_Service} instance.
48
-	 *
49
-	 * @since  3.10.0
50
-	 * @access private
51
-	 * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
52
-	 */
53
-	protected $user_service;
54
-
55
-	/**
56
-	 * A {@link Wordlift_Attachment_Service} instance.
57
-	 *
58
-	 * @since  3.10.0
59
-	 * @access private
60
-	 * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
61
-	 */
62
-	protected $attachment_service;
63
-
64
-	/**
65
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
66
-	 *
67
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
68
-	 * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
69
-	 * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
70
-	 * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
71
-	 *
72
-	 * @since 3.10.0
73
-	 *
74
-	 */
75
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
76
-
77
-		$this->entity_type_service = $entity_type_service;
78
-		$this->entity_service      = $entity_service;
79
-		$this->user_service        = $user_service;
80
-		$this->attachment_service  = $attachment_service;
81
-	}
82
-
83
-	/**
84
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
85
-	 * found while processing the post is set in the $references array.
86
-	 *
87
-	 * @param int $post_id The post id.
88
-	 * @param array $references An array of entity references.
89
-	 *
90
-	 * @return array A JSON-LD array.
91
-	 * @since 3.10.0
92
-	 *
93
-	 */
94
-	public function convert( $post_id, &$references = array() ) {
95
-
96
-		// Get the post instance.
97
-		$post = get_post( $post_id );
98
-		if ( null === $post ) {
99
-			// Post not found.
100
-			return null;
101
-		}
102
-
103
-		// Get the post URI @id.
104
-		$id = $this->entity_service->get_uri( $post->ID );
105
-
106
-		/*
21
+    /**
22
+     * The JSON-LD context.
23
+     *
24
+     * @since 3.10.0
25
+     */
26
+    const CONTEXT = 'http://schema.org';
27
+
28
+    /**
29
+     * A {@link Wordlift_Entity_Type_Service} instance.
30
+     *
31
+     * @since  3.10.0
32
+     * @access protected
33
+     * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
34
+     */
35
+    protected $entity_type_service;
36
+
37
+    /**
38
+     * A {@link Wordlift_Entity_Service} instance.
39
+     *
40
+     * @since  3.10.0
41
+     * @access protected
42
+     * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance.
43
+     */
44
+    protected $entity_service;
45
+
46
+    /**
47
+     * A {@link Wordlift_User_Service} instance.
48
+     *
49
+     * @since  3.10.0
50
+     * @access private
51
+     * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
52
+     */
53
+    protected $user_service;
54
+
55
+    /**
56
+     * A {@link Wordlift_Attachment_Service} instance.
57
+     *
58
+     * @since  3.10.0
59
+     * @access private
60
+     * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
61
+     */
62
+    protected $attachment_service;
63
+
64
+    /**
65
+     * Wordlift_Post_To_Jsonld_Converter constructor.
66
+     *
67
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
68
+     * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
69
+     * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
70
+     * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
71
+     *
72
+     * @since 3.10.0
73
+     *
74
+     */
75
+    public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
76
+
77
+        $this->entity_type_service = $entity_type_service;
78
+        $this->entity_service      = $entity_service;
79
+        $this->user_service        = $user_service;
80
+        $this->attachment_service  = $attachment_service;
81
+    }
82
+
83
+    /**
84
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
85
+     * found while processing the post is set in the $references array.
86
+     *
87
+     * @param int $post_id The post id.
88
+     * @param array $references An array of entity references.
89
+     *
90
+     * @return array A JSON-LD array.
91
+     * @since 3.10.0
92
+     *
93
+     */
94
+    public function convert( $post_id, &$references = array() ) {
95
+
96
+        // Get the post instance.
97
+        $post = get_post( $post_id );
98
+        if ( null === $post ) {
99
+            // Post not found.
100
+            return null;
101
+        }
102
+
103
+        // Get the post URI @id.
104
+        $id = $this->entity_service->get_uri( $post->ID );
105
+
106
+        /*
107 107
 		 * The `types` variable holds one or more entity types. The `type` variable isn't used anymore.
108 108
 		 *
109 109
 		 * @since 3.20.0 We support more than one entity type.
110 110
 		 *
111 111
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
112 112
 		 */
113
-		//		// Get the entity @type. We consider `post` BlogPostings.
114
-		//		$type = $this->entity_type_service->get( $post_id );
115
-		$types = $this->entity_type_service->get_names( $post_id );
116
-
117
-		// Prepare the response.
118
-		$jsonld = array(
119
-			'@context'    => self::CONTEXT,
120
-			'@id'         => $id,
121
-			'@type'       => self::make_one( $types ),
122
-			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
123
-		);
124
-
125
-		// Set the `mainEntityOfPage` property if the post has some contents.
126
-		/*
113
+        //		// Get the entity @type. We consider `post` BlogPostings.
114
+        //		$type = $this->entity_type_service->get( $post_id );
115
+        $types = $this->entity_type_service->get_names( $post_id );
116
+
117
+        // Prepare the response.
118
+        $jsonld = array(
119
+            '@context'    => self::CONTEXT,
120
+            '@id'         => $id,
121
+            '@type'       => self::make_one( $types ),
122
+            'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
123
+        );
124
+
125
+        // Set the `mainEntityOfPage` property if the post has some contents.
126
+        /*
127 127
 		 * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g.
128 128
 		 * because the content is written elsewhere.
129 129
 		 *
130 130
 		 * @since 3.20.0
131 131
 		 */
132
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
133
-		if ( ! empty( $post_content ) ) {
134
-			// We're setting the `mainEntityOfPage` to signal which one is the
135
-			// main entity for the specified URL. It might be however that the
136
-			// post/page is actually about another specific entity. How WL deals
137
-			// with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
138
-			//
139
-			// See http://schema.org/mainEntityOfPage
140
-			//
141
-			// No need to specify `'@type' => 'WebPage'.
142
-			//
143
-			// See https://github.com/insideout10/wordlift-plugin/issues/451.
144
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
145
-		};
146
-
147
-		$this->set_images( $post, $jsonld );
148
-
149
-		// Get the entities referenced by this post and set it to the `references`
150
-		// array so that the caller can do further processing, such as printing out
151
-		// more of those references.
152
-		$references_without_locations = $this->entity_service->get_related_entities( $post->ID );
153
-
154
-		/*
132
+        $post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
133
+        if ( ! empty( $post_content ) ) {
134
+            // We're setting the `mainEntityOfPage` to signal which one is the
135
+            // main entity for the specified URL. It might be however that the
136
+            // post/page is actually about another specific entity. How WL deals
137
+            // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
138
+            //
139
+            // See http://schema.org/mainEntityOfPage
140
+            //
141
+            // No need to specify `'@type' => 'WebPage'.
142
+            //
143
+            // See https://github.com/insideout10/wordlift-plugin/issues/451.
144
+            $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
145
+        };
146
+
147
+        $this->set_images( $post, $jsonld );
148
+
149
+        // Get the entities referenced by this post and set it to the `references`
150
+        // array so that the caller can do further processing, such as printing out
151
+        // more of those references.
152
+        $references_without_locations = $this->entity_service->get_related_entities( $post->ID );
153
+
154
+        /*
155 155
 		 * Add the locations to the references.
156 156
 		 *
157 157
 		 * @since 3.19.5
158 158
 		 *
159 159
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/858.
160 160
 		 */
161
-		// A reference to use in closure.
162
-		$entity_type_service = $this->entity_type_service;
163
-		$locations           = array_reduce( $references_without_locations, function ( $carry, $post_id ) use ( $entity_type_service ) {
164
-			// @see https://schema.org/location for the schema.org types using the `location` property.
165
-			if ( ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' )
166
-			     && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' )
167
-			     && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) ) {
168
-				return $carry;
169
-			}
170
-
171
-			return array_merge( $carry, get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION ) );
172
-		}, array() );
173
-
174
-		// Merge the references with the referenced locations if any.
175
-		$references = array_unique( array_merge( $references_without_locations, $locations ) );
176
-
177
-		return $jsonld;
178
-	}
179
-
180
-	/**
181
-	 * If the provided value starts with the schema.org context, we remove the schema.org
182
-	 * part since it is set with the '@context'.
183
-	 *
184
-	 * @param string $value The property value.
185
-	 *
186
-	 * @return string The property value without the context.
187
-	 * @since 3.10.0
188
-	 *
189
-	 */
190
-	public function relative_to_context( $value ) {
191
-
192
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
193
-	}
194
-
195
-	/**
196
-	 * Set the images, by looking for embedded images, for images loaded via the
197
-	 * gallery and for the featured image.
198
-	 *
199
-	 * Uses the cache service to store the results of this function for a day.
200
-	 *
201
-	 * @param WP_Post $post The target {@link WP_Post}.
202
-	 * @param array $jsonld The JSON-LD array.
203
-	 *
204
-	 * @since 3.10.0
205
-	 *
206
-	 */
207
-	protected function set_images( $post, &$jsonld ) {
208
-
209
-		// Prepare the attachment ids array.
210
-		$ids = array();
211
-
212
-		// Set the thumbnail id as first attachment id, if any.
213
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
214
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
215
-			$ids[] = $thumbnail_id;
216
-		}
217
-
218
-		// For the time being the following is being removed since the query
219
-		// initiated by `get_image_embeds` is consuming lots of CPU.
220
-		//
221
-		// See https://github.com/insideout10/wordlift-plugin/issues/689.
222
-		//
223
-		// Get the embeds, removing existing ids.
224
-		// $embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids );
225
-		$embeds = array();
226
-
227
-		// Get the gallery, removing existing ids.
228
-		$gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
229
-
230
-		// Map the attachment ids to images' data structured for schema.org use.
231
-		$images_with_sizes = array_filter(
232
-			array_reduce( array_merge( $ids, $embeds, $gallery ),
233
-				function ( $carry, $item ) {
234
-					/*
161
+        // A reference to use in closure.
162
+        $entity_type_service = $this->entity_type_service;
163
+        $locations           = array_reduce( $references_without_locations, function ( $carry, $post_id ) use ( $entity_type_service ) {
164
+            // @see https://schema.org/location for the schema.org types using the `location` property.
165
+            if ( ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' )
166
+                 && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' )
167
+                 && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) ) {
168
+                return $carry;
169
+            }
170
+
171
+            return array_merge( $carry, get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION ) );
172
+        }, array() );
173
+
174
+        // Merge the references with the referenced locations if any.
175
+        $references = array_unique( array_merge( $references_without_locations, $locations ) );
176
+
177
+        return $jsonld;
178
+    }
179
+
180
+    /**
181
+     * If the provided value starts with the schema.org context, we remove the schema.org
182
+     * part since it is set with the '@context'.
183
+     *
184
+     * @param string $value The property value.
185
+     *
186
+     * @return string The property value without the context.
187
+     * @since 3.10.0
188
+     *
189
+     */
190
+    public function relative_to_context( $value ) {
191
+
192
+        return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
193
+    }
194
+
195
+    /**
196
+     * Set the images, by looking for embedded images, for images loaded via the
197
+     * gallery and for the featured image.
198
+     *
199
+     * Uses the cache service to store the results of this function for a day.
200
+     *
201
+     * @param WP_Post $post The target {@link WP_Post}.
202
+     * @param array $jsonld The JSON-LD array.
203
+     *
204
+     * @since 3.10.0
205
+     *
206
+     */
207
+    protected function set_images( $post, &$jsonld ) {
208
+
209
+        // Prepare the attachment ids array.
210
+        $ids = array();
211
+
212
+        // Set the thumbnail id as first attachment id, if any.
213
+        $thumbnail_id = get_post_thumbnail_id( $post->ID );
214
+        if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
215
+            $ids[] = $thumbnail_id;
216
+        }
217
+
218
+        // For the time being the following is being removed since the query
219
+        // initiated by `get_image_embeds` is consuming lots of CPU.
220
+        //
221
+        // See https://github.com/insideout10/wordlift-plugin/issues/689.
222
+        //
223
+        // Get the embeds, removing existing ids.
224
+        // $embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids );
225
+        $embeds = array();
226
+
227
+        // Get the gallery, removing existing ids.
228
+        $gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
229
+
230
+        // Map the attachment ids to images' data structured for schema.org use.
231
+        $images_with_sizes = array_filter(
232
+            array_reduce( array_merge( $ids, $embeds, $gallery ),
233
+                function ( $carry, $item ) {
234
+                    /*
235 235
 					* @todo: we're not sure that we're getting attachment data here, we
236 236
 					* should filter `false`s.
237 237
 					*/
238 238
 
239
-					$sources = array_merge(
240
-						Wordlift_Image_Service::get_sources( $item ),
241
-						array( wp_get_attachment_image_src( $item, 'full' ) )
242
-					);
243
-
244
-					$sources_with_image = array_filter( $sources, function ( $source ) {
245
-						return ! empty( $source[0] );
246
-					} );
247
-
248
-					// Get the attachment data.
249
-					// $attachment = wp_get_attachment_image_src( $item, 'full' );
250
-
251
-					// var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) );
252
-
253
-					// Bail if image is not found.
254
-					// In some cases, you can delete the image from the database
255
-					// or from uploads dir, but the image id still exists as featured image
256
-					// or in [gallery] shortcode.
257
-					//					if ( empty( $attachment[0] ) ) {
258
-					if ( empty( $sources_with_image ) ) {
259
-						return $carry;
260
-					}
261
-
262
-					// Merge the arrays.
263
-					return array_merge(
264
-						$carry,
265
-						$sources_with_image
266
-					);
267
-				}
268
-				// Initial array.
269
-				, array() )
270
-		);
271
-
272
-		// Refactor data as per schema.org specifications.
273
-		$images = array_map( function ( $attachment ) {
274
-			return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
275
-				array(
276
-					'@type' => 'ImageObject',
277
-					'url'   => $attachment[0],
278
-				), $attachment
279
-			);
280
-		}, $images_with_sizes );
281
-
282
-		// Add images if present.
283
-		if ( 0 < count( $images ) ) {
284
-			$jsonld['image'] = $images;
285
-		}
286
-
287
-	}
288
-
289
-	/**
290
-	 * If the provided array of values contains only one value, then one single
291
-	 * value is returned, otherwise the original array is returned.
292
-	 *
293
-	 * @param array $value An array of values.
294
-	 *
295
-	 * @return mixed|array A single value or the original array.
296
-	 * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to
297
-	 *  {@link Wordlift_Abstract_Post_To_Jsonld_Converter}.
298
-	 * @since  3.8.0
299
-	 * @access private
300
-	 *
301
-	 */
302
-	protected static function make_one( $value ) {
303
-
304
-		return 1 === count( $value ) ? $value[0] : $value;
305
-	}
306
-
307
-	/**
308
-	 * Process the provided array by adding the width / height if the values
309
-	 * are available and are greater than 0.
310
-	 *
311
-	 * @param array $image The `ImageObject` array.
312
-	 * @param array $attachment The attachment array.
313
-	 *
314
-	 * @return array The enriched `ImageObject` array.
315
-	 * @since 3.14.0
316
-	 *
317
-	 */
318
-	public static function set_image_size( $image, $attachment ) {
319
-
320
-		// If you specify a "width" or "height" value you should leave out
321
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
322
-		//
323
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
324
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
325
-			$image['width'] = $attachment[1];
326
-		}
327
-
328
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
329
-			$image['height'] = $attachment[2];
330
-		}
331
-
332
-		return $image;
333
-	}
239
+                    $sources = array_merge(
240
+                        Wordlift_Image_Service::get_sources( $item ),
241
+                        array( wp_get_attachment_image_src( $item, 'full' ) )
242
+                    );
243
+
244
+                    $sources_with_image = array_filter( $sources, function ( $source ) {
245
+                        return ! empty( $source[0] );
246
+                    } );
247
+
248
+                    // Get the attachment data.
249
+                    // $attachment = wp_get_attachment_image_src( $item, 'full' );
250
+
251
+                    // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) );
252
+
253
+                    // Bail if image is not found.
254
+                    // In some cases, you can delete the image from the database
255
+                    // or from uploads dir, but the image id still exists as featured image
256
+                    // or in [gallery] shortcode.
257
+                    //					if ( empty( $attachment[0] ) ) {
258
+                    if ( empty( $sources_with_image ) ) {
259
+                        return $carry;
260
+                    }
261
+
262
+                    // Merge the arrays.
263
+                    return array_merge(
264
+                        $carry,
265
+                        $sources_with_image
266
+                    );
267
+                }
268
+                // Initial array.
269
+                , array() )
270
+        );
271
+
272
+        // Refactor data as per schema.org specifications.
273
+        $images = array_map( function ( $attachment ) {
274
+            return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
275
+                array(
276
+                    '@type' => 'ImageObject',
277
+                    'url'   => $attachment[0],
278
+                ), $attachment
279
+            );
280
+        }, $images_with_sizes );
281
+
282
+        // Add images if present.
283
+        if ( 0 < count( $images ) ) {
284
+            $jsonld['image'] = $images;
285
+        }
286
+
287
+    }
288
+
289
+    /**
290
+     * If the provided array of values contains only one value, then one single
291
+     * value is returned, otherwise the original array is returned.
292
+     *
293
+     * @param array $value An array of values.
294
+     *
295
+     * @return mixed|array A single value or the original array.
296
+     * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to
297
+     *  {@link Wordlift_Abstract_Post_To_Jsonld_Converter}.
298
+     * @since  3.8.0
299
+     * @access private
300
+     *
301
+     */
302
+    protected static function make_one( $value ) {
303
+
304
+        return 1 === count( $value ) ? $value[0] : $value;
305
+    }
306
+
307
+    /**
308
+     * Process the provided array by adding the width / height if the values
309
+     * are available and are greater than 0.
310
+     *
311
+     * @param array $image The `ImageObject` array.
312
+     * @param array $attachment The attachment array.
313
+     *
314
+     * @return array The enriched `ImageObject` array.
315
+     * @since 3.14.0
316
+     *
317
+     */
318
+    public static function set_image_size( $image, $attachment ) {
319
+
320
+        // If you specify a "width" or "height" value you should leave out
321
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
322
+        //
323
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
324
+        if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
325
+            $image['width'] = $attachment[1];
326
+        }
327
+
328
+        if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
329
+            $image['height'] = $attachment[2];
330
+        }
331
+
332
+        return $image;
333
+    }
334 334
 }
Please login to merge, or discard this patch.
Spacing   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 	 * @since 3.10.0
73 73
 	 *
74 74
 	 */
75
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
75
+	public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service) {
76 76
 
77 77
 		$this->entity_type_service = $entity_type_service;
78 78
 		$this->entity_service      = $entity_service;
@@ -91,17 +91,17 @@  discard block
 block discarded – undo
91 91
 	 * @since 3.10.0
92 92
 	 *
93 93
 	 */
94
-	public function convert( $post_id, &$references = array() ) {
94
+	public function convert($post_id, &$references = array()) {
95 95
 
96 96
 		// Get the post instance.
97
-		$post = get_post( $post_id );
98
-		if ( null === $post ) {
97
+		$post = get_post($post_id);
98
+		if (null === $post) {
99 99
 			// Post not found.
100 100
 			return null;
101 101
 		}
102 102
 
103 103
 		// Get the post URI @id.
104
-		$id = $this->entity_service->get_uri( $post->ID );
104
+		$id = $this->entity_service->get_uri($post->ID);
105 105
 
106 106
 		/*
107 107
 		 * The `types` variable holds one or more entity types. The `type` variable isn't used anymore.
@@ -112,14 +112,14 @@  discard block
 block discarded – undo
112 112
 		 */
113 113
 		//		// Get the entity @type. We consider `post` BlogPostings.
114 114
 		//		$type = $this->entity_type_service->get( $post_id );
115
-		$types = $this->entity_type_service->get_names( $post_id );
115
+		$types = $this->entity_type_service->get_names($post_id);
116 116
 
117 117
 		// Prepare the response.
118 118
 		$jsonld = array(
119 119
 			'@context'    => self::CONTEXT,
120 120
 			'@id'         => $id,
121
-			'@type'       => self::make_one( $types ),
122
-			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
121
+			'@type'       => self::make_one($types),
122
+			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt($post),
123 123
 		);
124 124
 
125 125
 		// Set the `mainEntityOfPage` property if the post has some contents.
@@ -129,8 +129,8 @@  discard block
 block discarded – undo
129 129
 		 *
130 130
 		 * @since 3.20.0
131 131
 		 */
132
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
133
-		if ( ! empty( $post_content ) ) {
132
+		$post_content = apply_filters('wl_post_content', $post->post_content, $post);
133
+		if ( ! empty($post_content)) {
134 134
 			// We're setting the `mainEntityOfPage` to signal which one is the
135 135
 			// main entity for the specified URL. It might be however that the
136 136
 			// post/page is actually about another specific entity. How WL deals
@@ -141,15 +141,15 @@  discard block
 block discarded – undo
141 141
 			// No need to specify `'@type' => 'WebPage'.
142 142
 			//
143 143
 			// See https://github.com/insideout10/wordlift-plugin/issues/451.
144
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
144
+			$jsonld['mainEntityOfPage'] = get_the_permalink($post->ID);
145 145
 		};
146 146
 
147
-		$this->set_images( $post, $jsonld );
147
+		$this->set_images($post, $jsonld);
148 148
 
149 149
 		// Get the entities referenced by this post and set it to the `references`
150 150
 		// array so that the caller can do further processing, such as printing out
151 151
 		// more of those references.
152
-		$references_without_locations = $this->entity_service->get_related_entities( $post->ID );
152
+		$references_without_locations = $this->entity_service->get_related_entities($post->ID);
153 153
 
154 154
 		/*
155 155
 		 * Add the locations to the references.
@@ -160,19 +160,19 @@  discard block
 block discarded – undo
160 160
 		 */
161 161
 		// A reference to use in closure.
162 162
 		$entity_type_service = $this->entity_type_service;
163
-		$locations           = array_reduce( $references_without_locations, function ( $carry, $post_id ) use ( $entity_type_service ) {
163
+		$locations           = array_reduce($references_without_locations, function($carry, $post_id) use ($entity_type_service) {
164 164
 			// @see https://schema.org/location for the schema.org types using the `location` property.
165
-			if ( ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' )
166
-			     && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' )
167
-			     && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) ) {
165
+			if ( ! $entity_type_service->has_entity_type($post_id, 'http://schema.org/Action')
166
+			     && ! $entity_type_service->has_entity_type($post_id, 'http://schema.org/Event')
167
+			     && ! $entity_type_service->has_entity_type($post_id, 'http://schema.org/Organization')) {
168 168
 				return $carry;
169 169
 			}
170 170
 
171
-			return array_merge( $carry, get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION ) );
172
-		}, array() );
171
+			return array_merge($carry, get_post_meta($post_id, Wordlift_Schema_Service::FIELD_LOCATION));
172
+		}, array());
173 173
 
174 174
 		// Merge the references with the referenced locations if any.
175
-		$references = array_unique( array_merge( $references_without_locations, $locations ) );
175
+		$references = array_unique(array_merge($references_without_locations, $locations));
176 176
 
177 177
 		return $jsonld;
178 178
 	}
@@ -187,9 +187,9 @@  discard block
 block discarded – undo
187 187
 	 * @since 3.10.0
188 188
 	 *
189 189
 	 */
190
-	public function relative_to_context( $value ) {
190
+	public function relative_to_context($value) {
191 191
 
192
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
192
+		return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value;
193 193
 	}
194 194
 
195 195
 	/**
@@ -204,14 +204,14 @@  discard block
 block discarded – undo
204 204
 	 * @since 3.10.0
205 205
 	 *
206 206
 	 */
207
-	protected function set_images( $post, &$jsonld ) {
207
+	protected function set_images($post, &$jsonld) {
208 208
 
209 209
 		// Prepare the attachment ids array.
210 210
 		$ids = array();
211 211
 
212 212
 		// Set the thumbnail id as first attachment id, if any.
213
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
214
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
213
+		$thumbnail_id = get_post_thumbnail_id($post->ID);
214
+		if ('' !== $thumbnail_id && 0 !== $thumbnail_id) {
215 215
 			$ids[] = $thumbnail_id;
216 216
 		}
217 217
 
@@ -225,24 +225,24 @@  discard block
 block discarded – undo
225 225
 		$embeds = array();
226 226
 
227 227
 		// Get the gallery, removing existing ids.
228
-		$gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
228
+		$gallery = array_diff($this->attachment_service->get_gallery($post), $ids, $embeds);
229 229
 
230 230
 		// Map the attachment ids to images' data structured for schema.org use.
231 231
 		$images_with_sizes = array_filter(
232
-			array_reduce( array_merge( $ids, $embeds, $gallery ),
233
-				function ( $carry, $item ) {
232
+			array_reduce(array_merge($ids, $embeds, $gallery),
233
+				function($carry, $item) {
234 234
 					/*
235 235
 					* @todo: we're not sure that we're getting attachment data here, we
236 236
 					* should filter `false`s.
237 237
 					*/
238 238
 
239 239
 					$sources = array_merge(
240
-						Wordlift_Image_Service::get_sources( $item ),
241
-						array( wp_get_attachment_image_src( $item, 'full' ) )
240
+						Wordlift_Image_Service::get_sources($item),
241
+						array(wp_get_attachment_image_src($item, 'full'))
242 242
 					);
243 243
 
244
-					$sources_with_image = array_filter( $sources, function ( $source ) {
245
-						return ! empty( $source[0] );
244
+					$sources_with_image = array_filter($sources, function($source) {
245
+						return ! empty($source[0]);
246 246
 					} );
247 247
 
248 248
 					// Get the attachment data.
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
 					// or from uploads dir, but the image id still exists as featured image
256 256
 					// or in [gallery] shortcode.
257 257
 					//					if ( empty( $attachment[0] ) ) {
258
-					if ( empty( $sources_with_image ) ) {
258
+					if (empty($sources_with_image)) {
259 259
 						return $carry;
260 260
 					}
261 261
 
@@ -266,21 +266,21 @@  discard block
 block discarded – undo
266 266
 					);
267 267
 				}
268 268
 				// Initial array.
269
-				, array() )
269
+				, array())
270 270
 		);
271 271
 
272 272
 		// Refactor data as per schema.org specifications.
273
-		$images = array_map( function ( $attachment ) {
273
+		$images = array_map(function($attachment) {
274 274
 			return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
275 275
 				array(
276 276
 					'@type' => 'ImageObject',
277 277
 					'url'   => $attachment[0],
278 278
 				), $attachment
279 279
 			);
280
-		}, $images_with_sizes );
280
+		}, $images_with_sizes);
281 281
 
282 282
 		// Add images if present.
283
-		if ( 0 < count( $images ) ) {
283
+		if (0 < count($images)) {
284 284
 			$jsonld['image'] = $images;
285 285
 		}
286 286
 
@@ -299,9 +299,9 @@  discard block
 block discarded – undo
299 299
 	 * @access private
300 300
 	 *
301 301
 	 */
302
-	protected static function make_one( $value ) {
302
+	protected static function make_one($value) {
303 303
 
304
-		return 1 === count( $value ) ? $value[0] : $value;
304
+		return 1 === count($value) ? $value[0] : $value;
305 305
 	}
306 306
 
307 307
 	/**
@@ -315,17 +315,17 @@  discard block
 block discarded – undo
315 315
 	 * @since 3.14.0
316 316
 	 *
317 317
 	 */
318
-	public static function set_image_size( $image, $attachment ) {
318
+	public static function set_image_size($image, $attachment) {
319 319
 
320 320
 		// If you specify a "width" or "height" value you should leave out
321 321
 		// 'px'. For example: "width":"4608px" should be "width":"4608".
322 322
 		//
323 323
 		// See https://github.com/insideout10/wordlift-plugin/issues/451.
324
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
324
+		if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) {
325 325
 			$image['width'] = $attachment[1];
326 326
 		}
327 327
 
328
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
328
+		if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) {
329 329
 			$image['height'] = $attachment[2];
330 330
 		}
331 331
 
Please login to merge, or discard this patch.
src/shortcodes/wordlift_shortcode_chord.php 2 patches
Indentation   +175 added lines, -175 removed lines patch added patch discarded remove patch
@@ -15,45 +15,45 @@  discard block
 block discarded – undo
15 15
  * @return mixed
16 16
  */
17 17
 function wl_shortcode_chord_most_referenced_entity_id() {
18
-	// Get the last 20 articles by post date.
19
-	// For each article get the entities they reference.
20
-	$post_ids = get_posts( array(
21
-		'numberposts' => 20,
22
-		'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
23
-		'fields'      => 'ids', // Only get post IDs.
24
-		'post_status' => 'publish',
25
-		'tax_query'   => array(
26
-			'relation' => 'OR',
27
-			array(
28
-				'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
29
-				'operator' => 'NOT EXISTS',
30
-			),
31
-			array(
32
-				'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
33
-				'field'    => 'slug',
34
-				'terms'    => 'article',
35
-			),
36
-		),
37
-		'orderby'     => 'post_date',
38
-		'order'       => 'DESC',
39
-	) );
40
-
41
-	if ( empty( $post_ids ) ) {
42
-		return null;
43
-	}
44
-
45
-	$entities = array();
46
-	foreach ( $post_ids as $id ) {
47
-		$entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) );
48
-	}
49
-
50
-	$famous_entities = array_count_values( $entities );
51
-	arsort( $famous_entities );
52
-	if ( sizeof( $famous_entities ) >= 1 ) {
53
-		return key( $famous_entities );
54
-	} else {
55
-		return $post_ids[0];
56
-	}
18
+    // Get the last 20 articles by post date.
19
+    // For each article get the entities they reference.
20
+    $post_ids = get_posts( array(
21
+        'numberposts' => 20,
22
+        'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
23
+        'fields'      => 'ids', // Only get post IDs.
24
+        'post_status' => 'publish',
25
+        'tax_query'   => array(
26
+            'relation' => 'OR',
27
+            array(
28
+                'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
29
+                'operator' => 'NOT EXISTS',
30
+            ),
31
+            array(
32
+                'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
33
+                'field'    => 'slug',
34
+                'terms'    => 'article',
35
+            ),
36
+        ),
37
+        'orderby'     => 'post_date',
38
+        'order'       => 'DESC',
39
+    ) );
40
+
41
+    if ( empty( $post_ids ) ) {
42
+        return null;
43
+    }
44
+
45
+    $entities = array();
46
+    foreach ( $post_ids as $id ) {
47
+        $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) );
48
+    }
49
+
50
+    $famous_entities = array_count_values( $entities );
51
+    arsort( $famous_entities );
52
+    if ( sizeof( $famous_entities ) >= 1 ) {
53
+        return key( $famous_entities );
54
+    } else {
55
+        return $post_ids[0];
56
+    }
57 57
 
58 58
 }
59 59
 
@@ -71,60 +71,60 @@  discard block
 block discarded – undo
71 71
  */
72 72
 function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) {
73 73
 
74
-	if ( ! is_null( $related ) ) {
75
-		if ( 0 === $depth ) {
76
-			return $related;
77
-		}
78
-	}
74
+    if ( ! is_null( $related ) ) {
75
+        if ( 0 === $depth ) {
76
+            return $related;
77
+        }
78
+    }
79 79
 
80
-	wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" );
80
+    wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" );
81 81
 
82
-	// Create a related array which will hold entities and relations.
83
-	if ( is_null( $related ) ) {
84
-		$related = array(
85
-			'entities'  => array( $entity_id ),
86
-			'relations' => array(),
87
-		);
88
-	}
82
+    // Create a related array which will hold entities and relations.
83
+    if ( is_null( $related ) ) {
84
+        $related = array(
85
+            'entities'  => array( $entity_id ),
86
+            'relations' => array(),
87
+        );
88
+    }
89 89
 
90
-	// Get related entities
91
-	$related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array(
92
-		'status' => 'publish',
93
-	) );
90
+    // Get related entities
91
+    $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array(
92
+        'status' => 'publish',
93
+    ) );
94 94
 
95
-	// If the current node is an entity, add related posts too
96
-	$related_post_ids = ( Wordlift_Entity_Service::get_instance()
97
-	                                             ->is_entity( $entity_id ) ) ?
98
-		wl_core_get_related_post_ids( $entity_id, array(
99
-			'status' => 'publish',
100
-		) ) :
101
-		array();
95
+    // If the current node is an entity, add related posts too
96
+    $related_post_ids = ( Wordlift_Entity_Service::get_instance()
97
+                                                    ->is_entity( $entity_id ) ) ?
98
+        wl_core_get_related_post_ids( $entity_id, array(
99
+            'status' => 'publish',
100
+        ) ) :
101
+        array();
102 102
 
103
-	// Merge results and remove duplicated entries
104
-	$related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) );
103
+    // Merge results and remove duplicated entries
104
+    $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) );
105 105
 
106
-	// TODO: List of entities ($rel) should be ordered by interest factors.
107
-	shuffle( $related_ids );
106
+    // TODO: List of entities ($rel) should be ordered by interest factors.
107
+    shuffle( $related_ids );
108 108
 
109
-	// Now we have all the related IDs.
110
-	foreach ( $related_ids as $related_id ) {
109
+    // Now we have all the related IDs.
110
+    foreach ( $related_ids as $related_id ) {
111 111
 
112
-		if ( count( $related['entities'] ) >= $max_size ) {
113
-			return $related;
114
-		}
112
+        if ( count( $related['entities'] ) >= $max_size ) {
113
+            return $related;
114
+        }
115 115
 
116
-		$related['relations'][] = array( $entity_id, $related_id );
116
+        $related['relations'][] = array( $entity_id, $related_id );
117 117
 
118
-		if ( ! in_array( $related_id, $related['entities'] ) ) {
119
-			// Found new related entity!
120
-			$related['entities'][] = $related_id;
118
+        if ( ! in_array( $related_id, $related['entities'] ) ) {
119
+            // Found new related entity!
120
+            $related['entities'][] = $related_id;
121 121
 
122
-			$related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size );
123
-		}
124
-	}
122
+            $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size );
123
+        }
124
+    }
125 125
 
126
-	// End condition 2: no more entities to search for.
127
-	return $related;
126
+    // End condition 2: no more entities to search for.
127
+    return $related;
128 128
 }
129 129
 
130 130
 /**
@@ -138,57 +138,57 @@  discard block
 block discarded – undo
138 138
  */
139 139
 function wl_shortcode_chord_get_graph( $data ) {
140 140
 
141
-	// Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class).
142
-	array_walk( $data['entities'], function ( &$item ) {
143
-		$post = get_post( $item );
144
-
145
-		// Skip non-existing posts.
146
-		if ( is_null( $post ) ) {
147
-			wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" );
148
-
149
-			return $item;
150
-		}
151
-
152
-		// Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set).
153
-		$term = Wordlift_Entity_Type_Service::get_instance()->get( $item );
154
-
155
-		// The following log may create a circular loop.
156
-		// wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" );
157
-
158
-		// TODO: get all images
159
-		$thumbnail    = null;
160
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
161
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
162
-			$attachment = wp_get_attachment_image_src( $thumbnail_id );
163
-			if ( false !== $attachment ) {
164
-				$thumbnail = esc_attr( $attachment[0] );
165
-			}
166
-		}
167
-
168
-		$entity = array(
169
-			'uri'        => wl_get_entity_uri( $item ),
170
-			'url'        => get_permalink( $item ),
171
-			'label'      => $post->post_title,
172
-			'type'       => $post->post_type,
173
-			'thumbnails' => array( $thumbnail ),
174
-			'css_class'  => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ),
175
-		);
176
-
177
-		$item = $entity;
178
-	} );
179
-
180
-	// Refactor the relations.
181
-	array_walk( $data['relations'], function ( &$item ) {
182
-		$relation = array(
183
-			's' => wl_get_entity_uri( $item[0] ),
184
-			'o' => wl_get_entity_uri( $item[1] ),
185
-		);
186
-
187
-		$item = $relation;
188
-	} );
189
-
190
-	// Return the JSON representation.
191
-	return $data;
141
+    // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class).
142
+    array_walk( $data['entities'], function ( &$item ) {
143
+        $post = get_post( $item );
144
+
145
+        // Skip non-existing posts.
146
+        if ( is_null( $post ) ) {
147
+            wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" );
148
+
149
+            return $item;
150
+        }
151
+
152
+        // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set).
153
+        $term = Wordlift_Entity_Type_Service::get_instance()->get( $item );
154
+
155
+        // The following log may create a circular loop.
156
+        // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" );
157
+
158
+        // TODO: get all images
159
+        $thumbnail    = null;
160
+        $thumbnail_id = get_post_thumbnail_id( $post->ID );
161
+        if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
162
+            $attachment = wp_get_attachment_image_src( $thumbnail_id );
163
+            if ( false !== $attachment ) {
164
+                $thumbnail = esc_attr( $attachment[0] );
165
+            }
166
+        }
167
+
168
+        $entity = array(
169
+            'uri'        => wl_get_entity_uri( $item ),
170
+            'url'        => get_permalink( $item ),
171
+            'label'      => $post->post_title,
172
+            'type'       => $post->post_type,
173
+            'thumbnails' => array( $thumbnail ),
174
+            'css_class'  => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ),
175
+        );
176
+
177
+        $item = $entity;
178
+    } );
179
+
180
+    // Refactor the relations.
181
+    array_walk( $data['relations'], function ( &$item ) {
182
+        $relation = array(
183
+            's' => wl_get_entity_uri( $item[0] ),
184
+            'o' => wl_get_entity_uri( $item[1] ),
185
+        );
186
+
187
+        $item = $relation;
188
+    } );
189
+
190
+    // Return the JSON representation.
191
+    return $data;
192 192
 }
193 193
 
194 194
 /**
@@ -199,13 +199,13 @@  discard block
 block discarded – undo
199 199
  */
200 200
 function wl_shortcode_chord_ajax() {
201 201
 
202
-	$post_id = $_REQUEST['post_id'];
203
-	$depth   = $_REQUEST['depth'];
202
+    $post_id = $_REQUEST['post_id'];
203
+    $depth   = $_REQUEST['depth'];
204 204
 
205
-	$relations = wl_shortcode_chord_get_relations( $post_id, $depth );
206
-	$graph     = wl_shortcode_chord_get_graph( $relations );
205
+    $relations = wl_shortcode_chord_get_relations( $post_id, $depth );
206
+    $graph     = wl_shortcode_chord_get_graph( $relations );
207 207
 
208
-	wl_core_send_json( $graph );
208
+    wl_core_send_json( $graph );
209 209
 }
210 210
 
211 211
 add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' );
@@ -216,42 +216,42 @@  discard block
 block discarded – undo
216 216
  */
217 217
 add_action( 'init', function () {
218 218
 
219
-	// Bail out if the `register_block_type` function isn't available.
220
-	if ( ! function_exists( 'register_block_type' ) ) {
221
-		return;
222
-	}
223
-
224
-	register_block_type( 'wordlift/chord', array(
225
-		'editor_script'   => 'wl-block-editor',
226
-		'render_callback' => function ( $attributes ) {
227
-			$attr_code = '';
228
-			foreach ( $attributes as $key => $value ) {
229
-				$attr_code .= $key . '="' . $value . '" ';
230
-			}
231
-
232
-			return '[wl_chord ' . $attr_code . ']';
233
-		},
234
-		'attributes'      => array(
235
-			'width'      => array(
236
-				'type'    => 'string',
237
-				'default' => '100%',
238
-			),
239
-			'height'     => array(
240
-				'type'    => 'string',
241
-				'default' => '500px',
242
-			),
243
-			'main_color' => array(
244
-				'type'    => 'string',
245
-				'default' => '000',
246
-			),
247
-			'depth'      => array(
248
-				'type'    => 'number',
249
-				'default' => 2,
250
-			),
251
-			'global'     => array(
252
-				'type'    => 'bool',
253
-				'default' => false,
254
-			),
255
-		),
256
-	) );
219
+    // Bail out if the `register_block_type` function isn't available.
220
+    if ( ! function_exists( 'register_block_type' ) ) {
221
+        return;
222
+    }
223
+
224
+    register_block_type( 'wordlift/chord', array(
225
+        'editor_script'   => 'wl-block-editor',
226
+        'render_callback' => function ( $attributes ) {
227
+            $attr_code = '';
228
+            foreach ( $attributes as $key => $value ) {
229
+                $attr_code .= $key . '="' . $value . '" ';
230
+            }
231
+
232
+            return '[wl_chord ' . $attr_code . ']';
233
+        },
234
+        'attributes'      => array(
235
+            'width'      => array(
236
+                'type'    => 'string',
237
+                'default' => '100%',
238
+            ),
239
+            'height'     => array(
240
+                'type'    => 'string',
241
+                'default' => '500px',
242
+            ),
243
+            'main_color' => array(
244
+                'type'    => 'string',
245
+                'default' => '000',
246
+            ),
247
+            'depth'      => array(
248
+                'type'    => 'number',
249
+                'default' => 2,
250
+            ),
251
+            'global'     => array(
252
+                'type'    => 'bool',
253
+                'default' => false,
254
+            ),
255
+        ),
256
+    ) );
257 257
 } );
Please login to merge, or discard this patch.
Spacing   +59 added lines, -60 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
 function wl_shortcode_chord_most_referenced_entity_id() {
18 18
 	// Get the last 20 articles by post date.
19 19
 	// For each article get the entities they reference.
20
-	$post_ids = get_posts( array(
20
+	$post_ids = get_posts(array(
21 21
 		'numberposts' => 20,
22 22
 		'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
23 23
 		'fields'      => 'ids', // Only get post IDs.
@@ -36,21 +36,21 @@  discard block
 block discarded – undo
36 36
 		),
37 37
 		'orderby'     => 'post_date',
38 38
 		'order'       => 'DESC',
39
-	) );
39
+	));
40 40
 
41
-	if ( empty( $post_ids ) ) {
41
+	if (empty($post_ids)) {
42 42
 		return null;
43 43
 	}
44 44
 
45 45
 	$entities = array();
46
-	foreach ( $post_ids as $id ) {
47
-		$entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) );
46
+	foreach ($post_ids as $id) {
47
+		$entities = array_merge($entities, wl_core_get_related_entity_ids($id));
48 48
 	}
49 49
 
50
-	$famous_entities = array_count_values( $entities );
51
-	arsort( $famous_entities );
52
-	if ( sizeof( $famous_entities ) >= 1 ) {
53
-		return key( $famous_entities );
50
+	$famous_entities = array_count_values($entities);
51
+	arsort($famous_entities);
52
+	if (sizeof($famous_entities) >= 1) {
53
+		return key($famous_entities);
54 54
 	} else {
55 55
 		return $post_ids[0];
56 56
 	}
@@ -69,57 +69,56 @@  discard block
 block discarded – undo
69 69
  * @uses wl_core_get_related_post_ids() to get the list of post ids that reference an entity.
70 70
  *
71 71
  */
72
-function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) {
72
+function wl_shortcode_chord_get_relations($entity_id, $depth = 2, $related = null, $max_size = 9) {
73 73
 
74
-	if ( ! is_null( $related ) ) {
75
-		if ( 0 === $depth ) {
74
+	if ( ! is_null($related)) {
75
+		if (0 === $depth) {
76 76
 			return $related;
77 77
 		}
78 78
 	}
79 79
 
80
-	wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" );
80
+	wl_write_log("wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: ".(is_null($related) ? 'yes' : 'no')." ]");
81 81
 
82 82
 	// Create a related array which will hold entities and relations.
83
-	if ( is_null( $related ) ) {
83
+	if (is_null($related)) {
84 84
 		$related = array(
85
-			'entities'  => array( $entity_id ),
85
+			'entities'  => array($entity_id),
86 86
 			'relations' => array(),
87 87
 		);
88 88
 	}
89 89
 
90 90
 	// Get related entities
91
-	$related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array(
91
+	$related_entity_ids = wl_core_get_related_entity_ids($entity_id, array(
92 92
 		'status' => 'publish',
93
-	) );
93
+	));
94 94
 
95 95
 	// If the current node is an entity, add related posts too
96
-	$related_post_ids = ( Wordlift_Entity_Service::get_instance()
97
-	                                             ->is_entity( $entity_id ) ) ?
98
-		wl_core_get_related_post_ids( $entity_id, array(
96
+	$related_post_ids = (Wordlift_Entity_Service::get_instance()
97
+	                                             ->is_entity($entity_id)) ?
98
+		wl_core_get_related_post_ids($entity_id, array(
99 99
 			'status' => 'publish',
100
-		) ) :
101
-		array();
100
+		)) : array();
102 101
 
103 102
 	// Merge results and remove duplicated entries
104
-	$related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) );
103
+	$related_ids = array_unique(array_merge($related_post_ids, $related_entity_ids));
105 104
 
106 105
 	// TODO: List of entities ($rel) should be ordered by interest factors.
107
-	shuffle( $related_ids );
106
+	shuffle($related_ids);
108 107
 
109 108
 	// Now we have all the related IDs.
110
-	foreach ( $related_ids as $related_id ) {
109
+	foreach ($related_ids as $related_id) {
111 110
 
112
-		if ( count( $related['entities'] ) >= $max_size ) {
111
+		if (count($related['entities']) >= $max_size) {
113 112
 			return $related;
114 113
 		}
115 114
 
116
-		$related['relations'][] = array( $entity_id, $related_id );
115
+		$related['relations'][] = array($entity_id, $related_id);
117 116
 
118
-		if ( ! in_array( $related_id, $related['entities'] ) ) {
117
+		if ( ! in_array($related_id, $related['entities'])) {
119 118
 			// Found new related entity!
120 119
 			$related['entities'][] = $related_id;
121 120
 
122
-			$related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size );
121
+			$related = wl_shortcode_chord_get_relations($related_id, ($depth - 1), $related, $max_size);
123 122
 		}
124 123
 	}
125 124
 
@@ -136,52 +135,52 @@  discard block
 block discarded – undo
136 135
  *
137 136
  * @return mixed|string
138 137
  */
139
-function wl_shortcode_chord_get_graph( $data ) {
138
+function wl_shortcode_chord_get_graph($data) {
140 139
 
141 140
 	// Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class).
142
-	array_walk( $data['entities'], function ( &$item ) {
143
-		$post = get_post( $item );
141
+	array_walk($data['entities'], function(&$item) {
142
+		$post = get_post($item);
144 143
 
145 144
 		// Skip non-existing posts.
146
-		if ( is_null( $post ) ) {
147
-			wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" );
145
+		if (is_null($post)) {
146
+			wl_write_log("wl_shortcode_chord_get_graph : post not found [ post id :: $item ]");
148 147
 
149 148
 			return $item;
150 149
 		}
151 150
 
152 151
 		// Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set).
153
-		$term = Wordlift_Entity_Type_Service::get_instance()->get( $item );
152
+		$term = Wordlift_Entity_Type_Service::get_instance()->get($item);
154 153
 
155 154
 		// The following log may create a circular loop.
156 155
 		// wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" );
157 156
 
158 157
 		// TODO: get all images
159 158
 		$thumbnail    = null;
160
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
161
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
162
-			$attachment = wp_get_attachment_image_src( $thumbnail_id );
163
-			if ( false !== $attachment ) {
164
-				$thumbnail = esc_attr( $attachment[0] );
159
+		$thumbnail_id = get_post_thumbnail_id($post->ID);
160
+		if ('' !== $thumbnail_id && 0 !== $thumbnail_id) {
161
+			$attachment = wp_get_attachment_image_src($thumbnail_id);
162
+			if (false !== $attachment) {
163
+				$thumbnail = esc_attr($attachment[0]);
165 164
 			}
166 165
 		}
167 166
 
168 167
 		$entity = array(
169
-			'uri'        => wl_get_entity_uri( $item ),
170
-			'url'        => get_permalink( $item ),
168
+			'uri'        => wl_get_entity_uri($item),
169
+			'url'        => get_permalink($item),
171 170
 			'label'      => $post->post_title,
172 171
 			'type'       => $post->post_type,
173
-			'thumbnails' => array( $thumbnail ),
174
-			'css_class'  => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ),
172
+			'thumbnails' => array($thumbnail),
173
+			'css_class'  => (isset($term['css_class']) ? $term['css_class'] : ''),
175 174
 		);
176 175
 
177 176
 		$item = $entity;
178 177
 	} );
179 178
 
180 179
 	// Refactor the relations.
181
-	array_walk( $data['relations'], function ( &$item ) {
180
+	array_walk($data['relations'], function(&$item) {
182 181
 		$relation = array(
183
-			's' => wl_get_entity_uri( $item[0] ),
184
-			'o' => wl_get_entity_uri( $item[1] ),
182
+			's' => wl_get_entity_uri($item[0]),
183
+			'o' => wl_get_entity_uri($item[1]),
185 184
 		);
186 185
 
187 186
 		$item = $relation;
@@ -202,34 +201,34 @@  discard block
 block discarded – undo
202 201
 	$post_id = $_REQUEST['post_id'];
203 202
 	$depth   = $_REQUEST['depth'];
204 203
 
205
-	$relations = wl_shortcode_chord_get_relations( $post_id, $depth );
206
-	$graph     = wl_shortcode_chord_get_graph( $relations );
204
+	$relations = wl_shortcode_chord_get_relations($post_id, $depth);
205
+	$graph     = wl_shortcode_chord_get_graph($relations);
207 206
 
208
-	wl_core_send_json( $graph );
207
+	wl_core_send_json($graph);
209 208
 }
210 209
 
211
-add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' );
212
-add_action( 'wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax' );
210
+add_action('wp_ajax_wl_chord', 'wl_shortcode_chord_ajax');
211
+add_action('wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax');
213 212
 
214 213
 /**
215 214
  * register_block_type for Gutenberg blocks
216 215
  */
217
-add_action( 'init', function () {
216
+add_action('init', function() {
218 217
 
219 218
 	// Bail out if the `register_block_type` function isn't available.
220
-	if ( ! function_exists( 'register_block_type' ) ) {
219
+	if ( ! function_exists('register_block_type')) {
221 220
 		return;
222 221
 	}
223 222
 
224
-	register_block_type( 'wordlift/chord', array(
223
+	register_block_type('wordlift/chord', array(
225 224
 		'editor_script'   => 'wl-block-editor',
226
-		'render_callback' => function ( $attributes ) {
225
+		'render_callback' => function($attributes) {
227 226
 			$attr_code = '';
228
-			foreach ( $attributes as $key => $value ) {
229
-				$attr_code .= $key . '="' . $value . '" ';
227
+			foreach ($attributes as $key => $value) {
228
+				$attr_code .= $key.'="'.$value.'" ';
230 229
 			}
231 230
 
232
-			return '[wl_chord ' . $attr_code . ']';
231
+			return '[wl_chord '.$attr_code.']';
233 232
 		},
234 233
 		'attributes'      => array(
235 234
 			'width'      => array(
@@ -253,5 +252,5 @@  discard block
 block discarded – undo
253 252
 				'default' => false,
254 253
 			),
255 254
 		),
256
-	) );
255
+	));
257 256
 } );
Please login to merge, or discard this patch.