/** * 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; } }
protected function prepare_export_data($record, $options, $elId) { $data = array(); if (!empty($options['cc_value'][$elId])) { $implode_delimiter = $options['delimiter'] == ',' ? '|' : ','; $element_name = !empty($options['cc_name'][$elId]) ? $options['cc_name'][$elId] : 'untitled_' . $elId; $fieldSnipped = (!empty($options['cc_php'][$elId]) and !empty($options['cc_code'][$elId])) ? $options['cc_code'][$elId] : false; switch ($options['cc_value'][$elId]) { case 'attributes': $_product_attributes = empty($record->post_parent) ? get_post_meta($record->ID, '_product_attributes', true) : get_post_meta($record->post_parent, '_product_attributes', true); if (empty(self::$_existing_attributes)) { global $wp_taxonomies; foreach ($wp_taxonomies as $key => $obj) { if (in_array($obj->name, array('nav_menu'))) { continue; } if (strpos($obj->name, "pa_") === 0 and strlen($obj->name) > 3 and !in_array($obj->name, self::$_existing_attributes)) { self::$_existing_attributes[] = $obj->name; } } } // combine taxonomies attributes if (!empty(self::$_existing_attributes)) { foreach (self::$_existing_attributes as $taxonomy_slug) { $taxonomy = get_taxonomy($taxonomy_slug); $data['Attribute Name (' . $taxonomy_slug . ')'] = $taxonomy->labels->name; $data['Attribute In Variations (' . $taxonomy_slug . ')'] = !empty($_product_attributes[$taxonomy_slug]['is_variation']) ? "yes" : "no"; $data['Attribute Is Visible (' . $taxonomy_slug . ')'] = !empty($_product_attributes[$taxonomy_slug]['is_visible']) ? "yes" : "no"; $data['Attribute Is Taxonomy (' . $taxonomy_slug . ')'] = !empty($_product_attributes[$taxonomy_slug]['is_taxonomy']) ? "yes" : "no"; $element_name = 'Attribute Value (' . $taxonomy_slug . ')'; if ($record->post_parent == 0) { $txes_list = get_the_terms($record->ID, $taxonomy_slug); if (!is_wp_error($txes_list) and !empty($txes_list)) { $attr_new = array(); foreach ($txes_list as $t) { $attr_new[] = $t->name; } $data[$element_name] = apply_filters('pmxe_woo_attribute', pmxe_filter(implode($implode_delimiter, $attr_new), $fieldSnipped), $record->ID); } else { $data[$element_name] = ''; } } else { $data[$element_name] = get_post_meta($record->ID, 'attribute_' . $taxonomy_slug, true); } } } // combine custom attributes if (!empty(self::$products_data['attributes'])) { foreach (self::$products_data['attributes'] as $attribute) { $attribute_standart_name = str_replace('attribute_', '', $attribute->meta_key); $attribute_name = ucfirst($attribute_standart_name); $data['Attribute Name (' . $attribute_name . ')'] = $attribute_name; $data['Attribute Value (' . $attribute_name . ')'] = get_post_meta($record->ID, $attribute->meta_key, true); $data['Attribute In Variations (' . $attribute_name . ')'] = !empty($_product_attributes[$attribute_standart_name]['is_variation']) ? "yes" : "no"; $data['Attribute Is Visible (' . $attribute_name . ')'] = !empty($_product_attributes[$attribute_standart_name]['is_visible']) ? "yes" : "no"; $data['Attribute Is Taxonomy (' . $attribute_name . ')'] = !empty($_product_attributes[$attribute_standart_name]['is_taxonomy']) ? "yes" : "no"; } } break; default: $cur_meta_values = get_post_meta($record->ID, $options['cc_value'][$elId]); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { switch ($options['cc_label'][$elId]) { 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']; } $data[$element_name . ' Paths'] = implode($implode_delimiter, $file_paths); $data[$element_name . ' Names'] = implode($implode_delimiter, $file_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); } $data[$element_name] = implode($implode_delimiter, $_skus); } break; default: if (empty($data[$element_name])) { $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $options['cc_value'][$elId], $record->ID); } else { $data[$element_name . '_' . $key] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $options['cc_value'][$elId], $record->ID); } break; } } } if (empty($cur_meta_values)) { $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter('', $fieldSnipped), $options['cc_value'][$elId], $record->ID); } break; } } return $data; }
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; }
/** * 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; } }
protected function prepare_export_data($record, $options, $elId, $preview) { // an array with data to export $data = array('items' => array(), 'taxes' => array(), 'shipping' => array(), 'coupons' => array(), 'surcharge' => array()); global $wpdb; $table_prefix = $wpdb->prefix; $implode_delimiter = XmlExportEngine::$implode; if (empty($this->order_id) or $this->order_id != $record->ID) { $this->order_id = $record->ID; $all_order_items = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_items WHERE order_id = {$record->ID}"); if (!empty($all_order_items)) { foreach ($all_order_items as $item) { switch ($item->order_item_type) { case 'line_item': $this->order_items[] = $item; break; case 'tax': $this->order_taxes[] = $item; break; case 'shipping': $this->order_shipping[] = $item; break; case 'coupon': $this->order_coupons[] = $item; break; case 'fee': $this->order_surcharge[] = $item; break; } } } $this->order_refunds = $wpdb->get_results("SELECT * FROM {$table_prefix}posts WHERE post_parent = {$record->ID} AND post_type = 'shop_order_refund'"); } if (!empty($options['cc_value'][$elId])) { $is_items_in_list = false; $is_item_data_in_list = false; foreach ($options['ids'] as $ID => $value) { if ($options['cc_options'][$ID] == 'items') { $is_items_in_list = true; } if (strpos($options['cc_label'][$ID], "item_data__") !== false) { $is_item_data_in_list = true; } } $fieldSnipped = (!empty($options['cc_php'][$elId]) and !empty($options['cc_code'][$elId])) ? $options['cc_code'][$elId] : false; if (!$is_items_in_list and $is_item_data_in_list) { if (!empty($this->order_items)) { foreach ($this->order_items as $n => $order_item) { $meta_data = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_item->order_item_id}", ARRAY_A); $item_data = array(); foreach ($options['ids'] as $subID => $subvalue) { if (strpos($options['cc_label'][$subID], 'item_data__') !== false) { $product_id = ''; $variation_id = ''; foreach ($meta_data as $meta) { if ($meta['meta_key'] == '_variation_id' and !empty($meta['meta_value'])) { $variation_id = $meta['meta_value']; } if ($meta['meta_key'] == '_product_id' and !empty($meta['meta_value'])) { $product_id = $meta['meta_value']; } } $_product_id = empty($variation_id) ? $product_id : $variation_id; $_product = get_post($_product_id); // do not export anything if product doesn't exist if (!empty($_product)) { $item_add_data = XmlExportCpt::prepare_data($_product, false, $this->acfs, $this->woo, $this->woo_order, ",", $preview, true, $subID); if (!empty($item_add_data)) { foreach ($item_add_data as $item_add_data_key => $item_add_data_value) { if (!isset($item_data[$item_add_data_key])) { $item_data[$item_add_data_key] = $item_add_data_value; } } } } } } if (!empty($item_data)) { $data['items'][] = $item_data; } } $this->order_items = null; } } switch ($options['cc_options'][$elId]) { case 'order': case 'customer': $data[$options['cc_name'][$elId]] = strpos($options['cc_value'][$elId], "_") === 0 ? get_post_meta($record->ID, $options['cc_value'][$elId], true) : $record->{$options['cc_value'][$elId]}; if ($options['cc_value'][$elId] == "post_title") { $data[$options['cc_name'][$elId]] = str_replace("–", '-', $data[$options['cc_name'][$elId]]); } $data[$options['cc_name'][$elId]] = pmxe_filter($data[$options['cc_name'][$elId]], $fieldSnipped); break; } } return $data; }
protected function prepare_export_data($record, $options, $elId, $preview) { // an array with data to export $data = array('items' => array(), 'taxes' => array(), 'shipping' => array(), 'coupons' => array(), 'surcharge' => array()); global $wpdb; $table_prefix = $wpdb->prefix; if (!empty($options['cc_value'][$elId])) { switch ($options['cc_options'][$elId]) { case 'order': case 'customer': $data[$options['cc_name'][$elId]] = strpos($options['cc_value'][$elId], "_") === 0 ? get_post_meta($record->ID, $options['cc_value'][$elId], true) : $record->{$options['cc_value'][$elId]}; if ($options['cc_value'][$elId] == "post_title") { $data[$options['cc_name'][$elId]] = str_replace("–", '-', $data[$options['cc_name'][$elId]]); } $data[$options['cc_name'][$elId]] = pmxe_filter($data[$options['cc_name'][$elId]], $fieldSnipped); break; } } return $data; }
/** * 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_acf_field_xml($field_value, $exportOptions, $ID, $recordID, &$xmlWriter, $element_name = '', $fieldSnipped = '', $group_id = '') { if (!empty($field_value)) { $field_value = maybe_unserialize($field_value); $field_name = $ID ? $exportOptions['cc_label'][$ID] : $exportOptions['name']; $field_options = $ID ? unserialize($exportOptions['cc_options'][$ID]) : $exportOptions; //$element_name = 'acf_' . $element_name; $put_to_xml = true; // switch ACF field type switch ($field_options['type']) { case 'date_time_picker': case 'date_picker': $field_value = date('Ymd', strtotime($field_value)); break; case 'file': case 'image': if (is_numeric($field_value)) { $field_value = wp_get_attachment_url($field_value); } elseif (is_array($field_value)) { $field_value = $field_value['url']; } break; case 'gallery': $v = array(); foreach ($field_value as $key => $item) { $v[] = $item['url']; } $field_value = implode(",", $v); break; case 'location-field': $localion_parts = explode("|", $field_value); if (!empty($localion_parts)) { $xmlWriter->startElement($element_name); $xmlWriter->startElement('address'); $xmlWriter->writeCData($localion_parts[0]); $xmlWriter->endElement(); if (!empty($localion_parts[1])) { $coordinates = explode(",", $localion_parts[1]); if (!empty($coordinates)) { $xmlWriter->startElement('lat'); $xmlWriter->writeCData($coordinates[0]); $xmlWriter->endElement(); $xmlWriter->startElement('lng'); $xmlWriter->writeCData($coordinates[1]); $xmlWriter->endElement(); } } $xmlWriter->endElement(); } $put_to_xml = false; break; case 'paypal_item': $xmlWriter->startElement($element_name); if (is_array($field_value)) { foreach ($field_value as $key => $value) { $xmlWriter->startElement($key); $xmlWriter->writeCData($value); $xmlWriter->endElement(); } } $xmlWriter->endElement(); $put_to_xml = false; break; case 'google_map': $xmlWriter->startElement($element_name); $xmlWriter->startElement('address'); $xmlWriter->writeCData($field_value['address']); $xmlWriter->endElement(); $xmlWriter->startElement('lat'); $xmlWriter->writeCData($field_value['lat']); $xmlWriter->endElement(); $xmlWriter->startElement('lng'); $xmlWriter->writeCData($field_value['lng']); $xmlWriter->endElement(); $xmlWriter->endElement(); $put_to_xml = false; break; case 'acf_cf7': case 'gravity_forms_field': if (!empty($field_options['multiple'])) { $field_value = implode(",", $field_value); } break; case 'page_link': if (is_array($field_value)) { $field_value = implode(",", $field_value); } break; case 'post_object': if (!empty($field_options['multiple'])) { $v = array(); foreach ($field_value as $key => $pid) { if (is_numeric($pid)) { $entry = get_post($pid); if ($entry) { $v[] = $entry->post_name; } } else { $v[] = $pid->post_name; } } $field_value = implode(",", $v); } else { if (is_numeric($field_value)) { $entry = get_post($field_value); if ($entry) { $field_value = $entry->post_name; } } else { $field_value = $field_value->post_name; } } break; case 'relationship': $v = array(); foreach ($field_value as $key => $pid) { $entry = get_post($pid); if ($entry) { $v[] = $entry->post_title; } } $field_value = implode(",", $v); break; case 'user': if (!empty($field_options['multiple'])) { $v = array(); foreach ($field_value as $key => $user) { if (is_numeric($user)) { $entry = get_user_by('ID', $user); if ($entry) { $v[] = $entry->user_email; } } else { $v[] = $user['user_email']; } } $field_value = implode(",", $v); } else { if (is_numeric($field_value)) { $entry = get_user_by('ID', $field_value); if ($entry) { $field_value = $entry->user_email; } } else { $field_value = $field_value['user_email']; } } break; case 'taxonomy': $xmlWriter->startElement($element_name); if (!in_array($field_options['field_type'], array('radio', 'select'))) { foreach ($field_value as $key => $tid) { $entry = get_term($tid, $field_options['taxonomy']); if ($entry and !is_wp_error($entry)) { $xmlWriter->startElement('term'); $xmlWriter->writeCData($entry->slug); $xmlWriter->endElement(); } } } else { $entry = get_term($field_value, $field_options['taxonomy']); if ($entry) { $xmlWriter->startElement('term'); $xmlWriter->writeCData($entry->slug); $xmlWriter->endElement(); } } $xmlWriter->endElement(); $put_to_xml = false; break; case 'select': if (!empty($field_options['multiple'])) { $field_value = implode(",", $field_value); } break; case 'checkbox': $field_value = implode(",", $field_value); break; case 'repeater': $xmlWriter->startElement($element_name); if (have_rows($field_name, $recordID)) { while (have_rows($field_name, $recordID)) { the_row(); $row = acf_get_row(); //$xmlWriter->startElementNs('key_' . $row['i'], 'row', null); $xmlWriter->startElement('row'); foreach ($row['field']['sub_fields'] as $sub_field) { // get $v = acf_format_value($row['value'][$row['i']][$sub_field['key']], $row['post_id'], $sub_field); pmxe_export_acf_field_xml($v, $sub_field, false, $recordID, $xmlWriter, $sub_field['name'], '', ''); } $xmlWriter->endElement(); } } $xmlWriter->endElement(); $put_to_xml = false; break; case 'flexible_content': $xmlWriter->startElement($element_name); // check if the flexible content field has rows of data if (have_rows($field_name)) { // loop through the rows of data while (have_rows($field_name)) { the_row(); $row = acf_get_row(); foreach ($row['field']['layouts'] as $layout) { if ($layout['name'] == $row['value'][$row['i']]['acf_fc_layout']) { $xmlWriter->startElement($row['value'][$row['i']]['acf_fc_layout'] . '_' . $row['i']); foreach ($layout['sub_fields'] as $sub_field) { if (isset($row['value'][$row['i']][$sub_field['key']])) { // get $v = acf_format_value($row['value'][$row['i']][$sub_field['key']], $row['post_id'], $sub_field); pmxe_export_acf_field_xml($v, $sub_field, false, $recordID, $xmlWriter, $sub_field['name'], '', ''); } } $xmlWriter->endElement(); } } } } else { // no layouts found } $xmlWriter->endElement(); $put_to_xml = false; break; default: break; } if ($put_to_xml) { $xmlWriter->startElement($element_name); $xmlWriter->writeCData(apply_filters('pmxe_acf_field', pmxe_filter(maybe_serialize($field_value), $fieldSnipped), $field_name, $recordID)); $xmlWriter->endElement(); } } }
protected function prepare_export_data($record, $options, $elId) { // an array with data to export $data = array('items' => array(), 'taxes' => array(), 'shipping' => array(), 'coupons' => array(), 'surcharge' => array()); global $wpdb; $table_prefix = $wpdb->prefix; if (empty($this->order_id) or $this->order_id != $record->ID) { $this->order_id = $record->ID; $all_order_items = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_items WHERE order_id = {$record->ID}"); if (!empty($all_order_items)) { foreach ($all_order_items as $item) { switch ($item->order_item_type) { case 'line_item': $this->order_items[] = $item; break; case 'tax': $this->order_taxes[] = $item; break; case 'shipping': $this->order_shipping[] = $item; break; case 'coupon': $this->order_coupons[] = $item; break; case 'fee': $this->order_surcharge[] = $item; break; } } } } if (!empty($options['cc_value'][$elId])) { $fieldSnipped = (!empty($options['cc_php'][$elId]) and !empty($options['cc_code'][$elId])) ? $options['cc_code'][$elId] : false; switch ($options['cc_options'][$elId]) { case 'order': case 'customer': $data[$options['cc_name'][$elId]] = strpos($options['cc_value'][$elId], "_") === 0 ? get_post_meta($record->ID, $options['cc_value'][$elId], true) : $record->{$options}['cc_value'][$elId]; if ($options['cc_value'][$elId] == "post_title") { $data[$options['cc_name'][$elId]] = str_replace("–", '-', $data[$options['cc_name'][$elId]]); } break; case 'items': if (!empty($this->order_items)) { foreach ($this->order_items as $n => $order_item) { $meta_data = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_item->order_item_id}", ARRAY_A); $item_data = array(); foreach ($options['ids'] as $subID => $subvalue) { if ($options['cc_type'][$subID] == 'woo_order' and $options['cc_options'][$subID] == 'items') { $element_name = $options['cc_name'][$subID] . ' #' . ($n + 1); $fieldSnipped = (!empty($options['cc_php'][$subID]) and !empty($options['cc_code'][$subID])) ? $options['cc_code'][$subID] : false; switch ($options['cc_value'][$subID]) { case '_product_id': case '__product_sku': case '__product_title': $product_id = ''; $variation_id = ''; foreach ($meta_data as $meta) { if ($meta['meta_key'] == '_variation_id' and !empty($meta['meta_value'])) { $variation_id = $meta['meta_value']; } if ($meta['meta_key'] == '_product_id' and !empty($meta['meta_value'])) { $product_id = $meta['meta_value']; } } $_product_id = empty($variation_id) ? $product_id : $variation_id; switch ($options['cc_value'][$subID]) { case '_product_id': $item_data[$element_name] = pmxe_filter($_product_id, $fieldSnipped); break; case '__product_sku': $item_data[$element_name] = pmxe_filter(get_post_meta($_product_id, '_sku', true), $fieldSnipped); break; case '__product_title': $_product = get_post($_product_id); $item_data[$element_name] = !is_null($_product) ? pmxe_filter($_product->post_title, $fieldSnipped) : ''; break; } break; case '__product_variation': $variations = array(); foreach ($meta_data as $meta) { if (strpos($meta['meta_key'], "pa_") === 0) { $variations[str_replace("pa_", "", $meta['meta_key'])] = $meta['meta_value']; } } if (!empty($variations)) { foreach ($variations as $key => $value) { $item_data[$element_name . " (" . sanitize_title($key) . ")"] = pmxe_filter($value, $fieldSnipped); } } break; case '_line_subtotal': $_line_total = 0; $_qty = 0; foreach ($meta_data as $meta) { if ($meta['meta_key'] == '_line_total') { $_line_total = $meta['meta_value']; } if ($meta['meta_key'] == '_qty') { $_qty = $meta['meta_value']; } } $_line_subtotal = $_qty ? number_format($_line_total / $_qty, 2) : 0; $item_data[$element_name] = pmxe_filter($_line_subtotal, $fieldSnipped); break; default: foreach ($meta_data as $meta) { if ($meta['meta_key'] == $options['cc_value'][$subID]) { $item_data[$element_name] = pmxe_filter($meta['meta_value'], $fieldSnipped); } } break; } } } if (!empty($item_data)) { $data['items'][] = $item_data; } } $this->order_items = null; } break; case 'taxes': // Order Taxes if (!empty($this->order_taxes)) { foreach ($this->order_taxes as $order_tax) { $meta_data = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_tax->order_item_id}", ARRAY_A); $rate_details = null; foreach ($meta_data as $meta) { if ($meta['meta_key'] == 'rate_id') { $rate_id = $meta['meta_value']; $rate_details = $wpdb->get_row("SELECT * FROM {$table_prefix}woocommerce_tax_rates WHERE tax_rate_id = {$rate_id}"); break; } } $tax_data = array(); foreach ($options['ids'] as $subID => $subvalue) { if ($options['cc_type'][$subID] == 'woo_order' and $options['cc_options'][$subID] == 'taxes') { $element_name = str_replace("per tax", $rate_details->tax_rate_name, $options['cc_name'][$subID]); $fieldSnipped = (!empty($options['cc_php'][$subID]) and !empty($options['cc_code'][$subID])) ? $options['cc_code'][$subID] : false; switch ($options['cc_value'][$subID]) { case 'tax_order_item_name': $tax_data[$element_name] = pmxe_filter($order_tax->order_item_name, $fieldSnipped); break; case 'tax_rate': $tax_data[$element_name] = pmxe_filter(!empty($rate_details) ? $rate_details->tax_rate : '', $fieldSnipped); break; case 'tax_amount': $tax_amount = 0; foreach ($meta_data as $meta) { if ($meta['meta_key'] == 'tax_amount' || $meta['meta_key'] == 'shipping_tax_amount') { $tax_amount += $meta['meta_value']; } } $tax_data[$element_name] = pmxe_filter($tax_amount, $fieldSnipped); break; } } } if (!empty($tax_data)) { $data['taxes'][] = $tax_data; } } $this->order_taxes = null; } // Calculate Total Tax Amount if ($options['cc_value'][$elId] == '_order_tax') { $_order_shipping_tax = get_post_meta($record->ID, '_order_shipping_tax', true); $_order_tax = get_post_meta($record->ID, '_order_tax', true); $_order_tax_total = $_order_shipping_tax + $_order_tax; $data[$options['cc_name'][$elId]] = pmxe_filter($_order_tax_total, $fieldSnipped); } if (!empty($this->order_shipping)) { foreach ($this->order_shipping as $order_ship) { $shipping_data = array(); foreach ($options['ids'] as $subID => $subvalue) { if ($options['cc_type'][$subID] == 'woo_order' and $options['cc_options'][$subID] == 'taxes') { $element_name = $options['cc_name'][$subID]; $fieldSnipped = (!empty($options['cc_php'][$subID]) and !empty($options['cc_code'][$subID])) ? $options['cc_code'][$subID] : false; switch ($options['cc_value'][$subID]) { case 'shipping_order_item_name': $shipping_data[$element_name] = pmxe_filter($order_ship->order_item_name, $fieldSnipped); break; case '_order_shipping': $shipping_data[$element_name] = pmxe_filter(get_post_meta($record->ID, '_order_shipping', true), $fieldSnipped); break; } } } if (!empty($shipping_data)) { $data['shipping'][] = $shipping_data; } } $this->order_shipping = null; } break; case 'fees': if (!empty($this->order_coupons)) { $this->__coupons_used = array(); foreach ($this->order_coupons as $order_coupon) { $this->__coupons_used[] = $order_coupon->order_item_name; $meta_data = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_coupon->order_item_id}", ARRAY_A); $coupons_data = array(); foreach ($options['ids'] as $subID => $subvalue) { if ($options['cc_type'][$subID] == 'woo_order' and $options['cc_options'][$subID] == 'fees') { $element_name = str_replace("per coupon", $order_coupon->order_item_name, $options['cc_name'][$subID]); $fieldSnipped = (!empty($options['cc_php'][$subID]) and !empty($options['cc_code'][$subID])) ? $options['cc_code'][$subID] : false; switch ($options['cc_value'][$subID]) { case 'discount_amount': foreach ($meta_data as $meta) { if ($meta['meta_key'] == 'discount_amount') { $coupons_data[$element_name] = pmxe_filter($meta['meta_value'] * -1, $fieldSnipped); break; } } break; } } } if (!empty($coupons_data)) { $data['coupons'][] = $coupons_data; } } $this->order_coupons = null; } // List of all coupons used if ($options['cc_value'][$elId] == '__coupons_used' and !is_null($this->__coupons_used)) { $implode_delimiter = $options['delimiter'] == ',' ? '|' : ','; $data[$options['cc_name'][$elId]] = pmxe_filter(implode($implode_delimiter, $this->__coupons_used), $fieldSnipped); } // Calculate Total Discount Amount if ($options['cc_value'][$elId] == '_cart_discount') { $_cart_discount = get_post_meta($record->ID, '_cart_discount', true); $data[$options['cc_name'][$elId]] = pmxe_filter($_cart_discount * -1, $fieldSnipped); } if (!empty($this->order_surcharge)) { $this->__total_fee_amount = 0; foreach ($this->order_surcharge as $order_surcharge) { $meta_data = $wpdb->get_results("SELECT * FROM {$table_prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_surcharge->order_item_id}", ARRAY_A); $surcharge_data = array(); foreach ($options['ids'] as $subID => $subvalue) { if ($options['cc_type'][$subID] == 'woo_order' and $options['cc_options'][$subID] == 'fees') { $element_name = str_replace("Amount (per surcharge)", "(" . $order_surcharge->order_item_name . ")", $options['cc_name'][$subID]); $fieldSnipped = (!empty($options['cc_php'][$subID]) and !empty($options['cc_code'][$subID])) ? $options['cc_code'][$subID] : false; switch ($options['cc_value'][$subID]) { case 'fee_line_total': foreach ($meta_data as $meta) { if ($meta['meta_key'] == '_line_total') { $surcharge_data[$element_name] = pmxe_filter($meta['meta_value'], $fieldSnipped); $this->__total_fee_amount += $meta['meta_value']; break; } } break; } } } if (!empty($surcharge_data)) { $data['surcharge'][] = $surcharge_data; } } $this->order_surcharge = null; } // Total Fee Amount if ($options['cc_value'][$elId] == '__total_fee_amount' and !is_null($this->__total_fee_amount)) { $data[$options['cc_name'][$elId]] = $this->__total_fee_amount; } break; } } return $data; }
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; } }
protected function prepare_export_data($record, $options, $elId) { $data = array(); $element_value = str_replace("item_data__", "", $options['cc_value'][$elId]); if (!empty($element_value)) { $implode_delimiter = XmlExportEngine::$implode; $element_name = !empty($options['cc_name'][$elId]) ? $options['cc_name'][$elId] : 'untitled_' . $elId; $fieldSnipped = (!empty($options['cc_php'][$elId]) and !empty($options['cc_code'][$elId])) ? $options['cc_code'][$elId] : false; switch ($element_value) { case 'attributes': $_product_attributes = empty($record->post_parent) ? get_post_meta($record->ID, '_product_attributes', true) : get_post_meta($record->post_parent, '_product_attributes', true); if (empty(self::$_existing_attributes)) { global $wp_taxonomies; foreach ($wp_taxonomies as $key => $obj) { if (in_array($obj->name, array('nav_menu'))) { continue; } if (strpos($obj->name, "pa_") === 0 and strlen($obj->name) > 3 and !in_array($obj->name, self::$_existing_attributes)) { self::$_existing_attributes[] = $obj->name; } } } // combine taxonomies attributes if (!empty(self::$_existing_attributes)) { foreach (self::$_existing_attributes as $taxonomy_slug) { $taxonomy = get_taxonomy($taxonomy_slug); $data['Attribute Name (' . $taxonomy_slug . ')'] = $taxonomy->labels->name; $data['Attribute In Variations (' . $taxonomy_slug . ')'] = !empty($_product_attributes[$taxonomy_slug]['is_variation']) ? "yes" : "no"; $data['Attribute Is Visible (' . $taxonomy_slug . ')'] = !empty($_product_attributes[$taxonomy_slug]['is_visible']) ? "yes" : "no"; $data['Attribute Is Taxonomy (' . $taxonomy_slug . ')'] = !empty($_product_attributes[$taxonomy_slug]['is_taxonomy']) ? "yes" : "no"; $element_name = 'Attribute Value (' . $taxonomy_slug . ')'; if ($record->post_parent == 0) { $txes_list = get_the_terms($record->ID, $taxonomy_slug); if (!is_wp_error($txes_list) and !empty($txes_list)) { $attr_new = array(); foreach ($txes_list as $t) { $attr_new[] = $t->name; } $data[$element_name] = apply_filters('pmxe_woo_attribute', pmxe_filter(implode($implode_delimiter, $attr_new), $fieldSnipped), $record->ID, $taxonomy_slug); } else { $data[$element_name] = pmxe_filter('', $fieldSnipped); } } else { $variation_attribute = get_post_meta($record->ID, 'attribute_' . $taxonomy_slug, true); $data[$element_name] = apply_filters('pmxe_woo_attribute', pmxe_filter($variation_attribute, $fieldSnipped), $record->ID, $taxonomy_slug); } } } // combine custom attributes if (!empty(self::$products_data['attributes'])) { foreach (self::$products_data['attributes'] as $attribute) { $attribute_standart_name = str_replace('attribute_', '', $attribute->meta_key); $attribute_name = ucfirst($attribute_standart_name); $data['Attribute Name (' . $attribute_name . ')'] = $attribute_name; $data['Attribute Value (' . $attribute_name . ')'] = get_post_meta($record->ID, $attribute->meta_key, true); $data['Attribute In Variations (' . $attribute_name . ')'] = !empty($_product_attributes[$attribute_standart_name]['is_variation']) ? "yes" : "no"; $data['Attribute Is Visible (' . $attribute_name . ')'] = !empty($_product_attributes[$attribute_standart_name]['is_visible']) ? "yes" : "no"; $data['Attribute Is Taxonomy (' . $attribute_name . ')'] = !empty($_product_attributes[$attribute_standart_name]['is_taxonomy']) ? "yes" : "no"; } } break; default: $cur_meta_values = get_post_meta($record->ID, $element_value); if (!empty($cur_meta_values) and is_array($cur_meta_values)) { foreach ($cur_meta_values as $key => $cur_meta_value) { $fieldOptions = $options['cc_options'][$elId]; $fieldSettings = empty($options['cc_settings'][$elId]) ? $fieldOptions : $options['cc_settings'][$elId]; switch ($element_value) { 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']; } } $data[$element_name . ' Paths'] = pmxe_filter(implode($implode_delimiter, $file_paths), $fieldSnipped); $data[$element_name . ' Names'] = pmxe_filter(implode($implode_delimiter, $file_names), $fieldSnipped); break; case '_crosssell_ids': case '_upsell_ids': $sell_export_type = empty($options['cc_settings'][$elId]) ? 'sku' : $options['cc_settings'][$elId]; $_ids = maybe_unserialize($cur_meta_value); $_values = array(); if (!empty($_ids)) { switch ($sell_export_type) { case 'sku': foreach ($_ids as $_id) { $_values[] = get_post_meta($_id, '_sku', true); } break; case 'id': $_values = $_ids; break; case 'name': foreach ($_ids as $_id) { $p = get_post($_id); if ($p) { $_values[] = $p->post_name; } } break; default: # code... break; } } $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter(implode($implode_delimiter, $_values), $fieldSnipped), $element_value, $record->ID); break; case '_sale_price_dates_from': case '_sale_price_dates_to': if (!empty($fieldSettings)) { switch ($fieldSettings) { case 'unix': $post_date = $cur_meta_value; break; default: $post_date = date($fieldSettings, $cur_meta_value); break; } } else { $post_date = $cur_meta_value; } $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter($post_date, $fieldSnipped), $element_value, $record->ID); // wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_post_date', pmxe_filter($post_date, $fieldSnipped), $record->ID) ); break; case '_tax_class': if ($cur_meta_value == '') { $tax_status = get_post_meta($record->ID, '_tax_status', true); if ('taxable' == $tax_status) { $cur_meta_value = 'standard'; } } $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter($cur_meta_value, $fieldSnipped), $element_value, $record->ID); break; default: if (empty($data[$element_name])) { $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $element_value, $record->ID); } else { $data[$element_name . '_' . $key] = apply_filters('pmxe_woo_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $element_value, $record->ID); } break; } } } if (empty($cur_meta_values)) { $data[$element_name] = apply_filters('pmxe_woo_field', pmxe_filter('', $fieldSnipped), $element_value, $record->ID); } break; } } return $data; }
protected function prepare_export_data($record, $options, $elId) { // an array with data to export $data = array(); global $wpdb; $table_prefix = $wpdb->prefix; if (!empty($options['cc_value'][$elId])) { $fieldSnipped = (!empty($options['cc_php'][$elId]) and !empty($options['cc_code'][$elId])) ? $options['cc_code'][$elId] : false; switch ($options['cc_options'][$elId]) { case 'order': case 'customer': $data[$options['cc_name'][$elId]] = strpos($options['cc_value'][$elId], "_") === 0 ? get_post_meta($record->ID, $options['cc_value'][$elId], true) : $record->{$options}['cc_value'][$elId]; if ($options['cc_value'][$elId] == "post_title") { $data[$options['cc_name'][$elId]] = str_replace("–", '-', $data[$options['cc_name'][$elId]]); } $data[$options['cc_name'][$elId]] = pmxe_filter($data[$options['cc_name'][$elId]], $fieldSnipped); break; } } return $data; }
function pmxe_export_acf_field_csv($field_value, $exportOptions, $ID, $recordID, &$article, &$acfs, $element_name = '', $fieldSnipped = '', $group_id = '', $preview = false, $parent_field_name = '', $return_value = false) { $put_to_csv = true; $field_name = $ID ? $exportOptions['cc_label'][$ID] : $exportOptions['name']; $field_options = $ID ? unserialize($exportOptions['cc_options'][$ID]) : $exportOptions; if (!empty($field_value)) { global $acf; $field_value = maybe_unserialize($field_value); $implode_delimiter = $exportOptions['delimiter'] == ',' ? '|' : ','; // switch ACF field type switch ($field_options['type']) { case 'date_time_picker': case 'date_picker': $field_value = date('Ymd', strtotime($field_value)); break; case 'file': case 'image': if (is_numeric($field_value)) { $field_value = wp_get_attachment_url($field_value); } elseif (is_array($field_value)) { $field_value = $field_value['url']; } break; case 'gallery': $v = array(); foreach ($field_value as $key => $item) { $v[] = $item['url']; } $field_value = implode($implode_delimiter, $v); break; case 'location-field': $localion_parts = explode("|", $field_value); $acfs[] = $element_name . '_address'; $acfs[] = $element_name . '_lat'; $acfs[] = $element_name . '_lng'; if (!empty($localion_parts)) { $article[$element_name . '_address'] = $localion_parts[0]; if (!empty($localion_parts[1])) { $coordinates = explode(",", $localion_parts[1]); if (!empty($coordinates)) { $article[$element_name . '_lat'] = $coordinates[0]; $article[$element_name . '_lng'] = $coordinates[1]; } } } $put_to_csv = false; break; case 'paypal_item': $acfs[] = $element_name . '_item_name'; $acfs[] = $element_name . '_item_description'; $acfs[] = $element_name . '_price'; if (is_array($field_value)) { foreach ($field_value as $key => $value) { $article[$element_name . '_' . $key] = $value; } } $put_to_csv = false; break; case 'google_map': $article[$element_name . '_address'] = $field_value['address']; $acfs[] = $element_name . '_address'; $article[$element_name . '_lat'] = $field_value['lat']; $acfs[] = $element_name . '_lat'; $article[$element_name . '_lng'] = $field_value['lng']; $acfs[] = $element_name . '_lng'; $put_to_csv = false; break; case 'acf_cf7': case 'gravity_forms_field': if (!empty($field_options['multiple'])) { $field_value = implode($implode_delimiter, $field_value); } break; case 'page_link': if (is_array($field_value)) { $field_value = implode($implode_delimiter, $field_value); } break; case 'post_object': if (!empty($field_options['multiple'])) { $v = array(); foreach ($field_value as $key => $pid) { if (is_numeric($pid)) { $entry = get_post($pid); if ($entry) { $v[] = $entry->post_name; } } else { $v[] = $pid->post_name; } } $field_value = implode($implode_delimiter, $v); } else { if (is_numeric($field_value)) { $entry = get_post($field_value); if ($entry) { $field_value = $entry->post_name; } } else { $field_value = $field_value->post_name; } } break; case 'relationship': $v = array(); foreach ($field_value as $key => $pid) { $entry = get_post($pid); if ($entry) { $v[] = $entry->post_title; } } $field_value = implode($implode_delimiter, $v); break; case 'user': if (!empty($field_options['multiple'])) { $v = array(); foreach ($field_value as $key => $user) { if (is_numeric($user)) { $entry = get_user_by('ID', $user); if ($entry) { $v[] = $entry->user_email; } } else { $v[] = $user['user_email']; } } $field_value = implode($implode_delimiter, $v); } else { if (is_numeric($field_value)) { $entry = get_user_by('ID', $field_value); if ($entry) { $field_value = $entry->user_email; } } else { $field_value = $field_value['user_email']; } } break; case 'taxonomy': if (!in_array($field_options['field_type'], array('radio', 'select'))) { $v = array(); foreach ($field_value as $key => $tid) { $entry = get_term($tid, $field_options['taxonomy']); if ($entry and !is_wp_error($entry)) { $v[] = $entry->name; } } $field_value = implode($implode_delimiter, $v); } else { $entry = get_term($field_value, $field_options['taxonomy']); if ($entry) { $field_value = $entry->name; } } break; case 'select': if (!empty($field_options['multiple'])) { $field_value = implode($implode_delimiter, $field_value); } break; case 'checkbox': $field_value = implode($implode_delimiter, $field_value); break; case 'repeater': if (have_rows($field_name, $recordID)) { $repeater_element_name = empty($ID) ? $parent_field_name : $element_name; if (!empty($ID)) { $acfs[$repeater_element_name] = array(); } $rowValues = array(); while (have_rows($field_name, $recordID)) { the_row(); $row = XmlExportACF::acf_get_row(); foreach ($row['field']['sub_fields'] as $sub_field) { if ($acf and version_compare($acf->settings['version'], '5.0.0') >= 0) { // get $v = $row['value'][$row['i']][$sub_field['key']]; //acf_format_value($row['value'][ $row['i'] ][ $sub_field['key'] ], $row['post_id'], $sub_field); } else { $v = get_sub_field($sub_field['name']); } $sub_field['delimiter'] = $exportOptions['delimiter']; switch ($sub_field['type']) { case 'repeater': pmxe_export_acf_field_csv($v, $sub_field, false, $recordID, $article, $acfs, str_replace('acf' . $group_id, '', $element_name) . '_' . $sub_field['name'], '', '', false, $element_name); break; case 'google_map': case 'paypal_item': case 'location-field': $rowValues[$sub_field['name']][] = is_array($v) ? implode($exportOptions['delimiter'], $v) : $v; break; default: $sub_field_value = pmxe_export_acf_field_csv($v, $sub_field, false, $recordID, $article, $acfs, str_replace('acf' . $group_id, '', $element_name) . '_' . $sub_field['name'], '', '', false, $element_name, true); $rowValues[$sub_field['name']][] = is_array($sub_field_value) ? implode($exportOptions['delimiter'], $sub_field_value) : $sub_field_value; break; } // if ($sub_field['type'] == 'repeater') // { // $sub_field['delimiter'] = $exportOptions['delimiter']; // pmxe_export_acf_field_csv($v, $sub_field, false, $recordID, $article, $acfs, str_replace('acf' . $group_id, '', $element_name) . '_' . $sub_field['name'], '', '', false, $element_name); // } // else // { // $rowValues[$sub_field['name']][] = (is_array($v)) ? implode($exportOptions['delimiter'], $v) : $v; // } } } foreach ($rowValues as $key => $values) { $article[$element_name . '_' . $key] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars(implode($exportOptions['delimiter'], $values)))) : implode($exportOptions['delimiter'], $values); if (!in_array($element_name . '_' . $key, $acfs[$repeater_element_name])) { $acfs[$repeater_element_name][] = $element_name . '_' . $key; } } } $put_to_csv = false; break; case 'flexible_content': // check if the flexible content field has rows of data if (have_rows($field_name)) { // loop through the rows of data while (have_rows($field_name)) { the_row(); $row = XmlExportACF::acf_get_row(); foreach ($row['field']['layouts'] as $layout) { if ($layout['name'] == $row['value'][$row['i']]['acf_fc_layout']) { foreach ($layout['sub_fields'] as $sub_field) { if (isset($row['value'][$row['i']][$sub_field['key']])) { // get $v = $row['value'][$row['i']][$sub_field['key']]; //acf_format_value($row['value'][ $row['i'] ][ $sub_field['key'] ], $row['post_id'], $sub_field); $article[$element_name . '_' . $layout['name'] . '_' . $row['i'] . '_' . $sub_field['name']] = $v; $acfs[] = $element_name . '_' . $layout['name'] . '_' . $row['i'] . '_' . $sub_field['name']; //pmxe_export_acf_field_csv($v, $sub_field, false, $recordID, $article, $acfs, str_replace('acf' . $group_id, '', $element_name) . '_' . $row['value'][ $row['i'] ]['acf_fc_layout'] . '_' . $row['i'] . '_' . $sub_field['name'], '', '', true); } } } } } } else { // no layouts found } $put_to_csv = false; break; default: break; } } if ($return_value) { return $field_value; } if ($put_to_csv) { switch ($field_options['type']) { case 'repeater': global $acf; if ($acf->settings['version'] and version_compare($acf->settings['version'], '5.0.0') >= 0) { $acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $field_options['ID'], 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC')); if (!empty($acf_fields)) { foreach ($acf_fields as $field) { $sub_name = $element_name . '_' . $field->post_excerpt; if (!in_array($sub_name, $acfs)) { $acfs[] = $sub_name; } } } } break; case 'google_map': case 'location-field': $acfs[] = $element_name . '_address'; $acfs[] = $element_name . '_lat'; $acfs[] = $element_name . '_lng'; break; case 'paypal_item': $acfs[] = $element_name . '_item_name'; $acfs[] = $element_name . '_item_description'; $acfs[] = $element_name . '_price'; break; default: $val = apply_filters('pmxe_acf_field', pmxe_filter(!empty($field_value) ? maybe_serialize($field_value) : '', $fieldSnipped), $field_name, $recordID); $article[$element_name] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val; $acfs[] = $element_name; break; } } }
public static function export_acf_field($field_value = '', $exportOptions, $ID, $pid, &$article, $xmlWriter = false, &$acfs, $element_name = '', $element_name_ns = '', $fieldSnipped = '', $group_id = '', $preview = false, $return_value = false) { global $acf; $put_to_csv = true; $field_name = $ID ? $exportOptions['cc_label'][$ID] : $exportOptions['name']; $field_options = $ID ? unserialize($exportOptions['cc_options'][$ID]) : $exportOptions; $field_settings = $ID ? json_decode($exportOptions['cc_settings'][$ID], true) : false; $is_xml_export = $xmlWriter and XmlExportEngine::$exportOptions['export_to'] == 'xml'; if (!empty($field_value)) { $field_value = maybe_unserialize($field_value); $implode_delimiter = (isset($exportOptions['delimiter']) and $exportOptions['delimiter'] == ',') ? '|' : ','; switch ($field_options['type']) { case 'date_time_picker': case 'date_picker': $field_value = date('Ymd', strtotime($field_value)); break; case 'file': case 'image': if (is_numeric($field_value)) { $field_value = wp_get_attachment_url($field_value); } elseif (is_array($field_value)) { $field_value = $field_value['url']; } break; case 'gallery': $v = array(); foreach ($field_value as $key => $item) { $v[] = $item['url']; } $field_value = implode($implode_delimiter, $v); break; case 'location-field': $localion_parts = explode("|", $field_value); if ($is_xml_export) { if (!empty($localion_parts)) { $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->startElement('address'); $xmlWriter->writeData($localion_parts[0]); $xmlWriter->endElement(); if (!empty($localion_parts[1])) { $coordinates = explode(",", $localion_parts[1]); if (!empty($coordinates)) { $xmlWriter->startElement('lat'); $xmlWriter->writeData($coordinates[0]); $xmlWriter->endElement(); $xmlWriter->startElement('lng'); $xmlWriter->writeData($coordinates[1]); $xmlWriter->endElement(); } } $xmlWriter->endElement(); } } else { if (!$return_value) { $acfs[$element_name] = array($element_name . '_address', $element_name . '_lat', $element_name . '_lng'); if (!empty($localion_parts)) { $article[$element_name . '_address'] = $localion_parts[0]; if (!empty($localion_parts[1])) { $coordinates = explode(",", $localion_parts[1]); if (!empty($coordinates)) { $article[$element_name . '_lat'] = $coordinates[0]; $article[$element_name . '_lng'] = $coordinates[1]; } } } } else { if (!empty($localion_parts)) { $return_value = array('address' => $localion_parts[0]); if (!empty($localion_parts[1])) { $coordinates = explode(",", $localion_parts[1]); if (!empty($coordinates)) { $return_value['lat'] = $coordinates[0]; $return_value['lng'] = $coordinates[1]; } } } } } $put_to_csv = false; break; case 'paypal_item': if ($is_xml_export) { $xmlWriter->beginElement($element_name_ns, $element_name, null); if (is_array($field_value)) { foreach ($field_value as $key => $value) { $xmlWriter->beginElement($element_name_ns, $key, null); $xmlWriter->writeData($value); $xmlWriter->endElement(); } } $xmlWriter->endElement(); } else { if (!$return_value) { $acfs[$element_name] = array($element_name . '_item_name', $element_name . '_item_description', $element_name . '_price'); if (is_array($field_value)) { foreach ($field_value as $key => $value) { $article[$element_name . '_' . $key] = $value; } } } } $put_to_csv = false; break; case 'google_map': if ($is_xml_export) { $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->startElement('address'); $xmlWriter->writeData($field_value['address']); $xmlWriter->endElement(); $xmlWriter->startElement('lat'); $xmlWriter->writeData($field_value['lat']); $xmlWriter->endElement(); $xmlWriter->startElement('lng'); $xmlWriter->writeData($field_value['lng']); $xmlWriter->endElement(); $xmlWriter->endElement(); } else { if (!$return_value) { $acfs[$element_name] = array($element_name . '_address', $element_name . '_lat', $element_name . '_lng'); $article[$element_name . '_address'] = $field_value['address']; $article[$element_name . '_lat'] = $field_value['lat']; $article[$element_name . '_lng'] = $field_value['lng']; } } $put_to_csv = false; break; case 'acf_cf7': case 'gravity_forms_field': if (!empty($field_options['multiple'])) { $field_value = implode($implode_delimiter, $field_value); } break; case 'page_link': if (is_array($field_value)) { $field_value = implode($implode_delimiter, $field_value); } break; case 'post_object': if (!empty($field_options['multiple'])) { $v = array(); foreach ($field_value as $key => $pid) { if (is_numeric($pid)) { $entry = get_post($pid); if ($entry) { $v[] = $entry->post_name; } } else { $v[] = $pid->post_name; } } $field_value = implode($implode_delimiter, $v); } else { if (is_numeric($field_value)) { $entry = get_post($field_value); if ($entry) { $field_value = $entry->post_name; } } else { $field_value = $field_value->post_name; } } break; case 'relationship': $v = array(); foreach ($field_value as $key => $pid) { $entry = get_post($pid); if ($entry) { $v[] = $entry->post_title; } } $field_value = implode($implode_delimiter, $v); break; case 'user': if (!empty($field_options['multiple'])) { $v = array(); foreach ($field_value as $key => $user) { if (is_numeric($user)) { $entry = get_user_by('ID', $user); if ($entry) { $v[] = $entry->user_email; } } else { $v[] = $user['user_email']; } } $field_value = implode($implode_delimiter, $v); } else { if (is_numeric($field_value)) { $entry = get_user_by('ID', $field_value); if ($entry) { $field_value = $entry->user_email; } } else { $field_value = $field_value['user_email']; } } break; case 'taxonomy': if ($is_xml_export) { $xmlWriter->beginElement($element_name_ns, $element_name, null); if (!in_array($field_options['field_type'], array('radio', 'select'))) { foreach ($field_value as $key => $tid) { $entry = get_term($tid, $field_options['taxonomy']); if ($entry and !is_wp_error($entry)) { $xmlWriter->startElement('term'); $xmlWriter->writeData($entry->name); $xmlWriter->endElement(); } } } else { $entry = get_term($field_value, $field_options['taxonomy']); if ($entry) { $xmlWriter->startElement('term'); $xmlWriter->writeData($entry->name); $xmlWriter->endElement(); } } $xmlWriter->endElement(); $put_to_csv = false; } else { if (!in_array($field_options['field_type'], array('radio', 'select'))) { $v = array(); foreach ($field_value as $key => $tid) { $entry = get_term($tid, $field_options['taxonomy']); if ($entry and !is_wp_error($entry)) { $v[] = $entry->name; } } $field_value = implode($implode_delimiter, $v); } else { $entry = get_term($field_value, $field_options['taxonomy']); if ($entry) { $field_value = $entry->name; } } } break; case 'select': if (!empty($field_options['multiple'])) { $field_value = implode($implode_delimiter, $field_value); } break; case 'checkbox': if (is_array($field_value)) { $field_value = implode($implode_delimiter, $field_value); } break; case 'repeater': if ($is_xml_export) { $xmlWriter->beginElement($element_name_ns, $element_name, null); } if (have_rows($field_name, $pid)) { $rowValues = array(); $repeater_sub_field_names = array(); while (have_rows($field_name, $pid)) { the_row(); $row = self::acf_get_row(); if ($is_xml_export) { $xmlWriter->startElement('row'); } foreach ($row['field']['sub_fields'] as $sub_field) { if ($acf and version_compare($acf->settings['version'], '5.0.0') >= 0) { $v = $row['value'][$row['i']][$sub_field['key']]; if ($is_xml_export) { $v = acf_format_value($v, $row['post_id'], $sub_field); } } else { $v = get_sub_field($sub_field['name']); } $sub_field['delimiter'] = $implode_delimiter; $sub_field_value = self::export_acf_field($v, $sub_field, false, $pid, $article, $xmlWriter, $acfs, $is_xml_export ? $sub_field['name'] : $element_name . '_' . $sub_field['name'], $element_name_ns, $fieldSnipped, '', $preview, $is_xml_export ? false : true); if (!$is_xml_export) { switch ($sub_field['type']) { case 'google_map': case 'paypal_item': case 'location-field': case 'repeater': if (!empty($sub_field_value)) { foreach ($sub_field_value as $repeater_key => $repeater_value) { $rowValues[$sub_field['name']][$repeater_key][] = is_array($repeater_value) ? implode($exportOptions['delimiter'], $repeater_value) : $repeater_value; } } break; default: $rowValues[$sub_field['name']][] = apply_filters('pmxe_acf_field', pmxe_filter(is_array($sub_field_value) ? implode($exportOptions['delimiter'], $sub_field_value) : $sub_field_value, $fieldSnipped), $sub_field['name'], $pid); break; } } } if ($is_xml_export) { $xmlWriter->endElement(); } } if ($return_value) { return $rowValues; } if (!$is_xml_export) { $additional_articles = array(); foreach ($rowValues as $key => $values) { $is_have_subvalues = array_filter(array_keys($values), 'is_numeric'); if (empty($is_have_subvalues)) { foreach ($values as $subkey => $subvalue) { if (!in_array($element_name . '_' . $key . '_' . $subkey, $repeater_sub_field_names)) { $repeater_sub_field_names[] = $element_name . '_' . $key . '_' . $subkey; } // Display each repeater row in its own csv line if (!empty($field_settings) and $field_settings['repeater_field_item_per_line']) { $base_value = array_shift($subvalue); $article[$element_name . '_' . $key . '_' . $subkey] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($base_value))) : $base_value; if (!empty($subvalue)) { foreach ($subvalue as $i => $addRowValue) { $additional_articles[$i]['settings'] = $field_settings; $additional_articles[$i]['content'][$element_name . '_' . $key . '_' . $subkey] = $addRowValue; } } } else { $article[$element_name . '_' . $key . '_' . $subkey] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars(implode($implode_delimiter, $subvalue)))) : implode($implode_delimiter, $subvalue); } } } else { if (!in_array($element_name . '_' . $key, $repeater_sub_field_names)) { $repeater_sub_field_names[] = $element_name . '_' . $key; } // Display each repeater row in its own csv line if (!empty($field_settings) and $field_settings['repeater_field_item_per_line']) { $base_value = array_shift($values); $article[$element_name . '_' . $key] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($base_value))) : $base_value; if (!empty($values)) { foreach ($values as $i => $addRowValue) { $additional_articles[$i]['settings'] = $field_settings; $additional_articles[$i]['content'][$element_name . '_' . $key] = $addRowValue; } } } else { $article[$element_name . '_' . $key] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars(implode($implode_delimiter, $values)))) : implode($implode_delimiter, $values); } } } if (!empty($repeater_sub_field_names)) { $acfs[$element_name] = $repeater_sub_field_names; } if (!empty($additional_articles)) { foreach ($additional_articles as $i => $additional_article) { self::$additional_articles[] = $additional_article; } } } } if ($is_xml_export) { $xmlWriter->endElement(); } $put_to_csv = false; break; case 'flexible_content': $fc_sub_field_names = array(); if ($is_xml_export) { $xmlWriter->beginElement($element_name_ns, $element_name, null); } // check if the flexible content field has rows of data if (have_rows($field_name)) { // loop through the rows of data while (have_rows($field_name)) { the_row(); $row = self::acf_get_row(); foreach ($row['field']['layouts'] as $layout) { if ($layout['name'] == $row['value'][$row['i']]['acf_fc_layout']) { if ($is_xml_export) { $xmlWriter->startElement($row['value'][$row['i']]['acf_fc_layout'] . '_' . $row['i']); } foreach ($layout['sub_fields'] as $sub_field) { $layout_field_name = $element_name . '_' . $layout['name'] . '_' . $row['i']; $v = ''; if (isset($row['value'][$row['i']][$sub_field['key']])) { $v = $row['value'][$row['i']][$sub_field['key']]; if ($is_xml_export) { $v = acf_format_value($v, $row['post_id'], $sub_field); } } $sub_field['delimiter'] = $implode_delimiter; $sub_field_values = self::export_acf_field($v, $sub_field, false, $pid, $article, $xmlWriter, $acfs, $is_xml_export ? $sub_field['name'] : $layout_field_name . '_' . $sub_field['name'], $element_name_ns, $fieldSnipped, '', $preview, $is_xml_export ? false : true); if (!$is_xml_export) { switch ($sub_field['type']) { case 'repeater': if (!empty($sub_field_values)) { foreach ($sub_field_values as $key => $values) { $article[$layout_field_name . '_' . $key] = $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars(implode($implode_delimiter, $values)))) : implode($implode_delimiter, $values); if (!in_array($layout_field_name . '_' . $key, $fc_sub_field_names)) { $fc_sub_field_names[] = $layout_field_name . '_' . $key; } } } break; default: $article[$layout_field_name . '_' . $sub_field['name']] = $v; if (!in_array($layout_field_name . '_' . $sub_field['name'], $fc_sub_field_names)) { $fc_sub_field_names[] = $layout_field_name . '_' . $sub_field['name']; } break; } } } if ($is_xml_export) { $xmlWriter->endElement(); } } } } } else { // no layouts found } if ($is_xml_export) { $xmlWriter->endElement(); } if (!empty($fc_sub_field_names)) { $acfs[$element_name] = $fc_sub_field_names; } $put_to_csv = false; break; default: break; } } if ($return_value) { return $field_value; } if ($put_to_csv) { $val = apply_filters('pmxe_acf_field', pmxe_filter(!empty($field_value) ? maybe_serialize($field_value) : '', $fieldSnipped), $field_name, $pid); if ($is_xml_export) { $xmlWriter->beginElement($element_name_ns, $element_name, null); $xmlWriter->writeData($val); $xmlWriter->endElement(); } else { // $article[$element_name] = ($preview) ? trim(preg_replace('~[\r\n]+~', ' ', htmlspecialchars($val))) : $val; wp_all_export_write_article($article, $element_name, $preview ? trim(preg_replace('~[\\r\\n]+~', ' ', htmlspecialchars($val))) : $val); if (!isset($acfs[$element_name])) { $acfs[$element_name] = $element_name; } } } }
/** * 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; } }