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