PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.14
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.14
2.2.14 2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
table-builder-block / scoped / deps / wpmet / utility-package / src / Banner / Banner.php

Banner.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables 2.2.14, at scoped/deps/wpmet/utility-package/src/Banner/Banner.php

204 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TableBuilderScopedDeps\Wpmet\UtilityPackage\Banner;
4
5 defined('ABSPATH') || exit;
6 use TableBuilderScopedDeps\Wpmet\UtilityPackage\Helper\Helper as UtilsHelper;
7 /**
8 * Showing Banners
9 * other stuffs
10 * Class Banner
11 * @package Wpmet\UtilityPackage
12 */
13 class Banner
14 {
15 // protected $script_version = '2.1.0';
16 protected $key = 'wpmet_banner';
17 protected $data;
18 protected $last_check;
19 protected $check_interval = 3600 * 6;
20 protected $plugin_screens;
21 protected $text_domain;
22 protected $filter_string;
23 protected $filter_array = array();
24 protected $api_url;
25 public function get_version()
26 {
27 // return $this->script_version;
28 return UtilsHelper::get_pac_version();
29 }
30 public function get_script_location()
31 {
32 return __FILE__;
33 }
34 public function call()
35 {
36 add_action('admin_head', array($this, 'display_content'));
37 }
38 public function display_content()
39 {
40 $this->get_data();
41 if (!empty($this->data->error)) {
42 return;
43 }
44 if (empty($this->data)) {
45 return;
46 }
47 foreach ($this->data as $content) {
48 if (!empty($this->filter_array) && $this->in_blacklist($content, $this->filter_array)) {
49 continue;
50 }
51 if ($content->start <= time() && time() <= $content->end) {
52 $screen = get_current_screen();
53 if ($this->is_correct_screen_to_show($content->screen, $screen->id) && class_exists('TableBuilderScopedDeps\Wpmet\UtilityPackage\Notice\Notice')) {
54 $inline_css = '';
55 $banner_unique_id = sanitize_key(isset($content->data->unique_key) && $content->data->unique_key != '' ? $content->data->unique_key : $content->id);
56 if (!empty($content->data->style_css)) {
57 $inline_css = ' style="' . esc_attr($content->data->style_css) . '"';
58 }
59 $instance = \TableBuilderScopedDeps\Wpmet\UtilityPackage\Notice\Notice::instance('wpmet-jhanda', $banner_unique_id)->set_dismiss('global', 3600 * 24 * 15);
60 if ($content->type == 'banner') {
61 $this->init_banner($content, $instance, $inline_css);
62 }
63 if ($content->type == 'notice') {
64 $this->init_notice($content, $instance, $inline_css);
65 }
66 }
67 }
68 }
69 }
70 private function init_notice($content, $instance, $inline_css)
71 {
72 $instance->set_message(wp_kses($content->data->notice_body, UtilsHelper::get_kses_array()));
73 if ($content->data->notice_image != '') {
74 $instance->set_logo(esc_url_raw($content->data->notice_image));
75 }
76 if ($content->data->button_text != '') {
77 $instance->set_button(array(
78 'default_class' => 'button',
79 'class' => 'button-secondary button-small',
80 // button-primary button-secondary button-small button-large button-link
81 'text' => sanitize_text_field($content->data->button_text),
82 'url' => esc_url_raw($content->data->button_link),
83 ));
84 }
85 $instance->call();
86 }
87 private function init_banner($content, $instance, $inline_css)
88 {
89 $html = '<a target="_blank" rel="noopener noreferrer"' . $inline_css . ' class="wpmet-jhanda-href" href="' . esc_url($content->data->banner_link) . '"><img style="display: block;margin: 0 auto;" src="' . esc_url($content->data->banner_image) . '" alt="" /></a>';
90 $instance->set_gutter(\false)->set_html($html)->call();
91 }
92 private function in_whitelist($conf, $list)
93 {
94 // The remote API is expected to always send a (possibly empty)
95 // "whitelist" field, but don't throw a PHP warning if a response
96 // ever omits it.
97 $match = $conf->data->whitelist ?? '';
98 if (empty($match)) {
99 return \true;
100 }
101 $match_arr = explode(',', $match);
102 foreach ($list as $word) {
103 if (in_array($word, $match_arr)) {
104 return \true;
105 }
106 }
107 return \false;
108 }
109 private function in_blacklist($conf, $list)
110 {
111 // Same defensive fallback as in_whitelist() above.
112 $match = $conf->data->blacklist ?? '';
113 if (empty($match)) {
114 return \false;
115 }
116 $match_arr = explode(',', $match);
117 foreach ($match_arr as $idx => $item) {
118 $match_arr[$idx] = trim($item);
119 }
120 foreach ($list as $word) {
121 if (in_array($word, $match_arr)) {
122 return \true;
123 }
124 }
125 return \false;
126 }
127 public function is_test($is_test = \false)
128 {
129 if ($is_test === \true) {
130 $this->check_interval = 1;
131 }
132 return $this;
133 }
134 public function set_text_domain($text_domain)
135 {
136 $this->text_domain = $text_domain;
137 return $this;
138 }
139 public function set_filter($filter_string)
140 {
141 $this->filter_string = $filter_string;
142 if (!empty($filter_string)) {
143 $filter = explode(',', $this->filter_string);
144 foreach ($filter as $id => $item) {
145 $this->filter_array[$id] = trim($item);
146 }
147 }
148 return $this;
149 }
150 public function set_api_url($url)
151 {
152 $this->api_url = $url;
153 return $this;
154 }
155 public function set_plugin_screens($screen)
156 {
157 $this->plugin_screens[] = $screen;
158 return $this;
159 }
160 private function get_data()
161 {
162 $this->data = get_option($this->text_domain . '__banner_data');
163 $this->data = $this->data == '' ? array() : $this->data;
164 $this->last_check = get_option($this->text_domain . '__banner_last_check');
165 $this->last_check = $this->last_check == '' ? 0 : $this->last_check;
166 if ($this->check_interval + $this->last_check < time()) {
167 $response = wp_remote_get($this->api_url . '/cache/' . $this->text_domain . '.json?nocache=' . time(), array('timeout' => 10, 'httpversion' => '1.1', 'limit_response_size' => MB_IN_BYTES));
168 // Bail on a transport error or a non-200 response before touching the body.
169 if (is_wp_error($response) || 200 !== (int) wp_remote_retrieve_response_code($response)) {
170 return;
171 }
172 $body = wp_remote_retrieve_body($response);
173 // Bail on an empty or implausibly large body (defense in depth alongside limit_response_size).
174 if ('' === $body || strlen($body) > MB_IN_BYTES) {
175 return;
176 }
177 $decoded = json_decode($body);
178 // Bail on malformed JSON so a poisoned/garbled API response never gets cached or rendered.
179 if (\JSON_ERROR_NONE !== json_last_error() || empty($decoded)) {
180 return;
181 }
182 $this->data = $decoded;
183 update_option($this->text_domain . '__banner_last_check', time());
184 update_option($this->text_domain . '__banner_data', $this->data);
185 }
186 }
187 public function is_correct_screen_to_show($b_screen, $screen_id)
188 {
189 if (in_array($b_screen, array($screen_id, 'all_page'))) {
190 return \true;
191 }
192 if ($b_screen == 'plugin_page') {
193 return in_array($screen_id, $this->plugin_screens);
194 }
195 return \false;
196 }
197 private static $instance;
198 public static function instance($text_domain = '')
199 {
200 self::$instance = new static();
201 return self::$instance->set_text_domain($text_domain);
202 }
203 }
204