-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar_selector-v3.php
executable file
·205 lines (161 loc) · 4.58 KB
/
sidebar_selector-v3.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
<?php
class acf_field_sidebar_selector extends acf_Field
{
// vars
var $settings, // will hold info such as dir / path
$defaults; // will hold default field options
/*--------------------------------------------------------------------------------------
*
* Constructor
* - This function is called when the field class is initalized on each page.
* - Here you can add filters / actions and setup any other functionality for your field
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function __construct($parent)
{
// do not delete!
parent::__construct($parent);
// set name / title
$this->name = 'sidebar_selector';
$this->title = __('Sidebar Selector', 'acf');
$this->defaults = array(
'allow_null' => '1',
'default_value' => ''
);
// settings
$this->settings = array(
'path' => $this->helpers_get_path(__FILE__),
'dir' => $this->helpers_get_dir(__FILE__),
'version' => '1.0.0'
);
}
/*
* helpers_get_path
*
* @description: calculates the path (works for plugin / theme folders)
* @since: 3.6
* @created: 30/01/13
*/
function helpers_get_path($file)
{
return trailingslashit(dirname($file));
}
/*
* helpers_get_dir
*
* @description: calculates the directory (works for plugin / theme folders)
* @since: 3.6
* @created: 30/01/13
*/
function helpers_get_dir($file)
{
$dir = trailingslashit(dirname($file));
$count = 0;
// sanitize for Win32 installs
$dir = str_replace('\\', '/', $dir);
// if file is in plugins folder
$wp_plugin_dir = str_replace('\\', '/', WP_PLUGIN_DIR);
$dir = str_replace($wp_plugin_dir, WP_PLUGIN_URL, $dir, $count);
if($count < 1)
{
// if file is in wp-content folder
$wp_content_dir = str_replace('\\', '/', WP_CONTENT_DIR);
$dir = str_replace($wp_content_dir, WP_CONTENT_URL, $dir, $count);
}
if($count < 1)
{
// if file is in ??? folder
$wp_dir = str_replace('\\', '/', ABSPATH);
$dir = str_replace($wp_dir, site_url('/'), $dir);
}
return $dir;
}
/*--------------------------------------------------------------------------------------
*
* create_options
* - this function is called from core/field_meta_box.php to create extra options
* for your field
*
* @params
* - $key (int) - the $_POST obejct key required to save the options to the field
* - $field (array) - the field object
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function create_options($key, $field)
{
$field = array_merge($this->defaults, $field);
$key = $field['name'];
// Create Field Options HTML
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Allow Null", 'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields[' . $key . '][allow_null]',
'value' => $field['allow_null'],
'layout' => 'horizontal',
'choices' => array(
'1' => __('Yes', 'acf'),
'0' => __('No', 'acf'),
)
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Default Value", 'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'text',
'name' => 'fields[' . $key . '][default_value]',
'value' => $field['default_value'],
));
?>
</td>
</tr>
<?php
}
/*--------------------------------------------------------------------------------------
*
* create_field
* - this function is called on edit screens to produce the html for this field
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function create_field($field)
{
global $wp_registered_sidebars;
// create Field HTML
?>
<div>
<select name='<?php echo $field['name'] ?>'>
<?php if ( !empty( $field['allow_null'] ) ) : ?>
<option value=''><?php _e( 'Select a Sidebar', 'acf' ) ?></option>
<?php endif ?>
<?php
foreach( $wp_registered_sidebars as $sidebar ) :
$selected = ( ( $field['value'] == $sidebar['id'] ) || ( empty( $field['value'] ) && $sidebar['id'] == $field['default_value'] ) ) ? 'selected="selected"' : '';
?>
<option <?php echo $selected ?> value='<?php echo $sidebar['id'] ?>'><?php echo $sidebar['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
<?php
}
}
?>