@@ -18,459 +18,459 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 23 | - */ |
|
| 24 | - private static $instance; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A {@link Wordlift_Log_Service} instance. |
|
| 28 | - * |
|
| 29 | - * @since 3.10.0 |
|
| 30 | - * @access private |
|
| 31 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 32 | - */ |
|
| 33 | - private $log; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var false |
|
| 37 | - */ |
|
| 38 | - private $disable_convert_filters; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 42 | - * |
|
| 43 | - * @param Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 44 | - * @param Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 45 | - * @param Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 46 | - * |
|
| 47 | - * @since 3.10.0 |
|
| 48 | - */ |
|
| 49 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) { |
|
| 50 | - parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() ); |
|
| 51 | - $this->disable_convert_filters = $disable_convert_filters; |
|
| 52 | - // Set a reference to the logger. |
|
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | - |
|
| 55 | - self::$instance = $this; |
|
| 56 | - |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public static function get_instance() { |
|
| 60 | - |
|
| 61 | - return self::$instance; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public function new_instance_with_filters_disabled() { |
|
| 65 | - return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 70 | - * found while processing the post is set in the $references array. |
|
| 71 | - * |
|
| 72 | - * @param int $post_id The post id. |
|
| 73 | - * @param array<Reference> $references An array of entity references. |
|
| 74 | - * @param array $references_infos |
|
| 75 | - * |
|
| 76 | - * @return array A JSON-LD array. |
|
| 77 | - * @since 3.10.0 |
|
| 78 | - */ |
|
| 79 | - public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) { |
|
| 80 | - |
|
| 81 | - // Get the post instance. |
|
| 82 | - $post = get_post( $post_id ); |
|
| 83 | - if ( null === $post ) { |
|
| 84 | - // Post not found. |
|
| 85 | - return null; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 89 | - $jsonld = parent::convert( $post_id, $references, $references_infos, $relations ); |
|
| 90 | - |
|
| 91 | - // Set WebPage by default. |
|
| 92 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 93 | - $jsonld['@type'] = 'WebPage'; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // Get the entity name. |
|
| 97 | - $jsonld['headline'] = $post->post_title; |
|
| 98 | - |
|
| 99 | - // Set the published and modified dates. |
|
| 100 | - /* |
|
| 21 | + /** |
|
| 22 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 23 | + */ |
|
| 24 | + private static $instance; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A {@link Wordlift_Log_Service} instance. |
|
| 28 | + * |
|
| 29 | + * @since 3.10.0 |
|
| 30 | + * @access private |
|
| 31 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 32 | + */ |
|
| 33 | + private $log; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var false |
|
| 37 | + */ |
|
| 38 | + private $disable_convert_filters; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 42 | + * |
|
| 43 | + * @param Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 44 | + * @param Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 45 | + * @param Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 46 | + * |
|
| 47 | + * @since 3.10.0 |
|
| 48 | + */ |
|
| 49 | + public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) { |
|
| 50 | + parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() ); |
|
| 51 | + $this->disable_convert_filters = $disable_convert_filters; |
|
| 52 | + // Set a reference to the logger. |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | + |
|
| 55 | + self::$instance = $this; |
|
| 56 | + |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public static function get_instance() { |
|
| 60 | + |
|
| 61 | + return self::$instance; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public function new_instance_with_filters_disabled() { |
|
| 65 | + return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 70 | + * found while processing the post is set in the $references array. |
|
| 71 | + * |
|
| 72 | + * @param int $post_id The post id. |
|
| 73 | + * @param array<Reference> $references An array of entity references. |
|
| 74 | + * @param array $references_infos |
|
| 75 | + * |
|
| 76 | + * @return array A JSON-LD array. |
|
| 77 | + * @since 3.10.0 |
|
| 78 | + */ |
|
| 79 | + public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) { |
|
| 80 | + |
|
| 81 | + // Get the post instance. |
|
| 82 | + $post = get_post( $post_id ); |
|
| 83 | + if ( null === $post ) { |
|
| 84 | + // Post not found. |
|
| 85 | + return null; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 89 | + $jsonld = parent::convert( $post_id, $references, $references_infos, $relations ); |
|
| 90 | + |
|
| 91 | + // Set WebPage by default. |
|
| 92 | + if ( empty( $jsonld['@type'] ) ) { |
|
| 93 | + $jsonld['@type'] = 'WebPage'; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // Get the entity name. |
|
| 97 | + $jsonld['headline'] = $post->post_title; |
|
| 98 | + |
|
| 99 | + // Set the published and modified dates. |
|
| 100 | + /* |
|
| 101 | 101 | * Set the `datePublished` and `dateModified` using the local timezone. |
| 102 | 102 | * |
| 103 | 103 | * @see https://github.com/insideout10/wordlift-plugin/issues/887 |
| 104 | 104 | * |
| 105 | 105 | * @since 3.20.0 |
| 106 | 106 | */ |
| 107 | - try { |
|
| 108 | - $default_timezone = date_default_timezone_get(); |
|
| 109 | - $timezone = get_option( 'timezone_string' ); |
|
| 110 | - if ( ! empty( $timezone ) ) { |
|
| 111 | - date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 112 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 113 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 114 | - date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 115 | - } else { |
|
| 116 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 117 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 118 | - } |
|
| 119 | - } catch ( Exception $e ) { |
|
| 120 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 121 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - // Get the word count for the post. |
|
| 125 | - /* |
|
| 107 | + try { |
|
| 108 | + $default_timezone = date_default_timezone_get(); |
|
| 109 | + $timezone = get_option( 'timezone_string' ); |
|
| 110 | + if ( ! empty( $timezone ) ) { |
|
| 111 | + date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 112 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 113 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 114 | + date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 115 | + } else { |
|
| 116 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 117 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 118 | + } |
|
| 119 | + } catch ( Exception $e ) { |
|
| 120 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 121 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + // Get the word count for the post. |
|
| 125 | + /* |
|
| 126 | 126 | * Do not display the `wordCount` on a `WebPage`. |
| 127 | 127 | * |
| 128 | 128 | * @see https://github.com/insideout10/wordlift-plugin/issues/888 |
| 129 | 129 | * |
| 130 | 130 | * @since 3.20.0 |
| 131 | 131 | */ |
| 132 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 133 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 134 | - $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 135 | - } |
|
| 132 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 133 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 134 | + $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /* |
|
| 137 | + /* |
|
| 138 | 138 | * Add keywords, articleSection, commentCount and inLanguage properties to `Article` JSON-LD |
| 139 | 139 | * |
| 140 | 140 | * @see https://github.com/insideout10/wordlift-plugin/issues/1140 |
| 141 | 141 | * |
| 142 | 142 | * @since 3.27.2 |
| 143 | 143 | */ |
| 144 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 145 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 146 | - $keywords = $post_adapter->keywords(); |
|
| 147 | - $article_section = $post_adapter->article_section(); |
|
| 148 | - $comment_count = $post_adapter->comment_count(); |
|
| 149 | - $locale = $post_adapter->locale(); |
|
| 150 | - |
|
| 151 | - if ( isset( $keywords ) ) { |
|
| 152 | - $jsonld['keywords'] = $keywords; |
|
| 153 | - } |
|
| 154 | - if ( ! empty( $article_section ) ) { |
|
| 155 | - $jsonld['articleSection'] = $article_section; |
|
| 156 | - } |
|
| 157 | - $jsonld['commentCount'] = $comment_count; |
|
| 158 | - $jsonld['inLanguage'] = $locale; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Set the publisher. |
|
| 162 | - $this->set_publisher( $jsonld ); |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Call the `wl_post_jsonld_author` filter. |
|
| 166 | - * |
|
| 167 | - * This filter checks if there are co-authors or a single author and |
|
| 168 | - * returns a JSON-LD fragment for the author(s). |
|
| 169 | - * |
|
| 170 | - * @param array $value { |
|
| 171 | - * |
|
| 172 | - * @type array $jsonld The JSON-LD structure. |
|
| 173 | - * @type int[] $references An array of post IDs. |
|
| 174 | - * } |
|
| 175 | - * |
|
| 176 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 177 | - * |
|
| 178 | - * @since 3.51.4 |
|
| 179 | - * |
|
| 180 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 181 | - */ |
|
| 182 | - $ret_val = apply_filters( |
|
| 183 | - 'wl_jsonld_author', |
|
| 184 | - array( |
|
| 185 | - 'author' => $this->get_author( $post->post_author, $references ), |
|
| 186 | - 'references' => $references, |
|
| 187 | - ), |
|
| 188 | - $post_id |
|
| 189 | - ); |
|
| 190 | - |
|
| 191 | - // Set the values returned by the author filter. |
|
| 192 | - /* |
|
| 144 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 145 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 146 | + $keywords = $post_adapter->keywords(); |
|
| 147 | + $article_section = $post_adapter->article_section(); |
|
| 148 | + $comment_count = $post_adapter->comment_count(); |
|
| 149 | + $locale = $post_adapter->locale(); |
|
| 150 | + |
|
| 151 | + if ( isset( $keywords ) ) { |
|
| 152 | + $jsonld['keywords'] = $keywords; |
|
| 153 | + } |
|
| 154 | + if ( ! empty( $article_section ) ) { |
|
| 155 | + $jsonld['articleSection'] = $article_section; |
|
| 156 | + } |
|
| 157 | + $jsonld['commentCount'] = $comment_count; |
|
| 158 | + $jsonld['inLanguage'] = $locale; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Set the publisher. |
|
| 162 | + $this->set_publisher( $jsonld ); |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Call the `wl_post_jsonld_author` filter. |
|
| 166 | + * |
|
| 167 | + * This filter checks if there are co-authors or a single author and |
|
| 168 | + * returns a JSON-LD fragment for the author(s). |
|
| 169 | + * |
|
| 170 | + * @param array $value { |
|
| 171 | + * |
|
| 172 | + * @type array $jsonld The JSON-LD structure. |
|
| 173 | + * @type int[] $references An array of post IDs. |
|
| 174 | + * } |
|
| 175 | + * |
|
| 176 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 177 | + * |
|
| 178 | + * @since 3.51.4 |
|
| 179 | + * |
|
| 180 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 181 | + */ |
|
| 182 | + $ret_val = apply_filters( |
|
| 183 | + 'wl_jsonld_author', |
|
| 184 | + array( |
|
| 185 | + 'author' => $this->get_author( $post->post_author, $references ), |
|
| 186 | + 'references' => $references, |
|
| 187 | + ), |
|
| 188 | + $post_id |
|
| 189 | + ); |
|
| 190 | + |
|
| 191 | + // Set the values returned by the author filter. |
|
| 192 | + /* |
|
| 193 | 193 | * Do not add the author JSON-LD if an invalid author was referenced in a post. |
| 194 | 194 | * |
| 195 | 195 | * @see https://github.com/insideout10/wordlift-plugin/issues/1728 |
| 196 | 196 | * |
| 197 | 197 | * @since 3.53.2 |
| 198 | 198 | */ |
| 199 | - if ( ! empty( $ret_val['author'] ) ) { |
|
| 200 | - $jsonld['author'] = $ret_val['author']; |
|
| 201 | - $references = $ret_val['references']; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - // Return the JSON-LD if filters are disabled by the client. |
|
| 205 | - if ( $this->disable_convert_filters ) { |
|
| 206 | - return $jsonld; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 211 | - * |
|
| 212 | - * @param array $value { |
|
| 213 | - * |
|
| 214 | - * @type array $jsonld The JSON-LD structure. |
|
| 215 | - * @type int[] $references An array of post IDs. |
|
| 216 | - * @type Relations $relations A set of `Relation`s. |
|
| 217 | - * } |
|
| 218 | - * @since 3.25.0 |
|
| 219 | - * @since 3.43.0 The filter provides a `Relations` instance. |
|
| 220 | - * |
|
| 221 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 222 | - * |
|
| 223 | - * @api |
|
| 224 | - */ |
|
| 225 | - $ret_val = apply_filters( |
|
| 226 | - 'wl_post_jsonld_array', |
|
| 227 | - array( |
|
| 228 | - 'jsonld' => $jsonld, |
|
| 229 | - 'references' => $references, // This one is only an array of post IDs. |
|
| 230 | - 'references_infos' => $references_infos, |
|
| 231 | - 'relations' => $relations, |
|
| 232 | - ), |
|
| 233 | - $post_id |
|
| 234 | - ); |
|
| 235 | - |
|
| 236 | - $jsonld = $ret_val['jsonld']; |
|
| 237 | - $references = $ret_val['references']; |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Call the `wl_post_jsonld` filter. |
|
| 241 | - * |
|
| 242 | - * @param array $jsonld The JSON-LD structure. |
|
| 243 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 244 | - * @param array $references The array of referenced entities. |
|
| 245 | - * |
|
| 246 | - * @since 3.14.0 |
|
| 247 | - * |
|
| 248 | - * @api |
|
| 249 | - */ |
|
| 250 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Get the author's JSON-LD fragment. |
|
| 255 | - * |
|
| 256 | - * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 257 | - * the referenced entity if configured for the {@link WP_User}. |
|
| 258 | - * |
|
| 259 | - * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 260 | - * @param array $references An array of referenced entities. |
|
| 261 | - * |
|
| 262 | - * @return string|array A JSON-LD structure. |
|
| 263 | - * @since 3.14.0 |
|
| 264 | - */ |
|
| 265 | - public function get_author( $author_id, &$references ) { |
|
| 266 | - |
|
| 267 | - // Get the entity bound to this user. |
|
| 268 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 269 | - |
|
| 270 | - if ( ! empty( $entity_id ) && 'publish' === get_post_status( $entity_id ) ) { |
|
| 271 | - // Add the author to the references. |
|
| 272 | - $author_uri = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id ); |
|
| 273 | - $references[] = $entity_id; |
|
| 274 | - |
|
| 275 | - // Return the JSON-LD for the referenced entity. |
|
| 276 | - return array( |
|
| 277 | - '@id' => $author_uri, |
|
| 278 | - ); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // If there's no entity bound return a simple author structure. |
|
| 282 | - if ( false !== get_userdata( $author_id ) ) { |
|
| 283 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 284 | - $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 285 | - $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 286 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 287 | - |
|
| 288 | - return array( |
|
| 289 | - '@type' => 'Person', |
|
| 290 | - '@id' => $author_uri, |
|
| 291 | - 'name' => $author, |
|
| 292 | - 'givenName' => $author_first_name, |
|
| 293 | - 'familyName' => $author_last_name, |
|
| 294 | - 'url' => get_author_posts_url( $author_id ), |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // No valid entity or author so return empty array |
|
| 299 | - return array(); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Enrich the provided params array with publisher data, if available. |
|
| 304 | - * |
|
| 305 | - * @param array $params The parameters array. |
|
| 306 | - * |
|
| 307 | - * @since 3.10.0 |
|
| 308 | - */ |
|
| 309 | - protected function set_publisher( &$params ) { |
|
| 310 | - |
|
| 311 | - // If the publisher id isn't set don't do anything. |
|
| 312 | - $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
| 313 | - if ( empty( $publisher_id ) ) { |
|
| 314 | - return; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // Get the post instance. |
|
| 318 | - $post = get_post( $publisher_id ); |
|
| 319 | - if ( ! is_a( $post, '\WP_Post' ) ) { |
|
| 320 | - // Publisher not found. |
|
| 321 | - return; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - // Get the item id. |
|
| 325 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id ); |
|
| 326 | - |
|
| 327 | - // Get the type. |
|
| 328 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 329 | - |
|
| 330 | - // Get the name. |
|
| 331 | - $name = $post->post_title; |
|
| 332 | - |
|
| 333 | - // Set the publisher data. |
|
| 334 | - $params['publisher'] = array( |
|
| 335 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 336 | - '@id' => $id, |
|
| 337 | - 'name' => $name, |
|
| 338 | - ); |
|
| 339 | - |
|
| 340 | - // Add the sameAs values associated with the publisher. |
|
| 341 | - $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 342 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 343 | - if ( ! empty( $sameas ) ) { |
|
| 344 | - $params['publisher']['sameAs'] = $sameas; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 348 | - // support the logo property. |
|
| 349 | - // |
|
| 350 | - // See http://schema.org/logo. |
|
| 351 | - if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 352 | - return; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - // Get the publisher logo. |
|
| 356 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 357 | - |
|
| 358 | - // Bail out if the publisher logo isn't set. |
|
| 359 | - if ( false === $publisher_logo ) { |
|
| 360 | - return; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // Copy over some useful properties. |
|
| 364 | - // |
|
| 365 | - // See https://developers.google.com/search/docs/data-types/articles. |
|
| 366 | - $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 367 | - $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 368 | - |
|
| 369 | - // If you specify a "width" or "height" value you should leave out |
|
| 370 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 371 | - // |
|
| 372 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 373 | - $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 374 | - $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 375 | - |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Get the publisher logo structure. |
|
| 380 | - * |
|
| 381 | - * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 382 | - * - the post has no featured image. |
|
| 383 | - * - the featured image has no file. |
|
| 384 | - * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 385 | - * |
|
| 386 | - * @param int $post_id The post id. |
|
| 387 | - * |
|
| 388 | - * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 389 | - * of errors. |
|
| 390 | - * @since 3.19.2 |
|
| 391 | - * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 392 | - */ |
|
| 393 | - private function get_publisher_logo( $post_id ) { |
|
| 394 | - |
|
| 395 | - // Get the featured image for the post. |
|
| 396 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 397 | - |
|
| 398 | - // Bail out if thumbnail not available. |
|
| 399 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 400 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 401 | - |
|
| 402 | - return false; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - // Get the uploads base URL. |
|
| 406 | - $uploads_dir = wp_upload_dir(); |
|
| 407 | - |
|
| 408 | - // Get the attachment metadata. |
|
| 409 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 410 | - |
|
| 411 | - // Bail out if the file isn't set. |
|
| 412 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 413 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 414 | - |
|
| 415 | - return false; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 419 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 420 | - |
|
| 421 | - // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
|
| 422 | - if ( ! file_exists( $path ) ) { |
|
| 423 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 424 | - |
|
| 425 | - $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 426 | - if ( $attachment_image_src ) { |
|
| 427 | - return array( |
|
| 428 | - 'url' => $attachment_image_src[0], |
|
| 429 | - 'width' => $attachment_image_src[1], |
|
| 430 | - 'height' => $attachment_image_src[2], |
|
| 431 | - ); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - // Bail out if we cant fetch wp_get_attachment_image_src |
|
| 435 | - return false; |
|
| 436 | - |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 440 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 441 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 442 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 443 | - |
|
| 444 | - return false; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 448 | - // and we don't actually know the end values. |
|
| 449 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 450 | - |
|
| 451 | - // If the file doesn't exist yet, create it. |
|
| 452 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 453 | - $original_file_editor->resize( 600, 60 ); |
|
| 454 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 458 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 459 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 460 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 461 | - |
|
| 462 | - return false; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - // Get the actual size. |
|
| 466 | - $size = $publisher_logo_editor->get_size(); |
|
| 467 | - |
|
| 468 | - // Finally return the array with data. |
|
| 469 | - return array( |
|
| 470 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 471 | - 'width' => $size['width'], |
|
| 472 | - 'height' => $size['height'], |
|
| 473 | - ); |
|
| 474 | - } |
|
| 199 | + if ( ! empty( $ret_val['author'] ) ) { |
|
| 200 | + $jsonld['author'] = $ret_val['author']; |
|
| 201 | + $references = $ret_val['references']; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + // Return the JSON-LD if filters are disabled by the client. |
|
| 205 | + if ( $this->disable_convert_filters ) { |
|
| 206 | + return $jsonld; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 211 | + * |
|
| 212 | + * @param array $value { |
|
| 213 | + * |
|
| 214 | + * @type array $jsonld The JSON-LD structure. |
|
| 215 | + * @type int[] $references An array of post IDs. |
|
| 216 | + * @type Relations $relations A set of `Relation`s. |
|
| 217 | + * } |
|
| 218 | + * @since 3.25.0 |
|
| 219 | + * @since 3.43.0 The filter provides a `Relations` instance. |
|
| 220 | + * |
|
| 221 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 222 | + * |
|
| 223 | + * @api |
|
| 224 | + */ |
|
| 225 | + $ret_val = apply_filters( |
|
| 226 | + 'wl_post_jsonld_array', |
|
| 227 | + array( |
|
| 228 | + 'jsonld' => $jsonld, |
|
| 229 | + 'references' => $references, // This one is only an array of post IDs. |
|
| 230 | + 'references_infos' => $references_infos, |
|
| 231 | + 'relations' => $relations, |
|
| 232 | + ), |
|
| 233 | + $post_id |
|
| 234 | + ); |
|
| 235 | + |
|
| 236 | + $jsonld = $ret_val['jsonld']; |
|
| 237 | + $references = $ret_val['references']; |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Call the `wl_post_jsonld` filter. |
|
| 241 | + * |
|
| 242 | + * @param array $jsonld The JSON-LD structure. |
|
| 243 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 244 | + * @param array $references The array of referenced entities. |
|
| 245 | + * |
|
| 246 | + * @since 3.14.0 |
|
| 247 | + * |
|
| 248 | + * @api |
|
| 249 | + */ |
|
| 250 | + return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Get the author's JSON-LD fragment. |
|
| 255 | + * |
|
| 256 | + * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 257 | + * the referenced entity if configured for the {@link WP_User}. |
|
| 258 | + * |
|
| 259 | + * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 260 | + * @param array $references An array of referenced entities. |
|
| 261 | + * |
|
| 262 | + * @return string|array A JSON-LD structure. |
|
| 263 | + * @since 3.14.0 |
|
| 264 | + */ |
|
| 265 | + public function get_author( $author_id, &$references ) { |
|
| 266 | + |
|
| 267 | + // Get the entity bound to this user. |
|
| 268 | + $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 269 | + |
|
| 270 | + if ( ! empty( $entity_id ) && 'publish' === get_post_status( $entity_id ) ) { |
|
| 271 | + // Add the author to the references. |
|
| 272 | + $author_uri = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id ); |
|
| 273 | + $references[] = $entity_id; |
|
| 274 | + |
|
| 275 | + // Return the JSON-LD for the referenced entity. |
|
| 276 | + return array( |
|
| 277 | + '@id' => $author_uri, |
|
| 278 | + ); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // If there's no entity bound return a simple author structure. |
|
| 282 | + if ( false !== get_userdata( $author_id ) ) { |
|
| 283 | + $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 284 | + $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 285 | + $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 286 | + $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 287 | + |
|
| 288 | + return array( |
|
| 289 | + '@type' => 'Person', |
|
| 290 | + '@id' => $author_uri, |
|
| 291 | + 'name' => $author, |
|
| 292 | + 'givenName' => $author_first_name, |
|
| 293 | + 'familyName' => $author_last_name, |
|
| 294 | + 'url' => get_author_posts_url( $author_id ), |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // No valid entity or author so return empty array |
|
| 299 | + return array(); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Enrich the provided params array with publisher data, if available. |
|
| 304 | + * |
|
| 305 | + * @param array $params The parameters array. |
|
| 306 | + * |
|
| 307 | + * @since 3.10.0 |
|
| 308 | + */ |
|
| 309 | + protected function set_publisher( &$params ) { |
|
| 310 | + |
|
| 311 | + // If the publisher id isn't set don't do anything. |
|
| 312 | + $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
| 313 | + if ( empty( $publisher_id ) ) { |
|
| 314 | + return; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // Get the post instance. |
|
| 318 | + $post = get_post( $publisher_id ); |
|
| 319 | + if ( ! is_a( $post, '\WP_Post' ) ) { |
|
| 320 | + // Publisher not found. |
|
| 321 | + return; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + // Get the item id. |
|
| 325 | + $id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id ); |
|
| 326 | + |
|
| 327 | + // Get the type. |
|
| 328 | + $type = $this->entity_type_service->get( $publisher_id ); |
|
| 329 | + |
|
| 330 | + // Get the name. |
|
| 331 | + $name = $post->post_title; |
|
| 332 | + |
|
| 333 | + // Set the publisher data. |
|
| 334 | + $params['publisher'] = array( |
|
| 335 | + '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 336 | + '@id' => $id, |
|
| 337 | + 'name' => $name, |
|
| 338 | + ); |
|
| 339 | + |
|
| 340 | + // Add the sameAs values associated with the publisher. |
|
| 341 | + $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 342 | + $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 343 | + if ( ! empty( $sameas ) ) { |
|
| 344 | + $params['publisher']['sameAs'] = $sameas; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 348 | + // support the logo property. |
|
| 349 | + // |
|
| 350 | + // See http://schema.org/logo. |
|
| 351 | + if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 352 | + return; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + // Get the publisher logo. |
|
| 356 | + $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 357 | + |
|
| 358 | + // Bail out if the publisher logo isn't set. |
|
| 359 | + if ( false === $publisher_logo ) { |
|
| 360 | + return; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // Copy over some useful properties. |
|
| 364 | + // |
|
| 365 | + // See https://developers.google.com/search/docs/data-types/articles. |
|
| 366 | + $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 367 | + $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 368 | + |
|
| 369 | + // If you specify a "width" or "height" value you should leave out |
|
| 370 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 371 | + // |
|
| 372 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 373 | + $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 374 | + $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 375 | + |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Get the publisher logo structure. |
|
| 380 | + * |
|
| 381 | + * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 382 | + * - the post has no featured image. |
|
| 383 | + * - the featured image has no file. |
|
| 384 | + * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 385 | + * |
|
| 386 | + * @param int $post_id The post id. |
|
| 387 | + * |
|
| 388 | + * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 389 | + * of errors. |
|
| 390 | + * @since 3.19.2 |
|
| 391 | + * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 392 | + */ |
|
| 393 | + private function get_publisher_logo( $post_id ) { |
|
| 394 | + |
|
| 395 | + // Get the featured image for the post. |
|
| 396 | + $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 397 | + |
|
| 398 | + // Bail out if thumbnail not available. |
|
| 399 | + if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 400 | + $this->log->info( "Featured image not set for post $post_id." ); |
|
| 401 | + |
|
| 402 | + return false; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + // Get the uploads base URL. |
|
| 406 | + $uploads_dir = wp_upload_dir(); |
|
| 407 | + |
|
| 408 | + // Get the attachment metadata. |
|
| 409 | + $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 410 | + |
|
| 411 | + // Bail out if the file isn't set. |
|
| 412 | + if ( ! isset( $metadata['file'] ) ) { |
|
| 413 | + $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 414 | + |
|
| 415 | + return false; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 419 | + $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 420 | + |
|
| 421 | + // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
|
| 422 | + if ( ! file_exists( $path ) ) { |
|
| 423 | + $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 424 | + |
|
| 425 | + $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 426 | + if ( $attachment_image_src ) { |
|
| 427 | + return array( |
|
| 428 | + 'url' => $attachment_image_src[0], |
|
| 429 | + 'width' => $attachment_image_src[1], |
|
| 430 | + 'height' => $attachment_image_src[2], |
|
| 431 | + ); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + // Bail out if we cant fetch wp_get_attachment_image_src |
|
| 435 | + return false; |
|
| 436 | + |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 440 | + $original_file_editor = wp_get_image_editor( $path ); |
|
| 441 | + if ( is_wp_error( $original_file_editor ) ) { |
|
| 442 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 443 | + |
|
| 444 | + return false; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 448 | + // and we don't actually know the end values. |
|
| 449 | + $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 450 | + |
|
| 451 | + // If the file doesn't exist yet, create it. |
|
| 452 | + if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 453 | + $original_file_editor->resize( 600, 60 ); |
|
| 454 | + $original_file_editor->save( $publisher_logo_path ); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 458 | + $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 459 | + if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 460 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 461 | + |
|
| 462 | + return false; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + // Get the actual size. |
|
| 466 | + $size = $publisher_logo_editor->get_size(); |
|
| 467 | + |
|
| 468 | + // Finally return the array with data. |
|
| 469 | + return array( |
|
| 470 | + 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 471 | + 'width' => $size['width'], |
|
| 472 | + 'height' => $size['height'], |
|
| 473 | + ); |
|
| 474 | + } |
|
| 475 | 475 | |
| 476 | 476 | } |
@@ -46,11 +46,11 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @since 3.10.0 |
| 48 | 48 | */ |
| 49 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false ) { |
|
| 50 | - parent::__construct( $entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create() ); |
|
| 49 | + public function __construct($entity_type_service, $user_service, $attachment_service, $disable_convert_filters = false) { |
|
| 50 | + parent::__construct($entity_type_service, $user_service, $attachment_service, Wordlift_Property_Getter_Factory::create()); |
|
| 51 | 51 | $this->disable_convert_filters = $disable_convert_filters; |
| 52 | 52 | // Set a reference to the logger. |
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter'); |
|
| 54 | 54 | |
| 55 | 55 | self::$instance = $this; |
| 56 | 56 | |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | public function new_instance_with_filters_disabled() { |
| 65 | - return new static( $this->entity_type_service, $this->user_service, $this->attachment_service, true ); |
|
| 65 | + return new static($this->entity_type_service, $this->user_service, $this->attachment_service, true); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -76,20 +76,20 @@ discard block |
||
| 76 | 76 | * @return array A JSON-LD array. |
| 77 | 77 | * @since 3.10.0 |
| 78 | 78 | */ |
| 79 | - public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) { |
|
| 79 | + public function convert($post_id, &$references = array(), &$references_infos = array(), $relations = null) { |
|
| 80 | 80 | |
| 81 | 81 | // Get the post instance. |
| 82 | - $post = get_post( $post_id ); |
|
| 83 | - if ( null === $post ) { |
|
| 82 | + $post = get_post($post_id); |
|
| 83 | + if (null === $post) { |
|
| 84 | 84 | // Post not found. |
| 85 | 85 | return null; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 89 | - $jsonld = parent::convert( $post_id, $references, $references_infos, $relations ); |
|
| 89 | + $jsonld = parent::convert($post_id, $references, $references_infos, $relations); |
|
| 90 | 90 | |
| 91 | 91 | // Set WebPage by default. |
| 92 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 92 | + if (empty($jsonld['@type'])) { |
|
| 93 | 93 | $jsonld['@type'] = 'WebPage'; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -106,19 +106,19 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | try { |
| 108 | 108 | $default_timezone = date_default_timezone_get(); |
| 109 | - $timezone = get_option( 'timezone_string' ); |
|
| 110 | - if ( ! empty( $timezone ) ) { |
|
| 111 | - date_default_timezone_set( $timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 112 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 113 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 114 | - date_default_timezone_set( $default_timezone ); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 109 | + $timezone = get_option('timezone_string'); |
|
| 110 | + if ( ! empty($timezone)) { |
|
| 111 | + date_default_timezone_set($timezone); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 112 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post); |
|
| 113 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i:sP', false, $post); |
|
| 114 | + date_default_timezone_set($default_timezone); //phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set |
|
| 115 | 115 | } else { |
| 116 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 117 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 116 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 117 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 118 | 118 | } |
| 119 | - } catch ( Exception $e ) { |
|
| 120 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 121 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 119 | + } catch (Exception $e) { |
|
| 120 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 121 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | // Get the word count for the post. |
@@ -129,8 +129,8 @@ discard block |
||
| 129 | 129 | * |
| 130 | 130 | * @since 3.20.0 |
| 131 | 131 | */ |
| 132 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 133 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 132 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 133 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 134 | 134 | $jsonld['wordCount'] = $post_adapter->word_count(); |
| 135 | 135 | } |
| 136 | 136 | |
@@ -141,17 +141,17 @@ discard block |
||
| 141 | 141 | * |
| 142 | 142 | * @since 3.27.2 |
| 143 | 143 | */ |
| 144 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 145 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 144 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 145 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 146 | 146 | $keywords = $post_adapter->keywords(); |
| 147 | 147 | $article_section = $post_adapter->article_section(); |
| 148 | 148 | $comment_count = $post_adapter->comment_count(); |
| 149 | 149 | $locale = $post_adapter->locale(); |
| 150 | 150 | |
| 151 | - if ( isset( $keywords ) ) { |
|
| 151 | + if (isset($keywords)) { |
|
| 152 | 152 | $jsonld['keywords'] = $keywords; |
| 153 | 153 | } |
| 154 | - if ( ! empty( $article_section ) ) { |
|
| 154 | + if ( ! empty($article_section)) { |
|
| 155 | 155 | $jsonld['articleSection'] = $article_section; |
| 156 | 156 | } |
| 157 | 157 | $jsonld['commentCount'] = $comment_count; |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | // Set the publisher. |
| 162 | - $this->set_publisher( $jsonld ); |
|
| 162 | + $this->set_publisher($jsonld); |
|
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | 165 | * Call the `wl_post_jsonld_author` filter. |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | $ret_val = apply_filters( |
| 183 | 183 | 'wl_jsonld_author', |
| 184 | 184 | array( |
| 185 | - 'author' => $this->get_author( $post->post_author, $references ), |
|
| 185 | + 'author' => $this->get_author($post->post_author, $references), |
|
| 186 | 186 | 'references' => $references, |
| 187 | 187 | ), |
| 188 | 188 | $post_id |
@@ -196,13 +196,13 @@ discard block |
||
| 196 | 196 | * |
| 197 | 197 | * @since 3.53.2 |
| 198 | 198 | */ |
| 199 | - if ( ! empty( $ret_val['author'] ) ) { |
|
| 199 | + if ( ! empty($ret_val['author'])) { |
|
| 200 | 200 | $jsonld['author'] = $ret_val['author']; |
| 201 | 201 | $references = $ret_val['references']; |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | // Return the JSON-LD if filters are disabled by the client. |
| 205 | - if ( $this->disable_convert_filters ) { |
|
| 205 | + if ($this->disable_convert_filters) { |
|
| 206 | 206 | return $jsonld; |
| 207 | 207 | } |
| 208 | 208 | |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | * |
| 248 | 248 | * @api |
| 249 | 249 | */ |
| 250 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 250 | + return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references); |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | /** |
@@ -262,14 +262,14 @@ discard block |
||
| 262 | 262 | * @return string|array A JSON-LD structure. |
| 263 | 263 | * @since 3.14.0 |
| 264 | 264 | */ |
| 265 | - public function get_author( $author_id, &$references ) { |
|
| 265 | + public function get_author($author_id, &$references) { |
|
| 266 | 266 | |
| 267 | 267 | // Get the entity bound to this user. |
| 268 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 268 | + $entity_id = $this->user_service->get_entity($author_id); |
|
| 269 | 269 | |
| 270 | - if ( ! empty( $entity_id ) && 'publish' === get_post_status( $entity_id ) ) { |
|
| 270 | + if ( ! empty($entity_id) && 'publish' === get_post_status($entity_id)) { |
|
| 271 | 271 | // Add the author to the references. |
| 272 | - $author_uri = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id ); |
|
| 272 | + $author_uri = Wordlift_Entity_Service::get_instance()->get_uri($entity_id); |
|
| 273 | 273 | $references[] = $entity_id; |
| 274 | 274 | |
| 275 | 275 | // Return the JSON-LD for the referenced entity. |
@@ -279,11 +279,11 @@ discard block |
||
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | // If there's no entity bound return a simple author structure. |
| 282 | - if ( false !== get_userdata( $author_id ) ) { |
|
| 283 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 284 | - $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 285 | - $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 286 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 282 | + if (false !== get_userdata($author_id)) { |
|
| 283 | + $author = get_the_author_meta('display_name', $author_id); |
|
| 284 | + $author_first_name = get_the_author_meta('first_name', $author_id); |
|
| 285 | + $author_last_name = get_the_author_meta('last_name', $author_id); |
|
| 286 | + $author_uri = $this->user_service->get_uri($author_id); |
|
| 287 | 287 | |
| 288 | 288 | return array( |
| 289 | 289 | '@type' => 'Person', |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | 'name' => $author, |
| 292 | 292 | 'givenName' => $author_first_name, |
| 293 | 293 | 'familyName' => $author_last_name, |
| 294 | - 'url' => get_author_posts_url( $author_id ), |
|
| 294 | + 'url' => get_author_posts_url($author_id), |
|
| 295 | 295 | ); |
| 296 | 296 | } |
| 297 | 297 | |
@@ -306,41 +306,41 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @since 3.10.0 |
| 308 | 308 | */ |
| 309 | - protected function set_publisher( &$params ) { |
|
| 309 | + protected function set_publisher(&$params) { |
|
| 310 | 310 | |
| 311 | 311 | // If the publisher id isn't set don't do anything. |
| 312 | 312 | $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
| 313 | - if ( empty( $publisher_id ) ) { |
|
| 313 | + if (empty($publisher_id)) { |
|
| 314 | 314 | return; |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | // Get the post instance. |
| 318 | - $post = get_post( $publisher_id ); |
|
| 319 | - if ( ! is_a( $post, '\WP_Post' ) ) { |
|
| 318 | + $post = get_post($publisher_id); |
|
| 319 | + if ( ! is_a($post, '\WP_Post')) { |
|
| 320 | 320 | // Publisher not found. |
| 321 | 321 | return; |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | // Get the item id. |
| 325 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $publisher_id ); |
|
| 325 | + $id = Wordlift_Entity_Service::get_instance()->get_uri($publisher_id); |
|
| 326 | 326 | |
| 327 | 327 | // Get the type. |
| 328 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 328 | + $type = $this->entity_type_service->get($publisher_id); |
|
| 329 | 329 | |
| 330 | 330 | // Get the name. |
| 331 | 331 | $name = $post->post_title; |
| 332 | 332 | |
| 333 | 333 | // Set the publisher data. |
| 334 | 334 | $params['publisher'] = array( |
| 335 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 335 | + '@type' => $this->relative_to_context($type['uri']), |
|
| 336 | 336 | '@id' => $id, |
| 337 | 337 | 'name' => $name, |
| 338 | 338 | ); |
| 339 | 339 | |
| 340 | 340 | // Add the sameAs values associated with the publisher. |
| 341 | 341 | $storage_factory = Wordlift_Storage_Factory::get_instance(); |
| 342 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 343 | - if ( ! empty( $sameas ) ) { |
|
| 342 | + $sameas = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id); |
|
| 343 | + if ( ! empty($sameas)) { |
|
| 344 | 344 | $params['publisher']['sameAs'] = $sameas; |
| 345 | 345 | } |
| 346 | 346 | |
@@ -348,15 +348,15 @@ discard block |
||
| 348 | 348 | // support the logo property. |
| 349 | 349 | // |
| 350 | 350 | // See http://schema.org/logo. |
| 351 | - if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 351 | + if (1 !== preg_match('~Organization$~', $type['uri'])) { |
|
| 352 | 352 | return; |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | // Get the publisher logo. |
| 356 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 356 | + $publisher_logo = $this->get_publisher_logo($post->ID); |
|
| 357 | 357 | |
| 358 | 358 | // Bail out if the publisher logo isn't set. |
| 359 | - if ( false === $publisher_logo ) { |
|
| 359 | + if (false === $publisher_logo) { |
|
| 360 | 360 | return; |
| 361 | 361 | } |
| 362 | 362 | |
@@ -390,14 +390,14 @@ discard block |
||
| 390 | 390 | * @since 3.19.2 |
| 391 | 391 | * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
| 392 | 392 | */ |
| 393 | - private function get_publisher_logo( $post_id ) { |
|
| 393 | + private function get_publisher_logo($post_id) { |
|
| 394 | 394 | |
| 395 | 395 | // Get the featured image for the post. |
| 396 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 396 | + $thumbnail_id = get_post_thumbnail_id($post_id); |
|
| 397 | 397 | |
| 398 | 398 | // Bail out if thumbnail not available. |
| 399 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 400 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 399 | + if (empty($thumbnail_id) || 0 === $thumbnail_id) { |
|
| 400 | + $this->log->info("Featured image not set for post $post_id."); |
|
| 401 | 401 | |
| 402 | 402 | return false; |
| 403 | 403 | } |
@@ -406,24 +406,24 @@ discard block |
||
| 406 | 406 | $uploads_dir = wp_upload_dir(); |
| 407 | 407 | |
| 408 | 408 | // Get the attachment metadata. |
| 409 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 409 | + $metadata = wp_get_attachment_metadata($thumbnail_id); |
|
| 410 | 410 | |
| 411 | 411 | // Bail out if the file isn't set. |
| 412 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 413 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 412 | + if ( ! isset($metadata['file'])) { |
|
| 413 | + $this->log->warn("Featured image file not found for post $post_id."); |
|
| 414 | 414 | |
| 415 | 415 | return false; |
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
| 419 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 419 | + $path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file']; |
|
| 420 | 420 | |
| 421 | 421 | // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
| 422 | - if ( ! file_exists( $path ) ) { |
|
| 423 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 422 | + if ( ! file_exists($path)) { |
|
| 423 | + $this->log->warn("Featured image file $path doesn't exist for post $post_id."); |
|
| 424 | 424 | |
| 425 | - $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 426 | - if ( $attachment_image_src ) { |
|
| 425 | + $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, ''); |
|
| 426 | + if ($attachment_image_src) { |
|
| 427 | 427 | return array( |
| 428 | 428 | 'url' => $attachment_image_src[0], |
| 429 | 429 | 'width' => $attachment_image_src[1], |
@@ -437,27 +437,27 @@ discard block |
||
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 440 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 441 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 442 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 440 | + $original_file_editor = wp_get_image_editor($path); |
|
| 441 | + if (is_wp_error($original_file_editor)) { |
|
| 442 | + $this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id."); |
|
| 443 | 443 | |
| 444 | 444 | return false; |
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
| 448 | 448 | // and we don't actually know the end values. |
| 449 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 449 | + $publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo'); |
|
| 450 | 450 | |
| 451 | 451 | // If the file doesn't exist yet, create it. |
| 452 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 453 | - $original_file_editor->resize( 600, 60 ); |
|
| 454 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 452 | + if ( ! file_exists($publisher_logo_path)) { |
|
| 453 | + $original_file_editor->resize(600, 60); |
|
| 454 | + $original_file_editor->save($publisher_logo_path); |
|
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 458 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 459 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 460 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 458 | + $publisher_logo_editor = wp_get_image_editor($publisher_logo_path); |
|
| 459 | + if (is_wp_error($publisher_logo_editor)) { |
|
| 460 | + $this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id."); |
|
| 461 | 461 | |
| 462 | 462 | return false; |
| 463 | 463 | } |
@@ -467,7 +467,7 @@ discard block |
||
| 467 | 467 | |
| 468 | 468 | // Finally return the array with data. |
| 469 | 469 | return array( |
| 470 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 470 | + 'url' => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])), |
|
| 471 | 471 | 'width' => $size['width'], |
| 472 | 472 | 'height' => $size['height'], |
| 473 | 473 | ); |