2013-08-06 10:41:20 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Meta Boxes
2013-08-06 10:41:20 +00:00
*
2015-11-03 12:28:01 +00:00
* Sets up the write panels used by products and orders ( custom post types ) .
2013-08-06 10:41:20 +00:00
*
2014-11-30 06:52:32 +00:00
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin / Meta Boxes
2013-08-06 10:41:20 +00:00
* @ version 2.1 . 0
*/
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-08-06 10:41:20 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Admin_Meta_Boxes .
2013-08-06 10:41:20 +00:00
*/
class WC_Admin_Meta_Boxes {
2016-01-05 15:32:16 +00:00
/**
* Is meta boxes saved once ?
*
* @ var boolean
*/
2015-02-04 11:34:59 +00:00
private static $saved_meta_boxes = false ;
2016-01-05 15:32:16 +00:00
/**
* Meta box error messages .
*
* @ var array
*/
2015-07-27 17:16:52 +00:00
public static $meta_box_errors = array ();
2013-08-06 10:41:20 +00:00
/**
2015-11-03 12:28:01 +00:00
* Constructor .
2013-08-06 10:41:20 +00:00
*/
public function __construct () {
add_action ( 'add_meta_boxes' , array ( $this , 'remove_meta_boxes' ), 10 );
add_action ( 'add_meta_boxes' , array ( $this , 'rename_meta_boxes' ), 20 );
add_action ( 'add_meta_boxes' , array ( $this , 'add_meta_boxes' ), 30 );
add_action ( 'save_post' , array ( $this , 'save_meta_boxes' ), 1 , 2 );
2014-03-03 13:27:20 +00:00
/**
2015-11-03 12:28:01 +00:00
* Save Order Meta Boxes .
2014-03-03 13:27:20 +00:00
*
2015-11-03 13:31:20 +00:00
* In order :
2015-11-03 12:28:01 +00:00
* Save the order items .
* Save the order totals .
* Save the order downloads .
2014-11-30 06:52:32 +00:00
* Save order data - also updates status and sends out admin emails if needed . Last to show latest data .
* Save actions - sends out other emails . Last to show latest data .
2014-03-03 13:27:20 +00:00
*/
add_action ( 'woocommerce_process_shop_order_meta' , 'WC_Meta_Box_Order_Items::save' , 10 , 2 );
add_action ( 'woocommerce_process_shop_order_meta' , 'WC_Meta_Box_Order_Downloads::save' , 30 , 2 );
add_action ( 'woocommerce_process_shop_order_meta' , 'WC_Meta_Box_Order_Data::save' , 40 , 2 );
2013-08-06 10:41:20 +00:00
add_action ( 'woocommerce_process_shop_order_meta' , 'WC_Meta_Box_Order_Actions::save' , 50 , 2 );
2016-12-12 13:00:08 +00:00
// Save Product Meta Boxes.
2013-08-06 10:41:20 +00:00
add_action ( 'woocommerce_process_product_meta' , 'WC_Meta_Box_Product_Data::save' , 10 , 2 );
add_action ( 'woocommerce_process_product_meta' , 'WC_Meta_Box_Product_Images::save' , 20 , 2 );
2016-12-12 13:00:08 +00:00
// Save Coupon Meta Boxes.
2013-08-06 10:41:20 +00:00
add_action ( 'woocommerce_process_shop_coupon_meta' , 'WC_Meta_Box_Coupon_Data::save' , 10 , 2 );
2016-12-12 13:00:08 +00:00
// Save Rating Meta Boxes.
add_action ( 'comment_edit_redirect' , 'WC_Meta_Box_Product_Reviews::save' , 1 , 2 );
// Error handling (for showing errors from meta boxes on next page load).
2013-08-06 10:41:20 +00:00
add_action ( 'admin_notices' , array ( $this , 'output_errors' ) );
add_action ( 'shutdown' , array ( $this , 'save_errors' ) );
}
/**
2015-11-03 12:28:01 +00:00
* Add an error message .
2013-08-06 10:41:20 +00:00
* @ param string $text
*/
public static function add_error ( $text ) {
self :: $meta_box_errors [] = $text ;
}
/**
2015-11-03 12:28:01 +00:00
* Save errors to an option .
2013-08-06 10:41:20 +00:00
*/
public function save_errors () {
update_option ( 'woocommerce_meta_box_errors' , self :: $meta_box_errors );
}
/**
* Show any stored error messages .
*/
public function output_errors () {
2017-03-31 11:10:20 +00:00
$errors = array_filter ( ( array ) get_option ( 'woocommerce_meta_box_errors' ) );
2013-08-06 10:41:20 +00:00
2013-12-11 01:43:12 +00:00
if ( ! empty ( $errors ) ) {
2013-08-06 10:41:20 +00:00
2015-12-03 10:14:00 +00:00
echo '<div id="woocommerce_errors" class="error notice is-dismissible">' ;
2014-11-30 06:52:32 +00:00
2013-12-11 01:43:12 +00:00
foreach ( $errors as $error ) {
2015-05-26 15:49:41 +00:00
echo '<p>' . wp_kses_post ( $error ) . '</p>' ;
2013-12-11 01:43:12 +00:00
}
2014-11-30 06:52:32 +00:00
2013-12-11 01:43:12 +00:00
echo '</div>' ;
2013-08-06 10:41:20 +00:00
2013-12-11 01:43:12 +00:00
// Clear
delete_option ( 'woocommerce_meta_box_errors' );
}
2013-08-06 10:41:20 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Add WC Meta boxes .
2013-08-06 10:41:20 +00:00
*/
public function add_meta_boxes () {
2016-12-12 13:00:08 +00:00
$screen = get_current_screen ();
$screen_id = $screen ? $screen -> id : '' ;
// Products.
2016-10-12 10:16:30 +00:00
add_meta_box ( 'postexcerpt' , __ ( 'Product short description' , 'woocommerce' ), 'WC_Meta_Box_Product_Short_Description::output' , 'product' , 'normal' );
add_meta_box ( 'woocommerce-product-data' , __ ( 'Product data' , 'woocommerce' ), 'WC_Meta_Box_Product_Data::output' , 'product' , 'normal' , 'high' );
add_meta_box ( 'woocommerce-product-images' , __ ( 'Product gallery' , 'woocommerce' ), 'WC_Meta_Box_Product_Images::output' , 'product' , 'side' , 'low' );
2013-08-06 10:41:20 +00:00
2016-12-12 13:00:08 +00:00
// Orders.
2014-07-11 11:43:42 +00:00
foreach ( wc_get_order_types ( 'order-meta-boxes' ) as $type ) {
2014-10-07 05:22:16 +00:00
$order_type_object = get_post_type_object ( $type );
2016-10-12 10:16:30 +00:00
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' );
2016-03-22 14:54:02 +00:00
add_meta_box ( 'woocommerce-order-items' , __ ( 'Items' , 'woocommerce' ), 'WC_Meta_Box_Order_Items::output' , $type , 'normal' , 'high' );
2016-10-12 10:16:30 +00:00
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' );
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' );
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' );
2014-07-11 11:43:42 +00:00
}
2013-08-06 10:41:20 +00:00
2016-12-12 13:00:08 +00:00
// Coupons.
2016-10-12 10:16:30 +00:00
add_meta_box ( 'woocommerce-coupon-data' , __ ( 'Coupon data' , 'woocommerce' ), 'WC_Meta_Box_Coupon_Data::output' , 'shop_coupon' , 'normal' , 'high' );
2016-12-12 13:00:08 +00:00
// Comment rating.
if ( 'comment' === $screen_id && isset ( $_GET [ 'c' ] ) && metadata_exists ( 'comment' , $_GET [ 'c' ], 'rating' ) ) {
add_meta_box ( 'woocommerce-rating' , __ ( 'Rating' , 'woocommerce' ), 'WC_Meta_Box_Product_Reviews::output' , 'comment' , 'normal' , 'high' );
}
2013-08-06 10:41:20 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Remove bloat .
2013-08-06 10:41:20 +00:00
*/
public function remove_meta_boxes () {
remove_meta_box ( 'postexcerpt' , 'product' , 'normal' );
remove_meta_box ( 'product_shipping_classdiv' , 'product' , 'side' );
2016-02-08 14:42:01 +00:00
remove_meta_box ( 'commentsdiv' , 'product' , 'normal' );
2013-08-06 10:41:20 +00:00
remove_meta_box ( 'commentstatusdiv' , 'product' , 'side' );
2016-02-08 14:42:01 +00:00
remove_meta_box ( 'commentstatusdiv' , 'product' , 'normal' );
2015-10-05 23:31:05 +00:00
remove_meta_box ( 'woothemes-settings' , 'shop_coupon' , 'normal' );
remove_meta_box ( 'commentstatusdiv' , 'shop_coupon' , 'normal' );
remove_meta_box ( 'slugdiv' , 'shop_coupon' , 'normal' );
2014-07-11 11:43:42 +00:00
foreach ( wc_get_order_types ( 'order-meta-boxes' ) as $type ) {
remove_meta_box ( 'commentsdiv' , $type , 'normal' );
remove_meta_box ( 'woothemes-settings' , $type , 'normal' );
remove_meta_box ( 'commentstatusdiv' , $type , 'normal' );
remove_meta_box ( 'slugdiv' , $type , 'normal' );
2016-02-08 14:42:01 +00:00
remove_meta_box ( 'submitdiv' , $type , 'side' );
2014-07-11 11:43:42 +00:00
}
2013-08-06 10:41:20 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Rename core meta boxes .
2013-08-06 10:41:20 +00:00
*/
public function rename_meta_boxes () {
global $post ;
// Comments/Reviews
2017-06-08 14:26:30 +00:00
if ( isset ( $post ) && ( 'publish' == $post -> post_status || 'private' == $post -> post_status ) && post_type_supports ( 'product' , 'comments' ) ) {
2013-08-06 10:41:20 +00:00
remove_meta_box ( 'commentsdiv' , 'product' , 'normal' );
add_meta_box ( 'commentsdiv' , __ ( 'Reviews' , 'woocommerce' ), 'post_comment_meta_box' , 'product' , 'normal' );
}
}
/**
2015-11-03 12:28:01 +00:00
* Check if we ' re saving , the trigger an action based on the post type .
2013-08-06 10:41:20 +00:00
*
* @ param int $post_id
* @ param object $post
*/
public function save_meta_boxes ( $post_id , $post ) {
2014-04-07 13:15:51 +00:00
// $post_id and $post are required
2015-02-04 11:34:59 +00:00
if ( empty ( $post_id ) || empty ( $post ) || self :: $saved_meta_boxes ) {
2014-04-07 13:15:51 +00:00
return ;
}
// Dont' save meta boxes for revisions or autosaves
if ( defined ( 'DOING_AUTOSAVE' ) || is_int ( wp_is_post_revision ( $post ) ) || is_int ( wp_is_post_autosave ( $post ) ) ) {
return ;
}
2014-07-20 23:53:55 +00:00
2014-04-07 13:15:51 +00:00
// Check the nonce
if ( empty ( $_POST [ 'woocommerce_meta_nonce' ] ) || ! wp_verify_nonce ( $_POST [ 'woocommerce_meta_nonce' ], 'woocommerce_save_data' ) ) {
return ;
2014-07-20 23:53:55 +00:00
}
2014-04-07 13:15:51 +00:00
// Check the post being saved == the $post_id to prevent triggering this call for other save_post events
if ( empty ( $_POST [ 'post_ID' ] ) || $_POST [ 'post_ID' ] != $post_id ) {
return ;
}
// Check user has permission to edit
if ( ! current_user_can ( 'edit_post' , $post_id ) ) {
return ;
}
2015-02-04 11:34:59 +00:00
// We need this save event to run once to avoid potential endless loops. This would have been perfect:
2016-09-02 02:48:11 +00:00
// remove_action( current_filter(), __METHOD__ );
2016-09-28 10:17:40 +00:00
// But cannot be used due to https://github.com/woocommerce/woocommerce/issues/6485
2016-01-05 15:32:16 +00:00
// When that is patched in core we can use the above. For now:
2015-02-04 11:34:59 +00:00
self :: $saved_meta_boxes = true ;
2014-04-07 13:15:51 +00:00
// Check the post type
2014-07-11 11:43:42 +00:00
if ( in_array ( $post -> post_type , wc_get_order_types ( 'order-meta-boxes' ) ) ) {
do_action ( 'woocommerce_process_shop_order_meta' , $post_id , $post );
2015-01-08 18:34:30 +00:00
} elseif ( in_array ( $post -> post_type , array ( 'product' , 'shop_coupon' ) ) ) {
2014-07-11 11:43:42 +00:00
do_action ( 'woocommerce_process_' . $post -> post_type . '_meta' , $post_id , $post );
2014-04-07 13:15:51 +00:00
}
2013-08-06 10:41:20 +00:00
}
}
2013-11-08 19:19:42 +00:00
new WC_Admin_Meta_Boxes ();