Completed
Pull Request — master (#142)
by Sudar
02:02
created

BD_Scheduler_Addon::get_cron_hook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Base class for all Scheduler Addons.
4
 *
5
 * @since   5.5
6
 *
7
 * @author  Sudar
8
 *
9
 * @package BulkDelete\Addons\Base
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
/**
14
 * Base class for Base Addons.
15
 *
16
 * @abstract
17
 *
18
 * @since 5.5
19
 */
20
abstract class BD_Scheduler_Addon extends BD_Addon {
21
	/**
22
	 * @var No base addon for this scheduler addon.
23
	 */
24
	protected $no_base_addon = false;
25
26
	/**
27
	 * @var Base addon name.
28
	 */
29
	protected $base_addon;
30
31
	/**
32
	 * @var Base addon version.
33
	 */
34
	protected $base_addon_version;
35
36
	/**
37
	 * @var Base addon class name.
38
	 */
39
	protected $base_addon_class_name;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $base_addon_class_name exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
40
41
	/**
42
	 * @var Base addon object.
43
	 */
44
	protected $base_addon_obj;
45
46
	/**
47
	 * @var Cron Hook to run the scheduler on.
48
	 */
49
	protected $cron_hook;
50
51
	/**
52
	 * Use `factory()` method to create instance of this class.
53
	 * Don't create instances directly.
54
	 *
55
	 * @since 5.5
56
	 * @see factory()
57
	 */
58
	public function __construct() {
59
		parent::__construct();
60
	}
61
62
	/**
63
	 * Check if the base addon is available.
64
	 *
65
	 * @access protected
66
	 *
67
	 * @since 5.5
68
	 *
69
	 * @todo check for version as well
70
	 *
71
	 * @param string Base Addon class name. Default null. If not specified then it is auto calculated based on addon name.
72
	 * @param mixed|null $addon_class_name
73
	 *
74
	 * @return bool True if base addon is found, False other wise
75
	 */
76
	protected function check_base_addon( $addon_class_name = null ) {
77
		if ( null == $addon_class_name ) {
78
			$this->base_addon_class_name = bd_get_addon_class_name( $this->base_addon );
0 ignored issues
show
Documentation Bug introduced by
It seems like bd_get_addon_class_name($this->base_addon) of type string is incompatible with the declared type object<Base> of property $base_addon_class_name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
		} else {
80
			$this->base_addon_class_name = $addon_class_name;
81
		}
82
83
		if ( class_exists( $this->base_addon_class_name ) ) {
84
			// Ugly workaround, since we need to support PHP 5.2
85
			$this->base_addon_obj = call_user_func( array( $this->base_addon_class_name, 'factory' ) );
86
87
			return true;
88
		} else {
89
			add_action( 'admin_notices', array( $this, 'addon_missing_notice' ) );
90
91
			return false;
92
		}
93
	}
94
95
	/**
96
	 * Show a notice if the base addon is not available.
97
	 *
98
	 * @since 5.5
99
	 */
100
	public function addon_missing_notice() {
101
		$campaign_args = array(
102
			'utm_source'   => 'wpadmin',
103
			'utm_campaign' => 'BulkDelete',
104
			'utm_medium'   => 'header-notice',
105
			'utm_content'  => $this->addon_code,
106
		);
107
		$addon_url = bd_get_addon_url( $this->base_addon, $campaign_args );
108
109
		printf(
110
			'<div class="error"><p>%s</p></div>',
111
			sprintf( __( '"%s" addon requires "<a href="%s" target="_blank">%s</a>" addon to be installed and activated!', 'bulk-delete' ), $this->addon_name, $addon_url , $this->base_addon )
112
		);
113
	}
114
115
	/**
116
	 * Setup hooks.
117
	 *
118
	 * @since 5.5
119
	 */
120
	protected function setup_hooks() {
121
		add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) );
122
123
		$cron_hook = $this->get_cron_hook();
124
		if ( ! empty( $cron_hook ) ) {
125
			add_action( $cron_hook, array( $this, 'do_delete' ), 10, 1 );
126
		}
127
	}
128
129
	/**
130
	 * Filter JS Array and add pro hooks.
131
	 *
132
	 * @since 5.5
133
	 *
134
	 * @param array $js_array JavaScript Array
135
	 *
136
	 * @return array Modified JavaScript Array
137
	 */
138
	public function filter_js_array( $js_array ) {
139
		$js_array['pro_iterators'][] = $this->get_module()->get_field_slug();
140
141
		return $js_array;
142
	}
143
144
	/**
145
	 * Hook handler.
146
	 *
147
	 * @since 5.5
148
	 *
149
	 * @param array $delete_options
150
	 */
151
	public function do_delete( $delete_options ) {
152
		do_action( 'bd_before_scheduler', $this->addon_name );
153
		$count = $this->get_module()->delete( $delete_options );
154
		do_action( 'bd_after_scheduler', $this->addon_name, $count );
155
	}
156
157
	/**
158
	 * Get the cron hook.
159
	 *
160
	 * @access protected
161
	 *
162
	 * @since 5.5
163
	 *
164
	 * @return string Cron hook.
165
	 */
166
	protected function get_cron_hook() {
167
		$cron_hook = '';
168
		if ( null != $this->base_addon_obj ) {
169
			$cron_hook = $this->base_addon_obj->get_cron_hook();
170
		}
171
172
		return $cron_hook;
173
	}
174
175
	/**
176
	 * Get base module.
177
	 *
178
	 * @access protected
179
	 *
180
	 * @since 5.5
181
	 *
182
	 * @return object Base module object
183
	 */
184
	protected function get_module() {
185
		if ( $this->no_base_addon ) {
186
			return $this->base_addon_obj;
187
		} else {
188
			return $this->base_addon_obj->get_module();
189
		}
190
	}
191
}
192