<?php
defined('ABSPATH') || exit;

if (!function_exists('shopcentral_default_allowed_product_meta')) {

    function shopcentral_default_allowed_product_meta( $keys, $product_id ) {
        $defaults = [
            '_sku',
            '_regular_price',
            '_sale_price',
            '_weight',
            '_length',
            '_width',
            '_height',
            '_purchase_note',
            '_virtual',
            '_downloadable',
            '_backorders',
            '_sold_individually',
            '_stock_status',
            '_manage_stock',
            '_stock',
            '_low_stock_amount',
            '_product_type',
            '_tax_status',
            '_tax_class',
        ];

        foreach ( $defaults as $k ) {
            if ( ! in_array( $k, $keys, true ) ) {
                $keys[] = $k;
            }
        }

        if ( ! empty( $product_id ) && function_exists( 'get_field_objects' ) ) {
            $field_objects = get_field_objects( $product_id );
            if ( ! empty( $field_objects ) && is_array( $field_objects ) ) {
                foreach ( $field_objects as $f ) {
                    if ( ! empty( $f['name'] ) && ! in_array( $f['name'], $keys, true ) ) {
                        $keys[] = $f['name'];
                    }
                    if ( ! empty( $f['key'] ) && ! in_array( $f['key'], $keys, true ) ) {
                        $keys[] = $f['key'];
                    }
                }
            }
        }

        return $keys;
    }

    add_filter( 'shopcentral_allowed_product_meta', 'shopcentral_default_allowed_product_meta', 10, 2 );
}
