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 / controllers / admin / export.php
wp-all-export / controllers / admin Last commit date
export.php 4 years ago feedback.php 10 years ago help.php 12 years ago manage.php 4 years ago settings.php 4 years ago
export.php
582 lines
1 <?php
2
3 /**
4 * Export configuration wizard
5 *
6 * @author Max Tsiplyakov <makstsiplyakov@gmail.com>
7 */
8 class PMXE_Admin_Export extends PMXE_Controller_Admin
9 {
10
11 protected $isWizard = true; // indicates whether controller is in wizard mode (otherwise it called to be delegated an edit action)
12
13 protected function init()
14 {
15
16 parent::init();
17
18 if ('PMXE_Admin_Manage' == PMXE_Plugin::getInstance()->getAdminCurrentScreen()->base) { // prereqisites are not checked when flow control is deligated
19 $id = $this->input->get('id');
20 $this->data['export'] = $export = new PMXE_Export_Record();
21 if (!$id or $export->getById($id)->isEmpty()) { // specified import is not found
22 wp_redirect(esc_url_raw(add_query_arg('page', 'pmxe-admin-manage', admin_url('admin.php'))));
23 die();
24 }
25 $this->isWizard = false;
26 $export->fix_template_options();
27 } else {
28 $action = PMXE_Plugin::getInstance()->getAdminCurrentScreen()->action;
29 $this->_step_ready($action);
30 }
31
32 // preserve id parameter as part of baseUrl
33 $id = $this->input->get('id') and $this->baseUrl = esc_url_raw(add_query_arg('id', $id, $this->baseUrl));
34
35 }
36
37 public function set($var, $val)
38 {
39 $this->{$var} = $val;
40 }
41
42 public function get($var)
43 {
44 return $this->{$var};
45 }
46
47 /**
48 * Checks whether corresponding step of wizard is complete
49 * @param string $action
50 * @return bool
51 */
52 protected function _step_ready($action)
53 {
54
55 // step #1: xml selction - has no prerequisites
56 if ('index' == $action) return true;
57
58 if ('element' == $action) return true;
59
60 $this->data['update_previous'] = $update_previous = new PMXE_Export_Record();
61
62 $update_previous->getById(PMXE_Plugin::$session->update_previous);
63
64 if (!$update_previous->isEmpty()) {
65 $update_previous->fix_template_options();
66 }
67
68 if ('options' == $action) return true;
69
70 if (!PMXE_Plugin::$session->has_session()) {
71 wp_redirect_or_javascript($this->baseUrl);
72 die();
73 }
74
75 if ('process' == $action) return true;
76
77 }
78
79 /**
80 * Step #1: Choose CPT
81 */
82 public function index()
83 {
84
85 $action = $this->input->get('action');
86
87 $DefaultOptions = array(
88 'cpt' => '',
89 'export_to' => 'xml',
90 'export_type' => 'specific',
91 'wp_query' => '',
92 'filter_rules_hierarhy' => '',
93 'product_matching_mode' => 'strict',
94 'wp_query_selector' => 'wp_query',
95 'auto_generate' => 0,
96 'taxonomy_to_export' => '',
97 'sub_post_type_to_export' => '',
98 'created_at_version' => PMXE_VERSION
99 );
100
101 if (!in_array($action, array('index'))) {
102 PMXE_Plugin::$session->clean_session();
103 $this->data['preload'] = false;
104 } else {
105 $DefaultOptions = (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + $DefaultOptions;
106 $this->data['preload'] = true;
107 }
108
109 $this->data['post'] = $post = $this->input->post($DefaultOptions);
110
111 if (is_array($this->data['post']['cpt'])) $this->data['post']['cpt'] = $this->data['post']['cpt'][0];
112
113 // Delete history
114 if(is_dir(PMXE_ROOT_DIR.'/history')) {
115 $history_files = PMXE_Helper::safe_glob(PMXE_ROOT_DIR . '/history/*', PMXE_Helper::GLOB_RECURSE | PMXE_Helper::GLOB_PATH);
116 if (!empty($history_files)) {
117 foreach ($history_files as $filePath) {
118 @file_exists($filePath) and @unlink($filePath);
119 }
120 }
121 }
122
123 if (!class_exists('XMLReader') or !class_exists('XMLWriter')) {
124 $this->errors->add('form-validation', __('Required PHP components are missing.<br/><br/>WP All Export requires XMLReader, and XMLWriter PHP modules to be installed.<br/>These are standard features of PHP, and are necessary for WP All Export to write the files you are trying to export.<br/>Please contact your web hosting provider and ask them to install and activate the DOMDocument, XMLReader, and XMLWriter PHP modules.', 'wp_all_export_plugin'));
125 }
126
127 if ($this->input->post('is_submitted')) {
128
129 PMXE_Plugin::$session->set('export_type', $post['export_type']);
130 PMXE_Plugin::$session->set('filter_rules_hierarhy', $post['filter_rules_hierarhy']);
131 PMXE_Plugin::$session->set('product_matching_mode', $post['product_matching_mode']);
132 PMXE_Plugin::$session->set('wp_query_selector', $post['wp_query_selector']);
133 PMXE_Plugin::$session->set('taxonomy_to_export', $post['taxonomy_to_export']);
134 PMXE_Plugin::$session->set('created_at_version', $post['created_at_version']);
135 PMXE_Plugin::$session->set('sub_post_type_to_export', $post['sub_post_type_to_export']);
136
137 if (!empty($post['auto_generate'])) {
138 $auto_generate = XmlCsvExport::auto_generate_export_fields($post, $this->errors);
139
140 foreach ($auto_generate as $key => $value) {
141 PMXE_Plugin::$session->set($key, $value);
142 }
143
144 PMXE_Plugin::$session->save_data();
145 } else {
146 $engine = new XmlExportEngine($post, $this->errors);
147 $engine->init_additional_data();
148 }
149 }
150
151 if ($this->input->post('is_submitted') and !$this->errors->get_error_codes()) {
152
153 check_admin_referer('choose-cpt', '_wpnonce_choose-cpt');
154
155 PMXE_Plugin::$session->save_data();
156
157 if (!empty($post['auto_generate'])) {
158 wp_redirect(esc_url_raw(add_query_arg('action', 'options', $this->baseUrl)));
159 die();
160 } else {
161 wp_redirect(esc_url_raw(add_query_arg('action', 'template', $this->baseUrl)));
162 die();
163 }
164
165 }
166
167 $this->render();
168 }
169
170 /**
171 * Step #2: Export Template
172 */
173 public function template()
174 {
175
176 $template = new PMXE_Template_Record();
177
178 $default = PMXE_Plugin::get_default_import_options();
179
180 $this->data['dismiss_warnings'] = 0;
181
182 if ($this->isWizard) {
183 // New export
184 $DefaultOptions = (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + $default;
185 $post = $this->input->post($DefaultOptions);
186 } else {
187 // Edit export
188 $DefaultOptions = $this->data['export']->options + $default;
189
190 if (empty($this->data['export']->options['export_variations'])) {
191 $DefaultOptions['export_variations'] = XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_PARENT_AND_VARIATION;
192 }
193 if (empty($this->data['export']->options['export_variations_title'])) {
194 $DefaultOptions['export_variations_title'] = XmlExportEngine::VARIATION_USE_DEFAULT_TITLE;
195 }
196 $post = $this->input->post($DefaultOptions);
197 $post['scheduled'] = $this->data['export']->scheduled;
198
199 foreach ($post as $key => $value) {
200 PMXE_Plugin::$session->set($key, $value);
201 }
202 $this->data['dismiss_warnings'] = get_option('wpae_dismiss_warnings_' . $this->data['export']->id, 0);
203 }
204
205 $max_input_vars = @ini_get('max_input_vars');
206
207 if (ctype_digit($max_input_vars) && count($_POST, COUNT_RECURSIVE) >= $max_input_vars) {
208 $this->errors->add('form-validation', sprintf(__('You\'ve reached your max_input_vars limit of %d. Please contact your web host to increase it.', 'wp_all_export_plugin'), $max_input_vars));
209 }
210
211 PMXE_Plugin::$session->save_data();
212
213 $this->data['post'] =& $post;
214
215 PMXE_Plugin::$session->set('is_loaded_template', '');
216
217 $this->data['engine'] = null;
218
219 XmlExportEngine::$exportQuery = PMXE_Plugin::$session->get('exportQuery');
220
221 if (($load_template = $this->input->post('load_template'))) { // init form with template selected
222 if (!$template->getById($load_template)->isEmpty()) {
223 $template_options = $template->options;
224 unset($template_options['cpt']);
225 unset($template_options['wp_query']);
226 unset($template_options['filter_rules_hierarhy']);
227 unset($template_options['product_matching_mode']);
228 unset($template_options['wp_query_selector']);
229 $this->data['post'] = array_merge($post, $template_options);
230 PMXE_Plugin::$session->set('is_loaded_template', $load_template);
231 }
232
233 } elseif ($this->input->post('is_submitted')) {
234 check_admin_referer('template', '_wpnonce_template');
235
236 if (empty($post['cc_type'][0]) && !in_array($post['xml_template_type'], array('custom', 'XmlGoogleMerchants'))) {
237 $this->errors->add('form-validation', __('You haven\'t selected any columns for export.', 'wp_all_export_plugin'));
238 }
239
240 if ('csv' == $post['export_to'] and '' == $post['delimiter']) {
241 $this->errors->add('form-validation', __('CSV delimiter must be specified.', 'wp_all_export_plugin'));
242 }
243
244 if ('xml' == $post['export_to'] && !in_array($post['xml_template_type'], array('custom', 'XmlGoogleMerchants'))) {
245 $post['main_xml_tag'] = preg_replace('/[^a-z0-9_]/i', '', $post['main_xml_tag']);
246 if (empty($post['main_xml_tag'])) {
247 $this->errors->add('form-validation', __('Main XML Tag is required.', 'wp_all_export_plugin'));
248 }
249
250 $post['record_xml_tag'] = preg_replace('/[^a-z0-9_]/i', '', $post['record_xml_tag']);
251 if (empty($post['record_xml_tag'])) {
252 $this->errors->add('form-validation', __('Single Record XML Tag is required.', 'wp_all_export_plugin'));
253 }
254
255 if ($post['main_xml_tag'] == $post['record_xml_tag']) {
256 $this->errors->add('form-validation', __('Main XML Tag equals to Single Record XML Tag.', 'wp_all_export_plugin'));
257 }
258 }
259
260 if (($post['export_to'] == XmlExportEngine::EXPORT_TYPE_XML) && in_array($post['xml_template_type'], array('custom', 'XmlGoogleMerchants'))) {
261
262 if (empty($post['custom_xml_template'])) {
263 $this->errors->add('form-validation', __('XML template is empty.', 'wp_all_export_plugin'));
264 }
265
266 // Convert Custom XML template to default
267 if (!empty($post['custom_xml_template'])) {
268
269 $post['custom_xml_template'] = str_replace('<ID>', '<id>', $post['custom_xml_template']);
270 $post['custom_xml_template'] = str_replace('</ID>', '</id>', $post['custom_xml_template']);
271
272 $post['custom_xml_template'] = str_replace("<!-- BEGIN POST LOOP -->", "<!-- BEGIN LOOP -->", $post['custom_xml_template']);
273 $post['custom_xml_template'] = str_replace("<!-- END POST LOOP -->", "<!-- END LOOP -->", $post['custom_xml_template']);
274
275 $this->data['engine'] = new XmlExportEngine($post, $this->errors);
276
277 $this->data['engine']->init_additional_data();
278
279 $this->data = array_merge($this->data, $this->data['engine']->init_available_data());
280
281 $result = $this->data['engine']->parse_custom_xml_template();
282
283 if (!$this->errors->get_error_codes()) {
284 $post = array_merge($post, $result);
285 }
286 }
287 }
288
289 if (!$this->errors->get_error_codes()) {
290
291 if (!empty($post['name']) and !empty($post['save_template_as'])) { // save template in database
292 $template->getByName($post['name'])->set(array(
293 'name' => $post['name'],
294 'options' => $post
295 ))->save();
296 PMXE_Plugin::$session->set('saved_template', $template->id);
297 }
298
299 if ($this->isWizard) {
300 foreach ($this->data['post'] as $key => $value) {
301 PMXE_Plugin::$session->set($key, $value);
302 }
303 PMXE_Plugin::$session->save_data();
304 wp_redirect(esc_url_raw(add_query_arg('action', 'options', $this->baseUrl)));
305 die();
306 } else {
307 $this->data['export']->set(array('options' => $post, 'settings_update_on' => date('Y-m-d H:i:s')))->save();
308
309 if (!empty($post['friendly_name'])) {
310 $this->data['export']->set(array('friendly_name' => $post['friendly_name'], 'scheduled' => (($post['is_scheduled']) ? $post['scheduled_period'] : '')))->save();
311 }
312
313 wp_redirect(esc_url_raw(add_query_arg(array('page' => 'pmxe-admin-manage', 'pmxe_nt' => urlencode(__('Options updated', 'pmxi_plugin'))) + array_intersect_key($_GET, array_flip($this->baseUrlParamNames)), admin_url('admin.php'))));
314 die();
315 }
316 }
317 }
318
319 if (empty($this->data['engine'])) {
320
321 $this->data['engine'] = new XmlExportEngine($post, $this->errors);
322
323 $this->data['engine']->init_additional_data();
324
325 $this->data = array_merge($this->data, $this->data['engine']->init_available_data());
326 }
327
328 $this->data['available_data_view'] = $this->data['engine']->render();
329
330 $this->data['available_fields_view'] = $this->data['engine']->render_new_field();
331
332 if (class_exists('SitePress')) {
333 global $sitepress;
334 $langs = $sitepress->get_active_languages();
335 if (!empty($langs)) {
336 // prepare active languages list
337 $language_list = array('all' => 'All');
338 foreach ($langs as $code => $langInfo) {
339 $language_list[$code] = "<img width='18' height='12' src='" . esc_attr($sitepress->get_flag_url($code)) . "' style='position:relative; top: 2px;'/> " . esc_html($langInfo['display_name']);
340 if(isset($this->default_language)){
341 if ($code == $this->default_language) $language_list[$code] .= ' ( <strong>default</strong> )';
342 }
343
344 }
345 }
346 $this->data['wpml_options'] = $language_list;
347 }
348
349 $this->render();
350 }
351
352 /**
353 * Step #3: Export Options
354 */
355 public function options()
356 {
357 $default = PMXE_Plugin::get_default_import_options();
358
359 if ($this->isWizard) {
360
361 $DefaultOptions = (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + $default;
362 $post = $this->input->post($DefaultOptions);
363
364 if(isset($post['update_previous'])) {
365 $exportId = $post['update_previous'];
366 } else {
367 $exportId = false;
368 }
369
370 if(!$exportId) {
371 $export = $this->data['update_previous'];
372 $export->set(
373 array(
374 'triggered' => 0,
375 'processing' => 0,
376 'exported' => 0,
377 'executing' => 0,
378 'canceled' => 0,
379 'options' => $post,
380 'friendly_name' => $this->getFriendlyName($post),
381 'last_activity' => date('Y-m-d H:i:s')
382
383 )
384 )->save();
385
386 PMXE_Plugin::$session->set('update_previous', $export->id);
387 PMXE_Plugin::$session->set('friendly_name', $this->getFriendlyName($post));
388 PMXE_Plugin::$session->save_data();
389 $exportId = $export->id;
390 }
391 $this->data['export_id'] = $exportId;
392 $this->data['export'] = new PMXE_Export_Record();
393 $this->data['export'] = $this->data['export']->getBy('id', $exportId);
394
395 if(empty($post['friendly_name'])) {
396 $post['friendly_name'] = $this->getFriendlyName($post);
397 }
398 }
399 else {
400 $DefaultOptions = $this->data['export']->options + $default;
401 if (empty($this->data['export']->options['export_variations'])) {
402 $DefaultOptions['export_variations'] = XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_PARENT_AND_VARIATION;
403 }
404 if (empty($this->data['export']->options['export_variations_title'])) {
405 $DefaultOptions['export_variations_title'] = XmlExportEngine::VARIATION_USE_DEFAULT_TITLE;
406 }
407 $post = $this->input->post($DefaultOptions);
408 $post['scheduled'] = $this->data['export']->scheduled;
409
410 foreach ($post as $key => $value) {
411 PMXE_Plugin::$session->set($key, $value);
412 }
413 PMXE_Plugin::$session->save_data();
414 $this->data['export_id'] = $this->data['export']->id;
415 }
416
417 $this->data['engine'] = new XmlExportEngine($post, $this->errors);
418
419 $this->data['engine']->init_available_data();
420
421 $this->data['post'] =& $post;
422
423 if ($this->input->post('is_submitted')) {
424
425 check_admin_referer('options', '_wpnonce_options');
426
427 if ($post['is_generate_templates'] and '' == $post['template_name']) {
428 $friendly_name = $this->getFriendlyName($post);
429 $post['template_name'] = $friendly_name;
430 }
431
432 if ($this->isWizard) {
433 if (!$this->errors->get_error_codes()) {
434 foreach ($this->data['post'] as $key => $value) {
435 PMXE_Plugin::$session->set($key, $value);
436 }
437 PMXE_Plugin::$session->save_data();
438 wp_redirect(esc_url_raw(add_query_arg('action', 'process', $this->baseUrl)));
439 die();
440 }
441 } else {
442 $this->errors->remove('count-validation');
443 if (!$this->errors->get_error_codes()) {
444 $this->data['export']->set(array('options' => $post, 'settings_update_on' => date('Y-m-d H:i:s')))->save();
445 if (!empty($post['friendly_name'])) {
446 $this->data['export']->set(array('friendly_name' => $post['friendly_name'], 'scheduled' => (($post['is_scheduled']) ? $post['scheduled_period'] : '')))->save();
447 }
448 wp_redirect(esc_url_raw(add_query_arg(array('page' => 'pmxe-admin-manage', 'pmxe_nt' => urlencode(__('Options updated', 'wp_all_export_plugin'))) + array_intersect_key($_GET, array_flip($this->baseUrlParamNames)), admin_url('admin.php'))));
449 die();
450 }
451 }
452 }
453
454 $this->render();
455 }
456
457 /**
458 * Step #4: Export Processing
459 */
460 public function process()
461 {
462 @set_time_limit(0);
463
464 $export = $this->data['update_previous'];
465
466 if (!PMXE_Plugin::is_ajax()) {
467
468 if ("" == PMXE_Plugin::$session->friendly_name) {
469
470 $post_types = PMXE_Plugin::$session->get('cpt');
471 if (!empty($post_types)) {
472 if (in_array('users', $post_types)) {
473 $friendly_name = 'Users Export - ' . date("Y F d H:i");
474 } elseif (in_array('shop_customer', $post_types)) {
475 $friendly_name = 'Customers Export - ' . date("Y F d H:i");
476 } elseif (in_array('comments', $post_types)) {
477 $friendly_name = 'Comments Export - ' . date("Y F d H:i");
478 } elseif (in_array('taxonomies', $post_types)) {
479 $tx = get_taxonomy(PMXE_Plugin::$session->get('taxonomy_to_export'));
480 if (!empty($tx->labels->name)) {
481 $friendly_name = $tx->labels->name . ' Export - ' . date("Y F d H:i");
482 } else {
483 $friendly_name = 'Taxonomy Terms Export - ' . date("Y F d H:i");
484 }
485 } else {
486 $post_type_details = get_post_type_object(array_shift($post_types));
487 $friendly_name = $post_type_details->labels->name . ' Export - ' . date("Y F d H:i");
488 }
489 } else {
490 $friendly_name = 'WP_Query Export - ' . date("Y F d H:i");
491 }
492
493 PMXE_Plugin::$session->set('friendly_name', $friendly_name);
494 }
495
496 PMXE_Plugin::$session->set('file', '');
497 PMXE_Plugin::$session->save_data();
498
499 $export->set(
500 array(
501 'triggered' => 0,
502 'processing' => 0,
503 'exported' => 0,
504 'executing' => 1,
505 'canceled' => 0,
506 'options' => PMXE_Plugin::$session->get_clear_session_data(),
507 'friendly_name' => PMXE_Plugin::$session->friendly_name,
508 'scheduled' => (PMXE_Plugin::$session->is_scheduled) ? PMXE_Plugin::$session->scheduled_period : '',
509 //'registered_on' => date('Y-m-d H:i:s'),
510 'last_activity' => date('Y-m-d H:i:s')
511 )
512 )->save();
513
514 // create an import for this export
515 if ($export->options['export_to'] == 'csv' || !in_array($export->options['xml_template_type'], array('custom', 'XmlGoogleMerchants'))) PMXE_Wpallimport::create_an_import($export);
516 PMXE_Plugin::$session->set('update_previous', $export->id);
517 PMXE_Plugin::$session->save_data();
518
519 do_action('pmxe_before_export', $export->id);
520
521 }
522
523 $this->render();
524 }
525
526 /**
527 * @param $post
528 * @return string
529 */
530 protected function getFriendlyName($post)
531 {
532 $friendly_name = '';
533 $post_types = PMXE_Plugin::$session->get('cpt');
534 if (!empty($post_types)) {
535 if (in_array('users', $post_types)) {
536 $friendly_name = 'Users Export - ' . date("Y F d H:i");
537 return $friendly_name;
538 } elseif (in_array('shop_customer', $post_types)) {
539 $friendly_name = 'Customers Export - ' . date("Y F d H:i");
540 return $friendly_name;
541 } elseif (in_array('comments', $post_types)) {
542 $friendly_name = 'Comments Export - ' . date("Y F d H:i");
543 return $friendly_name;
544 } elseif (in_array('taxonomies', $post_types)) {
545 $tx = get_taxonomy($post['taxonomy_to_export']);
546 if (!empty($tx->labels->name)) {
547 $friendly_name = $tx->labels->name . ' Export - ' . date("Y F d H:i");
548 return $friendly_name;
549 } else {
550 $friendly_name = 'Taxonomy Terms Export - ' . date("Y F d H:i");
551 return $friendly_name;
552 }
553 } else {
554 $is_rapid_add_on_export = PMXE_Helper::is_rapid_export_addon($post_types);
555 if($is_rapid_add_on_export) {
556 return 'Gravity Forms Entries Export - ' . date("Y F d H:i");
557 }
558
559 $post_type_details = get_post_type_object(array_shift($post_types));
560 $friendly_name = $post_type_details->labels->name . ' Export - ' . date("Y F d H:i");
561 return $friendly_name;
562 }
563 } else {
564 $friendly_name = 'WP_Query Export - ' . date("Y F d H:i");
565 return $friendly_name;
566 }
567 }
568
569 function insertAfter($input, $index, $newKey, $element) {
570 if (!array_key_exists($index, $input)) {
571 throw new Exception("Index not found");
572 }
573 $tmpArray = array();
574 foreach ($input as $key => $value) {
575 $tmpArray[$key] = $value;
576 if ($key === $index) {
577 $tmpArray[$newKey] = $element;
578 }
579 }
580 return $tmpArray;
581 }
582 }