/var/www/html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-exporters.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * Init WooCommerce data exporters.
 *
 * @package     WooCommerce\Admin
 * @version     3.1.0
 */

use Automattic\Jetpack\Constants;
use 
Automattic\WooCommerce\Enums\ProductType;

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() {
        if ( ! 
$this->export_allowed() ) {
            return;
        }

        
add_action'admin_menu', array( $this'add_to_menus' ) );
        
add_action'admin_head', array( $this'hide_from_menus' ) );
        
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' => 'export',
            
'callback'   => array( $this'product_exporter' ),
        );
    }

    
/**
     * 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' );
    }

    
/**
     * 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'] );
        }
    }

    
/**
     * Hide menu items from view so the pages exist, but the menu items do not.
     */
    
public function hide_from_menus() {
        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 ] );
                    }
                }
            }
        }
    }

    
/**
     * Enqueue scripts.
     */
    
public function admin_scripts() {
        
$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 );
        
wp_localize_script(
            
'wc-product-export',
            
'wc_product_export_params',
            array(
                
'export_nonce' => wp_create_nonce'wc-product-export' ),
            )
        );
    }

    
/**
     * 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() {
        if ( isset( 
$_GET['action'], $_GET['nonce'] ) && wp_verify_noncewp_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();

            if ( ! empty( 
$_GET['filename'] ) ) { // WPCS: input var ok.
                
$exporter->set_filenamewp_unslash$_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
            
}

            
$exporter->export();
        }
    }

    
/**
     * AJAX callback for doing the actual export to the CSV file.
     */
    
public function do_ajax_product_export() {
        
check_ajax_referer'wc-product-export''security' );

        if ( ! 
$this->export_allowed() ) {
            
wp_send_json_error( array( 'message' => __'Insufficient privileges to export products.''woocommerce' ) ) );
        }

        include_once 
WC_ABSPATH 'includes/export/class-wc-product-csv-exporter.php';

        
$step     = isset( $_POST['step'] ) ? absint$_POST['step'] ) : 1// WPCS: input var ok, sanitization ok.
        
$exporter = new WC_Product_CSV_Exporter();

        if ( ! empty( 
$_POST['columns'] ) ) { // WPCS: input var ok.
            
$exporter->set_column_nameswp_unslash$_POST['columns'] ) ); // WPCS: input var ok, sanitization ok.
        
}

        if ( ! empty( 
$_POST['selected_columns'] ) ) { // WPCS: input var ok.
            
$exporter->set_columns_to_exportwp_unslash$_POST['selected_columns'] ) ); // WPCS: input var ok, sanitization ok.
        
}

        if ( ! empty( 
$_POST['export_meta'] ) ) { // WPCS: input var ok.
            
$exporter->enable_meta_exporttrue );
        }

        if ( ! empty( 
$_POST['export_types'] ) ) { // WPCS: input var ok.
            
$exporter->set_product_types_to_exportwp_unslash$_POST['export_types'] ) ); // WPCS: input var ok, sanitization ok.
        
}

        if ( ! empty( 
$_POST['export_category'] ) && is_array$_POST['export_category'] ) ) {// WPCS: input var ok.
            
$exporter->set_product_category_to_exportwp_unslasharray_values$_POST['export_category'] ) ) ); // WPCS: input var ok, sanitization ok.
        
}

        if ( ! empty( 
$_POST['filename'] ) ) { // WPCS: input var ok.
            
$exporter->set_filenamewp_unslash$_POST['filename'] ) ); // WPCS: input var ok, sanitization ok.
        
}

        
$exporter->set_page$step );
        
$exporter->generate_file();

        
$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(),
            )
        );

        if ( 
100 === $exporter->get_percent_complete() ) {
            
wp_send_json_success(
                array(
                    
'step'       => 'done',
                    
'percentage' => 100,
                    
'url'        => add_query_arg$query_argsadmin_url'edit.php?post_type=product&page=product_exporter' ) ),
                )
            );
        } else {
            
wp_send_json_success(
                array(
                    
'step'       => ++$step,
                    
'percentage' => $exporter->get_percent_complete(),
                    
'columns'    => $exporter->get_column_names(),
                )
            );
        }
    }

    
/**
     * 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_typesProductType::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 );
    }
}

new 
WC_Admin_Exporters();