WC_Admin_Meta_Boxes

WC_Admin_Meta_Boxes.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-meta-boxes.php  
  1. class WC_Admin_Meta_Boxes { 
  2.  
  3. /** 
  4. * Is meta boxes saved once? 
  5. * @var boolean 
  6. */ 
  7. private static $saved_meta_boxes = false; 
  8.  
  9. /** 
  10. * Meta box error messages. 
  11. * @var array 
  12. */ 
  13. public static $meta_box_errors = array(); 
  14.  
  15. /** 
  16. * Constructor. 
  17. */ 
  18. public function __construct() { 
  19. add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ), 10 ); 
  20. add_action( 'add_meta_boxes', array( $this, 'rename_meta_boxes' ), 20 ); 
  21. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 ); 
  22. add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 ); 
  23.  
  24. /** 
  25. * Save Order Meta Boxes. 
  26. * In order: 
  27. * Save the order items. 
  28. * Save the order totals. 
  29. * Save the order downloads. 
  30. * Save order data - also updates status and sends out admin emails if needed. Last to show latest data. 
  31. * Save actions - sends out other emails. Last to show latest data. 
  32. */ 
  33. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Items::save', 10, 2 ); 
  34. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Downloads::save', 30, 2 ); 
  35. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2 ); 
  36. add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Actions::save', 50, 2 ); 
  37.  
  38. // Save Product Meta Boxes. 
  39. add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Data::save', 10, 2 ); 
  40. add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Images::save', 20, 2 ); 
  41.  
  42. // Save Coupon Meta Boxes. 
  43. add_action( 'woocommerce_process_shop_coupon_meta', 'WC_Meta_Box_Coupon_Data::save', 10, 2 ); 
  44.  
  45. // Save Rating Meta Boxes. 
  46. add_action( 'comment_edit_redirect', 'WC_Meta_Box_Product_Reviews::save', 1, 2 ); 
  47.  
  48. // Error handling (for showing errors from meta boxes on next page load). 
  49. add_action( 'admin_notices', array( $this, 'output_errors' ) ); 
  50. add_action( 'shutdown', array( $this, 'save_errors' ) ); 
  51.  
  52. /** 
  53. * Add an error message. 
  54. * @param string $text 
  55. */ 
  56. public static function add_error( $text ) { 
  57. self::$meta_box_errors[] = $text; 
  58.  
  59. /** 
  60. * Save errors to an option. 
  61. */ 
  62. public function save_errors() { 
  63. update_option( 'woocommerce_meta_box_errors', self::$meta_box_errors ); 
  64.  
  65. /** 
  66. * Show any stored error messages. 
  67. */ 
  68. public function output_errors() { 
  69. $errors = array_filter( (array) get_option( 'woocommerce_meta_box_errors' ) ); 
  70.  
  71. if ( ! empty( $errors ) ) { 
  72.  
  73. echo '<div id="woocommerce_errors" class="error notice is-dismissible">'; 
  74.  
  75. foreach ( $errors as $error ) { 
  76. echo '<p>' . wp_kses_post( $error ) . '</p>'; 
  77.  
  78. echo '</div>'; 
  79.  
  80. // Clear 
  81. delete_option( 'woocommerce_meta_box_errors' ); 
  82.  
  83. /** 
  84. * Add WC Meta boxes. 
  85. */ 
  86. public function add_meta_boxes() { 
  87. $screen = get_current_screen(); 
  88. $screen_id = $screen ? $screen->id : ''; 
  89.  
  90. // Products. 
  91. add_meta_box( 'postexcerpt', __( 'Product short description', 'woocommerce' ), 'WC_Meta_Box_Product_Short_Description::output', 'product', 'normal' ); 
  92. add_meta_box( 'woocommerce-product-data', __( 'Product data', 'woocommerce' ), 'WC_Meta_Box_Product_Data::output', 'product', 'normal', 'high' ); 
  93. add_meta_box( 'woocommerce-product-images', __( 'Product gallery', 'woocommerce' ), 'WC_Meta_Box_Product_Images::output', 'product', 'side', 'low' ); 
  94.  
  95. // Orders. 
  96. foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) { 
  97. $order_type_object = get_post_type_object( $type ); 
  98. 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' ); 
  99. add_meta_box( 'woocommerce-order-items', __( 'Items', 'woocommerce' ), 'WC_Meta_Box_Order_Items::output', $type, 'normal', 'high' ); 
  100. 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' ); 
  101. 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' ); 
  102. 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' ); 
  103.  
  104. // Coupons. 
  105. add_meta_box( 'woocommerce-coupon-data', __( 'Coupon data', 'woocommerce' ), 'WC_Meta_Box_Coupon_Data::output', 'shop_coupon', 'normal', 'high' ); 
  106.  
  107. // Comment rating. 
  108. if ( 'comment' === $screen_id && isset( $_GET['c'] ) && metadata_exists( 'comment', $_GET['c'], 'rating' ) ) { 
  109. add_meta_box( 'woocommerce-rating', __( 'Rating', 'woocommerce' ), 'WC_Meta_Box_Product_Reviews::output', 'comment', 'normal', 'high' ); 
  110.  
  111. /** 
  112. * Remove bloat. 
  113. */ 
  114. public function remove_meta_boxes() { 
  115. remove_meta_box( 'postexcerpt', 'product', 'normal' ); 
  116. remove_meta_box( 'product_shipping_classdiv', 'product', 'side' ); 
  117. remove_meta_box( 'commentsdiv', 'product', 'normal' ); 
  118. remove_meta_box( 'commentstatusdiv', 'product', 'side' ); 
  119. remove_meta_box( 'commentstatusdiv', 'product', 'normal' ); 
  120. remove_meta_box( 'woothemes-settings', 'shop_coupon', 'normal' ); 
  121. remove_meta_box( 'commentstatusdiv', 'shop_coupon', 'normal' ); 
  122. remove_meta_box( 'slugdiv', 'shop_coupon', 'normal' ); 
  123.  
  124. foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) { 
  125. remove_meta_box( 'commentsdiv', $type, 'normal' ); 
  126. remove_meta_box( 'woothemes-settings', $type, 'normal' ); 
  127. remove_meta_box( 'commentstatusdiv', $type, 'normal' ); 
  128. remove_meta_box( 'slugdiv', $type, 'normal' ); 
  129. remove_meta_box( 'submitdiv', $type, 'side' ); 
  130.  
  131. /** 
  132. * Rename core meta boxes. 
  133. */ 
  134. public function rename_meta_boxes() { 
  135. global $post; 
  136.  
  137. // Comments/Reviews 
  138. if ( isset( $post ) && ( 'publish' == $post->post_status || 'private' == $post->post_status ) ) { 
  139. remove_meta_box( 'commentsdiv', 'product', 'normal' ); 
  140.  
  141. add_meta_box( 'commentsdiv', __( 'Reviews', 'woocommerce' ), 'post_comment_meta_box', 'product', 'normal' ); 
  142.  
  143. /** 
  144. * Check if we're saving, the trigger an action based on the post type. 
  145. * @param int $post_id 
  146. * @param object $post 
  147. */ 
  148. public function save_meta_boxes( $post_id, $post ) { 
  149. // $post_id and $post are required 
  150. if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) { 
  151. return; 
  152.  
  153. // Dont' save meta boxes for revisions or autosaves 
  154. if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { 
  155. return; 
  156.  
  157. // Check the nonce 
  158. if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ) ) { 
  159. return; 
  160.  
  161. // Check the post being saved == the $post_id to prevent triggering this call for other save_post events 
  162. if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) { 
  163. return; 
  164.  
  165. // Check user has permission to edit 
  166. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  167. return; 
  168.  
  169. // We need this save event to run once to avoid potential endless loops. This would have been perfect: 
  170. // remove_action( current_filter(), __METHOD__ ); 
  171. // But cannot be used due to https://github.com/woocommerce/woocommerce/issues/6485 
  172. // When that is patched in core we can use the above. For now: 
  173. self::$saved_meta_boxes = true; 
  174.  
  175. // Check the post type 
  176. if ( in_array( $post->post_type, wc_get_order_types( 'order-meta-boxes' ) ) ) { 
  177. do_action( 'woocommerce_process_shop_order_meta', $post_id, $post ); 
  178. } elseif ( in_array( $post->post_type, array( 'product', 'shop_coupon' ) ) ) { 
  179. do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post );