woocommerce/includes/admin/class-wc-admin-exporters.php

153 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* Init WooCommerce data exporters.
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 3.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Admin_Exporters Class.
*/
class WC_Admin_Exporters {
/**
* Array of exporter IDs.
*
* @var string[]
*/
protected $exporters = array();
/**
* Constructor.
*/
public function __construct() {
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' ) );
add_action( 'admin_init', array( $this, 'download_export_file' ) );
add_action( 'wp_ajax_woocommerce_do_ajax_product_export', array( $this, 'do_ajax_product_export' ) );
// Register WooCommerce exporters.
$this->exporters['product_exporter'] = array(
'menu' => 'edit.php?post_type=product',
'name' => __( 'Product Export', 'woocommerce' ),
'capability' => 'edit_products',
'callback' => array( $this, 'product_exporter' ),
);
}
/**
* Add menu items for our custom exporters.
*/
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-16 14:21:29 +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
*/
public function hide_from_menus() {
2017-05-16 14:21:29 +00:00
global $submenu;
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() {
$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
}
/**
* Export page UI.
*/
public function product_exporter() {
include_once( WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' );
include_once( dirname( __FILE__ ) . '/views/html-admin-page-product-export.php' );
}
/**
* 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.
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.
*/
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 );
}
include_once( WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php' );
2017-12-07 12:35:13 +00:00
$step = isset( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; // WPCS: input var ok, sanitization ok.
$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 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.
2017-05-12 13:48:30 +00:00
}
$exporter->set_page( $step );
$exporter->generate_file();
2017-05-12 19:15:08 +00:00
if ( 100 === $exporter->get_percent_complete() ) {
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' ) ),
) );
} else {
wp_send_json_success( array(
'step' => ++$step,
2017-05-12 19:15:08 +00:00
'percentage' => $exporter->get_percent_complete(),
'columns' => $exporter->get_column_names(),
) );
}
}
}
new WC_Admin_Exporters();