暂无描述

class-wc-admin-meta-boxes.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * WooCommerce Meta Boxes
  4. *
  5. * Sets up the write panels used by products and orders (custom post types).
  6. *
  7. * @package WooCommerce\Admin\Meta Boxes
  8. */
  9. use Automattic\Jetpack\Constants;
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * WC_Admin_Meta_Boxes.
  13. */
  14. class WC_Admin_Meta_Boxes {
  15. /**
  16. * Is meta boxes saved once?
  17. *
  18. * @var boolean
  19. */
  20. private static $saved_meta_boxes = false;
  21. /**
  22. * Meta box error messages.
  23. *
  24. * @var array
  25. */
  26. public static $meta_box_errors = array();
  27. /**
  28. * Constructor.
  29. */
  30. public function __construct() {
  31. add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ), 10 );
  32. add_action( 'add_meta_boxes', array( $this, 'rename_meta_boxes' ), 20 );
  33. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 );
  34. add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
  35. /**
  36. * Save Order Meta Boxes.
  37. *
  38. * In order:
  39. * Save the order items.
  40. * Save the order totals.
  41. * Save the order downloads.
  42. * Save order data - also updates status and sends out admin emails if needed. Last to show latest data.
  43. * Save actions - sends out other emails. Last to show latest data.
  44. */
  45. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Items::save', 10 );
  46. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Downloads::save', 30, 2 );
  47. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40 );
  48. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Actions::save', 50, 2 );
  49. // Save Product Meta Boxes.
  50. add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Data::save', 10, 2 );
  51. add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Images::save', 20, 2 );
  52. // Save Coupon Meta Boxes.
  53. add_action( 'woocommerce_process_shop_coupon_meta', 'WC_Meta_Box_Coupon_Data::save', 10, 2 );
  54. // Save Rating Meta Boxes.
  55. add_filter( 'wp_update_comment_data', 'WC_Meta_Box_Product_Reviews::save', 1 );
  56. // Error handling (for showing errors from meta boxes on next page load).
  57. add_action( 'admin_notices', array( $this, 'output_errors' ) );
  58. add_action( 'shutdown', array( $this, 'save_errors' ) );
  59. add_filter( 'theme_product_templates', array( $this, 'remove_block_templates' ), 10, 1 );
  60. }
  61. /**
  62. * Add an error message.
  63. *
  64. * @param string $text Error to add.
  65. */
  66. public static function add_error( $text ) {
  67. self::$meta_box_errors[] = $text;
  68. }
  69. /**
  70. * Save errors to an option.
  71. */
  72. public function save_errors() {
  73. update_option( 'woocommerce_meta_box_errors', self::$meta_box_errors );
  74. }
  75. /**
  76. * Show any stored error messages.
  77. */
  78. public function output_errors() {
  79. $errors = array_filter( (array) get_option( 'woocommerce_meta_box_errors' ) );
  80. if ( ! empty( $errors ) ) {
  81. echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
  82. foreach ( $errors as $error ) {
  83. echo '<p>' . wp_kses_post( $error ) . '</p>';
  84. }
  85. echo '</div>';
  86. // Clear.
  87. delete_option( 'woocommerce_meta_box_errors' );
  88. }
  89. }
  90. /**
  91. * Add WC Meta boxes.
  92. */
  93. public function add_meta_boxes() {
  94. $screen = get_current_screen();
  95. $screen_id = $screen ? $screen->id : '';
  96. // Products.
  97. add_meta_box( 'postexcerpt', __( 'Product short description', 'woocommerce' ), 'WC_Meta_Box_Product_Short_Description::output', 'product', 'normal' );
  98. add_meta_box( 'woocommerce-product-data', __( 'Product data', 'woocommerce' ), 'WC_Meta_Box_Product_Data::output', 'product', 'normal', 'high' );
  99. add_meta_box( 'woocommerce-product-images', __( 'Product gallery', 'woocommerce' ), 'WC_Meta_Box_Product_Images::output', 'product', 'side', 'low' );
  100. // Orders.
  101. foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) {
  102. $order_type_object = get_post_type_object( $type );
  103. /* Translators: %s order type name. */
  104. add_meta_box( 'woocommerce-order-data', sprintf( __( '%s data', 'woocommerce' ), $order_type_object->labels->singular_name ), 'WC_Meta_Box_Order_Data::output', $type, 'normal', 'high' );
  105. add_meta_box( 'woocommerce-order-items', __( 'Items', 'woocommerce' ), 'WC_Meta_Box_Order_Items::output', $type, 'normal', 'high' );
  106. /* Translators: %s order type name. */
  107. add_meta_box( 'woocommerce-order-notes', sprintf( __( '%s notes', 'woocommerce' ), $order_type_object->labels->singular_name ), 'WC_Meta_Box_Order_Notes::output', $type, 'side', 'default' );
  108. add_meta_box( 'woocommerce-order-downloads', __( 'Downloadable product permissions', 'woocommerce' ) . wc_help_tip( __( 'Note: Permissions for order items will automatically be granted when the order status changes to processing/completed.', 'woocommerce' ) ), 'WC_Meta_Box_Order_Downloads::output', $type, 'normal', 'default' );
  109. /* Translators: %s order type name. */
  110. add_meta_box( 'woocommerce-order-actions', sprintf( __( '%s actions', 'woocommerce' ), $order_type_object->labels->singular_name ), 'WC_Meta_Box_Order_Actions::output', $type, 'side', 'high' );
  111. }
  112. // Coupons.
  113. add_meta_box( 'woocommerce-coupon-data', __( 'Coupon data', 'woocommerce' ), 'WC_Meta_Box_Coupon_Data::output', 'shop_coupon', 'normal', 'high' );
  114. // Comment rating.
  115. if ( 'comment' === $screen_id && isset( $_GET['c'] ) && metadata_exists( 'comment', wc_clean( wp_unslash( $_GET['c'] ) ), 'rating' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  116. add_meta_box( 'woocommerce-rating', __( 'Rating', 'woocommerce' ), 'WC_Meta_Box_Product_Reviews::output', 'comment', 'normal', 'high' );
  117. }
  118. }
  119. /**
  120. * Remove bloat.
  121. */
  122. public function remove_meta_boxes() {
  123. remove_meta_box( 'postexcerpt', 'product', 'normal' );
  124. remove_meta_box( 'product_shipping_classdiv', 'product', 'side' );
  125. remove_meta_box( 'commentsdiv', 'product', 'normal' );
  126. remove_meta_box( 'commentstatusdiv', 'product', 'side' );
  127. remove_meta_box( 'commentstatusdiv', 'product', 'normal' );
  128. remove_meta_box( 'woothemes-settings', 'shop_coupon', 'normal' );
  129. remove_meta_box( 'commentstatusdiv', 'shop_coupon', 'normal' );
  130. remove_meta_box( 'slugdiv', 'shop_coupon', 'normal' );
  131. foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) {
  132. remove_meta_box( 'commentsdiv', $type, 'normal' );
  133. remove_meta_box( 'woothemes-settings', $type, 'normal' );
  134. remove_meta_box( 'commentstatusdiv', $type, 'normal' );
  135. remove_meta_box( 'slugdiv', $type, 'normal' );
  136. remove_meta_box( 'submitdiv', $type, 'side' );
  137. }
  138. }
  139. /**
  140. * Rename core meta boxes.
  141. */
  142. public function rename_meta_boxes() {
  143. global $post;
  144. // Comments/Reviews.
  145. if ( isset( $post ) && ( 'publish' === $post->post_status || 'private' === $post->post_status ) && post_type_supports( 'product', 'comments' ) ) {
  146. remove_meta_box( 'commentsdiv', 'product', 'normal' );
  147. add_meta_box( 'commentsdiv', __( 'Reviews', 'woocommerce' ), 'post_comment_meta_box', 'product', 'normal' );
  148. }
  149. }
  150. /**
  151. * Check if we're saving, the trigger an action based on the post type.
  152. *
  153. * @param int $post_id Post ID.
  154. * @param object $post Post object.
  155. */
  156. public function save_meta_boxes( $post_id, $post ) {
  157. $post_id = absint( $post_id );
  158. // $post_id and $post are required
  159. if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) {
  160. return;
  161. }
  162. // Dont' save meta boxes for revisions or autosaves.
  163. if ( Constants::is_true( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
  164. return;
  165. }
  166. // Check the nonce.
  167. if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  168. return;
  169. }
  170. // Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
  171. if ( empty( $_POST['post_ID'] ) || absint( $_POST['post_ID'] ) !== $post_id ) {
  172. return;
  173. }
  174. // Check user has permission to edit.
  175. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  176. return;
  177. }
  178. // We need this save event to run once to avoid potential endless loops. This would have been perfect:
  179. // remove_action( current_filter(), __METHOD__ );
  180. // But cannot be used due to https://github.com/woocommerce/woocommerce/issues/6485
  181. // When that is patched in core we can use the above.
  182. self::$saved_meta_boxes = true;
  183. // Check the post type.
  184. if ( in_array( $post->post_type, wc_get_order_types( 'order-meta-boxes' ), true ) ) {
  185. do_action( 'woocommerce_process_shop_order_meta', $post_id, $post );
  186. } elseif ( in_array( $post->post_type, array( 'product', 'shop_coupon' ), true ) ) {
  187. do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post );
  188. }
  189. }
  190. /**
  191. * Remove block-based templates from the list of available templates for products.
  192. *
  193. * @param string[] $templates Array of template header names keyed by the template file name.
  194. *
  195. * @return string[] Templates array excluding block-based templates.
  196. */
  197. public function remove_block_templates( $templates ) {
  198. if ( count( $templates ) === 0 || ! function_exists( 'gutenberg_get_block_template' ) ) {
  199. return $templates;
  200. }
  201. $theme = wp_get_theme()->get_stylesheet();
  202. $filtered_templates = array();
  203. foreach ( $templates as $template_key => $template_name ) {
  204. $gutenberg_template = gutenberg_get_block_template( $theme . '//' . $template_key );
  205. if ( ! $gutenberg_template ) {
  206. $filtered_templates[ $template_key ] = $template_name;
  207. }
  208. }
  209. return $filtered_templates;
  210. }
  211. }
  212. new WC_Admin_Meta_Boxes();