| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Hooks; |
| 4 |
|
| 5 |
use StoreEngine\Classes\AbstractProduct; |
| 6 |
use StoreEngine\Classes\Integration; |
| 7 |
use StoreEngine\Classes\Product\SimpleProduct; |
| 8 |
use StoreEngine\Classes\ProductFactory; |
| 9 |
use WP_Post; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Price { |
| 16 |
|
| 17 |
|
| 18 |
public static function init() { |
| 19 |
$self = new self(); |
| 20 |
add_action('deleted_post', [ $self, 'delete_prices_and_integrations' ], 10, 2); |
| 21 |
} |
| 22 |
|
| 23 |
public function delete_prices_and_integrations( int $post_id, WP_Post $post ) { |
| 24 |
if ( 'storeengine_product' !== $post->post_type ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$product = ProductFactory::getProduct( $post_id ); |
| 29 |
$prices = $product->get_prices(); |
| 30 |
$price_ids = array_map(fn( $price) => $price->get_id(), $prices); |
| 31 |
if ( empty( $price_ids ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
$placeholders = implode(',', array_fill(0, count($prices), '%d')); |
| 35 |
|
| 36 |
global $wpdb; |
| 37 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 38 |
$wpdb->delete($wpdb->prefix . 'storeengine_product_price', [ 'product_id' => $post_id ], [ '%d' ]); |
| 39 |
|
| 40 |
$wpdb->query( |
| 41 |
$wpdb->prepare( |
| 42 |
"DELETE FROM {$wpdb->prefix}storeengine_integrations WHERE price_id in ($placeholders)", |
| 43 |
...$price_ids |
| 44 |
) |
| 45 |
); |
| 46 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 47 |
|
| 48 |
// clear cache. |
| 49 |
wp_cache_flush_group(AbstractProduct::CACHE_GROUP); |
| 50 |
wp_cache_flush_group(Integration::CACHE_GROUP); |
| 51 |
} |
| 52 |
|
| 53 |
} |
| 54 |
|