helper
2 years ago
importers
3 years ago
list-tables
2 years ago
marketplace-suggestions
2 years ago
meta-boxes
2 years ago
notes
2 years ago
plugin-updates
2 years ago
reports
2 years ago
settings
2 years ago
views
2 years ago
class-wc-admin-addons.php
2 years ago
class-wc-admin-api-keys-table-list.php
2 years ago
class-wc-admin-api-keys.php
2 years ago
class-wc-admin-assets.php
2 years ago
class-wc-admin-attributes.php
3 years ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard-setup.php
2 years ago
class-wc-admin-dashboard.php
3 years ago
class-wc-admin-duplicate-product.php
5 years ago
class-wc-admin-exporters.php
3 years ago
class-wc-admin-help.php
2 years ago
class-wc-admin-importers.php
2 years ago
class-wc-admin-log-table-list.php
2 years ago
class-wc-admin-menus.php
2 years ago
class-wc-admin-meta-boxes.php
2 years ago
class-wc-admin-notices.php
2 years ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
3 years ago
class-wc-admin-post-types.php
2 years ago
class-wc-admin-profile.php
2 years ago
class-wc-admin-reports.php
5 years ago
class-wc-admin-settings.php
2 years ago
class-wc-admin-setup-wizard.php
2 years ago
class-wc-admin-status.php
2 years ago
class-wc-admin-taxonomies.php
3 years ago
class-wc-admin-upload-downloadable-product.php
2 years ago
class-wc-admin-webhooks-table-list.php
2 years ago
class-wc-admin-webhooks.php
2 years ago
class-wc-admin.php
2 years ago
wc-admin-functions.php
2 years ago
wc-meta-box-functions.php
2 years ago
class-wc-admin-meta-boxes.php
314 lines
| 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 | |
| 10 | use Automattic\Jetpack\Constants; |
| 11 | use Automattic\WooCommerce\Internal\Admin\Orders\Edit as OrderEdit; |
| 12 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * WC_Admin_Meta_Boxes. |
| 18 | */ |
| 19 | class WC_Admin_Meta_Boxes { |
| 20 | /** |
| 21 | * Name of the option used to store errors to be displayed at the next suitable opportunity. |
| 22 | * |
| 23 | * @since 6.5.0 |
| 24 | */ |
| 25 | public const ERROR_STORE = 'woocommerce_meta_box_errors'; |
| 26 | |
| 27 | /** |
| 28 | * Is meta boxes saved once? |
| 29 | * |
| 30 | * @var boolean |
| 31 | */ |
| 32 | private static $saved_meta_boxes = false; |
| 33 | |
| 34 | /** |
| 35 | * Meta box error messages. |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | public static $meta_box_errors = array(); |
| 40 | |
| 41 | /** |
| 42 | * Constructor. |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ), 10 ); |
| 46 | add_action( 'add_meta_boxes', array( $this, 'rename_meta_boxes' ), 20 ); |
| 47 | add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 ); |
| 48 | add_action( 'add_meta_boxes', array( $this, 'add_product_boxes_sort_order' ), 40 ); |
| 49 | add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 ); |
| 50 | |
| 51 | OrderEdit::add_save_meta_boxes(); |
| 52 | |
| 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 | |
| 57 | // Save Coupon Meta Boxes. |
| 58 | add_action( 'woocommerce_process_shop_coupon_meta', 'WC_Meta_Box_Coupon_Data::save', 10, 2 ); |
| 59 | |
| 60 | // Save Rating Meta Boxes. |
| 61 | add_filter( 'wp_update_comment_data', 'WC_Meta_Box_Product_Reviews::save', 1 ); |
| 62 | |
| 63 | // Error handling (for showing errors from meta boxes on next page load). |
| 64 | add_action( 'admin_notices', array( $this, 'output_errors' ) ); |
| 65 | add_action( 'shutdown', array( $this, 'append_to_error_store' ) ); |
| 66 | |
| 67 | add_filter( 'theme_product_templates', array( $this, 'remove_block_templates' ), 10, 1 ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Add an error message. |
| 72 | * |
| 73 | * @param string $text Error to add. |
| 74 | */ |
| 75 | public static function add_error( $text ) { |
| 76 | self::$meta_box_errors[] = $text; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Save errors to an option. |
| 81 | * |
| 82 | * Note that calling this will overwrite any errors that have already been stored via the Options API. |
| 83 | * Unless you are sure you want this, consider using the append_to_error_store() method instead. |
| 84 | */ |
| 85 | public function save_errors() { |
| 86 | update_option( self::ERROR_STORE, self::$meta_box_errors ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * If additional errors have been added in the current request (ie, via the add_error() method) then they |
| 91 | * will be added to the persistent error store via the Options API. |
| 92 | * |
| 93 | * @since 6.5.0 |
| 94 | */ |
| 95 | public function append_to_error_store() { |
| 96 | if ( empty( self::$meta_box_errors ) ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $existing_errors = get_option( self::ERROR_STORE, array() ); |
| 101 | update_option( self::ERROR_STORE, array_unique( array_merge( $existing_errors, self::$meta_box_errors ) ) ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Show any stored error messages. |
| 106 | */ |
| 107 | public function output_errors() { |
| 108 | $errors = array_filter( (array) get_option( self::ERROR_STORE ) ); |
| 109 | |
| 110 | if ( ! empty( $errors ) ) { |
| 111 | |
| 112 | echo '<div id="woocommerce_errors" class="error notice is-dismissible">'; |
| 113 | |
| 114 | foreach ( $errors as $error ) { |
| 115 | echo '<p>' . wp_kses_post( $error ) . '</p>'; |
| 116 | } |
| 117 | |
| 118 | echo '</div>'; |
| 119 | |
| 120 | // Clear. |
| 121 | delete_option( self::ERROR_STORE ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Add WC Meta boxes. |
| 127 | */ |
| 128 | public function add_meta_boxes() { |
| 129 | $screen = get_current_screen(); |
| 130 | $screen_id = $screen ? $screen->id : ''; |
| 131 | |
| 132 | // Products. |
| 133 | add_meta_box( 'postexcerpt', __( 'Product short description', 'woocommerce' ), 'WC_Meta_Box_Product_Short_Description::output', 'product', 'normal' ); |
| 134 | add_meta_box( 'woocommerce-product-data', __( 'Product data', 'woocommerce' ), 'WC_Meta_Box_Product_Data::output', 'product', 'normal', 'high' ); |
| 135 | add_meta_box( 'woocommerce-product-images', __( 'Product gallery', 'woocommerce' ), 'WC_Meta_Box_Product_Images::output', 'product', 'side', 'low' ); |
| 136 | |
| 137 | // Orders. |
| 138 | foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) { |
| 139 | $order_type_object = get_post_type_object( $type ); |
| 140 | OrderEdit::add_order_meta_boxes( $type, $order_type_object->labels->singular_name ); |
| 141 | } |
| 142 | |
| 143 | // Coupons. |
| 144 | add_meta_box( 'woocommerce-coupon-data', __( 'Coupon data', 'woocommerce' ), 'WC_Meta_Box_Coupon_Data::output', 'shop_coupon', 'normal', 'high' ); |
| 145 | |
| 146 | // Comment rating. |
| 147 | if ( 'comment' === $screen_id && isset( $_GET['c'] ) && metadata_exists( 'comment', wc_clean( wp_unslash( $_GET['c'] ) ), 'rating' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 148 | add_meta_box( 'woocommerce-rating', __( 'Rating', 'woocommerce' ), 'WC_Meta_Box_Product_Reviews::output', 'comment', 'normal', 'high' ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Add default sort order for meta boxes on product page. |
| 154 | */ |
| 155 | public function add_product_boxes_sort_order() { |
| 156 | $current_value = get_user_meta( get_current_user_id(), 'meta-box-order_product', true ); |
| 157 | |
| 158 | if ( $current_value ) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | update_user_meta( |
| 163 | get_current_user_id(), |
| 164 | 'meta-box-order_product', |
| 165 | array( |
| 166 | 'side' => 'submitdiv,postimagediv,woocommerce-product-images,product_catdiv,tagsdiv-product_tag', |
| 167 | 'normal' => 'woocommerce-product-data,postcustom,slugdiv,postexcerpt', |
| 168 | 'advanced' => '', |
| 169 | ) |
| 170 | ); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Remove bloat. |
| 176 | */ |
| 177 | public function remove_meta_boxes() { |
| 178 | remove_meta_box( 'postexcerpt', 'product', 'normal' ); |
| 179 | remove_meta_box( 'product_shipping_classdiv', 'product', 'side' ); |
| 180 | remove_meta_box( 'commentsdiv', 'product', 'normal' ); |
| 181 | remove_meta_box( 'commentstatusdiv', 'product', 'side' ); |
| 182 | remove_meta_box( 'commentstatusdiv', 'product', 'normal' ); |
| 183 | remove_meta_box( 'woothemes-settings', 'shop_coupon', 'normal' ); |
| 184 | remove_meta_box( 'commentstatusdiv', 'shop_coupon', 'normal' ); |
| 185 | remove_meta_box( 'slugdiv', 'shop_coupon', 'normal' ); |
| 186 | |
| 187 | foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) { |
| 188 | remove_meta_box( 'commentsdiv', $type, 'normal' ); |
| 189 | remove_meta_box( 'woothemes-settings', $type, 'normal' ); |
| 190 | remove_meta_box( 'commentstatusdiv', $type, 'normal' ); |
| 191 | remove_meta_box( 'slugdiv', $type, 'normal' ); |
| 192 | remove_meta_box( 'submitdiv', $type, 'side' ); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Rename core meta boxes. |
| 198 | */ |
| 199 | public function rename_meta_boxes() { |
| 200 | global $post; |
| 201 | |
| 202 | // Comments/Reviews. |
| 203 | if ( isset( $post ) && ( 'publish' === $post->post_status || 'private' === $post->post_status ) && post_type_supports( 'product', 'comments' ) ) { |
| 204 | remove_meta_box( 'commentsdiv', 'product', 'normal' ); |
| 205 | add_meta_box( 'commentsdiv', __( 'Reviews', 'woocommerce' ), 'post_comment_meta_box', 'product', 'normal' ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Check if we're saving, the trigger an action based on the post type. |
| 211 | * |
| 212 | * @param int $post_id Post ID. |
| 213 | * @param object $post Post object. |
| 214 | */ |
| 215 | public function save_meta_boxes( $post_id, $post ) { |
| 216 | $post_id = absint( $post_id ); |
| 217 | |
| 218 | // $post_id and $post are required |
| 219 | if ( empty( $post_id ) || empty( $post ) || ! is_a( $post, 'WP_Post' ) || self::$saved_meta_boxes ) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | // Dont' save meta boxes for revisions or autosaves. |
| 224 | if ( Constants::is_true( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | // Check the nonce. |
| 229 | if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | // Check the post being saved == the $post_id to prevent triggering this call for other save_post events. |
| 234 | if ( empty( $_POST['post_ID'] ) || absint( $_POST['post_ID'] ) !== $post_id ) { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // Check user has permission to edit. |
| 239 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // We need this save event to run once to avoid potential endless loops. This would have been perfect: |
| 244 | // remove_action( current_filter(), __METHOD__ ); |
| 245 | // But cannot be used due to https://github.com/woocommerce/woocommerce/issues/6485 |
| 246 | // When that is patched in core we can use the above. |
| 247 | self::$saved_meta_boxes = true; |
| 248 | |
| 249 | // Check the post type. |
| 250 | if ( in_array( $post->post_type, wc_get_order_types( 'order-meta-boxes' ), true ) ) { |
| 251 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Save meta for shop order. |
| 257 | * |
| 258 | * @param int $post_id Post ID. |
| 259 | * @param object $post Post object. |
| 260 | * |
| 261 | * @since 2.1.0 |
| 262 | */ |
| 263 | do_action( 'woocommerce_process_shop_order_meta', $post_id, $post ); |
| 264 | } elseif ( in_array( $post->post_type, array( 'product', 'shop_coupon' ), true ) ) { |
| 265 | /** |
| 266 | * Save meta for product. |
| 267 | * |
| 268 | * @param int $post_id Post ID. |
| 269 | * @param object $post Post object. |
| 270 | * |
| 271 | * @since 2.1.0 |
| 272 | */ |
| 273 | do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Remove irrelevant block templates from the list of available templates for products. |
| 279 | * This will also remove custom created templates. |
| 280 | * |
| 281 | * @param string[] $templates Array of template header names keyed by the template file name. |
| 282 | * |
| 283 | * @return string[] Templates array excluding block-based templates. |
| 284 | */ |
| 285 | public function remove_block_templates( $templates ) { |
| 286 | if ( count( $templates ) === 0 || ! wc_current_theme_is_fse_theme() || ( ! function_exists( 'gutenberg_get_block_template' ) && ! function_exists( 'get_block_template' ) ) ) { |
| 287 | return $templates; |
| 288 | } |
| 289 | |
| 290 | $theme = wp_get_theme()->get_stylesheet(); |
| 291 | $filtered_templates = array(); |
| 292 | |
| 293 | foreach ( $templates as $template_key => $template_name ) { |
| 294 | // Filter out the single-product.html template as this is a duplicate of "Default Template". |
| 295 | if ( 'single-product' === $template_key ) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | $block_template = function_exists( 'gutenberg_get_block_template' ) ? |
| 300 | gutenberg_get_block_template( $theme . '//' . $template_key ) : |
| 301 | get_block_template( $theme . '//' . $template_key ); |
| 302 | |
| 303 | // If the block template has the product post type specified, include it. |
| 304 | if ( $block_template && is_array( $block_template->post_types ) && in_array( 'product', $block_template->post_types ) ) { |
| 305 | $filtered_templates[ $template_key ] = $template_name; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return $filtered_templates; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | new WC_Admin_Meta_Boxes(); |
| 314 |