PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.3.4
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.3.4
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 5 years ago settings.php 5 years ago
export.php
581 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(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 = 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(add_query_arg('action', 'options', $this->baseUrl));
159 die();
160 } else {
161 wp_redirect(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(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(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 )->save();
384
385 PMXE_Plugin::$session->set('update_previous', $export->id);
386 PMXE_Plugin::$session->set('friendly_name', $this->getFriendlyName($post));
387 PMXE_Plugin::$session->save_data();
388 $exportId = $export->id;
389 }
390 $this->data['export_id'] = $exportId;
391 $this->data['export'] = new PMXE_Export_Record();
392 $this->data['export'] = $this->data['export']->getBy('id', $exportId);
393
394 if(empty($post['friendly_name'])) {
395 $post['friendly_name'] = $this->getFriendlyName($post);
396 }
397 }
398 else {
399 $DefaultOptions = $this->data['export']->options + $default;
400 if (empty($this->data['export']->options['export_variations'])) {
401 $DefaultOptions['export_variations'] = XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_PARENT_AND_VARIATION;
402 }
403 if (empty($this->data['export']->options['export_variations_title'])) {
404 $DefaultOptions['export_variations_title'] = XmlExportEngine::VARIATION_USE_DEFAULT_TITLE;
405 }
406 $post = $this->input->post($DefaultOptions);
407 $post['scheduled'] = $this->data['export']->scheduled;
408
409 foreach ($post as $key => $value) {
410 PMXE_Plugin::$session->set($key, $value);
411 }
412 PMXE_Plugin::$session->save_data();
413 $this->data['export_id'] = $this->data['export']->id;
414 }
415
416 $this->data['engine'] = new XmlExportEngine($post, $this->errors);
417
418 $this->data['engine']->init_available_data();
419
420 $this->data['post'] =& $post;
421
422 if ($this->input->post('is_submitted')) {
423
424 check_admin_referer('options', '_wpnonce_options');
425
426 if ($post['is_generate_templates'] and '' == $post['template_name']) {
427 $friendly_name = $this->getFriendlyName($post);
428 $post['template_name'] = $friendly_name;
429 }
430
431 if ($this->isWizard) {
432 if (!$this->errors->get_error_codes()) {
433 foreach ($this->data['post'] as $key => $value) {
434 PMXE_Plugin::$session->set($key, $value);
435 }
436 PMXE_Plugin::$session->save_data();
437 wp_redirect(add_query_arg('action', 'process', $this->baseUrl));
438 die();
439 }
440 } else {
441 $this->errors->remove('count-validation');
442 if (!$this->errors->get_error_codes()) {
443 $this->data['export']->set(array('options' => $post, 'settings_update_on' => date('Y-m-d H:i:s')))->save();
444 if (!empty($post['friendly_name'])) {
445 $this->data['export']->set(array('friendly_name' => $post['friendly_name'], 'scheduled' => (($post['is_scheduled']) ? $post['scheduled_period'] : '')))->save();
446 }
447 wp_redirect(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')));
448 die();
449 }
450 }
451 }
452
453 $this->render();
454 }
455
456 /**
457 * Step #4: Export Processing
458 */
459 public function process()
460 {
461 @set_time_limit(0);
462
463 $export = $this->data['update_previous'];
464
465 if (!PMXE_Plugin::is_ajax()) {
466
467 if ("" == PMXE_Plugin::$session->friendly_name) {
468
469 $post_types = PMXE_Plugin::$session->get('cpt');
470 if (!empty($post_types)) {
471 if (in_array('users', $post_types)) {
472 $friendly_name = 'Users Export - ' . date("Y F d H:i");
473 } elseif (in_array('shop_customer', $post_types)) {
474 $friendly_name = 'Customers Export - ' . date("Y F d H:i");
475 } elseif (in_array('comments', $post_types)) {
476 $friendly_name = 'Comments Export - ' . date("Y F d H:i");
477 } elseif (in_array('taxonomies', $post_types)) {
478 $tx = get_taxonomy(PMXE_Plugin::$session->get('taxonomy_to_export'));
479 if (!empty($tx->labels->name)) {
480 $friendly_name = $tx->labels->name . ' Export - ' . date("Y F d H:i");
481 } else {
482 $friendly_name = 'Taxonomy Terms Export - ' . date("Y F d H:i");
483 }
484 } else {
485 $post_type_details = get_post_type_object(array_shift($post_types));
486 $friendly_name = $post_type_details->labels->name . ' Export - ' . date("Y F d H:i");
487 }
488 } else {
489 $friendly_name = 'WP_Query Export - ' . date("Y F d H:i");
490 }
491
492 PMXE_Plugin::$session->set('friendly_name', $friendly_name);
493 }
494
495 PMXE_Plugin::$session->set('file', '');
496 PMXE_Plugin::$session->save_data();
497
498 $export->set(
499 array(
500 'triggered' => 0,
501 'processing' => 0,
502 'exported' => 0,
503 'executing' => 1,
504 'canceled' => 0,
505 'options' => PMXE_Plugin::$session->get_clear_session_data(),
506 'friendly_name' => PMXE_Plugin::$session->friendly_name,
507 'scheduled' => (PMXE_Plugin::$session->is_scheduled) ? PMXE_Plugin::$session->scheduled_period : '',
508 //'registered_on' => date('Y-m-d H:i:s'),
509 'last_activity' => date('Y-m-d H:i:s')
510 )
511 )->save();
512
513 // create an import for this export
514 if ($export->options['export_to'] == 'csv' || !in_array($export->options['xml_template_type'], array('custom', 'XmlGoogleMerchants'))) PMXE_Wpallimport::create_an_import($export);
515 PMXE_Plugin::$session->set('update_previous', $export->id);
516 PMXE_Plugin::$session->save_data();
517
518 do_action('pmxe_before_export', $export->id);
519
520 }
521
522 $this->render();
523 }
524
525 /**
526 * @param $post
527 * @return string
528 */
529 protected function getFriendlyName($post)
530 {
531 $friendly_name = '';
532 $post_types = PMXE_Plugin::$session->get('cpt');
533 if (!empty($post_types)) {
534 if (in_array('users', $post_types)) {
535 $friendly_name = 'Users Export - ' . date("Y F d H:i");
536 return $friendly_name;
537 } elseif (in_array('shop_customer', $post_types)) {
538 $friendly_name = 'Customers Export - ' . date("Y F d H:i");
539 return $friendly_name;
540 } elseif (in_array('comments', $post_types)) {
541 $friendly_name = 'Comments Export - ' . date("Y F d H:i");
542 return $friendly_name;
543 } elseif (in_array('taxonomies', $post_types)) {
544 $tx = get_taxonomy($post['taxonomy_to_export']);
545 if (!empty($tx->labels->name)) {
546 $friendly_name = $tx->labels->name . ' Export - ' . date("Y F d H:i");
547 return $friendly_name;
548 } else {
549 $friendly_name = 'Taxonomy Terms Export - ' . date("Y F d H:i");
550 return $friendly_name;
551 }
552 } else {
553 $is_rapid_add_on_export = PMXE_Helper::is_rapid_export_addon($post_types);
554 if($is_rapid_add_on_export) {
555 return 'Gravity Forms Entries Export - ' . date("Y F d H:i");
556 }
557
558 $post_type_details = get_post_type_object(array_shift($post_types));
559 $friendly_name = $post_type_details->labels->name . ' Export - ' . date("Y F d H:i");
560 return $friendly_name;
561 }
562 } else {
563 $friendly_name = 'WP_Query Export - ' . date("Y F d H:i");
564 return $friendly_name;
565 }
566 }
567
568 function insertAfter($input, $index, $newKey, $element) {
569 if (!array_key_exists($index, $input)) {
570 throw new Exception("Index not found");
571 }
572 $tmpArray = array();
573 foreach ($input as $key => $value) {
574 $tmpArray[$key] = $value;
575 if ($key === $index) {
576 $tmpArray[$newKey] = $element;
577 }
578 }
579 return $tmpArray;
580 }
581 }