| @@ -1,79 +1,93 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace RadiusTheme\SB\Modules\WishList; | |
| 4 | - | |
| 5 | -use RadiusTheme\SB\Helpers\Fns; | |
| 6 | -use RadiusTheme\SB\Models\ExtraSettings; | |
| 7 | -use RadiusTheme\SB\Traits\SingletonTrait; | |
| 8 | - | |
| 9 | -// Do not allow directly accessing this file. | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit( 'This script cannot be accessed directly.' ); | |
| 12 | -} | |
| 13 | - | |
| 14 | -final class Wishlist { | |
| 15 | - | |
| 16 | - const KEY = 'rtsb_wishlist'; | |
| 17 | - | |
| 18 | - private $wishlist_db_key = 'rtsb_wishlist_db_version'; | |
| 19 | - | |
| 20 | - private $wishlist_db_version = '1.0.0'; | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * @var array | |
| 24 | - */ | |
| 25 | - public $settings = []; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * SingleTon | |
| 29 | - */ | |
| 30 | - use SingletonTrait; | |
| 31 | - | |
| 32 | - private function __construct() { | |
| 33 | - | |
| 34 | - if( ! ExtraSettings::instance()->get_option( $this->wishlist_db_key, get_option( $this->wishlist_db_key, false ) ) ){ | |
| 35 | - add_action( 'wp_loaded', [ $this, 'activate' ] ); | |
| 36 | - ExtraSettings::instance()->set_option( $this->wishlist_db_key, $this->wishlist_db_version ); | |
| 37 | - } | |
| 38 | - | |
| 39 | - //if ( ! get_option( $this->wishlist_db_key, '' ) ) { | |
| 40 | - // add_action( 'wp_loaded', [ $this, 'activate' ] ); | |
| 41 | - // update_option( $this->wishlist_db_key, $this->wishlist_db_version ); | |
| 42 | - //} | |
| 43 | - | |
| 44 | - new WishlistRouteV1(); | |
| 45 | - | |
| 46 | - WishlistFrontEnd::instance(); | |
| 47 | - | |
| 48 | - if ( is_user_logged_in() ) { | |
| 49 | - $cookie_name = WishlistFns::instance()->get_cookie_name(); | |
| 50 | - if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) { | |
| 51 | - $ids = WishlistFns::instance()->get_wishlist_ids(); | |
| 52 | - $product_ids = []; | |
| 53 | - foreach ( $ids as $pid ) { | |
| 54 | - $product_ids[ $pid ] = $pid; | |
| 55 | - } | |
| 56 | - WishlistFns::instance()->update_user_wishlist_ids( $product_ids ); | |
| 57 | - setcookie( self::KEY, '', time() - 3600, '/' ); | |
| 58 | - } | |
| 59 | - } | |
| 60 | - | |
| 61 | - | |
| 62 | - if ( is_admin() ) { | |
| 63 | - WishlistAdmin::instance(); | |
| 64 | - } | |
| 65 | - | |
| 66 | - do_action( 'rtsb/module/wishlist/loaded' ); | |
| 67 | - } | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * Do stuff upon plugin activation | |
| 71 | - * | |
| 72 | - * @return void | |
| 73 | - */ | |
| 74 | - public function activate() { | |
| 75 | - ( new WishlistInstaller() )->run(); | |
| 76 | - } | |
| 77 | - | |
| 78 | - | |
| 79 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace RadiusTheme\SB\Modules\WishList; | |
| 4 | + | |
| 5 | +use RadiusTheme\SB\Traits\SingletonTrait; | |
| 6 | + | |
| 7 | +// Do not allow directly accessing this file. | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 'This script cannot be accessed directly.' ); | |
| 10 | +} | |
| 11 | + | |
| 12 | +final class Wishlist { | |
| 13 | + | |
| 14 | + const KEY = 'rtsb_wishlist'; | |
| 15 | + // const WC_MENU_SLUG = 'wishlist'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * @var array | |
| 19 | + */ | |
| 20 | + public $settings = []; | |
| 21 | + | |
| 22 | + //protected $module_id = 'wishlist'; | |
| 23 | + | |
| 24 | + use SingletonTrait; | |
| 25 | + | |
| 26 | + private function __construct() { | |
| 27 | + | |
| 28 | + new WishlistRouteV1(); | |
| 29 | + | |
| 30 | + WishlistFrontEnd::instance(); | |
| 31 | + | |
| 32 | + if ( is_user_logged_in() ) { | |
| 33 | + $cookie_name = WishlistFns::instance()->get_cookie_name(); | |
| 34 | + if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) { | |
| 35 | + | |
| 36 | + $ids = WishlistFns::instance()->get_wishlist_ids(); | |
| 37 | + $product_ids = []; | |
| 38 | + foreach ( $ids as $pid ) { | |
| 39 | + $product_ids[ $pid ] = $pid; | |
| 40 | + } | |
| 41 | + WishlistFns::instance()->update_user_wishlist_ids( $product_ids ); | |
| 42 | + setcookie( self::KEY, '', time() - 3600, '/' ); | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + add_action( | |
| 47 | + 'init', | |
| 48 | + function () { | |
| 49 | + add_rewrite_endpoint( WishlistFns::WC_MENU_SLUG, EP_ROOT | EP_PAGES ); | |
| 50 | + } | |
| 51 | + ); | |
| 52 | + | |
| 53 | + add_filter( 'woocommerce_account_menu_items', [ $this, 'add_to_menu' ], 40 ); | |
| 54 | + add_action( 'woocommerce_account_wishlist_endpoint', [ $this, 'wishlist_content' ] ); | |
| 55 | + add_action( 'woocommerce_get_query_vars', [ $this, 'add_query_vars' ] ); | |
| 56 | + | |
| 57 | + do_action( 'rtsb/module/wishlist/loaded' ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * @param $var | |
| 62 | + * | |
| 63 | + * @return array | |
| 64 | + */ | |
| 65 | + public function add_query_vars( $var ) { | |
| 66 | + $var['wishlist'] = 'wishlist'; | |
| 67 | + return $var; | |
| 68 | + } | |
| 69 | + /** | |
| 70 | + * @param $menu_links | |
| 71 | + * | |
| 72 | + * @return mixed | |
| 73 | + */ | |
| 74 | + public function add_to_menu( $menu_links ) { | |
| 75 | + | |
| 76 | + $menu_links[ WishlistFns::WC_MENU_SLUG ] = esc_html__( 'Wishlist', 'shopbuilder' ); | |
| 77 | + | |
| 78 | + if ( isset( $menu_links['customer-logout'] ) ) { | |
| 79 | + | |
| 80 | + $logout = $menu_links['customer-logout']; | |
| 81 | + unset( $menu_links['customer-logout'] ); | |
| 82 | + $menu_links['customer-logout'] = $logout; | |
| 83 | + } else { | |
| 84 | + $menu_links['customer-logout'] = esc_html__( 'Logout', 'shopbuilder' ); | |
| 85 | + } | |
| 86 | + | |
| 87 | + return $menu_links; | |
| 88 | + } | |
| 89 | + | |
| 90 | + public function wishlist_content() { | |
| 91 | + echo do_shortcode( '[rtsb_wishlist]' ); | |
| 92 | + } | |
| 93 | +} | |