Completed
Push — 137-feature/refactor-post-modu... ( 580e59 )
by Sudar
07:52
created

BasePage::render_page()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 24
ccs 0
cts 4
cp 0
crap 2
rs 8.9713
c 0
b 0
f 0
1
<?php
2
namespace BulkWP\BulkDelete\Core\Base;
3
4 1
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 5.7.0
12
 */
13
abstract class BasePage {
14
15
	/**
16
	 * Slug of Bulk WP Menu.
17
	 */
18
	const BULK_WP_MENU_SLUG = 'bulk-delete-posts';
19
20
	/**
21
	 * Path to main plugin file.
22
	 *
23
	 * @var string
24
	 */
25
	protected $plugin_file;
26
27
	/**
28
	 * Page Slug.
29
	 *
30
	 * @var string
31
	 */
32
	protected $page_slug;
33
34
	/**
35
	 * Hook Suffix of the current page.
36
	 *
37
	 * @var string
38
	 */
39
	protected $hook_suffix;
40
41
	/**
42
	 * Current screen.
43
	 *
44
	 * @var \WP_Screen
45
	 */
46
	protected $screen;
47
48
	/**
49
	 * Minimum capability needed for viewing this page.
50
	 *
51
	 * @var string
52
	 */
53
	protected $capability = 'manage_options';
54
55
	/**
56
	 * Labels used in this page.
57
	 *
58
	 * @var array
59
	 */
60
	protected $label = array(
61
		'page_title' => '',
62
		'menu_title' => '',
63
	);
64
65
	/**
66
	 * Messages shown to the user.
67
	 *
68
	 * @var array
69
	 */
70
	protected $messages = array(
71
		'warning_message' => '',
72
	);
73
74
	/**
75
	 * Actions used in this page.
76
	 *
77
	 * @var array
78
	 */
79
	protected $actions = array();
80
81
	/**
82
	 * Initialize and setup variables and attributes of the page.
83
	 *
84
	 * @return void
85
	 */
86
	abstract protected function initialize();
87
88
	/**
89
	 * Render body content.
90
	 *
91
	 * @return void
92
	 */
93
	abstract protected function render_body();
94
95
	/**
96
	 * BasePage constructor.
97
	 *
98
	 * @param string $plugin_file Path to the main plugin file.
99
	 */
100
	public function __construct( $plugin_file ) {
101
		$this->plugin_file = $plugin_file;
102
		$this->initialize();
103
	}
104
105
	/**
106
	 * Register the page.
107
	 *
108
	 * This function will be called in the `admin_menu` hook.
109
	 */
110
	public function register() {
111
		$this->register_page();
112
		$this->register_hooks();
113
	}
114
115
	/**
116
	 * Register page as a submenu to the Bulk WP Menu.
117
	 */
118
	protected function register_page() {
119
		$hook_suffix = add_submenu_page(
120
			self::BULK_WP_MENU_SLUG,
121
			$this->label['page_title'],
122
			$this->label['menu_title'],
123
			$this->capability,
124
			$this->page_slug,
125
			array( $this, 'render_page' )
126
		);
127
128
		if ( false !== $hook_suffix ) {
129
			$this->hook_suffix = $hook_suffix;
130
		}
131
	}
132
133
	/**
134
	 * Register hooks.
135
	 */
136
	protected function register_hooks() {
137
		add_filter( 'bd_action_nonce_check', array( $this, 'verify_nonce' ), 10, 2 );
138
139
		add_action( "load-{$this->hook_suffix}", array( $this, 'setup_contextual_help' ) );
140
		add_filter( 'bd_admin_help_tabs', array( $this, 'render_help_tab' ), 10, 2 );
141
142
		add_action( "bd_admin_footer_for_{$this->page_slug}", array( $this, 'modify_admin_footer' ) );
143
144
		// TODO: Implement action links.
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
145
//		add_filter( 'bm_plugin_action_links', array( $this, 'append_to_plugin_action_links' ) );
146
	}
147
148
	/**
149
	 * Check for nonce before executing the action.
150
	 *
151
	 * @param bool   $result The current result.
152
	 * @param string $action Action name.
153
	 *
154
	 * @return bool True if nonce is verified, False otherwise.
155
	 */
156
	public function verify_nonce( $result, $action ) {
157
		if ( in_array( $action, $this->actions, true ) ) {
158
			if ( check_admin_referer( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" ) ) {
159
				return true;
160
			}
161
		}
162
163
		return $result;
164
	}
165
166
	/**
167
	 * Setup hooks for rendering contextual help.
168
	 */
169
	public function setup_contextual_help() {
170
		/**
171
		 * Add contextual help for admin screens.
172
		 *
173
		 * @since 5.1
174
		 *
175
		 * @param string Hook suffix of the current page.
176
		 */
177
		do_action( 'bd_add_contextual_help', $this->hook_suffix );
178
	}
179
180
	/**
181
	 * Modify help tabs for the current page.
182
	 *
183
	 * @param array  $help_tabs   Current list of help tabs.
184
	 * @param string $hook_suffix Hook Suffix of the page.
185
	 *
186
	 * @return array Modified list of help tabs.
187
	 */
188
	public function render_help_tab( $help_tabs, $hook_suffix ) {
189
		if ( $this->hook_suffix === $hook_suffix ) {
190
			$help_tabs = $this->add_help_tab( $help_tabs );
191
		}
192
193
		return $help_tabs;
194
	}
195
196
	/**
197
	 * Add help tabs.
198
	 *
199
	 * Help tabs can be added by overriding this function in the child class.
200
	 *
201
	 * @param array $help_tabs Current list of help tabs.
202
	 *
203
	 * @return array List of help tabs.
204
	 */
205
	protected function add_help_tab( $help_tabs ) {
206
		return $help_tabs;
207
	}
208
209
	/**
210
	 * Render the page.
211
	 */
212
	public function render_page() {
213
	?>
214
		<div class="wrap">
215
			<h2><?php echo esc_html( $this->label['page_title'] ); ?></h2>
216
			<?php settings_errors(); ?>
217
218
			<form method="post">
219
			<?php $this->render_nonce_fields(); ?>
220
221
			<div id = "poststuff">
222
				<div id="post-body" class="metabox-holder columns-1">
223
224
					<?php $this->render_header(); ?>
225
226
					<div id="postbox-container-2" class="postbox-container">
227
						<?php $this->render_body(); ?>
228
					</div> <!-- #postbox-container-2 -->
229
230
				</div> <!-- #post-body -->
231
			</div><!-- #poststuff -->
232
			</form>
233
		</div><!-- .wrap -->
234
	<?php
235
		$this->render_footer();
236
	}
237
238
	/**
239
	 * Print nonce fields.
240
	 */
241
	protected function render_nonce_fields() {
242
		wp_nonce_field( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" );
243
	}
244
245
	/**
246
	 * Render page header.
247
	 */
248
	protected function render_header() {
249
?>
250
		<div class="notice notice-warning">
251
			<p>
252
				<strong>
253
					<?php echo esc_html( $this->messages['warning_message'] ); ?>
254
				</strong>
255
			</p>
256
		</div>
257
<?php
258
	}
259
260
	/**
261
	 * Render page footer.
262
	 */
263
	protected function render_footer() {
264
		/**
265
		 * Runs just before displaying the footer text in the admin page.
266
		 *
267
		 * This action is primarily for adding extra content in the footer of admin page.
268
		 *
269
		 * @since 5.5.4
270
		 */
271
		do_action( "bd_admin_footer_for_{$this->page_slug}" );
272
	}
273
274
	/**
275
	 * Modify admin footer in Bulk Delete plugin pages.
276
	 */
277
	public function modify_admin_footer() {
278
		add_filter( 'admin_footer_text', 'bd_add_rating_link' );
279
	}
280
281
	/**
282
	 * Getter for screen.
283
	 *
284
	 * @return \WP_Screen Current screen.
285
	 */
286
	public function get_screen() {
287
		return $this->screen;
288
	}
289
290
	/**
291
	 * Getter for page_slug.
292
	 *
293
	 * @return string Slug of the page.
294
	 */
295
	public function get_page_slug() {
296
		return $this->page_slug;
297
	}
298
299
	/**
300
	 * Get the url to the plugin directory.
301
	 *
302
	 * @return string Url to plugin directory.
303
	 */
304
	protected function get_plugin_dir_url() {
305
		return plugin_dir_url( $this->plugin_file );
306
	}
307
}
308