2013-07-24 16:01:36 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Post Types Admin
2013-07-24 16:01:36 +00:00
*
2020-08-05 20:49:10 +00:00
* @ package WooCommerce\Admin
2017-11-13 21:40:10 +00:00
* @ version 3.3 . 0
2013-07-24 16:01:36 +00:00
*/
2020-01-29 05:21:29 +00:00
use Automattic\Jetpack\Constants ;
2020-10-01 08:57:12 +00:00
use Automattic\WooCommerce\Utilities\NumberUtil ;
2020-01-29 05:21:29 +00:00
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2016-11-16 20:48:42 +00:00
exit ;
2014-09-20 19:52:30 +00:00
}
2013-07-24 16:01:36 +00:00
2017-11-03 16:10:32 +00:00
if ( class_exists ( 'WC_Admin_Post_Types' , false ) ) {
new WC_Admin_Post_Types ();
return ;
}
2013-07-24 16:01:36 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Admin_Post_Types Class .
2014-06-04 10:16:19 +00:00
*
* Handles the edit posts views and some functionality on the edit post screen for WC post types .
2013-07-24 16:01:36 +00:00
*/
class WC_Admin_Post_Types {
/**
2015-11-03 12:28:01 +00:00
* Constructor .
2014-06-04 10:16:19 +00:00
*/
public function __construct () {
2020-08-26 20:50:34 +00:00
include_once __DIR__ . '/class-wc-admin-meta-boxes.php' ;
2017-11-03 16:10:32 +00:00
if ( ! function_exists ( 'duplicate_post_plugin_activation' ) ) {
2020-08-26 20:50:34 +00:00
include_once __DIR__ . '/class-wc-admin-duplicate-product.php' ;
2017-11-03 16:10:32 +00:00
}
// Load correct list table classes for current screen.
add_action ( 'current_screen' , array ( $this , 'setup_screen' ) );
add_action ( 'check_ajax_referer' , array ( $this , 'setup_screen' ) );
// Admin notices.
2014-06-04 10:16:19 +00:00
add_filter ( 'post_updated_messages' , array ( $this , 'post_updated_messages' ) );
2015-02-08 14:40:19 +00:00
add_filter ( 'bulk_post_updated_messages' , array ( $this , 'bulk_post_updated_messages' ), 10 , 2 );
2015-02-08 15:45:08 +00:00
2017-11-03 16:10:32 +00:00
// Disable Auto Save.
2014-06-04 10:16:19 +00:00
add_action ( 'admin_print_scripts' , array ( $this , 'disable_autosave' ) );
2017-11-03 16:10:32 +00:00
// Extra post data and screen elements.
2017-01-04 15:18:02 +00:00
add_action ( 'edit_form_top' , array ( $this , 'edit_form_top' ) );
2014-06-04 10:16:19 +00:00
add_filter ( 'enter_title_here' , array ( $this , 'enter_title_here' ), 1 , 2 );
add_action ( 'edit_form_after_title' , array ( $this , 'edit_form_after_title' ) );
2015-08-21 21:07:05 +00:00
add_filter ( 'default_hidden_meta_boxes' , array ( $this , 'hidden_meta_boxes' ), 10 , 2 );
2014-06-04 10:16:19 +00:00
add_action ( 'post_submitbox_misc_actions' , array ( $this , 'product_data_visibility' ) );
2017-11-03 16:10:32 +00:00
// Uploads.
2014-06-04 10:16:19 +00:00
add_filter ( 'upload_dir' , array ( $this , 'upload_dir' ) );
2020-02-05 09:35:26 +00:00
add_filter ( 'wp_unique_filename' , array ( $this , 'update_filename' ), 10 , 3 );
2014-06-04 10:16:19 +00:00
add_action ( 'media_upload_downloadable_product' , array ( $this , 'media_upload_downloadable_product' ) );
2016-10-11 11:44:54 +00:00
// Hide template for CPT archive.
add_filter ( 'theme_page_templates' , array ( $this , 'hide_cpt_archive_templates' ), 10 , 3 );
add_action ( 'edit_form_top' , array ( $this , 'show_cpt_archive_notice' ) );
2017-06-28 04:38:09 +00:00
// Add a post display state for special WC pages.
add_filter ( 'display_post_states' , array ( $this , 'add_display_post_states' ), 10 , 2 );
2017-11-03 16:10:32 +00:00
// Bulk / quick edit.
add_action ( 'bulk_edit_custom_box' , array ( $this , 'bulk_edit' ), 10 , 2 );
2018-03-05 18:59:17 +00:00
add_action ( 'quick_edit_custom_box' , array ( $this , 'quick_edit' ), 10 , 2 );
2017-11-03 16:10:32 +00:00
add_action ( 'save_post' , array ( $this , 'bulk_and_quick_edit_hook' ), 10 , 2 );
add_action ( 'woocommerce_product_bulk_and_quick_edit' , array ( $this , 'bulk_and_quick_edit_save_post' ), 10 , 2 );
}
/**
* Looks at the current screen and loads the correct list table handler .
*
* @ since 3.3 . 0
*/
public function setup_screen () {
2018-06-28 15:39:27 +00:00
global $wc_list_table ;
2020-04-15 07:34:34 +00:00
$request_data = $this -> request_data ();
2017-11-03 16:10:32 +00:00
$screen_id = false ;
if ( function_exists ( 'get_current_screen' ) ) {
$screen = get_current_screen ();
$screen_id = isset ( $screen , $screen -> id ) ? $screen -> id : '' ;
}
2020-07-14 09:36:24 +00:00
if ( ! empty ( $request_data [ 'screen' ] ) ) {
$screen_id = wc_clean ( wp_unslash ( $request_data [ 'screen' ] ) );
2017-11-03 16:10:32 +00:00
}
switch ( $screen_id ) {
2018-03-05 18:59:17 +00:00
case 'edit-shop_order' :
2020-08-26 20:50:34 +00:00
include_once __DIR__ . '/list-tables/class-wc-admin-list-table-orders.php' ;
2018-06-28 15:39:27 +00:00
$wc_list_table = new WC_Admin_List_Table_Orders ();
2017-11-03 16:10:32 +00:00
break ;
2018-03-05 18:59:17 +00:00
case 'edit-shop_coupon' :
2020-08-26 20:50:34 +00:00
include_once __DIR__ . '/list-tables/class-wc-admin-list-table-coupons.php' ;
2018-06-28 15:39:27 +00:00
$wc_list_table = new WC_Admin_List_Table_Coupons ();
2017-11-03 16:10:32 +00:00
break ;
2018-03-05 18:59:17 +00:00
case 'edit-product' :
2020-08-26 20:50:34 +00:00
include_once __DIR__ . '/list-tables/class-wc-admin-list-table-products.php' ;
2018-06-28 15:39:27 +00:00
$wc_list_table = new WC_Admin_List_Table_Products ();
2017-11-03 16:10:32 +00:00
break ;
}
2018-01-31 11:30:10 +00:00
// Ensure the table handler is only loaded once. Prevents multiple loads if a plugin calls check_ajax_referer many times.
remove_action ( 'current_screen' , array ( $this , 'setup_screen' ) );
remove_action ( 'check_ajax_referer' , array ( $this , 'setup_screen' ) );
2014-06-04 10:16:19 +00:00
}
2015-02-08 14:42:48 +00:00
/**
* Change messages when a post type is updated .
2017-11-03 16:10:32 +00:00
*
* @ param array $messages Array of messages .
2015-02-08 14:42:48 +00:00
* @ return array
*/
public function post_updated_messages ( $messages ) {
2017-11-03 16:10:32 +00:00
global $post ;
2015-02-08 14:42:48 +00:00
$messages [ 'product' ] = array (
2018-03-05 18:59:17 +00:00
0 => '' , // Unused. Messages start at index 1.
2018-09-26 10:43:04 +00:00
/* translators: %s: Product view URL. */
2018-03-05 18:59:17 +00:00
1 => sprintf ( __ ( 'Product updated. <a href="%s">View Product</a>' , 'woocommerce' ), esc_url ( get_permalink ( $post -> ID ) ) ),
2 => __ ( 'Custom field updated.' , 'woocommerce' ),
3 => __ ( 'Custom field deleted.' , 'woocommerce' ),
4 => __ ( 'Product updated.' , 'woocommerce' ),
5 => __ ( 'Revision restored.' , 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: product url */
2018-03-05 18:59:17 +00:00
6 => sprintf ( __ ( 'Product published. <a href="%s">View Product</a>' , 'woocommerce' ), esc_url ( get_permalink ( $post -> ID ) ) ),
7 => __ ( 'Product saved.' , 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: product url */
2018-03-05 18:59:17 +00:00
8 => sprintf ( __ ( 'Product submitted. <a target="_blank" href="%s">Preview product</a>' , 'woocommerce' ), esc_url ( add_query_arg ( 'preview' , 'true' , get_permalink ( $post -> ID ) ) ) ),
9 => sprintf (
2017-08-15 12:07:55 +00:00
/* translators: 1: date 2: product url */
__ ( 'Product scheduled for: %1$s. <a target="_blank" href="%2$s">Preview product</a>' , 'woocommerce' ),
2019-01-16 09:32:00 +00:00
'<strong>' . date_i18n ( __ ( 'M j, Y @ G:i' , 'woocommerce' ), strtotime ( $post -> post_date ) ),
esc_url ( get_permalink ( $post -> ID ) ) . '</strong>'
2017-08-15 12:07:55 +00:00
),
2016-10-29 10:16:03 +00:00
/* translators: %s: product url */
2017-11-03 16:10:32 +00:00
10 => sprintf ( __ ( 'Product draft updated. <a target="_blank" href="%s">Preview product</a>' , 'woocommerce' ), esc_url ( add_query_arg ( 'preview' , 'true' , get_permalink ( $post -> ID ) ) ) ),
2015-02-08 14:42:48 +00:00
);
$messages [ 'shop_order' ] = array (
2018-03-05 18:59:17 +00:00
0 => '' , // Unused. Messages start at index 1.
1 => __ ( 'Order updated.' , 'woocommerce' ),
2 => __ ( 'Custom field updated.' , 'woocommerce' ),
3 => __ ( 'Custom field deleted.' , 'woocommerce' ),
4 => __ ( 'Order updated.' , 'woocommerce' ),
5 => __ ( 'Revision restored.' , 'woocommerce' ),
6 => __ ( 'Order updated.' , 'woocommerce' ),
7 => __ ( 'Order saved.' , 'woocommerce' ),
8 => __ ( 'Order submitted.' , 'woocommerce' ),
9 => sprintf (
2017-08-15 12:07:55 +00:00
/* translators: %s: date */
__ ( 'Order scheduled for: %s.' , 'woocommerce' ),
2017-11-03 16:10:32 +00:00
'<strong>' . date_i18n ( __ ( 'M j, Y @ G:i' , 'woocommerce' ), strtotime ( $post -> post_date ) ) . '</strong>'
2017-08-15 12:07:55 +00:00
),
2015-02-08 14:42:48 +00:00
10 => __ ( 'Order draft updated.' , 'woocommerce' ),
2017-10-13 13:54:12 +00:00
11 => __ ( 'Order updated and sent.' , 'woocommerce' ),
2015-02-08 14:42:48 +00:00
);
$messages [ 'shop_coupon' ] = array (
2018-03-05 18:59:17 +00:00
0 => '' , // Unused. Messages start at index 1.
1 => __ ( 'Coupon updated.' , 'woocommerce' ),
2 => __ ( 'Custom field updated.' , 'woocommerce' ),
3 => __ ( 'Custom field deleted.' , 'woocommerce' ),
4 => __ ( 'Coupon updated.' , 'woocommerce' ),
5 => __ ( 'Revision restored.' , 'woocommerce' ),
6 => __ ( 'Coupon updated.' , 'woocommerce' ),
7 => __ ( 'Coupon saved.' , 'woocommerce' ),
8 => __ ( 'Coupon submitted.' , 'woocommerce' ),
9 => sprintf (
2017-08-15 12:07:55 +00:00
/* translators: %s: date */
__ ( 'Coupon scheduled for: %s.' , 'woocommerce' ),
2017-11-03 16:10:32 +00:00
'<strong>' . date_i18n ( __ ( 'M j, Y @ G:i' , 'woocommerce' ), strtotime ( $post -> post_date ) ) . '</strong>'
2017-08-15 12:07:55 +00:00
),
2016-08-27 01:46:45 +00:00
10 => __ ( 'Coupon draft updated.' , 'woocommerce' ),
2015-02-08 14:42:48 +00:00
);
return $messages ;
}
/**
2015-02-08 14:45:19 +00:00
* Specify custom bulk actions messages for different post types .
2017-11-03 16:10:32 +00:00
*
* @ param array $bulk_messages Array of messages .
* @ param array $bulk_counts Array of how many objects were updated .
2015-02-08 14:42:48 +00:00
* @ return array
*/
public function bulk_post_updated_messages ( $bulk_messages , $bulk_counts ) {
$bulk_messages [ 'product' ] = array (
2016-10-29 10:16:03 +00:00
/* translators: %s: product count */
2015-07-13 14:03:03 +00:00
'updated' => _n ( '%s product updated.' , '%s products updated.' , $bulk_counts [ 'updated' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: product count */
2015-07-13 14:03:03 +00:00
'locked' => _n ( '%s product not updated, somebody is editing it.' , '%s products not updated, somebody is editing them.' , $bulk_counts [ 'locked' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: product count */
2015-07-13 14:03:03 +00:00
'deleted' => _n ( '%s product permanently deleted.' , '%s products permanently deleted.' , $bulk_counts [ 'deleted' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: product count */
2015-07-13 14:03:03 +00:00
'trashed' => _n ( '%s product moved to the Trash.' , '%s products moved to the Trash.' , $bulk_counts [ 'trashed' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: product count */
2015-07-13 14:03:03 +00:00
'untrashed' => _n ( '%s product restored from the Trash.' , '%s products restored from the Trash.' , $bulk_counts [ 'untrashed' ], 'woocommerce' ),
2015-02-08 14:42:48 +00:00
);
$bulk_messages [ 'shop_order' ] = array (
2016-10-29 10:16:03 +00:00
/* translators: %s: order count */
2015-07-13 14:03:03 +00:00
'updated' => _n ( '%s order updated.' , '%s orders updated.' , $bulk_counts [ 'updated' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: order count */
2015-07-13 14:03:03 +00:00
'locked' => _n ( '%s order not updated, somebody is editing it.' , '%s orders not updated, somebody is editing them.' , $bulk_counts [ 'locked' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: order count */
2015-07-13 14:03:03 +00:00
'deleted' => _n ( '%s order permanently deleted.' , '%s orders permanently deleted.' , $bulk_counts [ 'deleted' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: order count */
2015-07-13 14:03:03 +00:00
'trashed' => _n ( '%s order moved to the Trash.' , '%s orders moved to the Trash.' , $bulk_counts [ 'trashed' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: order count */
2015-07-13 14:03:03 +00:00
'untrashed' => _n ( '%s order restored from the Trash.' , '%s orders restored from the Trash.' , $bulk_counts [ 'untrashed' ], 'woocommerce' ),
2015-02-08 14:42:48 +00:00
);
$bulk_messages [ 'shop_coupon' ] = array (
2016-10-29 10:16:03 +00:00
/* translators: %s: coupon count */
2015-07-13 14:03:03 +00:00
'updated' => _n ( '%s coupon updated.' , '%s coupons updated.' , $bulk_counts [ 'updated' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: coupon count */
2015-07-13 14:03:03 +00:00
'locked' => _n ( '%s coupon not updated, somebody is editing it.' , '%s coupons not updated, somebody is editing them.' , $bulk_counts [ 'locked' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: coupon count */
2015-07-13 14:03:03 +00:00
'deleted' => _n ( '%s coupon permanently deleted.' , '%s coupons permanently deleted.' , $bulk_counts [ 'deleted' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: coupon count */
2015-07-13 14:03:03 +00:00
'trashed' => _n ( '%s coupon moved to the Trash.' , '%s coupons moved to the Trash.' , $bulk_counts [ 'trashed' ], 'woocommerce' ),
2016-10-29 10:16:03 +00:00
/* translators: %s: coupon count */
2017-11-03 16:10:32 +00:00
'untrashed' => _n ( '%s coupon restored from the Trash.' , '%s coupons restored from the Trash.' , $bulk_counts [ 'untrashed' ], 'woocommerce' ),
);
2017-05-16 14:05:11 +00:00
2017-11-03 16:10:32 +00:00
return $bulk_messages ;
2017-05-16 14:05:11 +00:00
}
2014-06-04 10:16:19 +00:00
/**
2015-11-03 12:28:01 +00:00
* Custom bulk edit - form .
2014-06-04 10:16:19 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param string $column_name Column being shown .
* @ param string $post_type Post type being shown .
2014-06-04 10:16:19 +00:00
*/
public function bulk_edit ( $column_name , $post_type ) {
2017-11-03 16:10:32 +00:00
if ( 'price' !== $column_name || 'product' !== $post_type ) {
2014-06-04 10:16:19 +00:00
return ;
}
2018-03-05 18:59:17 +00:00
$shipping_class = get_terms (
2019-01-16 09:32:00 +00:00
'product_shipping_class' ,
array (
2018-03-05 18:59:17 +00:00
'hide_empty' => false ,
)
);
2014-11-04 19:49:15 +00:00
2018-03-05 18:59:17 +00:00
include WC () -> plugin_path () . '/includes/admin/views/html-bulk-edit-product.php' ;
2014-06-04 10:16:19 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Custom quick edit - form .
2014-06-04 10:16:19 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param string $column_name Column being shown .
* @ param string $post_type Post type being shown .
2014-06-04 10:16:19 +00:00
*/
public function quick_edit ( $column_name , $post_type ) {
2017-11-03 16:10:32 +00:00
if ( 'price' !== $column_name || 'product' !== $post_type ) {
2014-06-04 10:16:19 +00:00
return ;
}
2018-03-05 18:59:17 +00:00
$shipping_class = get_terms (
2019-01-16 09:32:00 +00:00
'product_shipping_class' ,
array (
2018-03-05 18:59:17 +00:00
'hide_empty' => false ,
)
);
2014-11-04 19:49:15 +00:00
2018-03-05 18:59:17 +00:00
include WC () -> plugin_path () . '/includes/admin/views/html-quick-edit-product.php' ;
2014-06-04 10:16:19 +00:00
}
2016-11-16 20:48:42 +00:00
/**
* Offers a way to hook into save post without causing an infinite loop
* when quick / bulk saving product info .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2017-11-03 16:10:32 +00:00
* @ param int $post_id Post ID being saved .
* @ param object $post Post object being saved .
2016-11-16 20:48:42 +00:00
*/
public function bulk_and_quick_edit_hook ( $post_id , $post ) {
remove_action ( 'save_post' , array ( $this , 'bulk_and_quick_edit_hook' ) );
do_action ( 'woocommerce_product_bulk_and_quick_edit' , $post_id , $post );
add_action ( 'save_post' , array ( $this , 'bulk_and_quick_edit_hook' ), 10 , 2 );
}
2014-06-04 10:16:19 +00:00
/**
2015-11-03 12:28:01 +00:00
* Quick and bulk edit saving .
2014-06-04 10:16:19 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param int $post_id Post ID being saved .
* @ param object $post Post object being saved .
2014-06-04 10:16:19 +00:00
* @ return int
*/
public function bulk_and_quick_edit_save_post ( $post_id , $post ) {
2020-04-15 07:34:34 +00:00
$request_data = $this -> request_data ();
2014-06-04 10:16:19 +00:00
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
2020-01-29 05:21:29 +00:00
if ( Constants :: is_true ( 'DOING_AUTOSAVE' ) ) {
2014-06-04 10:16:19 +00:00
return $post_id ;
}
2017-11-03 16:10:32 +00:00
// Don't save revisions and autosaves.
if ( wp_is_post_revision ( $post_id ) || wp_is_post_autosave ( $post_id ) || 'product' !== $post -> post_type || ! current_user_can ( 'edit_post' , $post_id ) ) {
2014-06-04 10:16:19 +00:00
return $post_id ;
}
2017-11-03 16:10:32 +00:00
// Check nonce.
2020-04-13 13:50:18 +00:00
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
2020-04-15 07:34:34 +00:00
if ( ! isset ( $request_data [ 'woocommerce_quick_edit_nonce' ] ) || ! wp_verify_nonce ( $request_data [ 'woocommerce_quick_edit_nonce' ], 'woocommerce_quick_edit_nonce' ) ) {
2014-06-04 10:16:19 +00:00
return $post_id ;
}
2017-11-03 16:10:32 +00:00
// Get the product and save.
2014-08-19 10:09:29 +00:00
$product = wc_get_product ( $post );
2014-06-04 10:16:19 +00:00
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ 'woocommerce_quick_edit' ] ) ) { // WPCS: input var ok.
2014-06-04 10:16:19 +00:00
$this -> quick_edit_save ( $post_id , $product );
} else {
$this -> bulk_edit_save ( $post_id , $product );
}
return $post_id ;
}
/**
2016-11-16 20:48:42 +00:00
* Quick edit .
2014-11-30 06:52:32 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param int $post_id Post ID being saved .
* @ param WC_Product $product Product object .
2014-06-04 10:16:19 +00:00
*/
private function quick_edit_save ( $post_id , $product ) {
2020-04-15 07:34:34 +00:00
$request_data = $this -> request_data ();
2016-11-16 20:48:42 +00:00
$data_store = $product -> get_data_store ();
$old_regular_price = $product -> get_regular_price ();
$old_sale_price = $product -> get_sale_price ();
2017-11-03 16:10:32 +00:00
$input_to_props = array (
'_weight' => 'weight' ,
'_length' => 'length' ,
'_width' => 'width' ,
'_height' => 'height' ,
'_visibility' => 'catalog_visibility' ,
'_tax_class' => 'tax_class' ,
'_tax_status' => 'tax_status' ,
);
foreach ( $input_to_props as $input_var => $prop ) {
2020-04-15 07:34:34 +00:00
if ( isset ( $request_data [ $input_var ] ) ) {
$product -> { " set_ { $prop } " }( wc_clean ( wp_unslash ( $request_data [ $input_var ] ) ) );
2017-11-03 16:10:32 +00:00
}
}
2014-06-04 10:16:19 +00:00
2020-04-15 07:34:34 +00:00
if ( isset ( $request_data [ '_sku' ] ) ) {
2020-04-13 13:50:18 +00:00
$sku = $product -> get_sku ();
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
2020-04-15 07:34:34 +00:00
$new_sku = ( string ) wc_clean ( $request_data [ '_sku' ] );
2014-06-04 10:16:19 +00:00
if ( $new_sku !== $sku ) {
if ( ! empty ( $new_sku ) ) {
2014-06-27 19:03:25 +00:00
$unique_sku = wc_product_has_unique_sku ( $post_id , $new_sku );
2014-06-27 18:58:29 +00:00
if ( $unique_sku ) {
2018-07-10 08:56:49 +00:00
$product -> set_sku ( wc_clean ( wp_unslash ( $new_sku ) ) );
2014-06-04 10:16:19 +00:00
}
} else {
2016-11-16 20:48:42 +00:00
$product -> set_sku ( '' );
2014-06-04 10:16:19 +00:00
}
}
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_shipping_class' ] ) ) {
if ( '_no_shipping_class' === $request_data [ '_shipping_class' ] ) {
2017-02-23 15:48:53 +00:00
$product -> set_shipping_class_id ( 0 );
} else {
2020-04-13 13:50:18 +00:00
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
2020-04-15 07:34:34 +00:00
$shipping_class_id = $data_store -> get_shipping_class_id_by_slug ( wc_clean ( $request_data [ '_shipping_class' ] ) );
2016-11-16 20:48:42 +00:00
$product -> set_shipping_class_id ( $shipping_class_id );
}
2014-11-04 19:49:15 +00:00
}
2020-04-15 07:34:34 +00:00
$product -> set_featured ( isset ( $request_data [ '_featured' ] ) );
2014-06-04 10:16:19 +00:00
2016-09-02 01:51:31 +00:00
if ( $product -> is_type ( 'simple' ) || $product -> is_type ( 'external' ) ) {
2014-06-04 10:16:19 +00:00
2020-04-15 07:34:34 +00:00
if ( isset ( $request_data [ '_regular_price' ] ) ) {
2020-04-13 13:50:18 +00:00
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
2020-04-15 07:34:34 +00:00
$new_regular_price = ( '' === $request_data [ '_regular_price' ] ) ? '' : wc_format_decimal ( $request_data [ '_regular_price' ] );
2016-11-16 20:48:42 +00:00
$product -> set_regular_price ( $new_regular_price );
2014-06-04 10:16:19 +00:00
} else {
$new_regular_price = null ;
}
2020-04-15 07:34:34 +00:00
if ( isset ( $request_data [ '_sale_price' ] ) ) {
2020-04-13 13:50:18 +00:00
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
2020-04-15 07:34:34 +00:00
$new_sale_price = ( '' === $request_data [ '_sale_price' ] ) ? '' : wc_format_decimal ( $request_data [ '_sale_price' ] );
2016-11-16 20:48:42 +00:00
$product -> set_sale_price ( $new_sale_price );
2014-06-04 10:16:19 +00:00
} else {
$new_sale_price = null ;
}
2017-11-03 16:10:32 +00:00
// Handle price - remove dates and set to lowest.
2014-06-04 10:16:19 +00:00
$price_changed = false ;
2017-11-03 16:10:32 +00:00
if ( ! is_null ( $new_regular_price ) && $new_regular_price !== $old_regular_price ) {
2014-06-04 10:16:19 +00:00
$price_changed = true ;
2017-11-03 16:10:32 +00:00
} elseif ( ! is_null ( $new_sale_price ) && $new_sale_price !== $old_sale_price ) {
2014-06-04 10:16:19 +00:00
$price_changed = true ;
}
if ( $price_changed ) {
2016-11-16 20:48:42 +00:00
$product -> set_date_on_sale_to ( '' );
$product -> set_date_on_sale_from ( '' );
2014-06-04 10:16:19 +00:00
}
}
2017-11-03 16:10:32 +00:00
// Handle Stock Data.
2020-04-13 13:50:18 +00:00
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2020-05-25 08:44:00 +00:00
$manage_stock = ! empty ( $request_data [ '_manage_stock' ] ) && 'grouped' !== $product -> get_type () ? 'yes' : 'no' ;
$backorders = ! empty ( $request_data [ '_backorders' ] ) ? wc_clean ( $request_data [ '_backorders' ] ) : 'no' ;
if ( ! empty ( $request_data [ '_stock_status' ] ) ) {
2020-04-15 07:34:34 +00:00
$stock_status = wc_clean ( $request_data [ '_stock_status' ] );
2020-04-14 10:48:02 +00:00
} else {
$stock_status = $product -> is_type ( 'variable' ) ? null : 'instock' ;
}
2020-04-13 13:50:18 +00:00
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2014-11-12 15:15:05 +00:00
2017-08-07 14:16:38 +00:00
$product -> set_manage_stock ( $manage_stock );
$product -> set_backorders ( $backorders );
2016-10-12 11:51:40 +00:00
if ( 'yes' === get_option ( 'woocommerce_manage_stock' ) ) {
2020-05-25 08:44:00 +00:00
$stock_amount = 'yes' === $manage_stock && isset ( $request_data [ '_stock' ] ) && is_numeric ( wp_unslash ( $request_data [ '_stock' ] ) ) ? wc_stock_amount ( wp_unslash ( $request_data [ '_stock' ] ) ) : '' ;
2017-08-07 14:16:38 +00:00
$product -> set_stock_quantity ( $stock_amount );
}
2016-10-12 11:51:40 +00:00
2020-05-25 08:44:00 +00:00
$product = $this -> maybe_update_stock_status ( $product , $stock_status );
2014-06-04 10:16:19 +00:00
2017-08-07 14:16:38 +00:00
$product -> save ();
2014-06-04 10:16:19 +00:00
do_action ( 'woocommerce_product_quick_edit_save' , $product );
}
/**
2015-11-03 12:28:01 +00:00
* Bulk edit .
2016-11-16 20:48:42 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param int $post_id Post ID being saved .
* @ param WC_Product $product Product object .
2014-06-04 10:16:19 +00:00
*/
public function bulk_edit_save ( $post_id , $product ) {
2020-05-25 08:44:00 +00:00
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
2020-04-13 13:50:18 +00:00
2020-04-15 07:34:34 +00:00
$request_data = $this -> request_data ();
2020-04-13 15:53:36 +00:00
$data_store = $product -> get_data_store ();
2014-06-04 10:16:19 +00:00
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ 'change_weight' ] ) && isset ( $request_data [ '_weight' ] ) ) {
$product -> set_weight ( wc_clean ( wp_unslash ( $request_data [ '_weight' ] ) ) );
2014-06-04 10:16:19 +00:00
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ 'change_dimensions' ] ) ) {
if ( isset ( $request_data [ '_length' ] ) ) {
$product -> set_length ( wc_clean ( wp_unslash ( $request_data [ '_length' ] ) ) );
2014-06-04 10:16:19 +00:00
}
2020-04-15 07:34:34 +00:00
if ( isset ( $request_data [ '_width' ] ) ) {
$product -> set_width ( wc_clean ( wp_unslash ( $request_data [ '_width' ] ) ) );
2014-06-04 10:16:19 +00:00
}
2020-04-15 07:34:34 +00:00
if ( isset ( $request_data [ '_height' ] ) ) {
$product -> set_height ( wc_clean ( wp_unslash ( $request_data [ '_height' ] ) ) );
2014-06-04 10:16:19 +00:00
}
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_tax_status' ] ) ) {
$product -> set_tax_status ( wc_clean ( $request_data [ '_tax_status' ] ) );
2014-06-04 10:16:19 +00:00
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_tax_class' ] ) ) {
$tax_class = wc_clean ( wp_unslash ( $request_data [ '_tax_class' ] ) );
2017-11-03 16:10:32 +00:00
if ( 'standard' === $tax_class ) {
2014-06-04 10:16:19 +00:00
$tax_class = '' ;
}
2016-11-16 20:48:42 +00:00
$product -> set_tax_class ( $tax_class );
2014-06-04 10:16:19 +00:00
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_shipping_class' ] ) ) {
if ( '_no_shipping_class' === $request_data [ '_shipping_class' ] ) {
2017-02-23 15:48:53 +00:00
$product -> set_shipping_class_id ( 0 );
} else {
2020-04-15 07:34:34 +00:00
$shipping_class_id = $data_store -> get_shipping_class_id_by_slug ( wc_clean ( $request_data [ '_shipping_class' ] ) );
2016-11-16 20:48:42 +00:00
$product -> set_shipping_class_id ( $shipping_class_id );
}
2014-11-04 19:49:15 +00:00
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_visibility' ] ) ) {
$product -> set_catalog_visibility ( wc_clean ( $request_data [ '_visibility' ] ) );
2014-06-04 10:16:19 +00:00
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_featured' ] ) ) {
2020-04-13 13:50:18 +00:00
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2020-04-15 07:34:34 +00:00
$product -> set_featured ( wp_unslash ( $request_data [ '_featured' ] ) );
2020-04-13 13:50:18 +00:00
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2014-06-04 10:16:19 +00:00
}
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_sold_individually' ] ) ) {
if ( 'yes' === $request_data [ '_sold_individually' ] ) {
2016-11-16 20:48:42 +00:00
$product -> set_sold_individually ( 'yes' );
2016-09-02 03:15:49 +00:00
} else {
2016-11-16 20:48:42 +00:00
$product -> set_sold_individually ( '' );
2014-06-04 10:16:19 +00:00
}
}
2017-11-03 16:10:32 +00:00
// Handle price - remove dates and set to lowest.
2018-03-05 18:59:17 +00:00
$change_price_product_types = apply_filters ( 'woocommerce_bulk_edit_save_price_product_types' , array ( 'simple' , 'external' ) );
2015-11-23 15:47:41 +00:00
$can_product_type_change_price = false ;
foreach ( $change_price_product_types as $product_type ) {
if ( $product -> is_type ( $product_type ) ) {
$can_product_type_change_price = true ;
break ;
}
}
if ( $can_product_type_change_price ) {
2020-04-13 15:53:36 +00:00
$regular_price_changed = $this -> set_new_price ( $product , 'regular' );
$sale_price_changed = $this -> set_new_price ( $product , 'sale' );
2014-06-04 10:16:19 +00:00
2020-04-13 15:53:36 +00:00
if ( $regular_price_changed || $sale_price_changed ) {
2016-11-16 20:48:42 +00:00
$product -> set_date_on_sale_to ( '' );
$product -> set_date_on_sale_from ( '' );
2014-06-04 10:16:19 +00:00
2016-11-16 20:48:42 +00:00
if ( $product -> get_regular_price () < $product -> get_sale_price () ) {
$product -> set_sale_price ( '' );
2014-06-04 10:16:19 +00:00
}
}
}
2017-11-03 16:10:32 +00:00
// Handle Stock Data.
2016-11-16 20:48:42 +00:00
$was_managing_stock = $product -> get_manage_stock () ? 'yes' : 'no' ;
$backorders = $product -> get_backorders ();
2020-04-15 07:34:34 +00:00
$backorders = ! empty ( $request_data [ '_backorders' ] ) ? wc_clean ( $request_data [ '_backorders' ] ) : $backorders ;
2014-06-04 10:16:19 +00:00
2020-04-15 07:34:34 +00:00
if ( ! empty ( $request_data [ '_manage_stock' ] ) ) {
$manage_stock = 'yes' === wc_clean ( $request_data [ '_manage_stock' ] ) && 'grouped' !== $product -> get_type () ? 'yes' : 'no' ;
2016-10-12 11:51:40 +00:00
} else {
$manage_stock = $was_managing_stock ;
}
2014-06-04 10:16:19 +00:00
2020-04-15 07:34:34 +00:00
$stock_amount = 'yes' === $manage_stock && ! empty ( $request_data [ 'change_stock' ] ) && isset ( $request_data [ '_stock' ] ) ? wc_stock_amount ( $request_data [ '_stock' ] ) : $product -> get_stock_quantity ();
2016-10-12 11:51:40 +00:00
2017-08-07 14:16:38 +00:00
$product -> set_manage_stock ( $manage_stock );
$product -> set_backorders ( $backorders );
2016-10-12 11:51:40 +00:00
if ( 'yes' === get_option ( 'woocommerce_manage_stock' ) ) {
2020-05-25 08:44:00 +00:00
$change_stock = absint ( $request_data [ 'change_stock' ] );
2019-01-16 09:22:11 +00:00
switch ( $change_stock ) {
case 2 :
2019-05-02 15:53:50 +00:00
wc_update_product_stock ( $product , $stock_amount , 'increase' , true );
2019-01-16 09:22:11 +00:00
break ;
case 3 :
2019-05-02 15:53:50 +00:00
wc_update_product_stock ( $product , $stock_amount , 'decrease' , true );
2019-01-16 09:22:11 +00:00
break ;
default :
2019-05-02 15:53:50 +00:00
wc_update_product_stock ( $product , $stock_amount , 'set' , true );
2019-01-16 09:22:11 +00:00
break ;
}
2019-10-21 18:51:44 +00:00
} else {
2019-10-22 05:26:08 +00:00
// Reset values if WooCommerce Setting - Manage Stock status is disabled.
2019-10-21 18:51:44 +00:00
$product -> set_stock_quantity ( '' );
$product -> set_manage_stock ( 'no' );
2019-10-21 17:15:05 +00:00
}
2016-10-12 11:51:40 +00:00
2020-04-15 07:34:34 +00:00
$stock_status = empty ( $request_data [ '_stock_status' ] ) ? null : wc_clean ( $request_data [ '_stock_status' ] );
2020-05-25 08:44:00 +00:00
$product = $this -> maybe_update_stock_status ( $product , $stock_status );
2014-06-04 10:16:19 +00:00
2017-08-07 14:16:38 +00:00
$product -> save ();
2014-06-04 10:16:19 +00:00
do_action ( 'woocommerce_product_bulk_edit_save' , $product );
2020-04-13 13:50:18 +00:00
2020-05-25 08:44:00 +00:00
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
2014-06-04 10:16:19 +00:00
}
2013-07-31 14:12:53 +00:00
/**
* Disable the auto - save functionality for Orders .
*/
2014-12-17 12:16:14 +00:00
public function disable_autosave () {
2014-10-07 05:16:59 +00:00
global $post ;
2013-07-31 14:12:53 +00:00
2017-11-03 16:10:32 +00:00
if ( $post && in_array ( get_post_type ( $post -> ID ), wc_get_order_types ( 'order-meta-boxes' ), true ) ) {
2014-10-07 05:16:59 +00:00
wp_dequeue_script ( 'autosave' );
}
2013-07-31 14:12:53 +00:00
}
2013-07-24 16:01:36 +00:00
2017-01-04 15:18:02 +00:00
/**
* Output extra data on post forms .
2017-11-03 16:10:32 +00:00
*
* @ param WP_Post $post Current post object .
2017-01-04 15:18:02 +00:00
*/
public function edit_form_top ( $post ) {
echo '<input type="hidden" id="original_post_title" name="original_post_title" value="' . esc_attr ( $post -> post_title ) . '" />' ;
}
2014-06-04 10:16:19 +00:00
/**
* Change title boxes in admin .
2017-11-03 16:10:32 +00:00
*
* @ param string $text Text to shown .
* @ param WP_Post $post Current post object .
2014-06-04 10:16:19 +00:00
* @ return string
*/
public function enter_title_here ( $text , $post ) {
switch ( $post -> post_type ) {
2017-11-13 21:40:10 +00:00
case 'product' :
$text = esc_html__ ( 'Product name' , 'woocommerce' );
break ;
case 'shop_coupon' :
$text = esc_html__ ( 'Coupon code' , 'woocommerce' );
break ;
2014-06-04 10:16:19 +00:00
}
return $text ;
}
/**
2015-08-21 21:12:03 +00:00
* Print coupon description textarea field .
2017-11-03 16:10:32 +00:00
*
* @ param WP_Post $post Current post object .
2014-06-04 10:16:19 +00:00
*/
2015-08-21 21:12:03 +00:00
public function edit_form_after_title ( $post ) {
2020-04-13 13:50:18 +00:00
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
2014-06-04 10:16:19 +00:00
if ( 'shop_coupon' === $post -> post_type ) {
?>
2020-04-13 13:50:18 +00:00
< textarea id = " woocommerce-coupon-description " name = " excerpt " cols = " 5 " rows = " 2 " placeholder = " <?php esc_attr_e( 'Description (optional)', 'woocommerce' ); ?> " >< ? php echo $post -> post_excerpt ; ?> </textarea>
2014-06-04 10:16:19 +00:00
< ? php
}
2020-04-13 13:50:18 +00:00
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
2014-06-04 10:16:19 +00:00
}
2015-08-21 21:07:05 +00:00
/**
* Hidden default Meta - Boxes .
2017-11-03 16:10:32 +00:00
*
* @ param array $hidden Hidden boxes .
* @ param object $screen Current screen .
2015-08-21 21:07:05 +00:00
* @ return array
*/
public function hidden_meta_boxes ( $hidden , $screen ) {
if ( 'product' === $screen -> post_type && 'post' === $screen -> base ) {
$hidden = array_merge ( $hidden , array ( 'postcustom' ) );
}
return $hidden ;
}
2014-06-04 10:16:19 +00:00
/**
* Output product visibility options .
*/
public function product_data_visibility () {
2016-12-08 10:56:45 +00:00
global $post , $thepostid , $product_object ;
2014-06-04 10:16:19 +00:00
2016-12-08 10:56:45 +00:00
if ( 'product' !== $post -> post_type ) {
2014-06-04 10:16:19 +00:00
return ;
}
2016-12-08 10:56:45 +00:00
$thepostid = $post -> ID ;
2017-11-13 21:40:10 +00:00
$product_object = $thepostid ? wc_get_product ( $thepostid ) : new WC_Product ();
2016-12-08 10:56:45 +00:00
$current_visibility = $product_object -> get_catalog_visibility ();
$current_featured = wc_bool_to_string ( $product_object -> get_featured () );
$visibility_options = wc_get_product_visibility_options ();
2014-06-04 10:16:19 +00:00
?>
< div class = " misc-pub-section " id = " catalog-visibility " >
2018-03-05 18:59:17 +00:00
< ? php esc_html_e ( 'Catalog visibility:' , 'woocommerce' ); ?>
< strong id = " catalog-visibility-display " >
< ? php
2014-06-04 10:16:19 +00:00
2018-03-05 18:59:17 +00:00
echo isset ( $visibility_options [ $current_visibility ] ) ? esc_html ( $visibility_options [ $current_visibility ] ) : esc_html ( $current_visibility );
2017-11-03 16:10:32 +00:00
2018-03-05 18:59:17 +00:00
if ( 'yes' === $current_featured ) {
echo ', ' . esc_html__ ( 'Featured' , 'woocommerce' );
}
?>
</ strong >
2014-06-04 10:16:19 +00:00
2017-11-03 16:10:32 +00:00
< a href = " #catalog-visibility " class = " edit-catalog-visibility hide-if-no-js " >< ? php esc_html_e ( 'Edit' , 'woocommerce' ); ?> </a>
2014-06-04 10:16:19 +00:00
< div id = " catalog-visibility-select " class = " hide-if-js " >
< input type = " hidden " name = " current_visibility " id = " current_visibility " value = " <?php echo esc_attr( $current_visibility ); ?> " />
< input type = " hidden " name = " current_featured " id = " current_featured " value = " <?php echo esc_attr( $current_featured ); ?> " />
< ? php
2017-11-03 16:22:35 +00:00
echo '<p>' . esc_html__ ( 'This setting determines which shop pages products will be listed on.' , 'woocommerce' ) . '</p>' ;
2014-06-04 10:16:19 +00:00
2017-11-03 16:10:32 +00:00
foreach ( $visibility_options as $name => $label ) {
echo '<input type="radio" name="_visibility" id="_visibility_' . esc_attr ( $name ) . '" value="' . esc_attr ( $name ) . '" ' . checked ( $current_visibility , $name , false ) . ' data-label="' . esc_attr ( $label ) . '" /> <label for="_visibility_' . esc_attr ( $name ) . '" class="selectit">' . esc_html ( $label ) . '</label><br />' ;
}
2014-06-04 10:16:19 +00:00
2017-11-18 15:58:09 +00:00
echo '<br /><input type="checkbox" name="_featured" id="_featured" ' . checked ( $current_featured , 'yes' , false ) . ' /> <label for="_featured">' . esc_html__ ( 'This is a featured product' , 'woocommerce' ) . '</label><br />' ;
2014-06-04 10:16:19 +00:00
?>
< p >
2017-11-03 16:10:32 +00:00
< a href = " #catalog-visibility " class = " save-post-visibility hide-if-no-js button " >< ? php esc_html_e ( 'OK' , 'woocommerce' ); ?> </a>
< a href = " #catalog-visibility " class = " cancel-post-visibility hide-if-no-js " >< ? php esc_html_e ( 'Cancel' , 'woocommerce' ); ?> </a>
2014-06-04 10:16:19 +00:00
</ p >
</ div >
</ div >
< ? php
}
/**
2017-11-03 16:10:32 +00:00
* Change upload dir for downloadable files .
2014-06-04 10:16:19 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param array $pathdata Array of paths .
2014-06-04 10:16:19 +00:00
* @ return array
*/
public function upload_dir ( $pathdata ) {
2020-04-13 13:50:18 +00:00
// phpcs:disable WordPress.Security.NonceVerification.Missing
if ( isset ( $_POST [ 'type' ] ) && 'downloadable_product' === $_POST [ 'type' ] ) {
2014-11-30 06:52:32 +00:00
2014-06-04 10:16:19 +00:00
if ( empty ( $pathdata [ 'subdir' ] ) ) {
$pathdata [ 'path' ] = $pathdata [ 'path' ] . '/woocommerce_uploads' ;
2016-09-01 20:50:14 +00:00
$pathdata [ 'url' ] = $pathdata [ 'url' ] . '/woocommerce_uploads' ;
2014-06-04 10:16:19 +00:00
$pathdata [ 'subdir' ] = '/woocommerce_uploads' ;
} else {
2017-11-18 19:06:45 +00:00
$new_subdir = '/woocommerce_uploads' . $pathdata [ 'subdir' ];
2014-06-04 10:16:19 +00:00
2017-11-18 19:06:45 +00:00
$pathdata [ 'path' ] = str_replace ( $pathdata [ 'subdir' ], $new_subdir , $pathdata [ 'path' ] );
$pathdata [ 'url' ] = str_replace ( $pathdata [ 'subdir' ], $new_subdir , $pathdata [ 'url' ] );
$pathdata [ 'subdir' ] = str_replace ( $pathdata [ 'subdir' ], $new_subdir , $pathdata [ 'subdir' ] );
2014-06-04 10:16:19 +00:00
}
}
return $pathdata ;
2020-04-13 13:50:18 +00:00
// phpcs:enable WordPress.Security.NonceVerification.Missing
2014-06-04 10:16:19 +00:00
}
2020-01-15 12:14:43 +00:00
/**
* Change filename for WooCommerce uploads and prepend unique chars for security .
*
* @ param string $full_filename Original filename .
* @ param string $ext Extension of file .
* @ param string $dir Directory path .
*
* @ return string New filename with unique hash .
2020-02-05 10:09:46 +00:00
* @ since 4.0
2020-01-15 12:14:43 +00:00
*/
2020-02-05 09:35:26 +00:00
public function update_filename ( $full_filename , $ext , $dir ) {
2020-04-13 13:50:18 +00:00
// phpcs:disable WordPress.Security.NonceVerification.Missing
if ( ! isset ( $_POST [ 'type' ] ) || ! 'downloadable_product' === $_POST [ 'type' ] ) {
2020-01-15 12:14:43 +00:00
return $full_filename ;
}
if ( ! strpos ( $dir , 'woocommerce_uploads' ) ) {
return $full_filename ;
}
2020-01-17 14:04:16 +00:00
if ( 'no' === get_option ( 'woocommerce_downloads_add_hash_to_filename' ) ) {
return $full_filename ;
}
2020-01-15 12:14:43 +00:00
return $this -> unique_filename ( $full_filename , $ext );
2020-04-13 13:50:18 +00:00
// phpcs:enable WordPress.Security.NonceVerification.Missing
2020-01-15 12:14:43 +00:00
}
/**
* Change filename to append random text .
*
* @ param string $full_filename Original filename with extension .
* @ param string $ext Extension .
*
* @ return string Modified filename .
*/
public function unique_filename ( $full_filename , $ext ) {
2020-01-17 14:04:16 +00:00
$ideal_random_char_length = 6 ; // Not going with a larger length because then downloaded filename will not be pretty.
2020-01-15 12:14:43 +00:00
$max_filename_length = 255 ; // Max file name length for most file systems.
$length_to_prepend = min ( $ideal_random_char_length , $max_filename_length - strlen ( $full_filename ) - 1 );
if ( 1 > $length_to_prepend ) {
return $full_filename ;
}
2020-04-13 13:50:18 +00:00
$suffix = strtolower ( wp_generate_password ( $length_to_prepend , false , false ) );
2020-01-15 12:14:43 +00:00
$filename = $full_filename ;
if ( strlen ( $ext ) > 0 ) {
2020-04-13 13:50:18 +00:00
$filename = substr ( $filename , 0 , strlen ( $filename ) - strlen ( $ext ) );
2020-01-15 12:14:43 +00:00
}
$full_filename = str_replace (
$filename ,
" $filename - $suffix " ,
$full_filename
);
return $full_filename ;
}
2014-06-04 10:16:19 +00:00
/**
* Run a filter when uploading a downloadable product .
*/
public function woocommerce_media_upload_downloadable_product () {
do_action ( 'media_upload_file' );
}
/**
2015-11-03 12:28:01 +00:00
* Grant downloadable file access to any newly added files on any existing .
* orders for this product that have previously been granted downloadable file access .
2014-06-04 10:16:19 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param int $product_id product identifier .
* @ param int $variation_id optional product variation identifier .
* @ param array $downloadable_files newly set files .
2020-07-16 20:13:08 +00:00
* @ deprecated 3.3 . 0 and moved to post - data class .
2014-06-04 10:16:19 +00:00
*/
public function process_product_file_download_paths ( $product_id , $variation_id , $downloadable_files ) {
2020-07-27 19:48:39 +00:00
wc_deprecated_function ( 'WC_Admin_Post_Types::process_product_file_download_paths' , '3.3' , '' );
2016-11-18 19:29:37 +00:00
WC_Post_Data :: process_product_file_download_paths ( $product_id , $variation_id , $downloadable_files );
2014-06-12 07:59:45 +00:00
}
2015-07-20 17:15:51 +00:00
2016-10-11 11:44:54 +00:00
/**
* When editing the shop page , we should hide templates .
2017-05-15 11:50:52 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param array $page_templates Templates array .
2018-09-26 10:43:04 +00:00
* @ param string $theme Classname .
2017-11-03 16:10:32 +00:00
* @ param WP_Post $post The current post object .
2016-10-11 11:44:54 +00:00
* @ return array
*/
2017-11-13 21:40:10 +00:00
public function hide_cpt_archive_templates ( $page_templates , $theme , $post ) {
2016-10-11 11:44:54 +00:00
$shop_page_id = wc_get_page_id ( 'shop' );
2018-09-26 10:43:04 +00:00
if ( $post && absint ( $post -> ID ) === $shop_page_id ) {
2016-10-11 11:44:54 +00:00
$page_templates = array ();
}
return $page_templates ;
}
/**
* Show a notice above the CPT archive .
2017-05-15 11:50:52 +00:00
*
2017-11-03 16:10:32 +00:00
* @ param WP_Post $post The current post object .
2016-10-11 11:44:54 +00:00
*/
public function show_cpt_archive_notice ( $post ) {
$shop_page_id = wc_get_page_id ( 'shop' );
2018-09-26 10:43:04 +00:00
if ( $post && absint ( $post -> ID ) === $shop_page_id ) {
2017-11-03 16:10:32 +00:00
echo '<div class="notice notice-info">' ;
2018-09-26 10:43:04 +00:00
/* translators: %s: URL to read more about the shop page. */
2017-11-18 17:16:18 +00:00
echo '<p>' . sprintf ( wp_kses_post ( __ ( 'This is the WooCommerce shop page. The shop page is a special archive that lists your products. <a href="%s">You can read more about this here</a>.' , 'woocommerce' ) ), 'https://docs.woocommerce.com/document/woocommerce-pages/#section-4' ) . '</p>' ;
2017-11-03 16:10:32 +00:00
echo '</div>' ;
2016-10-11 11:44:54 +00:00
}
}
2017-06-28 04:38:09 +00:00
/**
* Add a post display state for special WC pages in the page list table .
*
* @ param array $post_states An array of post display states .
* @ param WP_Post $post The current post object .
*/
public function add_display_post_states ( $post_states , $post ) {
if ( wc_get_page_id ( 'shop' ) === $post -> ID ) {
$post_states [ 'wc_page_for_shop' ] = __ ( 'Shop Page' , 'woocommerce' );
}
if ( wc_get_page_id ( 'cart' ) === $post -> ID ) {
$post_states [ 'wc_page_for_cart' ] = __ ( 'Cart Page' , 'woocommerce' );
}
if ( wc_get_page_id ( 'checkout' ) === $post -> ID ) {
$post_states [ 'wc_page_for_checkout' ] = __ ( 'Checkout Page' , 'woocommerce' );
}
if ( wc_get_page_id ( 'myaccount' ) === $post -> ID ) {
$post_states [ 'wc_page_for_myaccount' ] = __ ( 'My Account Page' , 'woocommerce' );
}
if ( wc_get_page_id ( 'terms' ) === $post -> ID ) {
2017-06-28 04:55:14 +00:00
$post_states [ 'wc_page_for_terms' ] = __ ( 'Terms and Conditions Page' , 'woocommerce' );
2017-06-28 04:38:09 +00:00
}
return $post_states ;
}
2020-04-13 15:53:36 +00:00
/**
* Apply product type constraints to stock status .
*
2020-04-14 10:48:02 +00:00
* @ param WC_Product $product The product whose stock status will be adjusted .
* @ param string | null $stock_status The stock status to use for adjustment , or null if no new stock status has been supplied in the request .
2020-04-13 15:53:36 +00:00
* @ return WC_Product The supplied product , or the synced product if it was a variable product .
*/
2020-05-25 08:44:00 +00:00
private function maybe_update_stock_status ( $product , $stock_status ) {
2020-04-13 15:53:36 +00:00
if ( $product -> is_type ( 'external' ) ) {
// External products are always in stock.
$product -> set_stock_status ( 'instock' );
2020-04-14 10:48:02 +00:00
} elseif ( isset ( $stock_status ) ) {
if ( $product -> is_type ( 'variable' ) && ! $product -> get_manage_stock () ) {
// Stock status is determined by children.
foreach ( $product -> get_children () as $child_id ) {
$child = wc_get_product ( $child_id );
if ( ! $product -> get_manage_stock () ) {
$child -> set_stock_status ( $stock_status );
$child -> save ();
}
2020-04-13 15:53:36 +00:00
}
2020-04-14 10:48:02 +00:00
$product = WC_Product_Variable :: sync ( $product , false );
} else {
$product -> set_stock_status ( $stock_status );
2020-04-13 15:53:36 +00:00
}
}
return $product ;
}
/**
* Set the new regular or sale price if requested .
*
* @ param WC_Product $product The product to set the new price for .
* @ param string $price_type 'regular' or 'sale' .
* @ return bool true if a new price has been set , false otherwise .
*/
private function set_new_price ( $product , $price_type ) {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
2020-04-15 07:34:34 +00:00
$request_data = $this -> request_data ();
if ( empty ( $request_data [ " change_ { $price_type } _price " ] ) || ! isset ( $request_data [ " _ { $price_type } _price " ] ) ) {
2020-04-13 15:53:36 +00:00
return false ;
}
$old_price = $product -> { " get_ { $price_type } _price " }();
$price_changed = false ;
2020-04-15 07:34:34 +00:00
$change_price = absint ( $request_data [ " change_ { $price_type } _price " ] );
$raw_price = wc_clean ( wp_unslash ( $request_data [ " _ { $price_type } _price " ] ) );
2020-04-13 15:53:36 +00:00
$is_percentage = ( bool ) strstr ( $raw_price , '%' );
$price = wc_format_decimal ( $raw_price );
switch ( $change_price ) {
case 1 :
$new_price = $price ;
break ;
case 2 :
if ( $is_percentage ) {
$percent = $price / 100 ;
2020-07-14 09:36:24 +00:00
$new_price = $old_price + ( $old_price * $percent );
2020-04-13 15:53:36 +00:00
} else {
$new_price = $old_price + $price ;
}
break ;
case 3 :
if ( $is_percentage ) {
$percent = $price / 100 ;
2020-07-14 09:36:24 +00:00
$new_price = max ( 0 , $old_price - ( $old_price * $percent ) );
2020-04-13 15:53:36 +00:00
} else {
$new_price = max ( 0 , $old_price - $price );
}
break ;
case 4 :
if ( 'sale' !== $price_type ) {
break ;
}
2020-04-16 07:01:21 +00:00
$regular_price = $product -> get_regular_price ();
2020-04-13 15:53:36 +00:00
if ( $is_percentage ) {
$percent = $price / 100 ;
2020-10-01 08:57:12 +00:00
$new_price = max ( 0 , $regular_price - ( NumberUtil :: round ( $regular_price * $percent , wc_get_price_decimals () ) ) );
2020-04-13 15:53:36 +00:00
} else {
2020-04-16 07:01:21 +00:00
$new_price = max ( 0 , $regular_price - $price );
2020-04-13 15:53:36 +00:00
}
break ;
default :
break ;
}
if ( isset ( $new_price ) && $new_price !== $old_price ) {
$price_changed = true ;
2020-10-01 08:57:12 +00:00
$new_price = NumberUtil :: round ( $new_price , wc_get_price_decimals () );
2020-04-13 15:53:36 +00:00
$product -> { " set_ { $price_type } _price " }( $new_price );
}
return $price_changed ;
// phpcs:disable WordPress.Security.NonceVerification.Recommended
}
2020-04-15 07:34:34 +00:00
/**
2020-05-25 08:44:00 +00:00
* Get the current request data ( $_REQUEST superglobal ) .
2020-04-15 07:34:34 +00:00
* This method is added to ease unit testing .
*
* @ return array The $_REQUEST superglobal .
*/
protected function request_data () {
return $_REQUEST ;
}
2013-07-24 16:01:36 +00:00
}
2014-06-12 07:59:45 +00:00
new WC_Admin_Post_Types ();