Plugin Directory

Changeset 3830659


Ignore:
Timestamp:
11/28/2026 12:33:42 AM (109 minutes ago)
Author:
nanopost
Message:

Update to version 1.8.0 from GitHub

Location:
nanopost-email-check
Files:
4 added
36 edited
1 copied

Legend:

Unmodified
Added
Removed
  • nanopost-email-check/tags/1.8.0/assets/proof.js

    r3829352 r3830659  
    6767                                }
    6868                                if ( 'missing' === data.status ) {
    69                                         return show( cfg.text.missing );
     69                                        return show( data.reason ? cfg.text.missing + ' ' + data.reason : cfg.text.missing );
     70                                }
     71                                if ( 'unknown' === data.status ) {
     72                                        return show( cfg.text.unknown );
    7073                                }
    7174                                if ( 'send_failed' === data.status ) {
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-admin.php

    r3829352 r3830659  
    2424        const REVIEW_URL = 'https://wordpress.org/support/plugin/nanopost-email-check/reviews/#new-post';
    2525
    26         const CONNECT_ACTION    = 'nanopost_email_check_connect';
    27         const RETURN_ACTION     = 'nanopost_email_check_connect_return';
    28         const DISCONNECT_ACTION = 'nanopost_email_check_disconnect';
    29         const RESULT_ACTION     = 'nanopost_email_check_test_result';
    30         const DISMISS_ENDED     = 'nanopost_email_check_dismiss_ended';
    31 
    32         /**
    33          * Per-user transients: the connect state (60 minutes) and the test to show proof for.
    34          */
    35         const STATE_PREFIX = 'nanopost_email_check_connect_state_';
     26        const TURN_ON_ACTION     = 'nanopost_email_check_turn_on';
     27        const TURN_OFF_ACTION    = 'nanopost_email_check_turn_off';
     28        const ALERT_EMAIL_ACTION = 'nanopost_email_check_alert_email';
     29        const RETURN_ACTION      = 'nanopost_email_check_connect_return';
     30        const RESULT_ACTION      = 'nanopost_email_check_test_result';
     31        const DISMISS_ENDED      = 'nanopost_email_check_dismiss_ended';
     32        const STATE_ACTION       = 'nanopost_email_check_canary_state';
     33
     34        /**
     35         * Per-user transient: the test to show proof for.
     36         */
    3637        const PROOF_PREFIX = 'nanopost_email_check_proof_';
    3738
    … …  
    4445                add_action( 'admin_post_' . self::SEND_ACTION, array( __CLASS__, 'handle_send' ) );
    4546                add_action( 'admin_post_' . self::TEST_ALERT_ACTION, array( __CLASS__, 'handle_test_alert' ) );
    46                 add_action( 'admin_post_' . self::CONNECT_ACTION, array( __CLASS__, 'handle_connect' ) );
     47                add_action( 'admin_post_' . self::TURN_ON_ACTION, array( __CLASS__, 'handle_turn_on' ) );
     48                add_action( 'admin_post_' . self::TURN_OFF_ACTION, array( __CLASS__, 'handle_turn_off' ) );
     49                add_action( 'admin_post_' . self::ALERT_EMAIL_ACTION, array( __CLASS__, 'handle_alert_email' ) );
    4750                add_action( 'admin_post_' . self::RETURN_ACTION, array( __CLASS__, 'handle_connect_return' ) );
    48                 add_action( 'admin_post_' . self::DISCONNECT_ACTION, array( __CLASS__, 'handle_disconnect' ) );
    4951                add_action( 'admin_post_' . self::DISMISS_ENDED, array( __CLASS__, 'handle_dismiss_ended' ) );
    5052                add_action( 'wp_ajax_' . self::RESULT_ACTION, array( __CLASS__, 'ajax_test_result' ) );
     53                add_action( 'wp_ajax_' . self::STATE_ACTION, array( __CLASS__, 'ajax_canary_state' ) );
    5154                add_filter( 'plugin_action_links_' . plugin_basename( NANOPOST_EMAIL_CHECK_FILE ), array( __CLASS__, 'action_links' ) );
    5255        }
    … …  
    105108                add_settings_field(
    106109                        'nanopost_email_check_interval',
    107                         __( 'Check how often', 'nanopost-email-check' ),
     110                        __( 'Test how often', 'nanopost-email-check' ),
    108111                        array( __CLASS__, 'field_interval' ),
    109112                        self::PAGE,
    … …  
    184187                );
    185188                self::locked_note( 'recipient' );
    186                 if ( NanoPost_Email_Check_Connection::is_connected() ) {
     189                if ( NanoPost_Email_Check_Connection::canary_active() ) {
    187190                        echo '<p class="description nanopost-email-check-connected-note"><strong>';
    188                         esc_html_e( 'While this site is connected to nanoPost, test emails go only to nanoPost. This address is used again after you disconnect.', 'nanopost-email-check' );
     191                        esc_html_e( 'Not used while Email Canary is on: test emails then go to Email Canary.', 'nanopost-email-check' );
    189192                        echo '</strong></p>';
    190193                }
    … …  
    317320
    318321        /**
    319          * Handle "Connect to nanoPost": make the state, then send the browser to the app,
    320          * where the owner logs in or signs up and approves the site.
    321          */
    322         public static function handle_connect() {
     322         * Handle "Turn on Email Canary", also "Turn on" again after a stop. With the same
     323         * address and a canary that only stopped, this is "start" (no new confirm). Otherwise
     324         * the plugin makes the canary with a token it made itself: a retry after a lost answer
     325         * sends the same token. Then a test goes out at once, for the proof of arrival.
     326         */
     327        public static function handle_turn_on() {
    323328                if ( ! current_user_can( self::CAPABILITY ) ) {
    324                         wp_die( esc_html__( 'You are not allowed to connect this site.', 'nanopost-email-check' ), 403 );
    325                 }
    326                 check_admin_referer( self::CONNECT_ACTION );
    327 
    328                 $state = wp_generate_password( 32, false, false );
    329                 set_transient( self::STATE_PREFIX . get_current_user_id(), $state, HOUR_IN_SECONDS );
    330 
    331                 $return = add_query_arg( 'action', self::RETURN_ACTION, admin_url( 'admin-post.php' ) );
    332                 wp_redirect( NanoPost_Email_Check_App::connect_url( $return, $state ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- The app is on another host by design; the URL is built from the API base.
     329                        wp_die( esc_html__( 'You are not allowed to change Email Canary.', 'nanopost-email-check' ), 403 );
     330                }
     331                check_admin_referer( self::TURN_ON_ACTION );
     332
     333                $email = isset( $_POST['alert_email'] ) ? sanitize_email( wp_unslash( $_POST['alert_email'] ) ) : '';
     334                if ( ! is_email( $email ) ) {
     335                        self::done( array( 'outcome' => 'canary-email' ) );
     336                }
     337
     338                $block = NanoPost_Email_Check_Canary::block();
     339                if ( NanoPost_Email_Check_Connection::is_connected() ) {
     340                        $same = self::is_alert_address( $email, $block );
     341                        if ( $same && ( null === $block || 'not_confirmed' !== $block['stopped_reason'] ) ) {
     342                                // The same address: "start". It also answers when the canary already
     343                                // watches (for example, an earlier click timed out here but reached the app).
     344                                $call = NanoPost_Email_Check_App::start( 'turn_on' );
     345                                if ( ! $call['ok'] && 409 !== $call['code'] ) {
     346                                        self::done( self::failure( 'canary-failed', $call ) );
     347                                }
     348                                if ( $call['ok'] && NanoPost_Email_Check_Connection::canary_active() ) {
     349                                        self::send_first_test();
     350                                        $block = NanoPost_Email_Check_Canary::block();
     351                                        self::done( array( 'outcome' => null !== $block && 'over_limit' === $block['watching'] ? 'canary-over-limit' : 'canary-on-again' ) );
     352                                }
     353                                self::done( array( 'outcome' => 'canary-still-off' ) );
     354                        }
     355                        if ( ! $same ) {
     356                                // Another address: a new canary (spec "Wrong owner"). The old one stops and
     357                                // is deleted after 30 days.
     358                                NanoPost_Email_Check_App::disconnect();
     359                        }
     360                        // Not confirmed in 24 hours: POST /connect again with the same token, which
     361                        // starts it again and sends a new confirm email.
     362                }
     363
     364                $call = NanoPost_Email_Check_App::connect( $email );
     365                if ( ! $call['ok'] ) {
     366                        self::done( self::failure( 'canary-failed', $call ) );
     367                }
     368                self::send_first_test();
     369                self::done( array( 'outcome' => 'canary-on' ) );
     370        }
     371
     372        /**
     373         * The test that goes out at "Turn on", for the proof of arrival.
     374         */
     375        private static function send_first_test() {
     376                set_transient( self::SEND_LOCK, 1, MINUTE_IN_SECONDS );
     377                foreach ( NanoPost_Email_Check_Plugin::run_checks( NanoPost_Email_Check_Result::TRIGGER_MANUAL ) as $result ) {
     378                        if ( '' !== $result->test_id ) {
     379                                set_transient( self::PROOF_PREFIX . get_current_user_id(), $result->test_id, 10 * MINUTE_IN_SECONDS );
     380                        }
     381                }
     382        }
     383
     384        /**
     385         * Handle "Turn Email Canary off". Before the confirm nothing is kept, so the plugin
     386         * ends the connection. Otherwise it is a stop that keeps the token. When nanoPost
     387         * cannot be reached, nothing changes here: Email Canary still watches.
     388         */
     389        public static function handle_turn_off() {
     390                if ( ! current_user_can( self::CAPABILITY ) ) {
     391                        wp_die( esc_html__( 'You are not allowed to change Email Canary.', 'nanopost-email-check' ), 403 );
     392                }
     393                check_admin_referer( self::TURN_OFF_ACTION );
     394
     395                $block = NanoPost_Email_Check_Canary::block();
     396                if ( null !== $block && 'unconfirmed' === $block['watching'] ) {
     397                        NanoPost_Email_Check_App::disconnect();
     398                        self::done( array( 'outcome' => 'canary-off' ) );
     399                }
     400                $call = NanoPost_Email_Check_App::stop( 'turn_off' );
     401                if ( ! $call['ok'] && 409 !== $call['code'] ) {
     402                        self::done( self::failure( 'canary-off-failed', $call ) );
     403                }
     404                self::done( array( 'outcome' => NanoPost_Email_Check_Connection::is_connected() ? 'canary-off' : 'canary-off-ended' ) );
     405        }
     406
     407        /**
     408         * Handle "Resend" (the same address) and "Change" (another address).
     409         */
     410        public static function handle_alert_email() {
     411                if ( ! current_user_can( self::CAPABILITY ) ) {
     412                        wp_die( esc_html__( 'You are not allowed to change Email Canary.', 'nanopost-email-check' ), 403 );
     413                }
     414                check_admin_referer( self::ALERT_EMAIL_ACTION );
     415
     416                $email = isset( $_POST['alert_email'] ) ? sanitize_email( wp_unslash( $_POST['alert_email'] ) ) : '';
     417                if ( ! is_email( $email ) ) {
     418                        self::done( array( 'outcome' => 'canary-email' ) );
     419                }
     420                $resend = strtolower( $email ) === strtolower( self::alert_address( NanoPost_Email_Check_Canary::block() ) );
     421                $call   = NanoPost_Email_Check_App::set_alert_email( $email );
     422                if ( ! $call['ok'] ) {
     423                        self::done( self::failure( 'email-failed', $call ) );
     424                }
     425                $block   = NanoPost_Email_Check_Canary::block();
     426                $limited = null !== $block && null !== $block['confirm_mail'] && 'limited' === $block['confirm_mail']['state'];
     427                self::done(
     428                        array(
     429                                'outcome' => $limited ? 'email-limited' : ( $resend ? 'email-resent' : 'email-changed' ),
     430                                'detail'  => $email,
     431                        )
     432                );
     433        }
     434
     435        /**
     436         * True when an address is one of the canary's own: confirmed, pending, or the one
     437         * typed last. Then "Turn on" starts the same canary.
     438         *
     439         * @param string     $email Address.
     440         * @param array|null $block Block, or null.
     441         * @return bool
     442         */
     443        private static function is_alert_address( $email, $block ) {
     444                $own = array( NanoPost_Email_Check_Connection::alert_email() );
     445                if ( null !== $block ) {
     446                        $own = array_merge( $own, wp_list_pluck( $block['alerts_to'], 'email' ) );
     447                }
     448                return in_array( strtolower( $email ), array_map( 'strtolower', array_filter( $own ) ), true );
     449        }
     450
     451        /**
     452         * The confirmed address that gets the alerts, else alert_address().
     453         *
     454         * @param array|null $block Block, or null.
     455         * @return string
     456         */
     457        private static function confirmed_address( $block ) {
     458                if ( null !== $block ) {
     459                        foreach ( $block['alerts_to'] as $entry ) {
     460                                if ( 'confirmed' === $entry['state'] ) {
     461                                        return $entry['email'];
     462                                }
     463                        }
     464                }
     465                return self::alert_address( $block );
     466        }
     467
     468        /**
     469         * The address the owner set for alerts: the newest pending one, else the confirmed
     470         * one, else the one typed last.
     471         *
     472         * @param array|null $block Block, or null.
     473         * @return string
     474         */
     475        public static function alert_address( $block ) {
     476                $confirmed = '';
     477                $pending   = '';
     478                if ( null !== $block ) {
     479                        foreach ( $block['alerts_to'] as $entry ) {
     480                                if ( 'pending' === $entry['state'] ) {
     481                                        $pending = $entry['email'];
     482                                } elseif ( '' === $confirmed ) {
     483                                        $confirmed = $entry['email'];
     484                                }
     485                        }
     486                }
     487                if ( '' !== $pending ) {
     488                        return $pending;
     489                }
     490                return '' !== $confirmed ? $confirmed : NanoPost_Email_Check_Connection::alert_email();
     491        }
     492
     493        /**
     494         * The notice for a failed call.
     495         *
     496         * @param string $outcome Outcome key.
     497         * @param array  $call    Call result.
     498         * @return array
     499         */
     500        private static function failure( $outcome, $call ) {
     501                return array(
     502                        'outcome' => $outcome,
     503                        'detail'  => '' !== $call['message'] ? $call['message'] : self::call_detail( $call ),
     504                );
     505        }
     506
     507        /**
     508         * Store the outcome for one notice and go back to the screen.
     509         *
     510         * @param array $outcome Outcome (and detail).
     511         */
     512        private static function done( $outcome ) {
     513                set_transient( self::notice_key(), $outcome, MINUTE_IN_SECONDS );
     514                wp_safe_redirect( self::page_url() . '#nanopost-email-check-connection' );
    333515                exit;
    334516        }
    335517
    336518        /**
    337          * The app sends the browser back here with a one-time code and the state. Checks the
    338          * state (it stands in for a nonce), then exchanges the code for a token.
     519         * Sites that started the browser connect of version 1.7 come back here after an
     520         * update. That flow is gone: show the screen, where "Turn on Email Canary" is.
    339521         */
    340522        public static function handle_connect_return() {
    341                 if ( ! current_user_can( self::CAPABILITY ) ) {
    342                         wp_die( esc_html__( 'You are not allowed to connect this site.', 'nanopost-email-check' ), 403 );
    343                 }
    344 
    345                 // phpcs:disable WordPress.Security.NonceVerification.Recommended -- The state is the one-time check; it is compared below.
    346                 $state = isset( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : '';
    347                 $code  = isset( $_GET['code'] ) ? sanitize_text_field( wp_unslash( $_GET['code'] ) ) : '';
    348                 $error = isset( $_GET['error'] ) ? sanitize_key( wp_unslash( $_GET['error'] ) ) : '';
    349                 // phpcs:enable WordPress.Security.NonceVerification.Recommended
    350 
    351                 $key    = self::STATE_PREFIX . get_current_user_id();
    352                 $stored = get_transient( $key );
    353                 delete_transient( $key );
    354 
    355                 if ( ! is_string( $stored ) || '' === $state || ! hash_equals( $stored, $state ) ) {
    356                         $outcome = array( 'outcome' => 'connect-state' );
    357                 } elseif ( '' === $code && 'cancelled' === $error ) {
    358                         // The owner pressed Cancel in nanoPost: back to the screen, as if nothing happened.
    359                         $outcome = null;
    360                 } elseif ( '' === $code ) {
    361                         $outcome = array( 'outcome' => 'connect-cancelled' );
    362                 } else {
    363                         $call    = NanoPost_Email_Check_App::exchange( $code );
    364                         $outcome = $call['ok']
    365                                 ? array( 'outcome' => 'connected' )
    366                                 : array(
    367                                         'outcome' => 'invalid_code' === $call['error'] ? 'connect-invalid' : 'connect-failed',
    368                                         'detail'  => '' !== $call['message'] ? $call['message'] : self::call_detail( $call ),
    369                                 );
    370                 }
    371 
    372                 if ( null !== $outcome ) {
    373                         set_transient( self::notice_key(), $outcome, MINUTE_IN_SECONDS );
    374                 }
    375                 wp_safe_redirect( self::page_url() );
    376                 exit;
    377         }
    378 
    379         /**
    380          * Handle "Disconnect": tell the app (best effort) and delete the connection here.
    381          */
    382         public static function handle_disconnect() {
    383                 if ( ! current_user_can( self::CAPABILITY ) ) {
    384                         wp_die( esc_html__( 'You are not allowed to disconnect this site.', 'nanopost-email-check' ), 403 );
    385                 }
    386                 check_admin_referer( self::DISCONNECT_ACTION );
    387 
    388                 NanoPost_Email_Check_App::disconnect();
    389                 set_transient( self::notice_key(), 'disconnected', MINUTE_IN_SECONDS );
    390523                wp_safe_redirect( self::page_url() );
    391524                exit;
    … …  
    430563                }
    431564
     565                $test   = NanoPost_Email_Check_Canary::take_test_answer( $call['body'] );
    432566                $body   = $call['body'];
    433567                $fields = array();
    434                 foreach ( array( 'status', 'sent_at', 'arrived_at', 'spf', 'dkim', 'dmarc' ) as $key ) {
     568                foreach ( array( 'sent_at', 'arrived_at' ) as $key ) {
    435569                        $fields[ $key ] = isset( $body[ $key ] ) && is_string( $body[ $key ] ) ? $body[ $key ] : null;
    436570                }
    437                 $fields['status']    = in_array( $fields['status'], array( 'waiting', 'arrived', 'missing', 'send_failed' ), true ) ? $fields['status'] : 'waiting';
    438                 $arrived             = null === $fields['arrived_at'] ? false : strtotime( $fields['arrived_at'] );
    439                 $fields['arrived']   = false === $arrived ? null : self::format_date( $arrived );
     571                foreach ( array( 'spf', 'dkim', 'dmarc' ) as $key ) {
     572                        $fields[ $key ] = '' === $test[ $key ] ? null : $test[ $key ];
     573                }
     574                $fields['status']    = in_array( $test['arrival'], NanoPost_Email_Check_Canary::ARRIVALS, true ) ? $test['arrival'] : 'waiting';
     575                $fields['arrived']   = 0 === $test['arrived_at'] ? null : self::format_date( $test['arrived_at'] );
     576                $fields['reason']    = NanoPost_Email_Check_Canary::reason_text( $test['reason'] );
     577                $fields['help_url']  = $test['help_url'];
    440578                $fields['check_url'] = isset( $body['check_url'] ) && is_string( $body['check_url'] ) ? esc_url_raw( $body['check_url'], array( 'https', 'http' ) ) : NanoPost_Email_Check_Connection::get()['check_url'];
    441579                wp_send_json_success( $fields );
     580        }
     581
     582        /**
     583         * The state of the canary, asked by the browser when the plugin screen opens. The
     584         * plugin calls "GET /check" at most once a minute. When the state changed, the answer
     585         * has the new HTML of the Email Canary part and of the log (escaped on the server).
     586         */
     587        public static function ajax_canary_state() {
     588                check_ajax_referer( self::STATE_ACTION );
     589                if ( ! current_user_can( self::CAPABILITY ) ) {
     590                        wp_send_json_error( array( 'error' => 'forbidden' ), 403 );
     591                }
     592                if ( ! NanoPost_Email_Check_Connection::is_connected() || ! NanoPost_Email_Check_Canary::refresh_for_screen() ) {
     593                        wp_send_json_success( array( 'changed' => false ) );
     594                }
     595
     596                ob_start();
     597                self::render_canary();
     598                $canary = (string) ob_get_clean();
     599                ob_start();
     600                self::render_log();
     601                $log = (string) ob_get_clean();
     602
     603                wp_send_json_success(
     604                        array(
     605                                'changed' => true,
     606                                'canary'  => $canary,
     607                                'log'     => $log,
     608                        )
     609                );
    442610        }
    443611
    … …  
    485653                echo '<div id="nanopost-email-check-status">';
    486654                self::render_status();
     655                echo '<div id="nanopost-email-check-send">';
     656                self::render_send_form();
     657                echo '</div>';
    487658                echo '</div>';
    488659
    … …  
    490661                self::render_connection();
    491662                echo '</div>';
     663                self::enqueue_canary_assets();
    492664
    493665                echo '<form id="nanopost-email-check-settings" method="post" action="' . esc_url( admin_url( 'options.php' ) ) . '">';
    … …  
    504676                }
    505677
    506                 echo '<div id="nanopost-email-check-send">';
    507                 self::render_send_form();
    508                 echo '</div>';
    509 
    510678                echo '<div id="nanopost-email-check-log">';
    511679                self::render_log();
    … …  
    537705                }
    538706                $proof = '';
    539                 if ( in_array( $outcome, array( 'passed', 'failed' ), true ) ) {
     707                if ( in_array( $outcome, array( 'passed', 'failed', 'canary-on', 'canary-on-again', 'canary-over-limit' ), true ) ) {
    540708                        $proof = get_transient( self::PROOF_PREFIX . get_current_user_id() );
    541709                        delete_transient( self::PROOF_PREFIX . get_current_user_id() );
    542                         $proof = is_string( $proof ) && NanoPost_Email_Check_Connection::is_connected() ? $proof : '';
     710                        $proof = is_string( $proof ) && NanoPost_Email_Check_Connection::canary_active() ? $proof : '';
    543711                }
    544712
    … …  
    563731                        $class   = 'notice-warning';
    564732                        $message = __( 'A test email was sent less than one minute ago. Wait one minute, then try again.', 'nanopost-email-check' );
    565                 } elseif ( 'connected' === $outcome ) {
     733                } elseif ( 'canary-on' === $outcome ) {
    566734                        $class   = 'notice-success';
    567                         $message = __( 'This site is connected to nanoPost. Test emails now go to nanoPost, and nanoPost alerts you when a test does not arrive.', 'nanopost-email-check' );
    568                 } elseif ( 'disconnected' === $outcome ) {
     735                        $message = __( 'Email Canary: turned on. A test email went out. Its arrival shows below.', 'nanopost-email-check' );
     736                } elseif ( 'canary-on-again' === $outcome ) {
    569737                        $class   = 'notice-success';
    570                         $message = __( 'This site is disconnected from nanoPost. Test emails go to the address in the settings again.', 'nanopost-email-check' );
    571                 } elseif ( 'connect-state' === $outcome ) {
    572                         $class   = 'notice-error';
    573                         $message = __( 'The connection was not made: the request expired or did not come from this screen. Click "Connect to nanoPost" again.', 'nanopost-email-check' );
    574                 } elseif ( 'connect-cancelled' === $outcome ) {
     738                        $message = __( 'Email Canary is on again.', 'nanopost-email-check' );
     739                } elseif ( 'canary-over-limit' === $outcome ) {
    575740                        $class   = 'notice-warning';
    576                         $message = __( 'The connection was not made. Click "Connect to nanoPost" to try again.', 'nanopost-email-check' );
    577                 } elseif ( 'connect-invalid' === $outcome ) {
    578                         $class   = 'notice-error';
    579                         $message = __( 'The connection was not made: the approval code was wrong, used or expired. Click "Connect to nanoPost" again.', 'nanopost-email-check' );
    580                 } elseif ( 'connect-failed' === $outcome ) {
     741                        $message = __( 'Email Canary: alerts are OFF. Your nanoPost account has reached its limit of sites. Remove one in nanoPost, or ask us for more.', 'nanopost-email-check' );
     742                } elseif ( 'canary-still-off' === $outcome ) {
     743                        $class   = 'notice-warning';
     744                        $message = __( 'Email Canary could not turn on again. See why below.', 'nanopost-email-check' );
     745                } elseif ( 'canary-off' === $outcome ) {
     746                        $class   = 'notice-success';
     747                        $message = __( 'Email Canary is off. Email Check sends test emails to the address in the settings again. Email Canary keeps the history for 30 days.', 'nanopost-email-check' );
     748                } elseif ( 'canary-off-ended' === $outcome ) {
     749                        $class   = 'notice-success';
     750                        $message = __( 'Email Canary is off. Email Check sends test emails to the address in the settings again.', 'nanopost-email-check' );
     751                } elseif ( 'canary-off-failed' === $outcome ) {
    581752                        $class = 'notice-error';
    582753                        /* translators: %s: error text or HTTP status. */
    583                         $message = sprintf( __( 'The connection was not made: could not reach nanoPost (%s). Try again later.', 'nanopost-email-check' ), $extra );
     754                        $message = sprintf( __( 'Could not reach nanoPost (%s). Email Canary is still on. Try again later.', 'nanopost-email-check' ), $extra );
     755                } elseif ( 'canary-failed' === $outcome ) {
     756                        $class = 'notice-error';
     757                        /* translators: %s: error text or HTTP status. */
     758                        $message = sprintf( __( 'Email Canary did not turn on: %s. Nothing changed. Try again later.', 'nanopost-email-check' ), $extra );
     759                } elseif ( 'canary-email' === $outcome ) {
     760                        $class   = 'notice-error';
     761                        $message = __( 'Enter a valid email address for the alerts.', 'nanopost-email-check' );
     762                } elseif ( 'email-resent' === $outcome ) {
     763                        $class = 'notice-success';
     764                        /* translators: %s: email address. */
     765                        $message = sprintf( __( 'Email Canary: we sent the confirm email again to %s.', 'nanopost-email-check' ), $extra );
     766                } elseif ( 'email-changed' === $outcome ) {
     767                        $class = 'notice-success';
     768                        /* translators: %s: email address. */
     769                        $message = sprintf( __( 'Email Canary: we sent a confirm email to %s. Alerts go there once it is confirmed.', 'nanopost-email-check' ), $extra );
     770                } elseif ( 'email-limited' === $outcome ) {
     771                        $class   = 'notice-warning';
     772                        $message = __( 'Email Canary: we cannot send another confirm email now. Try again tomorrow. Proof of arrival still works.', 'nanopost-email-check' );
     773                } elseif ( 'email-failed' === $outcome ) {
     774                        $class = 'notice-error';
     775                        /* translators: %s: error text or HTTP status. */
     776                        $message = sprintf( __( 'The alert address did not change: %s. Try again later.', 'nanopost-email-check' ), $extra );
    584777                } elseif ( 'passed' === $outcome && '' !== $proof ) {
    585778                        $class   = 'notice-success';
    586                         $message = __( 'WordPress accepted the test email for nanoPost. The proof of arrival shows below.', 'nanopost-email-check' );
     779                        $message = __( 'WordPress accepted the test email for Email Canary. The proof of arrival shows below.', 'nanopost-email-check' );
    587780                } elseif ( 'passed' === $outcome ) {
    588781                        $class   = 'notice-success';
    … …  
    605798
    606799        /**
    607          * "Reconnect" notice after the connection ended without the owner's click.
     800         * Notice after the connection ended without the owner's click.
    608801         */
    609802        private static function render_ended_notice() {
    … …  
    613806                }
    614807                $message = NanoPost_Email_Check_Connection::ENDED_SITE_CHANGED === $reason
    615                         ? __( 'The connection to nanoPost ended because the site address changed (for example a copied or moved site). Test emails go to the address in the settings again. Connect again to use nanoPost from this address.', 'nanopost-email-check' )
    616                         : __( 'nanoPost ended the connection to this site (it was disconnected or deleted in nanoPost). Test emails go to the address in the settings again. Reconnect to use nanoPost.', 'nanopost-email-check' );
     808                        ? __( 'Email Canary is off: the site address changed (a copied or moved site). Turn it on again to use Email Canary from this address.', 'nanopost-email-check' )
     809                        : __( 'Email Canary is off: nanoPost ended it for this site (it was deleted in nanoPost, or stopped for more than 30 days). Email Check sends test emails to the address in the settings again.', 'nanopost-email-check' );
    617810
    618811                echo '<div class="notice notice-warning nanopost-email-check-reconnect"><p>' . esc_html( $message ) . '</p><p>';
    619                 self::connect_button( __( 'Reconnect', 'nanopost-email-check' ) );
    620                 echo ' <a href="' . esc_url( wp_nonce_url( add_query_arg( 'action', self::DISMISS_ENDED, admin_url( 'admin-post.php' ) ), self::DISMISS_ENDED ) ) . '">' . esc_html__( 'Dismiss', 'nanopost-email-check' ) . '</a>';
     812                echo '<a href="#nanopost-email-check-connection">' . esc_html__( 'Turn on again', 'nanopost-email-check' ) . '</a> &middot; ';
     813                echo '<a href="' . esc_url( wp_nonce_url( add_query_arg( 'action', self::DISMISS_ENDED, admin_url( 'admin-post.php' ) ), self::DISMISS_ENDED ) ) . '">' . esc_html__( 'Dismiss', 'nanopost-email-check' ) . '</a>';
    621814                echo '</p></div>';
    622815        }
    … …  
    634827                echo '<p class="nanopost-email-check-proof-status" aria-live="polite">' . esc_html__( 'Waiting for the test email to arrive at nanoPost ...', 'nanopost-email-check' ) . '</p>';
    635828                if ( '' !== $check_url ) {
    636                         echo '<p>' . self::help_link( $check_url, __( 'Open the check in nanoPost', 'nanopost-email-check' ) ) . ' &middot; '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     829                        echo '<p>' . self::help_link( $check_url, __( 'Open in nanoPost', 'nanopost-email-check' ) ) . ' &middot; '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    637830                        echo self::help_link( NanoPost_Email_Check_Help::proof( NanoPost_Email_Check_Help::PLACE_PROOF ), __( 'About proof of arrival', 'nanopost-email-check' ) ) . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    638831                }
    … …  
    653846                                        /* translators: 1: SPF result, 2: DKIM result, 3: DMARC result, such as "pass". */
    654847                                        'auth'        => __( 'SPF: %1$s. DKIM: %2$s. DMARC: %3$s.', 'nanopost-email-check' ),
    655                                         'missing'     => __( 'The test email did not arrive at nanoPost in time. nanoPost alerts you about it.', 'nanopost-email-check' ),
     848                                        'missing'     => __( 'The test email did not arrive at nanoPost in time.', 'nanopost-email-check' ),
    656849                                        'send_failed' => __( 'The site could not send the test email. See the error in the log below.', 'nanopost-email-check' ),
    657                                         'unreachable' => __( 'Could not reach nanoPost. Open the check in nanoPost to see the result.', 'nanopost-email-check' ),
    658                                         'timeout'     => __( 'No result yet. The email can still arrive: open the check in nanoPost to see the result.', 'nanopost-email-check' ),
     850                                        'unknown'     => __( 'Email Canary cannot tell now. Email Check sends your test emails as usual.', 'nanopost-email-check' ),
     851                                        'unreachable' => __( 'Could not reach nanoPost. Open nanoPost to see the result.', 'nanopost-email-check' ),
     852                                        'timeout'     => __( 'No result yet. The email can still arrive: the log below shows it later.', 'nanopost-email-check' ),
    659853                                        'none'        => __( 'none', 'nanopost-email-check' ),
    660854                                ),
    … …  
    664858
    665859        /**
    666          * The nanoPost section: consent and "Connect", or the connection and "Disconnect".
     860         * The Email Canary area: the offer with the consent text, or the state of the canary
     861         * with what the owner can do. The inner part is replaced by the browser when a newer
     862         * state comes (assets/canary.js).
    667863         */
    668864        private static function render_connection() {
    669                 echo '<h2>' . esc_html__( 'nanoPost', 'nanopost-email-check' ) . '</h2>';
     865                echo '<h2>' . esc_html__( 'Email Canary', 'nanopost-email-check' ) . ' <span class="nanopost-email-check-tagline">' . esc_html__( 'Did it arrive? Does it keep arriving?', 'nanopost-email-check' ) . '</span></h2>';
     866                echo '<div id="nanopost-email-check-canary">';
     867                self::render_canary();
     868                echo '</div>';
    670869
    671870                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
    672                         echo '<p>' . esc_html__( 'Optional. Connect this site to nanoPost (app.nanopo.st), a mail monitoring service. Each test email then goes to nanoPost, which shows here whether it arrived, with its SPF, DKIM and DMARC results, and alerts you when a test does not arrive.', 'nanopost-email-check' ) . '</p>';
    673                         echo '<p>' . esc_html__( 'When you connect, this site sends to nanoPost: the site address and the address of its admin area; the versions of this plugin, WordPress and PHP; the check interval; the webhook URL, if you set one; and for each test its ID, the time, what started it, and whether wp_mail() succeeded, with its error text, which can contain host names or email addresses. nanoPost also receives the test emails, and keeps the IP address of each call for 30 days. Nothing is sent before you connect.', 'nanopost-email-check' ) . '</p>';
    674                         echo '<p>';
    675                         self::connect_button( __( 'Connect to nanoPost', 'nanopost-email-check' ) );
    676                         echo ' ' . self::help_link( NanoPost_Email_Check_Help::TERMS_URL, __( 'Terms', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    677                         echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::PRIVACY_URL, __( 'Privacy', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    678                         echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::connect( NanoPost_Email_Check_Help::PLACE_CONNECTION ), __( 'How connecting works', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    679                         echo '</p>';
    680871                        return;
    681872                }
    682 
    683873                $connection = NanoPost_Email_Check_Connection::get();
    684                 echo '<p>';
    685                 echo esc_html(
    686                         sprintf(
    687                                 /* translators: %s: the check's email address in nanoPost. */
    688                                 __( 'Connected. Test emails go to nanoPost (%s).', 'nanopost-email-check' ),
    689                                 $connection['address']
    690                         )
    691                 );
    692                 if ( '' !== $connection['check_url'] ) {
    693                         echo ' ' . self::help_link( $connection['check_url'], __( 'Open the check in nanoPost', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    694                 }
    695                 echo '</p>';
    696 
    697                 $too_old = NanoPost_Email_Check_Connection::too_old_message();
     874                $too_old    = NanoPost_Email_Check_Connection::too_old_message();
    698875                if ( '' !== $too_old ) {
    699                         echo '<div class="notice notice-error inline"><p>' . esc_html( $too_old ) . ' ' . esc_html__( 'Until then, the plugin does not call nanoPost. Test emails still go to nanoPost.', 'nanopost-email-check' ) . '</p></div>';
     876                        echo '<div class="notice notice-error inline"><p>' . esc_html( $too_old ) . ' ' . esc_html__( 'Until then, the plugin does not call nanoPost. Test emails still go to Email Canary.', 'nanopost-email-check' ) . '</p></div>';
    700877                } elseif ( $connection['retry_until'] > time() ) {
    701878                        /* translators: %s: date and time. */
    702879                        echo '<p class="description">' . esc_html( sprintf( __( 'nanoPost asked the plugin to wait. Calls start again at %s.', 'nanopost-email-check' ), self::format_date( $connection['retry_until'] ) ) ) . '</p>';
    703880                }
    704 
     881        }
     882
     883        /**
     884         * The state part of the Email Canary area.
     885         */
     886        private static function render_canary() {
     887                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     888                        self::render_offer();
     889                        return;
     890                }
     891
     892                $block = NanoPost_Email_Check_Canary::block();
     893                if ( null === $block ) {
     894                        echo '<p>' . esc_html__( 'Email Canary: no state from nanoPost yet. It shows here after the next test email.', 'nanopost-email-check' ) . '</p>';
     895                        self::render_canary_actions( true );
     896                        return;
     897                }
     898
     899                // "Cannot tell" is about arrivals: it takes the place of "on", and does not hide a
     900                // stop or a missing confirm. The rest of the area stays, so the owner can still
     901                // change where alerts go.
     902                $address = self::alert_address( $block );
     903                switch ( $block['watching'] ) {
     904                        case 'on':
     905                                if ( NanoPost_Email_Check_Canary::cannot_tell( $block ) ) {
     906                                        echo '<p class="nanopost-email-check-state"><span aria-hidden="true">?</span> <strong>' . esc_html__( 'Email Canary cannot tell now.', 'nanopost-email-check' ) . '</strong> ' . esc_html__( 'Email Check sends your test emails as usual.', 'nanopost-email-check' ) . '</p>';
     907                                        break;
     908                                }
     909                                echo '<p class="nanopost-email-check-state nanopost-email-check-state-on"><span aria-hidden="true">&#9679;</span> <strong>' . esc_html__( 'Email Canary is on', 'nanopost-email-check' ) . '</strong></p>';
     910                                break;
     911                        case 'unconfirmed':
     912                                echo '<div class="notice notice-warning inline"><p><span aria-hidden="true">&#9650;</span> <strong>' . esc_html__( 'Alerts are OFF until you confirm.', 'nanopost-email-check' ) . '</strong> ';
     913                                /* translators: %s: email address. */
     914                                echo esc_html( sprintf( __( 'We sent an email to %s.', 'nanopost-email-check' ), $address ) );
     915                                if ( $block['confirm_by'] > 0 ) {
     916                                        /* translators: %s: date and time. */
     917                                        echo ' ' . esc_html( sprintf( __( 'Until %s, Email Canary shows here whether test emails arrive, but sends no alerts. Then it turns off.', 'nanopost-email-check' ), self::format_date( $block['confirm_by'] ) ) );
     918                                }
     919                                echo ' ' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CONFIRM, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'Why confirm', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     920                                echo '</p>';
     921                                self::render_confirm_mail( $block, $address );
     922                                echo '</div>';
     923                                break;
     924                        case 'over_limit':
     925                                echo '<div class="notice notice-warning inline"><p><strong>' . esc_html__( 'Email Canary: alerts are OFF.', 'nanopost-email-check' ) . '</strong> ' . esc_html__( 'Your nanoPost account has reached its limit of sites. Remove one in nanoPost, or ask us for more.', 'nanopost-email-check' ) . '</p></div>';
     926                                break;
     927                        case 'stopped':
     928                                echo '<p class="nanopost-email-check-state"><strong>' . esc_html( self::stopped_text( $block['stopped_reason'], $address ) ) . '</strong></p>';
     929                                break;
     930                        default:
     931                                echo '<p>' . esc_html( NanoPost_Email_Check_Canary::open_text() ) . '</p>';
     932                }
     933
     934                foreach ( NanoPost_Email_Check_Canary::problems( $block ) as $problem ) {
     935                        echo '<div class="notice notice-error inline"><p><span aria-hidden="true">&#10005;</span> <strong>' . esc_html( NanoPost_Email_Check_Canary::problem_text( $problem ) ) . '</strong>';
     936                        $reason = 'missing' === $problem['type'] ? NanoPost_Email_Check_Canary::reason_text( $problem['reason'] ) : __( 'Real emails from this site can be refused.', 'nanopost-email-check' );
     937                        if ( 'missing' === $problem['type'] && '' === $reason && 'ok' === $block['sending'] && $problem['sent_at'] > 0 ) {
     938                                $reason = __( 'Email Check sent it, so it was probably lost or refused after that.', 'nanopost-email-check' );
     939                        }
     940                        if ( '' !== $reason ) {
     941                                echo ' ' . esc_html( $reason );
     942                        }
     943                        if ( null !== $block['alerts_to'] && '' !== $address ) {
     944                                /* translators: %s: email address. */
     945                                echo ' ' . esc_html( sprintf( __( 'We emailed %s.', 'nanopost-email-check' ), $address ) );
     946                        }
     947                        if ( '' !== $problem['help_url'] ) {
     948                                echo ' ' . self::help_link( $problem['help_url'], __( 'What to do', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     949                        }
     950                        echo '</p></div>';
     951                }
     952
     953                if ( 'stopped' === $block['watching'] ) {
     954                        self::render_turn_on_form( self::confirmed_address( $block ), __( 'Turn on Email Canary', 'nanopost-email-check' ) );
     955                        self::render_open_link( $block );
     956                        return;
     957                }
     958
     959                echo '<table class="form-table" role="presentation"><tbody>';
     960                $arrival = $block['last_arrival'];
     961                $label   = 'missing' === $block['arrival'] ? __( 'Last arrival', 'nanopost-email-check' ) : __( 'Last test', 'nanopost-email-check' );
     962                echo '<tr><th scope="row">' . esc_html( $label ) . '</th><td>';
     963                if ( null === $arrival || 0 === $arrival['received_at'] ) {
     964                        esc_html_e( 'No test email has arrived yet.', 'nanopost-email-check' );
     965                } else {
     966                        /* translators: %s: date and time. */
     967                        echo esc_html( sprintf( __( 'Arrived %s', 'nanopost-email-check' ), self::format_time( $arrival['received_at'] ) ) ) . '<br />' . NanoPost_Email_Check_Canary::auth_labels( $arrival ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- auth_labels() escapes.
     968                }
     969                echo '</td></tr>';
     970                if ( $block['next_expected_by'] > 0 && 'on' === $block['watching'] ) {
     971                        echo '<tr><th scope="row">' . esc_html__( 'Next test', 'nanopost-email-check' ) . '</th><td>';
     972                        /* translators: %s: date and time. */
     973                        echo esc_html( sprintf( __( 'Expected by %s', 'nanopost-email-check' ), self::format_time( $block['next_expected_by'] ) ) );
     974                        echo '</td></tr>';
     975                }
     976                if ( 'on' === $block['watching'] ) {
     977                        echo '<tr><th scope="row">' . esc_html__( 'Alerts go to', 'nanopost-email-check' ) . '</th><td>';
     978                        $confirmed = array();
     979                        $pending   = array();
     980                        $failing   = array();
     981                        foreach ( $block['alerts_to'] as $entry ) {
     982                                if ( 'pending' === $entry['state'] ) {
     983                                        $pending[] = $entry['email'];
     984                                } elseif ( 'confirmed' === $entry['state'] ) {
     985                                        $confirmed[] = $entry['email'];
     986                                } elseif ( 'failing' === $entry['state'] ) {
     987                                        $confirmed[] = $entry['email'];
     988                                        $failing[]   = $entry['email'];
     989                                } else {
     990                                        /* translators: 1: email address, 2: "Open nanoPost to see." */
     991                                        $confirmed[] = sprintf( __( '%1$s (%2$s)', 'nanopost-email-check' ), $entry['email'], NanoPost_Email_Check_Canary::open_text() );
     992                                }
     993                        }
     994                        echo esc_html( implode( ', ', $confirmed ) );
     995                        foreach ( $failing as $email ) {
     996                                /* translators: %s: email address. */
     997                                echo '<br /><strong class="nanopost-email-check-failing">' . esc_html( sprintf( __( 'Email Canary: alerts do not reach %s.', 'nanopost-email-check' ), $email ) ) . '</strong>';
     998                        }
     999                        if ( array() !== $pending ) {
     1000                                /* translators: %s: email address. */
     1001                                echo '<br /><span class="description">' . esc_html( sprintf( __( 'Waiting for the confirm of %s. Alerts go there once it is confirmed.', 'nanopost-email-check' ), implode( ', ', $pending ) ) ) . '</span>';
     1002                                self::render_confirm_mail( $block, end( $pending ) );
     1003                        }
     1004                        self::render_change_form( $address );
     1005                        echo '</td></tr>';
     1006                }
     1007                echo '<tr><th scope="row">' . esc_html__( 'Webhook', 'nanopost-email-check' ) . '</th><td>' . esc_html( self::webhook_host() ) . '</td></tr>';
     1008                echo '</tbody></table>';
     1009
     1010                if ( NanoPost_Email_Check_Canary::is_old( $block ) && $block['observed_at'] > 0 ) {
     1011                        /* translators: %s: date and time. */
     1012                        echo '<p class="description">' . esc_html( sprintf( __( 'This is the state as of %s. nanoPost has sent no newer one.', 'nanopost-email-check' ), self::format_date( $block['observed_at'] ) ) ) . '</p>';
     1013                }
     1014                self::render_canary_actions( true, $block );
     1015        }
     1016
     1017        /**
     1018         * The text for a canary that stopped watching, by its reason.
     1019         *
     1020         * @param string $reason  stopped_reason.
     1021         * @param string $address Alert address.
     1022         * @return string
     1023         */
     1024        private static function stopped_text( $reason, $address ) {
     1025                switch ( $reason ) {
     1026                        case 'not_confirmed':
     1027                                /* translators: %s: email address. */
     1028                                return sprintf( __( 'Email Canary is off: %s was not confirmed.', 'nanopost-email-check' ), $address );
     1029                        case 'over_limit':
     1030                                return __( 'Email Canary is off: your nanoPost account has reached its limit of sites. Remove one in nanoPost, or ask us for more.', 'nanopost-email-check' );
     1031                        case 'mail_link':
     1032                                return __( 'Email Canary is off (asked for by email).', 'nanopost-email-check' );
     1033                        case 'deactivated':
     1034                                return __( 'Email Canary is off (the plugin was deactivated).', 'nanopost-email-check' );
     1035                        default:
     1036                                return __( 'Email Canary is off. Email Canary keeps the history for 30 days.', 'nanopost-email-check' );
     1037                }
     1038        }
     1039
     1040        /**
     1041         * The offer, when Email Canary is not on: the alert address, the button, and the
     1042         * consent text (nothing is sent before the click).
     1043         */
     1044        private static function render_offer() {
     1045                $email = NanoPost_Email_Check_Connection::alert_email();
     1046                if ( '' === $email ) {
     1047                        $user  = wp_get_current_user();
     1048                        $email = $user instanceof WP_User ? (string) $user->user_email : '';
     1049                }
     1050
     1051                echo '<p>' . esc_html__( 'Email Check can tell you that WordPress sent the email. Email Canary checks that it arrived, and emails you when it does not.', 'nanopost-email-check' ) . '</p>';
     1052                if ( ! self::site_is_public() ) {
     1053                        echo '<div class="notice notice-warning inline"><p>' . esc_html__( 'This site address is not public. Test emails from it may never leave the server.', 'nanopost-email-check' ) . '</p></div>';
     1054                }
     1055                self::render_turn_on_form( $email, __( 'Turn on Email Canary', 'nanopost-email-check' ) );
     1056                echo '<p class="description nanopost-email-check-consent">' . esc_html__( 'When you turn on Email Canary, this site sends to nanoPost: the site address, the alert address above, the site\'s time zone, the test interval, the webhook URL, and the versions of this plugin, WordPress and PHP. After each test it sends the test ID, the time, how the test started, and whether WordPress accepted the email, with the error text if it did not. nanoPost keeps the IP address of each call for 30 days.', 'nanopost-email-check' ) . '</p>';
     1057                echo '<p>' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_SENT, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'What is sent to nanoPost', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1058                echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::TERMS_URL, __( 'Terms', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1059                echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::PRIVACY_URL, __( 'Privacy', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1060                echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_TURN_ON, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'How Email Canary works', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1061                echo '</p>';
     1062        }
     1063
     1064        /**
     1065         * "Send alerts to [address] [Turn on]".
     1066         *
     1067         * @param string $email Pre-filled address.
     1068         * @param string $label Button text.
     1069         */
     1070        private static function render_turn_on_form( $email, $label ) {
     1071                echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" class="nanopost-email-check-turn-on">';
     1072                echo '<input type="hidden" name="action" value="' . esc_attr( self::TURN_ON_ACTION ) . '" />';
     1073                wp_nonce_field( self::TURN_ON_ACTION );
     1074                echo '<p><label for="nanopost_email_check_alert_email">' . esc_html__( 'Send alerts to', 'nanopost-email-check' ) . '</label> ';
     1075                echo '<input type="email" required class="regular-text" id="nanopost_email_check_alert_email" name="alert_email" value="' . esc_attr( $email ) . '" autocomplete="email" /> ';
     1076                submit_button( $label, 'primary', 'submit', false );
     1077                echo '</p></form>';
     1078        }
     1079
     1080        /**
     1081         * Resend of the confirm email, or why it cannot go now.
     1082         *
     1083         * @param array  $block   Block.
     1084         * @param string $address The address waiting for its confirm.
     1085         */
     1086        private static function render_confirm_mail( $block, $address ) {
     1087                $mail = $block['confirm_mail'];
     1088                if ( null !== $mail && 'limited' === $mail['state'] ) {
     1089                        echo '<p>' . esc_html__( 'Email Canary: we cannot send another confirm email now. Try again tomorrow. Proof of arrival still works.', 'nanopost-email-check' ) . '</p>';
     1090                        return;
     1091                }
     1092                echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" class="nanopost-email-check-resend">';
     1093                echo '<input type="hidden" name="action" value="' . esc_attr( self::ALERT_EMAIL_ACTION ) . '" />';
     1094                echo '<input type="hidden" name="alert_email" value="' . esc_attr( $address ) . '" />';
     1095                wp_nonce_field( self::ALERT_EMAIL_ACTION );
     1096                if ( null !== $mail && $mail['next_allowed_at'] > time() ) {
     1097                        echo '<button type="button" class="button" disabled="disabled">' . esc_html__( 'Resend', 'nanopost-email-check' ) . '</button> ';
     1098                        /* translators: %s: date and time. */
     1099                        echo '<span class="description">' . esc_html( sprintf( __( 'You can resend it after %s.', 'nanopost-email-check' ), self::format_date( $mail['next_allowed_at'] ) ) ) . '</span>';
     1100                } else {
     1101                        submit_button( __( 'Resend', 'nanopost-email-check' ), 'secondary', 'submit', false );
     1102                }
     1103                echo '</form>';
     1104                if ( 'unconfirmed' === $block['watching'] ) {
     1105                        self::render_change_form( $address );
     1106                }
     1107        }
     1108
     1109        /**
     1110         * "Change" the alert address: a new confirm email goes to the new address.
     1111         *
     1112         * @param string $address Current address.
     1113         */
     1114        private static function render_change_form( $address ) {
     1115                echo '<details class="nanopost-email-check-change" style="display:inline-block;"><summary>' . esc_html__( 'Change', 'nanopost-email-check' ) . '</summary>';
    7051116                echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '">';
    706                 echo '<input type="hidden" name="action" value="' . esc_attr( self::DISCONNECT_ACTION ) . '" />';
    707                 wp_nonce_field( self::DISCONNECT_ACTION );
    708                 submit_button( __( 'Disconnect', 'nanopost-email-check' ), 'secondary', 'submit', false );
    709                 echo ' <span class="description">' . esc_html__( 'Test emails then go to the address in the settings again. nanoPost stops the check and deletes it after 30 days, unless this site connects again.', 'nanopost-email-check' ) . '</span>';
    710                 echo '</form>';
    711         }
    712 
    713         /**
    714          * A "Connect" button in its own small form.
    715          *
    716          * @param string $label Button text.
    717          */
    718         private static function connect_button( $label ) {
    719                 echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" style="display:inline;">';
    720                 echo '<input type="hidden" name="action" value="' . esc_attr( self::CONNECT_ACTION ) . '" />';
    721                 wp_nonce_field( self::CONNECT_ACTION );
    722                 submit_button( $label, 'primary', 'submit', false );
    723                 echo '</form>';
     1117                echo '<input type="hidden" name="action" value="' . esc_attr( self::ALERT_EMAIL_ACTION ) . '" />';
     1118                wp_nonce_field( self::ALERT_EMAIL_ACTION );
     1119                echo '<p><label for="nanopost_email_check_new_alert_email" class="screen-reader-text">' . esc_html__( 'New alert address', 'nanopost-email-check' ) . '</label>';
     1120                echo '<input type="email" required class="regular-text" id="nanopost_email_check_new_alert_email" name="alert_email" value="' . esc_attr( $address ) . '" autocomplete="email" /> ';
     1121                submit_button( __( 'Send confirm email', 'nanopost-email-check' ), 'secondary', 'submit', false );
     1122                echo '</p><p class="description">' . esc_html__( 'Only where alerts go changes. The new address must confirm by email.', 'nanopost-email-check' ) . ' ';
     1123                echo self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CHANGE, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'More', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1124                echo '</p></form></details>';
     1125        }
     1126
     1127        /**
     1128         * "Turn Email Canary off" and "Open in nanoPost".
     1129         *
     1130         * @param bool       $off   Show the off button.
     1131         * @param array|null $block Block, or null.
     1132         */
     1133        private static function render_canary_actions( $off, $block = null ) {
     1134                echo '<div class="nanopost-email-check-canary-actions">';
     1135                if ( $off ) {
     1136                        echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '">';
     1137                        echo '<input type="hidden" name="action" value="' . esc_attr( self::TURN_OFF_ACTION ) . '" />';
     1138                        wp_nonce_field( self::TURN_OFF_ACTION );
     1139                        submit_button( __( 'Turn Email Canary off', 'nanopost-email-check' ), 'secondary', 'submit', false );
     1140                        echo '</form>';
     1141                }
     1142                self::render_open_link( $block );
     1143                echo '</div>';
     1144        }
     1145
     1146        /**
     1147         * The small "Open in nanoPost" link.
     1148         *
     1149         * @param array|null $block Block, or null.
     1150         */
     1151        private static function render_open_link( $block ) {
     1152                $url = null !== $block && '' !== $block['check_url'] ? $block['check_url'] : NanoPost_Email_Check_Connection::get()['check_url'];
     1153                if ( '' !== $url ) {
     1154                        echo self::help_link( $url, __( 'Open in nanoPost', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1155                }
     1156        }
     1157
     1158        /**
     1159         * The webhook as the owner set it: its host, or "none".
     1160         *
     1161         * @return string
     1162         */
     1163        public static function webhook_host() {
     1164                $url = NanoPost_Email_Check_Settings::webhook_url();
     1165                if ( '' === $url ) {
     1166                        return __( 'none', 'nanopost-email-check' );
     1167                }
     1168                $host = (string) wp_parse_url( $url, PHP_URL_HOST );
     1169                return '' === $host ? $url : $host;
     1170        }
     1171
     1172        /**
     1173         * False for a site address that the internet cannot reach: localhost, a reserved
     1174         * name (.local, .test, .example, .invalid, .localhost), or a private or loopback IP.
     1175         *
     1176         * @return bool
     1177         */
     1178        public static function site_is_public() {
     1179                $host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
     1180                if ( '' === $host || 'localhost' === $host ) {
     1181                        return false;
     1182                }
     1183                if ( preg_match( '/\.(local|test|example|invalid|localhost)$/', $host ) ) {
     1184                        return false;
     1185                }
     1186                $ip = trim( $host, '[]' );
     1187                if ( false !== filter_var( $ip, FILTER_VALIDATE_IP ) ) {
     1188                        return false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
     1189                }
     1190                return true;
     1191        }
     1192
     1193        /**
     1194         * Styles for the SPF, DKIM and DMARC labels, and the script that asks for a newer
     1195         * state when the screen opens (while connected).
     1196         */
     1197        private static function enqueue_canary_assets() {
     1198                wp_register_style( 'nanopost-email-check-canary', false, array(), NANOPOST_EMAIL_CHECK_VERSION );
     1199                wp_enqueue_style( 'nanopost-email-check-canary' );
     1200                wp_add_inline_style( 'nanopost-email-check-canary', NanoPost_Email_Check_Canary::label_css() );
     1201
     1202                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     1203                        return;
     1204                }
     1205                wp_enqueue_script( 'nanopost-email-check-canary', plugins_url( 'assets/canary.js', NANOPOST_EMAIL_CHECK_FILE ), array(), NANOPOST_EMAIL_CHECK_VERSION, true );
     1206                wp_localize_script(
     1207                        'nanopost-email-check-canary',
     1208                        'nanopostEmailCheckCanary',
     1209                        array(
     1210                                'ajaxUrl' => admin_url( 'admin-ajax.php' ),
     1211                                'nonce'   => wp_create_nonce( self::STATE_ACTION ),
     1212                                'action'  => self::STATE_ACTION,
     1213                        )
     1214                );
     1215        }
     1216
     1217        /**
     1218         * What Email Canary said about one test, for its log row.
     1219         *
     1220         * @param NanoPost_Email_Check_Result $result Result.
     1221         * @return string HTML.
     1222         */
     1223        private static function canary_cell( $result ) {
     1224                if ( '' === $result->test_id ) {
     1225                        return '&mdash;';
     1226                }
     1227                if ( ! $result->passed ) {
     1228                        return esc_html__( '(no email to wait for)', 'nanopost-email-check' );
     1229                }
     1230                $canary  = $result->canary;
     1231                $arrival = isset( $canary['arrival'] ) ? (string) $canary['arrival'] : '';
     1232                if ( 'arrived' === $arrival ) {
     1233                        $time = isset( $canary['arrived_at'] ) ? (int) $canary['arrived_at'] : 0;
     1234                        $html = '<strong style="color:#00a32a;">' . esc_html__( 'Arrived', 'nanopost-email-check' ) . '</strong>';
     1235                        if ( $time > 0 ) {
     1236                                $html .= ' ' . esc_html( self::format_date( $time ) );
     1237                        }
     1238                        $results = array();
     1239                        foreach ( array( 'spf', 'dkim', 'dmarc' ) as $key ) {
     1240                                $results[ $key ] = isset( $canary[ $key ] ) ? (string) $canary[ $key ] : '';
     1241                        }
     1242                        return $html . '<br />' . NanoPost_Email_Check_Canary::auth_labels( $results );
     1243                }
     1244                if ( 'missing' === $arrival ) {
     1245                        $html   = '<strong style="color:#d63638;">' . esc_html__( 'Did not arrive', 'nanopost-email-check' ) . '</strong>';
     1246                        $reason = NanoPost_Email_Check_Canary::reason_text( isset( $canary['reason'] ) ? (string) $canary['reason'] : '' );
     1247                        if ( '' !== $reason ) {
     1248                                $html .= '<br />' . esc_html( $reason );
     1249                        }
     1250                        if ( ! empty( $canary['help_url'] ) ) {
     1251                                $html .= ' ' . self::help_link( (string) $canary['help_url'], __( 'What to do', 'nanopost-email-check' ) );
     1252                        }
     1253                        return $html;
     1254                }
     1255                if ( 'unknown' === $arrival ) {
     1256                        return esc_html__( 'Email Canary cannot tell now', 'nanopost-email-check' );
     1257                }
     1258                if ( '' === $arrival || 'waiting' === $arrival ) {
     1259                        return esc_html__( 'No result yet', 'nanopost-email-check' );
     1260                }
     1261                return esc_html( NanoPost_Email_Check_Canary::open_text() );
    7241262        }
    7251263
    … …  
    7321270                $stale  = NanoPost_Email_Check_Alerts::stale_since();
    7331271
    734                 echo '<h2>' . esc_html__( 'Status', 'nanopost-email-check' ) . '</h2>';
     1272                echo '<h2>' . esc_html__( 'Email Check', 'nanopost-email-check' ) . ' <span class="nanopost-email-check-tagline">' . esc_html__( 'Can this site send email?', 'nanopost-email-check' ) . '</span></h2>';
    7351273                echo '<table class="form-table" role="presentation"><tbody>';
    7361274
    737                 echo '<tr><th scope="row">' . esc_html__( 'Last check', 'nanopost-email-check' ) . '</th><td>';
     1275                echo '<tr><th scope="row">' . esc_html__( 'Last test', 'nanopost-email-check' ) . '</th><td>';
    7381276                if ( null === $latest ) {
    739                         esc_html_e( 'No check has run yet. Use "Send test now" below.', 'nanopost-email-check' );
     1277                        esc_html_e( 'No test has run yet. Use "Send test now" below.', 'nanopost-email-check' );
    7401278                } else {
    7411279                        printf(
    … …  
    7551293                $mailer = null === $latest ? '' : NanoPost_Email_Check_Mailer::describe( $latest->mailer );
    7561294                if ( '' !== $mailer ) {
    757                         echo '<tr><th scope="row">' . esc_html__( 'Mailer', 'nanopost-email-check' ) . '</th><td>' . esc_html( $mailer ) . '</td></tr>';
    758                 }
    759 
    760                 echo '<tr><th scope="row">' . esc_html__( 'Scheduled checks', 'nanopost-email-check' ) . '</th><td>';
     1295                        echo '<tr><th scope="row">' . esc_html__( 'Sends by', 'nanopost-email-check' ) . '</th><td>' . esc_html( $mailer ) . '</td></tr>';
     1296                }
     1297
     1298                echo '<tr><th scope="row">' . esc_html__( 'Schedule', 'nanopost-email-check' ) . '</th><td>';
    7611299                if ( null !== $stale ) {
    7621300                        echo '<strong style="color:#d63638;">' . esc_html__( 'Stopped', 'nanopost-email-check' ) . '</strong> &middot; ';
    … …  
    7761314                                sprintf(
    7771315                                        /* translators: 1: schedule name such as "Daily", 2: date and time. */
    778                                         __( '%1$s. Next check: %2$s', 'nanopost-email-check' ),
     1316                                        __( '%1$s. Next test: %2$s', 'nanopost-email-check' ),
    7791317                                        NanoPost_Email_Check_Settings::intervals()[ NanoPost_Email_Check_Settings::interval() ],
    7801318                                        self::format_time( $next )
    … …  
    7861324                echo '</td></tr>';
    7871325
     1326                // Each area repeats only what the owner set: where test emails go, and the webhook.
     1327                echo '<tr><th scope="row">' . esc_html__( 'Test emails go to', 'nanopost-email-check' ) . '</th><td>';
     1328                echo esc_html( NanoPost_Email_Check_Connection::canary_active() ? __( 'Email Canary', 'nanopost-email-check' ) : implode( ', ', NanoPost_Email_Check_Settings::recipients() ) );
     1329                echo '</td></tr>';
     1330                echo '<tr><th scope="row">' . esc_html__( 'Webhook', 'nanopost-email-check' ) . '</th><td>' . esc_html( self::webhook_host() ) . '</td></tr>';
     1331
    7881332                echo '</tbody></table>';
    7891333        }
    … …  
    7931337         */
    7941338        private static function render_send_form() {
    795                 echo '<h2>' . esc_html__( 'Send test now', 'nanopost-email-check' ) . '</h2>';
    796                 echo '<p>' . esc_html(
    797                         NanoPost_Email_Check_Connection::is_connected()
    798                                 ? __( 'Send one test email to nanoPost now, and see whether it arrives. Save any changed settings first.', 'nanopost-email-check' )
     1339                echo '<p class="description">' . esc_html(
     1340                        NanoPost_Email_Check_Connection::canary_active()
     1341                                ? __( 'Send one test email to Email Canary now, and see whether it arrives. Save any changed settings first.', 'nanopost-email-check' )
    7991342                                : sprintf(
    8001343                                        /* translators: %s: recipient email address. */
    … …  
    8161359                $results = NanoPost_Email_Check_Log::all();
    8171360
    818                 echo '<h2>' . esc_html__( 'Recent results', 'nanopost-email-check' ) . '</h2>';
     1361                echo '<h2>' . esc_html__( 'Recent tests', 'nanopost-email-check' ) . '</h2>';
    8191362
    8201363                if ( empty( $results ) ) {
    … …  
    8251368                echo '<table class="widefat striped"><thead><tr>';
    8261369                echo '<th scope="col">' . esc_html__( 'Time', 'nanopost-email-check' ) . '</th>';
    827                 echo '<th scope="col">' . esc_html__( 'Result', 'nanopost-email-check' ) . '</th>';
     1370                echo '<th scope="col">' . esc_html__( 'Email Check', 'nanopost-email-check' ) . '</th>';
     1371                $canary = array() !== array_filter( wp_list_pluck( $results, 'test_id' ) ) || NanoPost_Email_Check_Connection::is_connected();
     1372                if ( $canary ) {
     1373                        echo '<th scope="col">' . esc_html__( 'Email Canary', 'nanopost-email-check' ) . '</th>';
     1374                }
    8281375                echo '<th scope="col">' . esc_html__( 'Started by', 'nanopost-email-check' ) . '</th>';
    8291376                echo '<th scope="col">' . esc_html__( 'Sent to', 'nanopost-email-check' ) . '</th>';
    … …  
    8441391                        }
    8451392                        echo '</td>';
     1393                        if ( $canary ) {
     1394                                echo '<td class="nanopost-email-check-canary-cell">' . self::canary_cell( $result ) . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- canary_cell() escapes.
     1395                        }
    8461396                        echo '<td>' . esc_html( $result->trigger_label() ) . '</td>';
    847                         echo '<td>' . esc_html( $result->recipient ) . '</td>';
     1397                        echo '<td>' . esc_html( self::recipient_label( $result ) ) . '</td>';
    8481398                        echo '<td>';
    8491399                        if ( $result->passed ) {
    … …  
    8611411
    8621412        /**
     1413         * Where a test went, as the owner sees it: "Email Canary" for a test to the canary
     1414         * address (never the address itself), else the recipient.
     1415         *
     1416         * @param NanoPost_Email_Check_Result $result Result.
     1417         * @return string
     1418         */
     1419        public static function recipient_label( $result ) {
     1420                return '' !== $result->test_id ? __( 'Email Canary', 'nanopost-email-check' ) : $result->recipient;
     1421        }
     1422
     1423        /**
    8631424         * Pass or fail label.
    8641425         *
    … …  
    8681429        private static function status_badge( $passed ) {
    8691430                return $passed
    870                         ? '<strong style="color:#00a32a;">' . esc_html__( 'Passed', 'nanopost-email-check' ) . '</strong>'
    871                         : '<strong style="color:#d63638;">' . esc_html__( 'Failed', 'nanopost-email-check' ) . '</strong>';
     1431                        ? '<strong style="color:#00a32a;">' . esc_html__( 'Sent', 'nanopost-email-check' ) . '</strong>'
     1432                        : '<strong style="color:#d63638;">' . esc_html__( 'Could not send', 'nanopost-email-check' ) . '</strong>';
    8721433        }
    8731434
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-alerts.php

    r3820170 r3830659  
    111111         * Active alerts, keyed by a string that changes with each new problem.
    112112         *
    113          * @return array Key => type ("failed" or "stale").
     113         * @return array Key => type ("failed", "stale", or a problem of Email Canary:
     114         *               "canary-missing", "canary-auth").
    114115         */
    115116        public static function active() {
    … …  
    126127                }
    127128
     129                foreach ( self::canary_problems() as $problem ) {
     130                        $alerts[ $problem['key'] ] = 'canary-' . $problem['type'];
     131                }
     132
     133                $unconfirmed = self::unconfirmed();
     134                if ( null !== $unconfirmed ) {
     135                        $alerts[ 'canary-unconfirmed:' . $unconfirmed['confirm_by'] ] = 'canary-unconfirmed';
     136                }
     137
     138                foreach ( self::failing() as $email ) {
     139                        $alerts[ 'canary-failing:' . $email ] = 'canary-failing';
     140                }
     141
    128142                return $alerts;
     143        }
     144
     145        /**
     146         * The block while alerts are off until the owner confirms the alert address, or null.
     147         *
     148         * @return array|null
     149         */
     150        private static function unconfirmed() {
     151                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     152                        return null;
     153                }
     154                $block = NanoPost_Email_Check_Canary::block();
     155                return null !== $block && 'unconfirmed' === $block['watching'] ? $block : null;
     156        }
     157
     158        /**
     159         * Alert addresses that Email Canary's alerts do not reach (they bounce). The email
     160         * cannot tell the owner, so wp-admin does.
     161         *
     162         * @return string[]
     163         */
     164        private static function failing() {
     165                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     166                        return array();
     167                }
     168                $block = NanoPost_Email_Check_Canary::block();
     169                if ( null === $block ) {
     170                        return array();
     171                }
     172                $failing = array();
     173                foreach ( $block['alerts_to'] as $entry ) {
     174                        if ( 'failing' === $entry['state'] ) {
     175                                $failing[] = $entry['email'];
     176                        }
     177                }
     178                return $failing;
     179        }
     180
     181        /**
     182         * Problems that Email Canary emails about, from the stored state. None when the site is
     183         * not connected.
     184         *
     185         * @return array[]
     186         */
     187        private static function canary_problems() {
     188                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     189                        return array();
     190                }
     191                return NanoPost_Email_Check_Canary::problems( NanoPost_Email_Check_Canary::block() );
    129192        }
    130193
    … …  
    152215                        if ( 'failed' === $type ) {
    153216                                self::render_failed( $key );
     217                        } elseif ( 'stale' === $type ) {
     218                                self::render_stale( $key );
     219                        } elseif ( 'canary-unconfirmed' === $type ) {
     220                                self::render_unconfirmed( $key );
     221                        } elseif ( 'canary-failing' === $type ) {
     222                                self::render_failing( $key );
    154223                        } else {
    155                                 self::render_stale( $key );
     224                                self::render_canary( $key );
    156225                        }
    157226                }
    … …  
    167236
    168237                echo '<div id="nanopost-email-check-notice-failed" class="notice notice-error"><p>';
    169                 echo '<strong>' . esc_html__( 'Email check failed.', 'nanopost-email-check' ) . '</strong> ';
     238                echo '<strong>' . esc_html__( 'Email Check: this site could not send its test email.', 'nanopost-email-check' ) . '</strong> ';
    170239                echo esc_html(
    171240                        sprintf(
    … …  
    173242                                __( 'On %1$s, WordPress could not send the test email to %2$s. Your site may not be able to send email.', 'nanopost-email-check' ),
    174243                                NanoPost_Email_Check_Admin::format_date( $result->time ),
    175                                 $result->recipient
     244                                NanoPost_Email_Check_Admin::recipient_label( $result )
    176245                        )
    177246                );
    … …  
    188257
    189258        /**
     259         * Notice for a problem that Email Canary found, with the same core sentence as its
     260         * email.
     261         *
     262         * @param string $key Alert key.
     263         */
     264        private static function render_canary( $key ) {
     265                foreach ( self::canary_problems() as $problem ) {
     266                        if ( $key !== $problem['key'] ) {
     267                                continue;
     268                        }
     269                        echo '<div class="notice notice-error nanopost-email-check-notice-canary"><p>';
     270                        echo '<strong>' . esc_html( NanoPost_Email_Check_Canary::problem_text( $problem ) ) . '</strong>';
     271                        $reason = 'missing' === $problem['type'] ? NanoPost_Email_Check_Canary::reason_text( $problem['reason'] ) : __( 'Real emails from this site can be refused.', 'nanopost-email-check' );
     272                        if ( '' !== $reason ) {
     273                                echo ' ' . esc_html( $reason );
     274                        }
     275                        echo '</p>';
     276                        self::render_links( $key, '' === $problem['help_url'] ? array() : array( $problem['help_url'] => __( 'What to do', 'nanopost-email-check' ) ) );
     277                        echo '</div>';
     278                }
     279        }
     280
     281        /**
     282         * Notice for an alert address that Email Canary's alerts do not reach.
     283         *
     284         * @param string $key Alert key.
     285         */
     286        private static function render_failing( $key ) {
     287                $email = substr( $key, strlen( 'canary-failing:' ) );
     288                echo '<div class="notice notice-error nanopost-email-check-notice-failing"><p><strong>';
     289                /* translators: %s: email address. */
     290                echo esc_html( sprintf( __( 'Email Canary: alerts do not reach %s.', 'nanopost-email-check' ), $email ) ) . '</strong> ';
     291                echo esc_html__( 'Change the alert address on the plugin screen.', 'nanopost-email-check' );
     292                echo '</p>';
     293                self::render_links( $key, array( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CHANGE, NanoPost_Email_Check_Help::PLACE_NOTICE_CANARY ) => __( 'How to change it', 'nanopost-email-check' ) ) );
     294                echo '</div>';
     295        }
     296
     297        /**
     298         * Notice that stays until the confirm: alerts are off.
     299         *
     300         * @param string $key Alert key.
     301         */
     302        private static function render_unconfirmed( $key ) {
     303                $block = self::unconfirmed();
     304                echo '<div class="notice notice-warning nanopost-email-check-notice-unconfirmed"><p><strong>';
     305                echo esc_html__( 'Email Canary: alerts are OFF until you confirm.', 'nanopost-email-check' ) . '</strong> ';
     306                /* translators: %s: email address. */
     307                echo esc_html( sprintf( __( 'We sent an email to %s.', 'nanopost-email-check' ), NanoPost_Email_Check_Admin::alert_address( $block ) ) );
     308                echo '</p>';
     309                self::render_links( $key, array( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CONFIRM, NanoPost_Email_Check_Help::PLACE_NOTICE_CANARY ) => __( 'Why confirm', 'nanopost-email-check' ) ) );
     310                echo '</div>';
     311        }
     312
     313        /**
    190314         * Notice for stopped scheduled checks.
    191315         *
    … …  
    194318        private static function render_stale( $key ) {
    195319                echo '<div id="nanopost-email-check-notice-stale" class="notice notice-warning"><p>';
    196                 echo '<strong>' . esc_html__( 'Email checks have stopped.', 'nanopost-email-check' ) . '</strong> ';
     320                echo '<strong>' . esc_html__( 'Email Check: scheduled tests have stopped.', 'nanopost-email-check' ) . '</strong> ';
    197321                echo esc_html(
    198322                        sprintf(
    199323                                /* translators: %s: date and time. */
    200                                 __( 'No scheduled email check has finished since %s. WP-Cron may not be running on this site, or the check may be stopping with a PHP error.', 'nanopost-email-check' ),
     324                                __( 'No scheduled test has finished since %s. WP-Cron may not be running on this site, or the test may be stopping with a PHP error.', 'nanopost-email-check' ),
    201325                                NanoPost_Email_Check_Admin::format_date( (int) self::stale_since() )
    202326                        )
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-app.php

    r3829352 r3830659  
    1010/**
    1111 * The one place that talks to the app (https://app.nanopo.st/api/v1). Every call needs a
    12  * connection the owner made, except the code exchange that makes it. A failed call never
     12 * connection the owner made, or the token of "Turn on Email Canary". A failed call never
    1313 * fails a test and never blocks a page: the timeouts are short, and a call runs only in
    1414 * WP-Cron, WP-CLI, or a request the owner started with a click.
    … …  
    2424
    2525        /**
     26         * Single WP-Cron event after an activation: "start" with trigger "activate".
     27         */
     28        const START_HOOK = 'nanopost_email_check_start';
     29
     30        /**
     31         * A deactivation stops the canary on each site until this many seconds have passed,
     32         * so a large network does not time out.
     33         */
     34        const DEACTIVATE_BUDGET = 20;
     35
     36        /**
    2637         * Recent failed calls, for support. Not autoloaded.
    2738         */
    … …  
    4253        public static function init() {
    4354                add_action( self::SYNC_HOOK, array( __CLASS__, 'sync_site' ) );
     55                add_action( self::START_HOOK, array( __CLASS__, 'start_after_activate' ) );
    4456                add_action( 'update_option_' . NanoPost_Email_Check_Settings::OPTION, array( __CLASS__, 'queue_sync' ), 10, 0 );
    4557                add_action( 'add_option_' . NanoPost_Email_Check_Settings::OPTION, array( __CLASS__, 'queue_sync' ), 10, 0 );
    … …  
    6072
    6173        /**
    62          * The page in the browser where the owner approves the connection.
    63          *
    64          * @param string $return_url Where the app sends the browser back to.
    65          * @param string $state      The plugin's random state.
    66          * @return string
    67          */
    68         public static function connect_url( $return_url, $state ) {
    69                 $base = preg_replace( '#/api/v\d+$#', '', self::base_url() );
    70                 return add_query_arg(
    71                         array(
    72                                 'site'   => rawurlencode( home_url() ),
    73                                 'return' => rawurlencode( $return_url ),
    74                                 'state'  => rawurlencode( $state ),
    75                         ),
    76                         $base . '/connect'
    77                 );
    78         }
    79 
    80         /**
    81          * Exchange the one-time code for a token, and store the connection.
    82          *
    83          * @param string $code The code from the app's redirect.
    84          * @return array Call result (see request()).
    85          */
    86         public static function exchange( $code ) {
    87                 $result = self::request(
    88                         'POST',
    89                         '/connect/exchange',
    90                         array(
    91                                 'code'     => (string) $code,
    92                                 'site_url' => home_url(),
    93                         ),
    94                         self::TIMEOUT_LONG,
    95                         false
    96                 );
    97                 if ( $result['ok'] && ! NanoPost_Email_Check_Connection::save_from_exchange( $result['body'] ) ) {
     74         * "Turn on Email Canary": make the canary with the token the plugin made, and store the
     75         * connection. The owner's click (admin-post) or WP-CLI.
     76         *
     77         * @param string $email Alert address.
     78         * @return array Call result (see request()). error "bad_answer" when the answer lacks
     79         *               a valid canary address; "connected" when a connection already exists.
     80         */
     81        public static function connect( $email ) {
     82                $token = NanoPost_Email_Check_Connection::begin( $email );
     83                if ( '' === $token ) {
     84                        return self::not_sent( 'connected' );
     85                }
     86                $body = self::site_body();
     87                $hash = md5( (string) wp_json_encode( $body ) );
     88
     89                $body['alert_email'] = (string) $email;
     90                $result              = self::request( 'POST', '/connect', $body, self::TIMEOUT_LONG, true, false, $token );
     91
     92                if ( $result['ok'] && ! NanoPost_Email_Check_Connection::save_from_connect( $result['body'], $hash ) ) {
    9893                        $result['ok']    = false;
    9994                        $result['error'] = 'bad_answer';
    100                         self::log( 'POST', '/connect/exchange', $result );
     95                        self::log( 'POST', '/connect', $result );
    10196                }
    10297                if ( $result['ok'] ) {
     98                        NanoPost_Email_Check_Canary::forget();
     99                        NanoPost_Email_Check_Canary::take( $result['body'] );
    103100                        NanoPost_Email_Check_Webhook::reset_check( 'wp_mail' );
     101                }
     102                return $result;
     103        }
     104
     105        /**
     106         * "Resend" (the same address) or "Change" (another address) of the alert address.
     107         *
     108         * @param string $email Address.
     109         * @return array Call result.
     110         */
     111        public static function set_alert_email( $email ) {
     112                $result = self::request( 'PUT', '/alert-email', array( 'email' => (string) $email ), self::TIMEOUT_SHORT, true, true );
     113                if ( $result['ok'] ) {
     114                        NanoPost_Email_Check_Canary::take( $result['body'] );
     115                        NanoPost_Email_Check_Connection::set_alert_email( $email );
     116                }
     117                return $result;
     118        }
     119
     120        /**
     121         * Stop watching: "Turn Email Canary off" (turn_off) or a deactivation (deactivate). The
     122         * token is kept, so "Turn on" needs no new confirm.
     123         *
     124         * @param string $trigger turn_off or deactivate.
     125         * @param int    $timeout Timeout in seconds.
     126         * @return array Call result.
     127         */
     128        public static function stop( $trigger, $timeout = self::TIMEOUT_SHORT ) {
     129                $result = self::request( 'POST', '/stop', array( 'trigger' => (string) $trigger ), $timeout, true, true );
     130                if ( $result['ok'] ) {
     131                        NanoPost_Email_Check_Canary::take( $result['body'] );
     132                }
     133                return $result;
     134        }
     135
     136        /**
     137         * Watch again: "Turn on" with the same address (turn_on) or an activation (activate).
     138         *
     139         * @param string $trigger turn_on or activate.
     140         * @return array Call result.
     141         */
     142        public static function start( $trigger ) {
     143                $result = self::request( 'POST', '/start', array( 'trigger' => (string) $trigger ), self::TIMEOUT_SHORT, true, true );
     144                if ( $result['ok'] ) {
     145                        NanoPost_Email_Check_Canary::take( $result['body'] );
    104146                        self::queue_sync();
    105147                }
    106148                return $result;
     149        }
     150
     151        /**
     152         * Deactivation: tell nanoPost to stop watching (best effort), so it sends no "missing"
     153         * alert for a plugin that was turned off on purpose. The token stays, so an activation
     154         * can start it again. The stop link in each alert email is the second way.
     155         *
     156         * @param int $deadline Unix time after which no more calls go out (network loops).
     157         */
     158        public static function stop_on_deactivate( $deadline ) {
     159                if ( time() >= $deadline || ! NanoPost_Email_Check_Connection::canary_active() ) {
     160                        return;
     161                }
     162                $block = NanoPost_Email_Check_Canary::block();
     163                if ( null !== $block && 'unconfirmed' === $block['watching'] ) {
     164                        return;
     165                }
     166                self::stop( 'deactivate', min( self::TIMEOUT_SHORT, max( 1, $deadline - time() ) ) );
     167        }
     168
     169        /**
     170         * Activation: book the "start" call in WP-Cron, not in the activation request.
     171         */
     172        public static function queue_start() {
     173                if ( NanoPost_Email_Check_Connection::is_connected() && false === wp_next_scheduled( self::START_HOOK ) ) {
     174                        wp_schedule_single_event( time(), self::START_HOOK );
     175                }
     176        }
     177
     178        /**
     179         * WP-Cron after an activation. nanoPost starts watching again only when the stop came
     180         * from a deactivation; for any other stop it answers 409, and the canary stays off.
     181         */
     182        public static function start_after_activate() {
     183                if ( NanoPost_Email_Check_Connection::is_connected() ) {
     184                        self::start( 'activate' );
     185                }
    107186        }
    108187
    … …  
    115194         */
    116195        public static function disconnect( $timeout = self::TIMEOUT_SHORT ) {
    117                 $result = NanoPost_Email_Check_Connection::is_connected()
     196                $token  = NanoPost_Email_Check_Connection::get()['token'];
     197                $result = '' !== $token && NanoPost_Email_Check_Connection::is_connected()
    118198                        ? self::request( 'DELETE', '/connection', null, $timeout, true, true )
    119199                        : self::not_sent( 'not_connected' );
    … …  
    160240
    161241        /**
     242         * Ask the app for the state of the canary ("GET /check"). For the plugin screen only,
     243         * through admin-ajax.
     244         *
     245         * @return array Call result.
     246         */
     247        public static function check_state() {
     248                return self::request( 'GET', '/check', null, self::TIMEOUT_SHORT );
     249        }
     250
     251        /**
    162252         * The body of the site settings call.
    163253         *
    … …  
    173263                        'interval'       => NanoPost_Email_Check_Settings::interval(),
    174264                        'webhook_url'    => '' === $webhook ? null : $webhook,
     265                        'timezone'       => wp_timezone_string(),
    175266                );
    176267        }
    … …  
    183274         */
    184275        public static function sync_site() {
    185                 if ( ! NanoPost_Email_Check_Connection::calls_allowed() ) {
     276                if ( ! NanoPost_Email_Check_Connection::calls_allowed() || ! NanoPost_Email_Check_Connection::canary_active() ) {
    186277                        return null;
    187278                }
    … …  
    227318         * @param int        $timeout Timeout in seconds.
    228319         * @param bool       $auth    Send the token. Calls with the token need a connection.
    229          * @param bool       $always  Make the call also while calls are stopped (disconnect).
     320         * @param bool       $always  Make the call also while Email Canary is off (the calls
     321         *                            that start, stop or end it, and the alert address).
     322         * @param string     $token   A token for a site with no connection yet (POST /connect).
    230323         * @return array ok (bool), code (HTTP status, 0 when no answer), error (stable code),
    231          *               message (the app's text, may be shown), body (decoded JSON array).
    232          */
    233         public static function request( $method, $path, $body = null, $timeout = self::TIMEOUT_SHORT, $auth = true, $always = false ) {
     324         *               message (the app's text, may be shown), body (decoded JSON array),
     325         *               date (the Date header of the answer, or '').
     326         */
     327        public static function request( $method, $path, $body = null, $timeout = self::TIMEOUT_SHORT, $auth = true, $always = false, $token = '' ) {
    234328                $headers = array(
    235329                        'Accept'           => 'application/json',
    … …  
    237331                );
    238332
    239                 if ( $auth ) {
     333                if ( $auth && '' !== $token ) {
     334                        $connection = NanoPost_Email_Check_Connection::get();
     335                        if ( '' !== NanoPost_Email_Check_Connection::too_old_message() ) {
     336                                return self::not_sent( 'too_old' );
     337                        }
     338                        if ( $connection['retry_until'] > time() ) {
     339                                return self::not_sent( 'waiting' );
     340                        }
     341                        $headers['Authorization'] = 'Bearer ' . $token;
     342                } elseif ( $auth ) {
    240343                        if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
    241344                                return self::not_sent( 'not_connected' );
    242345                        }
    243                         if ( ! $always && ! NanoPost_Email_Check_Connection::calls_allowed() ) {
    244                                 return self::not_sent( 'stopped' );
     346                        // The disconnect always goes out: it ends the connection here too.
     347                        $ending = 'DELETE' === $method;
     348                        if ( ! $ending && '' !== NanoPost_Email_Check_Connection::too_old_message() ) {
     349                                return self::not_sent( 'too_old' );
     350                        }
     351                        if ( ! $ending && NanoPost_Email_Check_Connection::get()['retry_until'] > time() ) {
     352                                return self::not_sent( 'waiting' );
     353                        }
     354                        if ( ! $always && ! NanoPost_Email_Check_Connection::canary_active() && 'GET /check' !== $method . ' ' . $path ) {
     355                                return self::not_sent( 'canary_off' );
    245356                        }
    246357                        $headers['Authorization'] = 'Bearer ' . NanoPost_Email_Check_Connection::get()['token'];
    … …  
    268379                                'message' => $response->get_error_message(),
    269380                                'body'    => array(),
     381                                'date'    => '',
    270382                        );
    271383                        self::log( $method, $path, $result );
    … …  
    284396                        'message' => ! $ok && isset( $decoded['message'] ) && is_string( $decoded['message'] ) ? $decoded['message'] : '',
    285397                        'body'    => $decoded,
     398                        'date'    => (string) wp_remote_retrieve_header( $response, 'date' ),
    286399                );
    287400
    … …  
    292405                if ( 401 === $code && $auth ) {
    293406                        NanoPost_Email_Check_Connection::end( NanoPost_Email_Check_Connection::ENDED_REVOKED );
     407                } elseif ( 409 === $code && 'canary_stopped' === $result['error'] ) {
     408                        // Email Canary stopped watching (for example "Stop watching" in an alert email).
     409                        // The block says why; the next tests go to the recipient setting.
     410                        NanoPost_Email_Check_Canary::take( $decoded );
    294411                } elseif ( 410 === $code ) {
    295412                        NanoPost_Email_Check_Connection::mark_too_old( $result['message'] );
    … …  
    316433                        'message' => '',
    317434                        'body'    => array(),
     435                        'date'    => '',
    318436                );
    319437        }
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-cli.php

    r3829352 r3830659  
    6262         */
    6363        public function run() {
    64                 if ( NanoPost_Email_Check_Connection::is_connected() ) {
    65                         WP_CLI::log( __( 'Sending a test email to nanoPost ...', 'nanopost-email-check' ) );
     64                if ( NanoPost_Email_Check_Connection::canary_active() ) {
     65                        WP_CLI::log( __( 'Sending a test email to Email Canary ...', 'nanopost-email-check' ) );
    6666                } else {
    6767                        /* translators: %s: recipient email address. */
    … …  
    8585                        WP_CLI::success(
    8686                                sprintf(
    87                                         /* translators: 1: test ID, 2: URL of the check in nanoPost. */
    88                                         __( 'WordPress accepted the test email for nanoPost (test %1$s). See whether it arrived: %2$s', 'nanopost-email-check' ),
     87                                        /* translators: 1: test ID, 2: URL of the site's page in nanoPost. */
     88                                        __( 'WordPress accepted the test email for Email Canary (test %1$s). Its arrival shows on the plugin screen, and in nanoPost: %2$s', 'nanopost-email-check' ),
    8989                                        $results[0]->test_id,
    90                                         '' !== $check_url ? $check_url : __( 'the check page in nanoPost', 'nanopost-email-check' )
     90                                        '' !== $check_url ? $check_url : __( 'the site\'s page in nanoPost', 'nanopost-email-check' )
    9191                                )
    9292                        );
    9393                        if ( NanoPost_Email_Check_Result::ANNOUNCE_OK !== $results[0]->announce ) {
    94                                 WP_CLI::warning( __( 'The plugin could not tell nanoPost about this test. nanoPost still records the email if it arrives.', 'nanopost-email-check' ) );
     94                                WP_CLI::warning( __( 'The plugin could not tell Email Canary about this test. Email Canary still records the email if it arrives.', 'nanopost-email-check' ) );
    9595                        }
    9696                        return;
    … …  
    302302                }
    303303
     304                $block = $connected ? NanoPost_Email_Check_Canary::block() : null;
     305
    304306                return array(
    305307                        'connected'        => $connected,
     308                        'watching'         => null === $block ? null : $block['watching'],
     309                        'stopped_reason'   => null === $block || '' === $block['stopped_reason'] ? null : $block['stopped_reason'],
     310                        'arrival'          => null === $block ? null : $block['arrival'],
     311                        'last_arrival_at'  => null === $block || null === $block['last_arrival'] || 0 === $block['last_arrival']['received_at'] ? null : self::iso_time( $block['last_arrival']['received_at'] ),
     312                        'alerts_to'        => null === $block ? array() : $block['alerts_to'],
    306313                        'address'          => $connected ? $conn['address'] : null,
    307314                        'check_url'        => $connected && '' !== $conn['check_url'] ? $conn['check_url'] : null,
    … …  
    504511                $np = $data['nanopost'];
    505512                if ( ! $np['connected'] ) {
    506                         $text = __( 'Not connected. Connect on the plugin screen (Tools > Email Check).', 'nanopost-email-check' );
     513                        $text = __( 'Off. Turn it on on the plugin screen (Tools > Email Check).', 'nanopost-email-check' );
    507514                        if ( null !== $np['ended'] ) {
    508515                                $text .= ' ' . ( NanoPost_Email_Check_Connection::ENDED_SITE_CHANGED === $np['ended']
    509                                         ? __( 'The last connection ended because the site address changed.', 'nanopost-email-check' )
    510                                         : __( 'The last connection was ended in nanoPost.', 'nanopost-email-check' ) );
    511                         }
    512                 } else {
    513                         /* translators: %s: the check's email address in nanoPost. */
    514                         $text = sprintf( __( 'Connected. Test emails go to %s.', 'nanopost-email-check' ), $np['address'] );
     516                                        ? __( 'It turned off because the site address changed.', 'nanopost-email-check' )
     517                                        : __( 'nanoPost ended it for this site.', 'nanopost-email-check' ) );
     518                        }
     519                } else {
     520                        $states = array(
     521                                'on'          => __( 'On.', 'nanopost-email-check' ),
     522                                'unconfirmed' => __( 'Alerts are OFF until you confirm the alert address.', 'nanopost-email-check' ),
     523                                'over_limit'  => __( 'Alerts are OFF: the nanoPost account has reached its limit of sites.', 'nanopost-email-check' ),
     524                                'stopped'     => __( 'Off.', 'nanopost-email-check' ),
     525                        );
     526                        $text   = null === $np['watching'] ? __( 'On. No state from nanoPost yet.', 'nanopost-email-check' ) : ( isset( $states[ $np['watching'] ] ) ? $states[ $np['watching'] ] : NanoPost_Email_Check_Canary::open_text() );
     527                        if ( array() !== $np['alerts_to'] ) {
     528                                /* translators: %s: email addresses. */
     529                                $text .= ' ' . sprintf( __( 'Alerts go to %s.', 'nanopost-email-check' ), implode( ', ', wp_list_pluck( $np['alerts_to'], 'email' ) ) );
     530                        }
     531                        if ( null !== $np['last_arrival_at'] ) {
     532                                /* translators: %s: date and time. */
     533                                $text .= ' ' . sprintf( __( 'Last test email arrived %s.', 'nanopost-email-check' ), NanoPost_Email_Check_Admin::format_time( self::unix_time( $np['last_arrival_at'] ) ) );
     534                        }
    515535                        if ( 'stopped_too_old' === $np['calls'] ) {
    516536                                $text .= ' ' . $np['message'];
    … …  
    520540                        }
    521541                }
    522                 $add( __( 'nanoPost', 'nanopost-email-check' ), $text );
     542                $add( __( 'Email Canary', 'nanopost-email-check' ), $text );
    523543                if ( null !== $np['check_url'] ) {
    524                         $add( __( 'Check in nanoPost', 'nanopost-email-check' ), $np['check_url'] );
     544                        $add( __( 'Open in nanoPost', 'nanopost-email-check' ), $np['check_url'] );
    525545                }
    526546
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-connection.php

    r3829352 r3830659  
    5050                        'retry_until'  => 0,
    5151                        'site_hash'    => '',
     52                        'alert_email'  => '',
     53                        'pending_at'   => 0,
    5254                );
    5355
    … …  
    6163
    6264        /**
    63          * Store the connection from the answer to "Exchange the code".
    64          *
    65          * @param array $answer Decoded answer: token, check_id, address, check_url.
    66          * @return bool False when the answer lacks a token or a valid address.
    67          */
    68         public static function save_from_exchange( $answer ) {
     65         * Start "Turn on Email Canary": the token the plugin makes for the connect call. It is
     66         * stored before the call, so a retry sends the same token and the app does not make a
     67         * second canary. A pending token for the same address is used again; another address
     68         * gets a new token. Two requests at the same time get the same token (add_option()).
     69         *
     70         * @param string $email Alert address.
     71         * @return string The token, or '' while a connection with an address exists (except
     72         *                for a canary that was not confirmed, which connects again).
     73         */
     74        public static function begin( $email ) {
     75                $current = self::get();
     76                if ( '' !== $current['address'] && '' !== $current['token'] ) {
     77                        // A canary that was not confirmed in 24 hours: connect again with its token.
     78                        $block = NanoPost_Email_Check_Canary::block();
     79                        return null !== $block && 'not_confirmed' === $block['stopped_reason'] && $email === $current['alert_email'] ? $current['token'] : '';
     80                }
     81                if ( '' !== $current['token'] && $email === $current['alert_email'] ) {
     82                        return $current['token'];
     83                }
     84                $pending = array(
     85                        'token'       => self::new_token(),
     86                        'alert_email' => (string) $email,
     87                        'pending_at'  => time(),
     88                        'home_url'    => home_url(),
     89                );
     90                if ( '' === $current['token'] && ! add_option( self::OPTION, $pending, '', false ) ) {
     91                        // Another request stored a token a moment ago: use it when it is for this address.
     92                        wp_cache_delete( self::OPTION, 'options' );
     93                        $current = self::get();
     94                        if ( $email === $current['alert_email'] && '' !== $current['token'] ) {
     95                                return $current['token'];
     96                        }
     97                }
     98                update_option( self::OPTION, $pending, false );
     99                return $pending['token'];
     100        }
     101
     102        /**
     103         * A new token: 32 random bytes, base64url without padding (43 characters).
     104         *
     105         * @return string
     106         */
     107        public static function new_token() {
     108                return rtrim( strtr( base64_encode( random_bytes( 32 ) ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Encodes a random token, nothing is hidden.
     109        }
     110
     111        /**
     112         * Store the connection from the answer to "POST /connect". The token is the one the
     113         * plugin made (begin()).
     114         *
     115         * @param array  $answer    Decoded answer: address, check_url, check.
     116         * @param string $site_hash Hash of the site settings the connect call carried.
     117         * @return bool False when the answer lacks a valid address or no token is pending.
     118         */
     119        public static function save_from_connect( $answer, $site_hash ) {
    69120                $answer  = is_array( $answer ) ? $answer : array();
    70                 $token   = isset( $answer['token'] ) && is_string( $answer['token'] ) ? $answer['token'] : '';
     121                $current = self::get();
    71122                $address = isset( $answer['address'] ) && is_string( $answer['address'] ) ? $answer['address'] : '';
    72                 $url     = isset( $answer['check_url'] ) && is_string( $answer['check_url'] ) ? esc_url_raw( $answer['check_url'], array( 'https', 'http' ) ) : '';
    73 
    74                 if ( '' === $token || ! is_email( $address ) ) {
    75                         return false;
    76                 }
    77 
     123                if ( '' === $current['token'] || ! is_email( $address ) ) {
     124                        return false;
     125                }
    78126                update_option(
    79127                        self::OPTION,
    80128                        array(
    81                                 'token'        => $token,
     129                                'token'        => $current['token'],
    82130                                'address'      => $address,
    83                                 'check_id'     => isset( $answer['check_id'] ) && is_scalar( $answer['check_id'] ) ? (string) $answer['check_id'] : '',
    84                                 'check_url'    => $url,
     131                                'check_id'     => '',
     132                                'check_url'    => isset( $answer['check_url'] ) && is_string( $answer['check_url'] ) ? esc_url_raw( $answer['check_url'], array( 'https', 'http' ) ) : '',
    85133                                'home_url'     => home_url(),
    86134                                'connected_at' => time(),
     135                                'alert_email'  => $current['alert_email'],
     136                                'site_hash'    => (string) $site_hash,
    87137                        ),
    88138                        false
    … …  
    90140                delete_option( self::NOTICE_OPTION );
    91141                return true;
     142        }
     143
     144        /**
     145         * Store the link to the site's page in nanoPost, when it changed.
     146         *
     147         * @param string $url URL.
     148         */
     149        public static function set_check_url( $url ) {
     150                if ( self::get()['check_url'] !== $url ) {
     151                        self::update( array( 'check_url' => (string) $url ) );
     152                }
     153        }
     154
     155        /**
     156         * The alert address the owner last typed, or ''.
     157         *
     158         * @return string
     159         */
     160        public static function alert_email() {
     161                return self::get()['alert_email'];
     162        }
     163
     164        /**
     165         * Remember the alert address the owner typed (after "Change").
     166         *
     167         * @param string $email Address.
     168         */
     169        public static function set_alert_email( $email ) {
     170                self::update( array( 'alert_email' => (string) $email ) );
     171        }
     172
     173        /**
     174         * True while connected and Email Canary does not say it stopped watching. The test then
     175         * goes to the canary address. While stopped, it goes to the recipient setting, and the
     176         * plugin calls nanoPost only to start again or to end.
     177         *
     178         * @return bool
     179         */
     180        public static function canary_active() {
     181                if ( ! self::is_connected() ) {
     182                        return false;
     183                }
     184                $block = NanoPost_Email_Check_Canary::block();
     185                return null === $block || 'stopped' !== $block['watching'];
    92186        }
    93187
    … …  
    201295                }
    202296                wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
     297                NanoPost_Email_Check_Canary::forget();
    203298                NanoPost_Email_Check_Webhook::reset_check( 'wp_mail' );
    204299        }
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-help.php

    r3829352 r3830659  
    3333        const PLACE_NOTICE_STALE  = 'notice-stale';
    3434        const PLACE_CLI           = 'cli';
    35         const PLACE_CONNECTION    = 'connection';
     35        const PLACE_CANARY        = 'canary';
     36        const PLACE_NOTICE_CANARY = 'notice-canary';
    3637        const PLACE_PROOF         = 'proof';
    3738
    … …  
    107108
    108109        /**
    109          * What connecting to nanoPost does.
     110         * Anchors on the Email Canary help page. Agreed with the help pages; do not rename.
     111         */
     112        const CANARY_TURN_ON = 'turn-on-email-canary';
     113        const CANARY_CONFIRM = 'before-you-confirm-alerts-are-off';
     114        const CANARY_CHANGE  = 'change-the-alert-address';
     115        const CANARY_STOP    = 'stop-watching';
     116        const CANARY_SENT    = 'what-is-sent-to-nanopost';
     117
     118        /**
     119         * A part of the Email Canary help page.
    110120         *
    111          * @param string $place Where the link is shown.
     121         * @param string $anchor A CANARY_ constant.
     122         * @param string $place  Where the link is shown.
    112123         * @return string
    113124         */
    114         public static function connect( $place = '' ) {
    115                 return self::url( 'connect/', $place );
     125        public static function canary( $anchor, $place = '' ) {
     126                return self::url( 'canary/', $place, $anchor );
    116127        }
    117128
    … …  
    123134         */
    124135        public static function proof( $place = '' ) {
    125                 return self::url( 'connect/', $place, 'proof-of-arrival' );
     136                return self::canary( self::CANARY_TURN_ON, $place );
    126137        }
    127138
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-log.php

    r3818097 r3830659  
    2525                array_unshift( $log, $result->to_array() );
    2626                update_option( self::OPTION, array_slice( $log, 0, self::MAX ), false );
     27        }
     28
     29        /**
     30         * Add what Email Canary says about one test to its row. Does nothing when no row has
     31         * this test ID (for example, it is older than the last MAX results).
     32         *
     33         * @param string $test_id Test ID.
     34         * @param array  $fields  Values for the row's "canary" part.
     35         * @return bool True when a row changed.
     36         */
     37        public static function update_test( $test_id, $fields ) {
     38                if ( '' === $test_id ) {
     39                        return false;
     40                }
     41                $log = self::raw();
     42                foreach ( $log as $index => $row ) {
     43                        if ( is_array( $row ) && isset( $row['test_id'] ) && $test_id === $row['test_id'] ) {
     44                                $old                     = isset( $row['canary'] ) && is_array( $row['canary'] ) ? $row['canary'] : array();
     45                                $log[ $index ]['canary'] = array_merge( $old, (array) $fields );
     46                                if ( $log[ $index ]['canary'] === $old ) {
     47                                        return false;
     48                                }
     49                                update_option( self::OPTION, $log, false );
     50                                return true;
     51                        }
     52                }
     53                return false;
    2754        }
    2855
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-mail-check.php

    r3829352 r3830659  
    4444                $test_id    = '';
    4545
    46                 // While connected to nanoPost, the test goes only to the check's address, with a
    47                 // new test ID in it. The recipient setting is used again after a disconnect.
    48                 if ( NanoPost_Email_Check_Connection::is_connected() ) {
     46                // While Email Canary is on, the test goes only to the canary address, with a new
     47                // test ID in it. The recipient setting is used again when it is off.
     48                if ( NanoPost_Email_Check_Connection::canary_active() ) {
    4949                        $test_id    = NanoPost_Email_Check_Connection::new_test_id();
    5050                        $recipient  = NanoPost_Email_Check_Connection::test_address( $test_id );
    … …  
    8585                        $result->test_id  = $test_id;
    8686                        $call             = NanoPost_Email_Check_App::announce( $test_id, $time, $trigger, $sent, $sent ? '' : NanoPost_Email_Check_App::announce_error( $error, $cause ) );
    87                         $result->announce = $call['ok'] ? NanoPost_Email_Check_Result::ANNOUNCE_OK : ( 401 === $call['code'] ? NanoPost_Email_Check_Result::ANNOUNCE_ENDED : NanoPost_Email_Check_Result::ANNOUNCE_FAILED );
     87                        $result->announce = self::announce_outcome( $call );
     88                        if ( $call['ok'] && $sent ) {
     89                                NanoPost_Email_Check_Canary::schedule_fetch( $test_id, $call );
     90                        }
    8891                }
    8992
    9093                return $result;
     94        }
     95
     96        /**
     97         * What the announce answer means for the plugin's own webhook. A 2xx counts as "ok"
     98         * only when Email Canary watches (the block says "on"; an answer with no block, from
     99         * an app before the block, counts as watching). The block is stored.
     100         *
     101         * @param array $call Result of the announce call.
     102         * @return string A NanoPost_Email_Check_Result::ANNOUNCE_ constant.
     103         */
     104        private static function announce_outcome( $call ) {
     105                if ( 401 === $call['code'] ) {
     106                        return NanoPost_Email_Check_Result::ANNOUNCE_ENDED;
     107                }
     108                if ( 409 === $call['code'] ) {
     109                        return NanoPost_Email_Check_Result::ANNOUNCE_UNWATCHED;
     110                }
     111                if ( ! $call['ok'] ) {
     112                        return NanoPost_Email_Check_Result::ANNOUNCE_FAILED;
     113                }
     114                if ( NanoPost_Email_Check_Canary::take( $call['body'] ) && 'on' !== NanoPost_Email_Check_Canary::block()['watching'] ) {
     115                        return NanoPost_Email_Check_Result::ANNOUNCE_UNWATCHED;
     116                }
     117                return NanoPost_Email_Check_Result::ANNOUNCE_OK;
    91118        }
    92119
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-plugin.php

    r3829352 r3830659  
    2121                NanoPost_Email_Check_Webhook::init();
    2222                NanoPost_Email_Check_App::init();
     23                NanoPost_Email_Check_Canary::init();
    2324
    2425                if ( is_admin() ) {
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-result.php

    r3829352 r3830659  
    2828
    2929        /**
     30         * The app took the announce call, but Email Canary does not watch (not confirmed,
     31         * stopped, over the limit), so it sends no alert. The plugin's own webhook alerts.
     32         */
     33        const ANNOUNCE_UNWATCHED = 'unwatched';
     34
     35        /**
    3036         * Check id, from NanoPost_Email_Check_Check::id().
    3137         *
    … …  
    8894         * Whether nanoPost took the announce call for this test: ANNOUNCE_OK, ANNOUNCE_FAILED,
    8995         * ANNOUNCE_ENDED (nanoPost answered 401: the connection had ended, for example the
    90          * owner disconnected the site in nanoPost), or '' when no call was due (not connected).
     96         * owner disconnected the site in nanoPost), ANNOUNCE_UNWATCHED, or '' when no call was
     97         * due (not connected).
    9198         *
    9299         * @var string
    93100         */
    94101        public $announce = '';
     102
     103        /**
     104         * What Email Canary said about this test: arrival, arrived_at, spf, dkim, dmarc,
     105         * reason, help_url, fetched_at (see NanoPost_Email_Check_Canary::normalize_test()).
     106         * Empty until the plugin asked.
     107         *
     108         * @var array
     109         */
     110        public $canary = array();
    95111
    96112        /**
    … …  
    168184                        'test_id'   => $this->test_id,
    169185                        'announce'  => $this->announce,
     186                        'canary'    => $this->canary,
    170187                );
    171188        }
    … …  
    190207                                'test_id'   => '',
    191208                                'announce'  => '',
     209                                'canary'    => array(),
    192210                        )
    193211                );
    … …  
    195213                $result           = new self( $data['check'], $data['trigger'], $data['passed'], $data['error'], $data['recipient'], $data['time'], $data['mailer'] );
    196214                $result->test_id  = is_string( $data['test_id'] ) && preg_match( '/^[a-z0-9]{12}$/', $data['test_id'] ) ? $data['test_id'] : '';
    197                 $result->announce = in_array( $data['announce'], array( self::ANNOUNCE_OK, self::ANNOUNCE_FAILED, self::ANNOUNCE_ENDED ), true ) ? $data['announce'] : '';
     215                $result->announce = in_array( $data['announce'], array( self::ANNOUNCE_OK, self::ANNOUNCE_FAILED, self::ANNOUNCE_ENDED, self::ANNOUNCE_UNWATCHED ), true ) ? $data['announce'] : '';
     216                $result->canary   = is_array( $data['canary'] ) ? $data['canary'] : array();
    198217                return $result;
    199218        }
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-scheduler.php

    r3829352 r3830659  
    4040                if ( ! is_multisite() || ! $network_wide || wp_is_large_network() ) {
    4141                        self::ensure_scheduled();
     42                        NanoPost_Email_Check_App::queue_start();
    4243                        return;
    4344                }
    … …  
    4647                        switch_to_blog( $site_id );
    4748                        self::ensure_scheduled();
     49                        NanoPost_Email_Check_App::queue_start();
    4850                        restore_current_blog();
    4951                }
    … …  
    99101         */
    100102        public static function deactivate( $network_wide = false ) {
     103                $deadline = time() + NanoPost_Email_Check_App::DEACTIVATE_BUDGET;
    101104                if ( ! is_multisite() || ! $network_wide ) {
     105                        NanoPost_Email_Check_App::stop_on_deactivate( $deadline );
    102106                        wp_clear_scheduled_hook( self::HOOK );
    103107                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
     108                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     109                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    104110                        return;
    105111                }
    … …  
    107113                foreach ( self::site_ids() as $site_id ) {
    108114                        switch_to_blog( $site_id );
     115                        NanoPost_Email_Check_App::stop_on_deactivate( $deadline );
    109116                        wp_clear_scheduled_hook( self::HOOK );
    110117                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
     118                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     119                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    111120                        restore_current_blog();
    112121                }
  • nanopost-email-check/tags/1.8.0/includes/class-nanopost-email-check-webhook.php

    r3829352 r3830659  
    5858                // The screen shows "Reconnect", and the next test goes to the recipient setting,
    5959                // where a real mail problem alerts as usual.
     60                // Not when Email Canary does not watch (ANNOUNCE_UNWATCHED: not confirmed, stopped,
     61                // over the limit): it sends no alert then, so the plugin's webhook alerts as when
     62                // not connected.
    6063                if ( in_array( $result->announce, array( NanoPost_Email_Check_Result::ANNOUNCE_OK, NanoPost_Email_Check_Result::ANNOUNCE_ENDED ), true ) && ! $result->passed ) {
    6164                        if ( '' !== $previous ) {
    … …  
    107110        public static function payload( NanoPost_Email_Check_Result $result, $url ) {
    108111                $site = home_url();
    109 
     112                $name = self::site_name();
     113                $mail = 'wp_mail' === $result->check;
     114
     115                // "[Email Check]" says which part speaks; "[Email Canary]" messages come from nanoPost.
    110116                if ( $result->passed ) {
    111                         /* translators: %s: site address. */
    112                         $text = sprintf( __( 'Email check passed again on %s.', 'nanopost-email-check' ), $site );
     117                        $text = $mail
     118                                /* translators: %s: site address without https://. */
     119                                ? sprintf( __( '[Email Check] %s can send its test email again.', 'nanopost-email-check' ), $name )
     120                                /* translators: %s: site address without https://. */
     121                                : sprintf( __( '[Email Check] A check passed again on %s.', 'nanopost-email-check' ), $name );
    113122                } else {
    114                         /* translators: 1: site address, 2: error text. */
    115                         $text = sprintf( __( 'Email check failed on %1$s: %2$s', 'nanopost-email-check' ), $site, self::escape_markup( $result->error_message() ) );
     123                        $text = $mail
     124                                /* translators: 1: site address without https://, 2: error text. */
     125                                ? sprintf( __( '[Email Check] %1$s could not send its test email: %2$s', 'nanopost-email-check' ), $name, self::escape_markup( $result->error_message() ) )
     126                                /* translators: 1: site address without https://, 2: error text. */
     127                                : sprintf( __( '[Email Check] A check failed on %1$s: %2$s', 'nanopost-email-check' ), $name, self::escape_markup( $result->error_message() ) );
    116128                }
    117129
    … …  
    130142
    131143        /**
     144         * The site address without the scheme and trailing slash, as messages name the site.
     145         *
     146         * @return string
     147         */
     148        public static function site_name() {
     149                return (string) preg_replace( '#^https?://#i', '', untrailingslashit( home_url() ) );
     150        }
     151
     152        /**
    132153         * Post a sample alert, marked as a test, to the saved URL. The delivery is recorded,
    133154         * marked as a test; the status per check is not changed, so a test neither starts an
    … …  
    162183        public static function test_payload( $url ) {
    163184                $site = home_url();
    164                 /* translators: %s: site address. */
    165                 $text = sprintf( __( 'Test alert from Email Check on %s. This is only a test of the webhook. No check failed.', 'nanopost-email-check' ), $site );
     185                /* translators: %s: site address without https://. */
     186                $text = sprintf( __( '[Email Check] Test alert from %s. This is only a test of the webhook. No check failed.', 'nanopost-email-check' ), self::site_name() );
    166187
    167188                if ( self::is_google_chat( $url ) ) {
  • nanopost-email-check/tags/1.8.0/nanopost-email-check.php

    r3829352 r3830659  
    44 * Plugin URI:        https://nanopo.st/email-check-plugin/
    55 * Description:       Check that your site can still send email. Sends a scheduled test email, logs each result and warns you when a check fails.
    6  * Version:           1.7.0
     6 * Version:           1.8.0
    77 * Requires at least: 6.4
    88 * Requires PHP:      7.4
    … …  
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.7.0' );
     20define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.8.0' );
    2121define( 'NANOPOST_EMAIL_CHECK_FILE', __FILE__ );
    2222define( 'NANOPOST_EMAIL_CHECK_DIR', plugin_dir_path( __FILE__ ) );
    … …  
    3535require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-connection.php';
    3636require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-app.php';
     37require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-canary.php';
    3738require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-admin.php';
    3839
  • nanopost-email-check/tags/1.8.0/readme.txt

    r3829352 r3830659  
    55Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.7.0
     7Stable tag: 1.8.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    4949* **Mailer shown:** each result records how the site sends: SMTP server and port, PHP `mail()`, or another plugin, and which plugin set it up. When PHP `mail()` fails because the sendmail program is missing, the error says so.
    5050* **Multisite:** each site runs its own checks, with its own settings, log and notices.
    51 * **Optional proof of arrival:** connect the site to nanoPost (opt-in) to see whether each test email arrived, with its SPF, DKIM and DMARC results, and to get an alert from nanoPost when a test does not arrive.
    52 * **No outside requests** unless you set a webhook or connect to nanoPost.
     51* **Optional Email Canary:** turn it on (opt-in) to see whether each test email arrived at nanoPost, with its SPF, DKIM and DMARC results, in the log, in a notice and in Site Health, and to get an email when a test email does not arrive.
     52* **No outside requests** unless you set a webhook or turn on Email Canary.
    5353
    5454= What a pass means =
    … …  
    6060The plugin screen is at **Tools > Email Check**.
    6161
    62 Free. No paid version.
     62The plugin is free, with no paid version. Email Canary, the optional service, is free in this early period.
    6363
    6464Full documentation: [nanopo.st/email-check-plugin](https://nanopo.st/email-check-plugin/). Source code: [GitHub](https://github.com/nanoPost/nanopost-email-check).
    … …  
    9696= Does the plugin send data to nanoPost? =
    9797
    98 Only if you connect the site to nanoPost on the plugin screen. Without a connection, nothing is sent to nanoPost. See "External services" below for what a connected site sends.
    99 
    100 = What does connecting to nanoPost add? =
    101 
    102 A connected site sends each test email to nanoPost instead of the recipient you set. nanoPost shows whether the email arrived, with its SPF, DKIM and DMARC results ("Send test now" shows this proof of arrival on the plugin screen), and alerts you when a test does not arrive. Connecting is optional, and the plugin works in full without it. Disconnect on the plugin screen at any time: test emails then go to your recipient again.
     98Only if you turn on Email Canary on the plugin screen. Until then, nothing is sent to nanoPost. See "External services" below for what the site sends while Email Canary is on.
     99
     100= What does Email Canary add? =
     101
     102Email Check can tell you that WordPress accepted the test email. Email Canary checks that it arrived. While it is on, each test email goes to Email Canary instead of the recipient you set. The plugin screen then shows, for each test, whether it arrived at nanoPost, with its SPF, DKIM and DMARC results, and Email Canary emails you when a test email does not arrive or does not pass. Type an alert address and click "Turn on Email Canary": a test email goes out at once, and you confirm the address by email. Until you confirm, alerts are off. Email Canary is optional, and the plugin works in full without it. Turn it off on the plugin screen at any time: test emails then go to your recipient again.
    103103
    104104= The notice says the checks have stopped. What does that mean? =
    … …  
    141141== External services ==
    142142
    143 The plugin makes no outside requests unless you set a webhook URL or connect the site to nanoPost.
    144 
    145 = nanoPost app (optional, after you connect) =
    146 
    147 nanoPost (app.nanopo.st) is a mail monitoring service run by the plugin's authors. The plugin calls it only after you click "Connect to nanoPost" on the plugin screen and approve the site in your browser. Until then, nothing is sent. When you disconnect, or delete the plugin, the plugin tells nanoPost and stops all calls.
    148 
    149 While connected, the site sends:
    150 
    151 * when you connect: a one-time approval code, the site address, and the address of its admin area, so nanoPost can send your browser back after you approve;
    152 * the site address, the versions of the plugin, WordPress and PHP, the check interval, and the webhook URL if you set one: after you connect, and again when one of them changes;
    153 * for each test: a test ID, the time it was sent, what started it (the schedule, a user or WP-CLI), whether `wp_mail()` succeeded, and its error text (at most 500 characters; it can contain host names or email addresses);
    154 * when you press "Send test now": a request for that test's result, while the plugin screen is open (at most 2 minutes);
     143The plugin makes no outside requests unless you set a webhook URL or turn on Email Canary.
     144
     145= nanoPost Email Canary (optional, after you turn it on) =
     146
     147nanoPost Email Canary (app.nanopo.st) is a service run by the plugin's authors. It checks that each test email arrives, and emails you when one does not. The plugin calls it only after you type an alert address and click "Turn on Email Canary" on the plugin screen. Until then, nothing is sent. When you turn it off, deactivate the plugin or delete it, the plugin tells Email Canary.
     148
     149While Email Canary is on, the site sends:
     150
     151* when you turn it on: the site address, the alert address you typed, the site's time zone, the test interval, the webhook URL if you set one, the versions of the plugin, WordPress and PHP, and a random key that the plugin makes for this site;
     152* the same site settings again when one of them changes;
     153* for each test: a test ID, the time it was sent, what started it (the schedule, a user or WP-CLI), whether WordPress accepted the email, and its error text if it did not (at most 500 characters; it can contain host names or email addresses);
     154* a request for the result of each test, after it is due, and for the state of the site when you open the plugin screen;
     155* when you resend the confirm email or change the alert address: that address;
     156* when you turn it off or on, deactivate or activate the plugin: which of these happened;
    155157* the plugin version in every call.
    156158
    157 The test emails go to nanoPost's address for the site's check instead of the recipient you set. nanoPost receives each test email and its headers, as for any email. nanoPost keeps the IP address of each call for 30 days. The plugin sends no user data, no other email, and no other settings.
     159The test emails go to Email Canary's address for the site instead of the recipient you set. Email Canary receives each test email and its headers, as for any email. nanoPost keeps the IP address of each call for 30 days. The plugin sends no user names or other user data, no other email, and no other settings. The alert address is sent only as you typed it.
    158160
    159161* Terms of use: https://nanopo.st/terms/#the-app-appnanopost
    … …  
    166168The "Send test alert" button on the plugin screen, or `wp email-check test-alert`, sends one test message to the webhook URL, only when you use it. Its text says that it is a test and that no check failed. It has the same fields as a failure message, plus `"test": true`, so a receiver can handle it like a real alert or skip it. Real alerts do not have the `test` field. For a Google Chat URL, only the text line is sent. Example:
    167169
    168 `{"text": "Test alert from Email Check on https://example.com. This is only a test of the webhook. No check failed.", "site": "https://example.com", "status": "fail", "error": "Test alert. No check failed.", "time": "2026-11-24T09:30:00+00:00", "test": true}`
     170`{"text": "[Email Check] Test alert from example.com. This is only a test of the webhook. No check failed.", "site": "https://example.com", "status": "fail", "error": "Test alert. No check failed.", "time": "2026-11-24T09:30:00+00:00", "test": true}`
    169171
    170172The setup help names these services. The plugin sends to them only when you enter one of their webhook URLs:
    … …  
    183185== Screenshots ==
    184186
    185 1. The plugin screen at Tools > Email Check: status, settings, and the result log.
    186 2. The notice in wp-admin when a check fails, with a link to decode the error.
    187 3. The notice when scheduled checks have stopped running.
    188 4. An alert in a chat app when a check fails, sent through the optional webhook.
     1871. The plugin screen at Tools > Email Check: the Email Check and Email Canary areas, the settings and the recent tests.
     1882. The notice in wp-admin when the site could not send its test email, with a link to decode the error.
     1893. The notice when scheduled tests have stopped running.
     1904. An alert in a chat app when the site could not send its test email, sent through the optional webhook.
    189191
    190192== Changelog ==
     193
     194= 1.8.0 =
     195* Email Canary replaces "Connect to nanoPost": type an alert address and turn it on from the plugin screen. No sign-up in the browser. A test email goes out at once, with its proof of arrival.
     196* The plugin screen, notices and Site Health show the state from Email Canary: whether each test email arrived, with SPF, DKIM and DMARC as labels, also for scheduled tests.
     197* Resend or change the alert address, and turn Email Canary off and on again, on the plugin screen.
     198* A deactivation tells Email Canary to stop watching; an activation starts it again.
     199* While Email Canary is not watching (before the confirm, or off), the plugin's own webhook alerts as when it is not on.
     200* The plugin's webhook messages start with "[Email Check]" and name the site, for example "[Email Check] example.com could not send its test email: ...". The JSON fields do not change.
     201* Sites connected with 1.7 stay on, with no new confirm.
    191202
    192203= 1.7.0 =
  • nanopost-email-check/tags/1.8.0/uninstall.php

    r3829352 r3830659  
    5656        delete_option( 'nanopost_email_check_app_log' );
    5757        wp_clear_scheduled_hook( 'nanopost_email_check_site_sync' );
     58        delete_option( 'nanopost_email_check_canary' );
     59        delete_transient( 'nanopost_email_check_canary_lock' );
     60        wp_unschedule_hook( 'nanopost_email_check_fetch_test' );
     61        wp_clear_scheduled_hook( 'nanopost_email_check_start' );
    5862        delete_option( 'nanopost_email_check_settings' );
    5963        delete_option( 'nanopost_email_check_log' );
  • nanopost-email-check/trunk/assets/proof.js

    r3829352 r3830659  
    6767                                }
    6868                                if ( 'missing' === data.status ) {
    69                                         return show( cfg.text.missing );
     69                                        return show( data.reason ? cfg.text.missing + ' ' + data.reason : cfg.text.missing );
     70                                }
     71                                if ( 'unknown' === data.status ) {
     72                                        return show( cfg.text.unknown );
    7073                                }
    7174                                if ( 'send_failed' === data.status ) {
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-admin.php

    r3829352 r3830659  
    2424        const REVIEW_URL = 'https://wordpress.org/support/plugin/nanopost-email-check/reviews/#new-post';
    2525
    26         const CONNECT_ACTION    = 'nanopost_email_check_connect';
    27         const RETURN_ACTION     = 'nanopost_email_check_connect_return';
    28         const DISCONNECT_ACTION = 'nanopost_email_check_disconnect';
    29         const RESULT_ACTION     = 'nanopost_email_check_test_result';
    30         const DISMISS_ENDED     = 'nanopost_email_check_dismiss_ended';
    31 
    32         /**
    33          * Per-user transients: the connect state (60 minutes) and the test to show proof for.
    34          */
    35         const STATE_PREFIX = 'nanopost_email_check_connect_state_';
     26        const TURN_ON_ACTION     = 'nanopost_email_check_turn_on';
     27        const TURN_OFF_ACTION    = 'nanopost_email_check_turn_off';
     28        const ALERT_EMAIL_ACTION = 'nanopost_email_check_alert_email';
     29        const RETURN_ACTION      = 'nanopost_email_check_connect_return';
     30        const RESULT_ACTION      = 'nanopost_email_check_test_result';
     31        const DISMISS_ENDED      = 'nanopost_email_check_dismiss_ended';
     32        const STATE_ACTION       = 'nanopost_email_check_canary_state';
     33
     34        /**
     35         * Per-user transient: the test to show proof for.
     36         */
    3637        const PROOF_PREFIX = 'nanopost_email_check_proof_';
    3738
    … …  
    4445                add_action( 'admin_post_' . self::SEND_ACTION, array( __CLASS__, 'handle_send' ) );
    4546                add_action( 'admin_post_' . self::TEST_ALERT_ACTION, array( __CLASS__, 'handle_test_alert' ) );
    46                 add_action( 'admin_post_' . self::CONNECT_ACTION, array( __CLASS__, 'handle_connect' ) );
     47                add_action( 'admin_post_' . self::TURN_ON_ACTION, array( __CLASS__, 'handle_turn_on' ) );
     48                add_action( 'admin_post_' . self::TURN_OFF_ACTION, array( __CLASS__, 'handle_turn_off' ) );
     49                add_action( 'admin_post_' . self::ALERT_EMAIL_ACTION, array( __CLASS__, 'handle_alert_email' ) );
    4750                add_action( 'admin_post_' . self::RETURN_ACTION, array( __CLASS__, 'handle_connect_return' ) );
    48                 add_action( 'admin_post_' . self::DISCONNECT_ACTION, array( __CLASS__, 'handle_disconnect' ) );
    4951                add_action( 'admin_post_' . self::DISMISS_ENDED, array( __CLASS__, 'handle_dismiss_ended' ) );
    5052                add_action( 'wp_ajax_' . self::RESULT_ACTION, array( __CLASS__, 'ajax_test_result' ) );
     53                add_action( 'wp_ajax_' . self::STATE_ACTION, array( __CLASS__, 'ajax_canary_state' ) );
    5154                add_filter( 'plugin_action_links_' . plugin_basename( NANOPOST_EMAIL_CHECK_FILE ), array( __CLASS__, 'action_links' ) );
    5255        }
    … …  
    105108                add_settings_field(
    106109                        'nanopost_email_check_interval',
    107                         __( 'Check how often', 'nanopost-email-check' ),
     110                        __( 'Test how often', 'nanopost-email-check' ),
    108111                        array( __CLASS__, 'field_interval' ),
    109112                        self::PAGE,
    … …  
    184187                );
    185188                self::locked_note( 'recipient' );
    186                 if ( NanoPost_Email_Check_Connection::is_connected() ) {
     189                if ( NanoPost_Email_Check_Connection::canary_active() ) {
    187190                        echo '<p class="description nanopost-email-check-connected-note"><strong>';
    188                         esc_html_e( 'While this site is connected to nanoPost, test emails go only to nanoPost. This address is used again after you disconnect.', 'nanopost-email-check' );
     191                        esc_html_e( 'Not used while Email Canary is on: test emails then go to Email Canary.', 'nanopost-email-check' );
    189192                        echo '</strong></p>';
    190193                }
    … …  
    317320
    318321        /**
    319          * Handle "Connect to nanoPost": make the state, then send the browser to the app,
    320          * where the owner logs in or signs up and approves the site.
    321          */
    322         public static function handle_connect() {
     322         * Handle "Turn on Email Canary", also "Turn on" again after a stop. With the same
     323         * address and a canary that only stopped, this is "start" (no new confirm). Otherwise
     324         * the plugin makes the canary with a token it made itself: a retry after a lost answer
     325         * sends the same token. Then a test goes out at once, for the proof of arrival.
     326         */
     327        public static function handle_turn_on() {
    323328                if ( ! current_user_can( self::CAPABILITY ) ) {
    324                         wp_die( esc_html__( 'You are not allowed to connect this site.', 'nanopost-email-check' ), 403 );
    325                 }
    326                 check_admin_referer( self::CONNECT_ACTION );
    327 
    328                 $state = wp_generate_password( 32, false, false );
    329                 set_transient( self::STATE_PREFIX . get_current_user_id(), $state, HOUR_IN_SECONDS );
    330 
    331                 $return = add_query_arg( 'action', self::RETURN_ACTION, admin_url( 'admin-post.php' ) );
    332                 wp_redirect( NanoPost_Email_Check_App::connect_url( $return, $state ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- The app is on another host by design; the URL is built from the API base.
     329                        wp_die( esc_html__( 'You are not allowed to change Email Canary.', 'nanopost-email-check' ), 403 );
     330                }
     331                check_admin_referer( self::TURN_ON_ACTION );
     332
     333                $email = isset( $_POST['alert_email'] ) ? sanitize_email( wp_unslash( $_POST['alert_email'] ) ) : '';
     334                if ( ! is_email( $email ) ) {
     335                        self::done( array( 'outcome' => 'canary-email' ) );
     336                }
     337
     338                $block = NanoPost_Email_Check_Canary::block();
     339                if ( NanoPost_Email_Check_Connection::is_connected() ) {
     340                        $same = self::is_alert_address( $email, $block );
     341                        if ( $same && ( null === $block || 'not_confirmed' !== $block['stopped_reason'] ) ) {
     342                                // The same address: "start". It also answers when the canary already
     343                                // watches (for example, an earlier click timed out here but reached the app).
     344                                $call = NanoPost_Email_Check_App::start( 'turn_on' );
     345                                if ( ! $call['ok'] && 409 !== $call['code'] ) {
     346                                        self::done( self::failure( 'canary-failed', $call ) );
     347                                }
     348                                if ( $call['ok'] && NanoPost_Email_Check_Connection::canary_active() ) {
     349                                        self::send_first_test();
     350                                        $block = NanoPost_Email_Check_Canary::block();
     351                                        self::done( array( 'outcome' => null !== $block && 'over_limit' === $block['watching'] ? 'canary-over-limit' : 'canary-on-again' ) );
     352                                }
     353                                self::done( array( 'outcome' => 'canary-still-off' ) );
     354                        }
     355                        if ( ! $same ) {
     356                                // Another address: a new canary (spec "Wrong owner"). The old one stops and
     357                                // is deleted after 30 days.
     358                                NanoPost_Email_Check_App::disconnect();
     359                        }
     360                        // Not confirmed in 24 hours: POST /connect again with the same token, which
     361                        // starts it again and sends a new confirm email.
     362                }
     363
     364                $call = NanoPost_Email_Check_App::connect( $email );
     365                if ( ! $call['ok'] ) {
     366                        self::done( self::failure( 'canary-failed', $call ) );
     367                }
     368                self::send_first_test();
     369                self::done( array( 'outcome' => 'canary-on' ) );
     370        }
     371
     372        /**
     373         * The test that goes out at "Turn on", for the proof of arrival.
     374         */
     375        private static function send_first_test() {
     376                set_transient( self::SEND_LOCK, 1, MINUTE_IN_SECONDS );
     377                foreach ( NanoPost_Email_Check_Plugin::run_checks( NanoPost_Email_Check_Result::TRIGGER_MANUAL ) as $result ) {
     378                        if ( '' !== $result->test_id ) {
     379                                set_transient( self::PROOF_PREFIX . get_current_user_id(), $result->test_id, 10 * MINUTE_IN_SECONDS );
     380                        }
     381                }
     382        }
     383
     384        /**
     385         * Handle "Turn Email Canary off". Before the confirm nothing is kept, so the plugin
     386         * ends the connection. Otherwise it is a stop that keeps the token. When nanoPost
     387         * cannot be reached, nothing changes here: Email Canary still watches.
     388         */
     389        public static function handle_turn_off() {
     390                if ( ! current_user_can( self::CAPABILITY ) ) {
     391                        wp_die( esc_html__( 'You are not allowed to change Email Canary.', 'nanopost-email-check' ), 403 );
     392                }
     393                check_admin_referer( self::TURN_OFF_ACTION );
     394
     395                $block = NanoPost_Email_Check_Canary::block();
     396                if ( null !== $block && 'unconfirmed' === $block['watching'] ) {
     397                        NanoPost_Email_Check_App::disconnect();
     398                        self::done( array( 'outcome' => 'canary-off' ) );
     399                }
     400                $call = NanoPost_Email_Check_App::stop( 'turn_off' );
     401                if ( ! $call['ok'] && 409 !== $call['code'] ) {
     402                        self::done( self::failure( 'canary-off-failed', $call ) );
     403                }
     404                self::done( array( 'outcome' => NanoPost_Email_Check_Connection::is_connected() ? 'canary-off' : 'canary-off-ended' ) );
     405        }
     406
     407        /**
     408         * Handle "Resend" (the same address) and "Change" (another address).
     409         */
     410        public static function handle_alert_email() {
     411                if ( ! current_user_can( self::CAPABILITY ) ) {
     412                        wp_die( esc_html__( 'You are not allowed to change Email Canary.', 'nanopost-email-check' ), 403 );
     413                }
     414                check_admin_referer( self::ALERT_EMAIL_ACTION );
     415
     416                $email = isset( $_POST['alert_email'] ) ? sanitize_email( wp_unslash( $_POST['alert_email'] ) ) : '';
     417                if ( ! is_email( $email ) ) {
     418                        self::done( array( 'outcome' => 'canary-email' ) );
     419                }
     420                $resend = strtolower( $email ) === strtolower( self::alert_address( NanoPost_Email_Check_Canary::block() ) );
     421                $call   = NanoPost_Email_Check_App::set_alert_email( $email );
     422                if ( ! $call['ok'] ) {
     423                        self::done( self::failure( 'email-failed', $call ) );
     424                }
     425                $block   = NanoPost_Email_Check_Canary::block();
     426                $limited = null !== $block && null !== $block['confirm_mail'] && 'limited' === $block['confirm_mail']['state'];
     427                self::done(
     428                        array(
     429                                'outcome' => $limited ? 'email-limited' : ( $resend ? 'email-resent' : 'email-changed' ),
     430                                'detail'  => $email,
     431                        )
     432                );
     433        }
     434
     435        /**
     436         * True when an address is one of the canary's own: confirmed, pending, or the one
     437         * typed last. Then "Turn on" starts the same canary.
     438         *
     439         * @param string     $email Address.
     440         * @param array|null $block Block, or null.
     441         * @return bool
     442         */
     443        private static function is_alert_address( $email, $block ) {
     444                $own = array( NanoPost_Email_Check_Connection::alert_email() );
     445                if ( null !== $block ) {
     446                        $own = array_merge( $own, wp_list_pluck( $block['alerts_to'], 'email' ) );
     447                }
     448                return in_array( strtolower( $email ), array_map( 'strtolower', array_filter( $own ) ), true );
     449        }
     450
     451        /**
     452         * The confirmed address that gets the alerts, else alert_address().
     453         *
     454         * @param array|null $block Block, or null.
     455         * @return string
     456         */
     457        private static function confirmed_address( $block ) {
     458                if ( null !== $block ) {
     459                        foreach ( $block['alerts_to'] as $entry ) {
     460                                if ( 'confirmed' === $entry['state'] ) {
     461                                        return $entry['email'];
     462                                }
     463                        }
     464                }
     465                return self::alert_address( $block );
     466        }
     467
     468        /**
     469         * The address the owner set for alerts: the newest pending one, else the confirmed
     470         * one, else the one typed last.
     471         *
     472         * @param array|null $block Block, or null.
     473         * @return string
     474         */
     475        public static function alert_address( $block ) {
     476                $confirmed = '';
     477                $pending   = '';
     478                if ( null !== $block ) {
     479                        foreach ( $block['alerts_to'] as $entry ) {
     480                                if ( 'pending' === $entry['state'] ) {
     481                                        $pending = $entry['email'];
     482                                } elseif ( '' === $confirmed ) {
     483                                        $confirmed = $entry['email'];
     484                                }
     485                        }
     486                }
     487                if ( '' !== $pending ) {
     488                        return $pending;
     489                }
     490                return '' !== $confirmed ? $confirmed : NanoPost_Email_Check_Connection::alert_email();
     491        }
     492
     493        /**
     494         * The notice for a failed call.
     495         *
     496         * @param string $outcome Outcome key.
     497         * @param array  $call    Call result.
     498         * @return array
     499         */
     500        private static function failure( $outcome, $call ) {
     501                return array(
     502                        'outcome' => $outcome,
     503                        'detail'  => '' !== $call['message'] ? $call['message'] : self::call_detail( $call ),
     504                );
     505        }
     506
     507        /**
     508         * Store the outcome for one notice and go back to the screen.
     509         *
     510         * @param array $outcome Outcome (and detail).
     511         */
     512        private static function done( $outcome ) {
     513                set_transient( self::notice_key(), $outcome, MINUTE_IN_SECONDS );
     514                wp_safe_redirect( self::page_url() . '#nanopost-email-check-connection' );
    333515                exit;
    334516        }
    335517
    336518        /**
    337          * The app sends the browser back here with a one-time code and the state. Checks the
    338          * state (it stands in for a nonce), then exchanges the code for a token.
     519         * Sites that started the browser connect of version 1.7 come back here after an
     520         * update. That flow is gone: show the screen, where "Turn on Email Canary" is.
    339521         */
    340522        public static function handle_connect_return() {
    341                 if ( ! current_user_can( self::CAPABILITY ) ) {
    342                         wp_die( esc_html__( 'You are not allowed to connect this site.', 'nanopost-email-check' ), 403 );
    343                 }
    344 
    345                 // phpcs:disable WordPress.Security.NonceVerification.Recommended -- The state is the one-time check; it is compared below.
    346                 $state = isset( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : '';
    347                 $code  = isset( $_GET['code'] ) ? sanitize_text_field( wp_unslash( $_GET['code'] ) ) : '';
    348                 $error = isset( $_GET['error'] ) ? sanitize_key( wp_unslash( $_GET['error'] ) ) : '';
    349                 // phpcs:enable WordPress.Security.NonceVerification.Recommended
    350 
    351                 $key    = self::STATE_PREFIX . get_current_user_id();
    352                 $stored = get_transient( $key );
    353                 delete_transient( $key );
    354 
    355                 if ( ! is_string( $stored ) || '' === $state || ! hash_equals( $stored, $state ) ) {
    356                         $outcome = array( 'outcome' => 'connect-state' );
    357                 } elseif ( '' === $code && 'cancelled' === $error ) {
    358                         // The owner pressed Cancel in nanoPost: back to the screen, as if nothing happened.
    359                         $outcome = null;
    360                 } elseif ( '' === $code ) {
    361                         $outcome = array( 'outcome' => 'connect-cancelled' );
    362                 } else {
    363                         $call    = NanoPost_Email_Check_App::exchange( $code );
    364                         $outcome = $call['ok']
    365                                 ? array( 'outcome' => 'connected' )
    366                                 : array(
    367                                         'outcome' => 'invalid_code' === $call['error'] ? 'connect-invalid' : 'connect-failed',
    368                                         'detail'  => '' !== $call['message'] ? $call['message'] : self::call_detail( $call ),
    369                                 );
    370                 }
    371 
    372                 if ( null !== $outcome ) {
    373                         set_transient( self::notice_key(), $outcome, MINUTE_IN_SECONDS );
    374                 }
    375                 wp_safe_redirect( self::page_url() );
    376                 exit;
    377         }
    378 
    379         /**
    380          * Handle "Disconnect": tell the app (best effort) and delete the connection here.
    381          */
    382         public static function handle_disconnect() {
    383                 if ( ! current_user_can( self::CAPABILITY ) ) {
    384                         wp_die( esc_html__( 'You are not allowed to disconnect this site.', 'nanopost-email-check' ), 403 );
    385                 }
    386                 check_admin_referer( self::DISCONNECT_ACTION );
    387 
    388                 NanoPost_Email_Check_App::disconnect();
    389                 set_transient( self::notice_key(), 'disconnected', MINUTE_IN_SECONDS );
    390523                wp_safe_redirect( self::page_url() );
    391524                exit;
    … …  
    430563                }
    431564
     565                $test   = NanoPost_Email_Check_Canary::take_test_answer( $call['body'] );
    432566                $body   = $call['body'];
    433567                $fields = array();
    434                 foreach ( array( 'status', 'sent_at', 'arrived_at', 'spf', 'dkim', 'dmarc' ) as $key ) {
     568                foreach ( array( 'sent_at', 'arrived_at' ) as $key ) {
    435569                        $fields[ $key ] = isset( $body[ $key ] ) && is_string( $body[ $key ] ) ? $body[ $key ] : null;
    436570                }
    437                 $fields['status']    = in_array( $fields['status'], array( 'waiting', 'arrived', 'missing', 'send_failed' ), true ) ? $fields['status'] : 'waiting';
    438                 $arrived             = null === $fields['arrived_at'] ? false : strtotime( $fields['arrived_at'] );
    439                 $fields['arrived']   = false === $arrived ? null : self::format_date( $arrived );
     571                foreach ( array( 'spf', 'dkim', 'dmarc' ) as $key ) {
     572                        $fields[ $key ] = '' === $test[ $key ] ? null : $test[ $key ];
     573                }
     574                $fields['status']    = in_array( $test['arrival'], NanoPost_Email_Check_Canary::ARRIVALS, true ) ? $test['arrival'] : 'waiting';
     575                $fields['arrived']   = 0 === $test['arrived_at'] ? null : self::format_date( $test['arrived_at'] );
     576                $fields['reason']    = NanoPost_Email_Check_Canary::reason_text( $test['reason'] );
     577                $fields['help_url']  = $test['help_url'];
    440578                $fields['check_url'] = isset( $body['check_url'] ) && is_string( $body['check_url'] ) ? esc_url_raw( $body['check_url'], array( 'https', 'http' ) ) : NanoPost_Email_Check_Connection::get()['check_url'];
    441579                wp_send_json_success( $fields );
     580        }
     581
     582        /**
     583         * The state of the canary, asked by the browser when the plugin screen opens. The
     584         * plugin calls "GET /check" at most once a minute. When the state changed, the answer
     585         * has the new HTML of the Email Canary part and of the log (escaped on the server).
     586         */
     587        public static function ajax_canary_state() {
     588                check_ajax_referer( self::STATE_ACTION );
     589                if ( ! current_user_can( self::CAPABILITY ) ) {
     590                        wp_send_json_error( array( 'error' => 'forbidden' ), 403 );
     591                }
     592                if ( ! NanoPost_Email_Check_Connection::is_connected() || ! NanoPost_Email_Check_Canary::refresh_for_screen() ) {
     593                        wp_send_json_success( array( 'changed' => false ) );
     594                }
     595
     596                ob_start();
     597                self::render_canary();
     598                $canary = (string) ob_get_clean();
     599                ob_start();
     600                self::render_log();
     601                $log = (string) ob_get_clean();
     602
     603                wp_send_json_success(
     604                        array(
     605                                'changed' => true,
     606                                'canary'  => $canary,
     607                                'log'     => $log,
     608                        )
     609                );
    442610        }
    443611
    … …  
    485653                echo '<div id="nanopost-email-check-status">';
    486654                self::render_status();
     655                echo '<div id="nanopost-email-check-send">';
     656                self::render_send_form();
     657                echo '</div>';
    487658                echo '</div>';
    488659
    … …  
    490661                self::render_connection();
    491662                echo '</div>';
     663                self::enqueue_canary_assets();
    492664
    493665                echo '<form id="nanopost-email-check-settings" method="post" action="' . esc_url( admin_url( 'options.php' ) ) . '">';
    … …  
    504676                }
    505677
    506                 echo '<div id="nanopost-email-check-send">';
    507                 self::render_send_form();
    508                 echo '</div>';
    509 
    510678                echo '<div id="nanopost-email-check-log">';
    511679                self::render_log();
    … …  
    537705                }
    538706                $proof = '';
    539                 if ( in_array( $outcome, array( 'passed', 'failed' ), true ) ) {
     707                if ( in_array( $outcome, array( 'passed', 'failed', 'canary-on', 'canary-on-again', 'canary-over-limit' ), true ) ) {
    540708                        $proof = get_transient( self::PROOF_PREFIX . get_current_user_id() );
    541709                        delete_transient( self::PROOF_PREFIX . get_current_user_id() );
    542                         $proof = is_string( $proof ) && NanoPost_Email_Check_Connection::is_connected() ? $proof : '';
     710                        $proof = is_string( $proof ) && NanoPost_Email_Check_Connection::canary_active() ? $proof : '';
    543711                }
    544712
    … …  
    563731                        $class   = 'notice-warning';
    564732                        $message = __( 'A test email was sent less than one minute ago. Wait one minute, then try again.', 'nanopost-email-check' );
    565                 } elseif ( 'connected' === $outcome ) {
     733                } elseif ( 'canary-on' === $outcome ) {
    566734                        $class   = 'notice-success';
    567                         $message = __( 'This site is connected to nanoPost. Test emails now go to nanoPost, and nanoPost alerts you when a test does not arrive.', 'nanopost-email-check' );
    568                 } elseif ( 'disconnected' === $outcome ) {
     735                        $message = __( 'Email Canary: turned on. A test email went out. Its arrival shows below.', 'nanopost-email-check' );
     736                } elseif ( 'canary-on-again' === $outcome ) {
    569737                        $class   = 'notice-success';
    570                         $message = __( 'This site is disconnected from nanoPost. Test emails go to the address in the settings again.', 'nanopost-email-check' );
    571                 } elseif ( 'connect-state' === $outcome ) {
    572                         $class   = 'notice-error';
    573                         $message = __( 'The connection was not made: the request expired or did not come from this screen. Click "Connect to nanoPost" again.', 'nanopost-email-check' );
    574                 } elseif ( 'connect-cancelled' === $outcome ) {
     738                        $message = __( 'Email Canary is on again.', 'nanopost-email-check' );
     739                } elseif ( 'canary-over-limit' === $outcome ) {
    575740                        $class   = 'notice-warning';
    576                         $message = __( 'The connection was not made. Click "Connect to nanoPost" to try again.', 'nanopost-email-check' );
    577                 } elseif ( 'connect-invalid' === $outcome ) {
    578                         $class   = 'notice-error';
    579                         $message = __( 'The connection was not made: the approval code was wrong, used or expired. Click "Connect to nanoPost" again.', 'nanopost-email-check' );
    580                 } elseif ( 'connect-failed' === $outcome ) {
     741                        $message = __( 'Email Canary: alerts are OFF. Your nanoPost account has reached its limit of sites. Remove one in nanoPost, or ask us for more.', 'nanopost-email-check' );
     742                } elseif ( 'canary-still-off' === $outcome ) {
     743                        $class   = 'notice-warning';
     744                        $message = __( 'Email Canary could not turn on again. See why below.', 'nanopost-email-check' );
     745                } elseif ( 'canary-off' === $outcome ) {
     746                        $class   = 'notice-success';
     747                        $message = __( 'Email Canary is off. Email Check sends test emails to the address in the settings again. Email Canary keeps the history for 30 days.', 'nanopost-email-check' );
     748                } elseif ( 'canary-off-ended' === $outcome ) {
     749                        $class   = 'notice-success';
     750                        $message = __( 'Email Canary is off. Email Check sends test emails to the address in the settings again.', 'nanopost-email-check' );
     751                } elseif ( 'canary-off-failed' === $outcome ) {
    581752                        $class = 'notice-error';
    582753                        /* translators: %s: error text or HTTP status. */
    583                         $message = sprintf( __( 'The connection was not made: could not reach nanoPost (%s). Try again later.', 'nanopost-email-check' ), $extra );
     754                        $message = sprintf( __( 'Could not reach nanoPost (%s). Email Canary is still on. Try again later.', 'nanopost-email-check' ), $extra );
     755                } elseif ( 'canary-failed' === $outcome ) {
     756                        $class = 'notice-error';
     757                        /* translators: %s: error text or HTTP status. */
     758                        $message = sprintf( __( 'Email Canary did not turn on: %s. Nothing changed. Try again later.', 'nanopost-email-check' ), $extra );
     759                } elseif ( 'canary-email' === $outcome ) {
     760                        $class   = 'notice-error';
     761                        $message = __( 'Enter a valid email address for the alerts.', 'nanopost-email-check' );
     762                } elseif ( 'email-resent' === $outcome ) {
     763                        $class = 'notice-success';
     764                        /* translators: %s: email address. */
     765                        $message = sprintf( __( 'Email Canary: we sent the confirm email again to %s.', 'nanopost-email-check' ), $extra );
     766                } elseif ( 'email-changed' === $outcome ) {
     767                        $class = 'notice-success';
     768                        /* translators: %s: email address. */
     769                        $message = sprintf( __( 'Email Canary: we sent a confirm email to %s. Alerts go there once it is confirmed.', 'nanopost-email-check' ), $extra );
     770                } elseif ( 'email-limited' === $outcome ) {
     771                        $class   = 'notice-warning';
     772                        $message = __( 'Email Canary: we cannot send another confirm email now. Try again tomorrow. Proof of arrival still works.', 'nanopost-email-check' );
     773                } elseif ( 'email-failed' === $outcome ) {
     774                        $class = 'notice-error';
     775                        /* translators: %s: error text or HTTP status. */
     776                        $message = sprintf( __( 'The alert address did not change: %s. Try again later.', 'nanopost-email-check' ), $extra );
    584777                } elseif ( 'passed' === $outcome && '' !== $proof ) {
    585778                        $class   = 'notice-success';
    586                         $message = __( 'WordPress accepted the test email for nanoPost. The proof of arrival shows below.', 'nanopost-email-check' );
     779                        $message = __( 'WordPress accepted the test email for Email Canary. The proof of arrival shows below.', 'nanopost-email-check' );
    587780                } elseif ( 'passed' === $outcome ) {
    588781                        $class   = 'notice-success';
    … …  
    605798
    606799        /**
    607          * "Reconnect" notice after the connection ended without the owner's click.
     800         * Notice after the connection ended without the owner's click.
    608801         */
    609802        private static function render_ended_notice() {
    … …  
    613806                }
    614807                $message = NanoPost_Email_Check_Connection::ENDED_SITE_CHANGED === $reason
    615                         ? __( 'The connection to nanoPost ended because the site address changed (for example a copied or moved site). Test emails go to the address in the settings again. Connect again to use nanoPost from this address.', 'nanopost-email-check' )
    616                         : __( 'nanoPost ended the connection to this site (it was disconnected or deleted in nanoPost). Test emails go to the address in the settings again. Reconnect to use nanoPost.', 'nanopost-email-check' );
     808                        ? __( 'Email Canary is off: the site address changed (a copied or moved site). Turn it on again to use Email Canary from this address.', 'nanopost-email-check' )
     809                        : __( 'Email Canary is off: nanoPost ended it for this site (it was deleted in nanoPost, or stopped for more than 30 days). Email Check sends test emails to the address in the settings again.', 'nanopost-email-check' );
    617810
    618811                echo '<div class="notice notice-warning nanopost-email-check-reconnect"><p>' . esc_html( $message ) . '</p><p>';
    619                 self::connect_button( __( 'Reconnect', 'nanopost-email-check' ) );
    620                 echo ' <a href="' . esc_url( wp_nonce_url( add_query_arg( 'action', self::DISMISS_ENDED, admin_url( 'admin-post.php' ) ), self::DISMISS_ENDED ) ) . '">' . esc_html__( 'Dismiss', 'nanopost-email-check' ) . '</a>';
     812                echo '<a href="#nanopost-email-check-connection">' . esc_html__( 'Turn on again', 'nanopost-email-check' ) . '</a> &middot; ';
     813                echo '<a href="' . esc_url( wp_nonce_url( add_query_arg( 'action', self::DISMISS_ENDED, admin_url( 'admin-post.php' ) ), self::DISMISS_ENDED ) ) . '">' . esc_html__( 'Dismiss', 'nanopost-email-check' ) . '</a>';
    621814                echo '</p></div>';
    622815        }
    … …  
    634827                echo '<p class="nanopost-email-check-proof-status" aria-live="polite">' . esc_html__( 'Waiting for the test email to arrive at nanoPost ...', 'nanopost-email-check' ) . '</p>';
    635828                if ( '' !== $check_url ) {
    636                         echo '<p>' . self::help_link( $check_url, __( 'Open the check in nanoPost', 'nanopost-email-check' ) ) . ' &middot; '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     829                        echo '<p>' . self::help_link( $check_url, __( 'Open in nanoPost', 'nanopost-email-check' ) ) . ' &middot; '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    637830                        echo self::help_link( NanoPost_Email_Check_Help::proof( NanoPost_Email_Check_Help::PLACE_PROOF ), __( 'About proof of arrival', 'nanopost-email-check' ) ) . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    638831                }
    … …  
    653846                                        /* translators: 1: SPF result, 2: DKIM result, 3: DMARC result, such as "pass". */
    654847                                        'auth'        => __( 'SPF: %1$s. DKIM: %2$s. DMARC: %3$s.', 'nanopost-email-check' ),
    655                                         'missing'     => __( 'The test email did not arrive at nanoPost in time. nanoPost alerts you about it.', 'nanopost-email-check' ),
     848                                        'missing'     => __( 'The test email did not arrive at nanoPost in time.', 'nanopost-email-check' ),
    656849                                        'send_failed' => __( 'The site could not send the test email. See the error in the log below.', 'nanopost-email-check' ),
    657                                         'unreachable' => __( 'Could not reach nanoPost. Open the check in nanoPost to see the result.', 'nanopost-email-check' ),
    658                                         'timeout'     => __( 'No result yet. The email can still arrive: open the check in nanoPost to see the result.', 'nanopost-email-check' ),
     850                                        'unknown'     => __( 'Email Canary cannot tell now. Email Check sends your test emails as usual.', 'nanopost-email-check' ),
     851                                        'unreachable' => __( 'Could not reach nanoPost. Open nanoPost to see the result.', 'nanopost-email-check' ),
     852                                        'timeout'     => __( 'No result yet. The email can still arrive: the log below shows it later.', 'nanopost-email-check' ),
    659853                                        'none'        => __( 'none', 'nanopost-email-check' ),
    660854                                ),
    … …  
    664858
    665859        /**
    666          * The nanoPost section: consent and "Connect", or the connection and "Disconnect".
     860         * The Email Canary area: the offer with the consent text, or the state of the canary
     861         * with what the owner can do. The inner part is replaced by the browser when a newer
     862         * state comes (assets/canary.js).
    667863         */
    668864        private static function render_connection() {
    669                 echo '<h2>' . esc_html__( 'nanoPost', 'nanopost-email-check' ) . '</h2>';
     865                echo '<h2>' . esc_html__( 'Email Canary', 'nanopost-email-check' ) . ' <span class="nanopost-email-check-tagline">' . esc_html__( 'Did it arrive? Does it keep arriving?', 'nanopost-email-check' ) . '</span></h2>';
     866                echo '<div id="nanopost-email-check-canary">';
     867                self::render_canary();
     868                echo '</div>';
    670869
    671870                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
    672                         echo '<p>' . esc_html__( 'Optional. Connect this site to nanoPost (app.nanopo.st), a mail monitoring service. Each test email then goes to nanoPost, which shows here whether it arrived, with its SPF, DKIM and DMARC results, and alerts you when a test does not arrive.', 'nanopost-email-check' ) . '</p>';
    673                         echo '<p>' . esc_html__( 'When you connect, this site sends to nanoPost: the site address and the address of its admin area; the versions of this plugin, WordPress and PHP; the check interval; the webhook URL, if you set one; and for each test its ID, the time, what started it, and whether wp_mail() succeeded, with its error text, which can contain host names or email addresses. nanoPost also receives the test emails, and keeps the IP address of each call for 30 days. Nothing is sent before you connect.', 'nanopost-email-check' ) . '</p>';
    674                         echo '<p>';
    675                         self::connect_button( __( 'Connect to nanoPost', 'nanopost-email-check' ) );
    676                         echo ' ' . self::help_link( NanoPost_Email_Check_Help::TERMS_URL, __( 'Terms', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    677                         echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::PRIVACY_URL, __( 'Privacy', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    678                         echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::connect( NanoPost_Email_Check_Help::PLACE_CONNECTION ), __( 'How connecting works', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    679                         echo '</p>';
    680871                        return;
    681872                }
    682 
    683873                $connection = NanoPost_Email_Check_Connection::get();
    684                 echo '<p>';
    685                 echo esc_html(
    686                         sprintf(
    687                                 /* translators: %s: the check's email address in nanoPost. */
    688                                 __( 'Connected. Test emails go to nanoPost (%s).', 'nanopost-email-check' ),
    689                                 $connection['address']
    690                         )
    691                 );
    692                 if ( '' !== $connection['check_url'] ) {
    693                         echo ' ' . self::help_link( $connection['check_url'], __( 'Open the check in nanoPost', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
    694                 }
    695                 echo '</p>';
    696 
    697                 $too_old = NanoPost_Email_Check_Connection::too_old_message();
     874                $too_old    = NanoPost_Email_Check_Connection::too_old_message();
    698875                if ( '' !== $too_old ) {
    699                         echo '<div class="notice notice-error inline"><p>' . esc_html( $too_old ) . ' ' . esc_html__( 'Until then, the plugin does not call nanoPost. Test emails still go to nanoPost.', 'nanopost-email-check' ) . '</p></div>';
     876                        echo '<div class="notice notice-error inline"><p>' . esc_html( $too_old ) . ' ' . esc_html__( 'Until then, the plugin does not call nanoPost. Test emails still go to Email Canary.', 'nanopost-email-check' ) . '</p></div>';
    700877                } elseif ( $connection['retry_until'] > time() ) {
    701878                        /* translators: %s: date and time. */
    702879                        echo '<p class="description">' . esc_html( sprintf( __( 'nanoPost asked the plugin to wait. Calls start again at %s.', 'nanopost-email-check' ), self::format_date( $connection['retry_until'] ) ) ) . '</p>';
    703880                }
    704 
     881        }
     882
     883        /**
     884         * The state part of the Email Canary area.
     885         */
     886        private static function render_canary() {
     887                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     888                        self::render_offer();
     889                        return;
     890                }
     891
     892                $block = NanoPost_Email_Check_Canary::block();
     893                if ( null === $block ) {
     894                        echo '<p>' . esc_html__( 'Email Canary: no state from nanoPost yet. It shows here after the next test email.', 'nanopost-email-check' ) . '</p>';
     895                        self::render_canary_actions( true );
     896                        return;
     897                }
     898
     899                // "Cannot tell" is about arrivals: it takes the place of "on", and does not hide a
     900                // stop or a missing confirm. The rest of the area stays, so the owner can still
     901                // change where alerts go.
     902                $address = self::alert_address( $block );
     903                switch ( $block['watching'] ) {
     904                        case 'on':
     905                                if ( NanoPost_Email_Check_Canary::cannot_tell( $block ) ) {
     906                                        echo '<p class="nanopost-email-check-state"><span aria-hidden="true">?</span> <strong>' . esc_html__( 'Email Canary cannot tell now.', 'nanopost-email-check' ) . '</strong> ' . esc_html__( 'Email Check sends your test emails as usual.', 'nanopost-email-check' ) . '</p>';
     907                                        break;
     908                                }
     909                                echo '<p class="nanopost-email-check-state nanopost-email-check-state-on"><span aria-hidden="true">&#9679;</span> <strong>' . esc_html__( 'Email Canary is on', 'nanopost-email-check' ) . '</strong></p>';
     910                                break;
     911                        case 'unconfirmed':
     912                                echo '<div class="notice notice-warning inline"><p><span aria-hidden="true">&#9650;</span> <strong>' . esc_html__( 'Alerts are OFF until you confirm.', 'nanopost-email-check' ) . '</strong> ';
     913                                /* translators: %s: email address. */
     914                                echo esc_html( sprintf( __( 'We sent an email to %s.', 'nanopost-email-check' ), $address ) );
     915                                if ( $block['confirm_by'] > 0 ) {
     916                                        /* translators: %s: date and time. */
     917                                        echo ' ' . esc_html( sprintf( __( 'Until %s, Email Canary shows here whether test emails arrive, but sends no alerts. Then it turns off.', 'nanopost-email-check' ), self::format_date( $block['confirm_by'] ) ) );
     918                                }
     919                                echo ' ' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CONFIRM, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'Why confirm', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     920                                echo '</p>';
     921                                self::render_confirm_mail( $block, $address );
     922                                echo '</div>';
     923                                break;
     924                        case 'over_limit':
     925                                echo '<div class="notice notice-warning inline"><p><strong>' . esc_html__( 'Email Canary: alerts are OFF.', 'nanopost-email-check' ) . '</strong> ' . esc_html__( 'Your nanoPost account has reached its limit of sites. Remove one in nanoPost, or ask us for more.', 'nanopost-email-check' ) . '</p></div>';
     926                                break;
     927                        case 'stopped':
     928                                echo '<p class="nanopost-email-check-state"><strong>' . esc_html( self::stopped_text( $block['stopped_reason'], $address ) ) . '</strong></p>';
     929                                break;
     930                        default:
     931                                echo '<p>' . esc_html( NanoPost_Email_Check_Canary::open_text() ) . '</p>';
     932                }
     933
     934                foreach ( NanoPost_Email_Check_Canary::problems( $block ) as $problem ) {
     935                        echo '<div class="notice notice-error inline"><p><span aria-hidden="true">&#10005;</span> <strong>' . esc_html( NanoPost_Email_Check_Canary::problem_text( $problem ) ) . '</strong>';
     936                        $reason = 'missing' === $problem['type'] ? NanoPost_Email_Check_Canary::reason_text( $problem['reason'] ) : __( 'Real emails from this site can be refused.', 'nanopost-email-check' );
     937                        if ( 'missing' === $problem['type'] && '' === $reason && 'ok' === $block['sending'] && $problem['sent_at'] > 0 ) {
     938                                $reason = __( 'Email Check sent it, so it was probably lost or refused after that.', 'nanopost-email-check' );
     939                        }
     940                        if ( '' !== $reason ) {
     941                                echo ' ' . esc_html( $reason );
     942                        }
     943                        if ( null !== $block['alerts_to'] && '' !== $address ) {
     944                                /* translators: %s: email address. */
     945                                echo ' ' . esc_html( sprintf( __( 'We emailed %s.', 'nanopost-email-check' ), $address ) );
     946                        }
     947                        if ( '' !== $problem['help_url'] ) {
     948                                echo ' ' . self::help_link( $problem['help_url'], __( 'What to do', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     949                        }
     950                        echo '</p></div>';
     951                }
     952
     953                if ( 'stopped' === $block['watching'] ) {
     954                        self::render_turn_on_form( self::confirmed_address( $block ), __( 'Turn on Email Canary', 'nanopost-email-check' ) );
     955                        self::render_open_link( $block );
     956                        return;
     957                }
     958
     959                echo '<table class="form-table" role="presentation"><tbody>';
     960                $arrival = $block['last_arrival'];
     961                $label   = 'missing' === $block['arrival'] ? __( 'Last arrival', 'nanopost-email-check' ) : __( 'Last test', 'nanopost-email-check' );
     962                echo '<tr><th scope="row">' . esc_html( $label ) . '</th><td>';
     963                if ( null === $arrival || 0 === $arrival['received_at'] ) {
     964                        esc_html_e( 'No test email has arrived yet.', 'nanopost-email-check' );
     965                } else {
     966                        /* translators: %s: date and time. */
     967                        echo esc_html( sprintf( __( 'Arrived %s', 'nanopost-email-check' ), self::format_time( $arrival['received_at'] ) ) ) . '<br />' . NanoPost_Email_Check_Canary::auth_labels( $arrival ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- auth_labels() escapes.
     968                }
     969                echo '</td></tr>';
     970                if ( $block['next_expected_by'] > 0 && 'on' === $block['watching'] ) {
     971                        echo '<tr><th scope="row">' . esc_html__( 'Next test', 'nanopost-email-check' ) . '</th><td>';
     972                        /* translators: %s: date and time. */
     973                        echo esc_html( sprintf( __( 'Expected by %s', 'nanopost-email-check' ), self::format_time( $block['next_expected_by'] ) ) );
     974                        echo '</td></tr>';
     975                }
     976                if ( 'on' === $block['watching'] ) {
     977                        echo '<tr><th scope="row">' . esc_html__( 'Alerts go to', 'nanopost-email-check' ) . '</th><td>';
     978                        $confirmed = array();
     979                        $pending   = array();
     980                        $failing   = array();
     981                        foreach ( $block['alerts_to'] as $entry ) {
     982                                if ( 'pending' === $entry['state'] ) {
     983                                        $pending[] = $entry['email'];
     984                                } elseif ( 'confirmed' === $entry['state'] ) {
     985                                        $confirmed[] = $entry['email'];
     986                                } elseif ( 'failing' === $entry['state'] ) {
     987                                        $confirmed[] = $entry['email'];
     988                                        $failing[]   = $entry['email'];
     989                                } else {
     990                                        /* translators: 1: email address, 2: "Open nanoPost to see." */
     991                                        $confirmed[] = sprintf( __( '%1$s (%2$s)', 'nanopost-email-check' ), $entry['email'], NanoPost_Email_Check_Canary::open_text() );
     992                                }
     993                        }
     994                        echo esc_html( implode( ', ', $confirmed ) );
     995                        foreach ( $failing as $email ) {
     996                                /* translators: %s: email address. */
     997                                echo '<br /><strong class="nanopost-email-check-failing">' . esc_html( sprintf( __( 'Email Canary: alerts do not reach %s.', 'nanopost-email-check' ), $email ) ) . '</strong>';
     998                        }
     999                        if ( array() !== $pending ) {
     1000                                /* translators: %s: email address. */
     1001                                echo '<br /><span class="description">' . esc_html( sprintf( __( 'Waiting for the confirm of %s. Alerts go there once it is confirmed.', 'nanopost-email-check' ), implode( ', ', $pending ) ) ) . '</span>';
     1002                                self::render_confirm_mail( $block, end( $pending ) );
     1003                        }
     1004                        self::render_change_form( $address );
     1005                        echo '</td></tr>';
     1006                }
     1007                echo '<tr><th scope="row">' . esc_html__( 'Webhook', 'nanopost-email-check' ) . '</th><td>' . esc_html( self::webhook_host() ) . '</td></tr>';
     1008                echo '</tbody></table>';
     1009
     1010                if ( NanoPost_Email_Check_Canary::is_old( $block ) && $block['observed_at'] > 0 ) {
     1011                        /* translators: %s: date and time. */
     1012                        echo '<p class="description">' . esc_html( sprintf( __( 'This is the state as of %s. nanoPost has sent no newer one.', 'nanopost-email-check' ), self::format_date( $block['observed_at'] ) ) ) . '</p>';
     1013                }
     1014                self::render_canary_actions( true, $block );
     1015        }
     1016
     1017        /**
     1018         * The text for a canary that stopped watching, by its reason.
     1019         *
     1020         * @param string $reason  stopped_reason.
     1021         * @param string $address Alert address.
     1022         * @return string
     1023         */
     1024        private static function stopped_text( $reason, $address ) {
     1025                switch ( $reason ) {
     1026                        case 'not_confirmed':
     1027                                /* translators: %s: email address. */
     1028                                return sprintf( __( 'Email Canary is off: %s was not confirmed.', 'nanopost-email-check' ), $address );
     1029                        case 'over_limit':
     1030                                return __( 'Email Canary is off: your nanoPost account has reached its limit of sites. Remove one in nanoPost, or ask us for more.', 'nanopost-email-check' );
     1031                        case 'mail_link':
     1032                                return __( 'Email Canary is off (asked for by email).', 'nanopost-email-check' );
     1033                        case 'deactivated':
     1034                                return __( 'Email Canary is off (the plugin was deactivated).', 'nanopost-email-check' );
     1035                        default:
     1036                                return __( 'Email Canary is off. Email Canary keeps the history for 30 days.', 'nanopost-email-check' );
     1037                }
     1038        }
     1039
     1040        /**
     1041         * The offer, when Email Canary is not on: the alert address, the button, and the
     1042         * consent text (nothing is sent before the click).
     1043         */
     1044        private static function render_offer() {
     1045                $email = NanoPost_Email_Check_Connection::alert_email();
     1046                if ( '' === $email ) {
     1047                        $user  = wp_get_current_user();
     1048                        $email = $user instanceof WP_User ? (string) $user->user_email : '';
     1049                }
     1050
     1051                echo '<p>' . esc_html__( 'Email Check can tell you that WordPress sent the email. Email Canary checks that it arrived, and emails you when it does not.', 'nanopost-email-check' ) . '</p>';
     1052                if ( ! self::site_is_public() ) {
     1053                        echo '<div class="notice notice-warning inline"><p>' . esc_html__( 'This site address is not public. Test emails from it may never leave the server.', 'nanopost-email-check' ) . '</p></div>';
     1054                }
     1055                self::render_turn_on_form( $email, __( 'Turn on Email Canary', 'nanopost-email-check' ) );
     1056                echo '<p class="description nanopost-email-check-consent">' . esc_html__( 'When you turn on Email Canary, this site sends to nanoPost: the site address, the alert address above, the site\'s time zone, the test interval, the webhook URL, and the versions of this plugin, WordPress and PHP. After each test it sends the test ID, the time, how the test started, and whether WordPress accepted the email, with the error text if it did not. nanoPost keeps the IP address of each call for 30 days.', 'nanopost-email-check' ) . '</p>';
     1057                echo '<p>' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_SENT, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'What is sent to nanoPost', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1058                echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::TERMS_URL, __( 'Terms', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1059                echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::PRIVACY_URL, __( 'Privacy', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1060                echo ' &middot; ' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_TURN_ON, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'How Email Canary works', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1061                echo '</p>';
     1062        }
     1063
     1064        /**
     1065         * "Send alerts to [address] [Turn on]".
     1066         *
     1067         * @param string $email Pre-filled address.
     1068         * @param string $label Button text.
     1069         */
     1070        private static function render_turn_on_form( $email, $label ) {
     1071                echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" class="nanopost-email-check-turn-on">';
     1072                echo '<input type="hidden" name="action" value="' . esc_attr( self::TURN_ON_ACTION ) . '" />';
     1073                wp_nonce_field( self::TURN_ON_ACTION );
     1074                echo '<p><label for="nanopost_email_check_alert_email">' . esc_html__( 'Send alerts to', 'nanopost-email-check' ) . '</label> ';
     1075                echo '<input type="email" required class="regular-text" id="nanopost_email_check_alert_email" name="alert_email" value="' . esc_attr( $email ) . '" autocomplete="email" /> ';
     1076                submit_button( $label, 'primary', 'submit', false );
     1077                echo '</p></form>';
     1078        }
     1079
     1080        /**
     1081         * Resend of the confirm email, or why it cannot go now.
     1082         *
     1083         * @param array  $block   Block.
     1084         * @param string $address The address waiting for its confirm.
     1085         */
     1086        private static function render_confirm_mail( $block, $address ) {
     1087                $mail = $block['confirm_mail'];
     1088                if ( null !== $mail && 'limited' === $mail['state'] ) {
     1089                        echo '<p>' . esc_html__( 'Email Canary: we cannot send another confirm email now. Try again tomorrow. Proof of arrival still works.', 'nanopost-email-check' ) . '</p>';
     1090                        return;
     1091                }
     1092                echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" class="nanopost-email-check-resend">';
     1093                echo '<input type="hidden" name="action" value="' . esc_attr( self::ALERT_EMAIL_ACTION ) . '" />';
     1094                echo '<input type="hidden" name="alert_email" value="' . esc_attr( $address ) . '" />';
     1095                wp_nonce_field( self::ALERT_EMAIL_ACTION );
     1096                if ( null !== $mail && $mail['next_allowed_at'] > time() ) {
     1097                        echo '<button type="button" class="button" disabled="disabled">' . esc_html__( 'Resend', 'nanopost-email-check' ) . '</button> ';
     1098                        /* translators: %s: date and time. */
     1099                        echo '<span class="description">' . esc_html( sprintf( __( 'You can resend it after %s.', 'nanopost-email-check' ), self::format_date( $mail['next_allowed_at'] ) ) ) . '</span>';
     1100                } else {
     1101                        submit_button( __( 'Resend', 'nanopost-email-check' ), 'secondary', 'submit', false );
     1102                }
     1103                echo '</form>';
     1104                if ( 'unconfirmed' === $block['watching'] ) {
     1105                        self::render_change_form( $address );
     1106                }
     1107        }
     1108
     1109        /**
     1110         * "Change" the alert address: a new confirm email goes to the new address.
     1111         *
     1112         * @param string $address Current address.
     1113         */
     1114        private static function render_change_form( $address ) {
     1115                echo '<details class="nanopost-email-check-change" style="display:inline-block;"><summary>' . esc_html__( 'Change', 'nanopost-email-check' ) . '</summary>';
    7051116                echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '">';
    706                 echo '<input type="hidden" name="action" value="' . esc_attr( self::DISCONNECT_ACTION ) . '" />';
    707                 wp_nonce_field( self::DISCONNECT_ACTION );
    708                 submit_button( __( 'Disconnect', 'nanopost-email-check' ), 'secondary', 'submit', false );
    709                 echo ' <span class="description">' . esc_html__( 'Test emails then go to the address in the settings again. nanoPost stops the check and deletes it after 30 days, unless this site connects again.', 'nanopost-email-check' ) . '</span>';
    710                 echo '</form>';
    711         }
    712 
    713         /**
    714          * A "Connect" button in its own small form.
    715          *
    716          * @param string $label Button text.
    717          */
    718         private static function connect_button( $label ) {
    719                 echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" style="display:inline;">';
    720                 echo '<input type="hidden" name="action" value="' . esc_attr( self::CONNECT_ACTION ) . '" />';
    721                 wp_nonce_field( self::CONNECT_ACTION );
    722                 submit_button( $label, 'primary', 'submit', false );
    723                 echo '</form>';
     1117                echo '<input type="hidden" name="action" value="' . esc_attr( self::ALERT_EMAIL_ACTION ) . '" />';
     1118                wp_nonce_field( self::ALERT_EMAIL_ACTION );
     1119                echo '<p><label for="nanopost_email_check_new_alert_email" class="screen-reader-text">' . esc_html__( 'New alert address', 'nanopost-email-check' ) . '</label>';
     1120                echo '<input type="email" required class="regular-text" id="nanopost_email_check_new_alert_email" name="alert_email" value="' . esc_attr( $address ) . '" autocomplete="email" /> ';
     1121                submit_button( __( 'Send confirm email', 'nanopost-email-check' ), 'secondary', 'submit', false );
     1122                echo '</p><p class="description">' . esc_html__( 'Only where alerts go changes. The new address must confirm by email.', 'nanopost-email-check' ) . ' ';
     1123                echo self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CHANGE, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'More', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1124                echo '</p></form></details>';
     1125        }
     1126
     1127        /**
     1128         * "Turn Email Canary off" and "Open in nanoPost".
     1129         *
     1130         * @param bool       $off   Show the off button.
     1131         * @param array|null $block Block, or null.
     1132         */
     1133        private static function render_canary_actions( $off, $block = null ) {
     1134                echo '<div class="nanopost-email-check-canary-actions">';
     1135                if ( $off ) {
     1136                        echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '">';
     1137                        echo '<input type="hidden" name="action" value="' . esc_attr( self::TURN_OFF_ACTION ) . '" />';
     1138                        wp_nonce_field( self::TURN_OFF_ACTION );
     1139                        submit_button( __( 'Turn Email Canary off', 'nanopost-email-check' ), 'secondary', 'submit', false );
     1140                        echo '</form>';
     1141                }
     1142                self::render_open_link( $block );
     1143                echo '</div>';
     1144        }
     1145
     1146        /**
     1147         * The small "Open in nanoPost" link.
     1148         *
     1149         * @param array|null $block Block, or null.
     1150         */
     1151        private static function render_open_link( $block ) {
     1152                $url = null !== $block && '' !== $block['check_url'] ? $block['check_url'] : NanoPost_Email_Check_Connection::get()['check_url'];
     1153                if ( '' !== $url ) {
     1154                        echo self::help_link( $url, __( 'Open in nanoPost', 'nanopost-email-check' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in help_link().
     1155                }
     1156        }
     1157
     1158        /**
     1159         * The webhook as the owner set it: its host, or "none".
     1160         *
     1161         * @return string
     1162         */
     1163        public static function webhook_host() {
     1164                $url = NanoPost_Email_Check_Settings::webhook_url();
     1165                if ( '' === $url ) {
     1166                        return __( 'none', 'nanopost-email-check' );
     1167                }
     1168                $host = (string) wp_parse_url( $url, PHP_URL_HOST );
     1169                return '' === $host ? $url : $host;
     1170        }
     1171
     1172        /**
     1173         * False for a site address that the internet cannot reach: localhost, a reserved
     1174         * name (.local, .test, .example, .invalid, .localhost), or a private or loopback IP.
     1175         *
     1176         * @return bool
     1177         */
     1178        public static function site_is_public() {
     1179                $host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
     1180                if ( '' === $host || 'localhost' === $host ) {
     1181                        return false;
     1182                }
     1183                if ( preg_match( '/\.(local|test|example|invalid|localhost)$/', $host ) ) {
     1184                        return false;
     1185                }
     1186                $ip = trim( $host, '[]' );
     1187                if ( false !== filter_var( $ip, FILTER_VALIDATE_IP ) ) {
     1188                        return false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
     1189                }
     1190                return true;
     1191        }
     1192
     1193        /**
     1194         * Styles for the SPF, DKIM and DMARC labels, and the script that asks for a newer
     1195         * state when the screen opens (while connected).
     1196         */
     1197        private static function enqueue_canary_assets() {
     1198                wp_register_style( 'nanopost-email-check-canary', false, array(), NANOPOST_EMAIL_CHECK_VERSION );
     1199                wp_enqueue_style( 'nanopost-email-check-canary' );
     1200                wp_add_inline_style( 'nanopost-email-check-canary', NanoPost_Email_Check_Canary::label_css() );
     1201
     1202                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     1203                        return;
     1204                }
     1205                wp_enqueue_script( 'nanopost-email-check-canary', plugins_url( 'assets/canary.js', NANOPOST_EMAIL_CHECK_FILE ), array(), NANOPOST_EMAIL_CHECK_VERSION, true );
     1206                wp_localize_script(
     1207                        'nanopost-email-check-canary',
     1208                        'nanopostEmailCheckCanary',
     1209                        array(
     1210                                'ajaxUrl' => admin_url( 'admin-ajax.php' ),
     1211                                'nonce'   => wp_create_nonce( self::STATE_ACTION ),
     1212                                'action'  => self::STATE_ACTION,
     1213                        )
     1214                );
     1215        }
     1216
     1217        /**
     1218         * What Email Canary said about one test, for its log row.
     1219         *
     1220         * @param NanoPost_Email_Check_Result $result Result.
     1221         * @return string HTML.
     1222         */
     1223        private static function canary_cell( $result ) {
     1224                if ( '' === $result->test_id ) {
     1225                        return '&mdash;';
     1226                }
     1227                if ( ! $result->passed ) {
     1228                        return esc_html__( '(no email to wait for)', 'nanopost-email-check' );
     1229                }
     1230                $canary  = $result->canary;
     1231                $arrival = isset( $canary['arrival'] ) ? (string) $canary['arrival'] : '';
     1232                if ( 'arrived' === $arrival ) {
     1233                        $time = isset( $canary['arrived_at'] ) ? (int) $canary['arrived_at'] : 0;
     1234                        $html = '<strong style="color:#00a32a;">' . esc_html__( 'Arrived', 'nanopost-email-check' ) . '</strong>';
     1235                        if ( $time > 0 ) {
     1236                                $html .= ' ' . esc_html( self::format_date( $time ) );
     1237                        }
     1238                        $results = array();
     1239                        foreach ( array( 'spf', 'dkim', 'dmarc' ) as $key ) {
     1240                                $results[ $key ] = isset( $canary[ $key ] ) ? (string) $canary[ $key ] : '';
     1241                        }
     1242                        return $html . '<br />' . NanoPost_Email_Check_Canary::auth_labels( $results );
     1243                }
     1244                if ( 'missing' === $arrival ) {
     1245                        $html   = '<strong style="color:#d63638;">' . esc_html__( 'Did not arrive', 'nanopost-email-check' ) . '</strong>';
     1246                        $reason = NanoPost_Email_Check_Canary::reason_text( isset( $canary['reason'] ) ? (string) $canary['reason'] : '' );
     1247                        if ( '' !== $reason ) {
     1248                                $html .= '<br />' . esc_html( $reason );
     1249                        }
     1250                        if ( ! empty( $canary['help_url'] ) ) {
     1251                                $html .= ' ' . self::help_link( (string) $canary['help_url'], __( 'What to do', 'nanopost-email-check' ) );
     1252                        }
     1253                        return $html;
     1254                }
     1255                if ( 'unknown' === $arrival ) {
     1256                        return esc_html__( 'Email Canary cannot tell now', 'nanopost-email-check' );
     1257                }
     1258                if ( '' === $arrival || 'waiting' === $arrival ) {
     1259                        return esc_html__( 'No result yet', 'nanopost-email-check' );
     1260                }
     1261                return esc_html( NanoPost_Email_Check_Canary::open_text() );
    7241262        }
    7251263
    … …  
    7321270                $stale  = NanoPost_Email_Check_Alerts::stale_since();
    7331271
    734                 echo '<h2>' . esc_html__( 'Status', 'nanopost-email-check' ) . '</h2>';
     1272                echo '<h2>' . esc_html__( 'Email Check', 'nanopost-email-check' ) . ' <span class="nanopost-email-check-tagline">' . esc_html__( 'Can this site send email?', 'nanopost-email-check' ) . '</span></h2>';
    7351273                echo '<table class="form-table" role="presentation"><tbody>';
    7361274
    737                 echo '<tr><th scope="row">' . esc_html__( 'Last check', 'nanopost-email-check' ) . '</th><td>';
     1275                echo '<tr><th scope="row">' . esc_html__( 'Last test', 'nanopost-email-check' ) . '</th><td>';
    7381276                if ( null === $latest ) {
    739                         esc_html_e( 'No check has run yet. Use "Send test now" below.', 'nanopost-email-check' );
     1277                        esc_html_e( 'No test has run yet. Use "Send test now" below.', 'nanopost-email-check' );
    7401278                } else {
    7411279                        printf(
    … …  
    7551293                $mailer = null === $latest ? '' : NanoPost_Email_Check_Mailer::describe( $latest->mailer );
    7561294                if ( '' !== $mailer ) {
    757                         echo '<tr><th scope="row">' . esc_html__( 'Mailer', 'nanopost-email-check' ) . '</th><td>' . esc_html( $mailer ) . '</td></tr>';
    758                 }
    759 
    760                 echo '<tr><th scope="row">' . esc_html__( 'Scheduled checks', 'nanopost-email-check' ) . '</th><td>';
     1295                        echo '<tr><th scope="row">' . esc_html__( 'Sends by', 'nanopost-email-check' ) . '</th><td>' . esc_html( $mailer ) . '</td></tr>';
     1296                }
     1297
     1298                echo '<tr><th scope="row">' . esc_html__( 'Schedule', 'nanopost-email-check' ) . '</th><td>';
    7611299                if ( null !== $stale ) {
    7621300                        echo '<strong style="color:#d63638;">' . esc_html__( 'Stopped', 'nanopost-email-check' ) . '</strong> &middot; ';
    … …  
    7761314                                sprintf(
    7771315                                        /* translators: 1: schedule name such as "Daily", 2: date and time. */
    778                                         __( '%1$s. Next check: %2$s', 'nanopost-email-check' ),
     1316                                        __( '%1$s. Next test: %2$s', 'nanopost-email-check' ),
    7791317                                        NanoPost_Email_Check_Settings::intervals()[ NanoPost_Email_Check_Settings::interval() ],
    7801318                                        self::format_time( $next )
    … …  
    7861324                echo '</td></tr>';
    7871325
     1326                // Each area repeats only what the owner set: where test emails go, and the webhook.
     1327                echo '<tr><th scope="row">' . esc_html__( 'Test emails go to', 'nanopost-email-check' ) . '</th><td>';
     1328                echo esc_html( NanoPost_Email_Check_Connection::canary_active() ? __( 'Email Canary', 'nanopost-email-check' ) : implode( ', ', NanoPost_Email_Check_Settings::recipients() ) );
     1329                echo '</td></tr>';
     1330                echo '<tr><th scope="row">' . esc_html__( 'Webhook', 'nanopost-email-check' ) . '</th><td>' . esc_html( self::webhook_host() ) . '</td></tr>';
     1331
    7881332                echo '</tbody></table>';
    7891333        }
    … …  
    7931337         */
    7941338        private static function render_send_form() {
    795                 echo '<h2>' . esc_html__( 'Send test now', 'nanopost-email-check' ) . '</h2>';
    796                 echo '<p>' . esc_html(
    797                         NanoPost_Email_Check_Connection::is_connected()
    798                                 ? __( 'Send one test email to nanoPost now, and see whether it arrives. Save any changed settings first.', 'nanopost-email-check' )
     1339                echo '<p class="description">' . esc_html(
     1340                        NanoPost_Email_Check_Connection::canary_active()
     1341                                ? __( 'Send one test email to Email Canary now, and see whether it arrives. Save any changed settings first.', 'nanopost-email-check' )
    7991342                                : sprintf(
    8001343                                        /* translators: %s: recipient email address. */
    … …  
    8161359                $results = NanoPost_Email_Check_Log::all();
    8171360
    818                 echo '<h2>' . esc_html__( 'Recent results', 'nanopost-email-check' ) . '</h2>';
     1361                echo '<h2>' . esc_html__( 'Recent tests', 'nanopost-email-check' ) . '</h2>';
    8191362
    8201363                if ( empty( $results ) ) {
    … …  
    8251368                echo '<table class="widefat striped"><thead><tr>';
    8261369                echo '<th scope="col">' . esc_html__( 'Time', 'nanopost-email-check' ) . '</th>';
    827                 echo '<th scope="col">' . esc_html__( 'Result', 'nanopost-email-check' ) . '</th>';
     1370                echo '<th scope="col">' . esc_html__( 'Email Check', 'nanopost-email-check' ) . '</th>';
     1371                $canary = array() !== array_filter( wp_list_pluck( $results, 'test_id' ) ) || NanoPost_Email_Check_Connection::is_connected();
     1372                if ( $canary ) {
     1373                        echo '<th scope="col">' . esc_html__( 'Email Canary', 'nanopost-email-check' ) . '</th>';
     1374                }
    8281375                echo '<th scope="col">' . esc_html__( 'Started by', 'nanopost-email-check' ) . '</th>';
    8291376                echo '<th scope="col">' . esc_html__( 'Sent to', 'nanopost-email-check' ) . '</th>';
    … …  
    8441391                        }
    8451392                        echo '</td>';
     1393                        if ( $canary ) {
     1394                                echo '<td class="nanopost-email-check-canary-cell">' . self::canary_cell( $result ) . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- canary_cell() escapes.
     1395                        }
    8461396                        echo '<td>' . esc_html( $result->trigger_label() ) . '</td>';
    847                         echo '<td>' . esc_html( $result->recipient ) . '</td>';
     1397                        echo '<td>' . esc_html( self::recipient_label( $result ) ) . '</td>';
    8481398                        echo '<td>';
    8491399                        if ( $result->passed ) {
    … …  
    8611411
    8621412        /**
     1413         * Where a test went, as the owner sees it: "Email Canary" for a test to the canary
     1414         * address (never the address itself), else the recipient.
     1415         *
     1416         * @param NanoPost_Email_Check_Result $result Result.
     1417         * @return string
     1418         */
     1419        public static function recipient_label( $result ) {
     1420                return '' !== $result->test_id ? __( 'Email Canary', 'nanopost-email-check' ) : $result->recipient;
     1421        }
     1422
     1423        /**
    8631424         * Pass or fail label.
    8641425         *
    … …  
    8681429        private static function status_badge( $passed ) {
    8691430                return $passed
    870                         ? '<strong style="color:#00a32a;">' . esc_html__( 'Passed', 'nanopost-email-check' ) . '</strong>'
    871                         : '<strong style="color:#d63638;">' . esc_html__( 'Failed', 'nanopost-email-check' ) . '</strong>';
     1431                        ? '<strong style="color:#00a32a;">' . esc_html__( 'Sent', 'nanopost-email-check' ) . '</strong>'
     1432                        : '<strong style="color:#d63638;">' . esc_html__( 'Could not send', 'nanopost-email-check' ) . '</strong>';
    8721433        }
    8731434
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-alerts.php

    r3820170 r3830659  
    111111         * Active alerts, keyed by a string that changes with each new problem.
    112112         *
    113          * @return array Key => type ("failed" or "stale").
     113         * @return array Key => type ("failed", "stale", or a problem of Email Canary:
     114         *               "canary-missing", "canary-auth").
    114115         */
    115116        public static function active() {
    … …  
    126127                }
    127128
     129                foreach ( self::canary_problems() as $problem ) {
     130                        $alerts[ $problem['key'] ] = 'canary-' . $problem['type'];
     131                }
     132
     133                $unconfirmed = self::unconfirmed();
     134                if ( null !== $unconfirmed ) {
     135                        $alerts[ 'canary-unconfirmed:' . $unconfirmed['confirm_by'] ] = 'canary-unconfirmed';
     136                }
     137
     138                foreach ( self::failing() as $email ) {
     139                        $alerts[ 'canary-failing:' . $email ] = 'canary-failing';
     140                }
     141
    128142                return $alerts;
     143        }
     144
     145        /**
     146         * The block while alerts are off until the owner confirms the alert address, or null.
     147         *
     148         * @return array|null
     149         */
     150        private static function unconfirmed() {
     151                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     152                        return null;
     153                }
     154                $block = NanoPost_Email_Check_Canary::block();
     155                return null !== $block && 'unconfirmed' === $block['watching'] ? $block : null;
     156        }
     157
     158        /**
     159         * Alert addresses that Email Canary's alerts do not reach (they bounce). The email
     160         * cannot tell the owner, so wp-admin does.
     161         *
     162         * @return string[]
     163         */
     164        private static function failing() {
     165                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     166                        return array();
     167                }
     168                $block = NanoPost_Email_Check_Canary::block();
     169                if ( null === $block ) {
     170                        return array();
     171                }
     172                $failing = array();
     173                foreach ( $block['alerts_to'] as $entry ) {
     174                        if ( 'failing' === $entry['state'] ) {
     175                                $failing[] = $entry['email'];
     176                        }
     177                }
     178                return $failing;
     179        }
     180
     181        /**
     182         * Problems that Email Canary emails about, from the stored state. None when the site is
     183         * not connected.
     184         *
     185         * @return array[]
     186         */
     187        private static function canary_problems() {
     188                if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
     189                        return array();
     190                }
     191                return NanoPost_Email_Check_Canary::problems( NanoPost_Email_Check_Canary::block() );
    129192        }
    130193
    … …  
    152215                        if ( 'failed' === $type ) {
    153216                                self::render_failed( $key );
     217                        } elseif ( 'stale' === $type ) {
     218                                self::render_stale( $key );
     219                        } elseif ( 'canary-unconfirmed' === $type ) {
     220                                self::render_unconfirmed( $key );
     221                        } elseif ( 'canary-failing' === $type ) {
     222                                self::render_failing( $key );
    154223                        } else {
    155                                 self::render_stale( $key );
     224                                self::render_canary( $key );
    156225                        }
    157226                }
    … …  
    167236
    168237                echo '<div id="nanopost-email-check-notice-failed" class="notice notice-error"><p>';
    169                 echo '<strong>' . esc_html__( 'Email check failed.', 'nanopost-email-check' ) . '</strong> ';
     238                echo '<strong>' . esc_html__( 'Email Check: this site could not send its test email.', 'nanopost-email-check' ) . '</strong> ';
    170239                echo esc_html(
    171240                        sprintf(
    … …  
    173242                                __( 'On %1$s, WordPress could not send the test email to %2$s. Your site may not be able to send email.', 'nanopost-email-check' ),
    174243                                NanoPost_Email_Check_Admin::format_date( $result->time ),
    175                                 $result->recipient
     244                                NanoPost_Email_Check_Admin::recipient_label( $result )
    176245                        )
    177246                );
    … …  
    188257
    189258        /**
     259         * Notice for a problem that Email Canary found, with the same core sentence as its
     260         * email.
     261         *
     262         * @param string $key Alert key.
     263         */
     264        private static function render_canary( $key ) {
     265                foreach ( self::canary_problems() as $problem ) {
     266                        if ( $key !== $problem['key'] ) {
     267                                continue;
     268                        }
     269                        echo '<div class="notice notice-error nanopost-email-check-notice-canary"><p>';
     270                        echo '<strong>' . esc_html( NanoPost_Email_Check_Canary::problem_text( $problem ) ) . '</strong>';
     271                        $reason = 'missing' === $problem['type'] ? NanoPost_Email_Check_Canary::reason_text( $problem['reason'] ) : __( 'Real emails from this site can be refused.', 'nanopost-email-check' );
     272                        if ( '' !== $reason ) {
     273                                echo ' ' . esc_html( $reason );
     274                        }
     275                        echo '</p>';
     276                        self::render_links( $key, '' === $problem['help_url'] ? array() : array( $problem['help_url'] => __( 'What to do', 'nanopost-email-check' ) ) );
     277                        echo '</div>';
     278                }
     279        }
     280
     281        /**
     282         * Notice for an alert address that Email Canary's alerts do not reach.
     283         *
     284         * @param string $key Alert key.
     285         */
     286        private static function render_failing( $key ) {
     287                $email = substr( $key, strlen( 'canary-failing:' ) );
     288                echo '<div class="notice notice-error nanopost-email-check-notice-failing"><p><strong>';
     289                /* translators: %s: email address. */
     290                echo esc_html( sprintf( __( 'Email Canary: alerts do not reach %s.', 'nanopost-email-check' ), $email ) ) . '</strong> ';
     291                echo esc_html__( 'Change the alert address on the plugin screen.', 'nanopost-email-check' );
     292                echo '</p>';
     293                self::render_links( $key, array( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CHANGE, NanoPost_Email_Check_Help::PLACE_NOTICE_CANARY ) => __( 'How to change it', 'nanopost-email-check' ) ) );
     294                echo '</div>';
     295        }
     296
     297        /**
     298         * Notice that stays until the confirm: alerts are off.
     299         *
     300         * @param string $key Alert key.
     301         */
     302        private static function render_unconfirmed( $key ) {
     303                $block = self::unconfirmed();
     304                echo '<div class="notice notice-warning nanopost-email-check-notice-unconfirmed"><p><strong>';
     305                echo esc_html__( 'Email Canary: alerts are OFF until you confirm.', 'nanopost-email-check' ) . '</strong> ';
     306                /* translators: %s: email address. */
     307                echo esc_html( sprintf( __( 'We sent an email to %s.', 'nanopost-email-check' ), NanoPost_Email_Check_Admin::alert_address( $block ) ) );
     308                echo '</p>';
     309                self::render_links( $key, array( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_CONFIRM, NanoPost_Email_Check_Help::PLACE_NOTICE_CANARY ) => __( 'Why confirm', 'nanopost-email-check' ) ) );
     310                echo '</div>';
     311        }
     312
     313        /**
    190314         * Notice for stopped scheduled checks.
    191315         *
    … …  
    194318        private static function render_stale( $key ) {
    195319                echo '<div id="nanopost-email-check-notice-stale" class="notice notice-warning"><p>';
    196                 echo '<strong>' . esc_html__( 'Email checks have stopped.', 'nanopost-email-check' ) . '</strong> ';
     320                echo '<strong>' . esc_html__( 'Email Check: scheduled tests have stopped.', 'nanopost-email-check' ) . '</strong> ';
    197321                echo esc_html(
    198322                        sprintf(
    199323                                /* translators: %s: date and time. */
    200                                 __( 'No scheduled email check has finished since %s. WP-Cron may not be running on this site, or the check may be stopping with a PHP error.', 'nanopost-email-check' ),
     324                                __( 'No scheduled test has finished since %s. WP-Cron may not be running on this site, or the test may be stopping with a PHP error.', 'nanopost-email-check' ),
    201325                                NanoPost_Email_Check_Admin::format_date( (int) self::stale_since() )
    202326                        )
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-app.php

    r3829352 r3830659  
    1010/**
    1111 * The one place that talks to the app (https://app.nanopo.st/api/v1). Every call needs a
    12  * connection the owner made, except the code exchange that makes it. A failed call never
     12 * connection the owner made, or the token of "Turn on Email Canary". A failed call never
    1313 * fails a test and never blocks a page: the timeouts are short, and a call runs only in
    1414 * WP-Cron, WP-CLI, or a request the owner started with a click.
    … …  
    2424
    2525        /**
     26         * Single WP-Cron event after an activation: "start" with trigger "activate".
     27         */
     28        const START_HOOK = 'nanopost_email_check_start';
     29
     30        /**
     31         * A deactivation stops the canary on each site until this many seconds have passed,
     32         * so a large network does not time out.
     33         */
     34        const DEACTIVATE_BUDGET = 20;
     35
     36        /**
    2637         * Recent failed calls, for support. Not autoloaded.
    2738         */
    … …  
    4253        public static function init() {
    4354                add_action( self::SYNC_HOOK, array( __CLASS__, 'sync_site' ) );
     55                add_action( self::START_HOOK, array( __CLASS__, 'start_after_activate' ) );
    4456                add_action( 'update_option_' . NanoPost_Email_Check_Settings::OPTION, array( __CLASS__, 'queue_sync' ), 10, 0 );
    4557                add_action( 'add_option_' . NanoPost_Email_Check_Settings::OPTION, array( __CLASS__, 'queue_sync' ), 10, 0 );
    … …  
    6072
    6173        /**
    62          * The page in the browser where the owner approves the connection.
    63          *
    64          * @param string $return_url Where the app sends the browser back to.
    65          * @param string $state      The plugin's random state.
    66          * @return string
    67          */
    68         public static function connect_url( $return_url, $state ) {
    69                 $base = preg_replace( '#/api/v\d+$#', '', self::base_url() );
    70                 return add_query_arg(
    71                         array(
    72                                 'site'   => rawurlencode( home_url() ),
    73                                 'return' => rawurlencode( $return_url ),
    74                                 'state'  => rawurlencode( $state ),
    75                         ),
    76                         $base . '/connect'
    77                 );
    78         }
    79 
    80         /**
    81          * Exchange the one-time code for a token, and store the connection.
    82          *
    83          * @param string $code The code from the app's redirect.
    84          * @return array Call result (see request()).
    85          */
    86         public static function exchange( $code ) {
    87                 $result = self::request(
    88                         'POST',
    89                         '/connect/exchange',
    90                         array(
    91                                 'code'     => (string) $code,
    92                                 'site_url' => home_url(),
    93                         ),
    94                         self::TIMEOUT_LONG,
    95                         false
    96                 );
    97                 if ( $result['ok'] && ! NanoPost_Email_Check_Connection::save_from_exchange( $result['body'] ) ) {
     74         * "Turn on Email Canary": make the canary with the token the plugin made, and store the
     75         * connection. The owner's click (admin-post) or WP-CLI.
     76         *
     77         * @param string $email Alert address.
     78         * @return array Call result (see request()). error "bad_answer" when the answer lacks
     79         *               a valid canary address; "connected" when a connection already exists.
     80         */
     81        public static function connect( $email ) {
     82                $token = NanoPost_Email_Check_Connection::begin( $email );
     83                if ( '' === $token ) {
     84                        return self::not_sent( 'connected' );
     85                }
     86                $body = self::site_body();
     87                $hash = md5( (string) wp_json_encode( $body ) );
     88
     89                $body['alert_email'] = (string) $email;
     90                $result              = self::request( 'POST', '/connect', $body, self::TIMEOUT_LONG, true, false, $token );
     91
     92                if ( $result['ok'] && ! NanoPost_Email_Check_Connection::save_from_connect( $result['body'], $hash ) ) {
    9893                        $result['ok']    = false;
    9994                        $result['error'] = 'bad_answer';
    100                         self::log( 'POST', '/connect/exchange', $result );
     95                        self::log( 'POST', '/connect', $result );
    10196                }
    10297                if ( $result['ok'] ) {
     98                        NanoPost_Email_Check_Canary::forget();
     99                        NanoPost_Email_Check_Canary::take( $result['body'] );
    103100                        NanoPost_Email_Check_Webhook::reset_check( 'wp_mail' );
     101                }
     102                return $result;
     103        }
     104
     105        /**
     106         * "Resend" (the same address) or "Change" (another address) of the alert address.
     107         *
     108         * @param string $email Address.
     109         * @return array Call result.
     110         */
     111        public static function set_alert_email( $email ) {
     112                $result = self::request( 'PUT', '/alert-email', array( 'email' => (string) $email ), self::TIMEOUT_SHORT, true, true );
     113                if ( $result['ok'] ) {
     114                        NanoPost_Email_Check_Canary::take( $result['body'] );
     115                        NanoPost_Email_Check_Connection::set_alert_email( $email );
     116                }
     117                return $result;
     118        }
     119
     120        /**
     121         * Stop watching: "Turn Email Canary off" (turn_off) or a deactivation (deactivate). The
     122         * token is kept, so "Turn on" needs no new confirm.
     123         *
     124         * @param string $trigger turn_off or deactivate.
     125         * @param int    $timeout Timeout in seconds.
     126         * @return array Call result.
     127         */
     128        public static function stop( $trigger, $timeout = self::TIMEOUT_SHORT ) {
     129                $result = self::request( 'POST', '/stop', array( 'trigger' => (string) $trigger ), $timeout, true, true );
     130                if ( $result['ok'] ) {
     131                        NanoPost_Email_Check_Canary::take( $result['body'] );
     132                }
     133                return $result;
     134        }
     135
     136        /**
     137         * Watch again: "Turn on" with the same address (turn_on) or an activation (activate).
     138         *
     139         * @param string $trigger turn_on or activate.
     140         * @return array Call result.
     141         */
     142        public static function start( $trigger ) {
     143                $result = self::request( 'POST', '/start', array( 'trigger' => (string) $trigger ), self::TIMEOUT_SHORT, true, true );
     144                if ( $result['ok'] ) {
     145                        NanoPost_Email_Check_Canary::take( $result['body'] );
    104146                        self::queue_sync();
    105147                }
    106148                return $result;
     149        }
     150
     151        /**
     152         * Deactivation: tell nanoPost to stop watching (best effort), so it sends no "missing"
     153         * alert for a plugin that was turned off on purpose. The token stays, so an activation
     154         * can start it again. The stop link in each alert email is the second way.
     155         *
     156         * @param int $deadline Unix time after which no more calls go out (network loops).
     157         */
     158        public static function stop_on_deactivate( $deadline ) {
     159                if ( time() >= $deadline || ! NanoPost_Email_Check_Connection::canary_active() ) {
     160                        return;
     161                }
     162                $block = NanoPost_Email_Check_Canary::block();
     163                if ( null !== $block && 'unconfirmed' === $block['watching'] ) {
     164                        return;
     165                }
     166                self::stop( 'deactivate', min( self::TIMEOUT_SHORT, max( 1, $deadline - time() ) ) );
     167        }
     168
     169        /**
     170         * Activation: book the "start" call in WP-Cron, not in the activation request.
     171         */
     172        public static function queue_start() {
     173                if ( NanoPost_Email_Check_Connection::is_connected() && false === wp_next_scheduled( self::START_HOOK ) ) {
     174                        wp_schedule_single_event( time(), self::START_HOOK );
     175                }
     176        }
     177
     178        /**
     179         * WP-Cron after an activation. nanoPost starts watching again only when the stop came
     180         * from a deactivation; for any other stop it answers 409, and the canary stays off.
     181         */
     182        public static function start_after_activate() {
     183                if ( NanoPost_Email_Check_Connection::is_connected() ) {
     184                        self::start( 'activate' );
     185                }
    107186        }
    108187
    … …  
    115194         */
    116195        public static function disconnect( $timeout = self::TIMEOUT_SHORT ) {
    117                 $result = NanoPost_Email_Check_Connection::is_connected()
     196                $token  = NanoPost_Email_Check_Connection::get()['token'];
     197                $result = '' !== $token && NanoPost_Email_Check_Connection::is_connected()
    118198                        ? self::request( 'DELETE', '/connection', null, $timeout, true, true )
    119199                        : self::not_sent( 'not_connected' );
    … …  
    160240
    161241        /**
     242         * Ask the app for the state of the canary ("GET /check"). For the plugin screen only,
     243         * through admin-ajax.
     244         *
     245         * @return array Call result.
     246         */
     247        public static function check_state() {
     248                return self::request( 'GET', '/check', null, self::TIMEOUT_SHORT );
     249        }
     250
     251        /**
    162252         * The body of the site settings call.
    163253         *
    … …  
    173263                        'interval'       => NanoPost_Email_Check_Settings::interval(),
    174264                        'webhook_url'    => '' === $webhook ? null : $webhook,
     265                        'timezone'       => wp_timezone_string(),
    175266                );
    176267        }
    … …  
    183274         */
    184275        public static function sync_site() {
    185                 if ( ! NanoPost_Email_Check_Connection::calls_allowed() ) {
     276                if ( ! NanoPost_Email_Check_Connection::calls_allowed() || ! NanoPost_Email_Check_Connection::canary_active() ) {
    186277                        return null;
    187278                }
    … …  
    227318         * @param int        $timeout Timeout in seconds.
    228319         * @param bool       $auth    Send the token. Calls with the token need a connection.
    229          * @param bool       $always  Make the call also while calls are stopped (disconnect).
     320         * @param bool       $always  Make the call also while Email Canary is off (the calls
     321         *                            that start, stop or end it, and the alert address).
     322         * @param string     $token   A token for a site with no connection yet (POST /connect).
    230323         * @return array ok (bool), code (HTTP status, 0 when no answer), error (stable code),
    231          *               message (the app's text, may be shown), body (decoded JSON array).
    232          */
    233         public static function request( $method, $path, $body = null, $timeout = self::TIMEOUT_SHORT, $auth = true, $always = false ) {
     324         *               message (the app's text, may be shown), body (decoded JSON array),
     325         *               date (the Date header of the answer, or '').
     326         */
     327        public static function request( $method, $path, $body = null, $timeout = self::TIMEOUT_SHORT, $auth = true, $always = false, $token = '' ) {
    234328                $headers = array(
    235329                        'Accept'           => 'application/json',
    … …  
    237331                );
    238332
    239                 if ( $auth ) {
     333                if ( $auth && '' !== $token ) {
     334                        $connection = NanoPost_Email_Check_Connection::get();
     335                        if ( '' !== NanoPost_Email_Check_Connection::too_old_message() ) {
     336                                return self::not_sent( 'too_old' );
     337                        }
     338                        if ( $connection['retry_until'] > time() ) {
     339                                return self::not_sent( 'waiting' );
     340                        }
     341                        $headers['Authorization'] = 'Bearer ' . $token;
     342                } elseif ( $auth ) {
    240343                        if ( ! NanoPost_Email_Check_Connection::is_connected() ) {
    241344                                return self::not_sent( 'not_connected' );
    242345                        }
    243                         if ( ! $always && ! NanoPost_Email_Check_Connection::calls_allowed() ) {
    244                                 return self::not_sent( 'stopped' );
     346                        // The disconnect always goes out: it ends the connection here too.
     347                        $ending = 'DELETE' === $method;
     348                        if ( ! $ending && '' !== NanoPost_Email_Check_Connection::too_old_message() ) {
     349                                return self::not_sent( 'too_old' );
     350                        }
     351                        if ( ! $ending && NanoPost_Email_Check_Connection::get()['retry_until'] > time() ) {
     352                                return self::not_sent( 'waiting' );
     353                        }
     354                        if ( ! $always && ! NanoPost_Email_Check_Connection::canary_active() && 'GET /check' !== $method . ' ' . $path ) {
     355                                return self::not_sent( 'canary_off' );
    245356                        }
    246357                        $headers['Authorization'] = 'Bearer ' . NanoPost_Email_Check_Connection::get()['token'];
    … …  
    268379                                'message' => $response->get_error_message(),
    269380                                'body'    => array(),
     381                                'date'    => '',
    270382                        );
    271383                        self::log( $method, $path, $result );
    … …  
    284396                        'message' => ! $ok && isset( $decoded['message'] ) && is_string( $decoded['message'] ) ? $decoded['message'] : '',
    285397                        'body'    => $decoded,
     398                        'date'    => (string) wp_remote_retrieve_header( $response, 'date' ),
    286399                );
    287400
    … …  
    292405                if ( 401 === $code && $auth ) {
    293406                        NanoPost_Email_Check_Connection::end( NanoPost_Email_Check_Connection::ENDED_REVOKED );
     407                } elseif ( 409 === $code && 'canary_stopped' === $result['error'] ) {
     408                        // Email Canary stopped watching (for example "Stop watching" in an alert email).
     409                        // The block says why; the next tests go to the recipient setting.
     410                        NanoPost_Email_Check_Canary::take( $decoded );
    294411                } elseif ( 410 === $code ) {
    295412                        NanoPost_Email_Check_Connection::mark_too_old( $result['message'] );
    … …  
    316433                        'message' => '',
    317434                        'body'    => array(),
     435                        'date'    => '',
    318436                );
    319437        }
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-cli.php

    r3829352 r3830659  
    6262         */
    6363        public function run() {
    64                 if ( NanoPost_Email_Check_Connection::is_connected() ) {
    65                         WP_CLI::log( __( 'Sending a test email to nanoPost ...', 'nanopost-email-check' ) );
     64                if ( NanoPost_Email_Check_Connection::canary_active() ) {
     65                        WP_CLI::log( __( 'Sending a test email to Email Canary ...', 'nanopost-email-check' ) );
    6666                } else {
    6767                        /* translators: %s: recipient email address. */
    … …  
    8585                        WP_CLI::success(
    8686                                sprintf(
    87                                         /* translators: 1: test ID, 2: URL of the check in nanoPost. */
    88                                         __( 'WordPress accepted the test email for nanoPost (test %1$s). See whether it arrived: %2$s', 'nanopost-email-check' ),
     87                                        /* translators: 1: test ID, 2: URL of the site's page in nanoPost. */
     88                                        __( 'WordPress accepted the test email for Email Canary (test %1$s). Its arrival shows on the plugin screen, and in nanoPost: %2$s', 'nanopost-email-check' ),
    8989                                        $results[0]->test_id,
    90                                         '' !== $check_url ? $check_url : __( 'the check page in nanoPost', 'nanopost-email-check' )
     90                                        '' !== $check_url ? $check_url : __( 'the site\'s page in nanoPost', 'nanopost-email-check' )
    9191                                )
    9292                        );
    9393                        if ( NanoPost_Email_Check_Result::ANNOUNCE_OK !== $results[0]->announce ) {
    94                                 WP_CLI::warning( __( 'The plugin could not tell nanoPost about this test. nanoPost still records the email if it arrives.', 'nanopost-email-check' ) );
     94                                WP_CLI::warning( __( 'The plugin could not tell Email Canary about this test. Email Canary still records the email if it arrives.', 'nanopost-email-check' ) );
    9595                        }
    9696                        return;
    … …  
    302302                }
    303303
     304                $block = $connected ? NanoPost_Email_Check_Canary::block() : null;
     305
    304306                return array(
    305307                        'connected'        => $connected,
     308                        'watching'         => null === $block ? null : $block['watching'],
     309                        'stopped_reason'   => null === $block || '' === $block['stopped_reason'] ? null : $block['stopped_reason'],
     310                        'arrival'          => null === $block ? null : $block['arrival'],
     311                        'last_arrival_at'  => null === $block || null === $block['last_arrival'] || 0 === $block['last_arrival']['received_at'] ? null : self::iso_time( $block['last_arrival']['received_at'] ),
     312                        'alerts_to'        => null === $block ? array() : $block['alerts_to'],
    306313                        'address'          => $connected ? $conn['address'] : null,
    307314                        'check_url'        => $connected && '' !== $conn['check_url'] ? $conn['check_url'] : null,
    … …  
    504511                $np = $data['nanopost'];
    505512                if ( ! $np['connected'] ) {
    506                         $text = __( 'Not connected. Connect on the plugin screen (Tools > Email Check).', 'nanopost-email-check' );
     513                        $text = __( 'Off. Turn it on on the plugin screen (Tools > Email Check).', 'nanopost-email-check' );
    507514                        if ( null !== $np['ended'] ) {
    508515                                $text .= ' ' . ( NanoPost_Email_Check_Connection::ENDED_SITE_CHANGED === $np['ended']
    509                                         ? __( 'The last connection ended because the site address changed.', 'nanopost-email-check' )
    510                                         : __( 'The last connection was ended in nanoPost.', 'nanopost-email-check' ) );
    511                         }
    512                 } else {
    513                         /* translators: %s: the check's email address in nanoPost. */
    514                         $text = sprintf( __( 'Connected. Test emails go to %s.', 'nanopost-email-check' ), $np['address'] );
     516                                        ? __( 'It turned off because the site address changed.', 'nanopost-email-check' )
     517                                        : __( 'nanoPost ended it for this site.', 'nanopost-email-check' ) );
     518                        }
     519                } else {
     520                        $states = array(
     521                                'on'          => __( 'On.', 'nanopost-email-check' ),
     522                                'unconfirmed' => __( 'Alerts are OFF until you confirm the alert address.', 'nanopost-email-check' ),
     523                                'over_limit'  => __( 'Alerts are OFF: the nanoPost account has reached its limit of sites.', 'nanopost-email-check' ),
     524                                'stopped'     => __( 'Off.', 'nanopost-email-check' ),
     525                        );
     526                        $text   = null === $np['watching'] ? __( 'On. No state from nanoPost yet.', 'nanopost-email-check' ) : ( isset( $states[ $np['watching'] ] ) ? $states[ $np['watching'] ] : NanoPost_Email_Check_Canary::open_text() );
     527                        if ( array() !== $np['alerts_to'] ) {
     528                                /* translators: %s: email addresses. */
     529                                $text .= ' ' . sprintf( __( 'Alerts go to %s.', 'nanopost-email-check' ), implode( ', ', wp_list_pluck( $np['alerts_to'], 'email' ) ) );
     530                        }
     531                        if ( null !== $np['last_arrival_at'] ) {
     532                                /* translators: %s: date and time. */
     533                                $text .= ' ' . sprintf( __( 'Last test email arrived %s.', 'nanopost-email-check' ), NanoPost_Email_Check_Admin::format_time( self::unix_time( $np['last_arrival_at'] ) ) );
     534                        }
    515535                        if ( 'stopped_too_old' === $np['calls'] ) {
    516536                                $text .= ' ' . $np['message'];
    … …  
    520540                        }
    521541                }
    522                 $add( __( 'nanoPost', 'nanopost-email-check' ), $text );
     542                $add( __( 'Email Canary', 'nanopost-email-check' ), $text );
    523543                if ( null !== $np['check_url'] ) {
    524                         $add( __( 'Check in nanoPost', 'nanopost-email-check' ), $np['check_url'] );
     544                        $add( __( 'Open in nanoPost', 'nanopost-email-check' ), $np['check_url'] );
    525545                }
    526546
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-connection.php

    r3829352 r3830659  
    5050                        'retry_until'  => 0,
    5151                        'site_hash'    => '',
     52                        'alert_email'  => '',
     53                        'pending_at'   => 0,
    5254                );
    5355
    … …  
    6163
    6264        /**
    63          * Store the connection from the answer to "Exchange the code".
    64          *
    65          * @param array $answer Decoded answer: token, check_id, address, check_url.
    66          * @return bool False when the answer lacks a token or a valid address.
    67          */
    68         public static function save_from_exchange( $answer ) {
     65         * Start "Turn on Email Canary": the token the plugin makes for the connect call. It is
     66         * stored before the call, so a retry sends the same token and the app does not make a
     67         * second canary. A pending token for the same address is used again; another address
     68         * gets a new token. Two requests at the same time get the same token (add_option()).
     69         *
     70         * @param string $email Alert address.
     71         * @return string The token, or '' while a connection with an address exists (except
     72         *                for a canary that was not confirmed, which connects again).
     73         */
     74        public static function begin( $email ) {
     75                $current = self::get();
     76                if ( '' !== $current['address'] && '' !== $current['token'] ) {
     77                        // A canary that was not confirmed in 24 hours: connect again with its token.
     78                        $block = NanoPost_Email_Check_Canary::block();
     79                        return null !== $block && 'not_confirmed' === $block['stopped_reason'] && $email === $current['alert_email'] ? $current['token'] : '';
     80                }
     81                if ( '' !== $current['token'] && $email === $current['alert_email'] ) {
     82                        return $current['token'];
     83                }
     84                $pending = array(
     85                        'token'       => self::new_token(),
     86                        'alert_email' => (string) $email,
     87                        'pending_at'  => time(),
     88                        'home_url'    => home_url(),
     89                );
     90                if ( '' === $current['token'] && ! add_option( self::OPTION, $pending, '', false ) ) {
     91                        // Another request stored a token a moment ago: use it when it is for this address.
     92                        wp_cache_delete( self::OPTION, 'options' );
     93                        $current = self::get();
     94                        if ( $email === $current['alert_email'] && '' !== $current['token'] ) {
     95                                return $current['token'];
     96                        }
     97                }
     98                update_option( self::OPTION, $pending, false );
     99                return $pending['token'];
     100        }
     101
     102        /**
     103         * A new token: 32 random bytes, base64url without padding (43 characters).
     104         *
     105         * @return string
     106         */
     107        public static function new_token() {
     108                return rtrim( strtr( base64_encode( random_bytes( 32 ) ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Encodes a random token, nothing is hidden.
     109        }
     110
     111        /**
     112         * Store the connection from the answer to "POST /connect". The token is the one the
     113         * plugin made (begin()).
     114         *
     115         * @param array  $answer    Decoded answer: address, check_url, check.
     116         * @param string $site_hash Hash of the site settings the connect call carried.
     117         * @return bool False when the answer lacks a valid address or no token is pending.
     118         */
     119        public static function save_from_connect( $answer, $site_hash ) {
    69120                $answer  = is_array( $answer ) ? $answer : array();
    70                 $token   = isset( $answer['token'] ) && is_string( $answer['token'] ) ? $answer['token'] : '';
     121                $current = self::get();
    71122                $address = isset( $answer['address'] ) && is_string( $answer['address'] ) ? $answer['address'] : '';
    72                 $url     = isset( $answer['check_url'] ) && is_string( $answer['check_url'] ) ? esc_url_raw( $answer['check_url'], array( 'https', 'http' ) ) : '';
    73 
    74                 if ( '' === $token || ! is_email( $address ) ) {
    75                         return false;
    76                 }
    77 
     123                if ( '' === $current['token'] || ! is_email( $address ) ) {
     124                        return false;
     125                }
    78126                update_option(
    79127                        self::OPTION,
    80128                        array(
    81                                 'token'        => $token,
     129                                'token'        => $current['token'],
    82130                                'address'      => $address,
    83                                 'check_id'     => isset( $answer['check_id'] ) && is_scalar( $answer['check_id'] ) ? (string) $answer['check_id'] : '',
    84                                 'check_url'    => $url,
     131                                'check_id'     => '',
     132                                'check_url'    => isset( $answer['check_url'] ) && is_string( $answer['check_url'] ) ? esc_url_raw( $answer['check_url'], array( 'https', 'http' ) ) : '',
    85133                                'home_url'     => home_url(),
    86134                                'connected_at' => time(),
     135                                'alert_email'  => $current['alert_email'],
     136                                'site_hash'    => (string) $site_hash,
    87137                        ),
    88138                        false
    … …  
    90140                delete_option( self::NOTICE_OPTION );
    91141                return true;
     142        }
     143
     144        /**
     145         * Store the link to the site's page in nanoPost, when it changed.
     146         *
     147         * @param string $url URL.
     148         */
     149        public static function set_check_url( $url ) {
     150                if ( self::get()['check_url'] !== $url ) {
     151                        self::update( array( 'check_url' => (string) $url ) );
     152                }
     153        }
     154
     155        /**
     156         * The alert address the owner last typed, or ''.
     157         *
     158         * @return string
     159         */
     160        public static function alert_email() {
     161                return self::get()['alert_email'];
     162        }
     163
     164        /**
     165         * Remember the alert address the owner typed (after "Change").
     166         *
     167         * @param string $email Address.
     168         */
     169        public static function set_alert_email( $email ) {
     170                self::update( array( 'alert_email' => (string) $email ) );
     171        }
     172
     173        /**
     174         * True while connected and Email Canary does not say it stopped watching. The test then
     175         * goes to the canary address. While stopped, it goes to the recipient setting, and the
     176         * plugin calls nanoPost only to start again or to end.
     177         *
     178         * @return bool
     179         */
     180        public static function canary_active() {
     181                if ( ! self::is_connected() ) {
     182                        return false;
     183                }
     184                $block = NanoPost_Email_Check_Canary::block();
     185                return null === $block || 'stopped' !== $block['watching'];
    92186        }
    93187
    … …  
    201295                }
    202296                wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
     297                NanoPost_Email_Check_Canary::forget();
    203298                NanoPost_Email_Check_Webhook::reset_check( 'wp_mail' );
    204299        }
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-help.php

    r3829352 r3830659  
    3333        const PLACE_NOTICE_STALE  = 'notice-stale';
    3434        const PLACE_CLI           = 'cli';
    35         const PLACE_CONNECTION    = 'connection';
     35        const PLACE_CANARY        = 'canary';
     36        const PLACE_NOTICE_CANARY = 'notice-canary';
    3637        const PLACE_PROOF         = 'proof';
    3738
    … …  
    107108
    108109        /**
    109          * What connecting to nanoPost does.
     110         * Anchors on the Email Canary help page. Agreed with the help pages; do not rename.
     111         */
     112        const CANARY_TURN_ON = 'turn-on-email-canary';
     113        const CANARY_CONFIRM = 'before-you-confirm-alerts-are-off';
     114        const CANARY_CHANGE  = 'change-the-alert-address';
     115        const CANARY_STOP    = 'stop-watching';
     116        const CANARY_SENT    = 'what-is-sent-to-nanopost';
     117
     118        /**
     119         * A part of the Email Canary help page.
    110120         *
    111          * @param string $place Where the link is shown.
     121         * @param string $anchor A CANARY_ constant.
     122         * @param string $place  Where the link is shown.
    112123         * @return string
    113124         */
    114         public static function connect( $place = '' ) {
    115                 return self::url( 'connect/', $place );
     125        public static function canary( $anchor, $place = '' ) {
     126                return self::url( 'canary/', $place, $anchor );
    116127        }
    117128
    … …  
    123134         */
    124135        public static function proof( $place = '' ) {
    125                 return self::url( 'connect/', $place, 'proof-of-arrival' );
     136                return self::canary( self::CANARY_TURN_ON, $place );
    126137        }
    127138
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-log.php

    r3818097 r3830659  
    2525                array_unshift( $log, $result->to_array() );
    2626                update_option( self::OPTION, array_slice( $log, 0, self::MAX ), false );
     27        }
     28
     29        /**
     30         * Add what Email Canary says about one test to its row. Does nothing when no row has
     31         * this test ID (for example, it is older than the last MAX results).
     32         *
     33         * @param string $test_id Test ID.
     34         * @param array  $fields  Values for the row's "canary" part.
     35         * @return bool True when a row changed.
     36         */
     37        public static function update_test( $test_id, $fields ) {
     38                if ( '' === $test_id ) {
     39                        return false;
     40                }
     41                $log = self::raw();
     42                foreach ( $log as $index => $row ) {
     43                        if ( is_array( $row ) && isset( $row['test_id'] ) && $test_id === $row['test_id'] ) {
     44                                $old                     = isset( $row['canary'] ) && is_array( $row['canary'] ) ? $row['canary'] : array();
     45                                $log[ $index ]['canary'] = array_merge( $old, (array) $fields );
     46                                if ( $log[ $index ]['canary'] === $old ) {
     47                                        return false;
     48                                }
     49                                update_option( self::OPTION, $log, false );
     50                                return true;
     51                        }
     52                }
     53                return false;
    2754        }
    2855
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-mail-check.php

    r3829352 r3830659  
    4444                $test_id    = '';
    4545
    46                 // While connected to nanoPost, the test goes only to the check's address, with a
    47                 // new test ID in it. The recipient setting is used again after a disconnect.
    48                 if ( NanoPost_Email_Check_Connection::is_connected() ) {
     46                // While Email Canary is on, the test goes only to the canary address, with a new
     47                // test ID in it. The recipient setting is used again when it is off.
     48                if ( NanoPost_Email_Check_Connection::canary_active() ) {
    4949                        $test_id    = NanoPost_Email_Check_Connection::new_test_id();
    5050                        $recipient  = NanoPost_Email_Check_Connection::test_address( $test_id );
    … …  
    8585                        $result->test_id  = $test_id;
    8686                        $call             = NanoPost_Email_Check_App::announce( $test_id, $time, $trigger, $sent, $sent ? '' : NanoPost_Email_Check_App::announce_error( $error, $cause ) );
    87                         $result->announce = $call['ok'] ? NanoPost_Email_Check_Result::ANNOUNCE_OK : ( 401 === $call['code'] ? NanoPost_Email_Check_Result::ANNOUNCE_ENDED : NanoPost_Email_Check_Result::ANNOUNCE_FAILED );
     87                        $result->announce = self::announce_outcome( $call );
     88                        if ( $call['ok'] && $sent ) {
     89                                NanoPost_Email_Check_Canary::schedule_fetch( $test_id, $call );
     90                        }
    8891                }
    8992
    9093                return $result;
     94        }
     95
     96        /**
     97         * What the announce answer means for the plugin's own webhook. A 2xx counts as "ok"
     98         * only when Email Canary watches (the block says "on"; an answer with no block, from
     99         * an app before the block, counts as watching). The block is stored.
     100         *
     101         * @param array $call Result of the announce call.
     102         * @return string A NanoPost_Email_Check_Result::ANNOUNCE_ constant.
     103         */
     104        private static function announce_outcome( $call ) {
     105                if ( 401 === $call['code'] ) {
     106                        return NanoPost_Email_Check_Result::ANNOUNCE_ENDED;
     107                }
     108                if ( 409 === $call['code'] ) {
     109                        return NanoPost_Email_Check_Result::ANNOUNCE_UNWATCHED;
     110                }
     111                if ( ! $call['ok'] ) {
     112                        return NanoPost_Email_Check_Result::ANNOUNCE_FAILED;
     113                }
     114                if ( NanoPost_Email_Check_Canary::take( $call['body'] ) && 'on' !== NanoPost_Email_Check_Canary::block()['watching'] ) {
     115                        return NanoPost_Email_Check_Result::ANNOUNCE_UNWATCHED;
     116                }
     117                return NanoPost_Email_Check_Result::ANNOUNCE_OK;
    91118        }
    92119
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-plugin.php

    r3829352 r3830659  
    2121                NanoPost_Email_Check_Webhook::init();
    2222                NanoPost_Email_Check_App::init();
     23                NanoPost_Email_Check_Canary::init();
    2324
    2425                if ( is_admin() ) {
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-result.php

    r3829352 r3830659  
    2828
    2929        /**
     30         * The app took the announce call, but Email Canary does not watch (not confirmed,
     31         * stopped, over the limit), so it sends no alert. The plugin's own webhook alerts.
     32         */
     33        const ANNOUNCE_UNWATCHED = 'unwatched';
     34
     35        /**
    3036         * Check id, from NanoPost_Email_Check_Check::id().
    3137         *
    … …  
    8894         * Whether nanoPost took the announce call for this test: ANNOUNCE_OK, ANNOUNCE_FAILED,
    8995         * ANNOUNCE_ENDED (nanoPost answered 401: the connection had ended, for example the
    90          * owner disconnected the site in nanoPost), or '' when no call was due (not connected).
     96         * owner disconnected the site in nanoPost), ANNOUNCE_UNWATCHED, or '' when no call was
     97         * due (not connected).
    9198         *
    9299         * @var string
    93100         */
    94101        public $announce = '';
     102
     103        /**
     104         * What Email Canary said about this test: arrival, arrived_at, spf, dkim, dmarc,
     105         * reason, help_url, fetched_at (see NanoPost_Email_Check_Canary::normalize_test()).
     106         * Empty until the plugin asked.
     107         *
     108         * @var array
     109         */
     110        public $canary = array();
    95111
    96112        /**
    … …  
    168184                        'test_id'   => $this->test_id,
    169185                        'announce'  => $this->announce,
     186                        'canary'    => $this->canary,
    170187                );
    171188        }
    … …  
    190207                                'test_id'   => '',
    191208                                'announce'  => '',
     209                                'canary'    => array(),
    192210                        )
    193211                );
    … …  
    195213                $result           = new self( $data['check'], $data['trigger'], $data['passed'], $data['error'], $data['recipient'], $data['time'], $data['mailer'] );
    196214                $result->test_id  = is_string( $data['test_id'] ) && preg_match( '/^[a-z0-9]{12}$/', $data['test_id'] ) ? $data['test_id'] : '';
    197                 $result->announce = in_array( $data['announce'], array( self::ANNOUNCE_OK, self::ANNOUNCE_FAILED, self::ANNOUNCE_ENDED ), true ) ? $data['announce'] : '';
     215                $result->announce = in_array( $data['announce'], array( self::ANNOUNCE_OK, self::ANNOUNCE_FAILED, self::ANNOUNCE_ENDED, self::ANNOUNCE_UNWATCHED ), true ) ? $data['announce'] : '';
     216                $result->canary   = is_array( $data['canary'] ) ? $data['canary'] : array();
    198217                return $result;
    199218        }
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-scheduler.php

    r3829352 r3830659  
    4040                if ( ! is_multisite() || ! $network_wide || wp_is_large_network() ) {
    4141                        self::ensure_scheduled();
     42                        NanoPost_Email_Check_App::queue_start();
    4243                        return;
    4344                }
    … …  
    4647                        switch_to_blog( $site_id );
    4748                        self::ensure_scheduled();
     49                        NanoPost_Email_Check_App::queue_start();
    4850                        restore_current_blog();
    4951                }
    … …  
    99101         */
    100102        public static function deactivate( $network_wide = false ) {
     103                $deadline = time() + NanoPost_Email_Check_App::DEACTIVATE_BUDGET;
    101104                if ( ! is_multisite() || ! $network_wide ) {
     105                        NanoPost_Email_Check_App::stop_on_deactivate( $deadline );
    102106                        wp_clear_scheduled_hook( self::HOOK );
    103107                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
     108                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     109                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    104110                        return;
    105111                }
    … …  
    107113                foreach ( self::site_ids() as $site_id ) {
    108114                        switch_to_blog( $site_id );
     115                        NanoPost_Email_Check_App::stop_on_deactivate( $deadline );
    109116                        wp_clear_scheduled_hook( self::HOOK );
    110117                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
     118                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     119                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    111120                        restore_current_blog();
    112121                }
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-webhook.php

    r3829352 r3830659  
    5858                // The screen shows "Reconnect", and the next test goes to the recipient setting,
    5959                // where a real mail problem alerts as usual.
     60                // Not when Email Canary does not watch (ANNOUNCE_UNWATCHED: not confirmed, stopped,
     61                // over the limit): it sends no alert then, so the plugin's webhook alerts as when
     62                // not connected.
    6063                if ( in_array( $result->announce, array( NanoPost_Email_Check_Result::ANNOUNCE_OK, NanoPost_Email_Check_Result::ANNOUNCE_ENDED ), true ) && ! $result->passed ) {
    6164                        if ( '' !== $previous ) {
    … …  
    107110        public static function payload( NanoPost_Email_Check_Result $result, $url ) {
    108111                $site = home_url();
    109 
     112                $name = self::site_name();
     113                $mail = 'wp_mail' === $result->check;
     114
     115                // "[Email Check]" says which part speaks; "[Email Canary]" messages come from nanoPost.
    110116                if ( $result->passed ) {
    111                         /* translators: %s: site address. */
    112                         $text = sprintf( __( 'Email check passed again on %s.', 'nanopost-email-check' ), $site );
     117                        $text = $mail
     118                                /* translators: %s: site address without https://. */
     119                                ? sprintf( __( '[Email Check] %s can send its test email again.', 'nanopost-email-check' ), $name )
     120                                /* translators: %s: site address without https://. */
     121                                : sprintf( __( '[Email Check] A check passed again on %s.', 'nanopost-email-check' ), $name );
    113122                } else {
    114                         /* translators: 1: site address, 2: error text. */
    115                         $text = sprintf( __( 'Email check failed on %1$s: %2$s', 'nanopost-email-check' ), $site, self::escape_markup( $result->error_message() ) );
     123                        $text = $mail
     124                                /* translators: 1: site address without https://, 2: error text. */
     125                                ? sprintf( __( '[Email Check] %1$s could not send its test email: %2$s', 'nanopost-email-check' ), $name, self::escape_markup( $result->error_message() ) )
     126                                /* translators: 1: site address without https://, 2: error text. */
     127                                : sprintf( __( '[Email Check] A check failed on %1$s: %2$s', 'nanopost-email-check' ), $name, self::escape_markup( $result->error_message() ) );
    116128                }
    117129
    … …  
    130142
    131143        /**
     144         * The site address without the scheme and trailing slash, as messages name the site.
     145         *
     146         * @return string
     147         */
     148        public static function site_name() {
     149                return (string) preg_replace( '#^https?://#i', '', untrailingslashit( home_url() ) );
     150        }
     151
     152        /**
    132153         * Post a sample alert, marked as a test, to the saved URL. The delivery is recorded,
    133154         * marked as a test; the status per check is not changed, so a test neither starts an
    … …  
    162183        public static function test_payload( $url ) {
    163184                $site = home_url();
    164                 /* translators: %s: site address. */
    165                 $text = sprintf( __( 'Test alert from Email Check on %s. This is only a test of the webhook. No check failed.', 'nanopost-email-check' ), $site );
     185                /* translators: %s: site address without https://. */
     186                $text = sprintf( __( '[Email Check] Test alert from %s. This is only a test of the webhook. No check failed.', 'nanopost-email-check' ), self::site_name() );
    166187
    167188                if ( self::is_google_chat( $url ) ) {
  • nanopost-email-check/trunk/nanopost-email-check.php

    r3829352 r3830659  
    44 * Plugin URI:        https://nanopo.st/email-check-plugin/
    55 * Description:       Check that your site can still send email. Sends a scheduled test email, logs each result and warns you when a check fails.
    6  * Version:           1.7.0
     6 * Version:           1.8.0
    77 * Requires at least: 6.4
    88 * Requires PHP:      7.4
    … …  
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.7.0' );
     20define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.8.0' );
    2121define( 'NANOPOST_EMAIL_CHECK_FILE', __FILE__ );
    2222define( 'NANOPOST_EMAIL_CHECK_DIR', plugin_dir_path( __FILE__ ) );
    … …  
    3535require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-connection.php';
    3636require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-app.php';
     37require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-canary.php';
    3738require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-admin.php';
    3839
  • nanopost-email-check/trunk/readme.txt

    r3829352 r3830659  
    55Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.7.0
     7Stable tag: 1.8.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    4949* **Mailer shown:** each result records how the site sends: SMTP server and port, PHP `mail()`, or another plugin, and which plugin set it up. When PHP `mail()` fails because the sendmail program is missing, the error says so.
    5050* **Multisite:** each site runs its own checks, with its own settings, log and notices.
    51 * **Optional proof of arrival:** connect the site to nanoPost (opt-in) to see whether each test email arrived, with its SPF, DKIM and DMARC results, and to get an alert from nanoPost when a test does not arrive.
    52 * **No outside requests** unless you set a webhook or connect to nanoPost.
     51* **Optional Email Canary:** turn it on (opt-in) to see whether each test email arrived at nanoPost, with its SPF, DKIM and DMARC results, in the log, in a notice and in Site Health, and to get an email when a test email does not arrive.
     52* **No outside requests** unless you set a webhook or turn on Email Canary.
    5353
    5454= What a pass means =
    … …  
    6060The plugin screen is at **Tools > Email Check**.
    6161
    62 Free. No paid version.
     62The plugin is free, with no paid version. Email Canary, the optional service, is free in this early period.
    6363
    6464Full documentation: [nanopo.st/email-check-plugin](https://nanopo.st/email-check-plugin/). Source code: [GitHub](https://github.com/nanoPost/nanopost-email-check).
    … …  
    9696= Does the plugin send data to nanoPost? =
    9797
    98 Only if you connect the site to nanoPost on the plugin screen. Without a connection, nothing is sent to nanoPost. See "External services" below for what a connected site sends.
    99 
    100 = What does connecting to nanoPost add? =
    101 
    102 A connected site sends each test email to nanoPost instead of the recipient you set. nanoPost shows whether the email arrived, with its SPF, DKIM and DMARC results ("Send test now" shows this proof of arrival on the plugin screen), and alerts you when a test does not arrive. Connecting is optional, and the plugin works in full without it. Disconnect on the plugin screen at any time: test emails then go to your recipient again.
     98Only if you turn on Email Canary on the plugin screen. Until then, nothing is sent to nanoPost. See "External services" below for what the site sends while Email Canary is on.
     99
     100= What does Email Canary add? =
     101
     102Email Check can tell you that WordPress accepted the test email. Email Canary checks that it arrived. While it is on, each test email goes to Email Canary instead of the recipient you set. The plugin screen then shows, for each test, whether it arrived at nanoPost, with its SPF, DKIM and DMARC results, and Email Canary emails you when a test email does not arrive or does not pass. Type an alert address and click "Turn on Email Canary": a test email goes out at once, and you confirm the address by email. Until you confirm, alerts are off. Email Canary is optional, and the plugin works in full without it. Turn it off on the plugin screen at any time: test emails then go to your recipient again.
    103103
    104104= The notice says the checks have stopped. What does that mean? =
    … …  
    141141== External services ==
    142142
    143 The plugin makes no outside requests unless you set a webhook URL or connect the site to nanoPost.
    144 
    145 = nanoPost app (optional, after you connect) =
    146 
    147 nanoPost (app.nanopo.st) is a mail monitoring service run by the plugin's authors. The plugin calls it only after you click "Connect to nanoPost" on the plugin screen and approve the site in your browser. Until then, nothing is sent. When you disconnect, or delete the plugin, the plugin tells nanoPost and stops all calls.
    148 
    149 While connected, the site sends:
    150 
    151 * when you connect: a one-time approval code, the site address, and the address of its admin area, so nanoPost can send your browser back after you approve;
    152 * the site address, the versions of the plugin, WordPress and PHP, the check interval, and the webhook URL if you set one: after you connect, and again when one of them changes;
    153 * for each test: a test ID, the time it was sent, what started it (the schedule, a user or WP-CLI), whether `wp_mail()` succeeded, and its error text (at most 500 characters; it can contain host names or email addresses);
    154 * when you press "Send test now": a request for that test's result, while the plugin screen is open (at most 2 minutes);
     143The plugin makes no outside requests unless you set a webhook URL or turn on Email Canary.
     144
     145= nanoPost Email Canary (optional, after you turn it on) =
     146
     147nanoPost Email Canary (app.nanopo.st) is a service run by the plugin's authors. It checks that each test email arrives, and emails you when one does not. The plugin calls it only after you type an alert address and click "Turn on Email Canary" on the plugin screen. Until then, nothing is sent. When you turn it off, deactivate the plugin or delete it, the plugin tells Email Canary.
     148
     149While Email Canary is on, the site sends:
     150
     151* when you turn it on: the site address, the alert address you typed, the site's time zone, the test interval, the webhook URL if you set one, the versions of the plugin, WordPress and PHP, and a random key that the plugin makes for this site;
     152* the same site settings again when one of them changes;
     153* for each test: a test ID, the time it was sent, what started it (the schedule, a user or WP-CLI), whether WordPress accepted the email, and its error text if it did not (at most 500 characters; it can contain host names or email addresses);
     154* a request for the result of each test, after it is due, and for the state of the site when you open the plugin screen;
     155* when you resend the confirm email or change the alert address: that address;
     156* when you turn it off or on, deactivate or activate the plugin: which of these happened;
    155157* the plugin version in every call.
    156158
    157 The test emails go to nanoPost's address for the site's check instead of the recipient you set. nanoPost receives each test email and its headers, as for any email. nanoPost keeps the IP address of each call for 30 days. The plugin sends no user data, no other email, and no other settings.
     159The test emails go to Email Canary's address for the site instead of the recipient you set. Email Canary receives each test email and its headers, as for any email. nanoPost keeps the IP address of each call for 30 days. The plugin sends no user names or other user data, no other email, and no other settings. The alert address is sent only as you typed it.
    158160
    159161* Terms of use: https://nanopo.st/terms/#the-app-appnanopost
    … …  
    166168The "Send test alert" button on the plugin screen, or `wp email-check test-alert`, sends one test message to the webhook URL, only when you use it. Its text says that it is a test and that no check failed. It has the same fields as a failure message, plus `"test": true`, so a receiver can handle it like a real alert or skip it. Real alerts do not have the `test` field. For a Google Chat URL, only the text line is sent. Example:
    167169
    168 `{"text": "Test alert from Email Check on https://example.com. This is only a test of the webhook. No check failed.", "site": "https://example.com", "status": "fail", "error": "Test alert. No check failed.", "time": "2026-11-24T09:30:00+00:00", "test": true}`
     170`{"text": "[Email Check] Test alert from example.com. This is only a test of the webhook. No check failed.", "site": "https://example.com", "status": "fail", "error": "Test alert. No check failed.", "time": "2026-11-24T09:30:00+00:00", "test": true}`
    169171
    170172The setup help names these services. The plugin sends to them only when you enter one of their webhook URLs:
    … …  
    183185== Screenshots ==
    184186
    185 1. The plugin screen at Tools > Email Check: status, settings, and the result log.
    186 2. The notice in wp-admin when a check fails, with a link to decode the error.
    187 3. The notice when scheduled checks have stopped running.
    188 4. An alert in a chat app when a check fails, sent through the optional webhook.
     1871. The plugin screen at Tools > Email Check: the Email Check and Email Canary areas, the settings and the recent tests.
     1882. The notice in wp-admin when the site could not send its test email, with a link to decode the error.
     1893. The notice when scheduled tests have stopped running.
     1904. An alert in a chat app when the site could not send its test email, sent through the optional webhook.
    189191
    190192== Changelog ==
     193
     194= 1.8.0 =
     195* Email Canary replaces "Connect to nanoPost": type an alert address and turn it on from the plugin screen. No sign-up in the browser. A test email goes out at once, with its proof of arrival.
     196* The plugin screen, notices and Site Health show the state from Email Canary: whether each test email arrived, with SPF, DKIM and DMARC as labels, also for scheduled tests.
     197* Resend or change the alert address, and turn Email Canary off and on again, on the plugin screen.
     198* A deactivation tells Email Canary to stop watching; an activation starts it again.
     199* While Email Canary is not watching (before the confirm, or off), the plugin's own webhook alerts as when it is not on.
     200* The plugin's webhook messages start with "[Email Check]" and name the site, for example "[Email Check] example.com could not send its test email: ...". The JSON fields do not change.
     201* Sites connected with 1.7 stay on, with no new confirm.
    191202
    192203= 1.7.0 =
  • nanopost-email-check/trunk/uninstall.php

    r3829352 r3830659  
    5656        delete_option( 'nanopost_email_check_app_log' );
    5757        wp_clear_scheduled_hook( 'nanopost_email_check_site_sync' );
     58        delete_option( 'nanopost_email_check_canary' );
     59        delete_transient( 'nanopost_email_check_canary_lock' );
     60        wp_unschedule_hook( 'nanopost_email_check_fetch_test' );
     61        wp_clear_scheduled_hook( 'nanopost_email_check_start' );
    5862        delete_option( 'nanopost_email_check_settings' );
    5963        delete_option( 'nanopost_email_check_log' );
Note: See TracChangeset for help on using the changeset viewer.