BaseDeletePage   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 239
ccs 0
cts 96
cp 0
rs 10
c 0
b 0
f 0
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 2
A register_hooks() 0 5 1
A add_module() 0 6 2
A get_module() 0 8 2
A register_modules() 0 12 2
A render_nonce_fields() 0 6 1
A has_modules() 0 2 1
A on_load_page() 0 2 1
A get_item_type() 0 2 1
A render_footer() 0 11 1
A render_body() 0 2 1
A enqueue_assets() 0 97 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
 * Base class for all Bulk Delete pages that will have modules.
11
 *
12
 * @since 6.0.0
13
 */
14
abstract class BaseDeletePage extends BasePage {
15
	/**
16
	 * Item Type. Possible values 'posts', 'pages', 'users' etc.
17
	 *
18
	 * @var string
19
	 */
20
	protected $item_type;
21
22
	/**
23
	 * Modules registered to this page.
24
	 *
25
	 * @var \BulkWP\BulkDelete\Core\Base\BaseModule[]
26
	 */
27
	protected $modules = array();
28
29
	/**
30
	 * Register the modules after the page is registered.
31
	 */
32
	public function register() {
33
		parent::register();
34
35
		if ( $this->has_modules() ) {
36
			$this->register_modules();
37
		}
38
	}
39
40
	/**
41
	 * Add a module to the page.
42
	 *
43
	 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module Module to add.
44
	 */
45
	public function add_module( $module ) {
46
		if ( in_array( $module, $this->modules, true ) ) {
47
			return;
48
		}
49
50
		$this->modules[ $module->get_name() ] = $module;
51
	}
52
53
	/**
54
	 * Get module object instance by module class name.
55
	 *
56
	 * @param string $module_class_name Module class name.
57
	 *
58
	 * @return \BulkWP\BulkDelete\Core\Base\BaseModule|null Module object instance or null if no match found.
59
	 */
60
	public function get_module( $module_class_name ) {
61
		$short_class_name = bd_get_short_class_name( $module_class_name );
62
63
		if ( isset( $this->modules[ $short_class_name ] ) ) {
64
			return $this->modules[ $short_class_name ];
65
		}
66
67
		return null;
68
	}
69
70
	protected function register_hooks() {
71
		parent::register_hooks();
72
73
		add_action( 'admin_print_scripts-' . $this->hook_suffix, array( $this, 'enqueue_assets' ) );
74
		add_action( "load-{$this->hook_suffix}", array( $this, 'on_load_page' ) );
75
	}
76
77
	/**
78
	 * Enqueue Scripts and Styles.
79
	 */
80
	public function enqueue_assets() {
81
		/**
82
		 * Runs just before enqueuing scripts and styles in all Bulk WP admin pages.
83
		 *
84
		 * This action is primarily for registering or deregistering additional scripts or styles.
85
		 *
86
		 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
87
		 *
88
		 * @since 5.5.1
89
		 * @since 6.0.0 Added $page parameter.
90
		 */
91
		do_action( 'bd_before_admin_enqueue_scripts', $this );
92
93
		/**
94
		 * Runs just before enqueuing scripts and styles in a Bulk WP admin pages.
95
		 *
96
		 * This action is primarily for registering or deregistering additional scripts or styles.
97
		 *
98
		 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
99
		 *
100
		 * @since 6.0.1
101
		 */
102
		do_action( "bd_before_enqueue_page_assets_for_{$this->get_page_slug()}", $this );
103
104
		wp_enqueue_style( 'jquery-ui-smoothness', $this->get_plugin_dir_url() . 'assets/css/jquery-ui-smoothness.min.css', array(), '1.12.1' );
105
106
		wp_enqueue_script(
107
			'jquery-ui-timepicker-addon',
108
			$this->get_plugin_dir_url() . 'assets/js/jquery-ui-timepicker-addon.min.js',
109
			array( 'jquery-ui-slider', 'jquery-ui-datepicker' ),
110
			'1.6.3',
111
			true
112
		);
113
		wp_enqueue_style( 'jquery-ui-timepicker', $this->get_plugin_dir_url() . 'assets/css/jquery-ui-timepicker-addon.min.css', array( 'jquery-ui-smoothness' ), '1.6.3' );
114
115
		wp_enqueue_script( 'select2', $this->get_plugin_dir_url() . 'assets/js/select2.min.js', array( 'jquery' ), '4.0.5', true );
116
		wp_enqueue_style( 'select2', $this->get_plugin_dir_url() . 'assets/css/select2.min.css', array(), '4.0.5' );
117
118
		$postfix = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
119
		wp_enqueue_script(
120
			'bulk-delete',
121
			$this->get_plugin_dir_url() . 'assets/js/bulk-delete' . $postfix . '.js',
122
			array( 'jquery-ui-timepicker-addon', 'jquery-ui-tooltip', 'postbox' ),
123
			BulkDelete::VERSION,
124
			true
125
		);
126
		wp_enqueue_style(
127
			'bulk-delete',
128
			$this->get_plugin_dir_url() . 'assets/css/bulk-delete' . $postfix . '.css',
129
			array( 'jquery-ui-smoothness', 'jquery-ui-timepicker', 'select2' ),
130
			BulkDelete::VERSION
131
		);
132
133
		/**
134
		 * Filter JavaScript array.
135
		 *
136
		 * This filter can be used to extend the array that is passed to JavaScript
137
		 *
138
		 * @since 5.4
139
		 */
140
		$translation_array = apply_filters(
141
			'bd_javascript_array',
142
			array(
143
				'msg'              => array(),
144
				'validators'       => array(),
145
				'dt_iterators'     => array(),
146
				'pre_action_msg'   => array(), // deprecated since 6.0.1.
147
				'pre_delete_msg'   => array(),
148
				'pre_schedule_msg' => array(),
149
				'error_msg'        => array(),
150
				'pro_iterators'    => array(),
151
			)
152
		);
153
		wp_localize_script( 'bulk-delete', 'BulkWP', $translation_array ); // TODO: Change JavaScript variable to BulkWP.BulkDelete.
154
155
		/**
156
		 * Runs just after enqueuing scripts and styles in all Bulk WP admin pages.
157
		 *
158
		 * This action is primarily for registering additional scripts or styles.
159
		 *
160
		 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
161
		 *
162
		 * @since 5.5.1
163
		 * @since 6.0.0 Added $page parameter.
164
		 */
165
		do_action( 'bd_after_admin_enqueue_scripts', $this );
166
167
		/**
168
		 * Runs just after enqueuing scripts and styles in a Bulk WP admin pages.
169
		 *
170
		 * This action is primarily for registering or deregistering additional scripts or styles.
171
		 *
172
		 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
173
		 *
174
		 * @since 6.0.1
175
		 */
176
		do_action( "bd_after_enqueue_page_assets_for_{$this->get_page_slug()}", $this );
177
	}
178
179
	/**
180
	 * Trigger the add_meta_boxes hooks to allow modules to be added when the page is loaded.
181
	 */
182
	public function on_load_page() {
183
		do_action( 'add_meta_boxes_' . $this->hook_suffix, null );
184
	}
185
186
	/**
187
	 * Add additional nonce fields that are related to modules.
188
	 */
189
	protected function render_nonce_fields() {
190
		parent::render_nonce_fields();
191
192
		// Used to save closed meta boxes and their order.
193
		wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
194
		wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
195
	}
196
197
	/**
198
	 * Render meta boxes in body.
199
	 */
200
	protected function render_body() {
201
		do_meta_boxes( '', 'advanced', null );
202
	}
203
204
	/**
205
	 * Render footer.
206
	 */
207
	protected function render_footer() {
208
		parent::render_footer();
209
210
		/**
211
		 * Runs just before displaying the footer text in the admin page.
212
		 *
213
		 * This action is primarily for adding extra content in the footer of admin page.
214
		 *
215
		 * @since 5.5.4
216
		 */
217
		do_action( "bd_admin_footer_for_{$this->item_type}" );
218
	}
219
220
	/**
221
	 * Does this page have any modules?
222
	 *
223
	 * @return bool True if page has modules, False otherwise.
224
	 */
225
	protected function has_modules() {
226
		return ! empty( $this->modules );
227
	}
228
229
	/**
230
	 * Load all the registered modules.
231
	 */
232
	protected function register_modules() {
233
		foreach ( $this->modules as $module ) {
234
			$module->register( $this->hook_suffix, $this->page_slug );
235
			$this->actions[] = $module->get_action();
236
		}
237
238
		/**
239
		 * Triggered after all modules are registered.
240
		 *
241
		 * @since 6.0.0
242
		 */
243
		do_action( "bd_add_meta_box_for_{$this->get_item_type()}" );
244
	}
245
246
	/**
247
	 * Get the item type of the page.
248
	 *
249
	 * @return string Item type of the page.
250
	 */
251
	public function get_item_type() {
252
		return $this->item_type;
253
	}
254
}
255