-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWPSettingsBuilder.php
274 lines (263 loc) · 9.1 KB
/
WPSettingsBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* TODO: description for WPSettingsBuilder.php
*
* @package WPSettingsBuilder
* @subpackage WPSettingsBuilder.php
* @version $Id$
* @created 9/16/13 at 2:23 PM
*/
/**
* TODO: description for WPSettingsBuilder:WPSettingsBuilder
*
* @package WPSettingsBuilder
* @subpackage WPSettingsBuilder
* @version $Id$
* @author Damion M Broadaway <dbroadaw@nerdery.com>
* @created 9/16/13 at 2:23 PM
*/
class WPSettingsBuilder
{
private $_menuData;
private $_subMenuData;
private $_sectionData;
private $_fieldData;
private $_cptData;
private $_taxData;
public function __construct()
{
if (file_exists('WPSettingsBuilder_Data.php')) {
require_once 'WPSettingsBuilder_Data.php';
}
$this->_menuData = WPSettingsBuilder_Data::menu();
$this->_subMenuData = WPSettingsBuilder_Data::subMenu();
$this->_sectionData = WPSettingsBuilder_Data::section();
$this->_fieldData = WPSettingsBuilder_Data::fields();
$this->_cptData = WPSettingsBuilder_Data::customPostTypes();
$this->_taxData = WPSettingsBuilder_Data::taxonomy();
}
/**
* A wrapper for WordPress's method of adding
* menu items to the Dashboard menu
*
* Method expects the following well formatted array:
*
* array(
* array(
'page_title' => '',
'menu_title' => '',
'capability' => '',
'function' => array(
'',
''
),
'icon_url' => '',
'position' => ''
),
* array(...
* ),
* array(...
* ),
* Add as many sub arrays as need for menu items.
* );
*
* @see WPSettingsBuilder_Data.php for source data
* @link http://codex.wordpress.org/Function_Reference/add_menu_page
*
*/
public function addAdminMenu()
{
foreach ($this->_menuData as $menu_data){
add_menu_page(
$menu_data['page_title'],
$menu_data['menu_title'],
$menu_data['capability'],
$menu_data['menu_slug'],
$menu_data['function'],
$menu_data['icon_url'],
$menu_data['position']
);
}
}
/**
* A wrapper for WordPress's method of adding
* submenu items to the Dashboard menu
*
* Method expects $args to be the following well formatted array:
*
* array(
* array(
* 'parent_slug' => '',
* 'page_title' => '',
* 'menu_title' => '',
* 'capability' => '',
* 'menu_slug' => '',
* 'function' => array(
* '',
* ''
* )
* ),
* array(...
* ),
* array(...
* )
* Add as many sub arrays as needed to represent all submenu items
* )
*
* @see WPSettingsBuilder_Data.php for source data
* @link http://codex.wordpress.org/Function_Reference/add_submenu_page
*/
public function addAdminSubMenu()
{
foreach ($this->_subMenuData as $sub_menu_data) {
add_submenu_page(
$sub_menu_data['parent_slug'],
$sub_menu_data['page_title'],
$sub_menu_data['menu_title'],
$sub_menu_data['capability'],
$sub_menu_data['menu_slug'],
$sub_menu_data['function']
);
}
}
/**
* A wrapper for WordPress's method to add admin option sections
*
* Method expects $args to be the following well formatted array
*
* array(
* array(
'id' => '',
'title' => '',
'callback' => array(
'',
''
),
'page' => '',
),
* array(...
* ),
* array(...
* )
* Add as many sub arrays as needed to represent all sections
* )
*
* @see WPSettingsBuilder_Data.php for source data
* @link http://codex.wordpress.org/Function_Reference/add_settings_section
*/
public function addAdminSections()
{
foreach ($this->_sectionData as $section_data) {
add_settings_section(
$section_data['id'],
$section_data['title'],
array(
$section_data['callback_class'],
$section_data['callback_method']
),
$section_data['page']
);
}
}
/**
* A wrapper for WordPress's add_settings_field & register_settings
* methods
*
* Method expects $args to be the following well formatted array
*
* array(
* array(
* 'slug' => '',
* 'title' => '',
* 'callbackClass' => '',
* 'callbackMethod' => '',
* 'optionGroup' => '',
* 'section' => '',
* 'class' => '',
* 'desc' => '',
* ),
* array(...
* ),
* array(...
* )
* Add as many sub arrays as needed to represent all fields
* )
*
* @see WPSettingsBuilder_Data.php for source data
* @link http://codex.wordpress.org/Function_Reference/add_settings_section
*/
public function addAdminFields()
{
foreach ($this->_fieldData as $field_data ) {
add_settings_field(
$field_data['id'],
$field_data['title'],
$field_data['callback'],
$field_data['page'],
$field_data['section'],
$field_data['args']
);
register_setting(
$field_data['option_group'],
$field_data['option_name'],
$field_data['sanitize_callback']
);
}
}
public function addCustomPostType()
{
foreach ( $this->_cptData as $cpt_data ) {
$labels = array(
'name'
=> _x($cpt_data['post_type_singular'], 'post type general name'),
'singular_name'
=> _x($cpt_data['post_type_singular'], 'post type singular name'),
'add_new'
=> _x('Add New', $cpt_data['post_type_singular']),
'add_new_item'
=> __('Add New ' . $cpt_data['post_type_singular']),
'edit_item'
=> __('Edit ' . $cpt_data['post_type_singular']),
'new_items'
=> __('New ' . $cpt_data['post_type_singular']),
'all_items'
=> __('All ' . $cpt_data['post_type_plural']),
'view_item'
=> __('View ' . $cpt_data['post_type_plural']),
'search_items'
=> __('Search ' . $cpt_data['post_type_plural']),
'not_found'
=> __('No ' . $cpt_data['post_type_plural'] . ' found'),
'not_found_in_trash'
=> __('No ' . $cpt_data['post_type_plural'] . ' found in the trash'),
'parent_item_colon'
=> '',
'menu_name'
=> $cpt_data['post_type_plural']
);
$cpt_data['cpt_args']['label'] = $cpt_data['post_type_plural'];
$cpt_data['cpt_args']['labels'] = $labels;
register_post_type($cpt_data['post_type'], $cpt_data['cpt_args']);
}
}
public function addCustomTaxonomy()
{
foreach ( $this->_taxData as $taxonomy_data ) {
$labels = array(
'name' => _x($taxonomy_data['plural_tax_name'], 'taxonomy general name'),
'singular_name' => _x($taxonomy_data['singular_tax_name'], 'taxonomy singular name'),
'search_items' => __('Search ' . $taxonomy_data['plural_tax_name']),
'all_items' => __('All ' . $taxonomy_data['plural_tax_name']),
'parent_item' => __('Parent ' . $taxonomy_data['singular_tax_name']),
'parent_item_colon' => __('Parent ' . $taxonomy_data['singular_tax_name'] . ':'),
'edit_item' => __('Edit ' . $taxonomy_data['singular_tax_name']),
'update_item' => __('Update ' . $taxonomy_data['singular_tax_name']),
'add_new_item' => __('Add New ' . $taxonomy_data['singular_tax_name']),
'new_item_name' => __('New ' . $taxonomy_data['singular_tax_name'] . ' Name'),
'menu_name' => __($taxonomy_data['plural_tax_name']),
);
$taxonomy_data['tax_args']['labels'] = $labels;
register_taxonomy($taxonomy_data['tax_type'], $taxonomy_data['for_post_type_of'], $taxonomy_data['tax_args']);
}
}
}