admin
9 years ago
forms
9 years ago
integrations
10 years ago
mailchimp
10 years ago
views
9 years ago
class-api.php
9 years ago
class-array-bag.php
10 years ago
class-container.php
10 years ago
class-debug-log-reader.php
9 years ago
class-debug-log.php
9 years ago
class-dynamic-content-tags.php
10 years ago
class-field-formatter.php
10 years ago
class-field-guesser.php
10 years ago
class-field-map.php
9 years ago
class-mailchimp.php
10 years ago
class-plugin.php
10 years ago
class-queue-job.php
10 years ago
class-queue.php
9 years ago
class-request.php
10 years ago
class-tools.php
10 years ago
class-validator.php
10 years ago
class-visitor-tracking.php
10 years ago
default-actions.php
10 years ago
default-filters.php
10 years ago
deprecated-functions.php
10 years ago
functions.php
9 years ago
class-visitor-tracking.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class MC4WP_Visitor_Tracking |
| 5 | * |
| 6 | * @ignore |
| 7 | * @access private |
| 8 | * @deprecated 3.1 |
| 9 | */ |
| 10 | class MC4WP_Visitor_Tracking { |
| 11 | |
| 12 | /** |
| 13 | * @var array |
| 14 | */ |
| 15 | protected $data; |
| 16 | |
| 17 | /** |
| 18 | * @const string |
| 19 | */ |
| 20 | const COOKIE_NAME = '_mc4wp'; |
| 21 | |
| 22 | /** |
| 23 | * Add hooks |
| 24 | * |
| 25 | * @todo Hook into integration success as well |
| 26 | */ |
| 27 | public function add_hooks() { |
| 28 | add_action( 'mc4wp_form_subscribed', array( $this, 'on_form_success' ) ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @hooked `mc4wp_form_subscribed` |
| 33 | * @param MC4WP_Form $form |
| 34 | */ |
| 35 | public function on_form_success( MC4WP_Form $form ) { |
| 36 | $this->save( $form->data ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param string $key |
| 41 | * @param null $default |
| 42 | * @return mixed |
| 43 | */ |
| 44 | public function get_field( $key, $default = null ) { |
| 45 | $data = $this->load(); |
| 46 | |
| 47 | if( isset( $data[ $key ] ) ) { |
| 48 | return $data[ $key ]; |
| 49 | } |
| 50 | |
| 51 | return $default; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param array $data |
| 56 | * |
| 57 | * @todo Now, all previous is data is overwritten whenever this method is called. |
| 58 | */ |
| 59 | public function save( array $data ) { |
| 60 | $this->data = $data; |
| 61 | |
| 62 | $timestamp = strtotime( '+90 days' ); |
| 63 | |
| 64 | /** |
| 65 | * Filters the total expiration time for the tracking cookie. |
| 66 | * |
| 67 | * Defaults to 90 days in the future. |
| 68 | * |
| 69 | * @since 3.0 |
| 70 | * @param int $timestamp |
| 71 | */ |
| 72 | $expiration_time = apply_filters( 'mc4wp_cookie_expiration_time', $timestamp ); |
| 73 | |
| 74 | setcookie( self::COOKIE_NAME, json_encode( $data ), $expiration_time, '/' ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Load stored data from cookie. |
| 79 | */ |
| 80 | public function load() { |
| 81 | |
| 82 | if( empty( $this->data ) ) { |
| 83 | if( ! empty( $_COOKIE[ self::COOKIE_NAME ] ) ) { |
| 84 | $raw = stripslashes( $_COOKIE[ self::COOKIE_NAME ] ); |
| 85 | $data = json_decode( $raw, true ); |
| 86 | |
| 87 | if( is_array( $data ) ) { |
| 88 | $this->data = mc4wp_sanitize_deep( $data ); |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $this->data; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | } |