function pmwi_pmxi_do_not_update_existing($post_to_update_id, $import_id, $iteration) { $children = get_posts(array('post_parent' => $post_to_update_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => 'publish')); if ($children) { $postRecord = new PMXI_Post_Record(); foreach ($children as $child) { $postRecord->clear(); $postRecord->getBy(array('post_id' => $child, 'import_id' => $import_id)); if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $iteration))->update(); } } } }
function pmwi_pmxi_do_not_update_existing($post_to_update_id, $import_id, $iteration) { $args = array('post_type' => 'product_variation', 'meta_query' => array(array('key' => '_sku', 'value' => get_post_meta($post_to_update_id, '_sku', true)))); $query = new WP_Query($args); if ($query->have_posts()) { $duplicate_id = $query->post->ID; if ($duplicate_id) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('post_id' => $duplicate_id, 'import_id' => $import_id)); if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $iteration))->update(); } } } }
/** * Perform import operation * @param string $xml XML string to import * @param callback[optional] $logger Method where progress messages are submmitted * @return PMXI_Import_Record * @chainable */ public function process($xml, $logger = NULL, $chunk = false, $is_cron = false, $xpath_prefix = '', $loop = 0) { add_filter('user_has_cap', array($this, '_filter_has_cap_unfiltered_html')); kses_init(); // do not perform special filtering for imported content kses_remove_filters(); $cxpath = $xpath_prefix . $this->xpath; $this->options += PMXI_Plugin::get_default_import_options(); // make sure all options are defined $avoid_pingbacks = PMXI_Plugin::getInstance()->getOption('pingbacks'); $cron_sleep = (int) PMXI_Plugin::getInstance()->getOption('cron_sleep'); if ($avoid_pingbacks and !defined('WP_IMPORTING')) { define('WP_IMPORTING', true); } $postRecord = new PMXI_Post_Record(); $tmp_files = array(); // compose records to import $records = array(); $is_import_complete = false; try { $chunk == 1 and $logger and call_user_func($logger, __('Composing titles...', 'wp_all_import_plugin')); if (!empty($this->options['title'])) { $titles = XmlImportParser::factory($xml, $cxpath, $this->options['title'], $file)->parse($records); $tmp_files[] = $file; } else { $loop and $titles = array_fill(0, $loop, ''); } $chunk == 1 and $logger and call_user_func($logger, __('Composing excerpts...', 'wp_all_import_plugin')); $post_excerpt = array(); if (!empty($this->options['post_excerpt'])) { $post_excerpt = XmlImportParser::factory($xml, $cxpath, $this->options['post_excerpt'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $post_excerpt = array_fill(0, count($titles), ''); } if ("xpath" == $this->options['status']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing statuses...', 'wp_all_import_plugin')); $post_status = array(); if (!empty($this->options['status_xpath'])) { $post_status = XmlImportParser::factory($xml, $cxpath, $this->options['status_xpath'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $post_status = array_fill(0, count($titles), ''); } } if ("xpath" == $this->options['comment_status']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing comment statuses...', 'wp_all_import_plugin')); $comment_status = array(); if (!empty($this->options['comment_status_xpath'])) { $comment_status = XmlImportParser::factory($xml, $cxpath, $this->options['comment_status_xpath'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $comment_status = array_fill(0, count($titles), 'open'); } } if ("xpath" == $this->options['ping_status']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing ping statuses...', 'wp_all_import_plugin')); $ping_status = array(); if (!empty($this->options['ping_status_xpath'])) { $ping_status = XmlImportParser::factory($xml, $cxpath, $this->options['ping_status_xpath'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $ping_status = array_fill(0, count($titles), 'open'); } } if ("xpath" == $this->options['post_format']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing post formats...', 'wp_all_import_plugin')); $post_format = array(); if (!empty($this->options['post_format_xpath'])) { $post_format = XmlImportParser::factory($xml, $cxpath, $this->options['post_format_xpath'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $post_format = array_fill(0, count($titles), 'open'); } } if ("no" == $this->options['is_multiple_page_template']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing page templates...', 'wp_all_import_plugin')); $page_template = array(); if (!empty($this->options['single_page_template'])) { $page_template = XmlImportParser::factory($xml, $cxpath, $this->options['single_page_template'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $page_template = array_fill(0, count($titles), 'default'); } } if ($this->options['is_override_post_type'] and !empty($this->options['post_type_xpath'])) { $chunk == 1 and $logger and call_user_func($logger, __('Composing post types...', 'wp_all_import_plugin')); $post_type = array(); $post_type = XmlImportParser::factory($xml, $cxpath, $this->options['post_type_xpath'], $file)->parse($records); $tmp_files[] = $file; } else { if ('post' == $this->options['type'] and '' != $this->options['custom_type']) { $pType = $this->options['custom_type']; } else { $pType = $this->options['type']; } count($titles) and $post_type = array_fill(0, count($titles), $pType); } if ("no" == $this->options['is_multiple_page_parent']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing page parent...', 'wp_all_import_plugin')); $page_parent = array(); if (!empty($this->options['single_page_parent'])) { $page_parent = XmlImportParser::factory($xml, $cxpath, $this->options['single_page_parent'], $file)->parse($records); $tmp_files[] = $file; foreach ($page_parent as $key => $identity) { $page = 0; switch ($this->options['type']) { case 'post': if (!empty($identity)) { if (ctype_digit($identity)) { $page = get_post($identity); } else { $page = get_page_by_title($identity, OBJECT, $post_type[$key]); if (empty($page)) { $args = array('name' => $identity, 'post_type' => $post_type[$key], 'post_status' => 'any', 'numberposts' => 1); $my_posts = get_posts($args); if ($my_posts) { $page = $my_posts[0]; } } } } break; case 'page': $page = get_page_by_title($identity) or $page = get_page_by_path($identity) or ctype_digit($identity) and $page = get_post($identity); break; default: # code... break; } $page_parent[$key] = !empty($page) ? $page->ID : 0; } } else { count($titles) and $page_parent = array_fill(0, count($titles), 0); } } $chunk == 1 and $logger and call_user_func($logger, __('Composing authors...', 'wp_all_import_plugin')); $post_author = array(); $current_user = wp_get_current_user(); if (!empty($this->options['author'])) { $post_author = XmlImportParser::factory($xml, $cxpath, $this->options['author'], $file)->parse($records); $tmp_files[] = $file; foreach ($post_author as $key => $author) { $user = get_user_by('login', $author) or $user = get_user_by('slug', $author) or $user = get_user_by('email', $author) or ctype_digit($author) and $user = get_user_by('id', $author); $post_author[$key] = !empty($user) ? $user->ID : $current_user->ID; } } else { count($titles) and $post_author = array_fill(0, count($titles), $current_user->ID); } $chunk == 1 and $logger and call_user_func($logger, __('Composing slugs...', 'wp_all_import_plugin')); $post_slug = array(); if (!empty($this->options['post_slug'])) { $post_slug = XmlImportParser::factory($xml, $cxpath, $this->options['post_slug'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $post_slug = array_fill(0, count($titles), ''); } $chunk == 1 and $logger and call_user_func($logger, __('Composing menu order...', 'wp_all_import_plugin')); $menu_order = array(); if (!empty($this->options['order'])) { $menu_order = XmlImportParser::factory($xml, $cxpath, $this->options['order'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $menu_order = array_fill(0, count($titles), ''); } $chunk == 1 and $logger and call_user_func($logger, __('Composing contents...', 'wp_all_import_plugin')); if (!empty($this->options['content'])) { $contents = XmlImportParser::factory((!empty($this->options['is_keep_linebreaks']) and intval($this->options['is_keep_linebreaks'])) ? $xml : preg_replace('%\\r\\n?|\\n%', ' ', $xml), $cxpath, $this->options['content'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $contents = array_fill(0, count($titles), ''); } $chunk == 1 and $logger and call_user_func($logger, __('Composing dates...', 'wp_all_import_plugin')); if ('specific' == $this->options['date_type']) { $dates = XmlImportParser::factory($xml, $cxpath, $this->options['date'], $file)->parse($records); $tmp_files[] = $file; $warned = array(); // used to prevent the same notice displaying several times foreach ($dates as $i => $d) { if ($d == 'now') { $d = current_time('mysql'); } // Replace 'now' with the WordPress local time to account for timezone offsets (WordPress references its local time during publishing rather than the server’s time so it should use that) $time = strtotime($d); if (FALSE === $time) { in_array($d, $warned) or $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: unrecognized date format `%s`, assigning current date', 'wp_all_import_plugin'), $warned[] = $d)); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $time = time(); } $dates[$i] = date('Y-m-d H:i:s', $time); } } else { $dates_start = XmlImportParser::factory($xml, $cxpath, $this->options['date_start'], $file)->parse($records); $tmp_files[] = $file; $dates_end = XmlImportParser::factory($xml, $cxpath, $this->options['date_end'], $file)->parse($records); $tmp_files[] = $file; $warned = array(); // used to prevent the same notice displaying several times foreach ($dates_start as $i => $d) { $time_start = strtotime($dates_start[$i]); if (FALSE === $time_start) { in_array($dates_start[$i], $warned) or $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: unrecognized date format `%s`, assigning current date', 'wp_all_import_plugin'), $warned[] = $dates_start[$i])); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $time_start = time(); } $time_end = strtotime($dates_end[$i]); if (FALSE === $time_end) { in_array($dates_end[$i], $warned) or $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: unrecognized date format `%s`, assigning current date', 'wp_all_import_plugin'), $warned[] = $dates_end[$i])); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $time_end = time(); } $dates[$i] = date('Y-m-d H:i:s', mt_rand($time_start, $time_end)); } } // [custom taxonomies] require_once ABSPATH . 'wp-admin/includes/taxonomy.php'; $taxonomies = array(); $exclude_taxonomies = apply_filters('pmxi_exclude_taxonomies', class_exists('PMWI_Plugin') ? array('post_format', 'product_type', 'product_shipping_class') : array('post_format')); $post_taxonomies = array_diff_key(get_taxonomies_by_object_type(array($this->options['custom_type']), 'object'), array_flip($exclude_taxonomies)); if (!empty($post_taxonomies)) { foreach ($post_taxonomies as $ctx) { if ("" == $ctx->labels->name or class_exists('PMWI_Plugin') and strpos($ctx->name, "pa_") === 0 and $this->options['custom_type'] == "product") { continue; } $chunk == 1 and $logger and call_user_func($logger, sprintf(__('Composing terms for `%s` taxonomy...', 'wp_all_import_plugin'), $ctx->labels->name)); $tx_name = $ctx->name; $mapping_rules = !empty($this->options['tax_mapping'][$tx_name]) ? json_decode($this->options['tax_mapping'][$tx_name], true) : false; $taxonomies[$tx_name] = array(); if (!empty($this->options['tax_logic'][$tx_name]) and !empty($this->options['tax_assing'][$tx_name])) { switch ($this->options['tax_logic'][$tx_name]) { case 'single': if (!empty($this->options['tax_single_xpath'][$tx_name])) { $txes = XmlImportParser::factory($xml, $cxpath, $this->options['tax_single_xpath'][$tx_name], $file)->parse($records); $tmp_files[] = $file; foreach ($txes as $i => $tx) { $taxonomies[$tx_name][$i][] = wp_all_import_ctx_mapping(array('name' => $tx, 'parent' => false, 'assign' => isset($this->options['term_assing'][$tx_name]) ? $this->options['term_assing'][$tx_name] : true, 'is_mapping' => !empty($this->options['tax_enable_mapping'][$tx_name]), 'hierarchy_level' => 1, 'max_hierarchy_level' => 1), $mapping_rules, $tx_name); } } break; case 'multiple': if (!empty($this->options['tax_multiple_xpath'][$tx_name])) { $txes = XmlImportParser::factory($xml, $cxpath, $this->options['tax_multiple_xpath'][$tx_name], $file)->parse($records); $tmp_files[] = $file; foreach ($txes as $i => $tx) { $_tx = $tx; // apply mapping rules before splitting via separator symbol if (!empty($this->options['tax_enable_mapping'][$tx_name]) and !empty($this->options['tax_logic_mapping'][$tx_name])) { if (!empty($mapping_rules)) { foreach ($mapping_rules as $rule) { if (!empty($rule[trim($_tx)])) { $_tx = trim($rule[trim($_tx)]); break; } } } } $delimeted_taxonomies = explode(!empty($this->options['tax_multiple_delim'][$tx_name]) ? $this->options['tax_multiple_delim'][$tx_name] : ',', $_tx); if (!empty($delimeted_taxonomies)) { foreach ($delimeted_taxonomies as $cc) { $taxonomies[$tx_name][$i][] = wp_all_import_ctx_mapping(array('name' => $cc, 'parent' => false, 'assign' => isset($this->options['multiple_term_assing'][$tx_name]) ? $this->options['multiple_term_assing'][$tx_name] : true, 'is_mapping' => !empty($this->options['tax_enable_mapping'][$tx_name]) and empty($this->options['tax_logic_mapping'][$tx_name]), 'hierarchy_level' => 1, 'max_hierarchy_level' => 1), $mapping_rules, $tx_name); } } } } break; case 'hierarchical': if (!empty($this->options['tax_hierarchical_logic_entire'][$tx_name])) { if (!empty($this->options['tax_hierarchical_xpath'][$tx_name]) and is_array($this->options['tax_hierarchical_xpath'][$tx_name])) { count($titles) and $iterator = array_fill(0, count($titles), 0); $taxonomies_hierarchy_groups = array_fill(0, count($titles), array()); // separate hierarchy groups via symbol if (!empty($this->options['is_tax_hierarchical_group_delim'][$tx_name]) and !empty($this->options['tax_hierarchical_group_delim'][$tx_name])) { foreach ($this->options['tax_hierarchical_xpath'][$tx_name] as $k => $tx_xpath) { if (empty($tx_xpath)) { continue; } $txes = XmlImportParser::factory($xml, $cxpath, $tx_xpath, $file)->parse($records); $tmp_files[] = $file; foreach ($txes as $i => $tx) { $_tx = $tx; // apply mapping rules before splitting via separator symbol if (!empty($this->options['tax_enable_mapping'][$tx_name]) and !empty($this->options['tax_logic_mapping'][$tx_name])) { if (!empty($mapping_rules)) { foreach ($mapping_rules as $rule) { if (!empty($rule[trim($_tx)])) { $_tx = trim($rule[trim($_tx)]); break; } } } } $delimeted_groups = explode($this->options['tax_hierarchical_group_delim'][$tx_name], $_tx); if (!empty($delimeted_groups) and is_array($delimeted_groups)) { foreach ($delimeted_groups as $group) { if (!empty($group)) { array_push($taxonomies_hierarchy_groups[$i], $group); } } } } } } else { foreach ($this->options['tax_hierarchical_xpath'][$tx_name] as $k => $tx_xpath) { if (empty($tx_xpath)) { continue; } $txes = XmlImportParser::factory($xml, $cxpath, $tx_xpath, $file)->parse($records); $tmp_files[] = $file; foreach ($txes as $i => $tx) { array_push($taxonomies_hierarchy_groups[$i], $tx); } } } foreach ($taxonomies_hierarchy_groups as $i => $groups) { if (empty($groups)) { continue; } foreach ($groups as $kk => $tx) { $_tx = $tx; // apply mapping rules before splitting via separator symbol if (!empty($this->options['tax_enable_mapping'][$tx_name]) and !empty($this->options['tax_logic_mapping'][$tx_name])) { if (!empty($mapping_rules)) { foreach ($mapping_rules as $rule) { if (!empty($rule[trim($_tx)])) { $_tx = trim($rule[trim($_tx)]); break; } } } } $delimeted_taxonomies = explode(!empty($this->options['tax_hierarchical_delim'][$tx_name]) ? $this->options['tax_hierarchical_delim'][$tx_name] : ',', $_tx); if (!empty($delimeted_taxonomies)) { foreach ($delimeted_taxonomies as $j => $cc) { $is_assign_term = isset($this->options['tax_hierarchical_assing'][$tx_name][$k]) ? $this->options['tax_hierarchical_assing'][$tx_name][$k] : true; if (!empty($this->options['tax_hierarchical_last_level_assign'][$tx_name])) { $is_assign_term = count($delimeted_taxonomies) == $j + 1 ? 1 : 0; } $taxonomies[$tx_name][$i][] = wp_all_import_ctx_mapping(array('name' => $cc, 'parent' => (!empty($taxonomies[$tx_name][$i][$iterator[$i] - 1]) and $j) ? $taxonomies[$tx_name][$i][$iterator[$i] - 1] : false, 'assign' => $is_assign_term, 'is_mapping' => !empty($this->options['tax_enable_mapping'][$tx_name]) and empty($this->options['tax_logic_mapping'][$tx_name]), 'hierarchy_level' => $j + 1, 'max_hierarchy_level' => count($delimeted_taxonomies)), $mapping_rules, $tx_name); $iterator[$i]++; } } } } } } if (!empty($this->options['tax_hierarchical_logic_manual'][$tx_name])) { if (!empty($this->options['post_taxonomies'][$tx_name])) { $taxonomies_hierarchy = json_decode($this->options['post_taxonomies'][$tx_name], true); foreach ($taxonomies_hierarchy as $k => $taxonomy) { if ("" == $taxonomy['xpath']) { continue; } $txes_raw = XmlImportParser::factory($xml, $cxpath, $taxonomy['xpath'], $file)->parse($records); $tmp_files[] = $file; $warned = array(); foreach ($txes_raw as $i => $cc) { $_tx = $cc; // apply mapping rules before splitting via separator symbol if (!empty($this->options['tax_enable_mapping'][$tx_name]) and !empty($this->options['tax_logic_mapping'][$tx_name])) { if (!empty($mapping_rules)) { foreach ($mapping_rules as $rule) { if (!empty($rule[trim($_tx)])) { $_tx = trim($rule[trim($_tx)]); break; } } } } if (!empty($this->options['tax_manualhierarchy_delim'][$tx_name])) { $delimeted_taxonomies = explode($this->options['tax_manualhierarchy_delim'][$tx_name], $_tx); } if (empty($delimeted_taxonomies)) { continue; } if (empty($taxonomies_hierarchy[$k]['txn_names'][$i])) { $taxonomies_hierarchy[$k]['txn_names'][$i] = array(); } if (empty($taxonomies[$tx_name][$i])) { $taxonomies[$tx_name][$i] = array(); } $count_cats = count($taxonomies[$tx_name][$i]); foreach ($delimeted_taxonomies as $j => $dc) { if (!empty($taxonomy['parent_id'])) { foreach ($taxonomies_hierarchy as $key => $value) { if ($value['item_id'] == $taxonomy['parent_id'] and !empty($value['txn_names'][$i])) { foreach ($value['txn_names'][$i] as $parent) { $taxonomies[$tx_name][$i][] = wp_all_import_ctx_mapping(array('name' => trim($dc), 'parent' => $parent, 'assign' => isset($taxonomy['assign']) ? $taxonomy['assign'] : true, 'is_mapping' => !empty($this->options['tax_enable_mapping'][$tx_name]) and empty($this->options['tax_logic_mapping'][$tx_name]), 'hierarchy_level' => 1, 'max_hierarchy_level' => 1), $mapping_rules, $tx_name); } } } } else { $taxonomies[$tx_name][$i][] = wp_all_import_ctx_mapping(array('name' => trim($dc), 'parent' => false, 'assign' => isset($taxonomy['assign']) ? $taxonomy['assign'] : true, 'is_mapping' => !empty($this->options['tax_enable_mapping'][$tx_name]) and empty($this->options['tax_logic_mapping'][$tx_name]), 'hierarchy_level' => 1, 'max_hierarchy_level' => 1), $mapping_rules, $tx_name); } if ($count_cats < count($taxonomies[$tx_name][$i])) { $taxonomies_hierarchy[$k]['txn_names'][$i][] = $taxonomies[$tx_name][$i][count($taxonomies[$tx_name][$i]) - 1]; } } } } } } break; default: break; } } } } // [/custom taxonomies] // Composing featured images $image_sections = apply_filters('wp_all_import_image_sections', array(array('slug' => '', 'title' => __('Images', 'wp_all_import_plugin'), 'type' => 'images'))); if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'wp_all_import_plugin') . ': ' . $uploads['error']); $logger and call_user_func($logger, __('<b>WARNING</b>: No featured images will be created. Uploads folder is not found.', 'wp_all_import_plugin')); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } else { $images_bundle = array(); foreach ($image_sections as $section) { $chunk == 1 and $logger and call_user_func($logger, __('Composing URLs for ' . strtolower($section['title']) . '...', 'wp_all_import_plugin')); $featured_images = array(); if ("no" == $this->options[$section['slug'] . 'download_images']) { if ($this->options[$section['slug'] . 'featured_image']) { $featured_images = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'featured_image'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $featured_images = array_fill(0, count($titles), ''); } } else { if ($this->options[$section['slug'] . 'download_featured_image']) { $featured_images = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'download_featured_image'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $featured_images = array_fill(0, count($titles), ''); } } $images_bundle[empty($section['slug']) ? 'pmxi_gallery_image' : $section['slug']] = array('type' => $section['type'], 'files' => $featured_images); // Composing images meta titles $image_meta_titles_bundle = array(); if ($this->options[$section['slug'] . 'set_image_meta_title']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing ' . strtolower($section['title']) . ' meta data (titles)...', 'wp_all_import_plugin')); $image_meta_titles = array(); if ($this->options[$section['slug'] . 'image_meta_title']) { $image_meta_titles = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'image_meta_title'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_titles = array_fill(0, count($titles), ''); } $image_meta_titles_bundle[empty($section['slug']) ? 'pmxi_gallery_image' : $section['slug']] = $image_meta_titles; } // Composing images meta captions $image_meta_captions_bundle = array(); if ($this->options[$section['slug'] . 'set_image_meta_caption']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing ' . strtolower($section['title']) . ' meta data (captions)...', 'wp_all_import_plugin')); $image_meta_captions = array(); if ($this->options[$section['slug'] . 'image_meta_caption']) { $image_meta_captions = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'image_meta_caption'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_captions = array_fill(0, count($titles), ''); } $image_meta_captions_bundle[empty($section['slug']) ? 'pmxi_gallery_image' : $section['slug']] = $image_meta_captions; } // Composing images meta alt text $image_meta_alts_bundle = array(); if ($this->options[$section['slug'] . 'set_image_meta_alt']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing ' . strtolower($section['title']) . ' meta data (alt text)...', 'wp_all_import_plugin')); $image_meta_alts = array(); if ($this->options[$section['slug'] . 'image_meta_alt']) { $image_meta_alts = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'image_meta_alt'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_alts = array_fill(0, count($titles), ''); } $image_meta_alts_bundle[empty($section['slug']) ? 'pmxi_gallery_image' : $section['slug']] = $image_meta_alts; } // Composing images meta description $image_meta_descriptions_bundle = array(); if ($this->options[$section['slug'] . 'set_image_meta_description']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing ' . strtolower($section['title']) . ' meta data (description)...', 'wp_all_import_plugin')); $image_meta_descriptions = array(); if ($this->options[$section['slug'] . 'image_meta_description']) { $image_meta_descriptions = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'image_meta_description'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_descriptions = array_fill(0, count($titles), ''); } $image_meta_descriptions_bundle[empty($section['slug']) ? 'pmxi_gallery_image' : $section['slug']] = $image_meta_descriptions; } $auto_rename_images_bundle = array(); $auto_extensions_bundle = array(); if ("yes" == $this->options[$section['slug'] . 'download_images']) { // Composing images suffix $chunk == 1 and $this->options[$section['slug'] . 'auto_rename_images'] and $logger and call_user_func($logger, __('Composing ' . strtolower($section['title']) . ' suffix...', 'wp_all_import_plugin')); $auto_rename_images = array(); if ($this->options[$section['slug'] . 'auto_rename_images'] and !empty($this->options[$section['slug'] . 'auto_rename_images_suffix'])) { $auto_rename_images = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'auto_rename_images_suffix'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $auto_rename_images = array_fill(0, count($titles), ''); } $auto_rename_images_bundle[empty($section['slug']) ? 'pmxi_gallery_image' : $section['slug']] = $auto_rename_images; // Composing images extensions $chunk == 1 and $this->options[$section['slug'] . 'auto_set_extension'] and $logger and call_user_func($logger, __('Composing ' . strtolower($section['title']) . ' extensions...', 'wp_all_import_plugin')); $auto_extensions = array(); if ($this->options[$section['slug'] . 'auto_set_extension'] and !empty($this->options[$section['slug'] . 'new_extension'])) { $auto_extensions = XmlImportParser::factory($xml, $cxpath, $this->options[$section['slug'] . 'new_extension'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $auto_extensions = array_fill(0, count($titles), ''); } $auto_extensions_bundle[empty($section['slug']) ? 'pmxi_gallery_image' : $section['slug']] = $auto_extensions; } } } // Composing attachments if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'wp_all_import_plugin') . ': ' . $uploads['error']); $logger and call_user_func($logger, __('<b>WARNING</b>: No attachments will be created', 'wp_all_import_plugin')); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } else { $chunk == 1 and $logger and call_user_func($logger, __('Composing URLs for attachments files...', 'wp_all_import_plugin')); $attachments = array(); if ($this->options['attachments']) { // Detect if attachments is separated by comma $atchs = explode(',', $this->options['attachments']); if (!empty($atchs)) { $parse_multiple = true; foreach ($atchs as $atch) { if (!preg_match("/{.*}/", trim($atch))) { $parse_multiple = false; } } if ($parse_multiple) { foreach ($atchs as $atch) { $posts_attachments = XmlImportParser::factory($xml, $cxpath, trim($atch), $file)->parse($records); $tmp_files[] = $file; foreach ($posts_attachments as $i => $val) { $attachments[$i][] = $val; } } } else { $attachments = XmlImportParser::factory($xml, $cxpath, $this->options['attachments'], $file)->parse($records); $tmp_files[] = $file; } } } else { count($titles) and $attachments = array_fill(0, count($titles), ''); } } $chunk == 1 and $logger and call_user_func($logger, __('Composing unique keys...', 'wp_all_import_plugin')); if (!empty($this->options['unique_key'])) { $unique_keys = XmlImportParser::factory($xml, $cxpath, $this->options['unique_key'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $unique_keys = array_fill(0, count($titles), ''); } $chunk == 1 and $logger and call_user_func($logger, __('Processing posts...', 'wp_all_import_plugin')); $addons = array(); $addons_data = array(); // data parsing for WP All Import add-ons $chunk == 1 and $logger and call_user_func($logger, __('Data parsing via add-ons...', 'wp_all_import_plugin')); $parsingData = array('import' => $this, 'count' => count($titles), 'xml' => $xml, 'logger' => $logger, 'chunk' => $chunk, 'xpath_prefix' => $xpath_prefix); $parse_functions = apply_filters('wp_all_import_addon_parse', array()); foreach (PMXI_Admin_Addons::get_active_addons() as $class) { $model_class = str_replace("_Plugin", "_Import_Record", $class); if (class_exists($model_class)) { $addons[$class] = new $model_class(); $addons_data[$class] = method_exists($addons[$class], 'parse') ? $addons[$class]->parse($parsingData) : false; } else { if (!empty($parse_functions[$class])) { if (is_array($parse_functions[$class]) and is_callable($parse_functions[$class]) or !is_array($parse_functions[$class]) and function_exists($parse_functions[$class])) { $addons_data[$class] = call_user_func($parse_functions[$class], $parsingData); } } } } // save current import state to variables before import $created = $this->created; $updated = $this->updated; $skipped = $this->skipped; $specified_records = array(); if ($this->options['is_import_specified']) { $chunk == 1 and $logger and call_user_func($logger, __('Calculate specified records to import...', 'wp_all_import_plugin')); foreach (preg_split('% *, *%', $this->options['import_specified'], -1, PREG_SPLIT_NO_EMPTY) as $chank) { if (preg_match('%^(\\d+)-(\\d+)$%', $chank, $mtch)) { $specified_records = array_merge($specified_records, range(intval($mtch[1]), intval($mtch[2]))); } else { $specified_records = array_merge($specified_records, array(intval($chank))); } } } $simpleXml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $rootNodes = $simpleXml->xpath($cxpath); foreach ($titles as $i => $void) { $custom_type_details = get_post_type_object($post_type[$i]); if ($is_cron and $cron_sleep) { sleep($cron_sleep); } $logger and call_user_func($logger, __('---', 'wp_all_import_plugin')); $logger and call_user_func($logger, sprintf(__('Record #%s', 'wp_all_import_plugin'), $this->imported + $this->skipped + $i + 1)); wp_cache_flush(); $logger and call_user_func($logger, __('<b>ACTION</b>: pmxi_before_post_import ...', 'wp_all_import_plugin')); do_action('pmxi_before_post_import', $this->id); if (empty($titles[$i])) { if (!empty($addons_data['PMWI_Plugin']) and !empty($addons_data['PMWI_Plugin']['single_product_parent_ID'][$i])) { $titles[$i] = $addons_data['PMWI_Plugin']['single_product_parent_ID'][$i] . ' Product Variation'; } else { $logger and call_user_func($logger, __('<b>WARNING</b>: title is empty.', 'wp_all_import_plugin')); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } } if ($this->options['custom_type'] == 'import_users') { $articleData = array('user_pass' => $addons_data['PMUI_Plugin']['pmui_pass'][$i], 'user_login' => $addons_data['PMUI_Plugin']['pmui_logins'][$i], 'user_nicename' => $addons_data['PMUI_Plugin']['pmui_nicename'][$i], 'user_url' => $addons_data['PMUI_Plugin']['pmui_url'][$i], 'user_email' => $addons_data['PMUI_Plugin']['pmui_email'][$i], 'display_name' => $addons_data['PMUI_Plugin']['pmui_display_name'][$i], 'user_registered' => $addons_data['PMUI_Plugin']['pmui_registered'][$i], 'first_name' => $addons_data['PMUI_Plugin']['pmui_first_name'][$i], 'last_name' => $addons_data['PMUI_Plugin']['pmui_last_name'][$i], 'description' => $addons_data['PMUI_Plugin']['pmui_description'][$i], 'nickname' => $addons_data['PMUI_Plugin']['pmui_nickname'][$i], 'role' => '' == $addons_data['PMUI_Plugin']['pmui_role'][$i] ? 'subscriber' : strtolower($addons_data['PMUI_Plugin']['pmui_role'][$i])); $logger and call_user_func($logger, sprintf(__('Combine all data for user %s...', 'wp_all_import_plugin'), $articleData['user_login'])); } else { $articleData = array('post_type' => $post_type[$i], 'post_status' => "xpath" == $this->options['status'] ? $post_status[$i] : $this->options['status'], 'comment_status' => "xpath" == $this->options['comment_status'] ? $comment_status[$i] : $this->options['comment_status'], 'ping_status' => "xpath" == $this->options['ping_status'] ? $ping_status[$i] : $this->options['ping_status'], 'post_title' => !empty($this->options['is_leave_html']) ? html_entity_decode($titles[$i]) : $titles[$i], 'post_excerpt' => apply_filters('pmxi_the_excerpt', !empty($this->options['is_leave_html']) ? html_entity_decode($post_excerpt[$i]) : $post_excerpt[$i], $this->id), 'post_name' => $post_slug[$i], 'post_content' => apply_filters('pmxi_the_content', !empty($this->options['is_leave_html']) ? html_entity_decode($contents[$i]) : $contents[$i], $this->id), 'post_date' => $dates[$i], 'post_date_gmt' => get_gmt_from_date($dates[$i]), 'post_author' => $post_author[$i], 'menu_order' => (int) $menu_order[$i], 'post_parent' => "no" == $this->options['is_multiple_page_parent'] ? (int) $page_parent[$i] : (int) $this->options['parent']); $logger and call_user_func($logger, sprintf(__('Combine all data for post `%s`...', 'wp_all_import_plugin'), $articleData['post_title'])); } // Re-import Records Matching $post_to_update = false; $post_to_update_id = false; // if Auto Matching re-import option selected if ("manual" != $this->options['duplicate_matching']) { // find corresponding article among previously imported $logger and call_user_func($logger, sprintf(__('Find corresponding article among previously imported for post `%s`...', 'wp_all_import_plugin'), $articleData['post_title'])); $postRecord->clear(); $postRecord->getBy(array('unique_key' => $unique_keys[$i], 'import_id' => $this->id)); if (!$postRecord->isEmpty()) { $logger and call_user_func($logger, sprintf(__('Duplicate post was founded for post %s with unique key `%s`...', 'wp_all_import_plugin'), $articleData['post_title'], $unique_keys[$i])); if ($this->options['custom_type'] == 'import_users') { $post_to_update = get_user_by('id', $post_to_update_id = $postRecord->post_id); } else { $post_to_update = get_post($post_to_update_id = $postRecord->post_id); } } else { $logger and call_user_func($logger, sprintf(__('Duplicate post wasn\'t founded with unique key `%s`...', 'wp_all_import_plugin'), $unique_keys[$i])); } // if Manual Matching re-import option seleted } else { if ('custom field' == $this->options['duplicate_indicator']) { $custom_duplicate_value = XmlImportParser::factory($xml, $cxpath, $this->options['custom_duplicate_value'], $file)->parse($records); $tmp_files[] = $file; $custom_duplicate_name = XmlImportParser::factory($xml, $cxpath, $this->options['custom_duplicate_name'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $custom_duplicate_name = $custom_duplicate_value = array_fill(0, count($titles), ''); } $logger and call_user_func($logger, sprintf(__('Find corresponding article among database for post `%s`...', 'wp_all_import_plugin'), $articleData['post_title'])); // handle duplicates according to import settings if ($duplicates = pmxi_findDuplicates($articleData, $custom_duplicate_name[$i], $custom_duplicate_value[$i], $this->options['duplicate_indicator'])) { $duplicate_id = array_shift($duplicates); if ($duplicate_id) { $logger and call_user_func($logger, sprintf(__('Duplicate post was founded for post `%s`...', 'wp_all_import_plugin'), $articleData['post_title'])); if ($this->options['custom_type'] == 'import_users') { $post_to_update = get_user_by('id', $post_to_update_id = $duplicate_id); } else { $post_to_update = get_post($post_to_update_id = $duplicate_id); } } else { $logger and call_user_func($logger, sprintf(__('Duplicate post wasn\'n founded for post `%s`...', 'wp_all_import_plugin'), $articleData['post_title'])); } } } if (!empty($specified_records)) { if (!in_array($created + $updated + $skipped + 1, $specified_records)) { if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } $skipped++; $logger and call_user_func($logger, __('<b>SKIPPED</b>: by specified records option', 'wp_all_import_plugin')); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $logger and !$is_cron and PMXI_Plugin::$session->chunk_number++; $logger and !$is_cron and PMXI_Plugin::$session->save_data(); continue; } } // Duplicate record is founded if ($post_to_update) { $continue_import = true; $continue_import = apply_filters('wp_all_import_is_post_to_update', $post_to_update_id, wp_all_import_xml2array($rootNodes[$i])); if (!$continue_import) { if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } $skipped++; $logger and call_user_func($logger, sprintf(__('<b>SKIPPED</b>: By filter wp_all_import_is_post_to_update `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $logger and !$is_cron and PMXI_Plugin::$session->chunk_number++; $logger and !$is_cron and PMXI_Plugin::$session->save_data(); continue; } //$logger and call_user_func($logger, sprintf(__('Duplicate record is founded for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); // Do not update already existing records option selected if ("yes" == $this->options['is_keep_former_posts']) { if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } do_action('pmxi_do_not_update_existing', $post_to_update_id, $this->id, $this->iteration); $skipped++; $logger and call_user_func($logger, sprintf(__('<b>SKIPPED</b>: Previously imported record found for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $logger and !$is_cron and PMXI_Plugin::$session->chunk_number++; $logger and !$is_cron and PMXI_Plugin::$session->save_data(); continue; } $articleData['ID'] = $post_to_update_id; // Choose which data to update if ($this->options['update_all_data'] == 'no') { if (!in_array($this->options['custom_type'], array('import_users'))) { // preserve date of already existing article when duplicate is found if (!$this->options['is_update_categories'] and (is_object_in_taxonomy($post_type[$i], 'category') or is_object_in_taxonomy($post_type[$i], 'post_tag')) or $this->options['is_update_categories'] and $this->options['update_categories_logic'] != "full_update") { $logger and call_user_func($logger, sprintf(__('Preserve taxonomies of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); $existing_taxonomies = array(); foreach (array_keys($taxonomies) as $tx_name) { $txes_list = get_the_terms($articleData['ID'], $tx_name); if (is_wp_error($txes_list)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to get current taxonomies for article #%d, updating with those read from XML file', 'wp_all_import_plugin'), $articleData['ID'])); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } else { $txes_new = array(); if (!empty($txes_list)) { foreach ($txes_list as $t) { $txes_new[] = $t->term_taxonomy_id; } } $existing_taxonomies[$tx_name][$i] = $txes_new; } } } if (!$this->options['is_update_dates']) { // preserve date of already existing article when duplicate is found $articleData['post_date'] = $post_to_update->post_date; $articleData['post_date_gmt'] = $post_to_update->post_date_gmt; $logger and call_user_func($logger, sprintf(__('Preserve date of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_status']) { // preserve status and trashed flag $articleData['post_status'] = $post_to_update->post_status; $logger and call_user_func($logger, sprintf(__('Preserve status of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_content']) { $articleData['post_content'] = $post_to_update->post_content; $logger and call_user_func($logger, sprintf(__('Preserve content of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_title']) { $articleData['post_title'] = $post_to_update->post_title; $logger and call_user_func($logger, sprintf(__('Preserve title of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_slug']) { $articleData['post_name'] = $post_to_update->post_name; $logger and call_user_func($logger, sprintf(__('Preserve slug of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_excerpt']) { $articleData['post_excerpt'] = $post_to_update->post_excerpt; $logger and call_user_func($logger, sprintf(__('Preserve excerpt of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_menu_order']) { $articleData['menu_order'] = $post_to_update->menu_order; $logger and call_user_func($logger, sprintf(__('Preserve menu order of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_parent']) { $articleData['post_parent'] = $post_to_update->post_parent; $logger and call_user_func($logger, sprintf(__('Preserve post parent of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } if (!$this->options['is_update_author']) { $articleData['post_author'] = $post_to_update->post_author; $logger and call_user_func($logger, sprintf(__('Preserve post author of already existing article for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); } } else { if (!$this->options['is_update_first_name']) { $articleData['first_name'] = $post_to_update->first_name; } if (!$this->options['is_update_last_name']) { $articleData['last_name'] = $post_to_update->last_name; } if (!$this->options['is_update_role']) { unset($articleData['role']); } if (!$this->options['is_update_nickname']) { $articleData['nickname'] = get_user_meta($post_to_update->ID, 'nickname', true); } if (!$this->options['is_update_description']) { $articleData['description'] = get_user_meta($post_to_update->ID, 'description', true); } if (!$this->options['is_update_login']) { $articleData['user_login'] = $post_to_update->user_login; } if (!$this->options['is_update_password']) { unset($articleData['user_pass']); } if (!$this->options['is_update_nicename']) { $articleData['user_nicename'] = $post_to_update->user_nicename; } if (!$this->options['is_update_email']) { $articleData['user_email'] = $post_to_update->user_email; } if (!$this->options['is_update_registered']) { $articleData['user_registered'] = $post_to_update->user_registered; } if (!$this->options['is_update_display_name']) { $articleData['display_name'] = $post_to_update->display_name; } if (!$this->options['is_update_url']) { $articleData['user_url'] = $post_to_update->user_url; } } $logger and call_user_func($logger, sprintf(__('Applying filter `pmxi_article_data` for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); $articleData = apply_filters('pmxi_article_data', $articleData, $this, $post_to_update); } if (!in_array($this->options['custom_type'], array('import_users'))) { if ($this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_attachments']) { $logger and call_user_func($logger, sprintf(__('Deleting attachments for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); wp_delete_attachments($articleData['ID'], true, 'files'); } // handle obsolete attachments (i.e. delete or keep) according to import settings if ($this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_images'] and $this->options['update_images_logic'] == "full_update") { $logger and call_user_func($logger, sprintf(__('Deleting images for `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); wp_delete_attachments($articleData['ID'], !$this->options['do_not_remove_images'], 'images'); } } } elseif (!$postRecord->isEmpty()) { // existing post not found though it's track was found... clear the leftover, plugin will continue to treat record as new $postRecord->clear(); } // no new records are created. it will only update posts it finds matching duplicates for if (!$this->options['create_new_records'] and empty($articleData['ID'])) { if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } $logger and call_user_func($logger, __('<b>SKIPPED</b>: by do not create new posts option.', 'wp_all_import_plugin')); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $logger and !$is_cron and PMXI_Plugin::$session->chunk_number++; $skipped++; $logger and !$is_cron and PMXI_Plugin::$session->save_data(); continue; } // cloak urls with `WP Wizard Cloak` if corresponding option is set if (!empty($this->options['is_cloak']) and class_exists('PMLC_Plugin')) { if (preg_match_all('%<a\\s[^>]*href=(?(?=")"([^"]*)"|(?(?=\')\'([^\']*)\'|([^\\s>]*)))%is', $articleData['post_content'], $matches, PREG_PATTERN_ORDER)) { $hrefs = array_unique(array_merge(array_filter($matches[1]), array_filter($matches[2]), array_filter($matches[3]))); foreach ($hrefs as $url) { if (preg_match('%^\\w+://%i', $url)) { // mask only links having protocol // try to find matching cloaked link among already registered ones $list = new PMLC_Link_List(); $linkTable = $list->getTable(); $rule = new PMLC_Rule_Record(); $ruleTable = $rule->getTable(); $dest = new PMLC_Destination_Record(); $destTable = $dest->getTable(); $list->join($ruleTable, "{$ruleTable}.link_id = {$linkTable}.id")->join($destTable, "{$destTable}.rule_id = {$ruleTable}.id")->setColumns("{$linkTable}.*")->getBy(array("{$linkTable}.destination_type =" => 'ONE_SET', "{$linkTable}.is_trashed =" => 0, "{$linkTable}.preset =" => '', "{$linkTable}.expire_on =" => '0000-00-00', "{$ruleTable}.type =" => 'ONE_SET', "{$destTable}.weight =" => 100, "{$destTable}.url LIKE" => $url), NULL, 1, 1)->convertRecords(); if ($list->count()) { // matching link found $link = $list[0]; } else { // register new cloaked link global $wpdb; $slug = max(intval($wpdb->get_var("SELECT MAX(CONVERT(name, SIGNED)) FROM {$linkTable}")), intval($wpdb->get_var("SELECT MAX(CONVERT(slug, SIGNED)) FROM {$linkTable}")), 0); $i = 0; do { is_int(++$slug) and $slug > 0 or $slug = 1; $is_slug_found = !intval($wpdb->get_var("SELECT COUNT(*) FROM {$linkTable} WHERE name = '{$slug}' OR slug = '{$slug}'")); } while (!$is_slug_found and $i++ < 100000); if ($is_slug_found) { $link = new PMLC_Link_Record(array('name' => strval($slug), 'slug' => strval($slug), 'header_tracking_code' => '', 'footer_tracking_code' => '', 'redirect_type' => '301', 'destination_type' => 'ONE_SET', 'preset' => '', 'forward_url_params' => 1, 'no_global_tracking_code' => 0, 'expire_on' => '0000-00-00', 'created_on' => date('Y-m-d H:i:s'), 'is_trashed' => 0)); $link->insert(); $rule = new PMLC_Rule_Record(array('link_id' => $link->id, 'type' => 'ONE_SET', 'rule' => '')); $rule->insert(); $dest = new PMLC_Destination_Record(array('rule_id' => $rule->id, 'url' => $url, 'weight' => 100)); $dest->insert(); } else { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to create cloaked link for %s', 'wp_all_import_plugin'), $url)); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; $link = NULL; } } if ($link) { // cloaked link is found or created for url $articleData['post_content'] = preg_replace('%' . preg_quote($url, '%') . '(?=([\\s\'"]|$))%i', $link->getUrl(), $articleData['post_content']); } } } } } // insert article being imported if ($this->options['is_fast_mode']) { foreach (array('transition_post_status', 'save_post', 'pre_post_update', 'add_attachment', 'edit_attachment', 'edit_post', 'post_updated', 'wp_insert_post', 'save_post_' . $post_type[$i]) as $act) { remove_all_actions($act); } } if (!in_array($this->options['custom_type'], array('import_users'))) { if (empty($articleData['ID'])) { $logger and call_user_func($logger, sprintf(__('<b>CREATING</b> `%s` `%s`', 'wp_all_import_plugin'), $articleData['post_title'], $custom_type_details->labels->singular_name)); } else { $logger and call_user_func($logger, sprintf(__('<b>UPDATING</b> `%s` `%s`', 'wp_all_import_plugin'), $articleData['post_title'], $custom_type_details->labels->singular_name)); } $pid = wp_insert_post($articleData, true); } else { $pid = empty($articleData['ID']) ? wp_insert_user($articleData) : wp_update_user($articleData); $articleData['post_title'] = $articleData['user_login']; } if (is_wp_error($pid)) { $logger and call_user_func($logger, __('<b>ERROR</b>', 'wp_all_import_plugin') . ': ' . $pid->get_error_message()); $logger and !$is_cron and PMXI_Plugin::$session->errors++; $skipped++; } else { if ("manual" != $this->options['duplicate_matching'] or empty($articleData['ID'])) { // associate post with import $postRecord->isEmpty() and $postRecord->set(array('post_id' => $pid, 'import_id' => $this->id, 'unique_key' => $unique_keys[$i], 'product_key' => ($post_type[$i] == "product" and PMXI_Admin_Addons::get_addon('PMWI_Plugin')) ? $addons_data['PMWI_Plugin']['single_product_ID'][$i] : ''))->insert(); $postRecord->set(array('iteration' => $this->iteration))->update(); $logger and call_user_func($logger, sprintf(__('Associate post `%s` with current import ...', 'wp_all_import_plugin'), $articleData['post_title'])); } // [post format] if (current_theme_supports('post-formats') && post_type_supports($post_type[$i], 'post-formats')) { set_post_format($pid, "xpath" == $this->options['post_format'] ? $post_format[$i] : $this->options['post_format']); $logger and call_user_func($logger, sprintf(__('Associate post `%s` with post format %s ...', 'wp_all_import_plugin'), $articleData['post_title'], "xpath" == $this->options['post_format'] ? $post_format[$i] : $this->options['post_format'])); } // [/post format] // [addons import] // prepare data for import $importData = array('pid' => $pid, 'i' => $i, 'import' => $this, 'articleData' => $articleData, 'xml' => $xml, 'is_cron' => $is_cron, 'logger' => $logger, 'xpath_prefix' => $xpath_prefix, 'post_type' => $post_type[$i]); $import_functions = apply_filters('wp_all_import_addon_import', array()); // deligate operation to addons foreach (PMXI_Admin_Addons::get_active_addons() as $class) { if (class_exists($class)) { if (method_exists($addons[$class], 'import')) { $addons[$class]->import($importData); } } else { if (!empty($import_functions[$class])) { if (is_array($import_functions[$class]) and is_callable($import_functions[$class]) or !is_array($import_functions[$class]) and function_exists($import_functions[$class])) { call_user_func($import_functions[$class], $importData, $addons_data[$class]); } } } } // [/addons import] // Page Template if ('page' == $articleData['post_type'] and wp_all_import_is_update_cf('_wp_page_template', $this->options) and (!empty($this->options['page_template']) or "no" == $this->options['is_multiple_page_template'])) { update_post_meta($pid, '_wp_page_template', "no" == $this->options['is_multiple_page_template'] ? $page_template[$i] : $this->options['page_template']); } // [featured image] $is_allow_import_images = apply_filters('wp_all_import_is_allow_import_images', false, $articleData['post_type']); if (!empty($uploads) and false === $uploads['error'] and ($articleData['post_type'] == "product" and class_exists('PMWI_Plugin') or $is_allow_import_images) and (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_images'])) { if (!empty($images_bundle)) { $is_show_add_new_images = apply_filters('wp_all_import_is_show_add_new_images', true, $post_type[$i]); foreach ($images_bundle as $slug => $bundle_data) { $featured_images = $bundle_data['files']; $option_slug = $slug == 'pmxi_gallery_image' ? '' : $slug; if (!empty($featured_images[$i])) { $targetDir = $uploads['path']; $targetUrl = $uploads['url']; $logger and call_user_func($logger, __('<b>IMAGES:</b>', 'wp_all_import_plugin')); if (!@is_writable($targetDir)) { $logger and call_user_func($logger, sprintf(__('<b>ERROR</b>: Target directory %s is not writable', 'wp_all_import_plugin'), $targetDir)); } else { require_once ABSPATH . 'wp-admin/includes/image.php'; $success_images = false; $gallery_attachment_ids = array(); $imgs = array(); $featured_delim = "yes" == $this->options[$option_slug . 'download_images'] ? $this->options[$option_slug . 'download_featured_delim'] : $this->options[$option_slug . 'featured_delim']; $line_imgs = explode("\n", $featured_images[$i]); if (!empty($line_imgs)) { foreach ($line_imgs as $line_img) { $imgs = array_merge($imgs, !empty($featured_delim) ? str_getcsv($line_img, $featured_delim) : array($line_img)); } } // keep existing and add newest images if (!empty($articleData['ID']) and $this->options['is_update_images'] and $this->options['update_images_logic'] == "add_new" and $this->options['update_all_data'] == "no" and $is_show_add_new_images) { $logger and call_user_func($logger, __('- Keep existing and add newest images ...', 'wp_all_import_plugin')); $attachment_imgs = get_attached_media('image', $pid); if ($post_type[$i] == "product") { $gallery_attachment_ids = array_filter(explode(",", get_post_meta($pid, '_product_image_gallery', true))); } if ($attachment_imgs) { foreach ($attachment_imgs as $attachment_img) { $post_thumbnail_id = get_post_thumbnail_id($pid); if (empty($post_thumbnail_id) and $this->options[$option_slug . 'is_featured']) { set_post_thumbnail($pid, $attachment_img->ID); } elseif (!in_array($attachment_img->ID, $gallery_attachment_ids) and $post_thumbnail_id != $attachment_img->ID) { $gallery_attachment_ids[] = $attachment_img->ID; } } $success_images = true; } if (!empty($gallery_attachment_ids)) { foreach ($gallery_attachment_ids as $aid) { do_action($slug, $pid, $aid, wp_get_attachment_url($aid), 'update_images'); } } } if (!empty($imgs)) { if ($this->options[$option_slug . 'set_image_meta_title'] and !empty($image_meta_titles_bundle[$slug])) { $img_titles = array(); $line_img_titles = explode("\n", $image_meta_titles_bundle[$slug][$i]); if (!empty($line_img_titles)) { foreach ($line_img_titles as $line_img_title) { $img_titles = array_merge($img_titles, !empty($this->options[$option_slug . 'image_meta_title_delim']) ? str_getcsv($line_img_title, $this->options[$option_slug . 'image_meta_title_delim']) : array($line_img_title)); } } } if ($this->options[$option_slug . 'set_image_meta_caption'] and !empty($image_meta_captions_bundle[$slug])) { $img_captions = array(); $line_img_captions = explode("\n", $image_meta_captions_bundle[$slug][$i]); if (!empty($line_img_captions)) { foreach ($line_img_captions as $line_img_caption) { $img_captions = array_merge($img_captions, !empty($this->options[$option_slug . 'image_meta_caption_delim']) ? str_getcsv($line_img_caption, $this->options[$option_slug . 'image_meta_caption_delim']) : array($line_img_caption)); } } } if ($this->options[$option_slug . 'set_image_meta_alt'] and !empty($image_meta_alts_bundle[$slug])) { $img_alts = array(); $line_img_alts = explode("\n", $image_meta_alts_bundle[$slug][$i]); if (!empty($line_img_alts)) { foreach ($line_img_alts as $line_img_alt) { $img_alts = array_merge($img_alts, !empty($this->options[$option_slug . 'image_meta_alt_delim']) ? str_getcsv($line_img_alt, $this->options[$option_slug . 'image_meta_alt_delim']) : array($line_img_alt)); } } } if ($this->options[$option_slug . 'set_image_meta_description'] and !empty($image_meta_descriptions_bundle[$slug])) { $img_descriptions = array(); $line_img_descriptions = explode("\n", $image_meta_descriptions_bundle[$slug][$i]); if (!empty($line_img_descriptions)) { foreach ($line_img_descriptions as $line_img_description) { $img_descriptions = array_merge($img_descriptions, !empty($this->options[$option_slug . 'image_meta_description_delim']) ? str_getcsv($line_img_description, $this->options[$option_slug . 'image_meta_description_delim']) : array($line_img_description)); } } } $is_keep_existing_images = (!empty($articleData['ID']) and $this->options['is_update_images'] and $this->options['update_images_logic'] == "add_new" and $this->options['update_all_data'] == "no" and $is_show_add_new_images); foreach ($imgs as $k => $img_url) { if (empty($img_url)) { continue; } $attid = false; $attch = null; $url = str_replace(" ", "%20", trim($img_url)); $bn = basename($url); if ("yes" == $this->options[$option_slug . 'download_images'] and !empty($auto_extensions_bundle[$slug][$i]) and preg_match('%^(jpg|jpeg|png|gif)$%i', $auto_extensions_bundle[$slug][$i])) { $img_ext = $auto_extensions_bundle[$slug][$i]; } else { $img_ext = pmxi_getExtensionFromStr($url); $default_extension = pmxi_getExtension($bn); if ($img_ext == "") { $img_ext = pmxi_get_remote_image_ext($url); } } $logger and call_user_func($logger, sprintf(__('- Importing image `%s` for `%s` ...', 'wp_all_import_plugin'), $img_url, $articleData['post_title'])); // generate local file name $image_name = urldecode(($this->options[$option_slug . 'auto_rename_images'] and !empty($auto_rename_images_bundle[$slug][$i])) ? sanitize_file_name($img_ext ? str_replace("." . $default_extension, "", $auto_rename_images_bundle[$slug][$i]) : $auto_rename_images_bundle[$slug][$i]) : sanitize_file_name($img_ext ? str_replace("." . $default_extension, "", $bn) : $bn)) . ("" != $img_ext ? '.' . $img_ext : ''); // if wizard store image data to custom field $create_image = false; $download_image = true; $wp_filetype = false; if ($bundle_data['type'] == 'images' and base64_decode($url, true) !== false) { $img = @imagecreatefromstring(base64_decode($url)); if ($img) { $logger and call_user_func($logger, __('- Founded base64_encoded image', 'wp_all_import_plugin')); $image_filename = md5(time()) . '.jpg'; $image_filepath = $targetDir . '/' . $image_filename; imagejpeg($img, $image_filepath); if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $image_filepath)); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } else { $create_image = true; } } } else { $image_filename = wp_unique_filename($targetDir, $image_name); $image_filepath = $targetDir . '/' . $image_filename; // keep existing and add newest images if ($is_keep_existing_images) { $attch = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->posts . " WHERE (post_title = %s OR post_title = %s OR post_name = %s) AND post_type = %s AND post_parent = %d;", $image_name, preg_replace('/\\.[^.\\s]{3,4}$/', '', $image_name), sanitize_title($image_name), "attachment", $pid)); if ($attch != null) { $post_thumbnail_id = get_post_thumbnail_id($pid); if ($post_thumbnail_id == $attch->ID or in_array($attch->ID, $gallery_attachment_ids)) { continue; } } elseif (file_exists($targetDir . '/' . $image_name)) { if ($bundle_data['type'] == 'images' and $img_meta = wp_read_image_metadata($targetDir . '/' . $image_name)) { if (trim($img_meta['title']) && !is_numeric(sanitize_title($img_meta['title']))) { $img_title = $img_meta['title']; $attch = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->posts . " WHERE post_title = %s AND post_type = %s AND post_parent = %d;", $img_title, "attachment", $pid)); if ($attch != null) { $post_thumbnail_id = get_post_thumbnail_id($pid); if ($post_thumbnail_id == $attch->ID or in_array($attch->ID, $gallery_attachment_ids)) { continue; } } } } } } // search existing attachment if ($this->options[$option_slug . 'search_existing_images']) { $image_filename = $image_name; $attch = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->posts . " WHERE (post_title = %s OR post_title = %s OR post_name = %s) AND post_type = %s;", $image_name, preg_replace('/\\.[^.\\s]{3,4}$/', '', $image_name), sanitize_title($image_name), "attachment")); if ($attch != null) { $download_image = false; $attid = $attch->ID; } elseif (@file_exists($targetDir . '/' . $image_name)) { if ($bundle_data['type'] == 'images' and $img_meta = wp_read_image_metadata($targetDir . '/' . $image_name)) { if (trim($img_meta['title']) && !is_numeric(sanitize_title($img_meta['title']))) { $img_title = $img_meta['title']; $attch = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->posts . " WHERE post_title = %s AND post_type = %s AND post_parent = %d;", $img_title, "attachment", $pid)); if ($attch != null) { $download_image = false; $attid = $attch->ID; } } } } } if ($download_image) { // do not download images if ("yes" != $this->options[$option_slug . 'download_images']) { $image_filename = $image_name; $image_filepath = $targetDir . '/' . $image_filename; $wpai_uploads = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR; $wpai_image_path = $wpai_uploads . str_replace('%20', ' ', $url); $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` in `%s` folder', 'wp_all_import_plugin'), $wpai_image_path, $wpai_uploads)); if (@file_exists($wpai_image_path) and @copy($wpai_image_path, $image_filepath)) { $download_image = false; // valdate import attachments if ($bundle_data['type'] == 'files') { if (!($wp_filetype = wp_check_filetype(basename($image_filepath), null))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($image_filepath))); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; @unlink($image_filepath); } else { $create_image = true; $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully founded', 'wp_all_import_plugin'), $wpai_image_path)); } } elseif ($bundle_data['type'] == 'images') { if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $image_filepath)); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; @unlink($image_filepath); } else { $create_image = true; $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully founded', 'wp_all_import_plugin'), $wpai_image_path)); } } } } else { $logger and call_user_func($logger, sprintf(__('- Downloading image from `%s`', 'wp_all_import_plugin'), $url)); $request = get_file_curl($url, $image_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) { @unlink($image_filepath); // delete file since failed upload may result in empty file created } else { if ($bundle_data['type'] == 'images') { if ($image_info = @getimagesize($image_filepath) and in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $create_image = true; $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url)); } } elseif ($bundle_data['type'] == 'files') { if ($wp_filetype = wp_check_filetype(basename($image_filepath), null)) { $create_image = true; $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url)); } } } if (!$create_image) { $url = str_replace(" ", "%20", trim(pmxi_convert_encoding($img_url))); $request = get_file_curl($url, $image_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s cannot be saved locally as %s', 'wp_all_import_plugin'), $url, $image_filepath)); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; @unlink($image_filepath); // delete file since failed upload may result in empty file created } else { if ($bundle_data['type'] == 'images') { if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $url)); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; @unlink($image_filepath); } else { $create_image = true; $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url)); } } elseif ($bundle_data['type'] == 'files') { if (!($wp_filetype = wp_check_filetype(basename($image_filepath), null))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($url))); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; @unlink($image_filepath); } else { $create_image = true; $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully founded', 'wp_all_import_plugin'), $url)); } } } } } } } if ($create_image) { $logger and call_user_func($logger, sprintf(__('- Creating an attachment for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . $image_filename)); $attachment = array('post_mime_type' => $bundle_data['type'] == 'images' ? image_type_to_mime_type($image_info[2]) : $wp_filetype['type'], 'guid' => $targetUrl . '/' . $image_filename, 'post_title' => $image_name, 'post_content' => '', 'post_author' => $post_author[$i]); if ($bundle_data['type'] == 'images' and $image_meta = wp_read_image_metadata($image_filepath)) { if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) { $attachment['post_title'] = $image_meta['title']; } if (trim($image_meta['caption'])) { $attachment['post_content'] = $image_meta['caption']; } } $attid = wp_insert_attachment($attachment, $image_filepath, $pid); if (is_wp_error($attid)) { $logger and call_user_func($logger, __('- <b>WARNING</b>', 'wp_all_import_plugin') . ': ' . $attid->get_error_message()); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } else { // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once ABSPATH . 'wp-admin/includes/image.php'; wp_update_attachment_metadata($attid, wp_generate_attachment_metadata($attid, $image_filepath)); $update_attachment_meta = array(); if ($this->options[$option_slug . 'set_image_meta_title'] and !empty($img_titles[$k])) { $update_attachment_meta['post_title'] = $img_titles[$k]; } if ($this->options[$option_slug . 'set_image_meta_caption'] and !empty($img_captions[$k])) { $update_attachment_meta['post_excerpt'] = $img_captions[$k]; } if ($this->options[$option_slug . 'set_image_meta_description'] and !empty($img_descriptions[$k])) { $update_attachment_meta['post_content'] = $img_descriptions[$k]; } if ($this->options[$option_slug . 'set_image_meta_alt'] and !empty($img_alts[$k])) { update_post_meta($attid, '_wp_attachment_image_alt', $img_alts[$k]); } if (!empty($update_attachment_meta)) { $this->wpdb->update($this->wpdb->posts, $update_attachment_meta, array('ID' => $attid)); } } } if ($attid) { if ($attch != null and empty($attch->post_parent)) { wp_update_post(array('ID' => $attch->ID, 'post_parent' => $pid)); } $logger and call_user_func($logger, __('- <b>ACTION</b>: ' . $slug, 'wp_all_import_plugin')); do_action($slug, $pid, $attid, $image_filepath, $is_keep_existing_images ? 'add_images' : 'update_images'); $success_images = true; $post_thumbnail_id = get_post_thumbnail_id($pid); if ($bundle_data['type'] == 'images' and empty($post_thumbnail_id) and $this->options[$option_slug . 'is_featured']) { set_post_thumbnail($pid, $attid); } elseif (!in_array($attid, $gallery_attachment_ids) and $post_thumbnail_id != $attid) { $gallery_attachment_ids[] = $attid; } $logger and call_user_func($logger, sprintf(__('- Attachment has been successfully created for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . $image_filename)); } } } // Set product gallery images if ($post_type[$i] == "product") { update_post_meta($pid, '_product_image_gallery', !empty($gallery_attachment_ids) ? implode(',', $gallery_attachment_ids) : ''); } // Create entry as Draft if no images are downloaded successfully if (!$success_images and "yes" == $this->options[$option_slug . 'create_draft']) { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $pid)); $logger and call_user_func($logger, sprintf(__('- Post `%s` saved as Draft, because no images are downloaded successfully', 'wp_all_import_plugin'), $articleData['post_title'])); } } } else { // Create entry as Draft if no images are downloaded successfully if ("yes" == $this->options[$option_slug . 'create_draft']) { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $pid)); $logger and call_user_func($logger, sprintf(__('Post `%s` saved as Draft, because no images are downloaded successfully', 'wp_all_import_plugin'), $articleData['post_title'])); } } } } } else { if (!empty($images_bundle)) { foreach ($images_bundle as $slug => $bundle_data) { if (!empty($bundle_data['images'][$i])) { $imgs = array(); $featured_delim = "yes" == $this->options[$option_slug . 'download_images'] ? $this->options[$option_slug . 'download_featured_delim'] : $this->options[$option_slug . 'featured_delim']; $line_imgs = explode("\n", $bundle_data['images'][$i]); if (!empty($line_imgs)) { foreach ($line_imgs as $line_img) { $imgs = array_merge($imgs, !empty($featured_delim) ? str_getcsv($line_img, $featured_delim) : array($line_img)); } } foreach ($imgs as $img) { do_action($slug, $pid, false, $img, false); } } } } } // [/featured image] // [attachments] if (!empty($uploads) and false === $uploads['error'] and !empty($attachments[$i]) and (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_attachments'])) { $targetDir = $uploads['path']; $targetUrl = $uploads['url']; $logger and call_user_func($logger, __('<b>ATTACHMENTS:</b>', 'wp_all_import_plugin')); if (!@is_writable($targetDir)) { $logger and call_user_func($logger, sprintf(__('- <b>ERROR</b>: Target directory %s is not writable', 'wp_all_import_plugin'), trim($targetDir))); } else { // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once ABSPATH . 'wp-admin/includes/image.php'; if (!is_array($attachments[$i])) { $attachments[$i] = array($attachments[$i]); } $logger and call_user_func($logger, sprintf(__('- Importing attachments for `%s` ...', 'wp_all_import_plugin'), $articleData['post_title'])); foreach ($attachments[$i] as $attachment) { if ("" == $attachment) { continue; } $atchs = str_getcsv($attachment, $this->options['atch_delim']); if (!empty($atchs)) { foreach ($atchs as $atch_url) { if (empty($atch_url)) { continue; } $download_file = true; $atch_url = str_replace(" ", "%20", trim($atch_url)); $attachment_filename = urldecode(basename(parse_url(trim($atch_url), PHP_URL_PATH))); $attachment_filepath = $targetDir . '/' . sanitize_file_name($attachment_filename); if ($this->options['is_search_existing_attach']) { // search existing attachment $attch = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->posts . " WHERE (post_title = %s OR post_title = %s OR post_name = %s OR post_name = %s) AND post_type = %s;", $attachment_filename, preg_replace('/\\.[^.\\s]{3,4}$/', '', $attachment_filename), sanitize_title($attachment_filename), sanitize_title(preg_replace('/\\.[^.\\s]{3,4}$/', '', $attachment_filename)), "attachment")); if ($attch != null) { $download_file = false; $attach_id = $attch->ID; } } if ($download_file) { $attachment_filename = wp_unique_filename($targetDir, $attachment_filename); $attachment_filepath = $targetDir . '/' . sanitize_file_name($attachment_filename); $logger and call_user_func($logger, sprintf(__('- Filename for attachment was generated as %s', 'wp_all_import_plugin'), $attachment_filename)); $request = get_file_curl(trim($atch_url), $attachment_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($attachment_filepath, @file_get_contents(trim($atch_url)))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Attachment file %s cannot be saved locally as %s', 'wp_all_import_plugin'), trim($atch_url), $attachment_filepath)); is_wp_error($request) and $logger and call_user_func($logger, sprintf(__('- <b>WP Error</b>: %s', 'wp_all_import_plugin'), $request->get_error_message())); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; unlink($attachment_filepath); // delete file since failed upload may result in empty file created } elseif (!($wp_filetype = wp_check_filetype(basename($attachment_filename), null))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($atch_url))); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } else { $logger and call_user_func($logger, sprintf(__('- File %s has been successfully downloaded', 'wp_all_import_plugin'), $atch_url)); $attachment_data = array('guid' => $targetUrl . '/' . basename($attachment_filepath), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($attachment_filepath)), 'post_content' => '', 'post_status' => 'inherit', 'post_author' => $post_author[$i]); $attach_id = wp_insert_attachment($attachment_data, $attachment_filepath, $pid); if (is_wp_error($attach_id)) { $logger and call_user_func($logger, __('- <b>WARNING</b>', 'wp_all_import_plugin') . ': ' . $pid->get_error_message()); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } else { wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attachment_filepath)); $logger and call_user_func($logger, sprintf(__('- Attachment has been successfully created for post `%s`', 'wp_all_import_plugin'), $articleData['post_title'])); $logger and call_user_func($logger, __('- <b>ACTION</b>: pmxi_attachment_uploaded', 'wp_all_import_plugin')); do_action('pmxi_attachment_uploaded', $pid, $attach_id, $attachment_filepath); } } } else { $logger and call_user_func($logger, __('- <b>ACTION</b>: pmxi_attachment_uploaded', 'wp_all_import_plugin')); do_action('pmxi_attachment_uploaded', $pid, $attach_id, $attachment_filepath); } } } } } } // [/attachments] // [custom taxonomies] if (!empty($taxonomies)) { $logger and call_user_func($logger, __('<b>TAXONOMIES:</b>', 'wp_all_import_plugin')); foreach ($taxonomies as $tx_name => $txes) { // Skip updating product attributes if (PMXI_Admin_Addons::get_addon('PMWI_Plugin') and strpos($tx_name, "pa_") === 0) { continue; } if (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_categories']) { $logger and call_user_func($logger, sprintf(__('- Importing taxonomy `%s` ...', 'wp_all_import_plugin'), $tx_name)); if (!empty($this->options['tax_logic'][$tx_name]) and $this->options['tax_logic'][$tx_name] == 'hierarchical' and !empty($this->options['tax_hierarchical_logic'][$tx_name]) and $this->options['tax_hierarchical_logic'][$tx_name] == 'entire') { $logger and call_user_func($logger, sprintf(__('- Auto-nest enabled with separator `%s` ...', 'wp_all_import_plugin'), !empty($this->options['tax_hierarchical_delim'][$tx_name]) ? $this->options['tax_hierarchical_delim'][$tx_name] : ',')); } if (!empty($articleData['ID'])) { if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "all_except" and !empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and in_array($tx_name, $this->options['taxonomies_list'])) { $logger and call_user_func($logger, sprintf(__('- %s %s `%s` has been skipped attempted to `Leave these taxonomies alone, update all others`...', 'wp_all_import_plugin'), $custom_type_details->labels->singular_name, $tx_name, $single_tax['name'])); continue; } if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "only" and (!empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and !in_array($tx_name, $this->options['taxonomies_list']) or empty($this->options['taxonomies_list']))) { $logger and call_user_func($logger, sprintf(__('- %s %s `%s` has been skipped attempted to `Update only these taxonomies, leave the rest alone`...', 'wp_all_import_plugin'), $custom_type_details->labels->singular_name, $tx_name, $single_tax['name'])); continue; } } $assign_taxes = array(); if ($this->options['update_categories_logic'] == "add_new" and !empty($existing_taxonomies[$tx_name][$i])) { $assign_taxes = $existing_taxonomies[$tx_name][$i]; unset($existing_taxonomies[$tx_name][$i]); } elseif (!empty($existing_taxonomies[$tx_name][$i])) { unset($existing_taxonomies[$tx_name][$i]); } // create term if not exists if (!empty($txes[$i])) { foreach ($txes[$i] as $key => $single_tax) { $is_created_term = false; if (is_array($single_tax) and !empty($single_tax['name'])) { $parent_id = !empty($single_tax['parent']) ? pmxi_recursion_taxes($single_tax['parent'], $tx_name, $txes[$i], $key) : ''; $term = empty($this->options['tax_is_full_search_' . $this->options['tax_logic'][$tx_name]][$tx_name]) ? term_exists($single_tax['name'], $tx_name, (int) $parent_id) : term_exists($single_tax['name'], $tx_name); if (empty($term) and !is_wp_error($term)) { $term = empty($this->options['tax_is_full_search_' . $this->options['tax_logic'][$tx_name]][$tx_name]) ? term_exists(htmlspecialchars($single_tax['name']), $tx_name, (int) $parent_id) : term_exists(htmlspecialchars($single_tax['name']), $tx_name); if (empty($term) and !is_wp_error($term)) { $term_attr = array('parent' => !empty($parent_id) ? $parent_id : 0); $term = wp_insert_term($single_tax['name'], $tx_name, $term_attr); if (!is_wp_error($term)) { $is_created_term = true; if (empty($parent_id)) { $logger and call_user_func($logger, sprintf(__('- Creating parent %s %s `%s` ...', 'wp_all_import_plugin'), $custom_type_details->labels->singular_name, $tx_name, $single_tax['name'])); } else { $logger and call_user_func($logger, sprintf(__('- Creating child %s %s for %s named `%s` ...', 'wp_all_import_plugin'), $custom_type_details->labels->singular_name, $tx_name, is_array($single_tax['parent']) ? $single_tax['parent']['name'] : $single_tax['parent'], $single_tax['name'])); } } } } if (is_wp_error($term)) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: `%s`', 'wp_all_import_plugin'), $term->get_error_message())); $logger and !$is_cron and PMXI_Plugin::$session->warnings++; } elseif (!empty($term)) { $cat_id = $term['term_id']; if ($cat_id and $single_tax['assign']) { $term = get_term_by('id', $cat_id, $tx_name); if ($term->parent != '0' and !empty($this->options['tax_is_full_search_' . $this->options['tax_logic'][$tx_name]][$tx_name]) and empty($this->options['tax_assign_to_one_term_' . $this->options['tax_logic'][$tx_name]][$tx_name])) { $parent_ids = wp_all_import_get_parent_terms($cat_id, $tx_name); if (!empty($parent_ids)) { foreach ($parent_ids as $p) { if (!in_array($p, $assign_taxes)) { $assign_taxes[] = $p; } } } } if (!in_array($term->term_taxonomy_id, $assign_taxes)) { $assign_taxes[] = $term->term_taxonomy_id; } if (!$is_created_term) { if (empty($parent_id)) { $logger and call_user_func($logger, sprintf(__('- Attempted to create parent %s %s `%s`, duplicate detected. Importing %s to existing `%s` %s, ID %d, slug `%s` ...', 'wp_all_import_plugin'), $custom_type_details->labels->singular_name, $tx_name, $single_tax['name'], $custom_type_details->labels->singular_name, $term->name, $tx_name, $term->term_id, $term->slug)); } else { $logger and call_user_func($logger, sprintf(__('- Attempted to create child %s %s `%s`, duplicate detected. Importing %s to existing `%s` %s, ID %d, slug `%s` ...', 'wp_all_import_plugin'), $custom_type_details->labels->singular_name, $tx_name, $single_tax['name'], $custom_type_details->labels->singular_name, $term->name, $tx_name, $term->term_id, $term->slug)); } } } } } } } // associate taxes with post $this->associate_terms($pid, empty($assign_taxes) ? false : $assign_taxes, $tx_name, $logger, $is_cron); } else { $logger and call_user_func($logger, sprintf(__('- %s %s `%s` has been skipped attempted to `Do not update Taxonomies (incl. Categories and Tags)`...', 'wp_all_import_plugin'), $custom_type_details->labels->singular_name, $tx_name, $single_tax['name'])); } } if ($this->options['update_all_data'] == "no" and ($this->options['is_update_categories'] and $this->options['update_categories_logic'] != 'full_update' or !$this->options['is_update_categories'] and (is_object_in_taxonomy($post_type[$i], 'category') or is_object_in_taxonomy($post_type[$i], 'post_tag')))) { if (!empty($existing_taxonomies)) { foreach ($existing_taxonomies as $tx_name => $txes) { // Skip updating product attributes if (PMXI_Admin_Addons::get_addon('PMWI_Plugin') and strpos($tx_name, "pa_") === 0) { continue; } if (!empty($txes[$i])) { $this->associate_terms($pid, $txes[$i], $tx_name, $logger, $is_cron); } } } } } // [/custom taxonomies] if (empty($articleData['ID'])) { $logger and call_user_func($logger, sprintf(__('<b>CREATED</b> `%s` `%s` (ID: %s)', 'wp_all_import_plugin'), $articleData['post_title'], $custom_type_details->labels->singular_name, $pid)); } else { $logger and call_user_func($logger, sprintf(__('<b>UPDATED</b> `%s` `%s` (ID: %s)', 'wp_all_import_plugin'), $articleData['post_title'], $custom_type_details->labels->singular_name, $pid)); } // fire important hooks after custom fields are added if (!$this->options['is_fast_mode'] and $this->options['custom_type'] != 'import_users') { $post_object = get_post($pid); $is_update = !empty($articleData['ID']); do_action("save_post_" . $articleData['post_type'], $pid, $post_object, $is_update); do_action('save_post', $pid, $post_object, $is_update); do_action('wp_insert_post', $pid, $post_object, $is_update); } // [addons import] // prepare data for import $importData = array('pid' => $pid, 'import' => $this, 'logger' => $logger); $saved_functions = apply_filters('wp_all_import_addon_saved_post', array()); // deligate operation to addons foreach (PMXI_Admin_Addons::get_active_addons() as $class) { if (class_exists($class)) { if (method_exists($addons[$class], 'saved_post')) { $addons[$class]->saved_post($importData); } } else { if (!empty($saved_functions[$class])) { if (is_array($saved_functions[$class]) and is_callable($saved_functions[$class]) or !is_array($saved_functions[$class]) and function_exists($saved_functions[$class])) { call_user_func($saved_functions[$class], $importData); } } } } // [/addons import] $logger and call_user_func($logger, __('<b>ACTION</b>: pmxi_saved_post', 'wp_all_import_plugin')); do_action('pmxi_saved_post', $pid, $rootNodes[$i]); // hook that was triggered immediately after post saved if (empty($articleData['ID'])) { $created++; } else { $updated++; } if (!$is_cron and "default" == $this->options['import_processing']) { $processed_records = $created + $updated + $skipped; $logger and call_user_func($logger, sprintf(__('<span class="processing_info"><span class="created_count">%s</span><span class="updated_count">%s</span><span class="percents_count">%s</span></span>', 'wp_all_import_plugin'), $created, $updated, ceil($processed_records / $this->count * 100))); } } $logger and call_user_func($logger, __('<b>ACTION</b>: pmxi_after_post_import', 'wp_all_import_plugin')); do_action('pmxi_after_post_import', $this->id); $logger and !$is_cron and PMXI_Plugin::$session->chunk_number++; } wp_cache_flush(); $this->set(array('imported' => $created + $updated, 'created' => $created, 'updated' => $updated, 'skipped' => $skipped, 'last_activity' => date('Y-m-d H:i:s')))->update(); if (!$is_cron) { PMXI_Plugin::$session->save_data(); $records_count = $this->created + $this->updated + $this->skipped; $is_import_complete = $records_count == $this->count; // Delete posts that are no longer present in your file if ($is_import_complete and !empty($this->options['is_delete_missing']) and $this->options['duplicate_matching'] == 'auto') { $logger and call_user_func($logger, __('Removing previously imported posts which are no longer actual...', 'wp_all_import_plugin')); $postList = new PMXI_Post_List(); $missing_ids = array(); $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration)); if (!$missingPosts->isEmpty()) { foreach ($missingPosts as $missingPost) { $missing_ids[] = $missingPost['post_id']; } } // Delete posts from database if (!empty($missing_ids) && is_array($missing_ids)) { $logger and call_user_func($logger, __('<b>ACTION</b>: pmxi_delete_post', 'wp_all_import_plugin')); $logger and call_user_func($logger, __('Deleting posts from database', 'wp_all_import_plugin')); $missing_ids_arr = array_chunk($missing_ids, 100); foreach ($missing_ids_arr as $key => $ids) { if (!empty($ids)) { foreach ($ids as $k => $id) { $to_delete = true; // Instead of deletion, set Custom Field if ($this->options['is_update_missing_cf']) { update_post_meta($id, $this->options['update_missing_cf_name'], $this->options['update_missing_cf_value']); $to_delete = false; $logger and call_user_func($logger, sprintf(__('Instead of deletion post with ID `%s`, set Custom Field `%s` to value `%s`', 'wp_all_import_plugin'), $id, $this->options['update_missing_cf_name'], $this->options['update_missing_cf_value'])); } // Instead of deletion, change post status to Draft if ($this->options['set_missing_to_draft']) { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $id)); $to_delete = false; $logger and call_user_func($logger, sprintf(__('Instead of deletion, change post with ID `%s` status to Draft', 'wp_all_import_plugin'), $id)); } if ($to_delete) { // Remove attachments empty($this->options['is_keep_attachments']) and wp_delete_attachments($id, true, 'files'); // Remove images empty($this->options['is_keep_imgs']) and wp_delete_attachments($id, true, 'images'); // Clear post's relationships if ($post_type[$i] != "import_users") { wp_delete_object_term_relationships($id, get_object_taxonomies('' != $this->options['custom_type'] ? $this->options['custom_type'] : 'post')); } } else { unset($ids[$k]); } } if (!empty($ids)) { do_action('pmxi_delete_post', $ids); if ($this->options['custom_type'] == "import_users") { $sql = "delete a,b\n\t\t\t\t\t\t\t\t\t\tFROM " . $this->wpdb->users . " a\n\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->usermeta . " b ON ( a.ID = b.user_id )\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } else { $sql = "delete a,b,c\n\t\t\t\t\t\t\t\t\t\tFROM " . $this->wpdb->posts . " a\n\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->postmeta . " c ON ( a.ID = c.post_id )\t\t\t\t\n\t\t\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } $this->wpdb->query($sql); // Delete record form pmxi_posts $sql = "DELETE FROM " . PMXI_Plugin::getInstance()->getTablePrefix() . "posts WHERE post_id IN (" . implode(',', $ids) . ") AND import_id = %d"; $this->wpdb->query($this->wpdb->prepare($sql, $this->id)); $this->set(array('deleted' => $this->deleted + count($ids)))->update(); } } } } } // Set out of stock status for missing records [Woocommerce add-on option] if ($is_import_complete and empty($this->options['is_delete_missing']) and $post_type[$i] == "product" and class_exists('PMWI_Plugin') and !empty($this->options['missing_records_stock_status'])) { $logger and call_user_func($logger, __('Update stock status previously imported posts which are no longer actual...', 'wp_all_import_plugin')); $postList = new PMXI_Post_List(); $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration)); if (!$missingPosts->isEmpty()) { foreach ($missingPosts as $missingPost) { update_post_meta($missingPost['post_id'], '_stock_status', 'outofstock'); update_post_meta($missingPost['post_id'], '_stock', 0); $missingPostRecord = new PMXI_Post_Record(); $missingPostRecord->getBy('id', $missingPost['id']); if (!$missingPostRecord->isEmpty()) { $missingPostRecord->set(array('iteration' => $this->iteration))->update(); } unset($missingPostRecord); } } } } } catch (XmlImportException $e) { $logger and call_user_func($logger, __('<b>ERROR</b>', 'wp_all_import_plugin') . ': ' . $e->getMessage()); $logger and !$is_cron and PMXI_Plugin::$session->errors++; } $logger and $is_import_complete and call_user_func($logger, __('Cleaning temporary data...', 'wp_all_import_plugin')); foreach ($tmp_files as $file) { // remove all temporary files created @unlink($file); } if (($is_cron or $is_import_complete) and $this->options['is_delete_source']) { $logger and call_user_func($logger, __('Deleting source XML file...', 'wp_all_import_plugin')); // Delete chunks foreach (PMXI_Helper::safe_glob($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY . DIRECTORY_SEPARATOR . 'pmxi_chunk_*', PMXI_Helper::GLOB_RECURSE | PMXI_Helper::GLOB_PATH) as $filePath) { $logger and call_user_func($logger, __('Deleting chunks files...', 'wp_all_import_plugin')); @file_exists($filePath) and wp_all_import_remove_source($filePath, false); } if ($this->type != "ftp") { if (!@unlink($this->path)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to remove %s', 'wp_all_import_plugin'), $this->path)); } } else { $file_path_array = PMXI_Helper::safe_glob($this->path, PMXI_Helper::GLOB_NODIR | PMXI_Helper::GLOB_PATH); if (!empty($file_path_array)) { foreach ($file_path_array as $path) { if (!@unlink($path)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to remove %s', 'wp_all_import_plugin'), $path)); } } } } } if (!$is_cron and $is_import_complete) { $this->set(array('processing' => 0, 'triggered' => 0, 'queue_chunk_number' => 0, 'registered_on' => date('Y-m-d H:i:s'), 'iteration' => ++$this->iteration))->update(); $logger and call_user_func($logger, 'Done'); } remove_filter('user_has_cap', array($this, '_filter_has_cap_unfiltered_html')); kses_init(); // return any filtering rules back if they has been disabled for import procedure return $this; }
public function delete_missing_records($logger, $iteration) { if (!empty($this->options['is_delete_missing']) and $this->options['duplicate_matching'] == 'auto') { empty($this->deleted) and $logger and call_user_func($logger, __('Removing previously imported posts which are no longer actual...', 'wp_all_import_plugin')); $postList = new PMXI_Post_List(); $args = array('import_id' => $this->id, 'iteration !=' => $iteration); if (!empty($this->options['is_import_specified'])) { $args['specified'] = 1; } $missing_ids = array(); $missingPosts = $postList->getBy($args); if (!$missingPosts->isEmpty()) { foreach ($missingPosts as $missingPost) { $missing_ids[] = $missingPost; } } // Delete posts from database if (!empty($missing_ids) && is_array($missing_ids)) { $logger and call_user_func($logger, __('<b>ACTION</b>: pmxi_delete_post', 'wp_all_import_plugin')); $logger and call_user_func($logger, __('Deleting posts from database', 'wp_all_import_plugin')); $missing_ids_arr = array_chunk($missing_ids, $this->options['records_per_request']); foreach ($missing_ids_arr as $key => $missingPostRecords) { if (!empty($missingPostRecords)) { foreach ($missingPostRecords as $k => $missingPostRecord) { $to_delete = true; // Instead of deletion, set Custom Field if ($this->options['is_update_missing_cf']) { update_post_meta($missingPostRecord['post_id'], $this->options['update_missing_cf_name'], $this->options['update_missing_cf_value']); $to_delete = false; $logger and call_user_func($logger, sprintf(__('Instead of deletion post with ID `%s`, set Custom Field `%s` to value `%s`', 'wp_all_import_plugin'), $missingPostRecord['post_id'], $this->options['update_missing_cf_name'], $this->options['update_missing_cf_value'])); } // Instead of deletion, change post status to Draft if ($this->options['set_missing_to_draft']) { if ($final_post_type = get_post_type($missingPostRecord['post_id']) and $final_post_type != 'product_variation') { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $missingPostRecord['post_id'])); $logger and call_user_func($logger, sprintf(__('Instead of deletion, change post with ID `%s` status to Draft', 'wp_all_import_plugin'), $missingPostRecord['post_id'])); } $to_delete = false; } $to_delete = apply_filters('wp_all_import_is_post_to_delete', $to_delete, $missingPostRecord['post_id'], $this); if ($to_delete) { // Remove attachments empty($this->options['is_keep_attachments']) and wp_delete_attachments($missingPostRecord['post_id'], true, 'files'); // Remove images empty($this->options['is_keep_imgs']) and wp_delete_attachments($missingPostRecord['post_id'], true, 'images'); // Clear post's relationships if ($this->options['custom_type'] != "import_users") { wp_delete_object_term_relationships($missingPostRecord['post_id'], get_object_taxonomies('' != $this->options['custom_type'] ? $this->options['custom_type'] : 'post')); } } else { $postRecord = new PMXI_Post_Record(); $postRecord->getById($missingPostRecord['id']); if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $iteration))->save(); } do_action('pmxi_missing_post', $missingPostRecord['post_id']); unset($missingPostRecords[$k]); } } if (!empty($missingPostRecords)) { $ids = array(); foreach ($missingPostRecords as $k => $missingPostRecord) { $ids[] = $missingPostRecord['post_id']; } do_action('pmxi_delete_post', $ids); if ($this->options['custom_type'] == "import_users") { $sql = "delete a,b\n\t\t\t\t\t\t\t\tFROM " . $this->wpdb->users . " a\n\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->usermeta . " b ON ( a.ID = b.user_id )\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } else { $sql = "delete a,b,c\n\t\t\t\t\t\t\t\tFROM " . $this->wpdb->posts . " a\n\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->postmeta . " c ON ( a.ID = c.post_id )\t\t\t\t\n\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } $this->wpdb->query($sql); // Delete record form pmxi_posts $sql = "DELETE FROM " . PMXI_Plugin::getInstance()->getTablePrefix() . "posts WHERE post_id IN (" . implode(',', $ids) . ") AND import_id = %d"; $this->wpdb->query($this->wpdb->prepare($sql, $this->id)); $this->set(array('deleted' => $this->deleted + count($ids)))->update(); $logger and call_user_func($logger, sprintf(__('%d Posts deleted from database. IDs (%s)', 'wp_all_import_plugin'), $this->deleted, implode(",", $ids))); } } if (PMXI_Plugin::is_ajax() and "ajax" == $this->options['import_processing']) { break; } } return (count($missing_ids_arr) > 1 and "ajax" == $this->options['import_processing']) ? false : true; } } return true; }
/** * * Fires after wp_insert_post/wp_update_post * */ public function import($post_id, $data, $import, $articleData, $logger) { // return if this is a basic post ( not a translation ) if (empty($import['options']['wpml_addon']['matching_logic']) or empty($import['options']['wpml_addon']['import'])) { return; } // search for post to translate $parentImport = new PMXI_Import_Record(); $parentImport->getById($import['options']['wpml_addon']['import']); if (!$parentImport->isEmpty()) { // post is that must be translated $parent_post_id = false; $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('unique_key' => $data['unique_key'], 'import_id' => $import['options']['wpml_addon']['import'])); if (!$postRecord->isEmpty()) { $parent_post_id = $postRecord->post_id; } if ($parent_post_id) { $post_type = in_array(get_post_type($post_id), array('product', 'product_variation')) ? 'post_product' : 'post_' . get_post_type($post_id); $trid = $this->wpml->get_element_trid($parent_post_id, $post_type); if ($trid) { global $wpdb; // sync translation slug $parent_post = get_post($parent_post_id); if (!empty($parent_post) and $parent_post->post_title == $articleData['post_title']) { $wpdb->update($wpdb->posts, array('post_name' => $parent_post->post_name), array('ID' => $post_id)); } // create a translation $tid = $this->wpml->set_element_language_details($post_id, $post_type, $trid, $import->options['wpml_addon']['lng'], $parentImport->options['wpml_addon']['lng']); if (is_wp_error($tid)) { $logger and call_user_func($logger, __('<b>ERROR</b>', 'wpml-all-import') . ': ' . $tid->get_error_message()); } else { $logger and call_user_func($logger, sprintf(__('- Created `%s` translation for `%s`', 'wpml-all-import'), $import->options['wpml_addon']['lng'], $parent_post->post_title)); } } } } }
/** * Export CSV helper */ function pmxe_export_csv($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0) { ob_start(); // Prepare headers $headers = array(); $stream = fopen("php://output", 'w'); $max_attach_count = 0; $max_images_count = 0; $cf = array(); $woo = array(); $woo_order = array(); $acfs = array(); $taxes = array(); $attributes = array(); $articles = array(); $implode_delimiter = $exportOptions['delimiter'] == ',' ? '|' : ','; while ($exportQuery->have_posts()) { $attach_count = 0; $images_count = 0; $exportQuery->the_post(); $record = get_post(get_the_ID()); $article = array(); $article['post_type'] = $record->post_type; $article['ID'] = apply_filters('pmxe_post_id', get_the_ID()); $article['permalink'] = get_permalink(); global $wpdb; $table_prefix = $wpdb->prefix; if ($exportOptions['ids']) { if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'])); if ($postRecord->isEmpty()) { $postRecord->set(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $record->ID, 'product_key' => get_post_meta($record->ID, '_sku', true)))->save(); } unset($postRecord); } foreach ($exportOptions['ids'] as $ID => $value) { if (is_numeric($ID)) { if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) { continue; } $element_name = !empty($exportOptions['cc_name'][$ID]) ? $exportOptions['cc_name'][$ID] : 'untitled_' . $ID; $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false; switch ($exportOptions['cc_type'][$ID]) { case 'id': $article[$element_name] = apply_filters('pmxe_post_id', pmxe_filter(get_the_ID(), $fieldSnipped), get_the_ID()); break; case 'permalink': $article[$element_name] = apply_filters('pmxe_post_guid', pmxe_filter(get_permalink(), $fieldSnipped), get_the_ID()); break; case 'post_type': $pType = get_post_type(); if ($pType == 'product_variation') { $pType = 'product'; } $article[$element_name] = apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), get_the_ID()); break; case 'title': $article[$element_name] = apply_filters('pmxe_post_title', pmxe_filter($record->post_title, $fieldSnipped), get_the_ID()); break; case 'content': $val = apply_filters('pmxe_post_content', pmxe_filter($record->post_content, $fieldSnipped), get_the_ID()); $article[$element_name] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val; break; case 'media': $attachment_ids = array(); $_featured_image = get_post_meta(get_the_ID(), '_thumbnail_id', true); if (!empty($_featured_image)) { $attachment_ids[] = $_featured_image; } $_gallery = get_post_meta(get_the_ID(), '_product_image_gallery', true); if (!empty($_gallery)) { $gallery = explode(',', $_gallery); if (!empty($gallery) and is_array($gallery)) { foreach ($gallery as $aid) { if (!in_array($aid, $attachment_ids)) { $attachment_ids[] = $aid; } } } } if (!empty($attachment_ids)) { $img_urls = array(); $img_titles = array(); $img_captions = array(); $img_alts = array(); $img_descriptions = array(); foreach ($attachment_ids as $key => $attach_id) { $attach = get_post($attach_id); if (wp_attachment_is_image($attach->ID)) { $val = wp_get_attachment_url($attach->ID); if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'filenames': $val = basename(wp_get_attachment_url($attach->ID)); break; case 'filepaths': $val = get_attached_file($attach->ID); break; default: # code... break; } } $img_urls[] = apply_filters('pmxe_attachment_url', $val, get_the_ID(), $attach->ID); $img_titles[] = apply_filters('pmxe_attachment_title', $attach->post_title, get_the_ID(), $attach->ID); $img_captions[] = apply_filters('pmxe_attachment_caption', $attach->post_excerpt, get_the_ID(), $attach->ID); $img_descriptions[] = apply_filters('pmxe_attachment_content', $attach->post_content, get_the_ID(), $attach->ID); $img_alts[] = apply_filters('pmxe_attachment_alt', get_post_meta($record->ID, '_wp_attachment_image_alt', true), get_the_ID(), $attach->ID); $images_count++; } } if (!empty($img_urls)) { $article[$element_name . '_images'] = implode($implode_delimiter, $img_urls); } if (!empty($img_titles)) { $article[$element_name . '_titles'] = implode($implode_delimiter, $img_titles); } if (!empty($img_captions)) { $article[$element_name . '_captions'] = implode($implode_delimiter, $img_captions); } if (!empty($img_alts)) { $article[$element_name . '_alts'] = implode($implode_delimiter, $img_alts); } if (!empty($img_descriptions)) { $article[$element_name . '_descriptions'] = implode($implode_delimiter, $img_descriptions); } if ($max_images_count > $images_count) { $max_images_count = $images_count; } } break; case 'date': if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'unix': $post_date = get_post_time('U', true); break; default: $post_date = date($exportOptions['cc_options'][$ID], get_post_time('U', true)); break; } } else { $post_date = date("Ymd", get_post_time('U', true)); } $article[$element_name] = apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), get_the_ID()); break; case 'attachments': $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID)); if (!empty($attachment_imgs)) { $attachment_urls = array(); foreach ($attachment_imgs as $key => $attach) { if (!wp_attachment_is_image($attach->ID)) { $attachment_urls[] = apply_filters('pmxe_attachment_url', pmxe_filter(wp_get_attachment_url($attach->ID), $fieldSnipped), get_the_ID(), $attach->ID); $attach_count++; } } if (!empty($attachment_urls)) { $article[$element_name . '_attachments'] = implode($implode_delimiter, $attachment_urls); } if ($attach_count > $max_attach_count) { $max_attach_count = $attach_count; } } break; case 'parent': $article[$element_name] = apply_filters('pmxe_post_parent', pmxe_filter($record->post_parent, $fieldSnipped), get_the_ID()); break; case 'template': $article[$element_name] = apply_filters('pmxe_post_template', pmxe_filter(get_post_meta($record->ID, '_wp_page_template', true), $fieldSnipped), get_the_ID()); break; case 'order': $article[$element_name] = apply_filters('pmxe_menu_order', pmxe_filter($record->menu_order, $fieldSnipped), get_the_ID()); break; case 'status': $article[$element_name] = apply_filters('pmxe_post_status', pmxe_filter($record->post_status, $fieldSnipped), get_the_ID()); break; case 'format': $article[$element_name] = apply_filters('pmxe_post_format', pmxe_filter(get_post_format($record->ID), $fieldSnipped), get_the_ID()); break; case 'author': $article[$element_name] = apply_filters('pmxe_post_author', pmxe_filter($record->post_author, $fieldSnipped), get_the_ID()); break; case 'slug': $article[$element_name] = apply_filters('pmxe_post_slug', pmxe_filter($record->post_name, $fieldSnipped), get_the_ID()); break; case 'excerpt': $val = apply_filters('pmxe_post_excerpt', pmxe_filter($record->post_excerpt, $fieldSnipped), get_the_ID()); $article[$element_name] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val; break; case 'cf': if (!empty($exportOptions['cc_value'][$ID])) { $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_value'][$ID]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { if (empty($article[$element_name])) { $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); if (!in_array($element_name, $cf)) { $cf[] = $element_name; } } else { $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter($article[$element_name] . $implode_delimiter . maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); } } } if (empty($cur_meta_values)) { if (empty($article[$element_name])) { $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); if (!in_array($element_name, $cf)) { $cf[] = $element_name; } } // else{ // $article[$element_name . '_' . $key] = apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); // if (!in_array($element_name . '_' . $key, $cf)) $cf[] = $element_name . '_' . $key; // } } } break; case 'acf': if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) { global $acf; $field_options = unserialize($exportOptions['cc_options'][$ID]); switch ($field_options['type']) { case 'textarea': case 'oembed': case 'wysiwyg': case 'wp_wysiwyg': case 'date_time_picker': case 'date_picker': $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID, false); break; default: $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID); break; } pmxe_export_acf_field_csv($field_value, $exportOptions, $ID, $record->ID, $article, $acfs, $element_name, $fieldSnipped, $field_options['group_id'], $preview); } break; case 'woo': //$element_name = 'woo' . $element_name; if (!empty($exportOptions['cc_value'][$ID])) { $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_value'][$ID]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { switch ($exportOptions['cc_label'][$ID]) { case '_downloadable_files': $files = maybe_unserialize($cur_meta_value); $file_paths = array(); $file_names = array(); if (!empty($files)) { foreach ($files as $key => $file) { $file_paths[] = $file['file']; $file_names[] = $file['name']; } $article[$element_name . '_paths'] = implode($implode_delimiter, $file_paths); if (!in_array($element_name, $woo)) { $woo[] = $element_name . '_paths'; } $article[$element_name . '_names'] = implode($implode_delimiter, $file_names); if (!in_array($element_name, $woo)) { $woo[] = $element_name . '_names'; } } break; case '_crosssell_ids': case '_upsell_ids': $_upsell_ids = maybe_unserialize($cur_meta_value); $_skus = array(); if (!empty($_upsell_ids)) { foreach ($_upsell_ids as $_upsell_id) { $_skus[] = get_post_meta($_upsell_id, '_sku', true); } $article[$element_name] = implode($implode_delimiter, $_skus); $woo[] = $element_name; } break; default: if (empty($article[$element_name])) { $article[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); if (!in_array($element_name, $woo)) { $woo[] = $element_name; } } else { $article[$element_name . '_' . $key] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); if (!in_array($element_name . '_' . $key, $woo)) { $woo[] = $element_name . '_' . $key; } } break; } } } if (empty($cur_meta_values)) { if (empty($article[$element_name])) { $article[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); if (!in_array($element_name, $woo)) { $woo[] = $element_name; } switch ($exportOptions['cc_label'][$ID]) { case '_downloadable_files': if (!in_array($element_name, $woo)) { $woo[] = $element_name . '_paths'; } if (!in_array($element_name, $woo)) { $woo[] = $element_name . '_names'; } break; } } } } break; case 'woo_order': XmlExportWooCommerceOrder::getInstance()->export_csv($article, $woo_order, $record, $exportOptions, $ID); break; case 'attr': //$element_name = 'woo_' . $element_name; if (!empty($exportOptions['cc_value'][$ID])) { if ($record->post_parent == 0) { // $is_variable_product = false; // $product_terms = wp_get_post_terms( $record->ID, 'product_type' ); // if( ! empty($product_terms)){ // if( ! is_wp_error( $product_terms )){ // foreach($product_terms as $term){ // if ('variable' == $term->slug){ // $is_variable_product = true; // break; // } // } // } // } $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]); if (!is_wp_error($txes_list) and !empty($txes_list)) { $attr_new = array(); foreach ($txes_list as $t) { $attr_new[] = $t->slug; } //$element_name = ($is_variable_product) ? $element_name : 'attribute_' . $element_name; $article[$element_name] = apply_filters('pmxe_woo_attribute', pmxe_filter(implode($implode_delimiter, $attr_new), $fieldSnipped), get_the_ID()); } if (!in_array($element_name, $attributes)) { $attributes[] = $element_name; } } else { $attribute_pa = get_post_meta($record->ID, 'attribute_' . $exportOptions['cc_value'][$ID], true); //if ( ! empty($attribute_pa)){ $article['attribute_' . $element_name] = $attribute_pa; //} if (!in_array('attribute_' . $element_name, $attributes)) { $attributes[] = 'attribute_' . $element_name; } } } break; case 'cats': if (!empty($exportOptions['cc_value'][$ID])) { $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]); if (!is_wp_error($txes_list) and !empty($txes_list)) { $txes_ids = array(); $hierarchy_groups = array(); foreach ($txes_list as $t) { $txes_ids[] = $t->term_id; } foreach ($txes_list as $t) { if (wp_all_export_check_children_assign($t->term_id, $exportOptions['cc_value'][$ID], $txes_ids)) { $ancestors = get_ancestors($t->term_id, $exportOptions['cc_value'][$ID]); if (count($ancestors) > 0) { $hierarchy_group = array(); for ($i = count($ancestors) - 1; $i >= 0; $i--) { $term = get_term_by('id', $ancestors[$i], $exportOptions['cc_value'][$ID]); if ($term) { $hierarchy_group[] = $term->slug; } } $hierarchy_group[] = $t->slug; $hierarchy_groups[] = implode('>', $hierarchy_group); } else { $hierarchy_groups[] = $t->slug; } } } if (!empty($hierarchy_groups)) { $article[$element_name] = apply_filters('pmxe_post_taxonomy', pmxe_filter(implode($implode_delimiter, $hierarchy_groups), $fieldSnipped), get_the_ID()); } } if (!in_array($element_name, $taxes)) { $taxes[] = $element_name; } if ($exportOptions['cc_label'][$ID] == 'product_type' and get_post_type() == 'product_variation') { $article[$element_name] = 'variable'; $article['parent_sku'] = get_post_meta($record->post_parent, '_sku', true); } } break; case 'sql': if (!empty($exportOptions['cc_sql'][$ID])) { $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID())); if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) { // if shortcode defined if (strpos($exportOptions['cc_code'][$ID], '[') === 0) { $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])); } else { $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';'); } } $article[$element_name] = apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID()); } break; default: # code... break; } } } } $articles[] = $article; $articles = apply_filters('wp_all_export_csv_rows', $articles, $exportOptions); if ($preview) { break; } } if ($exportOptions['ids']) { foreach ($exportOptions['ids'] as $ID => $value) { if (is_numeric($ID)) { if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) { continue; } $element_name = !empty($exportOptions['cc_name'][$ID]) ? $exportOptions['cc_name'][$ID] : 'untitled_' . $ID; switch ($exportOptions['cc_type'][$ID]) { case 'media': $headers[] = $element_name . '_images'; $headers[] = $element_name . '_titles'; $headers[] = $element_name . '_captions'; $headers[] = $element_name . '_descriptions'; $headers[] = $element_name . '_alts'; break; case 'attachments': $headers[] = $element_name . '_attachments'; break; case 'cats': if (!empty($taxes)) { $tx = array_shift($taxes); $headers[] = $tx; if ($tx == 'product_type') { $headers[] = 'parent_sku'; } } break; case 'attr': if (!empty($attributes)) { $headers[] = array_shift($attributes); if (in_array('attribute_' . $element_name, $attributes)) { $headers[] = 'attribute_' . $element_name; foreach ($attributes as $akey => $avalue) { if ($avalue == 'attribute_' . $element_name) { unset($attributes[$akey]); break; } } } } break; case 'cf': if (!empty($cf)) { $headers[] = array_shift($cf); } break; case 'woo': if (!empty($woo)) { $headers[] = array_shift($woo); } break; case 'woo_order': XmlExportWooCommerceOrder::getInstance()->get_element_header($headers, $exportOptions, $ID); break; case 'acf': if (!empty($acfs)) { $headers[] = array_shift($acfs); } break; default: $headers[] = $element_name; break; } } } } if ($is_cron) { if (!$exported_by_cron) { fputcsv($stream, $headers, $exportOptions['delimiter']); } } else { if ($preview or empty(PMXE_Plugin::$session->file)) { fputcsv($stream, $headers, $exportOptions['delimiter']); } } foreach ($articles as $article) { $line = array(); foreach ($headers as $header) { $line[$header] = isset($article[$header]) ? $article[$header] : ''; } fputcsv($stream, $line, $exportOptions['delimiter']); } if ($preview) { return ob_get_clean(); } if ($is_cron) { file_put_contents($file_path, ob_get_clean(), FILE_APPEND); return $file_path; } else { if (empty(PMXE_Plugin::$session->file)) { $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); $wp_uploads = wp_upload_dir(); $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path']; $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to']; file_put_contents($export_file, ob_get_clean()); PMXE_Plugin::$session->set('file', $export_file); PMXE_Plugin::$session->save_data(); } else { file_put_contents(PMXE_Plugin::$session->file, ob_get_clean(), FILE_APPEND); } return true; } }
/** * Export CSV helper */ function pmxe_export_users_csv($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0) { ob_start(); // Prepare headers $headers = array(); $stream = fopen("php://output", 'w'); $max_attach_count = 0; $max_images_count = 0; $cf = array(); $woo = array(); $acfs = array(); $taxes = array(); $attributes = array(); $articles = array(); $implode_delimiter = $exportOptions['delimiter'] == ',' ? '|' : ','; foreach ($exportQuery->results as $user) { $attach_count = 0; $images_count = 0; //$exportQuery->the_post(); //$record = get_post( get_the_ID() ); $article = array(); //$article['post_type'] = $record->post_type; $article['ID'] = apply_filters('pmxe_user_id', $user->ID); //$article['permalink'] = get_permalink(); if ($exportOptions['ids']) { if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('post_id' => $user->ID, 'import_id' => $exportOptions['import_id'])); if ($postRecord->isEmpty()) { $postRecord->set(array('post_id' => $user->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $user->ID))->save(); } unset($postRecord); } foreach ($exportOptions['ids'] as $ID => $value) { if (is_numeric($ID)) { if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) { continue; } $element_name = !empty($exportOptions['cc_name'][$ID]) ? $exportOptions['cc_name'][$ID] : 'untitled_' . $ID; $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false; switch ($exportOptions['cc_type'][$ID]) { case 'id': $article[$element_name] = apply_filters('pmxe_user_id', pmxe_filter($user->ID, $fieldSnipped), $user->ID); break; case 'user_login': $article[$element_name] = apply_filters('pmxe_user_login', pmxe_filter($user->user_login, $fieldSnipped), $user->ID); break; case 'user_pass': $article[$element_name] = apply_filters('pmxe_user_pass', pmxe_filter($user->user_pass, $fieldSnipped), $user->ID); break; case 'user_email': $article[$element_name] = apply_filters('pmxe_user_email', pmxe_filter($user->user_email, $fieldSnipped), $user->ID); break; case 'user_nicename': $article[$element_name] = apply_filters('pmxe_user_nicename', pmxe_filter($user->user_nicename, $fieldSnipped), $user->ID); break; case 'user_url': $article[$element_name] = apply_filters('pmxe_user_url', pmxe_filter($user->user_url, $fieldSnipped), $user->ID); break; case 'user_activation_key': $article[$element_name] = apply_filters('pmxe_user_activation_key', pmxe_filter($user->user_activation_key, $fieldSnipped), $user->ID); break; case 'user_status': $article[$element_name] = apply_filters('pmxe_user_status', pmxe_filter($user->user_status, $fieldSnipped), $user->ID); break; case 'display_name': $article[$element_name] = apply_filters('pmxe_user_display_name', pmxe_filter($user->display_name, $fieldSnipped), $user->ID); break; case 'description': $val = apply_filters('pmxe_user_description', pmxe_filter($user->description, $fieldSnipped), $user->ID); $article[$element_name] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val; break; case 'user_registered': if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'unix': $post_date = strtotime($user->user_registered); break; default: $post_date = date($exportOptions['cc_options'][$ID], strtotime($user->user_registered)); break; } } else { $post_date = $user->user_registered; } $article[$element_name] = apply_filters('pmxe_user_registered', pmxe_filter($post_date, $fieldSnipped), $user->ID); break; case 'nickname': $article[$element_name] = apply_filters('pmxe_user_nickname', pmxe_filter($user->nickname, $fieldSnipped), $user->ID); break; case 'first_name': $article[$element_name] = apply_filters('pmxe_user_first_name', pmxe_filter($user->first_name, $fieldSnipped), $user->ID); break; case 'last_name': $article[$element_name] = apply_filters('pmxe_user_last_name', pmxe_filter($user->last_name, $fieldSnipped), $user->ID); break; case 'wp_capabilities': $article[$element_name] = apply_filters('pmxe_user_wp_capabilities', pmxe_filter(implode($implode_delimiter, $user->roles), $fieldSnipped), $user->ID); break; case 'cf': if (!empty($exportOptions['cc_value'][$ID])) { $cur_meta_values = get_user_meta($user->ID, $exportOptions['cc_value'][$ID]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { if (empty($article[$element_name])) { $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], $user->ID); if (!in_array($element_name, $cf)) { $cf[] = $element_name; } } else { $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter($article[$element_name] . $implode_delimiter . maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], $user->ID); } } } if (empty($cur_meta_values)) { if (empty($article[$element_name])) { $article[$element_name] = apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], $user->ID); if (!in_array($element_name, $cf)) { $cf[] = $element_name; } } // else{ // $article[$element_name . '_' . $key] = apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID()); // if (!in_array($element_name . '_' . $key, $cf)) $cf[] = $element_name . '_' . $key; // } } } break; case 'acf': if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) { global $acf; $field_options = unserialize($exportOptions['cc_options'][$ID]); switch ($field_options['type']) { case 'textarea': case 'oembed': case 'wysiwyg': case 'wp_wysiwyg': case 'date_time_picker': case 'date_picker': $field_value = get_field($exportOptions['cc_label'][$ID], $user->ID, false); break; default: $field_value = get_field($exportOptions['cc_label'][$ID], $user->ID); break; } pmxe_export_acf_field_csv($field_value, $exportOptions, $ID, $user->ID, $article, $acfs, $element_name, $fieldSnipped, $field_options['group_id'], $preview); } break; case 'sql': if (!empty($exportOptions['cc_sql'][$ID])) { global $wpdb; $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID())); if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) { // if shortcode defined if (strpos($exportOptions['cc_code'][$ID], '[') === 0) { $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])); } else { $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';'); } } $article[$element_name] = apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID()); } break; default: # code... break; } } } } $articles[] = $article; if ($preview) { break; } } if ($exportOptions['ids']) { foreach ($exportOptions['ids'] as $ID => $value) { if (is_numeric($ID)) { if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) { continue; } $element_name = !empty($exportOptions['cc_name'][$ID]) ? $exportOptions['cc_name'][$ID] : 'untitled_' . $ID; switch ($exportOptions['cc_type'][$ID]) { case 'cf': if (!empty($cf)) { $headers[] = array_shift($cf); } break; case 'acf': if (!empty($acfs)) { $headers[] = array_shift($acfs); } break; default: $headers[] = $element_name; break; } } } } if ($is_cron) { if (!$exported_by_cron) { fputcsv($stream, $headers, $exportOptions['delimiter']); } } else { if ($preview or empty(PMXE_Plugin::$session->file)) { fputcsv($stream, $headers, $exportOptions['delimiter']); } } foreach ($articles as $article) { $line = array(); foreach ($headers as $header) { $line[$header] = isset($article[$header]) ? $article[$header] : ''; } fputcsv($stream, $line, $exportOptions['delimiter']); } if ($preview) { return ob_get_clean(); } if ($is_cron) { file_put_contents($file_path, ob_get_clean(), FILE_APPEND); return $file_path; } else { if (empty(PMXE_Plugin::$session->file)) { $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); $wp_uploads = wp_upload_dir(); $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path']; $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to']; file_put_contents($export_file, ob_get_clean()); PMXE_Plugin::$session->set('file', $export_file); PMXE_Plugin::$session->save_data(); } else { file_put_contents(PMXE_Plugin::$session->file, ob_get_clean(), FILE_APPEND); } return true; } }
public static function prepare_data($entry, $xmlWriter = false, &$acfs, &$woo, &$woo_order, $implode_delimiter, $preview, $is_item_data = false, $subID = false) { $article = array(); // associate exported post with import if (!$is_item_data and wp_all_export_is_compatible() and XmlExportEngine::$exportOptions['is_generate_import'] and XmlExportEngine::$exportOptions['import_id']) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('post_id' => $entry->ID, 'import_id' => XmlExportEngine::$exportOptions['import_id'])); if ($postRecord->isEmpty()) { $postRecord->set(array('post_id' => $entry->ID, 'import_id' => XmlExportEngine::$exportOptions['import_id'], 'unique_key' => $entry->ID, 'product_key' => $entry->ID))->save(); } unset($postRecord); } $is_xml_export = false; if (!empty($xmlWriter) and XmlExportEngine::$exportOptions['export_to'] == 'xml' and !in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants'))) { $is_xml_export = true; } foreach (XmlExportEngine::$exportOptions['ids'] as $ID => $value) { $pType = $entry->post_type; if ($is_item_data and $subID != $ID) { continue; } // skip shop order items data if ($pType == "shop_order" and strpos(XmlExportEngine::$exportOptions['cc_label'][$ID], "item_data__") !== false and !$is_item_data) { continue; } $fieldName = XmlExportEngine::$exportOptions['cc_name'][$ID]; $fieldValue = str_replace("item_data__", "", XmlExportEngine::$exportOptions['cc_value'][$ID]); $fieldLabel = str_replace("item_data__", "", XmlExportEngine::$exportOptions['cc_label'][$ID]); $fieldSql = XmlExportEngine::$exportOptions['cc_sql'][$ID]; $fieldPhp = XmlExportEngine::$exportOptions['cc_php'][$ID]; $fieldCode = XmlExportEngine::$exportOptions['cc_code'][$ID]; $fieldType = XmlExportEngine::$exportOptions['cc_type'][$ID]; $fieldOptions = XmlExportEngine::$exportOptions['cc_options'][$ID]; $fieldSettings = empty(XmlExportEngine::$exportOptions['cc_settings'][$ID]) ? $fieldOptions : XmlExportEngine::$exportOptions['cc_settings'][$ID]; if (empty($fieldName) or empty($fieldType) or !is_numeric($ID)) { continue; } $element_name = !empty($fieldName) ? $fieldName : 'untitled_' . $ID; $element_name_ns = ''; if ($is_xml_export) { $element_name = !empty($fieldName) ? preg_replace('/[^a-z0-9_:-]/i', '', $fieldName) : 'untitled_' . $ID; if (strpos($element_name, ":") !== false) { $element_name_parts = explode(":", $element_name); $element_name_ns = empty($element_name_parts[0]) ? '' : $element_name_parts[0]; $element_name = empty($element_name_parts[1]) ? 'untitled_' . $ID : preg_replace('/[^a-z0-9_-]/i', '', $element_name_parts[1]); } } $fieldSnipped = (!empty($fieldPhp) and !empty($fieldCode)) ? $fieldCode : false; switch ($fieldType) { case 'id': if ($element_name == 'ID') { $element_name = 'id'; } wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_id', pmxe_filter($entry->ID, $fieldSnipped), $entry->ID)); break; case 'permalink': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_guid', pmxe_filter(get_permalink(), $fieldSnipped), $entry->ID)); break; case 'post_type': if ($entry->post_type == 'product_variation') { $pType = 'product'; } wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), $entry->ID)); break; case 'title': $val = apply_filters('pmxe_post_title', pmxe_filter($entry->post_title, $fieldSnipped)); wp_all_export_write_article($article, $element_name, $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val, $entry->ID); break; case 'content': $val = apply_filters('pmxe_post_content', pmxe_filter($entry->post_content, $fieldSnipped), $entry->ID); wp_all_export_write_article($article, $element_name, $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val); break; // Media Attachments // Media Attachments case 'attachments': case 'attachment_id': case 'attachment_url': case 'attachment_filename': case 'attachment_path': case 'attachment_title': case 'attachment_caption': case 'attachment_description': case 'attachment_alt': XmlExportMediaGallery::getInstance($entry->ID); $attachment_data = XmlExportMediaGallery::get_attachments($fieldType); wp_all_export_write_article($article, $element_name, apply_filters('pmxe_' . $fieldType, pmxe_filter(implode($implode_delimiter, $attachment_data), $fieldSnipped), $entry->ID)); break; // Media Images // Media Images case 'media': case 'image_id': case 'image_url': case 'image_filename': case 'image_path': case 'image_title': case 'image_caption': case 'image_description': case 'image_alt': $field_options = json_decode($fieldOptions, true); XmlExportMediaGallery::getInstance($entry->ID); $images_data = XmlExportMediaGallery::get_images($fieldType, $field_options); $images_separator = empty($field_options['image_separator']) ? $implode_delimiter : $field_options['image_separator']; wp_all_export_write_article($article, $element_name, apply_filters('pmxe_' . $fieldType, pmxe_filter(implode($images_separator, $images_data), $fieldSnipped), $entry->ID)); break; case 'date': if (!empty($fieldSettings)) { switch ($fieldSettings) { case 'unix': $post_date = get_post_time('U', true, $entry->ID); break; default: $post_date = date($fieldSettings, get_post_time('U', true, $entry->ID)); break; } } else { $post_date = date("Ymd", get_post_time('U', true, $entry->ID)); } wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), $entry->ID)); break; case 'parent': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_parent', pmxe_filter($entry->post_parent, $fieldSnipped), $entry->ID)); break; case 'comment_status': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_comment_status', pmxe_filter($entry->comment_status, $fieldSnipped), $entry->ID)); break; case 'ping_status': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_ping_status', pmxe_filter($entry->ping_status, $fieldSnipped), $entry->ID)); break; case 'template': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_template', pmxe_filter(get_post_meta($entry->ID, '_wp_page_template', true), $fieldSnipped), $entry->ID)); break; case 'order': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_menu_order', pmxe_filter($entry->menu_order, $fieldSnipped), $entry->ID)); break; case 'status': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_status', pmxe_filter($entry->post_status, $fieldSnipped), $entry->ID)); break; case 'format': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_format', pmxe_filter(get_post_format($entry->ID), $fieldSnipped), $entry->ID)); break; case 'author': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_author', pmxe_filter($entry->post_author, $fieldSnipped), $entry->ID)); break; case 'slug': wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_slug', pmxe_filter($entry->post_name, $fieldSnipped), $entry->ID)); break; case 'excerpt': $val = apply_filters('pmxe_post_excerpt', pmxe_filter($entry->post_excerpt, $fieldSnipped), $entry->ID); wp_all_export_write_article($article, $element_name, $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val); break; case 'cf': if (!empty($fieldValue)) { $val = ""; $cur_meta_values = get_post_meta($entry->ID, $fieldValue); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { if (empty($val)) { $val = apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $fieldValue, $entry->ID); } else { $val = apply_filters('pmxe_custom_field', pmxe_filter($val . $implode_delimiter . maybe_serialize($cur_meta_value), $fieldSnipped), $fieldValue, $entry->ID); } } wp_all_export_write_article($article, $element_name, $val); } if (empty($cur_meta_values)) { if (empty($article[$element_name])) { wp_all_export_write_article($article, $element_name, apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $fieldValue, $entry->ID)); } } } break; case 'acf': if (!empty($fieldLabel) and class_exists('acf')) { global $acf; $field_options = unserialize($fieldOptions); if (!$is_xml_export) { switch ($field_options['type']) { case 'textarea': case 'oembed': case 'wysiwyg': case 'wp_wysiwyg': case 'date_time_picker': case 'date_picker': $field_value = get_field($fieldLabel, $entry->ID, false); break; default: $field_value = get_field($fieldLabel, $entry->ID); break; } } else { $field_value = get_field($fieldLabel, $entry->ID); } XmlExportACF::export_acf_field($field_value, XmlExportEngine::$exportOptions, $ID, $entry->ID, $article, $xmlWriter, $acfs, $element_name, $element_name_ns, $fieldSnipped, $field_options['group_id'], $preview); } break; case 'woo': if ($is_xml_export) { XmlExportEngine::$woo_export->export_xml($xmlWriter, $entry, XmlExportEngine::$exportOptions, $ID); } else { XmlExportEngine::$woo_export->export_csv($article, $woo, $entry, XmlExportEngine::$exportOptions, $ID); } break; case 'woo_order': if ($is_xml_export) { XmlExportEngine::$woo_order_export->export_xml($xmlWriter, $entry, XmlExportEngine::$exportOptions, $ID, $preview); } else { XmlExportEngine::$woo_order_export->export_csv($article, $woo_order, $entry, XmlExportEngine::$exportOptions, $ID, $preview); } break; case 'attr': if (!empty($fieldValue)) { if ($entry->post_parent == 0) { $txes_list = get_the_terms($entry->ID, $fieldValue); if (!is_wp_error($txes_list) and !empty($txes_list)) { $attr_new = array(); foreach ($txes_list as $t) { $attr_new[] = $t->name; } wp_all_export_write_article($article, $element_name, apply_filters('pmxe_woo_attribute', pmxe_filter(implode($implode_delimiter, $attr_new), $fieldSnipped), $entry->ID, $fieldValue)); } } else { $attribute_pa = apply_filters('pmxe_woo_attribute', get_post_meta($entry->ID, 'attribute_' . $fieldValue, true), $entry->ID, $fieldValue); wp_all_export_write_article($article, $element_name, $attribute_pa); } // if ( ! in_array($element_name, $attributes)) $attributes[] = $element_name; } break; case 'cats': if (!empty($fieldValue)) { // get categories from parent product in case when variation exported $entry_id = $entry->post_type == 'product_variation' ? $entry->post_parent : $entry->ID; $txes_list = get_the_terms($entry_id, $fieldValue); $hierarchy_groups = array(); if (!is_wp_error($txes_list) and !empty($txes_list)) { $txes_ids = array(); foreach ($txes_list as $t) { $txes_ids[] = $t->term_id; } foreach ($txes_list as $t) { if (wp_all_export_check_children_assign($t->term_id, $fieldValue, $txes_ids)) { $ancestors = get_ancestors($t->term_id, $fieldValue); if (count($ancestors) > 0) { $hierarchy_group = array(); for ($i = count($ancestors) - 1; $i >= 0; $i--) { $term = get_term_by('id', $ancestors[$i], $fieldValue); if ($term) { $hierarchy_group[] = $term->name; } } $hierarchy_group[] = $t->name; $hierarchy_groups[] = implode('>', $hierarchy_group); } else { $hierarchy_groups[] = $t->name; } } } // if ( empty($hierarchy_groups) ) $hierarchy_groups = ''; } wp_all_export_write_article($article, $element_name, apply_filters('pmxe_post_taxonomy', pmxe_filter(implode($implode_delimiter, $hierarchy_groups), $fieldSnipped), $entry->ID)); // if ( ! in_array($element_name, $taxes)) $taxes[] = $element_name; if ($fieldLabel == 'product_type') { if ($entry->post_type == 'product_variation') { $article[$element_name] = 'variable'; } $article['parent_id'] = $entry->post_parent; if ($is_xml_export) { $xmlWriter->beginElement($element_name_ns, 'parent_id', null); $xmlWriter->writeData($article['parent_id'], 'parent_id'); $xmlWriter->closeElement(); } } } break; case 'sql': if (!empty($fieldSql)) { global $wpdb; $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $fieldSql)), $entry->ID)); if (!empty($fieldPhp) and !empty($fieldCode)) { // if shortcode defined if (strpos($fieldCode, '[') === 0) { $val = do_shortcode(str_replace("%%VALUE%%", $val, $fieldCode)); } else { $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $fieldCode)) . ';'); } } wp_all_export_write_article($article, $element_name, apply_filters('pmxe_sql_field', $val, $element_name, $entry->ID)); } break; case 'wpml_trid': $post_type = get_post_type($entry->ID); $post_type = apply_filters('wpml_element_type', $post_type); $post_language_details = apply_filters('wpml_element_language_details', null, array('element_id' => $entry->ID, 'element_type' => $post_type)); $trid = empty($post_language_details->trid) ? '' : $post_language_details->trid; wp_all_export_write_article($article, $element_name, apply_filters('pmxe_trid_field', $trid, $element_name, $entry->ID)); break; case 'wpml_lang': $post_type = get_post_type($entry->ID); $post_type = apply_filters('wpml_element_type', $post_type); $post_language_details = apply_filters('wpml_element_language_details', null, array('element_id' => $entry->ID, 'element_type' => $post_type)); $language_code = empty($post_language_details->language_code) ? '' : $post_language_details->language_code; wp_all_export_write_article($article, $element_name, apply_filters('pmxe_trid_field', $language_code, $element_name, $entry->ID)); break; default: # code... break; } if ($is_xml_export and isset($article[$element_name])) { $element_name_in_file = XmlCsvExport::_get_valid_header_name($element_name); $xmlWriter = apply_filters('wp_all_export_add_before_element', $xmlWriter, $element_name_in_file, XmlExportEngine::$exportID, $entry->ID); $xmlWriter->beginElement($element_name_ns, $element_name_in_file, null); $xmlWriter->writeData($article[$element_name], $element_name_in_file); $xmlWriter->closeElement(); $xmlWriter = apply_filters('wp_all_export_add_after_element', $xmlWriter, $element_name_in_file, XmlExportEngine::$exportID, $entry->ID); } } return $article; }
/** * Perform import operation * @param string $xml XML string to import * @param callback[optional] $logger Method where progress messages are submmitted * @return PMXI_Import_Record * @chainable */ public function process($xml, $logger = NULL, $chunk = false, $is_cron = false, $xpath_prefix = '') { add_filter('user_has_cap', array($this, '_filter_has_cap_unfiltered_html')); kses_init(); // do not perform special filtering for imported content $cxpath = $xpath_prefix . $this->xpath; $this->options += PMXI_Plugin::get_default_import_options(); // make sure all options are defined $avoid_pingbacks = PMXI_Plugin::getInstance()->getOption('pingbacks'); if ($avoid_pingbacks and !defined('WP_IMPORTING')) { define('WP_IMPORTING', true); } $postRecord = new PMXI_Post_Record(); $tmp_files = array(); // compose records to import $records = array(); $is_import_complete = false; try { $chunk == 1 and $logger and call_user_func($logger, __('Composing titles...', 'pmxi_plugin')); $titles = XmlImportParser::factory($xml, $cxpath, $this->template['title'], $file)->parse($records); $tmp_files[] = $file; $chunk == 1 and $logger and call_user_func($logger, __('Composing excerpts...', 'pmxi_plugin')); $post_excerpt = array(); if (!empty($this->options['post_excerpt'])) { $post_excerpt = XmlImportParser::factory($xml, $cxpath, $this->options['post_excerpt'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $post_excerpt = array_fill(0, count($titles), ''); } if ("xpath" == $this->options['status']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing statuses...', 'pmxi_plugin')); $post_status = array(); if (!empty($this->options['status_xpath'])) { $post_status = XmlImportParser::factory($xml, $cxpath, $this->options['status_xpath'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $post_status = array_fill(0, count($titles), ''); } } $chunk == 1 and $logger and call_user_func($logger, __('Composing authors...', 'pmxi_plugin')); $post_author = array(); $current_user = wp_get_current_user(); if (!empty($this->options['author'])) { $post_author = XmlImportParser::factory($xml, $cxpath, $this->options['author'], $file)->parse($records); $tmp_files[] = $file; foreach ($post_author as $key => $author) { $user = get_user_by('login', $author) or $user = get_user_by('slug', $author) or $user = get_user_by('email', $author) or ctype_digit($author) and $user = get_user_by('id', $author); $post_author[$key] = !empty($user) ? $user->ID : $current_user->ID; } } else { count($titles) and $post_author = array_fill(0, count($titles), $current_user->ID); } $chunk == 1 and $logger and call_user_func($logger, __('Composing slugs...', 'pmxi_plugin')); $post_slug = array(); if (!empty($this->options['post_slug'])) { $post_slug = XmlImportParser::factory($xml, $cxpath, $this->options['post_slug'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $post_slug = array_fill(0, count($titles), ''); } $chunk == 1 and $logger and call_user_func($logger, __('Composing contents...', 'pmxi_plugin')); $contents = XmlImportParser::factory((!empty($this->template['is_keep_linebreaks']) and intval($this->template['is_keep_linebreaks'])) ? $xml : preg_replace('%\\r\\n?|\\n%', ' ', $xml), $cxpath, $this->template['content'], $file)->parse($records); $tmp_files[] = $file; $chunk == 1 and $logger and call_user_func($logger, __('Composing dates...', 'pmxi_plugin')); if ('specific' == $this->options['date_type']) { $dates = XmlImportParser::factory($xml, $cxpath, $this->options['date'], $file)->parse($records); $tmp_files[] = $file; $warned = array(); // used to prevent the same notice displaying several times foreach ($dates as $i => $d) { if ($d == 'now') { $d = current_time('mysql'); } // Replace 'now' with the WordPress local time to account for timezone offsets (WordPress references its local time during publishing rather than the server’s time so it should use that) $time = strtotime($d); if (FALSE === $time) { in_array($d, $warned) or $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: unrecognized date format `%s`, assigning current date', 'pmxi_plugin'), $warned[] = $d)); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; $time = time(); } $dates[$i] = date('Y-m-d H:i:s', $time); } } else { $dates_start = XmlImportParser::factory($xml, $cxpath, $this->options['date_start'], $file)->parse($records); $tmp_files[] = $file; $dates_end = XmlImportParser::factory($xml, $cxpath, $this->options['date_end'], $file)->parse($records); $tmp_files[] = $file; $warned = array(); // used to prevent the same notice displaying several times foreach ($dates_start as $i => $d) { $time_start = strtotime($dates_start[$i]); if (FALSE === $time_start) { in_array($dates_start[$i], $warned) or $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: unrecognized date format `%s`, assigning current date', 'pmxi_plugin'), $warned[] = $dates_start[$i])); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; $time_start = time(); } $time_end = strtotime($dates_end[$i]); if (FALSE === $time_end) { in_array($dates_end[$i], $warned) or $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: unrecognized date format `%s`, assigning current date', 'pmxi_plugin'), $warned[] = $dates_end[$i])); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; $time_end = time(); } $dates[$i] = date('Y-m-d H:i:s', mt_rand($time_start, $time_end)); } } $tags = array(); if ($this->options['tags']) { $chunk == 1 and $logger and call_user_func($logger, __('Composing tags...', 'pmxi_plugin')); $tags_raw = XmlImportParser::factory($xml, $cxpath, $this->options['tags'], $file)->parse($records); $tmp_files[] = $file; foreach ($tags_raw as $i => $t_raw) { $tags[$i] = ''; if ('' != $t_raw) { $tags[$i] = implode(', ', str_getcsv($t_raw, $this->options['tags_delim'])); } } } else { count($titles) and $tags = array_fill(0, count($titles), ''); } // [posts categories] require_once ABSPATH . 'wp-admin/includes/taxonomy.php'; if ('post' == $this->options['type']) { $cats = array(); $categories_hierarchy = !empty($this->options['categories']) ? json_decode($this->options['categories']) : array(); if (!empty($categories_hierarchy) and is_array($categories_hierarchy)) { $chunk == 1 and $logger and call_user_func($logger, __('Composing categories...', 'pmxi_plugin')); $categories = array(); foreach ($categories_hierarchy as $k => $category) { if ("" == $category->xpath) { continue; } $cats_raw = XmlImportParser::factory($xml, $cxpath, str_replace('\'', '"', $category->xpath), $file)->parse($records); $tmp_files[] = $file; $warned = array(); // used to prevent the same notice displaying several times foreach ($cats_raw as $i => $c_raw) { if (empty($categories_hierarchy[$k]->cat_ids[$i])) { $categories_hierarchy[$k]->cat_ids[$i] = array(); } if (empty($cats[$i])) { $cats[$i] = array(); } $count_cats = count($cats[$i]); $delimeted_categories = explode($this->options['categories_delim'], $c_raw); if ('' != $c_raw) { foreach (explode($this->options['categories_delim'], $c_raw) as $j => $cc) { if ('' != $cc) { $cat = get_term_by('name', trim($cc), 'category') or $cat = get_term_by('slug', trim($cc), 'category') or ctype_digit($cc) and $cat = get_term_by('id', trim($cc), 'category'); if (!empty($category->parent_id)) { foreach ($categories_hierarchy as $key => $value) { if ($value->item_id == $category->parent_id and !empty($value->cat_ids[$i])) { foreach ($value->cat_ids[$i] as $parent) { if (!$j or !$this->options['categories_auto_nested']) { $filtered_cats = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => is_array($parent) ? $parent['name'] : $parent, 'assign' => $category->assign)), $category); foreach ($filtered_cats as $filtered_cat) { $cats[$i][] = $filtered_cat; } } elseif ($this->options['categories_auto_nested']) { $filtered_cats = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => !empty($delimeted_categories[$j - 1]) ? apply_filters('pmxi_parent_category', trim($delimeted_categories[$j - 1]), $category) : false, 'assign' => $category->assign)), $category); foreach ($filtered_cats as $filtered_cat) { $cats[$i][] = $filtered_cat; } } } } } } else { if (!$j or !$this->options['categories_auto_nested']) { $filtered_cats = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => false, 'assign' => $category->assign)), $category); foreach ($filtered_cats as $filtered_cat) { $cats[$i][] = $filtered_cat; } } elseif ($this->options['categories_auto_nested']) { $filtered_cats = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => !empty($delimeted_categories[$j - 1]) ? apply_filters('pmxi_parent_category', trim($delimeted_categories[$j - 1]), $category) : false, 'assign' => $category->assign)), $category); foreach ($filtered_cats as $filtered_cat) { $cats[$i][] = $filtered_cat; } } } } } } if ($count_cats < count($cats[$i])) { $categories_hierarchy[$k]->cat_ids[$i][] = apply_filters('pmxi_single_category', $cats[$i][count($cats[$i]) - 1], $categories_hierarchy[$k]); } } } } else { count($titles) and $cats = array_fill(0, count($titles), ''); } } // [/posts categories] // [custom taxonomies] $taxonomies = array(); $taxonomies_param = $this->options['type'] . '_taxonomies'; if ('page' == $this->options['type']) { $taxonomies_object_type = 'page'; } elseif ('' != $this->options['custom_type']) { $taxonomies_object_type = $this->options['custom_type']; } else { $taxonomies_object_type = 'post'; } if (!empty($this->options[$taxonomies_param]) and is_array($this->options[$taxonomies_param])) { foreach ($this->options[$taxonomies_param] as $tx_name => $tx_template) { if ('' != $tx_template) { $tx = get_taxonomy($tx_name); $taxonomies[$tx_name] = array(); if (!empty($tx->object_type) and in_array($taxonomies_object_type, $tx->object_type)) { $chunk == 1 and $logger and call_user_func($logger, sprintf(__('Composing terms for `%s` taxonomy...', 'pmxi_plugin'), $tx->labels->name)); $txes = array(); $taxonomies_hierarchy = json_decode($tx_template); foreach ($taxonomies_hierarchy as $k => $taxonomy) { if ("" == $taxonomy->xpath) { continue; } $txes_raw = XmlImportParser::factory($xml, $cxpath, str_replace('\'', '"', $taxonomy->xpath), $file)->parse($records); $tmp_files[] = $file; $warned = array(); foreach ($txes_raw as $i => $tx_raw) { if (empty($taxonomies_hierarchy[$k]->txn_names[$i])) { $taxonomies_hierarchy[$k]->txn_names[$i] = array(); } if (empty($taxonomies[$tx_name][$i])) { $taxonomies[$tx_name][$i] = array(); } $count_cats = count($taxonomies[$tx_name][$i]); $delimeted_taxonomies = explode(!empty($taxonomy->delim) ? $taxonomy->delim : ',', $tx_raw); if ('' != $tx_raw) { foreach (explode(!empty($taxonomy->delim) ? $taxonomy->delim : ',', $tx_raw) as $j => $cc) { if ('' != $cc) { $cat = get_term_by('name', trim($cc), $tx_name) or $cat = get_term_by('slug', trim($cc), $tx_name) or ctype_digit($cc) and $cat = get_term_by('id', $cc, $tx_name); if (!empty($taxonomy->parent_id)) { foreach ($taxonomies_hierarchy as $key => $value) { if ($value->item_id == $taxonomy->parent_id and !empty($value->txn_names[$i])) { foreach ($value->txn_names[$i] as $parent) { if (!$j or !$taxonomy->auto_nested) { $filtered_txs = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => $parent, 'assign' => $taxonomy->assign)), $taxonomy); foreach ($filtered_txs as $filtered_tx) { $taxonomies[$tx_name][$i][] = $filtered_tx; } } elseif ($taxonomy->auto_nested) { $filtered_txs = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => !empty($delimeted_taxonomies[$j - 1]) ? apply_filters('pmxi_parent_category', trim($delimeted_taxonomies[$j - 1]), $taxonomy) : false, 'assign' => $taxonomy->assign)), $taxonomy); foreach ($filtered_txs as $filtered_tx) { $taxonomies[$tx_name][$i][] = $filtered_tx; } } } } } } else { if (!$j or !$taxonomy->auto_nested) { $filtered_txs = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => false, 'assign' => $taxonomy->assign)), $taxonomy); foreach ($filtered_txs as $filtered_tx) { $taxonomies[$tx_name][$i][] = $filtered_tx; } } elseif ($taxonomy->auto_nested) { $filtered_txs = apply_filters('pmxi_single_category', array(array('name' => trim($cc), 'parent' => !empty($delimeted_taxonomies[$j - 1]) ? apply_filters('pmxi_parent_category', trim($delimeted_taxonomies[$j - 1]), $taxonomy) : false, 'assign' => $taxonomy->assign)), $taxonomy); foreach ($filtered_txs as $filtered_tx) { $taxonomies[$tx_name][$i][] = $filtered_tx; } } } } } } if ($count_cats < count($taxonomies[$tx_name][$i])) { $taxonomies_hierarchy[$k]->txn_names[$i][] = apply_filters('pmxi_single_category', $taxonomies[$tx_name][$i][count($taxonomies[$tx_name][$i]) - 1], $taxonomies_hierarchy[$k]); } } } } } } } // [/custom taxonomies] // serialized featured images if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $uploads['error']); $logger and call_user_func($logger, __('<b>WARNING</b>: No featured images will be created', 'pmxi_plugin')); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { $chunk == 1 and $logger and call_user_func($logger, __('Composing URLs for featured images...', 'pmxi_plugin')); $featured_images = array(); if ($this->options['featured_image']) { $featured_images = XmlImportParser::factory($xml, $cxpath, $this->options['featured_image'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $featured_images = array_fill(0, count($titles), ''); } } // serialized images meta data if ($this->options['set_image_meta_data']) { $uploads = wp_upload_dir(); // serialized images meta titles $chunk == 1 and $logger and call_user_func($logger, __('Composing image meta data (titles)...', 'pmxi_plugin')); $image_meta_titles = array(); if ($this->options['image_meta_title']) { $image_meta_titles = XmlImportParser::factory($xml, $cxpath, $this->options['image_meta_title'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_titles = array_fill(0, count($titles), ''); } // serialized images meta captions $chunk == 1 and $logger and call_user_func($logger, __('Composing image meta data (captions)...', 'pmxi_plugin')); $image_meta_captions = array(); if ($this->options['image_meta_caption']) { $image_meta_captions = XmlImportParser::factory($xml, $cxpath, $this->options['image_meta_caption'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_captions = array_fill(0, count($titles), ''); } // serialized images meta alt text $chunk == 1 and $logger and call_user_func($logger, __('Composing image meta data (alt text)...', 'pmxi_plugin')); $image_meta_alts = array(); if ($this->options['image_meta_alt']) { $image_meta_alts = XmlImportParser::factory($xml, $cxpath, $this->options['image_meta_alt'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_alts = array_fill(0, count($titles), ''); } // serialized images meta description $chunk == 1 and $logger and call_user_func($logger, __('Composing image meta data (description)...', 'pmxi_plugin')); $image_meta_descriptions = array(); if ($this->options['image_meta_description']) { $image_meta_descriptions = XmlImportParser::factory($xml, $cxpath, $this->options['image_meta_description'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $image_meta_descriptions = array_fill(0, count($titles), ''); } } // Composing images suffix $chunk == 1 and $this->options['auto_rename_images'] and $logger and call_user_func($logger, __('Composing images suffix...', 'pmxi_plugin')); $auto_rename_images = array(); if ($this->options['auto_rename_images'] and !empty($this->options['auto_rename_images_suffix'])) { $auto_rename_images = XmlImportParser::factory($xml, $cxpath, $this->options['auto_rename_images_suffix'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $auto_rename_images = array_fill(0, count($titles), ''); } // serialized attachments if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $uploads['error']); $logger and call_user_func($logger, __('<b>WARNING</b>: No attachments will be created', 'pmxi_plugin')); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session['pmxi_import']['warnings']; } else { $chunk == 1 and $logger and call_user_func($logger, __('Composing URLs for attachments files...', 'pmxi_plugin')); $attachments = array(); if ($this->options['attachments']) { // Detect if attachments is separated by comma $atchs = explode(',', $this->options['attachments']); if (!empty($atchs)) { $parse_multiple = true; foreach ($atchs as $atch) { if (!preg_match("/{.*}/", trim($atch))) { $parse_multiple = false; } } if ($parse_multiple) { foreach ($atchs as $atch) { $posts_attachments = XmlImportParser::factory($xml, $cxpath, trim($atch), $file)->parse($records); $tmp_files[] = $file; foreach ($posts_attachments as $i => $val) { $attachments[$i][] = $val; } } } else { $attachments = XmlImportParser::factory($xml, $cxpath, $this->options['attachments'], $file)->parse($records); $tmp_files[] = $file; } } } else { count($titles) and $attachments = array_fill(0, count($titles), ''); } } $chunk == 1 and $logger and call_user_func($logger, __('Composing unique keys...', 'pmxi_plugin')); $unique_keys = XmlImportParser::factory($xml, $cxpath, $this->options['unique_key'], $file)->parse($records); $tmp_files[] = $file; $chunk == 1 and $logger and call_user_func($logger, __('Processing posts...', 'pmxi_plugin')); if ('post' == $this->options['type'] and '' != $this->options['custom_type']) { $post_type = ($this->options['custom_type'] == 'product' and class_exists('PMWI_Plugin')) ? $this->options['custom_type'] : ($this->options['custom_type'] == 'page' ? 'page' : 'post'); } else { $post_type = $this->options['type']; } $addons = array(); $addons_data = array(); // data parsing for WP All Import add-ons $parsingData = array('import' => $this, 'count' => count($titles), 'xml' => $xml, 'logger' => $logger, 'chunk' => $chunk, 'xpath_prefix' => $xpath_prefix); foreach (PMXI_Admin_Addons::get_active_addons() as $class) { $model_class = str_replace("_Plugin", "_Import_Record", $class); if (class_exists($model_class)) { $addons[$class] = new $model_class(); $addons_data[$class] = method_exists($addons[$class], 'parse') ? $addons[$class]->parse($parsingData) : false; } else { $parse_func = $class . '_parse'; if (function_exists($parse_func)) { $addons_data[$class] = call_user_func($parse_func, $parsingData); } } } // save current import state to variables before import $created = $this->created; $updated = $this->updated; $skipped = $this->skipped; $specified_records = array(); if ($this->options['is_import_specified']) { foreach (preg_split('% *, *%', $this->options['import_specified'], -1, PREG_SPLIT_NO_EMPTY) as $chank) { if (preg_match('%^(\\d+)-(\\d+)$%', $chank, $mtch)) { $specified_records = array_merge($specified_records, range(intval($mtch[1]), intval($mtch[2]))); } else { $specified_records = array_merge($specified_records, array(intval($chank))); } } } foreach ($titles as $i => $void) { wp_cache_flush(); do_action('pmxi_before_post_import', $this->id); if (empty($titles[$i])) { if (!empty($addons_data['PMWI_Plugin']) and !empty($addons_data['PMWI_Plugin']['single_product_parent_ID'][$i])) { $titles[$i] = $addons_data['PMWI_Plugin']['single_product_parent_ID'][$i] . ' Product Variation'; } else { $skipped++; $logger and call_user_func($logger, __('<b>SKIPPED</b>: by empty title', 'pmxi_plugin')); $logger and PMXI_Plugin::$session['pmxi_import']['chunk_number'] = ++PMXI_Plugin::$session->data['pmxi_import']['chunk_number']; $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; pmxi_session_commit(); continue; } } $articleData = array('post_type' => $post_type, 'post_status' => "xpath" == $this->options['status'] ? $post_status[$i] : $this->options['status'], 'comment_status' => $this->options['comment_status'], 'ping_status' => $this->options['ping_status'], 'post_title' => !empty($this->template['is_leave_html']) ? html_entity_decode($titles[$i]) : $titles[$i], 'post_excerpt' => apply_filters('pmxi_the_excerpt', !empty($this->template['is_leave_html']) ? html_entity_decode($post_excerpt[$i]) : $post_excerpt[$i], $this->id), 'post_name' => $post_slug[$i], 'post_content' => apply_filters('pmxi_the_content', !empty($this->template['is_leave_html']) ? html_entity_decode($contents[$i]) : $contents[$i], $this->id), 'post_date' => $dates[$i], 'post_date_gmt' => get_gmt_from_date($dates[$i]), 'post_author' => $post_author[$i], 'tags_input' => $tags[$i]); if ('post' != $articleData['post_type']) { $articleData += array('menu_order' => $this->options['order'], 'post_parent' => $this->options['parent']); } // Re-import Records Matching $post_to_update = false; $post_to_update_id = false; // if Auto Matching re-import option selected if ("manual" != $this->options['duplicate_matching']) { // find corresponding article among previously imported $postRecord->clear(); $postRecord->getBy(array('unique_key' => $unique_keys[$i], 'import_id' => $this->id)); if (!$postRecord->isEmpty()) { $post_to_update = get_post($post_to_update_id = $postRecord->post_id); } // if Manual Matching re-import option seleted } else { $postRecord->clear(); // find corresponding article among previously imported $postRecord->getBy(array('unique_key' => $unique_keys[$i], 'import_id' => $this->id)); if ('custom field' == $this->options['duplicate_indicator']) { $custom_duplicate_value = XmlImportParser::factory($xml, $cxpath, $this->options['custom_duplicate_value'], $file)->parse($records); $tmp_files[] = $file; $custom_duplicate_name = XmlImportParser::factory($xml, $cxpath, $this->options['custom_duplicate_name'], $file)->parse($records); $tmp_files[] = $file; } else { count($titles) and $custom_duplicate_name = $custom_duplicate_value = array_fill(0, count($titles), ''); } // handle duplicates according to import settings if ($duplicates = pmxi_findDuplicates($articleData, $custom_duplicate_name[$i], $custom_duplicate_value[$i], $this->options['duplicate_indicator'])) { $duplicate_id = array_shift($duplicates); if ($duplicate_id) { $post_to_update = get_post($post_to_update_id = $duplicate_id); } } } if (!empty($specified_records)) { if (!in_array($created + $updated + $skipped + 1, $specified_records)) { if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } $skipped++; $logger and call_user_func($logger, __('<b>SKIPPED</b>: by specified records option', 'pmxi_plugin')); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; $logger and PMXI_Plugin::$session['pmxi_import']['chunk_number'] = ++PMXI_Plugin::$session->data['pmxi_import']['chunk_number']; pmxi_session_commit(); continue; } } // Duplicate record is founded if ($post_to_update) { // Do not update already existing records option selected if ("yes" == $this->options['is_keep_former_posts']) { if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } do_action('pmxi_do_not_update_existing', $post_to_update_id, $this->id, $this->iteration); $skipped++; $logger and call_user_func($logger, sprintf(__('<b>SKIPPED</b>: Previously imported record found for `%s`', 'pmxi_plugin'), $articleData['post_title'])); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; $logger and PMXI_Plugin::$session['pmxi_import']['chunk_number'] = ++PMXI_Plugin::$session->data['pmxi_import']['chunk_number']; pmxi_session_commit(); continue; } $articleData['ID'] = $post_to_update_id; // Choose which data to update if ($this->options['update_all_data'] == 'no') { // preserve date of already existing article when duplicate is found if (!$this->options['is_update_categories'] or $this->options['is_update_categories'] and $this->options['update_categories_logic'] != "full_update") { // preserve categories and tags of already existing article if corresponding setting is specified $cats_list = get_the_category($articleData['ID']); $existing_cats = array(); if (is_wp_error($cats_list)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to get current categories for article #%d, updating with those read from XML file', 'pmxi_plugin'), $articleData['ID'])); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { $cats_new = array(); foreach ($cats_list as $c) { $cats_new[] = $c->slug; } $existing_cats[$i] = $cats_new; } // Re-import Post Tags if (!$this->options['is_update_categories'] or $this->options['update_categories_logic'] == 'add_new' or $this->options['update_categories_logic'] == 'only' and empty($this->options['taxonomies_list']) or !empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and (in_array('post_tag', $this->options['taxonomies_list']) and $this->options['update_categories_logic'] == 'all_except' or !in_array('post_tag', $this->options['taxonomies_list']) and $this->options['update_categories_logic'] == 'only')) { $tags_list = get_the_tags($articleData['ID']); if (is_wp_error($tags_list)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to get current tags for article #%d, updating with those read from XML file', 'pmxi_plugin'), $articleData['ID'])); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { $tags_new = ($this->options['is_update_categories'] and $this->options['update_categories_logic'] == 'add_new') ? array_filter(explode(",", $tags[$i])) : array(); if ($tags_list) { foreach ($tags_list as $t) { if (!in_array($t->name, $tags_new)) { $tags_new[] = $t->name; } } } $articleData['tags_input'] = implode(', ', $tags_new); } } $existing_taxonomies = array(); foreach (array_keys($taxonomies) as $tx_name) { $txes_list = get_the_terms($articleData['ID'], $tx_name); if (is_wp_error($txes_list)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to get current taxonomies for article #%d, updating with those read from XML file', 'pmxi_plugin'), $articleData['ID'])); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { $txes_new = array(); if (!empty($txes_list)) { foreach ($txes_list as $t) { $txes_new[] = $t->slug; } } $existing_taxonomies[$tx_name][$i] = $txes_new; } } } else { //Remove existing taxonomies foreach (array_keys($taxonomies) as $tx_name) { wp_set_object_terms($articleData['ID'], NULL, $tx_name); } } if (!$this->options['is_update_dates']) { // preserve date of already existing article when duplicate is found $articleData['post_date'] = $post_to_update->post_date; $articleData['post_date_gmt'] = $post_to_update->post_date_gmt; } if (!$this->options['is_update_status']) { // preserve status and trashed flag $articleData['post_status'] = $post_to_update->post_status; } if (!$this->options['is_update_content']) { $articleData['post_content'] = $post_to_update->post_content; } if (!$this->options['is_update_title']) { $articleData['post_title'] = $post_to_update->post_title; } if (!$this->options['is_update_slug']) { $articleData['post_name'] = $post_to_update->post_name; } if (!$this->options['is_update_excerpt']) { $articleData['post_excerpt'] = $post_to_update->post_excerpt; } if (!$this->options['is_update_menu_order']) { $articleData['menu_order'] = $post_to_update->menu_order; } if (!$this->options['is_update_parent']) { $articleData['post_parent'] = $post_to_update->post_parent; } } if ($this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_attachments']) { wp_delete_attachments($articleData['ID'], true, 'files'); } // handle obsolete attachments (i.e. delete or keep) according to import settings if ($this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_images'] and $this->options['update_images_logic'] == "full_update") { wp_delete_attachments($articleData['ID'], $this->options['download_images'], 'images'); } } elseif (!$postRecord->isEmpty()) { // existing post not found though it's track was found... clear the leftover, plugin will continue to treat record as new $postRecord->delete(); } // no new records are created. it will only update posts it finds matching duplicates for if (!$this->options['create_new_records'] and empty($articleData['ID'])) { if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $this->iteration))->update(); } $logger and call_user_func($logger, __('<b>SKIPPED</b>: by do not create new posts option.', 'pmxi_plugin')); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; $logger and PMXI_Plugin::$session['pmxi_import']['chunk_number'] = ++PMXI_Plugin::$session->data['pmxi_import']['chunk_number']; $skipped++; pmxi_session_commit(); continue; } // cloak urls with `WP Wizard Cloak` if corresponding option is set if (!empty($this->options['is_cloak']) and class_exists('PMLC_Plugin')) { if (preg_match_all('%<a\\s[^>]*href=(?(?=")"([^"]*)"|(?(?=\')\'([^\']*)\'|([^\\s>]*)))%is', $articleData['post_content'], $matches, PREG_PATTERN_ORDER)) { $hrefs = array_unique(array_merge(array_filter($matches[1]), array_filter($matches[2]), array_filter($matches[3]))); foreach ($hrefs as $url) { if (preg_match('%^\\w+://%i', $url)) { // mask only links having protocol // try to find matching cloaked link among already registered ones $list = new PMLC_Link_List(); $linkTable = $list->getTable(); $rule = new PMLC_Rule_Record(); $ruleTable = $rule->getTable(); $dest = new PMLC_Destination_Record(); $destTable = $dest->getTable(); $list->join($ruleTable, "{$ruleTable}.link_id = {$linkTable}.id")->join($destTable, "{$destTable}.rule_id = {$ruleTable}.id")->setColumns("{$linkTable}.*")->getBy(array("{$linkTable}.destination_type =" => 'ONE_SET', "{$linkTable}.is_trashed =" => 0, "{$linkTable}.preset =" => '', "{$linkTable}.expire_on =" => '0000-00-00', "{$ruleTable}.type =" => 'ONE_SET', "{$destTable}.weight =" => 100, "{$destTable}.url LIKE" => $url), NULL, 1, 1)->convertRecords(); if ($list->count()) { // matching link found $link = $list[0]; } else { // register new cloaked link global $wpdb; $slug = max(intval($wpdb->get_var("SELECT MAX(CONVERT(name, SIGNED)) FROM {$linkTable}")), intval($wpdb->get_var("SELECT MAX(CONVERT(slug, SIGNED)) FROM {$linkTable}")), 0); $i = 0; do { is_int(++$slug) and $slug > 0 or $slug = 1; $is_slug_found = !intval($wpdb->get_var("SELECT COUNT(*) FROM {$linkTable} WHERE name = '{$slug}' OR slug = '{$slug}'")); } while (!$is_slug_found and $i++ < 100000); if ($is_slug_found) { $link = new PMLC_Link_Record(array('name' => strval($slug), 'slug' => strval($slug), 'header_tracking_code' => '', 'footer_tracking_code' => '', 'redirect_type' => '301', 'destination_type' => 'ONE_SET', 'preset' => '', 'forward_url_params' => 1, 'no_global_tracking_code' => 0, 'expire_on' => '0000-00-00', 'created_on' => date('Y-m-d H:i:s'), 'is_trashed' => 0)); $link->insert(); $rule = new PMLC_Rule_Record(array('link_id' => $link->id, 'type' => 'ONE_SET', 'rule' => '')); $rule->insert(); $dest = new PMLC_Destination_Record(array('rule_id' => $rule->id, 'url' => $url, 'weight' => 100)); $dest->insert(); } else { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to create cloaked link for %s', 'pmxi_plugin'), $url)); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; $link = NULL; } } if ($link) { // cloaked link is found or created for url $articleData['post_content'] = preg_replace('%' . preg_quote($url, '%') . '(?=([\\s\'"]|$))%i', $link->getUrl(), $articleData['post_content']); } } } } } // insert article being imported $pid = $this->options['is_fast_mode'] ? pmxi_insert_post($articleData, true) : wp_insert_post($articleData, true); if (is_wp_error($pid)) { $logger and call_user_func($logger, __('<b>ERROR</b>', 'pmxi_plugin') . ': ' . $pid->get_error_message()); $logger and PMXI_Plugin::$session['pmxi_import']['errors'] = ++PMXI_Plugin::$session->data['pmxi_import']['errors']; } else { if ("manual" != $this->options['duplicate_matching'] or empty($articleData['ID'])) { // associate post with import $postRecord->isEmpty() and $postRecord->set(array('post_id' => $pid, 'import_id' => $this->id, 'unique_key' => $unique_keys[$i], 'product_key' => ($post_type == "product" and PMXI_Admin_Addons::get_addon('PMWI_Plugin')) ? $addons_data['PMWI_Plugin']['single_product_ID'][$i] : ''))->insert(); $postRecord->set(array('iteration' => $this->iteration))->update(); } // [post format] if (current_theme_supports('post-formats') && post_type_supports($post_type, 'post-formats')) { set_post_format($pid, $this->options['post_format']); } // [/post format] // [custom fields] if (empty($articleData['ID']) or $this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_custom_fields'] or $this->options['update_all_data'] == 'no' and $this->options['is_update_attributes']) { // Delete all meta keys if (!empty($articleData['ID'])) { // Get all existing meta keys $existing_meta_keys = array(); foreach (get_post_meta($pid, '') as $cur_meta_key => $cur_meta_val) { $existing_meta_keys[] = $cur_meta_key; } // delete keys which are no longer correspond to import settings foreach ($existing_meta_keys as $cur_meta_key) { // Do not delete post meta for features image if (in_array($cur_meta_key, array('_thumbnail_id', '_product_image_gallery'))) { continue; } $field_to_delete = true; // apply addons filters if (!apply_filters('pmxi_custom_field_to_delete', $field_to_delete, $pid, $post_type, $this->options, $cur_meta_key)) { continue; } // Update all Custom Fields is defined if ($this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_custom_fields'] and $this->options['update_custom_fields_logic'] == "full_update") { delete_post_meta($pid, $cur_meta_key); } elseif ($this->options['update_all_data'] == 'no' and $this->options['is_update_custom_fields'] and $this->options['update_custom_fields_logic'] == "only") { if (!empty($this->options['custom_fields_list']) and is_array($this->options['custom_fields_list']) and in_array($cur_meta_key, $this->options['custom_fields_list'])) { delete_post_meta($pid, $cur_meta_key); } } elseif ($this->options['update_all_data'] == 'no' and $this->options['is_update_custom_fields'] and $this->options['update_custom_fields_logic'] == "all_except") { if (empty($this->options['custom_fields_list']) or !in_array($cur_meta_key, $this->options['custom_fields_list'])) { delete_post_meta($pid, $cur_meta_key); } } } } $encoded_meta = array(); foreach ($serialized_meta as $m_key => $values) { if (!empty($articleData['ID'])) { if ($this->options['update_all_data'] != 'yes') { $field_to_update = false; if ($this->options['is_update_custom_fields'] and $this->options['update_custom_fields_logic'] == "only" and !empty($this->options['custom_fields_list']) and is_array($this->options['custom_fields_list']) and in_array($m_key, $this->options['custom_fields_list'])) { $field_to_update = true; } if ($this->options['is_update_custom_fields'] and $this->options['update_custom_fields_logic'] == "all_except" and (empty($this->options['custom_fields_list']) or !in_array($m_key, $this->options['custom_fields_list']))) { $field_to_update = true; } if ($this->options['update_custom_fields_logic'] == "full_update") { $field_to_update = true; } // apply addons filters $field_to_update = apply_filters('pmxi_custom_field_to_update', $field_to_update, $post_type, $this->options, $m_key); if (!$field_to_update) { continue; } } } update_post_meta($pid, $m_key, apply_filters('pmxi_custom_field', is_serialized($values[$i]) ? unserialize($values[$i]) : $values[$i], $this->id)); do_action('pmxi_update_post_meta', $pid, $m_key, is_serialized($values[$i]) ? unserialize($values[$i]) : $values[$i]); // hook that was triggered after post meta data updated } } // [/custom fields] // [addons import] // prepare data for import $importData = array('pid' => $pid, 'i' => $i, 'import' => $this, 'articleData' => $articleData, 'xml' => $xml, 'is_cron' => $is_cron, 'logger' => $logger, 'xpath_prefix' => $xpath_prefix); // deligate operation to addons foreach (PMXI_Admin_Addons::get_active_addons() as $class) { if (class_exists($class)) { if (method_exists($addons[$class], 'import')) { $addons[$class]->import($importData); } } else { $import_func = $class . '_import'; if (function_exists($import_func)) { call_user_func($import_func, $importData, $addons_data[$class]); } } } // [/addons import] // Page Template if ('post' != $articleData['post_type'] and !empty($this->options['page_template'])) { update_post_meta($pid, '_wp_page_template', $this->options['page_template']); } // [featured image] if (!empty($uploads) and false === $uploads['error'] and !empty($featured_images[$i]) and (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_images'])) { require_once ABSPATH . 'wp-admin/includes/image.php'; $success_images = false; $gallery_attachment_ids = array(); $_pmxi_images = array(); $imgs = !empty($this->options['featured_delim']) ? str_getcsv($featured_images[$i], $this->options['featured_delim']) : explode("\n", $featured_images[$i]); if (!empty($imgs)) { if ($this->options['set_image_meta_data']) { $img_titles = !empty($this->options['image_meta_title_delim']) ? str_getcsv($image_meta_titles[$i], $this->options['image_meta_title_delim']) : explode("\n", $image_meta_titles[$i]); $img_captions = !empty($this->options['image_meta_caption_delim']) ? str_getcsv($image_meta_captions[$i], $this->options['image_meta_caption_delim']) : explode("\n", $image_meta_captions[$i]); $img_alts = !empty($this->options['image_meta_alt_delim']) ? str_getcsv($image_meta_alts[$i], $this->options['image_meta_alt_delim']) : explode("\n", $image_meta_alts[$i]); $img_descriptions = !empty($this->options['image_meta_description_delim']) ? str_getcsv($image_meta_descriptions[$i], $this->options['image_meta_description_delim']) : explode("\n", $image_meta_descriptions[$i]); } foreach ($imgs as $k => $img_url) { if (empty($img_url)) { continue; } $url = str_replace(" ", "%20", trim($img_url)); $bn = preg_replace('/[\\?|&].*/', '', basename($url)); $img_ext = pmxi_getExtensionFromStr($url); $default_extension = pmxi_getExtension($bn); if ($img_ext == "") { $img_ext = pmxi_get_remote_image_ext($url); } // generate local file name $image_name = urldecode(($this->options['auto_rename_images'] and "" != $auto_rename_images[$i]) ? sanitize_file_name($auto_rename_images[$i] . '_' . ($img_ext ? str_replace("." . $default_extension, "", $bn) : $bn)) : sanitize_file_name($img_ext ? str_replace("." . $default_extension, "", $bn) : $bn)) . ("" != $img_ext ? '.' . $img_ext : ''); // if wizard store image data to custom field $create_image = false; $download_image = true; if (base64_decode($url, true) !== false) { $img = @imagecreatefromstring(base64_decode($url)); if ($img) { $image_filename = md5(time()) . '.jpg'; $image_filepath = $uploads['path'] . '/' . $image_filename; imagejpeg($img, $image_filepath); if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'pmxi_plugin'), $image_filepath)); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { $create_image = true; } } } else { $image_filename = wp_unique_filename($uploads['path'], $image_name); $image_filepath = $uploads['path'] . '/' . $image_filename; // keep existing and add newest images if (!empty($articleData['ID']) and $this->options['is_update_images'] and $this->options['update_images_logic'] == "add_new" and $this->options['update_all_data'] == "no") { $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $pid)); if ($attachment_imgs) { foreach ($attachment_imgs as $attachment_img) { if ($attachment_img->guid == $uploads['url'] . '/' . $image_name) { $download_image = false; $success_images = true; if (!has_post_thumbnail($pid)) { set_post_thumbnail($pid, $attachment_img->ID); } else { $gallery_attachment_ids[] = $attachment_img->ID; } $logger and call_user_func($logger, sprintf(__('<b>Image SKIPPED</b>: The image %s is always exists for the %s', 'pmxi_plugin'), basename($attachment_img->guid), $articleData['post_title'])); } } } } if ($download_image) { // do not download images if (!$this->options['download_images']) { $image_filename = $image_name; $image_filepath = $uploads['path'] . '/' . $image_filename; $existing_attachment = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->prefix . "posts WHERE guid = '%s'", $uploads['url'] . '/' . $image_filename)); if (!empty($existing_attachment->ID)) { $download_image = false; $create_image = false; if (!has_post_thumbnail($pid)) { set_post_thumbnail($pid, $existing_attachment->ID); } else { $gallery_attachment_ids[] = $existing_attachment->ID; } do_action('pmxi_gallery_image', $pid, $existing_attachment->ID, $image_filepath); } else { if (@file_exists($image_filepath)) { $download_image = false; if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'pmxi_plugin'), $image_filepath)); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; @unlink($image_filepath); } else { $create_image = true; } } } } if ($download_image) { $request = get_file_curl($url, $image_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) { @unlink($image_filepath); // delete file since failed upload may result in empty file created } elseif ($image_info = @getimagesize($image_filepath) and in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $create_image = true; } if (!$create_image) { $url = str_replace(" ", "%20", trim(pmxi_convert_encoding($img_url))); $request = get_file_curl($url, $image_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: File %s cannot be saved locally as %s', 'pmxi_plugin'), $url, $image_filepath)); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; @unlink($image_filepath); // delete file since failed upload may result in empty file created } elseif (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'pmxi_plugin'), $url)); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; @unlink($image_filepath); } else { $create_image = true; } } } } } if ($create_image) { $attachment = array('post_mime_type' => image_type_to_mime_type($image_info[2]), 'guid' => $uploads['url'] . '/' . $image_filename, 'post_title' => $image_filename, 'post_content' => '', 'post_author' => $post_author[$i]); if ($image_meta = wp_read_image_metadata($image_filepath)) { if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) { $attachment['post_title'] = $image_meta['title']; } if (trim($image_meta['caption'])) { $attachment['post_content'] = $image_meta['caption']; } } $attid = $this->options['is_fast_mode'] ? pmxi_insert_attachment($attachment, $image_filepath, $pid) : wp_insert_attachment($attachment, $image_filepath, $pid); if (is_wp_error($attid)) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $attid->get_error_message()); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once ABSPATH . 'wp-admin/includes/image.php'; wp_update_attachment_metadata($attid, wp_generate_attachment_metadata($attid, $image_filepath)); if ($this->options['set_image_meta_data']) { $update_attachment_meta = array(); if (!empty($img_titles[$k])) { $update_attachment_meta['post_title'] = $img_titles[$k]; } if (!empty($img_captions[$k])) { $update_attachment_meta['post_excerpt'] = $img_captions[$k]; } if (!empty($img_descriptions[$k])) { $update_attachment_meta['post_content'] = $img_descriptions[$k]; } if (!empty($img_alts[$k])) { update_post_meta($attid, '_wp_attachment_image_alt', $img_alts[$k]); } if (!empty($update_attachment_meta)) { $this->wpdb->update($this->wpdb->posts, $update_attachment_meta, array('ID' => $attid)); } } do_action('pmxi_gallery_image', $pid, $attid, $image_filepath); $success_images = true; if (!has_post_thumbnail($pid)) { set_post_thumbnail($pid, $attid); } else { $gallery_attachment_ids[] = $attid; } } } } } // Set product gallery images if ($post_type == "product" and !empty($gallery_attachment_ids)) { update_post_meta($pid, '_product_image_gallery', implode(',', $gallery_attachment_ids)); } // Create entry as Draft if no images are downloaded successfully if (!$success_images and "yes" == $this->options['create_draft']) { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $pid)); } } // [/featured image] // [attachments] if (!empty($uploads) and false === $uploads['error'] and !empty($attachments[$i]) and (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_attachments'])) { // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once ABSPATH . 'wp-admin/includes/image.php'; if (!is_array($attachments[$i])) { $attachments[$i] = array($attachments[$i]); } foreach ($attachments[$i] as $attachment) { if ("" == $attachment) { continue; } $atchs = str_getcsv($attachment, $this->options['atch_delim']); if (!empty($atchs)) { foreach ($atchs as $atch_url) { if (empty($atch_url)) { continue; } $atch_url = str_replace(" ", "%20", trim($atch_url)); $attachment_filename = wp_unique_filename($uploads['path'], urldecode(basename(parse_url(trim($atch_url), PHP_URL_PATH)))); $attachment_filepath = $uploads['path'] . '/' . sanitize_file_name($attachment_filename); $request = get_file_curl(trim($atch_url), $attachment_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($attachment_filepath, @file_get_contents(trim($atch_url)))) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Attachment file %s cannot be saved locally as %s', 'pmxi_plugin'), trim($atch_url), $attachment_filepath)); is_wp_error($request) and $logger and call_user_func($logger, sprintf(__('<b>WP Error</b>: %s', 'pmxi_plugin'), $request->get_error_message())); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; unlink($attachment_filepath); // delete file since failed upload may result in empty file created } elseif (!($wp_filetype = wp_check_filetype(basename($attachment_filename), null))) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Can\'t detect attachment file type %s', 'pmxi_plugin'), trim($atch_url))); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { $attachment_data = array('guid' => $uploads['baseurl'] . '/' . _wp_relative_upload_path($attachment_filepath), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($attachment_filepath)), 'post_content' => '', 'post_status' => 'inherit', 'post_author' => $post_author[$i]); $attach_id = $this->options['is_fast_mode'] ? pmxi_insert_attachment($attachment_data, $attachment_filepath, $pid) : wp_insert_attachment($attachment_data, $attachment_filepath, $pid); if (is_wp_error($attach_id)) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $pid->get_error_message()); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } else { wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attachment_filepath)); do_action('pmxi_attachment_uploaded', $pid, $attid, $image_filepath); } } } } } } // [/attachments] // [custom taxonomies] if (!empty($taxonomies)) { foreach ($taxonomies as $tx_name => $txes) { // Skip updating product attributes if (PMXI_Admin_Addons::get_addon('PMWI_Plugin') and strpos($tx_name, "pa_") === 0) { continue; } if (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_categories']) { if (!empty($articleData['ID'])) { if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "all_except" and !empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and in_array($tx_name, $this->options['taxonomies_list'])) { continue; } if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "only" and (!empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and !in_array($tx_name, $this->options['taxonomies_list']) or empty($this->options['taxonomies_list']))) { continue; } } $assign_taxes = array(); if ($this->options['update_categories_logic'] == "add_new" and !empty($existing_taxonomies[$tx_name][$i])) { $assign_taxes = $existing_taxonomies[$tx_name][$i]; unset($existing_taxonomies[$tx_name][$i]); } elseif (!empty($existing_taxonomies[$tx_name][$i])) { unset($existing_taxonomies[$tx_name][$i]); } // create term if not exists if (!empty($txes[$i])) { foreach ($txes[$i] as $key => $single_tax) { if (is_array($single_tax)) { $parent_id = !empty($single_tax['parent']) ? pmxi_recursion_taxes($single_tax['parent'], $tx_name, $txes[$i], $key) : ''; $term = is_exists_term($tx_name, $single_tax['name'], (int) $parent_id); if (empty($term) and !is_wp_error($term)) { $term_attr = array('parent' => !empty($parent_id) ? $parent_id : 0); $term = wp_insert_term($single_tax['name'], $tx_name, $term_attr); } if (is_wp_error($term)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: `%s`', 'pmxi_plugin'), $term->get_error_message())); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } elseif (!empty($term)) { $cat_id = $term['term_id']; if ($cat_id and $single_tax['assign']) { $term = get_term_by('id', $cat_id, $tx_name); if (!in_array($term->slug, $assign_taxes)) { $assign_taxes[] = $term->slug; } } } } } } if (!empty($assign_taxes)) { // associate taxes with post $term_ids = wp_set_object_terms($pid, $assign_taxes, $tx_name); if (is_wp_error($term_ids)) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $term_ids->get_error_message()); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } } } } if (!empty($existing_taxonomies) and $this->options['update_all_data'] == "no" and $this->options['is_update_categories'] and $this->options['update_categories_logic'] != 'full_update') { foreach ($existing_taxonomies as $tx_name => $txes) { // Skip updating product attributes if (PMXI_Admin_Addons::get_addon('PMWI_Plugin') and strpos($tx_name, "pa_") === 0) { continue; } if (!empty($txes[$i])) { $term_ids = wp_set_object_terms($pid, $txes[$i], $tx_name); if (is_wp_error($term_ids)) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $term_ids->get_error_message()); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } } } } } // [/custom taxonomies] // [categories] if (!empty($cats[$i])) { if (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_categories']) { wp_set_object_terms($pid, NULL, 'category'); $is_update_cats = true; if (!empty($articleData['ID'])) { if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "all_except" and !empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and in_array('category', $this->options['taxonomies_list'])) { $is_update_cats = false; } if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "only" and (!empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and !in_array('category', $this->options['taxonomies_list']) or empty($this->options['taxonomies_list']))) { $is_update_cats = false; } } if ($is_update_cats) { $assign_cats = array(); if ($this->options['update_categories_logic'] == "add_new" and !empty($existing_cats[$i])) { $assign_cats = $existing_cats[$i]; unset($existing_cats[$i]); } elseif (!empty($existing_cats[$i])) { unset($existing_cats[$i]); } // create categories if it's doesn't exists foreach ($cats[$i] as $key => $single_cat) { if (is_array($single_cat)) { $parent_id = !empty($single_cat['parent']) ? pmxi_recursion_taxes($single_cat['parent'], 'category', $cats[$i], $key) : ''; $term = is_exists_term('category', $single_cat['name'], (int) $parent_id); if (empty($term) and !is_wp_error($term)) { $term = wp_insert_term($single_cat['name'], 'category', array('parent' => !empty($parent_id) ? (int) $parent_id : 0)); } if (is_wp_error($term)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: `%s`', 'pmxi_plugin'), $term->get_error_message())); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } elseif (!empty($term)) { $cat_id = $term['term_id']; if ($cat_id and $single_cat['assign']) { $term = get_term_by('id', $cat_id, 'category'); if (!in_array($term->slug, $assign_cats)) { $assign_cats[] = $term->slug; } } } } } // associate categories with post $cats_ids = wp_set_object_terms($pid, $assign_cats, 'category'); if (is_wp_error($cats_ids)) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $cats_ids->get_error_message()); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } } } } if (!empty($existing_cats[$i]) and $this->options['update_all_data'] == "no" and (!$this->options['is_update_categories'] or $this->options['is_update_categories'] and $this->options['update_categories_logic'] != 'full_update')) { $cats_ids = wp_set_object_terms($pid, $existing_cats[$i], 'category'); if (is_wp_error($cats_ids)) { $logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $cats_ids->get_error_message()); $logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings']; } } // [/categories] if (empty($articleData['ID'])) { $logger and call_user_func($logger, sprintf(__('`%s` post created successfully', 'pmxi_plugin'), $articleData['post_title'])); } else { $logger and call_user_func($logger, sprintf(__('`%s` post updated successfully', 'pmxi_plugin'), $articleData['post_title'])); } // [addons import] // prepare data for import $importData = array('pid' => $pid, 'import' => $this, 'logger' => $logger); // deligate operation to addons foreach (PMXI_Admin_Addons::get_active_addons() as $class) { if (class_exists($class)) { if (method_exists($addons[$class], 'saved_post')) { $addons[$class]->saved_post($importData); } } else { $saved_func = $class . '_saved_post'; if (function_exists($saved_func)) { call_user_func($saved_func, $importData); } } } // [/addons import] do_action('pmxi_saved_post', $pid); // hook that was triggered immediately after post saved if (empty($articleData['ID'])) { $created++; } else { $updated++; } if (!$is_cron and "default" == $this->options['import_processing']) { $processed_records = $created + $updated + $skipped + PMXI_Plugin::$session->data['pmxi_import']['errors']; $logger and call_user_func($logger, sprintf(__('<span class="processing_info"><span class="created_count">%s</span><span class="updated_count">%s</span><span class="percents_count">%s</span></span>', 'pmxi_plugin'), $created, $updated, ceil($processed_records / $this->count * 100))); } } do_action('pmxi_after_post_import', $this->id); $logger and PMXI_Plugin::$session['pmxi_import']['chunk_number'] = ++PMXI_Plugin::$session->data['pmxi_import']['chunk_number']; } wp_cache_flush(); $this->set(array('imported' => $created + $updated, 'created' => $created, 'updated' => $updated, 'skipped' => $skipped, 'queue_chunk_number' => $created + $updated + $skipped))->update(); if (!$is_cron) { pmxi_session_commit(); $records_count = $this->created + $this->updated + $this->skipped + PMXI_Plugin::$session->data['pmxi_import']['errors']; $is_import_complete = $records_count == $this->count; // Delete posts that are no longer present in your file if ($is_import_complete and !empty($this->options['is_delete_missing']) and $this->options['duplicate_matching'] == 'auto') { $logger and call_user_func($logger, 'Removing previously imported posts which are no longer actual...'); $postList = new PMXI_Post_List(); $missing_ids = array(); $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration)); if (!$missingPosts->isEmpty()) { foreach ($missingPosts as $missingPost) { $missing_ids[] = $missingPost['post_id']; // Instead of deletion, set Custom Field if ($this->options['is_update_missing_cf']) { update_post_meta($missingPost['post_id'], $this->options['update_missing_cf_name'], $this->options['update_missing_cf_value']); } // Instead of deletion, change post status to Draft if ($this->options['set_missing_to_draft']) { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $missingPost['post_id'])); } // Delete posts that are no longer present in your file if (!$this->options['is_update_missing_cf'] and !$this->options['set_missing_to_draft']) { // Remove attachments empty($this->options['is_keep_attachments']) and wp_delete_attachments($missingPost['post_id'], true, 'files'); // Remove images empty($this->options['is_keep_imgs']) and wp_delete_attachments($missingPost['post_id'], $this->options['download_images']); if (!empty($missingPost['id'])) { // Delete record form pmxi_posts $missingRecord = new PMXI_Post_Record(); $missingRecord->getById($missingPost['id'])->delete(); } else { $sql = "DELETE FROM " . PMXI_Plugin::getInstance()->getTablePrefix() . "posts WHERE post_id = " . $missingPost['post_id'] . " AND import_id = " . $missingPost['import_id']; $this->wpdb->query($this->wpdb->prepare($sql, '')); } // Clear post's relationships wp_delete_object_term_relationships($missingPost['post_id'], get_object_taxonomies('' != $this->options['custom_type'] ? $this->options['custom_type'] : 'post')); } } } // Delete posts from database if (!empty($missing_ids) && is_array($missing_ids) and !$this->options['is_update_missing_cf'] and !$this->options['set_missing_to_draft']) { $sql = "delete a,b,c\n\t\t\t\t\t\tFROM " . $this->wpdb->posts . " a\n\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->postmeta . " c ON ( a.ID = c.post_id )\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $missing_ids) . ")"; $this->wpdb->query($this->wpdb->prepare($sql, '')); do_action('pmxi_delete_post', $missing_ids); } } // Set out of stock status for missing records [Woocommerce add-on option] if ($is_import_complete and empty($this->options['is_delete_missing']) and $post_type == "product" and class_exists('PMWI_Plugin') and !empty($this->options['missing_records_stock_status'])) { $logger and call_user_func($logger, 'Update stock status previously imported posts which are no longer actual...'); $postList = new PMXI_Post_List(); $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration)); if (!$missingPosts->isEmpty()) { foreach ($missingPosts as $missingPost) { update_post_meta($missingPost['post_id'], '_stock_status', 'outofstock'); update_post_meta($missingPost['post_id'], '_stock', 0); } } } } } catch (XmlImportException $e) { $logger and call_user_func($logger, __('<b>ERROR</b>', 'pmxi_plugin') . ': ' . $e->getMessage()); $logger and PMXI_Plugin::$session['pmxi_import']['errors'] = ++PMXI_Plugin::$session->data['pmxi_import']['errors']; } $logger and $is_import_complete and call_user_func($logger, __('Cleaning temporary data...', 'pmxi_plugin')); foreach ($tmp_files as $file) { // remove all temporary files created @unlink($file); } if (($is_cron or $is_import_complete) and $this->options['is_delete_source']) { $logger and call_user_func($logger, __('Deleting source XML file...', 'pmxi_plugin')); // Delete chunks foreach (PMXI_Helper::safe_glob($uploads['path'] . '/pmxi_chunk_*', PMXI_Helper::GLOB_RECURSE | PMXI_Helper::GLOB_PATH) as $filePath) { @file_exists($filePath) and @unlink($filePath); } if ($this->type != "ftp") { if (!@unlink($this->path)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to remove %s', 'pmxi_plugin'), $this->path)); } } else { $file_path_array = PMXI_Helper::safe_glob($this->path, PMXI_Helper::GLOB_NODIR | PMXI_Helper::GLOB_PATH); if (!empty($file_path_array)) { foreach ($file_path_array as $path) { if (!@unlink($path)) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Unable to remove %s', 'pmxi_plugin'), $path)); } } } } } if (!$is_cron and $is_import_complete) { $this->set(array('processing' => 0, 'triggered' => 0, 'queue_chunk_number' => 0, 'registered_on' => date('Y-m-d H:i:s'), 'iteration' => ++$this->iteration))->update(); $logger and call_user_func($logger, 'Done'); } remove_filter('user_has_cap', array($this, '_filter_has_cap_unfiltered_html')); kses_init(); // return any filtering rules back if they has been disabled for import procedure return $this; }
protected function _import_order_notes(&$order, $order_id, $index) { if (!empty($this->data['pmwi_order']['notes'][$index])) { $notes_count = 0; foreach ($this->data['pmwi_order']['notes'][$index] as $noteIndex => $note) { if (empty($note['content'])) { continue; } $note_item = new PMXI_Post_Record(); $note_item->getBy(array('import_id' => $this->import->id, 'post_id' => $order_id, 'unique_key' => 'note-item-' . $order_id . '-' . $noteIndex)); if (!$note_item->isEmpty()) { $note_id = str_replace('note-item-', '', $note_item->product_key); $is_note_exist = get_comment($note_id); if (empty($is_note_exist) || $is_note_exist->comment_approved == 'trash') { $note_item->delete(); $note_item->clear(); if (!empty($is_note_exist) && $is_note_exist->comment_approved == 'trash') { wp_delete_comment($note_id, true); } } } $comment_author = empty($note['username']) ? 'WP All Import' : $note['username']; $comment_author_email = $note['email']; if (empty($comment_author) && empty($comment_author_email)) { if (is_user_logged_in()) { $user = get_user_by('id', get_current_user_id()); $comment_author_email = $user->user_email; } else { $comment_author_email = strtolower(__('WooCommerce', 'wp_all_import_plugin')) . '@'; $comment_author_email .= isset($_SERVER['HTTP_HOST']) ? str_replace('www.', '', $_SERVER['HTTP_HOST']) : 'noreply.com'; $comment_author_email = sanitize_email($comment_author_email); } } $comment_post_ID = $order_id; $comment_author_url = ''; $comment_content = $note['content']; $comment_agent = 'WooCommerce'; $comment_type = 'order_note'; $comment_parent = 0; $comment_approved = 1; $is_customer_note = $note['visibility'] == "private" ? 0 : 1; $commentdata = apply_filters('woocommerce_new_order_note_data', compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_agent', 'comment_type', 'comment_parent', 'comment_approved'), array('order_id' => $order_id, 'is_customer_note' => $is_customer_note)); if ($note_item->isEmpty()) { $comment_id = false; if (!empty($this->articleData['ID'])) { $current_notes = $this->get_order_notes($order_id); if (!empty($current_notes)) { foreach ($current_notes as $current_note) { if ($current_note->comment_content == $commentdata['comment_content']) { $comment_id = $current_note->comment_ID; break; } } } } if (!$comment_id) { $comment_id = wp_insert_comment($commentdata); if ($note['visibility'] != 'private') { add_comment_meta($comment_id, 'is_customer_note', 1); // send customer note notification if (empty($this->import->options['do_not_send_order_notifications'])) { do_action('woocommerce_new_customer_note', array('order_id' => $order_id, 'customer_note' => $commentdata['comment_content'])); } } } $note_item->set(array('import_id' => $this->import->id, 'post_id' => $order_id, 'unique_key' => 'note-item-' . $order_id . '-' . $noteIndex, 'product_key' => 'note-item-' . $comment_id, 'iteration' => $this->import->iteration))->save(); } else { $commentdata['comment_ID'] = str_replace('note-item-', '', $note_item->product_key); wp_update_comment($commentdata); if ($note['visibility'] != 'private') { update_comment_meta($commentdata['comment_ID'], 'is_customer_note', 1); } else { delete_comment_meta($commentdata['comment_ID'], 'is_customer_note'); } $note_item->set(array('iteration' => $this->import->iteration))->save(); } $notes_count++; } global $wpdb; $wpdb->update($wpdb->posts, array('comment_count' => $notes_count), array('ID' => $order_id), array('%d'), array('%d')); } }
/** * Export XML helper */ function pmxe_export_xml($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false) { $xmlWriter = new XMLWriter(); $xmlWriter->openMemory(); $xmlWriter->setIndent(true); $xmlWriter->setIndentString("\t"); $xmlWriter->startDocument('1.0', $exportOptions['encoding']); $xmlWriter->startElement('data'); global $wpdb; while ($exportQuery->have_posts()) { $exportQuery->the_post(); $record = get_post(get_the_ID()); // global $wpdb; // $table_prefix = $wpdb->prefix; // $order_items = null; // if ( "shop_order" == get_post_type() ) // { // $order_items = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_items WHERE order_id = {$record->ID}"); // } $xmlWriter->startElement('post'); if ($exportOptions['ids']) { if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'])); if ($postRecord->isEmpty()) { $postRecord->set(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $record->ID, 'product_key' => get_post_meta($record->ID, '_sku', true)))->save(); } unset($postRecord); } foreach ($exportOptions['ids'] as $ID => $value) { if (is_numeric($ID)) { if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) { continue; } $element_name = !empty($exportOptions['cc_name'][$ID]) ? str_replace(" ", "_", $exportOptions['cc_name'][$ID]) : 'untitled_' . $ID; $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false; switch ($exportOptions['cc_type'][$ID]) { case 'id': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_id', pmxe_filter(get_the_ID(), $fieldSnipped), get_the_ID())); break; case 'permalink': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_guid', pmxe_filter(get_permalink(), $fieldSnipped), get_the_ID())); break; case 'post_type': $pType = get_post_type(); if ($pType == 'product_variation') { $pType = 'product'; } $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), get_the_ID())); break; case 'title': $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_post_title', pmxe_filter($record->post_title, $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); break; case 'content': $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_post_content', pmxe_filter($record->post_content, $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); break; case 'media': $xmlWriter->startElement($element_name); $attachment_ids = array(); $_featured_image = get_post_meta(get_the_ID(), '_thumbnail_id', true); if (!empty($_featured_image)) { $attachment_ids[] = $_featured_image; } $_gallery = get_post_meta(get_the_ID(), '_product_image_gallery', true); if (!empty($_gallery)) { $gallery = explode(',', $_gallery); if (!empty($gallery) and is_array($gallery)) { foreach ($gallery as $aid) { if (!in_array($aid, $attachment_ids)) { $attachment_ids[] = $aid; } } } } if (!empty($attachment_ids)) { foreach ($attachment_ids as $attach_id) { $attach = get_post($attach_id); if (wp_attachment_is_image($attach->ID)) { $xmlWriter->startElement('image'); $val = wp_get_attachment_url($attach->ID); if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'filenames': $val = basename(wp_get_attachment_url($attach->ID)); break; case 'filepaths': $val = get_attached_file($attach->ID); break; default: # code... break; } } $xmlWriter->writeElement('file', apply_filters('pmxe_attachment_url', $val, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('title', apply_filters('pmxe_attachment_title', $attach->post_title, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('caption', apply_filters('pmxe_attachment_caption', $attach->post_excerpt, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('description', apply_filters('pmxe_attachment_content', $attach->post_content, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('alt', apply_filters('pmxe_attachment_alt', get_post_meta($record->ID, '_wp_attachment_image_alt', true), get_the_ID(), $attach->ID)); $xmlWriter->endElement(); } } } $xmlWriter->endElement(); break; case 'date': if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'unix': $post_date = get_post_time('U', true); break; default: $post_date = date($exportOptions['cc_options'][$ID], get_post_time('U', true)); break; } } else { $post_date = date("Ymd", get_post_time('U', true)); } $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), get_the_ID())); break; case 'attachments': $xmlWriter->startElement($element_name); $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID)); if (!empty($attachment_imgs)) { foreach ($attachment_imgs as $attach) { if (!wp_attachment_is_image($attach->ID)) { $xmlWriter->startElement('attach'); $xmlWriter->writeElement('url', apply_filters('pmxe_attachment_url', pmxe_filter(wp_get_attachment_url($attach->ID), $fieldSnipped), get_the_ID(), $attach->ID)); $xmlWriter->endElement(); } } } $xmlWriter->endElement(); // end attachments break; case 'parent': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_parent', pmxe_filter($record->post_parent, $fieldSnipped), get_the_ID())); break; case 'template': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_template', pmxe_filter(get_post_meta($record->ID, '_wp_page_template', true), $fieldSnipped), get_the_ID())); break; case 'order': $xmlWriter->writeElement($element_name, apply_filters('pmxe_menu_order', pmxe_filter($record->menu_order, $fieldSnipped), get_the_ID())); break; case 'status': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_status', pmxe_filter($record->post_status, $fieldSnipped), get_the_ID())); break; case 'format': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_format', pmxe_filter(get_post_format($record->ID), $fieldSnipped), get_the_ID())); break; case 'author': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_author', pmxe_filter($record->post_author, $fieldSnipped), get_the_ID())); break; case 'slug': $xmlWriter->writeElement($element_name, apply_filters('pmxe_post_slug', pmxe_filter($record->post_name, $fieldSnipped), get_the_ID())); break; case 'excerpt': $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_post_excerpt', pmxe_filter($record->post_excerpt, $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); break; case 'cf': if (!empty($exportOptions['cc_value'][$ID])) { $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_value'][$ID]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID())); $xmlWriter->endElement(); } } if (empty($cur_meta_values)) { $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID())); $xmlWriter->endElement(); } } break; case 'acf': if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) { global $acf; $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID); $field_options = unserialize($exportOptions['cc_options'][$ID]); pmxe_export_acf_field_xml($field_value, $exportOptions, $ID, $record->ID, $xmlWriter, $element_name, $fieldSnipped, $field_options['group_id']); } break; case 'woo': //$element_name = 'woo' . $element_name; if (!empty($exportOptions['cc_label'][$ID])) { $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_label'][$ID]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { switch ($exportOptions['cc_label'][$ID]) { case '_downloadable_files': $files = maybe_unserialize($cur_meta_value); $file_paths = array(); $file_names = array(); if (!empty($files)) { foreach ($files as $key => $file) { $file_paths[] = $file['file']; $file_names[] = $file['name']; } $xmlWriter->startElement($element_name . '_paths'); $xmlWriter->writeCData(implode(',', $file_paths)); $xmlWriter->endElement(); $xmlWriter->startElement($element_name . '_names'); $xmlWriter->writeCData(implode(',', $file_names)); $xmlWriter->endElement(); } break; case '_crosssell_ids': case '_upsell_ids': $_upsell_ids = maybe_unserialize($cur_meta_value); $_upsell_skus = array(); if (!empty($_upsell_ids)) { foreach ($_upsell_ids as $_upsell_id) { $_upsell_skus[] = get_post_meta($_upsell_id, '_sku', true); } $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('woo_field', pmxe_filter(implode(',', $_upsell_skus), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID())); $xmlWriter->endElement(); } break; default: $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID())); $xmlWriter->endElement(); break; } } } if (empty($cur_meta_values)) { $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('woo_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID())); $xmlWriter->endElement(); } } break; case 'woo_order': XmlExportWooCommerceOrder::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID); break; case 'attr': //$element_name = 'woo_' . $element_name; if (!empty($exportOptions['cc_value'][$ID])) { if ($record->post_parent == 0) { $is_variable_product = false; $product_terms = wp_get_post_terms($record->ID, 'product_type'); if (!empty($product_terms)) { if (!is_wp_error($product_terms)) { foreach ($product_terms as $term) { if ('variable' == $term->slug) { $is_variable_product = true; break; } } } } $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]); if (!is_wp_error($txes_list)) { $attr_new = array(); if (!empty($txes_list)) { foreach ($txes_list as $t) { $attr_new[] = $t->slug; } $xmlWriter->startElement($is_variable_product ? $element_name : 'attribute_' . $element_name); $xmlWriter->writeCData(apply_filters('pmxe_woo_attribute', pmxe_filter(implode('|', $attr_new), $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); } } } else { $attribute_pa = get_post_meta($record->ID, 'attribute_' . $exportOptions['cc_value'][$ID], true); if (!empty($attribute_pa)) { $xmlWriter->startElement('attribute_' . $element_name); $xmlWriter->writeCData(apply_filters('woo_field', $attribute_pa)); $xmlWriter->endElement(); } } } break; case 'cats': if (!empty($exportOptions['cc_value'][$ID])) { $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]); if (!is_wp_error($txes_list)) { $txes_ids = array(); $hierarchy_groups = array(); if (!empty($txes_list)) { foreach ($txes_list as $t) { $txes_ids[] = $t->term_id; } foreach ($txes_list as $t) { if (wp_all_export_check_children_assign($t->term_id, $exportOptions['cc_value'][$ID], $txes_ids)) { $ancestors = get_ancestors($t->term_id, $exportOptions['cc_value'][$ID]); if (count($ancestors) > 0) { $hierarchy_group = array(); for ($i = count($ancestors) - 1; $i >= 0; $i--) { $term = get_term_by('id', $ancestors[$i], $exportOptions['cc_value'][$ID]); if ($term) { $hierarchy_group[] = $term->slug; } } $hierarchy_group[] = $t->slug; $hierarchy_groups[] = implode(">", $hierarchy_group); } else { $hierarchy_groups[] = $t->slug; } } } if (!empty($hierarchy_groups)) { $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_post_taxonomy', pmxe_filter(implode('|', $hierarchy_groups), $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); } } } if ($exportOptions['cc_label'][$ID] == 'product_type' and get_post_type() == 'product_variation') { $xmlWriter->writeElement('parent_sku', get_post_meta($record->post_parent, '_sku', true)); $xmlWriter->writeElement($element_name, 'variable'); } } break; case 'sql': if (!empty($exportOptions['cc_sql'][$ID])) { $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID())); if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) { // if shortcode defined if (strpos($exportOptions['cc_code'][$ID], '[') === 0) { $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])); } else { $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';'); } } $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID())); $xmlWriter->endElement(); } break; default: # code... break; } } } } $xmlWriter->endElement(); // end post if ($preview) { break; } } $xmlWriter->endElement(); // end data if ($preview) { return wp_all_export_remove_colons($xmlWriter->flush(true)); } if ($is_cron) { $xml = $xmlWriter->flush(true); if (file_exists($file_path)) { file_put_contents($file_path, wp_all_export_remove_colons(substr(substr($xml, 45), 0, -8)), FILE_APPEND); } else { file_put_contents($file_path, wp_all_export_remove_colons(substr($xml, 0, -8))); } return $file_path; } else { if (empty(PMXE_Plugin::$session->file)) { $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); $wp_uploads = wp_upload_dir(); $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path']; $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to']; file_put_contents($export_file, wp_all_export_remove_colons(substr($xmlWriter->flush(true), 0, -8))); PMXE_Plugin::$session->set('file', $export_file); PMXE_Plugin::$session->save_data(); } else { file_put_contents(PMXE_Plugin::$session->file, wp_all_export_remove_colons(substr(substr($xmlWriter->flush(true), 45), 0, -8)), FILE_APPEND); } return true; } }
function pmxe_export_xml($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0) { require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php'; $xmlWriter = new PMXE_XMLWriter(); $xmlWriter->openMemory(); $xmlWriter->setIndent(true); $xmlWriter->setIndentString("\t"); $xmlWriter->startDocument('1.0', $exportOptions['encoding']); $xmlWriter->startElement($exportOptions['main_xml_tag']); if ($is_cron) { if (!$exported_by_cron) { $additional_data = apply_filters('wp_all_export_additional_data', array(), $exportOptions); if (!empty($additional_data)) { foreach ($additional_data as $key => $value) { $xmlWriter->startElement(preg_replace('/[^a-z0-9_-]/i', '', $key)); $xmlWriter->writeCData($value); $xmlWriter->endElement(); } } } } else { if (empty(PMXE_Plugin::$session->file)) { $additional_data = apply_filters('wp_all_export_additional_data', array(), $exportOptions); if (!empty($additional_data)) { foreach ($additional_data as $key => $value) { $xmlWriter->startElement(preg_replace('/[^a-z0-9_-]/i', '', $key)); $xmlWriter->writeCData($value); $xmlWriter->endElement(); } } } } global $wpdb; while ($exportQuery->have_posts()) { $exportQuery->the_post(); $record = get_post(get_the_ID()); $xmlWriter->startElement($exportOptions['record_xml_tag']); if ($exportOptions['ids']) { if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'])); if ($postRecord->isEmpty()) { $postRecord->set(array('post_id' => $record->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $record->ID, 'product_key' => $record->ID))->save(); } unset($postRecord); } foreach ($exportOptions['ids'] as $ID => $value) { if (is_numeric($ID)) { if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) { continue; } $element_name_ns = ''; $element_name = !empty($exportOptions['cc_name'][$ID]) ? preg_replace('/[^a-z0-9_:-]/i', '', $exportOptions['cc_name'][$ID]) : 'untitled_' . $ID; $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false; if (strpos($element_name, ":") !== false) { $element_name_parts = explode(":", $element_name); $element_name_ns = empty($element_name_parts[0]) ? '' : $element_name_parts[0]; $element_name = empty($element_name_parts[1]) ? 'untitled_' . $ID : preg_replace('/[^a-z0-9_-]/i', '', $element_name_parts[1]); } switch ($exportOptions['cc_type'][$ID]) { case 'id': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_id', pmxe_filter(get_the_ID(), $fieldSnipped), get_the_ID())); break; case 'permalink': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_guid', pmxe_filter(get_permalink(), $fieldSnipped), get_the_ID())); break; case 'post_type': $pType = get_post_type(); if ($pType == 'product_variation') { $pType = 'product'; } $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_type', pmxe_filter($pType, $fieldSnipped), get_the_ID())); break; case 'title': $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_post_title', pmxe_filter($record->post_title, $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); break; case 'content': $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_post_content', pmxe_filter($record->post_content, $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); break; case 'media': $xmlWriter->beginElement($element_name_ns, $element_name, null); $attachment_ids = array(); $_featured_image = get_post_meta(get_the_ID(), '_thumbnail_id', true); if (!empty($_featured_image)) { $attachment_ids[] = $_featured_image; } $_gallery = get_post_meta(get_the_ID(), '_product_image_gallery', true); if (!empty($_gallery)) { $gallery = explode(',', $_gallery); if (!empty($gallery) and is_array($gallery)) { foreach ($gallery as $aid) { if (!in_array($aid, $attachment_ids)) { $attachment_ids[] = $aid; } } } } $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID)); if (!empty($attachment_imgs)) { foreach ($attachment_imgs as $attach) { if (wp_attachment_is_image($attach->ID) and !in_array($attach->ID, $attachment_ids)) { $attachment_ids[] = $attach->ID; } } } if (!empty($attachment_ids)) { foreach ($attachment_ids as $attach_id) { $attach = get_post($attach_id); if ($attach and !is_wp_error($attach) and wp_attachment_is_image($attach->ID)) { $xmlWriter->startElement('image'); $val = wp_get_attachment_url($attach->ID); if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'filenames': $val = basename(wp_get_attachment_url($attach->ID)); break; case 'filepaths': $val = get_attached_file($attach->ID); break; default: # code... break; } } $xmlWriter->writeElement('file', apply_filters('pmxe_attachment_url', $val, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('title', apply_filters('pmxe_attachment_title', $attach->post_title, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('caption', apply_filters('pmxe_attachment_caption', $attach->post_excerpt, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('description', apply_filters('pmxe_attachment_content', $attach->post_content, get_the_ID(), $attach->ID)); $xmlWriter->writeElement('alt', apply_filters('pmxe_attachment_alt', get_post_meta($record->ID, '_wp_attachment_image_alt', true), get_the_ID(), $attach->ID)); $xmlWriter->endElement(); } } } $xmlWriter->endElement(); break; case 'date': if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'unix': $post_date = get_post_time('U', true); break; default: $post_date = date($exportOptions['cc_options'][$ID], get_post_time('U', true)); break; } } else { $post_date = date("Ymd", get_post_time('U', true)); } $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), get_the_ID())); break; case 'attachments': $xmlWriter->beginElement($element_name_ns, $element_name, null); $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $record->ID)); if (!empty($attachment_imgs)) { foreach ($attachment_imgs as $attach) { if (!wp_attachment_is_image($attach->ID)) { $xmlWriter->startElement('attach'); $xmlWriter->writeElement('url', apply_filters('pmxe_attachment_url', pmxe_filter(wp_get_attachment_url($attach->ID), $fieldSnipped), get_the_ID(), $attach->ID)); $xmlWriter->endElement(); } } } $xmlWriter->endElement(); // end attachments break; case 'parent': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_parent', pmxe_filter($record->post_parent, $fieldSnipped), get_the_ID())); break; case 'comment_status': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_comment_status', pmxe_filter($record->comment_status, $fieldSnipped), get_the_ID())); break; case 'ping_status': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_ping_status', pmxe_filter($record->ping_status, $fieldSnipped), get_the_ID())); break; case 'template': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_template', pmxe_filter(get_post_meta($record->ID, '_wp_page_template', true), $fieldSnipped), get_the_ID())); break; case 'order': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_menu_order', pmxe_filter($record->menu_order, $fieldSnipped), get_the_ID())); break; case 'status': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_status', pmxe_filter($record->post_status, $fieldSnipped), get_the_ID())); break; case 'format': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_format', pmxe_filter(get_post_format($record->ID), $fieldSnipped), get_the_ID())); break; case 'author': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_author', pmxe_filter($record->post_author, $fieldSnipped), get_the_ID())); break; case 'slug': $xmlWriter->putElement($element_name_ns, $element_name, null, apply_filters('pmxe_post_slug', pmxe_filter($record->post_name, $fieldSnipped), get_the_ID())); break; case 'excerpt': $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_post_excerpt', pmxe_filter($record->post_excerpt, $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); break; case 'cf': if (!empty($exportOptions['cc_value'][$ID])) { $cur_meta_values = get_post_meta($record->ID, $exportOptions['cc_value'][$ID]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID())); $xmlWriter->endElement(); } } if (empty($cur_meta_values)) { $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], get_the_ID())); $xmlWriter->endElement(); } } break; case 'acf': if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) { global $acf; $field_value = get_field($exportOptions['cc_label'][$ID], $record->ID); $field_options = unserialize($exportOptions['cc_options'][$ID]); pmxe_export_acf_field_xml($field_value, $exportOptions, $ID, $record->ID, $xmlWriter, $element_name, $element_name_ns, $fieldSnipped, $field_options['group_id']); } break; case 'woo': XmlExportWooCommerce::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID); break; case 'woo_order': XmlExportWooCommerceOrder::getInstance()->export_xml($xmlWriter, $record, $exportOptions, $ID); break; case 'attr': if (!empty($exportOptions['cc_value'][$ID])) { if ($record->post_parent == 0) { $is_variable_product = false; $product_terms = wp_get_post_terms($record->ID, 'product_type'); if (!empty($product_terms)) { if (!is_wp_error($product_terms)) { foreach ($product_terms as $term) { if ('variable' == $term->slug) { $is_variable_product = true; break; } } } } $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]); if (!is_wp_error($txes_list)) { $attr_new = array(); if (!empty($txes_list)) { foreach ($txes_list as $t) { $attr_new[] = $t->name; } $xmlWriter->beginElement($element_name_ns, $is_variable_product ? $element_name : 'attribute_' . $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_woo_attribute', pmxe_filter(implode('|', $attr_new), $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); } } } else { $attribute_pa = get_post_meta($record->ID, 'attribute_' . $exportOptions['cc_value'][$ID], true); if (!empty($attribute_pa)) { $xmlWriter->beginElement($element_name_ns, 'attribute_' . $element_name, null); $xmlWriter->writeCData(apply_filters('woo_field', $attribute_pa)); $xmlWriter->endElement(); } } } break; case 'cats': if (!empty($exportOptions['cc_value'][$ID])) { if ($exportOptions['cc_label'][$ID] == 'product_type' and get_post_type() == 'product_variation') { $xmlWriter->writeElement('parent_id', $record->post_parent); $xmlWriter->writeElement($element_name, 'variable'); } else { $txes_list = get_the_terms($record->ID, $exportOptions['cc_value'][$ID]); if (!is_wp_error($txes_list)) { $txes_ids = array(); $hierarchy_groups = array(); if (!empty($txes_list)) { foreach ($txes_list as $t) { $txes_ids[] = $t->term_id; } foreach ($txes_list as $t) { if (wp_all_export_check_children_assign($t->term_id, $exportOptions['cc_value'][$ID], $txes_ids)) { $ancestors = get_ancestors($t->term_id, $exportOptions['cc_value'][$ID]); if (count($ancestors) > 0) { $hierarchy_group = array(); for ($i = count($ancestors) - 1; $i >= 0; $i--) { $term = get_term_by('id', $ancestors[$i], $exportOptions['cc_value'][$ID]); if ($term) { $hierarchy_group[] = $term->name; } } $hierarchy_group[] = $t->name; $hierarchy_groups[] = implode(">", $hierarchy_group); } else { $hierarchy_groups[] = $t->name; } } } if (!empty($hierarchy_groups)) { $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_post_taxonomy', pmxe_filter(implode('|', $hierarchy_groups), $fieldSnipped), get_the_ID())); $xmlWriter->endElement(); } } } } } break; case 'sql': if (!empty($exportOptions['cc_sql'][$ID])) { $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID())); if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) { // if shortcode defined if (strpos($exportOptions['cc_code'][$ID], '[') === 0) { $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])); } else { $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';'); } } $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeCData(apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID())); $xmlWriter->endElement(); } break; default: # code... break; } } } } $xmlWriter->endElement(); // end post if ($preview) { break; } do_action('pmxe_exported_post', $record->ID); } $xmlWriter->endElement(); // end data if ($preview) { return $xmlWriter->flush(true); } //wp_all_export_remove_colons($xmlWriter->flush(true)); if ($is_cron) { $xml = $xmlWriter->flush(true); if (!$exported_by_cron) { // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters. if ($exportOptions['include_bom']) { file_put_contents($file_path, chr(0xef) . chr(0xbb) . chr(0xbf) . substr($xml, 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1)); } else { file_put_contents($file_path, substr($xml, 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1)); } } else { file_put_contents($file_path, substr(substr($xml, 41 + strlen($exportOptions['main_xml_tag'])), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1), FILE_APPEND); } return $file_path; } else { if (empty(PMXE_Plugin::$session->file)) { // generate export file name $export_file = wp_all_export_generate_export_file(XmlExportEngine::$exportID); // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters. if ($exportOptions['include_bom']) { file_put_contents($export_file, chr(0xef) . chr(0xbb) . chr(0xbf) . substr($xmlWriter->flush(true), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1)); } else { file_put_contents($export_file, substr($xmlWriter->flush(true), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1)); } PMXE_Plugin::$session->set('file', $export_file); PMXE_Plugin::$session->save_data(); } else { file_put_contents(PMXE_Plugin::$session->file, substr(substr($xmlWriter->flush(true), 41 + strlen($exportOptions['main_xml_tag'])), 0, (strlen($exportOptions['main_xml_tag']) + 4) * -1), FILE_APPEND); } return true; } }
function pmwi_link_all_variations($product_id, $options = array(), $import_id, $iteration = 0) { global $woocommerce; @set_time_limit(0); $post_id = intval($product_id); if (!$post_id) { return 0; } $variations = array(); $_product = get_product($post_id, array('product_type' => 'variable')); $v = $_product->get_attributes(); // Put variation attributes into an array foreach ($_product->get_attributes() as $attribute) { if (!$attribute['is_variation']) { continue; } $attribute_field_name = 'attribute_' . sanitize_title($attribute['name']); if ($attribute['is_taxonomy']) { $options = wc_get_product_terms($post_id, $attribute['name'], array('fields' => 'slugs')); } else { $options = explode('|', $attribute['value']); } $options = array_map('trim', $options); $variations[$attribute_field_name] = $options; } // Quit out if none were found if (sizeof($variations) == 0) { return 0; } // Get existing variations so we don't create duplicates $available_variations = array(); foreach ($_product->get_children() as $child_id) { $child = $_product->get_child($child_id); if (!empty($child->variation_id)) { $postRecord = new PMXI_Post_Record(); $postRecord->getBy(array('post_id' => $child->variation_id, 'import_id' => $import_id, 'unique_key' => 'Variation ' . $child->variation_id . ' of ' . $post_id)); if (!$postRecord->isEmpty()) { $postRecord->set(array('iteration' => $iteration))->update(); } $available_variations[] = $child->get_variation_attributes(); update_post_meta($child->variation_id, '_regular_price', get_post_meta($post_id, '_regular_price', true)); update_post_meta($child->variation_id, '_sale_price', get_post_meta($post_id, '_sale_price', true)); if (class_exists('woocommerce_wholesale_pricing')) { update_post_meta($child->variation_id, 'pmxi_wholesale_price', get_post_meta($post_id, 'pmxi_wholesale_price', true)); } update_post_meta($child->variation_id, '_sale_price_dates_from', get_post_meta($post_id, '_sale_price_dates_from', true)); update_post_meta($child->variation_id, '_sale_price_dates_to', get_post_meta($post_id, '_sale_price_dates_to', true)); update_post_meta($child->variation_id, '_price', get_post_meta($post_id, '_price', true)); update_post_meta($child->variation_id, '_stock', get_post_meta($post_id, '_stock', true)); update_post_meta($child->variation_id, '_stock_status', get_post_meta($post_id, '_stock_status', true)); update_post_meta($child->variation_id, '_manage_stock', get_post_meta($post_id, '_manage_stock', true)); update_post_meta($child->variation_id, '_backorders', get_post_meta($post_id, '_backorders', true)); do_action('pmxi_product_variation_saved', $child->variation_id); } } // Created posts will all have the following data $variation_post_data = array('post_title' => 'Product #' . $post_id . ' Variation', 'post_content' => '', 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation'); $variation_ids = array(); $added = 0; $possible_variations = $this->array_cartesian($variations); foreach ($possible_variations as $variation) { // Check if variation already exists if (in_array($variation, $available_variations)) { continue; } $variation_id = wp_insert_post($variation_post_data); $postRecord = new PMXI_Post_Record(); $postRecord->isEmpty() and $postRecord->set(array('post_id' => $variation_id, 'import_id' => $import_id, 'unique_key' => 'Variation ' . $variation_id . ' of ' . $post_id, 'product_key' => '', 'iteration' => $iteration))->insert(); update_post_meta($variation_id, '_regular_price', get_post_meta($post_id, '_regular_price', true)); update_post_meta($variation_id, '_sale_price', get_post_meta($post_id, '_sale_price', true)); if (class_exists('woocommerce_wholesale_pricing')) { update_post_meta($variation_id, 'pmxi_wholesale_price', get_post_meta($post_id, 'pmxi_wholesale_price', true)); } update_post_meta($variation_id, '_sale_price_dates_from', get_post_meta($post_id, '_sale_price_dates_from', true)); update_post_meta($variation_id, '_sale_price_dates_to', get_post_meta($post_id, '_sale_price_dates_to', true)); update_post_meta($variation_id, '_price', get_post_meta($post_id, '_price', true)); update_post_meta($variation_id, '_stock', get_post_meta($post_id, '_stock', true)); update_post_meta($variation_id, '_stock_status', get_post_meta($post_id, '_stock_status', true)); update_post_meta($variation_id, '_manage_stock', get_post_meta($post_id, '_manage_stock', true)); update_post_meta($variation_id, '_backorders', get_post_meta($post_id, '_backorders', true)); $variation_ids[] = $variation_id; foreach ($variation as $key => $value) { update_post_meta($variation_id, $key, $value); } $added++; do_action('pmxi_product_variation_saved', $variation_id); } $children = get_posts(array('post_parent' => $post_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'ASC', 'post_status' => array('draft', 'publish', 'trash', 'pending', 'future', 'private'))); $default_attributes = array(); foreach ($v as $attribute) { $default_attributes[sanitize_title($attribute['name'])] = array(); $values = array(); foreach ($children as $child) { $value = array_map('stripslashes', array_map('strip_tags', explode("|", trim(get_post_meta($child, 'attribute_' . sanitize_title($attribute['name']), true))))); if (!empty($value)) { foreach ($value as $val) { if (!in_array($val, $values, true)) { $values[] = $val; } } } if ($attribute['is_variation']) { if (!empty($values) and empty($default_attributes[$attribute['name']])) { switch ($this->import->options['default_attributes_type']) { case 'instock': $is_instock = get_post_meta($child, '_stock_status', true); if ($is_instock == 'instock') { $default_attributes[sanitize_title($attribute['name'])] = sanitize_title(is_array($value) ? $value[0] : $value); } break; case 'first': $default_attributes[sanitize_title($attribute['name'])] = sanitize_title(is_array($values) ? $values[0] : $values); break; default: # code... break; } } } } } if ($this->import->options['is_default_attributes']) { $this->pushmeta($post_id, '_default_attributes', $default_attributes); } wc_delete_product_transients($post_id); return $added; }
public function import($importData = array()) { extract($importData); if (!in_array($importData['post_type'], array('product', 'product_variation'))) { return; } $cxpath = $xpath_prefix . $import->xpath; global $woocommerce; extract($this->data); $is_new_product = empty($articleData['ID']); // Get types $product_type = empty($product_types[$i]) ? 'simple' : sanitize_title(stripslashes($product_types[$i])); if ($this->options['update_all_data'] == 'no' and !$this->options['is_update_product_type'] and !$is_new_product) { $product = get_product($pid); $product_type = $product->product_type; } $this->existing_meta_keys = array(); foreach (get_post_meta($pid, '') as $cur_meta_key => $cur_meta_val) { $this->existing_meta_keys[] = $cur_meta_key; } $this->post_meta_to_update = array(); // for bulk UPDATE SQL query $this->post_meta_to_insert = array(); // for bulk INSERT SQL query $this->articleData = $articleData; $total_sales = get_post_meta($pid, 'total_sales', true); if (empty($total_sales)) { update_post_meta($pid, 'total_sales', '0'); } $is_downloadable = $product_downloadable[$i]; $is_virtual = $product_virtual[$i]; $is_featured = $product_featured[$i]; // Product type + Downloadable/Virtual if ($is_new_product or $this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_product_type']) { //wp_set_object_terms( $pid, $product_type, 'product_type' ); $product_type_term = term_exists($product_type, 'product_type', 0); if (!empty($product_type_term) and !is_wp_error($product_type_term)) { $this->associate_terms($pid, array((int) $product_type_term['term_taxonomy_id']), 'product_type'); } } $this->pushmeta($pid, '_downloadable', $is_downloadable == "yes" ? 'yes' : 'no'); $this->pushmeta($pid, '_virtual', $is_virtual == "yes" ? 'yes' : 'no'); // Update post meta $this->pushmeta($pid, '_regular_price', $product_regular_price[$i] == "" ? '' : stripslashes($product_regular_price[$i])); $this->pushmeta($pid, '_sale_price', $product_sale_price[$i] == "" ? '' : stripslashes($product_sale_price[$i])); $this->pushmeta($pid, '_tax_status', stripslashes($product_tax_status[$i])); $this->pushmeta($pid, '_tax_class', stripslashes($product_tax_class[$i])); $this->pushmeta($pid, '_visibility', stripslashes($product_visibility[$i])); $this->pushmeta($pid, '_purchase_note', stripslashes($product_purchase_note[$i])); $this->pushmeta($pid, '_featured', $is_featured == "yes" ? 'yes' : 'no'); // Dimensions if ($is_virtual == 'no') { $this->pushmeta($pid, '_weight', stripslashes($product_weight[$i])); $this->pushmeta($pid, '_length', stripslashes($product_length[$i])); $this->pushmeta($pid, '_width', stripslashes($product_width[$i])); $this->pushmeta($pid, '_height', stripslashes($product_height[$i])); } else { $this->pushmeta($pid, '_weight', ''); $this->pushmeta($pid, '_length', ''); $this->pushmeta($pid, '_width', ''); $this->pushmeta($pid, '_height', ''); } if ($this->options['is_update_comment_status']) { $this->wpdb->update($this->wpdb->posts, array('comment_status' => $product_enable_reviews[$i] == 'yes' ? 'open' : 'closed'), array('ID' => $pid)); } if ($this->options['update_all_data'] == 'yes' or $this->options['is_update_menu_order']) { $this->wpdb->update($this->wpdb->posts, array('menu_order' => $product_menu_order[$i] != '' ? (int) $product_menu_order[$i] : 0), array('ID' => $pid)); } // Save shipping class if (pmwi_is_update_taxonomy($articleData, $this->options, 'product_shipping_class')) { $p_shipping_class = $product_shipping_class[$i] > 0 && $product_type != 'external' ? absint($product_shipping_class[$i]) : ''; if ($p_shipping_class != '') { if (ctype_digit($product_shipping_class[$i])) { $t_shipping_class = get_term_by('slug', $p_shipping_class, 'product_shipping_class'); if (!empty($t_shipping_class) and !is_wp_error($t_shipping_class)) { $p_shipping_class = (int) $t_shipping_class->term_taxonomy_id; } else { $t_shipping_class = term_exists((int) $p_shipping_class, 'product_shipping_class', 0); if (!empty($t_shipping_class) and !is_wp_error($t_shipping_class)) { $p_shipping_class = (int) $t_shipping_class['term_taxonomy_id']; } else { $t_shipping_class = wp_insert_term($p_shipping_class, 'product_shipping_class'); if (!empty($t_shipping_class) and !is_wp_error($t_shipping_class)) { $p_shipping_class = (int) $t_shipping_class['term_taxonomy_id']; } } } } else { $t_shipping_class = term_exists($product_shipping_class[$i], 'product_shipping_class', 0); if (!empty($t_shipping_class) and !is_wp_error($t_shipping_class)) { $p_shipping_class = (int) $t_shipping_class['term_taxonomy_id']; } else { $t_shipping_class = term_exists(htmlspecialchars(strtolower($product_shipping_class[$i])), 'product_shipping_class', 0); if (!empty($t_shipping_class) and !is_wp_error($t_shipping_class)) { $p_shipping_class = (int) $t_shipping_class['term_taxonomy_id']; } else { $t_shipping_class = wp_insert_term($product_shipping_class[$i], 'product_shipping_class'); if (!empty($t_shipping_class) and !is_wp_error($t_shipping_class)) { $p_shipping_class = (int) $t_shipping_class['term_taxonomy_id']; } } } } } if ($p_shipping_class !== false and !is_wp_error($p_shipping_class)) { $this->associate_terms($pid, array($p_shipping_class), 'product_shipping_class'); } } // Unique SKU $sku = $is_new_product ? '' : get_post_meta($pid, '_sku', true); $new_sku = wc_clean(trim(stripslashes($product_sku[$i]))); if ($new_sku == '' and $this->options['disable_auto_sku_generation']) { $this->pushmeta($pid, '_sku', ''); } elseif ($new_sku == '' and !$this->options['disable_auto_sku_generation']) { if ($is_new_product or $this->is_update_cf('_sku')) { $unique_keys = XmlImportParser::factory($xml, $cxpath, $this->options['unique_key'], $file)->parse(); $tmp_files[] = $file; foreach ($tmp_files as $file) { // remove all temporary files created @unlink($file); } $new_sku = substr(md5($unique_keys[$i]), 0, 12); } } if ($new_sku != '' and $new_sku !== $sku) { if (!empty($new_sku)) { if (!$this->options['disable_sku_matching'] and $this->wpdb->get_var($this->wpdb->prepare("\n\t\t\t\t\t\tSELECT " . $this->wpdb->posts . ".ID\n\t\t\t\t\t FROM " . $this->wpdb->posts . "\n\t\t\t\t\t LEFT JOIN " . $this->wpdb->postmeta . " ON (" . $this->wpdb->posts . ".ID = " . $this->wpdb->postmeta . ".post_id)\n\t\t\t\t\t WHERE " . $this->wpdb->posts . ".post_type = 'product'\n\t\t\t\t\t AND " . $this->wpdb->posts . ".post_status = 'publish'\n\t\t\t\t\t AND " . $this->wpdb->postmeta . ".meta_key = '_sku' AND " . $this->wpdb->postmeta . ".meta_value = '%s'\n\t\t\t\t\t ", $new_sku))) { $logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Product SKU must be unique.', 'pmxi_plugin'))); } else { $this->pushmeta($pid, '_sku', $new_sku); } } else { $this->pushmeta($pid, '_sku', ''); } } $this->pushmeta($pid, '_variation_description', wp_kses_post($product_variation_description[$i])); // Save Attributes $attributes = array(); $is_variation_attributes_defined = false; if ($this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_attributes'] or $is_new_product) { // Update Product Attributes $is_update_attributes = true; if (!empty($serialized_attributes)) { $attribute_position = 0; $attr_names = array(); foreach ($serialized_attributes as $anum => $attr_data) { $attr_name = $attr_data['names'][$i]; // if ( in_array( $attr_name, $this->reserved_terms ) ) { // $attr_name .= 's'; // } if (empty($attr_name) or in_array($attr_name, $attr_names)) { continue; } $attr_names[] = $attr_name; $is_visible = intval($attr_data['is_visible'][$i]); $is_variation = intval($attr_data['in_variation'][$i]); $is_taxonomy = intval($attr_data['in_taxonomy'][$i]); if ($is_variation and $attr_data['value'][$i] != "") { $is_variation_attributes_defined = true; } // Update only these Attributes, leave the rest alone if ($this->options['update_all_data'] == "no" and $this->options['is_update_attributes'] and $this->options['update_attributes_logic'] == 'only') { if (!empty($this->options['attributes_list']) and is_array($this->options['attributes_list'])) { if (!in_array($is_taxonomy ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($this->options['attributes_list'], 'trim'))) { $attribute_position++; continue; } } else { $is_update_attributes = false; break; } } // Leave these attributes alone, update all other Attributes if ($this->options['update_all_data'] == "no" and $this->options['is_update_attributes'] and $this->options['update_attributes_logic'] == 'all_except') { if (!empty($this->options['attributes_list']) and is_array($this->options['attributes_list'])) { if (in_array($is_taxonomy ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($this->options['attributes_list'], 'trim'))) { $attribute_position++; continue; } } } if ($is_taxonomy) { if (isset($attr_data['value'][$i])) { $values = array_map('stripslashes', array_map('strip_tags', explode('|', $attr_data['value'][$i]))); // Remove empty items in the array $values = array_filter($values, array($this, "filtering")); if (intval($attr_data['is_create_taxonomy_terms'][$i])) { $this->create_taxonomy($attr_name, $logger); } if (!empty($values) and taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { $attr_values = array(); foreach ($values as $key => $val) { $value = substr($val, 0, 199); $term = get_term_by('name', $value, wc_attribute_taxonomy_name($attr_name), ARRAY_A); if (empty($term) and !is_wp_error($term)) { $term = term_exists($value, wc_attribute_taxonomy_name($attr_name)); if (empty($term) and !is_wp_error($term)) { $term = term_exists(htmlspecialchars($value), wc_attribute_taxonomy_name($attr_name)); if (empty($term) and !is_wp_error($term) and intval($attr_data['is_create_taxonomy_terms'][$i])) { $term = wp_insert_term($value, wc_attribute_taxonomy_name($attr_name)); } } } if (!is_wp_error($term)) { $attr_values[] = (int) $term['term_taxonomy_id']; } } $values = $attr_values; $values = array_map('intval', $values); $values = array_unique($values); } else { $values = array(); } } // Update post terms if (taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { $this->associate_terms($pid, $values, wc_attribute_taxonomy_name($attr_name)); } if (!empty($values)) { // Add attribute to array, but don't set values $attributes[sanitize_title(wc_attribute_taxonomy_name($attr_name))] = array('name' => wc_attribute_taxonomy_name($attr_name), 'value' => '', 'position' => $attribute_position, 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => 1, 'is_create_taxonomy_terms' => !empty($attr_data['is_create_taxonomy_terms'][$i]) ? 1 : 0); } } else { if (taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { //wp_set_object_terms( $pid, NULL, wc_attribute_taxonomy_name( $attr_name ) ); $this->associate_terms($pid, NULL, wc_attribute_taxonomy_name($attr_name)); } if (!empty($attr_data['value'][$i])) { // Custom attribute - Add attribute to array and set the values $attributes[sanitize_title($attr_name)] = array('name' => sanitize_text_field($attr_name), 'value' => $attr_data['value'][$i], 'position' => $attribute_position, 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => 0); } } $attribute_position++; } } if ($is_new_product or $is_update_attributes) { $current_product_attributes = get_post_meta($pid, '_product_attributes', true); update_post_meta($pid, '_product_attributes', !empty($current_product_attributes) ? array_merge($current_product_attributes, $attributes) : $attributes); } } else { $is_variation_attributes_defined = true; } // is update attributes // Sales and prices if (!in_array($product_type, array('grouped'))) { $date_from = isset($product_sale_price_dates_from[$i]) ? $product_sale_price_dates_from[$i] : ''; $date_to = isset($product_sale_price_dates_to[$i]) ? $product_sale_price_dates_to[$i] : ''; // Dates if ($date_from) { $this->pushmeta($pid, '_sale_price_dates_from', strtotime($date_from)); } else { $this->pushmeta($pid, '_sale_price_dates_from', ''); } if ($date_to) { $this->pushmeta($pid, '_sale_price_dates_to', strtotime($date_to)); } else { $this->pushmeta($pid, '_sale_price_dates_to', ''); } if ($date_to && !$date_from) { $this->pushmeta($pid, '_sale_price_dates_from', strtotime('NOW', current_time('timestamp'))); } // Update price if on sale if ($product_sale_price[$i] == '') { if (!empty($this->articleData['ID']) and !$this->is_update_cf('_sale_price')) { $product_sale_price[$i] = get_post_meta($pid, '_sale_price', true); } } if ($product_sale_price[$i] != '' && $date_to == '' && $date_from == '') { $this->pushmeta($pid, '_price', empty($product_sale_price[$i]) ? '' : stripslashes($product_sale_price[$i])); } else { $this->pushmeta($pid, '_price', $product_regular_price[$i] == "" ? '' : stripslashes($product_regular_price[$i])); } if ($product_sale_price[$i] != '' && $date_from && strtotime($date_from) < strtotime('NOW', current_time('timestamp'))) { $this->pushmeta($pid, '_price', empty($product_sale_price[$i]) ? '' : stripslashes($product_sale_price[$i])); } if ($date_to && strtotime($date_to) < strtotime('NOW', current_time('timestamp'))) { $this->pushmeta($pid, '_price', $product_regular_price[$i] == "" ? '' : stripslashes($product_regular_price[$i])); $this->pushmeta($pid, '_sale_price_dates_from', ''); $this->pushmeta($pid, '_sale_price_dates_to', ''); } } if (in_array($product_type, array('simple', 'external'))) { if ($this->options['is_multiple_grouping_product'] != 'yes') { if ($this->options['grouping_indicator'] == 'xpath' and !is_numeric($product_grouping_parent[$i])) { $dpost = pmxi_findDuplicates(array('post_type' => 'product', 'ID' => $pid, 'post_parent' => $articleData['post_parent'], 'post_title' => $product_grouping_parent[$i])); if (!empty($dpost)) { $product_grouping_parent[$i] = $dpost[0]; } else { $product_grouping_parent[$i] = 0; } } elseif ($this->options['grouping_indicator'] != 'xpath') { $dpost = pmxi_findDuplicates($articleData, $custom_grouping_indicator_name[$i], $custom_grouping_indicator_value[$i], 'custom field'); if (!empty($dpost)) { $product_grouping_parent[$i] = array_shift($dpost); } else { $product_grouping_parent[$i] = 0; } } } if ("" != $product_grouping_parent[$i] and absint($product_grouping_parent[$i]) > 0) { $this->wpdb->update($this->wpdb->posts, array('post_parent' => absint($product_grouping_parent[$i])), array('ID' => $pid)); } } // Update parent if grouped so price sorting works and stays in sync with the cheapest child if ($product_type == 'grouped' || ("" != $product_grouping_parent[$i] and absint($product_grouping_parent[$i]) > 0)) { $clear_parent_ids = array(); if ($product_type == 'grouped') { $clear_parent_ids[] = $pid; } if ("" != $product_grouping_parent[$i] and absint($product_grouping_parent[$i]) > 0) { $clear_parent_ids[] = absint($product_grouping_parent[$i]); } if ($clear_parent_ids) { foreach ($clear_parent_ids as $clear_id) { $children_by_price = get_posts(array('post_parent' => $clear_id, 'orderby' => 'meta_value_num', 'order' => 'asc', 'meta_key' => '_price', 'posts_per_page' => 1, 'post_type' => 'product', 'fields' => 'ids')); if ($children_by_price) { foreach ($children_by_price as $child) { $child_price = get_post_meta($child, '_price', true); update_post_meta($clear_id, '_price', $child_price); } } // Clear cache/transients //wc_delete_product_transients( $clear_id ); } } } // Sold Individuall if ("yes" == $product_sold_individually[$i]) { $this->pushmeta($pid, '_sold_individually', 'yes'); } else { $this->pushmeta($pid, '_sold_individually', ''); } // Stock Data if ('yes' === get_option('woocommerce_manage_stock')) { $manage_stock = 'no'; $backorders = 'no'; $stock_status = wc_clean($product_stock_status[$i]); if ('external' === $product_type) { $stock_status = 'instock'; } elseif ('variable' === $product_type and !$this->options['link_all_variations']) { // Stock status is always determined by children so sync later $stock_status = ''; if ($product_manage_stock[$i] == 'yes') { $manage_stock = 'yes'; $backorders = wc_clean($product_allow_backorders[$i]); } } elseif ('grouped' !== $product_type && $product_manage_stock[$i] == 'yes') { $manage_stock = 'yes'; $backorders = wc_clean($product_allow_backorders[$i]); } $this->pushmeta($pid, '_manage_stock', $manage_stock); $this->pushmeta($pid, '_backorders', $backorders); if ($stock_status and $this->is_update_cf('_stock_status')) { update_post_meta($pid, '_stock_status', $stock_status); } if ($product_manage_stock[$i] == 'yes') { $this->is_update_cf('_stock') and update_post_meta($pid, '_stock', wc_stock_amount($product_stock_qty[$i])); } else { $this->pushmeta($pid, '_stock', ''); } } else { update_post_meta($pid, '_stock_status', wc_clean($product_stock_status[$i])); } // Upsells if (!empty($product_up_sells[$i])) { $upsells = array(); $ids = array_filter(explode(',', $product_up_sells[$i]), 'trim'); foreach ($ids as $id) { $args = array('post_type' => 'product', 'meta_query' => array(array('key' => '_sku', 'value' => $id))); $query = new WP_Query($args); if ($query->have_posts()) { $upsells[] = $query->post->ID; } wp_reset_postdata(); } $this->pushmeta($pid, '_upsell_ids', $upsells); } else { if ($is_new_product or $this->is_update_cf('_upsell_ids')) { delete_post_meta($pid, '_upsell_ids'); } } // Cross sells if (!empty($product_cross_sells[$i])) { $crosssells = array(); $ids = array_filter(explode(',', $product_cross_sells[$i]), 'trim'); foreach ($ids as $id) { $args = array('post_type' => 'product', 'meta_query' => array(array('key' => '_sku', 'value' => $id))); $query = new WP_Query($args); if ($query->have_posts()) { $crosssells[] = $query->post->ID; } wp_reset_postdata(); } $this->pushmeta($pid, '_crosssell_ids', $crosssells); } else { if ($is_new_product or $this->is_update_cf('_crosssell_ids')) { delete_post_meta($pid, '_crosssell_ids'); } } // Downloadable options if ($is_downloadable == 'yes') { $_download_limit = absint($product_download_limit[$i]); if (!$_download_limit) { $_download_limit = ''; } // 0 or blank = unlimited $_download_expiry = absint($product_download_expiry[$i]); if (!$_download_expiry) { $_download_expiry = ''; } // 0 or blank = unlimited // file paths will be stored in an array keyed off md5(file path) if (!empty($product_files[$i])) { $_file_paths = array(); $file_paths = explode($this->options['product_files_delim'], $product_files[$i]); $file_names = explode($this->options['product_files_names_delim'], $product_files_names[$i]); foreach ($file_paths as $fn => $file_path) { $file_path = trim($file_path); $_file_paths[md5($file_path)] = array('name' => !empty($file_names[$fn]) ? $file_names[$fn] : basename($file_path), 'file' => $file_path); } $this->pushmeta($pid, '_downloadable_files', $_file_paths); } if (isset($product_download_limit[$i])) { $this->pushmeta($pid, '_download_limit', esc_attr($_download_limit)); } if (isset($product_download_expiry[$i])) { $this->pushmeta($pid, '_download_expiry', esc_attr($_download_expiry)); } if (isset($product_download_type[$i])) { $this->pushmeta($pid, '_download_type', esc_attr($product_download_type)); } } // Product url if ($product_type == 'external') { if (isset($product_url[$i]) && $product_url[$i]) { $this->auto_cloak_links($import, $product_url[$i]); $this->pushmeta($pid, '_product_url', esc_attr($product_url[$i])); } if (isset($product_button_text[$i]) && $product_button_text[$i]) { $this->pushmeta($pid, '_button_text', esc_attr($product_button_text[$i])); } } // prepare bulk SQL query //$this->executeSQL(); wc_delete_product_transients($pid); // VARIATIONS if (in_array($product_type, array('variation', 'variable')) and !$this->options['link_all_variations'] and "xml" != $import->options['matching_parent']) { $set_defaults = false; $product_parent_post_id = false; //[search parent product] $first_is_parent = "yes"; if ("manual" != $this->options['duplicate_matching']) { // find corresponding article among previously imported if (!empty($single_product_parent_ID[$i])) { $postRecord = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->prefix . "pmxi_posts WHERE `import_id` = %d AND `product_key` = %s ORDER BY post_id ASC", $import->id, $single_product_parent_ID[$i])); $product_parent_post = !empty($postRecord) ? get_post($product_parent_post_id = $postRecord->post_id) : false; } else { $product_parent_post = false; } } else { if ($articleData['post_type'] == 'product') { $product_parent_post_id = $pid; $args = array('post_type' => 'product_variation', 'meta_query' => array(array('key' => '_sku', 'value' => get_post_meta($pid, '_sku', true)))); $query = new WP_Query($args); if ($query->have_posts()) { $duplicate_id = $query->post->ID; if ($duplicate_id) { $pid = $duplicate_id; $this->duplicate_post_meta($pid, $product_parent_post_id); $tmp = get_post_meta($product_parent_post_id, '_stock', true); $this->pushmeta($product_parent_post_id, '_stock_tmp', $tmp); if (empty($import->options['set_parent_stock'])) { $this->pushmeta($product_parent_post_id, '_stock', ''); } $tmp = get_post_meta($product_parent_post_id, '_regular_price', true); $this->pushmeta($product_parent_post_id, '_regular_price_tmp', $tmp); $this->pushmeta($product_parent_post_id, '_regular_price', ''); $tmp = get_post_meta($product_parent_post_id, '_price', true); $this->pushmeta($product_parent_post_id, '_price_tmp', $tmp); $this->pushmeta($product_parent_post_id, '_price', ''); } } wp_reset_postdata(); } elseif ($articleData['post_type'] == 'product_variation') { $variation_post = get_post($pid); $product_parent_post_id = $variation_post->post_parent; $product_parent_post = get_post($product_parent_post_id); } } $first_is_parent = in_array($import->options['matching_parent'], array("auto", "first_is_parent_title")) ? "yes" : "no"; //[\search parent product] if (!empty($product_parent_post_id) and ($product_parent_post_id != $pid or $product_parent_post_id == $pid and $first_is_parent == "no")) { $create_new_variation = ($product_parent_post_id == $pid and $first_is_parent == "no") ? true : false; if ($create_new_variation) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); // find corresponding article among previously imported $postRecord->getBy(array('unique_key' => 'Variation ' . $new_sku, 'import_id' => $import->id)); $pid = !$postRecord->isEmpty() ? $postRecord->post_id : false; } $variable_enabled = $product_enabled[$i] == "yes" ? 'yes' : 'no'; $attributes = array(); // Enabled or disabled $post_status = $variable_enabled == 'yes' ? 'publish' : 'private'; // Generate a useful post title if ("manual" != $this->options['duplicate_matching'] and $this->options['is_update_title']) { $variation_post_title = sprintf(__('Variation #%s of %s', 'woocommerce'), absint($pid), $articleData['post_title']); } else { $variation_post_title = $articleData['post_title']; } // Update or Add post $variation = array('post_title' => $variation_post_title, 'post_content' => '', 'post_status' => $post_status, 'post_parent' => $product_parent_post_id, 'post_type' => 'product_variation'); if (!$pid) { if ($this->options['create_new_records']) { $pid = wp_insert_post($variation); //$logger and call_user_func($logger, sprintf(__('<b>CREATED</b>: %s variation from parent product %s.', 'pmxi_plugin'), $variation_post_title, $articleData['post_title'])); if ($create_new_variation) { $this->duplicate_post_meta($pid, $product_parent_post_id); //$this->pushmeta($pid, '_sku', 'v' . get_post_meta($pid, '_sku', true)); // associate variation with import $postRecord->isEmpty() and $postRecord->set(array('post_id' => $pid, 'import_id' => $import->id, 'unique_key' => 'Variation ' . $new_sku, 'product_key' => ''))->insert(); $postRecord->set(array('iteration' => $import->iteration))->update(); } } } else { if ($create_new_variation) { $this->duplicate_post_meta($pid, $product_parent_post_id); //$this->pushmeta($pid, '_sku', 'v' . get_post_meta($pid, '_sku', true)); $postRecord->set(array('iteration' => $import->iteration))->update(); } $this->wpdb->update($this->wpdb->posts, $variation, array('ID' => $pid)); //$logger and call_user_func($logger, sprintf(__('<b>UPDATED</b>: %s variation for parent product %s.', 'pmxi_plugin'), $variation_post_title, $articleData['post_title'])); } if ($pid) { if ($this->options['create_draft']) { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'publish'), array('ID' => $pid)); } if ($first_is_parent == "no") { // Stock handling $this->pushmeta($pid, '_manage_stock', $v_product_manage_stock[$i]); if ('yes' === $v_product_manage_stock[$i]) { $this->is_update_cf('_stock') and update_post_meta($pid, '_stock', wc_stock_amount($v_stock[$i])); } else { $this->is_update_cf('_backorders') and delete_post_meta($pid, '_backorders'); $this->is_update_cf('_stock') and delete_post_meta($pid, '_stock'); } if (empty($this->options['set_parent_stock'])) { $this->pmwi_buf_prices($product_parent_post_id); $this->is_update_cf('_stock') and delete_post_meta($product_parent_post_id, '_stock'); } // Only update stock status to user setting if changed by the user, but do so before looking at stock levels at variation level if (!empty($v_stock_status[$i]) and $this->is_update_cf('_stock_status')) { update_post_meta($pid, '_stock_status', $v_stock_status[$i]); } if (pmwi_is_update_taxonomy($articleData, $this->options, 'product_shipping_class')) { //wp_set_object_terms( $pid, $p_shipping_class, 'product_shipping_class'); $this->associate_terms($pid, array($p_shipping_class), 'product_shipping_class'); } } else { $stock_status = wc_clean($product_stock_status[$i]); if ($stock_status and $this->is_update_cf('_stock_status')) { update_post_meta($pid, '_stock_status', $stock_status); } } if (empty($articleData['ID']) or $this->is_update_cf('_tax_class')) { if ($product_tax_class[$i] !== 'parent') { $this->pushmeta($pid, '_tax_class', sanitize_text_field($product_tax_class[$i])); } else { delete_post_meta($pid, '_tax_class'); } } if ($is_downloadable == 'yes') { $this->pushmeta($pid, '_download_limit', sanitize_text_field($product_download_limit[$i])); $this->pushmeta($pid, '_download_expiry', sanitize_text_field($product_download_expiry[$i])); $this->pushmeta($pid, '_download_type', sanitize_text_field($product_download_type[$i])); $_file_paths = array(); if (!empty($product_files[$i])) { $file_paths = explode($import->options['product_files_delim'], $product_files[$i]); $file_names = explode($import->options['product_files_names_delim'], $product_files_names[$i]); foreach ($file_paths as $fn => $file_path) { $file_path = sanitize_text_field($file_path); $_file_paths[md5($file_path)] = array('name' => !empty($file_names[$fn]) ? $file_names[$fn] : basename($file_path), 'file' => $file_path); } } $this->pushmeta($pid, '_downloadable_files', $_file_paths); } else { $this->pushmeta($pid, '_download_limit', ''); $this->pushmeta($pid, '_download_expiry', ''); $this->pushmeta($pid, '_download_type', ''); $this->pushmeta($pid, '_downloadable_files', ''); } if ($this->options['update_all_data'] == 'yes' or $this->options['update_all_data'] == 'no' and $this->options['is_update_product_type']) { //wp_set_object_terms( $pid, NULL, 'product_type' ); $this->associate_terms($pid, NULL, 'product_type'); } // Remove old taxonomies attributes so data is kept up to date if ($pid and ($import->options['update_all_data'] == "yes" or $import->options['update_all_data'] == "no" and $import->options['is_update_attributes'])) { // Update all Attributes if ($import->options['update_all_data'] == "yes" or $import->options['update_attributes_logic'] == 'full_update') { $this->wpdb->query($this->wpdb->prepare("DELETE FROM {$this->wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND post_id = %d;", $pid)); } wp_cache_delete($pid, 'post_meta'); } } // Update taxonomies if ($import->options['update_all_data'] == "yes" or $import->options['update_all_data'] == "no" and $import->options['is_update_attributes'] or $is_new_product) { $attr_names = array(); foreach ($serialized_attributes as $anum => $attr_data) { $attr_name = $attr_data['names'][$i]; if (empty($attr_name) or in_array($attr_name, $attr_names)) { continue; } $attr_names[] = $attr_name; // Update only these Attributes, leave the rest alone if ($import->options['update_all_data'] == "no" and $import->options['is_update_attributes'] and $import->options['update_attributes_logic'] == 'only') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (!in_array(intval($attr_data['in_taxonomy'][$i]) ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($import->options['attributes_list'], 'trim'))) { continue; } } else { break; } } // Leave these attributes alone, update all other Attributes if ($import->options['update_all_data'] == "no" and $import->options['is_update_attributes'] and $import->options['update_attributes_logic'] == 'all_except') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (in_array($is_taxonomy ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($import->options['attributes_list'], 'trim'))) { continue; } } } if (intval($attr_data['in_taxonomy'][$i]) and (strpos($attr_name, "pa_") === false or strpos($attr_name, "pa_") !== 0)) { $attr_name = "pa_" . $attr_name; } $is_variation = intval($attr_data['in_variation'][$i]); if ($is_variation) { // Don't use woocommerce_clean as it destroys sanitized characters $values = substr(intval($attr_data['in_taxonomy'][$i]) ? $attr_data['value'][$i] : $attr_data['value'][$i], 0, 199); if (intval($attr_data['in_taxonomy'][$i])) { $cname = wc_attribute_taxonomy_name(preg_replace("%^pa_%", "", $attr_name)); $term = get_term_by('name', $values, $cname, ARRAY_A); if (empty($term) and !is_wp_error($term)) { $term = term_exists($values, $cname); if (empty($term) and !is_wp_error($term)) { $term = term_exists(htmlspecialchars($values), $cname); } } if (!empty($term) and !is_wp_error($term)) { $term = get_term_by('id', $term['term_id'], $cname); if (!empty($term) and !is_wp_error($term)) { update_post_meta($pid, 'attribute_' . sanitize_title($attr_name), $term->slug); } } else { //$this->pushmeta($pid, 'attribute_' . sanitize_title( $attr_name ), ''); update_post_meta($pid, 'attribute_' . sanitize_title($attr_name), ''); } } else { update_post_meta($pid, 'attribute_' . sanitize_title($attr_name), $values); } } else { delete_post_meta($pid, 'attribute_' . sanitize_title($attr_name)); } } } $this->pmwi_buf_prices($product_parent_post_id); if ($product_parent_post_id) { wc_delete_product_transients($product_parent_post_id); } if ($create_new_variation) { do_action('pmxi_saved_post', $pid, null); } } $previousID = get_option('wp_all_import_' . $import->id . '_parent_product'); // [execute only for parent products] if (!empty($previousID) and (empty($product_parent_post_id) or $product_parent_post_id != $previousID or !isset($product_types[$i + 1]))) { $parent_product_ids = array($previousID); if (!isset($product_types[$i + 1]) and !empty($product_parent_post_id) and !in_array($product_parent_post_id, $parent_product_ids)) { $parent_product_ids[] = $product_parent_post_id; } if (empty($product_parent_post_id) and !in_array($pid, $parent_product_ids)) { $parent_product_ids[] = $pid; } foreach ($parent_product_ids as $post_parent) { $children = get_posts(array('post_parent' => $post_parent, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'ASC', 'post_status' => array('draft', 'publish', 'trash', 'pending', 'future', 'private'))); if (count($children)) { //wp_set_object_terms( $post_parent, 'variable', 'product_type' ); $product_type_term = term_exists('variable', 'product_type', 0); if (!empty($product_type_term) and !is_wp_error($product_type_term)) { $this->associate_terms($post_parent, array((int) $product_type_term['term_taxonomy_id']), 'product_type'); } $lowest_price = $lowest_regular_price = $lowest_sale_price = $highest_price = $highest_regular_price = $highest_sale_price = ''; $lowest_price_id = $lowest_regular_price_id = $lowest_sale_price_id = $highest_price_id = $highest_regular_price_id = $highest_sale_price_id = ''; $total_instock = 0; if ($children) { foreach ($children as $n => $child) { if ($first_is_parent == "no" and !$n) { $parent_thumbnail_id = get_post_thumbnail_id($post_parent); if ($parent_thumbnail_id) { update_post_meta($child, '_thumbnail_id', $parent_thumbnail_id); } } $_variation_stock = get_post_meta($child, '_stock_status', true); $total_instock += $_variation_stock == 'instock' ? 1 : 0; $child_price = get_post_meta($child, '_price', true); $child_regular_price = get_post_meta($child, '_regular_price', true); $child_sale_price = get_post_meta($child, '_sale_price', true); // Regular prices if (empty($lowest_regular_price) || (double) $child_regular_price < (double) $lowest_regular_price) { $lowest_regular_price = $child_regular_price; $lowest_regular_price_id = $child; } if (empty($highest_regular_price) || (double) $child_regular_price > (double) $highest_regular_price) { $highest_regular_price = $child_regular_price; $highest_regular_price_id = $child; } // Sale prices if ($child_price == $child_sale_price) { if ($child_sale_price !== '' && (!is_numeric($lowest_sale_price) || (double) $child_sale_price < (double) $lowest_sale_price)) { $lowest_sale_price = $child_sale_price; $lowest_sale_price_id = $child; } if ($child_sale_price !== '' && (!is_numeric($highest_sale_price) || (double) $child_sale_price > (double) $highest_sale_price)) { $highest_sale_price = $child_sale_price; $highest_sale_price_id = $child; } } } $lowest_price = $lowest_sale_price === '' || (double) $lowest_regular_price < (double) $lowest_sale_price ? $lowest_regular_price : $lowest_sale_price; $highest_price = $highest_sale_price === '' || (double) $highest_regular_price > (double) $highest_sale_price ? $highest_regular_price : $highest_sale_price; $lowest_price_id = $lowest_sale_price === '' || (double) $lowest_regular_price < (double) $lowest_sale_price ? $lowest_regular_price_id : $lowest_sale_price_id; $highest_price_id = $highest_sale_price === '' || (double) $highest_regular_price > (double) $highest_sale_price ? $highest_regular_price_id : $highest_sale_price_id; } //$parent_manage_stock = get_post_meta($post_parent, '_manage_stock', true); $this->pushmeta($post_parent, '_stock_status', $total_instock > 0 ? 'instock' : 'outofstock'); $this->pushmeta($post_parent, '_price', $lowest_price); update_post_meta($post_parent, '_min_variation_price', $lowest_price); update_post_meta($post_parent, '_max_variation_price', $highest_price); update_post_meta($post_parent, '_min_variation_regular_price', $lowest_regular_price); update_post_meta($post_parent, '_max_variation_regular_price', $highest_regular_price); update_post_meta($post_parent, '_min_variation_sale_price', $lowest_sale_price); update_post_meta($post_parent, '_max_variation_sale_price', $highest_sale_price); update_post_meta($post_parent, '_min_price_variation_id', $lowest_price_id); update_post_meta($post_parent, '_max_price_variation_id', $highest_price_id); update_post_meta($post_parent, '_min_regular_price_variation_id', $lowest_regular_price_id); update_post_meta($post_parent, '_max_regular_price_variation_id', $highest_regular_price_id); update_post_meta($post_parent, '_min_sale_price_variation_id', $lowest_sale_price_id); update_post_meta($post_parent, '_max_sale_price_variation_id', $highest_sale_price_id); // Update default attribute options setting if ($import->options['update_all_data'] == "yes" or $import->options['update_all_data'] == "no" and $import->options['is_update_attributes'] or $is_new_product) { $default_attributes = array(); $parent_attributes = array(); $unique_attributes = array(); $attribute_position = 0; $is_update_attributes = true; foreach ($children as $child) { $child_attributes = (array) maybe_unserialize(get_post_meta($child, '_product_attributes', true)); foreach ($child_attributes as $attr) { if (!in_array($attr['name'], $unique_attributes) and $attr['is_variation']) { $attributes[] = $attr; $unique_attributes[] = $attr['name']; } } } foreach ($attributes as $attribute) { $default_attributes[sanitize_title($attribute['name'])] = array(); $values = array(); foreach ($children as $child) { $value = array_map('stripslashes', array_map('strip_tags', explode("|", trim(get_post_meta($child, 'attribute_' . sanitize_title($attribute['name']), true))))); if (!empty($value)) { //$this->pushmeta($child, 'attribute_' . sanitize_title( $attribute['name'] ), $value[0]); foreach ($value as $val) { if ($attribute['is_taxonomy']) { $term = get_term_by('slug', $val, $attribute['name'], ARRAY_A); if (!empty($term) and !is_wp_error($term)) { $val = $term['name']; } } if (!in_array($val, $values, true)) { $values[] = $val; } } } if ($attribute['is_variation']) { if (!empty($values) and empty($default_attributes[$attribute['name']])) { switch ($import->options['default_attributes_type']) { case 'instock': $is_instock = get_post_meta($child, '_stock_status', true); if ($is_instock == 'instock') { $default_attributes[sanitize_title($attribute['name'])] = sanitize_title(is_array($value) ? $value[0] : $value); } break; case 'first': $default_attributes[sanitize_title($attribute['name'])] = sanitize_title(is_array($values) ? $values[0] : $values); break; default: # code... break; } } } } // Update only these Attributes, leave the rest alone if ($import->options['update_all_data'] == "no" and $import->options['is_update_attributes'] and $import->options['update_attributes_logic'] == 'only') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (!in_array($attribute['name'], array_filter($import->options['attributes_list'], 'trim'))) { $attribute_position++; continue; } } else { $is_update_attributes = false; break; } } // Leave these attributes alone, update all other Attributes if ($import->options['update_all_data'] == "no" and $import->options['is_update_attributes'] and $import->options['update_attributes_logic'] == 'all_except') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (in_array($attribute['name'], array_filter($import->options['attributes_list'], 'trim'))) { $attribute_position++; continue; } } } if ($attribute['is_taxonomy']) { if (!empty($values)) { // Remove empty items in the array $values = array_filter($values, array($this, "filtering")); $attr_values = array(); foreach ($values as $key => $value) { $term = get_term_by('name', $value, $attribute['name'], ARRAY_A); if (empty($term) and !is_wp_error($term)) { $term = term_exists($value, $attribute['name']); if (empty($term) and !is_wp_error($term)) { $term = term_exists(htmlspecialchars($value), $attribute['name']); if (empty($term) and !is_wp_error($term) and $attribute['is_create_taxonomy_terms']) { $term = wp_insert_term($value, $attribute['name']); } } } if (!is_wp_error($term)) { $attr_values[] = (int) $term['term_taxonomy_id']; } } $values = $attr_values; $values = array_map('intval', $values); $values = array_unique($values); } else { $values = array(); } // Update post terms if ($values and taxonomy_exists($attribute['name'])) { $this->associate_terms($post_parent, $values, $attribute['name']); } //do_action('wpai_parent_set_object_terms', $post_parent, $attribute['name']); if ($values) { // Add attribute to array, but don't set values $parent_attributes[sanitize_title($attribute['name'])] = array('name' => $attribute['name'], 'value' => '', 'position' => $attribute_position, 'is_visible' => $attribute['is_visible'], 'is_variation' => $attribute['is_variation'], 'is_taxonomy' => 1, 'is_create_taxonomy_terms' => $attribute['is_create_taxonomy_terms']); } } else { if (!empty($values)) { $parent_attributes[sanitize_title($attribute['name'])] = array('name' => sanitize_text_field($attribute['name']), 'value' => implode('|', $values), 'position' => $attribute_position, 'is_visible' => $attribute['is_visible'], 'is_variation' => $attribute['is_variation'], 'is_taxonomy' => 0); } } $attribute_position++; } if ($import->options['is_default_attributes'] and $is_update_attributes) { $this->pushmeta($post_parent, '_default_attributes', $default_attributes); } if (empty($articleData['ID']) or $is_update_attributes) { $current_product_attributes = get_post_meta($post_parent, '_product_attributes', true); update_post_meta($post_parent, '_product_attributes', !empty($current_product_attributes) ? array_merge($current_product_attributes, $parent_attributes) : $parent_attributes); } if ($this->options['make_simple_product'] and "manual" != $this->options['duplicate_matching']) { $product_attributes = get_post_meta($post_parent, '_product_attributes', true); if (empty($product_attributes)) { $this->make_simple_product($post_parent); } } } if (count($children) == 1 and $this->options['make_simple_product'] and "manual" != $this->options['duplicate_matching']) { $this->make_simple_product($post_parent); // foreach ( $children as $child ) { // wp_delete_post($child); // } } if (!isset($product_types[$i + 1])) { delete_option('wp_all_import_' . $import->id . '_parent_product'); } } elseif ($this->options['make_simple_product'] and "manual" != $this->options['duplicate_matching']) { $this->make_simple_product($post_parent); } wc_delete_product_transients($post_parent); do_action('wp_all_import_variable_product_imported', $post_parent); } } // \[execute only for parent products] update_option('wp_all_import_' . $import->id . '_parent_product', $product_parent_post_id ? $product_parent_post_id : $pid); } elseif (in_array($product_type, array('variable'))) { // Link All Variations if ("variable" == $product_type and $this->options['link_all_variations'] and $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_attributes'] or $is_new_product) { $added_variations = $this->pmwi_link_all_variations($pid, $this->options); $logger and call_user_func($logger, sprintf(__('<b>CREATED</b>: %s variations for parent product %s.', 'pmxi_plugin'), $added_variations, $articleData['post_title'])); } // Variable products have no prices $this->pmwi_buf_prices($pid); } if (in_array($product_type, array('grouped'))) { $this->pushmeta($pid, '_regular_price', ''); $this->pushmeta($pid, '_sale_price', ''); $this->pushmeta($pid, '_sale_price_dates_from', ''); $this->pushmeta($pid, '_sale_price_dates_to', ''); $this->pushmeta($pid, '_price', ''); } //$this->executeSQL(); // Find children elements by XPath and create variations if ("variable" == $product_type and "xml" == $import->options['matching_parent'] and "" != $import->options['variations_xpath'] and "" != $import->options['variable_sku'] and !$import->options['link_all_variations']) { $logger and call_user_func($logger, __('- Importing Variations', 'pmxi_plugin')); $variation_xpath = $cxpath . '[' . ($i + 1) . ']/' . ltrim(trim(str_replace("[*]", "", $import->options['variations_xpath']), '{}'), '/'); $records = array(); $variation_sku = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_sku'], $file)->parse($records); $tmp_files[] = $file; $count_variations = count($variation_sku); if ($count_variations > 0) { // Composing product is Manage stock if ($import->options['is_variable_product_manage_stock'] == 'xpath' and "" != $import->options['single_variable_product_manage_stock']) { if ($import->options['single_variable_product_manage_stock_use_parent']) { $parent_variable_product_manage_stock = XmlImportParser::factory($xml, $cxpath, $import->options['single_variable_product_manage_stock'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_product_manage_stock = array_fill(0, count($variation_sku), $parent_variable_product_manage_stock[$i]); } else { $variation_product_manage_stock = XmlImportParser::factory($xml, $variation_xpath, $import->options['single_variable_product_manage_stock'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_product_manage_stock = array_fill(0, count($variation_sku), $import->options['is_variable_product_manage_stock']); } // Variation Description if ($import->options['variable_description'] != "") { if ($import->options['variable_description_use_parent']) { $parent_variation_description = XmlImportParser::factory($xml, $cxpath, $import->options['variable_description'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_description = array_fill(0, count($variation_sku), $parent_variation_description[$i]); } else { $variation_description = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_description'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_description = array_fill(0, count($variation_sku), ''); } // Stock Qty if ($import->options['variable_stock'] != "") { if ($import->options['variable_stock_use_parent']) { $parent_variation_stock = XmlImportParser::factory($xml, $cxpath, $import->options['variable_stock'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_stock = array_fill(0, count($variation_sku), $parent_variation_stock[$i]); } else { $variation_stock = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_stock'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_stock = array_fill(0, count($variation_sku), ''); } // Stock Status if ($import->options['variable_stock_status'] == 'xpath' and "" != $import->options['single_variable_stock_status']) { $variable_stock_status = XmlImportParser::factory($xml, $variation_xpath, $import->options['single_variable_stock_status'], $file)->parse($records); $tmp_files[] = $file; } elseif ($import->options['variable_stock_status'] == 'auto') { count($variation_sku) and $variable_stock_status = array_fill(0, count($variation_sku), $import->options['variable_stock_status']); foreach ($variation_stock as $key => $value) { $variable_stock_status[$key] = (int) $value <= 0 ? 'outofstock' : 'instock'; } } else { count($variation_sku) and $variable_stock_status = array_fill(0, count($variation_sku), $import->options['variable_stock_status']); } // Image $variation_image = array(); if ($import->options['variable_image']) { if ($import->options['variable_image_use_parent']) { $parent_image = XmlImportParser::factory($xml, $cxpath, $import->options['variable_image'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_image = array_fill(0, count($variation_sku), $parent_image[$i]); } else { $variation_image = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_image'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_image = array_fill(0, count($variation_sku), ''); } // Regular Price if (!empty($import->options['variable_regular_price'])) { if ($import->options['variable_regular_price_use_parent']) { $parent_regular_price = array_map(array($this, 'adjust_price'), array_map(array($this, 'prepare_price'), XmlImportParser::factory($xml, $cxpath, $import->options['variable_regular_price'], $file)->parse($records)), array_fill(0, count($variation_sku), "variable_regular_price")); $tmp_files[] = $file; count($variation_sku) and $variation_regular_price = array_fill(0, count($variation_sku), $parent_regular_price[$i]); } else { $variation_regular_price = array_map(array($this, 'prepare_price'), XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_regular_price'], $file)->parse($records)); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_regular_price = array_fill(0, count($variation_sku), ''); } // Sale Price if (!empty($import->options['variable_sale_price'])) { if ($import->options['variable_sale_price_use_parent']) { $parent_sale_price = array_map(array($this, 'adjust_price'), array_map(array($this, 'prepare_price'), XmlImportParser::factory($xml, $cxpath, $import->options['variable_sale_price'], $file)->parse($records)), array_fill(0, count($variation_sku), "variable_sale_price")); $tmp_files[] = $file; count($variation_sku) and $variation_sale_price = array_fill(0, count($variation_sku), $parent_sale_price[$i]); } else { $variation_sale_price = array_map(array($this, 'prepare_price'), XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_sale_price'], $file)->parse($records)); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_sale_price = array_fill(0, count($variation_sku), ''); } // Who Sale Price if (!empty($import->options['variable_whosale_price'])) { if ($import->options['variable_whosale_price_use_parent']) { $parent_whosale_price = array_map(array($this, 'prepare_price'), XmlImportParser::factory($xml, $cxpath, $import->options['variable_whosale_price'], $file)->parse($records)); $tmp_files[] = $file; count($variation_sku) and $variation_whosale_price = array_fill(0, count($variation_sku), $parent_whosale_price[$i]); } else { $variation_whosale_price = array_map(array($this, 'prepare_price'), XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_whosale_price'], $file)->parse($records)); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_whosale_price = array_fill(0, count($variation_sku), ''); } if ($import->options['is_variable_sale_price_shedule']) { // Sale price dates from if (!empty($import->options['variable_sale_price_dates_from'])) { if ($import->options['variable_sale_dates_use_parent']) { $parent_sale_date_start = XmlImportParser::factory($xml, $cxpath, $import->options['variable_sale_price_dates_from'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_sale_price_dates_from = array_fill(0, count($variation_sku), $parent_sale_date_start[$i]); } else { $variation_sale_price_dates_from = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_sale_price_dates_from'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_sale_price_dates_from = array_fill(0, count($variation_sku), ''); } // Sale price dates to if (!empty($import->options['variable_sale_price_dates_to'])) { if ($import->options['variable_sale_dates_use_parent']) { $parent_sale_date_end = XmlImportParser::factory($xml, $cxpath, $import->options['variable_sale_price_dates_to'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_sale_price_dates_to = array_fill(0, count($variation_sku), $parent_sale_date_end[$i]); } else { $variation_sale_price_dates_to = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_sale_price_dates_to'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_sale_price_dates_to = array_fill(0, count($variation_sku), ''); } } // Composing product is Virtual if ($import->options['is_variable_product_virtual'] == 'xpath' and "" != $import->options['single_variable_product_virtual']) { if ($import->options['single_variable_product_virtual_use_parent']) { $parent_variable_product_virtual = XmlImportParser::factory($xml, $cxpath, $import->options['single_variable_product_virtual'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_product_virtual = array_fill(0, count($variation_sku), $parent_variable_product_virtual[$i]); } else { $variation_product_virtual = XmlImportParser::factory($xml, $variation_xpath, $import->options['single_variable_product_virtual'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_product_virtual = array_fill(0, count($variation_sku), $import->options['is_variable_product_virtual']); } // Composing product is Downloadable if ($import->options['is_variable_product_downloadable'] == 'xpath' and "" != $import->options['single_variable_product_downloadable']) { if ($import->options['single_variable_product_downloadable_use_parent']) { $parent_variable_product_downloadable = XmlImportParser::factory($xml, $cxpath, $import->options['single_variable_product_downloadable'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_product_downloadable = array_fill(0, count($variation_sku), $parent_variable_product_downloadable[$i]); } else { $variation_product_downloadable = XmlImportParser::factory($xml, $variation_xpath, $import->options['single_variable_product_downloadable'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_product_downloadable = array_fill(0, count($variation_sku), $import->options['is_variable_product_downloadable']); } // Weigth if (!empty($import->options['variable_weight'])) { if ($import->options['variable_weight_use_parent']) { $parent_weight = XmlImportParser::factory($xml, $cxpath, $import->options['variable_weight'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_weight = array_fill(0, count($variation_sku), $parent_weight[$i]); } else { $variation_weight = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_weight'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_weight = array_fill(0, count($variation_sku), ''); } // Length if (!empty($import->options['variable_length'])) { if ($import->options['variable_dimensions_use_parent']) { $parent_length = XmlImportParser::factory($xml, $cxpath, $import->options['variable_length'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_length = array_fill(0, count($variation_sku), $parent_length[$i]); } else { $variation_length = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_length'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_length = array_fill(0, count($variation_sku), ''); } // Width if (!empty($import->options['variable_width'])) { if ($import->options['variable_dimensions_use_parent']) { $parent_width = XmlImportParser::factory($xml, $cxpath, $import->options['variable_width'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_width = array_fill(0, count($variation_sku), $parent_width[$i]); } else { $variation_width = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_width'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_width = array_fill(0, count($variation_sku), ''); } // Heigth if (!empty($import->options['variable_height'])) { if ($import->options['variable_dimensions_use_parent']) { $parent_heigth = XmlImportParser::factory($xml, $cxpath, $import->options['variable_height'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_height = array_fill(0, count($variation_sku), $parent_heigth[$i]); } else { $variation_height = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_height'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_height = array_fill(0, count($variation_sku), ''); } // Composing product Shipping Class if ($import->options['is_multiple_variable_product_shipping_class'] != 'yes' and "" != $import->options['single_variable_product_shipping_class']) { if ($import->options['single_variable_product_shipping_class_use_parent']) { $parent_shipping_class = XmlImportParser::factory($xml, $cxpath, $import->options['single_variable_product_shipping_class'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_product_shipping_class = array_fill(0, count($variation_sku), $parent_shipping_class[$i]); } else { $variation_product_shipping_class = XmlImportParser::factory($xml, $variation_xpath, $import->options['single_variable_product_shipping_class'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_product_shipping_class = array_fill(0, count($variation_sku), $import->options['multiple_variable_product_shipping_class']); } // Composing product Tax Class if ($import->options['is_multiple_variable_product_tax_class'] != 'yes' and "" != $import->options['single_variable_product_tax_class']) { if ($import->options['single_variable_product_tax_class_use_parent']) { $parent_tax_class = XmlImportParser::factory($xml, $cxpath, $import->options['single_variable_product_tax_class'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_product_tax_class = array_fill(0, count($variation_sku), $parent_tax_class[$i]); } else { $variation_product_tax_class = XmlImportParser::factory($xml, $variation_xpath, $import->options['single_variable_product_tax_class'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_product_tax_class = array_fill(0, count($variation_sku), $import->options['multiple_variable_product_tax_class']); } // Download limit if (!empty($import->options['variable_download_limit'])) { if ($import->options['variable_download_limit_use_parent']) { $parent_download_limit = XmlImportParser::factory($xml, $cxpath, $import->options['variable_download_limit'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_download_limit = array_fill(0, count($variation_sku), $parent_download_limit[$i]); } else { $variation_download_limit = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_download_limit'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_download_limit = array_fill(0, count($variation_sku), ''); } // Download expiry if (!empty($import->options['variable_download_expiry'])) { if ($import->options['variable_download_expiry_use_parent']) { $parent_download_expiry = XmlImportParser::factory($xml, $cxpath, $import->options['variable_download_expiry'], $file)->parse($records); $tmp_files[] = $file; count($variation_sku) and $variation_download_expiry = array_fill(0, count($variation_sku), $parent_download_expiry[$i]); } else { $variation_download_expiry = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_download_expiry'], $file)->parse($records); $tmp_files[] = $file; } } else { count($variation_sku) and $variation_download_expiry = array_fill(0, count($variation_sku), ''); } // File paths if (!empty($import->options['variable_file_paths'])) { $variation_file_paths = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_file_paths'], $file)->parse($records); $tmp_files[] = $file; } else { count($variation_sku) and $variation_file_paths = array_fill(0, count($variation_sku), ''); } // File names if (!empty($import->options['variable_file_names'])) { $variation_file_names = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_file_names'], $file)->parse($records); $tmp_files[] = $file; } else { count($variation_sku) and $variation_file_names = array_fill(0, count($variation_sku), ''); } // Variation enabled if ($import->options['is_variable_product_enabled'] == 'xpath' and "" != $import->options['single_variable_product_enabled']) { $variation_product_enabled = XmlImportParser::factory($xml, $variation_xpath, $import->options['single_variable_product_enabled'], $file)->parse($records); $tmp_files[] = $file; } else { count($variation_sku) and $variation_product_enabled = array_fill(0, count($variation_sku), $import->options['is_variable_product_enabled']); } $variation_attribute_keys = array(); $variation_attribute_values = array(); $variation_attribute_in_variation = array(); $variation_attribute_is_visible = array(); $variation_attribute_in_taxonomy = array(); $variable_create_terms_in_not_exists = array(); if (!empty($import->options['variable_attribute_name'][0])) { foreach ($import->options['variable_attribute_name'] as $j => $attribute_name) { if ($attribute_name == "") { continue; } $variation_attribute_keys[$j] = XmlImportParser::factory($xml, $variation_xpath, $attribute_name, $file)->parse($records); $tmp_files[] = $file; $variation_attribute_values[$j] = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_attribute_value'][$j], $file)->parse($records); $tmp_files[] = $file; $variation_attribute_in_variation[$j] = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_in_variations'][$j], $file)->parse($records); $tmp_files[] = $file; $variation_attribute_is_visible[$j] = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_is_visible'][$j], $file)->parse($records); $tmp_files[] = $file; $variation_attribute_in_taxonomy[$j] = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_is_taxonomy'][$j], $file)->parse($records); $tmp_files[] = $file; $variable_create_terms_in_not_exists[$j] = XmlImportParser::factory($xml, $variation_xpath, $import->options['variable_create_taxonomy_in_not_exists'][$j], $file)->parse($records); $tmp_files[] = $file; } } // serialized attributes for product variations $variation_serialized_attributes = array(); if (!empty($variation_attribute_keys)) { foreach ($variation_attribute_keys as $j => $attribute_name) { if (!in_array($attribute_name[0], array_keys($variation_serialized_attributes))) { $variation_serialized_attributes[$attribute_name[0]] = array('value' => $variation_attribute_values[$j], 'is_visible' => $variation_attribute_is_visible[$j], 'in_variation' => $variation_attribute_in_variation[$j], 'in_taxonomy' => $variation_attribute_in_taxonomy[$j], 'is_create_taxonomy_terms' => $variable_create_terms_in_not_exists[$j]); } } } // Create Variations foreach ($variation_sku as $j => $void) { if ("" == $variation_sku[$j]) { continue; } if ($import->options['variable_sku_add_parent']) { $variation_sku[$j] = $product_sku[$i] . '-' . $variation_sku[$j]; } $variable_enabled = $variation_product_enabled[$j] == "yes" ? 'yes' : 'no'; // Enabled or disabled $post_status = $variable_enabled == 'yes' ? 'publish' : 'private'; $variation_to_update_id = false; $postRecord = new PMXI_Post_Record(); $postRecord->clear(); // Generate a useful post title $variation_post_title = sprintf(__('Variation #%s of %s', 'woocommerce'), $variation_sku[$j], $articleData['post_title']); // handle duplicates according to import settings /*if ($duplicates = pmxi_findDuplicates(array('post_title' => $variation_post_title, 'post_type' => 'product_variation', 'post_parent' => $pid),'','','parent')) { $duplicate_id = array_shift($duplicates); if ($duplicate_id) { $variation_to_update = get_post($variation_to_update_id = $duplicate_id); } } */ // Update or Add post $variation = array('post_title' => $variation_post_title, 'post_content' => '', 'post_status' => $post_status, 'post_parent' => $pid, 'post_type' => 'product_variation'); $variation_just_created = false; $postRecord->getBy(array('unique_key' => 'Variation ' . $variation_sku[$j] . ' of ' . $pid, 'import_id' => $import->id)); if (!$postRecord->isEmpty()) { $variation_to_update_id = $postRecord->post_id; $postRecord->set(array('iteration' => $import->iteration))->update(); } if (!$variation_to_update_id) { $variation_to_update_id = wp_insert_post($variation); // associate variation with import $postRecord->isEmpty() and $postRecord->set(array('post_id' => $variation_to_update_id, 'import_id' => $import->id, 'unique_key' => 'Variation ' . $variation_sku[$j] . ' of ' . $pid, 'product_key' => ''))->insert(); $postRecord->set(array('iteration' => $import->iteration))->update(); $variation_just_created = true; $logger and call_user_func($logger, sprintf(__('- `%s`: variation created successfully', 'pmxi_plugin'), sprintf(__('Variation #%s of %s', 'woocommerce'), absint($variation_to_update_id), esc_html(get_the_title($pid))))); } else { $this->wpdb->update($this->wpdb->posts, $variation, array('ID' => $variation_to_update_id)); //do_action( 'woocommerce_update_product_variation', $variation_to_update_id ); $logger and call_user_func($logger, sprintf(__('- `%s`: variation updated successfully', 'pmxi_plugin'), $variation_post_title)); } do_action('pmxi_update_product_variation', $variation_to_update_id); $existing_variation_meta_keys = array(); foreach (get_post_meta($variation_to_update_id, '') as $cur_meta_key => $cur_meta_val) { $existing_variation_meta_keys[] = $cur_meta_key; } // delete keys which are no longer correspond to import settings if (!empty($existing_variation_meta_keys)) { foreach ($existing_variation_meta_keys as $cur_meta_key) { // Do not delete post meta for features image if (in_array($cur_meta_key, array('_thumbnail_id', '_product_image_gallery'))) { continue; } // Update all data if ($import->options['update_all_data'] == 'yes') { delete_post_meta($variation_to_update_id, $cur_meta_key); continue; } // Do not update attributes if (!$import->options['is_update_attributes'] and (in_array($cur_meta_key, array('_default_attributes', '_product_attributes')) or strpos($cur_meta_key, "attribute_") === 0)) { continue; } // Update only these Attributes, leave the rest alone if ($import->options['is_update_attributes'] and $import->options['update_attributes_logic'] == 'only') { if ($cur_meta_key == '_product_attributes') { $current_product_attributes = get_post_meta($variation_to_update_id, '_product_attributes', true); if (!empty($current_product_attributes) and !empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { foreach ($current_product_attributes as $attr_name => $attr_value) { if (in_array($attr_name, array_filter($import->options['attributes_list'], 'trim'))) { unset($current_product_attributes[$attr_name]); } } } update_post_meta($variation_to_update_id, '_product_attributes', $current_product_attributes); continue; } if (strpos($cur_meta_key, "attribute_") === 0 and !empty($import->options['attributes_list']) and is_array($import->options['attributes_list']) and !in_array(str_replace("attribute_", "", $cur_meta_key), array_filter($import->options['attributes_list'], 'trim'))) { continue; } if (in_array($cur_meta_key, array('_default_attributes'))) { continue; } } // Leave these attributes alone, update all other Attributes if ($import->options['is_update_attributes'] and $import->options['update_attributes_logic'] == 'all_except') { if ($cur_meta_key == '_product_attributes') { if (empty($import->options['attributes_list'])) { delete_post_meta($variation_to_update_id, $cur_meta_key); continue; } $current_product_attributes = get_post_meta($variation_to_update_id, '_product_attributes', true); if (!empty($current_product_attributes) and !empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { foreach ($current_product_attributes as $attr_name => $attr_value) { if (!in_array($attr_name, array_filter($import->options['attributes_list'], 'trim'))) { unset($current_product_attributes[$attr_name]); } } } update_post_meta($variation_to_update_id, '_product_attributes', $current_product_attributes); continue; } if (strpos($cur_meta_key, "attribute_") === 0 and !empty($import->options['attributes_list']) and is_array($import->options['attributes_list']) and in_array(str_replace("attribute_", "", $cur_meta_key), array_filter($import->options['attributes_list'], 'trim'))) { continue; } if (in_array($cur_meta_key, array('_default_attributes'))) { continue; } } // Update all Custom Fields is defined if ($import->options['update_custom_fields_logic'] == "full_update") { delete_post_meta($variation_to_update_id, $cur_meta_key); } elseif ($import->options['update_custom_fields_logic'] == "only") { if (!empty($import->options['custom_fields_list']) and is_array($import->options['custom_fields_list']) and in_array($cur_meta_key, $import->options['custom_fields_list'])) { delete_post_meta($variation_to_update_id, $cur_meta_key); } } elseif ($import->options['update_custom_fields_logic'] == "all_except") { if (empty($import->options['custom_fields_list']) or !in_array($cur_meta_key, $import->options['custom_fields_list'])) { delete_post_meta($variation_to_update_id, $cur_meta_key); } } } } // Add any default post meta //add_post_meta( $variation_to_update_id, 'total_sales', '0', true ); $v_total_sales = get_post_meta($variation_to_update_id, 'total_sales', true); if (empty($v_total_sales)) { update_post_meta($variation_to_update_id, 'total_sales', '0'); } // Product type + Downloadable/Virtual wp_set_object_terms($variation_to_update_id, NULL, 'product_type'); update_post_meta($variation_to_update_id, '_downloadable', $variation_product_downloadable[$j] == "yes" ? 'yes' : 'no'); update_post_meta($variation_to_update_id, '_virtual', $variation_product_virtual[$j] == "yes" ? 'yes' : 'no'); // Update post meta if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_regular_price')) { update_post_meta($variation_to_update_id, '_regular_price', stripslashes($variation_regular_price[$j])); } if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_sale_price')) { update_post_meta($variation_to_update_id, '_sale_price', stripslashes($variation_sale_price[$j])); } if (class_exists('woocommerce_wholesale_pricing')) { update_post_meta($variation_to_update_id, 'pmxi_wholesale_price', stripslashes($variation_whosale_price[$j])); } // Dimensions if ($variation_product_virtual[$j] == 'no') { if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_weight')) { update_post_meta($variation_to_update_id, '_weight', stripslashes($variation_weight[$j])); } if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_length')) { update_post_meta($variation_to_update_id, '_length', stripslashes($variation_length[$j])); } if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_width')) { update_post_meta($variation_to_update_id, '_width', stripslashes($variation_width[$j])); } if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_height')) { update_post_meta($variation_to_update_id, '_height', stripslashes($variation_height[$j])); } } else { if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_weight')) { update_post_meta($variation_to_update_id, '_weight', ''); } if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_length')) { update_post_meta($variation_to_update_id, '_length', ''); } if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_width')) { update_post_meta($variation_to_update_id, '_width', ''); } if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_height')) { update_post_meta($variation_to_update_id, '_height', ''); } } // Save shipping class if (ctype_digit($variation_product_shipping_class[$j])) { $v_shipping_class = $variation_product_shipping_class[$j] > 0 ? absint($variation_product_shipping_class[$j]) : ''; } else { $vt_shipping_class = term_exists($variation_product_shipping_class[$j], 'product_shipping_class', 0); if (empty($vt_shipping_class) and !is_wp_error($vt_shipping_class)) { $vt_shipping_class = term_exists(htmlspecialchars($variation_product_shipping_class[$j]), 'product_shipping_class', 0); } if (!is_wp_error($vt_shipping_class)) { $v_shipping_class = (int) $vt_shipping_class['term_id']; } } wp_set_object_terms($variation_to_update_id, $v_shipping_class, 'product_shipping_class'); // Unique SKU $sku = get_post_meta($variation_to_update_id, '_sku', true); $new_sku = esc_html(trim(stripslashes($variation_sku[$j]))); if ($new_sku == '' and $import->options['disable_auto_sku_generation']) { if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_sku')) { update_post_meta($variation_to_update_id, '_sku', ''); } } elseif ($new_sku == '' and !$import->options['disable_auto_sku_generation']) { if ($variation_just_created or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_sku')) { $new_sku = substr(md5($variation_post_title), 0, 12); } } if ($new_sku == '') { update_post_meta($variation_to_update_id, '_sku', ''); } elseif ($new_sku !== $sku) { if (!empty($new_sku)) { if (!$import->options['disable_sku_matching'] and $this->wpdb->get_var($this->wpdb->prepare("\n\t\t\t\t\t\t\t\t\tSELECT " . $this->wpdb->posts . ".ID\n\t\t\t\t\t\t\t\t FROM " . $this->wpdb->posts . "\n\t\t\t\t\t\t\t\t LEFT JOIN " . $this->wpdb->postmeta . " ON (" . $this->wpdb->posts . ".ID = " . $this->wpdb->postmeta . ".post_id)\n\t\t\t\t\t\t\t\t WHERE " . $this->wpdb->posts . ".post_type = 'product'\n\t\t\t\t\t\t\t\t AND " . $this->wpdb->posts . ".post_status = 'publish'\n\t\t\t\t\t\t\t\t AND " . $this->wpdb->postmeta . ".meta_key = '_sku' AND " . $this->wpdb->postmeta . ".meta_value = '%s'\n\t\t\t\t\t\t\t\t ", $new_sku))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Product SKU must be unique.', 'pmxi_plugin'))); } else { update_post_meta($variation_to_update_id, '_sku', $new_sku); } } else { update_post_meta($variation_to_update_id, '_sku', ''); } } $date_from = isset($variation_sale_price_dates_from[$j]) ? $variation_sale_price_dates_from[$j] : ''; $date_to = isset($variation_sale_price_dates_to[$i]) ? $variation_sale_price_dates_to[$i] : ''; // Variable Description $this->pushmeta($variation_to_update_id, '_variation_description', wp_kses_post($variation_description[$j])); // Dates if ($date_from) { update_post_meta($variation_to_update_id, '_sale_price_dates_from', strtotime($date_from)); } else { update_post_meta($variation_to_update_id, '_sale_price_dates_from', ''); } if ($date_to) { update_post_meta($variation_to_update_id, '_sale_price_dates_to', strtotime($date_to)); } else { update_post_meta($variation_to_update_id, '_sale_price_dates_to', ''); } if ($date_to && !$date_from) { update_post_meta($variation_to_update_id, '_sale_price_dates_from', strtotime('NOW', current_time('timestamp'))); } // Update price if on sale if ($variation_sale_price[$j] == '') { if (!empty($this->articleData['ID']) and !$this->is_update_cf('_sale_price')) { $variation_sale_price[$j] = get_post_meta($variation_to_update_id, '_sale_price', true); } } if ($variation_sale_price[$j] != '' && $date_to == '' && $date_from == '') { $this->pushmeta($variation_to_update_id, '_price', stripslashes($variation_sale_price[$j])); } else { $this->pushmeta($variation_to_update_id, '_price', stripslashes($variation_regular_price[$j])); } if ($variation_sale_price[$j] != '' && $date_from && strtotime($date_from) < strtotime('NOW', current_time('timestamp'))) { update_post_meta($variation_to_update_id, '_price', stripslashes($variation_sale_price[$j])); } if ($date_to && strtotime($date_to) < strtotime('NOW', current_time('timestamp'))) { if (empty($articleData['ID']) or $this->is_update_custom_field($existing_variation_meta_keys, $import->options, '_price')) { update_post_meta($variation_to_update_id, '_price', stripslashes($variation_regular_price[$j])); } update_post_meta($variation_to_update_id, '_sale_price_dates_from', ''); update_post_meta($variation_to_update_id, '_sale_price_dates_to', ''); } // Stock Data if (strtolower($variation_product_manage_stock[$j]) == 'yes') { // Manage stock if (empty($articleData['ID']) or $this->is_update_cf('_manage_stock')) { update_post_meta($variation_to_update_id, '_manage_stock', 'yes'); } if (empty($articleData['ID']) or $this->is_update_cf('_stock_status')) { update_post_meta($variation_to_update_id, '_stock_status', stripslashes($variable_stock_status[$j])); } if (empty($articleData['ID']) or $this->is_update_cf('_stock')) { update_post_meta($variation_to_update_id, '_stock', (int) $variation_stock[$j]); } } else { if (empty($articleData['ID']) or $this->is_update_cf('_manage_stock')) { update_post_meta($variation_to_update_id, '_manage_stock', 'no'); } if (empty($articleData['ID']) or $this->is_update_cf('_stock_status')) { update_post_meta($variation_to_update_id, '_stock_status', stripslashes($variable_stock_status[$j])); } delete_post_meta($variation_to_update_id, '_backorders'); delete_post_meta($variation_to_update_id, '_stock'); } if ($variation_product_tax_class[$j] !== 'parent') { update_post_meta($variation_to_update_id, '_tax_class', sanitize_text_field($variation_product_tax_class[$j])); } else { delete_post_meta($variation_to_update_id, '_tax_class'); } if ($variation_product_downloadable[$j] == 'yes') { update_post_meta($variation_to_update_id, '_download_limit', sanitize_text_field($variation_download_limit[$j])); update_post_meta($variation_to_update_id, '_download_expiry', sanitize_text_field($variation_download_expiry[$j])); $_file_paths = array(); if (!empty($variation_file_paths[$j])) { $file_paths = explode($import->options['variable_product_files_delim'], $variation_file_paths[$j]); $file_names = explode($import->options['variable_product_files_names_delim'], $variation_file_names[$j]); foreach ($file_paths as $fn => $file_path) { $file_path = sanitize_text_field($file_path); $_file_paths[md5($file_path)] = array('name' => !empty($file_names[$fn]) ? $file_names[$fn] : basename($file_path), 'file' => $file_path); } } // grant permission to any newly added files on any existing orders for this product update_post_meta($variation_to_update_id, '_downloadable_files', $_file_paths); } else { update_post_meta($variation_to_update_id, '_download_limit', ''); update_post_meta($variation_to_update_id, '_download_expiry', ''); update_post_meta($variation_to_update_id, '_downloadable_files', ''); update_post_meta($variation_to_update_id, '_download_type', ''); } // Remove old taxonomies attributes so data is kept up to date if ($variation_to_update_id and ($import->options['update_all_data'] == 'yes' or $import->options['update_all_data'] == 'no' and $import->options['is_update_attributes'] or $variation_just_created)) { if ($import->options['update_all_data'] == 'yes' or $import->options['update_attributes_logic'] == 'full_update') { $this->wpdb->query($this->wpdb->prepare("DELETE FROM {$this->wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND post_id = %d;", $variation_to_update_id)); } wp_cache_delete($variation_to_update_id, 'post_meta'); } // Update taxonomies if ($import->options['update_all_data'] == 'yes' or $import->options['update_all_data'] == 'no' and $import->options['is_update_attributes'] or $variation_just_created) { foreach ($variation_serialized_attributes as $a_name => $attr_data) { $attr_name = $a_name; // Update only these Attributes, leave the rest alone if ($import->options['update_all_data'] == 'no' and $import->options['update_attributes_logic'] == 'only') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (!in_array(intval($attr_data['in_taxonomy'][$j]) ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($import->options['attributes_list'], 'trim'))) { continue; } } else { break; } } // Leave these attributes alone, update all other Attributes if ($import->options['update_all_data'] == 'no' and $import->options['update_attributes_logic'] == 'all_except') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (in_array(intval($attr_data['in_taxonomy'][$j]) ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($import->options['attributes_list'], 'trim'))) { continue; } } } if (intval($attr_data['in_taxonomy'][$j]) and (strpos($attr_name, "pa_") === false or strpos($attr_name, "pa_") !== 0)) { $attr_name = "pa_" . $attr_name; } $is_variation = intval($attr_data['in_variation'][$j]); // Don't use woocommerce_clean as it destroys sanitized characters $values = intval($attr_data['in_taxonomy'][$j]) ? $attr_data['value'][$j] : $attr_data['value'][$j]; if (intval($attr_data['in_taxonomy'][$j])) { if (intval($attr_data['is_create_taxonomy_terms'][0])) { $this->create_taxonomy($a_name, $logger); } $term = get_term_by('name', $values, wc_attribute_taxonomy_name($a_name), ARRAY_A); if (empty($term) and !is_wp_error($term)) { $term = term_exists($values, wc_attribute_taxonomy_name($a_name)); if (empty($term) and !is_wp_error($term)) { $term = term_exists(htmlspecialchars($values), wc_attribute_taxonomy_name($a_name)); if (empty($term) and !is_wp_error($term) and intval($attr_data['is_create_taxonomy_terms'][0])) { $term = wp_insert_term($values, wc_attribute_taxonomy_name($a_name)); } } } if (!is_wp_error($term)) { $term = get_term_by('id', $term['term_id'], wc_attribute_taxonomy_name($a_name)); update_post_meta($variation_to_update_id, 'attribute_' . sanitize_title($attr_name), $term->slug); } } else { update_post_meta($variation_to_update_id, 'attribute_' . sanitize_title($attr_name), $values); } } } if (!is_array($variation_image[$j])) { $variation_image[$j] = array($variation_image[$j]); } $uploads = wp_upload_dir(); if (!empty($uploads) and false === $uploads['error'] and !empty($variation_image[$j]) and (empty($articleData['ID']) or $import->options['update_all_data'] == "yes" or $import->options['update_all_data'] == "no" and $import->options['is_update_images'])) { $gallery_attachment_ids = array(); foreach ($variation_image[$j] as $featured_image) { $imgs = explode(',', $featured_image); if (!empty($imgs)) { foreach ($imgs as $img_url) { if (empty($img_url)) { continue; } $url = str_replace(" ", "%20", trim($img_url)); $bn = preg_replace('/[\\?|&].*/', '', basename($url)); $img_ext = pmxi_getExtensionFromStr($url); $default_extension = pmxi_getExtension($bn); if ($img_ext == "") { $img_ext = pmxi_get_remote_image_ext($url); } // generate local file name $image_name = urldecode(sanitize_file_name($img_ext ? str_replace("." . $default_extension, "", $bn) : $bn)) . ("" != $img_ext ? '.' . $img_ext : ''); // if wizard store image data to custom field $create_image = false; $download_image = true; $image_filename = wp_unique_filename($uploads['path'], $image_name); $image_filepath = $uploads['path'] . '/' . $image_filename; // keep existing and add newest images if (!empty($articleData['ID']) and $import->options['is_update_images'] and $import->options['update_images_logic'] == "add_new" and $import->options['update_all_data'] == "no") { $attachment_imgs = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $variation_to_update_id)); if ($attachment_imgs) { foreach ($attachment_imgs as $attachment_img) { if ($attachment_img->guid == $uploads['url'] . '/' . $image_name) { $download_image = false; $success_images = true; set_post_thumbnail($variation_to_update_id, $attachment_img->ID); $gallery_attachment_ids[] = $attachment_img->ID; $logger and call_user_func($logger, sprintf(__('- <b>Image SKIPPED</b>: The image %s is always exists for the %s', 'pmxi_plugin'), basename($attachment_img->guid), $variation_post_title)); } } } } if ($download_image) { // do not download images if (!$import->options['download_images'] or $import->options['variable_image_use_parent']) { $image_filename = $image_name; $image_filepath = $uploads['path'] . '/' . $image_filename; $existing_attachment = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->prefix . "posts WHERE guid = '%s'", $uploads['url'] . '/' . $image_filename)); if (!empty($existing_attachment->ID)) { $download_image = false; $create_image = false; set_post_thumbnail($variation_to_update_id, $existing_attachment->ID); $gallery_attachment_ids[] = $existing_attachment->ID; do_action('pmxi_gallery_image', $variation_to_update_id, $existing_attachment->ID, $image_filepath); } else { if (@file_exists($image_filepath)) { $download_image = false; if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'pmxi_plugin'), $image_filepath)); @unlink($image_filepath); } else { $create_image = true; } } } } if ($download_image) { $request = get_file_curl($url, $image_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) { @unlink($image_filepath); // delete file since failed upload may result in empty file created } elseif ($image_info = @getimagesize($image_filepath) and in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $create_image = true; } if (!$create_image) { $url = str_replace(" ", "%20", trim(pmxi_convert_encoding($img_url))); $request = get_file_curl($url, $image_filepath); if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s cannot be saved locally as %s', 'pmxi_plugin'), $url, $image_filepath)); @unlink($image_filepath); // delete file since failed upload may result in empty file created } elseif (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'pmxi_plugin'), $url)); @unlink($image_filepath); } else { $create_image = true; } } } } if ($create_image) { // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once ABSPATH . 'wp-admin/includes/image.php'; $attachment = array('post_mime_type' => image_type_to_mime_type($image_info[2]), 'guid' => $uploads['url'] . '/' . $image_filename, 'post_title' => $image_filename, 'post_content' => ''); if ($image_meta = wp_read_image_metadata($image_filepath)) { if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) { $attachment['post_title'] = $image_meta['title']; } if (trim($image_meta['caption'])) { $attachment['post_content'] = $image_meta['caption']; } } $attid = wp_insert_attachment($attachment, $image_filepath, $variation_to_update_id); if (is_wp_error($attid)) { $logger and call_user_func($logger, __('- <b>WARNING</b>', 'pmxi_plugin') . ': ' . $attid->get_error_message()); } else { wp_update_attachment_metadata($attid, wp_generate_attachment_metadata($attid, $image_filepath)); do_action('pmxi_gallery_image', $variation_to_update_id, $attid, $image_filepath); $success_images = true; set_post_thumbnail($variation_to_update_id, $attid); $gallery_attachment_ids[] = $attid; } } } } } // Set product gallery images if (!empty($gallery_attachment_ids)) { update_post_meta($variation_to_update_id, '_product_image_gallery', implode(',', $gallery_attachment_ids)); } } wc_delete_product_transients($variation_to_update_id); } foreach ($tmp_files as $file) { // remove all temporary files created if (file_exists($file)) { @unlink($file); } } // Update parent if variable so price sorting works and stays in sync with the cheapest child $children = get_posts(array('post_parent' => $pid, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => array('draft', 'publish', 'trash', 'pending', 'future', 'private'))); $lowest_price = $lowest_regular_price = $lowest_sale_price = $highest_price = $highest_regular_price = $highest_sale_price = ''; $total_instock = 0; if ($children) { foreach ($children as $child) { $_variation_stock = get_post_meta($child, '_stock_status', true); $total_instock += $_variation_stock == 'instock' ? 1 : 0; $child_price = get_post_meta($child, '_price', true); $child_regular_price = get_post_meta($child, '_regular_price', true); $child_sale_price = get_post_meta($child, '_sale_price', true); // Regular prices if (!is_numeric($lowest_regular_price) || $child_regular_price < $lowest_regular_price) { $lowest_regular_price = $child_regular_price; } if (!is_numeric($highest_regular_price) || $child_regular_price > $highest_regular_price) { $highest_regular_price = $child_regular_price; } // Sale prices if ($child_price == $child_sale_price) { if ($child_sale_price !== '' && (!is_numeric($lowest_sale_price) || $child_sale_price < $lowest_sale_price)) { $lowest_sale_price = $child_sale_price; } if ($child_sale_price !== '' && (!is_numeric($highest_sale_price) || $child_sale_price > $highest_sale_price)) { $highest_sale_price = $child_sale_price; } } } $lowest_price = $lowest_sale_price === '' || $lowest_regular_price < $lowest_sale_price ? $lowest_regular_price : $lowest_sale_price; $highest_price = $highest_sale_price === '' || $highest_regular_price > $highest_sale_price ? $highest_regular_price : $highest_sale_price; $this->pushmeta($pid, '_stock_status', $total_instock > 0 ? 'instock' : 'outofstock'); $this->pushmeta($pid, '_price', $lowest_price); update_post_meta($pid, '_min_variation_price', $lowest_price); update_post_meta($pid, '_max_variation_price', $highest_price); update_post_meta($pid, '_min_variation_regular_price', $lowest_regular_price); update_post_meta($pid, '_max_variation_regular_price', $highest_regular_price); update_post_meta($pid, '_min_variation_sale_price', $lowest_sale_price); update_post_meta($pid, '_max_variation_sale_price', $highest_sale_price); // Update default attribute options setting if ($import->options['update_all_data'] == 'yes' or $import->options['update_all_data'] == 'no' and $import->options['is_update_attributes'] or $variation_just_created) { $default_attributes = array(); $parent_attributes = array(); $attribute_position = 0; $is_update_attributes = true; foreach ($variation_serialized_attributes as $a_name => $attr_data) { $attr_name = $a_name; $values = array(); // Update only these Attributes, leave the rest alone if ($import->options['update_all_data'] == 'no' and $import->options['update_attributes_logic'] == 'only') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (!in_array(intval($attr_data['in_taxonomy'][$j]) ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($import->options['attributes_list'], 'trim'))) { $attribute_position++; continue; } } else { $is_update_attributes = false; break; } } // Leave these attributes alone, update all other Attributes if ($import->options['update_all_data'] == 'no' and $import->options['update_attributes_logic'] == 'all_except') { if (!empty($import->options['attributes_list']) and is_array($import->options['attributes_list'])) { if (in_array(intval($attr_data['in_taxonomy'][$j]) ? wc_attribute_taxonomy_name($attr_name) : $attr_name, array_filter($import->options['attributes_list'], 'trim'))) { $attribute_position++; continue; } } } foreach ($variation_sku as $j => $void) { $is_variation = intval($attr_data['in_variation'][$j]) ? 1 : 0; if ($is_variation) { $value = esc_attr(trim($attr_data['value'][$j])); if (!in_array($value, $values, true)) { $values[] = $value; } if (!empty($value) and empty($default_attributes[intval($attr_data['in_taxonomy'][$j]) ? wc_attribute_taxonomy_name($attr_name) : sanitize_title($attr_name)])) { switch ($import->options['default_attributes_type']) { case 'instock': if ($variable_stock_status[$j] == 'instock') { $default_attributes[intval($attr_data['in_taxonomy'][$j]) ? wc_attribute_taxonomy_name($attr_name) : sanitize_title($attr_name)] = sanitize_title($value); } break; case 'first': $default_attributes[intval($attr_data['in_taxonomy'][$j]) ? wc_attribute_taxonomy_name($attr_name) : sanitize_title($attr_name)] = sanitize_title($value); break; default: # code... break; } } } } if (intval($attr_data['in_taxonomy'][0])) { if (intval($attr_data['is_create_taxonomy_terms'][0])) { $this->create_taxonomy($attr_name, $logger); } if (isset($values) and taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { // Remove empty items in the array $values = array_filter($values, array($this, "filtering")); if (!empty($values)) { $attr_values = array(); foreach ($values as $key => $value) { $term = get_term_by('name', $value, wc_attribute_taxonomy_name($attr_name), ARRAY_A); if (empty($term) and !is_wp_error($term)) { $term = term_exists($value, wc_attribute_taxonomy_name($attr_name)); if (empty($term) and !is_wp_error($term)) { $term = term_exists(htmlspecialchars($value), wc_attribute_taxonomy_name($attr_name)); if (empty($term) and !is_wp_error($term) and intval($attr_data['is_create_taxonomy_terms'][0])) { $term = wp_insert_term($value, wc_attribute_taxonomy_name($attr_name)); } } } if (!is_wp_error($term)) { $attr_values[] = (int) $term['term_id']; } } $values = $attr_values; } } else { $values = array(); } // Update post terms if (taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { wp_set_object_terms($pid, $values, wc_attribute_taxonomy_name($attr_name)); } if ($values) { // Add attribute to array, but don't set values $parent_attributes[wc_attribute_taxonomy_name($attr_name)] = array('name' => wc_attribute_taxonomy_name($attr_name), 'value' => '', 'position' => $attribute_position, 'is_visible' => !empty($attr_data['is_visible'][0]) ? 1 : 0, 'is_variation' => !empty($attr_data['in_variation'][0]) ? 1 : 0, 'is_taxonomy' => 1, 'is_create_taxonomy_terms' => !empty($attr_data['is_create_taxonomy_terms'][0]) ? 1 : 0); } } else { if (taxonomy_exists(wc_attribute_taxonomy_name($attr_name))) { wp_set_object_terms($pid, NULL, wc_attribute_taxonomy_name($attr_name)); } $parent_attributes[sanitize_title($attr_name)] = array('name' => sanitize_text_field($attr_name), 'value' => implode('|', $values), 'position' => $attribute_position, 'is_visible' => !empty($attr_data['is_visible'][0]) ? 1 : 0, 'is_variation' => !empty($attr_data['in_variation'][0]) ? 1 : 0, 'is_taxonomy' => 0); } $attribute_position++; } if ($import->options['is_default_attributes'] and $is_update_attributes) { $current_default_attributes = get_post_meta($pid, '_default_attributes', true); update_post_meta($pid, '_default_attributes', !empty($current_default_attributes) ? array_merge($current_default_attributes, $default_attributes) : $default_attributes); } if ($is_update_attributes) { $current_product_attributes = get_post_meta($pid, '_product_attributes', true); update_post_meta($pid, '_product_attributes', !empty($current_product_attributes) ? array_merge($current_product_attributes, $parent_attributes) : $parent_attributes); } } } elseif ($import->options['make_simple_product']) { $this->make_simple_product($pid); } } elseif ($import->options['make_simple_product']) { $this->make_simple_product($pid); } } }
public function saved_post($importData) { if (!in_array($importData['import']->options['custom_type'], array('product', 'product_variation'))) { return; } $postRecord = new PMXI_Post_Record(); $postRecord->clear(); // find corresponding article among previously imported $postRecord->getBy(array('unique_key' => 'Variation ' . get_post_meta($importData['pid'], '_sku', true), 'import_id' => $importData['import']->id)); $pid = !$postRecord->isEmpty() ? $postRecord->post_id : false; if ($pid) { // Get all existing meta keys of parent product $existing_meta_keys = array(); foreach (get_post_meta($importData['pid'], '') as $cur_meta_key => $cur_meta_val) { $existing_meta_keys[$cur_meta_key] = $cur_meta_val; } if (!empty($existing_meta_keys) and !empty($importData['import']->options['custom_name'])) { foreach ($existing_meta_keys as $key => $value) { if (in_array($key, $importData['import']->options['custom_name'])) { $this->pushmeta($pid, $key, $value); } } } } if ($importData['import']->options['create_draft'] == 'yes') { $table = $this->wpdb->posts; $p = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM {$table} WHERE ID = %d;", $importData['pid'])); if ($p and $p->post_type == 'product_variation' and $p->post_status == 'draft') { $this->wpdb->update($this->wpdb->posts, array('post_status' => 'publish'), array('ID' => $importData['pid'])); } } }
/** * Export XML helper */ function pmxe_export_users_xml($exportQuery, $exportOptions, $preview = false, $is_cron = false, $file_path = false) { $xmlWriter = new XMLWriter(); $xmlWriter->openMemory(); $xmlWriter->setIndent(true); $xmlWriter->setIndentString("\t"); $xmlWriter->startDocument('1.0', $exportOptions['encoding']); $xmlWriter->startElement('data'); foreach ($exportQuery->results as $user) { //$exportQuery->the_post(); $record = get_post( get_the_ID() ); $xmlWriter->startElement('post'); if ($exportOptions['ids']) { if (wp_all_export_is_compatible() and $exportOptions['is_generate_import'] and $exportOptions['import_id']) { $postRecord = new PMXI_Post_Record(); $postRecord->clear(); $postRecord->getBy(array('post_id' => $user->ID, 'import_id' => $exportOptions['import_id'])); if ($postRecord->isEmpty()) { $postRecord->set(array('post_id' => $user->ID, 'import_id' => $exportOptions['import_id'], 'unique_key' => $user->ID))->save(); } unset($postRecord); } foreach ($exportOptions['ids'] as $ID => $value) { if (is_numeric($ID)) { if (empty($exportOptions['cc_name'][$ID]) or empty($exportOptions['cc_type'][$ID])) { continue; } $element_name = !empty($exportOptions['cc_name'][$ID]) ? str_replace(" ", "_", $exportOptions['cc_name'][$ID]) : 'untitled_' . $ID; $fieldSnipped = (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) ? $exportOptions['cc_code'][$ID] : false; switch ($exportOptions['cc_type'][$ID]) { case 'id': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_id', pmxe_filter($user->ID, $fieldSnipped), $user->ID)); break; case 'user_login': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_login', pmxe_filter($user->user_login, $fieldSnipped), $user->ID)); break; case 'user_pass': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_pass', pmxe_filter($user->user_pass, $fieldSnipped), $user->ID)); break; case 'user_email': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_email', pmxe_filter($user->user_email, $fieldSnipped), $user->ID)); break; case 'user_nicename': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_nicename', pmxe_filter($user->user_nicename, $fieldSnipped), $user->ID)); break; case 'user_url': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_url', pmxe_filter($user->user_url, $fieldSnipped), $user->ID)); break; /*case 'user_activation_key': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_activation_key', pmxe_filter($user->user_activation_key, $fieldSnipped), $user->ID)); break; case 'user_status': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_status', pmxe_filter($user->user_status, $fieldSnipped), $user->ID)); break;*/ /*case 'user_activation_key': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_activation_key', pmxe_filter($user->user_activation_key, $fieldSnipped), $user->ID)); break; case 'user_status': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_status', pmxe_filter($user->user_status, $fieldSnipped), $user->ID)); break;*/ case 'display_name': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_display_name', pmxe_filter($user->display_name, $fieldSnipped), $user->ID)); break; case 'user_registered': if (!empty($exportOptions['cc_options'][$ID])) { switch ($exportOptions['cc_options'][$ID]) { case 'unix': $post_date = strtotime($user->user_registered); break; default: $post_date = date($exportOptions['cc_options'][$ID], strtotime($user->user_registered)); break; } } else { $post_date = $user->user_registered; } $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_registered', pmxe_filter($post_date, $fieldSnipped), $user->ID)); break; case 'nickname': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_nickname', pmxe_filter($user->nickname, $fieldSnipped), $user->ID)); break; case 'first_name': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_first_name', pmxe_filter($user->first_name, $fieldSnipped), $user->ID)); break; case 'last_name': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_last_name', pmxe_filter($user->last_name, $fieldSnipped), $user->ID)); break; case 'wp_capabilities': $xmlWriter->writeElement($element_name, apply_filters('pmxe_user_wp_capabilities', pmxe_filter(implode(",", $user->roles), $fieldSnipped), $user->ID)); break; case 'description': $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_user_description', pmxe_filter($user->description, $fieldSnipped), $user->ID)); $xmlWriter->endElement(); break; case 'cf': if (!empty($exportOptions['cc_value'][$ID])) { $cur_meta_values = get_user_meta($user->ID, $exportOptions['cc_value'][$ID]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $exportOptions['cc_value'][$ID], $user->ID)); $xmlWriter->endElement(); } } if (empty($cur_meta_values)) { $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $exportOptions['cc_value'][$ID], $user->ID)); $xmlWriter->endElement(); } } break; case 'acf': if (!empty($exportOptions['cc_label'][$ID]) and class_exists('acf')) { global $acf; $field_value = get_field($exportOptions['cc_label'][$ID], $user->ID); $field_options = unserialize($exportOptions['cc_options'][$ID]); pmxe_export_acf_field_xml($field_value, $exportOptions, $ID, $user->ID, $xmlWriter, $element_name, $fieldSnipped, $field_options['group_id']); } break; case 'sql': if (!empty($exportOptions['cc_sql'][$ID])) { global $wpdb; $val = $wpdb->get_var($wpdb->prepare(stripcslashes(str_replace("%%ID%%", "%d", $exportOptions['cc_sql'][$ID])), get_the_ID())); if (!empty($exportOptions['cc_php'][$ID]) and !empty($exportOptions['cc_code'][$ID])) { // if shortcode defined if (strpos($exportOptions['cc_code'][$ID], '[') === 0) { $val = do_shortcode(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])); } else { $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $exportOptions['cc_code'][$ID])) . ';'); } } $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_sql_field', $val, $element_name, get_the_ID())); $xmlWriter->endElement(); } break; default: # code... break; } } } } $xmlWriter->endElement(); // end post if ($preview) { break; } } $xmlWriter->endElement(); // end data if ($preview) { return wp_all_export_remove_colons($xmlWriter->flush(true)); } if ($is_cron) { $xml = $xmlWriter->flush(true); if (file_exists($file_path)) { file_put_contents($file_path, wp_all_export_remove_colons(substr(substr($xml, 45), 0, -8)), FILE_APPEND); } else { file_put_contents($file_path, wp_all_export_remove_colons(substr($xml, 0, -8))); } return $file_path; } else { if (empty(PMXE_Plugin::$session->file)) { $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); $wp_uploads = wp_upload_dir(); $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path']; $export_file = $target . DIRECTORY_SEPARATOR . time() . '.' . $exportOptions['export_to']; file_put_contents($export_file, wp_all_export_remove_colons(substr($xmlWriter->flush(true), 0, -8))); PMXE_Plugin::$session->set('file', $export_file); PMXE_Plugin::$session->save_data(); } else { file_put_contents(PMXE_Plugin::$session->file, wp_all_export_remove_colons(substr(substr($xmlWriter->flush(true), 45), 0, -8)), FILE_APPEND); } return true; } }