Example #1
0
/**
*	AJAX action export processing
*/
function pmxe_wp_ajax_export()
{
    if (!check_ajax_referer('wp_all_export_secure', 'security', false)) {
        exit(__('Security check', 'wp_all_export_plugin'));
    }
    if (!current_user_can('manage_options')) {
        exit(__('Security check', 'wp_all_export_plugin'));
    }
    $wp_uploads = wp_upload_dir();
    $export = new PMXE_Export_Record();
    $export->getById(PMXE_Plugin::$session->update_previous);
    $exportOptions = (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + PMXE_Plugin::get_default_import_options();
    wp_reset_postdata();
    XmlExportEngine::$exportOptions = $exportOptions;
    XmlExportEngine::$is_user_export = $exportOptions['is_user_export'];
    $posts_per_page = $exportOptions['records_per_iteration'];
    if ('advanced' == $exportOptions['export_type']) {
        if (XmlExportEngine::$is_user_export) {
            $exportQuery = eval('return new WP_User_Query(array(' . $exportOptions['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $export->exported . ', \'number\' => ' . $posts_per_page . ' ));');
        } else {
            $exportQuery = eval('return new WP_Query(array(' . $exportOptions['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $export->exported . ', \'posts_per_page\' => ' . $posts_per_page . ' ));');
        }
    } else {
        XmlExportEngine::$post_types = $exportOptions['cpt'];
        if (!in_array('users', $exportOptions['cpt'])) {
            add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
            add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
            $exportQuery = new WP_Query(array('post_type' => $exportOptions['cpt'], 'post_status' => 'any', 'orderby' => 'ID', 'order' => 'ASC', 'offset' => $export->exported, 'posts_per_page' => $posts_per_page));
            remove_filter('posts_where', 'wp_all_export_posts_where');
            remove_filter('posts_join', 'wp_all_export_posts_join');
        } 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' => $posts_per_page, 'offset' => $export->exported));
            remove_action('pre_user_query', 'wp_all_export_pre_user_query');
        }
    }
    XmlExportEngine::$exportQuery = $exportQuery;
    $foundPosts = !XmlExportEngine::$is_user_export ? $exportQuery->found_posts : $exportQuery->get_total();
    $postCount = !XmlExportEngine::$is_user_export ? $exportQuery->post_count : count($exportQuery->get_results());
    if (!$export->exported) {
        if (!empty($export->attch_id)) {
            wp_delete_attachment($export->attch_id, true);
        }
        $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
        if ($is_secure_import and !empty($exportOptions['filepath'])) {
            wp_all_export_remove_source(wp_all_export_get_absolute_path($exportOptions['filepath']));
            $exportOptions['filepath'] = '';
        }
        PMXE_Plugin::$session->set('count', $foundPosts);
        PMXE_Plugin::$session->save_data();
    }
    // if posts still exists then export them
    if ($postCount) {
        switch ($exportOptions['export_to']) {
            case 'xml':
                if (!XmlExportEngine::$is_user_export) {
                    pmxe_export_xml($exportQuery, $exportOptions);
                } else {
                    pmxe_export_users_xml($exportQuery, $exportOptions);
                }
                break;
            case 'csv':
                if (!XmlExportEngine::$is_user_export) {
                    pmxe_export_csv($exportQuery, $exportOptions);
                } else {
                    pmxe_export_users_csv($exportQuery, $exportOptions);
                }
                break;
            default:
                # code...
                break;
        }
        wp_reset_postdata();
    }
    if ($postCount) {
        $export->set(array('exported' => $export->exported + $postCount))->save();
    }
    if ($posts_per_page != -1 and $postCount) {
        wp_send_json(array('exported' => $export->exported, 'percentage' => ceil($export->exported / $foundPosts * 100), 'done' => false, 'records_per_request' => $exportOptions['records_per_iteration']));
    } else {
        wp_reset_postdata();
        if (file_exists(PMXE_Plugin::$session->file)) {
            if ($exportOptions['export_to'] == 'xml') {
                file_put_contents(PMXE_Plugin::$session->file, '</data>', FILE_APPEND);
            }
            $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
            if (!$is_secure_import) {
                $wp_filetype = wp_check_filetype(basename(PMXE_Plugin::$session->file), null);
                $attachment_data = array('guid' => $wp_uploads['baseurl'] . '/' . _wp_relative_upload_path(PMXE_Plugin::$session->file), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename(PMXE_Plugin::$session->file)), 'post_content' => '', 'post_status' => 'inherit');
                $attach_id = wp_insert_attachment($attachment_data, PMXE_Plugin::$session->file);
                if (!$export->isEmpty()) {
                    $export->set(array('attch_id' => $attach_id))->save();
                }
            } else {
                $exportOptions['filepath'] = wp_all_export_get_relative_path(PMXE_Plugin::$session->file);
                if (!$export->isEmpty()) {
                    $export->set(array('options' => $exportOptions))->save();
                }
            }
            if (wp_all_export_is_compatible() and ($exportOptions['is_generate_templates'] or $exportOptions['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' => 0, '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' => $export->id);
                if (in_array('product', $exportOptions['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' == $exportOptions['export_to']) {
                    wp_all_export_prepare_template_xml($exportOptions, $templateOptions);
                } else {
                    wp_all_export_prepare_template_csv($exportOptions, $templateOptions);
                }
                $options = $templateOptions + PMXI_Plugin::get_default_import_options();
                if ($exportOptions['is_generate_templates']) {
                    $template = new PMXI_Template_Record();
                    $tpl_options = $options;
                    if ('csv' == $exportOptions['export_to']) {
                        $tpl_options['delimiter'] = $exportOptions['delimiter'];
                    }
                    $tpl_options['update_all_data'] = 'yes';
                    $tpl_options['is_update_status'] = 1;
                    $tpl_options['is_update_title'] = 1;
                    $tpl_options['is_update_author'] = 1;
                    $tpl_options['is_update_slug'] = 1;
                    $tpl_options['is_update_content'] = 1;
                    $tpl_options['is_update_excerpt'] = 1;
                    $tpl_options['is_update_dates'] = 1;
                    $tpl_options['is_update_menu_order'] = 1;
                    $tpl_options['is_update_parent'] = 1;
                    $tpl_options['is_update_attachments'] = 1;
                    $tpl_options['is_update_acf'] = 1;
                    $tpl_options['update_acf_logic'] = 'full_update';
                    $tpl_options['acf_list'] = '';
                    $tpl_options['is_update_product_type'] = 1;
                    $tpl_options['is_update_attributes'] = 1;
                    $tpl_options['update_attributes_logic'] = 'full_update';
                    $tpl_options['attributes_list'] = '';
                    $tpl_options['is_update_images'] = 1;
                    $tpl_options['is_update_custom_fields'] = 1;
                    $tpl_options['update_custom_fields_logic'] = 'full_update';
                    $tpl_options['custom_fields_list'] = '';
                    $tpl_options['is_update_categories'] = 1;
                    $tpl_options['update_categories_logic'] = 'full_update';
                    $tpl_options['taxonomies_list'] = '';
                    $tpl_data = array('name' => $exportOptions['template_name'], 'is_keep_linebreaks' => 0, 'is_leave_html' => 0, 'fix_characters' => 0, 'options' => $tpl_options);
                    if (!empty($exportOptions['template_name'])) {
                        // save template in database
                        $template->getByName($exportOptions['template_name'])->set($tpl_data)->save();
                    }
                }
                // associate exported posts with new import
                if ($exportOptions['is_generate_import']) {
                    $import = new PMXI_Import_Record();
                    $import->getById($exportOptions['import_id']);
                    if (!$import->isEmpty() and $import->parent_import_id == 99999) {
                        $xmlPath = PMXE_Plugin::$session->file;
                        $root_element = '';
                        $historyPath = PMXE_Plugin::$session->file;
                        if ('csv' == $exportOptions['export_to']) {
                            $options['delimiter'] = $exportOptions['delimiter'];
                            include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                            $path_info = pathinfo($xmlPath);
                            $path_parts = explode(DIRECTORY_SEPARATOR, $path_info['dirname']);
                            $security_folder = array_pop($path_parts);
                            $target = $is_secure_import ? $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY . DIRECTORY_SEPARATOR . $security_folder : $wp_uploads['path'];
                            $csv = new PMXI_CsvParser(array('filename' => $xmlPath, 'targetDir' => $target));
                            $historyPath = $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, 'iteration' => 1, 'count' => PMXE_Plugin::$session->count))->save();
                        $history_file = new PMXI_File_Record();
                        $history_file->set(array('name' => $import->name, 'import_id' => $import->id, 'path' => $historyPath, 'registered_on' => date('Y-m-d H:i:s')))->save();
                        $exportOptions['import_id'] = $import->id;
                        $export->set(array('options' => $exportOptions))->save();
                    }
                }
            }
        }
        $export->set(array('executing' => 0, 'canceled' => 0))->save();
        wp_send_json(array('exported' => $export->exported, 'percentage' => 100, 'done' => true, 'records_per_request' => $exportOptions['records_per_iteration'], 'file' => PMXE_Plugin::$session->file));
    }
}
Example #2
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;
 }
Example #3
0
/**
*	AJAX action for preview export row
*/
function pmxe_wp_ajax_export_preview()
{
    if (!check_ajax_referer('wp_all_export_secure', 'security', false)) {
        exit(json_encode(array('html' => __('Security check', 'wp_all_export_plugin'))));
    }
    if (!current_user_can('manage_options')) {
        exit(json_encode(array('html' => __('Security check', 'wp_all_export_plugin'))));
    }
    ob_start();
    $values = array();
    parse_str($_POST['data'], $values);
    $exportOptions = $values + (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + PMXE_Plugin::get_default_import_options();
    XmlExportEngine::$exportOptions = $exportOptions;
    XmlExportEngine::$is_user_export = $exportOptions['is_user_export'];
    if ('advanced' == $exportOptions['export_type']) {
        if (XmlExportEngine::$is_user_export) {
            $exportQuery = eval('return new WP_User_Query(array(' . $exportOptions['wp_query'] . ', \'offset\' => 0, \'number\' => 10));');
        } else {
            $exportQuery = eval('return new WP_Query(array(' . $exportOptions['wp_query'] . ', \'offset\' => 0, \'posts_per_page\' => 10));');
        }
    } else {
        XmlExportEngine::$post_types = $exportOptions['cpt'];
        if (!in_array('users', $exportOptions['cpt'])) {
            add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
            add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
            $exportQuery = new WP_Query(array('post_type' => $exportOptions['cpt'], 'post_status' => 'any', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 10));
            remove_filter('posts_where', 'wp_all_export_posts_where');
            remove_filter('posts_join', 'wp_all_export_posts_join');
        } 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' => 10));
            remove_action('pre_user_query', 'wp_all_export_pre_user_query');
        }
    }
    XmlExportEngine::$exportQuery = $exportQuery;
    ?>

	<div id="post-preview" class="wpallexport-preview">
		
		<div class="wpallexport-preview-content">
			
		<?php 
    switch ($exportOptions['export_to']) {
        case 'xml':
            $dom = new DOMDocument('1.0', $exportOptions['encoding']);
            $old = libxml_use_internal_errors(true);
            $xml = !XmlExportEngine::$is_user_export ? pmxe_export_xml($exportQuery, $exportOptions, true) : pmxe_export_users_xml($exportQuery, $exportOptions, true);
            $dom->loadXML($xml);
            libxml_use_internal_errors($old);
            $xpath = new DOMXPath($dom);
            if ($elements = @$xpath->query('/data') and $elements->length) {
                pmxe_render_xml_element($elements->item(0), true);
            }
            break;
        case 'csv':
            ?>
			
				<small>
				<?php 
            $csv = !XmlExportEngine::$is_user_export ? pmxe_export_csv($exportQuery, $exportOptions, true) : pmxe_export_users_csv($exportQuery, $exportOptions, true);
            if (!empty($csv)) {
                $csv_rows = array_filter(explode("\n", $csv));
                if ($csv_rows) {
                    ?>
							<table class="pmxe_preview" cellpadding="0" cellspacing="0">
							<?php 
                    foreach ($csv_rows as $rkey => $row) {
                        $cells = str_getcsv($row, $exportOptions['delimiter']);
                        if ($cells) {
                            ?>
									<tr>
										<?php 
                            foreach ($cells as $key => $value) {
                                ?>
											<td>
												<?php 
                                if (!$rkey) {
                                    ?>
<strong><?php 
                                }
                                ?>
												<?php 
                                echo $value;
                                ?>
												<?php 
                                if (!$rkey) {
                                    ?>
</strong><?php 
                                }
                                ?>
											</td>
											<?php 
                            }
                            ?>
									</tr>
									<?php 
                        }
                    }
                    ?>
							</table>
							<?php 
                }
            } else {
                _e('Data not found.', 'pmxe_plugin');
            }
            ?>
				</small>			
				<?php 
            break;
        default:
            _e('This format is not supported.', 'pmxe_plugin');
            break;
    }
    wp_reset_postdata();
    ?>

		</div>

	</div>

	<?php 
    exit(json_encode(array('html' => ob_get_clean())));
    die;
}