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