joppuyo / acf-image-aspect-ratio-crop

Image Aspect Ratio Crop field for Advanced Custom Fields
https://wordpress.org/plugins/acf-image-aspect-ratio-crop/
GNU General Public License v2.0
106 stars 16 forks source link

Rest API throws "Fatal Error: Call to undefined method stdClass::format_value_for_rest()" #130

Open kdechant opened 1 year ago

kdechant commented 1 year ago

When using an "Image Aspect Ratio Crop" field that is exposed to the WP REST API, I get a PHP error.

GET http://my.site.domain/wp-json/wp/v2/pages returns this:

Fatal error: Uncaught Error: Call to undefined method stdClass::format_value_for_rest() in /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/acf-rest-api-functions.php:86

  | Stack trace:   | #0 /path/to/site/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-repeater.php(982): acf_format_value_for_rest('44100', 13663, Array)   | #1 /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/acf-rest-api-functions.php(86): acf_field_repeater->format_value_for_rest(Array, 13663, Array)   | #2 /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/class-acf-rest-api.php(234): acf_format_value_for_rest(Array, 13663, Array, 'light')   | #3 /path/to/site/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(439): ACF_Rest_Api->load_fields(Array, 'acf', Object(WP_REST_Request), 'page')   | #4 /path/to/site/wp in /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/acf-rest-api-functions.php on line 86

This happens when I use the following plugins together:

There is a Field Group with a field of type "Repeater". One of the sub-fields of the repeater is the "Image Aspect Ratio Crop" field. The Field Group has the "Show in REST API" setting turned on.

The error also appears when I try to edit content in the WP admin.

The error doesn't occur when using ?acf_format=standard in the query string. (e.g., GET http://my.site.domain/wp-json/wp/v2/pages?acf_format=standard) but that doesn't fix the WP admin.

It appears that this occurs because the plugin doesn't call acf_register_field_type to register the field class here: https://github.com/joppuyo/acf-image-aspect-ratio-crop/blob/e9c4c119168042234884f3ba09ecc21440a9ff9e/fields/class-npx-acf-field-image-aspect-ratio-crop-v5.php#L1138

kdechant commented 1 year ago

Changing the "initialize" part in class-npx-acf-field-image-aspect-ratio-crop-v5.php to this seems to fix it:

// initialize
$instance = new npx_acf_field_image_aspect_ratio_crop($this->settings);
acf_register_field_type($instance);

This is how the other plugins register their fields, including the fields built in to ACF itself. Anyone know if there is any reason that wouldn't work?

joppuyo commented 1 year ago

Hey, thanks for the detailed bug report. Unfortunately I have very limited time to maintain this plugin but I will try to fix this issue as soon as possible.

jameelmoses commented 1 year ago

@joppuyo I've created a pull request that resolves the issue based on @kdechant's feedback

joppuyo commented 1 year ago

I have reserved some time next week to look through all issues in this plugin and I will also try to solve this issue.

joppuyo commented 1 year ago

Hey, right now version 6 of this plugin is minimally maintained and it gets only bug fixes. New features like ACF REST API compatibility will be added in version 7.