/** * Performs an HTTP POST to the specified URL with the CSV * * @since 3.0 * @param string $filename unused * @param string $csv the CSV to include the HTTP POST body * @throws Exception WP HTTP error handling */ public function perform_action($filename, $csv) { $args = apply_filters('wc_customer_order_csv_export_http_post_args', array('timeout' => 60, 'redirection' => 0, 'httpversion' => '1.0', 'sslverify' => true, 'blocking' => true, 'headers' => array('accept' => 'text/csv', 'content-type' => 'text/csv'), 'body' => $csv, 'cookies' => array(), 'user-agent' => "WordPress " . $GLOBALS['wp_version'])); $response = wp_safe_remote_post(get_option('wc_customer_order_csv_export_http_post_url'), $args); // check for errors if (is_wp_error($response)) { throw new Exception($response->get_error_message()); } // log responses wc_customer_order_csv_export()->log(print_r($response, true)); }
/** * Helper function to check the export format * * @param \WC_Customer_Order_CSV_Export_Generator $csv_generator the generator instance * @return bool - true if this is a one row per item format */ function sv_wc_csv_export_is_one_row($csv_generator) { $one_row_per_item = false; if (version_compare(wc_customer_order_csv_export()->get_version(), '4.0.0', '<')) { // pre 4.0 compatibility $one_row_per_item = 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format; } elseif (isset($csv_generator->format_definition)) { // post 4.0 (requires 4.0.3+) $one_row_per_item = 'item' === $csv_generator->format_definition['row_type']; } return $one_row_per_item; }
/** * Add `item_count` column data * * @param array $order_data the original column data * @param \WC_Order $order the order object * @param \WC_Customer_Order_CSV_Export_Generator $csv_generator the generator instance * @return array - the updated column data */ function sv_wc_csv_export_add_item_count_data($order_data, $order, $csv_generator) { $new_order_data = array(); $one_row_per_item = false; $custom_data = array('item_count' => $order->get_item_count()); if (version_compare(wc_customer_order_csv_export()->get_version(), '4.0.0', '<')) { // pre 4.0 compatibility $one_row_per_item = 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format; } elseif (isset($csv_generator->format_definition)) { // post 4.0 (requires 4.0.3+) $one_row_per_item = 'item' === $csv_generator->format_definition['row_type']; } if ($one_row_per_item) { foreach ($order_data as $data) { $new_order_data[] = array_merge((array) $data, $custom_data); } } else { $new_order_data = array_merge($order_data, $custom_data); } return $new_order_data; }
/** * Returns the export method object * * @since 3.0 * @param string $method the export method, `download`, `ftp`, `http_post`, or `email` * @return object the export method */ private function get_export_method($method) { // get the export method specified switch ($method) { case 'download': return wc_customer_order_csv_export()->load_class('/includes/export-methods/class-wc-customer-order-csv-export-method-download.php', 'WC_Customer_Order_CSV_Export_Method_Download'); case 'ftp': // abstract FTP class require_once wc_customer_order_csv_export()->get_plugin_path() . '/includes/export-methods/ftp/abstract-wc-customer-order-csv-export-method-file-transfer.php'; $security = get_option('wc_customer_order_csv_export_ftp_security'); switch ($security) { // FTP over SSH case 'sftp': return wc_customer_order_csv_export()->load_class('/includes/export-methods/ftp/class-wc-customer-order-csv-export-method-sftp.php', 'WC_Customer_Order_CSV_Export_Method_SFTP'); // FTP with Implicit SSL // FTP with Implicit SSL case 'ftp_ssl': return wc_customer_order_csv_export()->load_class('/includes/export-methods/ftp/class-wc-customer-order-csv-export-method-ftp-implicit-ssl.php', 'WC_Customer_Order_CSV_Export_Method_FTP_Implicit_SSL'); // FTP with explicit SSL/TLS *or* regular FTP // FTP with explicit SSL/TLS *or* regular FTP case 'ftps': case 'none': return wc_customer_order_csv_export()->load_class('/includes/export-methods/ftp/class-wc-customer-order-csv-export-method-ftp.php', 'WC_Customer_Order_CSV_Export_Method_FTP'); } break; case 'http_post': return wc_customer_order_csv_export()->load_class('/includes/export-methods/class-wc-customer-order-csv-export-method-http-post.php', 'WC_Customer_Order_CSV_Export_Method_HTTP_POST'); case 'email': return wc_customer_order_csv_export()->load_class('/includes/export-methods/class-wc-customer-order-csv-export-method-email.php', 'WC_Customer_Order_CSV_Export_Method_Email'); default: /** * CSV Export Get Export Method * * Triggered when getting the export method. This is designed for * custom methods to hook in and load their class so it can be * returned and used. * * @since 3.4.0 * @param \WC_Customer_Order_CSV_Export_Handler $this, handler instance */ do_action('wc_customer_order_csv_export_get_export_method', $this); $class_name = sprintf('WC_Customer_Order_CSV_Export_Custom_Method_%s', ucwords(strtolower($method))); return class_exists($class_name) ? new $class_name() : null; } }
/** * Returns settings array for use by output/save functions * * @since 3.0 * @param string $tab_id * @return array */ public static function get_settings($tab_id) { $order_statuses = wc_get_order_statuses(); // get the scheduled export time to display to user if ($scheduled_timestamp = wp_next_scheduled('wc_customer_order_csv_export_auto_export_orders')) { $scheduled_desc = sprintf(__('The next export is scheduled on <code>%s</code>', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), get_date_from_gmt(date('Y-m-d H:i:s', $scheduled_timestamp), wc_date_format() . ' ' . wc_time_format())); } else { $scheduled_desc = __('The export is not scheduled.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN); } $settings = array('export' => array(array('name' => __('Export', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_customer_order_csv_export_type', 'name' => __('Export Orders or Customers', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'radio', 'options' => array('orders' => __('Orders', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'customers' => __('Customers', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)), 'default' => 'orders'), array('type' => 'sectionend'), array('name' => __('Export Options', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_customer_order_csv_export_statuses', 'name' => __('Order Statuses', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Orders with these statuses will be included in the export.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'multiselect', 'options' => $order_statuses, 'default' => '', 'class' => 'wc-enhanced-select chosen_select show_if_orders', 'css' => 'min-width: 250px'), array('id' => 'wc_customer_order_csv_export_start_date', 'name' => __('Start Date', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => __('Start date of customers or orders to include in the exported file, in the format <code>YYYY-MM-DD.</code>', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_end_date', 'name' => __('End Date', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => __('End date of customers or orders to include in the exported file, in the format <code>YYYY-MM-DD.</code>', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_limit', 'name' => __('Limit Records', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => __('Limit the number of rows to be exported. Use this option when exporting very large files that are unable to complete in a single attempt.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'number', 'custom_attributes' => array('min' => 0)), array('id' => 'wc_customer_order_csv_export_offset', 'name' => __('Offset Records', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => __('Set the number of records to be skipped in this export. Use this option when exporting very large files that are unable to complete in a single attempt.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'number', 'custom_attributes' => array('min' => 0)), array('type' => 'sectionend')), 'settings' => array(array('name' => __('Export Format', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_customer_order_csv_export_order_format', 'name' => __('Order Export Format', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Default is a new format for v3.0, Import matches the Customer/Order CSV Import plugin format, and legacy is prior to version 3', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'select', 'options' => array('default' => __('Default', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default_one_row_per_item' => __('Default - One Row per Item', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'import' => __('CSV Import', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'legacy_one_row_per_item' => __('Legacy - One Row per Item', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'legacy_single_column' => __('Legacy - Single Column for all Items', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)), 'default' => 'default'), array('id' => 'wc_customer_order_csv_export_customer_format', 'name' => __('Customer Export Format', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Default is a new format for v3.0, Import matches the Customer/Order CSV Import plugin format, Legacy is prior to version 3', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'select', 'options' => array('default' => __('Default', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'import' => __('CSV Import', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'legacy' => __('Legacy', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)), 'default' => 'default'), array('id' => 'wc_customer_order_csv_export_order_filename', 'name' => __('Order Export Filename', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('The filename for exported orders. Merge variables: %%timestamp%%, %%order_ids%%', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => 'orders-export-%%timestamp%%.csv', 'css' => 'min-width: 300px;', 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_customer_filename', 'name' => __('Customer Export Filename', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('The filename for exported customers. Merge variables: %%timestamp%%', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => 'customers-export-%%timestamp%%.csv', 'css' => 'min-width: 300px;', 'type' => 'text'), array('type' => 'sectionend'), array('name' => __('Automated Export Settings', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_customer_order_csv_export_auto_export_method', 'name' => __('Automatically Export Orders', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Enable this to automatically export orders via the method & schedule selected.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'select', 'options' => array('disabled' => __('Disabled', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'ftp' => __('via FTP', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'http_post' => __('via HTTP POST', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'email' => __('via Email', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)), 'default' => 'disabled'), array('id' => 'wc_customer_order_csv_export_auto_export_start_time', 'name' => __('Export Start Time', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Any new orders will start exporting at this time.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => sprintf(__('Local time is <code>%s</code>.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), date_i18n(wc_time_format())) . ' ' . $scheduled_desc, 'default' => '', 'type' => 'text', 'css' => 'max-width: 100px;', 'class' => 'js-wc-customer-order-csv-export-auto-export-timepicker'), array('id' => 'wc_customer_order_csv_export_auto_export_interval', 'name' => __('Export Interval (in minutes)*', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Any new orders will be exported on this schedule.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => __('Required in order to schedule the automatic export.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => '30', 'type' => 'text', 'css' => 'max-width: 50px;'), array('id' => 'wc_customer_order_csv_export_auto_export_statuses', 'name' => __('Order Statuses', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Orders with these statuses will be included in the export.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'multiselect', 'options' => $order_statuses, 'default' => '', 'class' => 'wc-enhanced-select chosen_select', 'css' => 'min-width: 250px'), array('type' => 'sectionend'), array('id' => 'wc_customer_order_csv_export_ftp_settings', 'name' => __('FTP Settings', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_customer_order_csv_export_ftp_server', 'name' => __('Server Address', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('The address of the remote FTP server to upload to.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => '', 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_ftp_username', 'name' => __('Username', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('The username for the remote FTP server.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => '', 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_ftp_password', 'name' => __('Password', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('The password for the remote FTP server.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => '', 'type' => 'password'), array('id' => 'wc_customer_order_csv_export_ftp_port', 'name' => __('Port', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('The password for the remote FTP server.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => '21', 'type' => 'text', 'style' => 'max-width: 50px;'), array('id' => 'wc_customer_order_csv_export_ftp_path', 'name' => __('Initial Path', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('The initial path for the remote FTP server with trailing slash, but excluding leading slash.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => '', 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_ftp_security', 'name' => __('Security', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Select the security type for the remote FTP server.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => 'none', 'options' => array('none' => __('None', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'ftp_ssl' => __('FTP with Implicit SSL', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'ftps' => __('FTP with Explicit TLS/SSL', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'sftp' => __('SFTP (FTP over SSH)', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)), 'type' => 'select'), array('id' => 'wc_customer_order_csv_export_ftp_passive_mode', 'name' => __('Passive Mode', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => __('Enable passive mode if you are having issues connecting to FTP, especially if you see "PORT command successful" in the error log.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => 'no', 'type' => 'checkbox'), array('id' => 'wc_customer_order_csv_export_test_button', 'name' => __('Test FTP', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'method' => 'ftp', 'type' => 'csv_test_button'), array('type' => 'sectionend'), array('id' => 'wc_customer_order_csv_export_post_settings', 'name' => __('HTTP POST Settings', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_customer_order_csv_export_http_post_url', 'name' => __('HTTP POST URL', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Enter the URL to POST the exported CSV to.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => '', 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_test_button', 'name' => __('Test HTTP POST', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'method' => 'http_post', 'type' => 'csv_test_button'), array('type' => 'sectionend'), array('id' => 'wc_customer_order_csv_export_email_settings', 'name' => __('Email Settings', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_customer_order_csv_export_email_recipients', 'name' => __('Recipient(s)', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => sprintf(__('Enter recipients (comma separated) the exported CSV should be emailed to. Defaults to <em>%s</em>.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), esc_attr(get_option('admin_email'))), 'default' => '', 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_email_subject', 'name' => __('Email Subject', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc_tip' => __('Enter the email subject.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'default' => sprintf(__('[%s] Order CSV Export', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), get_option('blogname')), 'type' => 'text'), array('id' => 'wc_customer_order_csv_export_test_button', 'name' => __('Test Email', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'method' => 'email', 'type' => 'csv_test_button'), array('type' => 'sectionend'))); if (wc_customer_order_csv_export()->is_plugin_active('woocommerce-subscriptions.php')) { $settings['export'][] = array('name' => __('Subscriptions Options', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'title'); $settings['export'][] = array('id' => 'wc_customer_order_csv_export_subscription_orders', 'title' => __('Export Subscriptions Orders Only', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'desc' => __('Export subscription orders', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'checkbox', 'checkboxgroup' => 'start'); $settings['export'][] = array('id' => 'wc_customer_order_csv_export_subscription_renewals', 'desc' => __('Export renewal orders', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), 'type' => 'checkbox', 'checkboxgroup' => 'end'); $settings['export'][] = array('type' => 'sectionend'); } /** * Allow actors to add or remove settings from the CSV export pages. * * @since 3.0.6 * @param array $settings an array of settings for the given tab * @param string $tab_id current tab ID */ return apply_filters('wc_customer_order_csv_export_settings', $settings[$tab_id], $tab_id); }
/** * Modify the WP_User_Query to support filtering on the date the customer was created * * @since 3.0.5 * @param WP_User_Query $query */ public function modify_user_query($query) { if (wc_customer_order_csv_export()->admin->customer_export_start_date) { $query->query_where .= sprintf(" AND user_registered >= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i' )", esc_sql(wc_customer_order_csv_export()->admin->customer_export_start_date)); } if (wc_customer_order_csv_export()->admin->customer_export_end_date) { $query->query_where .= sprintf(" AND user_registered <= STR_TO_DATE( '%s', '%%Y-%%m-%%d %%h:%%i' )", esc_sql(wc_customer_order_csv_export()->admin->customer_export_end_date)); } }
/** * Output the System Status report table * * @since 3.11.0 */ public function add_system_status_report() { include wc_customer_order_csv_export()->get_plugin_path() . '/includes/admin/views/html-system-status-table.php'; }
function init_woocommerce_customer_order_csv_export() { /** * # WooCommerce Customer/Order CSV Export * * ## Plugin Overview * * This plugin exports customers and orders in CSV format. Customers can be exported via * CSV Export > Export and are selected from orders in a selectable date range. Orders can be * exported in bulk from CSV Export > Export and from the Orders / Edit Order screen, as well as auto-exported * via FTP and HTTP POST on a recurring schedule. * * ## Class Description * * The main class for Customer/Order CSV Export. This class handles general lifecycle and setup functions, as well * as marking new orders as un-exported and handling the AJAX export action on the Order screen. * * ## Admin Considerations * * A 'CSV Export' sub-menu item is added under 'WooCommerce', with two tabs: 'Export' for handling bulk exports of * both customers and orders, and 'Settings' which define the output format for both customers and orders, as well as * auto-export interval & FTP/HTTP POST settings. * * An 'Export Status' column is added to the Orders list table, along with a new order action icon for downloading the order * to a CSV. Another order action is added to the Edit Order screen under the order actions select box. * * ## Database * * ### Options Table * * + `wc_customer_order_csv_export_order_format` - order export format * + `wc_customer_order_csv_export_customer_format` - customer export format * + `wc_customer_order_csv_export_order_filename` - filename used for order exports * + `wc_customer_order_csv_export_customer_filename` - filename used for customer exports * + `wc_customer_order_csv_export_auto_export_method` - export method for auto-exports, defaults to 'disabled' * + `wc_customer_order_csv_export_auto_export_interval` - export interval for auto-exports, in minutes * + `wc_customer_order_csv_export_auto_export_statuses` - array of order statuses that are valid for auto-export * + `wc_customer_order_csv_export_ftp_server` - FTP server * + `wc_customer_order_csv_export_ftp_username` - FTP username * + `wc_customer_order_csv_export_ftp_password` - FTP password * + `wc_customer_order_csv_export_ftp_port` - FTP port * + `wc_customer_order_csv_export_ftp_path` - FTP initial path * + `wc_customer_order_csv_export_ftp_security` - type of FTP security, e.g. 'sftp' * + `wc_customer_order_csv_export_passive_mode` - whether to enable passive mode for FTP connections * + `wc_customer_order_csv_export_http_post_url` - the URL to POST exported CSV data to, when HTTP POST is enabled as a method * + `wc_customer_order_csv_export_version` the plugin version, set on install & upgrade * * ### Order Meta * * + `_wc_customer_order_csv_export_is_exported` - bool, indicates if an order has been auto-exported or not, set on post insert * ## Cron * * + `wc_customer_order_csv_export_auto_export_interval` - custom interval for auto-export action * + `wc_customer_order_csv_export_auto_export_orders` - custom hook for auto-exporting orders * */ class WC_Customer_Order_CSV_Export extends SV_WC_Plugin { /** plugin version number */ const VERSION = '3.10.2'; /** @var WC_Customer_Order_CSV_Export single instance of this plugin */ protected static $instance; /** plugin id */ const PLUGIN_ID = 'customer_order_csv_export'; /** plugin text domain */ const TEXT_DOMAIN = 'woocommerce-customer-order-csv-export'; /** @var \WC_Customer_Order_CSV_Export_Admin instance */ public $admin; /** @var \WC_Customer_Order_CSV_Export_Compatibility instance */ public $compatibility; /** @var \WC_Customer_Order_CSV_Export_Cron instance */ public $cron; /** * Setup main plugin class * * @since 3.0 * @return \WC_Customer_Order_CSV_Export */ public function __construct() { parent::__construct(self::PLUGIN_ID, self::VERSION, self::TEXT_DOMAIN); // required files $this->includes(); // Set orders as not-exported when created add_action('wp_insert_post', array($this, 'mark_order_not_exported'), 10, 2); // Admin if (is_admin()) { // handle single order CSV export download from order action button add_action('wp_ajax_wc_customer_order_csv_export_export_order', array($this, 'process_ajax_export_order')); if (!defined('DOING_AJAX')) { $this->admin_includes(); } } // clear schduled events on deactivation register_deactivation_hook($this->get_file(), array($this->cron, 'clear_scheduled_export')); } /** * Set each new order as not exported. This is done because querying orders that have a specific meta key / value * is much more reliable than querying orders that don't have a specific meta key / value AND prevents accidental * export of a massive set of old orders on first run * * @since 3.0 * @param int $post_id new order ID * @param object $post the post object */ public function mark_order_not_exported($post_id, $post) { if ('shop_order' == $post->post_type) { // force unique, because oddly this can be invoked when changing the status of an existing order add_post_meta($post_id, '_wc_customer_order_csv_export_is_exported', 0, true); } } /** * Downloads order in CSV format (from order action button on 'Orders' page) * * @since 3.0 */ public function process_ajax_export_order() { if (!is_admin() || !current_user_can('edit_posts')) { wp_die(__('You do not have sufficient permissions to access this page.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } if (!check_admin_referer('wc_customer_order_csv_export_export_order')) { wp_die(__('You have taken too long, please go back and try again.', WC_Customer_Order_CSV_Export::TEXT_DOMAIN)); } $order_id = !empty($_GET['order_id']) ? absint($_GET['order_id']) : ''; if (!$order_id) { die; } $export = new WC_Customer_Order_CSV_Export_Handler($order_id); $export->download(); wp_redirect(wp_get_referer()); exit; } /** * Includes required classes * * @since 3.0 */ public function includes() { // handles exporting / uploading / emailing require_once $this->get_plugin_path() . '/includes/class-wc-customer-order-csv-export-handler.php'; require_once $this->get_plugin_path() . '/includes/export-methods/interface-wc-customer-order-csv-export-method.php'; // handles generating CSV require_once $this->get_plugin_path() . '/includes/class-wc-customer-order-csv-export-generator.php'; // compatibility for legacy export formats require_once $this->get_plugin_path() . '/includes/class-wc-customer-order-csv-export-compatibility.php'; $this->compatibility = new WC_Customer_Order_CSV_Export_Compatibility(); // handles scheduling and execution of automatic export / upload require_once $this->get_plugin_path() . '/includes/class-wc-customer-order-csv-export-cron.php'; $this->cron = new WC_Customer_Order_CSV_Export_Cron(); } /** * Loads the Admin & AJAX classes * * @since 3.0 */ public function admin_includes() { // loads the admin settings page and adds functionality to the order admin require_once $this->get_plugin_path() . '/includes/admin/class-wc-customer-order-csv-export-admin.php'; $this->admin = new WC_Customer_Order_CSV_Export_Admin(); // message handler $this->admin->message_handler = $this->get_message_handler(); } /** * Load plugin text domain. * * @since 3.0 * @see SV_WC_Plugin::load_translation() */ public function load_translation() { load_plugin_textdomain('woocommerce-customer-order-csv-export', false, dirname(plugin_basename($this->get_file())) . '/i18n/languages'); } /** Admin Methods ******************************************************/ /** * Render a notice for the user to select their desired export format * * @since 3.4.0 * @see SV_WC_Plugin::add_admin_notices() */ public function add_admin_notices() { // show any dependency notices parent::add_admin_notices(); // add notice for selecting export format $this->get_admin_notice_handler()->add_admin_notice(sprintf(__('Thanks for installing the Customer/Order CSV Export plugin! To get started, please %sset your export format%s. ', WC_Customer_Order_CSV_Export::TEXT_DOMAIN), '<a href="' . $this->get_settings_url() . '">', '</a>'), 'export-format-notice', array('always_show_on_settings' => false, 'notice_class' => 'updated')); } /** Helper Methods ******************************************************/ /** * Main Customer/Order CSV Export Instance, ensures only one instance is/can be loaded * * @since 3.9.0 * @see wc_customer_order_csv_export() * @return WC_Customer_Order_CSV_Export */ public static function instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * Returns the plugin name, localized * * @since 3.0 * @see SV_WC_Plugin::get_plugin_name() * @return string the plugin name */ public function get_plugin_name() { return __('WooCommerce Customer/Order CSV Export', self::TEXT_DOMAIN); } /** * Returns __FILE__ * * @since 3.0 * @see SV_WC_Plugin::get_file() * @return string the full path and filename of the plugin file */ protected function get_file() { return __FILE__; } /** * Gets the plugin documentation url, which for Customer/Order CSV Export is non-standard * * @since 3.0.0 * @see SV_WC_Plugin::get_documentation_url() * @return string documentation URL */ public function get_documentation_url() { return 'http://docs.woothemes.com/document/ordercustomer-csv-exporter/'; } /** * Gets the plugin support URL * * @since 3.10.0 * @see SV_WC_Plugin::get_support_url() * @return string */ public function get_support_url() { return 'http://support.woothemes.com/'; } /** * Gets the URL to the settings page * * @since 3.0 * @see SV_WC_Plugin::is_plugin_settings() * @param string $_ unused * @return string URL to the settings page */ public function get_settings_url($_ = '') { return admin_url('admin.php?page=wc_customer_order_csv_export&tab=settings'); } /** * Returns conditional dependencies based on the FTP security selected * * @since 3.0 * @see SV_WC_Plugin::get_dependencies() * @return array of dependencies */ protected function get_dependencies() { // check if FTP is the chosen method if ('ftp' !== get_option('wc_customer_order_csv_export_auto_export_method')) { return array(); } $ftp_security = get_option('wc_customer_order_csv_export_ftp_security'); if ('sftp' == $ftp_security) { return array('ssh2'); } if ('ftp_ssl' == $ftp_security) { return array('curl'); } if ('ftps' == $ftp_security) { return array('ftp', 'openssl'); } return array(); } /** * Returns conditional function dependencies based on the FTP security selected * * @since 3.1 * @see SV_WC_Plugin::get_function_dependencies() * @return array of dependencies */ protected function get_function_dependencies() { // check if FTP is the chosen method if ('ftp' !== get_option('wc_customer_order_csv_export_auto_export_method')) { return array(); } $ftp_security = get_option('wc_customer_order_csv_export_ftp_security'); if ('ftps' == $ftp_security) { return array('ftp_ssl_connect'); } return array(); } /** Lifecycle Methods ******************************************************/ /** * Install default settings * * @since 3.0 * @see SV_WC_Plugin::install() */ protected function install() { require_once $this->get_plugin_path() . '/includes/admin/class-wc-customer-order-csv-export-admin.php'; foreach (WC_Customer_Order_CSV_Export_Admin::get_settings('settings') as $setting) { if (isset($setting['default'])) { update_option($setting['id'], $setting['default']); } } } /** * Upgrade to $installed_version * * @since 3.0.4 * @see SV_WC_Plugin::upgrade() */ protected function upgrade($installed_version) { // upgrade to 3.0.4 if (version_compare($installed_version, '3.0.4', '<')) { // wc_customer_order_csv_export_passive_mode > wc_customer_order_csv_export_ftp_passive_mode update_option('wc_customer_order_csv_export_ftp_passive_mode', get_option('wc_customer_order_csv_export_passive_mode')); delete_option('wc_customer_order_csv_export_passive_mode'); } // upgrate to 3.4.0 if (version_compare($installed_version, '3.4.0', '<')) { // update order statuses for 2.2+ $order_status_options = array('wc_customer_order_csv_export_statuses', 'wc_customer_order_csv_export_auto_export_statuses'); foreach ($order_status_options as $option) { $order_statuses = (array) get_option($option); $new_order_statuses = array(); foreach ($order_statuses as $status) { $new_order_statuses[] = 'wc-' . $status; } update_option($option, $new_order_statuses); } } } } // end \WC_Customer_Order_CSV_Export class /** * Returns the One True Instance of Customer/Order CSV Export * * @since 3.9.0 * @return <class name> */ function wc_customer_order_csv_export() { return WC_Customer_Order_CSV_Export::instance(); } /** * The WC_Customer_Order_CSV_Export global object * * @deprecated 3.9.0 * @name $wc_customer_order_csv_export * @global WC_Customer_Order_CSV_Export $GLOBALS['wc_customer_order_csv_export'] */ $GLOBALS['wc_customer_order_csv_export'] = wc_customer_order_csv_export(); }