BasePage::render_nonce_fields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

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