Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/bdthemes-element-pack/includes/swatches/mapping.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + namespace ElementPack\VariationSwatches;
3 +
4 + defined( 'ABSPATH' ) || exit;
5 +
6 + use ElementPack\VariationSwatches\Admin\Settings;
7 +
8 + class Mapping {
9 +
10 + protected $plugins;
11 +
12 + protected $default_product_meta;
13 +
14 + public function __construct() {
15 + $this->define_supports();
16 +
17 + $this->default_product_meta = [
18 + 'type' => '',
19 + 'shape' => '',
20 + 'size' => '',
21 + 'custom_size' => ['width' => '', 'height' => ''],
22 + 'swatches' => [],
23 + ];
24 + }
25 +
26 + public function define_supports() {
27 + $this->add_plugin( 'ep-variaiton-swatches', [
28 + 'priority' => 1,
29 + 'settings' => [
30 + 'shape' => 'ep_variaiton_swatches_shape',
31 + 'size' => 'ep_variaiton_swatches_size',
32 + 'tooltip' => 'ep_variaiton_swatches_tooltip',
33 + 'auto_button' => 'ep_variaiton_swatches_auto_button',
34 + 'show_selected_label' => 'ep_variaiton_swatches_show_selected_label',
35 + ],
36 + 'product_meta' => [
37 + 'key' => 'ep_variaiton_swatches',
38 + 'map' => [
39 + 'type' => 'type',
40 + 'shape' => 'shape',
41 + 'size' => 'size',
42 + 'custom_size' => 'custom_size',
43 + 'swatches' => 'swatches',
44 + ],
45 + ],
46 + ] );
47 +
48 + // $this->add_plugin( 'woo-variation-swatches', [
49 + // 'priority' => 5,
50 + // 'settings' => [
51 + // 'shape' => 'woo_variation_swatches[style]',
52 + // 'tooltip' => 'woo_variation_swatches[tooltip]',
53 + // 'auto_button' => 'woo_variation_swatches[default_to_button]',
54 + // ],
55 + // 'attribute_meta' => [
56 + // 'color' => 'product_attribute_color',
57 + // 'image' => 'product_attribute_image',
58 + // ],
59 + // ] );
60 +
61 + // $this->add_plugin( 'variation-swatches-for-woocommerce-pro', [
62 + // 'priority' => 10,
63 + // 'settings' => [
64 + // 'shape' => 'tawcvs_swatch_style',
65 + // 'size' => 'tawcvs_swatch_image_size',
66 + // 'tooltip' => 'tawcvs_swatch_tooltip',
67 + // ],
68 + // 'product_meta' => [
69 + // 'key' => 'tawcvs_swatches',
70 + // 'map' => [
71 + // 'type' => 'type',
72 + // 'shape' => 'style',
73 + // 'size' => 'size',
74 + // 'custom_size' => 'custom_size',
75 + // 'swatches' => 'swatches',
76 + // ],
77 + // ],
78 + // 'attribute_meta' => [
79 + // 'color' => 'color',
80 + // 'image' => 'image',
81 + // 'label' => 'label',
82 + // ],
83 + // ] );
84 + }
85 + public function add_plugin( $plugin_name, $options ) {
86 + $options = wp_parse_args( (array) $options, [
87 + 'priority' => 10,
88 + 'settings' => [],
89 + 'product_meta' => [],
90 + 'attribute_meta' => [],
91 + ] );
92 +
93 + if ( ! empty( $this->plugins[ $plugin_name ] ) ) {
94 + $options = array_replace_recursive( $this->plugins[ $plugin_name ], $options );
95 + }
96 +
97 + $this->plugins[ $plugin_name ] = $options;
98 +
99 + $this->sort_plugins();
100 + }
101 +
102 + public function sort_plugins() {
103 + if ( count( $this->plugins ) > 1 ) {
104 + uasort( $this->plugins, [ $this, 'compare_plugins_priority' ] );
105 + }
106 + }
107 +
108 +
109 +
110 + public function compare_plugins_priority( $first, $second ) {
111 + return intval( $first['priority'] ) - intval( $second['priority'] );
112 + }
113 +
114 + public function get_option_names( $option ) {
115 + $names = [];
116 +
117 + foreach ( $this->plugins as $plugin ) {
118 + if ( ! empty( $plugin['settings'][ $option ] ) ) {
119 + $names[] = $plugin['settings'][ $option ];
120 + }
121 + }
122 +
123 + return $names;
124 + }
125 +
126 + public function get_option_value( $option ) {
127 + $names = $this->get_option_names( $option );
128 + $value = false;
129 +
130 + if ( empty( $names ) ) {
131 + return false;
132 + }
133 +
134 + foreach ( $names as $name ) {
135 +
136 + $pos = strpos( $name, '[' );
137 +
138 + if ( false === $pos ) {
139 + $value = get_option( $name );
140 + } else {
141 + parse_str( $name, $params );
142 +
143 + $option_name = key( $params );
144 + $sub_options = current( $params );
145 + $value = get_option( $option_name );
146 +
147 + if ( ! is_array( $value ) ) {
148 + $value = false;
149 + break;
150 + }
151 +
152 + while ( is_array( $sub_options ) ) {
153 + $key = key( $sub_options );
154 + $sub_options = current( $sub_options );
155 + $value = isset( $value[ $key ] ) ? $value[ $key ] : false;
156 + }
157 + }
158 +
159 + if ( false !== $value ) {
160 + break;
161 + }
162 + }
163 +
164 + if ( method_exists( $this, 'sanitize_' . strtolower( $option ) ) ) {
165 + $value = call_user_func_array( [ $this, 'sanitize_' . strtolower( $option ) ], [ $value ] );
166 + }
167 +
168 + return $value;
169 + }
170 +
171 + public function get_meta_value( $attribute_name, $product_id = null ) {
172 + $meta = $this->get_product_meta( $product_id );
173 +
174 + if ( empty( $meta ) ) {
175 + return false;
176 + }
177 +
178 + if ( empty( $meta[ $attribute_name ] ) ) {
179 + return false;
180 + }
181 +
182 + return $meta[ $attribute_name ];
183 + }
184 +
185 + public function get_product_meta( $product_id ) {
186 + $product_id = $product_id ? $product_id : get_the_ID();
187 + $meta = false;
188 +
189 + foreach ( $this->plugins as $plugin ) {
190 + if ( empty( $plugin['product_meta']['key'] ) ) {
191 + continue;
192 + }
193 +
194 + $meta = get_post_meta( $product_id, $plugin['product_meta']['key'], true );
195 +
196 + if ( ! empty( $meta ) ) {
197 + break;
198 + }
199 + }
200 +
201 + if ( empty( $meta ) ) {
202 + return false;
203 + }
204 +
205 + $formated = [];
206 +
207 + foreach ( $meta as $attribute_name => $settings ) {
208 + $formated[ $attribute_name ] = wp_parse_args( $settings, $this->default_product_meta );
209 +
210 + foreach ( $plugin['product_meta']['map'] as $key => $pair ) {
211 + $value = $settings[ $pair ];
212 +
213 + // Sanitize.
214 + if ( method_exists( $this, 'sanitize_' . strtolower( $key ) ) ) {
215 + $value = call_user_func_array( [ $this, 'sanitize_' . strtolower( $key ) ], [ $value ] );
216 + }
217 +
218 + if ( 'custom_size' == $key ) {
219 + $value = $this->sanitize_size( $value );
220 + }
221 +
222 + $formated[ $attribute_name ][ $key ] = $value;
223 + }
224 + }
225 +
226 + return $formated;
227 + }
228 +
229 + public function get_attribute_meta( $term_id, $type ) {
230 + foreach ( $this->plugins as $plugin ) {
231 + if ( empty( $plugin['attribute_meta'] ) || empty( $plugin['attribute_meta'][ $type ] ) ) {
232 + continue;
233 + }
234 +
235 + $meta = get_term_meta( $term_id, $plugin['attribute_meta'][ $type ], true );
236 +
237 + if ( ! empty( $meta ) ) {
238 + break;
239 + }
240 + }
241 +
242 + if ( empty( $meta ) ) {
243 + return false;
244 + }
245 +
246 + if ( method_exists( $this, 'sanitize_' . strtolower( $type ) ) ) {
247 + $meta = call_user_func_array( [ $this, 'sanitize_' . strtolower( $type ) ], [ $meta ] );
248 + }
249 +
250 + return $meta;
251 + }
252 +
253 + public function sanitize_type( $value ) {
254 + return Settings::instance()->sanitize_type( $value );
255 + }
256 +
257 + public function sanitize_size( $value ) {
258 + if ( is_string( $value ) ) {
259 + return empty( $value ) || 'custom' == $value ? $value : '';
260 + } elseif ( is_array( $value ) ) {
261 + return Settings::instance()->sanitize_size( $value );
262 + }
263 +
264 + return '';
265 + }
266 +
267 + public function sanitize_shape( $value ) {
268 + $value = 'squared' == $value ? 'square' : $value;
269 +
270 + return Settings::instance()->sanitize_shape( $value );
271 + }
272 + }
273 +