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