abstracts
1 year ago
admin
1 year ago
data-stores
1 year ago
legacy
1 year ago
rest-api
1 year ago
traits
1 year ago
widgets
2 years ago
abstract-yith-wcwl-db.php
1 year ago
class-yith-wcwl-add-to-wishlist-button.php
1 year ago
class-yith-wcwl-ajax-handler.php
1 year ago
class-yith-wcwl-autoloader.php
1 year ago
class-yith-wcwl-cron.php
1 year ago
class-yith-wcwl-exception.php
1 year ago
class-yith-wcwl-form-handler.php
1 year ago
class-yith-wcwl-frontend.php
1 year ago
class-yith-wcwl-install.php
1 year ago
class-yith-wcwl-privacy.php
1 year ago
class-yith-wcwl-rendering-method-frontend-handler.php
1 year ago
class-yith-wcwl-session.php
1 year ago
class-yith-wcwl-shortcode.php
1 year ago
class-yith-wcwl-wishlist-factory.php
1 year ago
class-yith-wcwl-wishlist-item.php
1 year ago
class-yith-wcwl-wishlist.php
1 year ago
class-yith-wcwl-wishlists.php
1 year ago
class-yith-wcwl.php
1 year ago
functions-yith-wcwl-update.php
1 year ago
functions-yith-wcwl.php
1 year ago
class-yith-wcwl.php
475 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Main class |
| 4 | * |
| 5 | * @package YITH\Wishlist\Classes |
| 6 | * @author YITH <plugins@yithemes.com> |
| 7 | * @version 3.0.0 |
| 8 | */ |
| 9 | |
| 10 | defined( 'YITH_WCWL' ) || exit; // Exit if accessed directly |
| 11 | |
| 12 | if ( ! class_exists( 'YITH_WCWL' ) ) { |
| 13 | /** |
| 14 | * WooCommerce Wishlist |
| 15 | * |
| 16 | * @since 1.0.0 |
| 17 | */ |
| 18 | class YITH_WCWL extends YITH_WCWL_Legacy { |
| 19 | use YITH_WCWL_Extensible_Singleton_Trait; |
| 20 | |
| 21 | /** |
| 22 | * Admin class |
| 23 | * |
| 24 | * @var YITH_WCWL_Admin |
| 25 | */ |
| 26 | protected $wcwl_admin; |
| 27 | |
| 28 | /** |
| 29 | * Frontend class |
| 30 | * |
| 31 | * @var YITH_WCWL_Frontend |
| 32 | */ |
| 33 | protected $wcwl_frontend; |
| 34 | |
| 35 | /** |
| 36 | * Cron class |
| 37 | * |
| 38 | * @var YITH_WCWL_Cron |
| 39 | */ |
| 40 | protected $wcwl_cron; |
| 41 | |
| 42 | /** |
| 43 | * Session class |
| 44 | * |
| 45 | * @var YITH_WCWL_Session |
| 46 | */ |
| 47 | protected $wcwl_session; |
| 48 | |
| 49 | /** |
| 50 | * Emails |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | public $emails = array(); |
| 55 | |
| 56 | /** |
| 57 | * Last operation token |
| 58 | * |
| 59 | * @since 2.0.0 |
| 60 | * @var string |
| 61 | */ |
| 62 | public $last_operation_token; |
| 63 | |
| 64 | /** |
| 65 | * Query string parameter used to generate Wishlist urls |
| 66 | * |
| 67 | * @since 2.1.2 |
| 68 | * @var string |
| 69 | */ |
| 70 | public $wishlist_param = 'wishlist-action'; |
| 71 | |
| 72 | /** |
| 73 | * Constructor. |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | public function __construct() { |
| 78 | parent::__construct(); |
| 79 | |
| 80 | // register data stores. |
| 81 | add_filter( 'woocommerce_data_stores', array( $this, 'register_data_stores' ) ); |
| 82 | |
| 83 | // init frontend class. |
| 84 | $this->wcwl_frontend = YITH_WCWL_Frontend::get_instance(); |
| 85 | |
| 86 | // init crons. |
| 87 | $this->wcwl_cron = YITH_WCWL_Cron::get_instance(); |
| 88 | |
| 89 | // init session. |
| 90 | $this->wcwl_session = YITH_WCWL_Session::get_instance(); |
| 91 | |
| 92 | // init admin handling. |
| 93 | if ( is_admin() ) { |
| 94 | $this->wcwl_admin = YITH_WCWL_Admin::get_instance(); |
| 95 | } |
| 96 | |
| 97 | YITH_WCWL_Install::get_instance(); |
| 98 | YITH_WCWL_Rest_Server::get_instance(); |
| 99 | |
| 100 | // load plugin-fw. |
| 101 | add_action( 'plugins_loaded', array( $this, 'plugin_fw_loader' ), 15 ); |
| 102 | add_action( 'plugins_loaded', array( $this, 'privacy_loader' ), 20 ); |
| 103 | |
| 104 | // add rewrite rule. |
| 105 | add_action( 'init', array( $this, 'add_rewrite_rules' ), 0 ); |
| 106 | add_filter( 'query_vars', array( $this, 'add_public_query_var' ) ); |
| 107 | |
| 108 | // Polylang integration. |
| 109 | add_filter( 'pll_translation_url', array( $this, 'get_pll_wishlist_url' ), 10, 1 ); |
| 110 | |
| 111 | add_action( 'before_woocommerce_init', array( $this, 'declare_wc_features_support' ) ); |
| 112 | } |
| 113 | |
| 114 | /* === PLUGIN FW LOADER === */ |
| 115 | |
| 116 | /** |
| 117 | * Loads plugin fw, if not yet created |
| 118 | * |
| 119 | * @return void |
| 120 | * @since 2.0.0 |
| 121 | */ |
| 122 | public function plugin_fw_loader() { |
| 123 | if ( ! defined( 'YIT_CORE_PLUGIN' ) ) { |
| 124 | global $plugin_fw_data; |
| 125 | if ( ! empty( $plugin_fw_data ) ) { |
| 126 | $plugin_fw_file = array_shift( $plugin_fw_data ); |
| 127 | require_once $plugin_fw_file; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* === PRIVACY LOADER === */ |
| 133 | |
| 134 | /** |
| 135 | * Loads privacy class |
| 136 | * |
| 137 | * @return void |
| 138 | * @since 2.0.0 |
| 139 | */ |
| 140 | public function privacy_loader() { |
| 141 | if ( class_exists( 'YITH_Privacy_Plugin_Abstract' ) ) { |
| 142 | require_once YITH_WCWL_INC . 'class-yith-wcwl-privacy.php'; |
| 143 | new YITH_WCWL_Privacy(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* === GENERAL METHODS === */ |
| 148 | |
| 149 | /** |
| 150 | * Checks whether current user can add to the wishlist |
| 151 | * |
| 152 | * TODO: merge this into \YITH_WCWL_Wishlist::current_user_can |
| 153 | * |
| 154 | * @param int|bool $user_id User id to test; false to use current user id. |
| 155 | * @return bool Whether current user can add to wishlist |
| 156 | * @since 3.0.0 |
| 157 | */ |
| 158 | public function can_user_add_to_wishlist( $user_id = false ) { |
| 159 | $user_id = $user_id ? $user_id : get_current_user_id(); |
| 160 | $disable_wishlist_for_unauthenticated_users = get_option( 'yith_wcwl_disable_wishlist_for_unauthenticated_users' ); |
| 161 | $return = true; |
| 162 | |
| 163 | if ( 'yes' === $disable_wishlist_for_unauthenticated_users && ! $user_id ) { |
| 164 | $return = false; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * APPLY_FILTERS: yith_wcwl_can_user_add_to_wishlist |
| 169 | * |
| 170 | * Filter whether the current user can add products to the wishlist. |
| 171 | * |
| 172 | * @param bool $can_add_to_wishlist Whether the product is already in the wishlist |
| 173 | * @param int $user_id User ID |
| 174 | * |
| 175 | * @return bool |
| 176 | */ |
| 177 | return apply_filters( 'yith_wcwl_can_user_add_to_wishlist', $return, $user_id ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Register custom plugin Data Stores classes |
| 182 | * |
| 183 | * @param array $data_stores Array of registered data stores. |
| 184 | * @return array Array of filtered data store |
| 185 | */ |
| 186 | public function register_data_stores( $data_stores ) { |
| 187 | $data_stores[ 'wishlist' ] = 'YITH_WCWL_Wishlist_Data_Store'; |
| 188 | $data_stores[ 'wishlist-item' ] = 'YITH_WCWL_Wishlist_Item_Data_Store'; |
| 189 | |
| 190 | return $data_stores; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Add rewrite rules for wishlist |
| 195 | * |
| 196 | * @return void |
| 197 | * @since 2.0.0 |
| 198 | */ |
| 199 | public function add_rewrite_rules() { |
| 200 | global $wp_query; |
| 201 | |
| 202 | // filter wishlist param. |
| 203 | /** |
| 204 | * APPLY_FILTERS: yith_wcwl_wishlist_param |
| 205 | * |
| 206 | * Filter the wishlist param. |
| 207 | * |
| 208 | * @param string $wishlist_param Wishlist param |
| 209 | * |
| 210 | * @return string |
| 211 | */ |
| 212 | $this->wishlist_param = apply_filters( 'yith_wcwl_wishlist_param', $this->wishlist_param ); |
| 213 | |
| 214 | $wishlist_page_id = get_option( 'yith_wcwl_wishlist_page_id' ); |
| 215 | $wishlist_page_id = yith_wcwl_object_id( $wishlist_page_id, 'page', true, 'default' ); |
| 216 | |
| 217 | if ( empty( $wishlist_page_id ) ) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | $wishlist_page = get_post( $wishlist_page_id ); |
| 222 | $wishlist_page_slug = $wishlist_page ? $wishlist_page->post_name : false; |
| 223 | |
| 224 | if ( empty( $wishlist_page_slug ) ) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if ( defined( 'POLYLANG_VERSION' ) || defined( 'ICL_PLUGIN_PATH' ) ) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | $regex_paged = '((?!wp-json/.*)([^/]+/)*' . urldecode( $wishlist_page_slug ) . ')(/(.*))?/page/([0-9]{1,})/?$'; |
| 233 | $regex_simple = '((?!wp-json/.*)([^/]+/)*' . urldecode( $wishlist_page_slug ) . ')(/(.*))?/?$'; |
| 234 | |
| 235 | add_rewrite_rule( $regex_paged, 'index.php?pagename=$matches[1]&' . $this->wishlist_param . '=$matches[4]&paged=$matches[5]', 'top' ); |
| 236 | add_rewrite_rule( $regex_simple, 'index.php?pagename=$matches[1]&' . $this->wishlist_param . '=$matches[4]', 'top' ); |
| 237 | |
| 238 | $rewrite_rules = get_option( 'rewrite_rules' ); |
| 239 | |
| 240 | if ( ! is_array( $rewrite_rules ) || ! array_key_exists( $regex_paged, $rewrite_rules ) || ! array_key_exists( $regex_simple, $rewrite_rules ) ) { |
| 241 | flush_rewrite_rules(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Adds public query var for wishlist |
| 247 | * |
| 248 | * @param array $public_var Array of available query vars. |
| 249 | * @return array |
| 250 | * @since 2.0.0 |
| 251 | */ |
| 252 | public function add_public_query_var( $public_var ) { |
| 253 | $public_var[] = $this->wishlist_param; |
| 254 | $public_var[] = 'wishlist_id'; |
| 255 | |
| 256 | return $public_var; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Return wishlist page id, if any |
| 261 | * |
| 262 | * @return int Wishlist page id. |
| 263 | */ |
| 264 | public function get_wishlist_page_id() { |
| 265 | $wishlist_page_id = get_option( 'yith_wcwl_wishlist_page_id' ); |
| 266 | $wishlist_page_id = yith_wcwl_object_id( $wishlist_page_id ); |
| 267 | |
| 268 | /** |
| 269 | * APPLY_FILTERS: yith_wcwl_wishlist_page_id |
| 270 | * |
| 271 | * Filter the wishlist page ID. |
| 272 | * |
| 273 | * @param int $wishlist_page_id Wishlist page ID |
| 274 | * |
| 275 | * @return int |
| 276 | */ |
| 277 | return (int) apply_filters( 'yith_wcwl_wishlist_page_id', $wishlist_page_id ); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Build wishlist page URL. |
| 282 | * |
| 283 | * @param string $action Action string to use in the url. |
| 284 | * |
| 285 | * @return string |
| 286 | * @since 1.0.0 |
| 287 | */ |
| 288 | public function get_wishlist_url( $action = '' ) { |
| 289 | global $sitepress; |
| 290 | $wishlist_page_id = $this->get_wishlist_page_id(); |
| 291 | $wishlist_permalink = get_the_permalink( $wishlist_page_id ); |
| 292 | |
| 293 | $action_params = explode( '/', $action ); |
| 294 | $view = $action_params[ 0 ]; |
| 295 | $data = isset( $action_params[ 1 ] ) ? $action_params[ 1 ] : ''; |
| 296 | |
| 297 | if ( 'view' === $action && empty( $data ) ) { |
| 298 | return $wishlist_permalink; |
| 299 | } |
| 300 | |
| 301 | if ( get_option( 'permalink_structure' ) && ! defined( 'ICL_PLUGIN_PATH' ) && ! defined( 'POLYLANG_VERSION' ) ) { |
| 302 | $wishlist_permalink = trailingslashit( $wishlist_permalink ); |
| 303 | $base_url = trailingslashit( $wishlist_permalink . $action ); |
| 304 | } else { |
| 305 | $base_url = $wishlist_permalink; |
| 306 | $params = array(); |
| 307 | |
| 308 | if ( ! empty( $data ) ) { |
| 309 | $params[ $this->wishlist_param ] = $view; |
| 310 | |
| 311 | if ( 'view' === $view ) { |
| 312 | $params[ 'wishlist_id' ] = $data; |
| 313 | } elseif ( 'user' === $view ) { |
| 314 | $params[ 'user_id' ] = $data; |
| 315 | } |
| 316 | } else { |
| 317 | $params[ $this->wishlist_param ] = $view; |
| 318 | } |
| 319 | |
| 320 | $base_url = add_query_arg( $params, $base_url ); |
| 321 | } |
| 322 | |
| 323 | if ( defined( 'ICL_PLUGIN_PATH' ) && $sitepress->get_current_language() !== $sitepress->get_default_language() ) { |
| 324 | $base_url = add_query_arg( 'lang', $sitepress->get_current_language(), $base_url ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * APPLY_FILTERS: yith_wcwl_wishlist_page_url |
| 329 | * |
| 330 | * Filter the wishlist page URL. |
| 331 | * |
| 332 | * @param string $wishlist_page_url Wishlist page URL |
| 333 | * @param string $action Action |
| 334 | * |
| 335 | * @return string |
| 336 | */ |
| 337 | return apply_filters( 'yith_wcwl_wishlist_page_url', esc_url_raw( $base_url ), $action ); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Retrieve url for the wishlist that was affected by last operation |
| 342 | * |
| 343 | * @return string Url to view last operation wishlist |
| 344 | */ |
| 345 | public function get_last_operation_url() { |
| 346 | $action = 'view'; |
| 347 | |
| 348 | if ( ! empty( $this->last_operation_token ) ) { |
| 349 | $action .= "/{$this->last_operation_token}"; |
| 350 | } |
| 351 | |
| 352 | return $this->get_wishlist_url( $action ); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Generates Add to Wishlist url, to use when customer do not have js enabled |
| 357 | * |
| 358 | * @param int $product_id Product id to add to wishlist. |
| 359 | * @param array $args Any of the following parameters |
| 360 | * [ |
| 361 | * 'base_url' => '' |
| 362 | * 'wishlist_id' => 0, |
| 363 | * 'quantity' => 1, |
| 364 | * 'user_id' => false, |
| 365 | * 'dateadded' => '', |
| 366 | * 'wishlist_name' => '', |
| 367 | * 'wishlist_visibility' => 0 |
| 368 | * ]. |
| 369 | * @return string Add to wishlist url |
| 370 | */ |
| 371 | public function get_add_to_wishlist_url( $product_id, $args = array() ) { |
| 372 | $args = array_merge( |
| 373 | array( |
| 374 | 'add_to_wishlist' => $product_id, |
| 375 | ), |
| 376 | $args |
| 377 | ); |
| 378 | |
| 379 | if ( isset( $args[ 'base_url' ] ) ) { |
| 380 | $base_url = $args[ 'base_url' ]; |
| 381 | unset( $args[ 'base_url' ] ); |
| 382 | |
| 383 | $url = add_query_arg( $args, $base_url ); |
| 384 | } else { |
| 385 | $url = add_query_arg( $args ); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * APPLY_FILTERS: yith_wcwl_add_to_wishlist_url |
| 390 | * |
| 391 | * Filter the URL to add products to the wishlist. |
| 392 | * |
| 393 | * @param string $url URL to add to wishlist |
| 394 | * @param int $product_id Product ID |
| 395 | * @param array $args Array of parameters |
| 396 | * |
| 397 | * @return string |
| 398 | */ |
| 399 | return apply_filters( 'yith_wcwl_add_to_wishlist_url', esc_url_raw( wp_nonce_url( $url, 'add_to_wishlist' ) ), $product_id, $args ); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Build the URL used to remove an item from the wishlist. |
| 404 | * |
| 405 | * @param int $item_id Id of the item to remove. |
| 406 | * @return string |
| 407 | * @since 1.0.0 |
| 408 | */ |
| 409 | public function get_remove_url( $item_id ) { |
| 410 | return esc_url( wp_nonce_url( add_query_arg( 'remove_from_wishlist', $item_id ), 'remove_from_wishlist' ) ); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Returns available views for wishlist page |
| 415 | * |
| 416 | * @return string[] |
| 417 | * @since 3.0.0 |
| 418 | */ |
| 419 | public function get_available_views() { |
| 420 | /** |
| 421 | * APPLY_FILTERS: yith_wcwl_available_wishlist_views |
| 422 | * |
| 423 | * Filter the available views in the wishlist page. |
| 424 | * |
| 425 | * @param array $views Available views |
| 426 | * |
| 427 | * @return array |
| 428 | */ |
| 429 | $available_views = apply_filters( 'yith_wcwl_available_wishlist_views', array( 'view', 'user' ) ); |
| 430 | return $available_views; |
| 431 | } |
| 432 | |
| 433 | /* === POLYLANG INTEGRATION === */ |
| 434 | |
| 435 | /** |
| 436 | * Filters translation url for the wishlist page, when PolyLang is enabled |
| 437 | * |
| 438 | * @param string $url Translation url. |
| 439 | * @return string Filtered translation url for current page/post. |
| 440 | */ |
| 441 | public function get_pll_wishlist_url( $url ) { |
| 442 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 443 | if ( yith_wcwl_is_wishlist_page() && isset( $_GET[ $this->wishlist_param ] ) ) { |
| 444 | $wishlist_action = sanitize_text_field( wp_unslash( $_GET[ $this->wishlist_param ] ) ); |
| 445 | $user_id = isset( $_GET[ 'user_id' ] ) ? sanitize_text_field( wp_unslash( $_GET[ 'user_id' ] ) ) : ''; |
| 446 | $wishlist_id = isset( $_GET[ 'wishlist_id' ] ) ? sanitize_text_field( wp_unslash( $_GET[ 'wishlist_id' ] ) ) : ''; |
| 447 | |
| 448 | $params = array_filter( |
| 449 | array( |
| 450 | $this->wishlist_param => $wishlist_action, |
| 451 | 'user_id' => $user_id, |
| 452 | 'wishlist_id' => $wishlist_id, |
| 453 | ) |
| 454 | ); |
| 455 | |
| 456 | $url = add_query_arg( $params, $url ); |
| 457 | } |
| 458 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 459 | |
| 460 | return $url; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Declare support for WooCommerce features. |
| 465 | * |
| 466 | * @since 3.22.0 |
| 467 | */ |
| 468 | public function declare_wc_features_support() { |
| 469 | if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) { |
| 470 | \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', YITH_WCWL_INIT, true ); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 |