Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,37 +115,20 @@ class Page extends Model {
/**
* Get the number of pages for each group of status codes, e.g. 1xx, 2xx, 3xx
*
* @return mixed[] Assoc. array of status code to number of pages, e.g. '2' => 183
* @return array<string, int> Array of status code to number of pages.
*/
public static function get_http_status_codes_summary() {
global $wpdb;

$query = 'SELECT LEFT(http_status_code, 1) AS status, COUNT(*) AS count';
$query = 'SELECT LEFT(http_status_code, 1) AS code, COUNT(*) AS count';
$query .= ' FROM ' . self::table_name();
$query .= ' GROUP BY LEFT(http_status_code, 1)';
$query .= ' ORDER BY status';

$rows = $wpdb->get_results(
$query,
ARRAY_A
);

$http_codes = [
'1' => 0,
'2' => 0,
'3' => 0,
'4' => 0,
'5' => 0,
'6' => 0,
'7' => 0,
'8' => 0,
];

foreach ( $rows as $row ) {
$http_codes[ $row['status'] ] = $row['count'];
}
$query .= ' GROUP BY code';

$rows = array_column( $wpdb->get_results( $query, \ARRAY_A ), 'count', 'code' );

$http_status_codes = array_fill_keys( ['1', '2', '3', '4', '5', '6', '7', '8'], 0 );

return $http_codes;
return array_merge( $http_status_codes, $rows );
}

/**
Expand Down