Plugin Directory

Changeset 3832012


Ignore:
Timestamp:
11/28/2026 06:57:58 PM (14 hours ago)
Author:
nanopost
Message:

Update to version 1.9.0 from GitHub

Location:
nanopost-email-check
Files:
2 added
20 edited
1 copied

Legend:

Unmodified
Added
Removed
  • nanopost-email-check/tags/1.9.0/includes/class-nanopost-email-check-admin.php

    r3830659 r3832012  
    10501050
    10511051                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                self::render_key_line();
    10521053                if ( ! self::site_is_public() ) {
    10531054                        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>';
    10541055                }
    10551056                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 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. If an account key is set, the site sends it once, to add the site to that nanoPost account.', 'nanopost-email-check' ) . '</p>';
    10571058                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().
    10581059                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().
    … …  
    10601061                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().
    10611062                echo '</p>';
     1063        }
     1064
     1065        /**
     1066         * One line while an account key is set in wp-config.php: what it does on this site, or
     1067         * why it does not.
     1068         */
     1069        private static function render_key_line() {
     1070                if ( '' === NanoPost_Email_Check_Key::constant_key() ) {
     1071                        return;
     1072                }
     1073                $link  = ' ' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_KEY, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'How the account key works', 'nanopost-email-check' ) );
     1074                $error = NanoPost_Email_Check_Key::error();
     1075                if ( '' !== $error ) {
     1076                        echo '<div class="notice notice-error inline nanopost-email-check-key"><p>' . esc_html( NanoPost_Email_Check_Key::error_text( $error ) ) . $link . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $link is escaped in help_link().
     1077                        return;
     1078                }
     1079                switch ( NanoPost_Email_Check_Key::blocked_reason() ) {
     1080                        case 'not_public':
     1081                                $text = __( 'An account key is set in wp-config.php, but this site address is not public, so the key does not turn Email Canary on by itself.', 'nanopost-email-check' );
     1082                                break;
     1083                        case 'not_main_site':
     1084                                $text = __( 'An account key is set in wp-config.php. On a network it turns Email Canary on only for the main site. For this site, use WP-CLI: wp email-check connect --key=<key> --url=<site address>', 'nanopost-email-check' );
     1085                                break;
     1086                        case 'tried':
     1087                        case 'ended':
     1088                                $text = __( 'An account key is set in wp-config.php. It does not turn Email Canary on by itself again for this site address. Use the button below.', 'nanopost-email-check' );
     1089                                break;
     1090                        default:
     1091                                $text = __( 'An account key is set in wp-config.php. Email Canary turns on by itself in a moment, with alerts to the address of the nanoPost account.', 'nanopost-email-check' );
     1092                }
     1093                echo '<p class="description nanopost-email-check-key">' . esc_html( $text ) . $link . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $link is escaped in help_link().
    10621094        }
    10631095
  • nanopost-email-check/tags/1.9.0/includes/class-nanopost-email-check-app.php

    r3830659 r3832012  
    7575         * connection. The owner's click (admin-post) or WP-CLI.
    7676         *
    77          * @param string $email Alert address.
     77         * With an account key in place of the address (#58), the canary watches at once, with
     78         * alerts to the account's address. Exactly one of the two is sent. The key is sent in
     79         * this call only, and never stored.
     80         *
     81         * @param string $email Alert address, or '' with a key.
     82         * @param string $key   Account key, or ''.
    7883         * @return array Call result (see request()). error "bad_answer" when the answer lacks
    7984         *               a valid canary address; "connected" when a connection already exists.
    8085         */
    81         public static function connect( $email ) {
    82                 $token = NanoPost_Email_Check_Connection::begin( $email );
     86        public static function connect( $email, $key = '' ) {
     87                $identity = '' !== $key ? 'key:' . NanoPost_Email_Check_Key::fingerprint( $key ) : (string) $email;
     88                $token    = NanoPost_Email_Check_Connection::begin( $identity, '' === $key ? (string) $email : '' );
    8389                if ( '' === $token ) {
    8490                        return self::not_sent( 'connected' );
    … …  
    8793                $hash = md5( (string) wp_json_encode( $body ) );
    8894
    89                 $body['alert_email'] = (string) $email;
    90                 $result              = self::request( 'POST', '/connect', $body, self::TIMEOUT_LONG, true, false, $token );
     95                if ( '' !== $key ) {
     96                        $body['account_key'] = (string) $key;
     97                } else {
     98                        $body['alert_email'] = (string) $email;
     99                }
     100                $result = self::request( 'POST', '/connect', $body, self::TIMEOUT_LONG, true, false, $token );
    91101
    92102                if ( $result['ok'] && ! NanoPost_Email_Check_Connection::save_from_connect( $result['body'], $hash ) ) {
  • nanopost-email-check/tags/1.9.0/includes/class-nanopost-email-check-cli.php

    r3830659 r3832012  
    178178                        WP_CLI::halt( $code );
    179179                }
     180        }
     181
     182        /**
     183         * Turn Email Canary on, with an account key or an alert address.
     184         *
     185         * With a key, Email Canary watches at once, with alerts to the address of the nanoPost
     186         * account. With an address, a confirm email goes there, and alerts are off until it is
     187         * confirmed. Either way, one test email goes out at once. The key is sent in this call
     188         * only, and never stored.
     189         *
     190         * Exits with 0 when Email Canary is on (or waits for the confirm), and 1 when it is not,
     191         * or when its alerts are off because the nanoPost account has no room for this site.
     192         *
     193         * ## OPTIONS
     194         *
     195         * [--key=<key>]
     196         * : An account key from nanoPost (Settings > Profile > Account key). With no --key and no --email,
     197         * the key in the NANOPOST_EMAIL_CHECK_KEY constant is used.
     198         *
     199         * [--email=<email>]
     200         * : An alert address, in place of a key.
     201         *
     202         * ## EXAMPLES
     203         *
     204         *     $ wp email-check connect --key=npk_...
     205         *     Success: Email Canary is on. Alerts go to owner@example.com.
     206         *
     207         *     $ wp email-check connect --email=owner@example.com
     208         *     Success: Email Canary is on. We sent a confirm email to owner@example.com. Alerts are off until it is confirmed.
     209         *
     210         * @param array $args       Positional arguments (none).
     211         * @param array $assoc_args Options.
     212         */
     213        public function connect( $args, $assoc_args ) {
     214                $email = isset( $assoc_args['email'] ) ? sanitize_email( (string) $assoc_args['email'] ) : '';
     215                $key   = isset( $assoc_args['key'] ) ? trim( (string) $assoc_args['key'] ) : '';
     216                if ( isset( $assoc_args['email'] ) && isset( $assoc_args['key'] ) ) {
     217                        WP_CLI::error( __( 'Use --key or --email, not both.', 'nanopost-email-check' ) );
     218                }
     219                if ( isset( $assoc_args['email'] ) && ! is_email( $email ) ) {
     220                        WP_CLI::error( __( 'The --email value is not a valid email address.', 'nanopost-email-check' ) );
     221                }
     222                if ( '' === $email && '' === $key ) {
     223                        $key = NanoPost_Email_Check_Key::constant_key();
     224                        if ( '' === $key ) {
     225                                WP_CLI::error( __( 'Give --key or --email, or set NANOPOST_EMAIL_CHECK_KEY in wp-config.php.', 'nanopost-email-check' ) );
     226                        }
     227                }
     228                if ( NanoPost_Email_Check_Connection::is_connected() ) {
     229                        WP_CLI::error( __( 'Email Canary is already set up for this site. Turn it off on the plugin screen first.', 'nanopost-email-check' ) );
     230                }
     231
     232                $call = '' !== $key ? NanoPost_Email_Check_Key::connect( $key ) : NanoPost_Email_Check_App::connect( $email );
     233                if ( ! $call['ok'] ) {
     234                        if ( in_array( $call['error'], array( NanoPost_Email_Check_Key::ERROR_INVALID, NanoPost_Email_Check_Key::ERROR_EXISTS ), true ) ) {
     235                                WP_CLI::error( NanoPost_Email_Check_Key::error_text( $call['error'] ) );
     236                        }
     237                        $detail = '' !== $call['message'] ? $call['message'] : ( 0 === $call['code'] ? $call['error'] : 'HTTP ' . $call['code'] );
     238                        /* translators: %s: error text or HTTP status. */
     239                        WP_CLI::error( sprintf( __( 'Email Canary did not turn on: %s', 'nanopost-email-check' ), $detail ) );
     240                }
     241
     242                $block   = NanoPost_Email_Check_Canary::block();
     243                $results = NanoPost_Email_Check_Plugin::run_checks( NanoPost_Email_Check_Result::TRIGGER_CLI );
     244                if ( ! empty( $results ) && '' !== $results[0]->test_id ) {
     245                        /* translators: %s: test ID. */
     246                        WP_CLI::log( sprintf( __( 'A test email went out (test %s).', 'nanopost-email-check' ), $results[0]->test_id ) );
     247                }
     248                $address = NanoPost_Email_Check_Admin::alert_address( $block );
     249                if ( null !== $block && 'over_limit' === $block['watching'] ) {
     250                        WP_CLI::error( __( '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' ) );
     251                }
     252                if ( null !== $block && 'unconfirmed' === $block['watching'] ) {
     253                        /* translators: %s: email address. */
     254                        WP_CLI::success( sprintf( __( 'Email Canary is on. We sent a confirm email to %s. Alerts are off until it is confirmed.', 'nanopost-email-check' ), $address ) );
     255                        return;
     256                }
     257                /* translators: %s: email address. */
     258                WP_CLI::success( sprintf( __( 'Email Canary is on. Alerts go to %s.', 'nanopost-email-check' ), $address ) );
    180259        }
    181260
  • nanopost-email-check/tags/1.9.0/includes/class-nanopost-email-check-connection.php

    r3830659 r3832012  
    5151                        'site_hash'    => '',
    5252                        'alert_email'  => '',
     53                        'pending_for'  => '',
    5354                        'pending_at'   => 0,
    5455                );
    … …  
    6566         * Start "Turn on Email Canary": the token the plugin makes for the connect call. It is
    6667         * 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.
     68         * second canary. A pending token for the same address (or the same key) is used again;
     69         * another one gets a new token. Two requests at the same time get the same token
     70         * (add_option()).
     71         *
     72         * @param string $identity The alert address, or "key:" and the key's fingerprint.
     73         * @param string $email    The alert address to keep, or '' for a key.
    7174         * @return string The token, or '' while a connection with an address exists (except
    7275         *                for a canary that was not confirmed, which connects again).
    7376         */
    74         public static function begin( $email ) {
     77        public static function begin( $identity, $email = null ) {
     78                $email   = null === $email ? (string) $identity : (string) $email;
    7579                $current = self::get();
     80                // A connection stored by 1.8.0 has no pending_for: its alert address stands in.
     81                if ( '' === $current['pending_for'] ) {
     82                        $current['pending_for'] = $current['alert_email'];
     83                }
    7684                if ( '' !== $current['address'] && '' !== $current['token'] ) {
    7785                        // A canary that was not confirmed in 24 hours: connect again with its token.
    7886                        $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'] ) {
     87                        return null !== $block && 'not_confirmed' === $block['stopped_reason'] && $identity === $current['pending_for'] ? $current['token'] : '';
     88                }
     89                if ( '' !== $current['token'] && $identity === $current['pending_for'] ) {
    8290                        return $current['token'];
    8391                }
    8492                $pending = array(
    8593                        'token'       => self::new_token(),
    86                         'alert_email' => (string) $email,
     94                        'alert_email' => $email,
     95                        'pending_for' => (string) $identity,
    8796                        'pending_at'  => time(),
    8897                        'home_url'    => home_url(),
    … …  
    92101                        wp_cache_delete( self::OPTION, 'options' );
    93102                        $current = self::get();
    94                         if ( $email === $current['alert_email'] && '' !== $current['token'] ) {
     103                        if ( $identity === $current['pending_for'] && '' !== $current['token'] ) {
    95104                                return $current['token'];
    96105                        }
    … …  
    134143                                'connected_at' => time(),
    135144                                'alert_email'  => $current['alert_email'],
     145                                'pending_for'  => $current['pending_for'],
    136146                                'site_hash'    => (string) $site_hash,
    137147                        ),
  • nanopost-email-check/tags/1.9.0/includes/class-nanopost-email-check-help.php

    r3830659 r3832012  
    115115        const CANARY_STOP    = 'stop-watching';
    116116        const CANARY_SENT    = 'what-is-sent-to-nanopost';
     117        const CANARY_KEY     = 'turn-on-with-an-account-key';
    117118
    118119        /**
  • nanopost-email-check/tags/1.9.0/includes/class-nanopost-email-check-plugin.php

    r3830659 r3832012  
    2222                NanoPost_Email_Check_App::init();
    2323                NanoPost_Email_Check_Canary::init();
     24                NanoPost_Email_Check_Key::init();
    2425
    2526                if ( is_admin() ) {
  • nanopost-email-check/tags/1.9.0/includes/class-nanopost-email-check-scheduler.php

    r3830659 r3832012  
    107107                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
    108108                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     109                        wp_clear_scheduled_hook( NanoPost_Email_Check_Key::HOOK );
    109110                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    110111                        return;
    … …  
    117118                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
    118119                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     120                        wp_clear_scheduled_hook( NanoPost_Email_Check_Key::HOOK );
    119121                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    120122                        restore_current_blog();
  • nanopost-email-check/tags/1.9.0/nanopost-email-check.php

    r3830659 r3832012  
    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.8.0
     6 * Version:           1.9.0
    77 * Requires at least: 6.4
    88 * Requires PHP:      7.4
    … …  
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.8.0' );
     20define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.9.0' );
    2121define( 'NANOPOST_EMAIL_CHECK_FILE', __FILE__ );
    2222define( 'NANOPOST_EMAIL_CHECK_DIR', plugin_dir_path( __FILE__ ) );
    … …  
    3636require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-app.php';
    3737require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-canary.php';
     38require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-key.php';
    3839require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-admin.php';
    3940
  • nanopost-email-check/tags/1.9.0/readme.txt

    r3830659 r3832012  
    55Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.8.0
     7Stable tag: 1.9.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    121121`run` and `test-alert` have no once-a-minute limit. On multisite, choose the site with `--url`. For details, run `wp help email-check`, or read [settings in code and WP-CLI](https://nanopo.st/email-check-plugin/code-and-cli/).
    122122
     123= Can I turn on Email Canary on many sites at once? =
     124
     125Yes, with an account key. Make one in nanoPost (Settings > Profile > Account key), then add `define( 'NANOPOST_EMAIL_CHECK_KEY', '...' );` to `wp-config.php`, or run `wp email-check connect --key=...` on each site. Email Canary then turns on by itself, with alerts to your nanoPost account's address and no confirm email. The key only adds sites; it cannot read your account. It turns a site on once: not again after you turn it off, and not on a copy of the site at another address or on a site address that is not public. On a multisite network, the constant turns on only the main site; use WP-CLI with `--url` for the other sites.
     126
    123127= Does it work on multisite? =
    124128
    … …  
    145149= nanoPost Email Canary (optional, after you turn it on) =
    146150
    147 nanoPost 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.
     151nanoPost 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, or after you set a nanoPost account key in `wp-config.php` or run `wp email-check connect`. Until then, nothing is sent. When you turn it off, deactivate the plugin or delete it, the plugin tells Email Canary.
    148152
    149153While Email Canary is on, the site sends:
    150154
    151155* 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;
     156* if you set a nanoPost account key (in `wp-config.php` or with WP-CLI) in place of an alert address: that key, once, in the call that turns Email Canary on. The plugin does not store it;
    152157* the same site settings again when one of them changes;
    153158* 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);
    … …  
    191196
    192197== Changelog ==
     198
     199= 1.9.0 =
     200* Account key for agencies: `NANOPOST_EMAIL_CHECK_KEY` in `wp-config.php` turns Email Canary on by itself, once per site address, with alerts to the nanoPost account's address and no confirm email. The key is never stored.
     201* New WP-CLI command `wp email-check connect`, with `--key` or `--email`.
    193202
    194203= 1.8.0 =
  • nanopost-email-check/tags/1.9.0/uninstall.php

    r3830659 r3832012  
    6060        wp_unschedule_hook( 'nanopost_email_check_fetch_test' );
    6161        wp_clear_scheduled_hook( 'nanopost_email_check_start' );
     62        delete_option( 'nanopost_email_check_key_state' );
     63        wp_clear_scheduled_hook( 'nanopost_email_check_key_connect' );
    6264        delete_option( 'nanopost_email_check_settings' );
    6365        delete_option( 'nanopost_email_check_log' );
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-admin.php

    r3830659 r3832012  
    10501050
    10511051                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                self::render_key_line();
    10521053                if ( ! self::site_is_public() ) {
    10531054                        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>';
    10541055                }
    10551056                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 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. If an account key is set, the site sends it once, to add the site to that nanoPost account.', 'nanopost-email-check' ) . '</p>';
    10571058                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().
    10581059                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().
    … …  
    10601061                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().
    10611062                echo '</p>';
     1063        }
     1064
     1065        /**
     1066         * One line while an account key is set in wp-config.php: what it does on this site, or
     1067         * why it does not.
     1068         */
     1069        private static function render_key_line() {
     1070                if ( '' === NanoPost_Email_Check_Key::constant_key() ) {
     1071                        return;
     1072                }
     1073                $link  = ' ' . self::help_link( NanoPost_Email_Check_Help::canary( NanoPost_Email_Check_Help::CANARY_KEY, NanoPost_Email_Check_Help::PLACE_CANARY ), __( 'How the account key works', 'nanopost-email-check' ) );
     1074                $error = NanoPost_Email_Check_Key::error();
     1075                if ( '' !== $error ) {
     1076                        echo '<div class="notice notice-error inline nanopost-email-check-key"><p>' . esc_html( NanoPost_Email_Check_Key::error_text( $error ) ) . $link . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $link is escaped in help_link().
     1077                        return;
     1078                }
     1079                switch ( NanoPost_Email_Check_Key::blocked_reason() ) {
     1080                        case 'not_public':
     1081                                $text = __( 'An account key is set in wp-config.php, but this site address is not public, so the key does not turn Email Canary on by itself.', 'nanopost-email-check' );
     1082                                break;
     1083                        case 'not_main_site':
     1084                                $text = __( 'An account key is set in wp-config.php. On a network it turns Email Canary on only for the main site. For this site, use WP-CLI: wp email-check connect --key=<key> --url=<site address>', 'nanopost-email-check' );
     1085                                break;
     1086                        case 'tried':
     1087                        case 'ended':
     1088                                $text = __( 'An account key is set in wp-config.php. It does not turn Email Canary on by itself again for this site address. Use the button below.', 'nanopost-email-check' );
     1089                                break;
     1090                        default:
     1091                                $text = __( 'An account key is set in wp-config.php. Email Canary turns on by itself in a moment, with alerts to the address of the nanoPost account.', 'nanopost-email-check' );
     1092                }
     1093                echo '<p class="description nanopost-email-check-key">' . esc_html( $text ) . $link . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $link is escaped in help_link().
    10621094        }
    10631095
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-app.php

    r3830659 r3832012  
    7575         * connection. The owner's click (admin-post) or WP-CLI.
    7676         *
    77          * @param string $email Alert address.
     77         * With an account key in place of the address (#58), the canary watches at once, with
     78         * alerts to the account's address. Exactly one of the two is sent. The key is sent in
     79         * this call only, and never stored.
     80         *
     81         * @param string $email Alert address, or '' with a key.
     82         * @param string $key   Account key, or ''.
    7883         * @return array Call result (see request()). error "bad_answer" when the answer lacks
    7984         *               a valid canary address; "connected" when a connection already exists.
    8085         */
    81         public static function connect( $email ) {
    82                 $token = NanoPost_Email_Check_Connection::begin( $email );
     86        public static function connect( $email, $key = '' ) {
     87                $identity = '' !== $key ? 'key:' . NanoPost_Email_Check_Key::fingerprint( $key ) : (string) $email;
     88                $token    = NanoPost_Email_Check_Connection::begin( $identity, '' === $key ? (string) $email : '' );
    8389                if ( '' === $token ) {
    8490                        return self::not_sent( 'connected' );
    … …  
    8793                $hash = md5( (string) wp_json_encode( $body ) );
    8894
    89                 $body['alert_email'] = (string) $email;
    90                 $result              = self::request( 'POST', '/connect', $body, self::TIMEOUT_LONG, true, false, $token );
     95                if ( '' !== $key ) {
     96                        $body['account_key'] = (string) $key;
     97                } else {
     98                        $body['alert_email'] = (string) $email;
     99                }
     100                $result = self::request( 'POST', '/connect', $body, self::TIMEOUT_LONG, true, false, $token );
    91101
    92102                if ( $result['ok'] && ! NanoPost_Email_Check_Connection::save_from_connect( $result['body'], $hash ) ) {
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-cli.php

    r3830659 r3832012  
    178178                        WP_CLI::halt( $code );
    179179                }
     180        }
     181
     182        /**
     183         * Turn Email Canary on, with an account key or an alert address.
     184         *
     185         * With a key, Email Canary watches at once, with alerts to the address of the nanoPost
     186         * account. With an address, a confirm email goes there, and alerts are off until it is
     187         * confirmed. Either way, one test email goes out at once. The key is sent in this call
     188         * only, and never stored.
     189         *
     190         * Exits with 0 when Email Canary is on (or waits for the confirm), and 1 when it is not,
     191         * or when its alerts are off because the nanoPost account has no room for this site.
     192         *
     193         * ## OPTIONS
     194         *
     195         * [--key=<key>]
     196         * : An account key from nanoPost (Settings > Profile > Account key). With no --key and no --email,
     197         * the key in the NANOPOST_EMAIL_CHECK_KEY constant is used.
     198         *
     199         * [--email=<email>]
     200         * : An alert address, in place of a key.
     201         *
     202         * ## EXAMPLES
     203         *
     204         *     $ wp email-check connect --key=npk_...
     205         *     Success: Email Canary is on. Alerts go to owner@example.com.
     206         *
     207         *     $ wp email-check connect --email=owner@example.com
     208         *     Success: Email Canary is on. We sent a confirm email to owner@example.com. Alerts are off until it is confirmed.
     209         *
     210         * @param array $args       Positional arguments (none).
     211         * @param array $assoc_args Options.
     212         */
     213        public function connect( $args, $assoc_args ) {
     214                $email = isset( $assoc_args['email'] ) ? sanitize_email( (string) $assoc_args['email'] ) : '';
     215                $key   = isset( $assoc_args['key'] ) ? trim( (string) $assoc_args['key'] ) : '';
     216                if ( isset( $assoc_args['email'] ) && isset( $assoc_args['key'] ) ) {
     217                        WP_CLI::error( __( 'Use --key or --email, not both.', 'nanopost-email-check' ) );
     218                }
     219                if ( isset( $assoc_args['email'] ) && ! is_email( $email ) ) {
     220                        WP_CLI::error( __( 'The --email value is not a valid email address.', 'nanopost-email-check' ) );
     221                }
     222                if ( '' === $email && '' === $key ) {
     223                        $key = NanoPost_Email_Check_Key::constant_key();
     224                        if ( '' === $key ) {
     225                                WP_CLI::error( __( 'Give --key or --email, or set NANOPOST_EMAIL_CHECK_KEY in wp-config.php.', 'nanopost-email-check' ) );
     226                        }
     227                }
     228                if ( NanoPost_Email_Check_Connection::is_connected() ) {
     229                        WP_CLI::error( __( 'Email Canary is already set up for this site. Turn it off on the plugin screen first.', 'nanopost-email-check' ) );
     230                }
     231
     232                $call = '' !== $key ? NanoPost_Email_Check_Key::connect( $key ) : NanoPost_Email_Check_App::connect( $email );
     233                if ( ! $call['ok'] ) {
     234                        if ( in_array( $call['error'], array( NanoPost_Email_Check_Key::ERROR_INVALID, NanoPost_Email_Check_Key::ERROR_EXISTS ), true ) ) {
     235                                WP_CLI::error( NanoPost_Email_Check_Key::error_text( $call['error'] ) );
     236                        }
     237                        $detail = '' !== $call['message'] ? $call['message'] : ( 0 === $call['code'] ? $call['error'] : 'HTTP ' . $call['code'] );
     238                        /* translators: %s: error text or HTTP status. */
     239                        WP_CLI::error( sprintf( __( 'Email Canary did not turn on: %s', 'nanopost-email-check' ), $detail ) );
     240                }
     241
     242                $block   = NanoPost_Email_Check_Canary::block();
     243                $results = NanoPost_Email_Check_Plugin::run_checks( NanoPost_Email_Check_Result::TRIGGER_CLI );
     244                if ( ! empty( $results ) && '' !== $results[0]->test_id ) {
     245                        /* translators: %s: test ID. */
     246                        WP_CLI::log( sprintf( __( 'A test email went out (test %s).', 'nanopost-email-check' ), $results[0]->test_id ) );
     247                }
     248                $address = NanoPost_Email_Check_Admin::alert_address( $block );
     249                if ( null !== $block && 'over_limit' === $block['watching'] ) {
     250                        WP_CLI::error( __( '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' ) );
     251                }
     252                if ( null !== $block && 'unconfirmed' === $block['watching'] ) {
     253                        /* translators: %s: email address. */
     254                        WP_CLI::success( sprintf( __( 'Email Canary is on. We sent a confirm email to %s. Alerts are off until it is confirmed.', 'nanopost-email-check' ), $address ) );
     255                        return;
     256                }
     257                /* translators: %s: email address. */
     258                WP_CLI::success( sprintf( __( 'Email Canary is on. Alerts go to %s.', 'nanopost-email-check' ), $address ) );
    180259        }
    181260
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-connection.php

    r3830659 r3832012  
    5151                        'site_hash'    => '',
    5252                        'alert_email'  => '',
     53                        'pending_for'  => '',
    5354                        'pending_at'   => 0,
    5455                );
    … …  
    6566         * Start "Turn on Email Canary": the token the plugin makes for the connect call. It is
    6667         * 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.
     68         * second canary. A pending token for the same address (or the same key) is used again;
     69         * another one gets a new token. Two requests at the same time get the same token
     70         * (add_option()).
     71         *
     72         * @param string $identity The alert address, or "key:" and the key's fingerprint.
     73         * @param string $email    The alert address to keep, or '' for a key.
    7174         * @return string The token, or '' while a connection with an address exists (except
    7275         *                for a canary that was not confirmed, which connects again).
    7376         */
    74         public static function begin( $email ) {
     77        public static function begin( $identity, $email = null ) {
     78                $email   = null === $email ? (string) $identity : (string) $email;
    7579                $current = self::get();
     80                // A connection stored by 1.8.0 has no pending_for: its alert address stands in.
     81                if ( '' === $current['pending_for'] ) {
     82                        $current['pending_for'] = $current['alert_email'];
     83                }
    7684                if ( '' !== $current['address'] && '' !== $current['token'] ) {
    7785                        // A canary that was not confirmed in 24 hours: connect again with its token.
    7886                        $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'] ) {
     87                        return null !== $block && 'not_confirmed' === $block['stopped_reason'] && $identity === $current['pending_for'] ? $current['token'] : '';
     88                }
     89                if ( '' !== $current['token'] && $identity === $current['pending_for'] ) {
    8290                        return $current['token'];
    8391                }
    8492                $pending = array(
    8593                        'token'       => self::new_token(),
    86                         'alert_email' => (string) $email,
     94                        'alert_email' => $email,
     95                        'pending_for' => (string) $identity,
    8796                        'pending_at'  => time(),
    8897                        'home_url'    => home_url(),
    … …  
    92101                        wp_cache_delete( self::OPTION, 'options' );
    93102                        $current = self::get();
    94                         if ( $email === $current['alert_email'] && '' !== $current['token'] ) {
     103                        if ( $identity === $current['pending_for'] && '' !== $current['token'] ) {
    95104                                return $current['token'];
    96105                        }
    … …  
    134143                                'connected_at' => time(),
    135144                                'alert_email'  => $current['alert_email'],
     145                                'pending_for'  => $current['pending_for'],
    136146                                'site_hash'    => (string) $site_hash,
    137147                        ),
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-help.php

    r3830659 r3832012  
    115115        const CANARY_STOP    = 'stop-watching';
    116116        const CANARY_SENT    = 'what-is-sent-to-nanopost';
     117        const CANARY_KEY     = 'turn-on-with-an-account-key';
    117118
    118119        /**
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-plugin.php

    r3830659 r3832012  
    2222                NanoPost_Email_Check_App::init();
    2323                NanoPost_Email_Check_Canary::init();
     24                NanoPost_Email_Check_Key::init();
    2425
    2526                if ( is_admin() ) {
  • nanopost-email-check/trunk/includes/class-nanopost-email-check-scheduler.php

    r3830659 r3832012  
    107107                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
    108108                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     109                        wp_clear_scheduled_hook( NanoPost_Email_Check_Key::HOOK );
    109110                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    110111                        return;
    … …  
    117118                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::SYNC_HOOK );
    118119                        wp_clear_scheduled_hook( NanoPost_Email_Check_App::START_HOOK );
     120                        wp_clear_scheduled_hook( NanoPost_Email_Check_Key::HOOK );
    119121                        wp_unschedule_hook( NanoPost_Email_Check_Canary::FETCH_HOOK );
    120122                        restore_current_blog();
  • nanopost-email-check/trunk/nanopost-email-check.php

    r3830659 r3832012  
    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.8.0
     6 * Version:           1.9.0
    77 * Requires at least: 6.4
    88 * Requires PHP:      7.4
    … …  
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.8.0' );
     20define( 'NANOPOST_EMAIL_CHECK_VERSION', '1.9.0' );
    2121define( 'NANOPOST_EMAIL_CHECK_FILE', __FILE__ );
    2222define( 'NANOPOST_EMAIL_CHECK_DIR', plugin_dir_path( __FILE__ ) );
    … …  
    3636require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-app.php';
    3737require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-canary.php';
     38require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-key.php';
    3839require_once NANOPOST_EMAIL_CHECK_DIR . 'includes/class-nanopost-email-check-admin.php';
    3940
  • nanopost-email-check/trunk/readme.txt

    r3830659 r3832012  
    55Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.8.0
     7Stable tag: 1.9.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    121121`run` and `test-alert` have no once-a-minute limit. On multisite, choose the site with `--url`. For details, run `wp help email-check`, or read [settings in code and WP-CLI](https://nanopo.st/email-check-plugin/code-and-cli/).
    122122
     123= Can I turn on Email Canary on many sites at once? =
     124
     125Yes, with an account key. Make one in nanoPost (Settings > Profile > Account key), then add `define( 'NANOPOST_EMAIL_CHECK_KEY', '...' );` to `wp-config.php`, or run `wp email-check connect --key=...` on each site. Email Canary then turns on by itself, with alerts to your nanoPost account's address and no confirm email. The key only adds sites; it cannot read your account. It turns a site on once: not again after you turn it off, and not on a copy of the site at another address or on a site address that is not public. On a multisite network, the constant turns on only the main site; use WP-CLI with `--url` for the other sites.
     126
    123127= Does it work on multisite? =
    124128
    … …  
    145149= nanoPost Email Canary (optional, after you turn it on) =
    146150
    147 nanoPost 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.
     151nanoPost 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, or after you set a nanoPost account key in `wp-config.php` or run `wp email-check connect`. Until then, nothing is sent. When you turn it off, deactivate the plugin or delete it, the plugin tells Email Canary.
    148152
    149153While Email Canary is on, the site sends:
    150154
    151155* 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;
     156* if you set a nanoPost account key (in `wp-config.php` or with WP-CLI) in place of an alert address: that key, once, in the call that turns Email Canary on. The plugin does not store it;
    152157* the same site settings again when one of them changes;
    153158* 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);
    … …  
    191196
    192197== Changelog ==
     198
     199= 1.9.0 =
     200* Account key for agencies: `NANOPOST_EMAIL_CHECK_KEY` in `wp-config.php` turns Email Canary on by itself, once per site address, with alerts to the nanoPost account's address and no confirm email. The key is never stored.
     201* New WP-CLI command `wp email-check connect`, with `--key` or `--email`.
    193202
    194203= 1.8.0 =
  • nanopost-email-check/trunk/uninstall.php

    r3830659 r3832012  
    6060        wp_unschedule_hook( 'nanopost_email_check_fetch_test' );
    6161        wp_clear_scheduled_hook( 'nanopost_email_check_start' );
     62        delete_option( 'nanopost_email_check_key_state' );
     63        wp_clear_scheduled_hook( 'nanopost_email_check_key_connect' );
    6264        delete_option( 'nanopost_email_check_settings' );
    6365        delete_option( 'nanopost_email_check_log' );
Note: See TracChangeset for help on using the changeset viewer.