PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.17
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.17
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 / classes / class-cartflows-helper.php

class-cartflows-helper.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.17, at classes/class-cartflows-helper.php

521 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CARTFLOWS Helper.
4 *
5 * @package CARTFLOWS
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Class Cartflows_Helper.
14 */
15 class Cartflows_Helper {
16
17 /**
18 * Common global data
19 *
20 * @var zapier
21 */
22 private static $common = null;
23
24 /**
25 * Common Debug data
26 *
27 * @var zapier
28 */
29 private static $debug_data = null;
30
31 /**
32 * Installed Plugins
33 *
34 * @since 1.1.4
35 *
36 * @access private
37 * @var array Installed plugins list.
38 */
39 private static $installed_plugins = null;
40
41 /**
42 * Checkout Fields
43 *
44 * @var checkout_fields
45 */
46 private static $checkout_fields = null;
47
48 /**
49 * Returns an option from the database for
50 * the admin settings page.
51 *
52 * @param string $key The option key.
53 * @param mixed $default Option default value if option is not available.
54 * @param boolean $network_override Whether to allow the network admin setting to be overridden on subsites.
55 * @return string Return the option value
56 */
57 public static function get_admin_settings_option( $key, $default = false, $network_override = false ) {
58
59 // Get the site-wide option if we're in the network admin.
60 if ( $network_override && is_multisite() ) {
61 $value = get_site_option( $key, $default );
62 } else {
63 $value = get_option( $key, $default );
64 }
65
66 return $value;
67 }
68
69 /**
70 * Updates an option from the admin settings page.
71 *
72 * @param string $key The option key.
73 * @param mixed $value The value to update.
74 * @param bool $network Whether to allow the network admin setting to be overridden on subsites.
75 * @return mixed
76 */
77 static public function update_admin_settings_option( $key, $value, $network = false ) {
78
79 // Update the site-wide option since we're in the network admin.
80 if ( $network && is_multisite() ) {
81 update_site_option( $key, $value );
82 } else {
83 update_option( $key, $value );
84 }
85
86 }
87
88 /**
89 * Get single setting
90 *
91 * @since 1.1.4
92 *
93 * @param string $key Option key.
94 * @param string $default Option default value if not exist.
95 * @return mixed
96 */
97 static public function get_common_setting( $key = '', $default = '' ) {
98 $settings = self::get_common_settings();
99
100 if ( $settings && array_key_exists( $key, $settings ) ) {
101 return $settings[ $key ];
102 }
103
104 return $default;
105 }
106
107 /**
108 * Get single debug options
109 *
110 * @since 1.1.4
111 *
112 * @param string $key Option key.
113 * @param string $default Option default value if not exist.
114 * @return mixed
115 */
116 static public function get_debug_setting( $key = '', $default = '' ) {
117 $debug_data = self::get_debug_settings();
118
119 if ( $debug_data && array_key_exists( $key, $debug_data ) ) {
120 return $debug_data[ $key ];
121 }
122
123 return $default;
124 }
125
126 /**
127 * Get required plugins for page builder
128 *
129 * @since 1.1.4
130 *
131 * @param string $page_builder_slug Page builder slug.
132 * @param string $default Default page builder.
133 * @return array selected page builder required plugins list.
134 */
135 public static function get_required_plugins_for_page_builder( $page_builder_slug = '', $default = 'elementor' ) {
136 $plugins = self::get_plugins_groupby_page_builders();
137
138 if ( array_key_exists( $page_builder_slug, $plugins ) ) {
139 return $plugins[ $page_builder_slug ];
140 }
141
142 return $plugins[ $default ];
143 }
144
145 /**
146 * Get Plugins list by page builder.
147 *
148 * @since 1.1.4
149 *
150 * @return array Required Plugins list.
151 */
152 public static function get_plugins_groupby_page_builders() {
153
154 $divi_status = self::get_plugin_status( 'divi-builder/divi-builder.php' );
155 $theme_status = 'not-installed';
156 if ( $divi_status ) {
157 if ( true === Cartflows_Compatibility::get_instance()->is_divi_theme_installed() ) {
158 $theme_status = 'installed';
159 if ( false === Cartflows_Compatibility::get_instance()->is_divi_enabled() ) {
160 $theme_status = 'deactivate';
161 $divi_status = 'activate';
162 } else {
163 $divi_status = '';
164 }
165 }
166 }
167
168 $plugins = array(
169 'elementor' => array(
170 'title' => 'Elementor',
171 'plugins' => array(
172 array(
173 'slug' => 'elementor', // For download from wp.org.
174 'init' => 'elementor/elementor.php',
175 'status' => self::get_plugin_status( 'elementor/elementor.php' ),
176 ),
177 ),
178 ),
179 'divi' => array(
180 'title' => 'Divi',
181 'theme-status' => $theme_status,
182 'plugin-status' => $divi_status,
183 'plugins' => array(
184 array(
185 'slug' => 'divi-builder', // For download from wp.org.
186 'init' => 'divi-builder/divi-builder.php',
187 'status' => $divi_status,
188 ),
189 ),
190 ),
191 );
192
193 $plugins['beaver-builder'] = array(
194 'title' => 'Beaver Builder',
195 'plugins' => array(),
196 );
197
198 // Check Pro Exist.
199 if ( file_exists( WP_PLUGIN_DIR . '/' . 'bb-plugin/fl-builder.php' ) && ! is_plugin_active( 'beaver-builder-lite-version/fl-builder.php' ) ) {
200 $plugins['beaver-builder']['plugins'][] = array(
201 'slug' => 'bb-plugin',
202 'init' => 'bb-plugin/fl-builder.php',
203 'status' => self::get_plugin_status( 'bb-plugin/fl-builder.php' ),
204 );
205 } else {
206 $plugins['beaver-builder']['plugins'][] = array(
207 'slug' => 'beaver-builder-lite-version', // For download from wp.org.
208 'init' => 'beaver-builder-lite-version/fl-builder.php',
209 'status' => self::get_plugin_status( 'beaver-builder-lite-version/fl-builder.php' ),
210 );
211 }
212
213 if ( file_exists( WP_PLUGIN_DIR . '/' . 'bb-ultimate-addon/bb-ultimate-addon.php' ) && ! is_plugin_active( 'ultimate-addons-for-beaver-builder-lite/bb-ultimate-addon.php' ) ) {
214 $plugins['beaver-builder']['plugins'][] = array(
215 'slug' => 'bb-ultimate-addon',
216 'init' => 'bb-ultimate-addon/bb-ultimate-addon.php',
217 'status' => self::get_plugin_status( 'bb-ultimate-addon/bb-ultimate-addon.php' ),
218 );
219 } else {
220 $plugins['beaver-builder']['plugins'][] = array(
221 'slug' => 'ultimate-addons-for-beaver-builder-lite', // For download from wp.org.
222 'init' => 'ultimate-addons-for-beaver-builder-lite/bb-ultimate-addon.php',
223 'status' => self::get_plugin_status( 'ultimate-addons-for-beaver-builder-lite/bb-ultimate-addon.php' ),
224 );
225 }
226
227 return $plugins;
228 }
229
230 /**
231 * Get plugin status
232 *
233 * @since 1.1.4
234 *
235 * @param string $plugin_init_file Plguin init file.
236 * @return mixed
237 */
238 public static function get_plugin_status( $plugin_init_file ) {
239
240 if ( null == self::$installed_plugins ) {
241 self::$installed_plugins = get_plugins();
242 }
243
244 if ( ! isset( self::$installed_plugins[ $plugin_init_file ] ) ) {
245 return 'install';
246 } elseif ( ! is_plugin_active( $plugin_init_file ) ) {
247 return 'activate';
248 }
249
250 return;
251 }
252
253 /**
254 * Get zapier settings.
255 *
256 * @return array.
257 */
258 static public function get_common_settings() {
259
260 if ( null === self::$common ) {
261
262 $common_default = apply_filters(
263 'cartflows_common_settings_default',
264 array(
265 'disallow_indexing' => 'disable',
266 'global_checkout' => '',
267 'default_page_builder' => 'elementor',
268 )
269 );
270
271 $common = Cartflows_Helper::get_admin_settings_option( '_cartflows_common', false, true );
272
273 $common = wp_parse_args( $common, $common_default );
274
275 if ( ! did_action( 'wp' ) ) {
276 return $common;
277 } else {
278 self::$common = $common;
279 }
280 }
281
282 return self::$common;
283 }
284
285 /**
286 * Get debug settings data.
287 *
288 * @return array.
289 */
290 static public function get_debug_settings() {
291
292 if ( null === self::$debug_data ) {
293
294 $debug_data_default = apply_filters(
295 'cartflows_debug_settings_default',
296 array(
297 'allow_minified_files' => 'disable',
298 )
299 );
300
301 $debug_data = Cartflows_Helper::get_admin_settings_option( '_cartflows_debug_data', false, true );
302
303 $debug_data = wp_parse_args( $debug_data, $debug_data_default );
304
305 if ( ! did_action( 'wp' ) ) {
306 return $debug_data;
307 } else {
308 self::$debug_data = $debug_data;
309 }
310 }
311
312 return self::$debug_data;
313 }
314
315 /**
316 * Get Checkout field.
317 *
318 * @param string $key Field key.
319 * @param int $post_id Post id.
320 * @return array.
321 */
322 static public function get_checkout_fields( $key, $post_id ) {
323
324 $saved_fields = get_post_meta( $post_id, 'wcf_fields_' . $key, true );
325
326 if ( ! $saved_fields ) {
327 $saved_fields = array();
328 }
329
330 $fields = array_filter( $saved_fields );
331
332 if ( empty( $fields ) ) {
333 if ( 'billing' === $key || 'shipping' === $key ) {
334
335 $fields = WC()->countries->get_address_fields( WC()->countries->get_base_country(), $key . '_' );
336
337 update_post_meta( $post_id, 'wcf_fields_' . $key, $fields );
338 }
339 }
340
341 return $fields;
342 }
343
344 /**
345 * Add Checkout field.
346 *
347 * @param string $type Field type.
348 * @param string $field_key Field key.
349 * @param array $field_data Field data.
350 * @param int $post_id Post id.
351 * @return boolean.
352 */
353 static public function add_checkout_field( $type, $field_key, $field_data = array(), $post_id ) {
354
355 $fields = self::get_checkout_fields( $type, $post_id );
356
357 $fields[ $field_key ] = $field_data;
358
359 update_post_meta( $post_id, 'wcf_fields_' . $type, $fields );
360
361 return true;
362 }
363
364 /**
365 * Get checkout fields settings.
366 *
367 * @param string $type Field type.
368 * @param string $field_key Field key.
369 * @param int $post_id Post id.
370 * @return array.
371 */
372 static public function delete_checkout_field( $type, $field_key, $post_id ) {
373
374 $fields = self::get_checkout_fields( $type, $post_id );
375
376 if ( isset( $fields[ $field_key ] ) ) {
377 unset( $fields[ $field_key ] );
378 }
379
380 update_post_meta( $post_id, 'wcf_fields_' . $type, $fields );
381
382 return true;
383 }
384
385 /**
386 * Get checkout fields settings.
387 *
388 * @return array.
389 */
390 static public function get_checkout_fields_settings() {
391
392 if ( null === self::$checkout_fields ) {
393 $checkout_fields_default = array(
394 'enable_customization' => 'disable',
395 'enable_billing_fields' => 'disable',
396 );
397
398 $billing_fields = self::get_checkout_fields( 'billing' );
399
400 if ( is_array( $billing_fields ) && ! empty( $billing_fields ) ) {
401
402 foreach ( $billing_fields as $key => $value ) {
403
404 $checkout_fields_default[ $key ] = 'enable';
405 }
406 }
407
408 $checkout_fields = Cartflows_Helper::get_admin_settings_option( '_wcf_checkout_fields', false, true );
409
410 self::$checkout_fields = wp_parse_args( $checkout_fields, $checkout_fields_default );
411 }
412
413 return self::$checkout_fields;
414 }
415
416 /**
417 * Get meta options
418 *
419 * @since 1.0.0
420 * @param int $post_id Product ID.
421 * @param string $key Meta Key.
422 * @param string $default Default value.
423 * @return string Meta Value.
424 */
425 static public function get_meta_option( $post_id, $key, $default = '' ) {
426
427 $value = get_post_meta( $post_id, $key, true );
428
429 if ( ! $value ) {
430 $value = $default;
431 }
432
433 return $value;
434 }
435
436 /**
437 * Save meta option
438 *
439 * @since 1.0.0
440 * @param int $post_id Product ID.
441 * @param array $args Arguments array.
442 */
443 static public function save_meta_option( $post_id, $args = array() ) {
444
445 if ( is_array( $args ) && ! empty( $args ) ) {
446
447 foreach ( $args as $key => $value ) {
448
449 update_post_meta( $post_id, $key, $value );
450 }
451 }
452 }
453
454 /**
455 * Check if Elementor page builder is installed
456 *
457 * @since 1.0.0
458 *
459 * @access public
460 */
461 static public function _is_elementor_installed() {
462 $path = 'elementor/elementor.php';
463 $plugins = get_plugins();
464
465 return isset( $plugins[ $path ] );
466 }
467
468 /**
469 * Check if Step has product assigned.
470 *
471 * @since 1.0.0
472 * @param int $step_id step ID.
473 *
474 * @access public
475 */
476 static public function has_product_assigned( $step_id ) {
477
478 $step_type = get_post_meta( $step_id, 'wcf-step-type', true );
479
480 if ( 'checkout' == $step_type ) {
481 $product = get_post_meta( $step_id, 'wcf-checkout-products', true );
482 } else {
483 $product = get_post_meta( $step_id, 'wcf-offer-product', true );
484 }
485
486 if ( ! empty( $product ) ) {
487 return true;
488 }
489 return false;
490
491 }
492
493 /**
494 * Get attributes for cartflows wrap.
495 *
496 * @since 1.1.4
497 *
498 * @access public
499 */
500 static public function get_cartflows_container_atts() {
501
502 $attributes = apply_filters( 'cartflows_container_atts', array() );
503 $atts_string = '';
504
505 foreach ( $attributes as $key => $value ) {
506
507 if ( ! $value ) {
508 continue;
509 }
510
511 if ( true === $value ) {
512 $atts_string .= esc_html( $key ) . ' ';
513 } else {
514 $atts_string .= sprintf( '%s="%s" ', esc_html( $key ), esc_attr( $value ) );
515 }
516 }
517
518 return $atts_string;
519 }
520 }
521