示例#1
0
 public function __construct($post, &$errors)
 {
     $this->post = $post;
     $this->errors = $errors;
     $this->available_sections = array('default' => array('title' => __("Standard", "wp_all_export_plugin"), 'content' => 'default_fields'), 'cats' => array('title' => __("Taxonomies", "wp_all_export_plugin"), 'content' => 'existing_taxonomies'), 'cf' => array('title' => __("Custom Fields", "wp_all_export_plugin"), 'content' => 'existing_meta_keys'), 'other' => array('title' => __("Other", "wp_all_export_plugin"), 'content' => 'other_fields'));
     $this->filter_sections = array('general' => array('title' => __("General", "wp_all_export_plugin"), 'fields' => array('ID' => 'ID', 'post_title' => 'Title', 'post_content' => 'Content', 'post_parent' => 'Parent ID', 'post_date' => 'Date (Y-m-d H:i:s)', 'post_status' => 'Status')), 'author' => array('title' => __("Author", "wp_all_export_plugin"), 'fields' => array('post_author' => 'Author ID')));
     if ('specific' == $this->post['export_type']) {
         self::$post_types = !is_array($this->post['cpt']) ? array($this->post['cpt']) : $this->post['cpt'];
         if (in_array('product', self::$post_types) and !in_array('product_variation', self::$post_types)) {
             self::$post_types[] = 'product_variation';
         }
         if (in_array('users', self::$post_types)) {
             self::$is_user_export = true;
         }
     } else {
         if ('wp_user_query' == $this->post['wp_query_selector']) {
             self::$is_user_export = true;
         }
     }
     self::$exportOptions = $post;
     $filter_args = array('filter_rules_hierarhy' => $this->post['filter_rules_hierarhy'], 'product_matching_mode' => $this->post['product_matching_mode']);
     $this->filters = new XmlExportFiltering($filter_args);
     $this->init();
     $this->acf_export = XmlExportACF::getInstance();
     $this->woo_export = XmlExportWooCommerce::getInstance();
     $this->user_export = XmlExportUser::getInstance();
     $this->woo_order_export = XmlExportWooCommerceOrder::getInstance();
 }
示例#2
0
 public function __construct()
 {
     if (!class_exists('WooCommerce') or XmlExportEngine::$exportOptions['export_type'] == 'specific' and !in_array('product', XmlExportEngine::$post_types) or XmlExportEngine::$exportOptions['export_type'] == 'advanced' and strpos(XmlExportEngine::$exportOptions['wp_query'], 'product') === false) {
         self::$is_active = false;
         return;
     }
     $this->_woo_data = array('_visibility', '_stock_status', '_downloadable', '_virtual', '_regular_price', '_sale_price', '_purchase_note', '_featured', '_weight', '_length', '_width', '_height', '_sku', '_sale_price_dates_from', '_sale_price_dates_to', '_price', '_sold_individually', '_manage_stock', '_stock', '_upsell_ids', '_crosssell_ids', '_downloadable_files', '_download_limit', '_download_expiry', '_download_type', '_product_url', '_button_text', '_backorders', '_tax_status', '_tax_class', '_product_image_gallery', '_default_attributes', 'total_sales', '_product_attributes');
 }
 public function init_additional_data()
 {
     if (!self::$is_active) {
         return;
     }
     if (empty(self::$products_data) or 'PMXE_Admin_Manage' == PMXE_Plugin::getInstance()->getAdminCurrentScreen()->base) {
         self::$products_data = array();
         global $wpdb;
         $table_prefix = $wpdb->prefix;
         self::$products_data['attributes'] = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT meta_key FROM {$table_prefix}postmeta \n\t\t\t\t\t\tWHERE {$table_prefix}postmeta.meta_key LIKE %s AND {$table_prefix}postmeta.meta_key NOT LIKE %s", 'attribute_%', 'attribute_pa_%'));
         if (!empty(PMXE_Plugin::$session)) {
             PMXE_Plugin::$session->set('products_data', self::$products_data);
             PMXE_Plugin::$session->save_data();
         }
     }
 }
示例#4
0
 public static function prepare_import_template($exportOptions)
 {
     $options = $exportOptions;
     $is_xml_template = $options['export_to'] == 'xml';
     $required_add_ons = array();
     $cf_list = array();
     $attr_list = array();
     $taxs_list = array();
     $acf_list = array();
     $implode_delimiter = $options['delimiter'] == ',' ? '|' : ',';
     if (!empty($options['is_user_export'])) {
         self::$templateOptions['pmui']['import_users'] = 1;
     }
     foreach ($options['ids'] as $ID => $value) {
         if (empty($options['cc_type'][$ID])) {
             continue;
         }
         if ($is_xml_template) {
             $element_name = !empty($options['cc_name'][$ID]) ? str_replace(':', '_', preg_replace('/[^a-z0-9_:-]/i', '', $options['cc_name'][$ID])) : 'untitled_' . $ID;
         } else {
             $element_name = strtolower(!empty($options['cc_name'][$ID]) ? preg_replace('/[^a-z0-9_]/i', '', $options['cc_name'][$ID]) : 'untitled_' . $ID);
         }
         $element_type = $options['cc_type'][$ID];
         switch ($element_type) {
             case 'woo':
                 if (!empty($options['cc_value'][$ID])) {
                     if (empty($required_add_ons['PMWI_Plugin'])) {
                         $required_add_ons['PMWI_Plugin'] = array('name' => 'WooCommerce Add-On Pro', 'paid' => true, 'url' => 'http://www.wpallimport.com/woocommerce-product-import/');
                     }
                     XmlExportWooCommerce::prepare_import_template($options, self::$templateOptions, $cf_list, $attr_list, $element_name, $options['cc_label'][$ID]);
                 }
                 break;
             case 'acf':
                 if (empty($required_add_ons['PMAI_Plugin'])) {
                     $required_add_ons['PMAI_Plugin'] = array('name' => 'ACF Add-On Pro', 'paid' => true, 'url' => 'http://www.wpallimport.com/advanced-custom-fields/?utm_source=wordpress.org&utm_medium=wpai-import-template&utm_campaign=free+wp+all+export+plugin');
                 }
                 $field_options = unserialize($options['cc_options'][$ID]);
                 // add ACF group ID to the template options
                 if (!in_array($field_options['group_id'], self::$templateOptions['acf'])) {
                     self::$templateOptions['acf'][$field_options['group_id']] = 1;
                 }
                 self::$templateOptions['fields'][$field_options['key']] = XmlExportACF::prepare_import_template($options, self::$templateOptions, $acf_list, $element_name, $field_options);
                 break;
             default:
                 XmlExportCpt::prepare_import_template($options, self::$templateOptions, $cf_list, $attr_list, $taxs_list, $element_name, $ID);
                 XmlExportMediaGallery::prepare_import_template($options, self::$templateOptions, $element_name, $ID);
                 XmlExportWooCommerceOrder::prepare_import_template($options, self::$templateOptions, $element_name, $ID);
                 break;
         }
     }
     if (!empty($cf_list)) {
         self::$templateOptions['is_update_custom_fields'] = 1;
         self::$templateOptions['custom_fields_list'] = $cf_list;
     }
     if (!empty($attr_list)) {
         self::$templateOptions['is_update_attributes'] = 1;
         self::$templateOptions['update_attributes_logic'] = 'only';
         self::$templateOptions['attributes_list'] = $attr_list;
         self::$templateOptions['attributes_only_list'] = implode(',', $attr_list);
     } else {
         self::$templateOptions['is_update_attributes'] = 0;
     }
     if (!empty($taxs_list)) {
         self::$templateOptions['is_update_categories'] = 1;
         self::$templateOptions['taxonomies_list'] = $taxs_list;
     }
     if (!empty($acf_list)) {
         self::$templateOptions['is_update_acf'] = 1;
         self::$templateOptions['acf_list'] = $acf_list;
     }
     self::$templateOptions['required_add_ons'] = $required_add_ons;
 }
function pmxe_export_xml($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0)
{
    require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php';
    $xmlWriter = new PMXE_XMLWriter();
    $xmlWriter->openMemory();
    $xmlWriter->setIndent(true);
    $xmlWriter->setIndentString("\t");
    $xmlWriter->startDocument('1.0', $exportOptions['encoding']);
    $xmlWriter->startElement($exportOptions['main_xml_tag']);
    if ($is_cron) {
        if (!$exported_by_cron) {
            $additional_data = apply_filters('wp_all_export_additional_data', array(), $exportOptions);
            if (!empty($additional_data)) {
                foreach ($additional_data as $key => $value) {
                    $xmlWriter->startElement(preg_replace('/[^a-z0-9_-]/i', '', $key));
                    $xmlWriter->writeCData($value);
                    $xmlWriter->endElement();
                }
            }
        }
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            $additional_data = apply_filters('wp_all_export_additional_data', array(), $exportOptions);
            if (!empty($additional_data)) {
                foreach ($additional_data as $key => $value) {
                    $xmlWriter->startElement(preg_replace('/[^a-z0-9_-]/i', '', $key));
                    $xmlWriter->writeCData($value);
                    $xmlWriter->endElement();
                }
            }
        }
    }
    global $wpdb;
    while ($exportQuery->have_posts()) {
        $exportQuery->the_post();
        $record = get_post(get_the_ID());
        $xmlWriter->startElement($exportOptions['record_xml_tag']);
        if ($exportOptions['ids']) {
            if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) {
                $postRecord = new PMXI_Post_Record();
                $postRecord->clear();
                $postRecord->getBy(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id']));
                if ($postRecord->isEmpty()) {
                    $postRecord->set(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $record->ID, 'product_key' => $record->ID))->save();
                }
                unset($postRecord);
            }
            foreach ($exportOptions['ids'] as $ID => $value) {
                if (is_numeric($ID)) {
                    if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) {
                        continue;
                    }
                    $element_name_ns = '';
                    $element_name = !empty($exportOptions['cc_name'][$ID]) ? preg_replace('/[^a-z0-9_:-]/i', '', $exportOptions['cc_name'][$ID]) : 'untitled_' . $ID;
                    $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false;
                    if (strpos($element_name, ":") !== false) {
                        $element_name_parts = explode(":", $element_name);
                        $element_name_ns = empty($element_name_parts[0]) ? '' : $element_name_parts[0];
                        $element_name = empty($element_name_parts[1]) ? 'untitled_' . $ID : preg_replace('/[^a-z0-9_-]/i', '', $element_name_parts[1]);
                    }
                    switch ($exportOptions['cc_type'][$ID]) {
                        case 'id':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_id', pmxe_filter(get_the_ID(), $fieldSnipped), get_the_ID()));
                            break;
                        case 'permalink':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_guid', pmxe_filter(get_permalink(), $fieldSnipped), get_the_ID()));
                            break;
                        case 'post_type':
                            $pType = get_post_type();
                            if ($pType == 'product_variation') {
                                $pType = 'product';
                            }
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), get_the_ID()));
                            break;
                        case 'title':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_title', pmxe_filter($record->post_title, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'content':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_content', pmxe_filter($record->post_content, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'media':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $attachment_ids = array();
                            $_featured_image = get_post_meta(get_the_ID(), '_thumbnail_id', true);
                            if (!empty($_featured_image)) {
                                $attachment_ids[] = $_featured_image;
                            }
                            $_gallery = get_post_meta(get_the_ID(), '_product_image_gallery', true);
                            if (!empty($_gallery)) {
                                $gallery = explode(',', $_gallery);
                                if (!empty($gallery) and is_array($gallery)) {
                                    foreach ($gallery as $aid) {
                                        if (!in_array($aid, $attachment_ids)) {
                                            $attachment_ids[] = $aid;
                                        }
                                    }
                                }
                            }
                            $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID));
                            if (!empty($attachment_imgs)) {
                                foreach ($attachment_imgs as $attach) {
                                    if (wp_attachment_is_image($attach->ID) and !in_array($attach->ID, $attachment_ids)) {
                                        $attachment_ids[] = $attach->ID;
                                    }
                                }
                            }
                            if (!empty($attachment_ids)) {
                                foreach ($attachment_ids as $attach_id) {
                                    $attach = get_post($attach_id);
                                    if ($attach and !is_wp_error($attach) and wp_attachment_is_image($attach->ID)) {
                                        $xmlWriter->startElement('image');
                                        $val = wp_get_attachment_url($attach->ID);
                                        if (!empty($exportOptions['cc_options'][$ID])) {
                                            switch ($exportOptions['cc_options'][$ID]) {
                                                case 'filenames':
                                                    $val = basename(wp_get_attachment_url($attach->ID));
                                                    break;
                                                case 'filepaths':
                                                    $val = get_attached_file($attach->ID);
                                                    break;
                                                default:
                                                    # code...
                                                    break;
                                            }
                                        }
                                        $xmlWriter->writeElement('file', apply_filters('pmxe_attachment_url', $val, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('title', apply_filters('pmxe_attachment_title', $attach->post_title, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('caption', apply_filters('pmxe_attachment_caption', $attach->post_excerpt, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('description', apply_filters('pmxe_attachment_content', $attach->post_content, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('alt', apply_filters('pmxe_attachment_alt', get_post_meta($record->ID, '_wp_attachment_image_alt', true), get_the_ID(), $attach->ID));
                                        $xmlWriter->endElement();
                                    }
                                }
                            }
                            $xmlWriter->endElement();
                            break;
                        case 'date':
                            if (!empty($exportOptions['cc_options'][$ID])) {
                                switch ($exportOptions['cc_options'][$ID]) {
                                    case 'unix':
                                        $post_date = get_post_time('U', true);
                                        break;
                                    default:
                                        $post_date = date($exportOptions['cc_options'][$ID], get_post_time('U', true));
                                        break;
                                }
                            } else {
                                $post_date = date("Ymd", get_post_time('U', true));
                            }
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), get_the_ID()));
                            break;
                        case 'attachments':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID));
                            if (!empty($attachment_imgs)) {
                                foreach ($attachment_imgs as $attach) {
                                    if (!wp_attachment_is_image($attach->ID)) {
                                        $xmlWriter->startElement('attach');
                                        $xmlWriter->writeElement('url', apply_filters('pmxe_attachment_url', pmxe_filter(wp_get_attachment_url($attach->ID), $fieldSnipped), get_the_ID(), $attach->ID));
                                        $xmlWriter->endElement();
                                    }
                                }
                            }
                            $xmlWriter->endElement();
                            // end attachments
                            break;
                        case 'parent':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_parent', pmxe_filter($record->post_parent, $fieldSnipped), get_the_ID()));
                            break;
                        case 'comment_status':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_comment_status', pmxe_filter($record->comment_status, $fieldSnipped), get_the_ID()));
                            break;
                        case 'ping_status':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_ping_status', pmxe_filter($record->ping_status, $fieldSnipped), get_the_ID()));
                            break;
                        case 'template':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_template', pmxe_filter(get_post_meta($record->ID, '_wp_page_template', true), $fieldSnipped), get_the_ID()));
                            break;
                        case 'order':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_menu_order', pmxe_filter($record->menu_order, $fieldSnipped), get_the_ID()));
                            break;
                        case 'status':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_status', pmxe_filter($record->post_status, $fieldSnipped), get_the_ID()));
                            break;
                        case 'format':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_format', pmxe_filter(get_post_format($record->ID), $fieldSnipped), get_the_ID()));
                            break;
                        case 'author':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_author', pmxe_filter($record->post_author, $fieldSnipped), get_the_ID()));
                            break;
                        case 'slug':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_slug', pmxe_filter($record->post_name, $fieldSnipped), get_the_ID()));
                            break;
                        case 'excerpt':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_excerpt', pmxe_filter($record->post_excerpt, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'cf':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_value'][$ID]);
                                if (!empty($cur_meta_values) and is_array($cur_meta_values)) {
                                    foreach ($cur_meta_values as $key => $cur_meta_value) {
                                        $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                        $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()));
                                        $xmlWriter->endElement();
                                    }
                                }
                                if (empty($cur_meta_values)) {
                                    $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                    $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()));
                                    $xmlWriter->endElement();
                                }
                            }
                            break;
                        case 'acf':
                            if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) {
                                global $acf;
                                $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID);
                                $field_options = unserialize($exportOptions['cc_options'][$ID]);
                                pmxe_export_acf_field_xml($field_value, $exportOptions, $ID, $record->ID, $xmlWriter, $element_name, $element_name_ns, $fieldSnipped, $field_options['group_id']);
                            }
                            break;
                        case 'woo':
                            XmlExportWooCommerce::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID);
                            break;
                        case 'woo_order':
                            XmlExportWooCommerceOrder::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID);
                            break;
                        case 'attr':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                if ($record->post_parent == 0) {
                                    $is_variable_product = false;
                                    $product_terms = wp_get_post_terms($record->ID, 'product_type');
                                    if (!empty($product_terms)) {
                                        if (!is_wp_error($product_terms)) {
                                            foreach ($product_terms as $term) {
                                                if ('variable' == $term->slug) {
                                                    $is_variable_product = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]);
                                    if (!is_wp_error($txes_list)) {
                                        $attr_new = array();
                                        if (!empty($txes_list)) {
                                            foreach ($txes_list as $t) {
                                                $attr_new[] = $t->name;
                                            }
                                            $xmlWriter->beginElement($element_name_ns, $is_variable_product ? $element_name : 'attribute_' . $element_name, null);
                                            $xmlWriter->writeCData(apply_filters('pmxe_woo_attribute', pmxe_filter(implode('|', $attr_new), $fieldSnipped), get_the_ID()));
                                            $xmlWriter->endElement();
                                        }
                                    }
                                } else {
                                    $attribute_pa = get_post_meta($record->ID, 'attribute_' . $exportOptions['cc_value'][$ID], true);
                                    if (!empty($attribute_pa)) {
                                        $xmlWriter->beginElement($element_name_ns, 'attribute_' . $element_name, null);
                                        $xmlWriter->writeCData(apply_filters('woo_field', $attribute_pa));
                                        $xmlWriter->endElement();
                                    }
                                }
                            }
                            break;
                        case 'cats':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                if ($exportOptions['cc_label'][$ID] == 'product_type' and get_post_type() == 'product_variation') {
                                    $xmlWriter->writeElement('parent_id', $record->post_parent);
                                    $xmlWriter->writeElement($element_name, 'variable');
                                } else {
                                    $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]);
                                    if (!is_wp_error($txes_list)) {
                                        $txes_ids = array();
                                        $hierarchy_groups = array();
                                        if (!empty($txes_list)) {
                                            foreach ($txes_list as $t) {
                                                $txes_ids[] = $t->term_id;
                                            }
                                            foreach ($txes_list as $t) {
                                                if (wp_all_export_check_children_assign($t->term_id, $exportOptions['cc_value'][$ID], $txes_ids)) {
                                                    $ancestors = get_ancestors($t->term_id, $exportOptions['cc_value'][$ID]);
                                                    if (count($ancestors) > 0) {
                                                        $hierarchy_group = array();
                                                        for ($i = count($ancestors) - 1; $i >= 0; $i--) {
                                                            $term = get_term_by('id', $ancestors[$i], $exportOptions['cc_value'][$ID]);
                                                            if ($term) {
                                                                $hierarchy_group[] = $term->name;
                                                            }
                                                        }
                                                        $hierarchy_group[] = $t->name;
                                                        $hierarchy_groups[] = implode(">", $hierarchy_group);
                                                    } else {
                                                        $hierarchy_groups[] = $t->name;
                                                    }
                                                }
                                            }
                                            if (!empty($hierarchy_groups)) {
                                                $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                                $xmlWriter->writeCData(apply_filters('pmxe_post_taxonomy', pmxe_filter(implode('|', $hierarchy_groups), $fieldSnipped), get_the_ID()));
                                                $xmlWriter->endElement();
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        case 'sql':
                            if (!empty($exportOptions['cc_sql'][$ID])) {
                                $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID()));
                                if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) {
                                    // if shortcode defined
                                    if (strpos($exportOptions['cc_code'][$ID], '[') === 0) {
                                        $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID]));
                                    } else {
                                        $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';');
                                    }
                                }
                                $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                $xmlWriter->writeCData(apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID()));
                                $xmlWriter->endElement();
                            }
                            break;
                        default:
                            # code...
                            break;
                    }
                }
            }
        }
        $xmlWriter->endElement();
        // end post
        if ($preview) {
            break;
        }
        do_action('pmxe_exported_post', $record->ID);
    }
    $xmlWriter->endElement();
    // end data
    if ($preview) {
        return $xmlWriter->flush(true);
    }
    //wp_all_export_remove_colons($xmlWriter->flush(true));
    if ($is_cron) {
        $xml = $xmlWriter->flush(true);
        if (!$exported_by_cron) {
            // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
            if ($exportOptions['include_bom']) {
                file_put_contents($file_path, chr(0xef) . chr(0xbb) . chr(0xbf) . substr($xml, 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            } else {
                file_put_contents($file_path, substr($xml, 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            }
        } else {
            file_put_contents($file_path, substr(substr($xml, 41 + strlen($exportOptions['main_xml_tag'])), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1), FILE_APPEND);
        }
        return $file_path;
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            // generate export file name
            $export_file = wp_all_export_generate_export_file(XmlExportEngine::$exportID);
            // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
            if ($exportOptions['include_bom']) {
                file_put_contents($export_file, chr(0xef) . chr(0xbb) . chr(0xbf) . substr($xmlWriter->flush(true), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            } else {
                file_put_contents($export_file, substr($xmlWriter->flush(true), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            }
            PMXE_Plugin::$session->set('file', $export_file);
            PMXE_Plugin::$session->save_data();
        } else {
            file_put_contents(PMXE_Plugin::$session->file, substr(substr($xmlWriter->flush(true), 41 + strlen($exportOptions['main_xml_tag'])), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1), FILE_APPEND);
        }
        return true;
    }
}
/**
*	Export CSV helper
*/
function pmxe_export_csv($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0)
{
    ob_start();
    // Prepare headers
    $headers = array();
    $stream = fopen("php://output", 'w');
    $max_attach_count = 0;
    $max_images_count = 0;
    $cf = array();
    $woo = array();
    $woo_order = array();
    $acfs = array();
    $taxes = array();
    $attributes = array();
    $articles = array();
    $implode_delimiter = $exportOptions['delimiter'] == ',' ? '|' : ',';
    while ($exportQuery->have_posts()) {
        $attach_count = 0;
        $images_count = 0;
        $exportQuery->the_post();
        $record = get_post(get_the_ID());
        $article = array();
        $article['post_type'] = $record->post_type;
        $article['ID'] = apply_filters('pmxe_post_id', get_the_ID());
        $article['permalink'] = get_permalink();
        global $wpdb;
        $table_prefix = $wpdb->prefix;
        if ($exportOptions['ids']) {
            if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) {
                $postRecord = new PMXI_Post_Record();
                $postRecord->clear();
                $postRecord->getBy(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id']));
                if ($postRecord->isEmpty()) {
                    $postRecord->set(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $record->ID, 'product_key' => $record->ID))->save();
                }
                unset($postRecord);
            }
            foreach ($exportOptions['ids'] as $ID => $value) {
                if (is_numeric($ID)) {
                    if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) {
                        continue;
                    }
                    $element_name = !empty($exportOptions['cc_name'][$ID]) ? $exportOptions['cc_name'][$ID] : 'untitled_' . $ID;
                    $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false;
                    switch ($exportOptions['cc_type'][$ID]) {
                        case 'id':
                            $article[$element_name] = apply_filters('pmxe_post_id', pmxe_filter(get_the_ID(), $fieldSnipped), get_the_ID());
                            break;
                        case 'permalink':
                            $article[$element_name] = apply_filters('pmxe_post_guid', pmxe_filter(get_permalink(), $fieldSnipped), get_the_ID());
                            break;
                        case 'post_type':
                            $pType = get_post_type();
                            if ($pType == 'product_variation') {
                                $pType = 'product';
                            }
                            $article[$element_name] = apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), get_the_ID());
                            break;
                        case 'title':
                            $article[$element_name] = apply_filters('pmxe_post_title', pmxe_filter($record->post_title, $fieldSnipped), get_the_ID());
                            break;
                        case 'content':
                            $val = apply_filters('pmxe_post_content', pmxe_filter($record->post_content, $fieldSnipped), get_the_ID());
                            $article[$element_name] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val;
                            break;
                        case 'media':
                            $attachment_ids = array();
                            $_featured_image = get_post_meta(get_the_ID(), '_thumbnail_id', true);
                            if (!empty($_featured_image)) {
                                $attachment_ids[] = $_featured_image;
                            }
                            $_gallery = get_post_meta(get_the_ID(), '_product_image_gallery', true);
                            if (!empty($_gallery)) {
                                $gallery = explode(',', $_gallery);
                                if (!empty($gallery) and is_array($gallery)) {
                                    foreach ($gallery as $aid) {
                                        if (!in_array($aid, $attachment_ids)) {
                                            $attachment_ids[] = $aid;
                                        }
                                    }
                                }
                            }
                            $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID));
                            if (!empty($attachment_imgs)) {
                                foreach ($attachment_imgs as $attach) {
                                    if (wp_attachment_is_image($attach->ID) and !in_array($attach->ID, $attachment_ids)) {
                                        $attachment_ids[] = $attach->ID;
                                    }
                                }
                            }
                            if (!empty($attachment_ids)) {
                                $img_urls = array();
                                $img_titles = array();
                                $img_captions = array();
                                $img_alts = array();
                                $img_descriptions = array();
                                foreach ($attachment_ids as $key => $attach_id) {
                                    $attach = get_post($attach_id);
                                    if ($attach and !is_wp_error($attach) and wp_attachment_is_image($attach->ID)) {
                                        $val = wp_get_attachment_url($attach->ID);
                                        if (!empty($exportOptions['cc_options'][$ID])) {
                                            switch ($exportOptions['cc_options'][$ID]) {
                                                case 'filenames':
                                                    $val = basename(wp_get_attachment_url($attach->ID));
                                                    break;
                                                case 'filepaths':
                                                    $val = get_attached_file($attach->ID);
                                                    break;
                                                default:
                                                    # code...
                                                    break;
                                            }
                                        }
                                        $img_urls[] = apply_filters('pmxe_attachment_url', $val, get_the_ID(), $attach->ID);
                                        $img_titles[] = apply_filters('pmxe_attachment_title', $attach->post_title, get_the_ID(), $attach->ID);
                                        $img_captions[] = apply_filters('pmxe_attachment_caption', $attach->post_excerpt, get_the_ID(), $attach->ID);
                                        $img_descriptions[] = apply_filters('pmxe_attachment_content', $attach->post_content, get_the_ID(), $attach->ID);
                                        $img_alts[] = apply_filters('pmxe_attachment_alt', get_post_meta($record->ID, '_wp_attachment_image_alt', true), get_the_ID(), $attach->ID);
                                        $images_count++;
                                    }
                                }
                                if (!empty($img_urls)) {
                                    $article[$element_name . '_images'] = implode($implode_delimiter, $img_urls);
                                }
                                if (!empty($img_titles)) {
                                    $article[$element_name . '_titles'] = implode($implode_delimiter, $img_titles);
                                }
                                if (!empty($img_captions)) {
                                    $article[$element_name . '_captions'] = implode($implode_delimiter, $img_captions);
                                }
                                if (!empty($img_alts)) {
                                    $article[$element_name . '_alts'] = implode($implode_delimiter, $img_alts);
                                }
                                if (!empty($img_descriptions)) {
                                    $article[$element_name . '_descriptions'] = implode($implode_delimiter, $img_descriptions);
                                }
                                if ($max_images_count > $images_count) {
                                    $max_images_count = $images_count;
                                }
                            }
                            break;
                        case 'date':
                            if (!empty($exportOptions['cc_options'][$ID])) {
                                switch ($exportOptions['cc_options'][$ID]) {
                                    case 'unix':
                                        $post_date = get_post_time('U', true);
                                        break;
                                    default:
                                        $post_date = date($exportOptions['cc_options'][$ID], get_post_time('U', true));
                                        break;
                                }
                            } else {
                                $post_date = date("Ymd", get_post_time('U', true));
                            }
                            $article[$element_name] = apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), get_the_ID());
                            break;
                        case 'attachments':
                            $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID));
                            if (!empty($attachment_imgs)) {
                                $attachment_urls = array();
                                foreach ($attachment_imgs as $key => $attach) {
                                    if (!wp_attachment_is_image($attach->ID)) {
                                        $attachment_urls[] = apply_filters('pmxe_attachment_url', pmxe_filter(wp_get_attachment_url($attach->ID), $fieldSnipped), get_the_ID(), $attach->ID);
                                        $attach_count++;
                                    }
                                }
                                if (!empty($attachment_urls)) {
                                    $article[$element_name . '_attachments'] = implode($implode_delimiter, $attachment_urls);
                                }
                                if ($attach_count > $max_attach_count) {
                                    $max_attach_count = $attach_count;
                                }
                            }
                            break;
                        case 'parent':
                            $article[$element_name] = apply_filters('pmxe_post_parent', pmxe_filter($record->post_parent, $fieldSnipped), get_the_ID());
                            break;
                        case 'comment_status':
                            $article[$element_name] = apply_filters('pmxe_comment_status', pmxe_filter($record->comment_status, $fieldSnipped), get_the_ID());
                            break;
                        case 'ping_status':
                            $article[$element_name] = apply_filters('pmxe_ping_status', pmxe_filter($record->ping_status, $fieldSnipped), get_the_ID());
                            break;
                        case 'template':
                            $article[$element_name] = apply_filters('pmxe_post_template', pmxe_filter(get_post_meta($record->ID, '_wp_page_template', true), $fieldSnipped), get_the_ID());
                            break;
                        case 'order':
                            $article[$element_name] = apply_filters('pmxe_menu_order', pmxe_filter($record->menu_order, $fieldSnipped), get_the_ID());
                            break;
                        case 'status':
                            $article[$element_name] = apply_filters('pmxe_post_status', pmxe_filter($record->post_status, $fieldSnipped), get_the_ID());
                            break;
                        case 'format':
                            $article[$element_name] = apply_filters('pmxe_post_format', pmxe_filter(get_post_format($record->ID), $fieldSnipped), get_the_ID());
                            break;
                        case 'author':
                            $article[$element_name] = apply_filters('pmxe_post_author', pmxe_filter($record->post_author, $fieldSnipped), get_the_ID());
                            break;
                        case 'slug':
                            $article[$element_name] = apply_filters('pmxe_post_slug', pmxe_filter($record->post_name, $fieldSnipped), get_the_ID());
                            break;
                        case 'excerpt':
                            $val = apply_filters('pmxe_post_excerpt', pmxe_filter($record->post_excerpt, $fieldSnipped), get_the_ID());
                            $article[$element_name] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val;
                            break;
                        case 'cf':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_value'][$ID]);
                                if (!empty($cur_meta_values) and is_array($cur_meta_values)) {
                                    foreach ($cur_meta_values as $key => $cur_meta_value) {
                                        if (empty($article[$element_name])) {
                                            $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID());
                                            if (!in_array($element_name, $cf)) {
                                                $cf[] = $element_name;
                                            }
                                        } else {
                                            $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter($article[$element_name] . $implode_delimiter . maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID());
                                        }
                                    }
                                }
                                if (empty($cur_meta_values)) {
                                    if (empty($article[$element_name])) {
                                        $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID());
                                        if (!in_array($element_name, $cf)) {
                                            $cf[] = $element_name;
                                        }
                                    }
                                    // else{
                                    // 	$article[$element_name . '_' . $key] = apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID());
                                    // 	if (!in_array($element_name . '_' . $key, $cf)) $cf[] = $element_name . '_' . $key;
                                    // }
                                }
                            }
                            break;
                        case 'acf':
                            if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) {
                                global $acf;
                                $field_options = unserialize($exportOptions['cc_options'][$ID]);
                                switch ($field_options['type']) {
                                    case 'textarea':
                                    case 'oembed':
                                    case 'wysiwyg':
                                    case 'wp_wysiwyg':
                                    case 'date_time_picker':
                                    case 'date_picker':
                                        $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID, false);
                                        break;
                                    default:
                                        $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID);
                                        break;
                                }
                                pmxe_export_acf_field_csv($field_value, $exportOptions, $ID, $record->ID, $article, $acfs, $element_name, $fieldSnipped, $field_options['group_id'], $preview);
                            }
                            break;
                        case 'woo':
                            XmlExportWooCommerce::getInstance()->export_csv($article, $woo, $record, $exportOptions, $ID);
                            break;
                        case 'woo_order':
                            XmlExportWooCommerceOrder::getInstance()->export_csv($article, $woo_order, $record, $exportOptions, $ID);
                            break;
                        case 'attr':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                if ($record->post_parent == 0) {
                                    $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]);
                                    if (!is_wp_error($txes_list) and !empty($txes_list)) {
                                        $attr_new = array();
                                        foreach ($txes_list as $t) {
                                            $attr_new[] = $t->name;
                                        }
                                        $article[$element_name] = apply_filters('pmxe_woo_attribute', pmxe_filter(implode($implode_delimiter, $attr_new), $fieldSnipped), get_the_ID());
                                    }
                                    if (!in_array($element_name, $attributes)) {
                                        $attributes[] = $element_name;
                                    }
                                } else {
                                    $attribute_pa = get_post_meta($record->ID, 'attribute_' . $exportOptions['cc_value'][$ID], true);
                                    $article['attribute_' . $element_name] = $attribute_pa;
                                    if (!in_array('attribute_' . $element_name, $attributes)) {
                                        $attributes[] = 'attribute_' . $element_name;
                                    }
                                }
                            }
                            break;
                        case 'cats':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                $article[$element_name] = '';
                                $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]);
                                if (!is_wp_error($txes_list) and !empty($txes_list)) {
                                    $txes_ids = array();
                                    $hierarchy_groups = array();
                                    foreach ($txes_list as $t) {
                                        $txes_ids[] = $t->term_id;
                                    }
                                    foreach ($txes_list as $t) {
                                        if (wp_all_export_check_children_assign($t->term_id, $exportOptions['cc_value'][$ID], $txes_ids)) {
                                            $ancestors = get_ancestors($t->term_id, $exportOptions['cc_value'][$ID]);
                                            if (count($ancestors) > 0) {
                                                $hierarchy_group = array();
                                                for ($i = count($ancestors) - 1; $i >= 0; $i--) {
                                                    $term = get_term_by('id', $ancestors[$i], $exportOptions['cc_value'][$ID]);
                                                    if ($term) {
                                                        $hierarchy_group[] = $term->name;
                                                    }
                                                }
                                                $hierarchy_group[] = $t->name;
                                                $hierarchy_groups[] = implode('>', $hierarchy_group);
                                            } else {
                                                $hierarchy_groups[] = $t->name;
                                            }
                                        }
                                    }
                                    if (!empty($hierarchy_groups)) {
                                        $article[$element_name] = apply_filters('pmxe_post_taxonomy', pmxe_filter(implode($implode_delimiter, $hierarchy_groups), $fieldSnipped), get_the_ID());
                                    }
                                }
                                if (!in_array($element_name, $taxes)) {
                                    $taxes[] = $element_name;
                                }
                                if ($exportOptions['cc_label'][$ID] == 'product_type' and get_post_type() == 'product_variation') {
                                    $article[$element_name] = 'variable';
                                }
                                if ($exportOptions['cc_label'][$ID] == 'product_type') {
                                    $article['parent_id'] = $record->post_parent;
                                }
                                //if ( ! in_array('parent_sku', $taxes)) $taxes[] = 'parent_sku';
                            }
                            break;
                        case 'sql':
                            if (!empty($exportOptions['cc_sql'][$ID])) {
                                $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID()));
                                if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) {
                                    // if shortcode defined
                                    if (strpos($exportOptions['cc_code'][$ID], '[') === 0) {
                                        $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID]));
                                    } else {
                                        $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';');
                                    }
                                }
                                $article[$element_name] = apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID());
                            }
                            break;
                        default:
                            # code...
                            break;
                    }
                }
            }
        }
        $articles[] = $article;
        $articles = apply_filters('wp_all_export_csv_rows', $articles, $exportOptions);
        if ($preview) {
            break;
        }
        do_action('pmxe_exported_post', $record->ID);
    }
    if ($exportOptions['ids']) {
        foreach ($exportOptions['ids'] as $ID => $value) {
            if (is_numeric($ID)) {
                if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) {
                    continue;
                }
                $element_name = !empty($exportOptions['cc_name'][$ID]) ? $exportOptions['cc_name'][$ID] : 'untitled_' . $ID;
                switch ($exportOptions['cc_type'][$ID]) {
                    case 'media':
                        $headers[] = $element_name . '_images';
                        $headers[] = $element_name . '_titles';
                        $headers[] = $element_name . '_captions';
                        $headers[] = $element_name . '_descriptions';
                        $headers[] = $element_name . '_alts';
                        break;
                    case 'attachments':
                        $headers[] = $element_name . '_attachments';
                        break;
                    case 'cats':
                        if (!empty($taxes)) {
                            $tx = array_shift($taxes);
                            $headers[] = $tx;
                            if ($exportOptions['cc_label'][$ID] == 'product_type') {
                                $headers[] = 'parent_id';
                            }
                        }
                        break;
                    case 'attr':
                        if (!empty($attributes)) {
                            $headers[] = array_shift($attributes);
                            if (in_array('attribute_' . $element_name, $attributes)) {
                                $headers[] = 'attribute_' . $element_name;
                                foreach ($attributes as $akey => $avalue) {
                                    if ($avalue == 'attribute_' . $element_name) {
                                        unset($attributes[$akey]);
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    case 'cf':
                        if (!empty($cf)) {
                            $headers[] = array_shift($cf);
                        }
                        break;
                    case 'woo':
                        XmlExportWooCommerce::getInstance()->get_element_header($headers, $exportOptions, $ID);
                        break;
                    case 'woo_order':
                        XmlExportWooCommerceOrder::getInstance()->get_element_header($headers, $exportOptions, $ID);
                        break;
                    case 'acf':
                        if (!empty($acfs)) {
                            $single_acf_field = array_shift($acfs);
                            if (is_array($single_acf_field)) {
                                foreach ($single_acf_field as $acf_header) {
                                    $headers[] = $acf_header;
                                }
                            } else {
                                $headers[] = $single_acf_field;
                            }
                        }
                        break;
                    default:
                        $headers[] = $element_name;
                        break;
                }
            }
        }
    }
    if ($is_cron) {
        if (!$exported_by_cron) {
            fputcsv($stream, $headers, $exportOptions['delimiter']);
        }
    } else {
        if ($preview or empty(PMXE_Plugin::$session->file)) {
            fputcsv($stream, $headers, $exportOptions['delimiter']);
        }
    }
    foreach ($articles as $article) {
        $line = array();
        foreach ($headers as $header) {
            $line[$header] = isset($article[$header]) ? $article[$header] : '';
        }
        fputcsv($stream, $line, $exportOptions['delimiter']);
    }
    if ($preview) {
        return ob_get_clean();
    }
    if ($is_cron) {
        if (!$exported_by_cron) {
            // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
            if ($exportOptions['include_bom']) {
                file_put_contents($file_path, chr(0xef) . chr(0xbb) . chr(0xbf) . ob_get_clean());
            } else {
                file_put_contents($file_path, ob_get_clean());
            }
        } else {
            file_put_contents($file_path, ob_get_clean(), FILE_APPEND);
        }
        return $file_path;
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            // generate export file name
            $export_file = wp_all_export_generate_export_file(XmlExportEngine::$exportID);
            // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
            if ($exportOptions['include_bom']) {
                file_put_contents($export_file, chr(0xef) . chr(0xbb) . chr(0xbf) . ob_get_clean());
            } else {
                file_put_contents($export_file, ob_get_clean());
            }
            PMXE_Plugin::$session->set('file', $export_file);
            PMXE_Plugin::$session->save_data();
        } else {
            file_put_contents(PMXE_Plugin::$session->file, ob_get_clean(), FILE_APPEND);
        }
        return true;
    }
}