Viewing 3 reply threads
  • Author
    Posts
    • #4515
      Mehdi Salem
      Member

      Hello,

      It seems that having a group add_more field causes the image validation to fail (tested with jpeg).

      The issue seems to happen in the validate_image function in class-piklist-validate.php

      I modified the validate_image function and I got it to work. Seems that in case of a group add_more field, the $value parameter is passed as a bi-dimensional array causing looping through $value returning an array instead of a string and the is_numeric( $value ) check to fail… I had to add another condition & loop…
      Now the other issue is that for some reason the function exif_imagetype is not working with my group add_more field…??? had to use getimagesize() instead

      Find bellow my updated version of the validate_image function with explanatory comments. It is not poetry but I’m sure you’ll get inspired to fix this the Piklist way…

      public static function validate_image($index, $value, $options, $field, $fields)
        {
          $field_value = is_array($value) ? $value : array($value);
      
          foreach ($field_value as $value)
          {
            // new condition and loop runs here: in case $value is a bi-dimensional array (caused by group add_more fields...?)
            if(is_array($value))
            {
              foreach($value as $val)
              {
      
                if ( $field['type'] == 'file' && is_numeric( $val ) ) {
                  $val = wp_get_attachment_url( $val );
                }
      
                // replaced function exif_imagetype() with getimagesize() -- exif is not active on all servers... getimagesize is less efficient but does the job it seems
                if ( /*! @exif_imagetype( $value )*/ ! @getimagesize( $val ) ) {
                  return __( 'contains a file that is not an image.', 'piklist' );
      
                }
      
              }
            }
            // original code runs here if $value was not a bi-dimensional array
            else
            {
      
              if ( $field['type'] == 'file' && is_numeric( $value ) ) {
                $value = wp_get_attachment_url( $value );
              }
      
              // replaced function exif_imagetype() with getimagesize() again
              if ( /*! @exif_imagetype( $value )*/ ! @getimagesize( $value ) ) {
                return __( 'contains a file that is not an image.', 'piklist' );
      
              }
            }
          }
      
          return true;
        }

      MY FIELDS CODE BELLOW:

      <?php
      /*
      Title: >Content
      Post Type: post
      Order: 20
      */

      piklist(‘field’, array(
      ‘type’ => ‘group’
      ,’field’ => ‘folder_content_group’
      /*,’template’ => ‘photo_gallery_meta_no_label’*/
      ,’label’ => __(‘Gallery Photo’,’stopping-the-world-admin’)
      ,’description’ => __(‘Add photos to this folder.’, ‘stopping-the-world-admin’)
      ,’help’ => __(‘Adds a new photo to this gallery folder and sets its availability on the store.’,’stopping-the-world-admin’)
      ,’add_more’ => true
      ,’fields’ => array(
      array(
      ‘type’ => ‘file’
      ,’field’ => ‘single_photo’
      ,’label’ => __(‘Photo’,’stopping-the-world-admin’)
      ,’options’ => array(
      ‘modal_title’ => __(‘Add a Photo’,’stopping-the-world-admin’)
      ,’button’ => __(‘Add’,’stopping-the-world-admin’)
      )
      ,’required’ => true
      ,’validate’ => array(
      /*array(
      ‘type’ => ‘limit’,
      ‘options’ => array(
      ‘min’ => 1,
      ‘max’ => 1
      )
      )
      ,*/array(
      ‘type’ => ‘image’
      )
      )
      )
      )
      ));

    • #4516
      Mehdi Salem
      Member

      Sorry, my group add_more fields is not the complete version, emailing all files to [email protected] 🙂

    • #4517
      Mehdi Salem
      Member

      It seems my fix works only if no extra field was added using the add_more function… 🙁

      I imagined I’d better loop as long as $value will be an array and tested it successfully by implementing another function that would call itself until $value is not an array anymore. See new code bellow.

      However it good to notice that if the validate_image function returns true on image validation success (I mean both, your original function and my modified one) I get this: PHP Notice: Undefined index: ID in ../wp-includes/post.php on line 3628

      Here is new function, even sketchier than the first one but working 😉

      So my fix is replacing the original validate_image function by thiscombination of two functions now and thanks for checking on all this:

      public static function validate_image($index, $value, $options, $field, $fields)
        {
          $field_value = is_array($value) ? $value : array($value);
      
          self::hdev_loop_again_if_array($field_value, $field);
      
          return true;
        }
      
        public static function hdev_loop_again_if_array($value, $field)
        {
          if(is_array($value))
          {
            $this_class_instance = new self;
      
            foreach($value as $val)
            {
              $this_class_instance->hdev_loop_again_if_array($val, $field);
            }
          }
          else
          {
            $field_value = is_array($value) ? $value : array($value);
            foreach($field_value as $val)
            {
              if ( $field['type'] == 'file' && is_numeric( $val ) ) {
                $val = wp_get_attachment_url( $val );
              }
      
              // replaced function exif_imagetype() with getimagesize() -- exif is not active on all servers... getimagesize is less efficient but does the job it seems
              if ( /*! @exif_imagetype( $value )*/ ! @getimagesize( $val ) ) {
                return __( 'contains a file that is not an image.', 'piklist' );
      
              }
      
            }
          }
        }
    • #4523
      Steve
      Keymaster

      @mnlearth– Thank you for identifying this issue and submitting a patch. We will review and release a fix.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.