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

BaseDeletePage   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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