PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.14
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.14
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / admin / includes / Cmbird_Acf.php
commercebird / admin / includes Last commit date
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