take_month_off.php
12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Plugin Name: Fgc Take A Month Off
* Plugin URI: http://fgc.vn
* Description: Fgc Take A Month Off
* Author: Phuong An.
* Author URI: http://fgc.vn
* Version: 1.0
*
* Copyright 2016 Prospress, Inc. (email : freedoms@prospress.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Woocommerce
* @author Phuong An <phuongantt.na@gmail.com>
* @since 1.0
*/
add_action('wp_loaded', 'fgc_check_logged_in_take_month_off_page', 20);
function fgc_check_logged_in_take_month_off_page() {
$request_uri = $_SERVER["REQUEST_URI"];
if (strpos($request_uri, '/take-month-off') !== false) {
if (get_current_user_id() == 0) {
fgc_redirect_back();
}
}
}
add_action('wp_loaded', 'fgc_set_take_month_off', 20);
function fgc_set_take_month_off() {
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'set-take-month-off') {
$user_id = get_current_user_id();
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "woocommerce_take_month_off` WHERE `user_id` = $user_id AND `is_take_month_off` = 1";
$results = $wpdb->get_results($query);
if (empty($results)) {
$subscription_and_key = fgc_set_subcription_key($user_id);
if ($subscription_and_key) {
$subscription_key = $subscription_and_key['subscription_key'];
$subscription = $subscription_and_key['subscription'];
$wc_subscription = $subscription_and_key['wc_subcription'];
$current_next_payment_time = $wc_subscription->get_time('next_payment');
if ($current_next_payment_time > 0) {
// Put on hold
WC_Subscriptions_Manager::put_subscription_on_hold($user_id, $subscription_key);
// Set next payment time
$next_payment = wcs_add_months($current_next_payment_time, 1);
WC_Subscriptions_Manager::set_next_payment_date($subscription_key, $user_id, $next_payment);
$next_payment2 = strtotime(date('Y-m-d', $next_payment));
$date_active = strtotime('+30 days', strtotime(date('Y-m-d'))); //30 * 24 * 3600 + current_time('timestamp', true);
if ($date_active > $next_payment2) {
$date_active = strtotime('-3 days', $next_payment2);
}
// Set reactive time
global $wpdb;
$query = "INSERT INTO `" . $wpdb->prefix . "woocommerce_take_month_off` (`user_id`, `subscription_key`, `is_take_month_off`,`date_active`) VALUES ($user_id, '$subscription_key', 1,$date_active);";
// Insert new recod
$result = $wpdb->query($query);
if ($result) {
$stringTime = gmdate("dS F Y", $next_payment);
fgc_update_order_comment_after_take_month_off($subscription_key);
fgc_add_tag_after_take_month_off($user_id);
setcookie("isSetTakeMonthOff", '1', time() + 3600, '/');
do_action('fgc_set_take_month_off', $wc_subscription);
?>
Your wish is our command, enjoy your month off. Your next payment will be on <?php echo gmdate("dS F Y", $next_payment) ?>.<br/><br/>
<a class="ok button btnrow" href="/my-account">OK</a>
<?php
} else {
echo 'Can not insert to ' . $wpdb->prefix . "woocommerce_take_month_off";
}
} else {
echo 'Next payment time is invalid.';
}
} else {
echo 'Not found subscription.';
}
} else {
echo 'You are taking month off now.';
}
exit();
}
}
add_action('wp_loaded', 'fgc_set_reactive_subscriptions', 20);
function fgc_set_reactive_subscriptions() {
if (isset($_REQUEST['cr_action']) && $_REQUEST['cr_action'] == 'reactive-take-month-off') {
echo 'Start reactive:<br/>';
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "woocommerce_take_month_off` WHERE `is_take_month_off` = 1 AND `date_active` < " . strtotime("tomorrow");
$results = $wpdb->get_results($query);
foreach ($results as $result) {
if (WC_Subscriptions_Manager::activate_subscription($result->user_id, $result->subscription_key)) {
$query_update = "UPDATE `" . $wpdb->prefix . "woocommerce_take_month_off` SET `is_take_month_off` = 0 WHERE `user_id` = " . $result->user_id;
$result_update = $wpdb->query($query_update);
if ($result_update) {
echo '<br/>Reactive successfull for subscription of ';
} else {
echo '<br/>Can update on database with ';
}
} else {
echo '<br/>Can not active subscription of ';
}
echo 'user_id: ' . $result->user_id . ' , subscription_key: ' . $result->subscription_key;
}
echo '<br/><br/>Complete.';
exit();
}
}
add_action('wp_loaded', 'fgc_update_data_take_month_off', 20);
function fgc_update_data_take_month_off() {
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'updateDataTakeMonthOff') {
echo '<pre>';
echo 'Start update:<br/>';
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_set_on_re_active' AND `meta_value` > " . strtotime("now");
$results = $wpdb->get_results($query);
foreach ($results as $result) {
$order = new WC_Order($result->post_id);
$user_id = $order->user_id;
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions($user_id);
$subscription_key = '';
foreach ($subscriptions as $subscription) {
if ($subscription['status'] == 'on-hold') {
$subscription_key = WC_Subscriptions_Manager::get_subscription_key($subscription['order_id'], $subscription['product_id']);
$date_active = $result->meta_value;
$query_check_exist = "SELECT * FROM `" . $wpdb->prefix . "woocommerce_take_month_off` WHERE `subscription_key` = '$subscription_key' AND `is_take_month_off` = 1 ";
$results_check_exist = $wpdb->get_results($query_check_exist);
if (count($results_check_exist) == 0) {
$query_insert = "INSERT INTO `" . $wpdb->prefix . "woocommerce_take_month_off` (`user_id`, `subscription_key`, `is_take_month_off`,`date_active`) VALUES ($user_id, '$subscription_key', 1,$date_active);";
// Insert new recod
$result_insert = $wpdb->query($query_insert);
if ($result_insert) {
echo '<br/>' . $query_insert;
} else {
echo '<br/>Cant update for order: ' . $result->post_id;
}
} else {
echo '<br/>A subscription can not have two take month off at same time.';
}
break;
}
}
}
echo '<br/><br/>Complete.';
exit();
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'updateTakeMonthOffTag') {
echo '<pre>';
echo 'Start update:<br/>';
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_set_on_re_active' ";
$results = $wpdb->get_results($query);
foreach ($results as $result) {
$order = new WC_Order($result->post_id);
$user_id = $order->user_id;
if ($user_id > 0 && $user_id != '') {
$date_active = (int) $result->meta_value;
FGC_ActiveCampaign::add_tag_by_user_id('SUBSCRIPTION: Take Month Off', $user_id);
echo '<br/>Take Month Off: ' . $user_id;
if ($date_active > strtotime("now")) {
FGC_ActiveCampaign::add_tag_by_user_id('SUBSCRIPTION: On Hold', $user_id);
echo '<br/>On Hold: ' . $user_id;
}
} else {
echo '<br/>user_id: ' . $user_id;
}
}
echo '<br/><br/>Complete.';
exit();
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'checkTakeMonthOffNote') {
echo '<pre>';
echo 'Start update:<br/>';
fgc_update_order_comment_after_take_month_off('10451_308');
echo '<br/><br/>Complete.';
exit();
}
}
add_action('woocommerce_payment_complete', 'fgc_set_take_month_off_after_upgrade_downgrade', 30);
function fgc_set_take_month_off_after_upgrade_downgrade($order_id) {
$order = new WC_Order($order_id);
$user_id = $order->user_id;
global $wpdb;
$query = "SELECT * FROM `" . $wpdb->prefix . "woocommerce_take_month_off` WHERE `user_id` = $user_id AND `is_take_month_off` = 1";
$results = $wpdb->get_results($query);
if (!empty($results)) {
WC_Subscriptions_Manager::put_subscription_on_hold($user_id, $results[0]->subscription_key);
}
}
function fgc_redirect_back() {
echo '<META HTTP-EQUIV = "Refresh" Content = "0; URL=' . get_site_url() . '/login-page' . '">';
exit();
}
function fgc_set_subcription_key($user_id = 0) {
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions($user_id);
$subscription_key = '';
foreach ($subscriptions as $subscription) {
if ($subscription['status'] == 'active') {
$subscription_key = WC_Subscriptions_Manager::get_subscription_key($subscription['order_id'], $subscription['product_id']);
if ($subscription_key != '') {
$wc_subscription = wcs_get_subscription_from_key($subscription_key);
return array('subscription_key' => $subscription_key, 'subscription' => $subscription, 'wc_subcription' => $wc_subscription);
}
}
}
return false;
}
function fgc_update_order_comment_after_take_month_off($subscription_key) {
$wc_subscription = wcs_get_subscription_from_key($subscription_key);
$subscription_id = $wc_subscription->id;
global $wpdb;
$query = "SELECT `comment_ID` FROM `wp_comments` WHERE `comment_post_ID` = $subscription_id AND `comment_content` = 'Status changed from Active to On hold.' AND `comment_type` = 'order_note' ";
$result = $wpdb->get_results($query);
if (!empty($result)) {
$comment_id = $result[0]->comment_ID;
$query_update = "UPDATE `wp_comments` SET `comment_content` = 'Status changed from Active to On hold by user.' WHERE `comment_ID` = $comment_id ";
$wpdb->query($query_update);
}
}
function fgc_add_tag_after_take_month_off($user_id) {
FGC_ActiveCampaign::add_tag_by_user_id('SUBSCRIPTION: Take Month Off', $user_id);
FGC_ActiveCampaign::add_tag_by_user_id('SUBSCRIPTION: On Hold', $user_id);
}
function fgc_remove_tag_after_reactive($user_id) {
FGC_ActiveCampaign::remove_tag_by_user_id('SUBSCRIPTION: On Hold', $user_id);
}
global $wpdb;
$take_month_off_db_version = '1.1';
$fgc_installed_ver = get_option("take_month_off_db_version");
if ($fgc_installed_ver != $take_month_off_db_version) {
$table_name_new1 = $wpdb->prefix . 'woocommerce_take_month_off';
$sql1 = "CREATE TABLE IF NOT EXISTS `$table_name_new1` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`user_id` BIGINT(20) NULL,
`subscription_key` VARCHAR(255) NULL,
`date_active` BIGINT NULL,
`is_take_month_off` TINYINT(1) NULL,
PRIMARY KEY (`id`));";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql1);
update_option("take_month_off_db_version", $take_month_off_db_version);
}