Ejemplo n.º 1
1
/**
*	AJAX action for preview export row
*/
function pmxe_wp_ajax_wpae_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(PMXE_Plugin::$capabilities)) {
        exit(json_encode(array('html' => __('Security check', 'wp_all_export_plugin'))));
    }
    XmlExportEngine::$is_preview = true;
    $custom_xml_valid = true;
    ob_start();
    $values = array();
    parse_str($_POST['data'], $values);
    $export_id = isset($_GET['id']) ? stripcslashes($_GET['id']) : 0;
    $exportOptions = $values + (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + PMXE_Plugin::get_default_import_options();
    $exportOptions['custom_xml_template'] = isset($_POST['custom_xml']) ? stripcslashes($_POST['custom_xml']) : '';
    $exportOptions['custom_xml_template'] = str_replace('<ID>', '<id>', $exportOptions['custom_xml_template']);
    $exportOptions['custom_xml_template'] = str_replace('</ID>', '</id>', $exportOptions['custom_xml_template']);
    if (!empty($exportOptions['custom_xml_template'])) {
        $custom_xml_template_line_count = substr_count($exportOptions['custom_xml_template'], "\n");
    }
    $errors = new WP_Error();
    $engine = new XmlExportEngine($exportOptions, $errors);
    XmlExportEngine::$exportOptions = $exportOptions;
    XmlExportEngine::$is_user_export = $exportOptions['is_user_export'];
    XmlExportEngine::$is_comment_export = $exportOptions['is_comment_export'];
    XmlExportEngine::$exportID = $export_id;
    if (in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants'))) {
        if (empty(XmlExportEngine::$exportOptions['custom_xml_template'])) {
            $errors->add('form-validation', __('XML template is empty.', 'wp_all_export_plugin'));
        }
        if (!empty(XmlExportEngine::$exportOptions['custom_xml_template'])) {
            $engine->init_additional_data();
            $engine->init_available_data();
            $result = $engine->parse_custom_xml_template();
            $line_numbers = $result['line_numbers'];
            if (!$errors->get_error_codes()) {
                XmlExportEngine::$exportOptions = array_merge(XmlExportEngine::$exportOptions, $result);
            }
            $originalXmlTemplate = $exportOptions['custom_xml_template'];
            libxml_use_internal_errors(true);
            libxml_clear_errors();
            //Add root se we make sure there is a root tag
            $result['original_post_loop'] = '<root>' . $result['original_post_loop'] . '</root>';
            $custom_xml_template = simplexml_load_string($result['original_post_loop']);
            if ($custom_xml_template === false) {
                $custom_xml_template_errors = libxml_get_errors();
                libxml_clear_errors();
                $custom_xml_valid = false;
                // Remove one line because we added root
                $line_difference = $custom_xml_template_line_count - $line_numbers - 1;
            }
            $exportOptions['custom_xml_template'] = str_replace("<!-- BEGIN POST LOOP -->", "<!-- BEGIN LOOP -->", $exportOptions['custom_xml_template']);
            $exportOptions['custom_xml_template'] = str_replace("<!-- END POST LOOP -->", "<!-- END LOOP -->", $exportOptions['custom_xml_template']);
        }
    }
    if (isset($_GET['show_cdata'])) {
        XmlExportEngine::$exportOptions['show_cdata_in_preview'] = (bool) $_GET['show_cdata'];
    } else {
        XmlExportEngine::$exportOptions['show_cdata_in_preview'] = false;
    }
    if ($errors->get_error_codes()) {
        $msgs = $errors->get_error_messages();
        if (!is_array($msgs)) {
            $msgs = array($msgs);
        }
        foreach ($msgs as $msg) {
            ?>
			<div class="error"><p><?php 
            echo $msg;
            ?>
</p></div>
		<?php 
        }
        exit(json_encode(array('html' => ob_get_clean())));
    }
    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));');
        } elseif (XmlExportEngine::$is_comment_export) {
            $exportQuery = eval('return new WP_Comment_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']) or in_array('shop_customer', $exportOptions['cpt'])) {
            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');
        } elseif (in_array('comments', $exportOptions['cpt'])) {
            add_action('comments_clauses', 'wp_all_export_comments_clauses', 10, 1);
            global $wp_version;
            if (version_compare($wp_version, '4.2.0', '>=')) {
                $exportQuery = new WP_Comment_Query(array('orderby' => 'comment_ID', 'order' => 'ASC', 'number' => 10));
            } else {
                $exportQuery = get_comments(array('orderby' => 'comment_ID', 'order' => 'ASC', 'number' => 10));
            }
            remove_action('comments_clauses', 'wp_all_export_comments_clauses');
        } else {
            remove_all_actions('parse_query');
            remove_all_actions('pre_get_posts');
            remove_all_filters('posts_clauses');
            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');
        }
    }
    XmlExportEngine::$exportQuery = $exportQuery;
    $engine->init_additional_data();
    ?>

	<div id="post-preview" class="wpallexport-preview">
		
		<p class="wpallexport-preview-title"><?php 
    echo sprintf("Preview first 10 %s", wp_all_export_get_cpt_name($exportOptions['cpt'], 10));
    ?>
</p>

		<div class="wpallexport-preview-content">
			
		<?php 
    if (!$custom_xml_valid) {
        $error_msg = '<strong class="error">' . __('Invalid XML', 'wp_all_import_plugin') . '</strong><ul  class="error">';
        foreach ($custom_xml_template_errors as $error) {
            $error_msg .= '<li>';
            $error_msg .= __('Line', 'wp_all_import_plugin') . ' ' . ($error->line + $line_difference) . ', ';
            $error_msg .= __('Column', 'wp_all_import_plugin') . ' ' . $error->column . ', ';
            $error_msg .= __('Code', 'wp_all_import_plugin') . ' ' . $error->code . ': ';
            $error_msg .= '<em>' . trim(esc_html($error->message)) . '</em>';
            $error_msg .= '</li>';
        }
        $error_msg .= '</ul>';
        echo $error_msg;
        exit(json_encode(array('html' => ob_get_clean())));
    }
    $wp_uploads = wp_upload_dir();
    $functions = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_EXPORT_UPLOADS_BASE_DIRECTORY . DIRECTORY_SEPARATOR . 'functions.php';
    if (@file_exists($functions)) {
        require_once $functions;
    }
    switch ($exportOptions['export_to']) {
        case 'xml':
            $dom = new DOMDocument('1.0', $exportOptions['encoding']);
            libxml_use_internal_errors(true);
            try {
                $xml = XmlCsvExport::export_xml(true);
            } catch (WpaeMethodNotFoundException $e) {
                // Find the line where the function is
                $errorMessage = '';
                $functionName = $e->getMessage();
                $txtParts = explode("\n", $originalXmlTemplate);
                for ($i = 0, $length = count($txtParts); $i < $length; $i++) {
                    $tmp = strstr($txtParts[$i], $functionName);
                    if ($tmp) {
                        $errorMessage .= 'Error parsing XML feed: Call to undefined function <em>"' . $functionName . '"</em> on Line ' . ($i + 1);
                    }
                }
                $error_msg = '<span class="error">' . __($errorMessage, 'wp_all_import_plugin') . '</span>';
                echo $error_msg;
                exit(json_encode(array('html' => ob_get_clean())));
            } catch (WpaeInvalidStringException $e) {
                // Find the line where the function is
                $errorMessage = '';
                $functionName = $e->getMessage();
                $txtParts = explode("\n", $originalXmlTemplate);
                for ($i = 0, $length = count($txtParts); $i < $length; $i++) {
                    $tmp = strstr($txtParts[$i], $functionName);
                    if ($tmp) {
                        $errorMessage .= 'Error parsing XML feed: Unterminated string on line ' . ($i + 1);
                    }
                }
                $error_msg = '<span class="error">' . __($errorMessage, 'wp_all_import_plugin') . '</span>';
                echo $error_msg;
                exit(json_encode(array('html' => ob_get_clean())));
            } catch (WpaeTooMuchRecursionException $e) {
                $errorMessage = __('There was a problem parsing the custom XML template');
                $error_msg = '<span class="error">' . __($errorMessage, 'wp_all_import_plugin') . '</span>';
                echo $error_msg;
                exit(json_encode(array('html' => ob_get_clean())));
            }
            $xml_errors = false;
            $main_xml_tag = '';
            switch (XmlExportEngine::$exportOptions['xml_template_type']) {
                case 'custom':
                case 'XmlGoogleMerchants':
                    require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php';
                    $preview_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n<Preview>\n" . $xml . "\n</Preview>";
                    $preview_xml = str_replace('<![CDATA[', 'CDATABEGIN', $preview_xml);
                    $preview_xml = str_replace(']]>', 'CDATACLOSE', $preview_xml);
                    $preview_xml = str_replace('&amp;', '&', $preview_xml);
                    $preview_xml = str_replace('&', '&amp;', $preview_xml);
                    $xml = PMXE_XMLWriter::preprocess_xml(XmlExportEngine::$exportOptions['custom_xml_template_header']) . "\n" . $xml . "\n" . PMXE_XMLWriter::preprocess_xml(XmlExportEngine::$exportOptions['custom_xml_template_footer']);
                    $xml = str_replace('<![CDATA[', 'CDATABEGIN', $xml);
                    $xml = str_replace(']]>', 'CDATACLOSE', $xml);
                    $xml = str_replace('&amp;', '&', $xml);
                    $xml = str_replace('&', '&amp;', $xml);
                    // Determine XML root element
                    preg_match_all("%<[\\w]+[\\s|>]{1}%", XmlExportEngine::$exportOptions['custom_xml_template_header'], $matches);
                    if (!empty($matches[0])) {
                        $main_xml_tag = preg_replace("%[\\s|<|>]%", "", array_shift($matches[0]));
                    }
                    libxml_clear_errors();
                    $dom->loadXML($xml);
                    $xml_errors = libxml_get_errors();
                    libxml_clear_errors();
                    if (!$xml_errors) {
                        $xpath = new DOMXPath($dom);
                        if ($elements = @$xpath->query('/' . $main_xml_tag) and $elements->length) {
                            pmxe_render_xml_element($elements->item(0), true);
                        } else {
                            $xml_errors = true;
                        }
                    }
                    break;
                default:
                    libxml_clear_errors();
                    $dom->loadXML($xml);
                    $xml_errors = libxml_get_errors();
                    libxml_clear_errors();
                    $xpath = new DOMXPath($dom);
                    // Determine XML root element
                    $main_xml_tag = apply_filters('wp_all_export_main_xml_tag', $exportOptions['main_xml_tag'], XmlExportEngine::$exportID);
                    $elements = @$xpath->query('/' . $main_xml_tag);
                    if ($elements->length) {
                        pmxe_render_xml_element($elements->item(0), true);
                        $xml_errors = false;
                    } else {
                        $error_msg = '<strong>' . __('Can\'t preview the document.', 'wp_all_import_plugin') . '</strong><ul>';
                        $error_msg .= '<li>';
                        $error_msg .= __('You can continue export or try to use &lt;data&gt; tag as root element.', 'wp_all_import_plugin');
                        $error_msg .= '</li>';
                        $error_msg .= '</ul>';
                        echo $error_msg;
                        exit(json_encode(array('html' => ob_get_clean())));
                    }
                    break;
            }
            if ($xml_errors) {
                $preview_dom = new DOMDocument('1.0', $exportOptions['encoding']);
                libxml_clear_errors();
                $preview_dom->loadXML($preview_xml);
                $preview_xml_errors = libxml_get_errors();
                libxml_clear_errors();
                if ($preview_xml_errors) {
                    $error_msg = '<strong class="error">' . __('Invalid XML', 'wp_all_import_plugin') . '</strong><ul  class="error">';
                    foreach ($preview_xml_errors as $error) {
                        $error_msg .= '<li>';
                        $error_msg .= __('Line', 'wp_all_import_plugin') . ' ' . $error->line . ', ';
                        $error_msg .= __('Column', 'wp_all_import_plugin') . ' ' . $error->column . ', ';
                        $error_msg .= __('Code', 'wp_all_import_plugin') . ' ' . $error->code . ': ';
                        $error_msg .= '<em>' . trim(esc_html($error->message)) . '</em>';
                        $error_msg .= '</li>';
                    }
                    $error_msg .= '</ul>';
                    echo $error_msg;
                    exit(json_encode(array('html' => ob_get_clean())));
                } else {
                    $xpath = new DOMXPath($preview_dom);
                    if ($elements = @$xpath->query('/Preview') and $elements->length) {
                        pmxe_render_xml_element($elements->item(0), true);
                    } else {
                        $error_msg = '<strong>' . __('Can\'t preview the document. Root element is not detected.', 'wp_all_import_plugin') . '</strong><ul>';
                        $error_msg .= '<li>';
                        $error_msg .= __('You can continue export or try to use &lt;data&gt; tag as root element.', 'wp_all_import_plugin');
                        $error_msg .= '</li>';
                        $error_msg .= '</ul>';
                        echo $error_msg;
                        exit(json_encode(array('html' => ob_get_clean())));
                    }
                }
            }
            break;
        case 'csv':
            ?>
			
				<small>
				<?php 
            $csv = XmlCsvExport::export_csv(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.', 'wp_all_export_plugin');
            }
            ?>
				</small>			
				<?php 
            break;
        default:
            _e('This format is not supported.', 'wp_all_export_plugin');
            break;
    }
    wp_reset_postdata();
    ?>

		</div>

	</div>

	<?php 
    exit(json_encode(array('html' => ob_get_clean())));
    die;
}
Ejemplo n.º 2
0
function pmxe_export_xml($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0)
{
    require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php';
    $xmlWriter = new PMXE_XMLWriter();
    $xmlWriter->openMemory();
    $xmlWriter->setIndent(true);
    $xmlWriter->setIndentString("\t");
    $xmlWriter->startDocument('1.0', $exportOptions['encoding']);
    $xmlWriter->startElement($exportOptions['main_xml_tag']);
    if ($is_cron) {
        if (!$exported_by_cron) {
            $additional_data = apply_filters('wp_all_export_additional_data', array(), $exportOptions);
            if (!empty($additional_data)) {
                foreach ($additional_data as $key => $value) {
                    $xmlWriter->startElement(preg_replace('/[^a-z0-9_-]/i', '', $key));
                    $xmlWriter->writeCData($value);
                    $xmlWriter->endElement();
                }
            }
        }
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            $additional_data = apply_filters('wp_all_export_additional_data', array(), $exportOptions);
            if (!empty($additional_data)) {
                foreach ($additional_data as $key => $value) {
                    $xmlWriter->startElement(preg_replace('/[^a-z0-9_-]/i', '', $key));
                    $xmlWriter->writeCData($value);
                    $xmlWriter->endElement();
                }
            }
        }
    }
    global $wpdb;
    while ($exportQuery->have_posts()) {
        $exportQuery->the_post();
        $record = get_post(get_the_ID());
        $xmlWriter->startElement($exportOptions['record_xml_tag']);
        if ($exportOptions['ids']) {
            if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) {
                $postRecord = new PMXI_Post_Record();
                $postRecord->clear();
                $postRecord->getBy(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id']));
                if ($postRecord->isEmpty()) {
                    $postRecord->set(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $record->ID, 'product_key' => $record->ID))->save();
                }
                unset($postRecord);
            }
            foreach ($exportOptions['ids'] as $ID => $value) {
                if (is_numeric($ID)) {
                    if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) {
                        continue;
                    }
                    $element_name_ns = '';
                    $element_name = !empty($exportOptions['cc_name'][$ID]) ? preg_replace('/[^a-z0-9_:-]/i', '', $exportOptions['cc_name'][$ID]) : 'untitled_' . $ID;
                    $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false;
                    if (strpos($element_name, ":") !== false) {
                        $element_name_parts = explode(":", $element_name);
                        $element_name_ns = empty($element_name_parts[0]) ? '' : $element_name_parts[0];
                        $element_name = empty($element_name_parts[1]) ? 'untitled_' . $ID : preg_replace('/[^a-z0-9_-]/i', '', $element_name_parts[1]);
                    }
                    switch ($exportOptions['cc_type'][$ID]) {
                        case 'id':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_id', pmxe_filter(get_the_ID(), $fieldSnipped), get_the_ID()));
                            break;
                        case 'permalink':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_guid', pmxe_filter(get_permalink(), $fieldSnipped), get_the_ID()));
                            break;
                        case 'post_type':
                            $pType = get_post_type();
                            if ($pType == 'product_variation') {
                                $pType = 'product';
                            }
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), get_the_ID()));
                            break;
                        case 'title':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_title', pmxe_filter($record->post_title, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'content':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_content', pmxe_filter($record->post_content, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'media':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $attachment_ids = array();
                            $_featured_image = get_post_meta(get_the_ID(), '_thumbnail_id', true);
                            if (!empty($_featured_image)) {
                                $attachment_ids[] = $_featured_image;
                            }
                            $_gallery = get_post_meta(get_the_ID(), '_product_image_gallery', true);
                            if (!empty($_gallery)) {
                                $gallery = explode(',', $_gallery);
                                if (!empty($gallery) and is_array($gallery)) {
                                    foreach ($gallery as $aid) {
                                        if (!in_array($aid, $attachment_ids)) {
                                            $attachment_ids[] = $aid;
                                        }
                                    }
                                }
                            }
                            $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID));
                            if (!empty($attachment_imgs)) {
                                foreach ($attachment_imgs as $attach) {
                                    if (wp_attachment_is_image($attach->ID) and !in_array($attach->ID, $attachment_ids)) {
                                        $attachment_ids[] = $attach->ID;
                                    }
                                }
                            }
                            if (!empty($attachment_ids)) {
                                foreach ($attachment_ids as $attach_id) {
                                    $attach = get_post($attach_id);
                                    if ($attach and !is_wp_error($attach) and wp_attachment_is_image($attach->ID)) {
                                        $xmlWriter->startElement('image');
                                        $val = wp_get_attachment_url($attach->ID);
                                        if (!empty($exportOptions['cc_options'][$ID])) {
                                            switch ($exportOptions['cc_options'][$ID]) {
                                                case 'filenames':
                                                    $val = basename(wp_get_attachment_url($attach->ID));
                                                    break;
                                                case 'filepaths':
                                                    $val = get_attached_file($attach->ID);
                                                    break;
                                                default:
                                                    # code...
                                                    break;
                                            }
                                        }
                                        $xmlWriter->writeElement('file', apply_filters('pmxe_attachment_url', $val, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('title', apply_filters('pmxe_attachment_title', $attach->post_title, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('caption', apply_filters('pmxe_attachment_caption', $attach->post_excerpt, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('description', apply_filters('pmxe_attachment_content', $attach->post_content, get_the_ID(), $attach->ID));
                                        $xmlWriter->writeElement('alt', apply_filters('pmxe_attachment_alt', get_post_meta($record->ID, '_wp_attachment_image_alt', true), get_the_ID(), $attach->ID));
                                        $xmlWriter->endElement();
                                    }
                                }
                            }
                            $xmlWriter->endElement();
                            break;
                        case 'date':
                            if (!empty($exportOptions['cc_options'][$ID])) {
                                switch ($exportOptions['cc_options'][$ID]) {
                                    case 'unix':
                                        $post_date = get_post_time('U', true);
                                        break;
                                    default:
                                        $post_date = date($exportOptions['cc_options'][$ID], get_post_time('U', true));
                                        break;
                                }
                            } else {
                                $post_date = date("Ymd", get_post_time('U', true));
                            }
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), get_the_ID()));
                            break;
                        case 'attachments':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID));
                            if (!empty($attachment_imgs)) {
                                foreach ($attachment_imgs as $attach) {
                                    if (!wp_attachment_is_image($attach->ID)) {
                                        $xmlWriter->startElement('attach');
                                        $xmlWriter->writeElement('url', apply_filters('pmxe_attachment_url', pmxe_filter(wp_get_attachment_url($attach->ID), $fieldSnipped), get_the_ID(), $attach->ID));
                                        $xmlWriter->endElement();
                                    }
                                }
                            }
                            $xmlWriter->endElement();
                            // end attachments
                            break;
                        case 'parent':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_parent', pmxe_filter($record->post_parent, $fieldSnipped), get_the_ID()));
                            break;
                        case 'comment_status':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_comment_status', pmxe_filter($record->comment_status, $fieldSnipped), get_the_ID()));
                            break;
                        case 'ping_status':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_ping_status', pmxe_filter($record->ping_status, $fieldSnipped), get_the_ID()));
                            break;
                        case 'template':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_template', pmxe_filter(get_post_meta($record->ID, '_wp_page_template', true), $fieldSnipped), get_the_ID()));
                            break;
                        case 'order':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_menu_order', pmxe_filter($record->menu_order, $fieldSnipped), get_the_ID()));
                            break;
                        case 'status':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_status', pmxe_filter($record->post_status, $fieldSnipped), get_the_ID()));
                            break;
                        case 'format':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_format', pmxe_filter(get_post_format($record->ID), $fieldSnipped), get_the_ID()));
                            break;
                        case 'author':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_author', pmxe_filter($record->post_author, $fieldSnipped), get_the_ID()));
                            break;
                        case 'slug':
                            $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_slug', pmxe_filter($record->post_name, $fieldSnipped), get_the_ID()));
                            break;
                        case 'excerpt':
                            $xmlWriter->beginElement($element_name_ns, $element_name, null);
                            $xmlWriter->writeCData(apply_filters('pmxe_post_excerpt', pmxe_filter($record->post_excerpt, $fieldSnipped), get_the_ID()));
                            $xmlWriter->endElement();
                            break;
                        case 'cf':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_value'][$ID]);
                                if (!empty($cur_meta_values) and is_array($cur_meta_values)) {
                                    foreach ($cur_meta_values as $key => $cur_meta_value) {
                                        $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                        $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()));
                                        $xmlWriter->endElement();
                                    }
                                }
                                if (empty($cur_meta_values)) {
                                    $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                    $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()));
                                    $xmlWriter->endElement();
                                }
                            }
                            break;
                        case 'acf':
                            if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) {
                                global $acf;
                                $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID);
                                $field_options = unserialize($exportOptions['cc_options'][$ID]);
                                pmxe_export_acf_field_xml($field_value, $exportOptions, $ID, $record->ID, $xmlWriter, $element_name, $element_name_ns, $fieldSnipped, $field_options['group_id']);
                            }
                            break;
                        case 'woo':
                            XmlExportWooCommerce::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID);
                            break;
                        case 'woo_order':
                            XmlExportWooCommerceOrder::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID);
                            break;
                        case 'attr':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                if ($record->post_parent == 0) {
                                    $is_variable_product = false;
                                    $product_terms = wp_get_post_terms($record->ID, 'product_type');
                                    if (!empty($product_terms)) {
                                        if (!is_wp_error($product_terms)) {
                                            foreach ($product_terms as $term) {
                                                if ('variable' == $term->slug) {
                                                    $is_variable_product = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]);
                                    if (!is_wp_error($txes_list)) {
                                        $attr_new = array();
                                        if (!empty($txes_list)) {
                                            foreach ($txes_list as $t) {
                                                $attr_new[] = $t->name;
                                            }
                                            $xmlWriter->beginElement($element_name_ns, $is_variable_product ? $element_name : 'attribute_' . $element_name, null);
                                            $xmlWriter->writeCData(apply_filters('pmxe_woo_attribute', pmxe_filter(implode('|', $attr_new), $fieldSnipped), get_the_ID()));
                                            $xmlWriter->endElement();
                                        }
                                    }
                                } else {
                                    $attribute_pa = get_post_meta($record->ID, 'attribute_' . $exportOptions['cc_value'][$ID], true);
                                    if (!empty($attribute_pa)) {
                                        $xmlWriter->beginElement($element_name_ns, 'attribute_' . $element_name, null);
                                        $xmlWriter->writeCData(apply_filters('woo_field', $attribute_pa));
                                        $xmlWriter->endElement();
                                    }
                                }
                            }
                            break;
                        case 'cats':
                            if (!empty($exportOptions['cc_value'][$ID])) {
                                if ($exportOptions['cc_label'][$ID] == 'product_type' and get_post_type() == 'product_variation') {
                                    $xmlWriter->writeElement('parent_id', $record->post_parent);
                                    $xmlWriter->writeElement($element_name, 'variable');
                                } else {
                                    $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]);
                                    if (!is_wp_error($txes_list)) {
                                        $txes_ids = array();
                                        $hierarchy_groups = array();
                                        if (!empty($txes_list)) {
                                            foreach ($txes_list as $t) {
                                                $txes_ids[] = $t->term_id;
                                            }
                                            foreach ($txes_list as $t) {
                                                if (wp_all_export_check_children_assign($t->term_id, $exportOptions['cc_value'][$ID], $txes_ids)) {
                                                    $ancestors = get_ancestors($t->term_id, $exportOptions['cc_value'][$ID]);
                                                    if (count($ancestors) > 0) {
                                                        $hierarchy_group = array();
                                                        for ($i = count($ancestors) - 1; $i >= 0; $i--) {
                                                            $term = get_term_by('id', $ancestors[$i], $exportOptions['cc_value'][$ID]);
                                                            if ($term) {
                                                                $hierarchy_group[] = $term->name;
                                                            }
                                                        }
                                                        $hierarchy_group[] = $t->name;
                                                        $hierarchy_groups[] = implode(">", $hierarchy_group);
                                                    } else {
                                                        $hierarchy_groups[] = $t->name;
                                                    }
                                                }
                                            }
                                            if (!empty($hierarchy_groups)) {
                                                $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                                $xmlWriter->writeCData(apply_filters('pmxe_post_taxonomy', pmxe_filter(implode('|', $hierarchy_groups), $fieldSnipped), get_the_ID()));
                                                $xmlWriter->endElement();
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        case 'sql':
                            if (!empty($exportOptions['cc_sql'][$ID])) {
                                $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID()));
                                if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) {
                                    // if shortcode defined
                                    if (strpos($exportOptions['cc_code'][$ID], '[') === 0) {
                                        $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID]));
                                    } else {
                                        $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';');
                                    }
                                }
                                $xmlWriter->beginElement($element_name_ns, $element_name, null);
                                $xmlWriter->writeCData(apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID()));
                                $xmlWriter->endElement();
                            }
                            break;
                        default:
                            # code...
                            break;
                    }
                }
            }
        }
        $xmlWriter->endElement();
        // end post
        if ($preview) {
            break;
        }
        do_action('pmxe_exported_post', $record->ID);
    }
    $xmlWriter->endElement();
    // end data
    if ($preview) {
        return $xmlWriter->flush(true);
    }
    //wp_all_export_remove_colons($xmlWriter->flush(true));
    if ($is_cron) {
        $xml = $xmlWriter->flush(true);
        if (!$exported_by_cron) {
            // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
            if ($exportOptions['include_bom']) {
                file_put_contents($file_path, chr(0xef) . chr(0xbb) . chr(0xbf) . substr($xml, 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            } else {
                file_put_contents($file_path, substr($xml, 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            }
        } else {
            file_put_contents($file_path, substr(substr($xml, 41 + strlen($exportOptions['main_xml_tag'])), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1), FILE_APPEND);
        }
        return $file_path;
    } else {
        if (empty(PMXE_Plugin::$session->file)) {
            // generate export file name
            $export_file = wp_all_export_generate_export_file(XmlExportEngine::$exportID);
            // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
            if ($exportOptions['include_bom']) {
                file_put_contents($export_file, chr(0xef) . chr(0xbb) . chr(0xbf) . substr($xmlWriter->flush(true), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            } else {
                file_put_contents($export_file, substr($xmlWriter->flush(true), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1));
            }
            PMXE_Plugin::$session->set('file', $export_file);
            PMXE_Plugin::$session->save_data();
        } else {
            file_put_contents(PMXE_Plugin::$session->file, substr(substr($xmlWriter->flush(true), 41 + strlen($exportOptions['main_xml_tag'])), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1), FILE_APPEND);
        }
        return true;
    }
}
Ejemplo n.º 3
0
 private static function save_xml_to_file($xmlWriter, $file_path, $is_cron, $exported_by_cron)
 {
     $is_custom_xml = in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants'));
     if ($is_cron) {
         if (!$is_custom_xml) {
             $xml_header = apply_filters('wp_all_export_xml_header', '<?xml version="1.0" encoding="UTF-8"?>', XmlExportEngine::$exportID);
             $xml = str_replace('<?xml version="1.0" encoding="UTF-8"?>', $xml_header, $xmlWriter->wpae_flush());
         } else {
             $xml_header = XmlExportEngine::$exportOptions['custom_xml_template_header'];
             $xml = !$exported_by_cron ? PMXE_XMLWriter::preprocess_xml($xml_header) . $xmlWriter->wpae_flush() : $xmlWriter->wpae_flush();
         }
         if (!$exported_by_cron) {
             if (!$is_custom_xml) {
                 $xml = substr($xml, 0, (strlen(self::$main_xml_tag) + 4) * -1);
             }
             // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
             if (XmlExportEngine::$exportOptions['include_bom']) {
                 file_put_contents($file_path, chr(0xef) . chr(0xbb) . chr(0xbf) . $xml);
             } else {
                 file_put_contents($file_path, $xml);
             }
         } else {
             $xml = !$is_custom_xml ? substr(substr($xml, 41 + strlen(self::$main_xml_tag)), 0, (strlen(self::$main_xml_tag) + 4) * -1) : $xml;
             file_put_contents($file_path, $xml, FILE_APPEND);
         }
         return $file_path;
     } else {
         if (empty(PMXE_Plugin::$session->file)) {
             // generate export file name
             $export_file = wp_all_export_generate_export_file(XmlExportEngine::$exportID);
             if (!$is_custom_xml) {
                 $xml_header = apply_filters('wp_all_export_xml_header', '<?xml version="1.0" encoding="UTF-8"?>', XmlExportEngine::$exportID);
                 $xml = str_replace('<?xml version="1.0" encoding="UTF-8"?>', $xml_header, $xmlWriter->wpae_flush());
             } else {
                 $xml_header = XmlExportEngine::$exportOptions['custom_xml_template_header'];
                 $xml = PMXE_XMLWriter::preprocess_xml($xml_header) . $xmlWriter->wpae_flush();
             }
             if (!$is_custom_xml) {
                 $xml = substr($xml, 0, (strlen(self::$main_xml_tag) + 4) * -1);
             }
             // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters.
             if (XmlExportEngine::$exportOptions['include_bom']) {
                 file_put_contents($export_file, chr(0xef) . chr(0xbb) . chr(0xbf) . $xml);
             } else {
                 file_put_contents($export_file, $xml);
             }
             PMXE_Plugin::$session->set('file', $export_file);
             PMXE_Plugin::$session->save_data();
         } else {
             $xml = !$is_custom_xml ? substr(substr($xmlWriter->wpae_flush(), 41 + strlen(self::$main_xml_tag)), 0, (strlen(self::$main_xml_tag) + 4) * -1) : $xmlWriter->wpae_flush();
             file_put_contents(PMXE_Plugin::$session->file, $xml, FILE_APPEND);
         }
         return true;
     }
 }
Ejemplo n.º 4
0
 public static function export_xml($preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0)
 {
     require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php';
     $woo = array();
     $woo_order = array();
     $acfs = array();
     $taxes = array();
     $attributes = array();
     self::$main_xml_tag = apply_filters('wp_all_export_main_xml_tag', XmlExportEngine::$exportOptions['main_xml_tag'], XmlExportEngine::$exportID);
     self::$node_xml_tag = apply_filters('wp_all_export_record_xml_tag', XmlExportEngine::$exportOptions['record_xml_tag'], XmlExportEngine::$exportID);
     $implode_delimiter = XmlExportEngine::$exportOptions['delimiter'] == ',' ? '|' : ',';
     $xmlWriter = new PMXE_XMLWriter();
     $xmlWriter->openMemory();
     $xmlWriter->setIndent(true);
     $xmlWriter->setIndentString("\t");
     $xmlWriter->startDocument('1.0', XmlExportEngine::$exportOptions['encoding']);
     $xmlWriter->startElement(self::$main_xml_tag);
     // add additional data after XML root element
     self::xml_header($xmlWriter, $is_cron, $exported_by_cron);
     // [ Exporting requested data ]
     if (XmlExportEngine::$is_user_export) {
         foreach (XmlExportEngine::$exportQuery->results as $user) {
             $is_export_record = apply_filters('wp_all_export_xml_rows', true, $user, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID);
             if (!$is_export_record) {
                 continue;
             }
             // add additional information before each node
             self::before_xml_node($xmlWriter, $user->ID);
             $xmlWriter->startElement(self::$node_xml_tag);
             XmlExportUser::prepare_data($user, $xmlWriter, $acfs, $implode_delimiter, $preview);
             $xmlWriter->endElement();
             // end post
             // add additional information after each node
             self::after_xml_node($xmlWriter, $user->ID);
             if ($preview) {
                 break;
             }
             do_action('pmxe_exported_post', $user->ID, XmlExportEngine::$exportRecord);
         }
     } elseif (XmlExportEngine::$is_comment_export) {
         global $wp_version;
         if (version_compare($wp_version, '4.2.0', '>=')) {
             $comments = XmlExportEngine::$exportQuery->get_comments();
         } else {
             $comments = XmlExportEngine::$exportQuery;
         }
         foreach ($comments as $comment) {
             $is_export_record = apply_filters('wp_all_export_xml_rows', true, $comment, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID);
             if (!$is_export_record) {
                 continue;
             }
             // add additional information before each node
             self::before_xml_node($xmlWriter, $comment->comment_ID);
             $xmlWriter->startElement(self::$node_xml_tag);
             XmlExportComment::prepare_data($comment, $xmlWriter, $implode_delimiter, $preview);
             $xmlWriter->endElement();
             // end post
             // add additional information after each node
             self::after_xml_node($xmlWriter, $comment->comment_ID);
             if ($preview) {
                 break;
             }
             do_action('pmxe_exported_post', $comment->comment_ID, XmlExportEngine::$exportRecord);
         }
     } else {
         while (XmlExportEngine::$exportQuery->have_posts()) {
             XmlExportEngine::$exportQuery->the_post();
             $record = get_post(get_the_ID());
             $is_export_record = apply_filters('wp_all_export_xml_rows', true, $record, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID);
             if (!$is_export_record) {
                 continue;
             }
             // add additional information before each node
             self::before_xml_node($xmlWriter, $record->ID);
             $xmlWriter->startElement(self::$node_xml_tag);
             XmlExportCpt::prepare_data($record, $xmlWriter, $acfs, $woo, $woo_order, $implode_delimiter, $preview);
             $xmlWriter->endElement();
             // end post
             // add additional information after each node
             self::after_xml_node($xmlWriter, $record->ID);
             if ($preview) {
                 break;
             }
             do_action('pmxe_exported_post', $record->ID, XmlExportEngine::$exportRecord);
         }
         wp_reset_postdata();
     }
     // [ \Exporting requested data ]
     $xmlWriter->endElement();
     // close root XML element
     if ($preview) {
         return $xmlWriter->flush(true);
     }
     return self::save_xml_to_file($xmlWriter, $file_path, $is_cron, $exported_by_cron);
 }