PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.5.4
JetFormBuilder — Dynamic Blocks Form Builder v3.5.4
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / promo-banner / storage.php
jetformbuilder / modules / promo-banner Last commit date
assets 1 year ago module.php 1 year ago storage.php 1 year ago
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