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