| @@ -47,33 +47,95 @@ | ||
| 47 | 47 | */ |
| 48 | 48 | protected $option_id = 'lp_admin_notices'; |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | - * @var string | |
| 51 | + * Check is right plugin base. | |
| 52 | + * | |
| 53 | + * @return bool | |
| 52 | 54 | */ |
| 53 | - protected $dismissed_option_id = 'lp_admin_dismissed_notices'; | |
| 55 | + public static function check_plugin_base(): bool { | |
| 56 | + return 0 !== strcmp( LP_PLUGIN_BASENAME, 'learnpress/learnpress.php' ); | |
| 57 | + } | |
| 54 | 58 | |
| 55 | 59 | /** |
| 56 | - * LP_Admin_Notice construct | |
| 60 | + * Check LP has beta version. | |
| 61 | + * | |
| 62 | + * @return bool|[] | |
| 57 | 63 | */ |
| 58 | - protected function __construct() { | |
| 59 | - // add_action( 'init', array( $this, 'dismiss_notice' ) ); | |
| 60 | - add_action( 'init', array( $this, 'load' ) ); | |
| 61 | - add_action( 'admin_notices', array( $this, 'show_notices' ), 90 ); | |
| 64 | + public static function check_lp_beta_version() { | |
| 65 | + $url = 'https://learnpress.github.io/learnpress/lp-beta-version.json'; | |
| 66 | + $config = []; | |
| 67 | + | |
| 68 | + try { | |
| 69 | + $res = wp_remote_get( $url ); | |
| 70 | + if ( is_wp_error( $res ) ) { | |
| 71 | + throw new Exception( $res->get_error_message() ); | |
| 72 | + } | |
| 73 | + | |
| 74 | + $config = json_decode( wp_remote_retrieve_body( $res ), true ); | |
| 75 | + if ( json_last_error() ) { | |
| 76 | + throw new Exception( json_last_error_msg() ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + $version = $config['version'] ?? 0; | |
| 80 | + if ( ! $version ) { | |
| 81 | + throw new Exception( 'Version LP beta is invalid!' ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + if ( ! version_compare( $version, LEARNPRESS_VERSION, '>' ) ) { | |
| 85 | + return false; | |
| 86 | + } | |
| 87 | + } catch ( Throwable $e ) { | |
| 88 | + error_log( $e->getMessage() ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + return $config; | |
| 62 | 92 | } |
| 63 | 93 | |
| 64 | - public function load() { | |
| 65 | - $notices = get_option( $this->option_id ); | |
| 66 | - | |
| 67 | - if ( ! $notices ) { | |
| 68 | - return false; | |
| 94 | + /** | |
| 95 | + * Get description of beta version. | |
| 96 | + * | |
| 97 | + * @param array $config | |
| 98 | + * | |
| 99 | + * @return array | |
| 100 | + */ | |
| 101 | + public static function get_data_lp_beta( array $config = [] ): array { | |
| 102 | + try { | |
| 103 | + $keys = array_keys( $config ); | |
| 104 | + $title = $config['title'] ?? ''; | |
| 105 | + $description = $config['description'] ?? ''; | |
| 106 | + foreach ( $keys as $key ) { | |
| 107 | + $description = str_replace( '[[' . $key . ']]', $config[ $key ], $description ); | |
| 108 | + $title = str_replace( '[[' . $key . ']]', $config[ $key ], $title ); | |
| 109 | + } | |
| 110 | + } catch ( Throwable $e ) { | |
| 111 | + $title = ''; | |
| 112 | + $description = ''; | |
| 113 | + error_log( $e->getMessage() ); | |
| 69 | 114 | } |
| 70 | 115 | |
| 71 | - $this->notices = array_merge( $notices, $this->notices ); | |
| 116 | + return [ | |
| 117 | + 'title' => $title, | |
| 118 | + 'description' => $description, | |
| 119 | + ]; | |
| 120 | + } | |
| 72 | 121 | |
| 73 | - delete_option( $this->option_id ); | |
| 122 | + /** | |
| 123 | + * Tests the background handler's connection. | |
| 124 | + * | |
| 125 | + * @since 4.1.7.3.2 | |
| 126 | + * | |
| 127 | + * @return bool|WP_Error | |
| 128 | + */ | |
| 129 | + public static function check_wp_remote() { | |
| 130 | + $test_url = add_query_arg( 'lp_test_wp_remote', 1, home_url() ); | |
| 131 | + $args = [ | |
| 132 | + 'timeout' => 30, | |
| 133 | + ]; | |
| 134 | + $result = wp_remote_post( $test_url, $args ); | |
| 135 | + $body = ! is_wp_error( $result ) ? wp_remote_retrieve_body( $result ) : $result; | |
| 74 | 136 | |
| 75 | - return true; | |
| 137 | + return $body === '[TEST_REMOTE]' ? true : $body; | |
| 76 | 138 | } |
| 77 | 139 | |
| 78 | 140 | /** |
| 79 | 141 | * Add new notice to show in admin page. |
| @@ -87,10 +149,13 @@ | ||
| 87 | 149 | * @param bool $redirect |
| 88 | 150 | * @param bool $override |
| 89 | 151 | * |
| 90 | 152 | * @return boolean |
| 153 | + * @deprecated 4.2.5 | |
| 91 | 154 | */ |
| 92 | 155 | public function add( $message, $type = 'success', $dismissible = true, $id = '', $redirect = false, $override = false ) { |
| 156 | + _deprecated_function( __METHOD__, '4.2.5' ); | |
| 157 | + return false; | |
| 93 | 158 | if ( ! $id ) { |
| 94 | 159 | $id = uniqid(); |
| 95 | 160 | } |
| 96 | 161 | |
| @@ -108,84 +173,8 @@ | ||
| 108 | 173 | return false; |
| 109 | 174 | } |
| 110 | 175 | |
| 111 | 176 | /** |
| 112 | - * @param string|WP_Error $message | |
| 113 | - * @param string $type | |
| 114 | - * @param bool $dismissible | |
| 115 | - * @param string $id | |
| 116 | - * @param bool $override | |
| 117 | - * | |
| 118 | - * @return bool | |
| 119 | - */ | |
| 120 | - public function add_redirect( $message, $type = 'success', $dismissible = false, $id = '', $override = false ) { | |
| 121 | - return $this->add( $message, $type, $dismissible, $id, true, $override ); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * Show all notices. | |
| 126 | - * | |
| 127 | - * @return bool | |
| 128 | - */ | |
| 129 | - public function show_notices() { | |
| 130 | - if ( empty( $this->notices ) ) { | |
| 131 | - return false; | |
| 132 | - } | |
| 133 | - | |
| 134 | - $redirect_notices = array(); | |
| 135 | - | |
| 136 | - foreach ( $this->notices as $id => $notice ) { | |
| 137 | - $notice['message'] = $notice['message'] instanceof WP_Error ? $notice['message']->get_error_message() : $notice['message']; | |
| 138 | - | |
| 139 | - if ( $notice['redirect'] ) { | |
| 140 | - $notice['redirect'] = false; | |
| 141 | - $redirect_notices[ $id ] = $notice; | |
| 142 | - } else { | |
| 143 | - if ( ! $this->has_dismissed_notice( $id ) ) { | |
| 144 | - if ( preg_match( '/.php$/', $notice['message'] ) ) { | |
| 145 | - $view = $notice['message']; | |
| 146 | - } else { | |
| 147 | - $view = 'admin-notice.php'; | |
| 148 | - } | |
| 149 | - | |
| 150 | - learn_press_admin_view( $view, array_merge( $notice, array( 'id' => $id ) ) ); | |
| 151 | - } | |
| 152 | - } | |
| 153 | - } | |
| 154 | - | |
| 155 | - if ( $redirect_notices ) { | |
| 156 | - update_option( $this->option_id, $redirect_notices ); | |
| 157 | - } | |
| 158 | - | |
| 159 | - return true; | |
| 160 | - } | |
| 161 | - | |
| 162 | - /** | |
| 163 | - * Check if a notice is already added by id. | |
| 164 | - * | |
| 165 | - * @since 3.2.6 | |
| 166 | - * | |
| 167 | - * @param string[] $notice | |
| 168 | - * | |
| 169 | - * @return bool | |
| 170 | - */ | |
| 171 | - public function has_notice( $notice ) { | |
| 172 | - settype( $notice, 'array' ); | |
| 173 | - | |
| 174 | - if ( ! $this->notices ) { | |
| 175 | - return false; | |
| 176 | - } | |
| 177 | - | |
| 178 | - foreach ( $notice as $id ) { | |
| 179 | - if ( ! empty( $this->notices[ $id ] ) ) { | |
| 180 | - return true; | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | - return false; | |
| 185 | - } | |
| 186 | - | |
| 187 | - /** | |
| 188 | 177 | * Remove one or more notices from queue by ids. |
| 189 | 178 | * |
| 190 | 179 | * @since 3.2.6 |
| 191 | 180 | * |
| @@ -228,272 +217,8 @@ | ||
| 228 | 217 | */ |
| 229 | 218 | public function clear() { |
| 230 | 219 | $this->notices = array(); |
| 231 | 220 | delete_option( $this->option_id ); |
| 232 | - } | |
| 233 | - | |
| 234 | - /** | |
| 235 | - * @since 3.2.6 | |
| 236 | - */ | |
| 237 | - /*public function dismiss_notice() { | |
| 238 | - $id = LP_Request::get( 'lp-dismiss-notice' ); | |
| 239 | - | |
| 240 | - if ( ! $id ) { | |
| 241 | - return; | |
| 242 | - } | |
| 243 | - | |
| 244 | - $dismissed = get_option( $this->dismissed_option_id ); | |
| 245 | - | |
| 246 | - if ( ! $dismissed ) { | |
| 247 | - $dismissed = array(); | |
| 248 | - } | |
| 249 | - | |
| 250 | - do_action( 'learn-press/before-dismiss-notice', $id ); | |
| 251 | - | |
| 252 | - if ( array_search( $id, $dismissed ) === false ) { | |
| 253 | - $dismissed[] = $id; | |
| 254 | - update_option( $this->dismissed_option_id, $dismissed ); | |
| 255 | - } | |
| 256 | - | |
| 257 | - do_action( 'learn-press/dismissed-notice', $id ); | |
| 258 | - | |
| 259 | - learn_press_send_json( | |
| 260 | - apply_filters( | |
| 261 | - 'learn-press/dismissed-notice-response', | |
| 262 | - array( | |
| 263 | - 'dismissed' => $id, | |
| 264 | - ), | |
| 265 | - $id | |
| 266 | - ) | |
| 267 | - ); | |
| 268 | - }*/ | |
| 269 | - | |
| 270 | - /** | |
| 271 | - * Update option to turn-off a notice. | |
| 272 | - * | |
| 273 | - * @since 3.2.6 | |
| 274 | - * | |
| 275 | - * @param string $name | |
| 276 | - * @param string $value | |
| 277 | - * @param int $expired | |
| 278 | - */ | |
| 279 | - public function dismiss_notice_2( $name, $value, $expired = 0 ) { | |
| 280 | - if ( $expired ) { | |
| 281 | - set_transient( 'lp_dismiss_notice' . $name, $value, $expired ); | |
| 282 | - } else { | |
| 283 | - $values = get_option( 'lp_dismiss_notice' ); | |
| 284 | - if ( ! $values ) { | |
| 285 | - $values = array( $name => $value ); | |
| 286 | - } else { | |
| 287 | - $values[ $name ] = $value; | |
| 288 | - } | |
| 289 | - | |
| 290 | - update_option( 'lp_dismiss_notice', $values ); | |
| 291 | - } | |
| 292 | - } | |
| 293 | - | |
| 294 | - /** | |
| 295 | - * Check if a notice has dismissed. | |
| 296 | - * | |
| 297 | - * @since 3.2.6 | |
| 298 | - * | |
| 299 | - * @param string $name | |
| 300 | - * | |
| 301 | - * @return bool | |
| 302 | - */ | |
| 303 | - public function has_dismissed_notice( $name ) { | |
| 304 | - | |
| 305 | - $dismissed = get_option( $this->dismissed_option_id ); | |
| 306 | - | |
| 307 | - if ( ! $dismissed ) { | |
| 308 | - return false; | |
| 309 | - } | |
| 310 | - | |
| 311 | - return array_search( $name, $dismissed ) !== false; | |
| 312 | - } | |
| 313 | - | |
| 314 | - /** | |
| 315 | - * Restore dismissed notices by id. | |
| 316 | - * | |
| 317 | - * @since 3.2.6 | |
| 318 | - * | |
| 319 | - * @param string[] $notice | |
| 320 | - * | |
| 321 | - * @return bool | |
| 322 | - */ | |
| 323 | - public function restore_dismissed_notice( $notice ) { | |
| 324 | - $dismissed = get_option( $this->dismissed_option_id ); | |
| 325 | - | |
| 326 | - if ( ! $dismissed ) { | |
| 327 | - return false; | |
| 328 | - } | |
| 329 | - | |
| 330 | - settype( $notice, 'array' ); | |
| 331 | - | |
| 332 | - foreach ( $notice as $id ) { | |
| 333 | - $at = array_search( $id, $dismissed ); | |
| 334 | - | |
| 335 | - if ( false !== $at ) { | |
| 336 | - unset( $dismissed[ $at ] ); | |
| 337 | - } | |
| 338 | - } | |
| 339 | - | |
| 340 | - update_option( $this->dismissed_option_id, $dismissed ); | |
| 341 | - | |
| 342 | - return true; | |
| 343 | - } | |
| 344 | - | |
| 345 | - /** | |
| 346 | - * Clear all notices has dismissed. | |
| 347 | - * | |
| 348 | - * @since 3.2.6 | |
| 349 | - */ | |
| 350 | - public function clear_dismissed_notice() { | |
| 351 | - delete_option( $this->dismissed_option_id ); | |
| 352 | - } | |
| 353 | - | |
| 354 | - /** | |
| 355 | - * Remove a notice has been dismissed. | |
| 356 | - * | |
| 357 | - * @since 3.2.6 | |
| 358 | - * | |
| 359 | - * @param string|array $name - Optional. NULL will remove all notices. | |
| 360 | - * @param bool $expired - Optional. TRUE if dismiss notice as transient (in case $name passed). | |
| 361 | - * | |
| 362 | - * @return bool | |
| 363 | - */ | |
| 364 | - public function remove_dismissed_notice( $name = '' ) { | |
| 365 | - if ( ! $name ) { | |
| 366 | - global $wpdb; | |
| 367 | - | |
| 368 | - $query = $wpdb->prepare( "SELECT SUBSTR(option_name, 12) FROM {$wpdb->options} WHERE option_name LIKE %s", '%' . $wpdb->esc_like( '_transient_lp_dismiss_notice' ) . '%' ); | |
| 369 | - | |
| 370 | - $all_notices = $wpdb->get_col( $query ); | |
| 371 | - | |
| 372 | - if ( $all_notices ) { | |
| 373 | - foreach ( $all_notices as $notice ) { | |
| 374 | - delete_transient( $notice ); | |
| 375 | - } | |
| 376 | - } | |
| 377 | - | |
| 378 | - delete_option( 'lp_dismiss_notice' ); | |
| 379 | - | |
| 380 | - return true; | |
| 381 | - | |
| 382 | - } elseif ( is_array( $name ) ) { | |
| 383 | - foreach ( $name as $notice ) { | |
| 384 | - $this->remove_dismissed_notice( $notice ); | |
| 385 | - } | |
| 386 | - | |
| 387 | - return true; | |
| 388 | - } | |
| 389 | - | |
| 390 | - delete_transient( 'lp_dismiss_notice' . $name ); | |
| 391 | - | |
| 392 | - $values = get_option( 'lp_dismiss_notice' ); | |
| 393 | - | |
| 394 | - if ( ! $values ) { | |
| 395 | - return false; | |
| 396 | - } else { | |
| 397 | - if ( array_key_exists( $name, $values ) ) { | |
| 398 | - unset( $values[ $name ] ); | |
| 399 | - | |
| 400 | - update_option( 'lp_dismiss_notice', $values ); | |
| 401 | - | |
| 402 | - return $values; | |
| 403 | - } | |
| 404 | - } | |
| 405 | - | |
| 406 | - return false; | |
| 407 | - } | |
| 408 | - | |
| 409 | - /** | |
| 410 | - * @deprecated | |
| 411 | - */ | |
| 412 | - public function dismiss_notice_deprecated() { | |
| 413 | - $notice = learn_press_get_request( 'lp-hide-notice' ); | |
| 414 | - | |
| 415 | - if ( ! $notice ) { | |
| 416 | - return; | |
| 417 | - } | |
| 418 | - | |
| 419 | - $transient = learn_press_get_request( 't' ); | |
| 420 | - | |
| 421 | - if ( $transient ) { | |
| 422 | - set_transient( 'lp-hide-notice-' . $notice, 'yes', $transient ); | |
| 423 | - } else { | |
| 424 | - learn_press_update_user_option( 'hide-notice-' . $notice, 'yes' ); | |
| 425 | - } | |
| 426 | - | |
| 427 | - $redirect = apply_filters( 'learn_press_hide_notice_redirect', esc_url_raw( remove_query_arg( 'lp-hide-notice' ) ) ); | |
| 428 | - if ( $redirect ) { | |
| 429 | - wp_redirect( untrailingslashit( $redirect ) ); | |
| 430 | - exit(); | |
| 431 | - } | |
| 432 | - } | |
| 433 | - | |
| 434 | - /** | |
| 435 | - * Add new notice to queue | |
| 436 | - * | |
| 437 | - * @deprecated | |
| 438 | - * | |
| 439 | - * @param string $message The message want to display | |
| 440 | - * @param string $type The class name of WP message type updated|update-nag|error | |
| 441 | - * @param string $id Custom id for html element's ID | |
| 442 | - * @param bool | |
| 443 | - */ | |
| 444 | - public static function add_deprecated( $message, $type = 'success', $id = '', $redirect = false ) { | |
| 445 | - if ( $redirect ) { | |
| 446 | - $notices = get_transient( 'learn_press_redirect_notices' ); | |
| 447 | - if ( empty( $notices ) ) { | |
| 448 | - $notices = array(); | |
| 449 | - } | |
| 450 | - $notices[] = array( | |
| 451 | - 'type' => $type, | |
| 452 | - 'message' => $message, | |
| 453 | - 'id' => $id, | |
| 454 | - ); | |
| 455 | - set_transient( 'learn_press_redirect_notices', $notices ); | |
| 456 | - } else { | |
| 457 | - self::$_notices[] = array( | |
| 458 | - 'type' => $type, | |
| 459 | - 'message' => $message, | |
| 460 | - 'id' => $id, | |
| 461 | - ); | |
| 462 | - } | |
| 463 | - } | |
| 464 | - | |
| 465 | - public static function add_redirect_reprecated( $message, $type = 'updated', $id = '' ) { | |
| 466 | - self::add( $message, $type, $id, true ); | |
| 467 | - } | |
| 468 | - | |
| 469 | - /** | |
| 470 | - * Show all notices has registered | |
| 471 | - * | |
| 472 | - * @deprecated | |
| 473 | - */ | |
| 474 | - public static function show_notices_deprecated() { | |
| 475 | - if ( self::$_notices ) { | |
| 476 | - foreach ( self::$_notices as $notice ) { | |
| 477 | - if ( empty( $notice ) ) { | |
| 478 | - continue; | |
| 479 | - } | |
| 480 | - learn_press_admin_view( 'admin-notice.php', $notice ); | |
| 481 | - } | |
| 482 | - } | |
| 483 | - | |
| 484 | - $notices = get_transient( 'learn_press_redirect_notices' ); | |
| 485 | - if ( $notices ) { | |
| 486 | - foreach ( $notices as $notice ) { | |
| 487 | - if ( empty( $notice ) ) { | |
| 488 | - continue; | |
| 489 | - } | |
| 490 | - | |
| 491 | - learn_press_admin_view( 'admin-notice.php', $notice ); | |
| 492 | - } | |
| 493 | - | |
| 494 | - delete_transient( 'learn_press_redirect_notices' ); | |
| 495 | - } | |
| 496 | 221 | } |
| 497 | 222 | |
| 498 | 223 | /** |
| 499 | 224 | * Get single instance of this class |