2017-05-10 16:22:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-05-12 19:15:08 +00:00
|
|
|
* Handles the product CSV exporter UI in admin.
|
2017-05-10 16:22:08 +00:00
|
|
|
*
|
|
|
|
* @author Automattic
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin
|
|
|
|
* @version 3.1.0
|
|
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Admin_Product_Export Class.
|
|
|
|
*/
|
|
|
|
class WC_Admin_Product_Export {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
add_action( 'admin_menu', array( $this, 'admin_menu' ), 55 );
|
2017-05-12 14:03:00 +00:00
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
|
2017-05-10 16:22:08 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'download_export_file' ) );
|
|
|
|
add_action( 'wp_ajax_woocommerce_do_ajax_product_export', array( $this, 'do_ajax_product_export' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add menu items.
|
|
|
|
*/
|
|
|
|
public function admin_menu() {
|
2017-05-12 19:40:54 +00:00
|
|
|
add_submenu_page( 'edit.php?post_type=product', __( 'Product Export', 'woocommerce' ), __( 'Export', 'woocommerce' ), 'edit_products', 'product_exporter', array( $this, 'admin_screen' ) );
|
2017-05-10 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 14:03:00 +00:00
|
|
|
/**
|
|
|
|
* Enqueue scripts.
|
|
|
|
*/
|
|
|
|
public function admin_scripts() {
|
|
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
|
|
|
|
wp_register_script( 'wc-product-export', WC()->plugin_url() . '/assets/js/admin/wc-product-export' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
|
2017-05-12 19:15:08 +00:00
|
|
|
wp_localize_script( 'wc-product-export', 'wc_product_export_params', array(
|
|
|
|
'export_nonce' => wp_create_nonce( 'wc-product-export' ),
|
|
|
|
) );
|
2017-05-12 14:03:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 16:22:08 +00:00
|
|
|
/**
|
|
|
|
* Export page UI.
|
|
|
|
*/
|
|
|
|
public function admin_screen() {
|
2017-05-10 19:48:56 +00:00
|
|
|
include_once( WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' );
|
2017-05-10 16:22:08 +00:00
|
|
|
include_once( dirname( __FILE__ ) . '/views/html-admin-page-product-export.php' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serve the generated file.
|
|
|
|
*/
|
|
|
|
public function download_export_file() {
|
|
|
|
if ( isset( $_GET['action'], $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'product-csv' ) && 'download_product_csv' === $_GET['action'] ) {
|
|
|
|
include_once( WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' );
|
|
|
|
$exporter = new WC_Product_CSV_Exporter();
|
|
|
|
$exporter->export();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-12 19:15:08 +00:00
|
|
|
* AJAX callback for doing the actual export to the CSV file.
|
2017-05-10 16:22:08 +00:00
|
|
|
*/
|
|
|
|
public function do_ajax_product_export() {
|
2017-05-12 19:15:08 +00:00
|
|
|
check_ajax_referer( 'wc-product-export', 'security' );
|
|
|
|
|
|
|
|
if ( ! current_user_can( 'edit_products' ) ) {
|
|
|
|
wp_die( -1 );
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:22:08 +00:00
|
|
|
include_once( WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' );
|
|
|
|
|
|
|
|
$step = absint( $_POST['step'] );
|
|
|
|
$exporter = new WC_Product_CSV_Exporter();
|
|
|
|
|
|
|
|
if ( ! empty( $_POST['columns'] ) ) {
|
|
|
|
$exporter->set_column_names( $_POST['columns'] );
|
|
|
|
}
|
2017-05-10 19:48:56 +00:00
|
|
|
|
|
|
|
if ( ! empty( $_POST['selected_columns'] ) ) {
|
|
|
|
$exporter->set_columns_to_export( $_POST['selected_columns'] );
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:48:30 +00:00
|
|
|
if ( ! empty( $_POST['export_meta'] ) ) {
|
|
|
|
$exporter->enable_meta_export( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $_POST['export_types'] ) ) {
|
|
|
|
$exporter->set_product_types_to_export( $_POST['export_types'] );
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:22:08 +00:00
|
|
|
$exporter->set_page( $step );
|
|
|
|
$exporter->generate_file();
|
|
|
|
|
2017-05-12 19:15:08 +00:00
|
|
|
if ( 100 === $exporter->get_percent_complete() ) {
|
2017-05-10 16:22:08 +00:00
|
|
|
wp_send_json_success( array(
|
|
|
|
'step' => 'done',
|
2017-05-12 19:15:08 +00:00
|
|
|
'percentage' => 100,
|
2017-05-12 19:40:54 +00:00
|
|
|
'url' => add_query_arg( array( 'nonce' => wp_create_nonce( 'product-csv' ), 'action' => 'download_product_csv' ), admin_url( 'edit.php?post_type=product&page=product_exporter' ) ),
|
2017-05-10 16:22:08 +00:00
|
|
|
) );
|
|
|
|
} else {
|
|
|
|
wp_send_json_success( array(
|
|
|
|
'step' => ++$step,
|
2017-05-12 19:15:08 +00:00
|
|
|
'percentage' => $exporter->get_percent_complete(),
|
2017-05-10 16:22:08 +00:00
|
|
|
'columns' => $exporter->get_column_names(),
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new WC_Admin_Product_Export();
|