2017-05-10 16:22:08 +00:00
< ? php
/**
2017-05-17 10:24:27 +00:00
* Init WooCommerce data exporters .
2017-05-10 16:22:08 +00:00
*
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin
2017-05-17 10:24:27 +00:00
* @ version 3.1 . 0
2017-05-10 16:22:08 +00:00
*/
2017-05-17 10:24:27 +00:00
2020-01-29 05:21:29 +00:00
use Automattic\Jetpack\Constants ;
2017-05-10 16:22:08 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
2017-05-17 10:24:27 +00:00
* WC_Admin_Exporters Class .
2017-05-10 16:22:08 +00:00
*/
2017-05-17 10:24:27 +00:00
class WC_Admin_Exporters {
/**
* Array of exporter IDs .
*
* @ var string []
*/
protected $exporters = array ();
2017-05-10 16:22:08 +00:00
/**
* Constructor .
*/
public function __construct () {
2018-04-04 13:06:13 +00:00
if ( ! $this -> export_allowed () ) {
return ;
}
2017-05-17 10:24:27 +00:00
add_action ( 'admin_menu' , array ( $this , 'add_to_menus' ) );
add_action ( 'admin_head' , array ( $this , 'hide_from_menus' ) );
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' ) );
2017-05-17 10:24:27 +00:00
// Register WooCommerce exporters.
$this -> exporters [ 'product_exporter' ] = array (
'menu' => 'edit.php?post_type=product' ,
'name' => __ ( 'Product Export' , 'woocommerce' ),
2018-04-04 08:30:49 +00:00
'capability' => 'export' ,
2017-05-17 10:24:27 +00:00
'callback' => array ( $this , 'product_exporter' ),
);
2017-05-10 16:22:08 +00:00
}
2018-04-04 13:06:13 +00:00
/**
* Return true if WooCommerce export is allowed for current user , false otherwise .
*
* @ return bool Whether current user can perform export .
*/
protected function export_allowed () {
return current_user_can ( 'edit_products' ) && current_user_can ( 'export' );
}
2017-05-10 16:22:08 +00:00
/**
2017-05-17 10:24:27 +00:00
* Add menu items for our custom exporters .
2017-05-10 16:22:08 +00:00
*/
2017-05-17 10:24:27 +00:00
public function add_to_menus () {
foreach ( $this -> exporters as $id => $exporter ) {
add_submenu_page ( $exporter [ 'menu' ], $exporter [ 'name' ], $exporter [ 'name' ], $exporter [ 'capability' ], $id , $exporter [ 'callback' ] );
}
2017-05-10 16:22:08 +00:00
}
2017-05-16 14:21:29 +00:00
/**
2017-05-17 10:24:27 +00:00
* Hide menu items from view so the pages exist , but the menu items do not .
2017-05-16 14:21:29 +00:00
*/
2017-05-17 10:24:27 +00:00
public function hide_from_menus () {
2017-05-16 14:21:29 +00:00
global $submenu ;
2017-05-17 10:24:27 +00:00
foreach ( $this -> exporters as $id => $exporter ) {
if ( isset ( $submenu [ $exporter [ 'menu' ] ] ) ) {
foreach ( $submenu [ $exporter [ 'menu' ] ] as $key => $menu ) {
if ( $id === $menu [ 2 ] ) {
unset ( $submenu [ $exporter [ 'menu' ] ][ $key ] );
}
2017-05-16 14:21:29 +00:00
}
}
}
}
2017-05-12 14:03:00 +00:00
/**
* Enqueue scripts .
*/
public function admin_scripts () {
2020-02-04 22:02:57 +00:00
$suffix = Constants :: is_true ( 'SCRIPT_DEBUG' ) ? '' : '.min' ;
$version = Constants :: get_constant ( 'WC_VERSION' );
wp_register_script ( 'wc-product-export' , WC () -> plugin_url () . '/assets/js/admin/wc-product-export' . $suffix . '.js' , array ( 'jquery' ), $version );
2018-03-05 18:59:17 +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 .
*/
2017-05-17 10:24:27 +00:00
public function product_exporter () {
2018-03-05 18:59:17 +00:00
include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' ;
include_once dirname ( __FILE__ ) . '/views/html-admin-page-product-export.php' ;
2017-05-10 16:22:08 +00:00
}
/**
* Serve the generated file .
*/
public function download_export_file () {
2017-12-07 12:35:13 +00:00
if ( isset ( $_GET [ 'action' ], $_GET [ 'nonce' ] ) && wp_verify_nonce ( wp_unslash ( $_GET [ 'nonce' ] ), 'product-csv' ) && 'download_product_csv' === wp_unslash ( $_GET [ 'action' ] ) ) { // WPCS: input var ok, sanitization ok.
2018-03-05 18:59:17 +00:00
include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' ;
2017-05-10 16:22:08 +00:00
$exporter = new WC_Product_CSV_Exporter ();
2018-01-02 13:29:23 +00:00
if ( ! empty ( $_GET [ 'filename' ] ) ) { // WPCS: input var ok.
$exporter -> set_filename ( wp_unslash ( $_GET [ 'filename' ] ) ); // WPCS: input var ok, sanitization ok.
}
2017-05-10 16:22:08 +00:00
$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' );
2018-04-04 13:06:13 +00:00
if ( ! $this -> export_allowed () ) {
wp_send_json_error ( array ( 'message' => __ ( 'Insufficient privileges to export products.' , 'woocommerce' ) ) );
2017-05-12 19:15:08 +00:00
}
2018-03-05 18:59:17 +00:00
include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' ;
2017-05-10 16:22:08 +00:00
2017-12-07 12:35:13 +00:00
$step = isset ( $_POST [ 'step' ] ) ? absint ( $_POST [ 'step' ] ) : 1 ; // WPCS: input var ok, sanitization ok.
2017-05-10 16:22:08 +00:00
$exporter = new WC_Product_CSV_Exporter ();
2017-12-07 12:35:13 +00:00
if ( ! empty ( $_POST [ 'columns' ] ) ) { // WPCS: input var ok.
$exporter -> set_column_names ( wp_unslash ( $_POST [ 'columns' ] ) ); // WPCS: input var ok, sanitization ok.
2017-05-10 16:22:08 +00:00
}
2017-05-10 19:48:56 +00:00
2017-12-07 12:35:13 +00:00
if ( ! empty ( $_POST [ 'selected_columns' ] ) ) { // WPCS: input var ok.
$exporter -> set_columns_to_export ( wp_unslash ( $_POST [ 'selected_columns' ] ) ); // WPCS: input var ok, sanitization ok.
2017-05-10 19:48:56 +00:00
}
2017-12-07 12:35:13 +00:00
if ( ! empty ( $_POST [ 'export_meta' ] ) ) { // WPCS: input var ok.
2017-05-12 13:48:30 +00:00
$exporter -> enable_meta_export ( true );
}
2017-12-07 12:35:13 +00:00
if ( ! empty ( $_POST [ 'export_types' ] ) ) { // WPCS: input var ok.
$exporter -> set_product_types_to_export ( wp_unslash ( $_POST [ 'export_types' ] ) ); // WPCS: input var ok, sanitization ok.
2018-07-23 09:59:01 +00:00
}
2018-08-07 14:58:39 +00:00
if ( ! empty ( $_POST [ 'export_category' ] ) && is_array ( $_POST [ 'export_category' ] ) ) { // WPCS: input var ok.
$exporter -> set_product_category_to_export ( wp_unslash ( array_values ( $_POST [ 'export_category' ] ) ) ); // WPCS: input var ok, sanitization ok.
2017-05-12 13:48:30 +00:00
}
2018-01-02 13:16:43 +00:00
if ( ! empty ( $_POST [ 'filename' ] ) ) { // WPCS: input var ok.
$exporter -> set_filename ( wp_unslash ( $_POST [ 'filename' ] ) ); // WPCS: input var ok, sanitization ok.
}
2017-05-10 16:22:08 +00:00
$exporter -> set_page ( $step );
$exporter -> generate_file ();
2018-03-05 18:59:17 +00:00
$query_args = apply_filters (
'woocommerce_export_get_ajax_query_args' ,
array (
'nonce' => wp_create_nonce ( 'product-csv' ),
'action' => 'download_product_csv' ,
'filename' => $exporter -> get_filename (),
)
);
2017-12-20 20:20:02 +00:00
2017-05-12 19:15:08 +00:00
if ( 100 === $exporter -> get_percent_complete () ) {
2018-03-05 18:59:17 +00:00
wp_send_json_success (
array (
'step' => 'done' ,
'percentage' => 100 ,
'url' => add_query_arg ( $query_args , admin_url ( 'edit.php?post_type=product&page=product_exporter' ) ),
)
);
2017-05-10 16:22:08 +00:00
} else {
2018-03-05 18:59:17 +00:00
wp_send_json_success (
array (
'step' => ++ $step ,
'percentage' => $exporter -> get_percent_complete (),
'columns' => $exporter -> get_column_names (),
)
);
2017-05-10 16:22:08 +00:00
}
}
2021-01-27 03:43:14 +00:00
/**
* Gets the product types that can be exported .
*
* @ since 5.1 . 0
* @ return array The product types keys and labels .
*/
public static function get_product_types () {
$product_types = wc_get_product_types ();
$product_types [ 'variation' ] = __ ( 'Product variations' , 'woocommerce' );
/**
* Allow third - parties to filter the exportable product types .
*
* @ since 5.1 . 0
* @ param array $product_types {
* The product type key and label .
*
* @ type string Product type key - eg 'variable' , 'simple' etc .
* @ type string A translated product label which appears in the export product type dropdown .
* }
*/
return apply_filters ( 'woocommerce_exporter_product_types' , $product_types );
}
2017-05-10 16:22:08 +00:00
}
2017-05-17 10:24:27 +00:00
new WC_Admin_Exporters ();