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