Skip to content

Commit f7980a8

Browse files
swissspidyCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4384af0 commit f7980a8

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/Cron_Event_Command.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function unschedule( $args, $assoc_args ) {
327327
* : Delete all hooks.
328328
*
329329
* [--match-args=<args>]
330-
* : Only delete events whose arguments match the given JSON-encoded array or scalar value. Requires exactly one hook name.
330+
* : Only delete events whose arguments match the given JSON-encoded array or scalar value. Argument types must match exactly (for example, `["123"]` vs `[123]`). Requires exactly one hook name.
331331
*
332332
* ## EXAMPLES
333333
*
@@ -336,7 +336,7 @@ public function unschedule( $args, $assoc_args ) {
336336
* Success: Deleted a total of 2 cron events.
337337
*
338338
* # Delete a specific cron event by hook and arguments
339-
* $ wp cron event delete cron_test --match-args='[123]'
339+
* $ wp cron event delete cron_test --match-args='["123"]'
340340
* Success: Deleted a total of 1 cron event.
341341
*/
342342
public function delete( $args, $assoc_args ) {
@@ -351,12 +351,22 @@ public function delete( $args, $assoc_args ) {
351351
WP_CLI::error( 'The --match-args parameter cannot be combined with --all or --due-now.' );
352352
}
353353

354-
$decoded_args = json_decode( $match_args, true );
355-
if ( null === $decoded_args && JSON_ERROR_NONE !== json_last_error() ) {
356-
// Not valid JSON — treat as a single string argument wrapped in an array.
354+
$trimmed_match_args = ltrim( $match_args );
355+
356+
// Only JSON-decode when the value clearly looks like JSON:
357+
// - starts with '[' for arrays
358+
// - starts with '"' for explicitly quoted strings
359+
if ( '' !== $trimmed_match_args && ( '[' === $trimmed_match_args[0] || '"' === $trimmed_match_args[0] ) ) {
360+
$decoded_args = json_decode( $match_args, true );
361+
if ( null === $decoded_args && JSON_ERROR_NONE !== json_last_error() ) {
362+
// Not valid JSON — treat as a single string argument wrapped in an array.
363+
$decoded_args = array( $match_args );
364+
} elseif ( ! is_array( $decoded_args ) ) {
365+
$decoded_args = array( $decoded_args );
366+
}
367+
} else {
368+
// Treat non-JSON-looking values as a single string argument.
357369
$decoded_args = array( $match_args );
358-
} elseif ( ! is_array( $decoded_args ) ) {
359-
$decoded_args = array( $decoded_args );
360370
}
361371
}
362372

0 commit comments

Comments
 (0)