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( |
|
|
|
|
69
|
|
|
array( |
70
|
|
|
'slug' => '', |
71
|
|
|
'help' => array( |
72
|
|
|
'info' => array( |
73
|
|
|
array( |
74
|
|
|
'id' => 'helpId', |
75
|
|
|
'title' => __( 'Title', 'textdomain' ), |
|
|
|
|
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) { |
|
|
|
|
105
|
|
|
add_menu_page( |
|
|
|
|
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 ) { |
|
|
|
|
124
|
|
|
foreach ( $this->sub_menu_page as $page ) { |
125
|
|
|
$hook = add_submenu_page( |
|
|
|
|
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' ) ); |
|
|
|
|
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) { |
|
|
|
|
156
|
|
|
|
157
|
|
|
$user = get_current_user_id(); |
|
|
|
|
158
|
|
|
|
159
|
|
|
switch ($option) { |
160
|
|
|
case 'option_name_per_page': |
161
|
|
|
update_user_meta($user, 'option_name_per_page', $value); |
|
|
|
|
162
|
|
|
$output = $value; |
163
|
|
|
break; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ( $output ) return $output; // Related to PLUGIN_TABLE() |
|
|
|
|
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' ), |
|
|
|
|
180
|
|
|
'default' => 10, |
181
|
|
|
'option' => 'option_name_per_page' // Related to PLUGIN_TABLE() |
182
|
|
|
); |
183
|
|
|
add_screen_option( $option, $args ); |
|
|
|
|
184
|
|
|
$this->table = new PLUGIN_TABLE(); // Source /lib/table.php |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Menu page callback |
190
|
|
|
* |
191
|
|
|
* @return Html |
|
|
|
|
192
|
|
|
*/ |
193
|
|
|
public function menu_page_callback() { ?> |
194
|
|
|
|
195
|
|
|
<div class="wrap"> |
196
|
|
|
<h1><?php echo get_admin_page_title(); ?></h1> |
|
|
|
|
197
|
|
|
<br class="clear"> |
198
|
|
|
<?php settings_errors(); |
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Following is the settings form |
202
|
|
|
*/ ?> |
203
|
|
|
<form method="post" action=""> |
204
|
|
|
<?php settings_fields("settings_id"); |
|
|
|
|
205
|
|
|
do_settings_sections("settings_name"); |
|
|
|
|
206
|
|
|
submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?> |
|
|
|
|
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(); |
|
|
|
|
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' ); |
|
|
|
|
251
|
|
|
|
252
|
|
|
register_setting( 'settings_id', 'settings_field_name' ); |
|
|
|
|
253
|
|
|
add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' ); |
|
|
|
|
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>'; |
|
|
|
|
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
|
|
|
|