| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class defines all code necessary to run during the popup open. |
| 4 |
* |
| 5 |
* @since 1.0.7 |
| 6 |
* @package Woo_Quick_View |
| 7 |
* @subpackage Woo_Quick_View/includes |
| 8 |
* @author ShapedPlugin <support@shapedplugin.com> |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Woo Quick View- popup class |
| 17 |
* |
| 18 |
* @since 1.0 |
| 19 |
*/ |
| 20 |
class SP_WQV_Popup { |
| 21 |
|
| 22 |
/** |
| 23 |
* GetInstance |
| 24 |
* |
| 25 |
* @var SP_WQV_Popup single instance of the class |
| 26 |
* |
| 27 |
* @since 1.0 |
| 28 |
*/ |
| 29 |
protected static $_instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* The popup self instance. |
| 33 |
* |
| 34 |
* @since 1.0 |
| 35 |
* |
| 36 |
* @static |
| 37 |
* |
| 38 |
* @return SP_WQV_Popup |
| 39 |
*/ |
| 40 |
public static function getInstance() { |
| 41 |
if ( ! self::$_instance ) { |
| 42 |
self::$_instance = new self(); |
| 43 |
} |
| 44 |
|
| 45 |
return self::$_instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize the class |
| 50 |
*/ |
| 51 |
public function __construct() { |
| 52 |
add_action( 'wp_ajax_wqv_popup_content', array( $this, 'wqv_popup_content' ) ); |
| 53 |
add_action( 'wp_ajax_nopriv_wqv_popup_content', array( $this, 'wqv_popup_content' ) ); |
| 54 |
|
| 55 |
add_action( 'wc_ajax_sp_ajax_add_cart', array( $this, 'ql_woocommerce_ajax_add_to_cart' ) ); |
| 56 |
|
| 57 |
$plugin_setting = get_option( '_sp_wqvpro_options' ); |
| 58 |
$show_product_content = isset( $plugin_setting['wqvpro_show_product_content'] ) ? $plugin_setting['wqvpro_show_product_content'] : ''; |
| 59 |
$show_title = isset( $show_product_content['title'] ) ? $show_product_content['title'] : true; |
| 60 |
$show_price = isset( $show_product_content['price'] ) ? $show_product_content['price'] : true; |
| 61 |
$show_excerpt = isset( $show_product_content['excerpt'] ) ? $show_product_content['excerpt'] : true; |
| 62 |
$show_add_to_cart = isset( $show_product_content['add_to_cart'] ) ? $show_product_content['add_to_cart'] : true; |
| 63 |
$show_rating = isset( $show_product_content['rating'] ) ? $show_product_content['rating'] : true; |
| 64 |
$show_meta = isset( $show_product_content['meta'] ) ? $show_product_content['meta'] : true; |
| 65 |
if ( $show_title ) { |
| 66 |
add_action( 'wqv_product_content', 'woocommerce_template_single_title', 5 ); |
| 67 |
} |
| 68 |
if ( $show_rating ) { |
| 69 |
add_action( 'wqv_product_content', 'woocommerce_template_single_rating', 10 ); |
| 70 |
} |
| 71 |
if ( $show_price ) { |
| 72 |
add_action( 'wqv_product_content', 'woocommerce_template_single_price', 15 ); |
| 73 |
} |
| 74 |
if ( $show_excerpt ) { |
| 75 |
add_action( 'wqv_product_content', 'woocommerce_template_single_excerpt', 20 ); |
| 76 |
} |
| 77 |
if ( $show_add_to_cart ) { |
| 78 |
add_action( 'wqv_product_content', 'woocommerce_template_single_add_to_cart', 25 ); |
| 79 |
} |
| 80 |
if ( $show_meta ) { |
| 81 |
add_action( 'wqv_product_content', 'woocommerce_template_single_meta', 30 ); |
| 82 |
} |
| 83 |
$redirect_after_cart = isset( $plugin_setting['wqvpro_redirect_after_cart'] ) ? $plugin_setting['wqvpro_redirect_after_cart'] : ''; |
| 84 |
if ( 'same_page' === $redirect_after_cart ) { |
| 85 |
add_filter( 'woocommerce_add_to_cart_form_action', array( $this, 'avoid_redirect_to_single_page' ), 10, 1 ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Check if is quick view |
| 91 |
* |
| 92 |
* @access public |
| 93 |
* @since 1.3.1 |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function wqv_is_quick_view() { |
| 97 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 98 |
return ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && 'wqv_popup_content' === $_REQUEST['action'] ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Avoid redirect to single product page on add to cart action in quick view |
| 103 |
* |
| 104 |
* @since 1.3.1 |
| 105 |
* @param string $value The redirect url value. |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public function avoid_redirect_to_single_page( $value ) { |
| 109 |
if ( $this->wqv_is_quick_view() ) { |
| 110 |
return ''; |
| 111 |
} |
| 112 |
return $value; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Handles AJAX request for adding a product to the cart in WooCommerce. |
| 117 |
* |
| 118 |
* This function processes an AJAX request to add a product to the cart. It retrieves |
| 119 |
* the product ID from the request parameters and triggers an action related to the |
| 120 |
* added product. It also refreshes the cart fragments to update the cart display. |
| 121 |
*/ |
| 122 |
public function ql_woocommerce_ajax_add_to_cart() { |
| 123 |
// Disable nonce check for read-only data. |
| 124 |
// No need to use nonce for read-only data. |
| 125 |
|
| 126 |
// Initialize the product ID variable. |
| 127 |
$product_id = 0; |
| 128 |
|
| 129 |
// Retrieve the product ID from the 'product_id' parameter. |
| 130 |
if ( ! empty( $_POST['product_id'] ) ) { // phpcs:ignore -- WordPress.Security.NonceVerification.Recommended |
| 131 |
$product_id = apply_filters( 'ql_woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) ); // phpcs:ignore -- WordPress.Security.NonceVerification.Recommended |
| 132 |
} |
| 133 |
|
| 134 |
// If product ID is still not set, retrieve it from the 'add-to-cart' parameter. |
| 135 |
if ( empty( $product_id ) && ! empty( $_POST['add-to-cart'] ) ) { // phpcs:ignore |
| 136 |
$product_id = apply_filters( 'ql_woocommerce_add_to_cart_product_id', absint( $_POST['add-to-cart'] ) ); // phpcs:ignore -- WordPress.Security.NonceVerification.Recommended |
| 137 |
} |
| 138 |
|
| 139 |
// Trigger the 'ql_woocommerce_ajax_added_to_cart' action if product ID is set. |
| 140 |
if ( ! empty( $product_id ) ) { |
| 141 |
do_action( 'ql_woocommerce_ajax_added_to_cart', $product_id ); |
| 142 |
} |
| 143 |
|
| 144 |
// Refresh the cart fragments to update the cart display. |
| 145 |
WC_Ajax::get_refreshed_fragments(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Popup Content |
| 150 |
* |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function wqv_popup_content() { |
| 154 |
global $post, $product; |
| 155 |
|
| 156 |
// Validate product_id. |
| 157 |
$product_id = isset( $_GET['product_id'] ) ? absint( $_GET['product_id'] ) : 0; // phpcs:ignore -- Security check: input var ok. |
| 158 |
|
| 159 |
if ( ! $product_id ) { |
| 160 |
wp_die( 'Not Found', 'Not Found', array( 'response' => 404 ) ); |
| 161 |
} |
| 162 |
|
| 163 |
$post = get_post( $product_id ); |
| 164 |
|
| 165 |
// Validate post and post type and password protection. |
| 166 |
if ( ! $post || 'publish' !== $post->post_status || 'product' !== $post->post_type || post_password_required( $post ) ) { |
| 167 |
wp_die( 'Not Found', 'Not Found', array( 'response' => 404 ) ); |
| 168 |
} |
| 169 |
|
| 170 |
setup_postdata( $post ); |
| 171 |
|
| 172 |
$post_id = $post->ID; |
| 173 |
$product = wc_get_product( $post_id ); |
| 174 |
|
| 175 |
// Validate product visibility. |
| 176 |
if ( 'hidden' === $product->get_catalog_visibility() ) { |
| 177 |
wp_die( 'Not Found', 'Not Found', array( 'response' => 404 ) ); |
| 178 |
} |
| 179 |
|
| 180 |
$thumb_ids = array(); |
| 181 |
$image_ids = $product->get_gallery_image_ids(); |
| 182 |
|
| 183 |
$thumb_ids = array_merge( $thumb_ids, $image_ids ); |
| 184 |
$plugin_setting = get_option( '_sp_wqvpro_options' ); |
| 185 |
$image_lightbox = isset( $plugin_setting['wqvpro_product_image_lightbox'] ) ? $plugin_setting['wqvpro_product_image_lightbox'] : false; |
| 186 |
|
| 187 |
?> |
| 188 |
|
| 189 |
<div id="wqv-quick-view-content" class="mfp-with-anim sp-wqv-content sp-wqv-content-<?php echo esc_attr( get_the_id() ); ?>" data-id="<?php echo esc_attr( get_the_id() ); ?>"> |
| 190 |
|
| 191 |
<div class="wqv-product-images" data-attachments="<?php echo count( $thumb_ids ); ?>"> |
| 192 |
<div class="wqv-product-images-slider"> |
| 193 |
<?php |
| 194 |
if ( has_post_thumbnail( $post->ID ) ) { |
| 195 |
$attachment_id = get_post_thumbnail_id(); |
| 196 |
$image_title = get_the_title( $attachment_id ); |
| 197 |
$props = wc_get_product_attachment_props( $attachment_id, $post ); |
| 198 |
if ( $image_lightbox ) { |
| 199 |
$lightbox_start = '<a data-fancybox="wqvpro-gallery" title="' . esc_attr( $image_title ) . '" " href="' . esc_url( $props['url'] ) . '">'; |
| 200 |
$lightbox_end = '</a>'; |
| 201 |
} else { |
| 202 |
$lightbox_start = ''; |
| 203 |
$lightbox_end = ''; |
| 204 |
} |
| 205 |
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), 0, $props ); |
| 206 |
$thumb_image = wp_get_attachment_image_src( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ), 0 ); |
| 207 |
$thumb_image = is_array( $thumb_image ) ? $thumb_image : array( '' ); |
| 208 |
echo apply_filters( // phpcs:ignore |
| 209 |
'woocommerce_single_product_image_html', |
| 210 |
sprintf( |
| 211 |
'<span data-thumb="%s" class="selected" itemprop="image" title="%s">%s%s%s</span>', |
| 212 |
$thumb_image[0], |
| 213 |
esc_attr( $props['caption'] ), |
| 214 |
$lightbox_start, |
| 215 |
$image, |
| 216 |
$lightbox_end |
| 217 |
), |
| 218 |
$post->ID |
| 219 |
); |
| 220 |
|
| 221 |
} else { |
| 222 |
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<span data-thumb="%s" class="selected" itemprop="image"><img src="%s" alt="%s" /></span>', wc_placeholder_img_src(), wc_placeholder_img_src(), __( 'Placeholder', 'woo-quickview' ) ), $post->ID ); // phpcs:ignore |
| 223 |
} |
| 224 |
?> |
| 225 |
</div> <!-- wqv-product-images-slider --> |
| 226 |
|
| 227 |
</div> |
| 228 |
|
| 229 |
<div class="wqv-product-info"> |
| 230 |
<div class="wqv-product-content"> |
| 231 |
|
| 232 |
<?php do_action( 'wqv_product_content', $post_id ); ?> |
| 233 |
|
| 234 |
</div> |
| 235 |
</div> |
| 236 |
|
| 237 |
</div> |
| 238 |
<?php |
| 239 |
wp_reset_postdata(); |
| 240 |
|
| 241 |
// End the request properly. |
| 242 |
wp_die(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
new SP_WQV_Popup(); |
| 247 |
|