Completed
Pull Request — develop (#1350)
by Naveen
03:02
created

Video_Sitemap::remove_scheduled_generation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since 3.31.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Videoobject\Sitemap;
8
9
class Video_Sitemap {
10
11
	const CRON_ACTION_HOOK = 'wl_video_sitemap_generation';
12
13
	public function init() {
14
15
		add_action( 'wordlift_generate_video_sitemap_on', array( $this, 'schedule_generation' ) );
16
17
		add_action( self::CRON_ACTION_HOOK, array( $this, 'generate_video_sitemap' ) );
18
19
		add_action( 'wordlift_generate_video_sitemap_off', array( $this, 'remove_scheduled_generation' ) );
20
21
	}
22
23
	public function schedule_generation() {
24
		if ( ! wp_next_scheduled( self::CRON_ACTION_HOOK ) ) {
25
			wp_schedule_event( time(), 'daily', self::CRON_ACTION_HOOK );
26
		}
27
	}
28
29
	public function generate_video_sitemap() {
30
31
		$sitemap_start_tag = <<<EOF
32
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
33
        xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
34
EOF;
35
		$sitemap_body      = Xml_Generator::get_xml_for_all_posts_with_videos();
36
37
		$sitemap_end_tag = "</urlset>";
38
39
		$xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag;
40
41
		if ( ! defined( 'ABSPATH' ) ) {
42
			return;
43
		}
44
45
		file_put_contents( ABSPATH . 'wl-video-sitemap.xml', $xml );
46
47
	}
48
49
	public static function is_sitemap_already_generated() {
50
		if ( ! defined( 'ABSPATH' ) ) {
51
			return false;
52
		}
53
		return ABSPATH . 'wl-video-sitemap.xml';
54
	}
55
56
57
	public function remove_scheduled_generation() {
58
		if ( wp_next_scheduled( self::CRON_ACTION_HOOK ) ) {
59
			wp_clear_scheduled_hook( self::CRON_ACTION_HOOK );
60
		}
61
	}
62
63
}