PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.3.2
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.3.2
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 / models / export / record.php
wp-all-export / models / export Last commit date
list.php 10 years ago record.php 4 years ago
record.php
738 lines
1 <?php
2
3 class PMXE_Export_Record extends PMXE_Model_Record {
4
5 /**
6 * Initialize model instance
7 * @param array[optional] $data Array of record data to initialize object with
8 */
9 public function __construct($data = array()) {
10 parent::__construct($data);
11 $this->setTable(PMXE_Plugin::getInstance()->getTablePrefix() . 'exports');
12 }
13
14 /**
15 * Import all files matched by path
16 *
17 * @param callable[optional] $logger Method where progress messages are submmitted
18 *
19 * @return PMXE_Export_Record
20 * @chainable
21 */
22 public function execute($logger = NULL, $cron = false) {
23
24 $this->fix_template_options();
25
26 $wp_uploads = wp_upload_dir();
27
28 $this->set(array('processing' => 1))->update(); // lock cron requests
29
30 wp_reset_postdata();
31
32 XmlExportEngine::$exportOptions = $this->options;
33 XmlExportEngine::$is_user_export = $this->options['is_user_export'];
34 XmlExportEngine::$is_comment_export = $this->options['is_comment_export'];
35 XmlExportEngine::$is_taxonomy_export = empty($this->options['is_taxonomy_export']) ? false : $this->options['is_taxonomy_export'];
36 XmlExportEngine::$exportID = $this->id;
37 XmlExportEngine::$exportRecord = $this;
38 XmlExportEngine::$post_types = $this->options['cpt'];
39
40 if ( class_exists('SitePress') && ! empty(XmlExportEngine::$exportOptions['wpml_lang'])){
41 do_action( 'wpml_switch_language', XmlExportEngine::$exportOptions['wpml_lang'] );
42 }
43
44 if (empty(XmlExportEngine::$exportOptions['export_variations'])){
45 XmlExportEngine::$exportOptions['export_variations'] = XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_PARENT_AND_VARIATION;
46 }
47 if (empty(XmlExportEngine::$exportOptions['export_variations_title'])){
48 XmlExportEngine::$exportOptions['export_variations_title'] = XmlExportEngine::VARIATION_USE_PARENT_TITLE;
49 }
50
51 if (empty(XmlExportEngine::$exportOptions['xml_template_type'])) XmlExportEngine::$exportOptions['xml_template_type'] = 'simple';
52
53 $filter_args = array(
54 'filter_rules_hierarhy' => $this->options['filter_rules_hierarhy'],
55 'product_matching_mode' => $this->options['product_matching_mode'],
56 'taxonomy_to_export' => empty($this->options['taxonomy_to_export']) ? '' : $this->options['taxonomy_to_export']
57 );
58
59 $filters = \Wpae\Pro\Filtering\FilteringFactory::getFilterEngine();
60 $filters->init($filter_args);
61
62 if ('advanced' == $this->options['export_type'])
63 {
64 // [ Update where clause]
65 $filters->parse();
66
67 XmlExportEngine::$exportOptions['whereclause'] = $filters->get('queryWhere');
68 XmlExportEngine::$exportOptions['joinclause'] = $filters->get('queryJoin');
69
70 $this->set(array( 'options' => XmlExportEngine::$exportOptions ))->update();
71 // [\ Update where clause]
72
73 if (XmlExportEngine::$is_user_export)
74 {
75 add_action('pre_user_query', 'wp_all_export_pre_user_query', 10, 1);
76 $exportQuery = eval('return new WP_User_Query(array(' . $this->options['wp_query'] . ', \'offset\' => ' . $this->exported . ', \'number\' => ' . $this->options['records_per_iteration'] . '));');
77 remove_action('pre_user_query', 'wp_all_export_pre_user_query');
78 }
79 elseif (XmlExportEngine::$is_comment_export)
80 {
81 add_action('comments_clauses', 'wp_all_export_comments_clauses', 10, 1);
82 $exportQuery = eval('return new WP_Comment_Query(array(' . $this->options['wp_query'] . ', \'offset\' => ' . $this->exported . ', \'number\' => ' . $this->options['records_per_iteration'] . '));');
83 remove_action('comments_clauses', 'wp_all_export_comments_clauses');
84 }
85 else
86 {
87 remove_all_actions('parse_query');
88 remove_all_actions('pre_get_posts');
89 remove_all_filters('posts_clauses');
90
91 add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
92 add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
93 $exportQuery = eval('return new WP_Query(array(' . $this->options['wp_query'] . ', \'offset\' => ' . $this->exported . ', \'posts_per_page\' => ' . $this->options['records_per_iteration'] . '));');
94 remove_filter('posts_join', 'wp_all_export_posts_join');
95 remove_filter('posts_where', 'wp_all_export_posts_where');
96 }
97 }
98 else
99 {
100 // [ Update where clause]
101 $filters->parse();
102
103 XmlExportEngine::$exportOptions['whereclause'] = $filters->get('queryWhere');
104 XmlExportEngine::$exportOptions['joinclause'] = $filters->get('queryJoin');
105
106 $this->set(array( 'options' => XmlExportEngine::$exportOptions ))->update();
107 // [\ Update where clause]
108
109 if ( in_array('users', $this->options['cpt']) or in_array('shop_customer', $this->options['cpt']))
110 {
111 add_action('pre_user_query', 'wp_all_export_pre_user_query', 10, 1);
112 $exportQuery = new WP_User_Query( array( 'orderby' => 'ID', 'order' => 'ASC', 'number' => $this->options['records_per_iteration'], 'offset' => $this->exported));
113 remove_action('pre_user_query', 'wp_all_export_pre_user_query');
114 }
115 elseif ( in_array('comments', $this->options['cpt']))
116 {
117 add_action('comments_clauses', 'wp_all_export_comments_clauses', 10, 1);
118 global $wp_version;
119
120 if ( version_compare($wp_version, '4.2.0', '>=') )
121 {
122 $exportQuery = new WP_Comment_Query( array( 'orderby' => 'comment_ID', 'order' => 'ASC', 'number' => $this->options['records_per_iteration'], 'offset' => $this->exported));
123 }
124 else
125 {
126 $exportQuery = get_comments( array( 'orderby' => 'comment_ID', 'order' => 'ASC', 'number' => $this->options['records_per_iteration'], 'offset' => $this->exported));
127 }
128 remove_action('comments_clauses', 'wp_all_export_comments_clauses');
129 }
130 elseif ( in_array('taxonomies', $this->options['cpt']))
131 {
132 add_filter('terms_clauses', 'wp_all_export_terms_clauses', 10, 3);
133 $exportQuery = new WP_Term_Query( array( 'taxonomy' => $this->options['taxonomy_to_export'], 'orderby' => 'term_id', 'order' => 'ASC', 'number' => $this->options['records_per_iteration'], 'offset' => $this->exported, 'hide_empty' => false));
134 $postCount = count($exportQuery->get_terms());
135 remove_filter('terms_clauses', 'wp_all_export_terms_clauses');
136 }
137 else
138 {
139 remove_all_actions('parse_query');
140 remove_all_actions('pre_get_posts');
141 remove_all_filters('posts_clauses');
142
143 add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
144 add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
145
146 $exportQuery = new WP_Query( array( 'post_type' => $this->options['cpt'], 'post_status' => 'any', 'orderby' => 'ID', 'order' => 'ASC', 'ignore_sticky_posts' => 1, 'offset' => $this->exported, 'posts_per_page' => $this->options['records_per_iteration'] ));
147
148 remove_filter('posts_join', 'wp_all_export_posts_join');
149 remove_filter('posts_where', 'wp_all_export_posts_where');
150 }
151 }
152
153 XmlExportEngine::$exportQuery = $exportQuery;
154 $errors = new WP_Error();
155 $engine = new XmlExportEngine($this->options, $errors);
156
157 $file_path = false;
158
159 $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
160
161 if ( $this->exported == 0 )
162 {
163 // create an import for this export
164 if ( $this->options['export_to'] == 'csv' || ! empty($this->options['xml_template_type']) && ! in_array($this->options['xml_template_type'], array('custom', 'XmlGoogleMerchants')) ) PMXE_Wpallimport::create_an_import( $this );
165
166 // unlink previously generated files
167 $attachment_list = $this->options['attachment_list'];
168 if ( ! empty($attachment_list))
169 {
170 foreach ($attachment_list as $attachment) {
171 if (!is_numeric($attachment))
172 {
173 @unlink($attachment);
174 }
175 }
176 }
177 $exportOptions = $this->options;
178 $exportOptions['attachment_list'] = array();
179 $this->set(array(
180 'options' => $exportOptions
181 ))->save();
182
183 // generate export file name
184 $file_path = wp_all_export_generate_export_file( $this->id );
185
186 if ( ! $is_secure_import )
187 {
188 $wp_filetype = wp_check_filetype(basename($file_path), null );
189 $attachment_data = array(
190 'guid' => $wp_uploads['baseurl'] . '/' . _wp_relative_upload_path( $file_path ),
191 'post_mime_type' => $wp_filetype['type'],
192 'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_path)),
193 'post_content' => '',
194 'post_status' => 'inherit'
195 );
196
197 if ( empty($this->attch_id) )
198 {
199 $attach_id = wp_insert_attachment( $attachment_data, $file_path );
200 }
201 elseif($this->options['creata_a_new_export_file']) {
202 $attach_id = wp_insert_attachment( $attachment_data, $file_path );
203 }
204 else
205 {
206 $attach_id = $this->attch_id;
207 $attachment = get_post($attach_id);
208 if ($attachment)
209 {
210 update_attached_file( $attach_id, $file_path );
211 wp_update_attachment_metadata( $attach_id, $attachment_data );
212 }
213 else
214 {
215 $attach_id = wp_insert_attachment( $attachment_data, $file_path );
216 }
217 }
218
219 $exportOptions = $this->options;
220 if ( ! in_array($attach_id, $exportOptions['attachment_list'])){
221 $exportOptions['attachment_list'][] = $attach_id;
222 }
223
224 $this->set(array(
225 'attch_id' => $attach_id,
226 'options' => $exportOptions
227 ))->save();
228
229 }
230 else
231 {
232 $exportOptions = $this->options;
233 $exportOptions['filepath'] = $file_path;
234 $this->set(array(
235 'options' => $exportOptions
236 ))->save();
237 }
238
239 do_action('pmxe_before_export', $this->id);
240
241 }
242 else
243 {
244 if ( ! $is_secure_import )
245 {
246 $file_path = str_replace($wp_uploads['baseurl'], $wp_uploads['basedir'], wp_get_attachment_url( $this->attch_id ));
247 }
248 else
249 {
250 $file_path = wp_all_export_get_absolute_path($this->options['filepath']);
251 }
252 }
253
254 // [ get total founded records ]
255 if (XmlExportEngine::$is_comment_export)
256 {
257 global $wp_version;
258
259 if ( version_compare($wp_version, '4.2.0', '>=') )
260 {
261 $postCount = count($exportQuery->get_comments());
262 add_action('comments_clauses', 'wp_all_export_comments_clauses', 10, 1);
263 $result = new WP_Comment_Query( array( 'orderby' => 'comment_ID', 'order' => 'ASC', 'number' => 10, 'count' => true));
264 $foundPosts = $result->get_comments();
265 remove_action('comments_clauses', 'wp_all_export_comments_clauses');
266 }
267 else
268 {
269 $postCount = count($exportQuery);
270 add_action('comments_clauses', 'wp_all_export_comments_clauses', 10, 1);
271 $foundPosts = get_comments( array( 'orderby' => 'comment_ID', 'order' => 'ASC', 'number' => 10, 'count' => true));
272 remove_action('comments_clauses', 'wp_all_export_comments_clauses');
273 }
274 }
275 elseif(XmlExportEngine::$is_taxonomy_export){
276 add_filter('terms_clauses', 'wp_all_export_terms_clauses', 10, 3);
277 $result = new WP_Term_Query( array( 'taxonomy' => $this->options['taxonomy_to_export'], 'orderby' => 'term_id', 'order' => 'ASC', 'count' => true, 'hide_empty' => false));
278 $foundPosts = count($result->get_terms());
279 remove_filter('terms_clauses', 'wp_all_export_terms_clauses');
280 }
281 else
282 {
283 $foundPosts = ( ! XmlExportEngine::$is_user_export ) ? $exportQuery->found_posts : $exportQuery->get_total();
284 $postCount = ( ! XmlExportEngine::$is_user_export ) ? $exportQuery->post_count : count($exportQuery->get_results());
285 }
286 // [ \get total found records ]
287
288 XmlExportEngine::$exportOptions = $this->options;
289
290
291 switch ( $this->options['export_to'] ) {
292
293 case XmlExportEngine::EXPORT_TYPE_XML:
294
295 if($this->options['xml_template_type'] == XmlExportEngine::EXPORT_TYPE_GOOLE_MERCHANTS) {
296 $googleMerchantsServiceFactory = new \Wpae\App\Service\ExportGoogleMerchantsFactory();
297 $googleMerchantsService = $googleMerchantsServiceFactory->createService();
298 $googleMerchantsService->export($cron, $file_path, $this->exported);
299 } else {
300 XmlCsvExport::export_xml( false, $cron, $file_path, $this->exported );
301 }
302
303 break;
304
305 case XmlExportEngine::EXPORT_TYPE_CSV:
306
307 XmlCsvExport::export_csv( false, $cron, $file_path, $this->exported );
308 break;
309
310 default:
311 # code...
312 break;
313 }
314
315 $this->set(array(
316 'exported' => $this->exported + $postCount,
317 'last_activity' => date('Y-m-d H:i:s'),
318 'processing' => 0
319 ))->save();
320
321
322
323 if ( empty($foundPosts) )
324 {
325 $this->set(array(
326 'processing' => 0,
327 'triggered' => 0,
328 'canceled' => 0,
329 'registered_on' => date('Y-m-d H:i:s'),
330 'iteration' => ++$this->iteration
331 ))->update();
332
333 do_action('pmxe_after_export', $this->id, $this);
334 }
335 elseif ( ! $postCount or $foundPosts == $this->exported )
336 {
337 if ( file_exists($file_path))
338 {
339 if ( $this->options['export_to'] == 'xml' )
340 {
341 switch( XmlExportEngine::$exportOptions['xml_template_type'] ){
342 case 'XmlGoogleMerchants':
343 case 'custom':
344 require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php';
345 file_put_contents($file_path, PMXE_XMLWriter::preprocess_xml("\n".XmlExportEngine::$exportOptions['custom_xml_template_footer']), FILE_APPEND);
346 break;
347 }
348
349 if ( ! in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants')) )
350 {
351 $main_xml_tag = apply_filters('wp_all_export_main_xml_tag', $this->options['main_xml_tag'], $this->id);
352
353 file_put_contents($file_path, '</'.$main_xml_tag.'>', FILE_APPEND);
354
355 $xml_footer = apply_filters('wp_all_export_xml_footer', '', $this->id);
356
357 if ( ! empty($xml_footer) ) file_put_contents($file_path, $xml_footer, FILE_APPEND);
358 }
359 }
360
361 PMXE_Wpallimport::generateImportTemplate( $this, $file_path, $foundPosts );
362
363 if ($this->options['is_scheduled'] and "" != $this->options['scheduled_email']){
364
365 add_filter( 'wp_mail_content_type', array($this, 'set_html_content_type') );
366
367 $headers = 'From: '. get_bloginfo( 'name' ) .' <'. get_bloginfo( 'admin_email' ) .'>' . "\r\n";
368
369 $message = '<p>Export '. wp_all_export_clear_xss($this->options['friendly_name']) .' has been completed. You can find exported file in attachments.</p>';
370
371 wp_mail($this->options['scheduled_email'], __("WP All Export", "pmxe_plugin"), $message, $headers, array($file_path));
372
373 remove_filter( 'wp_mail_content_type', array($this, 'set_html_content_type') );
374 }
375
376 }
377
378 $this->set(array(
379 'processing' => 0,
380 'triggered' => 0,
381 'canceled' => 0,
382 'registered_on' => date('Y-m-d H:i:s'),
383 'iteration' => ++$this->iteration
384 ))->update();
385
386 do_action('pmxe_after_export', $this->id, $this);
387 } else {
388 do_action('pmxe_after_iteration', $this->id, $this);
389 }
390
391 $this->set('registered_on', date('Y-m-d H:i:s'))->save(); // update registered_on to indicated that job has been exectured even if no files are going to be imported by the rest of the method
392
393 return $this;
394 }
395
396 public function set_html_content_type(){
397 return 'text/html';
398 }
399
400 public function generate_bundle( $debug = false)
401 {
402 // do not generate export bundle if not supported
403 if ( ! self::is_bundle_supported($this->options) ) return;
404
405 $uploads = wp_upload_dir();
406
407 //generate temporary folder
408 $export_dir = wp_all_export_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY, $this->id ) . DIRECTORY_SEPARATOR;
409 $bundle_dir = $export_dir . 'bundle' . DIRECTORY_SEPARATOR;
410
411 // clear tmp dir
412 wp_all_export_rrmdir($bundle_dir);
413
414 @mkdir($bundle_dir);
415
416 $friendly_name = sanitize_file_name($this->friendly_name);
417
418 $template = "WP All Import Template - " . $friendly_name . ".txt";
419
420 $templates = array();
421
422 $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
423
424 if ( ! $is_secure_import)
425 {
426 $filepath = get_attached_file($this->attch_id);
427 }
428 else
429 {
430 $filepath = wp_all_export_get_absolute_path($this->options['filepath']);
431 }
432
433 @copy( $filepath, $bundle_dir . basename($filepath) );
434
435 if ( ! empty($this->options['tpl_data']))
436 {
437 $template_data = array($this->options['tpl_data']);
438
439 $template_data[0]['source_file_name'] = basename($filepath);
440
441 $template_options = maybe_unserialize($template_data[0]['options']);
442
443 $templates[$template_options['custom_type']] = $template_data;
444
445 $readme = __("The other two files in this zip are the export file containing all of your data and the import template for WP All Import. \n\nTo import this data, create a new import with WP All Import and upload this zip file.", "wp_all_export_plugin");
446
447 file_put_contents($bundle_dir . 'readme.txt', $readme);
448 }
449
450 // [ Add child exports to the bundle]
451 $exportList = new PMXE_Export_List();
452
453 foreach ($exportList->getBy('parent_id', $this->id)->convertRecords() as $child_export)
454 {
455 $is_generate_child_template = true;
456
457 switch ($child_export->export_post_type)
458 {
459 case 'product':
460 if ( ! $this->options['order_include_poducts'] ) $is_generate_child_template = false;
461 break;
462 case 'shop_coupon':
463 if ( ! $this->options['order_include_coupons'] ) $is_generate_child_template = false;
464 break;
465 case 'shop_customer':
466 if ( ! $this->options['order_include_customers'] ) $is_generate_child_template = false;
467 break;
468 }
469
470 if ( ! $is_generate_child_template ) continue;
471
472 if ( ! $is_secure_import)
473 {
474 $filepath = get_attached_file($child_export->attch_id);
475 }
476 else
477 {
478 $filepath = wp_all_export_get_absolute_path($child_export->options['filepath']);
479 }
480
481 if ( ! empty($child_export->options['tpl_data']))
482 {
483 $template_data = array($child_export->options['tpl_data']);
484
485 $template_data[0]['source_file_name'] = basename($filepath);
486
487 $template_key = ($child_export->export_post_type == 'shop_customer') ? 'import_users' : $child_export->export_post_type;
488
489 $templates[$template_key] = $template_data;
490 }
491
492 @copy( $filepath, $bundle_dir . basename($filepath) );
493 }
494 // \[ Add child exports to the bundle]
495
496 file_put_contents($bundle_dir . $template, json_encode($templates));
497
498 // if ($this->options['creata_a_new_export_file'] && ! empty($this->options['cpt']) and class_exists('WooCommerce') and in_array('shop_order', $this->options['cpt']) and empty($this->parent_id) )
499 // {
500 // $bundle_path = $export_dir . $friendly_name . '-' . ($this->iteration + 1) . '.zip';
501 // }
502 // else
503 // {
504 // $bundle_path = $export_dir . $friendly_name . '.zip';
505 // }
506
507 $bundle_path = $export_dir . $friendly_name . '.zip';
508
509 if ( @file_exists($bundle_path))
510 {
511 @unlink($bundle_path);
512 }
513
514 PMXE_Zip::zipDir($bundle_dir, $bundle_path);
515
516 // clear tmp dir
517 wp_all_export_rrmdir($bundle_dir);
518
519 $exportOptions = $this->options;
520 $exportOptions['bundlepath'] = wp_all_export_get_relative_path($bundle_path);
521 $this->set(array(
522 'options' => $exportOptions
523 ))->save();
524
525 return $bundle_path;
526 }
527
528 public function fix_template_options()
529 {
530 // migrate media options since @version 1.2.4
531 if ( empty($this->options['migration']) )
532 {
533 $options = $this->options;
534
535 $options['migration'] = PMXE_VERSION;
536
537 $is_migrate_media = false;
538
539 foreach ($options['ids'] as $ID => $value)
540 {
541 if ( in_array($options['cc_type'][$ID], array('media', 'attachments')))
542 {
543 $is_migrate_media = true;
544 break;
545 }
546 }
547
548 if ( ! $is_migrate_media )
549 {
550 $this->set(array('options' => $options))->save();
551
552 return $this;
553 }
554
555 $fields = array();
556
557 foreach ($options['ids'] as $ID => $value)
558 {
559 $field = array(
560 'cc_label' => empty($options['cc_label'][$ID]) ? '' : $options['cc_label'][$ID],
561 'cc_php' => empty($options['cc_php'][$ID]) ? '' : $options['cc_php'][$ID],
562 'cc_code' => empty($options['cc_code'][$ID]) ? '' : $options['cc_code'][$ID],
563 'cc_sql' => empty($options['cc_sql'][$ID]) ? '' : $options['cc_sql'][$ID],
564 'cc_type' => empty($options['cc_type'][$ID]) ? '' : $options['cc_type'][$ID],
565 'cc_options' => empty($options['cc_options'][$ID]) ? '' : $options['cc_options'][$ID],
566 'cc_value' => empty($options['cc_value'][$ID]) ? '' : $options['cc_value'][$ID],
567 'cc_name' => empty($options['cc_name'][$ID]) ? '' : $options['cc_name'][$ID],
568 'cc_settings' => empty($options['cc_settings'][$ID]) ? '' : $options['cc_settings'][$ID],
569 );
570
571 switch ($field['cc_type'])
572 {
573 case 'media':
574
575 switch ($field['cc_options'])
576 {
577 case 'urls':
578 $field['cc_label'] = 'url';
579 $field['cc_value'] = 'url';
580 $field['cc_type'] = 'image_url';
581 break;
582 case 'filenames':
583 $field['cc_label'] = 'filename';
584 $field['cc_value'] = 'filename';
585 $field['cc_type'] = 'image_filename';
586 break;
587 case 'filepaths':
588 $field['cc_label'] = 'path';
589 $field['cc_value'] = 'path';
590 $field['cc_type'] = 'image_path';
591 break;
592 default:
593 $field['cc_label'] = 'url';
594 $field['cc_value'] = 'url';
595 $field['cc_type'] = 'image_url';
596 break;
597 }
598
599 $field_name = $field['cc_name'];
600 $field['cc_name'] .= '_images';
601 $field['cc_options'] = '{"is_export_featured":true,"is_export_attached":true,"image_separator":"|"}';
602
603 $fields[] = $field;
604
605 $new_fields = array('title', 'caption', 'description', 'alt');
606
607 foreach ($new_fields as $value)
608 {
609 $new_field = array(
610 'cc_label' => $value,
611 'cc_php' => empty($options['cc_php'][$ID]) ? '' : $options['cc_php'][$ID],
612 'cc_code' => empty($options['cc_code'][$ID]) ? '' : $options['cc_code'][$ID],
613 'cc_sql' => empty($options['cc_sql'][$ID]) ? '' : $options['cc_sql'][$ID],
614 'cc_type' => 'image_' . $value,
615 'cc_options' => '{"is_export_featured":true,"is_export_attached":true,"image_separator":"|"}',
616 'cc_value' => $value,
617 'cc_name' => $field_name . '_' . $value,
618 'cc_settings' => ''
619 );
620
621 $fields[] = $new_field;
622 }
623
624 break;
625
626 case 'attachments':
627 $field['cc_type'] = 'attachment_url';
628 $field['cc_options'] = '';
629 $fields[] = $field;
630 break;
631
632 default:
633 $fields[] = $field;
634 break;
635 }
636 }
637
638 // reset fields settings
639 $options['ids'] = array();
640 $options['cc_label'] = array();
641 $options['cc_php'] = array();
642 $options['cc_code'] = array();
643 $options['cc_sql'] = array();
644 $options['cc_type'] = array();
645 $options['cc_options'] = array();
646 $options['cc_value'] = array();
647 $options['cc_name'] = array();
648 $options['cc_settings'] = array();
649
650 // apply new field settings
651 foreach ($fields as $ID => $field) {
652 $options['ids'][] = 1;
653 $options['cc_label'][] = $field['cc_label'];
654 $options['cc_php'][] = $field['cc_php'];
655 $options['cc_code'][] = $field['cc_code'];
656 $options['cc_sql'][] = $field['cc_sql'];
657 $options['cc_type'][] = $field['cc_type'];
658 $options['cc_options'][] = $field['cc_options'];
659 $options['cc_value'][] = $field['cc_value'];
660 $options['cc_name'][] = $field['cc_name'];
661 $options['cc_settings'][] = $field['cc_settings'];
662 }
663
664 $this->set(array('options' => $options))->save();
665 }
666
667 return $this;
668 }
669
670 public static function is_bundle_supported( $options )
671 {
672 // custom XML template do not support import bundle
673 if ( $options['export_to'] == 'xml' && ! empty($options['xml_template_type']) && in_array($options['xml_template_type'], array('custom', 'XmlGoogleMerchants')) ) return false;
674
675 // Export only parent product do not support import bundle
676 if ( ! empty($options['cpt']) and in_array($options['cpt'][0], array('product', 'product_variation')) and class_exists('WooCommerce') and $options['export_variations'] == XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_VARIATION){
677 return false;
678 }
679
680 $unsupported_post_types = array('comments');
681 return ( empty($options['cpt']) and ! in_array($options['wp_query_selector'], array('wp_comment_query')) or ! empty($options['cpt']) and ! in_array($options['cpt'][0], $unsupported_post_types) ) ? true : false;
682 }
683
684 /**
685 * Clear associations with posts
686 * @return PMXE_Export_Record
687 * @chainable
688 */
689 public function deletePosts() {
690 $post = new PMXE_Post_List();
691 $this->wpdb->query($this->wpdb->prepare('DELETE FROM ' . $post->getTable() . ' WHERE export_id = %s', $this->id));
692 return $this;
693 }
694
695 /**
696 * Delete associated sub exports
697 * @return PMXE_Export_Record
698 * @chainable
699 */
700 public function deleteChildren(){
701 $exportList = new PMXE_Export_List();
702 foreach ($exportList->getBy('parent_id', $this->id)->convertRecords() as $i) {
703 $i->delete();
704 }
705 return $this;
706 }
707
708 /**
709 * @see parent::delete()
710 */
711 public function delete() {
712 $this->deletePosts()->deleteChildren();
713 if ( ! empty($this->options['import_id']) and wp_all_export_is_compatible()){
714 $import = new PMXI_Import_Record();
715 $import->getById($this->options['import_id']);
716 if ( ! $import->isEmpty() and $import->parent_import_id == 99999 ){
717 $import->delete();
718 }
719 }
720 $export_file_path = wp_all_export_get_absolute_path($this->options['filepath']);
721 if ( @file_exists($export_file_path) ){
722 wp_all_export_remove_source($export_file_path);
723 }
724 if ( ! empty($this->attch_id) ){
725 wp_delete_attachment($this->attch_id, true);
726 }
727
728 $wp_uploads = wp_upload_dir();
729
730 $file_for_remote_access = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY . DIRECTORY_SEPARATOR . md5(PMXE_Plugin::getInstance()->getOption('cron_job_key') . $this->id) . '.' . $this->options['export_to'];
731
732 if ( @file_exists($file_for_remote_access)) @unlink($file_for_remote_access);
733
734 return parent::delete();
735 }
736
737 }
738