Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions features/option-list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

14 changes: 14 additions & 0 deletions php/commands/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public function add( $args, $assoc_args ) {
* [--search=<pattern>]
* : Use wildcards ( * and ? ) to match option name.
*
* [--exclude=<pattern>]
* : Pattern to exclude. Use wildcards ( * and ? ) to match option name.
*
* [--autoload=<value>]
* : Match only autoload options when value is on, and only not-autoload option when off.
*
Expand Down Expand Up @@ -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 = '';
Expand All @@ -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'] );
}
Expand Down Expand Up @@ -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
Expand Down