Completed
Pull Request — develop (#1576)
by Naveen
01:12
created
src/includes/class-wordlift-abstract-post-to-jsonld-converter.php 2 patches
Indentation   +393 added lines, -393 removed lines patch added patch discarded remove patch
@@ -23,419 +23,419 @@
 block discarded – undo
23 23
  */
24 24
 abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter {
25 25
 
26
-	/**
27
-	 * The JSON-LD context.
28
-	 *
29
-	 * @since 3.10.0
30
-	 */
31
-	const CONTEXT = 'http://schema.org';
32
-
33
-	/**
34
-	 * A {@link Wordlift_Entity_Type_Service} instance.
35
-	 *
36
-	 * @since  3.10.0
37
-	 * @access protected
38
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
39
-	 */
40
-	protected $entity_type_service;
41
-
42
-	/**
43
-	 * A {@link Wordlift_User_Service} instance.
44
-	 *
45
-	 * @since  3.10.0
46
-	 * @access private
47
-	 * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
48
-	 */
49
-	protected $user_service;
50
-
51
-	/**
52
-	 * A {@link Wordlift_Attachment_Service} instance.
53
-	 *
54
-	 * @since  3.10.0
55
-	 * @access private
56
-	 * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
57
-	 */
58
-	protected $attachment_service;
59
-
60
-	/**
61
-	 * @var Wordlift_Property_Getter
62
-	 */
63
-	private $property_getter;
64
-
65
-	/**
66
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
67
-	 *
68
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_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
-	 * @param \Wordlift_Property_Getter     $property_getter
72
-	 *
73
-	 * @since 3.10.0
74
-	 */
75
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) {
76
-		$this->entity_type_service = $entity_type_service;
77
-		$this->user_service        = $user_service;
78
-		$this->attachment_service  = $attachment_service;
79
-		$this->property_getter     = $property_getter;
80
-	}
81
-
82
-	/**
83
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
84
-	 * found while processing the post is set in the $references array.
85
-	 *
86
-	 * @param int   $post_id The post id.
87
-	 * @param array $references An array of entity references.
88
-	 * @param array $references_infos
89
-	 *
90
-	 * @return array A JSON-LD array.
91
-	 * @since 3.10.0
92
-	 */
93
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
94
-	public function convert( $post_id, &$references = array(), &$references_infos = 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 = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
105
-		if ( $id === null ) {
106
-			$id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri();
107
-		}
108
-
109
-		/*
26
+    /**
27
+     * The JSON-LD context.
28
+     *
29
+     * @since 3.10.0
30
+     */
31
+    const CONTEXT = 'http://schema.org';
32
+
33
+    /**
34
+     * A {@link Wordlift_Entity_Type_Service} instance.
35
+     *
36
+     * @since  3.10.0
37
+     * @access protected
38
+     * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
39
+     */
40
+    protected $entity_type_service;
41
+
42
+    /**
43
+     * A {@link Wordlift_User_Service} instance.
44
+     *
45
+     * @since  3.10.0
46
+     * @access private
47
+     * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
48
+     */
49
+    protected $user_service;
50
+
51
+    /**
52
+     * A {@link Wordlift_Attachment_Service} instance.
53
+     *
54
+     * @since  3.10.0
55
+     * @access private
56
+     * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
57
+     */
58
+    protected $attachment_service;
59
+
60
+    /**
61
+     * @var Wordlift_Property_Getter
62
+     */
63
+    private $property_getter;
64
+
65
+    /**
66
+     * Wordlift_Post_To_Jsonld_Converter constructor.
67
+     *
68
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_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
+     * @param \Wordlift_Property_Getter     $property_getter
72
+     *
73
+     * @since 3.10.0
74
+     */
75
+    public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) {
76
+        $this->entity_type_service = $entity_type_service;
77
+        $this->user_service        = $user_service;
78
+        $this->attachment_service  = $attachment_service;
79
+        $this->property_getter     = $property_getter;
80
+    }
81
+
82
+    /**
83
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
84
+     * found while processing the post is set in the $references array.
85
+     *
86
+     * @param int   $post_id The post id.
87
+     * @param array $references An array of entity references.
88
+     * @param array $references_infos
89
+     *
90
+     * @return array A JSON-LD array.
91
+     * @since 3.10.0
92
+     */
93
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
94
+    public function convert( $post_id, &$references = array(), &$references_infos = 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 = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
105
+        if ( $id === null ) {
106
+            $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri();
107
+        }
108
+
109
+        /*
110 110
 		 * The `types` variable holds one or more entity types. The `type` variable isn't used anymore.
111 111
 		 *
112 112
 		 * @since 3.20.0 We support more than one entity type.
113 113
 		 *
114 114
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
115 115
 		 */
116
-		// Get the entity @type. We consider `post` BlogPostings.
117
-		// $type = $this->entity_type_service->get( $post_id );
118
-		$types = $this->entity_type_service->get_names( $post_id );
119
-		$type  = self::make_one( $types );
120
-
121
-		// Prepare the response.
122
-		$jsonld = array(
123
-			'@context'    => self::CONTEXT,
124
-			'@id'         => $id,
125
-			'@type'       => $type,
126
-			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
127
-		);
128
-
129
-		// Set the `mainEntityOfPage` property if the post has some contents.
130
-		/*
116
+        // Get the entity @type. We consider `post` BlogPostings.
117
+        // $type = $this->entity_type_service->get( $post_id );
118
+        $types = $this->entity_type_service->get_names( $post_id );
119
+        $type  = self::make_one( $types );
120
+
121
+        // Prepare the response.
122
+        $jsonld = array(
123
+            '@context'    => self::CONTEXT,
124
+            '@id'         => $id,
125
+            '@type'       => $type,
126
+            'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
127
+        );
128
+
129
+        // Set the `mainEntityOfPage` property if the post has some contents.
130
+        /*
131 131
 		 * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g.
132 132
 		 * because the content is written elsewhere.
133 133
 		 *
134 134
 		 * @since 3.20.0
135 135
 		 */
136
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
137
-		if ( ! empty( $post_content ) ) {
138
-			// We're setting the `mainEntityOfPage` to signal which one is the
139
-			// main entity for the specified URL. It might be however that the
140
-			// post/page is actually about another specific entity. How WL deals
141
-			// with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
142
-			//
143
-			// See http://schema.org/mainEntityOfPage
144
-			//
145
-			// No need to specify `'@type' => 'WebPage'.
146
-			//
147
-			// See https://github.com/insideout10/wordlift-plugin/issues/451.
148
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
149
-
150
-			/**
151
-			 * @see https://github.com/insideout10/wordlift-plugin/issues/1207
152
-			 *
153
-			 * @since 3.27.7
154
-			 */
155
-			if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) {
156
-				$jsonld['mainEntityOfPage'] = array(
157
-					'@type'        => 'WebPage',
158
-					'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ),
159
-				);
160
-			}
161
-		};
162
-
163
-		$this->set_images( $this->attachment_service, $post, $jsonld );
164
-
165
-		// Get the entities referenced by this post and set it to the `references`
166
-		// array so that the caller can do further processing, such as printing out
167
-		// more of those references.
168
-		$references_without_locations = Object_Relation_Service::get_instance()
169
-															   ->get_references( $post_id, Object_Type_Enum::POST );
170
-
171
-		/*
136
+        $post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
137
+        if ( ! empty( $post_content ) ) {
138
+            // We're setting the `mainEntityOfPage` to signal which one is the
139
+            // main entity for the specified URL. It might be however that the
140
+            // post/page is actually about another specific entity. How WL deals
141
+            // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
142
+            //
143
+            // See http://schema.org/mainEntityOfPage
144
+            //
145
+            // No need to specify `'@type' => 'WebPage'.
146
+            //
147
+            // See https://github.com/insideout10/wordlift-plugin/issues/451.
148
+            $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
149
+
150
+            /**
151
+             * @see https://github.com/insideout10/wordlift-plugin/issues/1207
152
+             *
153
+             * @since 3.27.7
154
+             */
155
+            if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) {
156
+                $jsonld['mainEntityOfPage'] = array(
157
+                    '@type'        => 'WebPage',
158
+                    'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ),
159
+                );
160
+            }
161
+        };
162
+
163
+        $this->set_images( $this->attachment_service, $post, $jsonld );
164
+
165
+        // Get the entities referenced by this post and set it to the `references`
166
+        // array so that the caller can do further processing, such as printing out
167
+        // more of those references.
168
+        $references_without_locations = Object_Relation_Service::get_instance()
169
+                                                                ->get_references( $post_id, Object_Type_Enum::POST );
170
+
171
+        /*
172 172
 		 * Add the locations to the references.
173 173
 		 *
174 174
 		 * @since 3.19.5
175 175
 		 *
176 176
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/858.
177 177
 		 */
178
-		// A reference to use in closure.
179
-		$entity_type_service = $this->entity_type_service;
180
-		$locations           = array_reduce(
181
-			$references_without_locations,
182
-			function ( $carry, $reference ) use ( $entity_type_service ) {
183
-				/**
184
-				 * @var $reference Reference
185
-				 */
186
-				// @see https://schema.org/location for the schema.org types using the `location` property.
187
-				if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' )
188
-				 && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' )
189
-				 && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) {
190
-
191
-					return $carry;
192
-				}
193
-				$post_location_ids        = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION );
194
-				$post_location_references = array_map(
195
-					function ( $post_id ) {
196
-						return new Post_Reference( $post_id );
197
-					},
198
-					$post_location_ids
199
-				);
200
-
201
-				return array_merge( $carry, $post_location_references );
202
-			},
203
-			array()
204
-		);
205
-
206
-		// Merge the references with the referenced locations if any.
207
-		// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
208
-		$references = array_merge( $references_without_locations, $locations );
209
-
210
-		return $jsonld;
211
-	}
212
-
213
-	/**
214
-	 * If the provided value starts with the schema.org context, we remove the schema.org
215
-	 * part since it is set with the '@context'.
216
-	 *
217
-	 * @param string $value The property value.
218
-	 *
219
-	 * @return string The property value without the context.
220
-	 * @since 3.10.0
221
-	 */
222
-	public function relative_to_context( $value ) {
223
-
224
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
225
-	}
226
-
227
-	/**
228
-	 * Set the images, by looking for embedded images, for images loaded via the
229
-	 * gallery and for the featured image.
230
-	 *
231
-	 * Uses the cache service to store the results of this function for a day.
232
-	 *
233
-	 * @param $attachment_service Wordlift_Attachment_Service
234
-	 * @param WP_Post                                        $post The target {@link WP_Post}.
235
-	 * @param array                                          $jsonld The JSON-LD array.
236
-	 *
237
-	 * @since 3.10.0
238
-	 */
239
-	public static function set_images( $attachment_service, $post, &$jsonld ) {
240
-
241
-		// Prepare the attachment ids array.
242
-		$ids = array();
243
-
244
-		// Set the thumbnail id as first attachment id, if any.
245
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
246
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
247
-			$ids[] = $thumbnail_id;
248
-		}
249
-
250
-		// For the time being the following is being removed since the query
251
-		// initiated by `get_image_embeds` is consuming lots of CPU.
252
-		//
253
-		// See https://github.com/insideout10/wordlift-plugin/issues/689.
254
-		//
255
-		// Get the embeds, removing existing ids.
256
-		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
257
-		if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) {
258
-			$embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids );
259
-		} else {
260
-			$embeds = array();
261
-		}
262
-
263
-		// Get the gallery, removing existing ids.
264
-		$gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds );
265
-
266
-		// Map the attachment ids to images' data structured for schema.org use.
267
-		$images_with_sizes = array_filter(
268
-			array_reduce(
269
-				array_merge( $ids, $embeds, $gallery ),
270
-				function ( $carry, $item ) {
271
-					/*
178
+        // A reference to use in closure.
179
+        $entity_type_service = $this->entity_type_service;
180
+        $locations           = array_reduce(
181
+            $references_without_locations,
182
+            function ( $carry, $reference ) use ( $entity_type_service ) {
183
+                /**
184
+                 * @var $reference Reference
185
+                 */
186
+                // @see https://schema.org/location for the schema.org types using the `location` property.
187
+                if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' )
188
+                 && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' )
189
+                 && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) {
190
+
191
+                    return $carry;
192
+                }
193
+                $post_location_ids        = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION );
194
+                $post_location_references = array_map(
195
+                    function ( $post_id ) {
196
+                        return new Post_Reference( $post_id );
197
+                    },
198
+                    $post_location_ids
199
+                );
200
+
201
+                return array_merge( $carry, $post_location_references );
202
+            },
203
+            array()
204
+        );
205
+
206
+        // Merge the references with the referenced locations if any.
207
+        // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
208
+        $references = array_merge( $references_without_locations, $locations );
209
+
210
+        return $jsonld;
211
+    }
212
+
213
+    /**
214
+     * If the provided value starts with the schema.org context, we remove the schema.org
215
+     * part since it is set with the '@context'.
216
+     *
217
+     * @param string $value The property value.
218
+     *
219
+     * @return string The property value without the context.
220
+     * @since 3.10.0
221
+     */
222
+    public function relative_to_context( $value ) {
223
+
224
+        return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
225
+    }
226
+
227
+    /**
228
+     * Set the images, by looking for embedded images, for images loaded via the
229
+     * gallery and for the featured image.
230
+     *
231
+     * Uses the cache service to store the results of this function for a day.
232
+     *
233
+     * @param $attachment_service Wordlift_Attachment_Service
234
+     * @param WP_Post                                        $post The target {@link WP_Post}.
235
+     * @param array                                          $jsonld The JSON-LD array.
236
+     *
237
+     * @since 3.10.0
238
+     */
239
+    public static function set_images( $attachment_service, $post, &$jsonld ) {
240
+
241
+        // Prepare the attachment ids array.
242
+        $ids = array();
243
+
244
+        // Set the thumbnail id as first attachment id, if any.
245
+        $thumbnail_id = get_post_thumbnail_id( $post->ID );
246
+        if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
247
+            $ids[] = $thumbnail_id;
248
+        }
249
+
250
+        // For the time being the following is being removed since the query
251
+        // initiated by `get_image_embeds` is consuming lots of CPU.
252
+        //
253
+        // See https://github.com/insideout10/wordlift-plugin/issues/689.
254
+        //
255
+        // Get the embeds, removing existing ids.
256
+        // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
257
+        if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) {
258
+            $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids );
259
+        } else {
260
+            $embeds = array();
261
+        }
262
+
263
+        // Get the gallery, removing existing ids.
264
+        $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds );
265
+
266
+        // Map the attachment ids to images' data structured for schema.org use.
267
+        $images_with_sizes = array_filter(
268
+            array_reduce(
269
+                array_merge( $ids, $embeds, $gallery ),
270
+                function ( $carry, $item ) {
271
+                    /*
272 272
 					* @todo: we're not sure that we're getting attachment data here, we
273 273
 					* should filter `false`s.
274 274
 					*/
275 275
 
276
-					$sources = array_merge(
277
-						Wordlift_Image_Service::get_sources( $item ),
278
-						array( wp_get_attachment_image_src( $item, 'full' ) )
279
-					);
280
-
281
-					$sources_with_image = array_filter(
282
-						$sources,
283
-						function ( $source ) {
284
-							return ! empty( $source[0] );
285
-						}
286
-					);
287
-
288
-					// Get the attachment data.
289
-					// $attachment = wp_get_attachment_image_src( $item, 'full' );
290
-
291
-					// var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) );
292
-
293
-					// Bail if image is not found.
294
-					// In some cases, you can delete the image from the database
295
-					// or from uploads dir, but the image id still exists as featured image
296
-					// or in [gallery] shortcode.
297
-					// if ( empty( $attachment[0] ) ) {
298
-					if ( empty( $sources_with_image ) ) {
299
-						return $carry;
300
-					}
301
-
302
-					// Merge the arrays.
303
-					return array_merge(
304
-						$carry,
305
-						$sources_with_image
306
-					);
307
-				},
308
-				// Initial array.
309
-
310
-				array()
311
-			)
312
-		);
313
-
314
-		// Refactor data as per schema.org specifications.
315
-		$images = array_map(
316
-			function ( $attachment ) {
317
-				return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
318
-					array(
319
-						'@type' => 'ImageObject',
320
-						'url'   => $attachment[0],
321
-					),
322
-					$attachment
323
-				);
324
-			},
325
-			$images_with_sizes
326
-		);
327
-
328
-		// Add images if present.
329
-		if ( 0 < count( $images ) ) {
330
-			$jsonld['image'] = $images;
331
-		}
332
-
333
-	}
334
-
335
-	/**
336
-	 * If the provided array of values contains only one value, then one single
337
-	 * value is returned, otherwise the original array is returned.
338
-	 *
339
-	 * @param array $value An array of values.
340
-	 *
341
-	 * @return mixed|array A single value or the original array.
342
-	 * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to
343
-	 *  {@link Wordlift_Abstract_Post_To_Jsonld_Converter}.
344
-	 * @since  3.8.0
345
-	 * @access private
346
-	 */
347
-	protected static function make_one( $value ) {
348
-
349
-		return 1 === count( $value ) ? $value[0] : $value;
350
-	}
351
-
352
-	/**
353
-	 * Process the provided array by adding the width / height if the values
354
-	 * are available and are greater than 0.
355
-	 *
356
-	 * @param array $image The `ImageObject` array.
357
-	 * @param array $attachment The attachment array.
358
-	 *
359
-	 * @return array The enriched `ImageObject` array.
360
-	 * @since 3.14.0
361
-	 */
362
-	public static function set_image_size( $image, $attachment ) {
363
-
364
-		// If you specify a "width" or "height" value you should leave out
365
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
366
-		//
367
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
368
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
369
-			$image['width'] = $attachment[1];
370
-		}
371
-
372
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
373
-			$image['height'] = $attachment[2];
374
-		}
375
-
376
-		return $image;
377
-	}
378
-
379
-	/**
380
-	 * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property
381
-	 * for the post entity type.
382
-	 *
383
-	 * @param array   $jsonld The JSON-LD array.
384
-	 * @param array   $fields The entity types field array.
385
-	 * @param WP_Post $post The target {@link WP_Post} instance.
386
-	 * @param array   $references The references array.
387
-	 *
388
-	 * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
389
-	 *  the {@link Wordlift_Schema_Service} class.
390
-	 */
391
-	protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) {
392
-
393
-		// Set a reference to use in closures.
394
-		$converter = $this;
395
-
396
-		// Try each field on the entity.
397
-		foreach ( $fields as $key => $value ) {
398
-
399
-			// Get the predicate.
400
-			$name = $this->relative_to_context( $value['predicate'] );
401
-
402
-			// Get the value, the property service will get the right extractor
403
-			// for that property.
404
-			$value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST );
405
-
406
-			if ( empty( $value ) ) {
407
-				continue;
408
-			}
409
-
410
-			// Map the value to the property name.
411
-			// If we got an array with just one value, we return that one value.
412
-			// If we got a Wordlift_Property_Entity_Reference we get the URL.
413
-			$jsonld[ $name ] = self::make_one(
414
-				array_map(
415
-					function ( $item ) use ( $converter, &$references, &$references_infos ) {
416
-
417
-						if ( $item instanceof Wordlift_Property_Entity_Reference ) {
418
-
419
-							  $url = $item->get_url();
420
-
421
-							  // The refactored converters require the entity id.
422
-							// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
423
-							  $references[] = $item->to_reference();
424
-
425
-							// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
426
-							  $references_infos[] = array( 'reference' => $item );
427
-
428
-							  return array( '@id' => $url, );
429
-						}
430
-
431
-						return $converter->relative_to_context( $item );
432
-					},
433
-					$value
434
-				)
435
-			);
436
-
437
-		}
438
-
439
-	}
276
+                    $sources = array_merge(
277
+                        Wordlift_Image_Service::get_sources( $item ),
278
+                        array( wp_get_attachment_image_src( $item, 'full' ) )
279
+                    );
280
+
281
+                    $sources_with_image = array_filter(
282
+                        $sources,
283
+                        function ( $source ) {
284
+                            return ! empty( $source[0] );
285
+                        }
286
+                    );
287
+
288
+                    // Get the attachment data.
289
+                    // $attachment = wp_get_attachment_image_src( $item, 'full' );
290
+
291
+                    // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) );
292
+
293
+                    // Bail if image is not found.
294
+                    // In some cases, you can delete the image from the database
295
+                    // or from uploads dir, but the image id still exists as featured image
296
+                    // or in [gallery] shortcode.
297
+                    // if ( empty( $attachment[0] ) ) {
298
+                    if ( empty( $sources_with_image ) ) {
299
+                        return $carry;
300
+                    }
301
+
302
+                    // Merge the arrays.
303
+                    return array_merge(
304
+                        $carry,
305
+                        $sources_with_image
306
+                    );
307
+                },
308
+                // Initial array.
309
+
310
+                array()
311
+            )
312
+        );
313
+
314
+        // Refactor data as per schema.org specifications.
315
+        $images = array_map(
316
+            function ( $attachment ) {
317
+                return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
318
+                    array(
319
+                        '@type' => 'ImageObject',
320
+                        'url'   => $attachment[0],
321
+                    ),
322
+                    $attachment
323
+                );
324
+            },
325
+            $images_with_sizes
326
+        );
327
+
328
+        // Add images if present.
329
+        if ( 0 < count( $images ) ) {
330
+            $jsonld['image'] = $images;
331
+        }
332
+
333
+    }
334
+
335
+    /**
336
+     * If the provided array of values contains only one value, then one single
337
+     * value is returned, otherwise the original array is returned.
338
+     *
339
+     * @param array $value An array of values.
340
+     *
341
+     * @return mixed|array A single value or the original array.
342
+     * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to
343
+     *  {@link Wordlift_Abstract_Post_To_Jsonld_Converter}.
344
+     * @since  3.8.0
345
+     * @access private
346
+     */
347
+    protected static function make_one( $value ) {
348
+
349
+        return 1 === count( $value ) ? $value[0] : $value;
350
+    }
351
+
352
+    /**
353
+     * Process the provided array by adding the width / height if the values
354
+     * are available and are greater than 0.
355
+     *
356
+     * @param array $image The `ImageObject` array.
357
+     * @param array $attachment The attachment array.
358
+     *
359
+     * @return array The enriched `ImageObject` array.
360
+     * @since 3.14.0
361
+     */
362
+    public static function set_image_size( $image, $attachment ) {
363
+
364
+        // If you specify a "width" or "height" value you should leave out
365
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
366
+        //
367
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
368
+        if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
369
+            $image['width'] = $attachment[1];
370
+        }
371
+
372
+        if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
373
+            $image['height'] = $attachment[2];
374
+        }
375
+
376
+        return $image;
377
+    }
378
+
379
+    /**
380
+     * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property
381
+     * for the post entity type.
382
+     *
383
+     * @param array   $jsonld The JSON-LD array.
384
+     * @param array   $fields The entity types field array.
385
+     * @param WP_Post $post The target {@link WP_Post} instance.
386
+     * @param array   $references The references array.
387
+     *
388
+     * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
389
+     *  the {@link Wordlift_Schema_Service} class.
390
+     */
391
+    protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) {
392
+
393
+        // Set a reference to use in closures.
394
+        $converter = $this;
395
+
396
+        // Try each field on the entity.
397
+        foreach ( $fields as $key => $value ) {
398
+
399
+            // Get the predicate.
400
+            $name = $this->relative_to_context( $value['predicate'] );
401
+
402
+            // Get the value, the property service will get the right extractor
403
+            // for that property.
404
+            $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST );
405
+
406
+            if ( empty( $value ) ) {
407
+                continue;
408
+            }
409
+
410
+            // Map the value to the property name.
411
+            // If we got an array with just one value, we return that one value.
412
+            // If we got a Wordlift_Property_Entity_Reference we get the URL.
413
+            $jsonld[ $name ] = self::make_one(
414
+                array_map(
415
+                    function ( $item ) use ( $converter, &$references, &$references_infos ) {
416
+
417
+                        if ( $item instanceof Wordlift_Property_Entity_Reference ) {
418
+
419
+                                $url = $item->get_url();
420
+
421
+                                // The refactored converters require the entity id.
422
+                            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
423
+                                $references[] = $item->to_reference();
424
+
425
+                            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
426
+                                $references_infos[] = array( 'reference' => $item );
427
+
428
+                                return array( '@id' => $url, );
429
+                        }
430
+
431
+                        return $converter->relative_to_context( $item );
432
+                    },
433
+                    $value
434
+                )
435
+            );
436
+
437
+        }
438
+
439
+    }
440 440
 
441 441
 }
Please login to merge, or discard this patch.
Spacing   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 	 *
73 73
 	 * @since 3.10.0
74 74
 	 */
75
-	public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) {
75
+	public function __construct($entity_type_service, $user_service, $attachment_service, $property_getter) {
76 76
 		$this->entity_type_service = $entity_type_service;
77 77
 		$this->user_service        = $user_service;
78 78
 		$this->attachment_service  = $attachment_service;
@@ -91,19 +91,19 @@  discard block
 block discarded – undo
91 91
 	 * @since 3.10.0
92 92
 	 */
93 93
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
94
-	public function convert( $post_id, &$references = array(), &$references_infos = array() ) {
94
+	public function convert($post_id, &$references = array(), &$references_infos = 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 = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
105
-		if ( $id === null ) {
106
-			$id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri();
104
+		$id = Wordlift_Entity_Service::get_instance()->get_uri($post->ID);
105
+		if ($id === null) {
106
+			$id = 'get_uri returned null, dataset is '.wl_configuration_get_redlink_dataset_uri();
107 107
 		}
108 108
 
109 109
 		/*
@@ -115,15 +115,15 @@  discard block
 block discarded – undo
115 115
 		 */
116 116
 		// Get the entity @type. We consider `post` BlogPostings.
117 117
 		// $type = $this->entity_type_service->get( $post_id );
118
-		$types = $this->entity_type_service->get_names( $post_id );
119
-		$type  = self::make_one( $types );
118
+		$types = $this->entity_type_service->get_names($post_id);
119
+		$type  = self::make_one($types);
120 120
 
121 121
 		// Prepare the response.
122 122
 		$jsonld = array(
123 123
 			'@context'    => self::CONTEXT,
124 124
 			'@id'         => $id,
125 125
 			'@type'       => $type,
126
-			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ),
126
+			'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt($post),
127 127
 		);
128 128
 
129 129
 		// Set the `mainEntityOfPage` property if the post has some contents.
@@ -133,8 +133,8 @@  discard block
 block discarded – undo
133 133
 		 *
134 134
 		 * @since 3.20.0
135 135
 		 */
136
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
137
-		if ( ! empty( $post_content ) ) {
136
+		$post_content = apply_filters('wl_post_content', $post->post_content, $post);
137
+		if ( ! empty($post_content)) {
138 138
 			// We're setting the `mainEntityOfPage` to signal which one is the
139 139
 			// main entity for the specified URL. It might be however that the
140 140
 			// post/page is actually about another specific entity. How WL deals
@@ -145,28 +145,28 @@  discard block
 block discarded – undo
145 145
 			// No need to specify `'@type' => 'WebPage'.
146 146
 			//
147 147
 			// See https://github.com/insideout10/wordlift-plugin/issues/451.
148
-			$jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID );
148
+			$jsonld['mainEntityOfPage'] = get_the_permalink($post->ID);
149 149
 
150 150
 			/**
151 151
 			 * @see https://github.com/insideout10/wordlift-plugin/issues/1207
152 152
 			 *
153 153
 			 * @since 3.27.7
154 154
 			 */
155
-			if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) {
155
+			if (in_array($type, array('Occupation', 'OccupationAggregationByEmployer'), true)) {
156 156
 				$jsonld['mainEntityOfPage'] = array(
157 157
 					'@type'        => 'WebPage',
158
-					'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ),
158
+					'lastReviewed' => get_post_time('Y-m-d\TH:i:sP', true, $post, false),
159 159
 				);
160 160
 			}
161 161
 		};
162 162
 
163
-		$this->set_images( $this->attachment_service, $post, $jsonld );
163
+		$this->set_images($this->attachment_service, $post, $jsonld);
164 164
 
165 165
 		// Get the entities referenced by this post and set it to the `references`
166 166
 		// array so that the caller can do further processing, such as printing out
167 167
 		// more of those references.
168 168
 		$references_without_locations = Object_Relation_Service::get_instance()
169
-															   ->get_references( $post_id, Object_Type_Enum::POST );
169
+															   ->get_references($post_id, Object_Type_Enum::POST);
170 170
 
171 171
 		/*
172 172
 		 * Add the locations to the references.
@@ -179,33 +179,33 @@  discard block
 block discarded – undo
179 179
 		$entity_type_service = $this->entity_type_service;
180 180
 		$locations           = array_reduce(
181 181
 			$references_without_locations,
182
-			function ( $carry, $reference ) use ( $entity_type_service ) {
182
+			function($carry, $reference) use ($entity_type_service) {
183 183
 				/**
184 184
 				 * @var $reference Reference
185 185
 				 */
186 186
 				// @see https://schema.org/location for the schema.org types using the `location` property.
187
-				if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' )
188
-				 && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' )
189
-				 && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) {
187
+				if ( ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Action')
188
+				 && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Event')
189
+				 && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Organization')) {
190 190
 
191 191
 					return $carry;
192 192
 				}
193
-				$post_location_ids        = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION );
193
+				$post_location_ids        = get_post_meta($reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION);
194 194
 				$post_location_references = array_map(
195
-					function ( $post_id ) {
196
-						return new Post_Reference( $post_id );
195
+					function($post_id) {
196
+						return new Post_Reference($post_id);
197 197
 					},
198 198
 					$post_location_ids
199 199
 				);
200 200
 
201
-				return array_merge( $carry, $post_location_references );
201
+				return array_merge($carry, $post_location_references);
202 202
 			},
203 203
 			array()
204 204
 		);
205 205
 
206 206
 		// Merge the references with the referenced locations if any.
207 207
 		// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
208
-		$references = array_merge( $references_without_locations, $locations );
208
+		$references = array_merge($references_without_locations, $locations);
209 209
 
210 210
 		return $jsonld;
211 211
 	}
@@ -219,9 +219,9 @@  discard block
 block discarded – undo
219 219
 	 * @return string The property value without the context.
220 220
 	 * @since 3.10.0
221 221
 	 */
222
-	public function relative_to_context( $value ) {
222
+	public function relative_to_context($value) {
223 223
 
224
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
224
+		return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value;
225 225
 	}
226 226
 
227 227
 	/**
@@ -236,14 +236,14 @@  discard block
 block discarded – undo
236 236
 	 *
237 237
 	 * @since 3.10.0
238 238
 	 */
239
-	public static function set_images( $attachment_service, $post, &$jsonld ) {
239
+	public static function set_images($attachment_service, $post, &$jsonld) {
240 240
 
241 241
 		// Prepare the attachment ids array.
242 242
 		$ids = array();
243 243
 
244 244
 		// Set the thumbnail id as first attachment id, if any.
245
-		$thumbnail_id = get_post_thumbnail_id( $post->ID );
246
-		if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
245
+		$thumbnail_id = get_post_thumbnail_id($post->ID);
246
+		if ('' !== $thumbnail_id && 0 !== $thumbnail_id) {
247 247
 			$ids[] = $thumbnail_id;
248 248
 		}
249 249
 
@@ -254,34 +254,34 @@  discard block
 block discarded – undo
254 254
 		//
255 255
 		// Get the embeds, removing existing ids.
256 256
 		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
257
-		if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) {
258
-			$embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids );
257
+		if (apply_filters('wl_feature__enable__image-embeds', false)) {
258
+			$embeds = array_diff($attachment_service->get_image_embeds($post->post_content), $ids);
259 259
 		} else {
260 260
 			$embeds = array();
261 261
 		}
262 262
 
263 263
 		// Get the gallery, removing existing ids.
264
-		$gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds );
264
+		$gallery = array_diff($attachment_service->get_gallery($post), $ids, $embeds);
265 265
 
266 266
 		// Map the attachment ids to images' data structured for schema.org use.
267 267
 		$images_with_sizes = array_filter(
268 268
 			array_reduce(
269
-				array_merge( $ids, $embeds, $gallery ),
270
-				function ( $carry, $item ) {
269
+				array_merge($ids, $embeds, $gallery),
270
+				function($carry, $item) {
271 271
 					/*
272 272
 					* @todo: we're not sure that we're getting attachment data here, we
273 273
 					* should filter `false`s.
274 274
 					*/
275 275
 
276 276
 					$sources = array_merge(
277
-						Wordlift_Image_Service::get_sources( $item ),
278
-						array( wp_get_attachment_image_src( $item, 'full' ) )
277
+						Wordlift_Image_Service::get_sources($item),
278
+						array(wp_get_attachment_image_src($item, 'full'))
279 279
 					);
280 280
 
281 281
 					$sources_with_image = array_filter(
282 282
 						$sources,
283
-						function ( $source ) {
284
-							return ! empty( $source[0] );
283
+						function($source) {
284
+							return ! empty($source[0]);
285 285
 						}
286 286
 					);
287 287
 
@@ -295,7 +295,7 @@  discard block
 block discarded – undo
295 295
 					// or from uploads dir, but the image id still exists as featured image
296 296
 					// or in [gallery] shortcode.
297 297
 					// if ( empty( $attachment[0] ) ) {
298
-					if ( empty( $sources_with_image ) ) {
298
+					if (empty($sources_with_image)) {
299 299
 						return $carry;
300 300
 					}
301 301
 
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
 
314 314
 		// Refactor data as per schema.org specifications.
315 315
 		$images = array_map(
316
-			function ( $attachment ) {
316
+			function($attachment) {
317 317
 				return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size(
318 318
 					array(
319 319
 						'@type' => 'ImageObject',
@@ -326,7 +326,7 @@  discard block
 block discarded – undo
326 326
 		);
327 327
 
328 328
 		// Add images if present.
329
-		if ( 0 < count( $images ) ) {
329
+		if (0 < count($images)) {
330 330
 			$jsonld['image'] = $images;
331 331
 		}
332 332
 
@@ -344,9 +344,9 @@  discard block
 block discarded – undo
344 344
 	 * @since  3.8.0
345 345
 	 * @access private
346 346
 	 */
347
-	protected static function make_one( $value ) {
347
+	protected static function make_one($value) {
348 348
 
349
-		return 1 === count( $value ) ? $value[0] : $value;
349
+		return 1 === count($value) ? $value[0] : $value;
350 350
 	}
351 351
 
352 352
 	/**
@@ -359,17 +359,17 @@  discard block
 block discarded – undo
359 359
 	 * @return array The enriched `ImageObject` array.
360 360
 	 * @since 3.14.0
361 361
 	 */
362
-	public static function set_image_size( $image, $attachment ) {
362
+	public static function set_image_size($image, $attachment) {
363 363
 
364 364
 		// If you specify a "width" or "height" value you should leave out
365 365
 		// 'px'. For example: "width":"4608px" should be "width":"4608".
366 366
 		//
367 367
 		// See https://github.com/insideout10/wordlift-plugin/issues/451.
368
-		if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) {
368
+		if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) {
369 369
 			$image['width'] = $attachment[1];
370 370
 		}
371 371
 
372
-		if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) {
372
+		if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) {
373 373
 			$image['height'] = $attachment[2];
374 374
 		}
375 375
 
@@ -388,33 +388,33 @@  discard block
 block discarded – undo
388 388
 	 * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
389 389
 	 *  the {@link Wordlift_Schema_Service} class.
390 390
 	 */
391
-	protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) {
391
+	protected function process_type_custom_fields(&$jsonld, $fields, $post, &$references, &$references_infos) {
392 392
 
393 393
 		// Set a reference to use in closures.
394 394
 		$converter = $this;
395 395
 
396 396
 		// Try each field on the entity.
397
-		foreach ( $fields as $key => $value ) {
397
+		foreach ($fields as $key => $value) {
398 398
 
399 399
 			// Get the predicate.
400
-			$name = $this->relative_to_context( $value['predicate'] );
400
+			$name = $this->relative_to_context($value['predicate']);
401 401
 
402 402
 			// Get the value, the property service will get the right extractor
403 403
 			// for that property.
404
-			$value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST );
404
+			$value = $this->property_getter->get($post->ID, $key, Object_Type_Enum::POST);
405 405
 
406
-			if ( empty( $value ) ) {
406
+			if (empty($value)) {
407 407
 				continue;
408 408
 			}
409 409
 
410 410
 			// Map the value to the property name.
411 411
 			// If we got an array with just one value, we return that one value.
412 412
 			// If we got a Wordlift_Property_Entity_Reference we get the URL.
413
-			$jsonld[ $name ] = self::make_one(
413
+			$jsonld[$name] = self::make_one(
414 414
 				array_map(
415
-					function ( $item ) use ( $converter, &$references, &$references_infos ) {
415
+					function($item) use ($converter, &$references, &$references_infos) {
416 416
 
417
-						if ( $item instanceof Wordlift_Property_Entity_Reference ) {
417
+						if ($item instanceof Wordlift_Property_Entity_Reference) {
418 418
 
419 419
 							  $url = $item->get_url();
420 420
 
@@ -423,12 +423,12 @@  discard block
 block discarded – undo
423 423
 							  $references[] = $item->to_reference();
424 424
 
425 425
 							// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
426
-							  $references_infos[] = array( 'reference' => $item );
426
+							  $references_infos[] = array('reference' => $item);
427 427
 
428
-							  return array( '@id' => $url, );
428
+							  return array('@id' => $url,);
429 429
 						}
430 430
 
431
-						return $converter->relative_to_context( $item );
431
+						return $converter->relative_to_context($item);
432 432
 					},
433 433
 					$value
434 434
 				)
Please login to merge, or discard this patch.
src/includes/properties/class-wordlift-property-entity-reference.php 2 patches
Indentation   +113 added lines, -113 removed lines patch added patch discarded remove patch
@@ -20,118 +20,118 @@
 block discarded – undo
20 20
  */
21 21
 class Wordlift_Property_Entity_Reference {
22 22
 
23
-	/**
24
-	 * The URL associated with this entity reference.
25
-	 *
26
-	 * @since  3.8.0
27
-	 * @access private
28
-	 * @var string $url The URL associated with the entity reference.
29
-	 */
30
-	private $url;
31
-
32
-	/**
33
-	 * The entity post id.
34
-	 *
35
-	 * @since  3.10.0
36
-	 * @access private
37
-	 * @var int $id The entity post id.
38
-	 */
39
-	private $id;
40
-
41
-	private $required;
42
-	/**
43
-	 * @var int
44
-	 */
45
-	private $type;
46
-
47
-	/**
48
-	 * Create a Wordlift_Property_Entity_Reference instance with the provided URL.
49
-	 *
50
-	 * @param string $url The URL.
51
-	 * @param int    $id The entity post id.
52
-	 * @param bool   $required Whether this property is always required in SD output, default false.
53
-	 * @param int    $type Instance of Object_Enum_Type
54
-	 *
55
-	 * @since 3.8.0
56
-	 */
57
-	public function __construct( $url, $id, $required = false, $type = Object_Type_Enum::POST ) {
58
-
59
-		$this->url      = $url;
60
-		$this->id       = $id;
61
-		$this->required = $required;
62
-		$this->type     = $type;
63
-
64
-	}
65
-
66
-	/**
67
-	 * Return the type of the reference, one of the values in Object_Type_Enum
68
-	 * @return int
69
-	 * @since 3.38.0
70
-	 */
71
-	public function get_type() {
72
-		return $this->type;
73
-	}
74
-
75
-	/**
76
-	 * Get the URL associated with this entity reference.
77
-	 *
78
-	 * @return string The URL associated with the entity reference.
79
-	 * @since 3.8.0
80
-	 */
81
-	public function get_url() {
82
-
83
-		return $this->url;
84
-	}
85
-
86
-	/**
87
-	 * Get the entity id.
88
-	 *
89
-	 * @return int The entity id.
90
-	 * @since 3.10.0
91
-	 */
92
-	public function get_id() {
93
-
94
-		return $this->id;
95
-	}
96
-
97
-	/**
98
-	 * Get the required flag for this {@link Wordlift_Property_Entity_Reference}.
99
-	 *
100
-	 * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this
101
-	 * property needs to be output to SD (JSON-LD).
102
-	 *
103
-	 * @since 3.27.1
104
-	 */
105
-	public function get_required() {
106
-
107
-		return $this->required;
108
-
109
-	}
110
-
111
-	/**
112
-	 * Set the required flag for this {@link Wordlift_Property_Entity_Reference}.
113
-	 *
114
-	 * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this
115
-	 * property needs to be output to SD (JSON-LD).
116
-	 *
117
-	 * @param bool $value
118
-	 *
119
-	 * @since 3.27.1
120
-	 */
121
-	public function set_required( $value ) {
122
-
123
-		$this->required = $value;
124
-
125
-	}
126
-
127
-	public function to_reference() {
128
-
129
-		if ( Object_Type_Enum::POST === $this->type ) {
130
-			return new Post_Reference( $this->id );
131
-		}
132
-		if ( Object_Type_Enum::TERM === $this->type ) {
133
-			return new Term_Reference( $this->id );
134
-		}
135
-	}
23
+    /**
24
+     * The URL associated with this entity reference.
25
+     *
26
+     * @since  3.8.0
27
+     * @access private
28
+     * @var string $url The URL associated with the entity reference.
29
+     */
30
+    private $url;
31
+
32
+    /**
33
+     * The entity post id.
34
+     *
35
+     * @since  3.10.0
36
+     * @access private
37
+     * @var int $id The entity post id.
38
+     */
39
+    private $id;
40
+
41
+    private $required;
42
+    /**
43
+     * @var int
44
+     */
45
+    private $type;
46
+
47
+    /**
48
+     * Create a Wordlift_Property_Entity_Reference instance with the provided URL.
49
+     *
50
+     * @param string $url The URL.
51
+     * @param int    $id The entity post id.
52
+     * @param bool   $required Whether this property is always required in SD output, default false.
53
+     * @param int    $type Instance of Object_Enum_Type
54
+     *
55
+     * @since 3.8.0
56
+     */
57
+    public function __construct( $url, $id, $required = false, $type = Object_Type_Enum::POST ) {
58
+
59
+        $this->url      = $url;
60
+        $this->id       = $id;
61
+        $this->required = $required;
62
+        $this->type     = $type;
63
+
64
+    }
65
+
66
+    /**
67
+     * Return the type of the reference, one of the values in Object_Type_Enum
68
+     * @return int
69
+     * @since 3.38.0
70
+     */
71
+    public function get_type() {
72
+        return $this->type;
73
+    }
74
+
75
+    /**
76
+     * Get the URL associated with this entity reference.
77
+     *
78
+     * @return string The URL associated with the entity reference.
79
+     * @since 3.8.0
80
+     */
81
+    public function get_url() {
82
+
83
+        return $this->url;
84
+    }
85
+
86
+    /**
87
+     * Get the entity id.
88
+     *
89
+     * @return int The entity id.
90
+     * @since 3.10.0
91
+     */
92
+    public function get_id() {
93
+
94
+        return $this->id;
95
+    }
96
+
97
+    /**
98
+     * Get the required flag for this {@link Wordlift_Property_Entity_Reference}.
99
+     *
100
+     * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this
101
+     * property needs to be output to SD (JSON-LD).
102
+     *
103
+     * @since 3.27.1
104
+     */
105
+    public function get_required() {
106
+
107
+        return $this->required;
108
+
109
+    }
110
+
111
+    /**
112
+     * Set the required flag for this {@link Wordlift_Property_Entity_Reference}.
113
+     *
114
+     * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this
115
+     * property needs to be output to SD (JSON-LD).
116
+     *
117
+     * @param bool $value
118
+     *
119
+     * @since 3.27.1
120
+     */
121
+    public function set_required( $value ) {
122
+
123
+        $this->required = $value;
124
+
125
+    }
126
+
127
+    public function to_reference() {
128
+
129
+        if ( Object_Type_Enum::POST === $this->type ) {
130
+            return new Post_Reference( $this->id );
131
+        }
132
+        if ( Object_Type_Enum::TERM === $this->type ) {
133
+            return new Term_Reference( $this->id );
134
+        }
135
+    }
136 136
 
137 137
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
 	 *
55 55
 	 * @since 3.8.0
56 56
 	 */
57
-	public function __construct( $url, $id, $required = false, $type = Object_Type_Enum::POST ) {
57
+	public function __construct($url, $id, $required = false, $type = Object_Type_Enum::POST) {
58 58
 
59 59
 		$this->url      = $url;
60 60
 		$this->id       = $id;
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 	 *
119 119
 	 * @since 3.27.1
120 120
 	 */
121
-	public function set_required( $value ) {
121
+	public function set_required($value) {
122 122
 
123 123
 		$this->required = $value;
124 124
 
@@ -126,11 +126,11 @@  discard block
 block discarded – undo
126 126
 
127 127
 	public function to_reference() {
128 128
 
129
-		if ( Object_Type_Enum::POST === $this->type ) {
130
-			return new Post_Reference( $this->id );
129
+		if (Object_Type_Enum::POST === $this->type) {
130
+			return new Post_Reference($this->id);
131 131
 		}
132
-		if ( Object_Type_Enum::TERM === $this->type ) {
133
-			return new Term_Reference( $this->id );
132
+		if (Object_Type_Enum::TERM === $this->type) {
133
+			return new Term_Reference($this->id);
134 134
 		}
135 135
 	}
136 136
 
Please login to merge, or discard this patch.
src/includes/properties/class-wordlift-entity-property-service.php 2 patches
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -12,37 +12,37 @@
 block discarded – undo
12 12
  */
13 13
 class Wordlift_Entity_Property_Service extends Wordlift_Simple_Property_Service {
14 14
 
15
-	/**
16
-	 * {@inheritdoc}
17
-	 */
18
-	public function get( $id, $meta_key, $type ) {
19
-
20
-		$entity_service = Wordlift_Entity_Service::get_instance();
21
-
22
-		// Map each returned value to a Wordlift_Property_Entity_Reference.
23
-		return array_map(
24
-			function ( $item ) use ( $entity_service ) {
25
-
26
-				$regex = '/^(post|term)_(\d+)$/m';
27
-				if ( preg_match( $regex, $item, $matches ) ) {
28
-					$object_type = Object_Type_Enum::from_string( $matches[1] );
29
-					$identifier  = (int) $matches[2];
30
-					if ( Object_Type_Enum::POST === $object_type ) {
31
-						return new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ), $identifier );
32
-					} elseif ( Object_Type_Enum::TERM === $object_type ) {
33
-						return new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item, Object_Type_Enum::TERM ), $identifier, false, Object_Type_Enum::TERM );
34
-					}
35
-				}
36
-
37
-				// If the $item is a number and it's an existing post, return the
38
-				// URI of the referenced entity. Otherwise return the value.
39
-				return is_numeric( $item ) && null !== get_post( $item )
40
-				? new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ), (int) $item )
41
-				: $item;
42
-			},
43
-			parent::get( $id, $meta_key, $type )
44
-		);
45
-	}
15
+    /**
16
+     * {@inheritdoc}
17
+     */
18
+    public function get( $id, $meta_key, $type ) {
19
+
20
+        $entity_service = Wordlift_Entity_Service::get_instance();
21
+
22
+        // Map each returned value to a Wordlift_Property_Entity_Reference.
23
+        return array_map(
24
+            function ( $item ) use ( $entity_service ) {
25
+
26
+                $regex = '/^(post|term)_(\d+)$/m';
27
+                if ( preg_match( $regex, $item, $matches ) ) {
28
+                    $object_type = Object_Type_Enum::from_string( $matches[1] );
29
+                    $identifier  = (int) $matches[2];
30
+                    if ( Object_Type_Enum::POST === $object_type ) {
31
+                        return new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ), $identifier );
32
+                    } elseif ( Object_Type_Enum::TERM === $object_type ) {
33
+                        return new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item, Object_Type_Enum::TERM ), $identifier, false, Object_Type_Enum::TERM );
34
+                    }
35
+                }
36
+
37
+                // If the $item is a number and it's an existing post, return the
38
+                // URI of the referenced entity. Otherwise return the value.
39
+                return is_numeric( $item ) && null !== get_post( $item )
40
+                ? new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ), (int) $item )
41
+                : $item;
42
+            },
43
+            parent::get( $id, $meta_key, $type )
44
+        );
45
+    }
46 46
 
47 47
 
48 48
 
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -15,32 +15,32 @@
 block discarded – undo
15 15
 	/**
16 16
 	 * {@inheritdoc}
17 17
 	 */
18
-	public function get( $id, $meta_key, $type ) {
18
+	public function get($id, $meta_key, $type) {
19 19
 
20 20
 		$entity_service = Wordlift_Entity_Service::get_instance();
21 21
 
22 22
 		// Map each returned value to a Wordlift_Property_Entity_Reference.
23 23
 		return array_map(
24
-			function ( $item ) use ( $entity_service ) {
24
+			function($item) use ($entity_service) {
25 25
 
26 26
 				$regex = '/^(post|term)_(\d+)$/m';
27
-				if ( preg_match( $regex, $item, $matches ) ) {
28
-					$object_type = Object_Type_Enum::from_string( $matches[1] );
27
+				if (preg_match($regex, $item, $matches)) {
28
+					$object_type = Object_Type_Enum::from_string($matches[1]);
29 29
 					$identifier  = (int) $matches[2];
30
-					if ( Object_Type_Enum::POST === $object_type ) {
31
-						return new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ), $identifier );
32
-					} elseif ( Object_Type_Enum::TERM === $object_type ) {
33
-						return new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item, Object_Type_Enum::TERM ), $identifier, false, Object_Type_Enum::TERM );
30
+					if (Object_Type_Enum::POST === $object_type) {
31
+						return new Wordlift_Property_Entity_Reference($entity_service->get_uri($item), $identifier);
32
+					} elseif (Object_Type_Enum::TERM === $object_type) {
33
+						return new Wordlift_Property_Entity_Reference($entity_service->get_uri($item, Object_Type_Enum::TERM), $identifier, false, Object_Type_Enum::TERM);
34 34
 					}
35 35
 				}
36 36
 
37 37
 				// If the $item is a number and it's an existing post, return the
38 38
 				// URI of the referenced entity. Otherwise return the value.
39
-				return is_numeric( $item ) && null !== get_post( $item )
40
-				? new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ), (int) $item )
39
+				return is_numeric($item) && null !== get_post($item)
40
+				? new Wordlift_Property_Entity_Reference($entity_service->get_uri($item), (int) $item)
41 41
 				: $item;
42 42
 			},
43
-			parent::get( $id, $meta_key, $type )
43
+			parent::get($id, $meta_key, $type)
44 44
 		);
45 45
 	}
46 46
 
Please login to merge, or discard this patch.
src/wordlift/vocabulary-terms/jsonld/class-jsonld-generator.php 2 patches
Indentation   +147 added lines, -147 removed lines patch added patch discarded remove patch
@@ -13,152 +13,152 @@
 block discarded – undo
13 13
 
14 14
 class Jsonld_Generator {
15 15
 
16
-	/**
17
-	 * @var \Wordlift_Entity_Type_Service
18
-	 */
19
-	private $entity_type_service;
20
-	/**
21
-	 * @var \Wordlift_Property_Getter
22
-	 */
23
-	private $property_getter;
24
-	/**
25
-	 * @var Type_Service
26
-	 */
27
-	private $term_entity_type_service;
28
-	/**
29
-	 * @var \Wordlift_Entity_Service
30
-	 */
31
-	private $entity_service;
32
-
33
-	public function __construct( $entity_type_service, $property_getter ) {
34
-		$this->entity_type_service      = $entity_type_service;
35
-		$this->property_getter          = $property_getter;
36
-		$this->term_entity_type_service = Type_Service::get_instance();
37
-		$this->entity_service           = \Wordlift_Entity_Service::get_instance();
38
-	}
39
-
40
-	public function init() {
41
-		add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 );
42
-	}
43
-
44
-	public function wl_term_jsonld_array( $data, $term_id ) {
45
-		$jsonld     = $data['jsonld'];
46
-		$references = $data['references'];
47
-
48
-		$term_jsonld_data = $this->get_jsonld_data_for_term( $term_id );
49
-
50
-		// Return early if we dont have the entity data
51
-		// for the term.
52
-		if ( ! $term_jsonld_data ) {
53
-			return $data;
54
-		}
55
-
56
-		$term_jsonld = $term_jsonld_data['jsonld'];
57
-
58
-		$references = array_merge( $references, $term_jsonld_data['references'] );
59
-
60
-		array_unshift( $jsonld, $term_jsonld );
61
-
62
-		return array(
63
-			'jsonld'     => $jsonld,
64
-			'references' => $references,
65
-		);
66
-	}
67
-
68
-	private function get_jsonld_data_for_term( $term_id ) {
69
-
70
-		$id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM );
71
-
72
-		// If we don't have a dataset  URI, then don't publish the term data
73
-		// on this page.
74
-		if ( ! $id ) {
75
-			return false;
76
-		}
77
-
78
-		$references = array();
79
-		$term       = get_term( $term_id );
80
-		$permalink  = get_term_link( $term );
81
-
82
-		$custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id );
83
-		$term          = get_term( $term_id );
84
-		$jsonld        = array(
85
-			'@context'    => 'http://schema.org',
86
-			'name'        => $term->name,
87
-			'@type'       => $this->term_entity_type_service->get_entity_types_labels( $term_id ),
88
-			'@id'         => $id,
89
-			'description' => $term->description,
90
-		);
91
-
92
-		if ( ! $custom_fields || ! is_array( $custom_fields ) ) {
93
-			return $jsonld;
94
-		}
95
-
96
-		foreach ( $custom_fields as $key => $value ) {
97
-			$name  = $this->relative_to_schema_context( $value['predicate'] );
98
-			$value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM );
99
-			$value = $this->process_value( $value, $references );
100
-			if ( ! isset( $value ) ||
101
-				 is_array( $value ) && empty( $value ) ||
102
-				 is_string( $value ) && empty( $value ) ) {
103
-				continue;
104
-			}
105
-			$jsonld[ $name ] = $value;
106
-
107
-		}
108
-
109
-		if ( $permalink ) {
110
-			$jsonld['mainEntityOfPage'] = $permalink;
111
-		}
112
-
113
-		return apply_filters(
114
-			'wl_no_vocabulary_term_jsonld_array',
115
-			array(
116
-				'jsonld'     => $jsonld,
117
-				'references' => $references,
118
-			),
119
-			$term_id
120
-		);
121
-
122
-	}
123
-
124
-	private function relative_to_schema_context( $predicate ) {
125
-		return str_replace( 'http://schema.org/', '', $predicate );
126
-	}
127
-
128
-	private function process_value( $value, &$references ) {
129
-
130
-		if ( is_array( $value )
131
-			 && count( $value ) > 0
132
-			 && $value[0] instanceof \Wordlift_Property_Entity_Reference ) {
133
-
134
-			// All of the references from the custom fields are post references.
135
-			$references = array_merge(
136
-				$references,
137
-				array_map(
138
-					function ( $property_entity_reference ) {
139
-						/**
140
-						 * @var $property_entity_reference \Wordlift_Property_Entity_Reference
141
-						 */
142
-						return $property_entity_reference->to_reference();
143
-					},
144
-					$value
145
-				)
146
-			);
147
-
148
-
149
-			return array_map(
150
-				function ( $reference ) {
151
-					/**
152
-					 * @var $reference \Wordlift_Property_Entity_Reference
153
-					 */
154
-					return array( '@id' => $reference->get_url() );
155
-				},
156
-				$value
157
-			);
158
-
159
-		}
160
-
161
-		return $value;
162
-	}
16
+    /**
17
+     * @var \Wordlift_Entity_Type_Service
18
+     */
19
+    private $entity_type_service;
20
+    /**
21
+     * @var \Wordlift_Property_Getter
22
+     */
23
+    private $property_getter;
24
+    /**
25
+     * @var Type_Service
26
+     */
27
+    private $term_entity_type_service;
28
+    /**
29
+     * @var \Wordlift_Entity_Service
30
+     */
31
+    private $entity_service;
32
+
33
+    public function __construct( $entity_type_service, $property_getter ) {
34
+        $this->entity_type_service      = $entity_type_service;
35
+        $this->property_getter          = $property_getter;
36
+        $this->term_entity_type_service = Type_Service::get_instance();
37
+        $this->entity_service           = \Wordlift_Entity_Service::get_instance();
38
+    }
39
+
40
+    public function init() {
41
+        add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 );
42
+    }
43
+
44
+    public function wl_term_jsonld_array( $data, $term_id ) {
45
+        $jsonld     = $data['jsonld'];
46
+        $references = $data['references'];
47
+
48
+        $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id );
49
+
50
+        // Return early if we dont have the entity data
51
+        // for the term.
52
+        if ( ! $term_jsonld_data ) {
53
+            return $data;
54
+        }
55
+
56
+        $term_jsonld = $term_jsonld_data['jsonld'];
57
+
58
+        $references = array_merge( $references, $term_jsonld_data['references'] );
59
+
60
+        array_unshift( $jsonld, $term_jsonld );
61
+
62
+        return array(
63
+            'jsonld'     => $jsonld,
64
+            'references' => $references,
65
+        );
66
+    }
67
+
68
+    private function get_jsonld_data_for_term( $term_id ) {
69
+
70
+        $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM );
71
+
72
+        // If we don't have a dataset  URI, then don't publish the term data
73
+        // on this page.
74
+        if ( ! $id ) {
75
+            return false;
76
+        }
77
+
78
+        $references = array();
79
+        $term       = get_term( $term_id );
80
+        $permalink  = get_term_link( $term );
81
+
82
+        $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id );
83
+        $term          = get_term( $term_id );
84
+        $jsonld        = array(
85
+            '@context'    => 'http://schema.org',
86
+            'name'        => $term->name,
87
+            '@type'       => $this->term_entity_type_service->get_entity_types_labels( $term_id ),
88
+            '@id'         => $id,
89
+            'description' => $term->description,
90
+        );
91
+
92
+        if ( ! $custom_fields || ! is_array( $custom_fields ) ) {
93
+            return $jsonld;
94
+        }
95
+
96
+        foreach ( $custom_fields as $key => $value ) {
97
+            $name  = $this->relative_to_schema_context( $value['predicate'] );
98
+            $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM );
99
+            $value = $this->process_value( $value, $references );
100
+            if ( ! isset( $value ) ||
101
+                 is_array( $value ) && empty( $value ) ||
102
+                 is_string( $value ) && empty( $value ) ) {
103
+                continue;
104
+            }
105
+            $jsonld[ $name ] = $value;
106
+
107
+        }
108
+
109
+        if ( $permalink ) {
110
+            $jsonld['mainEntityOfPage'] = $permalink;
111
+        }
112
+
113
+        return apply_filters(
114
+            'wl_no_vocabulary_term_jsonld_array',
115
+            array(
116
+                'jsonld'     => $jsonld,
117
+                'references' => $references,
118
+            ),
119
+            $term_id
120
+        );
121
+
122
+    }
123
+
124
+    private function relative_to_schema_context( $predicate ) {
125
+        return str_replace( 'http://schema.org/', '', $predicate );
126
+    }
127
+
128
+    private function process_value( $value, &$references ) {
129
+
130
+        if ( is_array( $value )
131
+             && count( $value ) > 0
132
+             && $value[0] instanceof \Wordlift_Property_Entity_Reference ) {
133
+
134
+            // All of the references from the custom fields are post references.
135
+            $references = array_merge(
136
+                $references,
137
+                array_map(
138
+                    function ( $property_entity_reference ) {
139
+                        /**
140
+                         * @var $property_entity_reference \Wordlift_Property_Entity_Reference
141
+                         */
142
+                        return $property_entity_reference->to_reference();
143
+                    },
144
+                    $value
145
+                )
146
+            );
147
+
148
+
149
+            return array_map(
150
+                function ( $reference ) {
151
+                    /**
152
+                     * @var $reference \Wordlift_Property_Entity_Reference
153
+                     */
154
+                    return array( '@id' => $reference->get_url() );
155
+                },
156
+                $value
157
+            );
158
+
159
+        }
160
+
161
+        return $value;
162
+    }
163 163
 
164 164
 }
Please login to merge, or discard this patch.
Spacing   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
 	 */
31 31
 	private $entity_service;
32 32
 
33
-	public function __construct( $entity_type_service, $property_getter ) {
33
+	public function __construct($entity_type_service, $property_getter) {
34 34
 		$this->entity_type_service      = $entity_type_service;
35 35
 		$this->property_getter          = $property_getter;
36 36
 		$this->term_entity_type_service = Type_Service::get_instance();
@@ -38,26 +38,26 @@  discard block
 block discarded – undo
38 38
 	}
39 39
 
40 40
 	public function init() {
41
-		add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 );
41
+		add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2);
42 42
 	}
43 43
 
44
-	public function wl_term_jsonld_array( $data, $term_id ) {
44
+	public function wl_term_jsonld_array($data, $term_id) {
45 45
 		$jsonld     = $data['jsonld'];
46 46
 		$references = $data['references'];
47 47
 
48
-		$term_jsonld_data = $this->get_jsonld_data_for_term( $term_id );
48
+		$term_jsonld_data = $this->get_jsonld_data_for_term($term_id);
49 49
 
50 50
 		// Return early if we dont have the entity data
51 51
 		// for the term.
52
-		if ( ! $term_jsonld_data ) {
52
+		if ( ! $term_jsonld_data) {
53 53
 			return $data;
54 54
 		}
55 55
 
56 56
 		$term_jsonld = $term_jsonld_data['jsonld'];
57 57
 
58
-		$references = array_merge( $references, $term_jsonld_data['references'] );
58
+		$references = array_merge($references, $term_jsonld_data['references']);
59 59
 
60
-		array_unshift( $jsonld, $term_jsonld );
60
+		array_unshift($jsonld, $term_jsonld);
61 61
 
62 62
 		return array(
63 63
 			'jsonld'     => $jsonld,
@@ -65,48 +65,48 @@  discard block
 block discarded – undo
65 65
 		);
66 66
 	}
67 67
 
68
-	private function get_jsonld_data_for_term( $term_id ) {
68
+	private function get_jsonld_data_for_term($term_id) {
69 69
 
70
-		$id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM );
70
+		$id = $this->entity_service->get_uri($term_id, Object_Type_Enum::TERM);
71 71
 
72 72
 		// If we don't have a dataset  URI, then don't publish the term data
73 73
 		// on this page.
74
-		if ( ! $id ) {
74
+		if ( ! $id) {
75 75
 			return false;
76 76
 		}
77 77
 
78 78
 		$references = array();
79
-		$term       = get_term( $term_id );
80
-		$permalink  = get_term_link( $term );
79
+		$term       = get_term($term_id);
80
+		$permalink  = get_term_link($term);
81 81
 
82
-		$custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id );
83
-		$term          = get_term( $term_id );
82
+		$custom_fields = $this->entity_type_service->get_custom_fields_for_term($term_id);
83
+		$term          = get_term($term_id);
84 84
 		$jsonld        = array(
85 85
 			'@context'    => 'http://schema.org',
86 86
 			'name'        => $term->name,
87
-			'@type'       => $this->term_entity_type_service->get_entity_types_labels( $term_id ),
87
+			'@type'       => $this->term_entity_type_service->get_entity_types_labels($term_id),
88 88
 			'@id'         => $id,
89 89
 			'description' => $term->description,
90 90
 		);
91 91
 
92
-		if ( ! $custom_fields || ! is_array( $custom_fields ) ) {
92
+		if ( ! $custom_fields || ! is_array($custom_fields)) {
93 93
 			return $jsonld;
94 94
 		}
95 95
 
96
-		foreach ( $custom_fields as $key => $value ) {
97
-			$name  = $this->relative_to_schema_context( $value['predicate'] );
98
-			$value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM );
99
-			$value = $this->process_value( $value, $references );
100
-			if ( ! isset( $value ) ||
101
-				 is_array( $value ) && empty( $value ) ||
102
-				 is_string( $value ) && empty( $value ) ) {
96
+		foreach ($custom_fields as $key => $value) {
97
+			$name  = $this->relative_to_schema_context($value['predicate']);
98
+			$value = $this->property_getter->get($term_id, $key, Object_Type_Enum::TERM);
99
+			$value = $this->process_value($value, $references);
100
+			if ( ! isset($value) ||
101
+				 is_array($value) && empty($value) ||
102
+				 is_string($value) && empty($value)) {
103 103
 				continue;
104 104
 			}
105
-			$jsonld[ $name ] = $value;
105
+			$jsonld[$name] = $value;
106 106
 
107 107
 		}
108 108
 
109
-		if ( $permalink ) {
109
+		if ($permalink) {
110 110
 			$jsonld['mainEntityOfPage'] = $permalink;
111 111
 		}
112 112
 
@@ -121,21 +121,21 @@  discard block
 block discarded – undo
121 121
 
122 122
 	}
123 123
 
124
-	private function relative_to_schema_context( $predicate ) {
125
-		return str_replace( 'http://schema.org/', '', $predicate );
124
+	private function relative_to_schema_context($predicate) {
125
+		return str_replace('http://schema.org/', '', $predicate);
126 126
 	}
127 127
 
128
-	private function process_value( $value, &$references ) {
128
+	private function process_value($value, &$references) {
129 129
 
130
-		if ( is_array( $value )
131
-			 && count( $value ) > 0
132
-			 && $value[0] instanceof \Wordlift_Property_Entity_Reference ) {
130
+		if (is_array($value)
131
+			 && count($value) > 0
132
+			 && $value[0] instanceof \Wordlift_Property_Entity_Reference) {
133 133
 
134 134
 			// All of the references from the custom fields are post references.
135 135
 			$references = array_merge(
136 136
 				$references,
137 137
 				array_map(
138
-					function ( $property_entity_reference ) {
138
+					function($property_entity_reference) {
139 139
 						/**
140 140
 						 * @var $property_entity_reference \Wordlift_Property_Entity_Reference
141 141
 						 */
@@ -147,11 +147,11 @@  discard block
 block discarded – undo
147 147
 
148 148
 
149 149
 			return array_map(
150
-				function ( $reference ) {
150
+				function($reference) {
151 151
 					/**
152 152
 					 * @var $reference \Wordlift_Property_Entity_Reference
153 153
 					 */
154
-					return array( '@id' => $reference->get_url() );
154
+					return array('@id' => $reference->get_url());
155 155
 				},
156 156
 				$value
157 157
 			);
Please login to merge, or discard this patch.