-
Notifications
You must be signed in to change notification settings - Fork 68
Implement exclude revision flag to remove revision results from the search #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a94e53e
aaaada6
2a9a529
c5e760d
38ee18a
95cfad1
c979679
6f2c866
efbd997
0505dcb
bcfccbe
0420e71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1352,6 +1352,9 @@ public function prefix() { | |
| * - count | ||
| * --- | ||
| * | ||
| * [--exclude_revisions] | ||
| * : Exclude revisions from the search. | ||
| * | ||
| * The percent color codes available are: | ||
| * | ||
| * | Code | Color | ||
|
|
@@ -1490,6 +1493,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; | ||
|
|
@@ -1554,10 +1558,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 && $wpdb->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 && $wpdb->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 ) ); | ||
|
Comment on lines
+1564
to
+1573
|
||
| } 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 ) ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$post_type_sqlis computed for every text column, but it never changes and is only needed when$exclude_revisionsapplies. Moving this identifier escaping outside the inner$text_columnsloop (or only computing it when needed) would reduce repeated work and make the control flow easier to follow.