Completed
Push — 247-fix/delete-term-meta ( d5f818...2a1fe3 )
by Sudar
67:09 queued 61:16
created

BaseScheduler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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 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_action( 'init', array( $this, 'setup_cron' ) );
59
60
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
61
	}
62
63
	/**
64
	 * Setup cron job.
65
	 */
66
	public function setup_cron() {
67
		$this->setup_module();
68
69
		$cron_hook = $this->module->get_cron_hook();
0 ignored issues
show
Bug introduced by
The method get_cron_hook() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
		/** @scrutinizer ignore-call */ 
70
  $cron_hook = $this->module->get_cron_hook();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
		if ( ! empty( $cron_hook ) ) {
71
			add_action( $cron_hook, array( $this, 'do_delete' ) );
72
		}
73
	}
74
75
	/**
76
	 * Setup module from class name.
77
	 */
78
	protected function setup_module() {
79
		$bd = BulkDelete::get_instance();
80
81
		$this->module = $bd->get_module( $this->page_slug, $this->module_class_name );
82
	}
83
84
	/**
85
	 * Filter JS Array and add pro hooks.
86
	 *
87
	 * @param array $js_array JavaScript Array.
88
	 *
89
	 * @return array Modified JavaScript Array
90
	 */
91
	public function filter_js_array( $js_array ) {
92
		$js_array['pro_iterators'][] = $this->module->get_field_slug();
93
94
		return $js_array;
95
	}
96
97
	/**
98
	 * Trigger the deletion.
99
	 *
100
	 * @param array $delete_options Delete options.
101
	 */
102
	public function do_delete( $delete_options ) {
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