| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Installer; |
| 6 |
|
| 7 |
// HPOS: This file uses direct SQL on postmeta for migration/upgrade tasks only. |
| 8 |
// Do NOT use direct SQL for live WooCommerce order data access—use WooCommerce CRUD for HPOS compatibility. |
| 9 |
// All live order data access must use WooCommerce CRUD methods. |
| 10 |
|
| 11 |
/** |
| 12 |
* Upgrade class |
| 13 |
*/ |
| 14 |
class Upgrade { |
| 15 |
|
| 16 |
public function run() { |
| 17 |
if ( version_compare( '1.1.0', get_option( 'subscrpt_version' ), '>' ) ) { |
| 18 |
|
| 19 |
// create histories table |
| 20 |
$installer = new Installer(); |
| 21 |
$installer->create_tables(); |
| 22 |
|
| 23 |
// move product meta |
| 24 |
$this->move_product_meta(); |
| 25 |
// update subscription meta |
| 26 |
$this->update_subscription_meta(); |
| 27 |
$this->update_comment_meta(); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function move_product_meta() { |
| 32 |
global $wpdb; |
| 33 |
$products_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s", 'subscrpt_general' ) ); |
| 34 |
foreach ( $products_meta as $product_meta ) { |
| 35 |
update_post_meta( $product_meta->post_id, '_subscrpt_meta', unserialize( $product_meta->meta_value ) ); |
| 36 |
update_post_meta( |
| 37 |
$product_meta->post_id, |
| 38 |
'_subscrpt_user_cancel', |
| 39 |
get_post_meta( |
| 40 |
$product_meta->post_id, |
| 41 |
'_subscrpt_user_cancell', |
| 42 |
true |
| 43 |
) |
| 44 |
); |
| 45 |
delete_post_meta( $product_meta->post_id, '_subscrpt_user_cancell' ); |
| 46 |
delete_post_meta( $product_meta->post_id, 'subscrpt_general' ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public function update_subscription_meta() { |
| 51 |
global $wpdb; |
| 52 |
|
| 53 |
$subscriptions_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s", '_subscrpt_order_general' ) ); |
| 54 |
|
| 55 |
$histories = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s", '_subscrpt_order_history' ) ); |
| 56 |
|
| 57 |
foreach ( $subscriptions_meta as $subscription_meta ) { |
| 58 |
$subscription_meta_value = unserialize( $subscription_meta->meta_value ); |
| 59 |
$order_item_id = 0; |
| 60 |
$order = wc_get_order( $subscription_meta_value['order_id'] ); |
| 61 |
|
| 62 |
if ( $order ) { |
| 63 |
foreach ( $order->get_items() as $order_item ) { |
| 64 |
if ( $order_item->get_product_id() == $subscription_meta_value['product_id'] ) { |
| 65 |
$order_item_id = $order_item->get_id(); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
update_post_meta( |
| 71 |
$subscription_meta->post_id, |
| 72 |
'_order_subscrpt_meta', |
| 73 |
array( |
| 74 |
'order_id' => $subscription_meta_value['order_id'], |
| 75 |
'order_item_id' => $order_item_id, |
| 76 |
'trial' => $subscription_meta_value['trial'], |
| 77 |
'start_date' => $subscription_meta_value['start_date'], |
| 78 |
'next_date' => $subscription_meta_value['next_date'], |
| 79 |
) |
| 80 |
); |
| 81 |
|
| 82 |
delete_post_meta( $subscription_meta->post_id, '_subscrpt_order_general' ); |
| 83 |
} |
| 84 |
|
| 85 |
foreach ( $histories as $history ) { |
| 86 |
$histories_meta = unserialize( $history->meta_value ); |
| 87 |
|
| 88 |
foreach ( $histories_meta as $history_meta ) { |
| 89 |
$order = wc_get_order( $history_meta['order_id'] ); |
| 90 |
$product_meta = get_post_meta( $history_meta['product_id'], '_subscrpt_meta', true ); |
| 91 |
$order = wc_get_order( $history_meta['order_id'] ); |
| 92 |
|
| 93 |
if ( $order ) { |
| 94 |
$order_item_id = 0; |
| 95 |
foreach ( $order->get_items() as $order_item ) { |
| 96 |
if ( $order_item->get_product_id() == $history_meta['product_id'] ) { |
| 97 |
$order_item_id = $order_item->get_id(); |
| 98 |
wc_update_order_item_meta( |
| 99 |
$order_item->get_id(), |
| 100 |
'_subscrpt_meta', |
| 101 |
array( |
| 102 |
'time' => $product_meta['time'], |
| 103 |
'type' => $product_meta['type'], |
| 104 |
'trial' => $history_meta['trial'], |
| 105 |
'start_date' => $history_meta['start_date'], |
| 106 |
'next_date' => $history_meta['next_date'], |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$history_table = $wpdb->prefix . 'subscrpt_order_relation'; |
| 113 |
$wpdb->insert( |
| 114 |
$history_table, |
| 115 |
array( |
| 116 |
'subscription_id' => $history_meta['post_id'], |
| 117 |
'order_id' => $history_meta['order_id'], |
| 118 |
'order_item_id' => $order_item_id, |
| 119 |
'type' => 'Parent Order' === $history_meta['stats'] ? 'new' : 'renew', |
| 120 |
) |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
delete_post_meta( $history->post_id, '_subscrpt_order_history' ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public function update_comment_meta() { |
| 130 |
global $wpdb; |
| 131 |
$comments_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->commentmeta} WHERE meta_key = %s", 'subscrpt_activity' ) ); |
| 132 |
foreach ( $comments_meta as $comment_meta ) { |
| 133 |
update_comment_meta( $comment_meta->comment_id, '_subscrpt_activity', $comment_meta->meta_value ); |
| 134 |
delete_comment_meta( $comment_meta->comment_id, 'subscrpt_activity' ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|