|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @since 3.31.0 |
|
4
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Wordlift\Videoobject\Sitemap; |
|
8
|
|
|
|
|
9
|
|
|
use Wordlift\Videoobject\Data\Video\Video; |
|
10
|
|
|
use Wordlift\Videoobject\Data\Video_Storage\Meta_Storage; |
|
11
|
|
|
use Wordlift\Videoobject\Data\Video_Storage\Video_Storage_Factory; |
|
12
|
|
|
|
|
13
|
|
|
class Xml_Generator { |
|
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
|
|
|
$posts = get_posts( array( |
|
34
|
|
|
'fields' => 'ids', |
|
35
|
|
|
'numberposts' => - 1, |
|
36
|
|
|
'meta_query' => array( |
|
37
|
|
|
array( |
|
38
|
|
|
'key' => Meta_Storage::META_KEY, |
|
39
|
|
|
'compare' => 'EXISTS' |
|
40
|
|
|
) |
|
41
|
|
|
) |
|
42
|
|
|
) ); |
|
43
|
|
|
|
|
44
|
|
|
$all_posts_xml = ""; |
|
45
|
|
|
|
|
46
|
|
|
if ( ! $posts ) { |
|
47
|
|
|
return $all_posts_xml; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
foreach ( $posts as $post_id ) { |
|
51
|
|
|
$all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $all_posts_xml; |
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $post_id |
|
61
|
|
|
* |
|
62
|
|
|
* @return string XML string for single post. |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function get_xml_for_single_post( $post_id ) { |
|
65
|
|
|
$videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
66
|
|
|
if ( ! $videos ) { |
|
|
|
|
|
|
67
|
|
|
return ""; |
|
68
|
|
|
} |
|
69
|
|
|
$single_post_xml = ""; |
|
70
|
|
|
foreach ( $videos as $video ) { |
|
71
|
|
|
$single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $single_post_xml; |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $video Video |
|
81
|
|
|
* @param $post_id int |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function get_xml_for_single_video( $video, $post_id ) { |
|
86
|
|
|
|
|
87
|
|
|
$permalink = get_permalink( $post_id ); |
|
88
|
|
|
$title = esc_html( $video->name ); |
|
89
|
|
|
$description = esc_html( $video->description ); |
|
90
|
|
|
$thumbnail_url = $video->thumbnail_urls[0]; |
|
91
|
|
|
$content_url = $video->content_url; |
|
92
|
|
|
$embed_url = $video->embed_url; |
|
93
|
|
|
$duration_in_seconds = self::iso8601_to_seconds( $video->duration ); |
|
94
|
|
|
$is_live_video = $video->is_live_video ? 'yes' : 'no'; |
|
95
|
|
|
$view_count = $video->views; |
|
96
|
|
|
|
|
97
|
|
|
return <<<EOF |
|
98
|
|
|
<url> |
|
99
|
|
|
<loc>${permalink}</loc> |
|
100
|
|
|
<video:video> |
|
101
|
|
|
<video:thumbnail_loc>${thumbnail_url}</video:thumbnail_loc> |
|
102
|
|
|
<video:title>${title}</video:title> |
|
103
|
|
|
<video:description>${description}</video:description> |
|
104
|
|
|
<video:content_loc>${content_url}</video:content_loc> |
|
105
|
|
|
<video:player_loc>${embed_url}</video:player_loc> |
|
106
|
|
|
<video:duration>${duration_in_seconds}</video:duration> |
|
107
|
|
|
<video:view_count>${view_count}</video:view_count> |
|
108
|
|
|
<video:live>${is_live_video}</video:live> |
|
109
|
|
|
</video:video> |
|
110
|
|
|
</url> |
|
111
|
|
|
EOF; |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.