Settings::set_screen()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 3
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
namespace NirjharLo\WP_Plugin_Framework\Src;
3
4
use NirjharLo\WP_Plugin_Framework\Lib\Table as Table;
5
6
if ( ! defined( 'ABSPATH' ) ) exit;
7
8
/**
9
 * Backend settings page class, can have settings fields or data table
10
 *
11
 * @author     Nirjhar Lo
12
 * @package    wp-plugin-framework
13
 */
14
if ( ! class_exists( 'Settings' ) ) {
15
16
	class Settings {
17
18
19
		/**
20
		 * @var String
21
		 */
22
		public $capability;
23
24
25
		/**
26
		 * @var Array
27
		 */
28
		public $menu_page;
29
30
31
		/**
32
		 * @var Array
33
		 */
34
		public $sub_menu_page;
35
36
37
		/**
38
		 * @var Array
39
		 */
40
		public $help;
41
42
43
		/**
44
		 * @var String
45
		 */
46
		public $screen;
47
48
49
		/**
50
		 * @var Object
51
		 */
52
		 public $table;
53
54
55
		/**
56
		 * Add basic actions for menu and settings
57
		 *
58
		 * @return Void
59
		 */
60
		public function __construct() {
61
62
			$this->capability = 'manage_options';
63
			$this->menu_page = array( 'name' => '', 'heading' => '', 'slug' => '' );
64
			$this->sub_menu_page = array(
65
									'name' => '',
66
									'heading' => '',
67
									'slug' => '',
68
									'parent_slug' => '',
69
									'help' => '',//true/false,
70
									'screen' => '',//true/false
71
								);
72
			$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...
73
								array(
74
								'slug' => '',
75
								'help' => array(
76
											'info' => array(
77
														array(
78
															'id' => 'helpId',
79
															'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

79
															'title' => /** @scrutinizer ignore-call */ __( 'Title', 'textdomain' ),
Loading history...
80
															'content' => __( 'Description', 'textdomain' ),
81
														),
82
													),
83
											'link' => '<p><a href="#">' . __( 'helpLink', 'textdomain' ) . '</a></p>',
84
											)
85
								)
86
							);
87
			$this->screen = ''; // true/false
88
89
			/**
90
			 * Add menues and hooks
91
			 *
92
			add_action( 'admin_init', array( $this, 'add_settings' ) );
93
			add_action( 'admin_menu', array( $this, 'menu_page' ) );
94
			add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
95
			add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
96
			 *
97
			 */
98
		}
99
100
101
		/**
102
		 * Add a sample main menu page callback
103
		 *
104
		 * @return Void
105
		 */
106
		public function menu_page() {
107
108
			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...
109
				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

109
				/** @scrutinizer ignore-call */ 
110
    add_menu_page(
Loading history...
110
					$this->menu_page['name'],
111
					$this->menu_page['heading'],
112
					$this->capability,
113
					$this->menu_page['slug'],
114
					array( $this, 'menu_page_callback' )
115
				);
116
			}
117
		}
118
119
120
		/**
121
		 * Add a sample Submenu page callback
122
		 *
123
		 * @return Void
124
		 */
125
		public function sub_menu_page() {
126
127
			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...
128
				foreach ( $this->sub_menu_page as $page ) {
129
					$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

129
					$hook = /** @scrutinizer ignore-call */ add_submenu_page(
Loading history...
130
							$page['parent_slug'],
131
							$page['name'],
132
							$page['heading'],
133
							$this->capability,
134
							// For the first submenu page, slug should be same as menupage.
135
							$page['slug'],
136
							// For the first submenu page, callback should be same as menupage.
137
							array( $this, 'menu_page_callback' )
138
						);
139
					if ( $page['help'] ) {
140
						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

140
						/** @scrutinizer ignore-call */ 
141
      add_action( 'load-' . $hook, array( $this, 'help_tabs' ) );
Loading history...
141
					}
142
					if ( $page['screen'] ) {
143
						add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
144
					}
145
				}
146
			}
147
		}
148
149
150
		/**
151
		 * Set screen
152
		 *
153
		 * @param String $status
154
		 * @param String $option
155
		 * @param String $value
156
		 *
157
		 * @return String
158
		 */
159
		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

159
		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...
160
161
			$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

161
			$user = /** @scrutinizer ignore-call */ get_current_user_id();
Loading history...
162
163
			switch ($option) {
164
				case 'option_name_per_page':
165
					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

165
					/** @scrutinizer ignore-call */ 
166
     update_user_meta($user, 'option_name_per_page', $value);
Loading history...
166
					$output = $value;
167
					break;
168
			}
169
170
    		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...
171
		}
172
173
174
		/**
175
		 * Set screen option for Items table
176
		 *
177
		 * @return Void
178
		 */
179
		public function screen_option() {
180
181
			$option = 'per_page';
182
			$args   = array(
183
						'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

183
						'label'   => /** @scrutinizer ignore-call */ __( 'Show per page', 'textdomain' ),
Loading history...
184
						'default' => 10,
185
						'option'  => 'option_name_per_page' // Related to PLUGIN_TABLE()
186
						);
187
			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

187
			/** @scrutinizer ignore-call */ 
188
   add_screen_option( $option, $args );
Loading history...
188
			$this->table = new Table(); // Source /lib/table.php
189
		}
190
191
192
		/**
193
		 * Menu page callback
194
		 *
195
		 * @return Html
0 ignored issues
show
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\Src\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...
196
		 */
197
		public function menu_page_callback() { ?>
198
199
			<div class="wrap">
200
				<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

200
				<h1><?php echo /** @scrutinizer ignore-call */ get_admin_page_title(); ?></h1>
Loading history...
201
				<br class="clear">
202
				<?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

202
				<?php /** @scrutinizer ignore-call */ settings_errors();
Loading history...
203
204
					/**
205
					 * Following is the settings form
206
					 */ ?>
207
					<form method="post" action="">
208
						<?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

208
						<?php /** @scrutinizer ignore-call */ settings_fields("settings_id");
Loading history...
209
						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

209
						/** @scrutinizer ignore-call */ 
210
      do_settings_sections("settings_name");
Loading history...
210
						submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
0 ignored issues
show
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

210
						/** @scrutinizer ignore-call */ 
211
      submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
Loading history...
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

210
						submit_button( /** @scrutinizer ignore-call */ __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
Loading history...
211
					</form>
212
213
					<?php
214
					/**
215
					 * Following is the data table class
216
					 */ ?>
217
					<form method="post" action="">
218
					<?php
219
						$this->table->prepare_items();
220
						$this->table->display(); ?>
221
					</form>
222
				<br class="clear">
223
			</div>
224
		<?php
225
		}
226
227
228
		/**
229
		 * Add help tabs using help data
230
		 *
231
		 * @return Void
232
		 */
233
		public function help_tabs() {
234
235
			foreach ($this->helpData as $value) {
236
				if ($_GET['page'] == $value['slug']) {
237
					$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

237
					$this->screen = /** @scrutinizer ignore-call */ get_current_screen();
Loading history...
238
					foreach( $value['info'] as $key ) {
239
						$this->screen->add_help_tab( $key );
240
					}
241
					$this->screen->set_help_sidebar( $value['link'] );
242
				}
243
			}
244
		}
245
246
247
		/**
248
		 * Add different types of settings and corrosponding sections
249
		 *
250
		 * @return Void
251
		 */
252
		public function add_settings() {
253
254
			add_settings_section( 'settings_id', __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
0 ignored issues
show
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

254
			/** @scrutinizer ignore-call */ 
255
   add_settings_section( 'settings_id', __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
Loading history...
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

254
			add_settings_section( 'settings_id', /** @scrutinizer ignore-call */ __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
Loading history...
255
256
			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

256
			/** @scrutinizer ignore-call */ 
257
   register_setting( 'settings_id', 'settings_field_name' );
Loading history...
257
			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

257
			/** @scrutinizer ignore-call */ 
258
   add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' );
Loading history...
258
		}
259
260
261
		/**
262
		 * Section description
263
		 *
264
		 * @return Html
265
		 */
266
		public function section_cb() {
267
268
			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

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