Passed
Push — 671-feature/add-mutisite-suppo... ( 7c9bd5...e10db6 )
by Sudar
05:24
created

BasePage::get_bulkwp_menu_position()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
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 9
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace BulkWP\BulkDelete\Core\Base;
3
4
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
5
6
/**
7
 * Base class for all Admin Page including Bulk Delete pages and setting pages.
8
 *
9
 * All concrete implementation of a Bulk Delete Admin page will extend this class.
10
 *
11
 * @since 6.0.0
12
 */
13
abstract class BasePage {
14
	/**
15
	 * Slug of Bulk WP Menu.
16
	 */
17
	const BULK_WP_MENU_SLUG = 'bulk-delete-posts';
18
19
	/**
20
	 * Position in which the Bulk WP menu should appear.
21
	 */
22
	const MENU_POSITION = '26';
23
24
	/**
25
	 * Path to main plugin file.
26
	 *
27
	 * @var string
28
	 */
29
	protected $plugin_file;
30
31
	/**
32
	 * Page Slug.
33
	 *
34
	 * @var string
35
	 */
36
	protected $page_slug;
37
38
	/**
39
	 * Hook Suffix of the current page.
40
	 *
41
	 * @var string
42
	 */
43
	protected $hook_suffix;
44
45
	/**
46
	 * Current screen.
47
	 *
48
	 * @var \WP_Screen
49
	 */
50
	protected $screen;
51
52
	/**
53
	 * Minimum capability needed for viewing this page.
54
	 *
55
	 * @var string
56
	 */
57
	protected $capability = 'manage_options';
58
59
	/**
60
	 * Labels used in this page.
61
	 *
62
	 * @var array
63
	 */
64
	protected $label = array(
65
		'page_title' => '',
66
		'menu_title' => '',
67
	);
68
69
	/**
70
	 * Messages shown to the user.
71
	 *
72
	 * @var array
73
	 */
74
	protected $messages = array(
75
		'warning_message' => '',
76
	);
77
78
	/**
79
	 * Actions used in this page.
80
	 *
81
	 * @var array
82
	 */
83
	protected $actions = array();
84
85
	/**
86
	 * Should the link to this page be displayed in the plugin list. Default false.
87
	 *
88
	 * @var bool
89
	 */
90
	protected $show_link_in_plugin_list = false;
91
92
	/**
93
	 * Initialize and setup variables and attributes of the page.
94
	 *
95
	 * @return void
96
	 */
97
	abstract protected function initialize();
98
99
	/**
100
	 * Render body content.
101
	 *
102
	 * @return void
103
	 */
104
	abstract protected function render_body();
105
106
	/**
107
	 * BasePage constructor.
108
	 *
109
	 * @param string $plugin_file Path to the main plugin file.
110
	 */
111
	public function __construct( $plugin_file ) {
112
		$this->plugin_file = $plugin_file;
113
		$this->initialize();
114
	}
115
116
	/**
117
	 * Register the page.
118
	 *
119
	 * This function will be called in the `admin_menu` hook.
120
	 */
121
	public function register() {
122
		$this->register_page();
123
		$this->register_hooks();
124
	}
125
126
	/**
127
	 * Register page as a submenu to the Bulk WP Menu.
128
	 */
129
	protected function register_page() {
130
		$hook_suffix = add_submenu_page(
131
			$this->get_top_level_menu_slug(),
132
			$this->label['page_title'],
133
			$this->label['menu_title'],
134
			$this->capability,
135
			$this->page_slug,
136
			array( $this, 'render_page' )
137
		);
138
139
		if ( false !== $hook_suffix ) {
140
			$this->hook_suffix = $hook_suffix;
141
		}
142
	}
143
144
	/**
145
	 * Register hooks.
146
	 */
147
	protected function register_hooks() {
148
		add_filter( 'bd_action_nonce_check', array( $this, 'verify_nonce' ), 10, 2 );
149
150
		add_action( "load-{$this->hook_suffix}", array( $this, 'setup_contextual_help' ) );
151
		add_filter( 'bd_admin_help_tabs', array( $this, 'render_help_tab' ), 10, 2 );
152
153
		add_action( "bd_admin_footer_for_{$this->page_slug}", array( $this, 'modify_admin_footer' ) );
154
155
		if ( $this->show_link_in_plugin_list ) {
156
			add_filter( 'bd_plugin_action_links', array( $this, 'append_to_plugin_action_links' ) );
157
		}
158
	}
159
160
	/**
161
	 * Check for nonce before executing the action.
162
	 *
163
	 * @param bool   $result The current result.
164
	 * @param string $action Action name.
165
	 *
166
	 * @return bool True if nonce is verified, False otherwise.
167
	 */
168
	public function verify_nonce( $result, $action ) {
169
		/**
170
		 * List of actions for page.
171
		 *
172
		 * @param array    $actions Actions.
173
		 * @param BasePage $page    Page objects.
174
		 *
175
		 * @since 6.0.0
176
		 */
177
		$page_actions = apply_filters( 'bd_page_actions', $this->actions, $this );
178
179
		if ( in_array( $action, $page_actions, true ) ) {
180
			if ( check_admin_referer( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" ) ) {
181
				return true;
182
			}
183
		}
184
185
		return $result;
186
	}
187
188
	/**
189
	 * Setup hooks for rendering contextual help.
190
	 */
191
	public function setup_contextual_help() {
192
		/**
193
		 * Add contextual help for admin screens.
194
		 *
195
		 * @since 5.1
196
		 *
197
		 * @param string Hook suffix of the current page.
198
		 */
199
		do_action( 'bd_add_contextual_help', $this->hook_suffix );
200
	}
201
202
	/**
203
	 * Modify help tabs for the current page.
204
	 *
205
	 * @param array  $help_tabs   Current list of help tabs.
206
	 * @param string $hook_suffix Hook Suffix of the page.
207
	 *
208
	 * @return array Modified list of help tabs.
209
	 */
210
	public function render_help_tab( $help_tabs, $hook_suffix ) {
211
		if ( $this->hook_suffix === $hook_suffix ) {
212
			$help_tabs = $this->add_help_tab( $help_tabs );
213
		}
214
215
		return $help_tabs;
216
	}
217
218
	/**
219
	 * Add help tabs.
220
	 *
221
	 * Help tabs can be added by overriding this function in the child class.
222
	 *
223
	 * @param array $help_tabs Current list of help tabs.
224
	 *
225
	 * @return array List of help tabs.
226
	 */
227
	protected function add_help_tab( $help_tabs ) {
228
		return $help_tabs;
229
	}
230
231
	/**
232
	 * Render the page.
233
	 */
234
	public function render_page() {
235
	?>
236
		<div class="wrap">
237
			<h2><?php echo esc_html( $this->label['page_title'] ); ?></h2>
238
			<?php settings_errors(); ?>
239
240
			<form method="post">
241
			<?php $this->render_nonce_fields(); ?>
242
243
			<div id = "poststuff">
244
				<div id="post-body" class="metabox-holder columns-1">
245
246
					<?php $this->render_header(); ?>
247
248
					<div id="postbox-container-2" class="postbox-container">
249
						<?php $this->render_body(); ?>
250
					</div> <!-- #postbox-container-2 -->
251
252
				</div> <!-- #post-body -->
253
			</div><!-- #poststuff -->
254
			</form>
255
		</div><!-- .wrap -->
256
	<?php
257
		$this->render_footer();
258
	}
259
260
	/**
261
	 * Print nonce fields.
262
	 */
263
	protected function render_nonce_fields() {
264
		wp_nonce_field( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" );
265
	}
266
267
	/**
268
	 * Render page header.
269
	 */
270
	protected function render_header() {
271
		if ( empty( $this->messages['warning_message'] ) ) {
272
			return;
273
		}
274
?>
275
		<div class="notice notice-warning">
276
			<p>
277
				<strong>
278
					<?php echo esc_html( $this->messages['warning_message'] ); ?>
279
				</strong>
280
			</p>
281
		</div>
282
<?php
283
	}
284
285
	/**
286
	 * Render page footer.
287
	 */
288
	protected function render_footer() {
289
		/**
290
		 * Runs just before displaying the footer text in the admin page.
291
		 *
292
		 * This action is primarily for adding extra content in the footer of admin page.
293
		 *
294
		 * @since 5.5.4
295
		 */
296
		do_action( "bd_admin_footer_for_{$this->page_slug}" );
297
	}
298
299
	/**
300
	 * Modify admin footer in Bulk Delete plugin pages.
301
	 */
302
	public function modify_admin_footer() {
303
		add_filter( 'admin_footer_text', 'bd_add_rating_link' );
304
	}
305
306
	/**
307
	 * Append link to the current page in plugin list.
308
	 *
309
	 * @param array $links Array of links.
310
	 *
311
	 * @return array Modified list of links.
312
	 */
313
	public function append_to_plugin_action_links( $links ) {
314
		$links[ $this->get_page_slug() ] = '<a href="admin.php?page=' . $this->get_page_slug() . '">' . $this->label['page_title'] . '</a>';
315
316
		return $links;
317
	}
318
319
	/**
320
	 * Getter for screen.
321
	 *
322
	 * @return \WP_Screen Current screen.
323
	 */
324
	public function get_screen() {
325
		return $this->screen;
326
	}
327
328
	/**
329
	 * Getter for page_slug.
330
	 *
331
	 * @return string Slug of the page.
332
	 */
333
	public function get_page_slug() {
334
		return $this->page_slug;
335
	}
336
337
	/**
338
	 * Getter for Hook Suffix.
339
	 *
340
	 * @return string Hook Suffix of the page.
341
	 */
342
	public function get_hook_suffix() {
343
		return $this->hook_suffix;
344
	}
345
346
	/**
347
	 * Get the url to the plugin directory.
348
	 *
349
	 * @return string Url to plugin directory.
350
	 */
351
	protected function get_plugin_dir_url() {
352
		return plugin_dir_url( $this->plugin_file );
353
	}
354
355
	/**
356
	 * Get the Menu position of BulkWP menu.
357
	 *
358
	 * @return int Menu position.
359
	 */
360
	protected function get_bulkwp_menu_position() {
361
		/**
362
		 * Bulk WP Menu position.
363
		 *
364
		 * @param int Menu Position.
365
		 *
366
		 * @since 6.0.0
367
		 */
368
		return apply_filters( 'bd_bulkwp_menu_position', self::MENU_POSITION );
369
	}
370
371
	/**
372
	 * Get Top Level Menu Slug.
373
	 *
374
	 * @since 6.1.0
375
	 *
376
	 * @return string Top Level menu slug.
377
	 */
378
	protected function get_top_level_menu_slug() {
379
		return self::BULK_WP_MENU_SLUG;
380
	}
381
}
382