forked from jigoshop/jigoshop
/
jigoshop.php
577 lines (441 loc) · 18.8 KB
/
jigoshop.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
/*
Plugin Name: Jigoshop - WordPress eCommerce
Plugin URI: http://jigoshop.com
Description: An eCommerce plugin for WordPress.
Version: 0.9.8.1
Author: Jigowatt
Author URI: http://jigowatt.co.uk
Requires at least: 3.1
Tested up to: 3.1.3
*/
/**
* Jigoshop
*
* 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
*/
@session_start();
if (!defined("JIGOSHOP_VERSION")) define("JIGOSHOP_VERSION", "0.9.8.1");
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
load_plugin_textdomain('jigoshop', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
/**
* Installs and upgrades
**/
register_activation_hook( __FILE__, 'install_jigoshop' );
function jigoshop_update_check() {
if (get_site_option('jigoshop_db_version') != JIGOSHOP_VERSION) install_jigoshop();
}
if (is_admin()) add_action('init', 'jigoshop_update_check');
/**
* Include core files and classes
**/
include_once( 'classes/jigoshop.class.php' );
include_once( 'jigoshop_taxonomy.php' );
include_once( 'jigoshop_widgets.php' );
include_once( 'jigoshop_shortcodes.php' );
include_once( 'jigoshop_templates.php' );
include_once( 'jigoshop_template_actions.php' );
include_once( 'jigoshop_emails.php' );
include_once( 'jigoshop_query.php' );
include_once( 'jigoshop_cron.php' );
include_once( 'jigoshop_actions.php' );
include_once( 'gateways/gateways.class.php' );
include_once( 'gateways/gateway.class.php' );
include_once( 'shipping/shipping.class.php' );
include_once( 'shipping/shipping_method.class.php' );
/**
* Include admin area
**/
if (is_admin()) include_once( 'admin/jigoshop-admin.php' );
/**
* Include all classes, dro-ins and shipping/gateways modules
*/
$include_files = array();
// Classes
$include_files = array_merge($include_files, (array) glob( dirname(__FILE__)."/classes/*.php" ));
// Shipping
$include_files = array_merge($include_files, (array) glob( dirname(__FILE__)."/shipping/*.php" ));
// Payment Gateways
$include_files = array_merge($include_files, (array) glob( dirname(__FILE__)."/gateways/*.php" ));
// Drop-ins (addons, premium features etc)
$include_files = array_merge($include_files, (array) glob( dirname(__FILE__)."/drop-ins/*.php" ));
if ($include_files) :
foreach($include_files as $filename) :
if (!empty($filename) && strstr($filename, 'php')) :
include_once($filename);
endif;
endforeach;
endif;
$jigoshop = jigoshop::get();
// Init class singletons
$jigoshop_customer = jigoshop_customer::get(); // Customer class, sorts out session data such as location
$jigoshop_shipping = jigoshop_shipping::get(); // Shipping class. loads and stores shipping methods
$jigoshop_payment_gateways = jigoshop_payment_gateways::get(); // Payment gateways class. loads and stores payment methods
$jigoshop_cart = jigoshop_cart::get(); // Cart class, stores the cart contents
// Constants
if (!defined('JIGOSHOP_USE_CSS')) :
if (get_option('jigoshop_disable_css')=='yes') define('JIGOSHOP_USE_CSS', false);
else define('JIGOSHOP_USE_CSS', true);
endif;
if (!defined('JIGOSHOP_TEMPLATE_URL')) define('JIGOSHOP_TEMPLATE_URL', 'jigoshop/'); // Trailing slash is important :)
/**
* Add post thumbnail support to wordpress
**/
add_theme_support( 'post-thumbnails' );
/**
* Filters and hooks
**/
add_action('init', 'jigoshop_init', 0);
add_action('plugins_loaded', 'jigoshop_shipping::init', 1); // Load shipping methods - some may be added by plugins
add_action('plugins_loaded', 'jigoshop_payment_gateways::init', 1); // Load payment methods - some may be added by plugins
add_action('plugins_loaded', 'jigoshop_cart::calculate_totals', 2); // After methods are loaded we'll want to calc the totals of the cart
if (get_option('jigoshop_force_ssl_checkout')=='yes') add_action( 'wp_head', 'jigoshop_force_ssl');
add_action( 'wp_footer', 'jigoshop_demo_store' );
add_action( 'wp_footer', 'jigoshop_sharethis' );
/**
* IIS compat fix/fallback
**/
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
}
/**
* Support for Import/Export
*
* WordPress import should work - however, it fails to import custom product attribute taxonomies.
* This code grabs the file before it is imported and ensures the taxonomies are created.
**/
function jigoshop_import_start() {
global $wpdb;
$id = (int) $_POST['import_id'];
$file = get_attached_file( $id );
$parser = new WXR_Parser();
$import_data = $parser->parse( $file );
if (isset($import_data['posts'])) :
$posts = $import_data['posts'];
if ($posts && sizeof($posts)>0) foreach ($posts as $post) :
if ($post['post_type']=='product') :
if ($post['terms'] && sizeof($post['terms'])>0) :
foreach ($post['terms'] as $term) :
$domain = $term['domain'];
if (strstr($domain, 'product_attribute_')) :
// Make sure it exists!
if (!taxonomy_exists( $domain )) :
$nicename = ucfirst(str_replace('product_attribute_', '', $domain));
// Create the taxonomy
$wpdb->insert( $wpdb->prefix . "jigoshop_attribute_taxonomies", array( 'attribute_name' => $nicename, 'attribute_type' => 'text' ), array( '%s', '%s' ) );
// Register the taxonomy now so that the import works!
register_taxonomy( $domain,
array('product'),
array(
'hierarchical' => true,
'labels' => array(
'name' => $nicename,
'singular_name' => $nicename,
'search_items' => __( 'Search ', 'jigoshop') . $nicename,
'all_items' => __( 'All ', 'jigoshop') . $nicename,
'parent_item' => __( 'Parent ', 'jigoshop') . $nicename,
'parent_item_colon' => __( 'Parent ', 'jigoshop') . $nicename . ':',
'edit_item' => __( 'Edit ', 'jigoshop') . $nicename,
'update_item' => __( 'Update ', 'jigoshop') . $nicename,
'add_new_item' => __( 'Add New ', 'jigoshop') . $nicename,
'new_item_name' => __( 'New ', 'jigoshop') . $nicename
),
'show_ui' => false,
'query_var' => true,
'rewrite' => array( 'slug' => strtolower(sanitize_title($nicename)), 'with_front' => false, 'hierarchical' => true ),
)
);
update_option('jigowatt_update_rewrite_rules', '1');
endif;
endif;
endforeach;
endif;
endif;
endforeach;
endif;
}
add_action('import_start', 'jigoshop_import_start');
### Functions #########################################################
function jigoshop_init() {
jigoshop_post_type();
// Image sizes
add_image_size( 'shop_tiny', jigoshop::get_var('shop_tiny_w'), jigoshop::get_var('shop_tiny_h'), 'true' );
add_image_size( 'shop_thumbnail', jigoshop::get_var('shop_thumbnail_w'), jigoshop::get_var('shop_thumbnail_h'), 'true' );
add_image_size( 'shop_small', jigoshop::get_var('shop_small_w'), jigoshop::get_var('shop_small_h'), 'true' );
add_image_size( 'shop_large', jigoshop::get_var('shop_large_w'), jigoshop::get_var('shop_large_h'), 'true' );
// Include template functions here so they are pluggable by themes
include_once( 'jigoshop_template_functions.php' );
@ob_start();
add_role('customer', 'Customer', array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false
));
$css = file_exists(get_stylesheet_directory() . '/jigoshop/style.css') ? get_stylesheet_directory_uri() . '/jigoshop/style.css' : jigoshop::plugin_url() . '/assets/css/frontend.css';
if (JIGOSHOP_USE_CSS) wp_register_style('jigoshop_frontend_styles', $css );
wp_register_style('jigoshop_fancybox_styles', jigoshop::plugin_url() . '/assets/css/fancybox.css');
wp_register_style('jqueryui_styles', jigoshop::plugin_url() . '/assets/css/ui.css');
wp_register_script( 'fancybox', jigoshop::plugin_url() . '/assets/js/jquery.fancybox-1.3.4.pack.js', 'jquery', '1.0' );
wp_register_script( 'blockui', jigoshop::plugin_url() . '/assets/js/blockui.js', 'jquery', '1.0' );
wp_register_script( 'cookie', jigoshop::plugin_url() . '/assets/js/cookie.js', 'jquery', '1.0' );
wp_register_script( 'scrollto', jigoshop::plugin_url() . '/assets/js/scrollto.js', 'jquery', 'jquery', '1.0' );
wp_register_script( 'jigoshop_script', jigoshop::plugin_url() . '/assets/js/script.js.php', 'jquery', '1.0' );
wp_register_script( 'jqueryui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js', 'jquery', '1.0' );
wp_register_script( 'jquery.placeholder', jigoshop::plugin_url() . '/assets/js/jquery.placeholder.js', 'jquery', '1.0' );
if (is_admin()) :
wp_register_style('jigoshop_admin_styles', jigoshop::plugin_url() . '/assets/css/admin.css');
wp_register_style('jigoshop_admin_datepicker_styles', jigoshop::plugin_url() . '/assets/css/datepicker.css');
wp_enqueue_style('jigoshop_admin_styles');
wp_enqueue_style('jigoshop_admin_datepicker_styles');
wp_register_script( 'flot', jigoshop::plugin_url() . '/assets/js/jquery.flot.min.js' );
wp_enqueue_script('flot');
wp_enqueue_script('blockui');
wp_enqueue_script('cookie');
else :
wp_enqueue_style('jigoshop_frontend_styles');
wp_enqueue_style('jigoshop_fancybox_styles');
wp_enqueue_style('jqueryui_styles');
wp_enqueue_script('jquery');
wp_enqueue_script('jqueryui');
wp_enqueue_script('fancybox');
wp_enqueue_script('blockui');
wp_enqueue_script('cookie');
wp_enqueue_script('scrollto');
wp_enqueue_script('jigoshop_script');
wp_enqueue_script('jquery.placeholder');
endif;
}
add_filter('script_loader_src', 'jigoshop_script_query_string');
function jigoshop_script_query_string($src)
{
if ( FALSE === strpos($src, 'script.js.php') ) return $src;
$src = explode('?', $src);
$load_scripts = array();
if ( is_page(get_option('jigoshop_checkout_page_id')) || is_page(get_option('jigoshop_pay_page_id')) ) :
$load_scripts[] = 'checkout';
endif;
return $src[0] . '?load_scripts='.implode(',', $load_scripts);
}
/*
jigoshop_demo_store
Adds a demo store banner to the site
*/
function jigoshop_demo_store() {
if (get_option('jigoshop_demo_store')=='yes') :
echo '<p class="demo_store">'.__('This is a demo store for testing purposes — no orders shall be fulfilled.', 'jigoshop').'</p>';
endif;
}
/*
jigoshop_sharethis
Adds social sharing code to footer
*/
function jigoshop_sharethis() {
if (is_single() && get_option('jigoshop_sharethis')) :
echo '<script type="text/javascript" src="https://w.sharethis.com/button/buttons.js"></script><script type="text/javascript">stLight.options({publisher:"'.get_option('jigoshop_sharethis').'", onhover: false});</script>';
endif;
}
function is_cart() {
if (is_page(get_option('jigoshop_cart_page_id'))) return true;
return false;
}
function is_checkout() {
if (
is_page(get_option('jigoshop_checkout_page_id'))
) return true;
return false;
}
if (!function_exists('is_ajax')) {
function is_ajax() {
if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) return true;
return false;
}
}
function jigoshop_force_ssl() {
if (is_checkout() && !is_ssl()) :
wp_redirect( str_replace('http:', 'https:', get_permalink(get_option('jigoshop_checkout_page_id'))), 301 );
exit;
endif;
}
function jigoshop_force_ssl_images( $content ) {
if (is_ssl()) :
if (is_array($content)) :
$content = array_map('jigoshop_force_ssl_images', $content);
else :
$content = str_replace('http:', 'https:', $content);
endif;
endif;
return $content;
}
add_filter('post_thumbnail_html', 'jigoshop_force_ssl_images');
add_filter('widget_text', 'jigoshop_force_ssl_images');
add_filter('wp_get_attachment_url', 'jigoshop_force_ssl_images');
add_filter('wp_get_attachment_image_attributes', 'jigoshop_force_ssl_images');
add_filter('wp_get_attachment_url', 'jigoshop_force_ssl_images');
function get_jigoshop_currency_symbol() {
$currency = get_option('jigoshop_currency');
$currency_symbol = '';
switch ($currency) :
case 'AUD' :
case 'BRL' :
case 'CAD' :
case 'MXN' :
case 'NZD' :
case 'HKD' :
case 'SGD' :
case 'USD' : $currency_symbol = '$'; break;
case 'EUR' : $currency_symbol = '€'; break;
case 'JPY' : $currency_symbol = '¥'; break;
case 'CZK' :
case 'DKK' :
case 'HUF' :
case 'ILS' :
case 'MYR' :
case 'NOK' :
case 'PHP' :
case 'PLN' :
case 'SEK' :
case 'CHF' :
case 'TWD' :
case 'THB' : $currency_symbol = $currency; break;
case 'GBP' :
default : $currency_symbol = '£'; break;
endswitch;
return apply_filters('jigoshop_currency_symbol', $currency_symbol, $currency);
}
function jigoshop_price( $price, $args = array() ) {
extract(shortcode_atts(array(
'ex_tax_label' => '0'
), $args));
$return = '';
$num_decimals = (int) get_option('jigoshop_price_num_decimals');
$currency_pos = get_option('jigoshop_currency_pos');
$currency_symbol = get_jigoshop_currency_symbol();
$price = number_format( (double) $price, $num_decimals, get_option('jigoshop_price_decimal_sep'), get_option('jigoshop_price_thousand_sep') );
switch ($currency_pos) :
case 'left' :
$return = $currency_symbol . $price;
break;
case 'right' :
$return = $price . $currency_symbol;
break;
case 'left_space' :
$return = $currency_symbol . ' ' . $price;
break;
case 'right_space' :
$return = $price . ' ' . $currency_symbol;
break;
endswitch;
if ($ex_tax_label && get_option('jigoshop_calc_taxes')=='yes') $return .= __(' <small>(ex. tax)</small>', 'jigoshop');
return $return;
}
function jigoshop_let_to_num($v) {
$l = substr($v, -1);
$ret = substr($v, 0, -1);
switch(strtoupper($l)){
case 'P':
$ret *= 1024;
case 'T':
$ret *= 1024;
case 'G':
$ret *= 1024;
case 'M':
$ret *= 1024;
case 'K':
$ret *= 1024;
break;
}
return $ret;
}
function jigowatt_clean( $var ) {
return strip_tags(stripslashes(trim($var)));
}
global $jigoshop_body_classes;
function jigoshop_page_body_classes() {
global $jigoshop_body_classes;
$jigoshop_body_classes = (array) $jigoshop_body_classes;
if (is_checkout() || is_page(get_option('jigoshop_pay_page_id'))) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-checkout' ) );
if (is_cart()) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-cart' ) );
if (is_page(get_option('jigoshop_thanks_page_id'))) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-thanks' ) );
if (is_page(get_option('jigoshop_shop_page_id'))) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-shop' ) );
if (is_page(get_option('jigoshop_myaccount_page_id')) || is_page(get_option('jigoshop_edit_address_page_id')) || is_page(get_option('jigoshop_view_order_page_id')) || is_page(get_option('jigoshop_change_password_page_id'))) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-myaccount' ) );
}
add_action('wp_head', 'jigoshop_page_body_classes');
function jigoshop_add_body_class( $class = array() ) {
global $jigoshop_body_classes;
$jigoshop_body_classes = (array) $jigoshop_body_classes;
$jigoshop_body_classes = array_merge($class, $jigoshop_body_classes);
}
function jigoshop_body_class($classes) {
global $jigoshop_body_classes;
$jigoshop_body_classes = (array) $jigoshop_body_classes;
$classes = array_merge($classes, $jigoshop_body_classes);
return $classes;
}
add_filter('body_class','jigoshop_body_class');
### Extra Review Field in comments #########################################################
function jigoshop_add_comment_rating($comment_id) {
if ( isset($_POST['rating']) ) :
if (!$_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0) $_POST['rating'] = 5;
add_comment_meta( $comment_id, 'rating', $_POST['rating'], true );
endif;
}
add_action( 'comment_post', 'jigoshop_add_comment_rating', 1 );
function jigoshop_check_comment_rating($comment_data) {
// If posting a comment (not trackback etc) and not logged in
if ( isset($_POST['rating']) && !jigoshop::verify_nonce('comment_rating') )
wp_die( __('You have taken too long. Please go back and refresh the page.', 'jigoshop') );
elseif ( isset($_POST['rating']) && empty($_POST['rating']) && $comment_data['comment_type']== '' ) {
wp_die( __('Please rate the product.',"jigowatt") );
exit;
}
return $comment_data;
}
add_filter('preprocess_comment', 'jigoshop_check_comment_rating', 0);
### Comments #########################################################
function jigoshop_comments($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; global $post; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>" class="comment_container">
<?php echo get_avatar( $comment, $size='60' ); ?>
<div class="comment-text">
<div class="star-rating" title="<?php echo get_comment_meta( $comment->comment_ID, 'rating', true ); ?>">
<span style="width:<?php echo get_comment_meta( $comment->comment_ID, 'rating', true )*16; ?>px"><?php echo get_comment_meta( $comment->comment_ID, 'rating', true ); ?> <?php _e('out of 5', 'jigoshop'); ?></span>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<p class="meta"><em><?php _e('Your comment is awaiting approval','jigoshop'); ?></em></p>
<?php else : ?>
<p class="meta">
<?php _e('Rating by','jigoshop'); ?> <strong class="reviewer vcard"><span class="fn"><?php comment_author(); ?></span></strong> <?php _e('on','jigoshop'); ?> <?php echo get_comment_date('M jS Y'); ?>:
</p>
<?php endif; ?>
<div class="description"><?php comment_text(); ?></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<?php
}
### Exclude order comments from front end #########################################################
function jigoshop_exclude_order_comments( $clauses ) {
global $wpdb;
$clauses['join'] = "
LEFT JOIN $wpdb->posts ON $wpdb->comments.comment_post_ID = $wpdb->posts.ID
";
if ($clauses['where']) $clauses['where'] .= ' AND ';
$clauses['where'] .= "
$wpdb->posts.post_type NOT IN ('shop_order')
";
return $clauses;
}
if (!is_admin()) add_filter('comments_clauses', 'jigoshop_exclude_order_comments');