Actions
11 months ago
Connectors
11 months ago
Traits
1 year ago
Cmbird_Acf.php
1 year ago
Template.php
1 year ago
index.php
1 year ago
Cmbird_Acf.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CommerceBird\Admin; |
| 4 | |
| 5 | use CommerceBird\Admin\Actions\Ajax\ExactOnlineAjax; |
| 6 | use CommerceBird\Admin\Traits\Singleton; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | final class Cmbird_Acf { |
| 13 | use Singleton; |
| 14 | |
| 15 | public function __construct() { |
| 16 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 17 | add_filter( 'acf/load_field/name=costunit', array( $this, 'cost_units' ), 20 ); |
| 18 | add_filter( 'acf/load_field/name=costcenter', array( $this, 'cost_centers' ), 20 ); |
| 19 | add_filter( 'acf/load_field/name=glaccount', array( $this, 'gl_accounts' ), 20 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * GL Account custom field customization. |
| 24 | */ |
| 25 | public function gl_accounts( $field ): array { |
| 26 | $gl_accounts = ExactOnlineAjax::instance()->gl_account_get(); |
| 27 | return $this->extract_choice( $gl_accounts, $field ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Cost center custom field customization. |
| 32 | */ |
| 33 | public function cost_centers( $field ): array { |
| 34 | $cost_centers = ExactOnlineAjax::instance()->cost_center_get(); |
| 35 | |
| 36 | return $this->extract_choice( $cost_centers, $field ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Cost Units custom field customization. |
| 41 | */ |
| 42 | public function cost_units( $field ): array { |
| 43 | $cost_units = ExactOnlineAjax::instance()->cost_unit_get(); |
| 44 | |
| 45 | return $this->extract_choice( $cost_units, $field ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Register ACF fields for the REST API of WooCommerce Orders. |
| 50 | */ |
| 51 | public function register_routes() { |
| 52 | $exclude_type = [ 'acf-field-group', 'acf-field' ]; |
| 53 | $include_types = [ 'page' ]; |
| 54 | $post_types = array_diff( get_post_types( [ '_builtin' => false ] ), $exclude_type ); |
| 55 | $post_types = array_merge( $post_types, $include_types ); |
| 56 | |
| 57 | array_walk( |
| 58 | $post_types, |
| 59 | function ( $post_type ) { |
| 60 | register_rest_field( |
| 61 | $post_type, |
| 62 | 'ACF', |
| 63 | array( |
| 64 | 'get_callback' => function ($object) { |
| 65 | if ( function_exists( 'get_fields' ) ) { |
| 66 | return get_fields( $object['id'] ); |
| 67 | } else { |
| 68 | return null; |
| 69 | } |
| 70 | }, |
| 71 | 'schema' => null, |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param array $data - ACF data |
| 80 | * @param array $field - ACF field |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | private function extract_choice( array $data, array $field ): array { |
| 85 | if ( $data ) { |
| 86 | $field['choices'] = array(); |
| 87 | foreach ( $data as $choice ) { |
| 88 | $field['choices'][ $choice ] = $choice; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return $field; |
| 93 | } |
| 94 | } |
| 95 |