core/classes
parts
1 year ago
class-admin-notices.php
8.8 KB
3 months ago
class-block.php
3.2 KB
3 years ago
class-cache.php
922 B
3 years ago
class-dashboard-modals.php
4.2 KB
3 years ago
class-deactivation.php
1.6 KB
3 years ago
class-email-encoder.php
2.2 KB
3 years ago
class-email-message.php
7.7 KB
3 years ago
class-export-controller.php
3.5 KB
3 years ago
class-form-admin.php
19.7 KB
3 years ago
class-form-assets.php
6.7 KB
3 years ago
class-form-controller.php
21.4 KB
3 months ago
class-form-email.php
7.5 KB
3 years ago
class-form-messages.php
7.8 KB
3 years ago
class-form-option-limiter.php
8.1 KB
3 months ago
class-form-part-library.php
7.9 KB
2 years ago
class-form-part.php
884 B
3 years ago
class-form-setup.php
9.3 KB
3 years ago
class-form-shuffle.php
2.9 KB
2 years ago
class-form-styles.php
29.0 KB
1 year ago
class-happyforms-core.php
16.9 KB
2 years ago
class-happyforms-widget.php
3.0 KB
3 years ago
class-import-controller.php
3.1 KB
3 years ago
class-session.php
4.4 KB
3 years ago
class-tracking.php
2.0 KB
3 years ago
class-validation-messages.php
5.8 KB
3 years ago
class-wp-customize-form-manager.php
11.5 KB
9 months ago
class-session.php in Happyforms – Form Builder for WordPress: Drag & Drop Contact Forms, Surveys, Payments & Multipurpose Forms trunk, at core/classes/class-session.php
| 1 | <?php |
| 2 | |
| 3 | class HappyForms_Session { |
| 4 | |
| 5 | /** |
| 6 | * The singleton instance. |
| 7 | * |
| 8 | * @since 1.0 |
| 9 | * |
| 10 | * @var HappyForms_Session |
| 11 | */ |
| 12 | private static $instance; |
| 13 | |
| 14 | /** |
| 15 | * The list of registered errors. |
| 16 | * |
| 17 | * @since 1.4.6 |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | private $errors = array(); |
| 22 | |
| 23 | /** |
| 24 | * The list of registered notices. |
| 25 | * |
| 26 | * @since 1.4.6 |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | private $notices = array(); |
| 31 | |
| 32 | /** |
| 33 | * A list of submit values. |
| 34 | * |
| 35 | * @since 1.4.6 |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | private $values = array(); |
| 40 | |
| 41 | private $states = array(); |
| 42 | |
| 43 | /** |
| 44 | * Current form step |
| 45 | * |
| 46 | * @var int |
| 47 | */ |
| 48 | private $step = 0; |
| 49 | |
| 50 | /** |
| 51 | * Current literal step |
| 52 | * |
| 53 | * @var int |
| 54 | */ |
| 55 | private $literal_step = ''; |
| 56 | |
| 57 | /** |
| 58 | * The singleton constructor. |
| 59 | * |
| 60 | * @since 1.0 |
| 61 | * |
| 62 | * @return HappyForms_Message_Admin |
| 63 | */ |
| 64 | public static function instance() { |
| 65 | if ( is_null( self::$instance ) ) { |
| 66 | self::$instance = new self(); |
| 67 | } |
| 68 | |
| 69 | return self::$instance; |
| 70 | } |
| 71 | |
| 72 | public function add_error( $key, $message, $component = false ) { |
| 73 | if ( false !== $component ) { |
| 74 | if ( ! isset( $this->errors[$key] ) || ! is_array( $this->errors[$key] ) ) { |
| 75 | $this->errors[$key] = array(); |
| 76 | } |
| 77 | $this->errors[$key][$component] = $message; |
| 78 | } else { |
| 79 | $this->errors[$key] = $message; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public function remove_error( $key ) { |
| 84 | if ( isset( $this->errors[$key] ) ) { |
| 85 | unset( $this->errors[$key] ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Add a notice to be displayed on the next refresh. |
| 91 | * |
| 92 | * @since 1.0 |
| 93 | * |
| 94 | * @return void |
| 95 | */ |
| 96 | public function add_notice( $key, $message ) { |
| 97 | $this->notices[$key] = $message; |
| 98 | } |
| 99 | |
| 100 | public function add_state( $key, $state ) { |
| 101 | $this->states[$key][] = $state; |
| 102 | } |
| 103 | |
| 104 | public function get_states( $location = '' ) { |
| 105 | $states = array(); |
| 106 | |
| 107 | if ( isset( $this->states[$location] ) ) { |
| 108 | $states = $this->states[$location]; |
| 109 | } |
| 110 | |
| 111 | return $states; |
| 112 | } |
| 113 | |
| 114 | public function remove_notice( $key ) { |
| 115 | if ( isset( $this->notices[$key] ) ) { |
| 116 | unset( $this->notices[$key] ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public function add_value( $key, $value ) { |
| 121 | $this->values[$key] = $value; |
| 122 | } |
| 123 | |
| 124 | public function remove_value( $key ) { |
| 125 | if ( isset( $this->values[$key] ) ) { |
| 126 | unset( $this->values[$key] ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the messages for the given form and location. |
| 132 | * |
| 133 | * @since 1.0 |
| 134 | * |
| 135 | * @param string $location The location to fetch messages for. |
| 136 | * |
| 137 | * @return array |
| 138 | */ |
| 139 | public function get_messages( $location = '' ) { |
| 140 | $messages = array(); |
| 141 | |
| 142 | if ( isset( $this->notices[$location] ) ) { |
| 143 | $messages[] = array( |
| 144 | 'type' => 'success', |
| 145 | 'message' => $this->notices[$location], |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | if ( isset( $this->errors[$location] ) ) { |
| 150 | $messages[] = array( |
| 151 | 'type' => 'error', |
| 152 | 'message' => $this->errors[$location], |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | return $messages; |
| 157 | } |
| 158 | |
| 159 | public function get_value( $location = '', $component = false ) { |
| 160 | $value = false; |
| 161 | |
| 162 | if ( isset( $this->values[$location] ) ) { |
| 163 | $value = $this->values[$location]; |
| 164 | |
| 165 | if ( false !== $component ) { |
| 166 | $value = isset( $value[$component] ) ? $value[$component] : ''; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return $value; |
| 171 | } |
| 172 | |
| 173 | public function clear_values() { |
| 174 | $this->values = array(); |
| 175 | } |
| 176 | |
| 177 | public function current_step( $literal = false ) { |
| 178 | return $literal ? $this->literal_step : $this->step; |
| 179 | } |
| 180 | |
| 181 | public function next_step() { |
| 182 | $this->step = $this->step + 1; |
| 183 | } |
| 184 | |
| 185 | public function previous_step() { |
| 186 | $this->step = max( 0, $this->step - 1 ); |
| 187 | } |
| 188 | |
| 189 | public function set_step( $step ) { |
| 190 | $this->literal_step = $step; |
| 191 | $this->step = intval( $step ); |
| 192 | } |
| 193 | |
| 194 | public function reset_step() { |
| 195 | $this->step = 0; |
| 196 | } |
| 197 | |
| 198 | public function serialize() { |
| 199 | $data = array_merge( $this->values, array( |
| 200 | 'step' => $this->step, |
| 201 | ) ); |
| 202 | |
| 203 | $serialized = wp_slash( json_encode( $data, JSON_UNESCAPED_UNICODE ) ); |
| 204 | |
| 205 | return $serialized; |
| 206 | } |
| 207 | |
| 208 | public function unserialize( $data ) { |
| 209 | $data = json_decode( $data, true ); |
| 210 | |
| 211 | if ( ! isset( $data['step'] ) ) { |
| 212 | $data['step'] = 0; |
| 213 | } else { |
| 214 | $data['step'] = intval( $data['step'] ); |
| 215 | } |
| 216 | |
| 217 | return $data; |
| 218 | } |
| 219 | |
| 220 | public function from_data( $data, $apply_step = false ) { |
| 221 | $step = 0; |
| 222 | |
| 223 | if ( isset( $data['step'] ) ) { |
| 224 | $step = intval( $data['step'] ); |
| 225 | unset( $data['step'] ); |
| 226 | } |
| 227 | |
| 228 | foreach( $data as $key => $value ) { |
| 229 | $this->add_value( $key, $value ); |
| 230 | } |
| 231 | |
| 232 | if ( $apply_step ) { |
| 233 | $this->set_step( $step ); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | if ( ! function_exists( 'happyforms_get_session' ) ): |
| 240 | /** |
| 241 | * Get the HappyForms_Session class instance. |
| 242 | * |
| 243 | * @since 1.0 |
| 244 | * |
| 245 | * @return HappyForms_Session |
| 246 | */ |
| 247 | function happyforms_get_session() { |
| 248 | return HappyForms_Session::instance(); |
| 249 | } |
| 250 | |
| 251 | endif; |
| 252 |