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-form-handler.php
208 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Static class that will handle all form submission from customer |
| 4 | * |
| 5 | * @author YITH <plugins@yithemes.com> |
| 6 | * @package YITH\Wishlist\Classes |
| 7 | * @version 3.0.0 |
| 8 | */ |
| 9 | |
| 10 | if ( ! defined( 'YITH_WCWL' ) ) { |
| 11 | exit; |
| 12 | } // Exit if accessed directly |
| 13 | |
| 14 | if ( ! class_exists( 'YITH_WCWL_Form_Handler' ) ) { |
| 15 | /** |
| 16 | * WooCommerce Wishlist Form Handler |
| 17 | * |
| 18 | * @since 3.0.0 |
| 19 | */ |
| 20 | class YITH_WCWL_Form_Handler { |
| 21 | /** |
| 22 | * Performs all required add_actions to handle forms |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | public static function init() { |
| 27 | /** |
| 28 | * This check was added to prevent bots from accidentaly executing wishlist code |
| 29 | * |
| 30 | * @since 3.0.10 |
| 31 | */ |
| 32 | if ( ! self::process_form_handling() ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // add to wishlist when js is disabled. |
| 37 | add_action( 'init', array( 'YITH_WCWL_Form_Handler', 'add_to_wishlist' ) ); |
| 38 | |
| 39 | // remove from wishlist when js is disabled. |
| 40 | add_action( 'init', array( 'YITH_WCWL_Form_Handler', 'remove_from_wishlist' ) ); |
| 41 | |
| 42 | // remove from wishlist after add to cart. |
| 43 | add_action( 'woocommerce_add_to_cart', array( 'YITH_WCWL_Form_Handler', 'remove_from_wishlist_after_add_to_cart' ) ); |
| 44 | |
| 45 | // change wishlist title. |
| 46 | add_action( 'init', array( 'YITH_WCWL_Form_Handler', 'change_wishlist_title' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Return true if system can process request; false otherwise |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | public static function process_form_handling() { |
| 55 | $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : false; |
| 56 | |
| 57 | /** |
| 58 | * APPLY_FILTERS: yith_wcwl_block_user_agent |
| 59 | * |
| 60 | * Filter the conditions to block some user agents. |
| 61 | * |
| 62 | * @param bool $condition Conditions |
| 63 | * @param string $user_agent User agent |
| 64 | * |
| 65 | * @return bool |
| 66 | */ |
| 67 | if ( $user_agent && apply_filters( 'yith_wcwl_block_user_agent', preg_match( '/bot|crawl|slurp|spider|wordpress/i', $user_agent ), $user_agent ) ) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Adds a product to wishlist when js is disabled |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public static function add_to_wishlist() { |
| 80 | // add item to wishlist when javascript is not enabled. |
| 81 | if ( isset( $_GET['add_to_wishlist'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'add_to_wishlist' ) ) { |
| 82 | try { |
| 83 | yith_wcwl_wishlists()->add_item(yith_wcwl()->get_details()); |
| 84 | |
| 85 | /** |
| 86 | * APPLY_FILTERS: yith_wcwl_product_added_to_wishlist_message |
| 87 | * |
| 88 | * Filter the message shown when an item has been added to the wishlist. |
| 89 | * |
| 90 | * @param string $message Message |
| 91 | * |
| 92 | * @return string |
| 93 | */ |
| 94 | yith_wcwl_add_notice( apply_filters( 'yith_wcwl_product_added_to_wishlist_message', get_option( 'yith_wcwl_product_added_text' ) ), 'success' ); |
| 95 | } catch ( Exception $e ) { |
| 96 | /** |
| 97 | * APPLY_FILTERS: yith_wcwl_error_adding_to_wishlist_message |
| 98 | * |
| 99 | * Filter the error message shown when adding an item to the wishlist. |
| 100 | * |
| 101 | * @param string $message Message |
| 102 | * |
| 103 | * @return string |
| 104 | */ |
| 105 | yith_wcwl_add_notice( apply_filters( 'yith_wcwl_error_adding_to_wishlist_message', $e->getMessage() ), 'error' ); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Removes from wishlist when js is disabled |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | public static function remove_from_wishlist() { |
| 116 | // remove item from wishlist when javascript is not enabled. |
| 117 | if ( isset( $_GET['remove_from_wishlist'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'remove_from_wishlist' ) ) { |
| 118 | try { |
| 119 | yith_wcwl_wishlists()->remove_item( yith_wcwl()->get_details() ); |
| 120 | } catch ( Exception $e ) { |
| 121 | yith_wcwl_add_notice( $e->getMessage(), 'error' ); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Remove from wishlist after adding to cart |
| 128 | * |
| 129 | * @return void |
| 130 | */ |
| 131 | public static function remove_from_wishlist_after_add_to_cart() { |
| 132 | if ( 'yes' !== get_option( 'yith_wcwl_remove_after_add_to_cart' ) ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $args = array(); |
| 137 | |
| 138 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 139 | if ( isset( $_REQUEST['remove_from_wishlist_after_add_to_cart'] ) ) { |
| 140 | |
| 141 | $args['remove_from_wishlist'] = intval( $_REQUEST['remove_from_wishlist_after_add_to_cart'] ); |
| 142 | |
| 143 | if ( isset( $_REQUEST['wishlist_id'] ) ) { |
| 144 | $args['wishlist_id'] = sanitize_text_field( wp_unslash( $_REQUEST['wishlist_id'] ) ); |
| 145 | } |
| 146 | } elseif ( yith_wcwl_is_wishlist() && isset( $_REQUEST['add-to-cart'] ) ) { |
| 147 | $args['remove_from_wishlist'] = intval( $_REQUEST['add-to-cart'] ); |
| 148 | |
| 149 | if ( isset( $_REQUEST['wishlist_id'] ) ) { |
| 150 | $args['wishlist_id'] = sanitize_text_field( wp_unslash( $_REQUEST['wishlist_id'] ) ); |
| 151 | } |
| 152 | } |
| 153 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 154 | |
| 155 | if ( ! empty( $args['wishlist_id'] ) ) { |
| 156 | $wishlist = yith_wcwl_get_wishlist( $args['wishlist_id'] ); |
| 157 | |
| 158 | /** |
| 159 | * APPLY_FILTERS: yith_wcwl_remove_after_add_to_cart |
| 160 | * |
| 161 | * Filter the conditions to allow removing the product from the wishlist after it has been adding to the cart. |
| 162 | * |
| 163 | * @param bool $condition Conditions |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | if ( apply_filters( 'yith_wcwl_remove_after_add_to_cart', $wishlist && $wishlist->is_current_user_owner(), $wishlist ) ) { |
| 168 | try { |
| 169 | yith_wcwl_wishlists()->remove_item( yith_wcwl()->get_details( $args ) ); |
| 170 | } catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 171 | // we were unable to remove item from the wishlist; no follow up is provided. |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Change wishlist title |
| 179 | * |
| 180 | * @return void |
| 181 | * @since 2.0.0 |
| 182 | */ |
| 183 | public static function change_wishlist_title() { |
| 184 | if ( ! isset( $_POST['yith_wcwl_edit_wishlist'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['yith_wcwl_edit_wishlist'] ) ), 'yith_wcwl_edit_wishlist_action' ) || ! isset( $_POST['save_title'] ) || empty( $_POST['wishlist_name'] ) ) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | $wishlist_name = isset( $_POST['wishlist_name'] ) ? sanitize_text_field( wp_unslash( $_POST['wishlist_name'] ) ) : false; |
| 189 | $wishlist_id = isset( $_POST['wishlist_id'] ) ? sanitize_text_field( wp_unslash( $_POST['wishlist_id'] ) ) : false; |
| 190 | $wishlist = yith_wcwl_get_wishlist( $wishlist_id ); |
| 191 | |
| 192 | if ( ! $wishlist_name || strlen( $wishlist_name ) >= 65535 ) { |
| 193 | yith_wcwl_add_notice( __( 'Please, make sure to enter a valid title', 'yith-woocommerce-wishlist' ), 'error' ); |
| 194 | } else { |
| 195 | $wishlist->set_name( $wishlist_name ); |
| 196 | $wishlist->save(); |
| 197 | } |
| 198 | |
| 199 | $redirect_url = isset( $_REQUEST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_REQUEST['redirect_to'] ) ) : $wishlist->get_url(); |
| 200 | |
| 201 | wp_safe_redirect( $redirect_url ); |
| 202 | die; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | YITH_WCWL_Form_Handler::init(); |
| 208 |