Passed
Push — 160-fix/replace-deprecated-fun... ( 64667b )
by
unknown
04:27
created

BD_Base_Page   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 24.07%

Importance

Changes 0
Metric Value
dl 0
loc 258
ccs 13
cts 54
cp 0.2407
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A get_screen() 0 2 1
A setup_hooks() 0 6 1
A get_page_slug() 0 2 1
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 modify_admin_footer() 0 2 1
A render_nonce_fields() 0 2 1
B render_page() 0 24 1
A setup() 0 3 1
A render_footer() 0 9 1
A render_help_tab() 0 6 2
A add_menu() 0 8 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
	/**
27
	 * @var string Menu action.
28
	 */
29
	protected $menu_action = 'bd_after_primary_menus';
30
31
	/**
32
	 * @var string Minimum capability needed for viewing this page.
33
	 */
34
	protected $capability = 'manage_options';
35
36
	// TODO: Remove property after confirming on `class-bd-settings-page.php` file.
37
	/**
38
	 * @var bool Whether sidebar is needed or not.
39
	 */
40
	protected $render_sidebar = false;
41
42
	/**
43
	 * @var string The screen variable for this page.
44
	 */
45
	protected $screen;
46
47
	/**
48
	 * @var array Labels used in this page.
49
	 */
50
	protected $label = array();
51
52
	/**
53
	 * @var array Messages shown to the user.
54
	 */
55
	protected $messages = array();
56
57
	/**
58
	 * @var array Actions used in this page.
59
	 */
60
	protected $actions = array();
61
62
	/**
63
	 * Initialize and setup variables.
64
	 *
65
	 * @since 5.5.4
66
	 * @abstract
67
	 *
68
	 * @return void
69
	 */
70
	abstract protected function initialize();
71
72
	/**
73
	 * Render body content.
74
	 *
75
	 * @since 5.5.4
76
	 * @abstract
77
	 *
78
	 * @return void
79
	 */
80
	abstract protected function render_body();
81
82
	/**
83
	 * Use `factory()` method to create instance of this class.
84
	 * Don't create instances directly.
85
	 *
86
	 * @since 5.5.4
87
	 * @see factory()
88
	 */
89
	public function __construct() {
90
		$this->setup();
91
	}
92
93
	/**
94
	 * Setup the module.
95
	 *
96
	 * @since 5.5.4
97
	 */
98
	protected function setup() {
99
		$this->initialize();
100
		$this->setup_hooks();
101
	}
102
103
	/**
104
	 * Setup hooks.
105
	 *
106
	 * @since 5.5.4
107
	 */
108
	protected function setup_hooks() {
109
		add_action( $this->menu_action, array( $this, 'add_menu' ) );
110
		add_action( "bd_admin_footer_for_{$this->page_slug}", array( $this, 'modify_admin_footer' ) );
111
112
		add_filter( 'bd_action_nonce_check', array( $this, 'nonce_check' ), 10, 2 );
113
		add_filter( 'bd_admin_help_tabs', array( $this, 'render_help_tab' ), 10, 2 );
114
	}
115
116
	/**
117
	 * Add menu.
118
	 *
119
	 * @since 5.5.4
120
	 */
121
	public function add_menu() {
122
		$this->screen = add_submenu_page(
123
			Bulk_Delete::POSTS_PAGE_SLUG,
124
			$this->label['page_title'],
125
			$this->label['menu_title'],
126
			$this->capability,
127
			$this->page_slug,
128
			array( $this, 'render_page' )
129
		);
130
	}
131
132
	/**
133
	 * Check for nonce before executing the action.
134
	 *
135
	 * @since 5.5.4
136
	 *
137
	 * @param bool   $result The current result.
138
	 * @param string $action Action name.
139
	 */
140
	public function nonce_check( $result, $action ) {
141
		if ( in_array( $action, $this->actions ) ) {
142
			if ( check_admin_referer( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" ) ) {
143
				return true;
144
			}
145
		}
146
147
		return $result;
148
	}
149
150
	/**
151
	 * Modify help tabs for the current page.
152
	 *
153
	 * @since 5.5.4
154
	 *
155
	 * @param array  $help_tabs Current list of help tabs.
156
	 * @param string $screen    Current screen name.
157
	 *
158
	 * @return array Modified list of help tabs.
159
	 */
160
	public function render_help_tab( $help_tabs, $screen ) {
161
		if ( $this->screen == $screen ) {
162
			$help_tabs = $this->add_help_tab( $help_tabs );
163
		}
164
165
		return $help_tabs;
166
	}
167
168
	/**
169
	 * Add help tabs.
170
	 * Help tabs can be added by overriding this function in the child class.
171
	 *
172
	 * @since 5.5.4
173
	 *
174
	 * @param array $help_tabs Current list of help tabs.
175
	 *
176
	 * @return array List of help tabs.
177
	 */
178
	protected function add_help_tab( $help_tabs ) {
179
		return $help_tabs;
180
	}
181
182
	/**
183
	 * Render the page.
184
	 *
185
	 * @since 5.5.4
186
	 */
187
	public function render_page() {
188
?>
189
		<div class="wrap">
190
			<h2><?php echo $this->label['page_title'];?></h2>
191
			<?php settings_errors(); ?>
192
193
			<form method = "post">
194
			<?php $this->render_nonce_fields(); ?>
195
196
			<div id = "poststuff">
197
				<div id="post-body" class="metabox-holder columns-1">
198
199
					<?php $this->render_header(); ?>
200
201
					<div id="postbox-container-2" class="postbox-container">
202
						<?php $this->render_body(); ?>
203
					</div> <!-- #postbox-container-2 -->
204
205
				</div> <!-- #post-body -->
206
			</div><!-- #poststuff -->
207
			</form>
208
		</div><!-- .wrap -->
209
<?php
210
		$this->render_footer();
211
	}
212
213
	/**
214
	 * Print nonce fields.
215
	 *
216
	 * @since 5.5.4
217
	 */
218
	protected function render_nonce_fields() {
219
		wp_nonce_field( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" );
220
	}
221
222
	/**
223
	 * Render header for the page.
224
	 *
225
	 * If sidebar is enabled, then it is rendered as well.
226
	 *
227
	 * @since 5.5.4
228
	 */
229
	protected function render_header() {
230
?>
231
		<div class="notice notice-warning">
232
			<p><strong><?php echo $this->messages['warning_message']; ?></strong></p>
233
		</div>
234
<?php
235
	}
236
237
	/**
238
	 * Render footer.
239
	 *
240
	 * @since 5.5.4
241
	 */
242
	protected function render_footer() {
243
		/**
244
		 * Runs just before displaying the footer text in the admin page.
245
		 *
246
		 * This action is primarily for adding extra content in the footer of admin page.
247
		 *
248
		 * @since 5.5.4
249
		 */
250
		do_action( "bd_admin_footer_for_{$this->page_slug}" );
251
	}
252
253
	/**
254
	 * Modify admin footer in Bulk Delete plugin pages.
255
	 */
256
	public function modify_admin_footer() {
257
		add_filter( 'admin_footer_text', 'bd_add_rating_link' );
258
	}
259
260
	/**
261
	 * Getter for screen.
262
	 *
263
	 * @return string Current value of screen
264
	 */
265
	public function get_screen() {
266
		return $this->screen;
267
	}
268
269
	/**
270
	 * Getter for page_slug.
271
	 *
272
	 * @return string Current value of page_slug
273
	 */
274
	public function get_page_slug() {
275
		return $this->page_slug;
276
	}
277
}
278
?>
279