| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit; |
| 3 |
|
| 4 |
if ( ! function_exists( 'sellkit_update_option' ) ) { |
| 5 |
/** |
| 6 |
* Update option from options storage. |
| 7 |
* |
| 8 |
* @param string $option Option name. |
| 9 |
* @param mixed $value Update value. |
| 10 |
* |
| 11 |
* @return boolean False if value was not updated and true if value was updated. |
| 12 |
*/ |
| 13 |
function sellkit_update_option( $option, $value ) { |
| 14 |
$options = get_option( 'sellkit', [] ); |
| 15 |
|
| 16 |
// No need to update the same value. |
| 17 |
if ( isset( $options[ $option ] ) && $value === $options[ $option ] ) { |
| 18 |
return false; |
| 19 |
} |
| 20 |
|
| 21 |
// Update the option. |
| 22 |
$options[ $option ] = $value; |
| 23 |
update_option( 'sellkit', $options ); |
| 24 |
|
| 25 |
return true; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! function_exists( 'sellkit_get_option' ) ) { |
| 30 |
/** |
| 31 |
* Get option from options storage. |
| 32 |
* |
| 33 |
* @param string $option Option name. |
| 34 |
* @param boolean $default Default value. |
| 35 |
* |
| 36 |
* @return mixed Value set for the option. |
| 37 |
*/ |
| 38 |
function sellkit_get_option( $option, $default = false ) { |
| 39 |
$options = get_option( 'sellkit', [] ); |
| 40 |
|
| 41 |
if ( ! isset( $options[ $option ] ) ) { |
| 42 |
return $default; |
| 43 |
} |
| 44 |
|
| 45 |
return $options[ $option ]; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! function_exists( 'sellkit_htmlspecialchars' ) ) { |
| 50 |
/** |
| 51 |
* Sellkit html special chars is created because FILTER_SANITIZE_STRING is deprecated. |
| 52 |
* |
| 53 |
* @since 1.2.1 |
| 54 |
* @param string $input_type Type of input. |
| 55 |
* @param string $param Parameter key. |
| 56 |
*/ |
| 57 |
function sellkit_htmlspecialchars( $input_type, $param ) { |
| 58 |
$value = ''; |
| 59 |
|
| 60 |
if ( INPUT_GET === $input_type ) { |
| 61 |
$value = ! empty( $_GET[ $param ] ) ? htmlspecialchars( $_GET[ $param ] ) : ''; //phpcs:ignore |
| 62 |
} |
| 63 |
|
| 64 |
if ( INPUT_POST === $input_type ) { |
| 65 |
$value = ! empty( $_POST[ $param ] ) ? htmlspecialchars( $_POST[ $param ] ) : ''; //phpcs:ignore |
| 66 |
} |
| 67 |
|
| 68 |
if ( INPUT_COOKIE === $input_type ) { |
| 69 |
$value = ! empty( $_COOKIE[ $param ] ) ? htmlspecialchars( $_COOKIE[ $param ] ) : ''; //phpcs:ignore |
| 70 |
} |
| 71 |
|
| 72 |
return $value; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! function_exists( 'sellkit_decode_entity_title' ) ) { |
| 77 |
/** |
| 78 |
* Decode HTML entities in titles for JSON/React (fixes literal – etc. in chart headers). |
| 79 |
* |
| 80 |
* @param string $title Title string. |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
function sellkit_decode_entity_title( $title ) { |
| 84 |
if ( ! is_string( $title ) || '' === $title ) { |
| 85 |
return $title; |
| 86 |
} |
| 87 |
|
| 88 |
return html_entity_decode( $title, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Delete option from options storage. |
| 94 |
* |
| 95 |
* @param string $option Option name. |
| 96 |
* |
| 97 |
* @return boolean False if value was not deleted and true if value was deleted. |
| 98 |
*/ |
| 99 |
function sellkit_delete_option( $option ) { |
| 100 |
$options = get_option( 'sellkit', [] ); |
| 101 |
|
| 102 |
// Option not exist. |
| 103 |
if ( ! isset( $options[ $option ] ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
// Remove the option. |
| 108 |
unset( $options[ $option ] ); |
| 109 |
update_option( 'sellkit', $options ); |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get multi-select values. |
| 116 |
* |
| 117 |
* @since 1.1.0 |
| 118 |
* @param array $array Multi select data. |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
function sellkit_get_multi_select_values( $array ) { |
| 122 |
$values = []; |
| 123 |
|
| 124 |
foreach ( $array as $item ) { |
| 125 |
$values[] = $item['value']; |
| 126 |
} |
| 127 |
|
| 128 |
return $values; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Checks conditions validation. |
| 133 |
* |
| 134 |
* @since 1.1.0 |
| 135 |
* @SuppressWarnings(PHPMD.NPathComplexity) |
| 136 |
* @param array $conditions Conditions data. |
| 137 |
*/ |
| 138 |
function sellkit_conditions_validation( $conditions ) { |
| 139 |
$is_valid = true; |
| 140 |
|
| 141 |
if ( empty( $conditions ) ) { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
$first_condition = reset( $conditions ); |
| 146 |
$condition_type = ! empty( $first_condition['type'] ) ? $first_condition['type'] : 'and'; |
| 147 |
|
| 148 |
if ( 'or' === $condition_type ) { |
| 149 |
$is_valid = false; |
| 150 |
} |
| 151 |
|
| 152 |
foreach ( $conditions as $condition ) { |
| 153 |
if ( is_array( $condition['condition_value'] ) && ! empty( $condition['condition_value'][0]['value'] ) ) { |
| 154 |
$condition['condition_value'] = sellkit_get_multi_select_values( $condition['condition_value'] ); |
| 155 |
} |
| 156 |
|
| 157 |
$result = sellkit_condition_match( $condition['condition_subject'], $condition['condition_operator'], $condition['condition_value'] ); |
| 158 |
|
| 159 |
if ( is_wp_error( $result ) ) { |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! $result ) { |
| 164 |
$is_valid = false; |
| 165 |
} |
| 166 |
|
| 167 |
if ( $result && 'or' === $condition_type ) { |
| 168 |
$is_valid = true; |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if ( true === $is_valid ) { |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
if ( ! function_exists( 'array_key_last' ) ) { |
| 181 |
/** |
| 182 |
* Adds support for array_key_last function for lower php than 7.3.0 & WP lower than 5.9.0. |
| 183 |
* |
| 184 |
* @param array $array array. |
| 185 |
*/ |
| 186 |
function array_key_last( $array ) { |
| 187 |
if ( empty( $array ) ) { |
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
end( $array ); |
| 192 |
return key( $array ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
add_action( 'woocommerce_removed_coupon', 'sellkit_set_cookie_after_auto_apply_coupon_removed', 10, 1 ); |
| 197 |
|
| 198 |
/** |
| 199 |
* Create cookie for deleted auto apply coupon. |
| 200 |
* |
| 201 |
* @param string $coupon_code coupon code. |
| 202 |
*/ |
| 203 |
function sellkit_set_cookie_after_auto_apply_coupon_removed( $coupon_code ) { |
| 204 |
$cookie_value = null; |
| 205 |
$parsed_url = wp_parse_url( site_url() ); |
| 206 |
$domain = $parsed_url['host']; |
| 207 |
|
| 208 |
if ( isset( $_COOKIE['sellkit_rejected_coupon'] ) ) { |
| 209 |
$cookie_value = sanitize_text_field( wp_unslash( $_COOKIE['sellkit_rejected_coupon'] ) ); |
| 210 |
} |
| 211 |
|
| 212 |
$cookie_value = $cookie_value . ',' . $coupon_code; |
| 213 |
setcookie( 'sellkit_rejected_coupon', $cookie_value, time() + 600, '/', $domain ); |
| 214 |
} |
| 215 |
|
| 216 |
add_action( 'woocommerce_checkout_order_processed', 'sellkit_remove_rejected_auto_apply_coupon_cookie' ); |
| 217 |
|
| 218 |
/** |
| 219 |
* Remove cookie after make an order. |
| 220 |
*/ |
| 221 |
function sellkit_remove_rejected_auto_apply_coupon_cookie() { |
| 222 |
if ( isset( $_COOKIE['sellkit_rejected_coupon'] ) ) { |
| 223 |
$parsed_url = wp_parse_url( site_url() ); |
| 224 |
$domain = $parsed_url['host']; |
| 225 |
setcookie( 'sellkit_rejected_coupon', '', time() - 600, '/', $domain ); |
| 226 |
} |
| 227 |
} |
| 228 |
|