@@ -265,12 +265,12 @@ function render_field( $field ) {
265
265
$ type_options ['external ' ] = "External " ;
266
266
}
267
267
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>';
274
274
275
275
/**
276
276
* Generate some input fields.
@@ -281,6 +281,7 @@ function render_field( $field ) {
281
281
*/
282
282
if ( is_array ( $ type_options ) && count ( $ type_options ) > 0 ) {
283
283
?>
284
+ <label for="<?php echo esc_attr ( $ field ['name ' ] ) ?> [type]">Type</label>
284
285
<select name="<?php echo esc_attr ( $ field ['name ' ] ) ?> [type]">
285
286
<?php foreach ( $ type_options as $ option => $ label ) {
286
287
if ( is_array ( $ label ) ) {
@@ -324,7 +325,8 @@ function render_field( $field ) {
324
325
<?php echo $ label ?>
325
326
</label>
326
327
<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 ) ) {
328
330
printf (
329
331
'<option value="%s" %s>Archive</option> ' ,
330
332
$ key . '_archive ' ,
@@ -335,7 +337,7 @@ function render_field( $field ) {
335
337
<?php $ this_type = get_posts ( array ( 'post_type ' => $ key ) );
336
338
if ( $ this_type && count ( $ this_type ) > 0
337
339
) {
338
- if ( $ field ['post_type_archive ' ] ) {
340
+ if ( $ field ['post_type_archive ' ] && get_post_type_archive_link ( $ key ) ) {
339
341
echo '<option disabled>──────────</option> ' ;
340
342
}
341
343
foreach ( $ this_type as $ post ) {
@@ -746,30 +748,99 @@ function format_value( $value, $post_id, $field ) {
746
748
* @return $valid
747
749
*/
748
750
749
- /*
751
+ function validate_value ( $ valid , $ value , $ field , $ input ) {
750
752
751
- function validate_value( $valid, $value, $field, $input ){
753
+ $ valid_number = function ( $ possible_number ) {
754
+ return is_numeric ( $ possible_number );
755
+ };
752
756
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
+ }
758
764
765
+ return false ;
766
+ };
759
767
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
+ }
765
779
780
+ return false ;
781
+ };
766
782
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
+ }
769
794
770
- }
795
+ return false ;
796
+ };
771
797
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
+ }
773
844
774
845
775
846
/*
0 commit comments