PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.2
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.2
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / admin-core / ajax / meta-data.php

meta-data.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.2, at admin-core/ajax/meta-data.php

328 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Flows ajax actions.
4 *
5 * @package CartFlows
6 */
7
8 namespace CartflowsAdmin\AdminCore\Ajax;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 use CartflowsAdmin\AdminCore\Ajax\AjaxBase;
16 use CartflowsAdmin\AdminCore\Inc\AdminHelper;
17
18 /**
19 * Class Steps.
20 */
21 class MetaData extends AjaxBase {
22
23 /**
24 * Instance
25 *
26 * @access private
27 * @var object Class object.
28 * @since 1.0.0
29 */
30 private static $instance;
31
32 /**
33 * Initiator
34 *
35 * @since 1.0.0
36 * @return object initialized object of class.
37 */
38 public static function get_instance() {
39 if ( ! isset( self::$instance ) ) {
40 self::$instance = new self();
41 }
42 return self::$instance;
43 }
44
45 /**
46 * Register ajax events.
47 *
48 * @since 1.0.0
49 * @return void
50 */
51 public function register_ajax_events() {
52
53 $ajax_events = array(
54 'json_search_products',
55 'json_search_coupons',
56 );
57
58 $this->init_ajax_events( $ajax_events );
59 }
60
61 /**
62 * Clone step with its meta.
63 */
64 public function json_search_products() {
65
66 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
67 return;
68 }
69
70 check_ajax_referer( 'cartflows_json_search_products', 'security' );
71
72 global $wpdb;
73
74 if ( ! isset( $_POST['term'] ) ) {
75 return;
76 }
77
78 $term = ! empty( $_POST['term'] ) ? sanitize_text_field( wp_unslash( $_POST['term'] ) ) : '';
79
80 // CartFlows supported product types.
81 $supported_product_types = apply_filters( 'cartflows_supported_product_types_for_search', array( 'simple', 'variable', 'variation', 'subscription', 'variable-subscription', 'subscription_variation', 'course' ) );
82
83 // Allowed product types.
84 if ( isset( $_POST['allowed_products'] ) && ! empty( $_POST['allowed_products'] ) ) {
85
86 $allowed_product_types = sanitize_text_field( ( wp_unslash( $_POST['allowed_products'] ) ) );
87
88 $allowed_product_types = $this->sanitize_data_attributes( $allowed_product_types );
89
90 $supported_product_types = $allowed_product_types;
91 }
92
93 // Include product types.
94 if ( isset( $_POST['include_products'] ) && ! empty( $_POST['include_products'] ) ) {
95
96 $include_product_types = sanitize_text_field( ( wp_unslash( $_POST['include_products'] ) ) );
97
98 $include_product_types = $this->sanitize_data_attributes( $include_product_types );
99
100 $supported_product_types = array_merge( $supported_product_types, $include_product_types );
101 }
102
103 // Exclude product types.
104 if ( isset( $_POST['exclude_products'] ) && ! empty( $_POST['exclude_products'] ) ) {
105
106 $excluded_product_types = sanitize_text_field( ( wp_unslash( $_POST['exclude_products'] ) ) );
107
108 $excluded_product_types = $this->sanitize_data_attributes( $excluded_product_types );
109
110 $supported_product_types = array_diff( $supported_product_types, $excluded_product_types );
111 }
112
113 // Get all products data.
114 $data = \WC_Data_Store::load( 'product' );
115 $ids = $data->search_products( $term, '', true, false, 11 );
116
117 // Get all product objects.
118 $product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' );
119
120 // Remove the product objects whose product type are not in supported array.
121 $product_objects = array_filter(
122 $product_objects,
123 function ( $arr ) use ( $supported_product_types ) {
124 return $arr && is_a( $arr, 'WC_Product' ) && in_array( $arr->get_type(), $supported_product_types, true );
125 }
126 );
127
128 $products_found = array();
129
130 foreach ( $product_objects as $product_object ) {
131 $formatted_name = $product_object->get_name();
132 $managing_stock = $product_object->managing_stock();
133 $is_in_stock = $product_object->is_in_stock();
134 $product_type = $product_object->get_type();
135
136 $availibility_text = $this->get_stock_availability_text( $product_object );
137 $product_price_range = $this->get_formatted_product_price_range( $product_object );
138
139
140 if ( $managing_stock && ! empty( $_GET['display_stock'] ) ) {
141 $stock_amount = $product_object->get_stock_quantity();
142 /* Translators: %d stock amount */
143 $formatted_name .= ' &ndash; ' . sprintf( __( 'Stock: %d', 'cartflows' ), wc_format_stock_quantity_for_display( $stock_amount, $product_object ) );
144 }
145
146 array_push(
147 $products_found,
148 array(
149 'value' => $product_object->get_id(),
150 'label' => $formatted_name,
151 'original_price' => \Cartflows_Helper::get_product_original_price( $product_object ),
152 'product_name' => $product_object->get_name(),
153 'product_desc' => $product_object->get_short_description(),
154 'product_image' => get_the_post_thumbnail_url( $product_object->get_id() ),
155 'product_type' => $product_type,
156 'stock_status' => $availibility_text,
157 'in_stock' => $is_in_stock,
158 'price_range' => $product_price_range,
159 )
160 );
161 }
162
163 wp_send_json( $products_found );
164 }
165
166 /**
167 * Function to search coupons
168 */
169 public function json_search_coupons() {
170
171 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
172 return;
173 }
174
175 check_ajax_referer( 'cartflows_json_search_coupons', 'security' );
176
177 global $wpdb;
178
179 if ( ! isset( $_POST['term'] ) ) {
180 return;
181 }
182
183 $term = ! empty( $_POST['term'] ) ? sanitize_text_field( wp_unslash( $_POST['term'] ) ) : '';
184 if ( empty( $term ) ) {
185 die();
186 }
187
188 $posts = wp_cache_get( 'wcf_search_coupons', 'wcf_funnel_Cart' );
189
190 if ( false === $posts ) {
191 $posts = $wpdb->get_results(
192 $wpdb->prepare(
193 "SELECT *
194 FROM {$wpdb->prefix}posts
195 WHERE post_type = %s
196 AND post_title LIKE %s
197 AND post_status = %s",
198 'shop_coupon',
199 $wpdb->esc_like( $term ) . '%',
200 'publish'
201 )
202 ); // db call ok.
203 wp_cache_set( 'wcf_search_coupons', $posts, 'wcf_funnel_Cart' );
204 }
205
206 $coupons_found = array();
207 $all_discount_types = wc_get_coupon_types();
208
209 if ( $posts ) {
210 foreach ( $posts as $post ) {
211
212 $discount_type = get_post_meta( $post->ID, 'discount_type', true );
213
214 if ( ! empty( $all_discount_types[ $discount_type ] ) ) {
215 array_push(
216 $coupons_found,
217 array(
218 'value' => get_the_title( $post->ID ),
219 'label' => get_the_title( $post->ID ) . ' (Type: ' . $all_discount_types[ $discount_type ] . ')',
220 )
221 );
222 }
223 }
224 }
225
226 wp_send_json( $coupons_found );
227 }
228
229 /**
230 * Function to sanitize the product type data attribute.
231 *
232 * @param array $product_types product types.
233 */
234 public function sanitize_data_attributes( $product_types = array() ) {
235
236 if ( ! is_array( $product_types ) ) {
237 $product_types = explode( ',', $product_types );
238 }
239
240 // Sanitize the excluded types against valid product types.
241 foreach ( $product_types as $index => $value ) {
242 $product_types[ $index ] = strtolower( trim( $value ) );
243 }
244 return $product_types;
245 }
246
247 /**
248 * Function to get the stock availability text for a given product.
249 *
250 * This function checks the stock status of a product and returns a string indicating its availability.
251 * The availability text is translated to ensure it is user-friendly and consistent with the plugin's language.
252 *
253 * @param \WC_Product $product The product object to check the stock availability for.
254 * @return string The stock availability text for the product.
255 */
256 public function get_stock_availability_text( $product ) {
257
258 $availability_text = '';
259
260 if ( ! is_a( $product, 'WC_Product' ) ) {
261 return ''; // Return empty if product is not valid.
262 }
263
264 $availability = $product->get_availability();
265
266 if ( ! empty( $availability['class'] ) ) {
267
268 switch ( $availability['class'] ) {
269 case 'available-on-backorder':
270 $availability_text = __( 'On backorder', 'cartflows' );
271 break;
272 case 'in-stock':
273 $availability_text = __( 'In stock', 'cartflows' );
274 break;
275 case 'out-of-stock':
276 $availability_text = __( 'Out of stock', 'cartflows' );
277 break;
278 default:
279 break;
280 }
281 }
282
283 return $availability_text;
284 }
285
286 /**
287 * Function to generate a formatted price range for a product.
288 *
289 * This function determines the price range for a product based on its type. For variable products, it calculates the price range from the minimum and maximum prices of its variations. For other product types, it uses the product's price or regular price if available.
290 * The function returns a formatted string representing the price range.
291 *
292 * @param \WC_Product $product The product object to generate the price range for.
293 * @return string The formatted price range for the product.
294 */
295 public function get_formatted_product_price_range( $product ) {
296 $original_price = 0;
297
298 $product_type = $product->get_type();
299
300 if ( 'variable' === $product_type ) {
301 // Code for variation product.
302 $variation_price_range = $product->get_variation_prices( true );
303
304 $price = array();
305 $min_price = isset( $variation_price_range['price'] ) ? current( $variation_price_range['price'] ) : null;
306 $max_price = isset( $variation_price_range['price'] ) ? end( $variation_price_range['price'] ) : null;
307 $min_reg_price = isset( $variation_price_range['regular_price'] ) ? current( $variation_price_range['regular_price'] ) : null;
308 $max_reg_price = isset( $variation_price_range['regular_price'] ) ? end( $variation_price_range['regular_price'] ) : null;
309
310 if ( $min_price !== $max_price && ! is_null( $min_price ) && ! is_null( $max_price ) ) {
311 $original_price = html_entity_decode( wp_strip_all_tags( wc_format_price_range( $min_price, $max_price ) ) );
312 }
313
314 if ( $min_reg_price !== $max_reg_price && ! is_null( $min_reg_price ) && ! is_null( $max_reg_price ) ) {
315 if ( ! empty( $original_price ) ) {
316 $original_price = html_entity_decode( wp_strip_all_tags( wc_format_price_range( $min_reg_price, $max_reg_price ) ) );
317 }
318 }
319 } else {
320 $price = $product->get_price();
321 $original_price = ! empty( $price ) ? $price : $product->get_regular_price();
322 $original_price = html_entity_decode( wp_strip_all_tags( wc_price( $original_price ) ) );
323 }
324
325 return $original_price;
326 }
327 }
328