| @@ -1,239 +1,239 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Manage the admin notices and display them in admin | |
| 4 | - * | |
| 5 | - * @package LearnPress | |
| 6 | - * @author ThimPress | |
| 7 | - * @version 1.0 | |
| 8 | - */ | |
| 9 | - | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit; | |
| 12 | -} | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * Class LP_Admin_Notice | |
| 16 | - */ | |
| 17 | -class LP_Admin_Notice { | |
| 18 | - /** | |
| 19 | - * Store all notices which added anywhere before show | |
| 20 | - * | |
| 21 | - * @var array | |
| 22 | - */ | |
| 23 | - protected static $_notices = array(); | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * @since 3.2.6 | |
| 27 | - * | |
| 28 | - * @var LP_Admin_Notice | |
| 29 | - */ | |
| 30 | - protected static $instance = false; | |
| 31 | - | |
| 32 | - /** | |
| 33 | - * List of notices. | |
| 34 | - * | |
| 35 | - * @since 3.2.6 | |
| 36 | - * | |
| 37 | - * @var array | |
| 38 | - */ | |
| 39 | - protected $notices = array(); | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * Option key for storing notices. | |
| 43 | - * | |
| 44 | - * @since 3.2.6 | |
| 45 | - * | |
| 46 | - * @var string | |
| 47 | - */ | |
| 48 | - protected $option_id = 'lp_admin_notices'; | |
| 49 | - | |
| 50 | - /** | |
| 51 | - * Check is right plugin base. | |
| 52 | - * | |
| 53 | - * @return bool | |
| 54 | - */ | |
| 55 | - public static function check_plugin_base(): bool { | |
| 56 | - return 0 !== strcmp( LP_PLUGIN_BASENAME, 'learnpress/learnpress.php' ); | |
| 57 | - } | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * Check LP has beta version. | |
| 61 | - * | |
| 62 | - * @return bool|[] | |
| 63 | - */ | |
| 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; | |
| 92 | - } | |
| 93 | - | |
| 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() ); | |
| 114 | - } | |
| 115 | - | |
| 116 | - return [ | |
| 117 | - 'title' => $title, | |
| 118 | - 'description' => $description, | |
| 119 | - ]; | |
| 120 | - } | |
| 121 | - | |
| 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; | |
| 136 | - | |
| 137 | - return $body === '[TEST_REMOTE]' ? true : $body; | |
| 138 | - } | |
| 139 | - | |
| 140 | - /** | |
| 141 | - * Add new notice to show in admin page. | |
| 142 | - * | |
| 143 | - * @since 3.2.6 | |
| 144 | - * | |
| 145 | - * @param string|WP_Error $message | |
| 146 | - * @param string $type | |
| 147 | - * @param bool $dismissible | |
| 148 | - * @param string $id | |
| 149 | - * @param bool $redirect | |
| 150 | - * @param bool $override | |
| 151 | - * | |
| 152 | - * @return boolean | |
| 153 | - * @deprecated 4.2.5 | |
| 154 | - */ | |
| 155 | - public function add( $message, $type = 'success', $dismissible = true, $id = '', $redirect = false, $override = false ) { | |
| 156 | - _deprecated_function( __METHOD__, '4.2.5' ); | |
| 157 | - return false; | |
| 158 | - if ( ! $id ) { | |
| 159 | - $id = uniqid(); | |
| 160 | - } | |
| 161 | - | |
| 162 | - if ( empty( $this->notices[ $id ] ) || $override ) { | |
| 163 | - $this->notices[ $id ] = array( | |
| 164 | - 'message' => $message, | |
| 165 | - 'type' => $type ? $type : 'success', | |
| 166 | - 'redirect' => $redirect, | |
| 167 | - 'dismissible' => $dismissible, | |
| 168 | - ); | |
| 169 | - | |
| 170 | - return true; | |
| 171 | - } | |
| 172 | - | |
| 173 | - return false; | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Remove one or more notices from queue by ids. | |
| 178 | - * | |
| 179 | - * @since 3.2.6 | |
| 180 | - * | |
| 181 | - * @param string|array $notice | |
| 182 | - * | |
| 183 | - * @return array|bool | |
| 184 | - */ | |
| 185 | - public function remove( $notice ) { | |
| 186 | - settype( $notice, 'array' ); | |
| 187 | - | |
| 188 | - if ( ! $this->notices ) { | |
| 189 | - return false; | |
| 190 | - } | |
| 191 | - | |
| 192 | - foreach ( $notice as $id ) { | |
| 193 | - if ( ! empty( $this->notices[ $id ] ) ) { | |
| 194 | - unset( $this->notices[ $id ] ); | |
| 195 | - } | |
| 196 | - } | |
| 197 | - | |
| 198 | - // Also remove in db | |
| 199 | - $redirects = get_option( $this->option_id ); | |
| 200 | - if ( $redirects ) { | |
| 201 | - foreach ( $notice as $id ) { | |
| 202 | - if ( ! empty( $redirects[ $id ] ) ) { | |
| 203 | - unset( $redirects[ $id ] ); | |
| 204 | - } | |
| 205 | - } | |
| 206 | - | |
| 207 | - update_option( $this->option_id, $redirects ); | |
| 208 | - } | |
| 209 | - | |
| 210 | - return $this->notices; | |
| 211 | - } | |
| 212 | - | |
| 213 | - /** | |
| 214 | - * Clear all notices. | |
| 215 | - * | |
| 216 | - * @since 3.2.6 | |
| 217 | - */ | |
| 218 | - public function clear() { | |
| 219 | - $this->notices = array(); | |
| 220 | - delete_option( $this->option_id ); | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * Get single instance of this class | |
| 225 | - * | |
| 226 | - * @since 3.2.6 | |
| 227 | - * | |
| 228 | - * @return LP_Admin_Notice | |
| 229 | - */ | |
| 230 | - public static function instance() { | |
| 231 | - if ( ! self::$instance ) { | |
| 232 | - self::$instance = new self(); | |
| 233 | - } | |
| 234 | - | |
| 235 | - return self::$instance; | |
| 236 | - } | |
| 237 | -} | |
| 238 | - | |
| 239 | -LP_Admin_Notice::instance(); | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Manage the admin notices and display them in admin | |
| 4 | + * | |
| 5 | + * @package LearnPress | |
| 6 | + * @author ThimPress | |
| 7 | + * @version 1.0 | |
| 8 | + */ | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Class LP_Admin_Notice | |
| 16 | + */ | |
| 17 | +class LP_Admin_Notice { | |
| 18 | + /** | |
| 19 | + * Store all notices which added anywhere before show | |
| 20 | + * | |
| 21 | + * @var array | |
| 22 | + */ | |
| 23 | + protected static $_notices = array(); | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * @since 3.2.6 | |
| 27 | + * | |
| 28 | + * @var LP_Admin_Notice | |
| 29 | + */ | |
| 30 | + protected static $instance = false; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * List of notices. | |
| 34 | + * | |
| 35 | + * @since 3.2.6 | |
| 36 | + * | |
| 37 | + * @var array | |
| 38 | + */ | |
| 39 | + protected $notices = array(); | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Option key for storing notices. | |
| 43 | + * | |
| 44 | + * @since 3.2.6 | |
| 45 | + * | |
| 46 | + * @var string | |
| 47 | + */ | |
| 48 | + protected $option_id = 'lp_admin_notices'; | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Check is right plugin base. | |
| 52 | + * | |
| 53 | + * @return bool | |
| 54 | + */ | |
| 55 | + public static function check_plugin_base(): bool { | |
| 56 | + return 0 !== strcmp( LP_PLUGIN_BASENAME, 'learnpress/learnpress.php' ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Check LP has beta version. | |
| 61 | + * | |
| 62 | + * @return bool|[] | |
| 63 | + */ | |
| 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; | |
| 92 | + } | |
| 93 | + | |
| 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() ); | |
| 114 | + } | |
| 115 | + | |
| 116 | + return [ | |
| 117 | + 'title' => $title, | |
| 118 | + 'description' => $description, | |
| 119 | + ]; | |
| 120 | + } | |
| 121 | + | |
| 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; | |
| 136 | + | |
| 137 | + return $body === '[TEST_REMOTE]' ? true : $body; | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Add new notice to show in admin page. | |
| 142 | + * | |
| 143 | + * @since 3.2.6 | |
| 144 | + * | |
| 145 | + * @param string|WP_Error $message | |
| 146 | + * @param string $type | |
| 147 | + * @param bool $dismissible | |
| 148 | + * @param string $id | |
| 149 | + * @param bool $redirect | |
| 150 | + * @param bool $override | |
| 151 | + * | |
| 152 | + * @return boolean | |
| 153 | + * @deprecated 4.2.5 | |
| 154 | + */ | |
| 155 | + public function add( $message, $type = 'success', $dismissible = true, $id = '', $redirect = false, $override = false ) { | |
| 156 | + _deprecated_function( __METHOD__, '4.2.5' ); | |
| 157 | + return false; | |
| 158 | + if ( ! $id ) { | |
| 159 | + $id = uniqid(); | |
| 160 | + } | |
| 161 | + | |
| 162 | + if ( empty( $this->notices[ $id ] ) || $override ) { | |
| 163 | + $this->notices[ $id ] = array( | |
| 164 | + 'message' => $message, | |
| 165 | + 'type' => $type ? $type : 'success', | |
| 166 | + 'redirect' => $redirect, | |
| 167 | + 'dismissible' => $dismissible, | |
| 168 | + ); | |
| 169 | + | |
| 170 | + return true; | |
| 171 | + } | |
| 172 | + | |
| 173 | + return false; | |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * Remove one or more notices from queue by ids. | |
| 178 | + * | |
| 179 | + * @since 3.2.6 | |
| 180 | + * | |
| 181 | + * @param string|array $notice | |
| 182 | + * | |
| 183 | + * @return array|bool | |
| 184 | + */ | |
| 185 | + public function remove( $notice ) { | |
| 186 | + settype( $notice, 'array' ); | |
| 187 | + | |
| 188 | + if ( ! $this->notices ) { | |
| 189 | + return false; | |
| 190 | + } | |
| 191 | + | |
| 192 | + foreach ( $notice as $id ) { | |
| 193 | + if ( ! empty( $this->notices[ $id ] ) ) { | |
| 194 | + unset( $this->notices[ $id ] ); | |
| 195 | + } | |
| 196 | + } | |
| 197 | + | |
| 198 | + // Also remove in db | |
| 199 | + $redirects = get_option( $this->option_id ); | |
| 200 | + if ( $redirects ) { | |
| 201 | + foreach ( $notice as $id ) { | |
| 202 | + if ( ! empty( $redirects[ $id ] ) ) { | |
| 203 | + unset( $redirects[ $id ] ); | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + update_option( $this->option_id, $redirects ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + return $this->notices; | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Clear all notices. | |
| 215 | + * | |
| 216 | + * @since 3.2.6 | |
| 217 | + */ | |
| 218 | + public function clear() { | |
| 219 | + $this->notices = array(); | |
| 220 | + delete_option( $this->option_id ); | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * Get single instance of this class | |
| 225 | + * | |
| 226 | + * @since 3.2.6 | |
| 227 | + * | |
| 228 | + * @return LP_Admin_Notice | |
| 229 | + */ | |
| 230 | + public static function instance() { | |
| 231 | + if ( ! self::$instance ) { | |
| 232 | + self::$instance = new self(); | |
| 233 | + } | |
| 234 | + | |
| 235 | + return self::$instance; | |
| 236 | + } | |
| 237 | +} | |
| 238 | + | |
| 239 | +LP_Admin_Notice::instance(); | |