AcfAjax.php
1 month ago
ExactOnlineAjax.php
4 days ago
SettingsAjax.php
1 month ago
ZohoCRMAjax.php
4 months ago
ZohoInventoryAjax.php
4 days ago
index.php
1 year ago
AcfAjax.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CommerceBird\Admin\Actions\Ajax; |
| 4 | |
| 5 | use CommerceBird\Admin\Traits\AjaxRequest; |
| 6 | use CommerceBird\Admin\Traits\OptionStatus; |
| 7 | use CommerceBird\Admin\Traits\Singleton; |
| 8 | use CommerceBird\Admin\Traits\LogWriter; |
| 9 | |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | /** |
| 15 | * Initializes the ACF functions class. |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | * @return void |
| 19 | */ |
| 20 | final class AcfAjax { |
| 21 | |
| 22 | use Singleton; |
| 23 | use AjaxRequest; |
| 24 | use OptionStatus; |
| 25 | use LogWriter; |
| 26 | |
| 27 | |
| 28 | |
| 29 | private const ACTIONS = array( |
| 30 | |
| 31 | 'get_acf_fields' => 'get_acf_fields', |
| 32 | ); |
| 33 | |
| 34 | public function __construct() { |
| 35 | if ( ! class_exists( 'ACF' ) || ! function_exists( 'acf_get_field_groups' ) || ! function_exists( 'acf_get_fields' ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $this->load_actions(); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | public function get_acf_fields(): void { |
| 44 | if ( ! function_exists( 'acf_get_field_groups' ) || ! function_exists( 'acf_get_fields' ) ) { |
| 45 | $this->response['fields'] = array(); |
| 46 | $this->serve(); |
| 47 | return; |
| 48 | } |
| 49 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Admin-only AJAX call, nonce verified in parent. |
| 50 | $post_type_value = isset( $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : ''; |
| 51 | if ( 'users' === $post_type_value ) { |
| 52 | $post_type_value = 'all'; |
| 53 | $group_type = 'user_role'; |
| 54 | } else { |
| 55 | $group_type = 'post_type'; |
| 56 | } |
| 57 | // Get all field groups associated with WooCommerce products. |
| 58 | $groups = acf_get_field_groups( array( $group_type => $post_type_value ) ); |
| 59 | |
| 60 | // Check if there are any field groups. |
| 61 | if ( ! $groups ) { |
| 62 | $this->response['fields'] = array(); |
| 63 | $this->serve(); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $fields = array(); |
| 68 | foreach ( $groups as $group ) { |
| 69 | $group_fields = acf_get_fields( $group['key'] ); |
| 70 | if ( is_array( $group_fields ) ) { |
| 71 | $fields = array_merge( $fields, $group_fields ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $this->response['fields'] = $fields; |
| 76 | $this->serve(); |
| 77 | } |
| 78 | } |
| 79 |