rows(); if ( empty( $rows ) ) { printf( '
', sprintf( // Translators: 1 - A number of days. esc_html__( 'No 404s in the last %1$d days.', '404-to-301' ), (int) self::DAYS ) ); } else { echo ''; } $this->footer( ! empty( $rows ) ); } /** * The footer: a link into the logs, plus the promo when there is something to promote. * * @since 4.0.4 * * @param bool $hasRows Whether any 404s were listed. * @return void */ private function footer( bool $hasRows ): void { echo ''; } /** * Top 404s by hits from the last 30 days. * * Cached because the Dashboard is a hot page and this is decoration, not a * source of truth — a few minutes of staleness costs nothing. * * @since 4.0.4 * * @return array[] Rows of url and hits. */ private function rows(): array { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } $logs = Log::paginate( [ 'number' => self::ROWS, 'orderby' => 'hits', 'order' => 'DESC', 'status' => Log::STATUS_OPEN, 'date_query' => [ [ 'column' => 'updated_at', 'after' => gmdate( 'Y-m-d H:i:s', strtotime( '-' . self::DAYS . ' days' ) ), ], ], ] ); $rows = []; foreach ( $logs['items'] as $log ) { $rows[] = [ 'url' => (string) $log->url, 'hits' => (int) $log->hits, ]; } set_transient( self::CACHE, $rows, 15 * MINUTE_IN_SECONDS ); return $rows; } }