PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.8.3
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.8.3
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 2 months ago Connectors 2 months ago Security 7 months ago Traits 4 months ago Cmbird_Acf.php 7 months ago Template.php 4 months ago index.php 1 year ago
Cmbird_Acf.php
138 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 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Admin page detection.
78 if ( 'post.php' === $pagenow && isset( $_GET['post'] ) && 'shop_order' === get_post_type( sanitize_text_field( wp_unslash( $_GET['post'] ) ) ) ) {
79 return true;
80 }
81
82 // Check for the HPOS orders screen.
83 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Admin page detection.
84 if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'wc-orders' === $_GET['page'] && isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) {
85 return true;
86 }
87
88 return false;
89 }
90
91 /**
92 * Register ACF fields for the REST API of WooCommerce Orders.
93 */
94 public function register_routes() {
95 $exclude_type = array( 'acf-field-group', 'acf-field' );
96 $include_types = array( 'page' );
97 $post_types = array_diff( get_post_types( array( '_builtin' => false ) ), $exclude_type );
98 $post_types = array_merge( $post_types, $include_types );
99
100 array_walk(
101 $post_types,
102 function ( $post_type ) {
103 register_rest_field(
104 $post_type,
105 'ACF',
106 array(
107 'get_callback' => function ( $object ) {
108 if ( function_exists( 'get_fields' ) ) {
109 return get_fields( $object['id'] );
110 } else {
111 return null;
112 }
113 },
114 'schema' => null,
115 )
116 );
117 }
118 );
119 }
120
121 /**
122 * @param array $data - ACF data
123 * @param array $field - ACF field
124 *
125 * @return array
126 */
127 private function extract_choice( array $data, array $field ): array {
128 if ( $data ) {
129 $field['choices'] = array();
130 foreach ( $data as $choice ) {
131 $field['choices'][ $choice ] = $choice;
132 }
133 }
134
135 return $field;
136 }
137 }
138