From af1979f88f5896b53fcfac9aee874ce1eae255bb Mon Sep 17 00:00:00 2001 From: Simon Costea Date: Thu, 22 Oct 2026 11:43:12 +0300 Subject: [PATCH] Show the site report's result next to the button MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After pressing "Send site report now", the page reloaded at the top and WordPress moved the result notice under the page title, away from the button, so it looked as if nothing had happened. The report is now sent before the settings page is printed, so the browser keeps showing the button, disabled with "Sending…" and a spinner, until the result is ready. The page then comes back to the Site report section, shows the result in an inline notice under the button and moves the focus to it. A page that had expired now says so instead of showing nothing. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../includes/class-bcc-login-site-report.php | 98 +++++++++++++------ 1 file changed, 68 insertions(+), 30 deletions(-) diff --git a/plugins/bcc-login/includes/class-bcc-login-site-report.php b/plugins/bcc-login/includes/class-bcc-login-site-report.php index 5314e26..b815e3f 100644 --- a/plugins/bcc-login/includes/class-bcc-login-site-report.php +++ b/plugins/bcc-login/includes/class-bcc-login-site-report.php @@ -15,6 +15,12 @@ class BCC_Login_Site_Report { private $plugin; private $plugin_version; + /** + * @var array|WP_Error|null What pressing "Send site report now" gave: the report that was sent, or why it + * couldn't be sent. + */ + private $sent_now; + function __construct( BCC_Login_Settings $settings, $plugin, $plugin_version ) { $this->_settings = $settings; $this->plugin = $plugin; @@ -22,6 +28,9 @@ function __construct( BCC_Login_Settings $settings, $plugin, $plugin_version ) { // After the daily cleanup, so the leftover count shows what it couldn't remove yet. add_action( BCC_Login_Cleanup::CRON_HOOK, array( $this, 'send' ), 20 ); + // Before the settings page (Settings > BCC Login) is printed, so the browser keeps showing "Sending…" + // until the result is ready, instead of the top of the page. + add_action( 'load-settings_page_bcc_login', array( $this, 'send_now_handler' ) ); add_action( 'bcc_login_settings_maintenance', array( $this, 'render_send_now_button' ) ); } @@ -102,52 +111,81 @@ function render_send_now_button() { if ( ! current_user_can( 'administrator' ) ) { return; } + // After sending, the page comes back to this section and shows the result under the button. WordPress + // moves other notices to the top of the page, out of sight from down here. ?> -

+

-
+ - + + +
+ + + sent_now ) ) : ?> +
+

+

sent_now->get_error_message() ); ?>

+
+ sent_now ) : ?> +
+

+

sent_now['siteUrl'], + $this->sent_now['pluginVersion'], + $this->sent_now['wordpressVersion'], + $this->sent_now['phpVersion'], + null === $this->sent_now['users'] ? '?' : number_format_i18n( $this->sent_now['users'] ), + null === $this->sent_now['subscribers'] ? '?' : number_format_i18n( $this->sent_now['subscribers'] ) + ) ); ?>

+
+ send_now_handler(); - settings_errors( 'site_report_sent' ); } /** - * Sends the report when the button was pressed, and shows what was sent or why it couldn't be sent. + * Sends the report when the button was pressed, for render_send_now_button() to show what was sent or why + * it couldn't be sent. */ function send_now_handler() { - if ( - ! isset( $_POST['send_site_report'], $_POST['send_site_report_nonce'] ) || - ! wp_verify_nonce( $_POST['send_site_report_nonce'], 'send_site_report' ) || - ! current_user_can( 'administrator' ) - ) { + if ( ! isset( $_POST['send_site_report'] ) || ! current_user_can( 'administrator' ) ) { return; } - $report = $this->send_now(); - - // settings_errors() prints messages as HTML, and Auth0's error description comes from outside. - if ( is_wp_error( $report ) ) { - add_settings_error( 'site_report_sent', 'site_report_not_sent', esc_html( $report->get_error_message() ), 'error' ); + // The nonce stops other sites from sending the form on an administrator's behalf. It also runs out, + // e.g. when the page was left open for a day or loaded before logging in again. + if ( ! isset( $_POST['send_site_report_nonce'] ) || ! wp_verify_nonce( $_POST['send_site_report_nonce'], 'send_site_report' ) ) { + $this->sent_now = new WP_Error( 'bcc_login_site_report', __( 'The page had expired, so nothing was sent. Press the button again to send the site report.', 'bcc-login' ) ); return; } - add_settings_error( - 'site_report_sent', - 'site_report_sent', - esc_html( sprintf( - __( 'The site report was sent: %1$s, bcc-login %2$s, WordPress %3$s, PHP %4$s, users: %5$s, subscribers: %6$s.', 'bcc-login' ), - $report['siteUrl'], - $report['pluginVersion'], - $report['wordpressVersion'], - $report['phpVersion'], - null === $report['users'] ? '?' : $report['users'], - null === $report['subscribers'] ? '?' : $report['subscribers'] - ) ), - 'success' - ); + $this->sent_now = $this->send_now(); } function get_report() {