function wp_all_export_generate_export_file($export_id)
 {
     $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
     $wp_uploads = wp_upload_dir();
     $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY, $export_id) : $wp_uploads['path'];
     $export = new PMXE_Export_Record();
     $export->getById($export_id);
     if (!$export->isEmpty() and $export->options['creata_a_new_export_file']) {
         $export_file_name = sanitize_file_name($export->options['friendly_name']) . ' - ' . ($export->iteration + 1) . '.' . $export->options['export_to'];
     } else {
         $export_file_name = sanitize_file_name($export->options['friendly_name']) . '.' . $export->options['export_to'];
     }
     return $target . DIRECTORY_SEPARATOR . $export_file_name;
 }
Esempio n. 2
0
 public function generate_bundle($debug = false)
 {
     // do not generate export bundle if not supported
     if (!self::is_bundle_supported($this->options)) {
         return;
     }
     $uploads = wp_upload_dir();
     //generate temporary folder
     $export_dir = wp_all_export_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY, $this->id) . DIRECTORY_SEPARATOR;
     $bundle_dir = $export_dir . 'bundle' . DIRECTORY_SEPARATOR;
     // clear tmp dir
     wp_all_export_rrmdir($bundle_dir);
     @mkdir($bundle_dir);
     $friendly_name = sanitize_file_name($this->friendly_name);
     $template = "WP All Import Template - " . $friendly_name . ".txt";
     $templates = array();
     $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
     if (!$is_secure_import) {
         $filepath = get_attached_file($this->attch_id);
     } else {
         $filepath = wp_all_export_get_absolute_path($this->options['filepath']);
     }
     @copy($filepath, $bundle_dir . basename($filepath));
     if (!empty($this->options['tpl_data'])) {
         $template_data = array($this->options['tpl_data']);
         $template_data[0]['source_file_name'] = basename($filepath);
         $template_options = maybe_unserialize($template_data[0]['options']);
         $templates[$template_options['custom_type']] = $template_data;
         $readme = __("The other two files in this zip are the export file containing all of your data and the import template for WP All Import. \n\nTo import this data, create a new import with WP All Import and upload this zip file.", "wp_all_export_plugin");
         file_put_contents($bundle_dir . 'readme.txt', $readme);
     }
     file_put_contents($bundle_dir . $template, json_encode($templates));
     if ($this->options['creata_a_new_export_file'] && !empty($this->options['cpt']) and class_exists('WooCommerce') and in_array('shop_order', $this->options['cpt']) and empty($this->parent_id)) {
         $bundle_path = $export_dir . $friendly_name . '-' . ($this->iteration + 1) . '.zip';
     } else {
         $bundle_path = $export_dir . $friendly_name . '.zip';
     }
     if (@file_exists($bundle_path)) {
         @unlink($bundle_path);
     }
     PMXE_Zip::zipDir($bundle_dir, $bundle_path);
     // clear tmp dir
     wp_all_export_rrmdir($bundle_dir);
     $exportOptions = $this->options;
     $exportOptions['bundlepath'] = wp_all_export_get_relative_path($bundle_path);
     $this->set(array('options' => $exportOptions))->save();
     return $bundle_path;
 }
 function wp_all_export_generate_export_file($export_id)
 {
     $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
     $wp_uploads = wp_upload_dir();
     $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY, $export_id) : $wp_uploads['path'];
     $export = new PMXE_Export_Record();
     $export->getById($export_id);
     if (!$export->isEmpty()) {
         if ($export->options['creata_a_new_export_file'] and !XmlExportWooCommerceOrder::$is_active || $export->options['export_type'] != 'specific') {
             $export_file_name = sanitize_file_name($export->friendly_name) . ' - ' . ($export->iteration + 1) . '.' . $export->options['export_to'];
         } else {
             $export_file_name = sanitize_file_name($export->friendly_name) . '.' . $export->options['export_to'];
         }
     }
     return apply_filters('wp_all_export_export_file_name', $target . DIRECTORY_SEPARATOR . $export_file_name, $export_id);
 }
Esempio n. 4
0
/**
*	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' => get_post_meta($record->ID, '_sku', true)))->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;
                                        }
                                    }
                                }
                            }
                            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 (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 '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':
                            //$element_name = 'woo' . $element_name;
                            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) {
                                        switch ($exportOptions['cc_label'][$ID]) {
                                            case '_downloadable_files':
                                                $files = maybe_unserialize($cur_meta_value);
                                                $file_paths = array();
                                                $file_names = array();
                                                if (!empty($files)) {
                                                    foreach ($files as $key => $file) {
                                                        $file_paths[] = $file['file'];
                                                        $file_names[] = $file['name'];
                                                    }
                                                    $article[$element_name . '_paths'] = implode($implode_delimiter, $file_paths);
                                                    if (!in_array($element_name, $woo)) {
                                                        $woo[] = $element_name . '_paths';
                                                    }
                                                    $article[$element_name . '_names'] = implode($implode_delimiter, $file_names);
                                                    if (!in_array($element_name, $woo)) {
                                                        $woo[] = $element_name . '_names';
                                                    }
                                                }
                                                break;
                                            case '_crosssell_ids':
                                            case '_upsell_ids':
                                                $_upsell_ids = maybe_unserialize($cur_meta_value);
                                                $_skus = array();
                                                if (!empty($_upsell_ids)) {
                                                    foreach ($_upsell_ids as $_upsell_id) {
                                                        $_skus[] = get_post_meta($_upsell_id, '_sku', true);
                                                    }
                                                    $article[$element_name] = implode($implode_delimiter, $_skus);
                                                    $woo[] = $element_name;
                                                }
                                                break;
                                            default:
                                                if (empty($article[$element_name])) {
                                                    $article[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID());
                                                    if (!in_array($element_name, $woo)) {
                                                        $woo[] = $element_name;
                                                    }
                                                } else {
                                                    $article[$element_name . '_' . $key] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID());
                                                    if (!in_array($element_name . '_' . $key, $woo)) {
                                                        $woo[] = $element_name . '_' . $key;
                                                    }
                                                }
                                                break;
                                        }
                                    }
                                }
                                if (empty($cur_meta_values)) {
                                    if (empty($article[$element_name])) {
                                        $article[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID());
                                        if (!in_array($element_name, $woo)) {
                                            $woo[] = $element_name;
                                        }
                                        switch ($exportOptions['cc_label'][$ID]) {
                                            case '_downloadable_files':
                                                if (!in_array($element_name, $woo)) {
                                                    $woo[] = $element_name . '_paths';
                                                }
                                                if (!in_array($element_name, $woo)) {
                                                    $woo[] = $element_name . '_names';
                                                }
                                                break;
                                        }
                                    }
                                }
                            }
                            break;
                        case 'woo_order':
                            XmlExportWooCommerceOrder::getInstance()->export_csv($article, $woo_order, $record, $exportOptions, $ID);
                            break;
                        case 'attr':
                            //$element_name = 'woo_' . $element_name;
                            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) and !empty($txes_list)) {
                                        $attr_new = array();
                                        foreach ($txes_list as $t) {
                                            $attr_new[] = $t->slug;
                                        }
                                        //$element_name = ($is_variable_product) ? $element_name : 'attribute_' . $element_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);
                                    //if ( ! empty($attribute_pa)){
                                    $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])) {
                                $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->slug;
                                                    }
                                                }
                                                $hierarchy_group[] = $t->slug;
                                                $hierarchy_groups[] = implode('>', $hierarchy_group);
                                            } else {
                                                $hierarchy_groups[] = $t->slug;
                                            }
                                        }
                                    }
                                    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';
                                    $article['parent_sku'] = get_post_meta($record->post_parent, '_sku', true);
                                }
                            }
                            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;
        }
    }
    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 ($tx == 'product_type') {
                                $headers[] = 'parent_sku';
                            }
                        }
                        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':
                        if (!empty($woo)) {
                            $headers[] = array_shift($woo);
                        }
                        break;
                    case 'woo_order':
                        XmlExportWooCommerceOrder::getInstance()->get_element_header($headers, $exportOptions, $ID);
                        break;
                    case 'acf':
                        if (!empty($acfs)) {
                            $headers[] = array_shift($acfs);
                        }
                        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) {
        file_put_contents($file_path, ob_get_clean(), FILE_APPEND);
        return $file_path;
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
            $wp_uploads = wp_upload_dir();
            $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path'];
            $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to'];
            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;
    }
}
Esempio n. 5
0
/**
*	Export CSV helper
*/
function pmxe_export_users_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();
    $acfs = array();
    $taxes = array();
    $attributes = array();
    $articles = array();
    $implode_delimiter = $exportOptions['delimiter'] == ',' ? '|' : ',';
    foreach ($exportQuery->results as $user) {
        $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_user_id', $user->ID);
        //$article['permalink'] = get_permalink();
        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' => $user->ID, 'import_id' => $exportOptions['import_id']));
                if ($postRecord->isEmpty()) {
                    $postRecord->set(array('post_id' => $user->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $user->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_user_id', pmxe_filter($user->ID, $fieldSnipped), $user->ID);
                            break;
                        case 'user_login':
                            $article[$element_name] = apply_filters('pmxe_user_login', pmxe_filter($user->user_login, $fieldSnipped), $user->ID);
                            break;
                        case 'user_pass':
                            $article[$element_name] = apply_filters('pmxe_user_pass', pmxe_filter($user->user_pass, $fieldSnipped), $user->ID);
                            break;
                        case 'user_email':
                            $article[$element_name] = apply_filters('pmxe_user_email', pmxe_filter($user->user_email, $fieldSnipped), $user->ID);
                            break;
                        case 'user_nicename':
                            $article[$element_name] = apply_filters('pmxe_user_nicename', pmxe_filter($user->user_nicename, $fieldSnipped), $user->ID);
                            break;
                        case 'user_url':
                            $article[$element_name] = apply_filters('pmxe_user_url', pmxe_filter($user->user_url, $fieldSnipped), $user->ID);
                            break;
                        case 'user_activation_key':
                            $article[$element_name] = apply_filters('pmxe_user_activation_key', pmxe_filter($user->user_activation_key, $fieldSnipped), $user->ID);
                            break;
                        case 'user_status':
                            $article[$element_name] = apply_filters('pmxe_user_status', pmxe_filter($user->user_status, $fieldSnipped), $user->ID);
                            break;
                        case 'display_name':
                            $article[$element_name] = apply_filters('pmxe_user_display_name', pmxe_filter($user->display_name, $fieldSnipped), $user->ID);
                            break;
                        case 'description':
                            $val = apply_filters('pmxe_user_description', pmxe_filter($user->description, $fieldSnipped), $user->ID);
                            $article[$element_name] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val;
                            break;
                        case 'user_registered':
                            if (!empty($exportOptions['cc_options'][$ID])) {
                                switch ($exportOptions['cc_options'][$ID]) {
                                    case 'unix':
                                        $post_date = strtotime($user->user_registered);
                                        break;
                                    default:
                                        $post_date = date($exportOptions['cc_options'][$ID], strtotime($user->user_registered));
                                        break;
                                }
                            } else {
                                $post_date = $user->user_registered;
                            }
                            $article[$element_name] = apply_filters('pmxe_user_registered', pmxe_filter($post_date, $fieldSnipped), $user->ID);
                            break;
                        case 'nickname':
                            $article[$element_name] = apply_filters('pmxe_user_nickname', pmxe_filter($user->nickname, $fieldSnipped), $user->ID);
                            break;
                        case 'first_name':
                            $article[$element_name] = apply_filters('pmxe_user_first_name', pmxe_filter($user->first_name, $fieldSnipped), $user->ID);
                            break;
                        case 'last_name':
                            $article[$element_name] = apply_filters('pmxe_user_last_name', pmxe_filter($user->last_name, $fieldSnipped), $user->ID);
                            break;
                        case 'wp_capabilities':
                            $article[$element_name] = apply_filters('pmxe_user_wp_capabilities', pmxe_filter(implode($implode_delimiter, $user->roles), $fieldSnipped), $user->ID);
                            break;
                        case 'cf':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                $cur_meta_values = get_user_meta($user->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], $user->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], $user->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], $user->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], $user->ID, false);
                                        break;
                                    default:
                                        $field_value = get_field($exportOptions['cc_label'][$ID], $user->ID);
                                        break;
                                }
                                pmxe_export_acf_field_csv($field_value, $exportOptions, $ID, $user->ID, $article, $acfs, $element_name, $fieldSnipped, $field_options['group_id'], $preview);
                            }
                            break;
                        case 'sql':
                            if (!empty($exportOptions['cc_sql'][$ID])) {
                                global $wpdb;
                                $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;
        if ($preview) {
            break;
        }
    }
    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 'cf':
                        if (!empty($cf)) {
                            $headers[] = array_shift($cf);
                        }
                        break;
                    case 'acf':
                        if (!empty($acfs)) {
                            $headers[] = array_shift($acfs);
                        }
                        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) {
        file_put_contents($file_path, ob_get_clean(), FILE_APPEND);
        return $file_path;
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
            $wp_uploads = wp_upload_dir();
            $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path'];
            $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to'];
            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;
    }
}
Esempio n. 6
0
 /**
  * Import all files matched by path
  * @param callback[optional] $logger Method where progress messages are submmitted
  * @return PMXI_Import_Record
  * @chainable
  */
 public function execute($logger = NULL, $cron = false)
 {
     $this->set('registered_on', date('Y-m-d H:i:s'))->save();
     // update registered_on to indicated that job has been exectured even if no files are going to be imported by the rest of the method
     $wp_uploads = wp_upload_dir();
     $this->set(array('processing' => 1))->update();
     // lock cron requests
     wp_reset_postdata();
     XmlExportEngine::$exportOptions = $this->options;
     XmlExportEngine::$is_user_export = $this->options['is_user_export'];
     if ('advanced' == $this->options['export_type']) {
         if (XmlExportEngine::$is_user_export) {
             $exportQuery = eval('return new WP_User_Query(array(' . $this->options['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $this->exported . ', \'number\' => ' . $this->options['records_per_iteration'] . '));');
         } else {
             $exportQuery = eval('return new WP_Query(array(' . $this->options['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $this->exported . ', \'posts_per_page\' => ' . $this->options['records_per_iteration'] . '));');
         }
     } else {
         XmlExportEngine::$post_types = $this->options['cpt'];
         if (!in_array('users', $this->options['cpt'])) {
             add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
             add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
             $exportQuery = new WP_Query(array('post_type' => $this->options['cpt'], 'post_status' => 'any', 'orderby' => 'ID', 'order' => 'ASC', 'offset' => $this->exported, 'posts_per_page' => $this->options['records_per_iteration']));
             remove_filter('posts_join', 'wp_all_export_posts_join');
             remove_filter('posts_where', 'wp_all_export_posts_where');
         } else {
             add_action('pre_user_query', 'wp_all_export_pre_user_query', 10, 1);
             $exportQuery = new WP_User_Query(array('orderby' => 'ID', 'order' => 'ASC', 'number' => $this->options['records_per_iteration'], 'offset' => $this->exported));
             remove_action('pre_user_query', 'wp_all_export_pre_user_query');
         }
     }
     XmlExportEngine::$exportQuery = $exportQuery;
     $file_path = false;
     $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
     if ($this->exported == 0) {
         $import = new PMXI_Import_Record();
         $import->getById($this->options['import_id']);
         if ($import->isEmpty()) {
             $import->set(array('parent_import_id' => 99999, 'xpath' => '/', 'type' => 'upload', 'options' => array('empty'), 'root_element' => 'root', 'path' => 'path', 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'iteration' => 1))->save();
             $exportOptions = $this->options;
             $exportOptions['import_id'] = $import->id;
             $this->set(array('options' => $exportOptions))->save();
         } else {
             if ($import->parent_import_id != 99999) {
                 $newImport = new PMXI_Import_Record();
                 $newImport->set(array('parent_import_id' => 99999, 'xpath' => '/', 'type' => 'upload', 'options' => array('empty'), 'root_element' => 'root', 'path' => 'path', 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'iteration' => 1))->save();
                 $exportOptions = $this->options;
                 $exportOptions['import_id'] = $newImport->id;
                 $this->set(array('options' => $exportOptions))->save();
             }
         }
         if (!empty($this->attch_id)) {
             wp_delete_attachment($this->attch_id, true);
         }
         $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path'];
         $file_path = $target . DIRECTORY_SEPARATOR . time() . '.' . $this->options['export_to'];
         if (!$is_secure_import) {
             $wp_filetype = wp_check_filetype(basename($file_path), null);
             $attachment_data = array('guid' => $wp_uploads['baseurl'] . '/' . _wp_relative_upload_path($file_path), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file_path)), 'post_content' => '', 'post_status' => 'inherit');
             $attach_id = wp_insert_attachment($attachment_data, $file_path);
             $this->set(array('attch_id' => $attach_id))->save();
         } else {
             wp_all_export_remove_source(wp_all_export_get_absolute_path($this->options['filepath']));
             $exportOptions = $this->options;
             $exportOptions['filepath'] = $file_path;
             $this->set(array('options' => $exportOptions))->save();
         }
     } else {
         if (!$is_secure_import) {
             $file_path = str_replace($wp_uploads['baseurl'], $wp_uploads['basedir'], wp_get_attachment_url($this->attch_id));
         } else {
             $file_path = wp_all_export_get_absolute_path($this->options['filepath']);
         }
     }
     $foundPosts = !XmlExportEngine::$is_user_export ? $exportQuery->found_posts : $exportQuery->get_total();
     $postCount = !XmlExportEngine::$is_user_export ? $exportQuery->post_count : count($exportQuery->get_results());
     // if posts still exists then export them
     if ($postCount) {
         switch ($this->options['export_to']) {
             case 'xml':
                 if (!XmlExportEngine::$is_user_export) {
                     $exported_to_file = pmxe_export_xml($exportQuery, $this->options, false, $cron, $file_path);
                 } else {
                     $exported_to_file = pmxe_export_users_xml($exportQuery, $this->options, false, $cron, $file_path);
                 }
                 break;
             case 'csv':
                 if (!XmlExportEngine::$is_user_export) {
                     $exported_to_file = pmxe_export_csv($exportQuery, $this->options, false, $cron, $file_path, $this->exported);
                 } else {
                     $exported_to_file = pmxe_export_users_csv($exportQuery, $this->options, false, $cron, $file_path, $this->exported);
                 }
                 break;
             default:
                 # code...
                 break;
         }
         wp_reset_postdata();
         $this->set(array('exported' => $this->exported + $postCount, 'last_activity' => date('Y-m-d H:i:s'), 'processing' => 0))->save();
     } else {
         wp_reset_postdata();
         if (file_exists($file_path)) {
             if ($this->options['export_to'] == 'xml') {
                 file_put_contents($file_path, '</data>', FILE_APPEND);
             }
             if (wp_all_export_is_compatible() and ($this->options['is_generate_templates'] or $this->options['is_generate_import'])) {
                 $custom_type = empty($exportOptions['cpt']) ? 'post' : $exportOptions['cpt'][0];
                 $templateOptions = array('type' => (!empty($exportOptions['cpt']) and $exportOptions['cpt'][0] == 'page') ? 'page' : 'post', 'wizard_type' => 'new', 'deligate' => 'wpallexport', 'custom_type' => XmlExportEngine::$is_user_export ? 'import_users' : $custom_type, 'status' => 'xpath', 'is_multiple_page_parent' => 'no', 'unique_key' => '', 'acf' => array(), 'fields' => array(), 'is_multiple_field_value' => array(), 'multiple_value' => array(), 'fields_delimiter' => array(), 'update_all_data' => 'no', 'is_update_status' => 0, 'is_update_title' => 0, 'is_update_author' => 0, 'is_update_slug' => 0, 'is_update_content' => 0, 'is_update_excerpt' => 0, 'is_update_dates' => 0, 'is_update_menu_order' => 0, 'is_update_parent' => 0, 'is_update_attachments' => 0, 'is_update_acf' => 0, 'update_acf_logic' => 'only', 'acf_list' => '', 'is_update_product_type' => 1, 'is_update_attributes' => 0, 'update_attributes_logic' => 'only', 'attributes_list' => '', 'is_update_images' => 0, 'is_update_custom_fields' => 0, 'update_custom_fields_logic' => 'only', 'custom_fields_list' => '', 'is_update_categories' => 0, 'update_categories_logic' => 'only', 'taxonomies_list' => '', 'export_id' => $this->id);
                 if (in_array('product', $this->options['cpt'])) {
                     $templateOptions['_virtual'] = 1;
                     $templateOptions['_downloadable'] = 1;
                 }
                 if (XmlExportEngine::$is_user_export) {
                     $templateOptions['is_update_first_name'] = 0;
                     $templateOptions['is_update_last_name'] = 0;
                     $templateOptions['is_update_role'] = 0;
                     $templateOptions['is_update_nickname'] = 0;
                     $templateOptions['is_update_description'] = 0;
                     $templateOptions['is_update_login'] = 0;
                     $templateOptions['is_update_password'] = 0;
                     $templateOptions['is_update_nicename'] = 0;
                     $templateOptions['is_update_email'] = 0;
                     $templateOptions['is_update_registered'] = 0;
                     $templateOptions['is_update_display_name'] = 0;
                     $templateOptions['is_update_url'] = 0;
                 }
                 if ('xml' == $this->options['export_to']) {
                     wp_all_export_prepare_template_xml($this->options, $templateOptions);
                 } else {
                     wp_all_export_prepare_template_csv($this->options, $templateOptions);
                 }
                 $options = $templateOptions + PMXI_Plugin::get_default_import_options();
                 if ($this->options['is_generate_templates']) {
                     $template = new PMXI_Template_Record();
                     $tpl_data = array('name' => $this->options['template_name'], 'is_keep_linebreaks' => 0, 'is_leave_html' => 0, 'fix_characters' => 0, 'options' => $options);
                     if (!empty($this->options['template_name'])) {
                         // save template in database
                         $template->getByName($this->options['template_name'])->set($tpl_data)->save();
                     }
                 }
                 if ($this->options['is_generate_import']) {
                     $import = new PMXI_Import_Record();
                     $import->getById($this->options['import_id']);
                     if (!$import->isEmpty() and $import->parent_import_id == 99999) {
                         $xmlPath = $file_path;
                         $root_element = '';
                         if ('csv' == $this->options['export_to']) {
                             $options['delimiter'] = $this->options['delimiter'];
                             include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                             $path_parts = pathinfo($xmlPath);
                             $path_parts_arr = explode(DIRECTORY_SEPARATOR, $path_parts['dirname']);
                             $target = $is_secure_import ? $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY . DIRECTORY_SEPARATOR . array_pop($path_parts_arr) : $wp_uploads['path'];
                             $csv = new PMXI_CsvParser(array('filename' => $xmlPath, 'targetDir' => $target));
                             $xmlPath = $csv->xml_path;
                             $root_element = 'node';
                         } else {
                             $root_element = 'post';
                         }
                         $import->set(array('xpath' => '/' . $root_element, 'type' => 'upload', 'options' => $options, 'root_element' => $root_element, 'path' => $xmlPath, 'name' => basename($xmlPath), 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'count' => $exportQuery->found_posts))->save();
                         $history_file = new PMXI_File_Record();
                         $history_file->set(array('name' => $import->name, 'import_id' => $import->id, 'path' => $xmlPath, 'registered_on' => date('Y-m-d H:i:s')))->save();
                         $exportOptions = $this->options;
                         $exportOptions['import_id'] = $import->id;
                         $this->set(array('options' => $exportOptions))->save();
                     }
                 }
             }
             // update export file for remove access
             # 1 meg at a time, you can adjust this.
             $to_dirname = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::CRON_DIRECTORY . DIRECTORY_SEPARATOR . md5(PMXE_Plugin::getInstance()->getOption('cron_job_key') . $this->id);
             if (!@is_dir($to_dirname)) {
                 wp_mkdir_p($to_dirname);
             }
             if (!@file_exists($to_dirname . DIRECTORY_SEPARATOR . 'index.php')) {
                 @touch($to_dirname . DIRECTORY_SEPARATOR . 'index.php');
             }
             $to = $to_dirname . DIRECTORY_SEPARATOR . (!empty($this->friendly_name) ? sanitize_file_name($this->friendly_name) : 'feed') . '.' . $this->options['export_to'];
             $buffer_size = 1048576;
             $fin = @fopen($file_path, "rb");
             $fout = @fopen($to, "w");
             while (!@feof($fin)) {
                 @fwrite($fout, @fread($fin, $buffer_size));
             }
             @fclose($fin);
             @fclose($fout);
             if ($this->options['is_scheduled'] and "" != $this->options['scheduled_email']) {
                 add_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
                 $headers = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
                 $message = '<p>Export ' . $this->options['friendly_name'] . ' has been completed. You can find exported file in attachments.</p>';
                 wp_mail($this->options['scheduled_email'], __("WP All Export", "pmxe_plugin"), $message, $headers, array($file_path));
                 remove_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
             }
         }
         $this->set(array('processing' => 0, 'triggered' => 0, 'canceled' => 0, 'registered_on' => date('Y-m-d H:i:s')))->update();
     }
     return $this;
 }
Esempio n. 7
0
/**
*	Export XML helper
*/
function pmxe_export_xml($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false)
{
    $xmlWriter = new XMLWriter();
    $xmlWriter->openMemory();
    $xmlWriter->setIndent(true);
    $xmlWriter->setIndentString("\t");
    $xmlWriter->startDocument('1.0', $exportOptions['encoding']);
    $xmlWriter->startElement('data');
    global $wpdb;
    while ($exportQuery->have_posts()) {
        $exportQuery->the_post();
        $record = get_post(get_the_ID());
        // global $wpdb;
        // $table_prefix = $wpdb->prefix;
        // $order_items  = null;
        // if ( "shop_order" == get_post_type() )
        // {
        // 	$order_items  = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_items WHERE order_id = {$record->ID}");
        // }
        $xmlWriter->startElement('post');
        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' => get_post_meta($record->ID, '_sku', true)))->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]) ? str_replace(" ", "_", $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':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_id', pmxe_filter(get_the_ID(), $fieldSnipped), get_the_ID()));
                            break;
                        case 'permalink':
                            $xmlWriter->writeElement($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';
                            }
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), get_the_ID()));
                            break;
                        case 'title':
                            $xmlWriter->startElement($element_name);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_title', pmxe_filter($record->post_title, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'content':
                            $xmlWriter->startElement($element_name);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_content', pmxe_filter($record->post_content, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'media':
                            $xmlWriter->startElement($element_name);
                            $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;
                                        }
                                    }
                                }
                            }
                            if (!empty($attachment_ids)) {
                                foreach ($attachment_ids as $attach_id) {
                                    $attach = get_post($attach_id);
                                    if (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->writeElement($element_name, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), get_the_ID()));
                            break;
                        case 'attachments':
                            $xmlWriter->startElement($element_name);
                            $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->writeElement($element_name, apply_filters('pmxe_post_parent', pmxe_filter($record->post_parent, $fieldSnipped), get_the_ID()));
                            break;
                        case 'template':
                            $xmlWriter->writeElement($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':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_menu_order', pmxe_filter($record->menu_order, $fieldSnipped), get_the_ID()));
                            break;
                        case 'status':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_status', pmxe_filter($record->post_status, $fieldSnipped), get_the_ID()));
                            break;
                        case 'format':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_format', pmxe_filter(get_post_format($record->ID), $fieldSnipped), get_the_ID()));
                            break;
                        case 'author':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_author', pmxe_filter($record->post_author, $fieldSnipped), get_the_ID()));
                            break;
                        case 'slug':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_slug', pmxe_filter($record->post_name, $fieldSnipped), get_the_ID()));
                            break;
                        case 'excerpt':
                            $xmlWriter->startElement($element_name);
                            $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->startElement($element_name);
                                        $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->startElement($element_name);
                                    $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, $fieldSnipped, $field_options['group_id']);
                            }
                            break;
                        case 'woo':
                            //$element_name = 'woo' . $element_name;
                            if (!empty($exportOptions['cc_label'][$ID])) {
                                $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_label'][$ID]);
                                if (!empty($cur_meta_values) and is_array($cur_meta_values)) {
                                    foreach ($cur_meta_values as $key => $cur_meta_value) {
                                        switch ($exportOptions['cc_label'][$ID]) {
                                            case '_downloadable_files':
                                                $files = maybe_unserialize($cur_meta_value);
                                                $file_paths = array();
                                                $file_names = array();
                                                if (!empty($files)) {
                                                    foreach ($files as $key => $file) {
                                                        $file_paths[] = $file['file'];
                                                        $file_names[] = $file['name'];
                                                    }
                                                    $xmlWriter->startElement($element_name . '_paths');
                                                    $xmlWriter->writeCData(implode(',', $file_paths));
                                                    $xmlWriter->endElement();
                                                    $xmlWriter->startElement($element_name . '_names');
                                                    $xmlWriter->writeCData(implode(',', $file_names));
                                                    $xmlWriter->endElement();
                                                }
                                                break;
                                            case '_crosssell_ids':
                                            case '_upsell_ids':
                                                $_upsell_ids = maybe_unserialize($cur_meta_value);
                                                $_upsell_skus = array();
                                                if (!empty($_upsell_ids)) {
                                                    foreach ($_upsell_ids as $_upsell_id) {
                                                        $_upsell_skus[] = get_post_meta($_upsell_id, '_sku', true);
                                                    }
                                                    $xmlWriter->startElement($element_name);
                                                    $xmlWriter->writeCData(apply_filters('woo_field', pmxe_filter(implode(',', $_upsell_skus), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()));
                                                    $xmlWriter->endElement();
                                                }
                                                break;
                                            default:
                                                $xmlWriter->startElement($element_name);
                                                $xmlWriter->writeCData(apply_filters('woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()));
                                                $xmlWriter->endElement();
                                                break;
                                        }
                                    }
                                }
                                if (empty($cur_meta_values)) {
                                    $xmlWriter->startElement($element_name);
                                    $xmlWriter->writeCData(apply_filters('woo_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()));
                                    $xmlWriter->endElement();
                                }
                            }
                            break;
                        case 'woo_order':
                            XmlExportWooCommerceOrder::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID);
                            break;
                        case 'attr':
                            //$element_name = 'woo_' . $element_name;
                            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->slug;
                                            }
                                            $xmlWriter->startElement($is_variable_product ? $element_name : 'attribute_' . $element_name);
                                            $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->startElement('attribute_' . $element_name);
                                        $xmlWriter->writeCData(apply_filters('woo_field', $attribute_pa));
                                        $xmlWriter->endElement();
                                    }
                                }
                            }
                            break;
                        case 'cats':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                $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->slug;
                                                        }
                                                    }
                                                    $hierarchy_group[] = $t->slug;
                                                    $hierarchy_groups[] = implode(">", $hierarchy_group);
                                                } else {
                                                    $hierarchy_groups[] = $t->slug;
                                                }
                                            }
                                        }
                                        if (!empty($hierarchy_groups)) {
                                            $xmlWriter->startElement($element_name);
                                            $xmlWriter->writeCData(apply_filters('pmxe_post_taxonomy', pmxe_filter(implode('|', $hierarchy_groups), $fieldSnipped), get_the_ID()));
                                            $xmlWriter->endElement();
                                        }
                                    }
                                }
                                if ($exportOptions['cc_label'][$ID] == 'product_type' and get_post_type() == 'product_variation') {
                                    $xmlWriter->writeElement('parent_sku', get_post_meta($record->post_parent, '_sku', true));
                                    $xmlWriter->writeElement($element_name, 'variable');
                                }
                            }
                            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->startElement($element_name);
                                $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;
        }
    }
    $xmlWriter->endElement();
    // end data
    if ($preview) {
        return wp_all_export_remove_colons($xmlWriter->flush(true));
    }
    if ($is_cron) {
        $xml = $xmlWriter->flush(true);
        if (file_exists($file_path)) {
            file_put_contents($file_path, wp_all_export_remove_colons(substr(substr($xml, 45), 0, -8)), FILE_APPEND);
        } else {
            file_put_contents($file_path, wp_all_export_remove_colons(substr($xml, 0, -8)));
        }
        return $file_path;
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
            $wp_uploads = wp_upload_dir();
            $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path'];
            $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to'];
            file_put_contents($export_file, wp_all_export_remove_colons(substr($xmlWriter->flush(true), 0, -8)));
            PMXE_Plugin::$session->set('file', $export_file);
            PMXE_Plugin::$session->save_data();
        } else {
            file_put_contents(PMXE_Plugin::$session->file, wp_all_export_remove_colons(substr(substr($xmlWriter->flush(true), 45), 0, -8)), FILE_APPEND);
        }
        return true;
    }
}
Esempio n. 8
0
/**
*	Export XML helper
*/
function pmxe_export_users_xml($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false)
{
    $xmlWriter = new XMLWriter();
    $xmlWriter->openMemory();
    $xmlWriter->setIndent(true);
    $xmlWriter->setIndentString("\t");
    $xmlWriter->startDocument('1.0', $exportOptions['encoding']);
    $xmlWriter->startElement('data');
    foreach ($exportQuery->results as $user) {
        //$exportQuery->the_post(); $record = get_post( get_the_ID() );
        $xmlWriter->startElement('post');
        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' => $user->ID, 'import_id' => $exportOptions['import_id']));
                if ($postRecord->isEmpty()) {
                    $postRecord->set(array('post_id' => $user->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $user->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]) ? str_replace(" ", "_", $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':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_id', pmxe_filter($user->ID, $fieldSnipped), $user->ID));
                            break;
                        case 'user_login':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_login', pmxe_filter($user->user_login, $fieldSnipped), $user->ID));
                            break;
                        case 'user_pass':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_pass', pmxe_filter($user->user_pass, $fieldSnipped), $user->ID));
                            break;
                        case 'user_email':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_email', pmxe_filter($user->user_email, $fieldSnipped), $user->ID));
                            break;
                        case 'user_nicename':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_nicename', pmxe_filter($user->user_nicename, $fieldSnipped), $user->ID));
                            break;
                        case 'user_url':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_url', pmxe_filter($user->user_url, $fieldSnipped), $user->ID));
                            break;
                            /*case 'user_activation_key':
                            			$xmlWriter->writeElement($element_name, apply_filters('pmxe_user_activation_key', pmxe_filter($user->user_activation_key, $fieldSnipped), $user->ID));
                            			break;
                            		case 'user_status':
                            			$xmlWriter->writeElement($element_name, apply_filters('pmxe_user_status', pmxe_filter($user->user_status, $fieldSnipped), $user->ID));
                            			break;*/
                        /*case 'user_activation_key':
                        			$xmlWriter->writeElement($element_name, apply_filters('pmxe_user_activation_key', pmxe_filter($user->user_activation_key, $fieldSnipped), $user->ID));
                        			break;
                        		case 'user_status':
                        			$xmlWriter->writeElement($element_name, apply_filters('pmxe_user_status', pmxe_filter($user->user_status, $fieldSnipped), $user->ID));
                        			break;*/
                        case 'display_name':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_display_name', pmxe_filter($user->display_name, $fieldSnipped), $user->ID));
                            break;
                        case 'user_registered':
                            if (!empty($exportOptions['cc_options'][$ID])) {
                                switch ($exportOptions['cc_options'][$ID]) {
                                    case 'unix':
                                        $post_date = strtotime($user->user_registered);
                                        break;
                                    default:
                                        $post_date = date($exportOptions['cc_options'][$ID], strtotime($user->user_registered));
                                        break;
                                }
                            } else {
                                $post_date = $user->user_registered;
                            }
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_registered', pmxe_filter($post_date, $fieldSnipped), $user->ID));
                            break;
                        case 'nickname':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_nickname', pmxe_filter($user->nickname, $fieldSnipped), $user->ID));
                            break;
                        case 'first_name':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_first_name', pmxe_filter($user->first_name, $fieldSnipped), $user->ID));
                            break;
                        case 'last_name':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_last_name', pmxe_filter($user->last_name, $fieldSnipped), $user->ID));
                            break;
                        case 'wp_capabilities':
                            $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_wp_capabilities', pmxe_filter(implode(",", $user->roles), $fieldSnipped), $user->ID));
                            break;
                        case 'description':
                            $xmlWriter->startElement($element_name);
                            $xmlWriter->writeCData(apply_filters('pmxe_user_description', pmxe_filter($user->description, $fieldSnipped), $user->ID));
                            $xmlWriter->endElement();
                            break;
                        case 'cf':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                $cur_meta_values = get_user_meta($user->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->startElement($element_name);
                                        $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], $user->ID));
                                        $xmlWriter->endElement();
                                    }
                                }
                                if (empty($cur_meta_values)) {
                                    $xmlWriter->startElement($element_name);
                                    $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], $user->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], $user->ID);
                                $field_options = unserialize($exportOptions['cc_options'][$ID]);
                                pmxe_export_acf_field_xml($field_value, $exportOptions, $ID, $user->ID, $xmlWriter, $element_name, $fieldSnipped, $field_options['group_id']);
                            }
                            break;
                        case 'sql':
                            if (!empty($exportOptions['cc_sql'][$ID])) {
                                global $wpdb;
                                $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->startElement($element_name);
                                $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;
        }
    }
    $xmlWriter->endElement();
    // end data
    if ($preview) {
        return wp_all_export_remove_colons($xmlWriter->flush(true));
    }
    if ($is_cron) {
        $xml = $xmlWriter->flush(true);
        if (file_exists($file_path)) {
            file_put_contents($file_path, wp_all_export_remove_colons(substr(substr($xml, 45), 0, -8)), FILE_APPEND);
        } else {
            file_put_contents($file_path, wp_all_export_remove_colons(substr($xml, 0, -8)));
        }
        return $file_path;
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
            $wp_uploads = wp_upload_dir();
            $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path'];
            $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to'];
            file_put_contents($export_file, wp_all_export_remove_colons(substr($xmlWriter->flush(true), 0, -8)));
            PMXE_Plugin::$session->set('file', $export_file);
            PMXE_Plugin::$session->save_data();
        } else {
            file_put_contents(PMXE_Plugin::$session->file, wp_all_export_remove_colons(substr(substr($xmlWriter->flush(true), 45), 0, -8)), FILE_APPEND);
        }
        return true;
    }
}