Site icon Hip-Hop Website Design and Development

WP Rest_API- Post request for images returns empty

Iam new to php so I would appreciate your help.

Below code provides for rest api responses for taxonomy, metafields and gallery/image field.

When i issue a POST request via API, the image field returns as empty while the taxonomy and other meta fields return new value as expected.

As can be seen from below response json, the return value is [ ]

InputJson

{"Taxonomy1":[445],"meta":{‘field1′:’55’},"Imagefield":[‘/home/Imagefolder/1.jpeg’]}

ResponseJson

{"Taxonomy1":[445],"meta":{‘field1′:’55’},"Imagefield":[]}

Note:Imagefield is instance of GalleryField

I cant see why the Imagefield gives empty field([]). Is there anything that sticks out in the below code that needs to be edited? Any advise?

<?php

namespace DehakaManagers;

use DehakaCoreManager;
use DehakaModelPostCar;
use DehakaModelPostFieldAttachmentsField;
use DehakaModelPostFieldField;
use DehakaModelPostFieldGalleryField;
use DehakaModelPostFieldNumberField;
use DehakaModelPostFieldPricePriceField;
use DehakaModelPostFieldTaxonomyTaxonomy;
use WP_Post;

class RestApiManager extends Manager
{

    public function boot()
    {
        add_action('rest_api_init', [$this, 'registerFields']);

        add_action('rest_insert_' . Car::POST_TYPE, function (WP_Post $post, $request) {
            $meta = $request->get_param('meta');
            $car = new Car($post);

            dehakaApp('car_fields')
                ->filter(static function ($field) use ($meta) {
                    /* @var Field $field */
                    return !$field instanceof Taxonomy  && !$field instanceof GalleryField  && isset($meta[$field->getKey()]);
                })
                ->each(static function ($field) use ($car, $meta) {
                    $field->save($car, $meta[$field->getKey()]);
                });
        }, 10, 2);
    }

    public function registerFields()
    {
        foreach (dehakaApp('car_fields') as $field) {
            if ($field instanceof Taxonomy) {
                continue;
            }
            
            

            register_rest_field(Car::POST_TYPE, $field->getKey(), [
                'get_callback' => function ($post) use ($field) {
                    if (!$field instanceof GalleryField && !$field instanceof AttachmentsField && !$field instanceof PriceField) {
                        return get_post_meta($post['id'], $field->getKey(), true);
                    }

                    if ($field instanceof PriceField) {
                        $car = Car::getById($post['id']);
                        if (!$car) {
                            return [];
                        }

                        return $field->getValue($car);
                    }


                    $ids = explode(',', get_post_meta($post['id'], $field->getKey(), true));
                    if ($field instanceof GalleryField) {
                        $images = [];

                        foreach ($ids as $id) {
                            $imageUrl = wp_get_attachment_image_url($id, 'full');
                            if ($imageUrl) {
                                $images[] = $imageUrl;
                            }
                        }

                        return $images;
                    }

                    
                },
            ]);
        }
    }