| 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 = 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="' . $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($content->data->notice_body); |
| 73 |
if ($content->data->notice_image != '') { |
| 74 |
$instance->set_logo($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' => $content->data->button_text, |
| 82 |
'url' => $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" ' . $inline_css . ' class="wpmet-jhanda-href" href="' . $content->data->banner_link . '"><img style="display: block;margin: 0 auto;" src="' . $content->data->banner_image . '" /></a>'; |
| 90 |
$instance->set_gutter(\false)->set_html($html)->call(); |
| 91 |
} |
| 92 |
private function in_whitelist($conf, $list) |
| 93 |
{ |
| 94 |
$match = $conf->data->whitelist; |
| 95 |
if (empty($match)) { |
| 96 |
return \true; |
| 97 |
} |
| 98 |
$match_arr = \explode(',', $match); |
| 99 |
foreach ($list as $word) { |
| 100 |
if (\in_array($word, $match_arr)) { |
| 101 |
return \true; |
| 102 |
} |
| 103 |
} |
| 104 |
return \false; |
| 105 |
} |
| 106 |
private function in_blacklist($conf, $list) |
| 107 |
{ |
| 108 |
$match = $conf->data->blacklist; |
| 109 |
if (empty($match)) { |
| 110 |
return \false; |
| 111 |
} |
| 112 |
$match_arr = \explode(',', $match); |
| 113 |
foreach ($match_arr as $idx => $item) { |
| 114 |
$match_arr[$idx] = \trim($item); |
| 115 |
} |
| 116 |
foreach ($list as $word) { |
| 117 |
if (\in_array($word, $match_arr)) { |
| 118 |
return \true; |
| 119 |
} |
| 120 |
} |
| 121 |
return \false; |
| 122 |
} |
| 123 |
public function is_test($is_test = \false) |
| 124 |
{ |
| 125 |
if ($is_test === \true) { |
| 126 |
$this->check_interval = 1; |
| 127 |
} |
| 128 |
return $this; |
| 129 |
} |
| 130 |
public function set_text_domain($text_domain) |
| 131 |
{ |
| 132 |
$this->text_domain = $text_domain; |
| 133 |
return $this; |
| 134 |
} |
| 135 |
public function set_filter($filter_string) |
| 136 |
{ |
| 137 |
$this->filter_string = $filter_string; |
| 138 |
if (!empty($filter_string)) { |
| 139 |
$filter = \explode(',', $this->filter_string); |
| 140 |
foreach ($filter as $id => $item) { |
| 141 |
$this->filter_array[$id] = \trim($item); |
| 142 |
} |
| 143 |
} |
| 144 |
return $this; |
| 145 |
} |
| 146 |
public function set_api_url($url) |
| 147 |
{ |
| 148 |
$this->api_url = $url; |
| 149 |
return $this; |
| 150 |
} |
| 151 |
public function set_plugin_screens($screen) |
| 152 |
{ |
| 153 |
$this->plugin_screens[] = $screen; |
| 154 |
return $this; |
| 155 |
} |
| 156 |
private function get_data() |
| 157 |
{ |
| 158 |
$this->data = get_option($this->text_domain . '__banner_data'); |
| 159 |
$this->data = $this->data == '' ? array() : $this->data; |
| 160 |
$this->last_check = get_option($this->text_domain . '__banner_last_check'); |
| 161 |
$this->last_check = $this->last_check == '' ? 0 : $this->last_check; |
| 162 |
if ($this->check_interval + $this->last_check < \time()) { |
| 163 |
$response = wp_remote_get($this->api_url . '/cache/' . $this->text_domain . '.json?nocache=' . \time(), array('timeout' => 10, 'httpversion' => '1.1')); |
| 164 |
if (!is_wp_error($response) && isset($response['body']) && $response['body'] != '') { |
| 165 |
$response = \json_decode($response['body']); |
| 166 |
if (!empty($response)) { |
| 167 |
$this->data = $response; |
| 168 |
update_option($this->text_domain . '__banner_last_check', \time()); |
| 169 |
update_option($this->text_domain . '__banner_data', $this->data); |
| 170 |
} |
| 171 |
return; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
public function is_correct_screen_to_show($b_screen, $screen_id) |
| 176 |
{ |
| 177 |
if (\in_array($b_screen, array($screen_id, 'all_page'))) { |
| 178 |
return \true; |
| 179 |
} |
| 180 |
if ($b_screen == 'plugin_page') { |
| 181 |
return \in_array($screen_id, $this->plugin_screens); |
| 182 |
} |
| 183 |
return \false; |
| 184 |
} |
| 185 |
private static $instance; |
| 186 |
public static function instance($text_domain = '') |
| 187 |
{ |
| 188 |
self::$instance = new static(); |
| 189 |
return self::$instance->set_text_domain($text_domain); |
| 190 |
} |
| 191 |
} |
| 192 |
|