Completed
Push — dev/6.0.0 ( 66aafb...0eb546 )
by Sudar
43:54 queued 28:47
created

BaseScheduler::setup_module()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 item type that will be deleted by the Scheduler.
19
	 *
20
	 * @var string
21
	 */
22
	protected $item_type;
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
		$this->setup_module();
51
	}
52
53
	/**
54
	 * Setup module from class name.
55
	 */
56
	protected function setup_module() {
57
		$bd = BulkDelete::get_instance();
58
59
		$this->module = $bd->get_module( 'bulk-delete-' . $this->item_type, $this->module_class_name );
60
	}
61
62
	/**
63
	 * Register the scheduler.
64
	 *
65
	 * Setups the hooks and filters.
66
	 */
67
	public function register() {
68
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
69
70
		if ( is_null( $this->module ) ) {
71
			return;
72
		}
73
74
		$cron_hook = $this->module->get_cron_hook();
75
		if ( ! empty( $cron_hook ) ) {
76
			add_action( $cron_hook, array( $this, 'do_delete' ) );
77
		}
78
	}
79
80
	/**
81
	 * Filter JS Array and add pro hooks.
82
	 *
83
	 * @param array $js_array JavaScript Array.
84
	 *
85
	 * @return array Modified JavaScript Array
86
	 */
87
	public function filter_js_array( $js_array ) {
88
		$js_array['pro_iterators'][] = $this->module->get_field_slug();
89
90
		return $js_array;
91
	}
92
93
	/**
94
	 * Trigger the deletion.
95
	 *
96
	 * @param array $delete_options Delete options.
97
	 */
98
	public function do_delete( $delete_options ) {
99
		if ( is_null( $this->module ) ) {
100
			return;
101
		}
102
103
		/**
104
		 * Triggered before the scheduler is run.
105
		 *
106
		 * @since 6.0.0
107
		 *
108
		 * @param string $label Cron Label.
109
		 */
110
		do_action( 'bd_before_scheduler', $this->module->get_cron_label() );
111
112
		$items_deleted = $this->module->delete( $delete_options );
113
114
		/**
115
		 * Triggered after the scheduler is run.
116
		 *
117
		 * @since 6.0.0
118
		 *
119
		 * @param string $label         Cron Label.
120
		 * @param int    $items_deleted Number of items that were deleted.
121
		 */
122
		do_action( 'bd_after_scheduler', $this->module->get_cron_label(), $items_deleted );
123
	}
124
}
125