-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacf-options-page.php
437 lines (323 loc) · 10.3 KB
/
acf-options-page.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/*
Plugin Name: Advanced Custom Fields: Options Page
Plugin URI: http://www.advancedcustomfields.com/
Description: Adds the options page
Version: 1.0.1
Author: Elliot Condon
Author URI: http://www.elliotcondon.com/
License: GPL
Copyright: Elliot Condon
*/
class acf_options_page_plugin
{
var $settings;
/*
* Constructor
*
* @description:
* @since 1.0.0
* @created: 23/06/12
*/
function __construct()
{
// vars
$this->settings = array(
'title' => __('Options','acf'), // title / menu name ('Site Options')
'capability' => 'edit_posts', // capability to view options page
'pages' => array(), // an array of sub pages ('Header, Footer, Home, etc')
);
// actions
add_action('init', array($this,'init'), 1 );
add_action('admin_menu', array($this,'admin_menu'), 11, 0);
// filters
add_filter('acf/location/rule_types', array($this,'acf_location_rules_types'));
add_filter('acf/location/rule_values/options_page', array($this,'acf_location_rules_values_options_page'));
}
/*
* init
*
* @description:
* @since: 3.6
* @created: 12/02/13
*/
function init()
{
// filters
$this->settings = apply_filters('acf/options_page/settings', $this->settings);
if( isset($GLOBALS['acf_options_pages']) && !empty($GLOBALS['acf_options_pages']) )
{
$this->settings['pages'] = array_merge( $this->settings['pages'], $GLOBALS['acf_options_pages'] );
}
}
/*
* acf_location_rules_types
*
* @description:
* @since: 3.6
* @created: 2/02/13
*/
function acf_location_rules_types( $choices )
{
$choices[ __("Options Page",'acf') ]['options_page'] = __("Options Page",'acf');
return $choices;
}
/*
* acf_location_rules_values_options_page
*
* @description:
* @since: 3.6
* @created: 2/02/13
*/
function acf_location_rules_values_options_page( $choices )
{
$choices = array(
'acf-options' => $this->settings['title']
);
$titles = $this->settings['pages'];
if( !empty($titles) )
{
$choices = array();
foreach( $titles as $title )
{
$slug = 'acf-options-' . sanitize_title( $title );
$choices[ $slug ] = $title;
}
}
return $choices;
}
/*
* admin_menu
*
* @description:
* @since: 2.0.4
* @created: 5/12/12
*/
function admin_menu()
{
// vars
$parent_slug = 'acf-options';
$parent_title = $this->settings['title'];
$parent_menu = $this->settings['title'];
// redirect to first child
if( !empty($this->settings['pages']) )
{
$parent_title = $this->settings['pages'][0];
$parent_slug = 'acf-options-' . sanitize_title( $parent_title );
}
// Parent
$parent_page = add_menu_page($parent_title, $parent_menu, $this->settings['capability'], $parent_slug, array($this, 'html'));
// actions
add_action('load-' . $parent_page, array($this,'admin_load'));
if( !empty($this->settings['pages']) )
{
foreach($this->settings['pages'] as $c)
{
$sub_title = $c;
$sub_slug = 'acf-options-' . sanitize_title( $sub_title );
$child_page = add_submenu_page($parent_slug, $sub_title, $sub_title, $this->settings['capability'], $sub_slug, array($this, 'html'));
// actions
add_action('load-' . $child_page, array($this,'admin_load'));
}
}
}
/*
* load
*
* @description:
* @since: 3.6
* @created: 2/02/13
*/
function admin_load()
{
add_action('admin_enqueue_scripts', array($this,'admin_enqueue_scripts'));
add_action('admin_head', array($this,'admin_head'));
add_action('admin_footer', array($this,'admin_footer'));
}
/*
* admin_enqueue_scripts
*
* @description: run after post query but before any admin script / head actions. A good place to register all actions.
* @since: 3.6
* @created: 26/01/13
*/
function admin_enqueue_scripts()
{
// actions
do_action('acf/input/admin_enqueue_scripts');
}
/*
* admin_head
*
* @description:
* @since: 2.0.4
* @created: 5/12/12
*/
function admin_head()
{
// verify nonce
if( isset($_POST['acf_nonce']) && wp_verify_nonce($_POST['acf_nonce'], 'input') )
{
do_action('acf/save_post', 'options');
$this->data['admin_message'] = __("Options Updated",'acf');
}
// get field groups
$filter = array();
$metabox_ids = array();
$metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
if( empty($metabox_ids) )
{
$this->data['no_fields'] = true;
return false;
}
// Style
echo '<style type="text/css">#side-sortables.empty-container { border: 0 none; }</style>';
// add user js + css
do_action('acf/input/admin_head');
// get field groups
$acfs = apply_filters('acf/get_field_groups', array());
if( $acfs )
{
foreach( $acfs as $acf )
{
// load options
$acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']);
// vars
$show = in_array( $acf['id'], $metabox_ids ) ? 1 : 0;
if( !$show )
{
continue;
}
// add meta box
add_meta_box(
'acf_' . $acf['id'],
$acf['title'],
array($this, 'meta_box_input'),
'acf_options_page',
$acf['options']['position'],
'high',
array( 'field_group' => $acf, 'show' => $show, 'post_id' => 'options' )
);
}
// foreach($acfs as $acf)
}
// if($acfs)
}
/*
* meta_box_input
*
* @description:
* @since 1.0.0
* @created: 23/06/12
*/
function meta_box_input( $post, $args )
{
// vars
$options = $args['args'];
echo '<div class="options" data-layout="' . $options['field_group']['options']['layout'] . '" data-show="' . $options['show'] . '" style="display:none"></div>';
$fields = apply_filters('acf/field_group/get_fields', array(), $options['field_group']['id']);
do_action('acf/create_fields', $fields, $options['post_id']);
}
/*
* admin_footer
*
* @description:
* @since: 2.0.4
* @created: 5/12/12
*/
function admin_footer()
{
// add togle open / close postbox
?>
<script type="text/javascript">
(function($){
$('.postbox .handlediv').live('click', function(){
var postbox = $(this).closest('.postbox');
if( postbox.hasClass('closed') )
{
postbox.removeClass('closed');
}
else
{
postbox.addClass('closed');
}
});
})(jQuery);
</script>
<?php
}
/*
* html
*
* @description:
* @since: 2.0.4
* @created: 5/12/12
*/
function html()
{
?>
<div class="wrap no_move">
<div class="icon32" id="icon-options-general"><br></div>
<h2><?php echo get_admin_page_title(); ?></h2>
<?php if(isset($this->data['admin_message'])): ?>
<div id="message" class="updated"><p><?php echo $this->data['admin_message']; ?></p></div>
<?php endif; ?>
<?php if(isset($this->data['no_fields'])): ?>
<div id="message" class="updated"><p><?php _e("No Custom Field Group found for the options page",'acf'); ?>. <a href="<?php echo admin_url(); ?>post-new.php?post_type=acf"><?php _e("Create a Custom Field Group",'acf'); ?></a></p></div>
<?php else: ?>
<form id="post" method="post" name="post">
<div class="metabox-holder has-right-sidebar" id="poststuff">
<!-- Sidebar -->
<div class="inner-sidebar" id="side-info-column">
<!-- Update -->
<div class="postbox">
<h3 class="hndle"><span><?php _e("Publish",'acf'); ?></span></h3>
<div class="inside">
<input type="hidden" name="HTTP_REFERER" value="<?php echo $_SERVER['HTTP_REFERER'] ?>" />
<input type="hidden" name="acf_nonce" value="<?php echo wp_create_nonce( 'input' ); ?>" />
<input type="submit" class="acf-button" value="<?php _e("Save Options",'acf'); ?>" />
</div>
</div>
<?php $meta_boxes = do_meta_boxes('acf_options_page', 'side', null); ?>
</div>
<!-- Main -->
<div id="post-body">
<div id="post-body-content">
<?php $meta_boxes = do_meta_boxes('acf_options_page', 'normal', null); ?>
<script type="text/javascript">
(function($){
$('#poststuff .postbox[id*="acf_"]').addClass('acf_postbox');
})(jQuery);
</script>
</div>
</div>
</div>
</form>
<?php endif; ?>
</div>
<?php
}
}
new acf_options_page_plugin();
/*
* register_options_page()
*
* This function is used to register an options page
*
* @type function
* @since 3.0.0
* @date 29/01/13
*
* @param $title - the page title
*
* @return
*/
if( !function_exists('register_options_page') )
{
$GLOBALS['acf_options_pages'] = array();
function register_options_page( $title = "" )
{
$GLOBALS['acf_options_pages'][] = $title;
}
}
?>