PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.3.6
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.3.6
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / actions / wp_ajax_wpae_preview.php
wp-all-export / actions Last commit date
admin_head.php 4 years ago admin_init.php 6 years ago admin_menu.php 5 years ago admin_notices.php 4 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_review_modal.php 4 years ago wp_ajax_dismiss_warnings.php 5 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 4 years ago wp_ajax_send_feedback.php 4 years ago wp_ajax_wpae_available_rules.php 4 years ago wp_ajax_wpae_filtering.php 4 years ago wp_ajax_wpae_filtering_count.php 4 years ago wp_ajax_wpae_preview.php 4 years ago wp_ajax_wpae_upgrade_notice.php 4 years ago wp_ajax_wpallexport.php 4 years ago wp_loaded.php 4 years ago wpmu_new_blog.php 10 years ago
wp_ajax_wpae_preview.php
452 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 wp_kses_post($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
168 if(strpos($exportOptions['cpt'][0], 'custom_') === 0) {
169 $addon = GF_Export_Add_On::get_instance();
170
171 $filter_args = array(
172 'filter_rules_hierarhy' => empty($exportOptions['filter_rules_hierarhy']) ? array() : $exportOptions['filter_rules_hierarhy'],
173 'product_matching_mode' => empty($exportOptions['product_matching_mode']) ? 'strict' : $exportOptions['product_matching_mode'],
174 'taxonomy_to_export' => empty($exportOptions['taxonomy_to_export']) ? '' : $exportOptions['taxonomy_to_export'],
175 'sub_post_type_to_export' => empty($exportOptions['sub_post_type_to_export']) ? '' : $exportOptions['sub_post_type_to_export']
176 );
177
178 $exportQuery = $addon->add_on->get_query(0, 0, $filter_args);
179 } else {
180
181 remove_all_actions('parse_query');
182 remove_all_actions('pre_get_posts');
183 remove_all_filters('posts_clauses');
184
185 add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
186 add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
187 $exportQuery = new WP_Query(array('post_type' => $exportOptions['cpt'], 'post_status' => 'any', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 10));
188
189 remove_filter('posts_where', 'wp_all_export_posts_where');
190 remove_filter('posts_join', 'wp_all_export_posts_join');
191 }
192 }
193 }
194
195 XmlExportEngine::$exportQuery = $exportQuery;
196
197 $engine->init_additional_data();
198
199 ?>
200
201 <div id="post-preview" class="wpallexport-preview">
202
203 <p class="wpallexport-preview-title"><?php echo sprintf("Preview first 10 %s", esc_html(wp_all_export_get_cpt_name($exportOptions['cpt'], 10, $exportOptions))); ?></p>
204
205 <div class="wpallexport-preview-content">
206
207 <?php
208
209 if(!$custom_xml_valid) {
210 $error_msg = '<strong class="error">' . __('Invalid XML', 'wp_all_import_plugin') . '</strong><ul class="error">';
211 foreach($custom_xml_template_errors as $error) {
212 $error_msg .= '<li>';
213 $error_msg .= __('Line', 'wp_all_import_plugin') . ' ' . ($error->line + $line_difference) . ', ';
214 $error_msg .= __('Column', 'wp_all_import_plugin') . ' ' . $error->column . ', ';
215 $error_msg .= __('Code', 'wp_all_import_plugin') . ' ' . $error->code . ': ';
216 $error_msg .= '<em>' . trim(esc_html($error->message)) . '</em>';
217 $error_msg .= '</li>';
218 }
219 $error_msg .= '</ul>';
220 echo wp_kses_post($error_msg);
221 exit( json_encode(array('html' => ob_get_clean())) );
222 }
223
224 switch ($exportOptions['export_to']) {
225
226 case 'xml':
227
228 $dom = new DOMDocument('1.0', $exportOptions['encoding']);
229 libxml_use_internal_errors(true);
230 try{
231 $xml = XmlCsvExport::export_xml(true);
232 } catch (WpaeMethodNotFoundException $e) {
233 // Find the line where the function is
234 $errorMessage = '';
235 $functionName = $e->getMessage();
236 $txtParts = explode("\n",$originalXmlTemplate);
237 for ($i=0, $length = count($txtParts);$i<$length;$i++)
238 {
239 $tmp = strstr($txtParts[$i], $functionName);
240 if ($tmp) {
241 $errorMessage .= 'Error parsing XML feed: Call to undefined function <em>"'.$functionName.'"</em> on Line '.($i+1);
242 }
243 }
244
245 $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>';
246 echo wp_kses_post($error_msg);
247 exit( json_encode(array('html' => ob_get_clean())) );
248 } catch (WpaeInvalidStringException $e) {
249 // Find the line where the function is
250 $errorMessage = '';
251 $functionName = $e->getMessage();
252 $txtParts = explode("\n",$originalXmlTemplate);
253 for ($i=0, $length = count($txtParts);$i<$length;$i++)
254 {
255 $tmp = strstr($txtParts[$i], $functionName);
256 if ($tmp) {
257 $errorMessage .= 'Error parsing XML feed: Unterminated string on line '.($i+1);
258 }
259 }
260
261 $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>';
262 echo wp_kses_post($error_msg);
263 exit( json_encode(array('html' => ob_get_clean())) );
264 } catch (WpaeTooMuchRecursionException $e) {
265 $errorMessage = __('There was a problem parsing the custom XML template');
266 $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>';
267 echo wp_kses_post($error_msg);
268 exit( json_encode(array('html' => ob_get_clean())) );
269 }
270
271 $xml_errors = false;
272
273 $main_xml_tag = '';
274
275 switch ( XmlExportEngine::$exportOptions['xml_template_type'] ){
276
277 case 'custom':
278 case 'XmlGoogleMerchants':
279
280 require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php';
281
282 $preview_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n<Preview>\n" . $xml . "\n</Preview>";
283
284 $preview_xml = str_replace('<![CDATA[', 'CDATABEGIN', $preview_xml);
285 $preview_xml = str_replace(']]>', 'CDATACLOSE', $preview_xml);
286 $preview_xml = str_replace('&amp;', '&', $preview_xml);
287 $preview_xml = str_replace('&', '&amp;', $preview_xml);
288
289 $xml = PMXE_XMLWriter::preprocess_xml( XmlExportEngine::$exportOptions['custom_xml_template_header'] ) . "\n" . $xml . "\n" . PMXE_XMLWriter::preprocess_xml( XmlExportEngine::$exportOptions['custom_xml_template_footer'] );
290
291 $xml = str_replace('<![CDATA[', 'CDATABEGIN', $xml);
292 $xml = str_replace(']]>', 'CDATACLOSE', $xml);
293 $xml = str_replace('&amp;', '&', $xml);
294 $xml = str_replace('&', '&amp;', $xml);
295
296 // Determine XML root element
297 preg_match_all("%<[\w]+[\s|>]{1}%", XmlExportEngine::$exportOptions['custom_xml_template_header'], $matches);
298
299 if ( ! empty($matches[0]) ){
300 $main_xml_tag = preg_replace("%[\s|<|>]%","",array_shift($matches[0]));
301 }
302
303 libxml_clear_errors();
304 $dom->loadXML($xml);
305 $xml_errors = libxml_get_errors();
306 libxml_clear_errors();
307 if (! $xml_errors ){
308 $xpath = new DOMXPath($dom);
309 if (($elements = @$xpath->query('/' . $main_xml_tag)) and $elements->length){
310 pmxe_render_xml_element($elements->item( 0 ), true);
311 }
312 else{
313 $xml_errors = true;
314 }
315 }
316
317 break;
318
319 default:
320
321 libxml_clear_errors();
322 $dom->loadXML($xml);
323 $xml_errors = libxml_get_errors();
324 libxml_clear_errors();
325
326 $xpath = new DOMXPath($dom);
327
328 // Determine XML root element
329 $main_xml_tag = apply_filters('wp_all_export_main_xml_tag', $exportOptions['main_xml_tag'], XmlExportEngine::$exportID);
330 $elements = @$xpath->query('/' . $main_xml_tag);
331 if ($elements->length){
332 pmxe_render_xml_element($elements->item( 0 ), true);
333 $xml_errors = false;
334 }
335 else{
336 $error_msg = '<strong>' . __('Can\'t preview the document.', 'wp_all_import_plugin') . '</strong><ul>';
337 $error_msg .= '<li>';
338 $error_msg .= __('You can continue export or try to use &lt;data&gt; tag as root element.', 'wp_all_import_plugin');
339 $error_msg .= '</li>';
340 $error_msg .= '</ul>';
341 echo wp_kses_post($error_msg);
342 exit( json_encode(array('html' => ob_get_clean())) );
343 }
344 break;
345
346 }
347
348 if ( $xml_errors ){
349
350 $preview_dom = new DOMDocument('1.0', $exportOptions['encoding']);
351 libxml_clear_errors();
352 $preview_dom->loadXML($preview_xml);
353 $preview_xml_errors = libxml_get_errors();
354 libxml_clear_errors();
355
356 if ($preview_xml_errors){
357 $error_msg = '<strong class="error">' . __('Invalid XML', 'wp_all_import_plugin') . '</strong><ul class="error">';
358 foreach($preview_xml_errors as $error) {
359 $error_msg .= '<li>';
360 $error_msg .= __('Line', 'wp_all_import_plugin') . ' ' . $error->line . ', ';
361 $error_msg .= __('Column', 'wp_all_import_plugin') . ' ' . $error->column . ', ';
362 $error_msg .= __('Code', 'wp_all_import_plugin') . ' ' . $error->code . ': ';
363 $error_msg .= '<em>' . trim(esc_html($error->message)) . '</em>';
364 $error_msg .= '</li>';
365 }
366 $error_msg .= '</ul>';
367 echo wp_kses_post($error_msg);
368 exit( json_encode(array('html' => ob_get_clean())) );
369 }
370 else{
371 $xpath = new DOMXPath($preview_dom);
372 if (($elements = @$xpath->query('/Preview')) and $elements->length){
373 pmxe_render_xml_element($elements->item( 0 ), true);
374 }
375 else{
376 $error_msg = '<strong>' . __('Can\'t preview the document. Root element is not detected.', 'wp_all_import_plugin') . '</strong><ul>';
377 $error_msg .= '<li>';
378 $error_msg .= __('You can continue export or try to use &lt;data&gt; tag as root element.', 'wp_all_import_plugin');
379 $error_msg .= '</li>';
380 $error_msg .= '</ul>';
381 echo wp_kses_post($error_msg);
382 exit( json_encode(array('html' => ob_get_clean())) );
383 }
384 }
385 }
386
387 break;
388
389 case 'csv':
390 ?>
391 <small>
392 <?php
393
394 $csv = XmlCsvExport::export_csv( true );
395
396 if (!empty($csv)){
397 $csv_rows = array_filter(explode("\n", $csv));
398 if ($csv_rows){
399 ?>
400 <table class="pmxe_preview" cellpadding="0" cellspacing="0">
401 <?php
402 foreach ($csv_rows as $rkey => $row) {
403 $cells = str_getcsv($row, $exportOptions['delimiter']);
404 if ($cells){
405 ?>
406 <tr>
407 <?php
408 foreach ($cells as $key => $value) {
409 ?>
410 <td>
411 <?php if (!$rkey):?><strong><?php endif;?>
412 <?php echo esc_html($value); ?>
413 <?php if (!$rkey):?></strong><?php endif;?>
414 </td>
415 <?php
416 }
417 ?>
418 </tr>
419 <?php
420 }
421 }
422 ?>
423 </table>
424 <?php
425 }
426 }
427 else{
428 _e('Data not found.', 'wp_all_export_plugin');
429 }
430 ?>
431 </small>
432 <?php
433 break;
434
435 default:
436
437 _e('This format is not supported.', 'wp_all_export_plugin');
438
439 break;
440 }
441 wp_reset_postdata();
442 ?>
443
444 </div>
445
446 </div>
447
448 <?php
449
450 exit(json_encode(array('html' => ob_get_clean()))); die;
451 }
452