Completed
Push — develop ( 1d4633...83eb6a )
by David
02:35 queued 10s
created

Image_License_Scheduler::run()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file defines the scheduler that will get the domains linked via http using the {@link \Windowsreport_Companion\Http_Links\Http_Links_Service}
4
 * and it'll store the data using the {@link \Windowsreport_Companion\Http_Links\Http_Links_Domains_Data}.
5
 *
6
 * @author David Riccitelli <[email protected]>
7
 * @since 2.22.0
8
 * @package Windowsreport_Companion\Http_Links
9
 */
10
11
namespace Wordlift\Images_Licenses;
12
13
use Wordlift\Cache\Ttl_Cache;
14
15
class Image_License_Scheduler {
16
17
	const ACTION_NAME = 'wl_image_license_scheduler__run';
18
19
	/**
20
	 * @var Image_License_Service
21
	 */
22
	private $image_license_service;
23
	/**
24
	 * @var Ttl_Cache
25
	 */
26
	private $cache_service;
27
28
	/**
29
	 * @param Image_License_Service $image_license_service
30
	 * @param Ttl_Cache $cache_service
31
	 */
32
	public function __construct( $image_license_service, $cache_service ) {
33
34
		$this->image_license_service = $image_license_service;
35
		$this->cache_service         = $cache_service;
36
37
		// Do not bother to configure scheduled tasks while running on the front-end.
38
		add_action( 'wp_ajax_' . self::ACTION_NAME, array( $this, 'run' ) );
39
		add_action( self::ACTION_NAME, array( $this, 'run' ) );
40
41
		if ( is_admin() && ! wp_next_scheduled( self::ACTION_NAME ) ) {
42
			wp_schedule_event( time(), 'weekly', self::ACTION_NAME );
43
		}
44
45
	}
46
47
	public function run() {
48
49
		// Get and save the domains.
50
		$data = $this->image_license_service->get_non_public_domain_images();
51
52
		// Update the cache.
53
		$this->cache_service->put( Cached_Image_License_Service::GET_NON_PUBLIC_DOMAIN_IMAGES, $data );
54
55
		if ( wp_doing_ajax() &&
56
		     ( self::ACTION_NAME === filter_input( INPUT_GET, 'action' )
57
		       || self::ACTION_NAME === filter_input( INPUT_POST, 'action' ) ) ) {
58
			wp_send_json_success( $data );
59
		}
60
61
	}
62
63
}
64