| 1 |
<?php |
| 2 |
|
| 3 |
namespace Disco\App\DropDown; |
| 4 |
|
| 5 |
use Disco\App\Disco; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class DropDown |
| 9 |
* |
| 10 |
* @package CTXFeed |
| 11 |
* @subpackage app\DropDown |
| 12 |
* @author Ohidul Islam <[email protected]> |
| 13 |
* @link https://webappick.com |
| 14 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 15 |
* @category MyCategory |
| 16 |
*/ |
| 17 |
class AttributeDropDown { |
| 18 |
|
| 19 |
/** |
| 20 |
* Get WooCommerce Attributes. |
| 21 |
* |
| 22 |
* @return array |
| 23 |
* @throws \Exception Exception. |
| 24 |
*/ |
| 25 |
public static function get_global_attributes() { |
| 26 |
$taxonomies = array(); |
| 27 |
$global_attributes = wc_get_attribute_taxonomy_labels(); |
| 28 |
|
| 29 |
if ( count( $global_attributes ) ) { |
| 30 |
foreach ( $global_attributes as $key => $value ) { |
| 31 |
$taxonomies[sprintf( 'global_attribute_pa_%s', $key )] = DropDown::prepare_filters( |
| 32 |
$value, |
| 33 |
'select', |
| 34 |
array( |
| 35 |
'type' => 'select', |
| 36 |
'option_type' => 'manual', |
| 37 |
'multiple' => true, |
| 38 |
'options' => get_terms( |
| 39 |
array( |
| 40 |
'taxonomy' => 'pa_' . $key, |
| 41 |
'fields' => 'id=>name', |
| 42 |
) |
| 43 |
), |
| 44 |
) |
| 45 |
); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return array( |
| 50 |
'optionGroup' => esc_html__( 'Product Attributes', 'disco' ), |
| 51 |
'options' => $taxonomies, |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get Advance Custom Field (ACF) field list |
| 57 |
* |
| 58 |
* @return array ACF Fields |
| 59 |
* @throws \Exception If setting is not found. |
| 60 |
*/ |
| 61 |
public static function get_acf_attributes(): array {// phpcs:ignore |
| 62 |
$options = array(); |
| 63 |
|
| 64 |
if ( class_exists( 'ACF' ) && function_exists( 'acf_get_field_groups' ) ) { |
| 65 |
// DO NOT USE here: $fields = acf_get_fields($group['key']); |
| 66 |
// because it causes repeater field bugs and returns "trashed" fields |
| 67 |
$field_groups = acf_get_field_groups(); |
| 68 |
|
| 69 |
foreach ( $field_groups as $group ) { |
| 70 |
$fields = get_posts( |
| 71 |
array( |
| 72 |
'posts_per_page' => -1, |
| 73 |
'post_type' => 'acf-field', |
| 74 |
'orderby' => 'menu_order', |
| 75 |
'order' => 'ASC', |
| 76 |
'suppress_filters' => false, // DO NOT allow WPML to modify the query |
| 77 |
'post_parent' => $group['ID'], |
| 78 |
'post_status' => 'any', |
| 79 |
'update_post_meta_cache' => false, |
| 80 |
) |
| 81 |
); |
| 82 |
|
| 83 |
foreach ( $fields as $field ) { |
| 84 |
// The field name, not the field key: that is what the value is |
| 85 |
// stored under, and so what Product::get_product_meta() reads. |
| 86 |
$key = 'acf_fields_' . $field->post_excerpt; |
| 87 |
$choices = self::get_acf_field_choices( $field ); |
| 88 |
|
| 89 |
/** |
| 90 |
* A field with a fixed set of choices (checkbox, select, radio) |
| 91 |
* is matched against that set, so it gets the list conditions |
| 92 |
* and a dropdown of its own options. Free text fields have no |
| 93 |
* options to offer and keep the string conditions. |
| 94 |
*/ |
| 95 |
if ( empty( $choices ) ) { |
| 96 |
$options[ $key ] = DropDown::prepare_filters( $field->post_title ); |
| 97 |
|
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$options[ $key ] = DropDown::prepare_filters( |
| 102 |
$field->post_title, |
| 103 |
'select', |
| 104 |
array( |
| 105 |
'type' => 'select', |
| 106 |
'option_type' => 'manual', |
| 107 |
'multiple' => true, |
| 108 |
'options' => $choices, |
| 109 |
) |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if ( empty( $options ) ) { |
| 116 |
return array( |
| 117 |
'optionGroup' => '', |
| 118 |
'options' => array(), |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
return array( |
| 123 |
'optionGroup' => esc_html__( 'Advance Custom Fields (ACF)', 'disco' ), |
| 124 |
'options' => $options, |
| 125 |
'disabled' => !Disco::is_pro(), // Disable if not pro. |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* The choices an ACF field offers, keyed by the value that gets stored. |
| 131 |
* |
| 132 |
* Read from the field post's own settings rather than through the ACF API: |
| 133 |
* acf_get_fields() is avoided here because it misbehaves with repeaters and |
| 134 |
* returns trashed fields, and the settings blob carries everything needed. |
| 135 |
* |
| 136 |
* @param \WP_Post $field ACF field post. |
| 137 |
* @return array<string, string> Stored value => label, empty when the field has no fixed choices. |
| 138 |
*/ |
| 139 |
private static function get_acf_field_choices( $field ): array { |
| 140 |
$settings = maybe_unserialize( $field->post_content ); |
| 141 |
|
| 142 |
if ( ! is_array( $settings ) || empty( $settings['choices'] ) || ! is_array( $settings['choices'] ) ) { |
| 143 |
return array(); |
| 144 |
} |
| 145 |
|
| 146 |
$choices = array(); |
| 147 |
|
| 148 |
foreach ( $settings['choices'] as $value => $label ) { |
| 149 |
if ( ! is_scalar( $label ) ) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
$choices[ (string) $value ] = (string) $label; |
| 154 |
} |
| 155 |
|
| 156 |
return $choices; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|