| 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 |
* Checkout Fields |
| 26 |
* |
| 27 |
* @var checkout_fields |
| 28 |
*/ |
| 29 |
private static $checkout_fields = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns an option from the database for |
| 33 |
* the admin settings page. |
| 34 |
* |
| 35 |
* @param string $key The option key. |
| 36 |
* @param mixed $default Option default value if option is not available. |
| 37 |
* @param boolean $network_override Whether to allow the network admin setting to be overridden on subsites. |
| 38 |
* @return string Return the option value |
| 39 |
*/ |
| 40 |
public static function get_admin_settings_option( $key, $default = false, $network_override = false ) { |
| 41 |
|
| 42 |
// Get the site-wide option if we're in the network admin. |
| 43 |
if ( $network_override && is_multisite() ) { |
| 44 |
$value = get_site_option( $key, $default ); |
| 45 |
} else { |
| 46 |
$value = get_option( $key, $default ); |
| 47 |
} |
| 48 |
|
| 49 |
return $value; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Updates an option from the admin settings page. |
| 54 |
* |
| 55 |
* @param string $key The option key. |
| 56 |
* @param mixed $value The value to update. |
| 57 |
* @param bool $network Whether to allow the network admin setting to be overridden on subsites. |
| 58 |
* @return mixed |
| 59 |
*/ |
| 60 |
static public function update_admin_settings_option( $key, $value, $network = false ) { |
| 61 |
|
| 62 |
// Update the site-wide option since we're in the network admin. |
| 63 |
if ( $network && is_multisite() ) { |
| 64 |
update_site_option( $key, $value ); |
| 65 |
} else { |
| 66 |
update_option( $key, $value ); |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get zapier settings. |
| 73 |
* |
| 74 |
* @return array. |
| 75 |
*/ |
| 76 |
static public function get_common_settings() { |
| 77 |
|
| 78 |
if ( null === self::$common ) { |
| 79 |
|
| 80 |
$common_default = apply_filters( |
| 81 |
'cartflows_common_settings_default', |
| 82 |
array( |
| 83 |
'disallow_indexing' => 'disable', |
| 84 |
'global_checkout' => '', |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
$common = Cartflows_Helper::get_admin_settings_option( '_cartflows_common', false, true ); |
| 89 |
|
| 90 |
self::$common = wp_parse_args( $common, $common_default ); |
| 91 |
} |
| 92 |
|
| 93 |
return self::$common; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get Checkout field. |
| 98 |
* |
| 99 |
* @param string $key Field key. |
| 100 |
* @param int $post_id Post id. |
| 101 |
* @return array. |
| 102 |
*/ |
| 103 |
static public function get_checkout_fields( $key, $post_id ) { |
| 104 |
|
| 105 |
$saved_fields = get_post_meta( $post_id, 'wcf_fields_' . $key, true ); |
| 106 |
|
| 107 |
if ( ! $saved_fields ) { |
| 108 |
$saved_fields = array(); |
| 109 |
} |
| 110 |
|
| 111 |
$fields = array_filter( $saved_fields ); |
| 112 |
|
| 113 |
if ( empty( $fields ) ) { |
| 114 |
if ( 'billing' === $key || 'shipping' === $key ) { |
| 115 |
|
| 116 |
$fields = WC()->countries->get_address_fields( WC()->countries->get_base_country(), $key . '_' ); |
| 117 |
|
| 118 |
update_post_meta( $post_id, 'wcf_fields_' . $key, $fields ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $fields; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Add Checkout field. |
| 127 |
* |
| 128 |
* @param string $type Field type. |
| 129 |
* @param string $field_key Field key. |
| 130 |
* @param array $field_data Field data. |
| 131 |
* @param int $post_id Post id. |
| 132 |
* @return boolean. |
| 133 |
*/ |
| 134 |
static public function add_checkout_field( $type, $field_key, $field_data = array(), $post_id ) { |
| 135 |
|
| 136 |
$fields = self::get_checkout_fields( $type, $post_id ); |
| 137 |
|
| 138 |
$fields[ $field_key ] = $field_data; |
| 139 |
|
| 140 |
update_post_meta( $post_id, 'wcf_fields_' . $type, $fields ); |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get checkout fields settings. |
| 147 |
* |
| 148 |
* @param string $type Field type. |
| 149 |
* @param string $field_key Field key. |
| 150 |
* @param int $post_id Post id. |
| 151 |
* @return array. |
| 152 |
*/ |
| 153 |
static public function delete_checkout_field( $type, $field_key, $post_id ) { |
| 154 |
|
| 155 |
$fields = self::get_checkout_fields( $type, $post_id ); |
| 156 |
|
| 157 |
if ( isset( $fields[ $field_key ] ) ) { |
| 158 |
unset( $fields[ $field_key ] ); |
| 159 |
} |
| 160 |
|
| 161 |
update_post_meta( $post_id, 'wcf_fields_' . $type, $fields ); |
| 162 |
|
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get checkout fields settings. |
| 168 |
* |
| 169 |
* @return array. |
| 170 |
*/ |
| 171 |
static public function get_checkout_fields_settings() { |
| 172 |
|
| 173 |
if ( null === self::$checkout_fields ) { |
| 174 |
$checkout_fields_default = array( |
| 175 |
'enable_customization' => 'disable', |
| 176 |
'enable_billing_fields' => 'disable', |
| 177 |
); |
| 178 |
|
| 179 |
$billing_fields = self::get_checkout_fields( 'billing' ); |
| 180 |
|
| 181 |
if ( is_array( $billing_fields ) && ! empty( $billing_fields ) ) { |
| 182 |
|
| 183 |
foreach ( $billing_fields as $key => $value ) { |
| 184 |
|
| 185 |
$checkout_fields_default[ $key ] = 'enable'; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
$checkout_fields = Cartflows_Helper::get_admin_settings_option( '_wcf_checkout_fields', false, true ); |
| 190 |
|
| 191 |
self::$checkout_fields = wp_parse_args( $checkout_fields, $checkout_fields_default ); |
| 192 |
} |
| 193 |
|
| 194 |
return self::$checkout_fields; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get meta options |
| 199 |
* |
| 200 |
* @since 1.0.0 |
| 201 |
* @param int $post_id Product ID. |
| 202 |
* @param string $key Meta Key. |
| 203 |
* @param string $default Default value. |
| 204 |
* @return string Meta Value. |
| 205 |
*/ |
| 206 |
static public function get_meta_option( $post_id, $key, $default = '' ) { |
| 207 |
|
| 208 |
$value = get_post_meta( $post_id, $key, true ); |
| 209 |
|
| 210 |
if ( ! $value ) { |
| 211 |
$value = $default; |
| 212 |
} |
| 213 |
|
| 214 |
return $value; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Save meta option |
| 219 |
* |
| 220 |
* @since 1.0.0 |
| 221 |
* @param int $post_id Product ID. |
| 222 |
* @param array $args Arguments array. |
| 223 |
*/ |
| 224 |
static public function save_meta_option( $post_id, $args = array() ) { |
| 225 |
|
| 226 |
if ( is_array( $args ) && ! empty( $args ) ) { |
| 227 |
|
| 228 |
foreach ( $args as $key => $value ) { |
| 229 |
|
| 230 |
update_post_meta( $post_id, $key, $value ); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Check if Elementor page builder is installed |
| 237 |
* |
| 238 |
* @since 1.0.0 |
| 239 |
* |
| 240 |
* @access public |
| 241 |
*/ |
| 242 |
static public function _is_elementor_installed() { |
| 243 |
$path = 'elementor/elementor.php'; |
| 244 |
$plugins = get_plugins(); |
| 245 |
|
| 246 |
return isset( $plugins[ $path ] ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Check if Step has product assigned. |
| 251 |
* |
| 252 |
* @since 1.0.0 |
| 253 |
* @param int $step_id step ID. |
| 254 |
* |
| 255 |
* @access public |
| 256 |
*/ |
| 257 |
static public function has_product_assigned( $step_id ) { |
| 258 |
|
| 259 |
$step_type = get_post_meta( $step_id, 'wcf-step-type', true ); |
| 260 |
|
| 261 |
if ( 'checkout' == $step_type ) { |
| 262 |
$product = get_post_meta( $step_id, 'wcf-checkout-products', true ); |
| 263 |
} else { |
| 264 |
$product = get_post_meta( $step_id, 'wcf-offer-product', true ); |
| 265 |
} |
| 266 |
|
| 267 |
if ( ! empty( $product ) ) { |
| 268 |
return true; |
| 269 |
} |
| 270 |
return false; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Get attributes for cartflows wrap. |
| 276 |
* |
| 277 |
* @since x.x.x |
| 278 |
* |
| 279 |
* @access public |
| 280 |
*/ |
| 281 |
static public function get_cartflows_container_atts() { |
| 282 |
|
| 283 |
$attributes = apply_filters( 'cartflows_container_atts', array() ); |
| 284 |
$atts_string = ''; |
| 285 |
|
| 286 |
foreach ( $attributes as $key => $value ) { |
| 287 |
|
| 288 |
if ( ! $value ) { |
| 289 |
continue; |
| 290 |
} |
| 291 |
|
| 292 |
if ( true === $value ) { |
| 293 |
$atts_string .= esc_html( $key ) . ' '; |
| 294 |
} else { |
| 295 |
$atts_string .= sprintf( '%s="%s" ', esc_html( $key ), esc_attr( $value ) ); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return $atts_string; |
| 300 |
} |
| 301 |
} |
| 302 |
|