PluginProbe
Orderable – Restaurant & Food Ordering System / 1.10.1
Orderable – Restaurant & Food Ordering System v1.10.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.10.1, at inc/class-helpers.php

324 lines 10.2 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 * Add image to media library.
228 *
229 * @param string $url URL.
230 * @param int $associated_with_post Post ID if media is associated to post.
231 * @param string $file_name File name.
232 *
233 * @return bool|int|WP_Error
234 */
235 public static function add_to_media( $url, $associated_with_post = 0, $file_name = '' ) {
236 require_once ABSPATH . 'wp-admin/includes/file.php';
237 require_once ABSPATH . 'wp-admin/includes/media.php';
238 require_once ABSPATH . 'wp-admin/includes/image.php';
239
240 $tmp = download_url( $url );
241 $post_id = $associated_with_post;
242 $file_array = array();
243
244 // Set variables for storage.
245 // fix file filename for query strings.
246 preg_match( '/[^\?]+\.(jpg|jpe|jpeg|gif|png|apk)/i', $url, $matches );
247
248 if ( empty( $matches ) ) {
249 return false;
250 }
251
252 $file_array['name'] = ! empty( $file_name ) ? $file_name : basename( $matches[0] );
253 $file_array['tmp_name'] = $tmp;
254
255 // If error storing temporarily, unlink.
256 if ( is_wp_error( $tmp ) ) {
257 @unlink( $file_array['tmp_name'] );
258
259 return false;
260 }
261
262 // do the validation and storage stuff.
263 $id = media_handle_sideload( $file_array, $post_id );
264
265 // If error storing permanently, unlink.
266 if ( is_wp_error( $id ) ) {
267 @unlink( $file_array['tmp_name'] );
268
269 return false;
270 }
271
272 return $id;
273 }
274
275 /**
276 * Check for the template in theme if exits then returns the file path
277 * in theme, else returns the path in plugin.
278 *
279 * This function simplifies the path in the theme to orderable/{module-slug}/template.php.
280 * For example if the path of file in plugin is
281 * /inc/modules/drawer/templates/floating-cart.php then the path in the theme
282 * would be orderable/drawer/floating-cart.php.
283 *
284 * @param string $file File to load.
285 * @param string $module Module slug example 'addons-pro'. Default: false.
286 * @param bool $pro Whether to find the template in pro plugin. Default: false.
287 *
288 * @return string
289 */
290 public static function get_template_path( $file, $module = false, $pro = false ) {
291 // Look for the file in theme.
292 // Remove 'templates/' if it exists in the start.
293 if ( 'templates/' === substr( $file, 0, 10 ) ) {
294 $file = substr( $file, 10 );
295 }
296
297 $theme_path = 'orderable/' . $file;
298
299 if ( $module ) {
300 $theme_path = sprintf( 'orderable/%s/%s', $module, $file );
301 }
302
303 // If template found in the theme, then return the theme path.
304 $theme_template = locate_template( $theme_path );
305 if ( $theme_template ) {
306 return $theme_template;
307 }
308
309 // Else look in the plugin.
310
311 if ( $pro && ! defined( 'ORDERABLE_PRO_PATH' ) ) {
312 return false;
313 }
314
315 $orderable_path = $pro ? ORDERABLE_PRO_PATH : ORDERABLE_PATH;
316
317 if ( $module ) {
318 return sprintf( '%sinc/modules/%s/templates/%s', $orderable_path, $module, $file );
319 } else {
320 return $orderable_path . 'templates/' . $file;
321 }
322 }
323 }
324