Completed
Push — 137-feature/construct-post-mod... ( 2fadb4...c92423 )
by Maria Daniel Deepak
03:42
created

BD_Base_Page   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 363
Duplicated Lines 0 %

Test Coverage

Coverage 15.91%

Importance

Changes 0
Metric Value
dl 0
loc 363
ccs 14
cts 88
cp 0.1591
rs 10
c 0
b 0
f 0
wmc 24

18 Methods

Rating   Name   Duplication   Size   Complexity  
A get_screen() 0 2 1
A setup_hooks() 0 7 1
A get_page_slug() 0 2 1
A get_base_page_slug() 0 7 2
A nonce_check() 0 8 3
A __construct() 0 2 1
A add_help_tab() 0 2 1
A render_header() 0 4 1
A render_nonce_fields() 0 2 1
A modify_admin_footer() 0 2 1
A is_bulk_delete_menu_registered() 0 6 1
A add_script() 0 52 3
B render_page() 0 24 1
A setup() 0 3 1
A add_delete_settings_panel() 0 13 1
A render_footer() 0 9 1
A render_help_tab() 0 6 2
A add_menu() 0 11 1
1
<?php
2
/**
3
 * Base class for all Pages.
4
 *
5
 * @since   5.5.4
6
 *
7
 * @author  Sudar
8
 *
9
 * @package BulkDelete\Base\Page
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
/**
14
 * Base class for Pages.
15
 *
16
 * @abstract
17
 *
18
 * @since 5.5.4
19
 */
20
abstract class BD_Base_Page {
21
	/**
22
	 * @var string Page Slug.
23
	 */
24
	protected $page_slug;
25
26
	protected $item_type;
27
28
	/**
29
	 * @var string Menu action.
30
	 */
31
	protected $menu_action = 'bd_after_primary_menus';
32
33
	/**
34
	 * @var string Minimum capability needed for viewing this page.
35
	 */
36
	protected $capability = 'manage_options';
37
38
	// TODO: Remove property after confirming on `class-bd-settings-page.php` file.
39
	/**
40
	 * @var bool Whether sidebar is needed or not.
41
	 */
42
	protected $render_sidebar = false;
43
44
	/**
45
	 * @var string The screen variable for this page.
46
	 */
47
	protected $screen;
48
49
	/**
50
	 * @var array Labels used in this page.
51
	 */
52
	protected $label = array();
53
54
	/**
55
	 * @var array Messages shown to the user.
56
	 */
57
	protected $messages = array();
58
59
	/**
60
	 * @var array Actions used in this page.
61
	 */
62
	protected $actions = array();
63
64
	/**
65
	 * Initialize and setup variables.
66
	 *
67
	 * @since 5.5.4
68
	 * @abstract
69
	 *
70
	 * @return void
71
	 */
72
	abstract protected function initialize();
73
74
	/**
75
	 * Render body content.
76
	 *
77
	 * @since 5.5.4
78
	 * @abstract
79
	 *
80
	 * @return void
81
	 */
82
	abstract protected function render_body();
83
84
	/**
85
	 * Use `factory()` method to create instance of this class.
86
	 * Don't create instances directly.
87
	 *
88
	 * @since 5.5.4
89
	 * @see factory()
90
	 */
91 1
	public function __construct() {
92 1
		$this->setup();
93 1
	}
94
95
	/**
96
	 * Setup the module.
97
	 *
98
	 * @since 5.5.4
99
	 */
100 1
	protected function setup() {
101 1
		$this->initialize();
102 1
		$this->setup_hooks();
103 1
	}
104
105
	/**
106
	 * Setup hooks.
107
	 *
108
	 * @since 5.5.4
109
	 */
110 1
	protected function setup_hooks() {
111 1
		add_action( $this->menu_action, array( $this, 'add_menu' ) );
112 1
		add_action( "bd_admin_footer_for_{$this->page_slug}", array( $this, 'modify_admin_footer' ) );
113 1
		add_action( 'admin_print_scripts-' . $this->page_slug, array( $this, 'add_script' ) );
114
115 1
		add_filter( 'bd_action_nonce_check', array( $this, 'nonce_check' ), 10, 2 );
116 1
		add_filter( 'bd_admin_help_tabs', array( $this, 'render_help_tab' ), 10, 2 );
117 1
	}
118
119
	/**
120
	 * Add menu.
121
	 *
122
	 * @since 5.5.4
123
	 */
124
	public function add_menu() {
125
		$this->screen = add_submenu_page(
126
			$this->get_base_page_slug(),
127
			$this->label['page_title'],
128
			$this->label['menu_title'],
129
			$this->capability,
130
			$this->page_slug,
131
			array( $this, 'render_page' )
132
		);
133
134
		add_action( "load-{$this->screen}", array( $this, "add_delete_settings_panel" ) );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal add_delete_settings_panel does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
135
	}
136
137
	/**
138
	 * Check for nonce before executing the action.
139
	 *
140
	 * @since 5.5.4
141
	 *
142
	 * @param bool   $result The current result.
143
	 * @param string $action Action name.
144
	 */
145
	public function nonce_check( $result, $action ) {
146
		if ( in_array( $action, $this->actions ) ) {
147
			if ( check_admin_referer( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" ) ) {
148
				return true;
149
			}
150
		}
151
152
		return $result;
153
	}
154
155
	/**
156
	 * Modify help tabs for the current page.
157
	 *
158
	 * @since 5.5.4
159
	 *
160
	 * @param array  $help_tabs Current list of help tabs.
161
	 * @param string $screen    Current screen name.
162
	 *
163
	 * @return array Modified list of help tabs.
164
	 */
165
	public function render_help_tab( $help_tabs, $screen ) {
166
		if ( $this->screen == $screen ) {
167
			$help_tabs = $this->add_help_tab( $help_tabs );
168
		}
169
170
		return $help_tabs;
171
	}
172
173
	/**
174
	 * Add help tabs.
175
	 * Help tabs can be added by overriding this function in the child class.
176
	 *
177
	 * @since 5.5.4
178
	 *
179
	 * @param array $help_tabs Current list of help tabs.
180
	 *
181
	 * @return array List of help tabs.
182
	 */
183
	protected function add_help_tab( $help_tabs ) {
184
		return $help_tabs;
185
	}
186
187
	/**
188
	 * Render the page.
189
	 *
190
	 * @since 5.5.4
191
	 */
192
	public function render_page() {
193
?>
194
		<div class="wrap">
195
			<h2><?php echo $this->label['page_title'];?></h2>
196
			<?php settings_errors(); ?>
197
198
			<form method = "post">
199
			<?php $this->render_nonce_fields(); ?>
200
201
			<div id = "poststuff">
202
				<div id="post-body" class="metabox-holder columns-1">
203
204
					<?php $this->render_header(); ?>
205
206
					<div id="postbox-container-2" class="postbox-container">
207
						<?php $this->render_body(); ?>
208
					</div> <!-- #postbox-container-2 -->
209
210
				</div> <!-- #post-body -->
211
			</div><!-- #poststuff -->
212
			</form>
213
		</div><!-- .wrap -->
214
<?php
215
		$this->render_footer();
216
	}
217
218
	/**
219
	 * Print nonce fields.
220
	 *
221
	 * @since 5.5.4
222
	 */
223
	protected function render_nonce_fields() {
224
		wp_nonce_field( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" );
225
	}
226
227
	/**
228
	 * Render header for the page.
229
	 *
230
	 * If sidebar is enabled, then it is rendered as well.
231
	 *
232
	 * @since 5.5.4
233
	 */
234
	protected function render_header() {
235
?>
236
		<div class="notice notice-warning">
237
			<p><strong><?php echo $this->messages['warning_message']; ?></strong></p>
238
		</div>
239
<?php
240
	}
241
242
	/**
243
	 * Render footer.
244
	 *
245
	 * @since 5.5.4
246
	 */
247
	protected function render_footer() {
248
		/**
249
		 * Runs just before displaying the footer text in the admin page.
250
		 *
251
		 * This action is primarily for adding extra content in the footer of admin page.
252
		 *
253
		 * @since 5.5.4
254
		 */
255
		do_action( "bd_admin_footer_for_{$this->page_slug}" );
256
	}
257
258
	/**
259
	 * Modify admin footer in Bulk Delete plugin pages.
260
	 */
261
	public function modify_admin_footer() {
262
		add_filter( 'admin_footer_text', 'bd_add_rating_link' );
263
	}
264
265
	/**
266
	 * Enqueue Scripts and Styles.
267
	 */
268
	public function add_script() {
269
		global $wp_scripts;
270
		$bd = BULK_DELETE();
271
272
		/**
273
		 * Runs just before enqueuing scripts and styles in all Bulk WP admin pages.
274
		 *
275
		 * This action is primarily for registering or deregistering additional scripts or styles.
276
		 *
277
		 * @since 5.5.1
278
		 */
279
		do_action( 'bd_before_admin_enqueue_scripts' );
280
281
		wp_enqueue_script( 'jquery-ui-timepicker', plugins_url( '/assets/js/jquery-ui-timepicker-addon.min.js', $bd::$PLUGIN_FILE ), array( 'jquery-ui-slider', 'jquery-ui-datepicker' ), '1.5.4', true );
282
		wp_enqueue_style( 'jquery-ui-timepicker', plugins_url( '/assets/css/jquery-ui-timepicker-addon.min.css', $bd::$PLUGIN_FILE ), array(), '1.5.4' );
283
284
		wp_enqueue_script( 'select2', plugins_url( '/assets/js/select2.min.js', $bd::$PLUGIN_FILE ), array( 'jquery' ), '4.0.0', true );
285
		wp_enqueue_style( 'select2', plugins_url( '/assets/css/select2.min.css', $bd::$PLUGIN_FILE ), array(), '4.0.0' );
286
287
		$postfix = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
288
		wp_enqueue_script( $bd::JS_HANDLE, plugins_url( '/assets/js/bulk-delete' . $postfix . '.js', $bd::$PLUGIN_FILE ), array( 'jquery-ui-timepicker', 'jquery-ui-tooltip' ), $bd::VERSION, true );
289
		wp_enqueue_style( $bd::CSS_HANDLE, plugins_url( '/assets/css/bulk-delete' . $postfix . '.css', $bd::$PLUGIN_FILE ), array( 'select2' ), $bd::VERSION );
290
291
		$ui  = $wp_scripts->query( 'jquery-ui-core' );
292
		$url = "//ajax.googleapis.com/ajax/libs/jqueryui/{$ui->ver}/themes/smoothness/jquery-ui.css";
293
		wp_enqueue_style( 'jquery-ui-smoothness', $url, false, $ui->ver );
294
295
		/**
296
		 * Filter JavaScript array.
297
		 *
298
		 * This filter can be used to extend the array that is passed to JavaScript
299
		 *
300
		 * @since 5.4
301
		 */
302
		$translation_array = apply_filters( 'bd_javascript_array', array(
303
			'msg'            => array(),
304
			'validators'     => array(),
305
			'dt_iterators'   => array(),
306
			'pre_action_msg' => array(),
307
			'error_msg'      => array(),
308
			'pro_iterators'  => array(),
309
		) );
310
		wp_localize_script( $bd::JS_HANDLE, $bd::JS_VARIABLE, $translation_array );
311
312
		/**
313
		 * Runs just after enqueuing scripts and styles in all Bulk WP admin pages.
314
		 *
315
		 * This action is primarily for registering additional scripts or styles.
316
		 *
317
		 * @since 5.5.1
318
		 */
319
		do_action( 'bd_after_admin_enqueue_scripts' );
320
	}
321
322
	/**
323
	 * Getter for screen.
324
	 *
325
	 * @return string Current value of screen
326
	 */
327
	public function get_screen() {
328
		return $this->screen;
329
	}
330
331
	/**
332
	 * Getter for page_slug.
333
	 *
334
	 * @return string Current value of page_slug
335
	 */
336
	public function get_page_slug() {
337
		return $this->page_slug;
338
	}
339
340
	/**
341
	 * Get the page slug of base page.
342
	 *
343
	 * @return string Page slug.
344
	 */
345
	public function get_base_page_slug() {
346
		$bd = BULK_DELETE();
347
	    if ( $this->is_bulk_delete_menu_registered() ) {
348
			return $bd::POSTS_PAGE_SLUG;
349
		}
350
351
		return 'bulk-delete-posts';
352
	}
353
354
	/**
355
	 * Is the bulk delete menu already registered?
356
	 *
357
	 * @return bool True if registered, False otherwise.
358
	 */
359
	protected function is_bulk_delete_menu_registered() {
360
		global $admin_page_hooks;
361
362
		$bd = BULK_DELETE();
363
364
		return ! empty( $admin_page_hooks[ $bd::POSTS_PAGE_SLUG ] );
365
	}
366
367
	/**
368
	 * Add settings Panel for delete posts page.
369
	 */
370
	public function add_delete_settings_panel() {
371
		/**
372
		 * Add contextual help for admin screens.
373
		 *
374
		 * @since 5.1
375
		 */
376
		do_action( 'bd_add_contextual_help', $this->item_type );
377
378
		/* Trigger the add_meta_boxes hooks to allow meta boxes to be added */
379
		do_action( 'add_meta_boxes_' . $this->screen, null );
380
381
		/* Enqueue WordPress' script for handling the meta boxes */
382
		wp_enqueue_script( 'postbox' );
383
	}
384
}
385
?>
386