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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * WooCommerce Meta Boxes
  4. *
  5. * Sets up the write panels used by products and orders (custom post types).
  6. *
  7. * @author WooThemes
  8. * @category Admin
  9. * @package WooCommerce/Admin/Meta Boxes
  10. * @version 2.1.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly
  14. }
  15. /**
  16. * WC_Admin_Meta_Boxes.
  17. */
  18. class WC_Admin_Meta_Boxes {
  19. /**
  20. * Is meta boxes saved once?
  21. *
  22. * @var boolean
  23. */
  24. private static $saved_meta_boxes = false;
  25. /**
  26. * Meta box error messages.
  27. *
  28. * @var array
  29. */
  30. public static $meta_box_errors = array();
  31. /**
  32. * Constructor.
  33. */
  34. public function __construct() {
  35. add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ), 10 );
  36. add_action( 'add_meta_boxes', array( $this, 'rename_meta_boxes' ), 20 );
  37. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 );
  38. add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
  39. /**
  40. * Save Order Meta Boxes.
  41. *
  42. * In order:
  43. * Save the order items.
  44. * Save the order totals.
  45. * Save the order downloads.
  46. * Save order data - also updates status and sends out admin emails if needed. Last to show latest data.
  47. * Save actions - sends out other emails. Last to show latest data.
  48. */
  49. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Items::save', 10, 2 );
  50. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Downloads::save', 30, 2 );
  51. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2 );
  52. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Actions::save', 50, 2 );
  53. // Save Product Meta Boxes.
  54. add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Data::save', 10, 2 );
  55. add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Images::save', 20, 2 );
  56. // Save Coupon Meta Boxes.
  57. add_action( 'woocommerce_process_shop_coupon_meta', 'WC_Meta_Box_Coupon_Data::save', 10, 2 );
  58. // Save Rating Meta Boxes.
  59. add_filter( 'wp_update_comment_data', 'WC_Meta_Box_Product_Reviews::save', 1 );
  60. // Error handling (for showing errors from meta boxes on next page load).
  61. add_action( 'admin_notices', array( $this, 'output_errors' ) );
  62. add_action( 'shutdown', array( $this, 'save_errors' ) );
  63. }
  64. /**
  65. * Add an error message.
  66. *
  67. * @param string $text
  68. */
  69. public static function add_error( $text ) {
  70. self::$meta_box_errors[] = $text;
  71. }
  72. /**
  73. * Save errors to an option.
  74. */
  75. public function save_errors() {
  76. update_option( 'woocommerce_meta_box_errors', self::$meta_box_errors );
  77. }
  78. /**
  79. * Show any stored error messages.
  80. */
  81. public function output_errors() {
  82. $errors = array_filter( (array) get_option( 'woocommerce_meta_box_errors' ) );
  83. if ( ! empty( $errors ) ) {
  84. echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
  85. foreach ( $errors as $error ) {
  86. echo '<p>' . wp_kses_post( $error ) . '</p>';
  87. }
  88. echo '</div>';
  89. // Clear
  90. delete_option( 'woocommerce_meta_box_errors' );
  91. }
  92. }
  93. /**
  94. * Add WC Meta boxes.
  95. */
  96. public function add_meta_boxes() {
  97. $screen = get_current_screen();
  98. $screen_id = $screen ? $screen->id : '';
  99. // Products.
  100. add_meta_box( 'postexcerpt', __( 'Product short description', 'woocommerce' ), 'WC_Meta_Box_Product_Short_Description::output', 'product', 'normal' );
  101. add_meta_box( 'woocommerce-product-data', __( 'Product data', 'woocommerce' ), 'WC_Meta_Box_Product_Data::output', 'product', 'normal', 'high' );
  102. add_meta_box( 'woocommerce-product-images', __( 'Product gallery', 'woocommerce' ), 'WC_Meta_Box_Product_Images::output', 'product', 'side', 'low' );
  103. // Orders.
  104. foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) {
  105. $order_type_object = get_post_type_object( $type );
  106. 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' );
  107. add_meta_box( 'woocommerce-order-items', __( 'Items', 'woocommerce' ), 'WC_Meta_Box_Order_Items::output', $type, 'normal', 'high' );
  108. 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' );
  109. 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' );
  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', $_GET['c'], 'rating' ) ) {
  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
  154. * @param object $post
  155. */
  156. public function save_meta_boxes( $post_id, $post ) {
  157. // $post_id and $post are required
  158. if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) {
  159. return;
  160. }
  161. // Dont' save meta boxes for revisions or autosaves
  162. if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
  163. return;
  164. }
  165. // Check the nonce
  166. if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ) ) {
  167. return;
  168. }
  169. // Check the post being saved == the $post_id to prevent triggering this call for other save_post events
  170. if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
  171. return;
  172. }
  173. // Check user has permission to edit
  174. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  175. return;
  176. }
  177. // We need this save event to run once to avoid potential endless loops. This would have been perfect:
  178. // remove_action( current_filter(), __METHOD__ );
  179. // But cannot be used due to https://github.com/woocommerce/woocommerce/issues/6485
  180. // When that is patched in core we can use the above. For now:
  181. self::$saved_meta_boxes = true;
  182. // Check the post type
  183. if ( in_array( $post->post_type, wc_get_order_types( 'order-meta-boxes' ) ) ) {
  184. do_action( 'woocommerce_process_shop_order_meta', $post_id, $post );
  185. } elseif ( in_array( $post->post_type, array( 'product', 'shop_coupon' ) ) ) {
  186. do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post );
  187. }
  188. }
  189. }
  190. new WC_Admin_Meta_Boxes();