Actions
7 months ago
Connectors
9 months ago
Security
9 months ago
Traits
7 months ago
Cmbird_Acf.php
9 months ago
Template.php
9 months ago
index.php
1 year ago
Cmbird_Acf.php
136 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 | // return if the ACF Class is not available. |
| 17 | if ( ! class_exists( 'ACF' ) ) { |
| 18 | return; |
| 19 | } |
| 20 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 21 | add_filter( 'acf/load_field/name=costunit', array( $this, 'cost_units' ), 20 ); |
| 22 | add_filter( 'acf/load_field/name=costcenter', array( $this, 'cost_centers' ), 20 ); |
| 23 | add_filter( 'acf/load_field/name=glaccount', array( $this, 'gl_accounts' ), 20 ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * GL Account custom field customization. |
| 28 | */ |
| 29 | public function gl_accounts( $field ): array { |
| 30 | if ( ! $this->is_order_edit_page() ) { |
| 31 | return $field; |
| 32 | } |
| 33 | $gl_accounts = ExactOnlineAjax::instance()->gl_account_get_options(); |
| 34 | if ( ! is_array( $gl_accounts ) ) { |
| 35 | $gl_accounts = array(); |
| 36 | } |
| 37 | return $this->extract_choice( $gl_accounts, $field ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Cost center custom field customization. |
| 42 | */ |
| 43 | public function cost_centers( $field ): array { |
| 44 | if ( ! $this->is_order_edit_page() ) { |
| 45 | return $field; |
| 46 | } |
| 47 | $cost_centers = ExactOnlineAjax::instance()->cost_center_get_options(); |
| 48 | if ( ! is_array( $cost_centers ) ) { |
| 49 | $cost_centers = array(); |
| 50 | } |
| 51 | return $this->extract_choice( $cost_centers, $field ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Cost Units custom field customization. |
| 56 | */ |
| 57 | public function cost_units( $field ): array { |
| 58 | if ( ! $this->is_order_edit_page() ) { |
| 59 | return $field; |
| 60 | } |
| 61 | $cost_units = ExactOnlineAjax::instance()->cost_unit_get_options(); |
| 62 | if ( ! is_array( $cost_units ) ) { |
| 63 | $cost_units = array(); |
| 64 | } |
| 65 | return $this->extract_choice( $cost_units, $field ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Check if the current page is an order edit page. |
| 70 | * |
| 71 | * @return boolean |
| 72 | */ |
| 73 | private function is_order_edit_page(): bool { |
| 74 | global $pagenow; |
| 75 | |
| 76 | // Check for the classic post edit screen for orders. |
| 77 | if ( 'post.php' === $pagenow && isset( $_GET['post'] ) && 'shop_order' === get_post_type( $_GET['post'] ) ) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // Check for the HPOS orders screen. |
| 82 | if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'wc-orders' === $_GET['page'] && isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) { |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Register ACF fields for the REST API of WooCommerce Orders. |
| 91 | */ |
| 92 | public function register_routes() { |
| 93 | $exclude_type = array( 'acf-field-group', 'acf-field' ); |
| 94 | $include_types = array( 'page' ); |
| 95 | $post_types = array_diff( get_post_types( array( '_builtin' => false ) ), $exclude_type ); |
| 96 | $post_types = array_merge( $post_types, $include_types ); |
| 97 | |
| 98 | array_walk( |
| 99 | $post_types, |
| 100 | function ( $post_type ) { |
| 101 | register_rest_field( |
| 102 | $post_type, |
| 103 | 'ACF', |
| 104 | array( |
| 105 | 'get_callback' => function ( $object ) { |
| 106 | if ( function_exists( 'get_fields' ) ) { |
| 107 | return get_fields( $object['id'] ); |
| 108 | } else { |
| 109 | return null; |
| 110 | } |
| 111 | }, |
| 112 | 'schema' => null, |
| 113 | ) |
| 114 | ); |
| 115 | } |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param array $data - ACF data |
| 121 | * @param array $field - ACF field |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | private function extract_choice( array $data, array $field ): array { |
| 126 | if ( $data ) { |
| 127 | $field['choices'] = array(); |
| 128 | foreach ( $data as $choice ) { |
| 129 | $field['choices'][ $choice ] = $choice; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return $field; |
| 134 | } |
| 135 | } |
| 136 |