diff --git a/composer.json b/composer.json index 838cede..87f762d 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "wp-cli/checksum-command": "^2 || ^3", "wp-cli/core-command": "^2 || ^3", "wp-cli/cron-command": "^2 || ^3", - "wp-cli/entity-command": "^2 || ^3", + "wp-cli/entity-command": "^2.8.12 || ^3", "wp-cli/extension-command": "^2 || ^3", "wp-cli/language-command": "^2 || ^3", "wp-cli/wp-cli": "^3.0" diff --git a/doctor.yml b/doctor.yml index bf8fbf9..cddcad3 100644 --- a/doctor.yml +++ b/doctor.yml @@ -1,6 +1,8 @@ # Default check configuration for `wp doctor` autoload-options-size: check: Autoload_Options_Size +transients-size: + check: Transients_Size constant-savequeries-falsy: check: Constant_Definition options: diff --git a/features/check-autoload-options-size.feature b/features/check-autoload-options-size.feature index 1630fb6..500a506 100644 --- a/features/check-autoload-options-size.feature +++ b/features/check-autoload-options-size.feature @@ -19,7 +19,7 @@ Feature: Check the size of autoloaded options When I run `wp doctor check autoload-options-size --fields=message` Then STDOUT should contain: """ - is less than threshold (900kb) + does not exceed threshold (900kb) """ @less-than-wp-6.5 @@ -59,5 +59,5 @@ Feature: Check the size of autoloaded options When I run `wp doctor check autoload-options-size --fields=message --config=custom.yml` Then STDOUT should contain: """ - is less than threshold (800kb) + does not exceed threshold (800kb) """ diff --git a/features/check-transients-size.feature b/features/check-transients-size.feature new file mode 100644 index 0000000..46daac2 --- /dev/null +++ b/features/check-transients-size.feature @@ -0,0 +1,156 @@ +Feature: Check the size of autoloaded transients + + Scenario: Verify check description + Given an empty directory + + When I run `wp doctor list --fields=name,description` + Then STDOUT should be a table containing rows: + | name | description | + | transients-size | Warns when autoloaded transients size exceeds threshold of 900 kb. | + + Scenario: Autoloaded transients are less than 900 kb + Given a WP install + + When I run `wp doctor check transients-size --fields=name,status` + Then STDOUT should be a table containing rows: + | name | status | + | transients-size | success | + + When I run `wp doctor check transients-size --fields=message` + Then STDOUT should contain: + """ + does not exceed threshold (900kb) + """ + + Scenario: Autoloaded transients equal 900 kb + Given a WP install + And a wp-content/mu-plugins/exact-threshold-transients.php file: + """ + get_col( "SELECT option_name FROM {$wpdb->options}" ); + foreach ( $option_names as $option_name ) { + if ( + 0 === strpos( $option_name, '_transient_' ) + || 0 === strpos( $option_name, '_site_transient_' ) + ) { + delete_option( $option_name ); + } + } + + add_option( '_transient_doctor_exact_threshold', str_repeat( '9', 900 * 1024 ), '', true ); + }, + PHP_INT_MAX + ); + """ + + When I run `wp option list --transients --autoload=on --format=total_bytes` + Then STDOUT should be: + """ + 921600 + """ + + When I run `wp doctor check transients-size --fields=name,status` + Then STDOUT should be a table containing rows: + | name | status | + | transients-size | success | + + When I run `wp doctor check transients-size --fields=message` + Then STDOUT should contain: + """ + Autoloaded transients size (900kb) does not exceed threshold (900kb). + """ + + Scenario: Autoloaded transients are greater than 900 kb + Given a WP install + And a create-large-transients.php file: + """ + _message = $message; } + /** + * Format a byte count for check result messages. + * + * @param int|float $size Size in bytes. + * @param int $precision Precision. + * @return string + */ + protected static function format_bytes( $size, $precision = 2 ) { + if ( 0 >= $size ) { + return '0'; + } + + $suffixes = array( '', 'kb', 'mb', 'g', 't' ); + $base = min( (int) floor( log( $size, 1024 ) ), count( $suffixes ) - 1 ); + + return round( $size / pow( 1024, $base ), $precision ) . $suffixes[ $base ]; + } + /** * Run the check. * diff --git a/src/Check/Autoload_Options_Size.php b/src/Check/Autoload_Options_Size.php index 8752410..4ff3ffa 100644 --- a/src/Check/Autoload_Options_Size.php +++ b/src/Check/Autoload_Options_Size.php @@ -39,18 +39,7 @@ public function run() { $this->set_message( "Autoloaded options size ({$human_total}) exceeds threshold ({$human_threshold})" ); } else { $this->set_status( 'success' ); - $this->set_message( "Autoloaded options size ({$human_total}) is less than threshold ({$human_threshold})." ); + $this->set_message( "Autoloaded options size ({$human_total}) does not exceed threshold ({$human_threshold})." ); } } - - /** - * @param int|float $size Size in bytes. - * @param int $precision Precision. - * @return string - */ - private static function format_bytes( $size, $precision = 2 ) { - $base = log( $size, 1024 ); - $suffixes = array( '', 'kb', 'mb', 'g', 't' ); - return round( pow( 1024, $base - floor( $base ) ), $precision ) . $suffixes[ (int) floor( $base ) ]; - } } diff --git a/src/Check/Transients_Size.php b/src/Check/Transients_Size.php new file mode 100644 index 0000000..69de176 --- /dev/null +++ b/src/Check/Transients_Size.php @@ -0,0 +1,46 @@ + true, + 'autoload' => 'on', + 'format' => 'total_bytes', + ) + ); + $total_bytes = (int) ob_get_clean(); + + $threshold_bytes = $this->threshold_kb * 1024; + $human_threshold = self::format_bytes( $threshold_bytes ); + $human_total = self::format_bytes( $total_bytes ); + if ( $threshold_bytes < $total_bytes ) { + $this->set_status( 'warning' ); + $this->set_message( "Autoloaded transients size ({$human_total}) exceeds threshold ({$human_threshold})" ); + } else { + $this->set_status( 'success' ); + $this->set_message( "Autoloaded transients size ({$human_total}) does not exceed threshold ({$human_threshold})." ); + } + } +} diff --git a/src/Command.php b/src/Command.php index 3dd5753..da86f85 100644 --- a/src/Command.php +++ b/src/Command.php @@ -28,6 +28,7 @@ * | name | description | * +----------------------------+--------------------------------------------------------------------------------+ * | autoload-options-size | Warns when autoloaded options size exceeds threshold of 900 kb. | + * | transients-size | Warns when autoloaded transients size exceeds threshold of 900 kb. | * | constant-savequeries-falsy | Confirms expected state of the SAVEQUERIES constant. | * | constant-wp-debug-falsy | Confirms expected state of the WP_DEBUG constant. | * | core-update | Errors when new WordPress minor release is available; warns for major release. | @@ -325,6 +326,7 @@ function ( $check ) { * | name | description | * +----------------------------+--------------------------------------------------------------------------------+ * | autoload-options-size | Warns when autoloaded options size exceeds threshold of 900 kb. | + * | transients-size | Warns when autoloaded transients size exceeds threshold of 900 kb. | * | constant-savequeries-falsy | Confirms expected state of the SAVEQUERIES constant. | * | constant-wp-debug-falsy | Confirms expected state of the WP_DEBUG constant. | * | core-update | Errors when new WordPress minor release is available; warns for major release. |