|
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\Cache\Ttl_Cache; |
|
10
|
|
|
|
|
11
|
|
|
class Video_Sitemap { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Ttl_Cache |
|
15
|
|
|
*/ |
|
16
|
|
|
private $sitemap_cache; |
|
17
|
|
|
|
|
18
|
|
|
const XML_CACHE_KEY = 'video_sitemap'; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( $sitemap_cache ) { |
|
21
|
|
|
$this->sitemap_cache = $sitemap_cache; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function init() { |
|
25
|
|
|
if ( self::is_video_sitemap_enabled() ) { |
|
26
|
|
|
add_action( 'template_redirect', array( $this, 'print_video_sitemap' ), 1 ); |
|
27
|
|
|
} |
|
28
|
|
|
add_action( 'wordlift_videoobject_video_storage_updated', array( $this, 'flush_cache' ) ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function flush_cache() { |
|
32
|
|
|
$this->sitemap_cache->flush(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Print video sitemap. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function print_video_sitemap() { |
|
39
|
|
|
global $wp; |
|
40
|
|
|
|
|
41
|
|
|
$url = home_url( $wp->request ); |
|
42
|
|
|
|
|
43
|
|
|
$pattern = '/wl-video-sitemap\.xml$/m'; |
|
44
|
|
|
|
|
45
|
|
|
if ( preg_match( $pattern, $url ) !== 1 ) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
header( "Content-type: text/xml" ); |
|
50
|
|
|
|
|
51
|
|
|
$xml = $this->sitemap_cache->get( self::XML_CACHE_KEY ); |
|
52
|
|
|
|
|
53
|
|
|
if ( ! $xml ) { |
|
54
|
|
|
$xml = $this->get_sitemap_xml(); |
|
55
|
|
|
$this->sitemap_cache->put( self::XML_CACHE_KEY, $xml ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
echo $xml; |
|
59
|
|
|
die(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function is_video_sitemap_enabled() { |
|
63
|
|
|
return intval( get_option( '_wl_video_sitemap_generation', false ) ) === 1; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
private function get_sitemap_xml() { |
|
70
|
|
|
$sitemap_start_tag = <<<EOF |
|
71
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
|
72
|
|
|
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> |
|
73
|
|
|
EOF; |
|
74
|
|
|
$sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
75
|
|
|
|
|
76
|
|
|
$sitemap_end_tag = "</urlset>"; |
|
77
|
|
|
|
|
78
|
|
|
$xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag; |
|
79
|
|
|
|
|
80
|
|
|
return $xml; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |