| @@ -17,32 +17,23 @@ | ||
| 17 | 17 | */ |
| 18 | 18 | class User { |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | - * Holds the admin notices key. | |
| 22 | - * | |
| 23 | - * @var string Admin notices key. | |
| 21 | + * The admin notices key. | |
| 24 | 22 | */ |
| 25 | 23 | const ADMIN_NOTICES_KEY = 'elementor_admin_notices'; |
| 26 | 24 | |
| 27 | - /** | |
| 28 | - * Holds the editor introduction screen key. | |
| 29 | - * | |
| 30 | - * @var string Introduction key. | |
| 31 | - */ | |
| 32 | 25 | const INTRODUCTION_KEY = 'elementor_introduction'; |
| 33 | 26 | |
| 34 | - /** | |
| 35 | - * Holds the beta tester key. | |
| 36 | - * | |
| 37 | - * @var string Beta tester key. | |
| 38 | - */ | |
| 39 | 27 | const BETA_TESTER_META_KEY = 'elementor_beta_tester'; |
| 40 | 28 | |
| 41 | 29 | /** |
| 30 | + * API URL. | |
| 31 | + * | |
| 42 | 32 | * Holds the URL of the Beta Tester Opt-in API. |
| 43 | 33 | * |
| 44 | 34 | * @since 1.0.0 |
| 35 | + * @access private | |
| 45 | 36 | * |
| 46 | 37 | * @var string API URL. |
| 47 | 38 | */ |
| 48 | 39 | const BETA_TESTER_API_URL = 'https://my.elementor.com/api/v1/beta_tester/'; |
| @@ -47,17 +38,8 @@ | ||
| 47 | 38 | */ |
| 48 | 39 | const BETA_TESTER_API_URL = 'https://my.elementor.com/api/v1/beta_tester/'; |
| 49 | 40 | |
| 50 | 41 | /** |
| 51 | - * Holds the dismissed editor notices key. | |
| 52 | - * | |
| 53 | - * @since 3.19.0 | |
| 54 | - * | |
| 55 | - * @var string Editor notices key. | |
| 56 | - */ | |
| 57 | - const DISMISSED_EDITOR_NOTICES_KEY = 'elementor_dismissed_editor_notices'; | |
| 58 | - | |
| 59 | - /** | |
| 60 | 42 | * Init. |
| 61 | 43 | * |
| 62 | 44 | * Initialize Elementor user. |
| 63 | 45 | * |
| @@ -72,9 +54,8 @@ | ||
| 72 | 54 | add_action( 'elementor/ajax/register_actions', [ __CLASS__, 'register_ajax_actions' ] ); |
| 73 | 55 | } |
| 74 | 56 | |
| 75 | 57 | /** |
| 76 | - * @param Ajax $ajax | |
| 77 | 58 | * @since 2.1.0 |
| 78 | 59 | * @access public |
| 79 | 60 | * @static |
| 80 | 61 | */ |
| @@ -80,9 +61,8 @@ | ||
| 80 | 61 | */ |
| 81 | 62 | public static function register_ajax_actions( Ajax $ajax ) { |
| 82 | 63 | $ajax->register_ajax_action( 'introduction_viewed', [ __CLASS__, 'set_introduction_viewed' ] ); |
| 83 | 64 | $ajax->register_ajax_action( 'beta_tester_signup', [ __CLASS__, 'register_as_beta_tester' ] ); |
| 84 | - $ajax->register_ajax_action( 'dismissed_editor_notices', [ __CLASS__, 'set_dismissed_editor_notices' ] ); | |
| 85 | 65 | } |
| 86 | 66 | |
| 87 | 67 | /** |
| 88 | 68 | * Is current user can edit. |
| @@ -103,9 +83,9 @@ | ||
| 103 | 83 | if ( ! $post ) { |
| 104 | 84 | return false; |
| 105 | 85 | } |
| 106 | 86 | |
| 107 | - if ( 'trash' === get_post_status( $post->ID ) ) { | |
| 87 | + if ( 'trash' === get_post_status( $post_id ) ) { | |
| 108 | 88 | return false; |
| 109 | 89 | } |
| 110 | 90 | |
| 111 | 91 | if ( ! self::is_current_user_can_edit_post_type( $post->post_type ) ) { |
| @@ -118,13 +98,13 @@ | ||
| 118 | 98 | return false; |
| 119 | 99 | } |
| 120 | 100 | |
| 121 | 101 | $edit_cap = $post_type_object->cap->edit_post; |
| 122 | - if ( ! current_user_can( $edit_cap, $post->ID ) ) { | |
| 102 | + if ( ! current_user_can( $edit_cap, $post_id ) ) { | |
| 123 | 103 | return false; |
| 124 | 104 | } |
| 125 | 105 | |
| 126 | - if ( intval( get_option( 'page_for_posts' ) ) === $post->ID ) { | |
| 106 | + if ( get_option( 'page_for_posts' ) === $post_id ) { | |
| 127 | 107 | return false; |
| 128 | 108 | } |
| 129 | 109 | |
| 130 | 110 | return true; |
| @@ -189,22 +169,21 @@ | ||
| 189 | 169 | * |
| 190 | 170 | * Retrieve the list of notices for the current user. |
| 191 | 171 | * |
| 192 | 172 | * @since 2.0.0 |
| 193 | - * @access public | |
| 173 | + * @access private | |
| 194 | 174 | * @static |
| 195 | 175 | * |
| 196 | 176 | * @return array A list of user notices. |
| 197 | 177 | */ |
| 198 | - public static function get_user_notices() { | |
| 199 | - $notices = get_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, true ); | |
| 200 | - return is_array( $notices ) ? $notices : []; | |
| 178 | + private static function get_user_notices() { | |
| 179 | + return get_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, true ); | |
| 201 | 180 | } |
| 202 | 181 | |
| 203 | 182 | /** |
| 204 | - * Is admin notice viewed. | |
| 183 | + * Is user notice viewed. | |
| 205 | 184 | * |
| 206 | - * Whether the admin notice was viewed by the current user. | |
| 185 | + * Whether the notice was viewed by the user. | |
| 207 | 186 | * |
| 208 | 187 | * @since 1.0.0 |
| 209 | 188 | * @access public |
| 210 | 189 | * @static |
| @@ -210,45 +189,24 @@ | ||
| 210 | 189 | * @static |
| 211 | 190 | * |
| 212 | 191 | * @param int $notice_id The notice ID. |
| 213 | 192 | * |
| 214 | - * @return bool Whether the admin notice was viewed by the user. | |
| 193 | + * @return bool Whether the notice was viewed by the user. | |
| 215 | 194 | */ |
| 216 | 195 | public static function is_user_notice_viewed( $notice_id ) { |
| 217 | 196 | $notices = self::get_user_notices(); |
| 218 | 197 | |
| 219 | - if ( empty( $notices[ $notice_id ] ) ) { | |
| 198 | + if ( empty( $notices ) || empty( $notices[ $notice_id ] ) ) { | |
| 220 | 199 | return false; |
| 221 | 200 | } |
| 222 | 201 | |
| 223 | - // BC: Handles old structure ( `[ 'notice_id' => 'true' ]` ). | |
| 224 | - if ( 'true' === $notices[ $notice_id ] ) { | |
| 225 | - return true; | |
| 226 | - } | |
| 227 | - | |
| 228 | - return $notices[ $notice_id ]['is_viewed'] ?? false; | |
| 202 | + return true; | |
| 229 | 203 | } |
| 230 | 204 | |
| 231 | 205 | /** |
| 232 | - * Checks whether the current user is allowed to upload JSON files. | |
| 233 | - * | |
| 234 | - * Note: The 'json-upload' capability is managed by the Role Manager as a part of its blacklist restrictions. | |
| 235 | - * In this context, we are negating the user's permission check to use it as a whitelist, allowing uploads. | |
| 236 | - * | |
| 237 | - * @return bool Whether the current user can upload JSON files. | |
| 238 | - */ | |
| 239 | - public static function is_current_user_can_upload_json() { | |
| 240 | - return current_user_can( 'manage_options' ) || ! Plugin::instance()->role_manager->user_can( 'json-upload' ); | |
| 241 | - } | |
| 242 | - | |
| 243 | - public static function is_current_user_can_use_custom_html() { | |
| 244 | - return current_user_can( 'manage_options' ) || ! Plugin::instance()->role_manager->user_can( 'custom-html' ); | |
| 245 | - } | |
| 246 | - | |
| 247 | - /** | |
| 248 | 206 | * Set admin notice as viewed. |
| 249 | 207 | * |
| 250 | - * Flag the admin notice as viewed by the current user, using an authenticated ajax request. | |
| 208 | + * Flag the user admin notice as viewed using an authenticated ajax request. | |
| 251 | 209 | * |
| 252 | 210 | * Fired by `wp_ajax_elementor_set_admin_notice_viewed` action. |
| 253 | 211 | * |
| 254 | 212 | * @since 1.0.0 |
| @@ -255,18 +213,19 @@ | ||
| 255 | 213 | * @access public |
| 256 | 214 | * @static |
| 257 | 215 | */ |
| 258 | 216 | public static function ajax_set_admin_notice_viewed() { |
| 259 | - // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification | |
| 260 | - $notice_id = Utils::get_super_global_value( $_REQUEST, 'notice_id' ); | |
| 261 | - | |
| 262 | - if ( ! $notice_id ) { | |
| 217 | + if ( empty( $_REQUEST['notice_id'] ) ) { | |
| 263 | 218 | wp_die(); |
| 264 | 219 | } |
| 265 | 220 | |
| 266 | - check_admin_referer( 'elementor_set_admin_notice_viewed' ); | |
| 221 | + $notices = self::get_user_notices(); | |
| 222 | + if ( empty( $notices ) ) { | |
| 223 | + $notices = []; | |
| 224 | + } | |
| 267 | 225 | |
| 268 | - self::set_user_notice( $notice_id ); | |
| 226 | + $notices[ $_REQUEST['notice_id'] ] = 'true'; | |
| 227 | + update_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, $notices ); | |
| 269 | 228 | |
| 270 | 229 | if ( ! wp_doing_ajax() ) { |
| 271 | 230 | wp_safe_redirect( admin_url() ); |
| 272 | 231 | die; |
| @@ -275,30 +234,8 @@ | ||
| 275 | 234 | wp_die(); |
| 276 | 235 | } |
| 277 | 236 | |
| 278 | 237 | /** |
| 279 | - * @param string $notice_id | |
| 280 | - * @param bool $is_viewed | |
| 281 | - * @param array $meta | |
| 282 | - * | |
| 283 | - * @return void | |
| 284 | - */ | |
| 285 | - public static function set_user_notice( $notice_id, $is_viewed = true, $meta = null ) { | |
| 286 | - $notices = self::get_user_notices(); | |
| 287 | - | |
| 288 | - if ( ! is_array( $meta ) ) { | |
| 289 | - $meta = $notices[ $notice_id ]['meta'] ?? []; | |
| 290 | - } | |
| 291 | - | |
| 292 | - $notices[ $notice_id ] = [ | |
| 293 | - 'is_viewed' => $is_viewed, | |
| 294 | - 'meta' => $meta, | |
| 295 | - ]; | |
| 296 | - | |
| 297 | - update_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, $notices ); | |
| 298 | - } | |
| 299 | - | |
| 300 | - /** | |
| 301 | 238 | * @since 2.1.0 |
| 302 | 239 | * @access public |
| 303 | 240 | * @static |
| 304 | 241 | */ |
| @@ -309,16 +246,9 @@ | ||
| 309 | 246 | |
| 310 | 247 | update_user_meta( get_current_user_id(), self::INTRODUCTION_KEY, $user_introduction_meta ); |
| 311 | 248 | } |
| 312 | 249 | |
| 313 | - /** | |
| 314 | - * @throws \Exception If the user cannot install plugins. | |
| 315 | - */ | |
| 316 | 250 | public static function register_as_beta_tester( array $data ) { |
| 317 | - if ( ! current_user_can( 'install_plugins' ) ) { | |
| 318 | - throw new \Exception( 'You do not have permission to install plugins.' ); | |
| 319 | - } | |
| 320 | - | |
| 321 | 251 | update_user_meta( get_current_user_id(), self::BETA_TESTER_META_KEY, true ); |
| 322 | 252 | $response = wp_safe_remote_post( |
| 323 | 253 | self::BETA_TESTER_API_URL, |
| 324 | 254 | [ |
| @@ -359,59 +289,6 @@ | ||
| 359 | 289 | return empty( $user_introduction_meta[ $key ] ) ? '' : $user_introduction_meta[ $key ]; |
| 360 | 290 | } |
| 361 | 291 | |
| 362 | 292 | return $user_introduction_meta; |
| 363 | - } | |
| 364 | - | |
| 365 | - /** | |
| 366 | - * Get a user option with a fallback value. | |
| 367 | - * | |
| 368 | - * @param string $option Option key. | |
| 369 | - * @param int $user_id User ID. | |
| 370 | - * @param mixed $fallback Default fallback value. | |
| 371 | - * | |
| 372 | - * @return mixed | |
| 373 | - */ | |
| 374 | - public static function get_user_option_with_default( $option, $user_id, $fallback ) { | |
| 375 | - $value = get_user_option( $option, $user_id ); | |
| 376 | - | |
| 377 | - return ( false === $value ) ? $fallback : $value; | |
| 378 | - } | |
| 379 | - | |
| 380 | - /** | |
| 381 | - * Get dismissed editor notices. | |
| 382 | - * | |
| 383 | - * Retrieve the list of dismissed editor notices for the current user. | |
| 384 | - * | |
| 385 | - * @since 3.19.0 | |
| 386 | - * @access public | |
| 387 | - * @static | |
| 388 | - * | |
| 389 | - * @return array A list of dismissed editor notices. | |
| 390 | - */ | |
| 391 | - public static function get_dismissed_editor_notices() { | |
| 392 | - $notices = get_user_meta( get_current_user_id(), self::DISMISSED_EDITOR_NOTICES_KEY, true ); | |
| 393 | - | |
| 394 | - return is_array( $notices ) ? $notices : []; | |
| 395 | - } | |
| 396 | - | |
| 397 | - /** | |
| 398 | - * Set dismissed editor notices for the current user. | |
| 399 | - * | |
| 400 | - * @since 3.19.0 | |
| 401 | - * @access public | |
| 402 | - * @static | |
| 403 | - * | |
| 404 | - * @param array $data Editor notices. | |
| 405 | - * | |
| 406 | - * @return void | |
| 407 | - */ | |
| 408 | - public static function set_dismissed_editor_notices( array $data ) { | |
| 409 | - $editor_notices = self::get_dismissed_editor_notices(); | |
| 410 | - | |
| 411 | - if ( ! in_array( $data['dismissId'], $editor_notices, true ) ) { | |
| 412 | - $editor_notices[] = $data['dismissId']; | |
| 413 | - | |
| 414 | - update_user_meta( get_current_user_id(), self::DISMISSED_EDITOR_NOTICES_KEY, $editor_notices ); | |
| 415 | - } | |
| 416 | 293 | } |
| 417 | 294 | } |