-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
288 lines (237 loc) · 9.02 KB
/
plugin.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
<?php
if (!defined('ABSPATH')) exit;
/**
* View button for actions
*
* @since 1.0.0
* @package ca-quick-view
* @author Saiful <[email protected]>
*/
class CAWQV_FRONTEND
{
function __construct(){
add_action('wp_ajax_get_product', array($this,'get_product_callback'));
// If you want not logged in users to be allowed to use this function as well, register it again with this function:
add_action('wp_ajax_nopriv_get_product', array($this,'get_product_callback'));
// add_action('wp_footer', array($this,'add_ajax_script'));
add_action('wp_footer', array($this,'modal'));
add_action('woocommerce_after_shop_loop_item', array($this,'modal_button'));
//WooCommerce Hook
add_action('cawqv_show_product_sale_flash', 'woocommerce_show_product_sale_flash');
add_action('cawqv_product_content', 'woocommerce_template_single_title', 5);
add_action('cawqv_product_content', 'woocommerce_template_single_rating', 10);
add_action('cawqv_product_content', 'woocommerce_template_single_price', 15);
add_action('cawqv_product_content', 'woocommerce_template_single_excerpt', 20);
add_action('cawqv_product_content', 'woocommerce_template_single_add_to_cart', 25);
add_action('cawqv_product_content', 'woocommerce_template_single_meta', 30);
//Action for Add to cart
add_action('wp_enqueue_scripts', array( $this,'woocommerce_ajax_add_to_cart_js'));
add_action('wp_ajax_woocommerce_ajax_add_to_cart', array($this,'woocommerce_ajax_add_to_cart'));
add_action('wp_ajax_nopriv_woocommerce_ajax_add_to_cart', array($this,'woocommerce_ajax_add_to_cart'));
add_action('cawqv_view_product_image', array($this,'cawqv_image') , 20);
//add_action( $hook, $function_to_add, $priority, $accepted_args );
add_action('cawqv_product_gallery', array($this,'product_gallery') , 'class', 2);
}
/**
* View button for actions
*
* @since 1.0.0
*/
public function modal_button(){
global $product;
$product_id = $product->get_id();
$qv_label = __( 'Quick View', 'cawqv' );
$qv_button_label = get_option('qv_button_label', $qv_label ) ;
$qv_button_label = __( $qv_button_label, 'cawqv' );
?>
<button type="button" class="caqv-open-modal"
data-id="<?php echo esc_attr($product_id); ?>" >
<i class="btn-icon <?php echo esc_attr(get_option('cawqv_general_section', 'cawqv-icon-search')); ?>"></i>
<span><?php echo esc_html($qv_button_label, 'cawqv') ?> </span>
</button>
<?php
}
/**
* Get ID and content by Ajax
*
* @since 1.0.0
*/
public function get_product_callback(){
// retrieve post_id, and sanitize it to enhance security
if (!isset($_POST['id'])){
die();
}
$product_id = intval($_POST['id']);
wp('p=' . $product_id . '&post_type=product');
?>
<?php
// $template = CAWQV_DIR . '/templates/';
// ob_start();
// wc_get_template( 'view-ontent.php', array(), '', $template );
// $html = ob_get_contents(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
// ob_end_clean();
// echo $html;
// die();
ob_start();
include_once ('includes/content.php');
echo ob_get_clean();
// Must die() the function
die();
}
/**
* Prpeared image content
*
* @since 1.0.0
*/
public static function cawqv_image(){
include_once ('includes/image-content.php');
}
/**
* The modal body
*
* @since 1.0.0
*/
public function modal(){
$icon = get_option('cawqv_icon_picker', 'cawqv-icon-close');
$close_container = ($icon == 'default') ? '<div class="remodal-modal-close">×</div>' : '<div class="remodal-modal-close"><i class="' . $icon . '"></i></div>';
$output = '<div id="cawqv-modal" class="cawqv-modal" style="display:none">';
$output .= $close_container;
$output .= '<div id="modal_container"></div>';
$output .= '</div>';
$allowed_html = array(
'div' => array(
'id' => array(),
'style' => array(),
'class' => array(),
),
'i' => array(
'class' => array(),
),
);
// echo wp_kses_post( $output ,$allowed_html );
echo wp_kses( $output ,$allowed_html );
}
/**
* Ajax Add to cart script enqueue
*
* @since 1.0.0
*/
public function woocommerce_ajax_add_to_cart_js(){
if (function_exists('is_product') && is_product()){
wp_enqueue_script('woocommerce-ajax-add-to-cart', plugin_dir_url(__FILE__) . 'assets/js/woo-ajax-add-to-cart.js', array(
'jquery'
) , '', true);
}
}
/**
* Ajax Add to cart actions
*
* @since 1.0.0
*/
public function woocommerce_ajax_add_to_cart(){
$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));
$quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);
$variation_id = absint($_POST['variation_id']);
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
$product_status = get_post_status($product_id);
if ($passed_validation && WC()
->cart
->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status)
{
do_action('woocommerce_ajax_added_to_cart', $product_id);
if ('yes' === get_option('woocommerce_cart_redirect_after_add'))
{
wc_add_to_cart_message(array(
$product_id => $quantity
) , true);
}
WC_AJAX::get_refreshed_fragments();
}
else{
$data = array(
'error' => true,
'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id) , $product_id)
);
echo wp_send_json($data);
}
wp_die();
}
/**
* Product Gallery
*
* @since 1.0.0
*/
public function product_gallery($class){
global $post, $product, $woocommerce;
if ( has_post_thumbnail() ) {
$attachment_ids = $product->get_gallery_image_ids();
$props = wc_get_product_attachment_props( get_post_thumbnail_id(), $post );
$image = get_the_post_thumbnail(
$post->ID,
'shop_single',
array(
'title' => $props['title'],
'alt' => $props['alt'],
)
);
echo sprintf(
'<div class="%s">%s</div>',
$class,
$image // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
if ( $attachment_ids ) {
$loop = 0;
foreach ( $attachment_ids as $attachment_id ) {
$props = wc_get_product_attachment_props( $attachment_id, $post );
if ( ! $props['url'] ) {
continue;
}
echo sprintf(
'<div class="%s">%s</div>',
$class,
wp_get_attachment_image( $attachment_id, 'shop_single', 0, $props )
);
$loop++;
}
}
} else {
echo sprintf( '<div><img src="%s" alt="%s" /></div>', wc_placeholder_img_src(), __( 'Placeholder', 'cawqv' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Animation class name in array
*
* @since 1.0.0
*/
public static function cawqv_animation(){
return [
'default' => 'Default',
'fadeInUp' => 'fadeInUp',
'fadeInUp' => 'fadeInUp',
'fadeInDown' => 'fadeInDown',
'bounceIn' => 'bounceIn',
'bounceInUp' => 'bounceInUp',
'bounceInDown' => 'bounceInDown',
'bounceInRight' => 'bounceInRight',
'bounceInLeft' => 'bounceInLeft',
'backInDown' => 'backInDown',
'flipInX' => 'flipInX',
'flipInY' => 'flipInY',
'jackInTheBox' => 'jackInTheBox',
'slideInUp' => 'slideInUp',
'slideInDown' => 'slideInDown',
'rollIn' => 'rollIn',
'zoomIn' => 'zoomIn',
'zoomInUp' => 'zoomInUp',
'zoomInDown' => 'zoomInDown',
];
}
/**
* Ajax events execute
*
* @since 1.0.0
*/
public function add_ajax_script(){
}
} //End Class
new CAWQV_FRONTEND();