forked from jigoshop/jigoshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjigoshop_actions.php
More file actions
404 lines (295 loc) · 11.6 KB
/
jigoshop_actions.php
File metadata and controls
404 lines (295 loc) · 11.6 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* Various hooks Jigoshop core uses
*
* DISCLAIMER
*
* Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
* versions in the future. If you wish to customise Jigoshop core for your needs,
* please use our GitHub repository to publish essential changes for consideration.
*
* @package Jigoshop
* @category Core
* @author Jigowatt
* @copyright Copyright (c) 2011 Jigowatt Ltd.
* @license http://jigoshop.com/license/commercial-edition
*/
/**
* - When default permalinks are enabled, redirect shop page to post type archive url
* - Add to Cart
* - Clear cart
* - Restore an order via a link
* - Cancel a pending order
* - Download a file
* - Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER
*
* When default permalinks are enabled, redirect shop page to post type archive url
*
**/
if (get_option( 'permalink_structure' )=="") add_action( 'init', 'jigoshop_shop_page_archive_redirect' );
function jigoshop_shop_page_archive_redirect() {
if ( isset($_GET['page_id']) && $_GET['page_id'] == get_option('jigoshop_shop_page_id') ) :
wp_safe_redirect( get_post_type_archive_link('product') );
exit;
endif;
}
/**
* Add to cart
**/
add_action( 'init', 'jigoshop_add_to_cart_action' );
function jigoshop_add_to_cart_action( $url = false ) {
if (isset($_GET['add-to-cart']) && $_GET['add-to-cart']) :
if ( !jigoshop::verify_nonce('add_to_cart', '_GET') ) :
elseif (is_numeric($_GET['add-to-cart'])) :
$quantity = 1;
if (isset($_POST['quantity'])) $quantity = $_POST['quantity'];
jigoshop_cart::add_to_cart($_GET['add-to-cart'], $quantity);
jigoshop::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'jigoshop'), jigoshop_cart::get_cart_url()) );
elseif ($_GET['add-to-cart']=='group') :
// Group add to cart
if (isset($_POST['quantity']) && is_array($_POST['quantity'])) :
$total_quantity = 0;
foreach ($_POST['quantity'] as $item => $quantity) :
if ($quantity>0) :
jigoshop_cart::add_to_cart($item, $quantity);
jigoshop::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'jigoshop'), jigoshop_cart::get_cart_url()) );
$total_quantity = $total_quantity + $quantity;
endif;
endforeach;
if ($total_quantity==0) :
jigoshop::add_error( __('Please choose a quantity…', 'jigoshop') );
endif;
elseif ($_GET['product']) :
/* Link on product pages */
jigoshop::add_error( __('Please choose a product…', 'jigoshop') );
wp_redirect( get_permalink( $_GET['product'] ) );
exit;
endif;
endif;
$url = apply_filters('add_to_cart_redirect', $url);
// If has custom URL redirect there
if ( $url ) {
wp_safe_redirect( $url );
exit;
}
// Otherwise redirect to where they came
else if ( isset($_SERVER['HTTP_REFERER'])) {
wp_safe_redirect($_SERVER['HTTP_REFERER']);
exit;
}
// If all else fails redirect to root
else {
wp_safe_redirect('/');
exit;
}
endif;
}
/**
* Clear cart
**/
add_action( 'wp_header', 'jigoshop_clear_cart_on_return' );
function jigoshop_clear_cart_on_return() {
if (is_page(get_option('jigoshop_thanks_page_id'))) :
if (isset($_GET['order'])) $order_id = $_GET['order']; else $order_id = 0;
if (isset($_GET['key'])) $order_key = $_GET['key']; else $order_key = '';
if ($order_id > 0) :
$order = &new jigoshop_order( $order_id );
if ($order->order_key == $order_key) :
jigoshop_cart::empty_cart();
endif;
endif;
endif;
}
/**
* Clear the cart after payment - order will be processing or complete
**/
add_action( 'init', 'jigoshop_clear_cart_after_payment' );
function jigoshop_clear_cart_after_payment( $url = false ) {
if (isset($_SESSION['order_awaiting_payment']) && $_SESSION['order_awaiting_payment'] > 0) :
$order = &new jigoshop_order($_SESSION['order_awaiting_payment']);
if ($order->id > 0 && ($order->status=='completed' || $order->status=='processing')) :
jigoshop_cart::empty_cart();
unset($_SESSION['order_awaiting_payment']);
endif;
endif;
}
/**
* Process the login form
**/
add_action('init', 'jigoshop_process_login');
function jigoshop_process_login() {
if (isset($_POST['login']) && $_POST['login']) :
jigoshop::verify_nonce('login');
if ( !isset($_POST['username']) || empty($_POST['username']) ) jigoshop::add_error( __('Username is required.', 'jigoshop') );
if ( !isset($_POST['password']) || empty($_POST['password']) ) jigoshop::add_error( __('Password is required.', 'jigoshop') );
if (jigoshop::error_count()==0) :
$creds = array();
$creds['user_login'] = $_POST['username'];
$creds['user_password'] = $_POST['password'];
$creds['remember'] = true;
$secure_cookie = is_ssl() ? true : false;
$user = wp_signon( $creds, $secure_cookie );
if ( is_wp_error($user) ) :
jigoshop::add_error( $user->get_error_message() );
else :
if ( isset($_SERVER['HTTP_REFERER'])) {
wp_safe_redirect($_SERVER['HTTP_REFERER']);
exit;
}
wp_redirect(get_permalink(get_option('jigoshop_myaccount_page_id')));
exit;
endif;
endif;
endif;
}
/**
* Process ajax checkout form
*/
add_action('wp_ajax_jigoshop-checkout', 'jigoshop_process_checkout');
add_action('wp_ajax_nopriv_jigoshop-checkout', 'jigoshop_process_checkout');
function jigoshop_process_checkout () {
include_once jigoshop::plugin_path() . '/classes/jigoshop_checkout.class.php';
jigoshop_checkout::instance()->process_checkout();
die(0);
}
/**
* Cancel a pending order - hook into init function
**/
add_action('init', 'jigoshop_cancel_order');
function jigoshop_cancel_order() {
if ( isset($_GET['cancel_order']) && isset($_GET['order']) && isset($_GET['order_id']) ) :
$order_key = urldecode( $_GET['order'] );
$order_id = (int) $_GET['order_id'];
$order = &new jigoshop_order( $order_id );
if ($order->id == $order_id && $order->order_key == $order_key && $order->status=='pending' && jigoshop::verify_nonce('cancel_order', '_GET')) :
// Cancel the order + restore stock
$order->cancel_order( __('Order cancelled by customer.', 'jigoshop') );
// Message
jigoshop::add_message( __('Your order was cancelled.', 'jigoshop') );
elseif ($order->status!='pending') :
jigoshop::add_error( __('Your order is no longer pending and could not be cancelled. Please contact us if you need assistance.', 'jigoshop') );
else :
jigoshop::add_error( __('Invalid order.', 'jigoshop') );
endif;
wp_safe_redirect(jigoshop_cart::get_cart_url());
exit;
endif;
}
/**
* Download a file - hook into init function
**/
add_action('init', 'jigoshop_download_product');
function jigoshop_download_product() {
if ( isset($_GET['download_file']) && isset($_GET['order']) && isset($_GET['email']) ) :
global $wpdb;
$download_file = (int) urldecode($_GET['download_file']);
$order = urldecode( $_GET['order'] );
$email = urldecode( $_GET['email'] );
if (!is_email($email)) wp_safe_redirect( home_url() );
$downloads_remaining = $wpdb->get_var( $wpdb->prepare("
SELECT downloads_remaining
FROM ".$wpdb->prefix."jigoshop_downloadable_product_permissions
WHERE user_email = '$email'
AND order_key = '$order'
AND product_id = '$download_file'
;") );
if ($downloads_remaining=='0') :
wp_die( sprintf(__('Sorry, you have reached your download limit for this file. <a href="%s">Go to homepage →</a>', 'jigoshop'), home_url()) );
else :
if ($downloads_remaining>0) :
$wpdb->update( $wpdb->prefix . "jigoshop_downloadable_product_permissions", array(
'downloads_remaining' => $downloads_remaining - 1,
), array(
'user_email' => $email,
'order_key' => $order,
'product_id' => $download_file
), array( '%d' ), array( '%s', '%s', '%d' ) );
endif;
// Download the file
$file_path = ABSPATH . get_post_meta($download_file, 'file_path', true);
$file_path = realpath($file_path);
$file_extension = strtolower(substr(strrchr($file_path,"."),1));
switch ($file_extension) :
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpe": case "jpeg": case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
endswitch;
if (!file_exists($file_path)) wp_die( sprintf(__('File not found. <a href="%s">Go to homepage →</a>', 'jigoshop'), home_url()) );
@ini_set('zlib.output_compression', 'Off');
@set_time_limit(0);
@session_start();
@session_cache_limiter('none');
@set_magic_quotes_runtime(0);
@ob_end_clean();
@session_write_close();
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Robots: none");
header("Content-Type: ".$ctype."");
header("Content-Description: File Transfer");
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
// workaround for IE filename bug with multiple periods / multiple dots in filename
$iefilename = preg_replace('/\./', '%2e', basename($file_path), substr_count(basename($file_path), '.') - 1);
header("Content-Disposition: attachment; filename=\"".$iefilename."\";");
} else {
header("Content-Disposition: attachment; filename=\"".basename($file_path)."\";");
}
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($file_path));
@readfile("$file_path") or wp_die( sprintf(__('File not found. <a href="%s">Go to homepage →</a>', 'jigoshop'), home_url()) );
exit;
endif;
endif;
}
/**
* Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER
**/
add_action('order_status_completed', 'jigoshop_downloadable_product_permissions');
function jigoshop_downloadable_product_permissions( $order_id ) {
global $wpdb;
$order = &new jigoshop_order( $order_id );
if (sizeof($order->items)>0) foreach ($order->items as $item) :
if ($item['id']>0) :
$_product = &new jigoshop_product( $item['id'] );
if ( $_product->exists && $_product->is_type('downloadable') ) :
$user_email = $order->billing_email;
if ($order->user_id>0) :
$user_info = get_userdata($order->user_id);
if ($user_info->user_email) :
$user_email = $user_info->user_email;
endif;
else :
$order->user_id = 0;
endif;
$limit = trim(get_post_meta($_product->id, 'download_limit', true));
if (!empty($limit)) :
$limit = (int) $limit;
else :
$limit = '';
endif;
// Downloadable product - give access to the customer
$wpdb->insert( $wpdb->prefix . 'jigoshop_downloadable_product_permissions', array(
'product_id' => $_product->id,
'user_id' => $order->user_id,
'user_email' => $user_email,
'order_key' => $order->order_key.'3',
'downloads_remaining' => $limit
), array(
'%s',
'%s',
'%s',
'%s',
'%s'
) );
endif;
endif;
endforeach;
}