storage.php
130 lines
| 1 | <?php |
| 2 | namespace JFB_Modules\Promo_Banner; |
| 3 | |
| 4 | // If this file is called directly, abort. |
| 5 | if ( ! defined( 'WPINC' ) ) { |
| 6 | die; |
| 7 | } |
| 8 | |
| 9 | class Storage { |
| 10 | |
| 11 | protected $storage_key = null; |
| 12 | protected $api_url = 'https://account.jetformbuilder.com/wp-json/jfb/v1/promo'; |
| 13 | |
| 14 | public function __construct( $storage_key ) { |
| 15 | $this->storage_key = $storage_key; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Get hash for banner HTML |
| 20 | * |
| 21 | * @param string $banner_html banner content |
| 22 | * @return string |
| 23 | */ |
| 24 | public function get_banner_hash( $banner_html ) { |
| 25 | return md5( $banner_html ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Check if given banner was dismissed before by it's hash |
| 30 | * |
| 31 | * @param string $banner_hash Hash string. |
| 32 | * @return boolean |
| 33 | */ |
| 34 | public function is_banner_dismissed( $banner_hash ) { |
| 35 | |
| 36 | // banners visible only for loggen in users |
| 37 | if ( ! is_user_logged_in() ) { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | $dismissed_banners = get_user_meta( get_current_user_id(), $this->get_storage_key(), false ); |
| 42 | |
| 43 | if ( ! empty( $dismissed_banners ) && in_array( $banner_hash, $dismissed_banners ) ) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Add banner to the list of dismissed banners for current user |
| 52 | * |
| 53 | * @param [type] $banner_hash [description] |
| 54 | * @return [type] [description] |
| 55 | */ |
| 56 | public function dismiss_banner( $banner_hash ) { |
| 57 | add_user_meta( get_current_user_id(), $this->get_storage_key(), $banner_hash ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Transient cache key |
| 62 | * |
| 63 | * @return [type] [description] |
| 64 | */ |
| 65 | public function get_storage_key() { |
| 66 | return $this->storage_key; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns promo discount value with '%' sign in the end |
| 71 | * |
| 72 | * @return [type] [description] |
| 73 | */ |
| 74 | public function get_promo_value() { |
| 75 | |
| 76 | $promo_data = $this->get_promo_data(); |
| 77 | return isset( $promo_data['promo'] ) ? $promo_data['promo'] : false; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get banner HTML from transient cache or remotely. |
| 82 | * |
| 83 | * @return string|int |
| 84 | */ |
| 85 | public function get_banner_html() { |
| 86 | |
| 87 | $promo_data = $this->get_promo_data(); |
| 88 | return isset( $promo_data['banner'] ) ? $promo_data['banner'] : false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get all data for current promos |
| 93 | * |
| 94 | * @return array |
| 95 | */ |
| 96 | public function get_promo_data() { |
| 97 | |
| 98 | $data = get_transient( $this->get_storage_key() ); |
| 99 | |
| 100 | if ( false === $data ) { |
| 101 | $data = $this->get_remote_data(); |
| 102 | } |
| 103 | |
| 104 | set_transient( $this->get_storage_key(), $data, 6 * HOUR_IN_SECONDS ); |
| 105 | |
| 106 | return ! empty( $data ) && is_array( $data ) ? $data : array(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get remote content of the banner from API |
| 111 | * If there is no active promo right now - will be returned -1 |
| 112 | * |
| 113 | * @return string|int |
| 114 | */ |
| 115 | public function get_remote_data() { |
| 116 | |
| 117 | $response = wp_remote_get( $this->api_url ); |
| 118 | $body = wp_remote_retrieve_body( $response ); |
| 119 | |
| 120 | if ( $body && ! is_wp_error( $body ) ) { |
| 121 | $body = json_decode( $body, true ); |
| 122 | } else { |
| 123 | $body = array(); |
| 124 | } |
| 125 | |
| 126 | return is_array( $body ) ? $body : array(); |
| 127 | } |
| 128 | |
| 129 | } |
| 130 |