| 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 |
self::$common = wp_parse_args( $common, $common_default ); |
| 248 |
} |
| 249 |
|
| 250 |
return self::$common; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Get Checkout field. |
| 255 |
* |
| 256 |
* @param string $key Field key. |
| 257 |
* @param int $post_id Post id. |
| 258 |
* @return array. |
| 259 |
*/ |
| 260 |
static public function get_checkout_fields( $key, $post_id ) { |
| 261 |
|
| 262 |
$saved_fields = get_post_meta( $post_id, 'wcf_fields_' . $key, true ); |
| 263 |
|
| 264 |
if ( ! $saved_fields ) { |
| 265 |
$saved_fields = array(); |
| 266 |
} |
| 267 |
|
| 268 |
$fields = array_filter( $saved_fields ); |
| 269 |
|
| 270 |
if ( empty( $fields ) ) { |
| 271 |
if ( 'billing' === $key || 'shipping' === $key ) { |
| 272 |
|
| 273 |
$fields = WC()->countries->get_address_fields( WC()->countries->get_base_country(), $key . '_' ); |
| 274 |
|
| 275 |
update_post_meta( $post_id, 'wcf_fields_' . $key, $fields ); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return $fields; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Add Checkout field. |
| 284 |
* |
| 285 |
* @param string $type Field type. |
| 286 |
* @param string $field_key Field key. |
| 287 |
* @param array $field_data Field data. |
| 288 |
* @param int $post_id Post id. |
| 289 |
* @return boolean. |
| 290 |
*/ |
| 291 |
static public function add_checkout_field( $type, $field_key, $field_data = array(), $post_id ) { |
| 292 |
|
| 293 |
$fields = self::get_checkout_fields( $type, $post_id ); |
| 294 |
|
| 295 |
$fields[ $field_key ] = $field_data; |
| 296 |
|
| 297 |
update_post_meta( $post_id, 'wcf_fields_' . $type, $fields ); |
| 298 |
|
| 299 |
return true; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Get checkout fields settings. |
| 304 |
* |
| 305 |
* @param string $type Field type. |
| 306 |
* @param string $field_key Field key. |
| 307 |
* @param int $post_id Post id. |
| 308 |
* @return array. |
| 309 |
*/ |
| 310 |
static public function delete_checkout_field( $type, $field_key, $post_id ) { |
| 311 |
|
| 312 |
$fields = self::get_checkout_fields( $type, $post_id ); |
| 313 |
|
| 314 |
if ( isset( $fields[ $field_key ] ) ) { |
| 315 |
unset( $fields[ $field_key ] ); |
| 316 |
} |
| 317 |
|
| 318 |
update_post_meta( $post_id, 'wcf_fields_' . $type, $fields ); |
| 319 |
|
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Get checkout fields settings. |
| 325 |
* |
| 326 |
* @return array. |
| 327 |
*/ |
| 328 |
static public function get_checkout_fields_settings() { |
| 329 |
|
| 330 |
if ( null === self::$checkout_fields ) { |
| 331 |
$checkout_fields_default = array( |
| 332 |
'enable_customization' => 'disable', |
| 333 |
'enable_billing_fields' => 'disable', |
| 334 |
); |
| 335 |
|
| 336 |
$billing_fields = self::get_checkout_fields( 'billing' ); |
| 337 |
|
| 338 |
if ( is_array( $billing_fields ) && ! empty( $billing_fields ) ) { |
| 339 |
|
| 340 |
foreach ( $billing_fields as $key => $value ) { |
| 341 |
|
| 342 |
$checkout_fields_default[ $key ] = 'enable'; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
$checkout_fields = Cartflows_Helper::get_admin_settings_option( '_wcf_checkout_fields', false, true ); |
| 347 |
|
| 348 |
self::$checkout_fields = wp_parse_args( $checkout_fields, $checkout_fields_default ); |
| 349 |
} |
| 350 |
|
| 351 |
return self::$checkout_fields; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Get meta options |
| 356 |
* |
| 357 |
* @since 1.0.0 |
| 358 |
* @param int $post_id Product ID. |
| 359 |
* @param string $key Meta Key. |
| 360 |
* @param string $default Default value. |
| 361 |
* @return string Meta Value. |
| 362 |
*/ |
| 363 |
static public function get_meta_option( $post_id, $key, $default = '' ) { |
| 364 |
|
| 365 |
$value = get_post_meta( $post_id, $key, true ); |
| 366 |
|
| 367 |
if ( ! $value ) { |
| 368 |
$value = $default; |
| 369 |
} |
| 370 |
|
| 371 |
return $value; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Save meta option |
| 376 |
* |
| 377 |
* @since 1.0.0 |
| 378 |
* @param int $post_id Product ID. |
| 379 |
* @param array $args Arguments array. |
| 380 |
*/ |
| 381 |
static public function save_meta_option( $post_id, $args = array() ) { |
| 382 |
|
| 383 |
if ( is_array( $args ) && ! empty( $args ) ) { |
| 384 |
|
| 385 |
foreach ( $args as $key => $value ) { |
| 386 |
|
| 387 |
update_post_meta( $post_id, $key, $value ); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Check if Elementor page builder is installed |
| 394 |
* |
| 395 |
* @since 1.0.0 |
| 396 |
* |
| 397 |
* @access public |
| 398 |
*/ |
| 399 |
static public function _is_elementor_installed() { |
| 400 |
$path = 'elementor/elementor.php'; |
| 401 |
$plugins = get_plugins(); |
| 402 |
|
| 403 |
return isset( $plugins[ $path ] ); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Check if Step has product assigned. |
| 408 |
* |
| 409 |
* @since 1.0.0 |
| 410 |
* @param int $step_id step ID. |
| 411 |
* |
| 412 |
* @access public |
| 413 |
*/ |
| 414 |
static public function has_product_assigned( $step_id ) { |
| 415 |
|
| 416 |
$step_type = get_post_meta( $step_id, 'wcf-step-type', true ); |
| 417 |
|
| 418 |
if ( 'checkout' == $step_type ) { |
| 419 |
$product = get_post_meta( $step_id, 'wcf-checkout-products', true ); |
| 420 |
} else { |
| 421 |
$product = get_post_meta( $step_id, 'wcf-offer-product', true ); |
| 422 |
} |
| 423 |
|
| 424 |
if ( ! empty( $product ) ) { |
| 425 |
return true; |
| 426 |
} |
| 427 |
return false; |
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Get attributes for cartflows wrap. |
| 433 |
* |
| 434 |
* @since 1.1.4 |
| 435 |
* |
| 436 |
* @access public |
| 437 |
*/ |
| 438 |
static public function get_cartflows_container_atts() { |
| 439 |
|
| 440 |
$attributes = apply_filters( 'cartflows_container_atts', array() ); |
| 441 |
$atts_string = ''; |
| 442 |
|
| 443 |
foreach ( $attributes as $key => $value ) { |
| 444 |
|
| 445 |
if ( ! $value ) { |
| 446 |
continue; |
| 447 |
} |
| 448 |
|
| 449 |
if ( true === $value ) { |
| 450 |
$atts_string .= esc_html( $key ) . ' '; |
| 451 |
} else { |
| 452 |
$atts_string .= sprintf( '%s="%s" ', esc_html( $key ), esc_attr( $value ) ); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
return $atts_string; |
| 457 |
} |
| 458 |
} |
| 459 |
|