-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathacf-smart-button-v5.php
executable file
·362 lines (292 loc) · 10.7 KB
/
acf-smart-button-v5.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
<?php
class acf_field_smart_button extends acf_field {
/*
* __construct
*
* This function will setup the field type data
*
* @type function
* @since 1.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
/*
* name (string) Single word, no spaces. Underscores allowed
*/
$this->name = 'smart_button';
/*
* label (string) Multiple words, can include spaces, visible when selecting a field type
*/
$this->label = __('Smart Button', 'acf-smart-button');
/*
* category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
*/
$this->category = 'basic';
/*
* defaults (array) Array of default settings which are merged into the field object. These are used later in settings
*/
$this->defaults = array();
/*
* l10n (array) Array of strings that are used in JavaScript. This allows JS strings to be translated in PHP and loaded via:
* var message = acf.__('FIELD_NAME', 'error');
*/
$this->l10n = array(
'error' => __('Error! Please enter a higher value', 'acf-smart-button'),
);
/*
* setting (array) Array of default settings which are merged into the field object. These are used later in settings
*/
$this->settings = array(
'path' => apply_filters( 'acf/helpers/get_path', __FILE__ ),
'dir' => apply_filters( 'acf/helpers/get_dir', __FILE__ ),
'version' => '1.0.0'
);
// do not delete!
parent::__construct();
}
/*
* render_field_settings()
*
* Create extra settings for your field. These are visible when editing a field
*
* @type action
* @since 1.0.0
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field_settings( $field ) {
/*
* acf_render_field_setting
*
* This function will create a setting for your field. Simply pass the $field parameter and an array of field settings.
* The array of settings does not require a `value` or `prefix`; These settings are found from the $field array.
*
* More than one setting can be added by copy/paste the above code.
* Please note that you must also have a matching $defaults value for the field name (font_size)
*/
$field = array_merge( $this->defaults, $field );
/* add option to select which post type should be used on the post object. default: all */
acf_render_field_setting( $field, array(
'label' => __('Filter by Post Type', 'acf'),
'instructions' => '',
'type' => 'select',
'name' => 'post_type',
'choices' => acf_get_pretty_post_types(),
'multiple' => 1,
'ui' => 1,
'allow_null' => 1,
'placeholder' => __('All post types', 'acf'),
) );
}
/*
* render_field()
*
* Create the HTML interface for your field
*
* @param $field (array) the $field being rendered
*
* @type action
* @since 1.0.0
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field( $field ) {
$field = array_merge( $this->defaults, $field );
$field_name = esc_attr( $field['name'] );
/* overwrite fields with empty values to avoid warning */
$field['value']['text'] = isset( $field['value']['text'] ) ? $field['value']['text'] : null;
$field['value']['link'] = isset( $field['value']['link'] ) ? $field['value']['link'] : null;
$field['value']['post_id'] = isset( $field['value']['post_id'] ) ? $field['value']['post_id'] : null;
$field['value']['use_external'] = isset( $field['value']['use_external'] ) ? $field['value']['use_external'] : null;
$required = $field['required'] ? 'required' : null;
$required_asterisk = $field['required'] ? '<span class="acf-required">*</span>' : null;
?>
<table class="acf-smart-button-fields">
<tr>
<td valign="top" class="button-text">
<label><?php _e('Text', 'acf-smart-button'); echo $required_asterisk; ?></label>
<input type="text" value="<?php echo esc_attr( $field['value']['text'] ); ?>" name="<?php echo $field_name; ?>[text]" class="text"<?php echo $required ?>>
</td>
<td valign="top" class="external hidden">
<label><?php _e('External Link', 'acf-smart-button'); echo $required_asterisk; ?></label>
<?php
do_action('acf/render_field/type=url', array(
'type' => 'text',
'name' => $field_name . '[link]',
'value' => $field['value']['link'],
'id' => $field_name.'_external',
'class' => 'external',
'placeholder' => ''
));
?>
</td>
<td valign="top" class="internal">
<label><?php _e('Internal Link', 'acf-smart-button'); echo $required_asterisk; ?></label>
<?php
// str replace to get raw key (there seems to be no other way?)
$field_raw_key = str_replace( 'field_', '', $field['key'] );
?>
<div class="acf-field acf-field-<?php echo $field_raw_key; ?> acf-field-post-object" data-name="<?php echo $field['_name']; ?>[post_id]" data-type="post_object" data-key="<?php echo $field['key']; ?>">
<div class="acf-input">
<?php
// $types = array('post', 'page');
@do_action('acf/render_field/type=post_object', array(
'name' => $field_name . '[post_id]',
'value' => $field['value']['post_id'],
// 'post_type' => $types, // Removed so the selection isn't restricted to just posts and pages
'allow_null' => 1
//'_name' => 'acf[' . $field['_name'] . '][post_id]',
//'key' => 'acf[' . $field['key'] . '][post_id]'
));
?>
</div>
</div>
</td>
<td class="switcher-container">
<label><?php _e('Use external Link', 'acf-smart-button'); ?></label>
<div class="switcher">
<div class="button-link-switch">
<?php
$switcher_id = $field['id'] . '[use_external_switcher]';
?>
<input type="checkbox" name="<?php echo $field_name; ?>[use_external]" class="button-link-switch-checkbox" id="<?php echo $switcher_id; ?>"<?php if( $field['value']['use_external'] ) { echo ' checked'; } ?>>
<label class="button-link-switch-label" for="<?php echo $switcher_id; ?>"></label>
</div>
</td>
</tr>
</table>
<?php
}
/*
* input_admin_enqueue_scripts()
*
* This action is called in the admin_enqueue_scripts action on the edit screen where your field is created.
* Use this action to add CSS + JavaScript to assist your render_field() action.
*
* @type action (admin_enqueue_scripts)
* @since 1.0.0
*
* @param n/a
* @return n/a
*/
function input_admin_enqueue_scripts() {
$dir = plugin_dir_url( __FILE__ );
// register & include JS
wp_register_script( 'acf-smart-button', "{$dir}js/input.js" );
wp_enqueue_script( 'acf-smart-button' );
// register & include CSS
wp_register_style( 'acf-smart-button', "{$dir}css/input.css" );
wp_enqueue_style( 'acf-smart-button' );
}
/*
* format_value()
*
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template
*
* @type filter
* @since 1.0.0
*
* @param $value (mixed) the value which was loaded from the database
* @param $post_id (mixed) the $post_id from which the value was loaded
* @param $field (array) the field array holding all the field options
*
* @return $value (mixed) the modified value
*/
function format_value( $value, $post_id, $field ) {
// directly return false if there is no button text. A button is only valid if there is a valid text and url
if ( empty( $value['text'] ) ) {
return false;
}
// return url always as same data field, overwrite use_external with true or false for further processing
// Returns target="_blank" html as separate field to make the view even leaner
// internal
if( !array_key_exists( 'use_external', $value ) ) {
// return false if there is no post_id
if( empty( $value['post_id'] ) ) {
return false;
// add the values
} else {
$value['url'] = get_permalink($value['post_id']);
// empty target, is an empty string on purpose
$value['target'] = '';
}
// external
} else {
// return false if there is no link
if ( empty( $value['link'] ) ) {
return false;
// add the values
} else {
$value['url'] = $value['link'];
// set to open in a new window if external
$value['target'] = 'target="_blank"';
}
}
// unused fields that are not needed
unset( $value['link'] );
unset( $value['post_id'] );
unset( $value['use_external'] );
return $value;
}
/*
* validate_value()
*
* This filter is used to perform validation on the value prior to saving.
* All values are validated regardless of the field's required setting. This allows you to validate and return
* messages to the user if the value is not correct
*
* @type filter
* @since 1.0.4
*
* @param $valid (boolean) validation status based on the value and the field's required setting
* @param $value (mixed) the $_POST value
* @param $field (array) the field array holding all the field options
* @param $input (string) the corresponding input name for $_POST value
* @return $valid
*/
function validate_value( $valid, $value, $field, $input ) {
// store use_external for later use
$use_external = array_key_exists( 'use_external', $value ) ? true : false;
// unset use_external and target from value so we can easily check for empty $value
unset( $value['use_external'] );
unset( $value['target'] );
// not required and all fields are empty, so we're good
if( !$field['required'] && !$value ) {
return true;
}
// not required, but let's make sure corresponding fields are filled so we don't have partial data
if (!$field['required']) {
// link is set but no text
if( ( $value['link'] || $value['post_id'] ) && !$value['text'] ) {
$valid = __('Text is required with a link. Please either add text or remove the link.', 'acf-smart-button');
}
// text is present but no link set
if ( $value['text'] && ( ( $use_external && !$value['link'] ) || ( !$use_external && !$value['post_id'] ) ) ) {
$valid = __('A link is required with text. Please either add a link or remove the text.', 'acf-smart-button');
}
// field is required
} else {
// no text or link set
if ( !$value['text'] && $value['post_id'] && !$value['link'] ) {
$valid = __('Both text and link are required', 'acf-smart-button');
}
// link is set but no text
if( ( $value['link'] || $value['post_id'] ) && !$value['text'] ) {
$valid = __('Text is required', 'acf-smart-button');
}
// text is present but no link set
if ( $value['text'] && ( ( $use_external && !$value['link'] ) || ( !$use_external && !$value['post_id'] ) ) ) {
$valid = __('A link is required', 'acf-smart-button');
}
}
// return
return $valid;
}
}
// create field
new acf_field_smart_button();
?>