admin_head.php
9 years ago
admin_init.php
5 years ago
admin_menu.php
4 years ago
admin_notices.php
8 years ago
init.php
8 years ago
pmxe_after_export.php
8 years ago
pmxe_before_export.php
8 years ago
pmxe_exported_post.php
10 years ago
wp_ajax_dismiss_export_warnings.php
9 years ago
wp_ajax_dismiss_warnings.php
4 years ago
wp_ajax_generate_zapier_api_key.php
10 years ago
wp_ajax_redirect_after_addon_installed.php
6 years ago
wp_ajax_save_scheduling.php
8 years ago
wp_ajax_scheduling_dialog_content.php
5 years ago
wp_ajax_wpae_available_rules.php
8 years ago
wp_ajax_wpae_filtering.php
4 years ago
wp_ajax_wpae_filtering_count.php
4 years ago
wp_ajax_wpae_preview.php
8 years ago
wp_ajax_wpae_upgrade_notice.php
4 years ago
wp_ajax_wpallexport.php
8 years ago
wp_loaded.php
4 years ago
wpmu_new_blog.php
10 years ago
wp_ajax_wpae_preview.php
437 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AJAX action for preview export row |
| 4 | */ |
| 5 | |
| 6 | function pmxe_wp_ajax_wpae_preview(){ |
| 7 | |
| 8 | if ( ! check_ajax_referer( 'wp_all_export_secure', 'security', false )){ |
| 9 | exit( json_encode(array('html' => __('Security check', 'wp_all_export_plugin'))) ); |
| 10 | } |
| 11 | |
| 12 | if ( ! current_user_can( PMXE_Plugin::$capabilities ) ){ |
| 13 | exit( json_encode(array('html' => __('Security check', 'wp_all_export_plugin'))) ); |
| 14 | } |
| 15 | |
| 16 | XmlExportEngine::$is_preview = true; |
| 17 | |
| 18 | $custom_xml_valid = true; |
| 19 | |
| 20 | ob_start(); |
| 21 | |
| 22 | $values = array(); |
| 23 | |
| 24 | parse_str($_POST['data'], $values); |
| 25 | |
| 26 | if(is_array($values['cc_options'])) { |
| 27 | |
| 28 | foreach ($values['cc_options'] as &$value) { |
| 29 | $value = stripslashes($value); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | $export_id = (isset($_GET['id'])) ? stripcslashes($_GET['id']) : 0; |
| 34 | |
| 35 | $exportOptions = $values + (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + PMXE_Plugin::get_default_import_options(); |
| 36 | |
| 37 | $exportOptions['custom_xml_template'] = (isset($_POST['custom_xml'])) ? stripcslashes($_POST['custom_xml']) : ''; |
| 38 | $exportOptions['custom_xml_template'] = str_replace('<ID>','<id>', $exportOptions['custom_xml_template'] ); |
| 39 | $exportOptions['custom_xml_template'] = str_replace('</ID>','</id>', $exportOptions['custom_xml_template'] ); |
| 40 | |
| 41 | if ( ! empty($exportOptions['custom_xml_template'])) { |
| 42 | $custom_xml_template_line_count = substr_count($exportOptions['custom_xml_template'], "\n"); |
| 43 | } |
| 44 | |
| 45 | $errors = new WP_Error(); |
| 46 | |
| 47 | $engine = new XmlExportEngine($exportOptions, $errors); |
| 48 | |
| 49 | XmlExportEngine::$exportOptions = $exportOptions; |
| 50 | XmlExportEngine::$is_user_export = $exportOptions['is_user_export']; |
| 51 | XmlExportEngine::$is_comment_export = $exportOptions['is_comment_export']; |
| 52 | XmlExportEngine::$is_taxonomy_export = $exportOptions['is_taxonomy_export']; |
| 53 | XmlExportEngine::$exportID = $export_id; |
| 54 | |
| 55 | if ( class_exists('SitePress') && ! empty(XmlExportEngine::$exportOptions['wpml_lang'])){ |
| 56 | do_action( 'wpml_switch_language', XmlExportEngine::$exportOptions['wpml_lang'] ); |
| 57 | } |
| 58 | |
| 59 | if (XmlExportEngine::$exportOptions['export_to'] == XmlExportEngine::EXPORT_TYPE_XML && in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants')) ){ |
| 60 | |
| 61 | if ( empty(XmlExportEngine::$exportOptions['custom_xml_template']) ) |
| 62 | { |
| 63 | $errors->add('form-validation', __('XML template is empty.', 'wp_all_export_plugin')); |
| 64 | } |
| 65 | |
| 66 | if ( ! empty(XmlExportEngine::$exportOptions['custom_xml_template'])){ |
| 67 | |
| 68 | $engine->init_additional_data(); |
| 69 | |
| 70 | $engine->init_available_data(); |
| 71 | |
| 72 | $result = $engine->parse_custom_xml_template(); |
| 73 | $line_numbers = $result['line_numbers']; |
| 74 | if ( ! $errors->get_error_codes()) { |
| 75 | XmlExportEngine::$exportOptions = array_merge(XmlExportEngine::$exportOptions, $result); |
| 76 | } |
| 77 | |
| 78 | $originalXmlTemplate = $exportOptions['custom_xml_template']; |
| 79 | libxml_use_internal_errors(true); |
| 80 | libxml_clear_errors(); |
| 81 | |
| 82 | //Add root se we make sure there is a root tag |
| 83 | $result['original_post_loop'] = '<root>'.$result['original_post_loop'].'</root>'; |
| 84 | |
| 85 | $custom_xml_template = simplexml_load_string($result['original_post_loop']); |
| 86 | |
| 87 | if ($custom_xml_template === false) { |
| 88 | $custom_xml_template_errors = libxml_get_errors(); |
| 89 | libxml_clear_errors(); |
| 90 | $custom_xml_valid = false; |
| 91 | // Remove one line because we added root |
| 92 | $line_difference = $custom_xml_template_line_count - $line_numbers - 1; |
| 93 | } |
| 94 | $exportOptions['custom_xml_template'] = str_replace("<!-- BEGIN POST LOOP -->", "<!-- BEGIN LOOP -->", $exportOptions['custom_xml_template']); |
| 95 | $exportOptions['custom_xml_template'] = str_replace("<!-- END POST LOOP -->", "<!-- END LOOP -->", $exportOptions['custom_xml_template']); |
| 96 | |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if(isset($_GET['show_cdata'])) { |
| 101 | XmlExportEngine::$exportOptions['show_cdata_in_preview'] = (bool)$_GET['show_cdata']; |
| 102 | } else { |
| 103 | XmlExportEngine::$exportOptions['show_cdata_in_preview'] = false; |
| 104 | } |
| 105 | |
| 106 | if ( $errors->get_error_codes()) { |
| 107 | $msgs = $errors->get_error_messages(); |
| 108 | if ( ! is_array($msgs)) { |
| 109 | $msgs = array($msgs); |
| 110 | } |
| 111 | foreach ($msgs as $msg): ?> |
| 112 | <div class="error"><p><?php echo $msg ?></p></div> |
| 113 | <?php endforeach; |
| 114 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 115 | } |
| 116 | |
| 117 | if ( 'advanced' == $exportOptions['export_type'] ) |
| 118 | { |
| 119 | if ( XmlExportEngine::$is_user_export ) { |
| 120 | $exportQuery = eval('return new WP_User_Query(array(' . $exportOptions['wp_query'] . ', \'offset\' => 0, \'number\' => 10));'); |
| 121 | } |
| 122 | elseif ( XmlExportEngine::$is_comment_export ) { |
| 123 | $exportQuery = eval('return new WP_Comment_Query(array(' . $exportOptions['wp_query'] . ', \'offset\' => 0, \'number\' => 10));'); |
| 124 | } |
| 125 | else { |
| 126 | remove_all_actions('parse_query'); |
| 127 | remove_all_actions('pre_get_posts'); |
| 128 | remove_all_filters('posts_clauses'); |
| 129 | |
| 130 | $exportQuery = eval('return new WP_Query(array(' . $exportOptions['wp_query'] . ', \'offset\' => 0, \'posts_per_page\' => 10));'); |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | XmlExportEngine::$post_types = $exportOptions['cpt']; |
| 136 | |
| 137 | if ( in_array('users', $exportOptions['cpt']) or in_array('shop_customer', $exportOptions['cpt'])) |
| 138 | { |
| 139 | add_action('pre_user_query', 'wp_all_export_pre_user_query', 10, 1); |
| 140 | $exportQuery = new WP_User_Query( array( 'orderby' => 'ID', 'order' => 'ASC', 'number' => 10 )); |
| 141 | remove_action('pre_user_query', 'wp_all_export_pre_user_query'); |
| 142 | } |
| 143 | elseif ( in_array('taxonomies', $exportOptions['cpt'])) |
| 144 | { |
| 145 | add_filter('terms_clauses', 'wp_all_export_terms_clauses', 10, 3); |
| 146 | $exportQuery = new WP_Term_Query( array( 'taxonomy' => $exportOptions['taxonomy_to_export'], 'orderby' => 'term_id', 'order' => 'ASC', 'number' => 10, 'hide_empty' => false )); |
| 147 | remove_filter('terms_clauses', 'wp_all_export_terms_clauses'); |
| 148 | } |
| 149 | elseif( in_array('comments', $exportOptions['cpt'])) |
| 150 | { |
| 151 | add_action('comments_clauses', 'wp_all_export_comments_clauses', 10, 1); |
| 152 | |
| 153 | global $wp_version; |
| 154 | |
| 155 | if ( version_compare($wp_version, '4.2.0', '>=') ) |
| 156 | { |
| 157 | $exportQuery = new WP_Comment_Query( array( 'orderby' => 'comment_ID', 'order' => 'ASC', 'number' => 10 )); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | $exportQuery = get_comments( array( 'orderby' => 'comment_ID', 'order' => 'ASC', 'number' => 10 )); |
| 162 | } |
| 163 | remove_action('comments_clauses', 'wp_all_export_comments_clauses'); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | remove_all_actions('parse_query'); |
| 168 | remove_all_actions('pre_get_posts'); |
| 169 | remove_all_filters('posts_clauses'); |
| 170 | |
| 171 | add_filter('posts_join', 'wp_all_export_posts_join', 10, 1); |
| 172 | add_filter('posts_where', 'wp_all_export_posts_where', 10, 1); |
| 173 | $exportQuery = new WP_Query( array( 'post_type' => $exportOptions['cpt'], 'post_status' => 'any', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 10 )); |
| 174 | |
| 175 | remove_filter('posts_where', 'wp_all_export_posts_where'); |
| 176 | remove_filter('posts_join', 'wp_all_export_posts_join'); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | XmlExportEngine::$exportQuery = $exportQuery; |
| 181 | |
| 182 | $engine->init_additional_data(); |
| 183 | |
| 184 | ?> |
| 185 | |
| 186 | <div id="post-preview" class="wpallexport-preview"> |
| 187 | |
| 188 | <p class="wpallexport-preview-title"><?php echo sprintf("Preview first 10 %s", wp_all_export_get_cpt_name($exportOptions['cpt'], 10, $exportOptions)); ?></p> |
| 189 | |
| 190 | <div class="wpallexport-preview-content"> |
| 191 | |
| 192 | <?php |
| 193 | |
| 194 | if(!$custom_xml_valid) { |
| 195 | $error_msg = '<strong class="error">' . __('Invalid XML', 'wp_all_import_plugin') . '</strong><ul class="error">'; |
| 196 | foreach($custom_xml_template_errors as $error) { |
| 197 | $error_msg .= '<li>'; |
| 198 | $error_msg .= __('Line', 'wp_all_import_plugin') . ' ' . ($error->line + $line_difference) . ', '; |
| 199 | $error_msg .= __('Column', 'wp_all_import_plugin') . ' ' . $error->column . ', '; |
| 200 | $error_msg .= __('Code', 'wp_all_import_plugin') . ' ' . $error->code . ': '; |
| 201 | $error_msg .= '<em>' . trim(esc_html($error->message)) . '</em>'; |
| 202 | $error_msg .= '</li>'; |
| 203 | } |
| 204 | $error_msg .= '</ul>'; |
| 205 | echo $error_msg; |
| 206 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 207 | } |
| 208 | |
| 209 | switch ($exportOptions['export_to']) { |
| 210 | |
| 211 | case 'xml': |
| 212 | |
| 213 | $dom = new DOMDocument('1.0', $exportOptions['encoding']); |
| 214 | libxml_use_internal_errors(true); |
| 215 | try{ |
| 216 | $xml = XmlCsvExport::export_xml(true); |
| 217 | } catch (WpaeMethodNotFoundException $e) { |
| 218 | // Find the line where the function is |
| 219 | $errorMessage = ''; |
| 220 | $functionName = $e->getMessage(); |
| 221 | $txtParts = explode("\n",$originalXmlTemplate); |
| 222 | for ($i=0, $length = count($txtParts);$i<$length;$i++) |
| 223 | { |
| 224 | $tmp = strstr($txtParts[$i], $functionName); |
| 225 | if ($tmp) { |
| 226 | $errorMessage .= 'Error parsing XML feed: Call to undefined function <em>"'.$functionName.'"</em> on Line '.($i+1); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>'; |
| 231 | echo $error_msg; |
| 232 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 233 | } catch (WpaeInvalidStringException $e) { |
| 234 | // Find the line where the function is |
| 235 | $errorMessage = ''; |
| 236 | $functionName = $e->getMessage(); |
| 237 | $txtParts = explode("\n",$originalXmlTemplate); |
| 238 | for ($i=0, $length = count($txtParts);$i<$length;$i++) |
| 239 | { |
| 240 | $tmp = strstr($txtParts[$i], $functionName); |
| 241 | if ($tmp) { |
| 242 | $errorMessage .= 'Error parsing XML feed: Unterminated string on line '.($i+1); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>'; |
| 247 | echo $error_msg; |
| 248 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 249 | } catch (WpaeTooMuchRecursionException $e) { |
| 250 | $errorMessage = __('There was a problem parsing the custom XML template'); |
| 251 | $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>'; |
| 252 | echo $error_msg; |
| 253 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 254 | } |
| 255 | |
| 256 | $xml_errors = false; |
| 257 | |
| 258 | $main_xml_tag = ''; |
| 259 | |
| 260 | switch ( XmlExportEngine::$exportOptions['xml_template_type'] ){ |
| 261 | |
| 262 | case 'custom': |
| 263 | case 'XmlGoogleMerchants': |
| 264 | |
| 265 | require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php'; |
| 266 | |
| 267 | $preview_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n<Preview>\n" . $xml . "\n</Preview>"; |
| 268 | |
| 269 | $preview_xml = str_replace('<![CDATA[', 'CDATABEGIN', $preview_xml); |
| 270 | $preview_xml = str_replace(']]>', 'CDATACLOSE', $preview_xml); |
| 271 | $preview_xml = str_replace('&', '&', $preview_xml); |
| 272 | $preview_xml = str_replace('&', '&', $preview_xml); |
| 273 | |
| 274 | $xml = PMXE_XMLWriter::preprocess_xml( XmlExportEngine::$exportOptions['custom_xml_template_header'] ) . "\n" . $xml . "\n" . PMXE_XMLWriter::preprocess_xml( XmlExportEngine::$exportOptions['custom_xml_template_footer'] ); |
| 275 | |
| 276 | $xml = str_replace('<![CDATA[', 'CDATABEGIN', $xml); |
| 277 | $xml = str_replace(']]>', 'CDATACLOSE', $xml); |
| 278 | $xml = str_replace('&', '&', $xml); |
| 279 | $xml = str_replace('&', '&', $xml); |
| 280 | |
| 281 | // Determine XML root element |
| 282 | preg_match_all("%<[\w]+[\s|>]{1}%", XmlExportEngine::$exportOptions['custom_xml_template_header'], $matches); |
| 283 | |
| 284 | if ( ! empty($matches[0]) ){ |
| 285 | $main_xml_tag = preg_replace("%[\s|<|>]%","",array_shift($matches[0])); |
| 286 | } |
| 287 | |
| 288 | libxml_clear_errors(); |
| 289 | $dom->loadXML($xml); |
| 290 | $xml_errors = libxml_get_errors(); |
| 291 | libxml_clear_errors(); |
| 292 | if (! $xml_errors ){ |
| 293 | $xpath = new DOMXPath($dom); |
| 294 | if (($elements = @$xpath->query('/' . $main_xml_tag)) and $elements->length){ |
| 295 | pmxe_render_xml_element($elements->item( 0 ), true); |
| 296 | } |
| 297 | else{ |
| 298 | $xml_errors = true; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | break; |
| 303 | |
| 304 | default: |
| 305 | |
| 306 | libxml_clear_errors(); |
| 307 | $dom->loadXML($xml); |
| 308 | $xml_errors = libxml_get_errors(); |
| 309 | libxml_clear_errors(); |
| 310 | |
| 311 | $xpath = new DOMXPath($dom); |
| 312 | |
| 313 | // Determine XML root element |
| 314 | $main_xml_tag = apply_filters('wp_all_export_main_xml_tag', $exportOptions['main_xml_tag'], XmlExportEngine::$exportID); |
| 315 | $elements = @$xpath->query('/' . $main_xml_tag); |
| 316 | if ($elements->length){ |
| 317 | pmxe_render_xml_element($elements->item( 0 ), true); |
| 318 | $xml_errors = false; |
| 319 | } |
| 320 | else{ |
| 321 | $error_msg = '<strong>' . __('Can\'t preview the document.', 'wp_all_import_plugin') . '</strong><ul>'; |
| 322 | $error_msg .= '<li>'; |
| 323 | $error_msg .= __('You can continue export or try to use <data> tag as root element.', 'wp_all_import_plugin'); |
| 324 | $error_msg .= '</li>'; |
| 325 | $error_msg .= '</ul>'; |
| 326 | echo $error_msg; |
| 327 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 328 | } |
| 329 | break; |
| 330 | |
| 331 | } |
| 332 | |
| 333 | if ( $xml_errors ){ |
| 334 | |
| 335 | $preview_dom = new DOMDocument('1.0', $exportOptions['encoding']); |
| 336 | libxml_clear_errors(); |
| 337 | $preview_dom->loadXML($preview_xml); |
| 338 | $preview_xml_errors = libxml_get_errors(); |
| 339 | libxml_clear_errors(); |
| 340 | |
| 341 | if ($preview_xml_errors){ |
| 342 | $error_msg = '<strong class="error">' . __('Invalid XML', 'wp_all_import_plugin') . '</strong><ul class="error">'; |
| 343 | foreach($preview_xml_errors as $error) { |
| 344 | $error_msg .= '<li>'; |
| 345 | $error_msg .= __('Line', 'wp_all_import_plugin') . ' ' . $error->line . ', '; |
| 346 | $error_msg .= __('Column', 'wp_all_import_plugin') . ' ' . $error->column . ', '; |
| 347 | $error_msg .= __('Code', 'wp_all_import_plugin') . ' ' . $error->code . ': '; |
| 348 | $error_msg .= '<em>' . trim(esc_html($error->message)) . '</em>'; |
| 349 | $error_msg .= '</li>'; |
| 350 | } |
| 351 | $error_msg .= '</ul>'; |
| 352 | echo $error_msg; |
| 353 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 354 | } |
| 355 | else{ |
| 356 | $xpath = new DOMXPath($preview_dom); |
| 357 | if (($elements = @$xpath->query('/Preview')) and $elements->length){ |
| 358 | pmxe_render_xml_element($elements->item( 0 ), true); |
| 359 | } |
| 360 | else{ |
| 361 | $error_msg = '<strong>' . __('Can\'t preview the document. Root element is not detected.', 'wp_all_import_plugin') . '</strong><ul>'; |
| 362 | $error_msg .= '<li>'; |
| 363 | $error_msg .= __('You can continue export or try to use <data> tag as root element.', 'wp_all_import_plugin'); |
| 364 | $error_msg .= '</li>'; |
| 365 | $error_msg .= '</ul>'; |
| 366 | echo $error_msg; |
| 367 | exit( json_encode(array('html' => ob_get_clean())) ); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | break; |
| 373 | |
| 374 | case 'csv': |
| 375 | ?> |
| 376 | <small> |
| 377 | <?php |
| 378 | |
| 379 | $csv = XmlCsvExport::export_csv( true ); |
| 380 | |
| 381 | if (!empty($csv)){ |
| 382 | $csv_rows = array_filter(explode("\n", $csv)); |
| 383 | if ($csv_rows){ |
| 384 | ?> |
| 385 | <table class="pmxe_preview" cellpadding="0" cellspacing="0"> |
| 386 | <?php |
| 387 | foreach ($csv_rows as $rkey => $row) { |
| 388 | $cells = str_getcsv($row, $exportOptions['delimiter']); |
| 389 | if ($cells){ |
| 390 | ?> |
| 391 | <tr> |
| 392 | <?php |
| 393 | foreach ($cells as $key => $value) { |
| 394 | ?> |
| 395 | <td> |
| 396 | <?php if (!$rkey):?><strong><?php endif;?> |
| 397 | <?php echo $value; ?> |
| 398 | <?php if (!$rkey):?></strong><?php endif;?> |
| 399 | </td> |
| 400 | <?php |
| 401 | } |
| 402 | ?> |
| 403 | </tr> |
| 404 | <?php |
| 405 | } |
| 406 | } |
| 407 | ?> |
| 408 | </table> |
| 409 | <?php |
| 410 | } |
| 411 | } |
| 412 | else{ |
| 413 | _e('Data not found.', 'wp_all_export_plugin'); |
| 414 | } |
| 415 | ?> |
| 416 | </small> |
| 417 | <?php |
| 418 | break; |
| 419 | |
| 420 | default: |
| 421 | |
| 422 | _e('This format is not supported.', 'wp_all_export_plugin'); |
| 423 | |
| 424 | break; |
| 425 | } |
| 426 | wp_reset_postdata(); |
| 427 | ?> |
| 428 | |
| 429 | </div> |
| 430 | |
| 431 | </div> |
| 432 | |
| 433 | <?php |
| 434 | |
| 435 | exit(json_encode(array('html' => ob_get_clean()))); die; |
| 436 | } |
| 437 |