-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsave-for-later.php
501 lines (423 loc) · 15.8 KB
/
save-for-later.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
<?php
if ( !defined( 'ABSPATH' ) )
die( '-1' );
if ( ! class_exists( 'WC_Wishlist' ) ) {
/**
* Main WooCommerce Wishlist Class
*
* Contains the main hooks, functions & vars for WooCommerce Wishlist
*
* @class WC_Wishlist
* @version 1.0
* @package WC_Wishlist
* @category Extension
* @author codearachnid
*/
class WC_Wishlist {
protected static $instance;
private $default_ajax_response = array(
'msg' => null,
'status' => false,
'code' => null,
'wishlist' => array(),
'products' => array(),
'product' => array()
);
public $path;
public $dir;
public $url;
public $version = '1.0';
const PLUGIN_NAME = 'WC: Save For Later';
const MIN_WC_VERSION = '1.6.5';
const MIN_WP_VERSION = '3.4';
const MIN_PHP_VERSION = '5.3';
const POST_TYPE = 'woocommerce_wishlist';
function __construct() {
// register lazy autoloading
spl_autoload_register( 'self::lazy_loader' );
$this->check_install();
// enable the settings
if ( is_admin() )
new WC_Wishlist_Settings();
// set core vars
$this->path = self::get_plugin_path();
$this->dir = trailingslashit( basename( $this->path ) );
$this->url = plugins_url() . '/' . $this->dir;
$this->base_slug = apply_filters( 'woocommerce_wishlist_base_slug', 'wishlist' );
// core plugin items
add_action( 'init', array( $this, 'register_post_type' ) );
// ajax handlers
add_action( 'wp_ajax_woocommerce_wishlist', array( $this, 'ajax_handler' ) ); // authenticated users
add_action( 'wp_ajax_nopriv_woocommerce_wishlist', array( $this, 'ajax_handler' ) ); // authenticated users
// templating
add_shortcode( 'woocommerce_create_account', array( $this, 'shortcode_create_account' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ), 100 );
add_action( 'wp_footer', array( $this, 'wp_footer' ) );
add_action( 'woocommerce_wishlist_dock_meta', array( 'WC_Wishlist_Template', 'dock_title' ) );
// hook into WC for templating
add_filter( 'woocommerce_cart_item_remove_link', array('WC_Wishlist_Template', 'cart_item_save_link'), 10, 2 );
add_action( 'woocommerce_ajax_added_to_cart', array( $this, 'wc_ajax_added_to_cart' ) );
add_action( 'woocommerce_before_cart_table', array( 'WC_Wishlist_Template', 'checkout_notice' ) );
add_action( 'woocommerce_before_my_account', array( 'WC_Wishlist_Template', 'my_account_dashboard' ) );
add_action( 'woocommerce_before_shop_loop_item_title', array( 'WC_Wishlist_Template', 'product_image_overlay' ), 20 );
add_action( 'woocommerce_after_shop_loop_item', array( 'WC_Wishlist_Template', 'product_button' ), 20 ); // link on product collections page
add_action( 'woocommerce_after_add_to_cart_button', array( 'WC_Wishlist_Template', 'product_button' ), 20 ); // link on product single page
}
/**
* Sniff out request to move items from cart to wishlist
*
* @return mixed $status or $response
*/
// function cart_item_listener(){
// global $woocommerce;
// $status = false;
// if ( !empty($_GET['save_for_later']) && $product_id = absint( $_GET['save_for_later'] ) && $product_id &&
// $woocommerce->verify_nonce('cart', '_GET') &&
// is_user_logged_in() &&
// $this->add_product_to_wishlist( $product_id, null, array() ) ){
// // woocommerce_update_cart_action();
// }
// // if( is_ajax() ) {
// // $response = apply_filters( 'woocommerce_wishlist_ajax_cart_item_listener', wp_parse_args( $this->ajax_get_products(), $this->ajax_response_default() ) );
// // echo json_encode( $response );
// // die();
// // } else {
// // return $status;
// // }
// }
/**
* Leverage 'woocommerce_ajax_added_to_cart' to remove the product from wishlist
*
* @param int $product_id
* @return int $product_id
*/
function wc_ajax_added_to_cart( $product_id ) {
if ( $wishlist_id = $this->get_wishlist_id() ) {
woocommerce_wishlist_delete_meta( $wishlist_id, $product_id );
}
return $product_id;
}
/**
* Output a simple registration form when shortcode '[woocommerce_create_account]' is used
*
* @return void
*/
function shortcode_create_account() {
WC_Wishlist_Template::register_form();
}
function ajax_response_default(){
$defaults = apply_filters( 'woocommerce_wishlist_ajax_response_default', array(
'msg' => null,
'status' => false,
'code' => null,
'wishlist' => array(),
'products' => array(),
'product' => (object) array(
'ID' => null,
'title' => null,
'permalink' => null,
'thumbnail' => null
)));
return $defaults;
}
function ajax_handler(){
$defaults = $this->ajax_response_default();
$allowed_request = apply_filters( 'woocommerce_wishlist_ajax_allowed_request', array(
'wishlist_id' => null,
'product_id' => null,
'do_action' => null
));
$request = wp_parse_args( $_REQUEST, $allowed_request );
switch( $request['do_action'] ){
case 'lookup':
if ( ! $product_id = absint( $request['product_id'] ) ) {
$response = array(
'status' => 'error',
'code' => 501,
'msg' => __( 'You must supply a valid product ID to lookup.', 'woocommerce_wishlist' )
);
} else if ( ! $product = WC_Wishlist_Query::get_product( $product_id ) ) {
$response = array(
'status' => 'error',
'code' => 404,
'msg' => __( 'Product information could not be found by the ID you supplied.', 'woocommerce_wishlist' )
);
} else {
$response = array(
'status'=>'success',
'code' => 100,
'product' => $product
);
}
break;
case 'add':
global $woocommerce;
$wishlist_id = $this->get_wishlist_id();
if ( !empty( $request['product_id'] ) && $this->add_product_to_wishlist( $request['product_id'], null, $request['form'] ) ) {
// Sniff out request to move items from cart to wishlist
$woocommerce->cart->set_quantity( $request['remove_item'], 0 );
$response = $this->ajax_get_products();
} else {
$response = array(
'status'=>'error',
'code' => 501,
'msg' => __( 'The request to add the product to your wishlist failed.' )
);
}
break;
case 'get':
$response = $this->ajax_get_products();
break;
case 'remove':
$wishlist_id = $this->get_wishlist_id();
if ( !empty( $request['product_id'] ) && !empty($wishlist_id) && woocommerce_wishlist_delete_meta( $wishlist_id, $request['product_id'] ) ) {
$response = $this->ajax_get_products();
} else {
$response = array(
'status'=>'error',
'code' => 501,
'msg' => __( 'The request to remove the product from the wishlist failed.' )
);
}
break;
default:
$response = array(
'status'=>'error',
'code' => 503,
'msg' => __( 'The AJAX request is improperly formatted.', 'woocommerce_wishlist' )
);
break;
}
// for debugging pruposes
if( defined('WP_DEBUG') && WP_DEBUG )
$response['request'] = $request;
$response = apply_filters( 'woocommerce_wishlist_ajax_response', wp_parse_args( $response, $defaults ) );
echo json_encode( $response );
die();
}
function ajax_get_products() {
$wishlist = woocommerce_wishlist_get_active_wishlist();
$response = array(
'status' => 'success',
'code' => '100',
'wishlist' => $wishlist,
'products' => WC_Wishlist_Query::get_products( $wishlist->ID )
);
return $response;
}
function add_product_to_wishlist( $product_id, $wishlist_id = null, $attributes = array() ) {
$defaults = array(
'quantity' => 1,
'added' => time()
);
if ( ! $product_id = absint( $product_id ) )
return false;
$wishlist_id = $this->get_wishlist_id( $wishlist_id );
// if no wishlists are returned then let's protect
if ( empty( $wishlist_id ) ) {
// create a wishlist for current user | anon
$wishlist_id = WC_Wishlist_Query::create_wishlist();
}
// set wishlist meta and defaults
$attributes = wp_parse_args( $attributes, $defaults );
foreach ( $attributes as $key => $attribute ) {
woocommerce_wishlist_add_meta( $wishlist_id, $product_id, $key, $attribute );
}
return true;
}
/**
* Attempt to find the right wishlist ID if unknown
*
* will return anon wishlists if userid isn't known
* if the wishlist is provided and not a legit wishlist
* then we try to get the active wishlist
* @param int $wishlist_id
* @return int $wishlist_id
*/
function get_wishlist_id( $wishlist_id = null ){
$wishlist_id = ! empty( $wishlist_id ) && woocommerce_wishlist_is_wishlist( $wishlist_id ) ? $wishlist_id : woocommerce_wishlist_get_active_wishlist();
$wishlist_id = is_object( $wishlist_id ) && !empty( $wishlist_id->ID ) ? $wishlist_id->ID : $wishlist_id;
return apply_filters( 'woocommerce_wishlist_get_wishlist_id', $wishlist_id );
}
/**
* Stores the custom user capability string
*
* @return string $capability
*/
function get_user_capability() {
return 'woocommerce_wishlist_manage';
}
/**
* Setup the wishlist post type
*
* @return void
*/
function register_post_type() {
if ( post_type_exists( self::POST_TYPE ) ) return;
$capability = self::get_user_capability();
$post_type_args = apply_filters( 'woocommerce_wishlist_post_type_args', array(
'labels' => array(
'name' => __( 'WooWishLists', 'woocommerce_wishlist' ),
'singular_name' => __( 'WooWishList', 'woocommerce_wishlist' ),
'menu_name' => _x( 'WooWishLists', 'Admin menu name', 'woocommerce_wishlist' ),
'add_new' => __( 'Add WooWishList', 'woocommerce_wishlist' ),
'add_new_item' => __( 'Add New WooWishList', 'woocommerce_wishlist' ),
'edit' => __( 'Edit', 'woocommerce_wishlist' ),
'edit_item' => __( 'Edit WooWishList', 'woocommerce_wishlist' ),
'new_item' => __( 'New WooWishList', 'woocommerce_wishlist' ),
'view' => __( 'View WooWishList', 'woocommerce_wishlist' ),
'view_item' => __( 'View WooWishList', 'woocommerce_wishlist' ),
'search_items' => __( 'Search WooWishLists', 'woocommerce_wishlist' ),
'not_found' => __( 'No WooWishLists found', 'woocommerce_wishlist' ),
'not_found_in_trash' => __( 'No WooWishLists found in trash', 'woocommerce_wishlist' ),
'parent' => __( 'Parent WooWishList', 'woocommerce_wishlist' )
),
'description' => __( 'This is where you can add new woo-wish-lists to your store.', 'woocommerce_wishlist' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'capabilities' => array(
'publish_posts' => $capability,
'edit_posts' => $capability,
'edit_others_posts' => $capability,
'delete_posts' => $capability,
'delete_others_posts' => $capability,
'read_private_posts' => $capability,
'edit_post' => $capability,
'delete_post' => $capability,
'read_post' => $capability
),
'publicly_queryable' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'rewrite' => array(
'slug' => $this->base_slug,
'with_front' => true ),
'query_var' => true,
'supports' => array( 'title', 'custom-fields', 'author' ),
'has_archive' => false,
'show_in_menu' => false,
'show_in_nav_menus' => false
) );
register_post_type( self::POST_TYPE, $post_type_args );
do_action( 'woocommerce_wishlist_register_post_type' );
}
/**
* Enqueue styles and scripts on the frontend
*
* @return void
*/
function enqueue_assets() {
if ( !is_admin() ) {
wp_enqueue_style( 'woocommerce_wishlist_style', $this->url . 'assets/save-for-later.css', array( 'woocommerce_frontend_styles' ), 1.0, 'screen' );
wp_enqueue_script( 'woocommerce_wishlist_localstorage', $this->url . 'assets/jQuery.localStorage.js', array( 'jquery' ), 1.0, true );
wp_enqueue_script( 'woocommerce_wishlist_script', $this->url . 'assets/save-for-later.js', array( 'woocommerce_wishlist_localstorage' ), 1.0, true );
$localize_script = apply_filters( 'woocommerce_wishlist_localize_script', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'user_status' => is_user_logged_in(),
'css_colors_enabled' => WC_Wishlist_Settings::get_option( 'css_colors_enabled' ),
'css_colors' => WC_Wishlist_Settings::get_option( 'css_colors' ),
'header_show' => sprintf( '%s %s',
__( 'Show' ),
WC_Wishlist_Settings::get_option( 'frontend_label' ) ),
'header_hide' => sprintf( '%s %s',
__( 'Hide' ),
WC_Wishlist_Settings::get_option( 'frontend_label' ) ),
'template' => array(
'product' => WC_Wishlist_Template::dock_product_template(),
'not_found' => WC_Wishlist_Template::not_found()
)
) );
// using localized js namespace
wp_localize_script( 'woocommerce_wishlist_script', 'wc_wishlist_settings' , $localize_script );
}
}
/**
* Display the dock in the footer per options
*
* @return void
*/
function wp_footer() {
$wp_footer_enabled = WC_Wishlist_Settings::get_option( 'wp_footer_enabled' );
if ( $wp_footer_enabled == 'yes' &&
( WC_Wishlist_Settings::get_option( 'store_only' ) == 'no' ||
( WC_Wishlist_Settings::get_option( 'store_only' ) == 'yes' && ( is_WC() || is_cart() ) ) )
) {
// display the wishlist dock
WC_Wishlist_Template::dock();
}
do_action( 'woocommerce_wishlist_wp_footer', $wp_footer_enabled );
}
/**
* Generic SPL autoload registration method to load plugin classes
*
* @param string $class_name to load
* @return void
*/
public static function lazy_loader( $class_name ) {
$file = apply_filters( 'woocommerce_wishlist_lazy_loader', self::get_plugin_path() . 'classes/' . $class_name . '.php', $class_name );
if ( !empty( $file ) && file_exists( $file ) )
require_once $file;
}
/**
* Get the full path of the plugin on the server
*
* @return string
*/
public static function get_plugin_path() {
return trailingslashit( dirname( __FILE__ ) );
}
/**
* Ensure the plugin has everything it needs to run properly
* @return void
*/
public function check_install() {
register_activation_hook( __FILE__, array( 'WC_Wishlist_Install', 'activate' ) );
register_activation_hook( __FILE__, array( 'WC_Wishlist_Install', 'flush_rewrite_rules' ) );
if ( is_admin() && get_option( 'woocommerce_wishlist_db_version' ) != $this->version )
add_action( 'init', array( 'WC_Wishlist_Install', 'install_or_upgrade' ), 1 );
}
/**
* Check the minimum PHP & WP versions
*
* @static
* @return bool Whether the test passed
*/
public static function prerequisites() {;
$pass = TRUE;
// $pass = $pass && defined( WC_VERSION ) && version_compare( WC_VERSION, self::MIN_WC_VERSION, '>=' );
$pass = $pass && version_compare( phpversion(), self::MIN_PHP_VERSION, '>=' );
$pass = $pass && version_compare( get_bloginfo( 'version' ), self::MIN_WP_VERSION, '>=' );
return $pass;
}
public static function min_version_fail_notice() {
echo '<div class="error"><p>';
_e( sprintf( '%s requires the minimum versions of PHP v%s, WordPress v%s, and WooCommerce v%s in order to run properly.',
self::PLUGIN_NAME,
self::MIN_PHP_VERSION,
self::MIN_WP_VERSION,
self::MIN_WC_VERSION
), 'woocommerce_wishlist' );
echo '</p></div>';
}
public static function fail_notice() {
echo '<div class="error"><p>';
_e( sprintf( '%s requires that WooCommerce be active in order to be succesfully activated.',
self::PLUGIN_NAME
), 'woocommerce_wishlist' );
echo '</p></div>';
}
/**
* Static Singleton Factory Method
*
* @return object $instance
*/
public static function instance() {
if ( !isset( self::$instance ) ) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
}
}