Passed
Push — master ( 2434f2...f94701 )
by Nirjhar
08:29
created

PLUGIN_SETTINGS::help_tabs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 9
rs 10
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
 * @version    1.2.1
9
 * @package    wp-plugin-framework
10
 */
11
if ( ! class_exists( 'PLUGIN_SETTINGS' ) ) {
12
13
	final class PLUGIN_SETTINGS {
14
15
16
		/**
17
		 * @var String
18
		 */
19
		public $capability;
20
21
22
		/**
23
		 * @var Array
24
		 */
25
		public $menu_page;
26
27
28
		/**
29
		 * @var Array
30
		 */
31
		public $sub_menu_page;
32
33
34
		/**
35
		 * @var Array
36
		 */
37
		public $help;
38
39
40
		/**
41
		 * @var String
42
		 */
43
		public $screen;
44
45
46
		/**
47
		 * @var Object
48
		 */
49
		 public $table
50
51
52
		/**
53
		 * Add basic actions for menu and settings
54
		 *
55
		 * @return Void
56
		 */
57
		public function __construct() {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_PUBLIC, expecting ',' or ';' on line 57 at column 2
Loading history...
58
59
			$this->capability = 'manage_options';
60
			$this->menu_page = array( 'name' => '', 'heading' => '', 'slug' => '' );
61
			$this->sub_menu_page = array(
62
									'name' => '',
63
									'heading' => '',
64
									'slug' => '',
65
									'parent_slug' => '',
66
									'help' => '',//true/false,
67
									'screen' => '',//true/false
68
								);
69
			$this->helpData = array(
70
								array(
71
								'slug' => '',
72
								'help' => array(
73
											'info' => array(
74
														array(
75
															'id' => 'helpId',
76
															'title' => __( 'Title', 'textdomain' ),
77
															'content' => __( 'Description', 'textdomain' ),
78
														),
79
													),
80
											'link' => '<p><a href="#">' . __( 'helpLink', 'textdomain' ) . '</a></p>',
81
											)
82
								)
83
							);
84
			$this->screen = ''; // true/false
85
86
			/**
87
			 * Add menues and hooks
88
			 *
89
			add_action( 'admin_init', array( $this, 'add_settings' ) );
90
			add_action( 'admin_menu', array( $this, 'menu_page' ) );
91
			add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
92
			add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
93
			 *
94
			 */
95
		}
96
97
98
		/**
99
		 * Add a sample main menu page callback
100
		 *
101
		 * @return Void
102
		 */
103
		public function menu_page() {
104
105
			if ($this->menu_page) {
106
				add_menu_page(
107
					$this->menu_page['name'],
108
					$this->menu_page['heading'],
109
					$this->capability,
110
					$this->menu_page['slug'],
111
					array( $this, 'menu_page_callback' )
112
				);
113
			}
114
		}
115
116
117
		/**
118
		 * Add a sample Submenu page callback
119
		 *
120
		 * @return Void
121
		 */
122
		public function sub_menu_page() {
123
124
			if ( $this->sub_menu_page ) {
125
				foreach ( $this->sub_menu_page as $page ) {
126
					$hook = add_submenu_page(
127
							$page['parent_slug'],
128
							$page['name'],
129
							$page['heading'],
130
							$this->capability,
131
							// For the first submenu page, slug should be same as menupage.
132
							$page['slug'],
133
							// For the first submenu page, callback should be same as menupage.
134
							array( $this, 'menu_page_callback' )
135
						);
136
					if ( $page['help'] ) {
137
						add_action( 'load-' . $hook, array( $this, 'help_tabs' ) );
138
					}
139
					if ( $page['screen'] ) {
140
						add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
141
					}
142
				}
143
			}
144
		}
145
146
147
		/**
148
		 * Set screen
149
		 *
150
		 * @param String $status
151
		 * @param String $option
152
		 * @param String $value
153
		 *
154
		 * @return String
155
		 */
156
		public function set_screen($status, $option, $value) {
157
158
			$user = get_current_user_id();
159
160
			switch ($option) {
161
				case 'option_name_per_page':
162
					update_user_meta($user, 'option_name_per_page', $value);
163
					$output = $value;
164
					break;
165
			}
166
167
    		if ( $output ) return $output; // Related to PLUGIN_TABLE()
168
		}
169
170
171
		/**
172
		 * Set screen option for Items table
173
		 *
174
		 * @return Void
175
		 */
176
		public function screen_option() {
177
178
			$option = 'per_page';
179
			$args   = array(
180
						'label'   => __( 'Show per page', 'textdomain' ),
181
						'default' => 10,
182
						'option'  => 'option_name_per_page' // Related to PLUGIN_TABLE()
183
						);
184
			add_screen_option( $option, $args );
185
			$this->table = new PLUGIN_TABLE(); // Source /lib/table.php
186
		}
187
188
189
		/**
190
		 * Menu page callback
191
		 *
192
		 * @return Html
193
		 */
194
		public function menu_page_callback() { ?>
195
196
			<div class="wrap">
197
				<h1><?php echo get_admin_page_title(); ?></h1>
198
				<br class="clear">
199
				<?php settings_errors();
200
201
					/**
202
					 * Following is the settings form
203
					 */ ?>
204
					<form method="post" action="">
205
						<?php settings_fields("settings_id");
206
						do_settings_sections("settings_name");
207
						submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
208
					</form>
209
210
					<?php
211
					/**
212
					 * Following is the data table class
213
					 */ ?>
214
					<form method="post" action="">
215
					<?php
216
						$this->table->prepare_items();
217
						$this->table->display(); ?>
218
					</form>
219
				<br class="clear">
220
			</div>
221
		<?php
222
		}
223
224
225
		/**
226
		 * Add help tabs using help data
227
		 *
228
		 * @return Void
229
		 */
230
		public function help_tabs() {
231
232
			foreach ($this->helpData as $value) {
233
				if ($_GET['page'] == $value['slug']) {
234
					$this->screen = get_current_screen();
235
					foreach( $value['info'] as $key ) {
236
						$this->screen->add_help_tab( $key );
237
					}
238
					$this->screen->set_help_sidebar( $value['link'] );
239
				}
240
			}
241
		}
242
243
244
		/**
245
		 * Add different types of settings and corrosponding sections
246
		 *
247
		 * @return Void
248
		 */
249
		public function add_settings() {
250
251
			add_settings_section( 'settings_id', __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
252
253
			register_setting( 'settings_id', 'settings_field_name' );
254
			add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' );
255
		}
256
257
258
		/**
259
		 * Section description
260
		 *
261
		 * @return Html
262
		 */
263
		public function section_cb() {
264
265
			echo '<p class="description">' . __( 'Set up settings', 'textdomain' ) . '</p>';
266
		}
267
268
269
		/**
270
		 * Field explanation
271
		 *
272
		 * @return Html
273
		 */
274
		public function settings_field_cb() {
275
276
			//Choose any one from input, textarea, select or checkbox
277
			/**
278
			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 />';
279
			echo '<textarea name="settings_field_name" id="settings_field_name" value="' . get_option('settings_field_name') . '>'. __( 'Enter Value', 'textdomain' ) . '</textarea>';
280
			echo '<select name="settings_field_name" id="settings_field_name"><option value="value" ' . selected( 'value', get_option('settings_field_name'), false) . '>Value</option></select>';
281
			echo '<input type="checkbox" id="settings_field_name" name="settings_field_name" value="1"' . checked( 1, get_option('settings_field_name'), false ) . '/>';
282
			*/
283
		}
284
	}
285
} ?>
286