PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.1.5
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.1.5
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 8 years ago admin_init.php 8 years ago admin_menu.php 8 years ago admin_notices.php 8 years ago pmxe_after_export.php 8 years ago pmxe_before_export.php 8 years ago pmxe_exported_post.php 8 years ago wp_ajax_dismiss_export_warnings.php 8 years ago wp_ajax_generate_zapier_api_key.php 8 years ago wp_ajax_wpae_available_rules.php 8 years ago wp_ajax_wpae_filtering.php 8 years ago wp_ajax_wpae_filtering_count.php 8 years ago wp_ajax_wpae_preview.php 8 years ago wp_ajax_wpallexport.php 8 years ago wp_loaded.php 8 years ago wpmu_new_blog.php 8 years ago
wp_ajax_wpae_preview.php
448 lines
1 <?php
2 /**
3 * AJAX action for preview export row
4 */
5 function pmxe_wp_ajax_wpae_preview(){
6
7 if ( ! check_ajax_referer( 'wp_all_export_secure', 'security', false )){
8 exit( json_encode(array('html' => __('Security check', 'wp_all_export_plugin'))) );
9 }
10
11 if ( ! current_user_can( PMXE_Plugin::$capabilities ) ){
12 exit( json_encode(array('html' => __('Security check', 'wp_all_export_plugin'))) );
13 }
14
15 XmlExportEngine::$is_preview = true;
16
17 $custom_xml_valid = true;
18
19 ob_start();
20
21 $values = array();
22
23 parse_str($_POST['data'], $values);
24
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 ( 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 $wp_uploads = wp_upload_dir();
210
211 $functions = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_EXPORT_UPLOADS_BASE_DIRECTORY . DIRECTORY_SEPARATOR . 'functions.php';
212 if ( @file_exists($functions) ) {
213 require_once $functions;
214 }
215
216 switch ($exportOptions['export_to']) {
217
218 case 'xml':
219
220 $dom = new DOMDocument('1.0', $exportOptions['encoding']);
221 libxml_use_internal_errors(true);
222 try{
223 $xml = XmlCsvExport::export_xml(true);
224 } catch (WpaeMethodNotFoundException $e) {
225
226 // Find the line where the function is
227 $errorMessage = '';
228 $functionName = $e->getMessage();
229 $txtParts = explode("\n",$originalXmlTemplate);
230 for ($i=0, $length = count($txtParts);$i<$length;$i++)
231 {
232 $tmp = strstr($txtParts[$i], $functionName);
233 if ($tmp) {
234 $errorMessage .= 'Error parsing XML feed: Call to undefined function <em>"'.$functionName.'"</em> on Line '.($i+1);
235 }
236 }
237
238 $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>';
239 echo $error_msg;
240 exit( json_encode(array('html' => ob_get_clean())) );
241 } catch (WpaeInvalidStringException $e) {
242
243 // Find the line where the function is
244 $errorMessage = '';
245 $functionName = $e->getMessage();
246 $txtParts = explode("\n",$originalXmlTemplate);
247 for ($i=0, $length = count($txtParts);$i<$length;$i++)
248 {
249 $tmp = strstr($txtParts[$i], $functionName);
250 if ($tmp) {
251 $errorMessage .= 'Error parsing XML feed: Unterminated string on line '.($i+1);
252 }
253 }
254
255 $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>';
256 echo $error_msg;
257 exit( json_encode(array('html' => ob_get_clean())) );
258 } catch (WpaeTooMuchRecursionException $e) {
259 $errorMessage = __('There was a problem parsing the custom XML template');
260 $error_msg = '<span class="error">'.__($errorMessage, 'wp_all_import_plugin').'</span>';
261 echo $error_msg;
262 exit( json_encode(array('html' => ob_get_clean())) );
263
264 }
265
266 $xml_errors = false;
267
268 $main_xml_tag = '';
269
270 switch ( XmlExportEngine::$exportOptions['xml_template_type'] ){
271
272 case 'custom':
273 case 'XmlGoogleMerchants':
274
275 require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php';
276
277 $preview_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n<Preview>\n" . $xml . "\n</Preview>";
278
279 $preview_xml = str_replace('<![CDATA[', 'CDATABEGIN', $preview_xml);
280 $preview_xml = str_replace(']]>', 'CDATACLOSE', $preview_xml);
281 $preview_xml = str_replace('&amp;', '&', $preview_xml);
282 $preview_xml = str_replace('&', '&amp;', $preview_xml);
283
284 $xml = PMXE_XMLWriter::preprocess_xml( XmlExportEngine::$exportOptions['custom_xml_template_header'] ) . "\n" . $xml . "\n" . PMXE_XMLWriter::preprocess_xml( XmlExportEngine::$exportOptions['custom_xml_template_footer'] );
285
286 $xml = str_replace('<![CDATA[', 'CDATABEGIN', $xml);
287 $xml = str_replace(']]>', 'CDATACLOSE', $xml);
288 $xml = str_replace('&amp;', '&', $xml);
289 $xml = str_replace('&', '&amp;', $xml);
290
291 // Determine XML root element
292 preg_match_all("%<[\w]+[\s|>]{1}%", XmlExportEngine::$exportOptions['custom_xml_template_header'], $matches);
293
294 if ( ! empty($matches[0]) ){
295 $main_xml_tag = preg_replace("%[\s|<|>]%","",array_shift($matches[0]));
296 }
297
298 libxml_clear_errors();
299 $dom->loadXML($xml);
300 $xml_errors = libxml_get_errors();
301 libxml_clear_errors();
302 if (! $xml_errors ){
303 $xpath = new DOMXPath($dom);
304 if (($elements = @$xpath->query('/' . $main_xml_tag)) and $elements->length){
305 pmxe_render_xml_element($elements->item( 0 ), true);
306 }
307 else{
308 $xml_errors = true;
309 }
310 }
311
312 break;
313
314 default:
315
316 libxml_clear_errors();
317 $dom->loadXML($xml);
318 $xml_errors = libxml_get_errors();
319 libxml_clear_errors();
320
321 $xpath = new DOMXPath($dom);
322
323 // Determine XML root element
324 $main_xml_tag = apply_filters('wp_all_export_main_xml_tag', $exportOptions['main_xml_tag'], XmlExportEngine::$exportID);
325 $elements = @$xpath->query('/' . $main_xml_tag);
326 if ($elements->length){
327 pmxe_render_xml_element($elements->item( 0 ), true);
328 $xml_errors = false;
329 }
330 else{
331 $error_msg = '<strong>' . __('Can\'t preview the document.', 'wp_all_import_plugin') . '</strong><ul>';
332 $error_msg .= '<li>';
333 $error_msg .= __('You can continue export or try to use &lt;data&gt; tag as root element.', 'wp_all_import_plugin');
334 $error_msg .= '</li>';
335 $error_msg .= '</ul>';
336 echo $error_msg;
337 exit( json_encode(array('html' => ob_get_clean())) );
338 }
339
340 break;
341
342 }
343
344 if ( $xml_errors ){
345
346 $preview_dom = new DOMDocument('1.0', $exportOptions['encoding']);
347 libxml_clear_errors();
348 $preview_dom->loadXML($preview_xml);
349 $preview_xml_errors = libxml_get_errors();
350 libxml_clear_errors();
351
352 if ($preview_xml_errors){
353 $error_msg = '<strong class="error">' . __('Invalid XML', 'wp_all_import_plugin') . '</strong><ul class="error">';
354 foreach($preview_xml_errors as $error) {
355 $error_msg .= '<li>';
356 $error_msg .= __('Line', 'wp_all_import_plugin') . ' ' . $error->line . ', ';
357 $error_msg .= __('Column', 'wp_all_import_plugin') . ' ' . $error->column . ', ';
358 $error_msg .= __('Code', 'wp_all_import_plugin') . ' ' . $error->code . ': ';
359 $error_msg .= '<em>' . trim(esc_html($error->message)) . '</em>';
360 $error_msg .= '</li>';
361 }
362 $error_msg .= '</ul>';
363 echo $error_msg;
364 exit( json_encode(array('html' => ob_get_clean())) );
365 }
366 else{
367 $xpath = new DOMXPath($preview_dom);
368 if (($elements = @$xpath->query('/Preview')) and $elements->length){
369 pmxe_render_xml_element($elements->item( 0 ), true);
370 }
371 else{
372 $error_msg = '<strong>' . __('Can\'t preview the document. Root element is not detected.', 'wp_all_import_plugin') . '</strong><ul>';
373 $error_msg .= '<li>';
374 $error_msg .= __('You can continue export or try to use &lt;data&gt; tag as root element.', 'wp_all_import_plugin');
375 $error_msg .= '</li>';
376 $error_msg .= '</ul>';
377 echo $error_msg;
378 exit( json_encode(array('html' => ob_get_clean())) );
379 }
380 }
381 }
382
383 break;
384
385 case 'csv':
386 ?>
387 <small>
388 <?php
389
390 $csv = XmlCsvExport::export_csv( true );
391
392 if (!empty($csv)){
393 $csv_rows = array_filter(explode("\n", $csv));
394 if ($csv_rows){
395 ?>
396 <table class="pmxe_preview" cellpadding="0" cellspacing="0">
397 <?php
398 foreach ($csv_rows as $rkey => $row) {
399 $cells = str_getcsv($row, $exportOptions['delimiter']);
400 if ($cells){
401 ?>
402 <tr>
403 <?php
404 foreach ($cells as $key => $value) {
405 ?>
406 <td>
407 <?php if (!$rkey):?><strong><?php endif;?>
408 <?php echo $value; ?>
409 <?php if (!$rkey):?></strong><?php endif;?>
410 </td>
411 <?php
412 }
413 ?>
414 </tr>
415 <?php
416 }
417 }
418 ?>
419 </table>
420 <?php
421 }
422 }
423 else{
424 _e('Data not found.', 'wp_all_export_plugin');
425 }
426 ?>
427 </small>
428 <?php
429 break;
430
431 default:
432
433 _e('This format is not supported.', 'wp_all_export_plugin');
434
435 break;
436 }
437 wp_reset_postdata();
438 ?>
439
440 </div>
441
442 </div>
443
444 <?php
445
446 exit(json_encode(array('html' => ob_get_clean()))); die;
447 }
448