PluginProbe
WooCommerce / 11.0.1
WooCommerce v11.0.1
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / admin / class-wc-admin-exporters.php
class-wc-admin-exporters.php
245 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Init WooCommerce data exporters.
4 *
5 * @package WooCommerce\Admin
6 * @version x.x.x
7 */
8
9 use Automattic\Jetpack\Constants;
10 use Automattic\WooCommerce\Enums\ProductType;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * WC_Admin_Exporters Class.
18 */
19 class WC_Admin_Exporters {
20
21 /**
22 * Array of exporter IDs.
23 *
24 * @var string[]
25 */
26 protected $exporters = array();
27
28 /**
29 * Constructor.
30 */
31 public function __construct() {
32 if ( ! $this->export_allowed() ) {
33 return;
34 }
35
36 add_action( 'admin_menu', array( $this, 'add_to_menus' ) );
37 add_action( 'admin_head', array( $this, 'hide_from_menus' ) );
38 add_action( 'admin_head', array( $this, 'menu_highlight_for_product_export' ) );
39 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
40 add_action( 'admin_init', array( $this, 'download_export_file' ) );
41 add_action( 'wp_ajax_woocommerce_do_ajax_product_export', array( $this, 'do_ajax_product_export' ) );
42
43 // Register WooCommerce exporters.
44 $this->exporters['product_exporter'] = array(
45 'menu' => 'edit.php?post_type=product',
46 'name' => __( 'Product Export', 'woocommerce' ),
47 'capability' => 'export',
48 'callback' => array( $this, 'product_exporter' ),
49 );
50 }
51
52 /**
53 * Return true if WooCommerce export is allowed for current user, false otherwise.
54 *
55 * @return bool Whether current user can perform export.
56 */
57 protected function export_allowed() {
58 return current_user_can( 'edit_products' ) && current_user_can( 'export' );
59 }
60
61 /**
62 * Add menu items for our custom exporters.
63 */
64 public function add_to_menus() {
65 foreach ( $this->exporters as $id => $exporter ) {
66 add_submenu_page( $exporter['menu'], $exporter['name'], $exporter['name'], $exporter['capability'], $id, $exporter['callback'] );
67 }
68 }
69
70 /**
71 * Hide menu items from view so the pages exist, but the menu items do not.
72 */
73 public function hide_from_menus() {
74 global $submenu;
75
76 foreach ( $this->exporters as $id => $exporter ) {
77 if ( isset( $submenu[ $exporter['menu'] ] ) ) {
78 foreach ( $submenu[ $exporter['menu'] ] as $key => $menu ) {
79 if ( $id === $menu[2] ) {
80 unset( $submenu[ $exporter['menu'] ][ $key ] );
81 }
82 }
83 }
84 }
85 }
86
87 /**
88 * Highlight Products > All Products submenu for Product Exporter.
89 */
90 public function menu_highlight_for_product_export() {
91 global $submenu_file;
92
93 $screen = get_current_screen();
94
95 if ( $screen && 'product_page_product_exporter' === $screen->id ) {
96 $submenu_file = 'edit.php?post_type=product'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
97 }
98 }
99
100 /**
101 * Enqueue scripts.
102 */
103 public function admin_scripts() {
104 $suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min';
105 $version = Constants::get_constant( 'WC_VERSION' );
106 wp_register_script( 'wc-product-export', WC()->plugin_url() . '/assets/js/admin/wc-product-export' . $suffix . '.js', array( 'jquery' ), $version );
107 wp_localize_script(
108 'wc-product-export',
109 'wc_product_export_params',
110 array(
111 'export_nonce' => wp_create_nonce( 'wc-product-export' ),
112 )
113 );
114 }
115
116 /**
117 * Export page UI.
118 */
119 public function product_exporter() {
120 include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php';
121 include_once dirname( __FILE__ ) . '/views/html-admin-page-product-export.php';
122 }
123
124 /**
125 * Serve the generated file.
126 */
127 public function download_export_file() {
128 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.
129 include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php';
130 $exporter = new WC_Product_CSV_Exporter();
131
132 if ( ! empty( $_GET['filename'] ) ) { // WPCS: input var ok.
133 $exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
134 }
135
136 $exporter->export();
137 }
138 }
139
140 /**
141 * AJAX callback for doing the actual export to the CSV file.
142 */
143 public function do_ajax_product_export() {
144 check_ajax_referer( 'wc-product-export', 'security' );
145
146 if ( ! $this->export_allowed() ) {
147 wp_send_json_error( array( 'message' => __( 'Insufficient privileges to export products.', 'woocommerce' ) ) );
148 }
149
150 include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php';
151
152 $step = isset( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; // WPCS: input var ok, sanitization ok.
153 $exporter = new WC_Product_CSV_Exporter();
154
155 if ( ! empty( $_POST['columns'] ) ) { // WPCS: input var ok.
156 $exporter->set_column_names( wp_unslash( $_POST['columns'] ) ); // WPCS: input var ok, sanitization ok.
157 }
158
159 if ( ! empty( $_POST['selected_columns'] ) ) { // WPCS: input var ok.
160 $exporter->set_columns_to_export( wp_unslash( $_POST['selected_columns'] ) ); // WPCS: input var ok, sanitization ok.
161 }
162
163 if ( ! empty( $_POST['export_meta'] ) ) { // WPCS: input var ok.
164 $exporter->enable_meta_export( true );
165 }
166
167 if ( ! empty( $_POST['export_types'] ) ) { // WPCS: input var ok.
168 $exporter->set_product_types_to_export( wp_unslash( $_POST['export_types'] ) ); // WPCS: input var ok, sanitization ok.
169 }
170
171 if ( ! empty( $_POST['export_category'] ) && is_array( $_POST['export_category'] ) ) {// WPCS: input var ok.
172 $exporter->set_product_category_to_export( wp_unslash( array_values( $_POST['export_category'] ) ) ); // WPCS: input var ok, sanitization ok.
173 }
174
175 // Set specific product IDs if provided.
176 if ( ! empty( $_POST['export_product_ids'] ) ) { // WPCS: input var ok.
177 $ids_raw = explode( ',', sanitize_text_field( wp_unslash( $_POST['export_product_ids'] ) ) ); // WPCS: input var ok, sanitization ok.
178
179 if ( ! empty( $ids_raw ) ) {
180 $exporter->set_product_ids_to_export( $ids_raw );
181 }
182 }
183
184 if ( ! empty( $_POST['filename'] ) ) { // WPCS: input var ok.
185 $exporter->set_filename( wp_unslash( $_POST['filename'] ) ); // WPCS: input var ok, sanitization ok.
186 }
187
188 $exporter->set_page( $step );
189 $exporter->generate_file();
190
191 $query_args = apply_filters(
192 'woocommerce_export_get_ajax_query_args',
193 array(
194 'nonce' => wp_create_nonce( 'product-csv' ),
195 'action' => 'download_product_csv',
196 'filename' => $exporter->get_filename(),
197 )
198 );
199
200 if ( 100 === $exporter->get_percent_complete() ) {
201 wp_send_json_success(
202 array(
203 'step' => 'done',
204 'percentage' => 100,
205 'url' => add_query_arg( $query_args, admin_url( 'edit.php?post_type=product&page=product_exporter' ) ),
206 )
207 );
208 } else {
209 wp_send_json_success(
210 array(
211 'step' => ++$step,
212 'percentage' => $exporter->get_percent_complete(),
213 'columns' => $exporter->get_column_names(),
214 )
215 );
216 }
217 }
218
219 /**
220 * Gets the product types that can be exported.
221 *
222 * @since 5.1.0
223 * @return array The product types keys and labels.
224 */
225 public static function get_product_types() {
226 $product_types = wc_get_product_types();
227 $product_types[ ProductType::VARIATION ] = __( 'Product variations', 'woocommerce' );
228
229 /**
230 * Allow third-parties to filter the exportable product types.
231 *
232 * @since 5.1.0
233 * @param array $product_types {
234 * The product type key and label.
235 *
236 * @type string Product type key - eg 'variable', 'simple' etc.
237 * @type string A translated product label which appears in the export product type dropdown.
238 * }
239 */
240 return apply_filters( 'woocommerce_exporter_product_types', $product_types );
241 }
242 }
243
244 new WC_Admin_Exporters();
245