Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken in ACF 5.5.5 #42

Open
grassrootshost opened this issue Jan 19, 2017 · 7 comments
Open

Broken in ACF 5.5.5 #42

grassrootshost opened this issue Jan 19, 2017 · 7 comments

Comments

@grassrootshost
Copy link

After updating to 5.5.5, I am no longer able to save image fields with this plug-in.

I have the field loaded on an options page.

I select, and image, crop it, and save the page.

The page reloads without the image, and it is not saved to the database.

Same if I do not crop it.

Using the standard image field works fine.

@jomurgel
Copy link

jomurgel commented Jan 26, 2017

I can confirm this behavior as well. With debug on, I receive this error:

Notice: Undefined index: url in .../wp-content/plugins/advanced-custom-fields-pro/api/api-helpers.php on line 3851
Warning: Cannot modify header information - headers already sent by (output started at .../wp-content/plugins/advanced-custom-fields-pro/api/api-helpers.php:3851) in .../wp-admin/post.php on line 197
Warning: Cannot modify header information - headers already sent by (output started at .../wp-content/plugins/advanced-custom-fields-pro/api/api-helpers.php:3851) in .../wp-includes/pluggable.php on line 1179

However, downgrading to ACF 5.5.3 resolves the issues for me. I realize this doesn't help determine the origin of the issue, but some conflict exists as a result. No errors in the logs.

@NicolajKN
Copy link

Getting the same error as @jomurgel

@nickkeenan
Copy link

Some more detail on this issue:

My setup is:
WP 4.7.2
ACF PRO 5.5.5
ACF Pro Image Crop 1.4.10

There's a validation error in ACF Pro when the post is saved:

Undefined index: url in /srv/www/tsdca.org/current/web/app/plugins/acf-pro/api/api-helpers.php on line 3852
(next in callstack:
acf_validate_attachment( ) .../file.php:436
acf_field_file->validate_value( ) .../image.php:478
acf_field_image->validate_value( ) .../class-wp-hook.php:298

What seems to be happening is that ACF expects the image to be an object at this stage of validation (with a 'url' attribute), but ACF Image Crop is returning an attachment ID JSON for evaluation in this function, so validation basically doesn't happen and we get a bug error.

It looks like in addition to the update_value() child function in 24745e3 we'll need to validate proper Image Crop field JSON with a child function of validate_value() for Image Crop type fields.

I threw this into acf-image-crop-v5.php as a test and the notice goes away -- but of course now JSON isn't being validated at all! I'm not sure of all the potential cases here so I'm not sure that I can write a full pull request, but here's a stub to get the issue started if anyone is clearer on what needs to be validated:

    /*
    *  validate_value()
    *  Implement this function to avoid parent function taking over and trying to validate json data
    */
    function validate_value( $valid, $value, $field, $input ){

  		// bail early if empty
  		if( empty($value) ) return $valid;

  		
  		// bail early if is numeric
  		if( is_numeric($value) ) return $valid;


  		// bail early if not basic string
  		if( !is_string($value) ) return $valid;


  		// decode json
  		$image = json_decode($value);

      // TODO populate $error if poorly formed JSON or missing attributes

  		// append error
  		if( !empty($errors) ) {

  			$valid = implode("\n", $errors);

  		}

  		// return
  		return $valid;

	  }

@exophunk
Copy link

exophunk commented Mar 8, 2017

This is still broken. Is there any news on this?

ACF 5.5.10
Crop Plugin: 1.4.10

@grassrootshost
Copy link
Author

works fine for me after the last update.

@jessekahner
Copy link

jessekahner commented Mar 20, 2017

so i've put together a little filter for this. here it is:

    add_filter( 'acf/validate_save_post', 'my_validate_save_post', 4 );
    function my_validate_save_post(){

        remove_filter( 'acf/validate_save_post', 'my_validate_save_post', 4 );
        if( empty($_POST['acf']) ) return;
        
        // loop
        foreach( $_POST['acf'] as $field_key => $value ) {
            
            // get field
            $field = acf_get_field( $field_key );
            $input = 'acf[' . $field_key . ']';
            
            // var_dump($field);
            // bail early if not found
            if( !$field ) continue;
            
            if ( $field['type'] === 'image_crop' ) {
                $value = json_decode(stripslashes($value), true);
                $value['url'] = $value['cropped_image']['image'];
            }
            // validate
            acf_validate_value( $value, $field, $input );
            
        }
    }

it's been tested on WP 4.7.3 with ACF Pro 5.5.10 and the latest version of this plugin (1.4.10)

@Kwapi
Copy link

Kwapi commented Mar 28, 2017

@jessekahner Thanks for the patch. It works :)

UPDATE: After applying the patch I'm getting Illegal string offset 'image' for images that I haven't cropped. As soon as I crop them there's no errors. Anyone else experiencing this behaviour>

WP 4.7.3, ACF PRO 5.5.10, ACF Image crop 1.4.10

UPDATE 2: here's my hacky fix based on @jessekahner's work. Feel free to update it :)

add_filter( 'acf/validate_save_post', 'my_validate_save_post', 4 );
function my_validate_save_post(){

    remove_filter( 'acf/validate_save_post', 'my_validate_save_post', 4 );
    if( empty($_POST['acf']) ) return;

    // loop
    foreach( $_POST['acf'] as $field_key => $value ) {

        // get field
        $field = acf_get_field( $field_key );
        $input = 'acf[' . $field_key . ']';

        // var_dump($field);
        // bail early if not found
        if( !$field ) continue;

        if ( $field['type'] === 'image_crop' ) {
            $value = json_decode(stripslashes($value), true);
            
            // **the hacky fix**
            if(isset($value['cropped_image']['image'])){
                $value['url'] = $value['cropped_image']['image'];
            }else{
                $value['url'] = $value['cropped_image'];
            }

        }
        // validate
        acf_validate_value( $value, $field, $input );

    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants