Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
},
"require-dev": {
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/wp-cli-tests": "^4"
"wp-cli/wp-cli-tests": "dev-main"
},
"config": {
"process-timeout": 7200,
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"johnpbloch/wordpress-core-installer": true
"johnpbloch/wordpress-core-installer": true,
"phpstan/extension-installer": true
},
"lock": false
},
Expand Down Expand Up @@ -72,12 +73,14 @@
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpstan": "run-phpstan-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpstan",
"@phpunit",
"@behat"
]
Expand Down
18 changes: 18 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
parameters:
level: 9
paths:
- src
- cache-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
- vendor/wp-cli/wp-cli-tests
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
dynamicConstantNames:
- WP_DEBUG
- WP_DEBUG_LOG
- WP_DEBUG_DISPLAY
ignoreErrors:
- identifier: missingType.parameter
- identifier: missingType.return
25 changes: 20 additions & 5 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public function delete( $args, $assoc_args ) {
* Success: The cache was flushed.
*/
public function flush( $args, $assoc_args ) {

// TODO: Needs fixing in wp-cli/wp-cli
// @phpstan-ignore offsetAccess.nonOffsetAccessible
if ( WP_CLI::has_config( 'url' ) && ! empty( WP_CLI::get_config()['url'] ) && is_multisite() ) {
WP_CLI::warning( 'Flushing the cache may affect all sites in a multisite installation, depending on the implementation of the object cache.' );
}
Expand Down Expand Up @@ -439,7 +440,11 @@ public function flush_group( $args, $assoc_args ) {
*/
public function pluck( $args, $assoc_args ) {
list( $key ) = $args;
$group = Utils\get_flag_value( $assoc_args, 'group' );

/**
* @var string $group
*/
$group = Utils\get_flag_value( $assoc_args, 'group' );

$value = wp_cache_get( $key, $group );

Expand Down Expand Up @@ -512,11 +517,21 @@ function ( $key ) {
* - plaintext
* - json
* ---
*
* @param string[] $args
*/
public function patch( $args, $assoc_args ) {
list( $action, $key ) = $args;
$group = Utils\get_flag_value( $assoc_args, 'group' );
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );

/**
* @var string $group
*/
$group = Utils\get_flag_value( $assoc_args, 'group' );

/**
* @var string|null $expiration
*/
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );

$key_path = array_map(
function ( $key ) {
Expand Down Expand Up @@ -569,7 +584,7 @@ function ( $key ) {
if ( $patched_value === $old_value ) {
WP_CLI::success( "Value passed for cache key '$key' is unchanged." );
} else {
$success = wp_cache_set( $key, $patched_value, $group, $expiration );
$success = wp_cache_set( $key, $patched_value, $group, (int) $expiration );
if ( $success ) {
WP_CLI::success( "Updated cache key '$key'." );
} else {
Expand Down
44 changes: 31 additions & 13 deletions src/Transient_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ public function get( $args, $assoc_args ) {
*
* $ wp transient set sample_key "test data" 3600
* Success: Transient added.
*
* @param string[] $args
*/
public function set( $args, $assoc_args ) {
list( $key, $value ) = $args;

$expiration = Utils\get_flag_value( $args, 2, 0 );
$expiration = $args[2] ?? 0;

$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
if ( $func( $key, $value, $expiration ) ) {
if ( $func( $key, $value, (int) $expiration ) ) {
WP_CLI::success( 'Transient added.' );
} else {
WP_CLI::error( 'Transient could not be set.' );
Expand Down Expand Up @@ -180,9 +182,9 @@ public function set( $args, $assoc_args ) {
public function delete( $args, $assoc_args ) {
$key = ( ! empty( $args ) ) ? $args[0] : null;

$all = Utils\get_flag_value( $assoc_args, 'all' );
$expired = Utils\get_flag_value( $assoc_args, 'expired' );
$network = Utils\get_flag_value( $assoc_args, 'network' );
$all = (bool) Utils\get_flag_value( $assoc_args, 'all' );
$expired = (bool) Utils\get_flag_value( $assoc_args, 'expired' );
$network = (bool) Utils\get_flag_value( $assoc_args, 'network' );

if ( true === $all ) {
$this->delete_all( $network );
Expand Down Expand Up @@ -301,9 +303,9 @@ public function list_( $args, $assoc_args ) {
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only shows those stored in the database.' );
}

$network = Utils\get_flag_value( $assoc_args, 'network', false );
$unserialize = Utils\get_flag_value( $assoc_args, 'unserialize', false );
$human_readable = Utils\get_flag_value( $assoc_args, 'human-readable', false );
$network = (bool) Utils\get_flag_value( $assoc_args, 'network', false );
$unserialize = (bool) Utils\get_flag_value( $assoc_args, 'unserialize', false );
$human_readable = (bool) Utils\get_flag_value( $assoc_args, 'human-readable', false );

$fields = array( 'name', 'value', 'expiration' );
if ( isset( $assoc_args['fields'] ) ) {
Expand Down Expand Up @@ -505,7 +507,12 @@ function ( $key ) {
*/
public function patch( $args, $assoc_args ) {
list( $action, $key ) = $args;
$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );

/**
* @var string $expiration
*/
$expiration = Utils\get_flag_value( $assoc_args, 'expiration', 0 );
$expiration = (int) $expiration;

$read_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
$write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
Expand Down Expand Up @@ -582,20 +589,31 @@ function ( $key ) {
private function get_transient_expiration( $name, $is_site_transient = false, $human_readable = false ) {
if ( $is_site_transient ) {
if ( is_multisite() ) {
$expiration = (int) get_site_option( '_site_transient_timeout_' . $name );
/**
* @var string $expiration
*/
$expiration = get_site_option( '_site_transient_timeout_' . $name );
} else {
$expiration = (int) get_option( '_site_transient_timeout_' . $name );
/**
* @var string $expiration
*/
$expiration = get_option( '_site_transient_timeout_' . $name );
}
} else {
$expiration = (int) get_option( '_transient_timeout_' . $name );
/**
* @var string $expiration
*/
$expiration = get_option( '_transient_timeout_' . $name );
}

$expiration = (int) $expiration;

if ( 0 === $expiration ) {
return $human_readable ? 'never expires' : 'false';
}

if ( ! $human_readable ) {
return $expiration;
return (string) $expiration;
}

$now = time();
Expand Down