class-wc-admin-exporters.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Init WooCommerce data exporters.
  4. *
  5. * @package WooCommerce/Admin
  6. * @version 3.1.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * WC_Admin_Exporters Class.
  13. */
  14. class WC_Admin_Exporters {
  15. /**
  16. * Array of exporter IDs.
  17. *
  18. * @var string[]
  19. */
  20. protected $exporters = array();
  21. /**
  22. * Constructor.
  23. */
  24. public function __construct() {
  25. if ( ! $this->export_allowed() ) {
  26. return;
  27. }
  28. add_action( 'admin_menu', array( $this, 'add_to_menus' ) );
  29. add_action( 'admin_head', array( $this, 'hide_from_menus' ) );
  30. add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
  31. add_action( 'admin_init', array( $this, 'download_export_file' ) );
  32. add_action( 'wp_ajax_woocommerce_do_ajax_product_export', array( $this, 'do_ajax_product_export' ) );
  33. // Register WooCommerce exporters.
  34. $this->exporters['product_exporter'] = array(
  35. 'menu' => 'edit.php?post_type=product',
  36. 'name' => __( 'Product Export', 'woocommerce' ),
  37. 'capability' => 'export',
  38. 'callback' => array( $this, 'product_exporter' ),
  39. );
  40. }
  41. /**
  42. * Return true if WooCommerce export is allowed for current user, false otherwise.
  43. *
  44. * @return bool Whether current user can perform export.
  45. */
  46. protected function export_allowed() {
  47. return current_user_can( 'edit_products' ) && current_user_can( 'export' );
  48. }
  49. /**
  50. * Add menu items for our custom exporters.
  51. */
  52. public function add_to_menus() {
  53. foreach ( $this->exporters as $id => $exporter ) {
  54. add_submenu_page( $exporter['menu'], $exporter['name'], $exporter['name'], $exporter['capability'], $id, $exporter['callback'] );
  55. }
  56. }
  57. /**
  58. * Hide menu items from view so the pages exist, but the menu items do not.
  59. */
  60. public function hide_from_menus() {
  61. global $submenu;
  62. foreach ( $this->exporters as $id => $exporter ) {
  63. if ( isset( $submenu[ $exporter['menu'] ] ) ) {
  64. foreach ( $submenu[ $exporter['menu'] ] as $key => $menu ) {
  65. if ( $id === $menu[2] ) {
  66. unset( $submenu[ $exporter['menu'] ][ $key ] );
  67. }
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * Enqueue scripts.
  74. */
  75. public function admin_scripts() {
  76. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  77. wp_register_script( 'wc-product-export', WC()->plugin_url() . '/assets/js/admin/wc-product-export' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
  78. wp_localize_script(
  79. 'wc-product-export',
  80. 'wc_product_export_params',
  81. array(
  82. 'export_nonce' => wp_create_nonce( 'wc-product-export' ),
  83. )
  84. );
  85. }
  86. /**
  87. * Export page UI.
  88. */
  89. public function product_exporter() {
  90. include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php';
  91. include_once dirname( __FILE__ ) . '/views/html-admin-page-product-export.php';
  92. }
  93. /**
  94. * Serve the generated file.
  95. */
  96. public function download_export_file() {
  97. 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.
  98. include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php';
  99. $exporter = new WC_Product_CSV_Exporter();
  100. if ( ! empty( $_GET['filename'] ) ) { // WPCS: input var ok.
  101. $exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
  102. }
  103. $exporter->export();
  104. }
  105. }
  106. /**
  107. * AJAX callback for doing the actual export to the CSV file.
  108. */
  109. public function do_ajax_product_export() {
  110. check_ajax_referer( 'wc-product-export', 'security' );
  111. if ( ! $this->export_allowed() ) {
  112. wp_send_json_error( array( 'message' => __( 'Insufficient privileges to export products.', 'woocommerce' ) ) );
  113. }
  114. include_once WC_ABSPATH . 'includes/export/class-wc-product-csv-exporter.php';
  115. $step = isset( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; // WPCS: input var ok, sanitization ok.
  116. $exporter = new WC_Product_CSV_Exporter();
  117. if ( ! empty( $_POST['columns'] ) ) { // WPCS: input var ok.
  118. $exporter->set_column_names( wp_unslash( $_POST['columns'] ) ); // WPCS: input var ok, sanitization ok.
  119. }
  120. if ( ! empty( $_POST['selected_columns'] ) ) { // WPCS: input var ok.
  121. $exporter->set_columns_to_export( wp_unslash( $_POST['selected_columns'] ) ); // WPCS: input var ok, sanitization ok.
  122. }
  123. if ( ! empty( $_POST['export_meta'] ) ) { // WPCS: input var ok.
  124. $exporter->enable_meta_export( true );
  125. }
  126. if ( ! empty( $_POST['export_types'] ) ) { // WPCS: input var ok.
  127. $exporter->set_product_types_to_export( wp_unslash( $_POST['export_types'] ) ); // WPCS: input var ok, sanitization ok.
  128. }
  129. if ( ! empty( $_POST['filename'] ) ) { // WPCS: input var ok.
  130. $exporter->set_filename( wp_unslash( $_POST['filename'] ) ); // WPCS: input var ok, sanitization ok.
  131. }
  132. $exporter->set_page( $step );
  133. $exporter->generate_file();
  134. $query_args = apply_filters(
  135. 'woocommerce_export_get_ajax_query_args',
  136. array(
  137. 'nonce' => wp_create_nonce( 'product-csv' ),
  138. 'action' => 'download_product_csv',
  139. 'filename' => $exporter->get_filename(),
  140. )
  141. );
  142. if ( 100 === $exporter->get_percent_complete() ) {
  143. wp_send_json_success(
  144. array(
  145. 'step' => 'done',
  146. 'percentage' => 100,
  147. 'url' => add_query_arg( $query_args, admin_url( 'edit.php?post_type=product&page=product_exporter' ) ),
  148. )
  149. );
  150. } else {
  151. wp_send_json_success(
  152. array(
  153. 'step' => ++$step,
  154. 'percentage' => $exporter->get_percent_complete(),
  155. 'columns' => $exporter->get_column_names(),
  156. )
  157. );
  158. }
  159. }
  160. }
  161. new WC_Admin_Exporters();