diff --git a/features/db-search.feature b/features/db-search.feature index 1f5e8f5b..bdd705b7 100644 --- a/features/db-search.feature +++ b/features/db-search.feature @@ -914,6 +914,29 @@ Feature: Search through the database """ And STDERR should be empty + Scenario: Search with exclude revisions option + Given a WP install + And I run `wp post create --post_content="This is the original post content." --post_title="Original Post"` + # Create a revision + And I run `wp post update 1 --post_content="This is the updated post content."` + + When I run `wp db search "updated post content"` + Then STDOUT should contain: + """ + wp_posts:post_content + 1:This is the updated post content. + wp_posts:post_content + 5:This is the updated post content. + """ + + When I run `wp db search "updated post content" --exclude_revisions` + Then STDOUT should contain: + """ + wp_posts:post_content + 1:This is the updated post content. + """ + And STDERR should be empty + Scenario: Search with custom colors Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index 17d1c00e..ef65cfc1 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1264,6 +1264,9 @@ public function prefix() { * [--format=] * : Render output in a particular format. * + * [--exclude_revisions] + * : Exclude revisions from the search. + * * The percent color codes available are: * * | Code | Color @@ -1388,6 +1391,7 @@ public function search( $args, $assoc_args ) { $stats = Utils\get_flag_value( $assoc_args, 'stats', false ); $fields = Utils\get_flag_value( $assoc_args, 'fields' ); $format = Utils\get_flag_value( $assoc_args, 'format' ); + $exclude_revisions = Utils\get_flag_value( $assoc_args, 'exclude_revisions', false ); $column_count = 0; $row_count = 0; @@ -1452,10 +1456,19 @@ public function search( $args, $assoc_args ) { } foreach ( $text_columns as $column ) { - $column_sql = self::esc_sql_ident( $column ); + $column_sql = self::esc_sql_ident( $column ); + $post_type_sql = self::esc_sql_ident( 'post_type' ); if ( $regex ) { + if ( $exclude_revisions && 'wp_posts' === $table ) { + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like. + $results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$post_type_sql} NOT IN ( 'revision' )" ); + } else { + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like. + $results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}" ); + } + } elseif ( $exclude_revisions && 'wp_posts' === $table ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like. - $results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}" ); + $results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s AND {$post_type_sql} NOT IN ( 'revision' )", $esc_like_search ) ); } else { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like. $results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s;", $esc_like_search ) );