| 1 |
<?php namespace TierPricingTable\BackgroundProcessing\Updater\Updates; |
| 2 |
|
| 3 |
/** |
| 4 |
* Class VersionTwoDotZero |
| 5 |
* |
| 6 |
* @package TierPricingTable\BackgroundProcessing\Updater\Updates |
| 7 |
*/ |
| 8 |
class VersionTwoDotZero extends VersionAbstract { |
| 9 |
|
| 10 |
/** |
| 11 |
* Update to version |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public $version = '2.0'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $action = 'tiered_price_table_update_two_dot_zero'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Main method to run updating. Version 2.0 |
| 24 |
*/ |
| 25 |
public function run() { |
| 26 |
add_action( 'init', [ $this, 'update' ] ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Update |
| 31 |
*/ |
| 32 |
public function update() { |
| 33 |
$postsToUpdate = $this->getProductsToUpdate(); |
| 34 |
|
| 35 |
// Single post |
| 36 |
if ( $postsToUpdate instanceof \WP_Post ) { |
| 37 |
$this->updatePost( $postsToUpdate->ID ); |
| 38 |
} |
| 39 |
|
| 40 |
foreach ( $postsToUpdate as $postId ) { |
| 41 |
$this->updatePost( $postId ); |
| 42 |
} |
| 43 |
|
| 44 |
$this->complete(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param int $postId |
| 49 |
*/ |
| 50 |
protected function updatePost( $postId ) { |
| 51 |
|
| 52 |
$priceRules = get_post_meta( $postId, '_price_rules', true ); |
| 53 |
|
| 54 |
if ( ! empty( $priceRules ) ) { |
| 55 |
update_post_meta( $postId, '_fixed_price_rules', $priceRules ); |
| 56 |
} |
| 57 |
|
| 58 |
update_post_meta( $postId, '_price_rules_updated', 'yes' ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Complete updating. Delete old meta values. |
| 63 |
*/ |
| 64 |
protected function complete() { |
| 65 |
delete_post_meta_by_key( '_price_rules_updated' ); |
| 66 |
delete_post_meta_by_key( '_price_rules' ); |
| 67 |
|
| 68 |
$this->setCurrentDBVersion(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get list of products to update. |
| 73 |
* |
| 74 |
* @return array |
| 75 |
*/ |
| 76 |
protected function getProductsToUpdate() { |
| 77 |
$args = array( |
| 78 |
'post_type' => [ 'product', 'product_variation' ], |
| 79 |
'posts_per_page' => - 1, |
| 80 |
|
| 81 |
'meta_query' => array( |
| 82 |
'relation' => 'AND', |
| 83 |
array( |
| 84 |
'key' => '_price_rules', |
| 85 |
'compare' => 'EXISTS', |
| 86 |
), |
| 87 |
array( |
| 88 |
'key' => '_price_rules_updated', |
| 89 |
'compare' => 'NOT EXISTS', |
| 90 |
) |
| 91 |
), |
| 92 |
'fields' => 'ids' |
| 93 |
); |
| 94 |
|
| 95 |
$query = new \WP_Query( $args ); |
| 96 |
|
| 97 |
return $query->get_posts(); |
| 98 |
} |
| 99 |
} |