Passed
Push — master ( 2607e6...90d495 )
by Nirjhar
03:06
created

PLUGIN_SETTINGS::section_cb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
if ( ! class_exists( 'PLUGIN_SETTINGS' ) ) {
8
9
	final class PLUGIN_SETTINGS {
10
11
		public $capability;
12
		public $menuPage;
13
		public $subMenuPage;
14
		public $help;
15
		public $screen;
16
17
18
19
		// Add basic actions for menu and settings
20
		public function __construct() {
21
22
			$this->capability = 'manage_options';
23
			$this->menuPage = array( 'name' => '', 'heading' => '', 'slug' => '' );
24
			$this->subMenuPage = array(
25
									'name' => '',
26
									'heading' => '',
27
									'slug' => '',
28
									'parent_slug' => '',
29
									'help' => '',//true/false,
30
									'screen' => '',//true/false
31
								);
32
			$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...
33
								array(
34
								'slug' => '',
35
								'help' => array(
36
											'info' => array(
37
														array(
38
															'id' => 'helpId',
39
															'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

39
															'title' => /** @scrutinizer ignore-call */ __( 'Title', 'textdomain' ),
Loading history...
40
															'content' => __( 'Description', 'textdomain' ),
41
														),
42
													),
43
											'link' => '<p><a href="#">' . __( 'helpLink', 'textdomain' ) . '</a></p>',
44
											)
45
								)
46
							);
47
			$this->screen = ''; // true/false
48
49
			/**
50
			 * Add menues and hooks
51
			 *
52
			add_action( 'admin_init', array( $this, 'add_settings' ) );
53
			add_action( 'admin_menu', array( $this, 'menu_page' ) );
54
			add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
55
			add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
56
			*
57
			*/
58
		}
59
60
61
62
		// Add a sample main menu page callback
63
		public function menu_page() {
64
65
			if ($this->menuPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->menuPage of type array<string,string> 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...
66
				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

66
				/** @scrutinizer ignore-call */ 
67
    add_menu_page(
Loading history...
67
					$this->menuPage['name'],
68
					$this->menuPage['heading'],
69
					$this->capability,
70
					$this->menuPage['slug'],
71
					array( $this, 'menu_page_callback' )
72
				);
73
			}
74
		}
75
76
77
78
		//Add a sample Submenu page callback
79
		public function sub_menu_page() {
80
81
			if ($this->subMenuPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->subMenuPage of type array<string,string> 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...
82
				$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

82
				$hook = /** @scrutinizer ignore-call */ add_submenu_page(
Loading history...
83
							$this->subMenuPage['parent_slug'],
84
							$this->subMenuPage['name'],
85
							$this->subMenuPage['heading'],
86
							$this->capability,
87
							// For the first submenu page, slug should be same as menupage.
88
							$this->subMenuPage['slug'],
89
							// For the first submenu page, callback should be same as menupage.
90
							array( $this, 'menu_page_callback' )
91
						);
92
					if ($this->subMenuPage['help']) {
93
						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

93
						/** @scrutinizer ignore-call */ 
94
      add_action( 'load-' . $hook, array( $this, 'help_tabs' ) );
Loading history...
94
					}
95
					if ($this->subMenuPage['screen']) {
96
						add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
97
					}
98
				}
99
			}
100
101
102
103
		//Set screen option
104
		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

104
		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...
105
106
    		if ( 'option_name_per_page' == $option ) return $value; // Related to PLUGIN_TABLE()
107
    			//return $status;
108
		}
109
110
111
112
		//Set screen option for Items table
113
		public function screen_option() {
114
115
			$option = 'per_page';
116
			$args   = array(
117
						'label'   => __( 'Show per page', '' ),
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

117
						'label'   => /** @scrutinizer ignore-call */ __( 'Show per page', '' ),
Loading history...
118
						'default' => 10,
119
						'option'  => 'option_name_per_page' // Related to PLUGIN_TABLE()
120
						);
121
			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

121
			/** @scrutinizer ignore-call */ 
122
   add_screen_option( $option, $args );
Loading history...
122
			$this->Table = new PLUGIN_TABLE();
0 ignored issues
show
Bug Best Practice introduced by
The property Table does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
123
		}
124
125
126
127
		// Menu page callback
128
		public function menu_page_callback() { ?>
129
130
			<div class="wrap">
131
				<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

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

133
				<?php /** @scrutinizer ignore-call */ settings_errors();
Loading history...
134
135
					/**
136
					 * Following is the settings form
137
					 */ ?>
138
					<form method="post" action="">
139
						<?php settings_fields("settings_name");
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

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

140
						/** @scrutinizer ignore-call */ 
141
      do_settings_sections("settings_name");
Loading history...
141
						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

141
						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

141
						/** @scrutinizer ignore-call */ 
142
      submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
Loading history...
142
					</form>
143
144
					<?php
145
					/**
146
					 * Following is the data table class
147
					 */ ?>
148
					<form method="post" action="">
149
					<?php // Source /lib/table.php
150
						$table = new PLUGIN_TABLE();
151
						$table->prepare_items();
152
						$table->display(); ?>
153
					</form>
154
				<br class="clear">
155
			</div>
156
		<?php
157
		}
158
159
160
161
		// Add help tabs using help data
162
		public function help_tabs() {
163
164
			foreach ($this->helpData as $value) {
165
				if ($_GET['page'] == $value['slug']) {
166
					$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

166
					$this->screen = /** @scrutinizer ignore-call */ get_current_screen();
Loading history...
167
					foreach( $value['info'] as $key ) {
168
						$this->screen->add_help_tab( $key );
169
					}
170
					$this->screen->set_help_sidebar( $value['link'] );
171
				}
172
			}
173
		}
174
175
176
177
		//Add different types of settings and corrosponding sections
178
		public function add_settings() {
179
180
			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

180
			/** @scrutinizer ignore-call */ 
181
   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

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

181
			/** @scrutinizer ignore-call */ 
182
   register_setting( 'settings_name', 'settings_field' );
Loading history...
182
			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

182
			/** @scrutinizer ignore-call */ 
183
   add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' );
Loading history...
183
		}
184
185
186
187
		//Section description
188
		public function section_cb() {
189
190
			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

190
			echo '<p class="description">' . /** @scrutinizer ignore-call */ __( 'Set up settings', 'textdomain' ) . '</p>';
Loading history...
191
		}
192
193
194
195
		//Field explanation
196
		public function settings_field_cb() {
197
198
			//Choose any one from input, textarea, select or checkbox
199
			/**
200
			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 />';
201
			echo '<textarea name="settings_field_name" id="settings_field_name" value="' . get_option('settings_field_name') . '>'. __( 'Enter Value', 'textdomain' ) . '</textarea>';
202
			echo '<select name="settings_field_name" id="settings_field_name"><option value="value" ' . selected( 'value', get_option('settings_field_name'), false) . '>Value</option></select>';
203
			echo '<input type="checkbox" id="settings_field_name" name="settings_field_name" value="1"' . checked( 1, get_option('settings_field_name'), false ) . '/>';
204
			*/
205
		}
206
	}
207
} ?>
208