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