stories.php
322 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 = array(); |
| 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[] = array( $link_title, $weblink ); |
| 47 | |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | public function call() { |
| 52 | add_action( 'wp_dashboard_setup', array( $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 = array( $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 ] = array( |
| 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 == '' ? array() : $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( |
| 188 | $this->api_url . 'cache/stories.json?nocache=' . time(), |
| 189 | array( |
| 190 | 'timeout' => 10, |
| 191 | 'httpversion' => '1.1', |
| 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 = array(); |
| 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 | foreach ( $this->data as $story ) { |
| 237 | |
| 238 | if ( ! empty( $list ) && $this->in_blacklist( $story, $list ) ) { |
| 239 | |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | $this->set_stories( $story ); |
| 244 | } |
| 245 | |
| 246 | if ( empty( $this->stories ) ) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | $this->title = ( isset( $this->title ) && ! empty( $this->title ) ? $this->title . ' ' : '' ) . 'Stories'; |
| 251 | |
| 252 | wp_add_dashboard_widget( 'wpmet-stories', __( 'Wpmet Stories', 'elementskit-lite' ), array( $this, 'show' ) ); |
| 253 | |
| 254 | // Move our widget to top. |
| 255 | global $wp_meta_boxes; |
| 256 | |
| 257 | $dashboard = $wp_meta_boxes['dashboard']['normal']['core']; |
| 258 | $ours = []; |
| 259 | if(isset($dashboard['wpmet-stories'])) { |
| 260 | $ours = array( |
| 261 | 'wpmet-stories' => $dashboard['wpmet-stories'], |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | $wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard ); |
| 266 | } |
| 267 | |
| 268 | public function show() { |
| 269 | usort( |
| 270 | $this->stories, |
| 271 | function ( $a, $b ) { |
| 272 | if ( $a['priority'] == $b['priority'] ) { |
| 273 | return 0; |
| 274 | } |
| 275 | return ( $a['priority'] < $b['priority'] ) ? -1 : 1; |
| 276 | } |
| 277 | ); |
| 278 | include_once 'views/template.php'; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Crosscheck if Story library will be shown at current WP admin page or not |
| 283 | * |
| 284 | * @param string $b_screen |
| 285 | * @param string $screen_id |
| 286 | * |
| 287 | * @return boolean |
| 288 | */ |
| 289 | public function is_correct_screen_to_show( $b_screen, $screen_id ) { |
| 290 | |
| 291 | if ( in_array( $b_screen, array( $screen_id, 'all_page' ) ) ) { |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | if ( $b_screen == 'plugin_page' ) { |
| 297 | |
| 298 | return in_array( $screen_id, $this->plugin_screens ); |
| 299 | } |
| 300 | |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Define singleton instance |
| 306 | * |
| 307 | * @var [type] |
| 308 | */ |
| 309 | private static $instance; |
| 310 | |
| 311 | public static function instance( $text_domain = '' ) { |
| 312 | |
| 313 | if ( ! self::$instance ) { |
| 314 | self::$instance = new static(); |
| 315 | } |
| 316 | |
| 317 | return self::$instance->set_text_domain( $text_domain ); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | endif; |
| 322 |