Completed
Push — 233-feature/posts-by-post-stat... ( d2a551...3016f6 )
by Sudar
11:19 queued 07:05
created

BasePage   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 325
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 325
ccs 0
cts 98
cp 0
rs 10
c 0
b 0
f 0
wmc 24

18 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
A add_help_tab() 0 2 1
A render_nonce_fields() 0 2 1
A setup_contextual_help() 0 9 1
A __construct() 0 3 1
A register_hooks() 0 10 2
A render_footer() 0 9 1
A register_page() 0 12 2
A get_screen() 0 2 1
A modify_admin_footer() 0 2 1
A get_page_slug() 0 2 1
A verify_nonce() 0 8 3
A append_to_plugin_action_links() 0 4 1
B render_page() 0 24 1
A render_header() 0 10 2
A render_help_tab() 0 6 2
A get_hook_suffix() 0 2 1
A get_plugin_dir_url() 0 2 1
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
		if ( in_array( $action, $this->actions, true ) ) {
165
			if ( check_admin_referer( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" ) ) {
166
				return true;
167
			}
168
		}
169
170
		return $result;
171
	}
172
173
	/**
174
	 * Setup hooks for rendering contextual help.
175
	 */
176
	public function setup_contextual_help() {
177
		/**
178
		 * Add contextual help for admin screens.
179
		 *
180
		 * @since 5.1
181
		 *
182
		 * @param string Hook suffix of the current page.
183
		 */
184
		do_action( 'bd_add_contextual_help', $this->hook_suffix );
185
	}
186
187
	/**
188
	 * Modify help tabs for the current page.
189
	 *
190
	 * @param array  $help_tabs   Current list of help tabs.
191
	 * @param string $hook_suffix Hook Suffix of the page.
192
	 *
193
	 * @return array Modified list of help tabs.
194
	 */
195
	public function render_help_tab( $help_tabs, $hook_suffix ) {
196
		if ( $this->hook_suffix === $hook_suffix ) {
197
			$help_tabs = $this->add_help_tab( $help_tabs );
198
		}
199
200
		return $help_tabs;
201
	}
202
203
	/**
204
	 * Add help tabs.
205
	 *
206
	 * Help tabs can be added by overriding this function in the child class.
207
	 *
208
	 * @param array $help_tabs Current list of help tabs.
209
	 *
210
	 * @return array List of help tabs.
211
	 */
212
	protected function add_help_tab( $help_tabs ) {
213
		return $help_tabs;
214
	}
215
216
	/**
217
	 * Render the page.
218
	 */
219
	public function render_page() {
220
	?>
221
		<div class="wrap">
222
			<h2><?php echo esc_html( $this->label['page_title'] ); ?></h2>
223
			<?php settings_errors(); ?>
224
225
			<form method="post">
226
			<?php $this->render_nonce_fields(); ?>
227
228
			<div id = "poststuff">
229
				<div id="post-body" class="metabox-holder columns-1">
230
231
					<?php $this->render_header(); ?>
232
233
					<div id="postbox-container-2" class="postbox-container">
234
						<?php $this->render_body(); ?>
235
					</div> <!-- #postbox-container-2 -->
236
237
				</div> <!-- #post-body -->
238
			</div><!-- #poststuff -->
239
			</form>
240
		</div><!-- .wrap -->
241
	<?php
242
		$this->render_footer();
243
	}
244
245
	/**
246
	 * Print nonce fields.
247
	 */
248
	protected function render_nonce_fields() {
249
		wp_nonce_field( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" );
250
	}
251
252
	/**
253
	 * Render page header.
254
	 */
255
	protected function render_header() {
256
		if ( empty( $this->messages['warning_message'] ) ) {
257
			return;
258
		}
259
?>
260
		<div class="notice notice-warning">
261
			<p>
262
				<strong>
263
					<?php echo esc_html( $this->messages['warning_message'] ); ?>
264
				</strong>
265
			</p>
266
		</div>
267
<?php
268
	}
269
270
	/**
271
	 * Render page footer.
272
	 */
273
	protected function render_footer() {
274
		/**
275
		 * Runs just before displaying the footer text in the admin page.
276
		 *
277
		 * This action is primarily for adding extra content in the footer of admin page.
278
		 *
279
		 * @since 5.5.4
280
		 */
281
		do_action( "bd_admin_footer_for_{$this->page_slug}" );
282
	}
283
284
	/**
285
	 * Modify admin footer in Bulk Delete plugin pages.
286
	 */
287
	public function modify_admin_footer() {
288
		add_filter( 'admin_footer_text', 'bd_add_rating_link' );
289
	}
290
291
	/**
292
	 * Append link to the current page in plugin list.
293
	 *
294
	 * @param array $links Array of links.
295
	 *
296
	 * @return array Modified list of links.
297
	 */
298
	public function append_to_plugin_action_links( $links ) {
299
		$links[ $this->get_page_slug() ] = '<a href="admin.php?page=' . $this->get_page_slug() . '">' . $this->label['page_title'] . '</a>';
300
301
		return $links;
302
	}
303
304
	/**
305
	 * Getter for screen.
306
	 *
307
	 * @return \WP_Screen Current screen.
308
	 */
309
	public function get_screen() {
310
		return $this->screen;
311
	}
312
313
	/**
314
	 * Getter for page_slug.
315
	 *
316
	 * @return string Slug of the page.
317
	 */
318
	public function get_page_slug() {
319
		return $this->page_slug;
320
	}
321
322
	/**
323
	 * Getter for Hook Suffix.
324
	 *
325
	 * @return string Hook Suffix of the page.
326
	 */
327
	public function get_hook_suffix() {
328
		return $this->hook_suffix;
329
	}
330
331
	/**
332
	 * Get the url to the plugin directory.
333
	 *
334
	 * @return string Url to plugin directory.
335
	 */
336
	protected function get_plugin_dir_url() {
337
		return plugin_dir_url( $this->plugin_file );
338
	}
339
}
340