@@ -12,110 +12,110 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | class Xml_Generator { |
| 14 | 14 | |
| 15 | - private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 16 | - try { |
|
| 17 | - $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | - } catch ( \Exception $e ) { |
|
| 19 | - return 0; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - $days_to_seconds = $interval->d * 60 * 60 * 24; |
|
| 23 | - $hours_to_seconds = $interval->h * 60 * 60; |
|
| 24 | - $minutes_to_seconds = $interval->i * 60; |
|
| 25 | - $seconds = $interval->s; |
|
| 26 | - |
|
| 27 | - return $days_to_seconds + $hours_to_seconds + $minutes_to_seconds + $seconds; |
|
| 28 | - |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public static function get_xml_for_all_posts_with_videos() { |
|
| 32 | - /** |
|
| 33 | - * @since 3.31.6 |
|
| 34 | - * Filter the query args, add support for all custom public post types. |
|
| 35 | - */ |
|
| 36 | - $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 37 | - 'fields' => 'ids', |
|
| 38 | - 'numberposts' => - 1, |
|
| 39 | - 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 40 | - 'meta_query' => array( |
|
| 41 | - array( |
|
| 42 | - 'key' => Meta_Storage::META_KEY, |
|
| 43 | - 'compare' => 'EXISTS' |
|
| 44 | - ) |
|
| 45 | - ) |
|
| 46 | - ) ); |
|
| 47 | - |
|
| 48 | - $posts = get_posts( $video_sitemap_query_args ); |
|
| 49 | - |
|
| 50 | - $all_posts_xml = ""; |
|
| 51 | - |
|
| 52 | - if ( ! $posts ) { |
|
| 53 | - return $all_posts_xml; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - foreach ( $posts as $post_id ) { |
|
| 57 | - $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $all_posts_xml; |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param $post_id |
|
| 67 | - * |
|
| 68 | - * @return string XML string for single post. |
|
| 69 | - */ |
|
| 70 | - public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | - $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | - if ( ! $videos ) { |
|
| 73 | - return ""; |
|
| 74 | - } |
|
| 75 | - $single_post_xml = ""; |
|
| 76 | - foreach ( $videos as $video ) { |
|
| 77 | - $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return $single_post_xml; |
|
| 81 | - |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $video Video |
|
| 87 | - * @param $post_id int |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 92 | - |
|
| 93 | - $permalink = get_permalink( $post_id ); |
|
| 94 | - $title = esc_html( $video->name ); |
|
| 95 | - $description = esc_html( $video->description ); |
|
| 96 | - $thumbnail_url = $video->thumbnail_urls[0]; |
|
| 97 | - |
|
| 98 | - // If description is empty use title. |
|
| 99 | - if ( ! $description ) { |
|
| 100 | - $description = $title; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $optional_fields = array( |
|
| 104 | - 'content_loc' => $video->content_url, |
|
| 105 | - 'player_loc' => $video->embed_url, |
|
| 106 | - 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 107 | - 'view_count' => $video->views, |
|
| 108 | - 'live' => $video->is_live_video ? 'yes' : 'no' |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - $optional_data = ""; |
|
| 112 | - foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | - if ( $xml_value ) { |
|
| 114 | - $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return <<<EOF |
|
| 15 | + private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 16 | + try { |
|
| 17 | + $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | + } catch ( \Exception $e ) { |
|
| 19 | + return 0; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + $days_to_seconds = $interval->d * 60 * 60 * 24; |
|
| 23 | + $hours_to_seconds = $interval->h * 60 * 60; |
|
| 24 | + $minutes_to_seconds = $interval->i * 60; |
|
| 25 | + $seconds = $interval->s; |
|
| 26 | + |
|
| 27 | + return $days_to_seconds + $hours_to_seconds + $minutes_to_seconds + $seconds; |
|
| 28 | + |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public static function get_xml_for_all_posts_with_videos() { |
|
| 32 | + /** |
|
| 33 | + * @since 3.31.6 |
|
| 34 | + * Filter the query args, add support for all custom public post types. |
|
| 35 | + */ |
|
| 36 | + $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 37 | + 'fields' => 'ids', |
|
| 38 | + 'numberposts' => - 1, |
|
| 39 | + 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 40 | + 'meta_query' => array( |
|
| 41 | + array( |
|
| 42 | + 'key' => Meta_Storage::META_KEY, |
|
| 43 | + 'compare' => 'EXISTS' |
|
| 44 | + ) |
|
| 45 | + ) |
|
| 46 | + ) ); |
|
| 47 | + |
|
| 48 | + $posts = get_posts( $video_sitemap_query_args ); |
|
| 49 | + |
|
| 50 | + $all_posts_xml = ""; |
|
| 51 | + |
|
| 52 | + if ( ! $posts ) { |
|
| 53 | + return $all_posts_xml; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + foreach ( $posts as $post_id ) { |
|
| 57 | + $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $all_posts_xml; |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param $post_id |
|
| 67 | + * |
|
| 68 | + * @return string XML string for single post. |
|
| 69 | + */ |
|
| 70 | + public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | + $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | + if ( ! $videos ) { |
|
| 73 | + return ""; |
|
| 74 | + } |
|
| 75 | + $single_post_xml = ""; |
|
| 76 | + foreach ( $videos as $video ) { |
|
| 77 | + $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return $single_post_xml; |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $video Video |
|
| 87 | + * @param $post_id int |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 92 | + |
|
| 93 | + $permalink = get_permalink( $post_id ); |
|
| 94 | + $title = esc_html( $video->name ); |
|
| 95 | + $description = esc_html( $video->description ); |
|
| 96 | + $thumbnail_url = $video->thumbnail_urls[0]; |
|
| 97 | + |
|
| 98 | + // If description is empty use title. |
|
| 99 | + if ( ! $description ) { |
|
| 100 | + $description = $title; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $optional_fields = array( |
|
| 104 | + 'content_loc' => $video->content_url, |
|
| 105 | + 'player_loc' => $video->embed_url, |
|
| 106 | + 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 107 | + 'view_count' => $video->views, |
|
| 108 | + 'live' => $video->is_live_video ? 'yes' : 'no' |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + $optional_data = ""; |
|
| 112 | + foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | + if ( $xml_value ) { |
|
| 114 | + $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return <<<EOF |
|
| 119 | 119 | <url> |
| 120 | 120 | <loc>${permalink}</loc> |
| 121 | 121 | <video:video> |
@@ -127,6 +127,6 @@ discard block |
||
| 127 | 127 | </url> |
| 128 | 128 | EOF; |
| 129 | 129 | |
| 130 | - } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | } |
| 133 | 133 | \ No newline at end of file |
@@ -12,10 +12,10 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | class Xml_Generator { |
| 14 | 14 | |
| 15 | - private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 15 | + private static function iso8601_to_seconds($iso8601_interval_string) { |
|
| 16 | 16 | try { |
| 17 | - $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | - } catch ( \Exception $e ) { |
|
| 17 | + $interval = new \DateInterval($iso8601_interval_string); |
|
| 18 | + } catch (\Exception $e) { |
|
| 19 | 19 | return 0; |
| 20 | 20 | } |
| 21 | 21 | |
@@ -33,28 +33,28 @@ discard block |
||
| 33 | 33 | * @since 3.31.6 |
| 34 | 34 | * Filter the query args, add support for all custom public post types. |
| 35 | 35 | */ |
| 36 | - $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 36 | + $video_sitemap_query_args = apply_filters('wl_videoobject_sitemap_query_args', array( |
|
| 37 | 37 | 'fields' => 'ids', |
| 38 | - 'numberposts' => - 1, |
|
| 39 | - 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 38 | + 'numberposts' => -1, |
|
| 39 | + 'post_type' => get_post_types(array('public' => true)), |
|
| 40 | 40 | 'meta_query' => array( |
| 41 | 41 | array( |
| 42 | 42 | 'key' => Meta_Storage::META_KEY, |
| 43 | 43 | 'compare' => 'EXISTS' |
| 44 | 44 | ) |
| 45 | 45 | ) |
| 46 | - ) ); |
|
| 46 | + )); |
|
| 47 | 47 | |
| 48 | - $posts = get_posts( $video_sitemap_query_args ); |
|
| 48 | + $posts = get_posts($video_sitemap_query_args); |
|
| 49 | 49 | |
| 50 | 50 | $all_posts_xml = ""; |
| 51 | 51 | |
| 52 | - if ( ! $posts ) { |
|
| 52 | + if ( ! $posts) { |
|
| 53 | 53 | return $all_posts_xml; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - foreach ( $posts as $post_id ) { |
|
| 57 | - $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 56 | + foreach ($posts as $post_id) { |
|
| 57 | + $all_posts_xml .= self::get_xml_for_single_post($post_id); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | return $all_posts_xml; |
@@ -67,14 +67,14 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return string XML string for single post. |
| 69 | 69 | */ |
| 70 | - public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | - $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | - if ( ! $videos ) { |
|
| 70 | + public static function get_xml_for_single_post($post_id) { |
|
| 71 | + $videos = Video_Storage_Factory::get_storage()->get_all_videos($post_id); |
|
| 72 | + if ( ! $videos) { |
|
| 73 | 73 | return ""; |
| 74 | 74 | } |
| 75 | 75 | $single_post_xml = ""; |
| 76 | - foreach ( $videos as $video ) { |
|
| 77 | - $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 76 | + foreach ($videos as $video) { |
|
| 77 | + $single_post_xml .= self::get_xml_for_single_video($video, $post_id); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | return $single_post_xml; |
@@ -88,29 +88,29 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @return string |
| 90 | 90 | */ |
| 91 | - public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 91 | + public static function get_xml_for_single_video($video, $post_id) { |
|
| 92 | 92 | |
| 93 | - $permalink = get_permalink( $post_id ); |
|
| 94 | - $title = esc_html( $video->name ); |
|
| 95 | - $description = esc_html( $video->description ); |
|
| 93 | + $permalink = get_permalink($post_id); |
|
| 94 | + $title = esc_html($video->name); |
|
| 95 | + $description = esc_html($video->description); |
|
| 96 | 96 | $thumbnail_url = $video->thumbnail_urls[0]; |
| 97 | 97 | |
| 98 | 98 | // If description is empty use title. |
| 99 | - if ( ! $description ) { |
|
| 99 | + if ( ! $description) { |
|
| 100 | 100 | $description = $title; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $optional_fields = array( |
| 104 | 104 | 'content_loc' => $video->content_url, |
| 105 | 105 | 'player_loc' => $video->embed_url, |
| 106 | - 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 106 | + 'duration' => self::iso8601_to_seconds($video->duration), |
|
| 107 | 107 | 'view_count' => $video->views, |
| 108 | 108 | 'live' => $video->is_live_video ? 'yes' : 'no' |
| 109 | 109 | ); |
| 110 | 110 | |
| 111 | 111 | $optional_data = ""; |
| 112 | - foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | - if ( $xml_value ) { |
|
| 112 | + foreach ($optional_fields as $xml_key => $xml_value) { |
|
| 113 | + if ($xml_value) { |
|
| 114 | 114 | $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -11,172 +11,172 @@ |
||
| 11 | 11 | use Wordlift\Videoobject\Data\Video_Storage\Storage; |
| 12 | 12 | |
| 13 | 13 | class Jsonld { |
| 14 | - /** |
|
| 15 | - * @var Storage |
|
| 16 | - */ |
|
| 17 | - private $video_storage; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Jsonld constructor. |
|
| 21 | - * |
|
| 22 | - * @param $video_storage Storage |
|
| 23 | - */ |
|
| 24 | - public function __construct( $video_storage ) { |
|
| 25 | - add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 ); |
|
| 26 | - add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 ); |
|
| 27 | - $this->video_storage = $video_storage; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - public function wl_after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 32 | - if ( 0 === count( $jsonld ) ) { |
|
| 33 | - return $jsonld; |
|
| 34 | - } |
|
| 35 | - $current_item = $jsonld[0]; |
|
| 36 | - |
|
| 37 | - if ( ! array_key_exists( '@type', $current_item ) ) { |
|
| 38 | - // Cant determine type return early. |
|
| 39 | - return $jsonld; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $type = $current_item['@type']; |
|
| 43 | - if ( ( is_string( $type ) && $type === 'Article' ) || |
|
| 44 | - ( is_array( $type ) && in_array( 'Article', $type ) ) ) { |
|
| 45 | - return $jsonld; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $videos_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 49 | - if ( 0 === count( $videos_jsonld ) ) { |
|
| 50 | - return $jsonld; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // check if we have @id in jsonld for first item. |
|
| 54 | - $id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : ''; |
|
| 55 | - |
|
| 56 | - foreach ( $videos_jsonld as &$video_jsonld ) { |
|
| 57 | - if ( ! $id ) { |
|
| 58 | - continue; |
|
| 59 | - } |
|
| 60 | - if ( ! array_key_exists( 'mentions', $video_jsonld ) ) { |
|
| 61 | - $video_jsonld['mentions'] = array( '@id' => $id ); |
|
| 62 | - } else { |
|
| 63 | - $video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) ); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return array_merge( $jsonld, $videos_jsonld ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param $existing_video_data string | array associative or sequential array. |
|
| 72 | - * @param $new_video_data array Sequential array. |
|
| 73 | - * |
|
| 74 | - * @return array |
|
| 75 | - */ |
|
| 76 | - private function merge_video_data( $existing_video_data, $new_video_data ) { |
|
| 77 | - if ( ! is_array( $existing_video_data ) ) { |
|
| 78 | - $new_video_data[] = $existing_video_data; |
|
| 79 | - |
|
| 80 | - return $new_video_data; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - if ( $this->is_associative_array( $existing_video_data ) ) { |
|
| 84 | - $new_video_data[] = $existing_video_data; |
|
| 85 | - |
|
| 86 | - return $new_video_data; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return array_merge( $existing_video_data, $new_video_data ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function wl_post_jsonld( $jsonld, $post_id, $references ) { |
|
| 93 | - |
|
| 94 | - $video_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 95 | - if ( count( $video_jsonld ) === 0 ) { |
|
| 96 | - return $jsonld; |
|
| 97 | - } |
|
| 98 | - // Before adding the video jsonld check if the key |
|
| 99 | - // is present and additional data might be present, |
|
| 100 | - // if not present just add the data and return early. |
|
| 101 | - if ( ! array_key_exists( 'video', $jsonld ) ) { |
|
| 102 | - $jsonld['video'] = $video_jsonld; |
|
| 103 | - |
|
| 104 | - return $jsonld; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // since key exists, we need to merge the data based on type. |
|
| 108 | - $previous_video_data = $jsonld['video']; |
|
| 109 | - $jsonld['video'] = $this->merge_video_data( $previous_video_data, $video_jsonld ); |
|
| 110 | - |
|
| 111 | - return $jsonld; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @param $post_id int Post id. |
|
| 117 | - * |
|
| 118 | - * @return array |
|
| 119 | - */ |
|
| 120 | - public function get_videos_jsonld( $post_id ) { |
|
| 121 | - |
|
| 122 | - $videos = $this->video_storage->get_all_videos( $post_id ); |
|
| 123 | - |
|
| 124 | - $jsonld = array(); |
|
| 125 | - |
|
| 126 | - foreach ( $videos as $video ) { |
|
| 127 | - /** |
|
| 128 | - * @var $video Video |
|
| 129 | - */ |
|
| 130 | - $description = $video->description; |
|
| 131 | - if ( ! $video->description ) { |
|
| 132 | - // If description is empty then use the video title as description |
|
| 133 | - $description = $video->name; |
|
| 134 | - } |
|
| 135 | - $single_jsonld = array( |
|
| 136 | - '@context' => 'http://schema.org', |
|
| 137 | - '@type' => 'VideoObject', |
|
| 138 | - 'name' => $video->name, |
|
| 139 | - 'description' => $description, |
|
| 140 | - 'contentUrl' => $video->content_url, |
|
| 141 | - 'embedUrl' => $video->embed_url, |
|
| 142 | - 'uploadDate' => $video->upload_date, |
|
| 143 | - 'thumbnailUrl' => $video->thumbnail_urls, |
|
| 144 | - 'duration' => $video->duration, |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - if ( $video->views ) { |
|
| 148 | - $single_jsonld['interactionStatistic'] = array( |
|
| 149 | - '@type' => 'InteractionCounter', |
|
| 150 | - 'interactionType' => array( |
|
| 151 | - '@type' => 'http://schema.org/WatchAction' |
|
| 152 | - ), |
|
| 153 | - 'userInteractionCount' => $video->views |
|
| 154 | - ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if ( $video->is_live_video ) { |
|
| 158 | - $single_jsonld['publication'] = array( |
|
| 159 | - '@type' => 'BroadcastEvent', |
|
| 160 | - 'isLiveBroadcast' => true, |
|
| 161 | - 'startDate' => $video->live_video_start_date, |
|
| 162 | - 'endDate' => $video->live_video_end_date |
|
| 163 | - ); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $jsonld[] = $single_jsonld; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $jsonld; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - private function is_associative_array( $arr ) { |
|
| 174 | - if ( array() === $arr ) { |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
| 179 | - } |
|
| 14 | + /** |
|
| 15 | + * @var Storage |
|
| 16 | + */ |
|
| 17 | + private $video_storage; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Jsonld constructor. |
|
| 21 | + * |
|
| 22 | + * @param $video_storage Storage |
|
| 23 | + */ |
|
| 24 | + public function __construct( $video_storage ) { |
|
| 25 | + add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 ); |
|
| 26 | + add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 ); |
|
| 27 | + $this->video_storage = $video_storage; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + public function wl_after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 32 | + if ( 0 === count( $jsonld ) ) { |
|
| 33 | + return $jsonld; |
|
| 34 | + } |
|
| 35 | + $current_item = $jsonld[0]; |
|
| 36 | + |
|
| 37 | + if ( ! array_key_exists( '@type', $current_item ) ) { |
|
| 38 | + // Cant determine type return early. |
|
| 39 | + return $jsonld; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $type = $current_item['@type']; |
|
| 43 | + if ( ( is_string( $type ) && $type === 'Article' ) || |
|
| 44 | + ( is_array( $type ) && in_array( 'Article', $type ) ) ) { |
|
| 45 | + return $jsonld; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $videos_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 49 | + if ( 0 === count( $videos_jsonld ) ) { |
|
| 50 | + return $jsonld; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // check if we have @id in jsonld for first item. |
|
| 54 | + $id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : ''; |
|
| 55 | + |
|
| 56 | + foreach ( $videos_jsonld as &$video_jsonld ) { |
|
| 57 | + if ( ! $id ) { |
|
| 58 | + continue; |
|
| 59 | + } |
|
| 60 | + if ( ! array_key_exists( 'mentions', $video_jsonld ) ) { |
|
| 61 | + $video_jsonld['mentions'] = array( '@id' => $id ); |
|
| 62 | + } else { |
|
| 63 | + $video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) ); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return array_merge( $jsonld, $videos_jsonld ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param $existing_video_data string | array associative or sequential array. |
|
| 72 | + * @param $new_video_data array Sequential array. |
|
| 73 | + * |
|
| 74 | + * @return array |
|
| 75 | + */ |
|
| 76 | + private function merge_video_data( $existing_video_data, $new_video_data ) { |
|
| 77 | + if ( ! is_array( $existing_video_data ) ) { |
|
| 78 | + $new_video_data[] = $existing_video_data; |
|
| 79 | + |
|
| 80 | + return $new_video_data; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + if ( $this->is_associative_array( $existing_video_data ) ) { |
|
| 84 | + $new_video_data[] = $existing_video_data; |
|
| 85 | + |
|
| 86 | + return $new_video_data; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return array_merge( $existing_video_data, $new_video_data ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function wl_post_jsonld( $jsonld, $post_id, $references ) { |
|
| 93 | + |
|
| 94 | + $video_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 95 | + if ( count( $video_jsonld ) === 0 ) { |
|
| 96 | + return $jsonld; |
|
| 97 | + } |
|
| 98 | + // Before adding the video jsonld check if the key |
|
| 99 | + // is present and additional data might be present, |
|
| 100 | + // if not present just add the data and return early. |
|
| 101 | + if ( ! array_key_exists( 'video', $jsonld ) ) { |
|
| 102 | + $jsonld['video'] = $video_jsonld; |
|
| 103 | + |
|
| 104 | + return $jsonld; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // since key exists, we need to merge the data based on type. |
|
| 108 | + $previous_video_data = $jsonld['video']; |
|
| 109 | + $jsonld['video'] = $this->merge_video_data( $previous_video_data, $video_jsonld ); |
|
| 110 | + |
|
| 111 | + return $jsonld; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @param $post_id int Post id. |
|
| 117 | + * |
|
| 118 | + * @return array |
|
| 119 | + */ |
|
| 120 | + public function get_videos_jsonld( $post_id ) { |
|
| 121 | + |
|
| 122 | + $videos = $this->video_storage->get_all_videos( $post_id ); |
|
| 123 | + |
|
| 124 | + $jsonld = array(); |
|
| 125 | + |
|
| 126 | + foreach ( $videos as $video ) { |
|
| 127 | + /** |
|
| 128 | + * @var $video Video |
|
| 129 | + */ |
|
| 130 | + $description = $video->description; |
|
| 131 | + if ( ! $video->description ) { |
|
| 132 | + // If description is empty then use the video title as description |
|
| 133 | + $description = $video->name; |
|
| 134 | + } |
|
| 135 | + $single_jsonld = array( |
|
| 136 | + '@context' => 'http://schema.org', |
|
| 137 | + '@type' => 'VideoObject', |
|
| 138 | + 'name' => $video->name, |
|
| 139 | + 'description' => $description, |
|
| 140 | + 'contentUrl' => $video->content_url, |
|
| 141 | + 'embedUrl' => $video->embed_url, |
|
| 142 | + 'uploadDate' => $video->upload_date, |
|
| 143 | + 'thumbnailUrl' => $video->thumbnail_urls, |
|
| 144 | + 'duration' => $video->duration, |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + if ( $video->views ) { |
|
| 148 | + $single_jsonld['interactionStatistic'] = array( |
|
| 149 | + '@type' => 'InteractionCounter', |
|
| 150 | + 'interactionType' => array( |
|
| 151 | + '@type' => 'http://schema.org/WatchAction' |
|
| 152 | + ), |
|
| 153 | + 'userInteractionCount' => $video->views |
|
| 154 | + ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if ( $video->is_live_video ) { |
|
| 158 | + $single_jsonld['publication'] = array( |
|
| 159 | + '@type' => 'BroadcastEvent', |
|
| 160 | + 'isLiveBroadcast' => true, |
|
| 161 | + 'startDate' => $video->live_video_start_date, |
|
| 162 | + 'endDate' => $video->live_video_end_date |
|
| 163 | + ); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $jsonld[] = $single_jsonld; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $jsonld; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + private function is_associative_array( $arr ) { |
|
| 174 | + if ( array() === $arr ) { |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | 181 | |
| 182 | 182 | } |
@@ -21,50 +21,50 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @param $video_storage Storage |
| 23 | 23 | */ |
| 24 | - public function __construct( $video_storage ) { |
|
| 25 | - add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 ); |
|
| 26 | - add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 ); |
|
| 24 | + public function __construct($video_storage) { |
|
| 25 | + add_action('wl_post_jsonld', array($this, 'wl_post_jsonld'), 10, 3); |
|
| 26 | + add_action('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 10, 3); |
|
| 27 | 27 | $this->video_storage = $video_storage; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | |
| 31 | - public function wl_after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 32 | - if ( 0 === count( $jsonld ) ) { |
|
| 31 | + public function wl_after_get_jsonld($jsonld, $post_id, $context) { |
|
| 32 | + if (0 === count($jsonld)) { |
|
| 33 | 33 | return $jsonld; |
| 34 | 34 | } |
| 35 | 35 | $current_item = $jsonld[0]; |
| 36 | 36 | |
| 37 | - if ( ! array_key_exists( '@type', $current_item ) ) { |
|
| 37 | + if ( ! array_key_exists('@type', $current_item)) { |
|
| 38 | 38 | // Cant determine type return early. |
| 39 | 39 | return $jsonld; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | $type = $current_item['@type']; |
| 43 | - if ( ( is_string( $type ) && $type === 'Article' ) || |
|
| 44 | - ( is_array( $type ) && in_array( 'Article', $type ) ) ) { |
|
| 43 | + if ((is_string($type) && $type === 'Article') || |
|
| 44 | + (is_array($type) && in_array('Article', $type))) { |
|
| 45 | 45 | return $jsonld; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - $videos_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 49 | - if ( 0 === count( $videos_jsonld ) ) { |
|
| 48 | + $videos_jsonld = $this->get_videos_jsonld($post_id); |
|
| 49 | + if (0 === count($videos_jsonld)) { |
|
| 50 | 50 | return $jsonld; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // check if we have @id in jsonld for first item. |
| 54 | - $id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : ''; |
|
| 54 | + $id = array_key_exists('@id', $current_item) ? $current_item['@id'] : ''; |
|
| 55 | 55 | |
| 56 | - foreach ( $videos_jsonld as &$video_jsonld ) { |
|
| 57 | - if ( ! $id ) { |
|
| 56 | + foreach ($videos_jsonld as &$video_jsonld) { |
|
| 57 | + if ( ! $id) { |
|
| 58 | 58 | continue; |
| 59 | 59 | } |
| 60 | - if ( ! array_key_exists( 'mentions', $video_jsonld ) ) { |
|
| 61 | - $video_jsonld['mentions'] = array( '@id' => $id ); |
|
| 60 | + if ( ! array_key_exists('mentions', $video_jsonld)) { |
|
| 61 | + $video_jsonld['mentions'] = array('@id' => $id); |
|
| 62 | 62 | } else { |
| 63 | - $video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) ); |
|
| 63 | + $video_jsonld['mentions'] = array_merge($video_jsonld['mentions'], array('@id' => $id)); |
|
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - return array_merge( $jsonld, $videos_jsonld ); |
|
| 67 | + return array_merge($jsonld, $videos_jsonld); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -73,32 +73,32 @@ discard block |
||
| 73 | 73 | * |
| 74 | 74 | * @return array |
| 75 | 75 | */ |
| 76 | - private function merge_video_data( $existing_video_data, $new_video_data ) { |
|
| 77 | - if ( ! is_array( $existing_video_data ) ) { |
|
| 76 | + private function merge_video_data($existing_video_data, $new_video_data) { |
|
| 77 | + if ( ! is_array($existing_video_data)) { |
|
| 78 | 78 | $new_video_data[] = $existing_video_data; |
| 79 | 79 | |
| 80 | 80 | return $new_video_data; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - if ( $this->is_associative_array( $existing_video_data ) ) { |
|
| 83 | + if ($this->is_associative_array($existing_video_data)) { |
|
| 84 | 84 | $new_video_data[] = $existing_video_data; |
| 85 | 85 | |
| 86 | 86 | return $new_video_data; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - return array_merge( $existing_video_data, $new_video_data ); |
|
| 89 | + return array_merge($existing_video_data, $new_video_data); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - public function wl_post_jsonld( $jsonld, $post_id, $references ) { |
|
| 92 | + public function wl_post_jsonld($jsonld, $post_id, $references) { |
|
| 93 | 93 | |
| 94 | - $video_jsonld = $this->get_videos_jsonld( $post_id ); |
|
| 95 | - if ( count( $video_jsonld ) === 0 ) { |
|
| 94 | + $video_jsonld = $this->get_videos_jsonld($post_id); |
|
| 95 | + if (count($video_jsonld) === 0) { |
|
| 96 | 96 | return $jsonld; |
| 97 | 97 | } |
| 98 | 98 | // Before adding the video jsonld check if the key |
| 99 | 99 | // is present and additional data might be present, |
| 100 | 100 | // if not present just add the data and return early. |
| 101 | - if ( ! array_key_exists( 'video', $jsonld ) ) { |
|
| 101 | + if ( ! array_key_exists('video', $jsonld)) { |
|
| 102 | 102 | $jsonld['video'] = $video_jsonld; |
| 103 | 103 | |
| 104 | 104 | return $jsonld; |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | |
| 107 | 107 | // since key exists, we need to merge the data based on type. |
| 108 | 108 | $previous_video_data = $jsonld['video']; |
| 109 | - $jsonld['video'] = $this->merge_video_data( $previous_video_data, $video_jsonld ); |
|
| 109 | + $jsonld['video'] = $this->merge_video_data($previous_video_data, $video_jsonld); |
|
| 110 | 110 | |
| 111 | 111 | return $jsonld; |
| 112 | 112 | } |
@@ -117,18 +117,18 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @return array |
| 119 | 119 | */ |
| 120 | - public function get_videos_jsonld( $post_id ) { |
|
| 120 | + public function get_videos_jsonld($post_id) { |
|
| 121 | 121 | |
| 122 | - $videos = $this->video_storage->get_all_videos( $post_id ); |
|
| 122 | + $videos = $this->video_storage->get_all_videos($post_id); |
|
| 123 | 123 | |
| 124 | 124 | $jsonld = array(); |
| 125 | 125 | |
| 126 | - foreach ( $videos as $video ) { |
|
| 126 | + foreach ($videos as $video) { |
|
| 127 | 127 | /** |
| 128 | 128 | * @var $video Video |
| 129 | 129 | */ |
| 130 | 130 | $description = $video->description; |
| 131 | - if ( ! $video->description ) { |
|
| 131 | + if ( ! $video->description) { |
|
| 132 | 132 | // If description is empty then use the video title as description |
| 133 | 133 | $description = $video->name; |
| 134 | 134 | } |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | 'duration' => $video->duration, |
| 145 | 145 | ); |
| 146 | 146 | |
| 147 | - if ( $video->views ) { |
|
| 147 | + if ($video->views) { |
|
| 148 | 148 | $single_jsonld['interactionStatistic'] = array( |
| 149 | 149 | '@type' => 'InteractionCounter', |
| 150 | 150 | 'interactionType' => array( |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | ); |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - if ( $video->is_live_video ) { |
|
| 157 | + if ($video->is_live_video) { |
|
| 158 | 158 | $single_jsonld['publication'] = array( |
| 159 | 159 | '@type' => 'BroadcastEvent', |
| 160 | 160 | 'isLiveBroadcast' => true, |
@@ -170,12 +170,12 @@ discard block |
||
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | |
| 173 | - private function is_associative_array( $arr ) { |
|
| 174 | - if ( array() === $arr ) { |
|
| 173 | + private function is_associative_array($arr) { |
|
| 174 | + if (array() === $arr) { |
|
| 175 | 175 | return false; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | - return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
| 178 | + return array_keys($arr) !== range(0, count($arr) - 1); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |