Completed
Push — dev/5.5.4 ( 155266...c3760c )
by Sudar
02:05
created

BD_Base_Page::render_page()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.8571
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * Base class for all Pages.
4
 *
5
 * @since   5.5.4
6
 * @author  Sudar
7
 * @package BulkDelete\Base\Page
8
 */
9
10
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
11
12
/**
13
 * Base class for Pages.
14
 *
15
 * @abstract
16
 * @since 5.5.4
17
 */
18
abstract class BD_Base_Page {
19
	/**
20
	 * @var string Page Slug.
21
	 */
22
	protected $page_slug;
23
24
	/**
25
	 * @var string Menu action.
26
	 */
27
	protected $menu_action = 'bd_after_primary_menus';
28
29
	/**
30
	 * @var string Minimum capability needed for viewing this page.
31
	 */
32
	protected $capability = 'manage_options';
33
34
	/**
35
	 * @var bool Whether sidebar is needed or not.
36
	 */
37
	protected $render_sidebar = true;
38
39
	/**
40
	 * @var string The screen variable for this page.
41
	 */
42
	protected $screen;
43
44
	/**
45
	 * @var array Labels used in this page.
46
	 */
47
	protected $label = array();
48
49
	/**
50
	 * @var array Messages shown to the user.
51
	 */
52
	protected $messages = array();
53
54
	/**
55
	 * @var array Actions used in this page.
56
	 */
57
	protected $actions = array();
58
59
	/**
60
	 * Initialize and setup variables.
61
	 *
62
	 * @since 5.5.4
63
	 * @abstract
64
	 * @return void
65
	 */
66
	abstract protected function initialize();
67
68
	/**
69
	 * Render body content.
70
	 *
71
	 * @since 5.5.4
72
	 * @abstract
73
	 */
74
	abstract protected function render_body();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
75
76
	/**
77
	 * Use `factory()` method to create instance of this class.
78
	 * Don't create instances directly
79
	 *
80
	 * @since 5.5.4
81
	 *
82
	 * @see factory()
83
	 */
84
	public function __construct() {
85
		$this->setup();
86
	}
87
88
	/**
89
	 * Setup the module.
90
	 *
91
	 * @since 5.5.4
92
	 */
93
	protected function setup() {
94
		$this->initialize();
95
		$this->setup_hooks();
96
	}
97
98
	/**
99
	 * Setup hooks.
100
	 *
101
	 * @since 5.5.4
102
	 */
103
	protected function setup_hooks() {
104
		add_action( $this->menu_action, array( $this, 'add_menu' ) );
105
		add_action( "bd_admin_footer_for_{$this->page_slug}", array( $this, 'modify_admin_footer' ) );
106
107
		add_filter( 'bd_action_nonce_check', array( $this, 'nonce_check' ), 10, 2 );
108
		add_filter( 'bd_admin_help_tabs', array( $this, 'render_help_tab' ), 10, 2 );
109
	}
110
111
	/**
112
	 * Add menu.
113
	 *
114
	 * @since 5.5.4
115
	 */
116
	public function add_menu() {
117
		$bd = BULK_DELETE();
0 ignored issues
show
Unused Code introduced by
$bd is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
119
		$this->screen = add_submenu_page(
120
			Bulk_Delete::POSTS_PAGE_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
129
	/**
130
	 * Check for nonce before executing the action.
131
	 *
132
	 * @since 5.5.4
133
	 * @param bool   $result The current result.
134
	 * @param string $action Action name.
135
	 */
136
	public function nonce_check( $result, $action ) {
137
		if ( in_array( $action, $this->actions ) ) {
138
			if ( check_admin_referer( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" ) ) {
139
				return true;
140
			}
141
		}
142
143
		return $result;
144
	}
145
146
	/**
147
	 * Modify help tabs for the current page.
148
	 *
149
	 * @since 5.5.4
150
	 * @param array  $help_tabs Current list of help tabs.
151
	 * @param string $screen Current screen name.
152
	 * @return array Modified list of help tabs.
153
	 */
154
	public function render_help_tab( $help_tabs, $screen ) {
155
		if ( $this->screen == $screen ) {
156
			$help_tabs = $this->add_help_tab( $help_tabs );
157
		}
158
159
		return $help_tabs;
160
	}
161
162
	/**
163
	 * Add help tabs.
164
	 * Help tabs can be added by overriding this function in the child class.
165
	 *
166
	 * @since 5.5.4
167
	 * @param array $help_tabs Current list of help tabs.
168
	 * @return array List of help tabs.
169
	 */
170
	protected function add_help_tab( $help_tabs ) {
171
		return $help_tabs;
172
	}
173
174
	/**
175
	 * Render the page.
176
	 *
177
	 * @since 5.5.4
178
	 */
179
	public function render_page() {
180
?>
181
		<div class="wrap">
182
			<h2><?php echo $this->label['page_title'];?></h2>
183
			<?php settings_errors(); ?>
184
185
			<form method = "post">
186
			<?php $this->render_nonce_fields(); ?>
187
188
			<div id = "poststuff">
189
				<div id="post-body" class="metabox-holder columns-2">
190
191
					<?php $this->render_header(); ?>
192
193
					<div id="postbox-container-2" class="postbox-container">
194
						<?php $this->render_body(); ?>
195
					</div> <!-- #postbox-container-2 -->
196
197
				</div> <!-- #post-body -->
198
			</div><!-- #poststuff -->
199
			</form>
200
		</div><!-- .wrap -->
201
<?php
202
		$this->render_footer();
203
	}
204
205
	/**
206
	 * Print nonce fields.
207
	 *
208
	 * @since 5.5.4
209
	 */
210
	protected function render_nonce_fields() {
211
		wp_nonce_field( "bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce" );
212
	}
213
214
	/**
215
	 * Render header for the page.
216
	 *
217
	 * If sidebar is enabled, then it is rendered as well.
218
	 *
219
	 * @since 5.5.4
220
	 */
221
	protected function render_header() {
222
?>
223
		<div class="notice notice-warning">
224
			<p><strong><?php echo $this->messages['warning_message']; ?></strong></p>
225
		</div>
226
<?php
227
		if ( $this->render_sidebar ) {
228
			bd_render_sidebar_iframe();
229
		}
230
	}
231
232
	/**
233
	 * Render footer.
234
	 *
235
	 * @since 5.5.4
236
	 */
237
	protected function render_footer() {
238
		/**
239
		 * Runs just before displaying the footer text in the admin page.
240
		 *
241
		 * This action is primarily for adding extra content in the footer of admin page.
242
		 *
243
		 * @since 5.5.4
244
		 */
245
		do_action( "bd_admin_footer_for_{$this->page_slug}" );
246
	}
247
248
	/**
249
	 * Modify admin footer in Bulk Delete plugin pages.
250
	 */
251
	public function modify_admin_footer() {
252
		add_filter( 'admin_footer_text', 'bd_add_rating_link' );
253
	}
254
255
	/**
256
	 * Getter for screen.
257
	 *
258
	 * @return string Current value of screen
259
	 */
260
	public function get_screen() {
261
		return $this->screen;
262
	}
263
264
	/**
265
	 * Getter for page_slug.
266
	 *
267
	 * @return string Current value of page_slug
268
	 */
269
	public function get_page_slug() {
270
		return $this->page_slug;
271
	}
272
}
273
?>
274