diff --git a/features/option-list.feature b/features/option-list.feature index 3d3ddf552a..9d5b5bca93 100644 --- a/features/option-list.feature +++ b/features/option-list.feature @@ -31,3 +31,36 @@ Feature: List WordPress options """ siteurl """ + + Scenario: List option with exclude pattern + Given a WP install + + When I run `wp option add sample_test_field_one sample_test_field_value_one` + And I run `wp option add sample_test_field_two sample_test_field_value_two` + And I run `wp option list --search="sample_test_field_*" --format=csv` + Then STDOUT should be: + """ + option_name,option_value + sample_test_field_one,sample_test_field_value_one + sample_test_field_two,sample_test_field_value_two + """ + + When I run `wp option list --search="sample_test_field_*" --exclude="*field_one" --format=csv` + Then STDOUT should be: + """ + option_name,option_value + sample_test_field_two,sample_test_field_value_two + """ + + When I run `wp option list` + Then STDOUT should contain: + """ + sample_test_field_one + """ + + When I run `wp option list --exclude="sample_test_field_one"` + Then STDOUT should not contain: + """ + sample_test_field_one + """ + diff --git a/php/commands/option.php b/php/commands/option.php index 390179f363..04908ce391 100644 --- a/php/commands/option.php +++ b/php/commands/option.php @@ -130,6 +130,9 @@ public function add( $args, $assoc_args ) { * [--search=] * : Use wildcards ( * and ? ) to match option name. * + * [--exclude=] + * : Pattern to exclude. Use wildcards ( * and ? ) to match option name. + * * [--autoload=] * : Match only autoload options when value is on, and only not-autoload option when off. * @@ -202,6 +205,7 @@ public function list_( $args, $assoc_args ) { global $wpdb; $pattern = '%'; + $exclude = ''; $fields = array( 'option_name', 'option_value' ); $size_query = ",LENGTH(option_value) AS `size_bytes`"; $autoload_query = ''; @@ -213,6 +217,12 @@ public function list_( $args, $assoc_args ) { $pattern = str_replace( '?', '_', $pattern ); } + if ( isset( $assoc_args['exclude'] ) ) { + $exclude = self::esc_like( $assoc_args['exclude'] ); + $exclude = str_replace( '*', '%', $exclude ); + $exclude = str_replace( '?', '_', $exclude ); + } + if ( isset( $assoc_args['fields'] ) ) { $fields = explode( ',', $assoc_args['fields'] ); } @@ -245,6 +255,10 @@ public function list_( $args, $assoc_args ) { if ( $pattern ) { $where .= $wpdb->prepare( "WHERE `option_name` LIKE %s", $pattern ); } + + if ( $exclude ) { + $where .= $wpdb->prepare( " AND `option_name` NOT LIKE %s", $exclude ); + } $where .= $autoload_query . $transients_query; $results = $wpdb->get_results( "SELECT `option_name`,`option_value`,`autoload`" . $size_query