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