@@ -15,418 +15,418 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | - * |
|
| 21 | - * @since 3.10.0 |
|
| 22 | - * @access private |
|
| 23 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | - */ |
|
| 25 | - private $configuration_service; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A {@link Wordlift_Log_Service} instance. |
|
| 29 | - * |
|
| 30 | - * @since 3.10.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | - */ |
|
| 34 | - private $log; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | - * |
|
| 39 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 40 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 41 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 42 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 43 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.10.0 |
|
| 46 | - * |
|
| 47 | - */ |
|
| 48 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 49 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 50 | - |
|
| 51 | - $this->configuration_service = $configuration_service; |
|
| 52 | - |
|
| 53 | - // Set a reference to the logger. |
|
| 54 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 59 | - * found while processing the post is set in the $references array. |
|
| 60 | - * |
|
| 61 | - * @param int $post_id The post id. |
|
| 62 | - * @param array $references An array of entity references. |
|
| 63 | - * @param array $references_infos |
|
| 64 | - * |
|
| 65 | - * @return array A JSON-LD array. |
|
| 66 | - * @since 3.10.0 |
|
| 67 | - */ |
|
| 68 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 69 | - |
|
| 70 | - // Get the post instance. |
|
| 71 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 72 | - // Post not found. |
|
| 73 | - return null; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 77 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 78 | - |
|
| 79 | - // Set WebPage by default. |
|
| 80 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 81 | - $jsonld['@type'] = 'WebPage'; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // Get the entity name. |
|
| 85 | - $jsonld['headline'] = $post->post_title; |
|
| 86 | - |
|
| 87 | - // Set the published and modified dates. |
|
| 88 | - /* |
|
| 18 | + /** |
|
| 19 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | + * |
|
| 21 | + * @since 3.10.0 |
|
| 22 | + * @access private |
|
| 23 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | + */ |
|
| 25 | + private $configuration_service; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A {@link Wordlift_Log_Service} instance. |
|
| 29 | + * |
|
| 30 | + * @since 3.10.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | + */ |
|
| 34 | + private $log; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | + * |
|
| 39 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 40 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 41 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 42 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 43 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.10.0 |
|
| 46 | + * |
|
| 47 | + */ |
|
| 48 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 49 | + parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 50 | + |
|
| 51 | + $this->configuration_service = $configuration_service; |
|
| 52 | + |
|
| 53 | + // Set a reference to the logger. |
|
| 54 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 59 | + * found while processing the post is set in the $references array. |
|
| 60 | + * |
|
| 61 | + * @param int $post_id The post id. |
|
| 62 | + * @param array $references An array of entity references. |
|
| 63 | + * @param array $references_infos |
|
| 64 | + * |
|
| 65 | + * @return array A JSON-LD array. |
|
| 66 | + * @since 3.10.0 |
|
| 67 | + */ |
|
| 68 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 69 | + |
|
| 70 | + // Get the post instance. |
|
| 71 | + if ( null === $post = get_post( $post_id ) ) { |
|
| 72 | + // Post not found. |
|
| 73 | + return null; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 77 | + $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 78 | + |
|
| 79 | + // Set WebPage by default. |
|
| 80 | + if ( empty( $jsonld['@type'] ) ) { |
|
| 81 | + $jsonld['@type'] = 'WebPage'; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // Get the entity name. |
|
| 85 | + $jsonld['headline'] = $post->post_title; |
|
| 86 | + |
|
| 87 | + // Set the published and modified dates. |
|
| 88 | + /* |
|
| 89 | 89 | * Set the `datePublished` and `dateModified` using the local timezone. |
| 90 | 90 | * |
| 91 | 91 | * @see https://github.com/insideout10/wordlift-plugin/issues/887 |
| 92 | 92 | * |
| 93 | 93 | * @since 3.20.0 |
| 94 | 94 | */ |
| 95 | - try { |
|
| 96 | - $default_timezone = date_default_timezone_get(); |
|
| 97 | - date_default_timezone_set( get_option( 'timezone_string' ) ); |
|
| 98 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 99 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 100 | - date_default_timezone_set( $default_timezone ); |
|
| 101 | - } catch ( Exception $e ) { |
|
| 102 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 103 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // Get the word count for the post. |
|
| 107 | - /* |
|
| 95 | + try { |
|
| 96 | + $default_timezone = date_default_timezone_get(); |
|
| 97 | + date_default_timezone_set( get_option( 'timezone_string' ) ); |
|
| 98 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 99 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 100 | + date_default_timezone_set( $default_timezone ); |
|
| 101 | + } catch ( Exception $e ) { |
|
| 102 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 103 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // Get the word count for the post. |
|
| 107 | + /* |
|
| 108 | 108 | * Do not display the `wordCount` on a `WebPage`. |
| 109 | 109 | * |
| 110 | 110 | * @see https://github.com/insideout10/wordlift-plugin/issues/888 |
| 111 | 111 | * |
| 112 | 112 | * @since 3.20.0 |
| 113 | 113 | */ |
| 114 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 115 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 116 | - $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 117 | - } |
|
| 114 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 115 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 116 | + $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /* |
|
| 119 | + /* |
|
| 120 | 120 | * Add keywords, articleSection, commentCount and inLanguage properties to `Article` JSON-LD |
| 121 | 121 | * |
| 122 | 122 | * @see https://github.com/insideout10/wordlift-plugin/issues/1140 |
| 123 | 123 | * |
| 124 | 124 | * @since 3.27.2 |
| 125 | 125 | */ |
| 126 | - if ( ! empty( $jsonld['@type'] ) && 'Article' === $jsonld['@type'] ) { |
|
| 127 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 128 | - $keywords = $post_adapter->keywords(); |
|
| 129 | - $article_section = $post_adapter->article_section(); |
|
| 130 | - $comment_count = $post_adapter->comment_count(); |
|
| 131 | - $locale = $post_adapter->locale(); |
|
| 132 | - |
|
| 133 | - if ( isset( $keywords ) ) { |
|
| 134 | - $jsonld['keywords'] = $keywords; |
|
| 135 | - } |
|
| 136 | - if ( isset( $article_section ) ) { |
|
| 137 | - $jsonld['articleSection'] = $article_section; |
|
| 138 | - } |
|
| 139 | - $jsonld['commentCount'] = $comment_count; |
|
| 140 | - $jsonld['inLanguage'] = $locale; |
|
| 141 | - //$post_adapter->add_mentions($post_id, $references ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // Set the publisher. |
|
| 145 | - $this->set_publisher( $jsonld ); |
|
| 146 | - |
|
| 147 | - // Process the references if any. |
|
| 148 | - if ( 0 < count( $references ) ) { |
|
| 149 | - |
|
| 150 | - // Prepare the `about` and `mentions` array. |
|
| 151 | - $about = $mentions = array(); |
|
| 152 | - |
|
| 153 | - // If the entity is in the title, then it should be an `about`. |
|
| 154 | - foreach ( $references as $reference ) { |
|
| 155 | - |
|
| 156 | - // Get the entity labels. |
|
| 157 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 158 | - |
|
| 159 | - // Get the entity URI. |
|
| 160 | - $item = array( |
|
| 161 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 162 | - ); |
|
| 163 | - |
|
| 164 | - $escaped_labels = array_map( function ( $value ) { |
|
| 165 | - return preg_quote( $value, '/' ); |
|
| 166 | - }, $labels ); |
|
| 167 | - |
|
| 168 | - // Check if the labels match any part of the title. |
|
| 169 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 170 | - |
|
| 171 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 172 | - if ( $matches ) { |
|
| 173 | - $about[] = $item; |
|
| 174 | - } else { |
|
| 175 | - $mentions[] = $item; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // If we have abouts, assign them to the JSON-LD. |
|
| 180 | - if ( 0 < count( $about ) ) { |
|
| 181 | - $jsonld['about'] = $about; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // If we have mentions, assign them to the JSON-LD. |
|
| 185 | - if ( 0 < count( $mentions ) ) { |
|
| 186 | - $jsonld['mentions'] = $mentions; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - // Finally set the author. |
|
| 191 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 195 | - * |
|
| 196 | - * @param array $value { |
|
| 197 | - * |
|
| 198 | - * @type array $jsonld The JSON-LD structure. |
|
| 199 | - * @type int[] $references An array of post IDs. |
|
| 200 | - * } |
|
| 201 | - * @since 3.25.0 |
|
| 202 | - * |
|
| 203 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 204 | - * |
|
| 205 | - * @api |
|
| 206 | - */ |
|
| 207 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 208 | - 'jsonld' => $jsonld, |
|
| 209 | - 'references' => $references, |
|
| 210 | - ), $post_id ); |
|
| 211 | - $jsonld = $ret_val['jsonld']; |
|
| 212 | - $references = $ret_val['references']; |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Call the `wl_post_jsonld` filter. |
|
| 216 | - * |
|
| 217 | - * @param array $jsonld The JSON-LD structure. |
|
| 218 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 219 | - * @param array $references The array of referenced entities. |
|
| 220 | - * |
|
| 221 | - * @since 3.14.0 |
|
| 222 | - * |
|
| 223 | - * @api |
|
| 224 | - */ |
|
| 225 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Get the author's JSON-LD fragment. |
|
| 230 | - * |
|
| 231 | - * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 232 | - * the referenced entity if configured for the {@link WP_User}. |
|
| 233 | - * |
|
| 234 | - * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 235 | - * @param array $references An array of referenced entities. |
|
| 236 | - * |
|
| 237 | - * @return string|array A JSON-LD structure. |
|
| 238 | - * @since 3.14.0 |
|
| 239 | - * |
|
| 240 | - */ |
|
| 241 | - private function get_author( $author_id, &$references ) { |
|
| 242 | - |
|
| 243 | - // Get the entity bound to this user. |
|
| 244 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 245 | - |
|
| 246 | - // If there's no entity bound return a simple author structure. |
|
| 247 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 248 | - |
|
| 249 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 250 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 251 | - |
|
| 252 | - return array( |
|
| 253 | - '@type' => 'Person', |
|
| 254 | - '@id' => $author_uri, |
|
| 255 | - 'name' => $author, |
|
| 256 | - ); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - // Add the author to the references. |
|
| 260 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 261 | - $references[] = $entity_id; |
|
| 262 | - |
|
| 263 | - // Return the JSON-LD for the referenced entity. |
|
| 264 | - return array( |
|
| 265 | - '@id' => $author_uri, |
|
| 266 | - ); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Enrich the provided params array with publisher data, if available. |
|
| 271 | - * |
|
| 272 | - * @param array $params The parameters array. |
|
| 273 | - * |
|
| 274 | - * @since 3.10.0 |
|
| 275 | - * |
|
| 276 | - */ |
|
| 277 | - protected function set_publisher( &$params ) { |
|
| 278 | - |
|
| 279 | - // If the publisher id isn't set don't do anything. |
|
| 280 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 281 | - return; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - // Get the post instance. |
|
| 285 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 286 | - // Publisher not found. |
|
| 287 | - return; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - // Get the item id. |
|
| 291 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 292 | - |
|
| 293 | - // Get the type. |
|
| 294 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 295 | - |
|
| 296 | - // Get the name. |
|
| 297 | - $name = $post->post_title; |
|
| 298 | - |
|
| 299 | - // Set the publisher data. |
|
| 300 | - $params['publisher'] = array( |
|
| 301 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 302 | - '@id' => $id, |
|
| 303 | - 'name' => $name, |
|
| 304 | - ); |
|
| 305 | - |
|
| 306 | - // Add the sameAs values associated with the publisher. |
|
| 307 | - $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 308 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 309 | - if ( ! empty( $sameas ) ) { |
|
| 310 | - $params['publisher']['sameAs'] = $sameas; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 314 | - // support the logo property. |
|
| 315 | - // |
|
| 316 | - // See http://schema.org/logo. |
|
| 317 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 318 | - return; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - // Get the publisher logo. |
|
| 322 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 323 | - |
|
| 324 | - // Bail out if the publisher logo isn't set. |
|
| 325 | - if ( false === $publisher_logo ) { |
|
| 326 | - return; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - // Copy over some useful properties. |
|
| 330 | - // |
|
| 331 | - // See https://developers.google.com/search/docs/data-types/articles. |
|
| 332 | - $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 333 | - $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 334 | - |
|
| 335 | - // If you specify a "width" or "height" value you should leave out |
|
| 336 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 337 | - // |
|
| 338 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 339 | - $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 340 | - $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 341 | - |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Get the publisher logo structure. |
|
| 346 | - * |
|
| 347 | - * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 348 | - * - the post has no featured image. |
|
| 349 | - * - the featured image has no file. |
|
| 350 | - * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 351 | - * |
|
| 352 | - * @param int $post_id The post id. |
|
| 353 | - * |
|
| 354 | - * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 355 | - * of errors. |
|
| 356 | - * @since 3.19.2 |
|
| 357 | - * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 358 | - * |
|
| 359 | - */ |
|
| 360 | - private function get_publisher_logo( $post_id ) { |
|
| 361 | - |
|
| 362 | - // Get the featured image for the post. |
|
| 363 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 364 | - |
|
| 365 | - // Bail out if thumbnail not available. |
|
| 366 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 367 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 368 | - |
|
| 369 | - return false; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - // Get the uploads base URL. |
|
| 373 | - $uploads_dir = wp_upload_dir(); |
|
| 374 | - |
|
| 375 | - // Get the attachment metadata. |
|
| 376 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 377 | - |
|
| 378 | - // Bail out if the file isn't set. |
|
| 379 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 380 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 381 | - |
|
| 382 | - return false; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 386 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 387 | - |
|
| 388 | - // Bail out if the file isn't found. |
|
| 389 | - if ( ! file_exists( $path ) ) { |
|
| 390 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 391 | - |
|
| 392 | - return false; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 396 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 397 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 398 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 399 | - |
|
| 400 | - return false; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 404 | - // and we don't actually know the end values. |
|
| 405 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 406 | - |
|
| 407 | - // If the file doesn't exist yet, create it. |
|
| 408 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 409 | - $original_file_editor->resize( 600, 60 ); |
|
| 410 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 414 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 415 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 416 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 417 | - |
|
| 418 | - return false; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - // Get the actual size. |
|
| 422 | - $size = $publisher_logo_editor->get_size(); |
|
| 423 | - |
|
| 424 | - // Finally return the array with data. |
|
| 425 | - return array( |
|
| 426 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 427 | - 'width' => $size['width'], |
|
| 428 | - 'height' => $size['height'], |
|
| 429 | - ); |
|
| 430 | - } |
|
| 126 | + if ( ! empty( $jsonld['@type'] ) && 'Article' === $jsonld['@type'] ) { |
|
| 127 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 128 | + $keywords = $post_adapter->keywords(); |
|
| 129 | + $article_section = $post_adapter->article_section(); |
|
| 130 | + $comment_count = $post_adapter->comment_count(); |
|
| 131 | + $locale = $post_adapter->locale(); |
|
| 132 | + |
|
| 133 | + if ( isset( $keywords ) ) { |
|
| 134 | + $jsonld['keywords'] = $keywords; |
|
| 135 | + } |
|
| 136 | + if ( isset( $article_section ) ) { |
|
| 137 | + $jsonld['articleSection'] = $article_section; |
|
| 138 | + } |
|
| 139 | + $jsonld['commentCount'] = $comment_count; |
|
| 140 | + $jsonld['inLanguage'] = $locale; |
|
| 141 | + //$post_adapter->add_mentions($post_id, $references ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // Set the publisher. |
|
| 145 | + $this->set_publisher( $jsonld ); |
|
| 146 | + |
|
| 147 | + // Process the references if any. |
|
| 148 | + if ( 0 < count( $references ) ) { |
|
| 149 | + |
|
| 150 | + // Prepare the `about` and `mentions` array. |
|
| 151 | + $about = $mentions = array(); |
|
| 152 | + |
|
| 153 | + // If the entity is in the title, then it should be an `about`. |
|
| 154 | + foreach ( $references as $reference ) { |
|
| 155 | + |
|
| 156 | + // Get the entity labels. |
|
| 157 | + $labels = $this->entity_service->get_labels( $reference ); |
|
| 158 | + |
|
| 159 | + // Get the entity URI. |
|
| 160 | + $item = array( |
|
| 161 | + '@id' => $this->entity_service->get_uri( $reference ), |
|
| 162 | + ); |
|
| 163 | + |
|
| 164 | + $escaped_labels = array_map( function ( $value ) { |
|
| 165 | + return preg_quote( $value, '/' ); |
|
| 166 | + }, $labels ); |
|
| 167 | + |
|
| 168 | + // Check if the labels match any part of the title. |
|
| 169 | + $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 170 | + |
|
| 171 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 172 | + if ( $matches ) { |
|
| 173 | + $about[] = $item; |
|
| 174 | + } else { |
|
| 175 | + $mentions[] = $item; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // If we have abouts, assign them to the JSON-LD. |
|
| 180 | + if ( 0 < count( $about ) ) { |
|
| 181 | + $jsonld['about'] = $about; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // If we have mentions, assign them to the JSON-LD. |
|
| 185 | + if ( 0 < count( $mentions ) ) { |
|
| 186 | + $jsonld['mentions'] = $mentions; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + // Finally set the author. |
|
| 191 | + $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 195 | + * |
|
| 196 | + * @param array $value { |
|
| 197 | + * |
|
| 198 | + * @type array $jsonld The JSON-LD structure. |
|
| 199 | + * @type int[] $references An array of post IDs. |
|
| 200 | + * } |
|
| 201 | + * @since 3.25.0 |
|
| 202 | + * |
|
| 203 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 204 | + * |
|
| 205 | + * @api |
|
| 206 | + */ |
|
| 207 | + $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 208 | + 'jsonld' => $jsonld, |
|
| 209 | + 'references' => $references, |
|
| 210 | + ), $post_id ); |
|
| 211 | + $jsonld = $ret_val['jsonld']; |
|
| 212 | + $references = $ret_val['references']; |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Call the `wl_post_jsonld` filter. |
|
| 216 | + * |
|
| 217 | + * @param array $jsonld The JSON-LD structure. |
|
| 218 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 219 | + * @param array $references The array of referenced entities. |
|
| 220 | + * |
|
| 221 | + * @since 3.14.0 |
|
| 222 | + * |
|
| 223 | + * @api |
|
| 224 | + */ |
|
| 225 | + return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Get the author's JSON-LD fragment. |
|
| 230 | + * |
|
| 231 | + * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 232 | + * the referenced entity if configured for the {@link WP_User}. |
|
| 233 | + * |
|
| 234 | + * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 235 | + * @param array $references An array of referenced entities. |
|
| 236 | + * |
|
| 237 | + * @return string|array A JSON-LD structure. |
|
| 238 | + * @since 3.14.0 |
|
| 239 | + * |
|
| 240 | + */ |
|
| 241 | + private function get_author( $author_id, &$references ) { |
|
| 242 | + |
|
| 243 | + // Get the entity bound to this user. |
|
| 244 | + $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 245 | + |
|
| 246 | + // If there's no entity bound return a simple author structure. |
|
| 247 | + if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 248 | + |
|
| 249 | + $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 250 | + $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 251 | + |
|
| 252 | + return array( |
|
| 253 | + '@type' => 'Person', |
|
| 254 | + '@id' => $author_uri, |
|
| 255 | + 'name' => $author, |
|
| 256 | + ); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + // Add the author to the references. |
|
| 260 | + $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 261 | + $references[] = $entity_id; |
|
| 262 | + |
|
| 263 | + // Return the JSON-LD for the referenced entity. |
|
| 264 | + return array( |
|
| 265 | + '@id' => $author_uri, |
|
| 266 | + ); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Enrich the provided params array with publisher data, if available. |
|
| 271 | + * |
|
| 272 | + * @param array $params The parameters array. |
|
| 273 | + * |
|
| 274 | + * @since 3.10.0 |
|
| 275 | + * |
|
| 276 | + */ |
|
| 277 | + protected function set_publisher( &$params ) { |
|
| 278 | + |
|
| 279 | + // If the publisher id isn't set don't do anything. |
|
| 280 | + if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 281 | + return; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + // Get the post instance. |
|
| 285 | + if ( null === $post = get_post( $publisher_id ) ) { |
|
| 286 | + // Publisher not found. |
|
| 287 | + return; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + // Get the item id. |
|
| 291 | + $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 292 | + |
|
| 293 | + // Get the type. |
|
| 294 | + $type = $this->entity_type_service->get( $publisher_id ); |
|
| 295 | + |
|
| 296 | + // Get the name. |
|
| 297 | + $name = $post->post_title; |
|
| 298 | + |
|
| 299 | + // Set the publisher data. |
|
| 300 | + $params['publisher'] = array( |
|
| 301 | + '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 302 | + '@id' => $id, |
|
| 303 | + 'name' => $name, |
|
| 304 | + ); |
|
| 305 | + |
|
| 306 | + // Add the sameAs values associated with the publisher. |
|
| 307 | + $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 308 | + $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 309 | + if ( ! empty( $sameas ) ) { |
|
| 310 | + $params['publisher']['sameAs'] = $sameas; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 314 | + // support the logo property. |
|
| 315 | + // |
|
| 316 | + // See http://schema.org/logo. |
|
| 317 | + if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 318 | + return; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + // Get the publisher logo. |
|
| 322 | + $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 323 | + |
|
| 324 | + // Bail out if the publisher logo isn't set. |
|
| 325 | + if ( false === $publisher_logo ) { |
|
| 326 | + return; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + // Copy over some useful properties. |
|
| 330 | + // |
|
| 331 | + // See https://developers.google.com/search/docs/data-types/articles. |
|
| 332 | + $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 333 | + $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 334 | + |
|
| 335 | + // If you specify a "width" or "height" value you should leave out |
|
| 336 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 337 | + // |
|
| 338 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 339 | + $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 340 | + $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 341 | + |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Get the publisher logo structure. |
|
| 346 | + * |
|
| 347 | + * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 348 | + * - the post has no featured image. |
|
| 349 | + * - the featured image has no file. |
|
| 350 | + * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 351 | + * |
|
| 352 | + * @param int $post_id The post id. |
|
| 353 | + * |
|
| 354 | + * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 355 | + * of errors. |
|
| 356 | + * @since 3.19.2 |
|
| 357 | + * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 358 | + * |
|
| 359 | + */ |
|
| 360 | + private function get_publisher_logo( $post_id ) { |
|
| 361 | + |
|
| 362 | + // Get the featured image for the post. |
|
| 363 | + $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 364 | + |
|
| 365 | + // Bail out if thumbnail not available. |
|
| 366 | + if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 367 | + $this->log->info( "Featured image not set for post $post_id." ); |
|
| 368 | + |
|
| 369 | + return false; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + // Get the uploads base URL. |
|
| 373 | + $uploads_dir = wp_upload_dir(); |
|
| 374 | + |
|
| 375 | + // Get the attachment metadata. |
|
| 376 | + $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 377 | + |
|
| 378 | + // Bail out if the file isn't set. |
|
| 379 | + if ( ! isset( $metadata['file'] ) ) { |
|
| 380 | + $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 381 | + |
|
| 382 | + return false; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 386 | + $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 387 | + |
|
| 388 | + // Bail out if the file isn't found. |
|
| 389 | + if ( ! file_exists( $path ) ) { |
|
| 390 | + $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 391 | + |
|
| 392 | + return false; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 396 | + $original_file_editor = wp_get_image_editor( $path ); |
|
| 397 | + if ( is_wp_error( $original_file_editor ) ) { |
|
| 398 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 399 | + |
|
| 400 | + return false; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 404 | + // and we don't actually know the end values. |
|
| 405 | + $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 406 | + |
|
| 407 | + // If the file doesn't exist yet, create it. |
|
| 408 | + if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 409 | + $original_file_editor->resize( 600, 60 ); |
|
| 410 | + $original_file_editor->save( $publisher_logo_path ); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 414 | + $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 415 | + if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 416 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 417 | + |
|
| 418 | + return false; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + // Get the actual size. |
|
| 422 | + $size = $publisher_logo_editor->get_size(); |
|
| 423 | + |
|
| 424 | + // Finally return the array with data. |
|
| 425 | + return array( |
|
| 426 | + 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 427 | + 'width' => $size['width'], |
|
| 428 | + 'height' => $size['height'], |
|
| 429 | + ); |
|
| 430 | + } |
|
| 431 | 431 | |
| 432 | 432 | } |
@@ -45,13 +45,13 @@ discard block |
||
| 45 | 45 | * @since 3.10.0 |
| 46 | 46 | * |
| 47 | 47 | */ |
| 48 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 49 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 48 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service) { |
|
| 49 | + parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service); |
|
| 50 | 50 | |
| 51 | 51 | $this->configuration_service = $configuration_service; |
| 52 | 52 | |
| 53 | 53 | // Set a reference to the logger. |
| 54 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter'); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -65,19 +65,19 @@ discard block |
||
| 65 | 65 | * @return array A JSON-LD array. |
| 66 | 66 | * @since 3.10.0 |
| 67 | 67 | */ |
| 68 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 68 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
| 69 | 69 | |
| 70 | 70 | // Get the post instance. |
| 71 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 71 | + if (null === $post = get_post($post_id)) { |
|
| 72 | 72 | // Post not found. |
| 73 | 73 | return null; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 77 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 77 | + $jsonld = parent::convert($post_id, $references, $references_infos); |
|
| 78 | 78 | |
| 79 | 79 | // Set WebPage by default. |
| 80 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 80 | + if (empty($jsonld['@type'])) { |
|
| 81 | 81 | $jsonld['@type'] = 'WebPage'; |
| 82 | 82 | } |
| 83 | 83 | |
@@ -94,13 +94,13 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | try { |
| 96 | 96 | $default_timezone = date_default_timezone_get(); |
| 97 | - date_default_timezone_set( get_option( 'timezone_string' ) ); |
|
| 98 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 99 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 100 | - date_default_timezone_set( $default_timezone ); |
|
| 101 | - } catch ( Exception $e ) { |
|
| 102 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 103 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 97 | + date_default_timezone_set(get_option('timezone_string')); |
|
| 98 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post); |
|
| 99 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i:sP', false, $post); |
|
| 100 | + date_default_timezone_set($default_timezone); |
|
| 101 | + } catch (Exception $e) { |
|
| 102 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 103 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Get the word count for the post. |
@@ -111,8 +111,8 @@ discard block |
||
| 111 | 111 | * |
| 112 | 112 | * @since 3.20.0 |
| 113 | 113 | */ |
| 114 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 115 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 114 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 115 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 116 | 116 | $jsonld['wordCount'] = $post_adapter->word_count(); |
| 117 | 117 | } |
| 118 | 118 | |
@@ -123,17 +123,17 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @since 3.27.2 |
| 125 | 125 | */ |
| 126 | - if ( ! empty( $jsonld['@type'] ) && 'Article' === $jsonld['@type'] ) { |
|
| 127 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 126 | + if ( ! empty($jsonld['@type']) && 'Article' === $jsonld['@type']) { |
|
| 127 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 128 | 128 | $keywords = $post_adapter->keywords(); |
| 129 | 129 | $article_section = $post_adapter->article_section(); |
| 130 | 130 | $comment_count = $post_adapter->comment_count(); |
| 131 | 131 | $locale = $post_adapter->locale(); |
| 132 | 132 | |
| 133 | - if ( isset( $keywords ) ) { |
|
| 133 | + if (isset($keywords)) { |
|
| 134 | 134 | $jsonld['keywords'] = $keywords; |
| 135 | 135 | } |
| 136 | - if ( isset( $article_section ) ) { |
|
| 136 | + if (isset($article_section)) { |
|
| 137 | 137 | $jsonld['articleSection'] = $article_section; |
| 138 | 138 | } |
| 139 | 139 | $jsonld['commentCount'] = $comment_count; |
@@ -142,34 +142,34 @@ discard block |
||
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | // Set the publisher. |
| 145 | - $this->set_publisher( $jsonld ); |
|
| 145 | + $this->set_publisher($jsonld); |
|
| 146 | 146 | |
| 147 | 147 | // Process the references if any. |
| 148 | - if ( 0 < count( $references ) ) { |
|
| 148 | + if (0 < count($references)) { |
|
| 149 | 149 | |
| 150 | 150 | // Prepare the `about` and `mentions` array. |
| 151 | 151 | $about = $mentions = array(); |
| 152 | 152 | |
| 153 | 153 | // If the entity is in the title, then it should be an `about`. |
| 154 | - foreach ( $references as $reference ) { |
|
| 154 | + foreach ($references as $reference) { |
|
| 155 | 155 | |
| 156 | 156 | // Get the entity labels. |
| 157 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 157 | + $labels = $this->entity_service->get_labels($reference); |
|
| 158 | 158 | |
| 159 | 159 | // Get the entity URI. |
| 160 | 160 | $item = array( |
| 161 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 161 | + '@id' => $this->entity_service->get_uri($reference), |
|
| 162 | 162 | ); |
| 163 | 163 | |
| 164 | - $escaped_labels = array_map( function ( $value ) { |
|
| 165 | - return preg_quote( $value, '/' ); |
|
| 166 | - }, $labels ); |
|
| 164 | + $escaped_labels = array_map(function($value) { |
|
| 165 | + return preg_quote($value, '/'); |
|
| 166 | + }, $labels); |
|
| 167 | 167 | |
| 168 | 168 | // Check if the labels match any part of the title. |
| 169 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 169 | + $matches = 1 === preg_match('/'.implode('|', $escaped_labels).'/', $post->post_title); |
|
| 170 | 170 | |
| 171 | 171 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 172 | - if ( $matches ) { |
|
| 172 | + if ($matches) { |
|
| 173 | 173 | $about[] = $item; |
| 174 | 174 | } else { |
| 175 | 175 | $mentions[] = $item; |
@@ -177,18 +177,18 @@ discard block |
||
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // If we have abouts, assign them to the JSON-LD. |
| 180 | - if ( 0 < count( $about ) ) { |
|
| 180 | + if (0 < count($about)) { |
|
| 181 | 181 | $jsonld['about'] = $about; |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // If we have mentions, assign them to the JSON-LD. |
| 185 | - if ( 0 < count( $mentions ) ) { |
|
| 185 | + if (0 < count($mentions)) { |
|
| 186 | 186 | $jsonld['mentions'] = $mentions; |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | // Finally set the author. |
| 191 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 191 | + $jsonld['author'] = $this->get_author($post->post_author, $references); |
|
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | 194 | * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
@@ -204,10 +204,10 @@ discard block |
||
| 204 | 204 | * |
| 205 | 205 | * @api |
| 206 | 206 | */ |
| 207 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 207 | + $ret_val = apply_filters('wl_post_jsonld_array', array( |
|
| 208 | 208 | 'jsonld' => $jsonld, |
| 209 | 209 | 'references' => $references, |
| 210 | - ), $post_id ); |
|
| 210 | + ), $post_id); |
|
| 211 | 211 | $jsonld = $ret_val['jsonld']; |
| 212 | 212 | $references = $ret_val['references']; |
| 213 | 213 | |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | * |
| 223 | 223 | * @api |
| 224 | 224 | */ |
| 225 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 225 | + return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references); |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | /** |
@@ -238,16 +238,16 @@ discard block |
||
| 238 | 238 | * @since 3.14.0 |
| 239 | 239 | * |
| 240 | 240 | */ |
| 241 | - private function get_author( $author_id, &$references ) { |
|
| 241 | + private function get_author($author_id, &$references) { |
|
| 242 | 242 | |
| 243 | 243 | // Get the entity bound to this user. |
| 244 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 244 | + $entity_id = $this->user_service->get_entity($author_id); |
|
| 245 | 245 | |
| 246 | 246 | // If there's no entity bound return a simple author structure. |
| 247 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 247 | + if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) { |
|
| 248 | 248 | |
| 249 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 250 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 249 | + $author = get_the_author_meta('display_name', $author_id); |
|
| 250 | + $author_uri = $this->user_service->get_uri($author_id); |
|
| 251 | 251 | |
| 252 | 252 | return array( |
| 253 | 253 | '@type' => 'Person', |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | // Add the author to the references. |
| 260 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 260 | + $author_uri = $this->entity_service->get_uri($entity_id); |
|
| 261 | 261 | $references[] = $entity_id; |
| 262 | 262 | |
| 263 | 263 | // Return the JSON-LD for the referenced entity. |
@@ -274,39 +274,39 @@ discard block |
||
| 274 | 274 | * @since 3.10.0 |
| 275 | 275 | * |
| 276 | 276 | */ |
| 277 | - protected function set_publisher( &$params ) { |
|
| 277 | + protected function set_publisher(&$params) { |
|
| 278 | 278 | |
| 279 | 279 | // If the publisher id isn't set don't do anything. |
| 280 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 280 | + if (null === $publisher_id = $this->configuration_service->get_publisher_id()) { |
|
| 281 | 281 | return; |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | // Get the post instance. |
| 285 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 285 | + if (null === $post = get_post($publisher_id)) { |
|
| 286 | 286 | // Publisher not found. |
| 287 | 287 | return; |
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | // Get the item id. |
| 291 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 291 | + $id = $this->entity_service->get_uri($publisher_id); |
|
| 292 | 292 | |
| 293 | 293 | // Get the type. |
| 294 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 294 | + $type = $this->entity_type_service->get($publisher_id); |
|
| 295 | 295 | |
| 296 | 296 | // Get the name. |
| 297 | 297 | $name = $post->post_title; |
| 298 | 298 | |
| 299 | 299 | // Set the publisher data. |
| 300 | 300 | $params['publisher'] = array( |
| 301 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 301 | + '@type' => $this->relative_to_context($type['uri']), |
|
| 302 | 302 | '@id' => $id, |
| 303 | 303 | 'name' => $name, |
| 304 | 304 | ); |
| 305 | 305 | |
| 306 | 306 | // Add the sameAs values associated with the publisher. |
| 307 | 307 | $storage_factory = Wordlift_Storage_Factory::get_instance(); |
| 308 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 309 | - if ( ! empty( $sameas ) ) { |
|
| 308 | + $sameas = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id); |
|
| 309 | + if ( ! empty($sameas)) { |
|
| 310 | 310 | $params['publisher']['sameAs'] = $sameas; |
| 311 | 311 | } |
| 312 | 312 | |
@@ -314,15 +314,15 @@ discard block |
||
| 314 | 314 | // support the logo property. |
| 315 | 315 | // |
| 316 | 316 | // See http://schema.org/logo. |
| 317 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 317 | + if ('http://schema.org/Organization' !== $type['uri']) { |
|
| 318 | 318 | return; |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | // Get the publisher logo. |
| 322 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 322 | + $publisher_logo = $this->get_publisher_logo($post->ID); |
|
| 323 | 323 | |
| 324 | 324 | // Bail out if the publisher logo isn't set. |
| 325 | - if ( false === $publisher_logo ) { |
|
| 325 | + if (false === $publisher_logo) { |
|
| 326 | 326 | return; |
| 327 | 327 | } |
| 328 | 328 | |
@@ -357,14 +357,14 @@ discard block |
||
| 357 | 357 | * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
| 358 | 358 | * |
| 359 | 359 | */ |
| 360 | - private function get_publisher_logo( $post_id ) { |
|
| 360 | + private function get_publisher_logo($post_id) { |
|
| 361 | 361 | |
| 362 | 362 | // Get the featured image for the post. |
| 363 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 363 | + $thumbnail_id = get_post_thumbnail_id($post_id); |
|
| 364 | 364 | |
| 365 | 365 | // Bail out if thumbnail not available. |
| 366 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 367 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 366 | + if (empty($thumbnail_id) || 0 === $thumbnail_id) { |
|
| 367 | + $this->log->info("Featured image not set for post $post_id."); |
|
| 368 | 368 | |
| 369 | 369 | return false; |
| 370 | 370 | } |
@@ -373,47 +373,47 @@ discard block |
||
| 373 | 373 | $uploads_dir = wp_upload_dir(); |
| 374 | 374 | |
| 375 | 375 | // Get the attachment metadata. |
| 376 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 376 | + $metadata = wp_get_attachment_metadata($thumbnail_id); |
|
| 377 | 377 | |
| 378 | 378 | // Bail out if the file isn't set. |
| 379 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 380 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 379 | + if ( ! isset($metadata['file'])) { |
|
| 380 | + $this->log->warn("Featured image file not found for post $post_id."); |
|
| 381 | 381 | |
| 382 | 382 | return false; |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
| 386 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 386 | + $path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file']; |
|
| 387 | 387 | |
| 388 | 388 | // Bail out if the file isn't found. |
| 389 | - if ( ! file_exists( $path ) ) { |
|
| 390 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 389 | + if ( ! file_exists($path)) { |
|
| 390 | + $this->log->warn("Featured image file $path doesn't exist for post $post_id."); |
|
| 391 | 391 | |
| 392 | 392 | return false; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 396 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 397 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 398 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 396 | + $original_file_editor = wp_get_image_editor($path); |
|
| 397 | + if (is_wp_error($original_file_editor)) { |
|
| 398 | + $this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id."); |
|
| 399 | 399 | |
| 400 | 400 | return false; |
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
| 404 | 404 | // and we don't actually know the end values. |
| 405 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 405 | + $publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo'); |
|
| 406 | 406 | |
| 407 | 407 | // If the file doesn't exist yet, create it. |
| 408 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 409 | - $original_file_editor->resize( 600, 60 ); |
|
| 410 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 408 | + if ( ! file_exists($publisher_logo_path)) { |
|
| 409 | + $original_file_editor->resize(600, 60); |
|
| 410 | + $original_file_editor->save($publisher_logo_path); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 414 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 415 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 416 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 414 | + $publisher_logo_editor = wp_get_image_editor($publisher_logo_path); |
|
| 415 | + if (is_wp_error($publisher_logo_editor)) { |
|
| 416 | + $this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id."); |
|
| 417 | 417 | |
| 418 | 418 | return false; |
| 419 | 419 | } |
@@ -423,7 +423,7 @@ discard block |
||
| 423 | 423 | |
| 424 | 424 | // Finally return the array with data. |
| 425 | 425 | return array( |
| 426 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 426 | + 'url' => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])), |
|
| 427 | 427 | 'width' => $size['width'], |
| 428 | 428 | 'height' => $size['height'], |
| 429 | 429 | ); |
@@ -16,258 +16,258 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Post_Adapter { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The post id to which the adopter relates. |
|
| 21 | - * |
|
| 22 | - * @since 3.14.0 |
|
| 23 | - * |
|
| 24 | - * @var integer $post_id . |
|
| 25 | - */ |
|
| 26 | - private $post_id; |
|
| 27 | - |
|
| 28 | - const TYPE_ENTITY_LINK = 0; |
|
| 29 | - const TYPE_TERM_LINK = 1; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Create the {@link Wordlift_Post_Adatpter} instance. |
|
| 33 | - * |
|
| 34 | - * @param integer $post_id the post ID of the post the adopter relates to. |
|
| 35 | - * |
|
| 36 | - * @since 3.14.0 |
|
| 37 | - * |
|
| 38 | - */ |
|
| 39 | - public function __construct( $post_id ) { |
|
| 40 | - |
|
| 41 | - $this->post_id = $post_id; |
|
| 42 | - |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Get the word count of the post content. |
|
| 47 | - * |
|
| 48 | - * The count is calculated over the post content after stripping shortcodes and html tags. |
|
| 49 | - * |
|
| 50 | - * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
| 51 | - * @since 3.14.0 |
|
| 52 | - * |
|
| 53 | - */ |
|
| 54 | - public function word_count() { |
|
| 55 | - |
|
| 56 | - $post = get_post( $this->post_id ); |
|
| 57 | - |
|
| 58 | - /* |
|
| 19 | + /** |
|
| 20 | + * The post id to which the adopter relates. |
|
| 21 | + * |
|
| 22 | + * @since 3.14.0 |
|
| 23 | + * |
|
| 24 | + * @var integer $post_id . |
|
| 25 | + */ |
|
| 26 | + private $post_id; |
|
| 27 | + |
|
| 28 | + const TYPE_ENTITY_LINK = 0; |
|
| 29 | + const TYPE_TERM_LINK = 1; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Create the {@link Wordlift_Post_Adatpter} instance. |
|
| 33 | + * |
|
| 34 | + * @param integer $post_id the post ID of the post the adopter relates to. |
|
| 35 | + * |
|
| 36 | + * @since 3.14.0 |
|
| 37 | + * |
|
| 38 | + */ |
|
| 39 | + public function __construct( $post_id ) { |
|
| 40 | + |
|
| 41 | + $this->post_id = $post_id; |
|
| 42 | + |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Get the word count of the post content. |
|
| 47 | + * |
|
| 48 | + * The count is calculated over the post content after stripping shortcodes and html tags. |
|
| 49 | + * |
|
| 50 | + * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
| 51 | + * @since 3.14.0 |
|
| 52 | + * |
|
| 53 | + */ |
|
| 54 | + public function word_count() { |
|
| 55 | + |
|
| 56 | + $post = get_post( $this->post_id ); |
|
| 57 | + |
|
| 58 | + /* |
|
| 59 | 59 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 60 | 60 | * because the content is written elsewhere. |
| 61 | 61 | * |
| 62 | 62 | * @since 3.20.0 |
| 63 | 63 | */ |
| 64 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 65 | - |
|
| 66 | - return self::str_word_count_utf8( strip_tags( strip_shortcodes( $post_content ) ) ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Count words in the string, taking into account UTF-8 characters. |
|
| 71 | - * |
|
| 72 | - * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
| 73 | - * |
|
| 74 | - * @since 3.20.0 |
|
| 75 | - * |
|
| 76 | - * @param string $str The target string. |
|
| 77 | - * |
|
| 78 | - * @return int The number of words. |
|
| 79 | - */ |
|
| 80 | - private static function str_word_count_utf8( $str ) { |
|
| 81 | - |
|
| 82 | - return count( preg_split( '~[^\p{L}\p{N}\']+~u', $str ) ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
| 87 | - * |
|
| 88 | - * @param int $post_id Post ID. |
|
| 89 | - * |
|
| 90 | - * @return string The post permalink. |
|
| 91 | - * @since 3.20.0 |
|
| 92 | - * |
|
| 93 | - */ |
|
| 94 | - public static function get_production_permalink( $post_id ) { |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
| 98 | - * |
|
| 99 | - * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
| 100 | - * post permalink if we find them. |
|
| 101 | - */ |
|
| 102 | - global $wp_version; |
|
| 103 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 104 | - $permalink = get_permalink( $post_id ); |
|
| 105 | - |
|
| 106 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
| 111 | - * when the production environment is copied over from a staging environment with staging |
|
| 112 | - * URLs. |
|
| 113 | - * |
|
| 114 | - * @param string $permalink_url The default permalink. |
|
| 115 | - * @param int $post_id The post id. |
|
| 116 | - * |
|
| 117 | - * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
| 118 | - * @since 3.20.0 |
|
| 119 | - * |
|
| 120 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 121 | - * |
|
| 122 | - */ |
|
| 123 | - |
|
| 124 | - // Get all the URIs for the entity, i.e. itemid and sameAs. |
|
| 125 | - $uris = array_merge( |
|
| 126 | - (array) Wordlift_Entity_Service::get_instance()->get_uri( $post_id ), |
|
| 127 | - get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 128 | - ); |
|
| 129 | - |
|
| 130 | - // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
| 131 | - $terms = array(); |
|
| 132 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
| 133 | - // Try to find one term matching the entity. |
|
| 134 | - $terms = get_terms( array( |
|
| 135 | - 'number' => 1, |
|
| 136 | - 'hide_empty' => false, |
|
| 137 | - 'update_term_meta_cache' => false, |
|
| 138 | - 'meta_key' => '_wl_entity_id', |
|
| 139 | - 'meta_value' => $uris, |
|
| 140 | - 'meta_compare' => 'IN', |
|
| 141 | - ) ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // If found use the term link, otherwise the permalink. |
|
| 145 | - if ( 1 === count( $terms ) ) { |
|
| 146 | - $term = current( $terms ); |
|
| 147 | - $permalink = get_term_link( $term ); |
|
| 148 | - $type = self::TYPE_TERM_LINK; |
|
| 149 | - } else { |
|
| 150 | - $term = null; |
|
| 151 | - $permalink = get_permalink( $post_id ); |
|
| 152 | - $type = self::TYPE_ENTITY_LINK; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Apply the `wl_production_permalink` filter. |
|
| 157 | - * |
|
| 158 | - * @param string $permalink The permalink. |
|
| 159 | - * @param int $post_id The post id. |
|
| 160 | - * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
| 161 | - * @param WP_Term $term The term if type is term link, otherwise null. |
|
| 162 | - * |
|
| 163 | - * @since 3.23.0 add the permalink type and term parameters. |
|
| 164 | - * |
|
| 165 | - */ |
|
| 166 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Get comma separated tags to be used as keywords |
|
| 171 | - * |
|
| 172 | - * @return string|void Comma separated tags |
|
| 173 | - * |
|
| 174 | - * @since 3.27.2 |
|
| 175 | - */ |
|
| 176 | - public function keywords() { |
|
| 177 | - $tags = get_the_tags( $this->post_id ); |
|
| 178 | - |
|
| 179 | - if ( empty( $tags ) ) { |
|
| 180 | - return; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return implode( ',', array_map( function ( $tag ) { |
|
| 184 | - return $tag->name; |
|
| 185 | - }, $tags ) ); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Get comma separated categories to be used as article section |
|
| 190 | - * |
|
| 191 | - * @return string|void Comma separated categories |
|
| 192 | - * |
|
| 193 | - * @since 3.27.2 |
|
| 194 | - */ |
|
| 195 | - public function article_section() { |
|
| 196 | - $categories = get_the_category( $this->post_id ); |
|
| 197 | - |
|
| 198 | - if ( empty( $categories ) ) { |
|
| 199 | - return; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return implode( ',', array_map( function ( $tag ) { |
|
| 203 | - return $tag->cat_name; |
|
| 204 | - }, $categories ) ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Get comment count |
|
| 209 | - * |
|
| 210 | - * @return string|void Comment count |
|
| 211 | - * |
|
| 212 | - * @since 3.27.2 |
|
| 213 | - */ |
|
| 214 | - public function comment_count() { |
|
| 215 | - return get_comments_number( $this->post_id ); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Get Language |
|
| 220 | - * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
| 221 | - * |
|
| 222 | - * @return string|void Language code (locale) |
|
| 223 | - * |
|
| 224 | - * @since 3.27.2 |
|
| 225 | - */ |
|
| 226 | - public function locale() { |
|
| 227 | - // WPML handling |
|
| 228 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
| 229 | - $post_language = wpml_get_language_information( $this->post_id ); |
|
| 230 | - |
|
| 231 | - return $post_language['locale']; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Polylang handling |
|
| 235 | - if ( function_exists( 'pll_get_post_language' ) ) { |
|
| 236 | - return pll_get_post_language( $this->post_id, 'locale' ); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - return get_locale(); |
|
| 240 | - |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Add mentions to an article. |
|
| 245 | - * To add mentions we just push the referenced entities to mentions. |
|
| 246 | - * |
|
| 247 | - * @param $post_id int |
|
| 248 | - * @param $references int[] |
|
| 249 | - */ |
|
| 250 | - public function add_mentions( $post_id, &$references ) { |
|
| 251 | - // Create empty array for references. |
|
| 252 | - if ( ! is_array( $references ) ) { |
|
| 253 | - $references = array(); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - $tags = get_the_tags( $post_id ); |
|
| 257 | - // Loop through the tags and push it to references. |
|
| 258 | - foreach ( $tags as $tag ) { |
|
| 259 | - /** |
|
| 260 | - * @var $tag WP_Term |
|
| 261 | - */ |
|
| 262 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
| 263 | - foreach ( $entity_uris as $uri ) { |
|
| 264 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
| 265 | - if ( $referenced_entity instanceof WP_Post ) { |
|
| 266 | - // push the referenced entities to references. |
|
| 267 | - $references[] = $referenced_entity->ID; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - } |
|
| 64 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 65 | + |
|
| 66 | + return self::str_word_count_utf8( strip_tags( strip_shortcodes( $post_content ) ) ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Count words in the string, taking into account UTF-8 characters. |
|
| 71 | + * |
|
| 72 | + * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
| 73 | + * |
|
| 74 | + * @since 3.20.0 |
|
| 75 | + * |
|
| 76 | + * @param string $str The target string. |
|
| 77 | + * |
|
| 78 | + * @return int The number of words. |
|
| 79 | + */ |
|
| 80 | + private static function str_word_count_utf8( $str ) { |
|
| 81 | + |
|
| 82 | + return count( preg_split( '~[^\p{L}\p{N}\']+~u', $str ) ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
| 87 | + * |
|
| 88 | + * @param int $post_id Post ID. |
|
| 89 | + * |
|
| 90 | + * @return string The post permalink. |
|
| 91 | + * @since 3.20.0 |
|
| 92 | + * |
|
| 93 | + */ |
|
| 94 | + public static function get_production_permalink( $post_id ) { |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
| 98 | + * |
|
| 99 | + * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
| 100 | + * post permalink if we find them. |
|
| 101 | + */ |
|
| 102 | + global $wp_version; |
|
| 103 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 104 | + $permalink = get_permalink( $post_id ); |
|
| 105 | + |
|
| 106 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
| 111 | + * when the production environment is copied over from a staging environment with staging |
|
| 112 | + * URLs. |
|
| 113 | + * |
|
| 114 | + * @param string $permalink_url The default permalink. |
|
| 115 | + * @param int $post_id The post id. |
|
| 116 | + * |
|
| 117 | + * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
| 118 | + * @since 3.20.0 |
|
| 119 | + * |
|
| 120 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 121 | + * |
|
| 122 | + */ |
|
| 123 | + |
|
| 124 | + // Get all the URIs for the entity, i.e. itemid and sameAs. |
|
| 125 | + $uris = array_merge( |
|
| 126 | + (array) Wordlift_Entity_Service::get_instance()->get_uri( $post_id ), |
|
| 127 | + get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 128 | + ); |
|
| 129 | + |
|
| 130 | + // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
| 131 | + $terms = array(); |
|
| 132 | + if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
| 133 | + // Try to find one term matching the entity. |
|
| 134 | + $terms = get_terms( array( |
|
| 135 | + 'number' => 1, |
|
| 136 | + 'hide_empty' => false, |
|
| 137 | + 'update_term_meta_cache' => false, |
|
| 138 | + 'meta_key' => '_wl_entity_id', |
|
| 139 | + 'meta_value' => $uris, |
|
| 140 | + 'meta_compare' => 'IN', |
|
| 141 | + ) ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // If found use the term link, otherwise the permalink. |
|
| 145 | + if ( 1 === count( $terms ) ) { |
|
| 146 | + $term = current( $terms ); |
|
| 147 | + $permalink = get_term_link( $term ); |
|
| 148 | + $type = self::TYPE_TERM_LINK; |
|
| 149 | + } else { |
|
| 150 | + $term = null; |
|
| 151 | + $permalink = get_permalink( $post_id ); |
|
| 152 | + $type = self::TYPE_ENTITY_LINK; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Apply the `wl_production_permalink` filter. |
|
| 157 | + * |
|
| 158 | + * @param string $permalink The permalink. |
|
| 159 | + * @param int $post_id The post id. |
|
| 160 | + * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
| 161 | + * @param WP_Term $term The term if type is term link, otherwise null. |
|
| 162 | + * |
|
| 163 | + * @since 3.23.0 add the permalink type and term parameters. |
|
| 164 | + * |
|
| 165 | + */ |
|
| 166 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Get comma separated tags to be used as keywords |
|
| 171 | + * |
|
| 172 | + * @return string|void Comma separated tags |
|
| 173 | + * |
|
| 174 | + * @since 3.27.2 |
|
| 175 | + */ |
|
| 176 | + public function keywords() { |
|
| 177 | + $tags = get_the_tags( $this->post_id ); |
|
| 178 | + |
|
| 179 | + if ( empty( $tags ) ) { |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return implode( ',', array_map( function ( $tag ) { |
|
| 184 | + return $tag->name; |
|
| 185 | + }, $tags ) ); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Get comma separated categories to be used as article section |
|
| 190 | + * |
|
| 191 | + * @return string|void Comma separated categories |
|
| 192 | + * |
|
| 193 | + * @since 3.27.2 |
|
| 194 | + */ |
|
| 195 | + public function article_section() { |
|
| 196 | + $categories = get_the_category( $this->post_id ); |
|
| 197 | + |
|
| 198 | + if ( empty( $categories ) ) { |
|
| 199 | + return; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return implode( ',', array_map( function ( $tag ) { |
|
| 203 | + return $tag->cat_name; |
|
| 204 | + }, $categories ) ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Get comment count |
|
| 209 | + * |
|
| 210 | + * @return string|void Comment count |
|
| 211 | + * |
|
| 212 | + * @since 3.27.2 |
|
| 213 | + */ |
|
| 214 | + public function comment_count() { |
|
| 215 | + return get_comments_number( $this->post_id ); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Get Language |
|
| 220 | + * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
| 221 | + * |
|
| 222 | + * @return string|void Language code (locale) |
|
| 223 | + * |
|
| 224 | + * @since 3.27.2 |
|
| 225 | + */ |
|
| 226 | + public function locale() { |
|
| 227 | + // WPML handling |
|
| 228 | + if ( function_exists( 'wpml_get_language_information' ) ) { |
|
| 229 | + $post_language = wpml_get_language_information( $this->post_id ); |
|
| 230 | + |
|
| 231 | + return $post_language['locale']; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Polylang handling |
|
| 235 | + if ( function_exists( 'pll_get_post_language' ) ) { |
|
| 236 | + return pll_get_post_language( $this->post_id, 'locale' ); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + return get_locale(); |
|
| 240 | + |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Add mentions to an article. |
|
| 245 | + * To add mentions we just push the referenced entities to mentions. |
|
| 246 | + * |
|
| 247 | + * @param $post_id int |
|
| 248 | + * @param $references int[] |
|
| 249 | + */ |
|
| 250 | + public function add_mentions( $post_id, &$references ) { |
|
| 251 | + // Create empty array for references. |
|
| 252 | + if ( ! is_array( $references ) ) { |
|
| 253 | + $references = array(); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $tags = get_the_tags( $post_id ); |
|
| 257 | + // Loop through the tags and push it to references. |
|
| 258 | + foreach ( $tags as $tag ) { |
|
| 259 | + /** |
|
| 260 | + * @var $tag WP_Term |
|
| 261 | + */ |
|
| 262 | + $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
| 263 | + foreach ( $entity_uris as $uri ) { |
|
| 264 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
| 265 | + if ( $referenced_entity instanceof WP_Post ) { |
|
| 266 | + // push the referenced entities to references. |
|
| 267 | + $references[] = $referenced_entity->ID; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | 273 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | * @since 3.14.0 |
| 37 | 37 | * |
| 38 | 38 | */ |
| 39 | - public function __construct( $post_id ) { |
|
| 39 | + public function __construct($post_id) { |
|
| 40 | 40 | |
| 41 | 41 | $this->post_id = $post_id; |
| 42 | 42 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | public function word_count() { |
| 55 | 55 | |
| 56 | - $post = get_post( $this->post_id ); |
|
| 56 | + $post = get_post($this->post_id); |
|
| 57 | 57 | |
| 58 | 58 | /* |
| 59 | 59 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @since 3.20.0 |
| 63 | 63 | */ |
| 64 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 64 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 65 | 65 | |
| 66 | - return self::str_word_count_utf8( strip_tags( strip_shortcodes( $post_content ) ) ); |
|
| 66 | + return self::str_word_count_utf8(strip_tags(strip_shortcodes($post_content))); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @return int The number of words. |
| 79 | 79 | */ |
| 80 | - private static function str_word_count_utf8( $str ) { |
|
| 80 | + private static function str_word_count_utf8($str) { |
|
| 81 | 81 | |
| 82 | - return count( preg_split( '~[^\p{L}\p{N}\']+~u', $str ) ); |
|
| 82 | + return count(preg_split('~[^\p{L}\p{N}\']+~u', $str)); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @since 3.20.0 |
| 92 | 92 | * |
| 93 | 93 | */ |
| 94 | - public static function get_production_permalink( $post_id ) { |
|
| 94 | + public static function get_production_permalink($post_id) { |
|
| 95 | 95 | |
| 96 | 96 | /** |
| 97 | 97 | * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
@@ -100,10 +100,10 @@ discard block |
||
| 100 | 100 | * post permalink if we find them. |
| 101 | 101 | */ |
| 102 | 102 | global $wp_version; |
| 103 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 104 | - $permalink = get_permalink( $post_id ); |
|
| 103 | + if (version_compare($wp_version, '4.5', '<')) { |
|
| 104 | + $permalink = get_permalink($post_id); |
|
| 105 | 105 | |
| 106 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
| 106 | + return apply_filters('wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -123,32 +123,32 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | // Get all the URIs for the entity, i.e. itemid and sameAs. |
| 125 | 125 | $uris = array_merge( |
| 126 | - (array) Wordlift_Entity_Service::get_instance()->get_uri( $post_id ), |
|
| 127 | - get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 126 | + (array) Wordlift_Entity_Service::get_instance()->get_uri($post_id), |
|
| 127 | + get_post_meta($post_id, Wordlift_Schema_Service::FIELD_SAME_AS) |
|
| 128 | 128 | ); |
| 129 | 129 | |
| 130 | 130 | // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
| 131 | 131 | $terms = array(); |
| 132 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
| 132 | + if (defined('WL_ENABLE_TERM_LINKING') && WL_ENABLE_TERM_LINKING) { |
|
| 133 | 133 | // Try to find one term matching the entity. |
| 134 | - $terms = get_terms( array( |
|
| 134 | + $terms = get_terms(array( |
|
| 135 | 135 | 'number' => 1, |
| 136 | 136 | 'hide_empty' => false, |
| 137 | 137 | 'update_term_meta_cache' => false, |
| 138 | 138 | 'meta_key' => '_wl_entity_id', |
| 139 | 139 | 'meta_value' => $uris, |
| 140 | 140 | 'meta_compare' => 'IN', |
| 141 | - ) ); |
|
| 141 | + )); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | // If found use the term link, otherwise the permalink. |
| 145 | - if ( 1 === count( $terms ) ) { |
|
| 146 | - $term = current( $terms ); |
|
| 147 | - $permalink = get_term_link( $term ); |
|
| 145 | + if (1 === count($terms)) { |
|
| 146 | + $term = current($terms); |
|
| 147 | + $permalink = get_term_link($term); |
|
| 148 | 148 | $type = self::TYPE_TERM_LINK; |
| 149 | 149 | } else { |
| 150 | 150 | $term = null; |
| 151 | - $permalink = get_permalink( $post_id ); |
|
| 151 | + $permalink = get_permalink($post_id); |
|
| 152 | 152 | $type = self::TYPE_ENTITY_LINK; |
| 153 | 153 | } |
| 154 | 154 | |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | * @since 3.23.0 add the permalink type and term parameters. |
| 164 | 164 | * |
| 165 | 165 | */ |
| 166 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
| 166 | + return apply_filters('wl_production_permalink', $permalink, $post_id, $type, $term); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -174,15 +174,15 @@ discard block |
||
| 174 | 174 | * @since 3.27.2 |
| 175 | 175 | */ |
| 176 | 176 | public function keywords() { |
| 177 | - $tags = get_the_tags( $this->post_id ); |
|
| 177 | + $tags = get_the_tags($this->post_id); |
|
| 178 | 178 | |
| 179 | - if ( empty( $tags ) ) { |
|
| 179 | + if (empty($tags)) { |
|
| 180 | 180 | return; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - return implode( ',', array_map( function ( $tag ) { |
|
| 183 | + return implode(',', array_map(function($tag) { |
|
| 184 | 184 | return $tag->name; |
| 185 | - }, $tags ) ); |
|
| 185 | + }, $tags)); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -193,15 +193,15 @@ discard block |
||
| 193 | 193 | * @since 3.27.2 |
| 194 | 194 | */ |
| 195 | 195 | public function article_section() { |
| 196 | - $categories = get_the_category( $this->post_id ); |
|
| 196 | + $categories = get_the_category($this->post_id); |
|
| 197 | 197 | |
| 198 | - if ( empty( $categories ) ) { |
|
| 198 | + if (empty($categories)) { |
|
| 199 | 199 | return; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - return implode( ',', array_map( function ( $tag ) { |
|
| 202 | + return implode(',', array_map(function($tag) { |
|
| 203 | 203 | return $tag->cat_name; |
| 204 | - }, $categories ) ); |
|
| 204 | + }, $categories)); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * @since 3.27.2 |
| 213 | 213 | */ |
| 214 | 214 | public function comment_count() { |
| 215 | - return get_comments_number( $this->post_id ); |
|
| 215 | + return get_comments_number($this->post_id); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -225,15 +225,15 @@ discard block |
||
| 225 | 225 | */ |
| 226 | 226 | public function locale() { |
| 227 | 227 | // WPML handling |
| 228 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
| 229 | - $post_language = wpml_get_language_information( $this->post_id ); |
|
| 228 | + if (function_exists('wpml_get_language_information')) { |
|
| 229 | + $post_language = wpml_get_language_information($this->post_id); |
|
| 230 | 230 | |
| 231 | 231 | return $post_language['locale']; |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | // Polylang handling |
| 235 | - if ( function_exists( 'pll_get_post_language' ) ) { |
|
| 236 | - return pll_get_post_language( $this->post_id, 'locale' ); |
|
| 235 | + if (function_exists('pll_get_post_language')) { |
|
| 236 | + return pll_get_post_language($this->post_id, 'locale'); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | return get_locale(); |
@@ -247,22 +247,22 @@ discard block |
||
| 247 | 247 | * @param $post_id int |
| 248 | 248 | * @param $references int[] |
| 249 | 249 | */ |
| 250 | - public function add_mentions( $post_id, &$references ) { |
|
| 250 | + public function add_mentions($post_id, &$references) { |
|
| 251 | 251 | // Create empty array for references. |
| 252 | - if ( ! is_array( $references ) ) { |
|
| 252 | + if ( ! is_array($references)) { |
|
| 253 | 253 | $references = array(); |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - $tags = get_the_tags( $post_id ); |
|
| 256 | + $tags = get_the_tags($post_id); |
|
| 257 | 257 | // Loop through the tags and push it to references. |
| 258 | - foreach ( $tags as $tag ) { |
|
| 258 | + foreach ($tags as $tag) { |
|
| 259 | 259 | /** |
| 260 | 260 | * @var $tag WP_Term |
| 261 | 261 | */ |
| 262 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
| 263 | - foreach ( $entity_uris as $uri ) { |
|
| 264 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
| 265 | - if ( $referenced_entity instanceof WP_Post ) { |
|
| 262 | + $entity_uris = get_term_meta($tag->term_id, '_wl_entity_id'); |
|
| 263 | + foreach ($entity_uris as $uri) { |
|
| 264 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity($uri); |
|
| 265 | + if ($referenced_entity instanceof WP_Post) { |
|
| 266 | 266 | // push the referenced entities to references. |
| 267 | 267 | $references[] = $referenced_entity->ID; |
| 268 | 268 | } |