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