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