Skip to content

Commit 13f6690

Browse files
committed
Added field validation
1 parent f3ab1f3 commit 13f6690

File tree

1 file changed

+95
-24
lines changed

1 file changed

+95
-24
lines changed

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

+95-24
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ function render_field( $field ) {
265265
$type_options['external'] = "External";
266266
}
267267

268-
echo '<pre>';
269-
var_dump( $field );
270-
271-
272-
var_dump( $type_options );
273-
echo '</pre>';
268+
// echo '<pre>';
269+
// var_dump( $field );
270+
//
271+
//
272+
// var_dump( $type_options );
273+
// echo '</pre>';
274274

275275
/**
276276
* Generate some input fields.
@@ -281,6 +281,7 @@ function render_field( $field ) {
281281
*/
282282
if ( is_array( $type_options ) && count( $type_options ) > 0 ) {
283283
?>
284+
<label for="<?php echo esc_attr( $field['name'] ) ?>[type]">Type</label>
284285
<select name="<?php echo esc_attr( $field['name'] ) ?>[type]">
285286
<?php foreach ( $type_options as $option => $label ) {
286287
if ( is_array( $label ) ) {
@@ -324,7 +325,8 @@ function render_field( $field ) {
324325
<?php echo $label ?>
325326
</label>
326327
<select name="<?php echo esc_attr( $field['name'] ) ?>[content][<?php echo esc_attr( $key ) ?>]">
327-
<?php if ( $field['post_type_archive'] ) {
328+
<option value="placeholder"></option>
329+
<?php if ( $field['post_type_archive'] && get_post_type_archive_link( $key ) ) {
328330
printf(
329331
'<option value="%s" %s>Archive</option>',
330332
$key . '_archive',
@@ -335,7 +337,7 @@ function render_field( $field ) {
335337
<?php $this_type = get_posts( array( 'post_type' => $key ) );
336338
if ( $this_type && count( $this_type ) > 0
337339
) {
338-
if ( $field['post_type_archive'] ) {
340+
if ( $field['post_type_archive'] && get_post_type_archive_link( $key ) ) {
339341
echo '<option disabled>──────────</option>';
340342
}
341343
foreach ( $this_type as $post ) {
@@ -746,30 +748,99 @@ function format_value( $value, $post_id, $field ) {
746748
* @return $valid
747749
*/
748750

749-
/*
751+
function validate_value( $valid, $value, $field, $input ) {
750752

751-
function validate_value( $valid, $value, $field, $input ){
753+
$valid_number = function ( $possible_number ) {
754+
return is_numeric( $possible_number );
755+
};
752756

753-
// Basic usage
754-
if( $value < $field['custom_minimum_setting'] )
755-
{
756-
$valid = false;
757-
}
757+
$valid_archive = function ( $possible_archive ) {
758+
$archive_raw = explode( '_', $possible_archive );
759+
if ( count( $archive_raw ) >= 2 ) {
760+
if ( get_post_type_archive_link( $archive_raw[0] ) ) {
761+
return true;
762+
}
763+
}
758764

765+
return false;
766+
};
759767

760-
// Advanced usage
761-
if( $value < $field['custom_minimum_setting'] )
762-
{
763-
$valid = __('The value is too little!','acf-zelda'),
764-
}
768+
/**
769+
* Assumes we've already checked that $possible_post is a number.
770+
*
771+
* @param $possible_post
772+
*
773+
* @return bool
774+
*/
775+
$valid_post = function ( $possible_post ) {
776+
if ( get_permalink( intval( $possible_post ) ) ) {
777+
return true;
778+
}
765779

780+
return false;
781+
};
766782

767-
// return
768-
return $valid;
783+
/**
784+
* Assumes we've already checked that $possible_taxonomy is a number.
785+
*
786+
* @param $possible_taxonomy
787+
*
788+
* @return bool
789+
*/
790+
$valid_taxonomy = function ( $possible_taxonomy ) {
791+
if ( get_term( intval( $possible_taxonomy ) ) ) {
792+
return true;
793+
}
769794

770-
}
795+
return false;
796+
};
771797

772-
*/
798+
$type = explode( '/', $value['type'] );
799+
$destination = Arrays::pluck( $value, $type );
800+
801+
switch ( $type[0] ) {
802+
case 'content':
803+
if ( $valid_archive( $destination ) ) {
804+
return true;
805+
} elseif ( $valid_number( $destination ) && $valid_post( $destination ) ) {
806+
return true;
807+
}
808+
809+
return __( 'Enter a valid content item.', 'acf-zelda' );
810+
break;
811+
812+
case 'taxonomy':
813+
if ( $valid_number( $destination ) && $valid_taxonomy( $destination ) ) {
814+
return true;
815+
}
816+
817+
return __( 'Enter a valid taxonomy.', 'acf-zelda' );
818+
break;
819+
820+
case 'email':
821+
$emails = explode( ',', $destination );
822+
foreach ( $emails as $email ) {
823+
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
824+
return sprintf( __( 'This email address is invalid: %s', 'my-text-domain' ), esc_html( $email ) );
825+
}
826+
}
827+
828+
return true;
829+
break;
830+
831+
case 'external':
832+
if ( filter_var( $destination, FILTER_VALIDATE_URL ) ) {
833+
return true;
834+
}
835+
836+
return __( 'Enter a valid URL.', 'acf-zelda' );
837+
break;
838+
839+
default:
840+
return false;
841+
break;
842+
}
843+
}
773844

774845

775846
/*

0 commit comments

Comments
 (0)