-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathIsFieldTrait.php
141 lines (122 loc) · 3.87 KB
/
IsFieldTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Product\AttributeMapping\Traits;
defined( 'ABSPATH' ) || exit;
/**
* Trait for fields
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product\AttributeMapping\Traits
*/
trait IsFieldTrait {
/**
* Returns false for the is_enum property
*
* @return false
*/
public static function is_enum(): bool {
return false;
}
/**
* Returns the attribute sources
*
* @return array The available sources
*/
public static function get_sources(): array {
return apply_filters(
'woocommerce_gla_attribute_mapping_sources',
array_merge(
self::get_source_product_fields(),
self::get_source_taxonomies(),
self::get_source_custom_attributes()
),
self::get_id()
);
}
/**
* Gets the taxonomies and global attributes to render them as options in the frontend.
*
* @return array An array with the taxonomies and global attributes
*/
public static function get_source_taxonomies(): array {
$object_taxonomies = get_object_taxonomies( 'product', 'objects' );
$taxonomies = [];
$attributes = [];
$sources = [];
foreach ( $object_taxonomies as $taxonomy ) {
if ( taxonomy_is_product_attribute( $taxonomy->name ) ) {
$attributes[ 'taxonomy:' . $taxonomy->name ] = $taxonomy->label;
continue;
}
$taxonomies[ 'taxonomy:' . $taxonomy->name ] = $taxonomy->label;
}
asort( $taxonomies );
asort( $attributes );
$attributes = apply_filters( 'woocommerce_gla_attribute_mapping_sources_global_attributes', $attributes );
$taxonomies = apply_filters( 'woocommerce_gla_attribute_mapping_sources_taxonomies', $taxonomies );
if ( ! empty( $attributes ) ) {
$sources = array_merge(
[
'disabled:attributes' => __( '- Global attributes -', 'google-listings-and-ads' ),
],
$attributes
);
}
if ( ! empty( $taxonomies ) ) {
$sources = array_merge(
$sources,
[
'disabled:taxonomies' => __( '- Taxonomies -', 'google-listings-and-ads' ),
],
$taxonomies
);
}
return $sources;
}
/**
* Get a list of the available product sources.
*
* @return array An array with the available product sources.
*/
public static function get_source_product_fields(): array {
$fields = [
'product:backorders' => __( 'Allow backorders setting', 'google-listings-and-ads' ),
'product:title' => __( 'Product title', 'google-listings-and-ads' ),
'product:sku' => __( 'SKU', 'google-listings-and-ads' ),
'product:stock_quantity' => __( 'Stock Qty', 'google-listings-and-ads' ),
'product:stock_status' => __( 'Stock Status', 'google-listings-and-ads' ),
'product:tax_class' => __( 'Tax class', 'google-listings-and-ads' ),
'product:name' => __( 'Variation title', 'google-listings-and-ads' ),
'product:weight' => __( 'Weight (raw value, no units)', 'google-listings-and-ads' ),
'product:weight_with_unit' => __( 'Weight (with units)', 'google-listings-and-ads' ),
];
asort( $fields );
$fields = array_merge(
[
'disabled:product' => __( '- Product fields -', 'google-listings-and-ads' ),
],
$fields
);
return apply_filters( 'woocommerce_gla_attribute_mapping_sources_product_fields', $fields );
}
/**
* Allowing to register custom attributes by using a filter.
*
* @return array The custom attributes
*/
public static function get_source_custom_attributes(): array {
$attributes = [];
$attribute_keys = apply_filters( 'woocommerce_gla_attribute_mapping_sources_custom_attributes', [] );
foreach ( $attribute_keys as $key ) {
$attributes[ 'attribute:' . $key ] = $key;
}
if ( ! empty( $attributes ) ) {
$attributes = array_merge(
[
'disabled:attribute' => __( '- Custom Attributes -', 'google-listings-and-ads' ),
],
$attributes
);
}
return $attributes;
}
}