@@ -7,198 +7,198 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Jsonld_Article_Wrapper { |
| 9 | 9 | |
| 10 | - public static $article_types = array( |
|
| 11 | - 'Article', |
|
| 12 | - 'AdvertiserContentArticle', |
|
| 13 | - 'NewsArticle', |
|
| 14 | - 'AnalysisNewsArticle', |
|
| 15 | - 'AskPublicNewsArticle', |
|
| 16 | - 'BackgroundNewsArticle', |
|
| 17 | - 'OpinionNewsArticle', |
|
| 18 | - 'ReportageNewsArticle', |
|
| 19 | - 'ReviewNewsArticle', |
|
| 20 | - 'Report', |
|
| 21 | - 'SatiricalArticle', |
|
| 22 | - 'ScholarlyArticle', |
|
| 23 | - 'MedicalScholarlyArticle', |
|
| 24 | - 'SocialMediaPosting', |
|
| 25 | - 'BlogPosting', |
|
| 26 | - 'LiveBlogPosting', |
|
| 27 | - 'DiscussionForumPosting', |
|
| 28 | - 'TechArticle', |
|
| 29 | - 'APIReference', |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 34 | - */ |
|
| 35 | - private $post_to_jsonld_converter; |
|
| 36 | - /** |
|
| 37 | - * @var \Wordlift_Cached_Post_Converter |
|
| 38 | - */ |
|
| 39 | - private $cached_postid_to_jsonld_converter; |
|
| 40 | - /** |
|
| 41 | - * @var \Wordlift_Entity_Uri_Service |
|
| 42 | - */ |
|
| 43 | - private $entity_uri_service; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Jsonld_Article_Wrapper constructor. |
|
| 47 | - * |
|
| 48 | - * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 49 | - * @param $cached_postid_to_jsonld_converter |
|
| 50 | - */ |
|
| 51 | - public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 52 | - |
|
| 53 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 54 | - |
|
| 55 | - add_filter( |
|
| 56 | - 'wl_after_get_jsonld', |
|
| 57 | - array( |
|
| 58 | - $this, |
|
| 59 | - 'after_get_jsonld', |
|
| 60 | - ), |
|
| 61 | - PHP_INT_MAX - 100, |
|
| 62 | - 3 |
|
| 63 | - ); |
|
| 64 | - |
|
| 65 | - $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 66 | - |
|
| 67 | - $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 71 | - |
|
| 72 | - // Invalid data structure |
|
| 73 | - if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) { |
|
| 74 | - return $jsonld; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if ( Jsonld_Context_Enum::PAGE !== $context |
|
| 78 | - // Returns true for "1", "true", "on" and "yes". Returns false otherwise. |
|
| 79 | - && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) { |
|
| 80 | - return $jsonld; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Copy the 1st array element |
|
| 84 | - $post_jsonld = $jsonld[0]; |
|
| 85 | - |
|
| 86 | - // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 87 | - // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 88 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 89 | - return $jsonld; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $references = array(); |
|
| 93 | - $reference_infos = array(); |
|
| 94 | - |
|
| 95 | - // Convert the post as Article. |
|
| 96 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() ); |
|
| 97 | - |
|
| 98 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 99 | - // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 100 | - $article_jsonld['@type'] = 'Article'; |
|
| 101 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 102 | - |
|
| 103 | - // Copy over the URLs. |
|
| 104 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 105 | - $article_jsonld['url'] = $post_jsonld['url']; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 109 | - |
|
| 110 | - // Only add authors if the key is present. |
|
| 111 | - if ( isset( $article_jsonld['author'] ) ) { |
|
| 112 | - $this->add_author_data_if_missing( $jsonld, $article_jsonld['author'] ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $jsonld; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param array $jsonld |
|
| 120 | - * @param array|false|null $author Either a keyed or an indexed array. |
|
| 121 | - * |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - private function add_author_data_if_missing( &$jsonld, $author ) { |
|
| 125 | - |
|
| 126 | - // For each author when if the `@id` is present and if not, add the author data. |
|
| 127 | - $entities_ids = $this->get_list_of_entities_ids( $author ); |
|
| 128 | - foreach ( $entities_ids as $entity_id ) { |
|
| 129 | - if ( $this->is_author_entity_present_in_graph( $jsonld, $entity_id ) ) { |
|
| 130 | - continue; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $jsonld[] = $this->get_author_data_by_entity_id( $entity_id ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * This function takes any input parameter and when it's an array, it'll return the @id properties in this array as |
|
| 140 | - * an array of `@id`s. |
|
| 141 | - * |
|
| 142 | - * @param mixed $items Preferably an array, either keyed or indexed. |
|
| 143 | - * |
|
| 144 | - * @return array An array of `@id`s. Empty if not found. |
|
| 145 | - */ |
|
| 146 | - private function get_list_of_entities_ids( $items ) { |
|
| 147 | - // If this is not an array, then what are we doing here? we return an empty array. |
|
| 148 | - if ( ! is_array( $items ) || empty( $items ) ) { |
|
| 149 | - return array(); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // If this is a keyed array and the `@id` is set, then we return that single `@id`. |
|
| 153 | - if ( isset( $items['@id'] ) ) { |
|
| 154 | - return array( $items['@id'] ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // Poor way to check whether this is an indexed array. |
|
| 158 | - if ( ! isset( $items[0] ) ) { |
|
| 159 | - return array(); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - // If this is an indexed array then we return the `@id` for all the items in the array. |
|
| 163 | - return array_filter( array_column( $items, '@id' ) ); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - private function is_article( $schema_types ) { |
|
| 167 | - |
|
| 168 | - $array_intersect = array_intersect( self::$article_types, (array) $schema_types ); |
|
| 169 | - |
|
| 170 | - return ! empty( $array_intersect ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - private function get_author_data_by_entity_id( $author_linked_entity_id ) { |
|
| 174 | - |
|
| 175 | - $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 176 | - |
|
| 177 | - if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - $references = array(); |
|
| 182 | - $reference_infos = array(); |
|
| 183 | - |
|
| 184 | - return $this->cached_postid_to_jsonld_converter->convert( |
|
| 185 | - $author_entity_post->ID, |
|
| 186 | - $references, |
|
| 187 | - $reference_infos, |
|
| 188 | - new Relations() |
|
| 189 | - ); |
|
| 190 | - |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 194 | - |
|
| 195 | - foreach ( $jsonld as $item ) { |
|
| 196 | - if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 197 | - return true; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 10 | + public static $article_types = array( |
|
| 11 | + 'Article', |
|
| 12 | + 'AdvertiserContentArticle', |
|
| 13 | + 'NewsArticle', |
|
| 14 | + 'AnalysisNewsArticle', |
|
| 15 | + 'AskPublicNewsArticle', |
|
| 16 | + 'BackgroundNewsArticle', |
|
| 17 | + 'OpinionNewsArticle', |
|
| 18 | + 'ReportageNewsArticle', |
|
| 19 | + 'ReviewNewsArticle', |
|
| 20 | + 'Report', |
|
| 21 | + 'SatiricalArticle', |
|
| 22 | + 'ScholarlyArticle', |
|
| 23 | + 'MedicalScholarlyArticle', |
|
| 24 | + 'SocialMediaPosting', |
|
| 25 | + 'BlogPosting', |
|
| 26 | + 'LiveBlogPosting', |
|
| 27 | + 'DiscussionForumPosting', |
|
| 28 | + 'TechArticle', |
|
| 29 | + 'APIReference', |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 34 | + */ |
|
| 35 | + private $post_to_jsonld_converter; |
|
| 36 | + /** |
|
| 37 | + * @var \Wordlift_Cached_Post_Converter |
|
| 38 | + */ |
|
| 39 | + private $cached_postid_to_jsonld_converter; |
|
| 40 | + /** |
|
| 41 | + * @var \Wordlift_Entity_Uri_Service |
|
| 42 | + */ |
|
| 43 | + private $entity_uri_service; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Jsonld_Article_Wrapper constructor. |
|
| 47 | + * |
|
| 48 | + * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 49 | + * @param $cached_postid_to_jsonld_converter |
|
| 50 | + */ |
|
| 51 | + public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 52 | + |
|
| 53 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 54 | + |
|
| 55 | + add_filter( |
|
| 56 | + 'wl_after_get_jsonld', |
|
| 57 | + array( |
|
| 58 | + $this, |
|
| 59 | + 'after_get_jsonld', |
|
| 60 | + ), |
|
| 61 | + PHP_INT_MAX - 100, |
|
| 62 | + 3 |
|
| 63 | + ); |
|
| 64 | + |
|
| 65 | + $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 66 | + |
|
| 67 | + $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 71 | + |
|
| 72 | + // Invalid data structure |
|
| 73 | + if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) { |
|
| 74 | + return $jsonld; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if ( Jsonld_Context_Enum::PAGE !== $context |
|
| 78 | + // Returns true for "1", "true", "on" and "yes". Returns false otherwise. |
|
| 79 | + && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) { |
|
| 80 | + return $jsonld; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Copy the 1st array element |
|
| 84 | + $post_jsonld = $jsonld[0]; |
|
| 85 | + |
|
| 86 | + // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 87 | + // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 88 | + if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 89 | + return $jsonld; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $references = array(); |
|
| 93 | + $reference_infos = array(); |
|
| 94 | + |
|
| 95 | + // Convert the post as Article. |
|
| 96 | + $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() ); |
|
| 97 | + |
|
| 98 | + $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 99 | + // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 100 | + $article_jsonld['@type'] = 'Article'; |
|
| 101 | + $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 102 | + |
|
| 103 | + // Copy over the URLs. |
|
| 104 | + if ( isset( $post_jsonld['url'] ) ) { |
|
| 105 | + $article_jsonld['url'] = $post_jsonld['url']; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + array_unshift( $jsonld, $article_jsonld ); |
|
| 109 | + |
|
| 110 | + // Only add authors if the key is present. |
|
| 111 | + if ( isset( $article_jsonld['author'] ) ) { |
|
| 112 | + $this->add_author_data_if_missing( $jsonld, $article_jsonld['author'] ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return $jsonld; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param array $jsonld |
|
| 120 | + * @param array|false|null $author Either a keyed or an indexed array. |
|
| 121 | + * |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + private function add_author_data_if_missing( &$jsonld, $author ) { |
|
| 125 | + |
|
| 126 | + // For each author when if the `@id` is present and if not, add the author data. |
|
| 127 | + $entities_ids = $this->get_list_of_entities_ids( $author ); |
|
| 128 | + foreach ( $entities_ids as $entity_id ) { |
|
| 129 | + if ( $this->is_author_entity_present_in_graph( $jsonld, $entity_id ) ) { |
|
| 130 | + continue; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $jsonld[] = $this->get_author_data_by_entity_id( $entity_id ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * This function takes any input parameter and when it's an array, it'll return the @id properties in this array as |
|
| 140 | + * an array of `@id`s. |
|
| 141 | + * |
|
| 142 | + * @param mixed $items Preferably an array, either keyed or indexed. |
|
| 143 | + * |
|
| 144 | + * @return array An array of `@id`s. Empty if not found. |
|
| 145 | + */ |
|
| 146 | + private function get_list_of_entities_ids( $items ) { |
|
| 147 | + // If this is not an array, then what are we doing here? we return an empty array. |
|
| 148 | + if ( ! is_array( $items ) || empty( $items ) ) { |
|
| 149 | + return array(); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // If this is a keyed array and the `@id` is set, then we return that single `@id`. |
|
| 153 | + if ( isset( $items['@id'] ) ) { |
|
| 154 | + return array( $items['@id'] ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // Poor way to check whether this is an indexed array. |
|
| 158 | + if ( ! isset( $items[0] ) ) { |
|
| 159 | + return array(); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + // If this is an indexed array then we return the `@id` for all the items in the array. |
|
| 163 | + return array_filter( array_column( $items, '@id' ) ); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + private function is_article( $schema_types ) { |
|
| 167 | + |
|
| 168 | + $array_intersect = array_intersect( self::$article_types, (array) $schema_types ); |
|
| 169 | + |
|
| 170 | + return ! empty( $array_intersect ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + private function get_author_data_by_entity_id( $author_linked_entity_id ) { |
|
| 174 | + |
|
| 175 | + $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 176 | + |
|
| 177 | + if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + $references = array(); |
|
| 182 | + $reference_infos = array(); |
|
| 183 | + |
|
| 184 | + return $this->cached_postid_to_jsonld_converter->convert( |
|
| 185 | + $author_entity_post->ID, |
|
| 186 | + $references, |
|
| 187 | + $reference_infos, |
|
| 188 | + new Relations() |
|
| 189 | + ); |
|
| 190 | + |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 194 | + |
|
| 195 | + foreach ( $jsonld as $item ) { |
|
| 196 | + if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | 204 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
| 49 | 49 | * @param $cached_postid_to_jsonld_converter |
| 50 | 50 | */ |
| 51 | - public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 51 | + public function __construct($post_to_jsonld_converter, $cached_postid_to_jsonld_converter) { |
|
| 52 | 52 | |
| 53 | 53 | $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
| 54 | 54 | |
@@ -67,16 +67,16 @@ discard block |
||
| 67 | 67 | $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 70 | + public function after_get_jsonld($jsonld, $post_id, $context) { |
|
| 71 | 71 | |
| 72 | 72 | // Invalid data structure |
| 73 | - if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) { |
|
| 73 | + if ( ! is_array($jsonld) || ! isset($jsonld[0]) || ! is_array($jsonld[0])) { |
|
| 74 | 74 | return $jsonld; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - if ( Jsonld_Context_Enum::PAGE !== $context |
|
| 77 | + if (Jsonld_Context_Enum::PAGE !== $context |
|
| 78 | 78 | // Returns true for "1", "true", "on" and "yes". Returns false otherwise. |
| 79 | - && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) { |
|
| 79 | + && ! filter_input(INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN)) { |
|
| 80 | 80 | return $jsonld; |
| 81 | 81 | } |
| 82 | 82 | |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | |
| 86 | 86 | // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
| 87 | 87 | // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
| 88 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 88 | + if ( ! isset($post_jsonld['@id']) || ! isset($post_jsonld['@type']) || $this->is_article($post_jsonld['@type'])) { |
|
| 89 | 89 | return $jsonld; |
| 90 | 90 | } |
| 91 | 91 | |
@@ -93,23 +93,23 @@ discard block |
||
| 93 | 93 | $reference_infos = array(); |
| 94 | 94 | |
| 95 | 95 | // Convert the post as Article. |
| 96 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() ); |
|
| 96 | + $article_jsonld = $this->post_to_jsonld_converter->convert($post_id, $references, $reference_infos, new Relations()); |
|
| 97 | 97 | |
| 98 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 98 | + $article_jsonld['@id'] = $post_jsonld['@id'].'#article'; |
|
| 99 | 99 | // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
| 100 | 100 | $article_jsonld['@type'] = 'Article'; |
| 101 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 101 | + $article_jsonld['about'] = array('@id' => $post_jsonld['@id']); |
|
| 102 | 102 | |
| 103 | 103 | // Copy over the URLs. |
| 104 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 104 | + if (isset($post_jsonld['url'])) { |
|
| 105 | 105 | $article_jsonld['url'] = $post_jsonld['url']; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 108 | + array_unshift($jsonld, $article_jsonld); |
|
| 109 | 109 | |
| 110 | 110 | // Only add authors if the key is present. |
| 111 | - if ( isset( $article_jsonld['author'] ) ) { |
|
| 112 | - $this->add_author_data_if_missing( $jsonld, $article_jsonld['author'] ); |
|
| 111 | + if (isset($article_jsonld['author'])) { |
|
| 112 | + $this->add_author_data_if_missing($jsonld, $article_jsonld['author']); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | return $jsonld; |
@@ -121,16 +121,16 @@ discard block |
||
| 121 | 121 | * |
| 122 | 122 | * @return void |
| 123 | 123 | */ |
| 124 | - private function add_author_data_if_missing( &$jsonld, $author ) { |
|
| 124 | + private function add_author_data_if_missing(&$jsonld, $author) { |
|
| 125 | 125 | |
| 126 | 126 | // For each author when if the `@id` is present and if not, add the author data. |
| 127 | - $entities_ids = $this->get_list_of_entities_ids( $author ); |
|
| 128 | - foreach ( $entities_ids as $entity_id ) { |
|
| 129 | - if ( $this->is_author_entity_present_in_graph( $jsonld, $entity_id ) ) { |
|
| 127 | + $entities_ids = $this->get_list_of_entities_ids($author); |
|
| 128 | + foreach ($entities_ids as $entity_id) { |
|
| 129 | + if ($this->is_author_entity_present_in_graph($jsonld, $entity_id)) { |
|
| 130 | 130 | continue; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - $jsonld[] = $this->get_author_data_by_entity_id( $entity_id ); |
|
| 133 | + $jsonld[] = $this->get_author_data_by_entity_id($entity_id); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | } |
@@ -143,38 +143,38 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return array An array of `@id`s. Empty if not found. |
| 145 | 145 | */ |
| 146 | - private function get_list_of_entities_ids( $items ) { |
|
| 146 | + private function get_list_of_entities_ids($items) { |
|
| 147 | 147 | // If this is not an array, then what are we doing here? we return an empty array. |
| 148 | - if ( ! is_array( $items ) || empty( $items ) ) { |
|
| 148 | + if ( ! is_array($items) || empty($items)) { |
|
| 149 | 149 | return array(); |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // If this is a keyed array and the `@id` is set, then we return that single `@id`. |
| 153 | - if ( isset( $items['@id'] ) ) { |
|
| 154 | - return array( $items['@id'] ); |
|
| 153 | + if (isset($items['@id'])) { |
|
| 154 | + return array($items['@id']); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | // Poor way to check whether this is an indexed array. |
| 158 | - if ( ! isset( $items[0] ) ) { |
|
| 158 | + if ( ! isset($items[0])) { |
|
| 159 | 159 | return array(); |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | // If this is an indexed array then we return the `@id` for all the items in the array. |
| 163 | - return array_filter( array_column( $items, '@id' ) ); |
|
| 163 | + return array_filter(array_column($items, '@id')); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - private function is_article( $schema_types ) { |
|
| 166 | + private function is_article($schema_types) { |
|
| 167 | 167 | |
| 168 | - $array_intersect = array_intersect( self::$article_types, (array) $schema_types ); |
|
| 168 | + $array_intersect = array_intersect(self::$article_types, (array) $schema_types); |
|
| 169 | 169 | |
| 170 | - return ! empty( $array_intersect ); |
|
| 170 | + return ! empty($array_intersect); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - private function get_author_data_by_entity_id( $author_linked_entity_id ) { |
|
| 173 | + private function get_author_data_by_entity_id($author_linked_entity_id) { |
|
| 174 | 174 | |
| 175 | - $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 175 | + $author_entity_post = $this->entity_uri_service->get_entity($author_linked_entity_id); |
|
| 176 | 176 | |
| 177 | - if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 177 | + if ( ! $author_entity_post instanceof \WP_Post) { |
|
| 178 | 178 | return false; |
| 179 | 179 | } |
| 180 | 180 | |
@@ -190,10 +190,10 @@ discard block |
||
| 190 | 190 | |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 193 | + private function is_author_entity_present_in_graph($jsonld, $author_entity_id) { |
|
| 194 | 194 | |
| 195 | - foreach ( $jsonld as $item ) { |
|
| 196 | - if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 195 | + foreach ($jsonld as $item) { |
|
| 196 | + if ($item && array_key_exists('@id', $item) && $item['@id'] === $author_entity_id) { |
|
| 197 | 197 | return true; |
| 198 | 198 | } |
| 199 | 199 | } |
@@ -22,35 +22,35 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | function _wl_jsonld_author__author_filter( $args_arr, $post_id ) { |
| 24 | 24 | |
| 25 | - $references = $args_arr['references']; |
|
| 25 | + $references = $args_arr['references']; |
|
| 26 | 26 | |
| 27 | - $coauthor_plugin_path = 'co-authors-plus/co-authors-plus.php'; |
|
| 27 | + $coauthor_plugin_path = 'co-authors-plus/co-authors-plus.php'; |
|
| 28 | 28 | |
| 29 | - // If the co-authors plugin is active. |
|
| 30 | - if ( ! is_plugin_active( $coauthor_plugin_path ) || ! function_exists( 'get_coauthors' ) ) { |
|
| 31 | - return $args_arr; |
|
| 32 | - } |
|
| 29 | + // If the co-authors plugin is active. |
|
| 30 | + if ( ! is_plugin_active( $coauthor_plugin_path ) || ! function_exists( 'get_coauthors' ) ) { |
|
| 31 | + return $args_arr; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - $coauthors = get_coauthors( $post_id ); |
|
| 34 | + $coauthors = get_coauthors( $post_id ); |
|
| 35 | 35 | |
| 36 | - // And we have multiple authors on a post. |
|
| 37 | - if ( empty( $coauthors ) ) { |
|
| 38 | - return $args_arr; |
|
| 39 | - } |
|
| 36 | + // And we have multiple authors on a post. |
|
| 37 | + if ( empty( $coauthors ) ) { |
|
| 38 | + return $args_arr; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - // Clear the existing author. |
|
| 42 | - $author = array(); |
|
| 41 | + // Clear the existing author. |
|
| 42 | + $author = array(); |
|
| 43 | 43 | |
| 44 | - // Build array of authors. |
|
| 45 | - $wordlift_post_to_jsonld_converter = Wordlift_Post_To_Jsonld_Converter::get_instance(); |
|
| 46 | - foreach ( $coauthors as $coauthor ) { |
|
| 47 | - $author[] = $wordlift_post_to_jsonld_converter->get_author( $coauthor->ID, $references ); |
|
| 48 | - } |
|
| 44 | + // Build array of authors. |
|
| 45 | + $wordlift_post_to_jsonld_converter = Wordlift_Post_To_Jsonld_Converter::get_instance(); |
|
| 46 | + foreach ( $coauthors as $coauthor ) { |
|
| 47 | + $author[] = $wordlift_post_to_jsonld_converter->get_author( $coauthor->ID, $references ); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - return array( |
|
| 51 | - 'author' => $author, |
|
| 52 | - 'references' => $references, |
|
| 53 | - ); |
|
| 50 | + return array( |
|
| 51 | + 'author' => $author, |
|
| 52 | + 'references' => $references, |
|
| 53 | + ); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | // Add the filter |
@@ -20,21 +20,21 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
| 22 | 22 | */ |
| 23 | -function _wl_jsonld_author__author_filter( $args_arr, $post_id ) { |
|
| 23 | +function _wl_jsonld_author__author_filter($args_arr, $post_id) { |
|
| 24 | 24 | |
| 25 | 25 | $references = $args_arr['references']; |
| 26 | 26 | |
| 27 | 27 | $coauthor_plugin_path = 'co-authors-plus/co-authors-plus.php'; |
| 28 | 28 | |
| 29 | 29 | // If the co-authors plugin is active. |
| 30 | - if ( ! is_plugin_active( $coauthor_plugin_path ) || ! function_exists( 'get_coauthors' ) ) { |
|
| 30 | + if ( ! is_plugin_active($coauthor_plugin_path) || ! function_exists('get_coauthors')) { |
|
| 31 | 31 | return $args_arr; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - $coauthors = get_coauthors( $post_id ); |
|
| 34 | + $coauthors = get_coauthors($post_id); |
|
| 35 | 35 | |
| 36 | 36 | // And we have multiple authors on a post. |
| 37 | - if ( empty( $coauthors ) ) { |
|
| 37 | + if (empty($coauthors)) { |
|
| 38 | 38 | return $args_arr; |
| 39 | 39 | } |
| 40 | 40 | |
@@ -43,8 +43,8 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | // Build array of authors. |
| 45 | 45 | $wordlift_post_to_jsonld_converter = Wordlift_Post_To_Jsonld_Converter::get_instance(); |
| 46 | - foreach ( $coauthors as $coauthor ) { |
|
| 47 | - $author[] = $wordlift_post_to_jsonld_converter->get_author( $coauthor->ID, $references ); |
|
| 46 | + foreach ($coauthors as $coauthor) { |
|
| 47 | + $author[] = $wordlift_post_to_jsonld_converter->get_author($coauthor->ID, $references); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | return array( |
@@ -54,4 +54,4 @@ discard block |
||
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | // Add the filter |
| 57 | -add_filter( 'wl_jsonld_author', '_wl_jsonld_author__author_filter', 10, 2 ); |
|
| 57 | +add_filter('wl_jsonld_author', '_wl_jsonld_author__author_filter', 10, 2); |
|