Completed
Push — 552-feature/reassign-contnet-o... ( 90950f...fe8e75 )
by Sudar
07:51 queued 02:59
created

BaseScheduler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A filter_js_array() 0 4 1
A __construct() 0 2 1
A register() 0 4 1
A setup_module() 0 4 1
A do_delete() 0 25 2
A setup_cron() 0 10 3
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Base;
4
5
use BulkWP\BulkDelete\Core\BulkDelete;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Encapsulates the logic for running a scheduler for a module.
11
 *
12
 * All Schedulers for Modules will be extending this class.
13
 *
14
 * @since 6.0.0
15
 */
16
abstract class BaseScheduler {
17
	/**
18
	 * The page slug of the module for which this is the Scheduler.
19
	 *
20
	 * @var string
21
	 */
22
	protected $page_slug;
23
24
	/**
25
	 * The class name of the module to which this is the scheduler.
26
	 *
27
	 * @var string
28
	 */
29
	protected $module_class_name;
30
31
	/**
32
	 * The module to which this is the scheduler.
33
	 *
34
	 * @var \BulkWP\BulkDelete\Core\Base\BaseModule
35
	 */
36
	protected $module = null;
37
38
	/**
39
	 * Initialize and setup variables.
40
	 *
41
	 * This method can be overridden by sub-classes if additional customization is needed.
42
	 */
43
	abstract protected function initialize();
44
45
	/**
46
	 * Create new instances of the Scheduler.
47
	 */
48
	public function __construct() {
49
		$this->initialize();
50
	}
51
52
	/**
53
	 * Register the scheduler.
54
	 *
55
	 * Setups the hooks and filters.
56
	 */
57
	public function register() {
58
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
59
60
		$this->setup_cron();
61
	}
62
63
	/**
64
	 * Setup cron job.
65
	 */
66
	public function setup_cron() {
67
		$this->setup_module();
68
69
		if ( is_null( $this->module ) ) {
70
			return;
71
		}
72
73
		$cron_hook = $this->module->get_cron_hook();
74
		if ( ! empty( $cron_hook ) ) {
75
			add_action( $cron_hook, array( $this, 'do_delete' ) );
76
		}
77
	}
78
79
	/**
80
	 * Setup module from class name.
81
	 */
82
	protected function setup_module() {
83
		$bd = BulkDelete::get_instance();
84
85
		$this->module = $bd->get_module( $this->page_slug, $this->module_class_name );
86
	}
87
88
	/**
89
	 * Filter JS Array and add pro hooks.
90
	 *
91
	 * @param array $js_array JavaScript Array.
92
	 *
93
	 * @return array Modified JavaScript Array
94
	 */
95
	public function filter_js_array( $js_array ) {
96
		$js_array['pro_iterators'][] = $this->module->get_field_slug();
97
98
		return $js_array;
99
	}
100
101
	/**
102
	 * Trigger the deletion.
103
	 *
104
	 * @param array $delete_options Delete options.
105
	 */
106
	public function do_delete( $delete_options ) {
107
		if ( is_null( $this->module ) ) {
108
			return;
109
		}
110
111
		/**
112
		 * Triggered before the scheduler is run.
113
		 *
114
		 * @since 6.0.0
115
		 *
116
		 * @param string $label Cron Label.
117
		 */
118
		do_action( 'bd_before_scheduler', $this->module->get_cron_label() );
119
120
		$items_deleted = $this->module->delete( $delete_options );
121
122
		/**
123
		 * Triggered after the scheduler is run.
124
		 *
125
		 * @since 6.0.0
126
		 *
127
		 * @param string $label         Cron Label.
128
		 * @param int    $items_deleted Number of items that were deleted.
129
		 */
130
		do_action( 'bd_after_scheduler', $this->module->get_cron_label(), $items_deleted );
131
	}
132
}
133