function woo_ce_save_fields($dataset, $fields = array()) { if ($dataset && !empty($fields)) { $type = $dataset[0]; woo_ce_update_option($type . '_fields', $fields); } }
function woo_ce_get_line_ending() { $output = "\n"; $line_ending_formatting = woo_ce_get_option( 'line_ending_formatting', 'windows' ); if( $line_ending_formatting == false || $line_ending_formatting == '' ) { error_log( '[store-exporter-deluxe] Line ending formatting export option was corrupted, defaulted to windows' ); $line_ending_formatting = 'windows'; woo_ce_update_option( 'line_ending_formatting', 'windows' ); } switch( $line_ending_formatting ) { case 'windows': $output = "\n"; break; case 'mac': $output = "\r"; break; case 'unix': $output = "\n"; break; } return $output; }
function woo_ce_html_page() { global $wpdb, $woo_ce, $export; $title = apply_filters('woo_ce_template_header', ''); woo_ce_template_header($title); woo_ce_support_donate(); $action = woo_get_action(); switch ($action) { case 'export': $message = __('Chosen WooCommerce details have been exported from your store.', 'woo_ce'); $output = '<div class="updated settings-error"><p><strong>' . $message . '</strong></p></div>'; if (isset($woo_ce['debug']) && $woo_ce['debug']) { if (!isset($woo_ce['debug_log'])) { $woo_ce['debug_log'] = __('No export entries were found, please try again with different export filters.', 'woo_ce'); } $output .= '<h3>' . sprintf(__('Export Log: %s', 'woo_ce'), $export->filename) . '</h3>'; $output .= '<textarea id="export_log">' . $woo_ce['debug_log'] . '</textarea>'; } echo $output; woo_ce_manage_form(); break; case 'update': if (isset($_POST['custom_orders'])) { $custom_orders = $_POST['custom_orders']; $custom_orders = explode("\n", trim($custom_orders)); $size = count($custom_orders); if ($size) { for ($i = 0; $i < $size; $i++) { $custom_orders[$i] = trim($custom_orders[$i]); } woo_ce_update_option('custom_orders', $custom_orders); } } if (isset($_POST['custom_order_items'])) { $custom_order_items = $_POST['custom_order_items']; if (!empty($custom_order_items)) { $custom_order_items = explode("\n", trim($custom_order_items)); $size = count($custom_order_items); if ($size) { for ($i = 0; $i < $size; $i++) { $custom_order_items[$i] = trim($custom_order_items[$i]); } woo_ce_update_option('custom_order_items', $custom_order_items); } } else { woo_ce_update_option('custom_order_items', ''); } } $message = __('Custom Fields saved.', 'woo_ce'); $output = '<div class="updated settings-error"><p><strong>' . $message . '</strong></p></div>'; echo $output; woo_ce_manage_form(); break; default: woo_ce_manage_form(); break; } woo_ce_template_footer(); }
function woo_cd_html_page() { global $wpdb, $export; $title = apply_filters( 'woo_ce_template_header', __( 'Store Exporter Deluxe', 'woo_ce' ) ); woo_cd_template_header( $title ); $action = woo_get_action(); switch( $action ) { case 'export': if( WOO_CD_DEBUG ) { if( false === ( $export_log = get_transient( WOO_CD_PREFIX . '_debug_log' ) ) ) { $export_log = __( 'No export entries were found within the debug Transient, please try again with different export filters.', 'woo_ce' ); } else { $export_log = base64_decode( $export_log ); } delete_transient( WOO_CD_PREFIX . '_debug_log' ); $output = ' <h3>' . sprintf( __( 'Export Details: %s', 'woo_ce' ), esc_attr( $export->filename ) ) . '</h3> <p>' . __( 'This prints the $export global that contains the different export options and filters to help reproduce this on another instance of WordPress. Very useful for debugging blank or unexpected exports.', 'woo_ce' ) . '</p> <textarea id="export_log">' . esc_textarea( print_r( $export, true ) ) . '</textarea> <hr />'; if( in_array( $export->export_format, array( 'csv', 'xls' ) ) ) { $output .= ' <script> $j(function() { $j(\'#export_sheet\').CSVToTable(\'\', { startLine: 0'; if( in_array( $export->export_format, array( 'xls', 'xlsx' ) ) ) { $output .= ', separator: "\t"'; } $output .= ' }); }); </script> <h3>' . __( 'Export', 'woo_ce' ) . '</h3> <p>' . __( 'We use the <a href="http://code.google.com/p/jquerycsvtotable/" target="_blank"><em>CSV to Table plugin</em></a> to see first hand formatting errors or unexpected values within the export file.', 'woo_ce' ) . '</p> <div id="export_sheet">' . esc_textarea( $export_log ) . '</div> <p class="description">' . __( 'This jQuery plugin can fail with <code>\'Item count (#) does not match header count\'</code> notices which simply mean the number of headers detected does not match the number of cell contents.', 'woo_ce' ) . '</p> <hr />'; } $output .= ' <h3>' . __( 'Export Log', 'woo_ce' ) . '</h3> <p>' . __( 'This prints the raw export contents and is helpful when the jQuery plugin above fails due to major formatting errors.', 'woo_ce' ) . '</p> <textarea id="export_log" wrap="off">' . esc_textarea( $export_log ) . '</textarea> <hr /> '; echo $output; } woo_cd_manage_form(); break; case 'update': // Save Custom Product Meta if( isset( $_POST['custom_products'] ) ) { $custom_products = $_POST['custom_products']; $custom_products = explode( "\n", trim( $custom_products ) ); $size = count( $custom_products ); if( !empty( $size ) ) { for( $i = 0; $i < $size; $i++ ) $custom_products[$i] = sanitize_text_field( trim( $custom_products[$i] ) ); woo_ce_update_option( 'custom_products', $custom_products ); } } // Save Custom Attributes if( isset( $_POST['custom_attributes'] ) ) { $custom_attributes = $_POST['custom_attributes']; $custom_attributes = explode( "\n", trim( $custom_attributes ) ); $size = count( $custom_attributes ); if( !empty( $size ) ) { for( $i = 0; $i < $size; $i++ ) $custom_attributes[$i] = sanitize_text_field( trim( $custom_attributes[$i] ) ); woo_ce_update_option( 'custom_attributes', $custom_attributes ); } } // Save Custom Product Add-ons if( isset( $_POST['custom_product_addons'] ) ) { $custom_product_addons = $_POST['custom_product_addons']; $custom_product_addons = explode( "\n", trim( $custom_product_addons ) ); $size = count( $custom_product_addons ); if( !empty( $size ) ) { for( $i = 0; $i < $size; $i++ ) $custom_product_addons[$i] = sanitize_text_field( trim( $custom_product_addons[$i] ) ); woo_ce_update_option( 'custom_product_addons', $custom_product_addons ); } } // Save Custom Order Meta if( isset( $_POST['custom_orders'] ) ) { $custom_orders = $_POST['custom_orders']; $custom_orders = explode( "\n", trim( $custom_orders ) ); $size = count( $custom_orders ); if( $size ) { for( $i = 0; $i < $size; $i++ ) $custom_orders[$i] = sanitize_text_field( trim( $custom_orders[$i] ) ); woo_ce_update_option( 'custom_orders', $custom_orders ); } } // Save Custom Order Item Meta if( isset( $_POST['custom_order_items'] ) ) { $custom_order_items = $_POST['custom_order_items']; if( !empty( $custom_order_items ) ) { $custom_order_items = explode( "\n", trim( $custom_order_items ) ); $size = count( $custom_order_items ); if( $size ) { for( $i = 0; $i < $size; $i++ ) $custom_order_items[$i] = sanitize_text_field( trim( $custom_order_items[$i] ) ); woo_ce_update_option( 'custom_order_items', $custom_order_items ); } } else { woo_ce_update_option( 'custom_order_items', '' ); } } // Save Custom User Meta if( isset( $_POST['custom_users'] ) ) { $custom_users = $_POST['custom_users']; $custom_users = explode( "\n", trim( $custom_users ) ); $size = count( $custom_users ); if( $size ) { for( $i = 0; $i < $size; $i++ ) $custom_users[$i] = sanitize_text_field( trim( $custom_users[$i] ) ); woo_ce_update_option( 'custom_users', $custom_users ); } } // Save Custom Customer Meta if( isset( $_POST['custom_customers'] ) ) { $custom_customers = $_POST['custom_customers']; $custom_customers = explode( "\n", trim( $custom_customers ) ); $size = count( $custom_customers ); if( $size ) { for( $i = 0; $i < $size; $i++ ) $custom_customers[$i] = sanitize_text_field( trim( $custom_customers[$i] ) ); woo_ce_update_option( 'custom_customers', $custom_customers ); } } $message = __( 'Custom Fields saved. You can now select those additional fields from the Export Fields list.', 'woo_ce' ); woo_cd_admin_notice_html( $message ); woo_cd_manage_form(); break; default: woo_cd_manage_form(); break; } woo_cd_template_footer(); }
function woo_ce_save_fields($type = '', $fields = array(), $sorting = array()) { // Default fields if ($fields == false && !is_array($fields)) { $fields = array(); } $types = array_keys(woo_ce_return_export_types()); if (in_array($type, $types) && !empty($fields)) { woo_ce_update_option($type . '_fields', array_map('sanitize_text_field', $fields)); woo_ce_update_option($type . '_sorting', array_map('absint', $sorting)); } }
function woo_ce_cron_export( $gui = '', $type = '', $is_scheduled = false ) { global $export; $export = new stdClass; $export->cron = ( $is_scheduled ? 0 : 1 ); $export->scheduled_export = ( $is_scheduled ? 1 : 0 ); $export->start_time = time(); $export->idle_memory_start = woo_ce_current_memory_usage(); $export->error = ''; $bits = ''; $type = ( isset( $_GET['type'] ) ? $_GET['type'] : $type ); if( empty( $type ) ) { if( $gui == 'gui' ) { $output = sprintf( '<p>%s</p>', __( 'No export type was provided.', 'woo_ce' ) ); } else { error_log( sprintf( '[store-exporter-deluxe] -: Error: %s', __( 'No export type was provided', 'woo_ce' ) ) ); return false; } } else { $types = array_keys( woo_ce_return_export_types() ); $export->type = $type; // Check that export is in the list of available exports if( !in_array( $export->type, $types ) ) { if( $gui == 'gui' ) { $output = '<p>' . __( 'An invalid export type was provided.', 'woo_ce' ) . '</p>'; } else { error_log( sprintf( '[store-exporter-deluxe] -: Error: %s', __( 'An invalid export type was provided', 'woo_ce' ) ) ); return false; } } else { $export->export_format = ( isset( $_GET['format'] ) ? sanitize_text_field( $_GET['format'] ) : woo_ce_get_option( 'export_format', 'csv' ) ); // Override the export format if outputting to screen in friendly design if( $gui == 'gui' && in_array( $export->export_format, array( 'csv', 'xls', 'xlsx' ) ) ) $export->export_format = 'csv'; // Override the export format if this is a scheduled export if( $export->scheduled_export ) $export->export_format = woo_ce_get_option( 'auto_format', 'csv' ); // Override the export format if the single order Transient is set $single_export_format = get_transient( WOO_CD_PREFIX . '_single_export_format' ); if( $single_export_format !== false ) $export->export_format = $single_export_format; unset( $single_export_format ); $export->delimiter = ( isset( $_GET['delimiter'] ) ? sanitize_text_field( $_GET['delimiter'] ) : woo_ce_get_option( 'delimiter', ',' ) ); if( $export->delimiter == '' || $export->delimiter == false ) { error_log( '[store-exporter-deluxe] Warning: Delimiter export option was corrupted, defaulted to ,' ); $export->delimiter = ','; woo_ce_update_option( 'delimiter', ',' ); } else if( $export->delimiter == 'TAB' ) { $export->delimiter = "\t"; } $export->category_separator = ( isset( $_GET['category_separator'] ) ? sanitize_text_field( $_GET['category_separator'] ) : woo_ce_get_option( 'category_separator', '|' ) ); // Override for line break (LF) support in Category Separator if( $export->category_separator == 'LF' ) $export->category_separator = "\n"; $export->bom = ( isset( $_GET['bom'] ) ? absint( $_GET['bom'] ) : woo_ce_get_option( 'bom', 1 ) ); $export->encoding = ( isset( $_GET['encoding'] ) ? sanitize_text_field( $_GET['encoding'] ) : woo_ce_get_option( 'encoding', 'UTF-8' ) ); $export->timeout = woo_ce_get_option( 'timeout', 600 ); $export->escape_formatting = ( isset( $_GET['escape_formatting'] ) ? sanitize_text_field( $_GET['escape_formatting'] ) : woo_ce_get_option( 'escape_formatting', 'all' ) ); $export->header_formatting = ( isset( $_GET['header_formatting'] ) ? absint( $_GET['header_formatting'] ) : woo_ce_get_option( 'header_formatting', 1 ) ); $export->date_format = woo_ce_get_option( 'date_format', 'd/m/Y' ); $export->order_items = ( isset( $_GET['order_items'] ) ? sanitize_text_field( $_GET['order_items'] ) : woo_ce_get_option( 'order_items_formatting', 'unique' ) ); $export->order_items_types = ( isset( $_GET['order_items_types'] ) ? sanitize_text_field( $_GET['order_items_types'] ) : woo_ce_get_option( 'order_items_types', false ) ); $export->upsell_formatting = woo_ce_get_option( 'upsell_formatting', 1 ); $export->crosssell_formatting = woo_ce_get_option( 'crosssell_formatting', 1 ); $export->gallery_formatting = woo_ce_get_option( 'gallery_formatting', 0 ); $export->gallery_unique = woo_ce_get_option( 'gallery_unique', 0 ); $export->max_product_gallery = woo_ce_get_option( 'max_product_gallery', 3 ); $export->filename = woo_ce_generate_filename( $export->type ); switch( $export->export_format ) { case 'csv': $php_excel_format = 'SED_CSV'; $file_extension = 'csv'; $post_mime_type = 'text/csv'; break; case 'xls': $php_excel_format = 'Excel5'; $file_extension = 'xls'; $post_mime_type = 'application/vnd.ms-excel'; break; case 'xlsx': $php_excel_format = 'Excel2007'; $file_extension = 'xlsx'; $post_mime_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break; case 'xml': $file_extension = 'xml'; $post_mime_type = 'application/xml'; break; case 'rss': $file_extension = 'xml'; $post_mime_type = 'application/rss+xml'; break; default: error_log( sprintf( '[store-exporter-deluxe] -: Warning: %s', __( 'An invalid export format was provided', 'woo_ce' ) ) ); return; break; } $export->filename = $export->filename . '.' . $file_extension; $export->limit_volume = ( isset( $_GET['limit'] ) ? absint( $_GET['limit'] ) : -1 ); $export->offset = ( isset( $_GET['offset'] ) ? absint( $_GET['offset'] ) : 0 ); // Select all export fields for CRON export $export->fields = woo_ce_cron_export_fields( $export->type, $export->scheduled_export ); // Grab to value if response is e-mail or remote POST if( in_array( $gui, array( 'email', 'post' ) ) ) { if( $gui == 'email' ) $export->to = ( isset( $_GET['to'] ) ? sanitize_email( $_GET['to'] ) : woo_ce_get_option( 'email_to', '' ) ); else if( $gui == 'post' ) $export->to = ( isset( $_GET['to'] ) ? esc_url_raw( $_GET['to'] ) : woo_ce_get_option( 'post_to', '' ) ); } $export = woo_ce_check_cron_export_arguments( $export ); $export->args = array( 'limit_volume' => $export->limit_volume, 'offset' => $export->offset, 'encoding' => $export->encoding, 'date_format' => $export->date_format, 'order_items' => $export->order_items, 'order_items_types' => $export->order_items_types ); $orderby = ( isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : null ); $order = ( isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : null ); switch( $export->type ) { case 'product': $export->args['product_orderby'] = $orderby; $export->args['product_order'] = $order; if( $export->scheduled_export ) { $product_filter_type = woo_ce_get_option( 'auto_product_type', false ); $product_filter_status = woo_ce_get_option( 'auto_product_status', false ); $product_filter_stock = woo_ce_get_option( 'auto_product_stock', false ); $product_filter_category = woo_ce_get_option( 'auto_product_category', false ); $product_filter_tag = woo_ce_get_option( 'auto_product_tag', false ); $export->args['product_type'] = ( !empty( $product_filter_type ) ? $product_filter_type : false ); $export->args['product_status'] = ( !empty( $product_filter_status ) ? $product_filter_status : false ); $export->args['product_stock'] = ( !empty( $product_filter_stock ) ? $product_filter_stock : false ); $export->args['product_categories'] = ( !empty( $product_filter_category ) ? $product_filter_category : false ); $export->args['product_tags'] = ( !empty( $product_filter_tag ) ? $product_filter_tag : false ); } else { $export->args['product_status'] = ( isset( $_GET['product_status'] ) ? sanitize_text_field( $_GET['product_status'] ) : null ); $export->args['product_stock'] = ( isset( $_GET['stock_status'] ) ? sanitize_text_field( $_GET['stock_status'] ) : null ); } break; case 'category': $export->args['category_orderby'] = $orderby; $export->args['category_order'] = $order; break; case 'tag': $export->args['tag_orderby'] = $orderby; $export->args['tag_order'] = $order; break; case 'order': $export->args['order_orderby'] = $orderby; $export->args['order_order'] = $order; $export->args['order_ids'] = ( isset( $_GET['order_ids'] ) ? sanitize_text_field( $_GET['order_ids'] ) : null ); // Override Filter Orders by Order ID if a single order transient is set $single_export_order_ids = get_transient( WOO_CD_PREFIX . '_single_export_order_ids' ); if( $single_export_order_ids !== false ) $export->args['order_ids'] = sanitize_text_field( $single_export_order_ids ); unset( $single_export_order_ids ); if( $export->scheduled_export ) { // Scheduled export engine // Order Status $order_filter_status = woo_ce_get_option( 'auto_order_status', '' ); $export->args['order_status'] = ( !empty( $order_filter_status ) ? (array)$order_filter_status : array() ); // Order Date $order_dates_filter = woo_ce_get_option( 'auto_order_date', false ); if( $order_dates_filter ) { $export->args['order_dates_filter'] = $order_dates_filter; switch( $order_dates_filter ) { case 'manual': $order_filter_dates_from = woo_ce_get_option( 'auto_order_dates_from', false ); $order_filter_dates_to = woo_ce_get_option( 'auto_order_dates_to', false ); $export->args['order_dates_from'] = ( !empty( $order_filter_dates_from ) ? sanitize_text_field( $order_filter_dates_from ) : false ); $export->args['order_dates_to'] = ( !empty( $order_filter_dates_to ) ? sanitize_text_field( $order_filter_dates_to ) : false ); break; case 'variable': $order_filter_date_variable = woo_ce_get_option( 'auto_order_date_variable', false ); $order_filter_date_variable_length = woo_ce_get_option( 'auto_order_date_variable_length', false ); $export->args['order_dates_filter_variable'] = ( !empty( $order_filter_date_variable ) ? absint( $order_filter_date_variable ) : false ); $export->args['order_dates_filter_variable_length'] = ( !empty( $order_filter_date_variable_length ) ? sanitize_text_field( $order_filter_date_variable_length ) : false ); break; } } // Product $order_filter_product = woo_ce_get_option( 'auto_order_product', '' ); $export->args['order_product'] = ( !empty( $order_filter_product ) ? (array)$order_filter_product : array() ); // Billing Country $order_filter_billing_country = woo_ce_get_option( 'auto_order_billing_country', false ); $export->args['order_billing_country'] = ( !empty( $order_filter_billing_country ) ? array_map( 'sanitize_text_field', $order_filter_billing_country ) : false ); // Shipping Country $order_filter_shipping_country = woo_ce_get_option( 'auto_order_shipping_country', false ); $export->args['order_shipping_country'] = ( !empty( $order_filter_shipping_country ) ? array_map( 'sanitize_text_field', $order_filter_shipping_country ) : false ); // Payment Gateway $order_filter_payment = woo_ce_get_option( 'auto_order_payment', array() ); $export->args['order_payment'] = ( !empty( $order_filter_payment ) ? array_map( 'sanitize_text_field', $order_filter_payment ) : false ); // Shipping Method $order_filter_shipping = woo_ce_get_option( 'auto_order_shipping', array() ); $export->args['order_shipping'] = ( !empty( $order_filter_shipping ) ? array_map( 'sanitize_text_field', $order_filter_shipping ) : false ); } else { // CRON export engine // Order Status if( isset( $_GET['order_status'] ) ) { $order_filter_status = sanitize_text_field( $_GET['order_status'] ); $order_filter_status = explode( ',', $order_filter_status ); $export->args['order_status'] = $order_filter_status; } // Product if( isset( $_GET['order_product'] ) ) { $order_filter_product = sanitize_text_field( $_GET['order_product'] ); $order_filter_product = explode( ',', $order_filter_product ); $export->args['order_product'] = $order_filter_product; } // Order Date if( isset( $_GET['order_date_from'] ) && isset( $_GET['order_date_to'] ) ) { $order_filter_dates_from = $_GET['order_date_from']; $order_filter_dates_to = $_GET['order_date_to']; $export->args['order_dates_filter'] = 'manual'; $export->args['order_dates_from'] = ( !empty( $order_filter_dates_from ) ? sanitize_text_field( $order_filter_dates_from ) : false ); $export->args['order_dates_to'] = ( !empty( $order_filter_dates_to ) ? sanitize_text_field( $order_filter_dates_to ) : false ); } // Billing Country if( isset( $_GET['billing_country'] ) ) { $order_filter_billing_country = sanitize_text_field( $_GET['billing_country'] ); $order_filter_billing_country = explode( ',', $order_filter_billing_country ); $export->args['order_billing_country'] = ( !empty( $order_filter_billing_country ) ? $order_filter_billing_country : false ); } // Shipping Country if( isset( $_GET['shipping_country'] ) ) { $order_filter_shipping_country = sanitize_text_field( $_GET['shipping_country'] ); $order_filter_shipping_country = explode( ',', $order_filter_shipping_country ); $export->args['order_shipping_country'] = ( !empty( $order_filter_shipping_country ) ? $order_filter_shipping_country : false ); } // Payment Gateway if( isset( $_GET['payment_gateway'] ) ) { $order_filter_payment = sanitize_text_field( $_GET['order_payment'] ); $order_filter_payment = explode( ',', $order_filter_payment ); $export->args['order_payment'] = ( !empty( $order_filter_payment ) ? $order_filter_payment : false ); } // Shipping Method if( isset( $_GET['shipping_method'] ) ) { $order_filter_shipping = sanitize_text_field( $_GET['shipping_method'] ); $order_filter_shipping = explode( ',', $order_filter_shipping ); $export->args['order_shipping'] = ( !empty( $order_filter_shipping ) ? $order_filter_shipping : false ); } } break; case 'subscription': $export->args['subscription_orderby'] = $orderby; $export->args['subscription_order'] = $order; break; case 'product_vendor': $export->args['product_vendor_orderby'] = $orderby; $export->args['product_vendor_order'] = $order; break; case 'commission': // Commission Date $commission_dates_filter = woo_ce_get_option( 'auto_commission_date', false ); if( $commission_dates_filter ) { $export->args['commission_dates_filter'] = $commission_dates_filter; switch( $commission_dates_filter ) { case 'manual': $commission_filter_dates_from = woo_ce_get_option( 'auto_commission_dates_from', false ); $commission_filter_dates_to = woo_ce_get_option( 'auto_commission_date_to', false ); $export->args['commission_dates_from'] = ( !empty( $commission_filter_dates_from ) ? sanitize_text_field( $commission_filter_dates_from ) : false ); $export->args['commission_dates_to'] = ( !empty( $commission_filter_dates_to ) ? sanitize_text_field( $commission_filter_dates_to ) : false ); break; case 'variable': $commission_filter_date_variable = woo_ce_get_option( 'auto_commission_date_variable', false ); $commission_filter_date_variable_length = woo_ce_get_option( 'auto_commission_date_variable_length', false ); $export->args['commission_dates_filter_variable'] = ( !empty( $commission_filter_date_variable ) ? absint( $commission_filter_date_variable ) : false ); $export->args['commission_dates_filter_variable_length'] = ( !empty( $commission_filter_date_variable_length ) ? sanitize_text_field( $commission_filter_date_variable_length ) : false ); break; } } break; case 'shipping_class': $export->args['shipping_class_orderby'] = $orderby; $export->args['shipping_class_order'] = $order; break; } $export->filename = woo_ce_generate_filename( $export->type ) . '.' . $file_extension; // Let's spin up PHPExcel for supported Export Types and Export Formats if( in_array( $export->export_format, array( 'csv', 'xls', 'xlsx' ) ) ) { // Check if we are using PHPExcel or not for supported Export Types $dataset = woo_ce_export_dataset( $export->type ); if( !empty( $dataset ) ) { // Check that PHPExcel is where we think it is if( file_exists( WOO_CD_PATH . 'classes/PHPExcel.php' ) ) { // Check if PHPExcel has already been loaded if( !class_exists( 'PHPExcel' ) ) { include_once( WOO_CD_PATH . 'classes/PHPExcel.php' ); } else { error_log( sprintf( '[store-exporter-deluxe] %s: Warning: %s', $export->filename, __( 'The PHPExcel library was already loaded by another WordPress Plugin, if there\'s issues with your export file you know where to look.', 'woo_ce' ) ) ); } $excel = new PHPExcel(); $excel->setActiveSheetIndex( 0 ); $excel->getActiveSheet()->setTitle( ucfirst( $export->type ) ); $row = 1; // Skip headers if Heading Formatting is turned off if( $export->header_formatting ) { $col = 0; foreach( $export->columns as $column ) { $excel->getActiveSheet()->setCellValueByColumnAndRow( $col, $row, $column ); $excel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->getStyle()->getFont()->setBold( true ); $excel->getActiveSheet()->getColumnDimensionByColumn( $col )->setAutoSize( true ); $col++; } $row = 2; } foreach( $dataset as $data ) { $col = 0; foreach( array_keys( $export->fields ) as $field ) { $excel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->getStyle()->getFont()->setBold( false ); if( $export->encoding == 'UTF-8' ) { if( woo_ce_detect_value_string( ( isset( $data->$field ) ? $data->$field : null ) ) ) { // Treat this cell as a string $excel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->setValueExplicit( ( isset( $data->$field ) ? wp_specialchars_decode( $data->$field ) : '' ), PHPExcel_Cell_DataType::TYPE_STRING ); } else { $excel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->setValue( ( isset( $data->$field ) ? wp_specialchars_decode( $data->$field ) : '' ) ); } } else { // PHPExcel only deals with UTF-8 regardless of encoding type if( woo_ce_detect_value_string( ( isset( $data->$field ) ? $data->$field : null ) ) ) { // Treat this cell as a string $excel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->setValueExplicit( ( isset( $data->$field ) ? utf8_encode( wp_specialchars_decode( $data->$field ) ) : '' ), PHPExcel_Cell_DataType::TYPE_STRING ); } else { $excel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->setValue( ( isset( $data->$field ) ? utf8_encode( wp_specialchars_decode( $data->$field ) ) : '' ) ); } } $col++; } $row++; } switch( $export->export_format ) { case 'csv': // We need to load this after the PHPExcel Class has been created woo_cd_load_phpexcel_sed_csv_writer(); break; } $objWriter = PHPExcel_IOFactory::createWriter( $excel, $php_excel_format ); switch( $export->export_format ) { case 'csv': $objWriter->setUseBOM( true ); // Check if we're using a non-standard delimiter if( $export->delimiter != ',' ) $objWriter->setDelimiter( $export->delimiter ); break; case 'xlsx': $objWriter->setPreCalculateFormulas( false ); break; } if( in_array( $gui, array( 'raw' ) ) ) { $objWriter->save( 'php://output' ); } else { // Save to file and insert to WordPress Media $temp_filename = tempnam( apply_filters( 'woo_ce_sys_get_temp_dir', sys_get_temp_dir() ), 'tmp' ); // Check if we were given a temporary filename if( $temp_filename == false ) { $message = sprintf( __( 'We could not create a temporary export file in %s, ensure that WordPress can read and write files here and try again.', 'woo_ce' ), apply_filters( 'woo_ce_sys_get_temp_dir', sys_get_temp_dir() ) ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $message ) ); } else { $objWriter->save( $temp_filename ); $bits = file_get_contents( $temp_filename ); } unlink( $temp_filename ); } // Clean up PHPExcel $excel->disconnectWorksheets(); unset( $objWriter, $excel ); } else { error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, __( 'We couldn\'t load the PHPExcel library, this file should be present.' ) ) ); } } // Run the default engine for the XML and RSS export formats } else if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { // Check if SimpleXMLElement is present if( class_exists( 'SED_SimpleXMLElement' ) ) { if( $export->export_format == 'xml' ) { $xml = new SED_SimpleXMLElement( sprintf( apply_filters( 'woo_ce_export_xml_first_line', '<?xml version="1.0" encoding="%s"?><%s/>' ), esc_attr( $export->encoding ), esc_attr( apply_filters( 'woo_ce_export_xml_store_node', 'store' ) ) ) ); if( woo_ce_get_option( 'xml_attribute_url', 1 ) ) $xml->addAttribute( 'url', get_site_url() ); if( woo_ce_get_option( 'xml_attribute_date', 1 ) ) $xml->addAttribute( 'date', date( 'Y-m-d' ) ); if( woo_ce_get_option( 'xml_attribute_time', 0 ) ) $xml->addAttribute( 'time', date( 'H:i:s' ) ); if( woo_ce_get_option( 'xml_attribute_title', 1 ) ) $xml->addAttribute( 'name', htmlspecialchars( get_bloginfo( 'name' ) ) ); if( woo_ce_get_option( 'xml_attribute_export', 1 ) ) $xml->addAttribute( 'export', htmlspecialchars( $export->type ) ); if( woo_ce_get_option( 'xml_attribute_orderby', 1 ) ) $xml->addAttribute( 'orderby', $orderby ); if( woo_ce_get_option( 'xml_attribute_order', 1 ) ) $xml->addAttribute( 'order', $order ); if( woo_ce_get_option( 'xml_attribute_limit', 1 ) ) $xml->addAttribute( 'limit', $export->limit_volume ); if( woo_ce_get_option( 'xml_attribute_offset', 1 ) ) $xml->addAttribute( 'offset', $export->offset ); $xml = woo_ce_export_dataset( $export->type, $xml ); } else if( $export->export_format == 'rss' ) { $xml = new SED_SimpleXMLElement( sprintf( apply_filters( 'woo_ce_export_rss_first_line', '<?xml version="1.0" encoding="%s"?><rss version="2.0"%s/>' ), esc_attr( $export->encoding ), ' xmlns:g="http://base.google.com/ns/1.0"' ) ); $child = $xml->addChild( apply_filters( 'woo_ce_export_rss_channel_node', 'channel' ) ); $child->addChild( 'title', woo_ce_get_option( 'rss_title', '' ) ); $child->addChild( 'link', woo_ce_get_option( 'rss_link', '' ) ); $child->addChild( 'description', woo_ce_get_option( 'rss_description', '' ) ); $bits = woo_ce_export_dataset( $export->type, $child ); } $bits = woo_ce_format_xml( $xml ); } else { $bits = false; error_log( '[store-exporter-deluxe] Error: The SimpleXMLElement class does not exist for XML file generation' ); } } if( !empty( $bits ) ) { $output = '<p>' . __( 'Export completed successfully.', 'woo_ce' ) . '</p>'; if( $gui == 'gui' ) $output .= '<textarea readonly="readonly">' . esc_textarea( str_replace( '<br />', "\n", $bits ) ) . '</textarea>'; } else { if( $gui == 'gui' ) { $output = sprintf( '<p>%s</p>', __( 'No export entries were found.', 'woo_ce' ) ); } else { error_log( sprintf( '[store-exporter-deluxe] %s: Warning: %s', $export->filename, __( 'No export entries were found', 'woo_ce' ) ) ); return false; } } } } // Return raw export to browser without file headers if( $gui == 'raw' && !empty( $bits ) ) { return $bits; // Return export as file download to browser } else if( $gui == 'download' && !empty( $bits ) ) { woo_ce_generate_file_headers( $post_mime_type ); if( defined( 'DOING_AJAX' ) || get_transient( WOO_CD_PREFIX . '_single_export_format' ) !== false ) echo $bits; else return $bits; // HTTP Post export contents to remote URL } else if( $gui == 'post' && !empty( $bits ) ) { $args = apply_filters( 'woo_ce_cron_export_post_args', array( 'method' => 'POST', 'timeout' => 60, 'redirection' => 0, 'httpversion' => '1.0', 'sslverify' => false, 'blocking' => true, 'headers' => array( 'accept' => $post_mime_type, 'content-type' => $post_mime_type ), 'body' => $bits, 'cookies' => array(), 'user-agent' => sprintf( 'WordPress/%s', $GLOBALS['wp_version'] ), ) ); $response = wp_remote_post( $export->to, $args ); if( is_wp_error( $response ) ) { error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $response->get_error_message() ) ); return false; } else { error_log( sprintf( '[store-exporter-deluxe] %s: Success: %s', $export->filename, sprintf( __( 'Remote POST sent to %s', 'woo_ce' ), $export->to ) ) ); } // Output to screen in friendly design with on-screen error responses } else if( $gui == 'gui' ) { if( file_exists( WOO_CD_PATH . 'templates/admin/cron.php' ) ) { include_once( WOO_CD_PATH . 'templates/admin/cron.php' ); } else { error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, __( 'Could not load template file within /templates/admin/cron.php', 'woo_ce' ) ) ); } if( isset( $output ) ) echo $output; echo ' </body> </html>'; // Save export file to WordPress Media before sending/saving/etc. action } else if( in_array( $gui, array( 'gui', 'archive', 'url', 'file', 'email', 'ftp' ) ) ) { $upload = false; if( $export->filename && !empty( $bits ) ) { $post_ID = woo_ce_save_file_attachment( $export->filename, $post_mime_type ); $upload = wp_upload_bits( $export->filename, null, $bits ); if( ( $post_ID == false ) || $upload['error'] ) { wp_delete_attachment( $post_ID, true ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $upload['error'] ) ); return false; } if( file_exists( ABSPATH . 'wp-admin/includes/image.php' ) ) { include_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $post_ID, $upload['file'] ); wp_update_attachment_metadata( $post_ID, $attach_data ); update_attached_file( $post_ID, $upload['file'] ); if( !empty( $post_ID ) ) { woo_ce_save_file_guid( $post_ID, $export->type, $upload['url'] ); woo_ce_save_file_details( $post_ID ); } } else { error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, __( 'Could not load image.php within /wp-admin/includes/image.php', 'woo_ce' ) ) ); } } // Return URL to export file if( $gui == 'url' ) return $upload['url']; // Return system path to export file if( $gui == 'file' ) return $upload['file']; // E-mail export file to preferred address or WordPress site owner address if( $gui == 'email' ) { global $woocommerce; $mailer = $woocommerce->mailer(); $subject = woo_ce_cron_email_subject( $export->type, $export->filename ); $attachment = $upload['file']; $email_heading = sprintf( __( 'Export: %s', 'woo_ce' ), ucwords( $export->type ) ); $recipient_name = apply_filters( 'woo_ce_email_recipient_name', __( 'there', 'woo_ce' ) ); $email_contents = apply_filters( 'woo_ce_email_contents', wpautop( __( 'Please find attached your export ready to review.', 'woo_ce' ) ) ); // Buffer ob_start(); // Get mail template if( file_exists( WOO_CD_PATH . 'templates/emails/scheduled_export.php' ) ) { include_once( WOO_CD_PATH . 'templates/emails/scheduled_export.php' ); } else { echo wpautop( sprintf( __( 'Hi %s', 'woo_ce' ), $recipient_name ) ); echo $email_contents; error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, sprintf( __( 'Could not load template file %s within %s, defaulted to hard coded template.', 'woo_ce' ), 'scheduled_export.php', '/templates/emails/...' ) ) ); } // Get contents $message = ob_get_clean(); // Send the mail using WooCommerce mailer if( function_exists( 'woocommerce_mail' ) ) { woocommerce_mail( $export->to, $subject, $message, null, $attachment ); } else { // Default to wp_mail() add_filter( 'wp_mail_content_type', 'woo_ce_set_html_content_type' ); wp_mail( $export->to, $subject, $message, null, $attachment ); remove_filter( 'wp_mail_content_type', 'woo_ce_set_html_content_type' ); } // Delete the export file regardless of whether e-mail was successful or not wp_delete_attachment( $post_ID, true ); error_log( sprintf( '[store-exporter-deluxe] %s: Success: %s', $export->filename, sprintf( __( 'Scheduled export e-mail of %s sent to %s', 'woo_ce' ), $export->filename, $export->to ) ) ); } if( $gui == 'ftp' ) { // Load up our FTP/SFTP resources $host = woo_ce_get_option( 'auto_ftp_method_host', '' ); if( !empty( $host ) ) $host = woo_ce_format_ftp_host( $host ); $port = woo_ce_get_option( 'auto_ftp_method_port', '' ); $port = ( !empty( $port ) ? absint( $port ) : false ); $user = woo_ce_get_option( 'auto_ftp_method_user', '' ); $pass = woo_ce_get_option( 'auto_ftp_method_pass', '' ); $path = woo_ce_get_option( 'auto_ftp_method_path', '' ); $filename = woo_ce_get_option( 'auto_ftp_method_filename', '' ); // Switch to fixed export filename if provided if( !empty( $filename ) ) $export->filename = woo_ce_generate_filename( $export->type, $filename ) . '.' . $file_extension; // Check what protocol are we using; FTP or SFTP? $protocol = woo_ce_get_option( 'auto_ftp_method_protocol', 'ftp' ); switch( $protocol ) { case 'ftp': default: // Check if ftp_connect() is available if( function_exists( 'ftp_connect' ) ) { $passive = woo_ce_get_option( 'auto_ftp_method_passive', '' ); $timeout = woo_ce_get_option( 'auto_ftp_method_timeout', '' ); if( $connection = @ftp_connect( $host, $port ) ) { // Update the FTP timeout if available and if a timeout was provided at export $remote_timeout = ftp_get_option( $connection, FTP_TIMEOUT_SEC ); $timeout = absint( $timeout ); if( $remote_timeout !== false && !empty( $timeout ) ) { // Compare the server timeout and the timeout provided at export if( $remote_timeout <> $timeout ) { if( ftp_set_option( $connection, FTP_TIMEOUT_SEC, $timeout ) == false ) error_log( sprintf( '[store-exporter-deluxe] %s: Warning: %s', $export->filename, sprintf( __( 'Could not change the FTP server timeout on %s', 'woo_ce' ), $host ) ) ); } } unset( $remote_timeout ); if( ftp_login( $connection, $user, $pass ) ) { // Check if Transfer Mode is set to Auto/Pasive and if passive mode is available if( in_array( $passive, array( 'auto', 'passive' ) ) ) { $features = ftp_raw( $connection, 'FEAT' ); if( !empty( $features ) ) { if( in_array( 'PASV', $features ) ) { if( ftp_pasv( $connection, true ) == false ) error_log( sprintf( '[store-exporter-deluxe] %s: Warning: %s', 'woo_ce', $export->filename, sprintf( __( 'Could not switch to FTP passive mode on %s', 'woo_ce' ), $host ) ) ); } } } // Change directory if neccesary if( !empty( $directory ) ) { $current_directory = ftp_pwd( $connection ); if( $current_directory !== false && @ftp_chdir( $connection, $path ) ) ftp_chdir( $connection, $path ); } if( ftp_put( $connection, sprintf( '%s/%s', $path, $export->filename ), $upload['file'], FTP_ASCII ) ) { error_log( sprintf( '[store-exporter-deluxe] %s: Success: %s', $export->filename, sprintf( __( 'Scheduled export of %s to %s via FTP uploaded', 'woo_ce' ), $export->filename, $path ) ) ); } else { $export->error = sprintf( __( 'There was a problem uploading %s to %s via FTP, response: %s', 'woo_ce' ), $export->filename, $path, woo_ce_error_get_last_message() ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $export->error ) ); } } else { $export->error = sprintf( __( 'Login incorrect for user %s on FTP server at %s, response: %s', 'woo_ce' ), $user, $host, woo_ce_error_get_last_message() ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $export->error ) ); } } else { $export->error = sprintf( __( 'There was a problem connecting to %s via FTP', 'woo_ce' ), $host ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $export->error ) ); } } else { $export->error = __( 'The function ftp_connect() is disabled within your WordPress site, cannot upload to FTP server', 'woo_ce' ); error_log( __( '[store-exporter-deluxe] %s: Error: %s', 'woo_ce' ), $export->filename, $export->error ); } break; case 'sftp': // Check if ssh2_connect() is available if( function_exists( 'ssh2_connect' ) ) { if( $connection = @ssh2_connect( $host, $port ) ) { if( ssh2_auth_password( $connection, $user, $pass ) ) { // Initialize SFTP subsystem if( $session = ssh2_sftp( $connection ) ) { if( $handle = fopen( sprintf( 'ssh2.sftp://%s/%s/%s', $session, $path, $export->filename ), 'w+' ) ) { error_log( sprintf( '[store-exporter-deluxe] %s: Success: %s', $export->filename, sprintf( __( 'Scheduled export of %s to %s via SFTP uploaded', 'woo_ce' ), $export->filename, $path ) ) ); } else { $export->error = sprintf( __( 'There was a problem uploading %s to %s via SFTP', 'woo_ce' ), $export->filename, $path ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $export->error ) ); } } else { $export->error = sprintf( __( 'Could not initialize SFTP subsystem on SFTP server at %s', 'woo_ce' ), $host ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $export->error ) ); } } else { $export->error = sprintf( __( 'Login incorrect for user %s on SFTP server at %s', 'woo_ce' ), $user, $host ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $export->error ) ); } } else { $export->error = sprintf( __( 'There was a problem connecting to %s via SFTP', 'woo_ce' ), $host ); error_log( sprintf( '[store-exporter-deluxe] %s: Error: %s', $export->filename, $export->error ) ); } } else { $export->error = __( 'The function ssh2_connect() is disabled within your WordPress site, cannot upload to SFTP server', 'woo_ce' ); error_log( sprintf( __( '[store-exporter-deluxe] %s: Error: %s', 'woo_ce' ), $export->filename, $export->error ) ); } break; } // Delete the export file regardless of whether upload was successful or not wp_delete_attachment( $post_ID, true ); } } // Only include scheduled exports to the Recent Scheduled Exports list if( $export->scheduled_export ) { $recent_exports = woo_ce_get_option( 'recent_scheduled_exports', array() ); if( empty( $recent_exports ) ) $recent_exports = array(); $size = count( $recent_exports ); // Get the limit from the WordPress Dashboard widget if( !$widget_options = get_option( 'woo_ce_recent_scheduled_export_widget_options', array() ) ) { $widget_options = array( 'number' => 5 ); } // Check if we have maxed out our recent scheduled exports if( $size >= $widget_options['number'] ) array_shift( $recent_exports ); $post_ID = ( isset( $post_ID ) ? $post_ID : 0 ); $recent_exports[] = array( 'post_id' => ( empty( $export->error ) ? $post_ID : 0 ), 'name' => $export->filename, 'date' => time(), 'method' => $gui, 'error' => $export->error ); woo_ce_update_option( 'recent_scheduled_exports', $recent_exports ); } delete_option( WOO_CD_PREFIX . '_exported' ); // If the CRON process gets this far, pass on the good news! return true; }
function woo_ce_recent_scheduled_export_widget() { $recent_exports = woo_ce_get_option( 'recent_scheduled_exports', array() ); if( empty( $recent_exports ) ) $recent_exports = array(); $size = count( $recent_exports ); $recent_exports = array_reverse( $recent_exports ); // Get widget options if( !$widget_options = get_option( 'woo_ce_recent_scheduled_export_widget_options', array() ) ) { $widget_options = array( 'number' => 5 ); } // Check if we need to limit the number of recent exports if( $size > $widget_options['number'] ) { $i = $size; // Loop through the recent exports till we get it down to our limit for( $i; $i >= $widget_options['number']; $i-- ) { unset( $recent_exports[$i] ); } // Save the changes so we don't have to do this next screen refresh woo_ce_update_option( 'recent_scheduled_exports', $recent_exports ); } if( file_exists( WOO_CD_PATH . 'templates/admin/dashboard_widget-recent_scheduled_export.php' ) ) { include_once( WOO_CD_PATH . 'templates/admin/dashboard_widget-recent_scheduled_export.php' ); } else { $message = sprintf( __( 'We couldn\'t load the widget template file <code>%s</code> within <code>%s</code>, this file should be present.', 'woo_ce' ), 'dashboard_widget-recent_scheduled_export.php', WOO_CD_PATH . 'templates/admin/...' ); ob_start(); ?> <p><strong><?php echo $message; ?></strong></p> <p><?php _e( 'You can see this error for one of a few common reasons', 'woo_ce' ); ?>:</p> <ul class="ul-disc"> <li><?php _e( 'WordPress was unable to create this file when the Plugin was installed or updated', 'woo_ce' ); ?></li> <li><?php _e( 'The Plugin files have been recently changed and there has been a file conflict', 'woo_ce' ); ?></li> <li><?php _e( 'The Plugin file has been locked and cannot be opened by WordPress', 'woo_ce' ); ?></li> </ul> <p><?php _e( 'Jump onto our website and download a fresh copy of this Plugin as it might be enough to fix this issue. If this persists get in touch with us.', 'woo_ce' ); ?></p> <?php ob_end_flush(); } }
function woo_ce_admin_save_settings() { // Strip file extension from export filename $export_filename = strip_tags( $_POST['export_filename'] ); if( ( strpos( $export_filename, '.csv' ) !== false ) || ( strpos( $export_filename, '.xml' ) !== false ) || ( strpos( $export_filename, '.xls' ) !== false ) ) $export_filename = str_replace( array( '.csv', '.xml', '.xls' ), '', $export_filename ); woo_ce_update_option( 'export_filename', $export_filename ); woo_ce_update_option( 'delete_file', absint( $_POST['delete_file'] ) ); woo_ce_update_option( 'encoding', sanitize_text_field( $_POST['encoding'] ) ); woo_ce_update_option( 'delimiter', sanitize_text_field( $_POST['delimiter'] ) ); woo_ce_update_option( 'category_separator', sanitize_text_field( $_POST['category_separator'] ) ); woo_ce_update_option( 'bom', absint( $_POST['bom'] ) ); woo_ce_update_option( 'escape_formatting', sanitize_text_field( $_POST['escape_formatting'] ) ); woo_ce_update_option( 'header_formatting', absint( $_POST['header_formatting'] ) ); if( $_POST['date_format'] == 'custom' && !empty( $_POST['date_format_custom'] ) ) woo_ce_update_option( 'date_format', sanitize_text_field( $_POST['date_format_custom'] ) ); else woo_ce_update_option( 'date_format', sanitize_text_field( $_POST['date_format'] ) ); woo_ce_update_option( 'email_to', sanitize_text_field( $_POST['email_to'] ) ); woo_ce_update_option( 'email_subject', sanitize_text_field( $_POST['email_subject'] ) ); woo_ce_update_option( 'post_to', sanitize_text_field( $_POST['post_to'] ) ); // XML settings woo_ce_update_option( 'xml_attribute_url', ( isset( $_POST['xml_attribute_url'] ) ? absint( $_POST['xml_attribute_url'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_title', ( isset( $_POST['xml_attribute_title'] ) ? absint( $_POST['xml_attribute_title'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_date', ( isset( $_POST['xml_attribute_date'] ) ? absint( $_POST['xml_attribute_date'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_time', ( isset( $_POST['xml_attribute_time'] ) ? absint( $_POST['xml_attribute_time'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_export', ( isset( $_POST['xml_attribute_export'] ) ? absint( $_POST['xml_attribute_export'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_orderby', ( isset( $_POST['xml_attribute_orderby'] ) ? absint( $_POST['xml_attribute_orderby'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_order', ( isset( $_POST['xml_attribute_order'] ) ? absint( $_POST['xml_attribute_order'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_limit', ( isset( $_POST['xml_attribute_limit'] ) ? absint( $_POST['xml_attribute_limit'] ) : 0 ) ); woo_ce_update_option( 'xml_attribute_offset', ( isset( $_POST['xml_attribute_offset'] ) ? absint( $_POST['xml_attribute_offset'] ) : 0 ) ); // RSS settings woo_ce_update_option( 'rss_title', ( isset( $_POST['rss_title'] ) ? sanitize_text_field( $_POST['rss_title'] ) : '' ) ); woo_ce_update_option( 'rss_link', ( isset( $_POST['rss_link'] ) ? esc_url_raw( $_POST['rss_link'] ) : '' ) ); woo_ce_update_option( 'rss_description', ( isset( $_POST['rss_description'] ) ? sanitize_text_field( $_POST['rss_description'] ) : '' ) ); // Scheduled export settings $enable_auto = absint( $_POST['enable_auto'] ); $auto_schedule = sanitize_text_field( $_POST['auto_schedule'] ); $auto_interval = absint( $_POST['auto_interval'] ); $auto_commence = sanitize_text_field( $_POST['auto_commence'] ); $auto_commence_date = sanitize_text_field( $_POST['auto_commence_date'] ); $site_hash = md5( get_option( 'siteurl' ) ); // Display additional notice if Enabled Scheduled Exports is enabled/disabled or scheduling options are modified if( woo_ce_get_option( 'enable_auto', 0 ) <> $enable_auto || woo_ce_get_option( 'auto_schedule', 'custom' ) <> $auto_schedule || woo_ce_get_option( 'auto_interval', 1440 ) <> $auto_interval || woo_ce_get_option( 'auto_commence', 'now' ) <> $auto_commence || woo_ce_get_option( 'auto_commence_date', date( 'd/m/Y H:i' ) ) <> $auto_commence_date ) { // Save these fields before we re-load the WP-CRON schedule woo_ce_update_option( 'enable_auto', $enable_auto ); // Remove from WP-CRON schedule if disabled if( $enable_auto == 0 ) { woo_ce_cron_activation(); // Re-load the WP-CRON schedule with our new scheduling options } else if( woo_ce_get_option( 'auto_schedule', 'custom' ) <> $auto_schedule || woo_ce_get_option( 'auto_interval', 1440 ) <> $auto_interval || woo_ce_get_option( 'auto_commence', 'now' ) <> $auto_commence || woo_ce_get_option( 'auto_commence_date', date( 'd/m/Y H:i' ) ) <> $auto_commence_date || woo_ce_get_option( 'site_hash', '' ) <> $site_hash ) { // Save these fields before we re-load the WP-CRON schedule woo_ce_update_option( 'auto_commence', $auto_commence ); woo_ce_update_option( 'auto_commence_date', $auto_commence_date ); woo_ce_update_option( 'auto_schedule', $auto_schedule ); woo_ce_update_option( 'auto_interval', $auto_interval ); // Update the Site URL hash we use for live vs. staging checks woo_ce_update_option( 'site_hash', $site_hash ); woo_ce_cron_activation( true ); } switch( $auto_schedule ) { case 'daily': case 'weekly': case 'monthly': $interval = $auto_schedule; break; case 'custom': $interval = sprintf( __( 'in %d minute(s)', 'woo_ce' ), $auto_interval ); break; } switch( $auto_commence ) { case 'now': $commence = __( ' from now' ); break; case 'future': $commence = sprintf( __( ' starting %s' ), $auto_commence_date ); break; } $message = sprintf( __( 'Scheduled exports has been %s.', 'woo_ce' ), ( ( $enable_auto == 1 ) ? sprintf( __( 'activated, next scheduled export will run %s%s', 'woo_ce' ), $interval, $commence ) : __( 'de-activated, no further automated exports will occur', 'woo_ce' ) ) ); woo_cd_admin_notice( $message ); } woo_ce_update_option( 'auto_type', sanitize_text_field( $_POST['auto_type'] ) ); woo_ce_update_option( 'auto_product_type', ( isset( $_POST['product_filter_type'] ) ? array_map( 'sanitize_text_field', $_POST['product_filter_type'] ) : array() ) ); woo_ce_update_option( 'auto_product_status', ( isset( $_POST['product_filter_status'] ) ? array_map( 'sanitize_text_field', $_POST['product_filter_status'] ) : array() ) ); woo_ce_update_option( 'auto_product_stock', sanitize_text_field( $_POST['product_filter_stock'] ) ); woo_ce_update_option( 'auto_product_category', ( isset( $_POST['product_filter_category'] ) ? array_map( 'absint', $_POST['product_filter_category'] ) : array() ) ); woo_ce_update_option( 'auto_product_tag', ( isset( $_POST['product_filter_tag'] ) ? array_map( 'absint', $_POST['product_filter_tag'] ) : array() ) ); woo_ce_update_option( 'auto_order_date', sanitize_text_field( $_POST['order_dates_filter'] ) ); woo_ce_update_option( 'auto_order_dates_from', sanitize_text_field( $_POST['order_dates_from'] ) ); woo_ce_update_option( 'auto_order_dates_to', sanitize_text_field( $_POST['order_dates_to'] ) ); woo_ce_update_option( 'auto_order_date_variable', sanitize_text_field( $_POST['order_dates_filter_variable'] ) ); woo_ce_update_option( 'auto_order_date_variable_length', sanitize_text_field( $_POST['order_dates_filter_variable_length'] ) ); woo_ce_update_option( 'auto_order_status', ( isset( $_POST['order_filter_status'] ) ? woo_ce_format_product_filters( array_map( 'sanitize_text_field', $_POST['order_filter_status'] ) ) : array() ) ); // Check if we are dealing with a string or array $auto_order_product = ( isset( $_POST['order_filter_product'] ) ? $_POST['order_filter_product'] : false ); // Select2 passes us a string whereas Chosen gives us an array if( is_array( $auto_order_product ) && count( $auto_order_product ) == 1 ) $auto_order_product = explode( ',', $auto_order_product[0] ); woo_ce_update_option( 'auto_order_product', ( !empty( $auto_order_product ) ? woo_ce_format_product_filters( array_map( 'absint', $auto_order_product ) ) : array() ) ); unset( $auto_order_product ); woo_ce_update_option( 'auto_order_billing_country', ( isset( $_POST['order_filter_billing_country'] ) ? array_map( 'sanitize_text_field', $_POST['order_filter_billing_country'] ) : array() ) ); woo_ce_update_option( 'auto_order_shipping_country', ( isset( $_POST['order_filter_shipping_country'] ) ? array_map( 'sanitize_text_field', $_POST['order_filter_shipping_country'] ) : array() ) ); woo_ce_update_option( 'auto_order_payment', ( isset( $_POST['order_filter_payment'] ) ? array_map( 'sanitize_text_field', $_POST['order_filter_payment'] ) : array() ) ); woo_ce_update_option( 'auto_order_shipping', ( isset( $_POST['order_filter_shipping'] ) ? array_map( 'sanitize_text_field', $_POST['order_filter_shipping'] ) : array() ) ); woo_ce_update_option( 'auto_format', sanitize_text_field( $_POST['auto_format'] ) ); woo_ce_update_option( 'auto_method', sanitize_text_field( $_POST['auto_method'] ) ); woo_ce_update_option( 'auto_ftp_method_host', ( isset( $_POST['ftp_method_host'] ) ? woo_ce_format_ftp_host( sanitize_text_field( $_POST['ftp_method_host'] ) ) : '' ) ); woo_ce_update_option( 'auto_ftp_method_user', sanitize_text_field( $_POST['ftp_method_user'] ) ); // Update FTP password only if it is filled in if( !empty( $_POST['ftp_method_pass'] ) ) woo_ce_update_option( 'auto_ftp_method_pass', sanitize_text_field( $_POST['ftp_method_pass'] ) ); woo_ce_update_option( 'auto_ftp_method_port', sanitize_text_field( $_POST['ftp_method_port'] ) ); woo_ce_update_option( 'auto_ftp_method_protocol', sanitize_text_field( $_POST['ftp_method_protocol'] ) ); woo_ce_update_option( 'auto_ftp_method_path', sanitize_text_field( $_POST['ftp_method_path'] ) ); // Strip file extension from export filename $ftp_filename = strip_tags( $_POST['ftp_method_filename'] ); if( ( strpos( $ftp_filename, '.csv' ) !== false ) || ( strpos( $ftp_filename, '.xml' ) !== false ) || ( strpos( $ftp_filename, '.xls' ) !== false ) || ( strpos( $ftp_filename, '.xlsx' ) !== false ) ) $ftp_filename = str_replace( array( '.csv', '.xml', '.xls', '.xlsx' ), '', $ftp_filename ); woo_ce_update_option( 'auto_ftp_method_filename', $ftp_filename ); unset( $ftp_filename ); woo_ce_update_option( 'auto_ftp_method_passive', sanitize_text_field( $_POST['ftp_method_passive'] ) ); woo_ce_update_option( 'auto_ftp_method_timeout', sanitize_text_field( $_POST['ftp_method_timeout'] ) ); woo_ce_update_option( 'scheduled_fields', sanitize_text_field( $_POST['scheduled_fields'] ) ); // CRON settings $enable_cron = absint( $_POST['enable_cron'] ); // Display additional notice if Enabled CRON is enabled/disabled if( woo_ce_get_option( 'enable_cron', 0 ) <> $enable_cron ) { $message = sprintf( __( 'CRON support has been %s.', 'woo_ce' ), ( ( $enable_cron == 1 ) ? __( 'enabled', 'woo_ce' ) : __( 'disabled', 'woo_ce' ) ) ); woo_cd_admin_notice( $message ); } woo_ce_update_option( 'enable_cron', $enable_cron ); woo_ce_update_option( 'secret_key', sanitize_text_field( $_POST['secret_key'] ) ); woo_ce_update_option( 'cron_fields', sanitize_text_field( $_POST['cron_fields'] ) ); // Orders Screen woo_ce_update_option( 'order_actions_csv', ( isset( $_POST['order_actions_csv'] ) ? absint( $_POST['order_actions_csv'] ) : 0 ) ); woo_ce_update_option( 'order_actions_xml', ( isset( $_POST['order_actions_xml'] ) ? absint( $_POST['order_actions_xml'] ) : 0 ) ); woo_ce_update_option( 'order_actions_xls', ( isset( $_POST['order_actions_xls'] ) ? absint( $_POST['order_actions_xls'] ) : 0 ) ); woo_ce_update_option( 'order_actions_xlsx', ( isset( $_POST['order_actions_xlsx'] ) ? absint( $_POST['order_actions_xlsx'] ) : 0 ) ); // Export Triggers woo_ce_update_option( 'enable_trigger_new_order', ( isset( $_POST['enable_trigger_new_order'] ) ? absint( $_POST['enable_trigger_new_order'] ) : 0 ) ); woo_ce_update_option( 'trigger_new_order_format', sanitize_text_field( $_POST['trigger_new_order_format'] ) ); woo_ce_update_option( 'trigger_new_order_method', sanitize_text_field( $_POST['trigger_new_order_method'] ) ); woo_ce_update_option( 'trigger_new_order_fields', sanitize_text_field( $_POST['trigger_new_order_fields'] ) ); $message = __( 'Changes have been saved.', 'woo_ce' ); woo_cd_admin_notice( $message ); }
function woo_ce_html_page() { global $wpdb, $export; $title = apply_filters('woo_ce_template_header', ''); woo_ce_template_header($title); woo_ce_support_donate(); $action = woo_get_action(); switch ($action) { case 'export': $message = __('Chosen WooCommerce details have been exported from your store.', 'woo_ce'); woo_ce_admin_notice($message); $output = ''; if (WOO_CE_DEBUG) { if (false === ($export_log = get_transient(WOO_CE_PREFIX . '_debug_log'))) { $export_log = __('No export entries were found, please try again with different export filters.', 'woo_ce'); } else { delete_transient(WOO_CE_PREFIX . '_debug_log'); $export_log = base64_decode($export_log); } $output = ' <h3>' . __('Export Details') . '</h3> <textarea id="export_log">' . print_r($export, true) . '</textarea><hr /> <h3>' . sprintf(__('Export Log: %s', 'woo_ce'), $export->filename) . '</h3> <textarea id="export_log">' . $export_log . '</textarea> '; } echo $output; woo_ce_manage_form(); break; case 'update': // Save Custom Product Meta if (isset($_POST['custom_products'])) { $custom_products = $_POST['custom_products']; $custom_products = explode("\n", trim($custom_products)); $size = count($custom_products); if ($size) { for ($i = 0; $i < $size; $i++) { $custom_products[$i] = trim($custom_products[$i]); } woo_ce_update_option('custom_products', $custom_products); } } // Save Custom Order Meta if (isset($_POST['custom_orders'])) { $custom_orders = $_POST['custom_orders']; $custom_orders = explode("\n", trim($custom_orders)); $size = count($custom_orders); if ($size) { for ($i = 0; $i < $size; $i++) { $custom_orders[$i] = trim($custom_orders[$i]); } woo_ce_update_option('custom_orders', $custom_orders); } } // Save Custom Order Item Meta if (isset($_POST['custom_order_items'])) { $custom_order_items = $_POST['custom_order_items']; if (!empty($custom_order_items)) { $custom_order_items = explode("\n", trim($custom_order_items)); $size = count($custom_order_items); if ($size) { for ($i = 0; $i < $size; $i++) { $custom_order_items[$i] = trim($custom_order_items[$i]); } woo_ce_update_option('custom_order_items', $custom_order_items); } } else { woo_ce_update_option('custom_order_items', ''); } } $message = __('Custom Fields saved.', 'woo_ce'); woo_ce_admin_notice($message); woo_ce_manage_form(); break; default: woo_ce_manage_form(); break; } woo_ce_template_footer(); }
function woo_ce_get_orders( $export_type = 'order', $args = array() ) { global $export; $limit_volume = -1; $offset = 0; if( $args ) { $order_ids = ( isset( $args['order_ids'] ) ? $args['order_ids'] : false ); $payment = ( isset( $args['order_payment'] ) ? $args['order_payment'] : false ); $shipping = ( isset( $args['order_shipping'] ) ? $args['order_shipping'] : false ); $user_roles = ( isset( $args['order_user_roles'] ) ? $args['order_user_roles'] : false ); $coupons = ( isset( $args['order_coupons'] ) ? $args['order_coupons'] : false ); $product = ( isset( $args['order_product'] ) ? $args['order_product'] : false ); $product_category = ( isset( $args['order_category'] ) ? $args['order_category'] : false ); $product_tag = ( isset( $args['order_tag'] ) ? $args['order_tag'] : false ); $product_brand = ( isset( $args['order_brand'] ) ? $args['order_brand'] : false ); $limit_volume = ( isset( $args['limit_volume'] ) ? $args['limit_volume'] : false ); $offset = $args['offset']; $orderby = ( isset( $args['order_orderby'] ) ? $args['order_orderby'] : 'ID' ); $order = ( isset( $args['order_order'] ) ? $args['order_order'] : 'ASC' ); $order_dates_filter = ( isset( $args['order_dates_filter'] ) ? $args['order_dates_filter'] : false ); switch( $order_dates_filter ) { case 'today': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), date( 'd' ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), date( 'd' ) ) ); break; case 'yesterday': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '-2 days' ) ), date( 'd', strtotime( '-2 days' ) ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '-1 days' ) ), date( 'd', strtotime( '-1 days' ) ) ) ); break; case 'current_week': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'this Monday' ) ), date( 'd', strtotime( 'this Monday' ) ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'next Sunday' ) ), date( 'd', strtotime( 'next Sunday' ) ) ) ); break; case 'last_week': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'last Monday' ) ), date( 'd', strtotime( 'last Monday' ) ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'last Sunday' ) ), date( 'd', strtotime( 'last Sunday' ) ) ) ); break; case 'current_month': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), 1 ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '+1 month' ) ), 0 ) ); break; case 'last_month': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '-1 month' ) ), 1 ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), 0 ) ); break; case 'manual': $order_dates_from = woo_ce_format_order_date( $args['order_dates_from'] ); $order_dates_to = woo_ce_format_order_date( $args['order_dates_to'] ); break; case 'variable': $order_filter_date_variable = $args['order_dates_filter_variable']; $order_filter_date_variable_length = $args['order_dates_filter_variable_length']; if( $order_filter_date_variable !== false && $order_filter_date_variable_length !== false ) { $timestamp = strtotime( sprintf( '-%d %s', $order_filter_date_variable, $order_filter_date_variable_length ) ); $order_dates_from = date( 'd-m-Y', mktime( date( 'H', $timestamp ), date( 'i', $timestamp ), date( 's', $timestamp ), date( 'n', $timestamp ), date( 'd', $timestamp ), date( 'Y', $timestamp ) ) ); $order_dates_to = date( 'd-m-Y', time() ); unset( $order_filter_date_variable, $order_filter_date_variable_length, $timestamp ); } break; default: $order_dates_from = false; $order_dates_to = false; break; } if( !empty( $order_dates_from ) && !empty( $order_dates_to ) ) { $order_dates_to = explode( '-', $order_dates_to ); // Check that a valid date was provided if( isset( $order_dates_to[0] ) && isset( $order_dates_to[1] ) && isset( $order_dates_to[2] ) ) $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, $order_dates_to[1], $order_dates_to[0]+1, $order_dates_to[2] ) ); else $order_dates_to = false; } $order_status = ( isset( $args['order_status'] ) ? $args['order_status'] : array() ); $user_ids = ( isset( $args['order_customer'] ) ? $args['order_customer'] : false ); $billing_country = ( isset( $args['order_billing_country'] ) ? $args['order_billing_country'] : false ); $shipping_country = ( isset( $args['order_shipping_country'] ) ? $args['order_shipping_country'] : false ); $order_items = $args['order_items']; } $post_type = 'shop_order'; $args = array( 'post_type' => $post_type, 'orderby' => $orderby, 'order' => $order, 'offset' => $offset, 'posts_per_page' => $limit_volume, 'fields' => 'ids', 'suppress_filters' => false ); $woocommerce_version = woo_get_woo_version(); // Check if this is a pre-WooCommerce 2.2 instance if( version_compare( $woocommerce_version, '2.2' ) >= 0 ) $args['post_status'] = ( function_exists( 'wc_get_order_statuses' ) ? apply_filters( 'woo_ce_order_post_status', array_keys( wc_get_order_statuses() ) ) : 'any' ); else $args['post_status'] = apply_filters( 'woo_ce_order_post_status', 'publish' ); if( !empty( $order_ids ) ) { $order_ids = explode( ',', $order_ids ); // Check if we're looking up a Sequential Order Number if( class_exists( 'WC_Seq_Order_Number' ) || class_exists( 'WC_Seq_Order_Number_Pro' ) ) { $args['meta_query'][] = array( 'key' => ( class_exists( 'WC_Seq_Order_Number_Pro' ) ? '_order_number_formatted' : '_order_number' ), 'value' => $order_ids ); } else { $size = count( $order_ids ); if( $size > 1 ) $args['post__in'] = array_map( 'absint', $order_ids ); else $args['p'] = absint( $order_ids[0] ); } } if( !empty( $payment ) ) { $args['meta_query'][] = array( 'key' => '_payment_method', 'value' => $payment ); } if( !empty( $order_status ) ) { // Check if this is a WooCommerce 2.2+ instance (new Post Status) if( version_compare( $woocommerce_version, '2.2' ) >= 0 ) { $args['post_status'] = $order_status; if( $export->cron ) { // Something weird is going on so we'll override WordPress on this one $args['post_status'] = implode( ',', $order_status ); $args['suppress_filters'] = false; add_filter( 'posts_where' , 'woo_ce_wp_query_order_where_override' ); } } else { $term_taxonomy = 'shop_order_status'; $args['tax_query'] = array( array( 'taxonomy' => $term_taxonomy, 'field' => 'slug', 'terms' => $order_status ) ); } } if( !empty( $user_ids ) ) { // Check if we're dealing with a string or list of users if( is_string( $user_ids ) ) $user_ids = explode( ',', $user_ids ); $size = count( $user_ids ); $user_emails = array(); foreach( $user_ids as $user_id ) { if( $user = get_userdata( $user_id ) ) $user_emails[] = $user->user_email; } if( !empty( $user_emails ) ) { $args['meta_query'][] = array( 'key' => '_billing_email', 'value' => $user_emails ); } unset( $user_id, $size, $user_emails ); } if( !empty( $billing_country ) ) { $args['meta_query'][] = array( 'key' => '_billing_country', 'value' => $billing_country ); } if( !empty( $shipping_country ) ) { $args['meta_query'][] = array( 'key' => '_shipping_country', 'value' => $shipping_country ); } // Filter Order dates by dropping those outside the date range if( !empty( $order_dates_from ) && !empty( $order_dates_to ) ) { $args['date_query'] = array( array( 'column' => 'post_date_gmt', 'before' => $order_dates_to, 'after' => $order_dates_from, 'inclusive' => true ) ); } if( $order_dates_filter == 'last_export' ) { $args['meta_query'][] = array( 'key' => '_woo_cd_exported', 'value' => 1, 'compare' => 'NOT EXISTS' ); } $orders = array(); // Allow other developers to bake in their own filters $args = apply_filters( 'woo_ce_get_orders_args', $args ); $order_ids = new WP_Query( $args ); // Something weird is going on so we'll override WordPress on this one if( !empty( $order_status ) && $export->cron && version_compare( $woocommerce_version, '2.2' ) >= 0 ) remove_filter( 'posts_where' , 'woo_ce_wp_query_order_where_override' ); if( $order_ids->posts ) { foreach( $order_ids->posts as $order_id ) { // Get WooCommerce Order details $order = woo_ce_get_order_wc_data( $order_id ); // Filter Orders by User Roles $order->user_id = get_post_meta( $order->id, '_customer_user', true ); if( $user_roles ) { $user_ids = array(); $size = count( $export->args['order_user_roles'] ); for( $i = 0; $i < $size; $i++ ) { $args = array( 'role' => $export->args['order_user_roles'][$i], 'fields' => 'ID' ); $user_id = get_users( $args ); $user_ids = array_merge( $user_ids, $user_id ); } if( !in_array( $order->user_id, $user_ids ) ) { unset( $order ); continue; } } // Filter Orders by Coupons $order->coupon_code = woo_ce_get_order_assoc_coupon( $order->id ); if( $coupons ) { $coupon_ids = array(); $size = count( $export->args['order_coupons'] ); for( $i = 0; $i < $size; $i++ ) $coupon_ids[] = get_the_title( $coupons[$i] ); if( !in_array( $order->coupon_code, $coupon_ids ) ) { unset( $order ); continue; } } // Filter Orders by Product if( $product ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $product_ids = array(); foreach( $order_items as $order_item ) { $product_ids = array_merge( $product_ids, (array)$order_item->product_id ); } if( count( array_intersect( $product, $product_ids ) ) == 0 ) { unset( $order ); continue; } } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Product Category if( $product_category ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $term_taxonomy = 'product_cat'; $args = array( 'fields' => 'ids' ); $category_ids = array(); foreach( $order_items as $order_item ) { if( $product_categories = wp_get_post_terms( $order_item->product_id, $term_taxonomy, $args ) ) { $category_ids = array_merge( $category_ids, $product_categories ); unset( $product_categories ); } } if( count( array_intersect( $product_category, $category_ids ) ) == 0 ) { unset( $order ); continue; } unset( $category_ids ); } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Product Tag if( $product_tag ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $term_taxonomy = 'product_tag'; $args = array( 'fields' => 'ids' ); $tag_ids = array(); foreach( $order_items as $order_item ) { if( $product_tags = wp_get_post_terms( $order_item->product_id, $term_taxonomy, $args ) ) { $tag_ids = array_merge( $tag_ids, $product_tags ); unset( $product_tags ); } } if( empty( $tag_ids ) || count( array_intersect( $product_tag, $tag_ids ) ) == 0 ) { unset( $order ); continue; } unset( $tag_ids ); } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Product Brand if( $product_brand ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $term_taxonomy = apply_filters( 'woo_ce_brand_term_taxonomy', 'product_brand' ); $args = array( 'fields' => 'ids' ); $brand_ids = array(); foreach( $order_items as $order_item ) { if( $product_brands = wp_get_post_terms( $order_item->product_id, $term_taxonomy, $args ) ) { $brand_ids = array_merge( $brand_ids, $product_brands ); unset( $product_brands ); } } if( empty( $brand_ids ) || count( array_intersect( $product_brand, $brand_ids ) ) == 0 ) { unset( $order ); continue; } unset( $brand_ids ); } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Shipping Method if( $shipping ) { $shipping_id = woo_ce_get_order_assoc_shipping_method_id( $order->id ); if( !in_array( $shipping_id, $shipping ) ) { unset( $order ); continue; } unset( $shipping_id ); } $orders[] = $order_id; // Mark this Order as exported if Since last export Date filter is used if( $order_dates_filter == 'last_export' ) { update_post_meta( $order_id, '_woo_cd_exported', 1 ); // Add an Order Note $note = __( 'Order was exported successfully.', 'woo_ce' ); $order->add_order_note( $note ); unset( $note ); } } $export->total_rows = count( $orders ); unset( $order_ids, $order_id ); } switch( $export_type ) { case 'order': if( WOO_CD_DEBUG !== true ) { if( $order_dates_filter == 'last_export' ) { // Save the Order ID's list to a WordPress Transient incase the export fails woo_ce_update_option( 'exported', $orders ); } } return $orders; break; case 'customer': $customers = array(); if( !empty( $orders ) ) { foreach( $orders as $order_id ) { $order = woo_ce_get_order_data( $order_id, 'customer', $export->args ); if( $duplicate_key = woo_ce_is_duplicate_customer( $customers, $order ) ) { $customers[$duplicate_key]->total_spent = $customers[$duplicate_key]->total_spent + woo_ce_format_price( get_post_meta( $order_id, '_order_total', true ) ); $customers[$duplicate_key]->total_orders++; if( strtolower( $order->payment_status ) == 'completed' ) $customers[$duplicate_key]->completed_orders++; } else { $customers[$order_id] = $order; $customers[$order_id]->total_spent = woo_ce_format_price( get_post_meta( $order_id, '_order_total', true ) ); $customers[$order_id]->completed_orders = 0; if( strtolower( $order->payment_status ) == 'completed' ) $customers[$order_id]->completed_orders = 1; $customers[$order_id]->total_orders = 1; } } } return $customers; break; } }