PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.1.5
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.1.5
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / libraries / XmlExportWooCommerce.php
wp-all-export / libraries Last commit date
VariableProductTitle 8 years ago .gitkeep 8 years ago WpaeInvalidPhpException.php 8 years ago WpaeInvalidStringException.php 8 years ago WpaeMethodNotFoundException.php 8 years ago WpaePhpInterpreterErrorHandler.php 8 years ago WpaeString.php 8 years ago WpaeTooMuchRecursionException.php 8 years ago WpaeXmlProcessor.php 8 years ago XmlCsvExport.php 8 years ago XmlExportACF.php 8 years ago XmlExportComment.php 8 years ago XmlExportCpt.php 8 years ago XmlExportEngine.php 8 years ago XmlExportFiltering.php 8 years ago XmlExportMediaGallery.php 8 years ago XmlExportTaxonomy.php 8 years ago XmlExportUser.php 8 years ago XmlExportWooCommerce.php 8 years ago XmlExportWooCommerceCoupon.php 8 years ago XmlExportWooCommerceOrder.php 8 years ago XmlGoogleMerchants.php 8 years ago XmlSpec.php 8 years ago
XmlExportWooCommerce.php
1231 lines
1 <?php
2
3 if ( ! class_exists('XmlExportWooCommerce') )
4 {
5 final class XmlExportWooCommerce
6 {
7 public static $products_data = null;
8
9 private $init_fields = array(
10 array(
11 'name' => 'SKU',
12 'type' => 'woo',
13 'label' => '_sku'
14 ),
15 array(
16 'name' => 'product_type',
17 'type' => 'cats',
18 'label' => 'product_type',
19 ),
20 array(
21 'label' => 'parent',
22 'name' => 'Parent Product ID',
23 'type' => 'parent',
24 )
25 );
26
27 private $_woo_data = array();
28 private $_product_data = array();
29
30 /** @var \Wpae\App\Service\WooCommerceVersion */
31 private $wooCommerceVersion;
32
33 private static $_existing_attributes = array();
34
35 public static $is_active = true;
36
37 public function __construct()
38 {
39 $this->_woo_data = array(
40 '_visibility', '_stock_status', '_downloadable', '_virtual', '_regular_price', '_sale_price', '_purchase_note', '_featured', '_weight', '_length',
41 '_width', '_height', '_sku', '_sale_price_dates_from', '_sale_price_dates_to', '_price', '_sold_individually', '_manage_stock', '_stock', '_upsell_ids', '_crosssell_ids',
42 '_downloadable_files', '_download_limit', '_download_expiry', '_download_type', '_product_url', '_button_text', '_backorders', '_tax_status', '_tax_class', '_product_image_gallery', '_default_attributes',
43 'total_sales', '_product_attributes', '_product_version', '_variation_description', '_wc_rating_count', '_wc_review_count', '_wc_average_rating'
44 );
45
46 $this->wooCommerceVersion = new \Wpae\App\Service\WooCommerceVersion();
47 $this->_product_data = array('_sku', '_price', '_regular_price','_sale_price', '_stock_status', '_stock', '_visibility', '_product_url', 'total_sales', 'attributes');
48
49 if ( ! class_exists('WooCommerce')
50 or ( XmlExportEngine::$exportOptions['export_type'] == 'specific' and ! in_array('product', XmlExportEngine::$post_types) )
51 or ( XmlExportEngine::$exportOptions['export_type'] == 'advanced' and strpos(XmlExportEngine::$exportOptions['wp_query'], 'product') === false ) ) {
52 self::$is_active = false;
53 return;
54 }
55
56 self::$is_active = true;
57
58 if ( empty(PMXE_Plugin::$session) ) // if cron execution
59 {
60 $id = $_GET['export_id'];
61 $export = new PMXE_Export_Record();
62 $export->getById($id);
63 if ( $export->options['export_to'] == 'csv'){
64 $this->init_additional_data();
65 }
66 }
67 else
68 {
69 self::$products_data = PMXE_Plugin::$session->get('products_data');
70
71 if (empty(self::$products_data))
72 {
73 $this->init_additional_data();
74 }
75 }
76
77 global $wp_taxonomies;
78
79 $max_input_vars = @ini_get('max_input_vars');
80 if (empty($max_input_vars)) $max_input_vars = 100;
81 foreach ($wp_taxonomies as $key => $obj) { if (in_array($obj->name, array('nav_menu'))) continue;
82
83 if (strpos($obj->name, "pa_") === 0 and strlen($obj->name) > 3 ){
84
85 if ( count($this->init_fields) < $max_input_vars / 10 ){
86 $this->init_fields[] = array(
87 'name' => $obj->label,
88 'label' => $obj->name,
89 'type' => 'attr'
90 );
91 }
92 }
93 }
94
95 if ( ! empty(self::$products_data['attributes']))
96 {
97 foreach (self::$products_data['attributes'] as $attribute) {
98 if ( count($this->init_fields) < $max_input_vars / 10 ) {
99 $this->init_fields[] = $this->fix_titles(array(
100 'name' => str_replace('attribute_', '', $attribute->meta_key),
101 'label' => $attribute->meta_key,
102 'type' => 'cf'
103 ));
104 }
105 }
106 }
107
108 $this->init_fields[] = array(
109 'name' => 'Product Attributes',
110 'label' => '_product_attributes',
111 'type' => 'woo'
112 );
113
114 add_filter("wp_all_export_init_fields", array( &$this, "filter_init_fields"), 10, 1);
115 add_filter("wp_all_export_default_fields", array( &$this, "filter_default_fields"), 10, 1);
116 add_filter("wp_all_export_other_fields", array( &$this, "filter_other_fields"), 10, 1);
117 add_filter("wp_all_export_available_sections", array( &$this, "filter_available_sections"), 10, 1);
118 add_filter("wp_all_export_available_data", array( &$this, "filter_available_data"), 10, 1);
119
120 }
121
122 // [FILTERS]
123
124 /**
125 *
126 * Filter Init Fields
127 *
128 */
129 public function filter_init_fields($init_fields){
130 foreach ($this->init_fields as $field) {
131 $init_fields[] = $field;
132 }
133 return array_map(array( &$this, 'fix_titles'), $init_fields);
134 }
135
136 /**
137 *
138 * Filter Default Fields
139 *
140 */
141 public function filter_default_fields($default_fields){
142 foreach ($default_fields as $key => $field) {
143 $default_fields[$key]['auto'] = true;
144 }
145 $default_fields[] = array(
146 'label' => 'parent',
147 'name' => 'Parent Product ID',
148 'type' => 'parent',
149 'auto' => true
150 );
151 return array_map(array( &$this, 'fix_titles'), $default_fields);
152 }
153
154 /**
155 *
156 * Filter Other Fields
157 *
158 */
159 public function filter_other_fields($other_fields){
160
161 $advanced_fields = $this->get_fields_for_product_advanced_section();
162
163 foreach ($advanced_fields as $advanced_field)
164 {
165 $other_fields[] = $advanced_field;
166 }
167
168 // add needed fields to auto generate list
169 foreach ($other_fields as $key => $field)
170 {
171 if ( strpos($field['label'], '_min_') === 0 || strpos($field['label'], '_max_') === 0 )
172 continue;
173
174 if ($field['type'] == 'parent'){
175 unset($other_fields[$key]);
176 continue;
177 }
178
179 if ( $field['type'] != 'attr' ) $other_fields[$key]['auto'] = true;
180
181 $other_fields[$key] = $this->fix_titles($other_fields[$key]);
182 }
183
184 return $other_fields;
185 }
186 /**
187 *
188 * Helper method to fix fields title
189 *
190 */
191 protected function fix_titles($field)
192 {
193 if (is_array($field))
194 {
195 $field['name'] = $this->fix_title($field['name']);
196 }
197 else
198 {
199 $field = $this->fix_title($field);
200 }
201 return $field;
202 }
203 /**
204 *
205 * Helper method to fix single title
206 *
207 */
208 protected function fix_title($title)
209 {
210 $uc_title = ucwords(trim(str_replace("_", " ", $title)));
211
212 return stripos($uc_title, "width") === false ? str_ireplace(array(' id ', ' url ', ' sku ', ' wc '), array('ID', 'URL', 'SKU', 'WC'), $uc_title) : $uc_title;
213 }
214
215 // helper method
216 protected function get_fields_for_product_advanced_section()
217 {
218 $advanced_fields = array();
219
220 if ( ! empty($this->_woo_data))
221 {
222 foreach ($this->_woo_data as $woo_key)
223 {
224 if ( strpos($woo_key, 'attribute_pa_') === 0 ) continue;
225
226 if ( ! in_array($woo_key, $this->_product_data) )
227 {
228 switch ($woo_key)
229 {
230 case '_upsell_ids':
231 $advanced_fields[] = array(
232 'name' => 'Up-Sells',
233 'label' => $woo_key,
234 'type' => 'woo'
235 );
236 break;
237 case '_crosssell_ids':
238 $advanced_fields[] = array(
239 'name' => 'Cross-Sells',
240 'label' => $woo_key,
241 'type' => 'woo'
242 );
243 break;
244
245 default:
246 $advanced_fields[] = $this->fix_titles(array(
247 'name' => $woo_key,
248 'label' => $woo_key,
249 'type' => 'woo'
250 ));
251 break;
252 }
253 }
254 }
255 }
256 return $advanced_fields;
257 }
258
259 protected function get_fields_for_product_attributes_section()
260 {
261 $attributes_fields = array();
262
263 if ( empty(self::$_existing_attributes) )
264 {
265 global $wp_taxonomies;
266
267 foreach ($wp_taxonomies as $key => $obj) { if (in_array($obj->name, array('nav_menu'))) continue;
268
269 if (strpos($obj->name, "pa_") === 0 and strlen($obj->name) > 3 and ! in_array($obj->name, self::$_existing_attributes))
270 self::$_existing_attributes[] = $obj->name;
271 }
272 }
273
274 if ( ! empty(self::$_existing_attributes) )
275 {
276 foreach (self::$_existing_attributes as $key => $tx_name) {
277 $tx = get_taxonomy($tx_name);
278 $attributes_fields[] = array(
279 'name' => $tx->label,
280 'label' => $tx_name,
281 'type' => 'attr'
282 );
283 }
284 }
285
286 if ( ! empty(self::$products_data['attributes']))
287 {
288 foreach (self::$products_data['attributes'] as $attribute) {
289 $attributes_fields[] = $this->fix_titles(array(
290 'name' => str_replace('attribute_', '', $attribute->meta_key),
291 'label' => $attribute->meta_key,
292 'type' => 'cf'
293 ));
294 }
295 }
296
297 return $attributes_fields;
298 }
299
300 /**
301 *
302 * Filter Available Data
303 *
304 */
305 public function filter_available_data($available_data){
306 $available_data['woo_data'] = $this->_woo_data;
307 $available_data['existing_attributes'] = self::$_existing_attributes;
308 $available_data['product_fields'] = $this->get_fields_for_product_data_section();
309 // $available_data['product_attributes'] = $this->get_fields_for_product_attributes_section();
310
311 if ( ! empty($available_data['existing_taxonomies']) ) {
312 $existing_taxonomies = $available_data['existing_taxonomies'];
313 $available_data['existing_taxonomies'] = array();
314 foreach ($existing_taxonomies as $taxonomy) {
315 $tx = get_taxonomy($taxonomy['label']);
316 if ($taxonomy['label'] == 'product_shipping_class')
317 {
318 $available_data['product_fields'][] = array(
319 'name' => 'Shipping Class',
320 'label' => $taxonomy['label'],
321 'type' => 'cats',
322 'auto' => true
323 );
324 }
325 else
326 {
327 $available_data['existing_taxonomies'][] = array(
328 'name' => ($taxonomy['label'] == 'product_type') ? 'Product Type' : (empty($tx->label) ? $tx->name : $tx->label),
329 'label' => $taxonomy['label'],
330 'type' => 'cats',
331 'auto' => true
332 );
333 }
334 }
335 }
336
337 return $available_data;
338 }
339 // helper method
340 protected function get_fields_for_product_data_section()
341 {
342 $product_fields = array();
343 if ( ! empty($this->_product_data) )
344 {
345 foreach ($this->_product_data as $woo_key)
346 {
347 switch ($woo_key)
348 {
349 case '_product_url':
350 $product_fields[] = array(
351 'name' => 'External Product URL',
352 'label' => $woo_key,
353 'type' => 'woo',
354 'auto' => true
355 );
356 break;
357
358 default:
359 $product_fields[] = $this->fix_titles(array(
360 'name' => $woo_key,
361 'label' => $woo_key,
362 'type' => 'woo',
363 'auto' => true
364 ));
365 break;
366 }
367 }
368 }
369 return $product_fields;
370 }
371
372 /**
373 *
374 * Filter Sections in Available Data
375 *
376 */
377 public function filter_available_sections($available_sections){
378
379 $available_sections['other']['title'] = __("Other", "wp_all_export_plugin");
380
381 $product_data = array(
382 'product_data' => array(
383 'title' => __("Product Data", "wp_all_export_plugin"),
384 'content' => 'product_fields',
385 'additional' => array(
386 'attributes' => array(
387 'title' => __("Attributes", "wp_all_export_plugin"),
388 'meta' => $this->get_fields_for_product_attributes_section()
389 )
390 )
391 ),
392 // 'product_attributes' => array(
393 // 'title' => __("Attributes", "wp_all_export_plugin"),
394 // 'content' => 'product_attributes'
395 // ),
396 );
397 return array_merge(array_slice($available_sections, 0, 1), $product_data, array_slice($available_sections, 1));
398 }
399
400 // [\FILTERS]
401
402 public function init( & $existing_meta_keys = array() ){
403
404 if ( ! self::$is_active ) return;
405
406 $hide_fields = array('_edit_lock', '_edit_last');
407
408 $this->filter_meta_keys( $existing_meta_keys );
409
410 global $wp_taxonomies;
411
412 foreach ($wp_taxonomies as $key => $obj) { if (in_array($obj->name, array('nav_menu'))) continue;
413
414 if (strpos($obj->name, "pa_") === 0 and strlen($obj->name) > 3 and ! in_array($obj->name, self::$_existing_attributes))
415 self::$_existing_attributes[] = $obj->name;
416 }
417
418 }
419 // helper method
420 protected function filter_meta_keys( & $meta_keys = array() )
421 {
422 if ( ! empty($meta_keys) )
423 {
424 foreach ($meta_keys as $key => $record_meta_key) {
425
426 if ( in_array($record_meta_key, $this->_woo_data) ) unset($meta_keys[$key]);
427
428 if ( strpos($record_meta_key, 'attribute_pa_') === 0 || strpos($record_meta_key, '_min_') === 0 || strpos($record_meta_key, '_max_') === 0){
429 if ( ! in_array($record_meta_key, $this->_woo_data)){
430 $this->_woo_data[] = $record_meta_key;
431 unset($meta_keys[$key]);
432 }
433 }
434 if ( strpos($record_meta_key, 'attribute_') === 0 && strpos($record_meta_key, 'attribute_pa_') === false )
435 {
436 unset($meta_keys[$key]);
437 }
438 }
439 }
440 }
441
442 public function init_additional_data()
443 {
444 if ( ! self::$is_active ) return;
445
446 $cs = PMXE_Plugin::getInstance()->getAdminCurrentScreen();
447
448 if ( empty(self::$products_data) or ! empty($cs) and 'PMXE_Admin_Manage' == $cs->base )
449 {
450
451 self::$products_data = array();
452
453 global $wpdb;
454
455 $table_prefix = $wpdb->prefix;
456
457 self::$products_data['attributes'] = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT meta_key FROM {$table_prefix}postmeta
458 WHERE {$table_prefix}postmeta.meta_key LIKE %s AND {$table_prefix}postmeta.meta_key NOT LIKE %s", 'attribute_%', 'attribute_pa_%'));
459
460 if ( ! empty(PMXE_Plugin::$session) )
461 {
462 PMXE_Plugin::$session->set('products_data', self::$products_data);
463 PMXE_Plugin::$session->save_data();
464 }
465 }
466 }
467
468 // [prepare fields for order items]
469 public function get_all_fields_for_order_items()
470 {
471 $meta_keys = wp_all_export_get_existing_meta_by_cpt('product');
472
473 $this->filter_meta_keys( $meta_keys );
474
475 $default_data = $this->get_default_fields_for_order_items();
476
477 $product_data = $this->get_fields_for_product_data_section();
478
479 $taxes_data = array();
480
481 foreach ($product_data as $field)
482 {
483 if ( ! in_array($field['label'], array('_sku', 'attributes'))) $default_data[] = $field;
484 }
485
486 $existing_taxonomies = wp_all_export_get_existing_taxonomies_by_cpt('product');
487
488 foreach ($existing_taxonomies as $taxonomy)
489 {
490 $tx = get_taxonomy($taxonomy['label']);
491 if ($taxonomy['label'] == 'product_shipping_class')
492 {
493 $product_data[] = array(
494 'name' => 'Shipping Class',
495 'label' => $taxonomy['name'],
496 'type' => 'cats',
497 'auto' => true
498 );
499 }
500 else
501 {
502 $taxes_data[] = array(
503 'name' => ($taxonomy['label'] == 'product_type') ? 'Product Type' : $tx->label,
504 'label' => $taxonomy['label'],
505 'type' => 'cats',
506 'auto' => true
507 );
508 }
509 }
510
511 return array(
512 'product_data' => array(
513 'title' => __('Product Data', 'wp_all_export_plugin'),
514 'meta' => $default_data,
515 // 'additional' => array(
516 // 'attributes' => array(
517 // 'title' => __("Attributes", "wp_all_export_plugin"),
518 // 'meta' => $this->get_fields_for_product_attributes_section()
519 // )
520 // )
521 ),
522 'taxonomies' => array(
523 'title' => __('Taxonomies', 'wp_all_export_plugin'),
524 'meta' => $taxes_data
525 ),
526 'cf' => array(
527 'title' => __('Custom Fields', 'wp_all_export_plugin'),
528 'meta' => $meta_keys
529 ),
530 'attributes' => array(
531 'title' => __('Attributes', 'wp_all_export_plugin'),
532 'meta' => $this->get_fields_for_product_attributes_section()
533 ),
534 'advanced' => array(
535 'title' => __('Advanced', 'wp_all_export_plugin'),
536 'meta' => $this->get_fields_for_product_advanced_section()
537 )
538 );
539 }
540 // helper method
541 protected function get_default_fields_for_order_items()
542 {
543 $exclude_default_fields = array('id', 'title', 'permalink');
544
545 $default_fields = array();
546 foreach (XmlExportEngine::$default_fields as $field)
547 {
548 if ( ! in_array($field['type'], $exclude_default_fields) ) $default_fields[] = $field;
549 }
550 return $default_fields;
551 }
552 // [\prepare fields for order items]
553
554 protected function prepare_export_data( $record, $options, $elId )
555 {
556 $data = array();
557
558 $element_value = str_replace("item_data__", "", $options['cc_value'][$elId]);
559
560 if ( ! empty($element_value) )
561 {
562 $implode_delimiter = XmlExportEngine::$implode;
563
564 $element_name = ( ! empty($options['cc_name'][$elId]) ) ? $options['cc_name'][$elId] : 'untitled_' . $elId;
565 $fieldSnipped = ( ! empty($options['cc_php'][$elId]) and ! empty($options['cc_code'][$elId]) ) ? $options['cc_code'][$elId] : false;
566
567 switch ($element_value)
568 {
569 case 'attributes':
570 $_product_attributes = (empty($record->post_parent)) ? get_post_meta($record->ID, '_product_attributes', true) : get_post_meta($record->post_parent, '_product_attributes', true);
571
572 if ( empty(self::$_existing_attributes) )
573 {
574 global $wp_taxonomies;
575
576 foreach ($wp_taxonomies as $key => $obj) { if (in_array($obj->name, array('nav_menu'))) continue;
577
578 if (strpos($obj->name, "pa_") === 0 and strlen($obj->name) > 3 and ! in_array($obj->name, self::$_existing_attributes))
579 self::$_existing_attributes[] = $obj->name;
580 }
581 }
582
583 // combine taxonomies attributes
584 if ( ! empty(self::$_existing_attributes))
585 {
586 foreach (self::$_existing_attributes as $taxonomy_slug) {
587
588 $taxonomy = get_taxonomy($taxonomy_slug);
589
590 $taxonomy_slug_xpath = str_replace("-", "_", $taxonomy_slug);
591 $data['Attribute Name (' . $taxonomy_slug_xpath . ')'] = $taxonomy->labels->singular_name;
592 $data['Attribute In Variations (' . $taxonomy_slug_xpath . ')'] = (!empty($_product_attributes[strtolower(urlencode($taxonomy_slug))]['is_variation'])) ? "yes" : "no";
593 $data['Attribute Is Visible (' . $taxonomy_slug_xpath . ')'] = (!empty($_product_attributes[strtolower(urlencode($taxonomy_slug))]['is_visible'])) ? "yes" : "no";
594 $data['Attribute Is Taxonomy (' . $taxonomy_slug_xpath . ')'] = (!empty($_product_attributes[strtolower(urlencode($taxonomy_slug))]['is_taxonomy'])) ? "yes" : "no";
595
596 $element_name = 'Attribute Value (' . $taxonomy_slug_xpath . ')';
597
598 if ($record->post_parent == 0)
599 {
600 if (isset($_product_attributes[strtolower(urlencode($taxonomy_slug))]['value'])) {
601 $txes_list = get_the_terms($record->ID, $taxonomy_slug);
602 if (!is_wp_error($txes_list) and !empty($txes_list)) {
603 $attr_new = array();
604 foreach ($txes_list as $t) {
605 $attr_new[] = $t->name;
606 }
607 $data[$element_name] = apply_filters('pmxe_woo_attribute', pmxe_filter(implode($implode_delimiter, $attr_new), $fieldSnipped), $record->ID, $taxonomy_slug);
608 }
609 else {
610 $data[$element_name] = pmxe_filter('', $fieldSnipped);
611 }
612 }
613 else
614 {
615 $data[$element_name] = pmxe_filter('', $fieldSnipped);
616 }
617 }
618 else
619 {
620 $variation_attribute = get_post_meta($record->ID, 'attribute_' . strtolower(urlencode($taxonomy_slug)), true);
621 $term = get_term_by('slug', $variation_attribute, $taxonomy_slug);
622 if ($term and !is_wp_error($term)){
623 $variation_attribute = $term->name;
624 }
625 $data[$element_name] = apply_filters('pmxe_woo_attribute', pmxe_filter($variation_attribute, $fieldSnipped), $record->ID, $taxonomy_slug);
626 }
627 }
628 }
629
630 // combine custom attributes
631 if ( ! empty(self::$products_data['attributes']))
632 {
633 foreach (self::$products_data['attributes'] as $attribute)
634 {
635 $attribute_standart_name = str_replace('attribute_', '', $attribute->meta_key);
636 $attribute_name = ucfirst($attribute_standart_name);
637
638 $data['Attribute Name (' . $attribute_name . ')'] = $attribute_name;
639 $data['Attribute Value (' . $attribute_name . ')'] = get_post_meta($record->ID, $attribute->meta_key, true);
640
641 $data['Attribute In Variations (' . $attribute_name . ')'] = (!empty($_product_attributes[$attribute_standart_name]['is_variation'])) ? "yes" : "no";
642 $data['Attribute Is Visible (' . $attribute_name . ')'] = (!empty($_product_attributes[$attribute_standart_name]['is_visible'])) ? "yes" : "no";
643 $data['Attribute Is Taxonomy (' . $attribute_name . ')'] = (!empty($_product_attributes[$attribute_standart_name]['is_taxonomy'])) ? "yes" : "no";
644 }
645 }
646
647 break;
648
649 default:
650
651 $cur_meta_values = get_post_meta($record->ID, $element_value);
652
653 if ( ! empty($cur_meta_values) and is_array($cur_meta_values) )
654 {
655 foreach ($cur_meta_values as $key => $cur_meta_value)
656 {
657 $fieldOptions = $options['cc_options'][$elId];
658 $fieldSettings = empty($options['cc_settings'][$elId]) ? $fieldOptions : $options['cc_settings'][$elId];
659
660 switch ($element_value)
661 {
662 case '_downloadable_files':
663
664 $files = maybe_unserialize($cur_meta_value);
665 $file_paths = array();
666 $file_names = array();
667
668 if ( ! empty($files) ){
669
670 foreach ($files as $key => $file) {
671 $file_paths[] = $file['file'];
672 $file_names[] = $file['name'];
673 }
674
675 }
676 $data[$element_name . ' Paths'] = pmxe_filter(implode($implode_delimiter, $file_paths), $fieldSnipped);
677 $data[$element_name . ' Names'] = pmxe_filter(implode($implode_delimiter, $file_names), $fieldSnipped);
678
679 break;
680 case '_crosssell_ids':
681 case '_upsell_ids':
682
683 $sell_export_type = empty($options['cc_settings'][$elId]) ? 'sku' : $options['cc_settings'][$elId];
684
685 $_ids = maybe_unserialize($cur_meta_value);
686 $_values = array();
687 if (!empty($_ids)){
688
689 switch ($sell_export_type) {
690 case 'sku':
691 foreach ($_ids as $_id) {
692 $_values[] = get_post_meta($_id, '_sku', true);
693 }
694 break;
695 case 'id':
696 $_values = $_ids;
697 break;
698 case 'name':
699 foreach ($_ids as $_id) {
700 $p = get_post($_id);
701 if ($p)
702 {
703 $_values[] = $p->post_name;
704 }
705 }
706 break;
707 default:
708 # code...
709 break;
710 }
711 }
712 $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter(implode($implode_delimiter, $_values), $fieldSnipped), $element_value, $record->ID);
713 break;
714
715 case '_sale_price_dates_from':
716 case '_sale_price_dates_to':
717
718 $post_date = $cur_meta_value ? prepare_date_field_value($fieldSettings, $cur_meta_value) : "";
719
720 $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter($post_date, $fieldSnipped), $element_value, $record->ID);
721
722 // wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), $record->ID) );
723 break;
724
725 case '_tax_class':
726
727 if ( $cur_meta_value == '' ){
728 $tax_status = get_post_meta($record->ID, '_tax_status', true);
729 if ( 'taxable' == $tax_status ){
730 $cur_meta_value = 'standard';
731 }
732 }
733 $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter($cur_meta_value, $fieldSnipped), $element_value, $record->ID);
734 break;
735
736 default:
737
738 if ( empty($data[$element_name]) )
739 {
740 $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $element_value, $record->ID);
741 }
742 else
743 {
744 $data[$element_name . '_' . $key] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $element_value, $record->ID);
745 }
746
747 break;
748 }
749 }
750 }
751
752 if($this->wooCommerceVersion->isWooCommerceNewerThan('3.0')) {
753 if($element_value == '_featured') {
754 if($record->post_type == 'product_variation') {
755 $product = new WC_Product($record->post_parent);
756 }
757 else {
758 $product = new WC_Product($record->ID);
759 }
760
761 if($product) {
762 $value = $product->is_featured() ? 'yes' : 'no';
763 $data[$element_name] = apply_filters('pmxe_woo_field',pmxe_filter($value, $fieldSnipped), $element_value, $record->ID);
764 }
765 }
766 }
767
768 if ( empty($cur_meta_values) && $element_value != '_featured' )
769 {
770 $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter('', $fieldSnipped), $element_value, $record->ID);
771 }
772
773 break;
774 }
775
776 }
777
778
779 return $data;
780 }
781
782 public function export_csv( & $article, & $titles, $record, $options, $element_key )
783 {
784 $data_to_export = $this->prepare_export_data( $record, $options, $element_key );
785
786 foreach ($data_to_export as $key => $data)
787 {
788 // $article[$key] = $data;
789 wp_all_export_write_article( $article, $key, $data );
790 }
791 }
792
793 public function get_element_header( & $headers, $options, $element_key )
794 {
795 $element_value = str_replace("item_data__", "", $options['cc_value'][$element_key]);
796
797 switch ($element_value)
798 {
799 case 'attributes':
800
801 // headers for taxonomy attributes
802 if ( ! empty(self::$_existing_attributes))
803 {
804 foreach (self::$_existing_attributes as $taxonomy_slug) {
805
806 $taxonomy = get_taxonomy($taxonomy_slug);
807 $taxonomy_slug_xpath = str_replace("-", "_", $taxonomy_slug);
808 $headers[] = 'Attribute Name (' . $taxonomy_slug_xpath . ')';
809 $headers[] = 'Attribute Value (' . $taxonomy_slug_xpath . ')';
810 $headers[] = 'Attribute In Variations (' . $taxonomy_slug_xpath . ')';
811 $headers[] = 'Attribute Is Visible (' . $taxonomy_slug_xpath . ')';
812 $headers[] = 'Attribute Is Taxonomy (' . $taxonomy_slug_xpath . ')';
813 }
814 }
815
816 // headers for custom attributes
817 if ( ! empty(self::$products_data['attributes']))
818 {
819 foreach (self::$products_data['attributes'] as $attribute)
820 {
821 $attribute_name = ucfirst(str_replace('attribute_', '', $attribute->meta_key));
822
823 $headers[] = 'Attribute Name (' . $attribute_name . ')';
824 $headers[] = 'Attribute Value (' . $attribute_name . ')';
825 $headers[] = 'Attribute In Variations (' . $attribute_name . ')';
826 $headers[] = 'Attribute Is Visible (' . $attribute_name . ')';
827 $headers[] = 'Attribute Is Taxonomy (' . $attribute_name . ')';
828 }
829 }
830
831 break;
832
833 case '_downloadable_files':
834
835 $headers[] = $options['cc_name'][$element_key] . ' Paths';
836 $headers[] = $options['cc_name'][$element_key] . ' Names';
837
838 break;
839
840 default:
841
842 if ( ! in_array($options['cc_name'][$element_key], $headers))
843 {
844 $headers[] = $options['cc_name'][$element_key];
845 }
846 else
847 {
848 $is_added = false;
849 $i = 0;
850 do
851 {
852 $new_element_name = $options['cc_name'][$element_key] . '_' . md5($i);
853
854 if ( ! in_array($new_element_name, $headers) )
855 {
856 $headers[] = $new_element_name;
857 $is_added = true;
858 }
859
860 $i++;
861 }
862 while ( ! $is_added );
863 }
864
865 break;
866 }
867 }
868
869 public function export_xml( & $xmlWriter, $record, $options, $elId ){
870
871 if ( ! self::$is_active ) return;
872
873 $data_to_export = $this->prepare_export_data( $record, $options, $elId );
874
875 foreach ($data_to_export as $key => $data)
876 {
877 $element_name_ns = '';
878 $element_name = str_replace("-", "_", preg_replace('/[^a-z0-9:_]/i', '', $key));
879 if (strpos($element_name, ":") !== false)
880 {
881 $element_name_parts = explode(":", $element_name);
882 $element_name_ns = (empty($element_name_parts[0])) ? '' : $element_name_parts[0];
883 $element_name = (empty($element_name_parts[1])) ? 'untitled_' . $ID : $element_name_parts[1];
884 }
885
886 $xmlWriter = apply_filters('wp_all_export_add_before_element', $xmlWriter, $element_name, XmlExportEngine::$exportID, $record->ID);
887
888 $xmlWriter->beginElement($element_name_ns, $element_name, null);
889 $xmlWriter->writeData($data, $element_name);
890 $xmlWriter->closeElement();
891
892 $xmlWriter = apply_filters('wp_all_export_add_after_element', $xmlWriter, $element_name, XmlExportEngine::$exportID, $record->ID);
893 }
894
895 }
896
897 public static function prepare_import_template( $exportOptions, &$templateOptions, &$cf_list, &$attr_list, $element_name, $element_key )
898 {
899 if ( ! in_array($element_key, $cf_list)) $cf_list[] = $element_key;
900
901 $is_xml_template = $exportOptions['export_to'] == 'xml';
902
903 $implode_delimiter = XmlExportEngine::$implode;
904
905 switch ($element_key)
906 {
907 case '_visibility':
908 $templateOptions['is_product_visibility'] = 'xpath';
909 $templateOptions['single_product_visibility'] = '{'. $element_name .'[1]}';
910 break;
911 case '_stock_status':
912 $templateOptions['product_stock_status'] = 'xpath';
913 $templateOptions['single_product_stock_status'] = '{'. $element_name .'[1]}';
914 break;
915 case '_downloadable':
916 $templateOptions['is_product_downloadable'] = 'xpath';
917 $templateOptions['single_product_downloadable'] = '{'. $element_name .'[1]}';
918 break;
919 case '_virtual':
920 $templateOptions['is_product_virtual'] = 'xpath';
921 $templateOptions['single_product_virtual'] = '{'. $element_name .'[1]}';
922 break;
923 case '_price':
924 $templateOptions['single_product_regular_price'] = '{'. $element_name .'[1]}';
925 break;
926 case '_regular_price':
927 $templateOptions['single_product_regular_price'] = '{'. $element_name .'[1]}';
928 if ( ! in_array('_price', $cf_list)) $cf_list[] = '_price';
929 break;
930 case '_sale_price':
931 $templateOptions['single_product_sale_price'] = '{'. $element_name .'[1]}';
932 if ( ! in_array('_price', $cf_list)) $cf_list[] = '_price';
933 break;
934 case '_purchase_note':
935 $templateOptions['single_product_purchase_note'] = '{'. $element_name .'[1]}';
936 break;
937 case '_featured':
938 $templateOptions['is_product_featured'] = 'xpath';
939 $templateOptions['single_product_featured'] = '{'. $element_name .'[1]}';
940 break;
941 case '_weight':
942 $templateOptions['single_product_weight'] = '{'. $element_name .'[1]}';
943 break;
944 case '_length':
945 $templateOptions['single_product_length'] = '{'. $element_name .'[1]}';
946 break;
947 case '_width':
948 $templateOptions['single_product_width'] = '{'. $element_name .'[1]}';
949 break;
950 case '_height':
951 $templateOptions['single_product_height'] = '{'. $element_name .'[1]}';
952 break;
953 case '_sku':
954 $templateOptions['single_product_sku'] = '{'. $element_name .'[1]}';
955 //$templateOptions['single_product_parent_id'] = '{parent_id[1]}';
956 break;
957 case '_sale_price_dates_from':
958 $templateOptions['single_sale_price_dates_from'] = '{'. $element_name .'[1]}';
959 break;
960 case '_sale_price_dates_to':
961 $templateOptions['single_sale_price_dates_to'] = '{'. $element_name .'[1]}';
962 break;
963 case '_sold_individually':
964 $templateOptions['product_sold_individually'] = 'xpath';
965 $templateOptions['single_product_sold_individually'] = '{'. $element_name .'[1]}';
966 break;
967 case '_manage_stock':
968 $templateOptions['is_product_manage_stock'] = 'xpath';
969 $templateOptions['single_product_manage_stock'] = '{'. $element_name .'[1]}';
970 break;
971 case '_stock':
972 $templateOptions['single_product_stock_qty'] = '{'. $element_name .'[1]}';
973 break;
974 case '_upsell_ids':
975
976 if ($implode_delimiter == "|")
977 $templateOptions['single_product_up_sells'] = '[str_replace("|", ",",{' . $element_name . '[1]})]';
978 else
979 $templateOptions['single_product_up_sells'] = '{' . $element_name . '[1]}';
980
981 break;
982 case '_crosssell_ids':
983
984 if ($implode_delimiter == "|")
985 $templateOptions['single_product_cross_sells'] = '[str_replace("|", ",",{' . $element_name . '[1]})]';
986 else
987 $templateOptions['single_product_cross_sells'] = '{' . $element_name . '[1]}';
988
989 break;
990 case '_downloadable_files':
991 if ($is_xml_template)
992 {
993 $templateOptions['single_product_files'] = '{'. $element_name .'Paths[1]}';
994 $templateOptions['single_product_files_names'] = '{'. $element_name .'Names[1]}';
995 }
996 else
997 {
998 $templateOptions['single_product_files'] = '{'. $element_name .'paths[1]}';
999 $templateOptions['single_product_files_names'] = '{'. $element_name .'names[1]}';
1000 }
1001 break;
1002 case '_download_limit':
1003 $templateOptions['single_product_download_limit'] = '{'. $element_name .'[1]}';
1004 break;
1005 case '_download_expiry':
1006 $templateOptions['single_product_download_expiry'] = '{'. $element_name .'[1]}';
1007 break;
1008 case '_download_type':
1009 $templateOptions['single_product_download_type'] = '{'. $element_name .'[1]}';
1010 break;
1011 case '_product_url':
1012 $templateOptions['single_product_url'] = '{'. $element_name .'[1]}';
1013 break;
1014 case '_button_text':
1015 $templateOptions['single_product_button_text'] = '{'. $element_name .'[1]}';
1016 break;
1017 case '_tax_status':
1018 $templateOptions['is_multiple_product_tax_status'] = 'no';
1019 $templateOptions['single_product_tax_status'] = '{'. $element_name .'[1]}';
1020 break;
1021 case '_tax_class':
1022 $templateOptions['is_multiple_product_tax_class'] = 'no';
1023 $templateOptions['single_product_tax_class'] = '{'. $element_name .'[1]}';
1024 break;
1025 case '_backorders':
1026 $templateOptions['product_allow_backorders'] = 'xpath';
1027 $templateOptions['single_product_allow_backorders'] = '{'. $element_name .'[1]}';
1028 break;
1029 case '_variation_description':
1030 $templateOptions['single_product_variation_description'] = '{'. $element_name .'[1]}';
1031 break;
1032 case '_product_attributes':
1033 case '_default_attributes':
1034 $templateOptions['custom_name'][] = $element_key;
1035 $templateOptions['custom_value'][] = '{'. $element_name .'[1]}';
1036 $templateOptions['custom_format'][] = 0;
1037 break;
1038 case 'attributes':
1039
1040 global $wp_taxonomies;
1041
1042 foreach ($wp_taxonomies as $key => $obj) { if (in_array($obj->name, array('nav_menu'))) continue;
1043
1044 if (strpos($obj->name, "pa_") === 0 and strlen($obj->name) > 3)
1045 {
1046 $attribute_name = preg_replace('/[^a-z0-9_]/i', '', str_replace("-", "_", $obj->name));
1047
1048 if ($is_xml_template)
1049 {
1050 $attributeOptions = array(
1051 'attribute_name' => 'AttributeName',
1052 'attribute_value' => 'AttributeValue',
1053 'advanced_in_variations_xpath' => 'AttributeInVariations',
1054 'advanced_is_visible_xpath' => 'AttributeIsVisible',
1055 'advanced_is_taxonomy_xpath' => 'AttributeIsTaxonomy'
1056 );
1057
1058 foreach ($attributeOptions as $templateKey => $xpathKey){
1059
1060 if ( ! in_array('{'. $xpathKey . $attribute_name .'[1]}', $templateOptions[$templateKey]) ){
1061 $templateOptions[$templateKey][] = '{'. $xpathKey . $attribute_name .'[1]}';
1062 }
1063 else{
1064 $is_added = false;
1065 $i = 2;
1066 do {
1067 $new_element_name = '{'. $xpathKey . $attribute_name .'['. $i .']}';
1068
1069 if ( ! in_array($new_element_name, $templateOptions[$templateKey]) ) {
1070 $templateOptions[$templateKey][] = $new_element_name;
1071 $is_added = true;
1072 }
1073 $i++;
1074 }
1075 while ( ! $is_added );
1076 }
1077 }
1078 }
1079 else
1080 {
1081 $attributeOptions = array(
1082 'attribute_name' => 'attributename',
1083 'attribute_value' => 'attributevalue',
1084 'advanced_in_variations_xpath' => 'attributeinvariations',
1085 'advanced_is_visible_xpath' => 'attributeisvisible',
1086 'advanced_is_taxonomy_xpath' => 'attributeistaxonomy'
1087 );
1088
1089 foreach ($attributeOptions as $templateKey => $xpathKey){
1090
1091 if ( ! in_array('{'. $xpathKey . $attribute_name .'[1]}', $templateOptions[$templateKey]) ){
1092 $templateOptions[$templateKey][] = '{'. $xpathKey . $attribute_name .'[1]}';
1093 }
1094 else{
1095 $is_added = false;
1096 $i = 2;
1097 do {
1098 $new_element_name = '{'. $xpathKey . $attribute_name .'_'. $i .'[1]}';
1099
1100 if ( ! in_array($new_element_name, $templateOptions[$templateKey]) ) {
1101 $templateOptions[$templateKey][] = $new_element_name;
1102 $is_added = true;
1103 }
1104 $i++;
1105 }
1106 while ( ! $is_added );
1107 }
1108 }
1109 }
1110
1111 $templateOptions['in_variations'][] = "1";
1112 $templateOptions['is_visible'][] = "1";
1113 $templateOptions['is_taxonomy'][] = "1";
1114 $templateOptions['create_taxonomy_in_not_exists'][] = "1";
1115
1116 $templateOptions['is_advanced'][] = "1";
1117 $templateOptions['advanced_is_create_terms'][] = "yes";
1118 $templateOptions['advanced_in_variations'][] = "xpath";
1119 $templateOptions['advanced_is_visible'][] = "xpath";
1120 $templateOptions['advanced_is_taxonomy'][] = "xpath";
1121
1122 $attr_list[] = $obj->name;
1123 }
1124 }
1125
1126 global $wpdb;
1127
1128 $table_prefix = $wpdb->prefix;
1129
1130 $attributes = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT meta_key FROM {$table_prefix}postmeta
1131 WHERE {$table_prefix}postmeta.meta_key LIKE %s AND {$table_prefix}postmeta.meta_key NOT LIKE %s", 'attribute_%', 'attribute_pa_%'));
1132
1133 if ( ! empty($attributes))
1134 {
1135 foreach ($attributes as $attribute)
1136 {
1137 if ($is_xml_template)
1138 {
1139 if (strpos($attribute->meta_key, "%") === false){
1140 $attribute_name = ucfirst(str_replace('attribute_', '', $attribute->meta_key));
1141 }
1142 else{
1143 $attribute_name = str_replace('attribute_', '', preg_replace('/[^a-z0-9_]/i', '', $attribute->meta_key));
1144 }
1145
1146 $templateOptions['attribute_name'][] = '{AttributeName' . $attribute_name .'[1]}';
1147 $templateOptions['attribute_value'][] = '{AttributeValue' . $attribute_name .'[1]}';
1148 $templateOptions['advanced_in_variations_xpath'][] = '{AttributeInVariations' . $attribute_name .'[1]}';
1149 $templateOptions['advanced_is_visible_xpath'][] = '{AttributeIsVisible' . $attribute_name .'[1]}';
1150 $templateOptions['advanced_is_taxonomy_xpath'][] = '{AttributeIsTaxonomy' . $attribute_name .'[1]}';
1151 }
1152 else
1153 {
1154 $attributeOptions = array(
1155 'attribute_name' => 'attributename',
1156 'attribute_value' => 'attributevalue',
1157 'advanced_in_variations_xpath' => 'attributeinvariations',
1158 'advanced_is_visible_xpath' => 'attributeisvisible',
1159 'advanced_is_taxonomy_xpath' => 'attributeistaxonomy'
1160 );
1161
1162 $attribute_name = preg_replace('/[^a-z0-9_]/i', '', str_replace('attribute_', '', XmlExportEngine::sanitizeFieldName($attribute->meta_key)));
1163
1164 foreach ($attributeOptions as $templateKey => $xpathKey){
1165
1166 if ( ! in_array('{'. $xpathKey . $attribute_name .'[1]}', $templateOptions[$templateKey]) ){
1167 $templateOptions[$templateKey][] = '{'. $xpathKey . $attribute_name .'[1]}';
1168 }
1169 else{
1170 $is_added = false;
1171 $i = 2;
1172 do {
1173 $new_element_name = '{'. $xpathKey . $attribute_name .'_'. $i .'[1]}';
1174
1175 if ( ! in_array($new_element_name, $templateOptions[$templateKey]) ) {
1176 $templateOptions[$templateKey][] = $new_element_name;
1177 $is_added = true;
1178 }
1179 $i++;
1180 }
1181 while ( ! $is_added );
1182 }
1183 }
1184 }
1185
1186 $templateOptions['in_variations'][] = "1";
1187 $templateOptions['is_visible'][] = "1";
1188 $templateOptions['is_taxonomy'][] = "1";
1189 $templateOptions['create_taxonomy_in_not_exists'][] = "1";
1190
1191 $templateOptions['is_advanced'][] = "1";
1192 $templateOptions['advanced_is_create_terms'][] = "yes";
1193 $templateOptions['advanced_in_variations'][] = "xpath";
1194 $templateOptions['advanced_is_visible'][] = "xpath";
1195 $templateOptions['advanced_is_taxonomy'][] = "xpath";
1196
1197 $attr_list[] = str_replace('attribute_', '', $attribute->meta_key);
1198 }
1199 }
1200
1201 break;
1202 default:
1203 # code...
1204 break;
1205 }
1206 }
1207
1208 /**
1209 * __get function.
1210 *
1211 * @access public
1212 * @param mixed $key
1213 * @return mixed
1214 */
1215 public function __get( $key ) {
1216 return $this->get( $key );
1217 }
1218
1219 /**
1220 * Get a session variable
1221 *
1222 * @param string $key
1223 * @param mixed $default used if the session variable isn't set
1224 * @return mixed value of session variable
1225 */
1226 public function get( $key, $default = null ) {
1227 return isset( $this->{$key} ) ? $this->{$key} : $default;
1228 }
1229 }
1230 }
1231