-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_page.php
311 lines (232 loc) · 7.53 KB
/
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
<?php
/*--------------------------------------------------------------------------
*
* Acf_options_page
*
* @author Elliot Condon
* @since 2.0.4
*
*-------------------------------------------------------------------------*/
class acf_options_page
{
var $parent;
var $dir;
var $data;
/*--------------------------------------------------------------------------------------
*
* Acf_options_page
*
* @author Elliot Condon
* @since 2.0.4
*
*-------------------------------------------------------------------------------------*/
function __construct($parent)
{
// vars
$this->parent = $parent;
$this->dir = $parent->dir;
// data for passing variables
$this->data = array();
// actions
add_action('admin_menu', array($this,'admin_menu'));
}
/*--------------------------------------------------------------------------------------
*
* admin_menu
*
* @author Elliot Condon
* @since 2.0.4
*
*-------------------------------------------------------------------------------------*/
function admin_menu()
{
// validate
if(!$this->parent->is_field_unlocked('options_page'))
{
return true;
}
// vars
$parent_slug = 'acf-options';
$parent_title = __('Options','acf');
$parent_menu = __('Options','acf');
// redirect to first child
$custom = apply_filters('acf_register_options_page',array());
if( !empty($custom) )
{
$parent_slug = $custom[0]['slug'];
$parent_title = $custom[0]['title'];
}
// allow override of title
$parent_menu = apply_filters('acf_options_page_title', $parent_menu);
// Parent
$parent_page = add_menu_page($parent_title, $parent_menu, 'edit_posts', $parent_slug, array($this, 'html'));
// some fields require js + css
add_action('admin_print_scripts-'.$parent_page, array($this, 'admin_print_scripts'));
add_action('admin_print_styles-'.$parent_page, array($this, 'admin_print_styles'));
// Add admin head
add_action('admin_head-'.$parent_page, array($this,'admin_head'));
add_action('admin_footer-'.$parent_page, array($this,'admin_footer'));
if(!empty($custom))
{
foreach($custom as $c)
{
$child_page = add_submenu_page($parent_slug, $c['title'], $c['title'], 'edit_posts', $c['slug'], array($this, 'html'));
// some fields require js + css
add_action('admin_print_scripts-'.$child_page, array($this, 'admin_print_scripts'));
add_action('admin_print_styles-'.$child_page, array($this, 'admin_print_styles'));
// Add admin head
add_action('admin_head-'.$child_page, array($this,'admin_head'));
add_action('admin_footer-'.$child_page, array($this,'admin_footer'));
}
}
}
/*--------------------------------------------------------------------------------------
*
* admin_head
*
* @author Elliot Condon
* @since 2.0.4
*
*-------------------------------------------------------------------------------------*/
function admin_head()
{
// save
if(isset($_POST['update_options']))
{
do_action('acf_save_post', 'options');
$this->data['admin_message'] = __("Options Updated",'acf');
}
$metabox_ids = $this->parent->get_input_metabox_ids(false, false);
if(empty($metabox_ids))
{
$this->data['no_fields'] = true;
return false;
}
// Style
echo '<link rel="stylesheet" type="text/css" href="'.$this->parent->dir.'/css/global.css?ver=' . $this->parent->version . '" />';
echo '<link rel="stylesheet" type="text/css" href="'.$this->parent->dir.'/css/input.css?ver=' . $this->parent->version . '" />';
echo '<style type="text/css">#side-sortables.empty-container { border: 0 none; }</style>';
// Javascript
echo '<script type="text/javascript" src="'.$this->parent->dir.'/js/input-actions.js?ver=' . $this->parent->version . '" ></script>';
echo '<script type="text/javascript">acf.post_id = 0; </script>';
// add user js + css
do_action('acf_head-input');
// get acf's
$acfs = $this->parent->get_field_groups();
if($acfs)
{
foreach($acfs as $acf)
{
// hide / show
$show = in_array($acf['id'], $metabox_ids) ? "true" : "false";
if($show == "true")
{
// add meta box
add_meta_box(
'acf_' . $acf['id'],
$acf['title'],
array($this->parent->input, 'meta_box_input'),
'acf_options_page',
$acf['options']['position'],
'high',
array( 'fields' => $acf['fields'], 'options' => $acf['options'], 'show' => $show, 'post_id' => "options" )
);
}
}
}
}
/*--------------------------------------------------------------------------------------
*
* admin_footer
*
* @author Elliot Condon
* @since 2.0.4
*
*-------------------------------------------------------------------------------------*/
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
}
/*---------------------------------------------------------------------------------------------
* admin_print_scripts / admin_print_styles
*
* @author Elliot Condon
* @since 2.0.4
*
---------------------------------------------------------------------------------------------*/
function admin_print_scripts() {
do_action('acf_print_scripts-input');
}
function admin_print_styles() {
do_action('acf_print_styles-input');
}
/*--------------------------------------------------------------------------------------
*
* options_page
*
* @author Elliot Condon
* @since 2.0.4
*
*-------------------------------------------------------------------------------------*/
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="submit" class="acf-button" value="<?php _e("Save Options",'acf'); ?>" name="update_options" />
</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
}
}
?>