API: System Status: Only load necessary data

The system status endpoint supports the global `_fields` query parameter
for limiting which fields are returned. That is, if you send a request
with a query string of `?_fields=environment`, only the environment
property will be returned. However, we still calculate every property.

This change ensure that we only gather the data that is necessary for
the response. Since some of this data can be somewhat slow (like reading
from the disk in the case of get_theme_info), this could have a pretty
nice performance benefit in some cases.
This commit is contained in:
Josh Betz 2022-04-28 13:18:43 -05:00
parent 245dd98b45
commit 66c21a5e8b
1 changed files with 38 additions and 12 deletions

View File

@ -585,18 +585,44 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
* @return array
*/
public function get_item_mappings_per_fields( $fields ) {
return array(
'environment' => $this->get_environment_info_per_fields( $fields ),
'database' => $this->get_database_info(),
'active_plugins' => $this->get_active_plugins(),
'inactive_plugins' => $this->get_inactive_plugins(),
'dropins_mu_plugins' => $this->get_dropins_mu_plugins(),
'theme' => $this->get_theme_info(),
'settings' => $this->get_settings(),
'security' => $this->get_security_info(),
'pages' => $this->get_pages(),
'post_type_counts' => $this->get_post_type_counts(),
);
$items = array();
foreach( $fields as $field ) {
switch( $field ) {
case 'environment':
$items['environment'] = $this->get_environment_info_per_fields( $fields );
break;
case 'database':
$items['database'] = $this->get_database_info();
break;
case 'active_plugins':
$items['active_plugins'] = $this->get_active_plugins();
break;
case 'inactive_plugins':
$items['inactive_plugins'] = $this->get_inactive_plugins();
break;
case 'dropins_mu_plugins':
$items['dropins_mu_plugins'] = $this->get_dropins_mu_plugins();
break;
case 'theme':
$items['theme'] = $this->get_theme_info();
break;
case 'settings':
$items['settings'] = $this->get_settings();
break;
case 'security':
$items['security'] = $this->get_security_info();
break;
case 'pages':
$items['pages'] = $this->get_pages();
break;
case 'post_type_counts':
$items['post_type_counts'] = $this->get_post_type_counts();
break;
}
}
return $items;
}
/**