@@ -6,143 +6,143 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Jsonld_Article_Wrapper { |
| 8 | 8 | |
| 9 | - public static $article_types = array( |
|
| 10 | - 'Article', |
|
| 11 | - 'AdvertiserContentArticle', |
|
| 12 | - 'NewsArticle', |
|
| 13 | - 'AnalysisNewsArticle', |
|
| 14 | - 'AskPublicNewsArticle', |
|
| 15 | - 'BackgroundNewsArticle', |
|
| 16 | - 'OpinionNewsArticle', |
|
| 17 | - 'ReportageNewsArticle', |
|
| 18 | - 'ReviewNewsArticle', |
|
| 19 | - 'Report', |
|
| 20 | - 'SatiricalArticle', |
|
| 21 | - 'ScholarlyArticle', |
|
| 22 | - 'MedicalScholarlyArticle', |
|
| 23 | - 'SocialMediaPosting', |
|
| 24 | - 'BlogPosting', |
|
| 25 | - 'LiveBlogPosting', |
|
| 26 | - 'DiscussionForumPosting', |
|
| 27 | - 'TechArticle', |
|
| 28 | - 'APIReference' |
|
| 29 | - ); |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 33 | - */ |
|
| 34 | - private $post_to_jsonld_converter; |
|
| 35 | - /** |
|
| 36 | - * @var \Wordlift_Cached_Post_Converter |
|
| 37 | - */ |
|
| 38 | - private $cached_postid_to_jsonld_converter; |
|
| 39 | - /** |
|
| 40 | - * @var \Wordlift_Entity_Uri_Service |
|
| 41 | - */ |
|
| 42 | - private $entity_uri_service; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Jsonld_Article_Wrapper constructor. |
|
| 46 | - * |
|
| 47 | - * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 48 | - * @param $cached_postid_to_jsonld_converter |
|
| 49 | - */ |
|
| 50 | - public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 51 | - |
|
| 52 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 53 | - |
|
| 54 | - add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 ); |
|
| 55 | - |
|
| 56 | - $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 57 | - |
|
| 58 | - $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 62 | - |
|
| 63 | - if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 64 | - || ! is_array( $jsonld[0] ) ) { |
|
| 65 | - return $jsonld; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - // Copy the 1st array element |
|
| 69 | - $post_jsonld = $jsonld[0]; |
|
| 70 | - |
|
| 71 | - // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 72 | - // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 73 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 74 | - return $jsonld; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Convert the post as Article. |
|
| 78 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 79 | - |
|
| 80 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 81 | - // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 82 | - $article_jsonld['@type'] = 'Article'; |
|
| 83 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - // Copy over the URLs. |
|
| 87 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 88 | - $article_jsonld['url'] = $post_jsonld['url']; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 92 | - |
|
| 93 | - $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * The author entities can be present in graph for some entity types |
|
| 97 | - * for Person and Organization, so check before we add it to graph. |
|
| 98 | - * reference : https://schema.org/author |
|
| 99 | - */ |
|
| 100 | - if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 101 | - $jsonld[] = $author_jsonld; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $jsonld; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - private function is_article( $schema_types ) { |
|
| 108 | - |
|
| 109 | - $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 110 | - |
|
| 111 | - return ! empty( $array_intersect ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - private function get_author_linked_entity( $article_jsonld ) { |
|
| 115 | - if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 116 | - return false; |
|
| 117 | - } |
|
| 9 | + public static $article_types = array( |
|
| 10 | + 'Article', |
|
| 11 | + 'AdvertiserContentArticle', |
|
| 12 | + 'NewsArticle', |
|
| 13 | + 'AnalysisNewsArticle', |
|
| 14 | + 'AskPublicNewsArticle', |
|
| 15 | + 'BackgroundNewsArticle', |
|
| 16 | + 'OpinionNewsArticle', |
|
| 17 | + 'ReportageNewsArticle', |
|
| 18 | + 'ReviewNewsArticle', |
|
| 19 | + 'Report', |
|
| 20 | + 'SatiricalArticle', |
|
| 21 | + 'ScholarlyArticle', |
|
| 22 | + 'MedicalScholarlyArticle', |
|
| 23 | + 'SocialMediaPosting', |
|
| 24 | + 'BlogPosting', |
|
| 25 | + 'LiveBlogPosting', |
|
| 26 | + 'DiscussionForumPosting', |
|
| 27 | + 'TechArticle', |
|
| 28 | + 'APIReference' |
|
| 29 | + ); |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 33 | + */ |
|
| 34 | + private $post_to_jsonld_converter; |
|
| 35 | + /** |
|
| 36 | + * @var \Wordlift_Cached_Post_Converter |
|
| 37 | + */ |
|
| 38 | + private $cached_postid_to_jsonld_converter; |
|
| 39 | + /** |
|
| 40 | + * @var \Wordlift_Entity_Uri_Service |
|
| 41 | + */ |
|
| 42 | + private $entity_uri_service; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Jsonld_Article_Wrapper constructor. |
|
| 46 | + * |
|
| 47 | + * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 48 | + * @param $cached_postid_to_jsonld_converter |
|
| 49 | + */ |
|
| 50 | + public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 51 | + |
|
| 52 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 53 | + |
|
| 54 | + add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 ); |
|
| 55 | + |
|
| 56 | + $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 57 | + |
|
| 58 | + $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 62 | + |
|
| 63 | + if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 64 | + || ! is_array( $jsonld[0] ) ) { |
|
| 65 | + return $jsonld; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + // Copy the 1st array element |
|
| 69 | + $post_jsonld = $jsonld[0]; |
|
| 70 | + |
|
| 71 | + // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 72 | + // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 73 | + if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 74 | + return $jsonld; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Convert the post as Article. |
|
| 78 | + $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 79 | + |
|
| 80 | + $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 81 | + // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 82 | + $article_jsonld['@type'] = 'Article'; |
|
| 83 | + $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + // Copy over the URLs. |
|
| 87 | + if ( isset( $post_jsonld['url'] ) ) { |
|
| 88 | + $article_jsonld['url'] = $post_jsonld['url']; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + array_unshift( $jsonld, $article_jsonld ); |
|
| 92 | + |
|
| 93 | + $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * The author entities can be present in graph for some entity types |
|
| 97 | + * for Person and Organization, so check before we add it to graph. |
|
| 98 | + * reference : https://schema.org/author |
|
| 99 | + */ |
|
| 100 | + if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 101 | + $jsonld[] = $author_jsonld; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $jsonld; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + private function is_article( $schema_types ) { |
|
| 108 | + |
|
| 109 | + $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 110 | + |
|
| 111 | + return ! empty( $array_intersect ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + private function get_author_linked_entity( $article_jsonld ) { |
|
| 115 | + if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 116 | + return false; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $author = $article_jsonld['author']; |
|
| 119 | + $author = $article_jsonld['author']; |
|
| 120 | 120 | |
| 121 | - if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 121 | + if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - $author_linked_entity_id = $author['@id']; |
|
| 125 | + $author_linked_entity_id = $author['@id']; |
|
| 126 | 126 | |
| 127 | - $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 127 | + $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 128 | 128 | |
| 129 | - if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 130 | - return false; |
|
| 131 | - } |
|
| 129 | + if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 130 | + return false; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID ); |
|
| 133 | + return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID ); |
|
| 134 | 134 | |
| 135 | - } |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 137 | + private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 138 | 138 | |
| 139 | - foreach ( $jsonld as $item ) { |
|
| 140 | - if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - } |
|
| 139 | + foreach ( $jsonld as $item ) { |
|
| 140 | + if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - return false; |
|
| 146 | - } |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | } |
| 149 | 149 | \ No newline at end of file |
@@ -12,176 +12,176 @@ |
||
| 12 | 12 | use Wordlift\Videoobject\Data\Video_Storage\Storage; |
| 13 | 13 | |
| 14 | 14 | class Jsonld { |
| 15 | - /** |
|
| 16 | - * @var Storage |
|
| 17 | - */ |
|
| 18 | - private $video_storage; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Jsonld constructor. |
|
| 22 | - * |
|
| 23 | - * @param $video_storage Storage |
|
| 24 | - */ |
|
| 25 | - public function __construct( $video_storage ) { |
|
| 26 | - add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 ); |
|
| 27 | - add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 ); |
|
| 28 | - $this->video_storage = $video_storage; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - public function wl_after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 33 | - if ( 0 === count( $jsonld ) ) { |
|
| 34 | - return $jsonld; |
|
| 35 | - } |
|
| 36 | - $current_item = $jsonld[0]; |
|
| 37 | - |
|
| 38 | - if ( ! array_key_exists( '@type', $current_item ) ) { |
|
| 39 | - // Cant determine type return early. |
|
| 40 | - return $jsonld; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - $type = $current_item['@type']; |
|
| 44 | - if ( is_string( $type ) ) { |
|
| 45 | - $type = array( $type ); |
|
| 46 | - } |
|
| 47 | - // If its a article or descendant of article, then dont add the |
|
| 48 | - // videoobject in this hook, they will be already added to video property. |
|
| 49 | - if ( array_intersect( Jsonld_Article_Wrapper::$article_types, $type ) ) { |
|
| 50 | - return $jsonld; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - $videos_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 54 | - if ( 0 === count( $videos_jsonld ) ) { |
|
| 55 | - return $jsonld; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - // check if we have @id in jsonld for first item. |
|
| 59 | - $id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : ''; |
|
| 60 | - |
|
| 61 | - foreach ( $videos_jsonld as &$video_jsonld ) { |
|
| 62 | - if ( ! $id ) { |
|
| 63 | - continue; |
|
| 64 | - } |
|
| 65 | - if ( ! array_key_exists( 'mentions', $video_jsonld ) ) { |
|
| 66 | - $video_jsonld['mentions'] = array( '@id' => $id ); |
|
| 67 | - } else { |
|
| 68 | - $video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) ); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - return array_merge( $jsonld, $videos_jsonld ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param $existing_video_data string | array associative or sequential array. |
|
| 77 | - * @param $new_video_data array Sequential array. |
|
| 78 | - * |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - private function merge_video_data( $existing_video_data, $new_video_data ) { |
|
| 82 | - if ( ! is_array( $existing_video_data ) ) { |
|
| 83 | - $new_video_data[] = $existing_video_data; |
|
| 84 | - |
|
| 85 | - return $new_video_data; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - if ( $this->is_associative_array( $existing_video_data ) ) { |
|
| 89 | - $new_video_data[] = $existing_video_data; |
|
| 90 | - |
|
| 91 | - return $new_video_data; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - return array_merge( $existing_video_data, $new_video_data ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - public function wl_post_jsonld( $jsonld, $post_id, $references ) { |
|
| 98 | - |
|
| 99 | - $video_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 100 | - if ( count( $video_jsonld ) === 0 ) { |
|
| 101 | - return $jsonld; |
|
| 102 | - } |
|
| 103 | - // Before adding the video jsonld check if the key |
|
| 104 | - // is present and additional data might be present, |
|
| 105 | - // if not present just add the data and return early. |
|
| 106 | - if ( ! array_key_exists( 'video', $jsonld ) ) { |
|
| 107 | - $jsonld['video'] = $video_jsonld; |
|
| 108 | - |
|
| 109 | - return $jsonld; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // since key exists, we need to merge the data based on type. |
|
| 113 | - $previous_video_data = $jsonld['video']; |
|
| 114 | - $jsonld['video'] = $this->merge_video_data( $previous_video_data, $video_jsonld ); |
|
| 115 | - |
|
| 116 | - return $jsonld; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param $post_id int Post id. |
|
| 122 | - * |
|
| 123 | - * @return array |
|
| 124 | - */ |
|
| 125 | - public function get_videos_jsonld( $post_id ) { |
|
| 126 | - |
|
| 127 | - $videos = $this->video_storage->get_all_videos( $post_id ); |
|
| 128 | - |
|
| 129 | - $jsonld = array(); |
|
| 130 | - |
|
| 131 | - foreach ( $videos as $video ) { |
|
| 132 | - /** |
|
| 133 | - * @var $video Video |
|
| 134 | - */ |
|
| 135 | - $description = $video->description; |
|
| 136 | - if ( ! $video->description ) { |
|
| 137 | - // If description is empty then use the video title as description |
|
| 138 | - $description = $video->name; |
|
| 139 | - } |
|
| 140 | - $single_jsonld = array( |
|
| 141 | - '@context' => 'http://schema.org', |
|
| 142 | - '@type' => 'VideoObject', |
|
| 143 | - 'name' => $video->name, |
|
| 144 | - 'description' => $description, |
|
| 145 | - 'contentUrl' => $video->content_url, |
|
| 146 | - 'embedUrl' => $video->embed_url, |
|
| 147 | - 'uploadDate' => $video->upload_date, |
|
| 148 | - 'thumbnailUrl' => $video->thumbnail_urls, |
|
| 149 | - 'duration' => $video->duration, |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - if ( $video->views ) { |
|
| 153 | - $single_jsonld['interactionStatistic'] = array( |
|
| 154 | - '@type' => 'InteractionCounter', |
|
| 155 | - 'interactionType' => array( |
|
| 156 | - '@type' => 'http://schema.org/WatchAction' |
|
| 157 | - ), |
|
| 158 | - 'userInteractionCount' => $video->views |
|
| 159 | - ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if ( $video->is_live_video ) { |
|
| 163 | - $single_jsonld['publication'] = array( |
|
| 164 | - '@type' => 'BroadcastEvent', |
|
| 165 | - 'isLiveBroadcast' => true, |
|
| 166 | - 'startDate' => $video->live_video_start_date, |
|
| 167 | - 'endDate' => $video->live_video_end_date |
|
| 168 | - ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $jsonld[] = $single_jsonld; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return $jsonld; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - private function is_associative_array( $arr ) { |
|
| 179 | - if ( array() === $arr ) { |
|
| 180 | - return false; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
| 184 | - } |
|
| 15 | + /** |
|
| 16 | + * @var Storage |
|
| 17 | + */ |
|
| 18 | + private $video_storage; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Jsonld constructor. |
|
| 22 | + * |
|
| 23 | + * @param $video_storage Storage |
|
| 24 | + */ |
|
| 25 | + public function __construct( $video_storage ) { |
|
| 26 | + add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 ); |
|
| 27 | + add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 ); |
|
| 28 | + $this->video_storage = $video_storage; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + public function wl_after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 33 | + if ( 0 === count( $jsonld ) ) { |
|
| 34 | + return $jsonld; |
|
| 35 | + } |
|
| 36 | + $current_item = $jsonld[0]; |
|
| 37 | + |
|
| 38 | + if ( ! array_key_exists( '@type', $current_item ) ) { |
|
| 39 | + // Cant determine type return early. |
|
| 40 | + return $jsonld; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + $type = $current_item['@type']; |
|
| 44 | + if ( is_string( $type ) ) { |
|
| 45 | + $type = array( $type ); |
|
| 46 | + } |
|
| 47 | + // If its a article or descendant of article, then dont add the |
|
| 48 | + // videoobject in this hook, they will be already added to video property. |
|
| 49 | + if ( array_intersect( Jsonld_Article_Wrapper::$article_types, $type ) ) { |
|
| 50 | + return $jsonld; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + $videos_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 54 | + if ( 0 === count( $videos_jsonld ) ) { |
|
| 55 | + return $jsonld; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + // check if we have @id in jsonld for first item. |
|
| 59 | + $id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : ''; |
|
| 60 | + |
|
| 61 | + foreach ( $videos_jsonld as &$video_jsonld ) { |
|
| 62 | + if ( ! $id ) { |
|
| 63 | + continue; |
|
| 64 | + } |
|
| 65 | + if ( ! array_key_exists( 'mentions', $video_jsonld ) ) { |
|
| 66 | + $video_jsonld['mentions'] = array( '@id' => $id ); |
|
| 67 | + } else { |
|
| 68 | + $video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) ); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + return array_merge( $jsonld, $videos_jsonld ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param $existing_video_data string | array associative or sequential array. |
|
| 77 | + * @param $new_video_data array Sequential array. |
|
| 78 | + * |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + private function merge_video_data( $existing_video_data, $new_video_data ) { |
|
| 82 | + if ( ! is_array( $existing_video_data ) ) { |
|
| 83 | + $new_video_data[] = $existing_video_data; |
|
| 84 | + |
|
| 85 | + return $new_video_data; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + if ( $this->is_associative_array( $existing_video_data ) ) { |
|
| 89 | + $new_video_data[] = $existing_video_data; |
|
| 90 | + |
|
| 91 | + return $new_video_data; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + return array_merge( $existing_video_data, $new_video_data ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + public function wl_post_jsonld( $jsonld, $post_id, $references ) { |
|
| 98 | + |
|
| 99 | + $video_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 100 | + if ( count( $video_jsonld ) === 0 ) { |
|
| 101 | + return $jsonld; |
|
| 102 | + } |
|
| 103 | + // Before adding the video jsonld check if the key |
|
| 104 | + // is present and additional data might be present, |
|
| 105 | + // if not present just add the data and return early. |
|
| 106 | + if ( ! array_key_exists( 'video', $jsonld ) ) { |
|
| 107 | + $jsonld['video'] = $video_jsonld; |
|
| 108 | + |
|
| 109 | + return $jsonld; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // since key exists, we need to merge the data based on type. |
|
| 113 | + $previous_video_data = $jsonld['video']; |
|
| 114 | + $jsonld['video'] = $this->merge_video_data( $previous_video_data, $video_jsonld ); |
|
| 115 | + |
|
| 116 | + return $jsonld; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param $post_id int Post id. |
|
| 122 | + * |
|
| 123 | + * @return array |
|
| 124 | + */ |
|
| 125 | + public function get_videos_jsonld( $post_id ) { |
|
| 126 | + |
|
| 127 | + $videos = $this->video_storage->get_all_videos( $post_id ); |
|
| 128 | + |
|
| 129 | + $jsonld = array(); |
|
| 130 | + |
|
| 131 | + foreach ( $videos as $video ) { |
|
| 132 | + /** |
|
| 133 | + * @var $video Video |
|
| 134 | + */ |
|
| 135 | + $description = $video->description; |
|
| 136 | + if ( ! $video->description ) { |
|
| 137 | + // If description is empty then use the video title as description |
|
| 138 | + $description = $video->name; |
|
| 139 | + } |
|
| 140 | + $single_jsonld = array( |
|
| 141 | + '@context' => 'http://schema.org', |
|
| 142 | + '@type' => 'VideoObject', |
|
| 143 | + 'name' => $video->name, |
|
| 144 | + 'description' => $description, |
|
| 145 | + 'contentUrl' => $video->content_url, |
|
| 146 | + 'embedUrl' => $video->embed_url, |
|
| 147 | + 'uploadDate' => $video->upload_date, |
|
| 148 | + 'thumbnailUrl' => $video->thumbnail_urls, |
|
| 149 | + 'duration' => $video->duration, |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + if ( $video->views ) { |
|
| 153 | + $single_jsonld['interactionStatistic'] = array( |
|
| 154 | + '@type' => 'InteractionCounter', |
|
| 155 | + 'interactionType' => array( |
|
| 156 | + '@type' => 'http://schema.org/WatchAction' |
|
| 157 | + ), |
|
| 158 | + 'userInteractionCount' => $video->views |
|
| 159 | + ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if ( $video->is_live_video ) { |
|
| 163 | + $single_jsonld['publication'] = array( |
|
| 164 | + '@type' => 'BroadcastEvent', |
|
| 165 | + 'isLiveBroadcast' => true, |
|
| 166 | + 'startDate' => $video->live_video_start_date, |
|
| 167 | + 'endDate' => $video->live_video_end_date |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $jsonld[] = $single_jsonld; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return $jsonld; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + private function is_associative_array( $arr ) { |
|
| 179 | + if ( array() === $arr ) { |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | 186 | |
| 187 | 187 | } |
@@ -22,54 +22,54 @@ discard block |
||
| 22 | 22 | * |
| 23 | 23 | * @param $video_storage Storage |
| 24 | 24 | */ |
| 25 | - public function __construct( $video_storage ) { |
|
| 26 | - add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 ); |
|
| 27 | - add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 ); |
|
| 25 | + public function __construct($video_storage) { |
|
| 26 | + add_action('wl_post_jsonld', array($this, 'wl_post_jsonld'), 10, 3); |
|
| 27 | + add_action('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 10, 3); |
|
| 28 | 28 | $this->video_storage = $video_storage; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | |
| 32 | - public function wl_after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 33 | - if ( 0 === count( $jsonld ) ) { |
|
| 32 | + public function wl_after_get_jsonld($jsonld, $post_id, $context) { |
|
| 33 | + if (0 === count($jsonld)) { |
|
| 34 | 34 | return $jsonld; |
| 35 | 35 | } |
| 36 | 36 | $current_item = $jsonld[0]; |
| 37 | 37 | |
| 38 | - if ( ! array_key_exists( '@type', $current_item ) ) { |
|
| 38 | + if ( ! array_key_exists('@type', $current_item)) { |
|
| 39 | 39 | // Cant determine type return early. |
| 40 | 40 | return $jsonld; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | $type = $current_item['@type']; |
| 44 | - if ( is_string( $type ) ) { |
|
| 45 | - $type = array( $type ); |
|
| 44 | + if (is_string($type)) { |
|
| 45 | + $type = array($type); |
|
| 46 | 46 | } |
| 47 | 47 | // If its a article or descendant of article, then dont add the |
| 48 | 48 | // videoobject in this hook, they will be already added to video property. |
| 49 | - if ( array_intersect( Jsonld_Article_Wrapper::$article_types, $type ) ) { |
|
| 49 | + if (array_intersect(Jsonld_Article_Wrapper::$article_types, $type)) { |
|
| 50 | 50 | return $jsonld; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - $videos_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 54 | - if ( 0 === count( $videos_jsonld ) ) { |
|
| 53 | + $videos_jsonld = $this->get_videos_jsonld($post_id); |
|
| 54 | + if (0 === count($videos_jsonld)) { |
|
| 55 | 55 | return $jsonld; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | // check if we have @id in jsonld for first item. |
| 59 | - $id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : ''; |
|
| 59 | + $id = array_key_exists('@id', $current_item) ? $current_item['@id'] : ''; |
|
| 60 | 60 | |
| 61 | - foreach ( $videos_jsonld as &$video_jsonld ) { |
|
| 62 | - if ( ! $id ) { |
|
| 61 | + foreach ($videos_jsonld as &$video_jsonld) { |
|
| 62 | + if ( ! $id) { |
|
| 63 | 63 | continue; |
| 64 | 64 | } |
| 65 | - if ( ! array_key_exists( 'mentions', $video_jsonld ) ) { |
|
| 66 | - $video_jsonld['mentions'] = array( '@id' => $id ); |
|
| 65 | + if ( ! array_key_exists('mentions', $video_jsonld)) { |
|
| 66 | + $video_jsonld['mentions'] = array('@id' => $id); |
|
| 67 | 67 | } else { |
| 68 | - $video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) ); |
|
| 68 | + $video_jsonld['mentions'] = array_merge($video_jsonld['mentions'], array('@id' => $id)); |
|
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - return array_merge( $jsonld, $videos_jsonld ); |
|
| 72 | + return array_merge($jsonld, $videos_jsonld); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -78,32 +78,32 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @return array |
| 80 | 80 | */ |
| 81 | - private function merge_video_data( $existing_video_data, $new_video_data ) { |
|
| 82 | - if ( ! is_array( $existing_video_data ) ) { |
|
| 81 | + private function merge_video_data($existing_video_data, $new_video_data) { |
|
| 82 | + if ( ! is_array($existing_video_data)) { |
|
| 83 | 83 | $new_video_data[] = $existing_video_data; |
| 84 | 84 | |
| 85 | 85 | return $new_video_data; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - if ( $this->is_associative_array( $existing_video_data ) ) { |
|
| 88 | + if ($this->is_associative_array($existing_video_data)) { |
|
| 89 | 89 | $new_video_data[] = $existing_video_data; |
| 90 | 90 | |
| 91 | 91 | return $new_video_data; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - return array_merge( $existing_video_data, $new_video_data ); |
|
| 94 | + return array_merge($existing_video_data, $new_video_data); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - public function wl_post_jsonld( $jsonld, $post_id, $references ) { |
|
| 97 | + public function wl_post_jsonld($jsonld, $post_id, $references) { |
|
| 98 | 98 | |
| 99 | - $video_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 100 | - if ( count( $video_jsonld ) === 0 ) { |
|
| 99 | + $video_jsonld = $this->get_videos_jsonld($post_id); |
|
| 100 | + if (count($video_jsonld) === 0) { |
|
| 101 | 101 | return $jsonld; |
| 102 | 102 | } |
| 103 | 103 | // Before adding the video jsonld check if the key |
| 104 | 104 | // is present and additional data might be present, |
| 105 | 105 | // if not present just add the data and return early. |
| 106 | - if ( ! array_key_exists( 'video', $jsonld ) ) { |
|
| 106 | + if ( ! array_key_exists('video', $jsonld)) { |
|
| 107 | 107 | $jsonld['video'] = $video_jsonld; |
| 108 | 108 | |
| 109 | 109 | return $jsonld; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | // since key exists, we need to merge the data based on type. |
| 113 | 113 | $previous_video_data = $jsonld['video']; |
| 114 | - $jsonld['video'] = $this->merge_video_data( $previous_video_data, $video_jsonld ); |
|
| 114 | + $jsonld['video'] = $this->merge_video_data($previous_video_data, $video_jsonld); |
|
| 115 | 115 | |
| 116 | 116 | return $jsonld; |
| 117 | 117 | } |
@@ -122,18 +122,18 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @return array |
| 124 | 124 | */ |
| 125 | - public function get_videos_jsonld( $post_id ) { |
|
| 125 | + public function get_videos_jsonld($post_id) { |
|
| 126 | 126 | |
| 127 | - $videos = $this->video_storage->get_all_videos( $post_id ); |
|
| 127 | + $videos = $this->video_storage->get_all_videos($post_id); |
|
| 128 | 128 | |
| 129 | 129 | $jsonld = array(); |
| 130 | 130 | |
| 131 | - foreach ( $videos as $video ) { |
|
| 131 | + foreach ($videos as $video) { |
|
| 132 | 132 | /** |
| 133 | 133 | * @var $video Video |
| 134 | 134 | */ |
| 135 | 135 | $description = $video->description; |
| 136 | - if ( ! $video->description ) { |
|
| 136 | + if ( ! $video->description) { |
|
| 137 | 137 | // If description is empty then use the video title as description |
| 138 | 138 | $description = $video->name; |
| 139 | 139 | } |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | 'duration' => $video->duration, |
| 150 | 150 | ); |
| 151 | 151 | |
| 152 | - if ( $video->views ) { |
|
| 152 | + if ($video->views) { |
|
| 153 | 153 | $single_jsonld['interactionStatistic'] = array( |
| 154 | 154 | '@type' => 'InteractionCounter', |
| 155 | 155 | 'interactionType' => array( |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | ); |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - if ( $video->is_live_video ) { |
|
| 162 | + if ($video->is_live_video) { |
|
| 163 | 163 | $single_jsonld['publication'] = array( |
| 164 | 164 | '@type' => 'BroadcastEvent', |
| 165 | 165 | 'isLiveBroadcast' => true, |
@@ -175,12 +175,12 @@ discard block |
||
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | |
| 178 | - private function is_associative_array( $arr ) { |
|
| 179 | - if ( array() === $arr ) { |
|
| 178 | + private function is_associative_array($arr) { |
|
| 179 | + if (array() === $arr) { |
|
| 180 | 180 | return false; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
| 183 | + return array_keys($arr) !== range(0, count($arr) - 1); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | |