| 1 |
<?php |
| 2 |
if ( ! function_exists( 'sellkit_update_option' ) ) { |
| 3 |
/** |
| 4 |
* Update option from options storage. |
| 5 |
* |
| 6 |
* @param string $option Option name. |
| 7 |
* @param mixed $value Update value. |
| 8 |
* |
| 9 |
* @return boolean False if value was not updated and true if value was updated. |
| 10 |
*/ |
| 11 |
function sellkit_update_option( $option, $value ) { |
| 12 |
$options = get_option( 'sellkit', [] ); |
| 13 |
|
| 14 |
// No need to update the same value. |
| 15 |
if ( isset( $options[ $option ] ) && $value === $options[ $option ] ) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
|
| 19 |
// Update the option. |
| 20 |
$options[ $option ] = $value; |
| 21 |
update_option( 'sellkit', $options ); |
| 22 |
|
| 23 |
return true; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
if ( ! function_exists( 'sellkit_get_option' ) ) { |
| 28 |
/** |
| 29 |
* Get option from options storage. |
| 30 |
* |
| 31 |
* @param string $option Option name. |
| 32 |
* @param boolean $default Default value. |
| 33 |
* |
| 34 |
* @return mixed Value set for the option. |
| 35 |
*/ |
| 36 |
function sellkit_get_option( $option, $default = false ) { |
| 37 |
$options = get_option( 'sellkit', [] ); |
| 38 |
|
| 39 |
if ( ! isset( $options[ $option ] ) ) { |
| 40 |
return $default; |
| 41 |
} |
| 42 |
|
| 43 |
return $options[ $option ]; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! function_exists( 'sellkit_htmlspecialchars' ) ) { |
| 48 |
/** |
| 49 |
* Sellkit html special chars is created because FILTER_SANITIZE_STRING is deprecated. |
| 50 |
* |
| 51 |
* @since 1.2.1 |
| 52 |
* @param string $input_type Type of input. |
| 53 |
* @param string $param Parameter key. |
| 54 |
*/ |
| 55 |
function sellkit_htmlspecialchars( $input_type, $param ) { |
| 56 |
$value = ''; |
| 57 |
|
| 58 |
if ( INPUT_GET === $input_type ) { |
| 59 |
$value = ! empty( $_GET[ $param ] ) ? htmlspecialchars( $_GET[ $param ] ) : ''; //phpcs:ignore |
| 60 |
} |
| 61 |
|
| 62 |
if ( INPUT_POST === $input_type ) { |
| 63 |
$value = ! empty( $_POST[ $param ] ) ? htmlspecialchars( $_POST[ $param ] ) : ''; //phpcs:ignore |
| 64 |
} |
| 65 |
|
| 66 |
if ( INPUT_COOKIE === $input_type ) { |
| 67 |
$value = ! empty( $_COOKIE[ $param ] ) ? htmlspecialchars( $_COOKIE[ $param ] ) : ''; //phpcs:ignore |
| 68 |
} |
| 69 |
|
| 70 |
return $value; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Delete option from options storage. |
| 76 |
* |
| 77 |
* @param string $option Option name. |
| 78 |
* |
| 79 |
* @return boolean False if value was not deleted and true if value was deleted. |
| 80 |
*/ |
| 81 |
function sellkit_delete_option( $option ) { |
| 82 |
$options = get_option( 'sellkit', [] ); |
| 83 |
|
| 84 |
// Option not exist. |
| 85 |
if ( ! isset( $options[ $option ] ) ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
// Remove the option. |
| 90 |
unset( $options[ $option ] ); |
| 91 |
update_option( 'sellkit', $options ); |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get multi-select values. |
| 98 |
* |
| 99 |
* @since 1.1.0 |
| 100 |
* @param array $array Multi select data. |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
function sellkit_get_multi_select_values( $array ) { |
| 104 |
$values = []; |
| 105 |
|
| 106 |
foreach ( $array as $item ) { |
| 107 |
$values[] = $item['value']; |
| 108 |
} |
| 109 |
|
| 110 |
return $values; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Checks conditions validation. |
| 115 |
* |
| 116 |
* @since 1.1.0 |
| 117 |
* @SuppressWarnings(PHPMD.NPathComplexity) |
| 118 |
* @param array $conditions Conditions data. |
| 119 |
*/ |
| 120 |
function sellkit_conditions_validation( $conditions ) { |
| 121 |
$is_valid = true; |
| 122 |
$condition_type = ! empty( $conditions[1]['type'] ) ? $conditions[1]['type'] : 'and'; |
| 123 |
|
| 124 |
if ( 'or' === $condition_type ) { |
| 125 |
$is_valid = false; |
| 126 |
} |
| 127 |
|
| 128 |
foreach ( $conditions as $condition ) { |
| 129 |
if ( is_array( $condition['condition_value'] ) && ! empty( $condition['condition_value'][0]['value'] ) ) { |
| 130 |
$condition['condition_value'] = sellkit_get_multi_select_values( $condition['condition_value'] ); |
| 131 |
} |
| 132 |
|
| 133 |
$result = sellkit_condition_match( $condition['condition_subject'], $condition['condition_operator'], $condition['condition_value'] ); |
| 134 |
|
| 135 |
if ( is_wp_error( $result ) ) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! $result ) { |
| 140 |
$is_valid = false; |
| 141 |
} |
| 142 |
|
| 143 |
if ( $result && 'or' === $condition_type ) { |
| 144 |
$is_valid = true; |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if ( true === $is_valid ) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! function_exists( 'array_key_last' ) ) { |
| 157 |
/** |
| 158 |
* Adds support for array_key_last function for lower php than 7.3.0 & WP lower than 5.9.0. |
| 159 |
* |
| 160 |
* @param array $array array. |
| 161 |
*/ |
| 162 |
function array_key_last( $array ) { |
| 163 |
if ( empty( $array ) ) { |
| 164 |
return null; |
| 165 |
} |
| 166 |
|
| 167 |
end( $array ); |
| 168 |
return key( $array ); |
| 169 |
} |
| 170 |
} |
| 171 |
|