@@ -15,256 +15,256 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | - * |
|
| 21 | - * @since 3.10.0 |
|
| 22 | - * @access private |
|
| 23 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | - */ |
|
| 25 | - private $configuration_service; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A {@link Wordlift_Log_Service} instance. |
|
| 29 | - * |
|
| 30 | - * @since 3.10.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | - */ |
|
| 34 | - private $log; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | - * |
|
| 39 | - * @since 3.10.0 |
|
| 40 | - * |
|
| 41 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 42 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 43 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 44 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 45 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 46 | - */ |
|
| 47 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 48 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 49 | - |
|
| 50 | - $this->configuration_service = $configuration_service; |
|
| 51 | - |
|
| 52 | - // Set a reference to the logger. |
|
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 58 | - * found while processing the post is set in the $references array. |
|
| 59 | - * |
|
| 60 | - * @since 3.10.0 |
|
| 61 | - * |
|
| 62 | - * @param int $post_id The post id. |
|
| 63 | - * @param array $references An array of entity references. |
|
| 64 | - * |
|
| 65 | - * @return array A JSON-LD array. |
|
| 66 | - */ |
|
| 67 | - public function convert( $post_id, &$references = array() ) { |
|
| 68 | - |
|
| 69 | - // Get the post instance. |
|
| 70 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 71 | - // Post not found. |
|
| 72 | - return null; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 76 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 77 | - |
|
| 78 | - // Get the entity name. |
|
| 79 | - $jsonld['headline'] = $post->post_title; |
|
| 80 | - |
|
| 81 | - // Set the published and modified dates. |
|
| 82 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 83 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 84 | - |
|
| 85 | - // Get the word count for the post. |
|
| 86 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 87 | - $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 88 | - |
|
| 89 | - // Set the publisher. |
|
| 90 | - $this->set_publisher( $jsonld ); |
|
| 91 | - |
|
| 92 | - // Process the references if any. |
|
| 93 | - if ( 0 < count( $references ) ) { |
|
| 94 | - |
|
| 95 | - // Prepare the `about` and `mentions` array. |
|
| 96 | - $about = $mentions = array(); |
|
| 97 | - |
|
| 98 | - // If the entity is in the title, then it should be an `about`. |
|
| 99 | - foreach ( $references as $reference ) { |
|
| 100 | - |
|
| 101 | - // Get the entity labels. |
|
| 102 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 103 | - |
|
| 104 | - // Get the entity URI. |
|
| 105 | - $item = array( |
|
| 106 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 107 | - ); |
|
| 108 | - |
|
| 109 | - $escaped_lables = array_map( |
|
| 110 | - function ( $value ) { |
|
| 111 | - return preg_quote( $value, '/' ); |
|
| 112 | - }, $labels |
|
| 113 | - ); |
|
| 114 | - |
|
| 115 | - // Check if the labels match any part of the title. |
|
| 116 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_lables ) . '/', $post->post_title ); |
|
| 117 | - |
|
| 118 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 119 | - if ( $matches ) { |
|
| 120 | - $about[] = $item; |
|
| 121 | - } else { |
|
| 122 | - $mentions[] = $item; |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // If we have abouts, assign them to the JSON-LD. |
|
| 127 | - if ( 0 < count( $about ) ) { |
|
| 128 | - $jsonld['about'] = $about; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // If we have mentions, assign them to the JSON-LD. |
|
| 132 | - if ( 0 < count( $mentions ) ) { |
|
| 133 | - $jsonld['mentions'] = $mentions; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // Finally set the author. |
|
| 138 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Call the `wl_post_jsonld` filter. |
|
| 142 | - * |
|
| 143 | - * @api |
|
| 144 | - * |
|
| 145 | - * @since 3.14.0 |
|
| 146 | - * |
|
| 147 | - * @param array $jsonld The JSON-LD structure. |
|
| 148 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 149 | - * @param array $references The array of referenced entities. |
|
| 150 | - */ |
|
| 151 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Get the author's JSON-LD fragment. |
|
| 156 | - * |
|
| 157 | - * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 158 | - * the referenced entity if configured for the {@link WP_User}. |
|
| 159 | - * |
|
| 160 | - * @since 3.14.0 |
|
| 161 | - * |
|
| 162 | - * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 163 | - * @param array $references An array of referenced entities. |
|
| 164 | - * |
|
| 165 | - * @return string|array A JSON-LD structure. |
|
| 166 | - */ |
|
| 167 | - private function get_author( $author_id, &$references ) { |
|
| 168 | - |
|
| 169 | - // Get the entity bound to this user. |
|
| 170 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 171 | - |
|
| 172 | - // If there's no entity bound return a simple author structure. |
|
| 173 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 174 | - |
|
| 175 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 176 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 177 | - |
|
| 178 | - return array( |
|
| 179 | - '@type' => 'Person', |
|
| 180 | - '@id' => $author_uri, |
|
| 181 | - 'name' => $author, |
|
| 182 | - ); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Add the author to the references. |
|
| 186 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 187 | - $references[] = $entity_id; |
|
| 188 | - |
|
| 189 | - // Return the JSON-LD for the referenced entity. |
|
| 190 | - return array( |
|
| 191 | - '@id' => $author_uri, |
|
| 192 | - ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Enrich the provided params array with publisher data, if available. |
|
| 197 | - * |
|
| 198 | - * @since 3.10.0 |
|
| 199 | - * |
|
| 200 | - * @param array $params The parameters array. |
|
| 201 | - */ |
|
| 202 | - protected function set_publisher( &$params ) { |
|
| 203 | - |
|
| 204 | - // If the publisher id isn't set don't do anything. |
|
| 205 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 206 | - return; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // Get the post instance. |
|
| 210 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 211 | - // Publisher not found. |
|
| 212 | - return; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // Get the item id. |
|
| 216 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 217 | - |
|
| 218 | - // Get the type. |
|
| 219 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 220 | - |
|
| 221 | - // Get the name. |
|
| 222 | - $name = $post->post_title; |
|
| 223 | - |
|
| 224 | - // Set the publisher data. |
|
| 225 | - $params['publisher'] = array( |
|
| 226 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 227 | - '@id' => $id, |
|
| 228 | - 'name' => $name, |
|
| 229 | - ); |
|
| 230 | - |
|
| 231 | - // Add the sameAs values associated with the publisher. |
|
| 232 | - $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 233 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 234 | - if ( ! empty( $sameas ) ) { |
|
| 235 | - $params['publisher']['sameAs'] = $sameas; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 239 | - // support the logo property. |
|
| 240 | - // |
|
| 241 | - // See http://schema.org/logo. |
|
| 242 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 243 | - return; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // Get the logo, WP < 4.4 way: only post ID accepted here. |
|
| 247 | - if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) { |
|
| 248 | - return; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - // Get the image URL. |
|
| 252 | - if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, 'full' ) ) { |
|
| 253 | - return; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - // Copy over some useful properties. |
|
| 257 | - // |
|
| 258 | - // See https://developers.google.com/search/docs/data-types/articles. |
|
| 259 | - $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 260 | - $params['publisher']['logo']['url'] = $attachment[0]; |
|
| 261 | - // If you specify a "width" or "height" value you should leave out |
|
| 262 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 263 | - // |
|
| 264 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 265 | - $params['publisher']['logo']['width'] = $attachment[1]; |
|
| 266 | - $params['publisher']['logo']['height'] = $attachment[2]; |
|
| 267 | - |
|
| 268 | - } |
|
| 18 | + /** |
|
| 19 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | + * |
|
| 21 | + * @since 3.10.0 |
|
| 22 | + * @access private |
|
| 23 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | + */ |
|
| 25 | + private $configuration_service; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A {@link Wordlift_Log_Service} instance. |
|
| 29 | + * |
|
| 30 | + * @since 3.10.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | + */ |
|
| 34 | + private $log; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | + * |
|
| 39 | + * @since 3.10.0 |
|
| 40 | + * |
|
| 41 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 42 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 43 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 44 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 45 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 46 | + */ |
|
| 47 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 48 | + parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 49 | + |
|
| 50 | + $this->configuration_service = $configuration_service; |
|
| 51 | + |
|
| 52 | + // Set a reference to the logger. |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 58 | + * found while processing the post is set in the $references array. |
|
| 59 | + * |
|
| 60 | + * @since 3.10.0 |
|
| 61 | + * |
|
| 62 | + * @param int $post_id The post id. |
|
| 63 | + * @param array $references An array of entity references. |
|
| 64 | + * |
|
| 65 | + * @return array A JSON-LD array. |
|
| 66 | + */ |
|
| 67 | + public function convert( $post_id, &$references = array() ) { |
|
| 68 | + |
|
| 69 | + // Get the post instance. |
|
| 70 | + if ( null === $post = get_post( $post_id ) ) { |
|
| 71 | + // Post not found. |
|
| 72 | + return null; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 76 | + $jsonld = parent::convert( $post_id, $references ); |
|
| 77 | + |
|
| 78 | + // Get the entity name. |
|
| 79 | + $jsonld['headline'] = $post->post_title; |
|
| 80 | + |
|
| 81 | + // Set the published and modified dates. |
|
| 82 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 83 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 84 | + |
|
| 85 | + // Get the word count for the post. |
|
| 86 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 87 | + $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 88 | + |
|
| 89 | + // Set the publisher. |
|
| 90 | + $this->set_publisher( $jsonld ); |
|
| 91 | + |
|
| 92 | + // Process the references if any. |
|
| 93 | + if ( 0 < count( $references ) ) { |
|
| 94 | + |
|
| 95 | + // Prepare the `about` and `mentions` array. |
|
| 96 | + $about = $mentions = array(); |
|
| 97 | + |
|
| 98 | + // If the entity is in the title, then it should be an `about`. |
|
| 99 | + foreach ( $references as $reference ) { |
|
| 100 | + |
|
| 101 | + // Get the entity labels. |
|
| 102 | + $labels = $this->entity_service->get_labels( $reference ); |
|
| 103 | + |
|
| 104 | + // Get the entity URI. |
|
| 105 | + $item = array( |
|
| 106 | + '@id' => $this->entity_service->get_uri( $reference ), |
|
| 107 | + ); |
|
| 108 | + |
|
| 109 | + $escaped_lables = array_map( |
|
| 110 | + function ( $value ) { |
|
| 111 | + return preg_quote( $value, '/' ); |
|
| 112 | + }, $labels |
|
| 113 | + ); |
|
| 114 | + |
|
| 115 | + // Check if the labels match any part of the title. |
|
| 116 | + $matches = 1 === preg_match( '/' . implode( '|', $escaped_lables ) . '/', $post->post_title ); |
|
| 117 | + |
|
| 118 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 119 | + if ( $matches ) { |
|
| 120 | + $about[] = $item; |
|
| 121 | + } else { |
|
| 122 | + $mentions[] = $item; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // If we have abouts, assign them to the JSON-LD. |
|
| 127 | + if ( 0 < count( $about ) ) { |
|
| 128 | + $jsonld['about'] = $about; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // If we have mentions, assign them to the JSON-LD. |
|
| 132 | + if ( 0 < count( $mentions ) ) { |
|
| 133 | + $jsonld['mentions'] = $mentions; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // Finally set the author. |
|
| 138 | + $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Call the `wl_post_jsonld` filter. |
|
| 142 | + * |
|
| 143 | + * @api |
|
| 144 | + * |
|
| 145 | + * @since 3.14.0 |
|
| 146 | + * |
|
| 147 | + * @param array $jsonld The JSON-LD structure. |
|
| 148 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 149 | + * @param array $references The array of referenced entities. |
|
| 150 | + */ |
|
| 151 | + return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Get the author's JSON-LD fragment. |
|
| 156 | + * |
|
| 157 | + * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 158 | + * the referenced entity if configured for the {@link WP_User}. |
|
| 159 | + * |
|
| 160 | + * @since 3.14.0 |
|
| 161 | + * |
|
| 162 | + * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 163 | + * @param array $references An array of referenced entities. |
|
| 164 | + * |
|
| 165 | + * @return string|array A JSON-LD structure. |
|
| 166 | + */ |
|
| 167 | + private function get_author( $author_id, &$references ) { |
|
| 168 | + |
|
| 169 | + // Get the entity bound to this user. |
|
| 170 | + $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 171 | + |
|
| 172 | + // If there's no entity bound return a simple author structure. |
|
| 173 | + if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 174 | + |
|
| 175 | + $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 176 | + $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 177 | + |
|
| 178 | + return array( |
|
| 179 | + '@type' => 'Person', |
|
| 180 | + '@id' => $author_uri, |
|
| 181 | + 'name' => $author, |
|
| 182 | + ); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Add the author to the references. |
|
| 186 | + $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 187 | + $references[] = $entity_id; |
|
| 188 | + |
|
| 189 | + // Return the JSON-LD for the referenced entity. |
|
| 190 | + return array( |
|
| 191 | + '@id' => $author_uri, |
|
| 192 | + ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Enrich the provided params array with publisher data, if available. |
|
| 197 | + * |
|
| 198 | + * @since 3.10.0 |
|
| 199 | + * |
|
| 200 | + * @param array $params The parameters array. |
|
| 201 | + */ |
|
| 202 | + protected function set_publisher( &$params ) { |
|
| 203 | + |
|
| 204 | + // If the publisher id isn't set don't do anything. |
|
| 205 | + if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 206 | + return; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // Get the post instance. |
|
| 210 | + if ( null === $post = get_post( $publisher_id ) ) { |
|
| 211 | + // Publisher not found. |
|
| 212 | + return; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // Get the item id. |
|
| 216 | + $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 217 | + |
|
| 218 | + // Get the type. |
|
| 219 | + $type = $this->entity_type_service->get( $publisher_id ); |
|
| 220 | + |
|
| 221 | + // Get the name. |
|
| 222 | + $name = $post->post_title; |
|
| 223 | + |
|
| 224 | + // Set the publisher data. |
|
| 225 | + $params['publisher'] = array( |
|
| 226 | + '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 227 | + '@id' => $id, |
|
| 228 | + 'name' => $name, |
|
| 229 | + ); |
|
| 230 | + |
|
| 231 | + // Add the sameAs values associated with the publisher. |
|
| 232 | + $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 233 | + $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 234 | + if ( ! empty( $sameas ) ) { |
|
| 235 | + $params['publisher']['sameAs'] = $sameas; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 239 | + // support the logo property. |
|
| 240 | + // |
|
| 241 | + // See http://schema.org/logo. |
|
| 242 | + if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 243 | + return; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // Get the logo, WP < 4.4 way: only post ID accepted here. |
|
| 247 | + if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) { |
|
| 248 | + return; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + // Get the image URL. |
|
| 252 | + if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, 'full' ) ) { |
|
| 253 | + return; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + // Copy over some useful properties. |
|
| 257 | + // |
|
| 258 | + // See https://developers.google.com/search/docs/data-types/articles. |
|
| 259 | + $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 260 | + $params['publisher']['logo']['url'] = $attachment[0]; |
|
| 261 | + // If you specify a "width" or "height" value you should leave out |
|
| 262 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 263 | + // |
|
| 264 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 265 | + $params['publisher']['logo']['width'] = $attachment[1]; |
|
| 266 | + $params['publisher']['logo']['height'] = $attachment[2]; |
|
| 267 | + |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | 270 | } |
@@ -44,13 +44,13 @@ discard block |
||
| 44 | 44 | * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
| 45 | 45 | * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
| 46 | 46 | */ |
| 47 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 48 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 47 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service) { |
|
| 48 | + parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service); |
|
| 49 | 49 | |
| 50 | 50 | $this->configuration_service = $configuration_service; |
| 51 | 51 | |
| 52 | 52 | // Set a reference to the logger. |
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter'); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
@@ -64,59 +64,59 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return array A JSON-LD array. |
| 66 | 66 | */ |
| 67 | - public function convert( $post_id, &$references = array() ) { |
|
| 67 | + public function convert($post_id, &$references = array()) { |
|
| 68 | 68 | |
| 69 | 69 | // Get the post instance. |
| 70 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 70 | + if (null === $post = get_post($post_id)) { |
|
| 71 | 71 | // Post not found. |
| 72 | 72 | return null; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 76 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 76 | + $jsonld = parent::convert($post_id, $references); |
|
| 77 | 77 | |
| 78 | 78 | // Get the entity name. |
| 79 | 79 | $jsonld['headline'] = $post->post_title; |
| 80 | 80 | |
| 81 | 81 | // Set the published and modified dates. |
| 82 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 83 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 82 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 83 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 84 | 84 | |
| 85 | 85 | // Get the word count for the post. |
| 86 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 86 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 87 | 87 | $jsonld['wordCount'] = $post_adapter->word_count(); |
| 88 | 88 | |
| 89 | 89 | // Set the publisher. |
| 90 | - $this->set_publisher( $jsonld ); |
|
| 90 | + $this->set_publisher($jsonld); |
|
| 91 | 91 | |
| 92 | 92 | // Process the references if any. |
| 93 | - if ( 0 < count( $references ) ) { |
|
| 93 | + if (0 < count($references)) { |
|
| 94 | 94 | |
| 95 | 95 | // Prepare the `about` and `mentions` array. |
| 96 | 96 | $about = $mentions = array(); |
| 97 | 97 | |
| 98 | 98 | // If the entity is in the title, then it should be an `about`. |
| 99 | - foreach ( $references as $reference ) { |
|
| 99 | + foreach ($references as $reference) { |
|
| 100 | 100 | |
| 101 | 101 | // Get the entity labels. |
| 102 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 102 | + $labels = $this->entity_service->get_labels($reference); |
|
| 103 | 103 | |
| 104 | 104 | // Get the entity URI. |
| 105 | 105 | $item = array( |
| 106 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 106 | + '@id' => $this->entity_service->get_uri($reference), |
|
| 107 | 107 | ); |
| 108 | 108 | |
| 109 | 109 | $escaped_lables = array_map( |
| 110 | - function ( $value ) { |
|
| 111 | - return preg_quote( $value, '/' ); |
|
| 110 | + function($value) { |
|
| 111 | + return preg_quote($value, '/'); |
|
| 112 | 112 | }, $labels |
| 113 | 113 | ); |
| 114 | 114 | |
| 115 | 115 | // Check if the labels match any part of the title. |
| 116 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_lables ) . '/', $post->post_title ); |
|
| 116 | + $matches = 1 === preg_match('/'.implode('|', $escaped_lables).'/', $post->post_title); |
|
| 117 | 117 | |
| 118 | 118 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 119 | - if ( $matches ) { |
|
| 119 | + if ($matches) { |
|
| 120 | 120 | $about[] = $item; |
| 121 | 121 | } else { |
| 122 | 122 | $mentions[] = $item; |
@@ -124,18 +124,18 @@ discard block |
||
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | // If we have abouts, assign them to the JSON-LD. |
| 127 | - if ( 0 < count( $about ) ) { |
|
| 127 | + if (0 < count($about)) { |
|
| 128 | 128 | $jsonld['about'] = $about; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // If we have mentions, assign them to the JSON-LD. |
| 132 | - if ( 0 < count( $mentions ) ) { |
|
| 132 | + if (0 < count($mentions)) { |
|
| 133 | 133 | $jsonld['mentions'] = $mentions; |
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // Finally set the author. |
| 138 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 138 | + $jsonld['author'] = $this->get_author($post->post_author, $references); |
|
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * Call the `wl_post_jsonld` filter. |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | * @param int $post_id The {@link WP_Post} `id`. |
| 149 | 149 | * @param array $references The array of referenced entities. |
| 150 | 150 | */ |
| 151 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 151 | + return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -164,16 +164,16 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @return string|array A JSON-LD structure. |
| 166 | 166 | */ |
| 167 | - private function get_author( $author_id, &$references ) { |
|
| 167 | + private function get_author($author_id, &$references) { |
|
| 168 | 168 | |
| 169 | 169 | // Get the entity bound to this user. |
| 170 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 170 | + $entity_id = $this->user_service->get_entity($author_id); |
|
| 171 | 171 | |
| 172 | 172 | // If there's no entity bound return a simple author structure. |
| 173 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 173 | + if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) { |
|
| 174 | 174 | |
| 175 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 176 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 175 | + $author = get_the_author_meta('display_name', $author_id); |
|
| 176 | + $author_uri = $this->user_service->get_uri($author_id); |
|
| 177 | 177 | |
| 178 | 178 | return array( |
| 179 | 179 | '@type' => 'Person', |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // Add the author to the references. |
| 186 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 186 | + $author_uri = $this->entity_service->get_uri($entity_id); |
|
| 187 | 187 | $references[] = $entity_id; |
| 188 | 188 | |
| 189 | 189 | // Return the JSON-LD for the referenced entity. |
@@ -199,39 +199,39 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @param array $params The parameters array. |
| 201 | 201 | */ |
| 202 | - protected function set_publisher( &$params ) { |
|
| 202 | + protected function set_publisher(&$params) { |
|
| 203 | 203 | |
| 204 | 204 | // If the publisher id isn't set don't do anything. |
| 205 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 205 | + if (null === $publisher_id = $this->configuration_service->get_publisher_id()) { |
|
| 206 | 206 | return; |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | // Get the post instance. |
| 210 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 210 | + if (null === $post = get_post($publisher_id)) { |
|
| 211 | 211 | // Publisher not found. |
| 212 | 212 | return; |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | // Get the item id. |
| 216 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 216 | + $id = $this->entity_service->get_uri($publisher_id); |
|
| 217 | 217 | |
| 218 | 218 | // Get the type. |
| 219 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 219 | + $type = $this->entity_type_service->get($publisher_id); |
|
| 220 | 220 | |
| 221 | 221 | // Get the name. |
| 222 | 222 | $name = $post->post_title; |
| 223 | 223 | |
| 224 | 224 | // Set the publisher data. |
| 225 | 225 | $params['publisher'] = array( |
| 226 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 226 | + '@type' => $this->relative_to_context($type['uri']), |
|
| 227 | 227 | '@id' => $id, |
| 228 | 228 | 'name' => $name, |
| 229 | 229 | ); |
| 230 | 230 | |
| 231 | 231 | // Add the sameAs values associated with the publisher. |
| 232 | 232 | $storage_factory = Wordlift_Storage_Factory::get_instance(); |
| 233 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 234 | - if ( ! empty( $sameas ) ) { |
|
| 233 | + $sameas = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id); |
|
| 234 | + if ( ! empty($sameas)) { |
|
| 235 | 235 | $params['publisher']['sameAs'] = $sameas; |
| 236 | 236 | } |
| 237 | 237 | |
@@ -239,17 +239,17 @@ discard block |
||
| 239 | 239 | // support the logo property. |
| 240 | 240 | // |
| 241 | 241 | // See http://schema.org/logo. |
| 242 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 242 | + if ('http://schema.org/Organization' !== $type['uri']) { |
|
| 243 | 243 | return; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | // Get the logo, WP < 4.4 way: only post ID accepted here. |
| 247 | - if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) { |
|
| 247 | + if ('' === $thumbnail_id = get_post_thumbnail_id($post->ID)) { |
|
| 248 | 248 | return; |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | // Get the image URL. |
| 252 | - if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, 'full' ) ) { |
|
| 252 | + if (false === $attachment = wp_get_attachment_image_src($thumbnail_id, 'full')) { |
|
| 253 | 253 | return; |
| 254 | 254 | } |
| 255 | 255 | |