class-opt-in-condition-abstract.php
5 months ago
class-opt-in-condition-archive-pages.php
5 months ago
class-opt-in-condition-categories.php
5 months ago
class-opt-in-condition-cookie-set.php
3 years ago
class-opt-in-condition-cpt.php
4 years ago
class-opt-in-condition-from-referrer.php
4 years ago
class-opt-in-condition-on-browser.php
3 years ago
class-opt-in-condition-on-url.php
5 months ago
class-opt-in-condition-page-templates.php
4 years ago
class-opt-in-condition-pages.php
5 months ago
class-opt-in-condition-posts.php
4 years ago
class-opt-in-condition-shown-less-than.php
3 years ago
class-opt-in-condition-source-of-arrival.php
5 months ago
class-opt-in-condition-tags.php
5 months ago
class-opt-in-condition-user-registration.php
5 months ago
class-opt-in-condition-user-roles.php
5 months ago
class-opt-in-condition-visitor-commented.php
5 years ago
class-opt-in-condition-visitor-country.php
5 years ago
class-opt-in-condition-visitor-device.php
6 years ago
class-opt-in-condition-visitor-logged-in-status.php
5 months ago
class-opt-in-condition-wc-archive-pages.php
5 months ago
class-opt-in-condition-wc-categories.php
5 months ago
class-opt-in-condition-wc-pages.php
5 months ago
class-opt-in-condition-wc-static-pages.php
5 months ago
class-opt-in-condition-wc-tags.php
5 months ago
class-opt-in-condition-wp-conditions.php
5 months ago
class-opt-in-condition-cookie-set.php
182 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Opt_In_Condition_Cookie_Set class. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since 4.2.1 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Opt_In_Condition_Cookie_Set |
| 11 | * Handles the current cookie name and/or value |
| 12 | * |
| 13 | * @since 4.2.1 |
| 14 | */ |
| 15 | class Opt_In_Condition_Cookie_Set extends Opt_In_Condition_Abstract { |
| 16 | /** |
| 17 | * Prefix for all the condintions methods. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | private $function_prefix = 'hustle_cookie_set_'; |
| 22 | |
| 23 | /** |
| 24 | * Helper variable to keep cookie keys. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | private $cookie_keys; |
| 29 | |
| 30 | /** |
| 31 | * Helper variable to keep browser cookie. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | private $browser_cookie_value; |
| 36 | |
| 37 | /** |
| 38 | * Returns whether the condition was met. |
| 39 | * |
| 40 | * @since 4.2.1 |
| 41 | */ |
| 42 | public function is_allowed() { |
| 43 | if ( empty( $this->args->cookie_name ) || ! isset( $this->args->filter_type ) ) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | $this->cookie_keys = array_keys( $_COOKIE ); |
| 48 | // Value is only used for bool operations. |
| 49 | $this->browser_cookie_value = isset( $_COOKIE[ $this->args->cookie_name ] ) |
| 50 | ? sanitize_text_field( wp_unslash( $_COOKIE[ $this->args->cookie_name ] ) ) : ''; |
| 51 | return $this->is_cookie_set(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Launches the needed function accoridng to what user set in admin panel. |
| 56 | * |
| 57 | * @return boolean |
| 58 | */ |
| 59 | private function is_cookie_set() { |
| 60 | $method_name = $this->function_prefix . $this->args->cookie_value_conditions; |
| 61 | |
| 62 | if ( ! method_exists( $this, $method_name ) ) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if ( 'exists' === $this->args->filter_type ) { |
| 67 | $is_cookie_set = call_user_func( array( $this, $method_name ) ); |
| 68 | return $is_cookie_set; |
| 69 | } else { |
| 70 | $method_name = $this->function_prefix . 'anything'; |
| 71 | $is_cookie_set = call_user_func( array( $this, $method_name ) ); |
| 72 | |
| 73 | return ! $is_cookie_set; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks if cookie itself is set. Value doesn't matters. |
| 79 | * |
| 80 | * @return boolean |
| 81 | */ |
| 82 | private function hustle_cookie_set_anything() { |
| 83 | return in_array( $this->args->cookie_name, $this->cookie_keys, true ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Checks if cookie is set and value is equal. |
| 88 | */ |
| 89 | private function hustle_cookie_set_equals() { |
| 90 | return ! empty( $this->browser_cookie_value ) && |
| 91 | $this->browser_cookie_value === $this->args->cookie_value; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Checks if cookie is set and value is not equal. |
| 96 | */ |
| 97 | private function hustle_cookie_set_doesnt_equals() { |
| 98 | return ! empty( $this->browser_cookie_value ) && |
| 99 | $this->browser_cookie_value !== $this->args->cookie_value; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Checks if cookie is set and value contains the value set by user. |
| 104 | */ |
| 105 | private function hustle_cookie_set_contains() { |
| 106 | return ! empty( $this->browser_cookie_value ) && |
| 107 | ! empty( $this->args->cookie_value ) && |
| 108 | strpos( $this->browser_cookie_value, $this->args->cookie_value ) !== false; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Checks if cookie is set and value does not contains the value set by user. |
| 113 | */ |
| 114 | private function hustle_cookie_set_doesnt_contain() { |
| 115 | return ! empty( $this->browser_cookie_value ) && |
| 116 | ! empty( $this->args->cookie_value ) && |
| 117 | strpos( $this->browser_cookie_value, $this->args->cookie_value ) === false; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Checks if cookie is set and value is less than value set by user. |
| 122 | */ |
| 123 | private function hustle_cookie_set_less_than() { |
| 124 | return ! empty( $this->browser_cookie_value ) && |
| 125 | ! empty( $this->args->cookie_value ) && |
| 126 | is_numeric( $this->browser_cookie_value ) && |
| 127 | is_numeric( $this->args->cookie_value ) && |
| 128 | $this->browser_cookie_value < $this->args->cookie_value; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Checks if cookie is set and value is less or equal than value set by user. |
| 133 | */ |
| 134 | private function hustle_cookie_set_less_equal_than() { |
| 135 | return ! empty( $this->browser_cookie_value ) && |
| 136 | ! empty( $this->args->cookie_value ) && |
| 137 | is_numeric( $this->browser_cookie_value ) && |
| 138 | is_numeric( $this->args->cookie_value ) && |
| 139 | $this->browser_cookie_value <= $this->args->cookie_value; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Checks if cookie is set and value is greater than value set by user. |
| 144 | */ |
| 145 | private function hustle_cookie_set_greater_than() { |
| 146 | return ! empty( $this->browser_cookie_value ) && |
| 147 | ! empty( $this->args->cookie_value ) && |
| 148 | is_numeric( $this->browser_cookie_value ) && |
| 149 | is_numeric( $this->args->cookie_value ) && |
| 150 | $this->browser_cookie_value > $this->args->cookie_value; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Checks if cookie is set and value is greater or equal than value set by user. |
| 155 | */ |
| 156 | private function hustle_cookie_set_greater_equal_than() { |
| 157 | return ! empty( $this->browser_cookie_value ) && |
| 158 | ! empty( $this->args->cookie_value ) && |
| 159 | is_numeric( $this->browser_cookie_value ) && |
| 160 | is_numeric( $this->args->cookie_value ) && |
| 161 | $this->browser_cookie_value >= $this->args->cookie_value; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Checks if cookie is set and it value matches the pattern set by the user. |
| 166 | */ |
| 167 | private function hustle_cookie_set_matches_pattern() { |
| 168 | return ! empty( $this->browser_cookie_value ) && |
| 169 | ! empty( $this->args->cookie_value ) && |
| 170 | preg_match( $this->args->cookie_value, $this->browser_cookie_value ) === 1; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Checks if cookie is set and it value doesn't matches the pattern set by the user. |
| 175 | */ |
| 176 | private function hustle_cookie_set_doesnt_match_pattern() { |
| 177 | return ! empty( $this->browser_cookie_value ) && |
| 178 | ! empty( $this->args->cookie_value ) && |
| 179 | preg_match( $this->args->cookie_value, $this->browser_cookie_value ) === 0; |
| 180 | } |
| 181 | } |
| 182 |