Açıklama Yok

class-wc-admin-exporters.php 6.5KB

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