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( |
|
|
|
|
33
|
|
|
array( |
34
|
|
|
'slug' => '', |
35
|
|
|
'help' => array( |
36
|
|
|
'info' => array( |
37
|
|
|
array( |
38
|
|
|
'id' => 'helpId', |
39
|
|
|
'title' => __( 'Title', 'textdomain' ), |
|
|
|
|
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) { |
|
|
|
|
66
|
|
|
add_menu_page( |
|
|
|
|
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) { |
|
|
|
|
82
|
|
|
$hook = add_submenu_page( |
|
|
|
|
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' ) ); |
|
|
|
|
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) { |
|
|
|
|
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', '' ), |
|
|
|
|
118
|
|
|
'default' => 10, |
119
|
|
|
'option' => 'option_name_per_page' // Related to PLUGIN_TABLE() |
120
|
|
|
); |
121
|
|
|
add_screen_option( $option, $args ); |
|
|
|
|
122
|
|
|
$this->Table = new PLUGIN_TABLE(); |
|
|
|
|
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> |
|
|
|
|
132
|
|
|
<br class="clear"> |
133
|
|
|
<?php settings_errors(); |
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Following is the settings form |
137
|
|
|
*/ ?> |
138
|
|
|
<form method="post" action=""> |
139
|
|
|
<?php settings_fields("settings_name"); |
|
|
|
|
140
|
|
|
do_settings_sections("settings_name"); |
|
|
|
|
141
|
|
|
submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?> |
|
|
|
|
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(); |
|
|
|
|
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' ); |
|
|
|
|
181
|
|
|
register_setting( 'settings_name', 'settings_field' ); |
|
|
|
|
182
|
|
|
add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' ); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
//Section description |
188
|
|
|
public function section_cb() { |
189
|
|
|
|
190
|
|
|
echo '<p class="description">' . __( 'Set up settings', 'textdomain' ) . '</p>'; |
|
|
|
|
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
|
|
|
|