Skip to content

Commit f3129c8

Browse files
committed
Refactor how values are stored
1 parent 13f6690 commit f3129c8

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

fields/class-livy-acf-field-zelda-v5.php

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ function render_field( $field ) {
227227
* Review the data of $field.
228228
* This will show what data is available
229229
*/
230+
$stored = $field['value'] ?? null;
231+
$stored_type = $stored['type'] ?? false;
232+
$stored_value = $stored['value'] ?? false;
233+
$stored_class = $stored['class'] ?? false;
234+
$stored_text = $stored['text'] ?? false;
230235

231236
/**
232237
* Generate a list of possible link types.
@@ -265,13 +270,6 @@ function render_field( $field ) {
265270
$type_options['external'] = "External";
266271
}
267272

268-
// echo '<pre>';
269-
// var_dump( $field );
270-
//
271-
//
272-
// var_dump( $type_options );
273-
// echo '</pre>';
274-
275273
/**
276274
* Generate some input fields.
277275
*/
@@ -288,13 +286,13 @@ function render_field( $field ) {
288286
printf(
289287
'<optgroup label="%s">%s</optgroup>',
290288
$label['label'],
291-
join( '', Arrays::mapKeys( function ( $value, $key ) use ( $field, $label ) {
289+
join( '', Arrays::mapKeys( function ( $value, $key ) use ( $stored_type, $label ) {
292290
return [
293291
sprintf(
294292
'<option value="%s/%s" %s>%s</option>',
295293
$label['slug'],
296294
$key,
297-
$label['slug'] . '/' . $key == $field['value']['type'] ? 'selected' : null,
295+
$label['slug'] . '/' . $key == $stored_type ? 'selected' : null,
298296
$value
299297
)
300298
];
@@ -304,7 +302,7 @@ function render_field( $field ) {
304302
printf(
305303
'<option value="%s" %s>%s</option>',
306304
$option,
307-
$option == $field['value']['type'] ? 'selected' : null,
305+
$option == $stored_type ? 'selected' : null,
308306
$label
309307
);
310308
}
@@ -330,7 +328,7 @@ function render_field( $field ) {
330328
printf(
331329
'<option value="%s" %s>Archive</option>',
332330
$key . '_archive',
333-
$field['value']['content'][ $key ] == $key . '_archive' ? 'selected' : null
331+
$stored_value == $key . '_archive' ? 'selected' : null
334332
);
335333
} ?>
336334

@@ -343,7 +341,7 @@ function render_field( $field ) {
343341
foreach ( $this_type as $post ) {
344342
printf( '<option value="%s" %s>%s</option>',
345343
$post->ID,
346-
(int) $field['value']['content'][ $key ] == $post->ID ? 'selected' : null,
344+
intval( $stored_value ) == $post->ID ? 'selected' : null,
347345
$post->post_title
348346
);
349347
}
@@ -367,14 +365,15 @@ function render_field( $field ) {
367365
<?php echo $label ?>
368366
</label>
369367
<select name="<?php echo esc_attr( $field['name'] ) ?>[taxonomy][<?php echo esc_attr( $key ) ?>]">
368+
<option value="placeholder"></option>
370369
<?php $this_taxonomy = get_terms( array( 'taxonomy' => $key ) );
371370
if ( $this_taxonomy && count( $this_taxonomy ) > 0
372371
) {
373372
foreach ( $this_taxonomy as $taxonomy ) {
374373
/** @var $taxonomy \WP_Term */
375374
printf( '<option value="%s" %s>%s</option>',
376375
$taxonomy->term_taxonomy_id,
377-
(int) $field['value']['taxonomy'][ $key ] == $taxonomy->term_taxonomy_id ? 'selected' : null,
376+
intval( $stored_value ) == $taxonomy->term_taxonomy_id ? 'selected' : null,
378377
$taxonomy->name
379378
);
380379
}
@@ -394,7 +393,7 @@ function render_field( $field ) {
394393
<label for="<?php echo esc_attr( $field['name'] ) ?>[email]"><?php echo $type_options['email']
395394
?></label>
396395
<input type="email" name="<?php echo esc_attr( $field['name'] ) ?>[email]"
397-
value="<?php echo esc_attr( $field['value']['email'] ) ?>"/>
396+
value="<?php echo 'email' === $stored_type ? $stored_value : null; ?>"/>
398397
<?php
399398
}
400399

@@ -406,7 +405,7 @@ function render_field( $field ) {
406405
<label for="<?php echo esc_attr( $field['name'] ) ?>[external]"><?php echo $type_options['external']
407406
?></label>
408407
<input type="url" name="<?php echo esc_attr( $field['name'] ) ?>[external]"
409-
value="<?php echo esc_attr( $field['value']['external'] ) ?>"/>
408+
value="<?php echo 'external' === $stored_type ? $stored_value : null; ?>"/>
410409
<?php
411410
}
412411

@@ -417,7 +416,7 @@ function render_field( $field ) {
417416
?>
418417
<label for="<?php echo esc_attr( $field['name'] ) ?>[user_class]">Class</label>
419418
<input type="text" name="<?php echo esc_attr( $field['name'] ) ?>[user_class]"
420-
value="<?php echo esc_attr( $field['value']['user_class'] ) ?>">
419+
value="<?php echo $stored_class; ?>">
421420
<?php
422421
} else {
423422
// Still submit this value, but make it null
@@ -434,7 +433,7 @@ function render_field( $field ) {
434433
?>
435434
<label for="<?php echo esc_attr( $field['name'] ) ?>[user_text]">Text</label>
436435
<input type="text" name="<?php echo esc_attr( $field['name'] ) ?>[user_text]"
437-
value="<?php echo esc_attr( $field['value']['user_text'] ) ?>">
436+
value="<?php echo $stored_text ?>">
438437
<?php
439438
} else {
440439
// Still submit this value, but make it the default text
@@ -632,6 +631,26 @@ function load_value( $value, $post_id, $field ) {
632631
633632
*/
634633

634+
/**
635+
* Get the value for the data type.
636+
*
637+
* @param $type (string)
638+
* @param $data (array)
639+
*
640+
* @return bool|mixed
641+
*/
642+
function get_type_value_from_form( $type, $data ) {
643+
if ( ! is_string( $type ) || ! is_array( $data ) ) {
644+
return false;
645+
}
646+
647+
$directions = explode( '/', $type );
648+
649+
$return = Arrays::pluck( $data, $directions, true );
650+
651+
return null !== $return ? $return : false;
652+
}
653+
635654

636655
/*
637656
* update_value()
@@ -649,9 +668,14 @@ function load_value( $value, $post_id, $field ) {
649668
*/
650669

651670
function update_value( $value, $post_id, $field ) {
671+
$destination = $this->get_type_value_from_form( $value['type'], $value );
652672

653-
return $value;
654-
673+
return array(
674+
'type' => $value['type'],
675+
'value' => $destination,
676+
'class' => $value['user_class'] ?? false,
677+
'text' => $value['user_text'] ?? false,
678+
);
655679
}
656680

657681

@@ -681,7 +705,7 @@ function format_value( $value, $post_id, $field ) {
681705

682706
$type = explode( '/', $value['type'] );
683707

684-
$destination_raw = Arrays::pluck( $value, $type );
708+
$destination_raw = $value['value'];
685709
$destination = false;
686710
switch ( $type[0] ) {
687711
case 'content' :
@@ -707,8 +731,8 @@ function format_value( $value, $post_id, $field ) {
707731

708732
if ( $destination ) {
709733

710-
$class = trim( $value['user_class']
711-
? esc_attr( $field['link_class'] . ' ' . $value['user_class'] )
734+
$class = trim( $value['class']
735+
? esc_attr( $field['link_class'] . ' ' . $value['class'] )
712736
: esc_attr( $field['link_class'] ) );
713737

714738
$target = $field['new_tab']
@@ -720,7 +744,7 @@ function format_value( $value, $post_id, $field ) {
720744
esc_attr( $destination ),
721745
esc_attr( $class ),
722746
$target,
723-
$value['user_text']
747+
$value['text']
724748
);
725749
}
726750

@@ -795,6 +819,18 @@ function validate_value( $valid, $value, $field, $input ) {
795819
return false;
796820
};
797821

822+
if ( ! empty( $value['user_class'] ) ) {
823+
if ( 1 !== preg_match( '/^[a-zA-Z_\-0-9 ]*$/m', $value['user_class'] ) ) {
824+
return __( 'Enter valid class names.', 'acf-zelda' );
825+
}
826+
}
827+
828+
if ( ! empty( $value['user_text'] ) ) {
829+
if ( wp_kses_post( $value['user_text'] ) !== $value['user_text'] ) {
830+
return __( 'Enter valid link text.', 'acf-zelda' );
831+
}
832+
}
833+
798834
$type = explode( '/', $value['type'] );
799835
$destination = Arrays::pluck( $value, $type );
800836

0 commit comments

Comments
 (0)