(string) $alert['id'], 'site' => $site->id, 'siteName' => $site->display_name(), 'type' => (string) $alert['type'], 'severity' => (string) $alert['severity'], 'state' => AlertStore::state( $alert ), 'back' => isset( $alert['back'] ) ? (string) $alert['back'] : '', 'kind' => '', 'headline' => '', 'url' => '', 'path' => '', 'title' => '', 'summary' => '', 'metrics' => array(), 'insights' => array(), 'next' => '', 'checks' => array(), 'confidence' => array( 'level' => 'strong', 'text' => '', ), 'facts' => array(), 'tables' => array(), ); switch ( $alert['type'] ) { case Detector::NO_CLICKS: $out = array_merge( $out, SearchWording::no_clicks( $d, $site ) ); break; case Detector::CLICKS: $out = array_merge( $out, SearchWording::clicks( $d, $site ) ); break; case Detector::RANKINGS: $out = array_merge( $out, SearchWording::rankings( $d, $site ) ); break; case Detector::PAGE: $out = array_merge( $out, SearchWording::page( $d ) ); break; case Detector::LEADS: $out = array_merge( $out, LeadWording::leads( $d, $site ) ); break; case Detector::TRACKING: $out = array_merge( $out, LeadWording::tracking( $d ) ); break; case Detector::SESSIONS: $out = array_merge( $out, LeadWording::sessions( $d, $site ) ); break; } $out['checks'] = array_slice( array_values( array_filter( $out['checks'] ) ), 0, 3 ); // How much it touches the client's goals and priorities (Edit site → Goals & priorities). $out['priority'] = Priorities::for_alert( $alert, $site ); $out['basis'] = self::basis( $alert, $period ); $out['since'] = self::since( $alert ); $out['firstSeen'] = ! empty( $alert['first_seen'] ) ? wp_date( 'j M', (int) $alert['first_seen'] ) : ''; $out['status'] = self::status_line( $alert ); $out['reportUrl'] = self::report_url( $site, $period ); $out['slackAt'] = ! empty( $alert['slack_at'] ) ? wp_date( 'j M', (int) $alert['slack_at'] ) : ''; $out['copy'] = Slack::plain( $out ); return $out; } /** * Where the numbers come from, and for which dates. * * @param array $alert Alert. * @param array $period Period. * @return string */ private static function basis( array $alert, array $period ) { $source = Detector::SEARCH === $alert['source'] ? __( 'Search Console', 'turtlereport' ) : __( 'GA4, organic search', 'turtlereport' ); if ( empty( $period['s'] ) ) { return $source; } return sprintf( /* translators: 1: data source, 2: date range, 3: earlier date range. */ __( '%1$s · %2$s vs %3$s', 'turtlereport' ), $source, Wording::range( $period['s'], $period['e'] ), Wording::range( $period['ps'], $period['pe'] ) ); } /** * "Detected today", "Detected 3 Oct". * * @param array $alert Alert. * @return string */ private static function since( array $alert ) { $first = isset( $alert['first_seen'] ) ? (int) $alert['first_seen'] : 0; if ( ! $first ) { return ''; } if ( wp_date( 'Y-m-d', $first ) === wp_date( 'Y-m-d' ) ) { return __( 'Detected today', 'turtlereport' ); } /* translators: %s: date the issue was first detected. */ return sprintf( __( 'Detected %s', 'turtlereport' ), wp_date( 'j M', $first ) ); } /** * A short line about the alert's state. * * @param array $alert Alert. * @return string */ private static function status_line( array $alert ) { $state = AlertStore::state( $alert ); $who = ''; if ( ! empty( $alert['changed_by'] ) ) { $user = get_userdata( (int) $alert['changed_by'] ); $who = $user && ! empty( $user->display_name ) ? (string) $user->display_name : ''; } switch ( $state ) { case AlertStore::SNOOZED: /* translators: %s: date. */ return sprintf( __( 'Snoozed until %s. It comes back then if the problem is still there.', 'turtlereport' ), wp_date( 'j M', (int) $alert['until'] ) ); case AlertStore::HANDLED: return '' !== $who /* translators: 1: person, 2: date. */ ? sprintf( __( 'Marked as handled by %1$s on %2$s. It comes back if it gets worse, and clears by itself when the numbers recover.', 'turtlereport' ), $who, wp_date( 'j M', (int) $alert['changed_at'] ) ) /* translators: %s: date. */ : sprintf( __( 'Marked as handled on %s. It comes back if it gets worse, and clears by itself when the numbers recover.', 'turtlereport' ), wp_date( 'j M', (int) $alert['changed_at'] ) ); case AlertStore::RESOLVED: /* translators: %s: date. */ return sprintf( __( 'Resolved by itself on %s: the numbers recovered.', 'turtlereport' ), wp_date( 'j M', (int) $alert['resolved_at'] ) ); } switch ( isset( $alert['back'] ) ? $alert['back'] : '' ) { case 'snooze_over': return __( 'Back after the snooze: the problem is still there.', 'turtlereport' ); case 'worse': return __( 'This got worse since it was first flagged.', 'turtlereport' ); case 'again': return __( 'This happened before, recovered, and is back.', 'turtlereport' ); } return ''; } /** * The client's report for the alert's dates. * * @param Profile $site Site. * @param array $period Period. * @return string */ private static function report_url( Profile $site, array $period ) { $args = array( 'site' => $site->id ); if ( ! empty( $period['s'] ) ) { $args += array( 'start' => $period['s'], 'end' => $period['e'], 'cstart' => $period['ps'], 'cend' => $period['pe'], ); } return Links::url( $args ); } }