Controllers
10 months ago
Fly-Out
10 months ago
Helpers
10 months ago
Methods
10 months ago
SettingsPages
10 months ago
Views
10 months ago
class-help-contact-us.php
10 months ago
class-plugin-updated-notice.php
10 months ago
class-premium-features.php
10 months ago
class-settings-page.php
10 months ago
class-setup-wizard.php
10 months ago
class-user-listing.php
10 months ago
class-user-notices.php
10 months ago
class-user-profile.php
10 months ago
class-user-registered.php
10 months ago
index.php
10 months ago
class-plugin-updated-notice.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for WP2FA update notices. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage user-utils |
| 7 | * @copyright 2025 Melapress |
| 8 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 9 | * @link https://wordpress.org/plugins/wp-2fa/ |
| 10 | */ |
| 11 | |
| 12 | namespace WP2FA\Admin; |
| 13 | |
| 14 | use WP2FA\Utils\Settings_Utils; |
| 15 | |
| 16 | /** |
| 17 | * Plugin_Updated_Notice class with user notification filters |
| 18 | * |
| 19 | * @since 2.7.0 |
| 20 | */ |
| 21 | if ( ! class_exists( '\WP2FA\Admin\Plugin_Updated_Notice' ) ) { |
| 22 | /** |
| 23 | * Plugin_Updated_Notice - Class for displaying notices to our users. |
| 24 | */ |
| 25 | class Plugin_Updated_Notice { |
| 26 | |
| 27 | /** |
| 28 | * Lets set things up |
| 29 | * |
| 30 | * @since 2.7.0 |
| 31 | */ |
| 32 | public static function init() { |
| 33 | add_action( 'admin_init', array( __CLASS__, 'on_plugin_update' ), 10 ); |
| 34 | add_action( 'admin_notices', array( __CLASS__, 'plugin_update_banner' ) ); |
| 35 | add_action( 'network_admin_notices', array( __CLASS__, 'plugin_update_banner' ) ); |
| 36 | add_action( 'wp_ajax_dismiss_update_notice', array( __CLASS__, 'dismiss_update_notice' ) ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * The nag content |
| 41 | * |
| 42 | * @since 2.7.0 |
| 43 | * @return void |
| 44 | */ |
| 45 | public static function plugin_update_banner() { |
| 46 | $screen = get_current_screen(); |
| 47 | $correct_screen = ( 'toplevel_page_wp-2fa-policies-network' === $screen->base || 'toplevel_page_wp-2fa-policies' === $screen->base ); |
| 48 | |
| 49 | if ( $correct_screen && Settings_Utils::get_option( 'wp_2fa_update_notice_needed', false ) ) { |
| 50 | /* translators: %s: version number. */ |
| 51 | printf( |
| 52 | '<div id="wp_2fa_update_notice" class="notice notice-success is-dismissible"><img src="%1$s"><p><strong>%2$s</strong></p><p>%3$s</p><a href="https://melapress.com/wordpress-2fa/releases/" target="_blank" class="button button-primary dismiss_update_notice" data-dismiss-nonce="%4$s">%5$s</a></p></div>', |
| 53 | esc_url( WP_2FA_URL . 'dist/images/wp-2fa-square.png' ), |
| 54 | esc_html__( 'Thank you for updating WP 2FA.', 'wp-2fa' ), |
| 55 | sprintf( esc_html__( 'This is version %s. Check out the release notes to see what is new and improved in this update.', 'wp-2fa' ), esc_html( WP_2FA_VERSION ) ), |
| 56 | esc_attr( wp_create_nonce( 'wp_2fa_dismiss_update_notice_nonce' ) ), |
| 57 | esc_html__( 'Release notes', 'wp-2fa' ) |
| 58 | ); |
| 59 | ?> |
| 60 | <script type="text/javascript"> |
| 61 | //<![CDATA[ |
| 62 | jQuery(document).ready(function( $ ) { |
| 63 | jQuery( 'body' ).on( 'click', 'a.dismiss_update_notice, #wp_2fa_update_notice .notice-dismiss', function ( e ) { |
| 64 | e.preventDefault(); |
| 65 | var nonce = jQuery( '#wp_2fa_update_notice [data-dismiss-nonce]' ).data( 'dismiss-nonce' ); |
| 66 | |
| 67 | jQuery.ajax({ |
| 68 | type: 'POST', |
| 69 | url: '<?php echo esc_url( \admin_url( 'admin-ajax.php' ) ); ?>', |
| 70 | data: { |
| 71 | action: 'dismiss_update_notice', |
| 72 | nonce : nonce, |
| 73 | }, |
| 74 | success: function ( result ) { |
| 75 | jQuery( '#wp_2fa_update_notice' ).slideUp( 300 ); |
| 76 | } |
| 77 | }); |
| 78 | }); |
| 79 | }); |
| 80 | //]]> |
| 81 | </script> |
| 82 | <style> |
| 83 | #wp_2fa_update_notice { |
| 84 | border: 2px solid #0f5cf2 |
| 85 | } |
| 86 | #wp_2fa_update_notice .button-primary { |
| 87 | background: #0f5cf2; |
| 88 | border-color: #0f5cf2; |
| 89 | } |
| 90 | #wp_2fa_update_notice img { |
| 91 | float: left; |
| 92 | max-width: 100px; |
| 93 | margin: 10px 12px 10px 0; |
| 94 | } |
| 95 | </style> |
| 96 | <?php |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Redirects user to admin on plugin update. |
| 102 | * |
| 103 | * @since 2.7.0 |
| 104 | * @return void |
| 105 | */ |
| 106 | public static function on_plugin_update() { |
| 107 | if ( Settings_Utils::get_option( 'wp_2fa_update_redirection_needed', false ) ) { |
| 108 | delete_site_option( 'wp_2fa_update_redirection_needed' ); |
| 109 | update_site_option( 'wp_2fa_update_notice_needed', true ); |
| 110 | $args = array( |
| 111 | 'page' => 'wp-2fa-policies', |
| 112 | ); |
| 113 | $url = add_query_arg( $args, network_admin_url( 'admin.php' ) ); |
| 114 | wp_safe_redirect( $url ); |
| 115 | exit; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Handle notice dismissal. |
| 121 | * |
| 122 | * @since 2.7.0 |
| 123 | * @return void |
| 124 | */ |
| 125 | public static function dismiss_update_notice() { |
| 126 | // Grab POSTed data. |
| 127 | $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : false; |
| 128 | // Check nonce. |
| 129 | if ( ! current_user_can( 'manage_options' ) || empty( $nonce ) || ! wp_verify_nonce( $nonce, 'wp_2fa_dismiss_update_notice_nonce' ) ) { |
| 130 | wp_send_json_error( esc_html__( 'Nonce Verification Failed.', 'wp-2fa' ) ); |
| 131 | } |
| 132 | |
| 133 | delete_site_option( 'wp_2fa_update_notice_needed' ); |
| 134 | |
| 135 | wp_send_json_success( esc_html__( 'Complete.', 'wp-2fa' ) ); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 |