PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.5.0
Subscriptions for WooCommerce with Stripe Recurring Payments v1.5.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Upgrade.php

Upgrade.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.5.0, at includes/Upgrade.php

139 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 class Upgrade {
12
13 public function run() {
14 if ( version_compare( '1.1.0', get_option( 'subscrpt_version' ), '>' ) ) {
15
16 // create histories table
17 $installer = new Installer();
18 $installer->create_tables();
19
20 // move product meta
21 $this->move_product_meta();
22 // update subscription meta
23 $this->update_subscription_meta();
24 $this->update_comment_meta();
25 }
26 }
27
28 public function move_product_meta() {
29 global $wpdb;
30 $product_meta_query = 'SELECT * FROM ' . $wpdb->prefix . "postmeta WHERE meta_key='subscrpt_general'";
31 $products_meta = $wpdb->get_results( $product_meta_query );
32 foreach ( $products_meta as $product_meta ) {
33 update_post_meta( $product_meta->post_id, '_subscrpt_meta', unserialize( $product_meta->meta_value ) );
34 update_post_meta(
35 $product_meta->post_id,
36 '_subscrpt_user_cancel',
37 get_post_meta(
38 $product_meta->post_id,
39 '_subscrpt_user_cancell',
40 true
41 )
42 );
43 delete_post_meta( $product_meta->post_id, '_subscrpt_user_cancell' );
44 delete_post_meta( $product_meta->post_id, 'subscrpt_general' );
45 }
46 }
47
48 public function update_subscription_meta() {
49 global $wpdb;
50
51 $subscription_meta_query = 'SELECT * FROM ' . $wpdb->prefix . "postmeta WHERE meta_key='_subscrpt_order_general'";
52 $subscriptions_meta = $wpdb->get_results( $subscription_meta_query );
53
54 $subscription_history_query = 'SELECT * FROM ' . $wpdb->prefix . "postmeta WHERE meta_key='_subscrpt_order_history'";
55 $histories = $wpdb->get_results( $subscription_history_query );
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 $query = 'SELECT * FROM ' . $wpdb->prefix . "commentmeta WHERE meta_key='subscrpt_activity'";
132 $comments_meta = $wpdb->get_results( $query );
133 foreach ( $comments_meta as $comment_meta ) {
134 update_comment_meta( $comment_meta->comment_id, '_subscrpt_activity', $comment_meta->meta_value );
135 delete_comment_meta( $comment_meta->comment_id, 'subscrpt_activity' );
136 }
137 }
138 }
139