| 1 |
<?php |
| 2 |
namespace Wpmet\Libs; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
if(!class_exists('\Wpmet\Libs\Banner')): |
| 7 |
|
| 8 |
class Banner { |
| 9 |
|
| 10 |
protected $script_version = '2.1.0'; |
| 11 |
|
| 12 |
protected $key = 'wpmet_banner'; |
| 13 |
protected $data; |
| 14 |
protected $last_check; |
| 15 |
protected $check_interval = (3600 * 6); |
| 16 |
|
| 17 |
protected $plugin_screens; |
| 18 |
|
| 19 |
protected $text_domain; |
| 20 |
protected $filter_string; |
| 21 |
protected $filter_array = []; |
| 22 |
protected $api_url; |
| 23 |
|
| 24 |
|
| 25 |
public function get_version(){ |
| 26 |
return $this->script_version; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_script_location(){ |
| 30 |
return __FILE__; |
| 31 |
} |
| 32 |
|
| 33 |
public function call(){ |
| 34 |
add_action( 'admin_head', [$this, 'display_content'] ); |
| 35 |
} |
| 36 |
|
| 37 |
public function display_content(){ |
| 38 |
$this->get_data(); |
| 39 |
|
| 40 |
if(!empty($this->data->error)) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if(empty($this->data)) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
foreach($this->data as $content) { |
| 49 |
|
| 50 |
if(!empty($this->filter_array) && $this->in_blacklist($content, $this->filter_array)) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
if($content->start <= time() && time() <= $content->end) { |
| 55 |
$screen = get_current_screen(); |
| 56 |
if($this->is_correct_screen_to_show($content->screen, $screen->id) && class_exists('\Oxaim\Libs\Notice')) { |
| 57 |
|
| 58 |
$inline_css = ''; |
| 59 |
$banner_unique_id = ((isset($content->data->unique_key) && $content->data->unique_key != '') ? $content->data->unique_key : $content->id ); |
| 60 |
|
| 61 |
if(!empty($content->data->style_css)) { |
| 62 |
$inline_css =' style="'.$content->data->style_css.'"'; |
| 63 |
} |
| 64 |
|
| 65 |
$instance = \Oxaim\Libs\Notice::instance('wpmet-jhanda', $banner_unique_id) |
| 66 |
->set_dismiss('global', (3600 * 24 * 15)); |
| 67 |
|
| 68 |
if($content->type == 'banner'){ |
| 69 |
$this->init_banner($content, $instance, $inline_css); |
| 70 |
} |
| 71 |
|
| 72 |
if($content->type == 'notice'){ |
| 73 |
$this->init_notice($content, $instance, $inline_css); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
private function init_notice($content, $instance, $inline_css){ |
| 82 |
|
| 83 |
$instance->set_message($content->data->notice_body); |
| 84 |
|
| 85 |
if($content->data->notice_image != ''){ |
| 86 |
$instance->set_logo($content->data->notice_image); |
| 87 |
} |
| 88 |
if($content->data->button_text != ''){ |
| 89 |
$instance->set_button([ |
| 90 |
'default_class' => 'button', |
| 91 |
'class' => 'button-secondary button-small', // button-primary button-secondary button-small button-large button-link |
| 92 |
'text' => $content->data->button_text, |
| 93 |
'url' => $content->data->button_link, |
| 94 |
]); |
| 95 |
} |
| 96 |
$instance->call(); |
| 97 |
} |
| 98 |
|
| 99 |
private function init_banner($content, $instance, $inline_css){ |
| 100 |
|
| 101 |
$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>'; |
| 102 |
|
| 103 |
$instance->set_gutter(false) |
| 104 |
->set_html($html) |
| 105 |
->call(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
private function in_whitelist($conf, $list) { |
| 110 |
|
| 111 |
$match = $conf->data->whitelist; |
| 112 |
|
| 113 |
if(empty($match)) { |
| 114 |
return true; |
| 115 |
}; |
| 116 |
|
| 117 |
$match_arr = explode(',', $match); |
| 118 |
|
| 119 |
foreach($list as $word) { |
| 120 |
if(in_array($word, $match_arr)) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
private function in_blacklist($conf, $list) { |
| 130 |
|
| 131 |
$match = $conf->data->blacklist; |
| 132 |
|
| 133 |
if(empty($match)) { |
| 134 |
return false; |
| 135 |
}; |
| 136 |
|
| 137 |
$match_arr = explode(',', $match); |
| 138 |
|
| 139 |
foreach($match_arr as $idx => $item) { |
| 140 |
|
| 141 |
$match_arr[$idx] = trim($item); |
| 142 |
} |
| 143 |
|
| 144 |
foreach($list as $word) { |
| 145 |
if(in_array($word, $match_arr)) { |
| 146 |
return true; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
public function is_test($is_test = false) { |
| 155 |
if($is_test === true){ |
| 156 |
$this->check_interval = 1; |
| 157 |
} |
| 158 |
|
| 159 |
return $this; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
public function set_text_domain($text_domain) { |
| 164 |
$this->text_domain = $text_domain; |
| 165 |
|
| 166 |
return $this; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
public function set_filter($filter_string) { |
| 171 |
$this->filter_string = $filter_string; |
| 172 |
if(!empty($filter_string)) { |
| 173 |
|
| 174 |
$filter = explode(',', $this->filter_string); |
| 175 |
|
| 176 |
foreach ($filter as $id => $item) { |
| 177 |
$this->filter_array[$id] = trim($item); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return $this; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
public function set_api_url($url) { |
| 186 |
$this->api_url = $url; |
| 187 |
|
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
public function set_plugin_screens($screen) { |
| 192 |
$this->plugin_screens[] = $screen; |
| 193 |
|
| 194 |
return $this; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
private function get_data() { |
| 199 |
$this->data = get_option($this->text_domain . '__banner_data'); |
| 200 |
$this->data = $this->data == '' ? [] : $this->data; |
| 201 |
|
| 202 |
$this->last_check = get_option($this->text_domain . '__banner_last_check'); |
| 203 |
$this->last_check = $this->last_check == '' ? 0 : $this->last_check; |
| 204 |
|
| 205 |
if(($this->check_interval + $this->last_check) < time()){ |
| 206 |
$response = wp_remote_get( $this->api_url . '/cache/'.$this->text_domain.'.json?nocache='.time(), |
| 207 |
[ |
| 208 |
'timeout' => 10, |
| 209 |
'httpversion' => '1.1', |
| 210 |
] |
| 211 |
); |
| 212 |
|
| 213 |
if(!is_wp_error($response) && isset($response['body']) && $response['body'] != ''){ |
| 214 |
|
| 215 |
$response = json_decode($response['body']); |
| 216 |
|
| 217 |
if(!empty($response)) { |
| 218 |
$this->data = $response; |
| 219 |
update_option($this->text_domain . '__banner_last_check', time()); |
| 220 |
update_option($this->text_domain . '__banner_data', $this->data); |
| 221 |
} |
| 222 |
|
| 223 |
return; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
public function is_correct_screen_to_show($b_screen, $screen_id) { |
| 230 |
|
| 231 |
if(in_array($b_screen, [$screen_id, 'all_page'])) { |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
if($b_screen == 'plugin_page') { |
| 237 |
return in_array($screen_id, $this->plugin_screens); |
| 238 |
} |
| 239 |
|
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
private static $instance; |
| 244 |
|
| 245 |
public static function instance($text_domain = '') { |
| 246 |
|
| 247 |
self::$instance = new static(); |
| 248 |
return self::$instance->set_text_domain($text_domain); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
endif; |
| 253 |
|