'hourly', self::WEEKLY => 'weekly', ) as $hook => $recurrence ) { $next = wp_next_scheduled( $hook ); if ( $on && ! $next ) { wp_schedule_event( time() + 5 * MINUTE_IN_SECONDS, $recurrence, $hook ); } elseif ( ! $on && $next ) { wp_clear_scheduled_hook( $hook ); } } } /** * Hourly: compare the open changes with the ones already seen. */ public static function check(): void { if ( ! Connection::is_linked() || Site::is_staging() ) { return; } Ledger::flush(); $res = Ledger::changes(); if ( ! $res['ok'] ) { return; } $open = array(); foreach ( (array) ( $res['data']['proposed'] ?? array() ) as $row ) { $open[ (string) $row['id'] ] = (string) ( $row['title'] ?? '' ); } $seen = get_option( self::SEEN, null ); update_option( self::SEEN, array_keys( $open ), false ); if ( ! is_array( $seen ) ) { // The first check only learns what is already there. return; } $new = array_diff_key( $open, array_flip( $seen ) ); if ( ! $new ) { return; } $fresh = get_option( self::FRESH, array() ); $fresh = is_array( $fresh ) ? $fresh : array(); foreach ( $new as $id => $title ) { $fresh[ $id ] = array( 'title' => $title, 't' => time(), ); } update_option( self::FRESH, array_slice( $fresh, -50, null, true ), false ); } /** * New changes since this user last hid the notice. * * @return array */ private static function fresh_for_user(): array { $fresh = get_option( self::FRESH, array() ); if ( ! is_array( $fresh ) ) { return array(); } $since = (int) get_user_option( self::HIDDEN, get_current_user_id() ); return array_filter( $fresh, static function ( $row ) use ( $since ) { return (int) ( $row['t'] ?? 0 ) > $since; } ); } /** * The admin notice for new changes. */ public static function notice(): void { if ( ! current_user_can( Roles::BRIEFS ) || ! Connection::is_linked() || Site::is_staging() ) { return; } // Only on the dashboard, never across the rest of wp-admin. $screen = get_current_screen(); if ( ! $screen || 'dashboard' !== $screen->id ) { return; } $fresh = self::fresh_for_user(); if ( ! $fresh ) { return; } $titles = array_slice( wp_list_pluck( $fresh, 'title' ), 0, 3 ); echo '

' . esc_html( sprintf( /* translators: %d: number of new changes */ _n( 'Outercite has %d new change for this site.', 'Outercite has %d new changes for this site.', count( $fresh ), 'outercite' ), count( $fresh ) ) ) . ' ' . esc_html( implode( '; ', $titles ) ) . '

'; echo '' . esc_html__( 'See changes', 'outercite' ) . ' '; echo '' . esc_html__( 'Hide', 'outercite' ) . '

'; } /** * Hide the new-changes notice for this user until more arrive. */ public static function handle_hide(): void { check_admin_referer( 'outercite_hide_new' ); if ( ! current_user_can( Roles::BRIEFS ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } update_user_option( get_current_user_id(), self::HIDDEN, time() ); wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() ); exit; } /** * Weekly: email the open changes to the people picked in settings. */ public static function send_weekly(): void { if ( ! Connection::is_linked() || Site::is_staging() ) { return; } global $wpdb; $users = get_users( array( // A per-site user option, so opting in on one site of a network is not opting in everywhere. 'meta_key' => $wpdb->get_blog_prefix() . self::OPT_IN, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- weekly, a handful of users. 'meta_value' => '1', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- as above. 'number' => 50, ) ); if ( ! $users ) { return; } Ledger::flush(); $res = Ledger::changes(); if ( ! $res['ok'] ) { return; } $all = (array) ( $res['data']['proposed'] ?? array() ); $open = array_slice( $all, 0, 10 ); if ( ! $open ) { return; } $site = get_bloginfo( 'name' ); $lines = array(); foreach ( $open as $row ) { $lines[] = '- ' . (string) ( $row['title'] ?? '' ) . "\n " . Admin::brief_url( (string) $row['id'] ); } $body = sprintf( /* translators: 1: number listed, 2: site name */ __( 'The %1$d most important open Outercite changes for %2$s this week:', 'outercite' ), count( $open ), $site ) . "\n\n" . implode( "\n\n", $lines ) . "\n\n" . __( 'Open a change to read its brief, start a draft, or have Outercite write it. Publishing the page marks it live.', 'outercite' ) . "\n"; $subject = sprintf( /* translators: 1: number of changes, 2: site name */ _n( '%1$d open change for %2$s', '%1$d open changes for %2$s', count( $all ), 'outercite' ), count( $all ), $site ); foreach ( $users as $user ) { if ( user_can( $user, Roles::BRIEFS ) ) { wp_mail( $user->user_email, $subject, $body ); } } Activity::add( 'email', __( 'Sent the weekly changes email', 'outercite' ) ); } /** * Save who gets the weekly email. */ public static function handle_save(): void { check_admin_referer( 'outercite_save_emails' ); if ( ! current_user_can( Roles::CONNECT ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } $picked = isset( $_POST['users'] ) && is_array( $_POST['users'] ) ? array_map( 'absint', wp_unslash( $_POST['users'] ) ) : array(); foreach ( self::eligible_users() as $user ) { if ( in_array( (int) $user->ID, $picked, true ) ) { update_user_option( $user->ID, self::OPT_IN, '1' ); } else { delete_user_option( $user->ID, self::OPT_IN ); } } Admin::flash( 'success', __( 'Saved.', 'outercite' ) ); wp_safe_redirect( Admin::settings_url( array( 'tab' => 'emails' ) ) ); exit; } /** * People who can see briefs, so may get the email. * * @return \WP_User[] */ private static function eligible_users(): array { return array_values( array_filter( get_users( array( 'number' => 200 ) ), static function ( $user ) { return user_can( $user, Roles::BRIEFS ); } ) ); } /** * The emails tab on the settings screen. */ public static function render(): void { echo '

' . esc_html__( 'Weekly email', 'outercite' ) . '

'; echo '

' . esc_html__( 'Every week, the people you pick get the open changes for this site, with a link to each brief. Editors also see a notice on the dashboard when new changes arrive.', 'outercite' ) . '

'; echo '
'; wp_nonce_field( 'outercite_save_emails' ); echo '
'; foreach ( self::eligible_users() as $user ) { printf( '', (int) $user->ID, checked( '1', get_user_option( self::OPT_IN, $user->ID ), false ), esc_html( $user->display_name ), esc_html( $user->user_email ) ); } echo '

'; } }