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