abstracts
4 weeks ago
admin
4 days ago
blocks
11 months ago
cli
4 weeks ago
customizer
4 months ago
data-stores
4 weeks ago
emails
4 days ago
export
1 year ago
gateways
4 weeks ago
import
3 months ago
integrations
2 months ago
interfaces
4 months ago
legacy
4 months ago
libraries
4 weeks ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 months ago
react-admin
4 weeks ago
rest-api
4 weeks ago
shipping
3 months ago
shortcodes
4 weeks ago
theme-support
2 years ago
tracks
4 weeks ago
traits
5 years ago
walkers
5 years ago
wccom-site
4 weeks ago
widgets
2 months ago
class-wc-ajax.php
4 weeks ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
9 months ago
class-wc-background-emailer.php
2 months ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
4 weeks ago
class-wc-breadcrumb.php
4 months ago
class-wc-cache-helper.php
4 weeks ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
3 months ago
class-wc-cart-totals.php
11 months ago
class-wc-cart.php
4 weeks ago
class-wc-checkout.php
4 weeks ago
class-wc-cli.php
10 months ago
class-wc-comments.php
4 months ago
class-wc-countries.php
2 months ago
class-wc-coupon.php
2 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
4 weeks ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 months ago
class-wc-discounts.php
11 months ago
class-wc-download-handler.php
4 weeks ago
class-wc-emails.php
4 weeks ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
3 months ago
class-wc-frontend-scripts.php
2 months ago
class-wc-geo-ip.php
9 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
2 months ago
class-wc-https.php
3 years ago
class-wc-install.php
4 days ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
4 months ago
class-wc-meta-data.php
4 weeks ago
class-wc-order-factory.php
2 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
5 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 weeks ago
class-wc-order-item-shipping.php
5 months ago
class-wc-order-item-tax.php
4 weeks ago
class-wc-order-item.php
4 weeks ago
class-wc-order-query.php
4 weeks ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
4 weeks ago
class-wc-payment-gateways.php
4 weeks ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
2 months ago
class-wc-post-types.php
4 weeks ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
10 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 months ago
class-wc-product-download.php
4 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
3 months ago
class-wc-product-grouped.php
3 months ago
class-wc-product-query.php
4 months ago
class-wc-product-simple.php
11 months ago
class-wc-product-variable.php
4 weeks ago
class-wc-product-variation.php
4 weeks ago
class-wc-query.php
4 weeks ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
2 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
7 months ago
class-wc-shipping.php
2 months ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
4 weeks ago
class-wc-tax.php
2 months ago
class-wc-template-loader.php
7 months ago
class-wc-tracker.php
4 weeks ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
4 weeks ago
class-woocommerce.php
4 days ago
wc-account-functions.php
4 weeks ago
wc-attribute-functions.php
4 weeks ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
4 weeks ago
wc-conditional-functions.php
11 months ago
wc-core-functions.php
2 months ago
wc-coupon-functions.php
5 months ago
wc-deprecated-functions.php
4 weeks ago
wc-formatting-functions.php
4 weeks ago
wc-interactivity-api-functions.php
2 months ago
wc-notice-functions.php
5 months ago
wc-order-functions.php
1 month ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
4 months ago
wc-page-functions.php
1 month ago
wc-product-functions.php
2 months ago
wc-rest-functions.php
7 months ago
wc-stock-functions.php
7 months ago
wc-template-functions.php
4 weeks ago
wc-template-hooks.php
10 months ago
wc-term-functions.php
2 months ago
wc-update-functions.php
4 days ago
wc-user-functions.php
4 weeks ago
wc-webhook-functions.php
4 weeks ago
wc-widget-functions.php
5 years ago
class-wc-post-data.php
1179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Post Data |
| 4 | * |
| 5 | * Standardises certain post data on save. |
| 6 | * |
| 7 | * @package WooCommerce\Classes\Data |
| 8 | * @version 2.2.0 |
| 9 | */ |
| 10 | |
| 11 | use Automattic\WooCommerce\Enums\OrderStatus; |
| 12 | use Automattic\WooCommerce\Enums\OrderInternalStatus; |
| 13 | use Automattic\WooCommerce\Enums\ProductStatus; |
| 14 | use Automattic\WooCommerce\Enums\ProductType; |
| 15 | use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer; |
| 16 | use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore as ProductAttributesLookupDataStore; |
| 17 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 18 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 19 | |
| 20 | defined( 'ABSPATH' ) || exit; |
| 21 | |
| 22 | /** |
| 23 | * Post data class. |
| 24 | */ |
| 25 | class WC_Post_Data { |
| 26 | |
| 27 | /** |
| 28 | * Editing term. |
| 29 | * |
| 30 | * @var object |
| 31 | */ |
| 32 | private static $editing_term = null; |
| 33 | |
| 34 | /** |
| 35 | * Hook in methods. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public static function init() { |
| 40 | add_action( 'clean_post_cache', array( __CLASS__, 'invalidate_products_last_modified' ), 10, 2 ); |
| 41 | add_action( 'clean_post_cache', array( __CLASS__, 'invalidate_db_block_templates_cache' ), 10, 2 ); |
| 42 | add_filter( 'post_type_link', array( __CLASS__, 'variation_post_link' ), 10, 2 ); |
| 43 | add_action( 'shutdown', array( __CLASS__, 'do_deferred_product_sync' ), 10 ); |
| 44 | add_action( 'set_object_terms', array( __CLASS__, 'force_default_term' ), 10, 5 ); |
| 45 | add_action( 'set_object_terms', array( __CLASS__, 'delete_product_query_transients' ) ); |
| 46 | add_action( 'set_object_terms', array( __CLASS__, 'recount_terms_for_product_visibility_change' ), 10, 6 ); |
| 47 | add_action( 'deleted_term_relationships', array( __CLASS__, 'delete_product_query_transients' ) ); |
| 48 | add_action( 'woocommerce_product_set_stock_status', array( __CLASS__, 'delete_product_query_transients' ) ); |
| 49 | add_action( 'woocommerce_product_set_visibility', array( __CLASS__, 'delete_product_query_transients' ) ); |
| 50 | add_action( 'woocommerce_product_type_changed', array( __CLASS__, 'product_type_changed' ), 10, 3 ); |
| 51 | |
| 52 | add_action( 'edit_term', array( __CLASS__, 'edit_term' ), 10, 3 ); |
| 53 | add_action( 'edited_term', array( __CLASS__, 'edited_term' ), 10, 3 ); |
| 54 | add_filter( 'update_order_item_metadata', array( __CLASS__, 'update_order_item_metadata' ), 10, 5 ); |
| 55 | add_filter( 'update_post_metadata', array( __CLASS__, 'update_post_metadata' ), 10, 5 ); |
| 56 | add_filter( 'wp_insert_post_data', array( __CLASS__, 'wp_insert_post_data' ) ); |
| 57 | add_filter( 'oembed_response_data', array( __CLASS__, 'filter_oembed_response_data' ), 10, 2 ); |
| 58 | add_filter( 'wp_untrash_post_status', array( __CLASS__, 'wp_untrash_post_status' ), 10, 3 ); |
| 59 | |
| 60 | // Status transitions. |
| 61 | add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 ); |
| 62 | add_action( 'delete_post', array( __CLASS__, 'delete_post_data' ) ); |
| 63 | add_action( 'wp_trash_post', array( __CLASS__, 'trash_post' ) ); |
| 64 | add_action( 'untrashed_post', array( __CLASS__, 'untrash_post' ) ); |
| 65 | add_action( 'before_delete_post', array( __CLASS__, 'before_delete_order' ) ); |
| 66 | add_action( 'woocommerce_before_delete_order', array( __CLASS__, 'before_delete_order' ) ); |
| 67 | |
| 68 | // Meta cache flushing. |
| 69 | add_action( 'updated_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 ); |
| 70 | add_action( 'added_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 ); |
| 71 | add_action( 'deleted_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 ); |
| 72 | add_action( 'updated_order_item_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 ); |
| 73 | |
| 74 | // Product Variations - Attributes. |
| 75 | // Priority 50 to make sure this runs after WooCommerce attribute migrations. |
| 76 | add_action( 'woocommerce_attribute_updated', array( __CLASS__, 'handle_global_attribute_updated' ), 50, 3 ); |
| 77 | add_action( 'woocommerce_attribute_deleted', array( __CLASS__, 'handle_global_attribute_updated' ), 10, 3 ); |
| 78 | // Product Variations - Terms. |
| 79 | add_action( 'edited_term', array( __CLASS__, 'handle_attribute_term_updated' ), 10, 3 ); |
| 80 | add_action( 'delete_term', array( __CLASS__, 'handle_attribute_term_deleted' ), 10, 4 ); |
| 81 | // Product Variations - Parent Product Updates Attributes. |
| 82 | add_action( 'woocommerce_product_attributes_updated', array( __CLASS__, 'on_product_attributes_updated' ), 10, 1 ); |
| 83 | // Product Variations - Action Scheduler. |
| 84 | add_action( 'wc_regenerate_product_variation_summaries', array( __CLASS__, 'regenerate_product_variation_summaries' ), 10, 1 ); |
| 85 | add_action( 'wc_regenerate_attribute_variation_summaries', array( __CLASS__, 'regenerate_attribute_variation_summaries' ), 10, 1 ); |
| 86 | add_action( 'wc_regenerate_term_variation_summaries', array( __CLASS__, 'regenerate_term_variation_summaries' ), 10, 2 ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Link to parent products when getting permalink for variation. |
| 91 | * |
| 92 | * @param string $permalink Permalink. |
| 93 | * @param WP_Post $post Post data. |
| 94 | * |
| 95 | * @return string |
| 96 | */ |
| 97 | public static function variation_post_link( $permalink, $post ) { |
| 98 | if ( isset( $post->ID, $post->post_type ) && 'product_variation' === $post->post_type ) { |
| 99 | $variation = wc_get_product( $post->ID ); |
| 100 | |
| 101 | if ( $variation && $variation->get_parent_id() ) { |
| 102 | return $variation->get_permalink(); |
| 103 | } |
| 104 | } |
| 105 | return $permalink; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sync products queued to sync. |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | public static function do_deferred_product_sync() { |
| 114 | global $wc_deferred_product_sync; |
| 115 | |
| 116 | if ( ! empty( $wc_deferred_product_sync ) ) { |
| 117 | $wc_deferred_product_sync = wp_parse_id_list( $wc_deferred_product_sync ); |
| 118 | array_walk( $wc_deferred_product_sync, array( __CLASS__, 'deferred_product_sync' ) ); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Sync a product. |
| 124 | * |
| 125 | * @param int $product_id Product ID. |
| 126 | * |
| 127 | * @return void |
| 128 | */ |
| 129 | public static function deferred_product_sync( $product_id ) { |
| 130 | $product = wc_get_product( $product_id ); |
| 131 | |
| 132 | if ( is_callable( array( $product, 'sync' ) ) ) { |
| 133 | $product->sync( $product ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * When a post status changes. |
| 139 | * |
| 140 | * @param string $new_status New status. |
| 141 | * @param string $old_status Old status. |
| 142 | * @param WP_Post $post Post data. |
| 143 | * |
| 144 | * @return void |
| 145 | */ |
| 146 | public static function transition_post_status( $new_status, $old_status, $post ) { |
| 147 | if ( ( ProductStatus::PUBLISH === $new_status || ProductStatus::PUBLISH === $old_status ) && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) { |
| 148 | self::delete_product_query_transients(); |
| 149 | } |
| 150 | |
| 151 | if ( ProductStatus::PUBLISH === $new_status && ProductStatus::PUBLISH !== $old_status && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) { |
| 152 | /** |
| 153 | * Fires when a product or product variation transitions to published status. |
| 154 | * |
| 155 | * @since 10.8.0 |
| 156 | * @param int $product_id The product or variation ID. |
| 157 | */ |
| 158 | do_action( 'woocommerce_product_published', $post->ID ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Delete product view transients when needed e.g. when post status changes, or visibility/stock status is modified. |
| 164 | * |
| 165 | * @return void |
| 166 | */ |
| 167 | public static function delete_product_query_transients() { |
| 168 | WC_Cache_Helper::get_transient_version( 'product_query', true ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Invalidate the cached products last modified timestamp when a product post cache is cleaned. |
| 173 | * |
| 174 | * This does not use wp_cache_set_last_changed() because the cached value is exposed to |
| 175 | * clients via the Last-Modified HTTP header for collection cache invalidation. WordPress |
| 176 | * core's last_changed pattern auto-seeds with the current time on cache miss, which is |
| 177 | * acceptable for opaque cache-key salts but would force all clients to unnecessarily |
| 178 | * invalidate their local caches. Instead, invalidating the cache here allows the read side |
| 179 | * in ProductQuery::get_last_modified() to fall back to the DB and re-seed with the real |
| 180 | * last modification time. |
| 181 | * |
| 182 | * @since 10.6.0 |
| 183 | * |
| 184 | * @param int $post_id Post ID. |
| 185 | * @param WP_Post $post Post object. |
| 186 | */ |
| 187 | public static function invalidate_products_last_modified( $post_id, $post ): void { |
| 188 | if ( $post instanceof WP_Post && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) { |
| 189 | wp_cache_delete( 'last_modified', 'wc_products' ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Invalidates cache entries related to fetching block templates from DB. Please reference to |
| 195 | * `Utils\BlockTemplateUtils::get_block_templates_from_db` for further details. |
| 196 | * |
| 197 | * @param int $post_id Post ID. |
| 198 | * @param \WP_Post $post Post object. |
| 199 | * |
| 200 | * @internal |
| 201 | * @since 10.7.0 |
| 202 | * |
| 203 | * @return void |
| 204 | */ |
| 205 | public static function invalidate_db_block_templates_cache( $post_id, $post ): void { |
| 206 | if ( $post instanceof \WP_Post && in_array( $post->post_type, array( 'wp_template_part', 'wp_template' ), true ) ) { |
| 207 | wp_cache_delete( $post->post_type . '-ids', 'woocommerce_blocks' ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Handle type changes. |
| 213 | * |
| 214 | * @since 3.0.0 |
| 215 | * |
| 216 | * @param WC_Product $product Product data. |
| 217 | * @param string $from Origin type. |
| 218 | * @param string $to New type. |
| 219 | * |
| 220 | * @return void |
| 221 | */ |
| 222 | public static function product_type_changed( $product, $from, $to ) { |
| 223 | /** |
| 224 | * Filter to prevent variations from being deleted while switching from a variable product type to a variable product type. |
| 225 | * |
| 226 | * @since 5.0.0 |
| 227 | * |
| 228 | * @param bool A boolean value of true will delete the variations. |
| 229 | * @param WC_Product $product Product data. |
| 230 | * @return string $from Origin type. |
| 231 | * @param string $to New type. |
| 232 | */ |
| 233 | if ( apply_filters( 'woocommerce_delete_variations_on_product_type_change', ProductType::VARIABLE === $from && ProductType::VARIABLE !== $to, $product, $from, $to ) ) { |
| 234 | // If the product is no longer variable, we should ensure all variations are removed. |
| 235 | $data_store = WC_Data_Store::load( 'product-variable' ); |
| 236 | $data_store->delete_variations( $product->get_id(), true ); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * When editing a term, check for product attributes. |
| 242 | * |
| 243 | * @param int $term_id Term ID. |
| 244 | * @param int $tt_id Term taxonomy ID. |
| 245 | * @param string $taxonomy Taxonomy slug. |
| 246 | * |
| 247 | * @return void |
| 248 | */ |
| 249 | public static function edit_term( $term_id, $tt_id, $taxonomy ) { |
| 250 | if ( strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 251 | self::$editing_term = get_term_by( 'id', $term_id, $taxonomy ); |
| 252 | } else { |
| 253 | self::$editing_term = null; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * When a term is edited, check for product attributes and update variations. |
| 259 | * |
| 260 | * @param int $term_id Term ID. |
| 261 | * @param int $tt_id Term taxonomy ID. |
| 262 | * @param string $taxonomy Taxonomy slug. |
| 263 | * |
| 264 | * @return void |
| 265 | */ |
| 266 | public static function edited_term( $term_id, $tt_id, $taxonomy ) { |
| 267 | if ( ! is_null( self::$editing_term ) && strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 268 | $edited_term = get_term_by( 'id', $term_id, $taxonomy ); |
| 269 | |
| 270 | if ( $edited_term->slug !== self::$editing_term->slug ) { |
| 271 | global $wpdb; |
| 272 | |
| 273 | $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND meta_value = %s;", $edited_term->slug, 'attribute_' . sanitize_title( $taxonomy ), self::$editing_term->slug ) ); |
| 274 | |
| 275 | $wpdb->query( |
| 276 | $wpdb->prepare( |
| 277 | "UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, %s, %s ) WHERE meta_key = '_default_attributes'", |
| 278 | serialize( self::$editing_term->taxonomy ) . serialize( self::$editing_term->slug ), |
| 279 | serialize( $edited_term->taxonomy ) . serialize( $edited_term->slug ) |
| 280 | ) |
| 281 | ); |
| 282 | } |
| 283 | } else { |
| 284 | self::$editing_term = null; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Ensure floats are correctly converted to strings based on PHP locale. |
| 290 | * |
| 291 | * @param null $check Whether to allow updating metadata for the given type. |
| 292 | * @param int $object_id Object ID. |
| 293 | * @param string $meta_key Meta key. |
| 294 | * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
| 295 | * @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries. |
| 296 | * @return null|bool |
| 297 | */ |
| 298 | public static function update_order_item_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
| 299 | if ( ! empty( $meta_value ) && is_float( $meta_value ) ) { |
| 300 | |
| 301 | // Convert float to string. |
| 302 | $meta_value = wc_float_to_string( $meta_value ); |
| 303 | |
| 304 | // Update meta value with new string. |
| 305 | update_metadata( 'order_item', $object_id, $meta_key, $meta_value, $prev_value ); |
| 306 | |
| 307 | return true; |
| 308 | } |
| 309 | return $check; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Ensure floats are correctly converted to strings based on PHP locale. |
| 314 | * |
| 315 | * @param null $check Whether to allow updating metadata for the given type. |
| 316 | * @param int $object_id Object ID. |
| 317 | * @param string $meta_key Meta key. |
| 318 | * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
| 319 | * @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries. |
| 320 | * @return null|bool |
| 321 | */ |
| 322 | public static function update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
| 323 | // Delete product cache if someone uses meta directly. |
| 324 | if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) { |
| 325 | wp_cache_delete( 'product-' . $object_id, 'products' ); |
| 326 | } |
| 327 | |
| 328 | if ( ! empty( $meta_value ) && is_float( $meta_value ) && ! registered_meta_key_exists( 'post', $meta_key ) && in_array( get_post_type( $object_id ), array_merge( wc_get_order_types(), array( 'shop_coupon', 'product', 'product_variation' ) ), true ) ) { |
| 329 | |
| 330 | // Convert float to string. |
| 331 | $meta_value = wc_float_to_string( $meta_value ); |
| 332 | |
| 333 | // Update meta value with new string. |
| 334 | update_metadata( 'post', $object_id, $meta_key, $meta_value, $prev_value ); |
| 335 | |
| 336 | return true; |
| 337 | } |
| 338 | return $check; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Forces the order posts to have a title in a certain format (containing the date). |
| 343 | * Forces certain product data based on the product's type, e.g. grouped products cannot have a parent. |
| 344 | * |
| 345 | * @param array $data An array of slashed post data. |
| 346 | * @return array |
| 347 | */ |
| 348 | public static function wp_insert_post_data( $data ) { |
| 349 | if ( 'shop_order' === $data['post_type'] && isset( $data['post_date'] ) ) { |
| 350 | $order_title = 'Order'; |
| 351 | if ( $data['post_date'] ) { |
| 352 | $order_title .= ' – ' . date_i18n( 'F j, Y @ h:i A', strtotime( $data['post_date'] ) ); |
| 353 | } |
| 354 | $data['post_title'] = $order_title; |
| 355 | } elseif ( 'product' === $data['post_type'] && isset( $_POST['product-type'] ) ) { // WPCS: input var ok, CSRF ok. |
| 356 | $product_type = wc_clean( wp_unslash( $_POST['product-type'] ) ); // WPCS: input var ok, CSRF ok. |
| 357 | switch ( $product_type ) { |
| 358 | case ProductType::GROUPED: |
| 359 | case ProductType::VARIABLE: |
| 360 | $data['post_parent'] = 0; |
| 361 | break; |
| 362 | } |
| 363 | } elseif ( 'product' === $data['post_type'] && ProductStatus::AUTO_DRAFT === $data['post_status'] ) { |
| 364 | $data['post_title'] = 'AUTO-DRAFT'; |
| 365 | } elseif ( 'shop_coupon' === $data['post_type'] ) { |
| 366 | // Coupons should never allow unfiltered HTML. |
| 367 | $data['post_title'] = wp_filter_kses( $data['post_title'] ); |
| 368 | } |
| 369 | |
| 370 | return $data; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Change embed data for certain post types. |
| 375 | * |
| 376 | * @since 3.2.0 |
| 377 | * @param array $data The response data. |
| 378 | * @param WP_Post $post The post object. |
| 379 | * @return array |
| 380 | */ |
| 381 | public static function filter_oembed_response_data( $data, $post ) { |
| 382 | if ( in_array( $post->post_type, array( 'shop_order', 'shop_coupon' ), true ) ) { |
| 383 | return array(); |
| 384 | } |
| 385 | return $data; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Removes variations etc. belonging to a deleted post, and clears transients. |
| 390 | * |
| 391 | * @internal Use the delete_post function instead. |
| 392 | * @since 9.8.0 |
| 393 | * |
| 394 | * @param mixed $id ID of post being deleted. |
| 395 | * |
| 396 | * @return void |
| 397 | */ |
| 398 | public static function delete_post_data( $id ) { |
| 399 | $container = wc_get_container(); |
| 400 | |
| 401 | $post_type = self::get_post_type( $id ); |
| 402 | switch ( $post_type ) { |
| 403 | case 'product': |
| 404 | $data_store = WC_Data_Store::load( 'product-variable' ); |
| 405 | $data_store->delete_variations( $id, true ); |
| 406 | $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' ); |
| 407 | $container->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id ); |
| 408 | |
| 409 | $parent_id = wp_get_post_parent_id( $id ); |
| 410 | if ( $parent_id ) { |
| 411 | wc_delete_product_transients( $parent_id ); |
| 412 | } |
| 413 | |
| 414 | break; |
| 415 | case 'product_variation': |
| 416 | $data_store = WC_Data_Store::load( 'product' ); |
| 417 | $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' ); |
| 418 | wc_delete_product_transients( wp_get_post_parent_id( $id ) ); |
| 419 | $container->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id ); |
| 420 | |
| 421 | break; |
| 422 | case 'shop_order': |
| 423 | case DataSynchronizer::PLACEHOLDER_ORDER_POST_TYPE: |
| 424 | global $wpdb; |
| 425 | |
| 426 | $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); |
| 427 | |
| 428 | if ( ! is_null( $refunds ) ) { |
| 429 | foreach ( $refunds as $refund ) { |
| 430 | wp_delete_post( $refund->ID, true ); |
| 431 | } |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Removes variations etc. belonging to a deleted post, and clears transients, if the user has permission. |
| 439 | * |
| 440 | * @param mixed $id ID of post being deleted. |
| 441 | * |
| 442 | * @return void |
| 443 | */ |
| 444 | public static function delete_post( $id ) { |
| 445 | $container = wc_get_container(); |
| 446 | if ( ! $container->get( LegacyProxy::class )->call_function( 'current_user_can', 'delete_posts' ) || ! $id ) { |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | self::delete_post_data( $id ); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Trash post. |
| 455 | * |
| 456 | * @param mixed $id Post ID. |
| 457 | * |
| 458 | * @return void |
| 459 | */ |
| 460 | public static function trash_post( $id ) { |
| 461 | if ( ! $id ) { |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | $post_type = self::get_post_type( $id ); |
| 466 | |
| 467 | // If this is an order, trash any refunds too. |
| 468 | if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) { |
| 469 | global $wpdb; |
| 470 | |
| 471 | $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); |
| 472 | |
| 473 | foreach ( $refunds as $refund ) { |
| 474 | $wpdb->update( $wpdb->posts, array( 'post_status' => OrderStatus::TRASH ), array( 'ID' => $refund->ID ) ); |
| 475 | } |
| 476 | |
| 477 | wc_delete_shop_order_transients( $id ); |
| 478 | |
| 479 | // If this is a product, trash children variations. |
| 480 | } elseif ( 'product' === $post_type ) { |
| 481 | $data_store = WC_Data_Store::load( 'product-variable' ); |
| 482 | $data_store->delete_variations( $id, false ); |
| 483 | wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id ); |
| 484 | } elseif ( 'product_variation' === $post_type ) { |
| 485 | wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id ); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Untrash post. |
| 491 | * |
| 492 | * @param mixed $id Post ID. |
| 493 | * |
| 494 | * @return void |
| 495 | */ |
| 496 | public static function untrash_post( $id ) { |
| 497 | if ( ! $id ) { |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | $post_type = self::get_post_type( $id ); |
| 502 | |
| 503 | if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) { |
| 504 | global $wpdb; |
| 505 | |
| 506 | $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); |
| 507 | |
| 508 | foreach ( $refunds as $refund ) { |
| 509 | $wpdb->update( $wpdb->posts, array( 'post_status' => OrderInternalStatus::COMPLETED ), array( 'ID' => $refund->ID ) ); |
| 510 | } |
| 511 | |
| 512 | wc_delete_shop_order_transients( $id ); |
| 513 | |
| 514 | } elseif ( 'product' === $post_type ) { |
| 515 | $data_store = WC_Data_Store::load( 'product-variable' ); |
| 516 | $data_store->untrash_variations( $id ); |
| 517 | |
| 518 | wc_product_force_unique_sku( $id ); |
| 519 | self::clear_global_unique_id_if_necessary( $id ); |
| 520 | |
| 521 | wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id ); |
| 522 | } elseif ( 'product_variation' === $post_type ) { |
| 523 | wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id ); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Clear global unique id if it's not unique. |
| 529 | * |
| 530 | * @param mixed $id Post ID. |
| 531 | * |
| 532 | * @return void |
| 533 | */ |
| 534 | private static function clear_global_unique_id_if_necessary( $id ) { |
| 535 | $product = wc_get_product( $id ); |
| 536 | if ( $product && ! wc_product_has_global_unique_id( $id, $product->get_global_unique_id() ) ) { |
| 537 | $product->set_global_unique_id( '' ); |
| 538 | $product->save(); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Get the post type for a given post. |
| 544 | * |
| 545 | * @param int $id The post id. |
| 546 | * @return string The post type. |
| 547 | */ |
| 548 | private static function get_post_type( $id ) { |
| 549 | return wc_get_container()->get( LegacyProxy::class )->call_function( 'get_post_type', $id ); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Before deleting an order, do some cleanup. |
| 554 | * |
| 555 | * @since 3.2.0 |
| 556 | * @param int $order_id Order ID. |
| 557 | * |
| 558 | * @return void |
| 559 | */ |
| 560 | public static function before_delete_order( $order_id ) { |
| 561 | if ( OrderUtil::is_order( $order_id, wc_get_order_types() ) ) { |
| 562 | // Clean up user. |
| 563 | $order = wc_get_order( $order_id ); |
| 564 | |
| 565 | // Check for `get_customer_id`, since this may be e.g. a refund order (which doesn't implement it). |
| 566 | $customer_id = is_callable( array( $order, 'get_customer_id' ) ) ? $order->get_customer_id() : 0; |
| 567 | |
| 568 | if ( $customer_id > 0 && 'shop_order' === $order->get_type() ) { |
| 569 | $customer = new WC_Customer( $customer_id ); |
| 570 | $order_count = $customer->get_order_count(); |
| 571 | $order_count --; |
| 572 | |
| 573 | if ( 0 === $order_count ) { |
| 574 | $customer->set_is_paying_customer( false ); |
| 575 | $customer->save(); |
| 576 | } |
| 577 | |
| 578 | // Delete order count and last order meta. |
| 579 | delete_user_meta( $customer_id, '_order_count' ); |
| 580 | delete_user_meta( $customer_id, '_last_order' ); |
| 581 | } |
| 582 | |
| 583 | // Clean up items. |
| 584 | self::delete_order_items( $order_id ); |
| 585 | self::delete_order_downloadable_permissions( $order_id ); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Remove item meta on permanent deletion. |
| 591 | * |
| 592 | * @param int $postid Post ID. |
| 593 | * |
| 594 | * @return void |
| 595 | */ |
| 596 | public static function delete_order_items( $postid ) { |
| 597 | global $wpdb; |
| 598 | |
| 599 | if ( OrderUtil::is_order( $postid, wc_get_order_types() ) ) { |
| 600 | do_action( 'woocommerce_delete_order_items', $postid ); |
| 601 | |
| 602 | $wpdb->query( |
| 603 | " |
| 604 | DELETE {$wpdb->prefix}woocommerce_order_items, {$wpdb->prefix}woocommerce_order_itemmeta |
| 605 | FROM {$wpdb->prefix}woocommerce_order_items |
| 606 | JOIN {$wpdb->prefix}woocommerce_order_itemmeta ON {$wpdb->prefix}woocommerce_order_items.order_item_id = {$wpdb->prefix}woocommerce_order_itemmeta.order_item_id |
| 607 | WHERE {$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}'; |
| 608 | " |
| 609 | ); // WPCS: unprepared SQL ok. |
| 610 | |
| 611 | do_action( 'woocommerce_deleted_order_items', $postid ); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Remove downloadable permissions on permanent order deletion. |
| 617 | * |
| 618 | * @param int $postid Post ID. |
| 619 | * |
| 620 | * @return void |
| 621 | */ |
| 622 | public static function delete_order_downloadable_permissions( $postid ) { |
| 623 | if ( OrderUtil::is_order( $postid, wc_get_order_types() ) ) { |
| 624 | do_action( 'woocommerce_delete_order_downloadable_permissions', $postid ); |
| 625 | |
| 626 | $data_store = WC_Data_Store::load( 'customer-download' ); |
| 627 | $data_store->delete_by_order_id( $postid ); |
| 628 | |
| 629 | do_action( 'woocommerce_deleted_order_downloadable_permissions', $postid ); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Flush meta cache for CRUD objects on direct update. |
| 635 | * |
| 636 | * @param int $meta_id Meta ID. |
| 637 | * @param int $object_id Object ID. |
| 638 | * @param string $meta_key Meta key. |
| 639 | * @param mixed $meta_value Meta value. |
| 640 | * |
| 641 | * @return void |
| 642 | */ |
| 643 | public static function flush_object_meta_cache( $meta_id, $object_id, $meta_key, $meta_value ) { |
| 644 | WC_Cache_Helper::invalidate_cache_group( 'object_' . $object_id ); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Ensure default category gets set. |
| 649 | * |
| 650 | * @since 3.3.0 |
| 651 | * @param int $object_id Product ID. |
| 652 | * @param array $terms Terms array. |
| 653 | * @param array $tt_ids Term ids array. |
| 654 | * @param string $taxonomy Taxonomy name. |
| 655 | * @param bool $append Are we appending or setting terms. |
| 656 | * |
| 657 | * @return void |
| 658 | */ |
| 659 | public static function force_default_term( $object_id, $terms, $tt_ids, $taxonomy, $append ) { |
| 660 | if ( ! $append && 'product_cat' === $taxonomy && empty( $tt_ids ) && 'product' === get_post_type( $object_id ) ) { |
| 661 | $default_term = absint( get_option( 'default_product_cat', 0 ) ); |
| 662 | $tt_ids = array_map( 'absint', $tt_ids ); |
| 663 | |
| 664 | if ( $default_term && ! in_array( $default_term, $tt_ids, true ) ) { |
| 665 | wp_set_post_terms( $object_id, array( $default_term ), 'product_cat', true ); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Recounts product terms when product visibility changes affect catalog display. |
| 672 | * |
| 673 | * @param int $object_id The object ID. |
| 674 | * @param array $terms An array of object terms. |
| 675 | * @param array $tt_ids An array of term taxonomy IDs. |
| 676 | * @param string $taxonomy Taxonomy slug. |
| 677 | * @param bool $append Whether to append new terms to the old terms. |
| 678 | * @param array $old_tt_ids The old array of term taxonomy IDs. |
| 679 | * |
| 680 | * @since 10.4.0 |
| 681 | * |
| 682 | * @return void |
| 683 | */ |
| 684 | public static function recount_terms_for_product_visibility_change( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { |
| 685 | if ( 'product_visibility' !== $taxonomy ) { |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | if ( $append ) { |
| 690 | $modified_tt_ids = $tt_ids; |
| 691 | } else { |
| 692 | $modified_tt_ids = array_merge( array_diff( $tt_ids, $old_tt_ids ), array_diff( $old_tt_ids, $tt_ids ) ); |
| 693 | } |
| 694 | |
| 695 | if ( empty( $modified_tt_ids ) ) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | // Despite the name, wc_get_product_visibility_term_ids() actually returns an associative array with term_taxonomy_ids. |
| 700 | $visibility_tt_ids = wc_get_product_visibility_term_ids(); |
| 701 | |
| 702 | $tt_ids_modifying_term_counts = array(); |
| 703 | if ( ! empty( $visibility_tt_ids['exclude-from-catalog'] ) ) { |
| 704 | $tt_ids_modifying_term_counts[] = $visibility_tt_ids['exclude-from-catalog']; |
| 705 | } |
| 706 | |
| 707 | if ( ! empty( $visibility_tt_ids['outofstock'] ) && 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 708 | $tt_ids_modifying_term_counts[] = $visibility_tt_ids['outofstock']; |
| 709 | } |
| 710 | |
| 711 | if ( ! empty( array_intersect( $modified_tt_ids, $tt_ids_modifying_term_counts ) ) ) { |
| 712 | _wc_recount_terms_by_product( $object_id ); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Ensure statuses are correctly reassigned when restoring orders and products. |
| 718 | * |
| 719 | * @param string $new_status The new status of the post being restored. |
| 720 | * @param int $post_id The ID of the post being restored. |
| 721 | * @param string $previous_status The status of the post at the point where it was trashed. |
| 722 | * @return string |
| 723 | */ |
| 724 | public static function wp_untrash_post_status( $new_status, $post_id, $previous_status ) { |
| 725 | $post_types = array( 'shop_order', 'shop_coupon', 'product', 'product_variation' ); |
| 726 | |
| 727 | if ( in_array( get_post_type( $post_id ), $post_types, true ) ) { |
| 728 | $new_status = $previous_status; |
| 729 | } |
| 730 | |
| 731 | return $new_status; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * When setting stock level, ensure the stock status is kept in sync. |
| 736 | * |
| 737 | * @param int $meta_id Meta ID. |
| 738 | * @param int $object_id Object ID. |
| 739 | * @param string $meta_key Meta key. |
| 740 | * @param mixed $meta_value Meta value. |
| 741 | * @deprecated 3.3 |
| 742 | * |
| 743 | * @return void |
| 744 | */ |
| 745 | public static function sync_product_stock_status( $meta_id, $object_id, $meta_key, $meta_value ) {} |
| 746 | |
| 747 | /** |
| 748 | * Update changed downloads. |
| 749 | * |
| 750 | * @deprecated 3.3.0 No action is necessary on changes to download paths since download_id is no longer based on file hash. |
| 751 | * @param int $product_id Product ID. |
| 752 | * @param int $variation_id Variation ID. Optional product variation identifier. |
| 753 | * @param array $downloads Newly set files. |
| 754 | * |
| 755 | * @return void |
| 756 | */ |
| 757 | public static function process_product_file_download_paths( $product_id, $variation_id, $downloads ) { |
| 758 | wc_deprecated_function( __FUNCTION__, '3.3' ); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Delete transients when terms are set. |
| 763 | * |
| 764 | * @deprecated 3.6 |
| 765 | * @param int $object_id Object ID. |
| 766 | * @param mixed $terms An array of object terms. |
| 767 | * @param array $tt_ids An array of term taxonomy IDs. |
| 768 | * @param string $taxonomy Taxonomy slug. |
| 769 | * @param mixed $append Whether to append new terms to the old terms. |
| 770 | * @param array $old_tt_ids Old array of term taxonomy IDs. |
| 771 | * |
| 772 | * @return void |
| 773 | */ |
| 774 | public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { |
| 775 | if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) { |
| 776 | self::delete_product_query_transients(); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Regenerates attribute summaries for a list of variations. |
| 782 | * |
| 783 | * @since 10.2.0 |
| 784 | * @param array $variation_ids Array of variation IDs. |
| 785 | * |
| 786 | * @return void |
| 787 | */ |
| 788 | private static function regenerate_variation_summaries( $variation_ids ) { |
| 789 | if ( empty( $variation_ids ) ) { |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | $variation_ids = array_unique( array_filter( array_map( 'intval', $variation_ids ) ) ); |
| 794 | |
| 795 | foreach ( $variation_ids as $variation_id ) { |
| 796 | self::regenerate_variation_attribute_summary( $variation_id ); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Regenerates the attribute summary for a single variation. |
| 802 | * |
| 803 | * @since 10.2.0 |
| 804 | * @param int $variation_id Variation ID. |
| 805 | * |
| 806 | * @return void |
| 807 | */ |
| 808 | public static function regenerate_variation_attribute_summary( $variation_id ) { |
| 809 | global $wpdb; |
| 810 | |
| 811 | $product = wc_get_product( $variation_id ); |
| 812 | if ( ! $product || ! $product->is_type( 'variation' ) ) { |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | $data_store = WC_Data_Store::load( 'product-variation' ); |
| 817 | if ( $data_store->has_callable( 'get_attribute_summary' ) ) { |
| 818 | $new_summary = $data_store->get_attribute_summary( $product ); |
| 819 | $current_excerpt = get_post_field( 'post_excerpt', $variation_id ); |
| 820 | if ( $new_summary === $current_excerpt ) { |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Update directly via $wpdb for performance: Avoid firing save_post hooks, loading full post objects, |
| 826 | * and creating revisions. This is safe here as we're only updating post_excerpt. |
| 827 | */ |
| 828 | $wpdb->update( |
| 829 | $wpdb->posts, |
| 830 | array( 'post_excerpt' => $new_summary ), |
| 831 | array( 'ID' => $variation_id ) |
| 832 | ); |
| 833 | clean_post_cache( $variation_id ); |
| 834 | /** |
| 835 | * Fires after the attribute summary of a product variation has been updated. |
| 836 | * |
| 837 | * @since 10.2.0 |
| 838 | * @param int $variation_id The ID of the product variation. |
| 839 | */ |
| 840 | do_action( 'woocommerce_updated_product_attribute_summary', $variation_id ); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * Gets the threshold for synchronous regeneration of variation summaries. |
| 846 | * |
| 847 | * @since 10.2.0 |
| 848 | * @return int |
| 849 | */ |
| 850 | public static function get_variation_summaries_sync_threshold() { |
| 851 | /** |
| 852 | * Filters the threshold for synchronous regeneration of variation attribute summaries. |
| 853 | * If the number of variations affected by an update is below this threshold, the summaries |
| 854 | * are regenerated synchronously. Otherwise, the regeneration is scheduled asynchronously. |
| 855 | * |
| 856 | * @since 10.2.0 |
| 857 | * @param int $threshold The default threshold value (50). |
| 858 | * @return int The filtered threshold value. |
| 859 | */ |
| 860 | return absint( apply_filters( 'woocommerce_regenerate_variation_summaries_sync_threshold', 50 ) ); |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Handles updates to a global attribute by triggering variation summary regeneration. |
| 865 | * |
| 866 | * @since 10.2.0 |
| 867 | * @param int $attribute_id Attribute ID. |
| 868 | * @param string $attribute Attribute name. |
| 869 | * @param string $old_slug Old attribute slug. |
| 870 | * |
| 871 | * @return void |
| 872 | */ |
| 873 | public static function handle_global_attribute_updated( $attribute_id, $attribute, $old_slug ) { |
| 874 | // We use this trigger for both updates and deletions of global attributes. |
| 875 | // They pass different parameters to $old_slug - deleted attributes include the "pa_" prefix, while updated attributes do not. |
| 876 | // Remove it if existing for consistency. |
| 877 | if ( strpos( $old_slug, 'pa_' ) === 0 ) { |
| 878 | $old_slug = substr( $old_slug, 3 ); |
| 879 | } |
| 880 | $taxonomy = 'pa_' . $old_slug; |
| 881 | $threshold = self::get_variation_summaries_sync_threshold(); |
| 882 | // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 883 | $args = array( |
| 884 | 'post_type' => 'product_variation', |
| 885 | 'post_status' => 'any', |
| 886 | 'posts_per_page' => $threshold + 1, |
| 887 | 'fields' => 'ids', |
| 888 | 'meta_query' => array( |
| 889 | array( |
| 890 | 'key' => 'attribute_' . $taxonomy, |
| 891 | 'compare' => 'EXISTS', |
| 892 | ), |
| 893 | ), |
| 894 | ); |
| 895 | // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 896 | |
| 897 | $variation_ids = get_posts( $args ); |
| 898 | |
| 899 | if ( empty( $variation_ids ) ) { |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | if ( count( $variation_ids ) <= $threshold ) { |
| 904 | // Update variation summaries that used this product attribute, but |
| 905 | // wait until shutdown. This will allow WooC to carry out post_meta migrations |
| 906 | // if the slug of the attribute changed. |
| 907 | add_action( |
| 908 | 'shutdown', |
| 909 | function () use ( $variation_ids ) { |
| 910 | self::regenerate_variation_summaries( $variation_ids ); |
| 911 | } |
| 912 | ); |
| 913 | } else { |
| 914 | $new_slug = ! empty( $attribute['attribute_name'] ) ? $attribute['attribute_name'] : $old_slug; |
| 915 | $new_taxonomy = 'pa_' . $new_slug; |
| 916 | |
| 917 | self::schedule_variation_summary_regeneration( |
| 918 | 'wc_regenerate_attribute_variation_summaries', |
| 919 | array( $new_taxonomy ), |
| 920 | 'Taxonomy: ' . $taxonomy . ', Attribute ID: ' . $attribute_id |
| 921 | ); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * Regenerates variation summaries for all variations using a specific attribute taxonomy. |
| 927 | * |
| 928 | * @since 10.2.0 |
| 929 | * @param string $taxonomy Attribute taxonomy. |
| 930 | * |
| 931 | * @return void |
| 932 | */ |
| 933 | public static function regenerate_attribute_variation_summaries( $taxonomy ) { |
| 934 | // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 935 | $variation_ids = get_posts( |
| 936 | array( |
| 937 | 'post_type' => 'product_variation', |
| 938 | 'numberposts' => -1, |
| 939 | 'fields' => 'ids', |
| 940 | 'meta_query' => array( |
| 941 | array( |
| 942 | 'key' => 'attribute_' . $taxonomy, |
| 943 | 'compare' => 'EXISTS', |
| 944 | ), |
| 945 | ), |
| 946 | ) |
| 947 | ); |
| 948 | // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 949 | self::regenerate_variation_summaries( $variation_ids ); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Handles regeneration of variation summaries when a variable product's attributes are updated. |
| 954 | * |
| 955 | * @since 10.2.0 |
| 956 | * @param WC_Product $product The variable product whose attributes were updated. |
| 957 | * |
| 958 | * @return void |
| 959 | */ |
| 960 | public static function on_product_attributes_updated( $product ) { |
| 961 | if ( $product->is_type( 'variable' ) ) { |
| 962 | global $wpdb; |
| 963 | $threshold = self::get_variation_summaries_sync_threshold(); |
| 964 | $variation_ids = $wpdb->get_col( |
| 965 | $wpdb->prepare( |
| 966 | "SELECT DISTINCT ID |
| 967 | FROM {$wpdb->posts} |
| 968 | WHERE post_parent = %d |
| 969 | AND post_type = %s |
| 970 | LIMIT %d |
| 971 | ", |
| 972 | $product->get_id(), |
| 973 | 'product_variation', |
| 974 | $threshold + 1 |
| 975 | ) |
| 976 | ); |
| 977 | |
| 978 | if ( empty( $variation_ids ) ) { |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | if ( count( $variation_ids ) <= $threshold ) { |
| 983 | // If the number of variations is below the threshold, regenerate summaries synchronously. |
| 984 | $variation_ids = $product->get_children(); |
| 985 | self::regenerate_variation_summaries( $variation_ids ); |
| 986 | } else { |
| 987 | self::schedule_variation_summary_regeneration( |
| 988 | 'wc_regenerate_product_variation_summaries', |
| 989 | array( $product->get_id() ), |
| 990 | 'Product ID: ' . $product->get_id() |
| 991 | ); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * Regenerates variation summaries for all variations of a variable product. |
| 998 | * |
| 999 | * @since 10.2.0 |
| 1000 | * @param int $product_id Variable product ID. |
| 1001 | * |
| 1002 | * @return void |
| 1003 | */ |
| 1004 | public static function regenerate_product_variation_summaries( $product_id ) { |
| 1005 | $product = wc_get_product( $product_id ); |
| 1006 | if ( ! $product || ! $product->is_type( 'variable' ) ) { |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | $variation_ids = $product->get_children(); |
| 1011 | self::regenerate_variation_summaries( $variation_ids ); |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * Hook called after a term is updated to handle updates for product variations. |
| 1016 | * |
| 1017 | * @param int $term_id Term ID. |
| 1018 | * @param int $tt_id Term taxonomy ID. |
| 1019 | * @param string $taxonomy Taxonomy slug. |
| 1020 | * |
| 1021 | * @return void |
| 1022 | */ |
| 1023 | public static function handle_attribute_term_updated( $term_id, $tt_id, $taxonomy ) { |
| 1024 | if ( strpos( $taxonomy, 'pa_' ) !== 0 ) { |
| 1025 | return; |
| 1026 | } |
| 1027 | |
| 1028 | $new_term = get_term( $term_id, $taxonomy ); |
| 1029 | if ( is_wp_error( $new_term ) || ! $new_term ) { |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | $meta_key = 'attribute_' . $taxonomy; |
| 1034 | global $wpdb; |
| 1035 | |
| 1036 | $threshold = self::get_variation_summaries_sync_threshold(); |
| 1037 | $variation_ids = $wpdb->get_col( |
| 1038 | $wpdb->prepare( |
| 1039 | "SELECT DISTINCT pm.post_id |
| 1040 | FROM $wpdb->postmeta pm |
| 1041 | INNER JOIN $wpdb->posts p ON pm.post_id = p.ID |
| 1042 | WHERE pm.meta_key = %s |
| 1043 | AND pm.meta_value = %s |
| 1044 | AND p.post_type = 'product_variation' |
| 1045 | LIMIT %d |
| 1046 | ", |
| 1047 | $meta_key, |
| 1048 | $new_term->slug, |
| 1049 | $threshold + 1 |
| 1050 | ) |
| 1051 | ); |
| 1052 | if ( empty( $variation_ids ) ) { |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | if ( count( $variation_ids ) <= $threshold ) { |
| 1057 | // If the number of variations is below the threshold, regenerate summaries synchronously. |
| 1058 | self::regenerate_variation_summaries( $variation_ids ); |
| 1059 | } else { |
| 1060 | self::schedule_variation_summary_regeneration( |
| 1061 | 'wc_regenerate_term_variation_summaries', |
| 1062 | array( $taxonomy, $new_term->slug ), |
| 1063 | 'Taxonomy: ' . $taxonomy . ', Term ID: ' . $term_id |
| 1064 | ); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * Hook called after a term is deleted to handle updates for product variations. |
| 1070 | * |
| 1071 | * @param int $term_id Term ID. |
| 1072 | * @param int $tt_id Term taxonomy ID. |
| 1073 | * @param string $taxonomy Taxonomy slug. |
| 1074 | * @param WP_Term $deleted_term Copy of the already-deleted term. |
| 1075 | * |
| 1076 | * @return void |
| 1077 | */ |
| 1078 | public static function handle_attribute_term_deleted( $term_id, $tt_id, $taxonomy, $deleted_term ) { |
| 1079 | if ( strpos( $taxonomy, 'pa_' ) !== 0 ) { |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | $meta_key = 'attribute_' . $taxonomy; |
| 1084 | global $wpdb; |
| 1085 | $threshold = self::get_variation_summaries_sync_threshold(); |
| 1086 | $variation_ids = $wpdb->get_col( |
| 1087 | $wpdb->prepare( |
| 1088 | "SELECT DISTINCT pm.post_id |
| 1089 | FROM $wpdb->postmeta pm |
| 1090 | INNER JOIN $wpdb->posts p ON pm.post_id = p.ID |
| 1091 | WHERE pm.meta_key = %s |
| 1092 | AND pm.meta_value = %s |
| 1093 | AND p.post_type = 'product_variation' |
| 1094 | LIMIT %d |
| 1095 | ", |
| 1096 | $meta_key, |
| 1097 | $deleted_term->slug, |
| 1098 | $threshold + 1 |
| 1099 | ) |
| 1100 | ); |
| 1101 | |
| 1102 | if ( empty( $variation_ids ) ) { |
| 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | if ( count( $variation_ids ) <= $threshold ) { |
| 1107 | // If the number of variations is below the threshold, regenerate summaries synchronously. |
| 1108 | self::regenerate_variation_summaries( $variation_ids ); |
| 1109 | } else { |
| 1110 | self::schedule_variation_summary_regeneration( |
| 1111 | 'wc_regenerate_term_variation_summaries', |
| 1112 | array( $taxonomy, $deleted_term->slug ), |
| 1113 | 'Taxonomy: ' . $taxonomy . ', Term ID: ' . $term_id |
| 1114 | ); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Schedule an asynchronous action to regenerate product variation summaries. |
| 1120 | * |
| 1121 | * This method uses the WooCommerce Action Scheduler to queue a single regeneration action |
| 1122 | * for product variation summaries. It first checks whether an identical action with the |
| 1123 | * given arguments is already scheduled to avoid duplicate jobs. If the Action Scheduler |
| 1124 | * is not available, a warning is logged instead. |
| 1125 | * |
| 1126 | * @param string $action_name The name/identifier of the scheduled action (hook name). |
| 1127 | * @param array $args Arguments to pass to the scheduled action callback. |
| 1128 | * @param string $warning_message Message to log when the Action Scheduler is unavailable. |
| 1129 | * @param string $group Optional. The Action Scheduler group to associate with |
| 1130 | * the scheduled action. Default 'woocommerce'. |
| 1131 | * |
| 1132 | * @return void |
| 1133 | */ |
| 1134 | private static function schedule_variation_summary_regeneration( $action_name, $args, $warning_message, $group = 'woocommerce' ) { |
| 1135 | if ( function_exists( 'as_schedule_single_action' ) ) { |
| 1136 | // Prevent duplicate scheduling of the action. |
| 1137 | $when = as_next_scheduled_action( $action_name, $args, $group ); |
| 1138 | if ( ! $when ) { |
| 1139 | as_schedule_single_action( time() + 1, $action_name, $args, $group ); |
| 1140 | } |
| 1141 | } else { |
| 1142 | wc_get_logger()->warning( |
| 1143 | 'Action Scheduler unavailable for product variation summary regeneration. ' . $warning_message, |
| 1144 | array( 'source' => 'woocommerce-variations' ) |
| 1145 | ); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * Regenerates variation summaries for all variations using a specific term. |
| 1151 | * |
| 1152 | * @since 10.2.0 |
| 1153 | * @param string $taxonomy Taxonomy slug. |
| 1154 | * @param string $term_slug Term slug. |
| 1155 | * |
| 1156 | * @return void |
| 1157 | */ |
| 1158 | public static function regenerate_term_variation_summaries( $taxonomy, $term_slug ) { |
| 1159 | global $wpdb; |
| 1160 | |
| 1161 | $variation_ids = $wpdb->get_col( |
| 1162 | $wpdb->prepare( |
| 1163 | "SELECT pm.post_id FROM {$wpdb->postmeta} pm |
| 1164 | INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID |
| 1165 | WHERE pm.meta_key = %s |
| 1166 | AND pm.meta_value = %s |
| 1167 | AND p.post_type = %s", |
| 1168 | 'attribute_' . $taxonomy, |
| 1169 | $term_slug, |
| 1170 | 'product_variation' |
| 1171 | ) |
| 1172 | ); |
| 1173 | |
| 1174 | self::regenerate_variation_summaries( $variation_ids ); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | WC_Post_Data::init(); |
| 1179 |