PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.13
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.13
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.13, at classes/class-cartflows-helper.php

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