PluginProbe
Orderable – Restaurant & Food Ordering System / 1.12.1
Orderable – Restaurant & Food Ordering System v1.12.1
0.1.4 0.1.5 0.2.0 1.0.0 1.1.0 1.1.1 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.19.1 1.19.2 1.2.0 1.20.0 1.20.1 All 44 releases
orderable / inc / class-helpers.php

class-helpers.php in Orderable – Restaurant & Food Ordering System 1.12.1, at inc/class-helpers.php

487 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper methods.
4 *
5 * @package Orderable/Classes
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Helpers class.
12 */
13 class Orderable_Helpers {
14 /**
15 * Get term ID by slug.
16 *
17 * @param string $slug
18 * @param string $taxonomy
19 *
20 * @return int|bool
21 */
22 public static function get_term_id_by_slug( $slug, $taxonomy = 'product_cat' ) {
23 global $wpdb;
24
25 $results = $wpdb->get_var( $wpdb->prepare(
26 "SELECT DISTINCT t.term_id
27 FROM wp_term_taxonomy AS tt
28 INNER JOIN wp_terms AS t ON tt.term_id = t.term_id
29 WHERE t.slug = %s
30 AND tt.taxonomy = %s",
31 $slug,
32 $taxonomy
33 ) );
34
35 return $results ? absint( $results ) : false;
36 }
37
38 /**
39 * Load classes.
40 *
41 * @param array $classes Key = filename / Value = Class name.
42 * @param string $module Module folder name.
43 */
44 public static function load_classes( $classes, $module, $root = ORDERABLE_MODULES_PATH ) {
45 foreach ( $classes as $file_name => $class_name ) {
46 $path = $root . $module . '/class-' . $file_name . '.php';
47
48 if ( ! file_exists( $path ) ) {
49 continue;
50 }
51
52 require_once $path;
53
54 if ( ! method_exists( $class_name, 'run' ) ) {
55 continue;
56 }
57
58 $class_name::run();
59 }
60 }
61
62 /**
63 * Get pro button.
64 *
65 * @param string $campaign
66 * @param string $text
67 * @param bool $lock_icon
68 *
69 * @return string
70 */
71 public static function get_pro_button( $campaign = '', $text = '', $lock_icon = true ) {
72 $text = empty( $text ) ? __( 'Available in Pro', 'orderable' ) : $text;
73 $lock_icon = $lock_icon ? '<span class="dashicons dashicons-lock"></span>' : '';
74 $url = self::get_pro_url( $campaign );
75
76 return sprintf( '<a href="%s" class="orderable-admin-button orderable-admin-button--pro" target="_blank">%s %s</a>', esc_url( $url ), $lock_icon, $text );
77 }
78
79 /**
80 * Get pro URL.
81 *
82 * @param string $campaign Campaign.
83 *
84 * @return string
85 */
86 public static function get_pro_url( $campaign = '', $path = '' ) {
87 $campaign = ! empty( $campaign ) ? sprintf( '&utm_campaign=%s', $campaign ) : '';
88
89 return sprintf( 'https://orderable.com/%s?utm_source=Orderable&utm_medium=Plugin%s', $path, $campaign );
90 }
91
92 /**
93 * Prepare post type labels.
94 *
95 * @param array $labels
96 *
97 * @return array|bool
98 */
99 public static function prepare_post_type_labels( $labels = array() ) {
100 if ( empty( $labels ) ) {
101 return false;
102 }
103
104 return array(
105 'name' => $labels['plural'],
106 'singular_name' => $labels['singular'],
107 'menu_name' => $labels['plural'],
108 'name_admin_bar' => $labels['singular'],
109 'add_new' => __( 'Add New', 'orderable' ),
110 'add_new_item' => sprintf( __( 'Add New %s', 'orderable' ), $labels['singular'] ),
111 'new_item' => sprintf( __( 'New %s', 'orderable' ), $labels['singular'] ),
112 'edit_item' => sprintf( __( 'Edit %s', 'orderable' ), $labels['singular'] ),
113 'view_item' => sprintf( __( 'View %s', 'orderable' ), $labels['singular'] ),
114 'all_items' => $labels['plural'],
115 'search_items' => sprintf( __( 'Search %s', 'orderable' ), $labels['plural'] ),
116 'parent_item_colon' => sprintf( __( 'Parent %s:', 'orderable' ), $labels['plural'] ),
117 'not_found' => sprintf( __( 'No %s found.', 'orderable' ), strtolower( $labels['plural'] ) ),
118 'not_found_in_trash' => sprintf( __( 'No %s found in trash.', 'orderable' ), strtolower( $labels['plural'] ) ),
119 'featured_image' => sprintf( _x( '%s Featured Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'orderable' ), $labels['singular'] ),
120 'set_featured_image' => _x( 'Set featured image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'orderable' ),
121 'remove_featured_image' => _x( 'Remove featured image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'orderable' ),
122 'use_featured_image' => _x( 'Use as featured image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'orderable' ),
123 'archives' => sprintf( _x( '%s archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'orderable' ), $labels['plural'] ),
124 'insert_into_item' => sprintf( _x( 'Insert into %s', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'orderable' ), strtolower( $labels['singular'] ) ),
125 'uploaded_to_this_item' => sprintf( _x( 'Uploaded to this %s', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'orderable' ), strtolower( $labels['singular'] ) ),
126 'filter_items_list' => sprintf( _x( 'Filter %s list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'orderable' ), strtolower( $labels['plural'] ) ),
127 'items_list_navigation' => sprintf( _x( '%s list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'orderable' ), $labels['plural'] ),
128 'items_list' => sprintf( _x( '%s list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'orderable' ), $labels['plural'] ),
129 );
130 }
131
132 /**
133 * Outputs the pro feature modal.
134 */
135 public static function orderable_pro_modal() {
136 require_once self::get_template_path( 'templates/admin/orderable-pro-modal.php' );
137 }
138
139 /**
140 * Custom kses method so we can also modify CSS properties allowed.
141 *
142 * @param string $content
143 * @param string $context
144 *
145 * @return string
146 */
147 public static function kses( $content, $context = '' ) {
148 if ( empty( $content ) ) {
149 return $content;
150 }
151
152 // Modify allowed CSS.
153 add_filter( 'safe_style_css', array( __CLASS__, 'safe_css' ) );
154
155 $content = wp_kses( $content, self::kses_allowed_html( $context ) );
156
157 // Disable CSS modification.
158 remove_filter( 'safe_style_css', array( __CLASS__, 'safe_css' ) );
159
160 return $content;
161 }
162
163 /**
164 * Sanitise output and allow form elements.
165 *
166 * @param string $context
167 *
168 * @return array
169 */
170 public static function kses_allowed_html( $context = '' ) {
171 $allowed_html = wp_kses_allowed_html( 'post' );
172
173 if ( 'form' === $context ) {
174 $allowed_attributes = array(
175 'name' => array(),
176 'class' => array(),
177 'value' => array(),
178 'type' => array(),
179 'selected' => array(),
180 );
181
182 $allowed_html['span'] = $allowed_attributes;
183 $allowed_html['select'] = $allowed_attributes;
184 $allowed_html['option'] = $allowed_attributes;
185 $allowed_html['input'] = $allowed_attributes;
186 }
187
188 return $allowed_html;
189 }
190
191 /**
192 * Modify safe CSS values.
193 *
194 * @param array $safe_css
195 *
196 * @return array
197 */
198 public static function safe_css( $safe_css = array() ) {
199 $safe_css[] = 'display';
200
201 return $safe_css;
202 }
203
204 /**
205 * Delete Orderable transients.
206 */
207 public static function delete_orderable_transients() {
208 global $wpdb;
209
210 $wpdb->query( "
211 DELETE FROM $wpdb->options
212 WHERE option_name LIKE ('%%\_transient\_timeout\_orderable\_%%')
213 OR option_name LIKE ('%%\_transient\_orderable\_%%')
214 " );
215 }
216
217 /**
218 * Has notices?
219 *
220 * @return bool
221 */
222 public static function has_notices() {
223 return ! empty( WC()->session->get( 'wc_notices', array() ) );
224 }
225
226 /**
227 * Is a variable product type?
228 *
229 * @param string $product_type The product type.
230 * @return boolean
231 */
232 public static function is_variable_product( $product_type ) {
233 $variable_types = array(
234 'variable',
235 'variable-subscription',
236 );
237
238 /**
239 * Filter the variable types used by the is_variable_product function.
240 *
241 * @param array $variable_types The variable types.
242 *
243 * @return array New variable types.
244 * @since 1.4.0
245 * @hook orderable_variable_types
246 */
247 $variable_types = apply_filters( 'orderable_variable_types', $variable_types );
248
249 return in_array( $product_type, $variable_types, true );
250 }
251
252 /**
253 * Is a variation product type?
254 *
255 * @param string $product_type The product type.
256 * @return boolean
257 */
258 public static function is_variation_product( $product_type ) {
259 $variation_types = array(
260 'variation',
261 'subscription_variation',
262 );
263
264 /**
265 * Filter the variable types used by the is_variable_product function.
266 *
267 * @param array $variation_types The variable types.
268 *
269 * @return array New variation types.
270 * @since 1.4.0
271 * @hook orderable_variation_types
272 */
273 $variation_types = apply_filters( 'orderable_variation_types', $variation_types );
274
275 return in_array( $product_type, $variation_types, true );
276 }
277
278 /** Add image to media library.
279 *
280 * @param string $url URL.
281 * @param int $associated_with_post Post ID if media is associated to post.
282 * @param string $file_name File name.
283 *
284 * @return bool|int|WP_Error
285 */
286 public static function add_to_media( $url, $associated_with_post = 0, $file_name = '' ) {
287 require_once ABSPATH . 'wp-admin/includes/file.php';
288 require_once ABSPATH . 'wp-admin/includes/media.php';
289 require_once ABSPATH . 'wp-admin/includes/image.php';
290
291 $tmp = download_url( $url );
292 $post_id = $associated_with_post;
293 $file_array = array();
294
295 // Set variables for storage.
296 // fix file filename for query strings.
297 preg_match( '/[^\?]+\.(jpg|jpe|jpeg|gif|png|apk)/i', $url, $matches );
298
299 if ( empty( $matches ) ) {
300 return false;
301 }
302
303 $file_array['name'] = ! empty( $file_name ) ? $file_name : basename( $matches[0] );
304 $file_array['tmp_name'] = $tmp;
305
306 // If error storing temporarily, unlink.
307 if ( is_wp_error( $tmp ) ) {
308 @unlink( $file_array['tmp_name'] );
309
310 return false;
311 }
312
313 // do the validation and storage stuff.
314 $id = media_handle_sideload( $file_array, $post_id );
315
316 // If error storing permanently, unlink.
317 if ( is_wp_error( $id ) ) {
318 @unlink( $file_array['tmp_name'] );
319
320 return false;
321 }
322
323 return $id;
324 }
325
326 /**
327 * Check for the template in theme if exits then returns the file path
328 * in theme, else returns the path in plugin.
329 *
330 * This function simplifies the path in the theme to orderable/{module-slug}/template.php.
331 * For example if the path of file in plugin is
332 * /inc/modules/drawer/templates/floating-cart.php then the path in the theme
333 * would be orderable/drawer/floating-cart.php.
334 *
335 * @param string $file File to load.
336 * @param string $module Module slug example 'addons-pro'. Default: false.
337 * @param bool $pro Whether to find the template in pro plugin. Default: false.
338 *
339 * @return string
340 */
341 public static function get_template_path( $file, $module = false, $pro = false ) {
342 // Look for the file in theme.
343 // Remove 'templates/' if it exists in the start.
344 if ( 'templates/' === substr( $file, 0, 10 ) ) {
345 $file = substr( $file, 10 );
346 }
347
348 $theme_path = 'orderable/' . $file;
349
350 if ( $module ) {
351 $theme_path = sprintf( 'orderable/%s/%s', $module, $file );
352 }
353
354 // If template found in the theme, then return the theme path.
355 $theme_template = locate_template( $theme_path );
356 if ( $theme_template ) {
357 return $theme_template;
358 }
359
360 // Else look in the plugin.
361
362 if ( $pro && ! defined( 'ORDERABLE_PRO_PATH' ) ) {
363 return false;
364 }
365
366 $orderable_path = $pro ? ORDERABLE_PRO_PATH : ORDERABLE_PATH;
367
368 if ( $module ) {
369 return sprintf( '%sinc/modules/%s/templates/%s', $orderable_path, $module, $file );
370 } else {
371 return $orderable_path . 'templates/' . $file;
372 }
373 }
374
375 /**
376 * Get the allowed HTML tags to SVG.
377 *
378 * This function is usefull when we need to escape
379 * SVG elements using wp_kses().
380 *
381 * @return array
382 */
383 public static function get_svg_allowed_html_tags() {
384 $allowed_html_tags['svg'] = array(
385 'xmlns' => array(),
386 'fill' => array(),
387 'height' => array(),
388 'width' => array(),
389 'viewbox' => array(),
390 );
391
392 $allowed_html_tags['g'] = array();
393
394 $allowed_html_tags['circle'] = array(
395 'cx' => array(),
396 'cy' => array(),
397 'r' => array(),
398 );
399
400 $allowed_html_tags['rect'] = array(
401 'x' => array(),
402 'y' => array(),
403 'rx' => array(),
404 'height' => array(),
405 'width' => array(),
406 'transform' => array(),
407 'fill' => array(),
408 );
409
410 $allowed_html_tags['path'] = array(
411 'd' => array(),
412 'fill' => array(),
413 'fill-rule' => array(),
414 'clip-rule' => array(),
415 );
416
417 /**
418 * Filter the allowed SVG tags.
419 *
420 * @since 1.18.0
421 * @hook orderable_allowed_svg_tags
422 * @param array $allowed_html_tags The SVG allowed tags.
423 * @return array New value
424 */
425 $allowed_html_tags = apply_filters( 'orderable_allowed_svg_tags', $allowed_html_tags );
426
427 return $allowed_html_tags;
428 }
429
430 /**
431 * Check if a product is in the cart and return the cart item
432 * if true. Otherwise, it returns false.
433 *
434 * @param int $product_id The product ID.
435 * @return array|false
436 */
437 public static function is_product_in_the_cart( $product_id ) {
438 $cart_item = false;
439
440 if ( is_admin() || empty( WC()->cart ) ) {
441 return $cart_item;
442 }
443
444 foreach ( WC()->cart->get_cart() as $cart_item_data ) {
445 if ( $product_id !== $cart_item_data['product_id'] ) {
446 continue;
447 }
448
449 $cart_item = $cart_item_data;
450 break;
451 }
452
453 return $cart_item;
454 }
455
456 /**
457 * Get product quantity in the cart.
458 *
459 * If the product type is variation, it returns
460 * the quantity based on the variable product (parent).
461 *
462 * @param in $product_id The product ID.
463 * @return int
464 */
465 public static function get_product_quantity_in_the_cart( $product_id ) {
466 $quantity = 0;
467
468 if ( is_admin() && ! wp_doing_ajax() ) {
469 return $quantity;
470 }
471
472 if ( empty( WC()->cart ) ) {
473 return $quantity;
474 }
475
476 foreach ( WC()->cart->get_cart() as $cart_item ) {
477 if ( $product_id !== $cart_item['product_id'] ) {
478 continue;
479 }
480
481 $quantity += absint( $cart_item['quantity'] );
482 }
483
484 return $quantity;
485 }
486 }
487