init.php
319 lines
| 1 | <?php |
| 2 | namespace Wpmet\Libs\Announcements; |
| 3 | |
| 4 | defined('ABSPATH') || exit; |
| 5 | |
| 6 | if (!class_exists('\Wpmet\Libs\Announcements\Init')): |
| 7 | |
| 8 | class Init { |
| 9 | |
| 10 | protected $script_version = '1.0.1'; |
| 11 | |
| 12 | protected $key; |
| 13 | protected $api; |
| 14 | protected $data; |
| 15 | protected $title; |
| 16 | protected $plugin_link = []; |
| 17 | protected $last_check; |
| 18 | protected $check_interval = (3600 * 3); |
| 19 | |
| 20 | protected $plugin_screens; |
| 21 | |
| 22 | protected $text_domain; |
| 23 | protected $filter_string; |
| 24 | protected $api_url; |
| 25 | |
| 26 | private $announcements; |
| 27 | |
| 28 | /** |
| 29 | * Get version of this script |
| 30 | * |
| 31 | * @return string Version name |
| 32 | */ |
| 33 | public function get_version() { |
| 34 | return $this->script_version; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get current directory path |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function get_script_location() { |
| 43 | return __FILE__; |
| 44 | } |
| 45 | |
| 46 | public function set_plugin($link_title, $weblink = 'https://wpmet.com/') { |
| 47 | $this->plugin_link[] = [ $link_title, $weblink ]; |
| 48 | |
| 49 | return $this; |
| 50 | } |
| 51 | |
| 52 | public function call() { |
| 53 | |
| 54 | $this->key = 'wpmet_announcements'; |
| 55 | $this->api = $this->api_url . '?csrf=wpmet_true&nocache=' . time(); |
| 56 | |
| 57 | $this->get_announcements(); |
| 58 | |
| 59 | if (!empty($this->data->error)) { |
| 60 | |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (empty($this->data)) { |
| 65 | |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $list = []; |
| 70 | |
| 71 | if (!empty($this->filter_string)) { |
| 72 | |
| 73 | $list = explode(',', $this->filter_string); |
| 74 | |
| 75 | foreach ($list as $idx => $item) { |
| 76 | $list[$idx] = trim($item); |
| 77 | } |
| 78 | $list = array_filter($list); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | foreach ($this->data as $announcement) { |
| 83 | |
| 84 | if (!empty($list) && $this->in_blacklist($announcement, $list)) { |
| 85 | |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | $this->set_announcements($announcement); |
| 90 | } |
| 91 | |
| 92 | if(empty($this->announcements)) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | add_action('wp_dashboard_setup', [$this, 'show_announcements_widget']); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | private function in_whitelist($conf, $list) { |
| 101 | |
| 102 | $match = $conf->data->whitelist; |
| 103 | |
| 104 | if (empty($match)) { |
| 105 | |
| 106 | return true; |
| 107 | }; |
| 108 | |
| 109 | $match_arr = explode(',', $match); |
| 110 | |
| 111 | foreach ($list as $word) { |
| 112 | if (in_array($word, $match_arr)) { |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | private function in_blacklist($conf, $list) { |
| 122 | |
| 123 | $match = $conf->data->blacklist; |
| 124 | |
| 125 | if (empty($match)) { |
| 126 | |
| 127 | return false; |
| 128 | }; |
| 129 | |
| 130 | $match_arr = explode(',', $match); |
| 131 | |
| 132 | foreach ($match_arr as $idx => $item) { |
| 133 | |
| 134 | $match_arr[$idx] = trim($item); |
| 135 | } |
| 136 | |
| 137 | foreach ($list as $word) { |
| 138 | if (in_array($word, $match_arr)) { |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | public function set_title($title = '') { |
| 148 | $this->title = $title; |
| 149 | |
| 150 | return $this; |
| 151 | } |
| 152 | |
| 153 | public function is_test($is_test = false) { |
| 154 | |
| 155 | if ($is_test === true) { |
| 156 | $this->check_interval = 1; |
| 157 | } |
| 158 | |
| 159 | return $this; |
| 160 | } |
| 161 | |
| 162 | public function set_text_domain($text_domain) { |
| 163 | |
| 164 | $this->text_domain = $text_domain; |
| 165 | |
| 166 | return $this; |
| 167 | } |
| 168 | |
| 169 | public function set_filter($filter_string) { |
| 170 | |
| 171 | $this->filter_string = $filter_string; |
| 172 | |
| 173 | return $this; |
| 174 | } |
| 175 | |
| 176 | public function set_api_url($url) { |
| 177 | |
| 178 | $this->api_url = $url; |
| 179 | |
| 180 | return $this; |
| 181 | } |
| 182 | |
| 183 | public function set_plugin_screens($screen) { |
| 184 | |
| 185 | $this->plugin_screens[] = $screen; |
| 186 | |
| 187 | return $this; |
| 188 | } |
| 189 | |
| 190 | private function set_announcements($announcement) { |
| 191 | $filter = []; |
| 192 | foreach (get_option('active_plugins') as $plugin) { |
| 193 | $temp = pathinfo($plugin); |
| 194 | if(!empty($temp)) { |
| 195 | $filter[] = trim($temp['filename']); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (isset($this->announcements[$announcement->id])) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | // if start and endtime is set, check current time is inside the timeframe |
| 204 | if ((!empty($announcement->start) && !empty($announcement->end)) && (intval($announcement->start) > time() || intval($announcement->end) < time())) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | if(empty(array_intersect($filter, $announcement->plugins))) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | $this->announcements[$announcement->id] = [ |
| 213 | 'id' => $announcement->id, |
| 214 | 'title' => $announcement->title, |
| 215 | 'description' => $announcement->description, |
| 216 | 'type' => $announcement->type, |
| 217 | 'priority' => $announcement->priority, |
| 218 | 'announcements_link' => $announcement->data->announcements_link, |
| 219 | 'announcements_image' => $announcement->data->announcements_image, |
| 220 | ]; |
| 221 | } |
| 222 | |
| 223 | private function get_announcements() { |
| 224 | $this->data = get_option($this->text_domain . '__announcements_data'); |
| 225 | $this->data = $this->data == '' ? [] : $this->data; |
| 226 | |
| 227 | $this->last_check = get_option($this->text_domain . '__announcements_last_check'); |
| 228 | |
| 229 | $this->last_check = empty($this->last_check) ? 0 : $this->last_check; |
| 230 | |
| 231 | if (($this->check_interval + $this->last_check) < time()) { |
| 232 | $response = wp_remote_get($this->api, |
| 233 | array( |
| 234 | 'timeout' => 10, |
| 235 | 'httpversion' => '1.1', |
| 236 | ) |
| 237 | ); |
| 238 | |
| 239 | if (!is_wp_error($response) && isset($response['body']) && $response['body'] != '') { |
| 240 | |
| 241 | $response = json_decode($response['body']); |
| 242 | |
| 243 | // if (!empty($response)) { } |
| 244 | $this->data = $response; |
| 245 | |
| 246 | update_option($this->text_domain . '__announcements_last_check', time()); |
| 247 | update_option($this->text_domain . '__announcements_data', $this->data); |
| 248 | |
| 249 | return; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | public function show_announcements_widget() { |
| 255 | $this->title = (isset($this->title) && !empty($this->title) ? $this->title . ' ' : '') . 'Announcements'; |
| 256 | |
| 257 | wp_add_dashboard_widget( 'wpmet-announcements', __( 'Wpmet Stories', 'wp-social' ), [ $this, 'show' ] ); |
| 258 | |
| 259 | // Move our widget to top. |
| 260 | global $wp_meta_boxes; |
| 261 | |
| 262 | $dashboard = $wp_meta_boxes['dashboard']['normal']['core']; |
| 263 | $ours = [ |
| 264 | 'wpmet-announcements' => $dashboard['wpmet-announcements'], |
| 265 | ]; |
| 266 | |
| 267 | $wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard ); |
| 268 | } |
| 269 | |
| 270 | public function show() { |
| 271 | usort($this->announcements, function ($a, $b) { |
| 272 | return $a['priority'] <=> $b['priority']; |
| 273 | }); |
| 274 | |
| 275 | include_once 'views/template.php'; |
| 276 | |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Crosscheck if Announcement library will be shown at current WP admin page or not |
| 281 | * |
| 282 | * @param string $b_screen |
| 283 | * @param string $screen_id |
| 284 | * |
| 285 | * @return boolean |
| 286 | */ |
| 287 | public function is_correct_screen_to_show($b_screen, $screen_id) { |
| 288 | |
| 289 | if (in_array($b_screen, [$screen_id, 'all_page'])) { |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | if ($b_screen == 'plugin_page') { |
| 295 | |
| 296 | return in_array($screen_id, $this->plugin_screens); |
| 297 | } |
| 298 | |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Define singleton instance |
| 304 | * |
| 305 | * @var [type] |
| 306 | */ |
| 307 | private static $instance; |
| 308 | |
| 309 | public static function instance($text_domain = '') { |
| 310 | |
| 311 | if(!self::$instance) { |
| 312 | self::$instance = new static(); |
| 313 | } |
| 314 | |
| 315 | return self::$instance->set_text_domain($text_domain); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | endif; |