Passed
Push — master ( f94701...93534b )
by Nirjhar
02:18
created

PLUGIN_SETTINGS::menu_page()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit;
3
4
/**
5
 * Backend settings page class, can have settings fields or data table
6
 *
7
 * @author     Nirjhar Lo
8
 * @package    wp-plugin-framework
9
 */
10
if ( ! class_exists( 'PLUGIN_SETTINGS' ) ) {
11
12
	class PLUGIN_SETTINGS {
13
14
15
		/**
16
		 * @var String
17
		 */
18
		public $capability;
19
20
21
		/**
22
		 * @var Array
23
		 */
24
		public $menu_page;
25
26
27
		/**
28
		 * @var Array
29
		 */
30
		public $sub_menu_page;
31
32
33
		/**
34
		 * @var Array
35
		 */
36
		public $help;
37
38
39
		/**
40
		 * @var String
41
		 */
42
		public $screen;
43
44
45
		/**
46
		 * @var Object
47
		 */
48
		 public $table;
49
50
51
		/**
52
		 * Add basic actions for menu and settings
53
		 *
54
		 * @return Void
55
		 */
56
		public function __construct() {
57
58
			$this->capability = 'manage_options';
59
			$this->menu_page = array( 'name' => '', 'heading' => '', 'slug' => '' );
60
			$this->sub_menu_page = array(
61
									'name' => '',
62
									'heading' => '',
63
									'slug' => '',
64
									'parent_slug' => '',
65
									'help' => '',//true/false,
66
									'screen' => '',//true/false
67
								);
68
			$this->helpData = array(
0 ignored issues
show
Bug Best Practice introduced by
The property helpData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
69
								array(
70
								'slug' => '',
71
								'help' => array(
72
											'info' => array(
73
														array(
74
															'id' => 'helpId',
75
															'title' => __( 'Title', 'textdomain' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
															'title' => /** @scrutinizer ignore-call */ __( 'Title', 'textdomain' ),
Loading history...
76
															'content' => __( 'Description', 'textdomain' ),
77
														),
78
													),
79
											'link' => '<p><a href="#">' . __( 'helpLink', 'textdomain' ) . '</a></p>',
80
											)
81
								)
82
							);
83
			$this->screen = ''; // true/false
84
85
			/**
86
			 * Add menues and hooks
87
			 *
88
			add_action( 'admin_init', array( $this, 'add_settings' ) );
89
			add_action( 'admin_menu', array( $this, 'menu_page' ) );
90
			add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
91
			add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
92
			 *
93
			 */
94
		}
95
96
97
		/**
98
		 * Add a sample main menu page callback
99
		 *
100
		 * @return Void
101
		 */
102
		public function menu_page() {
103
104
			if ($this->menu_page) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->menu_page of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
				add_menu_page(
0 ignored issues
show
Bug introduced by
The function add_menu_page was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
				/** @scrutinizer ignore-call */ 
106
    add_menu_page(
Loading history...
106
					$this->menu_page['name'],
107
					$this->menu_page['heading'],
108
					$this->capability,
109
					$this->menu_page['slug'],
110
					array( $this, 'menu_page_callback' )
111
				);
112
			}
113
		}
114
115
116
		/**
117
		 * Add a sample Submenu page callback
118
		 *
119
		 * @return Void
120
		 */
121
		public function sub_menu_page() {
122
123
			if ( $this->sub_menu_page ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sub_menu_page of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
124
				foreach ( $this->sub_menu_page as $page ) {
125
					$hook = add_submenu_page(
0 ignored issues
show
Bug introduced by
The function add_submenu_page was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
					$hook = /** @scrutinizer ignore-call */ add_submenu_page(
Loading history...
126
							$page['parent_slug'],
127
							$page['name'],
128
							$page['heading'],
129
							$this->capability,
130
							// For the first submenu page, slug should be same as menupage.
131
							$page['slug'],
132
							// For the first submenu page, callback should be same as menupage.
133
							array( $this, 'menu_page_callback' )
134
						);
135
					if ( $page['help'] ) {
136
						add_action( 'load-' . $hook, array( $this, 'help_tabs' ) );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
						/** @scrutinizer ignore-call */ 
137
      add_action( 'load-' . $hook, array( $this, 'help_tabs' ) );
Loading history...
137
					}
138
					if ( $page['screen'] ) {
139
						add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
140
					}
141
				}
142
			}
143
		}
144
145
146
		/**
147
		 * Set screen
148
		 *
149
		 * @param String $status
150
		 * @param String $option
151
		 * @param String $value
152
		 *
153
		 * @return String
154
		 */
155
		public function set_screen($status, $option, $value) {
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

155
		public function set_screen(/** @scrutinizer ignore-unused */ $status, $option, $value) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
157
			$user = get_current_user_id();
0 ignored issues
show
Bug introduced by
The function get_current_user_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
			$user = /** @scrutinizer ignore-call */ get_current_user_id();
Loading history...
158
159
			switch ($option) {
160
				case 'option_name_per_page':
161
					update_user_meta($user, 'option_name_per_page', $value);
0 ignored issues
show
Bug introduced by
The function update_user_meta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
					/** @scrutinizer ignore-call */ 
162
     update_user_meta($user, 'option_name_per_page', $value);
Loading history...
162
					$output = $value;
163
					break;
164
			}
165
166
    		if ( $output ) return $output; // Related to PLUGIN_TABLE()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output does not seem to be defined for all execution paths leading up to this point.
Loading history...
167
		}
168
169
170
		/**
171
		 * Set screen option for Items table
172
		 *
173
		 * @return Void
174
		 */
175
		public function screen_option() {
176
177
			$option = 'per_page';
178
			$args   = array(
179
						'label'   => __( 'Show per page', 'textdomain' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
						'label'   => /** @scrutinizer ignore-call */ __( 'Show per page', 'textdomain' ),
Loading history...
180
						'default' => 10,
181
						'option'  => 'option_name_per_page' // Related to PLUGIN_TABLE()
182
						);
183
			add_screen_option( $option, $args );
0 ignored issues
show
Bug introduced by
The function add_screen_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
			/** @scrutinizer ignore-call */ 
184
   add_screen_option( $option, $args );
Loading history...
184
			$this->table = new PLUGIN_TABLE(); // Source /lib/table.php
185
		}
186
187
188
		/**
189
		 * Menu page callback
190
		 *
191
		 * @return Html
0 ignored issues
show
Bug introduced by
The type Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
192
		 */
193
		public function menu_page_callback() { ?>
194
195
			<div class="wrap">
196
				<h1><?php echo get_admin_page_title(); ?></h1>
0 ignored issues
show
Bug introduced by
The function get_admin_page_title was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

196
				<h1><?php echo /** @scrutinizer ignore-call */ get_admin_page_title(); ?></h1>
Loading history...
197
				<br class="clear">
198
				<?php settings_errors();
0 ignored issues
show
Bug introduced by
The function settings_errors was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
				<?php /** @scrutinizer ignore-call */ settings_errors();
Loading history...
199
200
					/**
201
					 * Following is the settings form
202
					 */ ?>
203
					<form method="post" action="">
204
						<?php settings_fields("settings_id");
0 ignored issues
show
Bug introduced by
The function settings_fields was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
						<?php /** @scrutinizer ignore-call */ settings_fields("settings_id");
Loading history...
205
						do_settings_sections("settings_name");
0 ignored issues
show
Bug introduced by
The function do_settings_sections was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
						/** @scrutinizer ignore-call */ 
206
      do_settings_sections("settings_name");
Loading history...
206
						submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
						submit_button( /** @scrutinizer ignore-call */ __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
Loading history...
Bug introduced by
The function submit_button was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
						/** @scrutinizer ignore-call */ 
207
      submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
Loading history...
207
					</form>
208
209
					<?php
210
					/**
211
					 * Following is the data table class
212
					 */ ?>
213
					<form method="post" action="">
214
					<?php
215
						$this->table->prepare_items();
216
						$this->table->display(); ?>
217
					</form>
218
				<br class="clear">
219
			</div>
220
		<?php
221
		}
222
223
224
		/**
225
		 * Add help tabs using help data
226
		 *
227
		 * @return Void
228
		 */
229
		public function help_tabs() {
230
231
			foreach ($this->helpData as $value) {
232
				if ($_GET['page'] == $value['slug']) {
233
					$this->screen = get_current_screen();
0 ignored issues
show
Bug introduced by
The function get_current_screen was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
					$this->screen = /** @scrutinizer ignore-call */ get_current_screen();
Loading history...
234
					foreach( $value['info'] as $key ) {
235
						$this->screen->add_help_tab( $key );
236
					}
237
					$this->screen->set_help_sidebar( $value['link'] );
238
				}
239
			}
240
		}
241
242
243
		/**
244
		 * Add different types of settings and corrosponding sections
245
		 *
246
		 * @return Void
247
		 */
248
		public function add_settings() {
249
250
			add_settings_section( 'settings_id', __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

250
			add_settings_section( 'settings_id', /** @scrutinizer ignore-call */ __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
Loading history...
Bug introduced by
The function add_settings_section was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

250
			/** @scrutinizer ignore-call */ 
251
   add_settings_section( 'settings_id', __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
Loading history...
251
252
			register_setting( 'settings_id', 'settings_field_name' );
0 ignored issues
show
Bug introduced by
The function register_setting was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

252
			/** @scrutinizer ignore-call */ 
253
   register_setting( 'settings_id', 'settings_field_name' );
Loading history...
253
			add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' );
0 ignored issues
show
Bug introduced by
The function add_settings_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

253
			/** @scrutinizer ignore-call */ 
254
   add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' );
Loading history...
254
		}
255
256
257
		/**
258
		 * Section description
259
		 *
260
		 * @return Html
261
		 */
262
		public function section_cb() {
263
264
			echo '<p class="description">' . __( 'Set up settings', 'textdomain' ) . '</p>';
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
			echo '<p class="description">' . /** @scrutinizer ignore-call */ __( 'Set up settings', 'textdomain' ) . '</p>';
Loading history...
265
		}
266
267
268
		/**
269
		 * Field explanation
270
		 *
271
		 * @return Html
272
		 */
273
		public function settings_field_cb() {
274
275
			//Choose any one from input, textarea, select or checkbox
276
			/**
277
			echo '<input type="text" class="medium-text" name="settings_field_name" id="settings_field_name" value="' . get_option('settings_field_name') . '" placeholder="' . __( 'Enter Value', 'textdomain' ) . '" required />';
278
			echo '<textarea name="settings_field_name" id="settings_field_name" value="' . get_option('settings_field_name') . '>'. __( 'Enter Value', 'textdomain' ) . '</textarea>';
279
			echo '<select name="settings_field_name" id="settings_field_name"><option value="value" ' . selected( 'value', get_option('settings_field_name'), false) . '>Value</option></select>';
280
			echo '<input type="checkbox" id="settings_field_name" name="settings_field_name" value="1"' . checked( 1, get_option('settings_field_name'), false ) . '/>';
281
			*/
282
		}
283
	}
284
} ?>
285