forked from weDevsOfficial/wp-user-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpuf-subscription.php
executable file
·333 lines (274 loc) · 10.1 KB
/
wpuf-subscription.php
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* WPUF subscription manager
*
* @since 0.2
* @author Tareq Hasan
* @package WP User Frontend
*/
class WPUF_Subscription {
function __construct() {
add_filter( 'wpuf_add_post_args', array($this, 'set_pending'), 10, 1 );
add_filter( 'wpuf_after_post_redirect', array($this, 'post_redirect'), 10, 2 );
add_action( 'wpuf_add_post_after_insert', array($this, 'monitor_new_post'), 10, 1 );
add_action( 'wpuf_payment_received', array($this, 'payment_received') );
add_shortcode( 'wpuf_sub_info', array($this, 'subscription_info') );
add_shortcode( 'wpuf_sub_pack', array($this, 'subscription_packs') );
}
/**
* Get a subscription row from database
*
* @global object $wpdb
* @param int $sub_id subscription pack id
* @return object|bool
*/
public function get_subscription( $sub_id ) {
global $wpdb;
$sql = "SELECT * FROM {$wpdb->prefix}wpuf_subscription WHERE id=$sub_id";
$row = $wpdb->get_row( $sql );
return $row;
}
/**
* Get all the subscription package
*
* @global object $wpdb
* @return object|bool
*/
public function get_subscription_packs() {
global $wpdb;
$sql = "SELECT * FROM {$wpdb->prefix}wpuf_subscription ORDER BY created DESC";
$result = $wpdb->get_results( $sql );
return $result;
}
/**
* Checks against the user, if he is valid for posting new post
*
* @global object $userdata
* @param int $post_id
* @return bool
*/
function has_post_error( $post_id = 0 ) {
global $userdata;
if ( wpuf_get_option( 'charge_posting', 'wpuf_payment' ) == 'no' ) {
return false;
}
//get duration and count
$duration = ( $userdata->wpuf_sub_validity ) ? $userdata->wpuf_sub_validity : 0;
$count = ( $userdata->wpuf_sub_pcount ) ? $userdata->wpuf_sub_pcount : 0;
$error = false;
//validate duration and count
if ( !$duration || !$count ) {
//if someone is zero
return true;
}
//if duration is expired
if ( $duration != 'unlimited' ) {
$diff = strtotime( $duration ) - time();
if ( $diff < 0 ) {
return true;
}
}
//no balance
if ( $count != 'unlimited' && $count <= 0 ) {
return true;
}
return false;
}
/**
* Set the new post status if charging is active
*
* @param string $postdata
* @return string
*/
function set_pending( $postdata ) {
if ( wpuf_get_option( 'charge_posting', 'wpuf_payment' ) == 'yes' ) {
$postdata['post_status'] = 'pending';
}
return $postdata;
}
/**
* Checks the posting validity after a new post
*
* @global object $userdata
* @global object $wpdb
* @param int $post_id
*/
function monitor_new_post( $post_id ) {
global $wpdb;
$userdata = get_userdata( get_current_user_id() );
if ( $this->has_post_error( $post_id ) ) {
//there is some error and it needs payment
//add a uniqid to track the post easily
$order_id = uniqid( rand( 10, 1000 ), false );
add_post_meta( $post_id, 'wpuf_order_id', $order_id, true );
} else {
$count = ( $userdata->wpuf_sub_pcount ) ? $userdata->wpuf_sub_pcount : 0;
//decrease the post count, if not umlimited
if ( $count != 'unlimited' ) {
$count = intval( $count );
update_usermeta( $userdata->ID, 'wpuf_sub_pcount', $count - 1 );
//set the post status to publish
wp_update_post( array('ID' => $post_id, 'post_status' => 'publish') );
}
}
}
/**
* Redirect to payment page after new post
*
* @param string $str
* @param type $post_id
* @return string
*/
function post_redirect( $post_url, $post_id ) {
if ( $this->has_post_error( $post_id ) ) {
$redirect = get_permalink( wpuf_get_option( 'payment_page', 'wpuf_payment' ) ) . '?action=wpuf_pay&type=post&post_id=' . $post_id;
return $redirect;
}
return $post_url;
}
/**
* Perform actions when a new payment is made
*
* @param array $info payment info
*/
function payment_received( $info ) {
if ( $info['post_id'] ) {
$this->handle_post_publish( $info['post_id'] );
} else if ( $info['pack_id'] ) {
$this->new_subscription( $info['user_id'], $info['pack_id'] );
}
}
/**
* Store new subscription info on user profile
*
* if data = 0, means 'unlimited'
*
* @param int $user_id
* @param int $pack_id subscription pack id
*/
public function new_subscription( $user_id, $pack_id ) {
$subscription = $this->get_subscription( $pack_id );
if ( $user_id && $subscription ) {
//store the duration
if ( $subscription->duration == 0 ) {
update_user_meta( $user_id, 'wpuf_sub_validity', 'unlimited' );
} else {
//store that future date in usermeta
$duration = date( 'Y-m-d G:i:s', strtotime( date( 'Y-m-d G:i:s', time() ) . " +{$subscription->duration} day" ) );
update_user_meta( $user_id, 'wpuf_sub_validity', $duration );
}
//store post count
if ( $subscription->count == 0 ) {
update_user_meta( $user_id, 'wpuf_sub_pcount', 'unlimited' );
} else {
update_user_meta( $user_id, 'wpuf_sub_pcount', $subscription->count );
}
//store pack id
update_user_meta( $user_id, 'wpuf_sub_pack', $subscription->id );
}
}
/**
* Publish the post if payment is made
*
* @param int $post_id
*/
function handle_post_publish( $order_id ) {
global $wpdb;
//$post = get_post( $post_id );
$sql = $wpdb->prepare( "SELECT p.ID, p.post_status
FROM $wpdb->posts p, $wpdb->postmeta m
WHERE p.ID = m.post_id AND p.post_status <> 'publish' AND m.meta_key = 'wpuf_order_id' AND m.meta_value = '$order_id'" );
$post = $wpdb->get_row( $sql );
if ( $post && $post->post_status != 'publish' ) {
$update_post = array(
'ID' => $post->ID,
'post_status' => 'publish'
);
wp_update_post( $update_post );
}
}
/**
* Generate users subscription info with a shortcode
*
* @global type $userdata
*/
function subscription_info() {
global $userdata;
ob_start();
$userdata = get_userdata( $userdata->ID ); //wp 3.3 fix
if ( wpuf_get_option( 'charge_posting', 'wpuf_payment' ) == 'yes' && is_user_logged_in() ) {
$duration = ( $userdata->wpuf_sub_validity ) ? $userdata->wpuf_sub_validity : 0;
$count = ( $userdata->wpuf_sub_pcount ) ? $userdata->wpuf_sub_pcount : 0;
$diff = strtotime( $duration ) - time();
//var_dump( $duration, $count, $diff );
//var_dump( $userdata );
$d_str = '';
$c_str = '';
if ( $duration == 0 ) {
$d_str = 0;
} elseif ( $duration == 'unlimited' ) {
$d_str = __( 'Unlimited duration', 'wpuf' );
} elseif ( $diff <= 0 ) {
$d_str = __( 'Expired', 'wpuf' );
} elseif ( $diff > 0 ) {
$d_str = 'Till ' . date_i18n( 'd M, Y H:i', strtotime( $duration ) );
}
if ( $count == 0 ) {
$c_str = 0;
} elseif ( $count == 'unlimited' ) {
$c_str = 'unlimited post';
} else {
$c_str = $count;
}
?>
<div class="wpuf_sub_info">
<h3><?php _e( 'Subscription Details', 'wpuf' ); ?></h3>
<div class="text">
<strong><?php _e( 'Validity:', 'wpuf' ); ?></strong> <?php echo $d_str; ?>,
<strong><?php _e( 'Post Left:', 'wpuf' ); ?></strong> <?php echo $c_str; ?>
</div>
</div>
<?php
}
return ob_get_clean();
}
/**
* Show the subscription packs that are built
* from admin Panel
*/
function subscription_packs() {
$packs = $this->get_subscription_packs();
ob_start();
if ( $packs ) {
echo '<ul class="wpuf_packs">';
foreach ($packs as $pack) {
$duration = ( $pack->duration == 0 ) ? 'unlimited' : $pack->duration;
$count = ( $pack->count == 0 ) ? 'unlimited' : $pack->count;
?>
<li>
<h3><?php echo $pack->name; ?> - <?php echo $pack->description; ?></h3>
<p><?php echo $count; ?> posts for <?php echo $duration; ?> days.
<span class="cost"><?php echo wpuf_get_option( 'currency_symbol', 'wpuf_payment' ) . $pack->cost; ?></span>
</p>
<p><a href="<?php echo get_permalink( wpuf_get_option( 'payment_page', 'wpuf_payment' ) ); ?>?action=wpuf_pay&type=pack&pack_id=<?php echo $pack->id; ?>"><?php _e( 'Buy Now', 'wpuf' ); ?></a></p>
</li>
<?php
}
echo '</ul>';
}
return ob_get_clean();
}
/**
* Show a info message when posting if payment is enabled
*/
function add_post_info() {
if ( $this->has_post_error() ) {
?>
<div class="info">
<?php printf( __( 'This will cost you <strong>%s</strong>. to add a new post. You may buy some bulk package too. ', 'wpuf' ), wpuf_get_option( 'currency_symbol', 'wpuf_payment' ) . wpuf_get_option( 'cost_per_post', 'wpuf_payment' ) ); ?>
</div>
<?php
}
}
}
$wpuf_subscription = new WPUF_Subscription();