Merge pull request #18880 from woocommerce/fix/18856

Added ability to search for all post statuses
This commit is contained in:
Claudiu Lodromanean 2018-02-08 09:27:01 -08:00 committed by GitHub
commit becd77ae93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View File

@ -459,7 +459,7 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table {
// Search using CRUD.
if ( isset( $query_vars['s'] ) ) {
$data_store = WC_Data_Store::load( 'product' );
$ids = $data_store->search_products( wc_clean( $query_vars['s'] ), '', true );
$ids = $data_store->search_products( wc_clean( $query_vars['s'] ), '', true, true );
$query_vars['post__in'] = array_merge( $ids, array( 0 ) );
// So we know we are searching products.
$query_vars['product_search'] = true;

View File

@ -1304,9 +1304,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
* @param string $term Search term.
* @param string $type Type of product.
* @param bool $include_variations Include variations in search or not.
* @param bool $all_statuses Should we search all statuses or limit to published?
* @return array of ids
*/
public function search_products( $term, $type = '', $include_variations = false ) {
public function search_products( $term, $type = '', $include_variations = false, $all_statuses = false ) {
global $wpdb;
$like_term = '%' . $wpdb->esc_like( $term ) . '%';
@ -1314,6 +1315,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$post_statuses = current_user_can( 'edit_private_products' ) ? array( 'private', 'publish' ) : array( 'publish' );
$type_join = '';
$type_where = '';
$status_where = '';
if ( $type ) {
if ( in_array( $type, array( 'virtual', 'downloadable' ), true ) ) {
@ -1322,11 +1324,15 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
}
}
if ( ! $all_statuses ) {
$status_where = " AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "') ";
}
// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
$product_ids = $wpdb->get_col(
$search_results = $wpdb->get_results(
// phpcs:disable
$wpdb->prepare(
"SELECT DISTINCT posts.ID FROM {$wpdb->posts} posts
"SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id
$type_join
WHERE (
@ -1337,7 +1343,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
)
)
AND posts.post_type IN ('" . implode( "','", $post_types ) . "')
AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "')
$status_where
$type_where
ORDER BY posts.post_parent ASC, posts.post_title ASC",
$like_term,
@ -1347,6 +1353,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
// phpcs:enable
);
$product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
if ( is_numeric( $term ) ) {
$post_id = absint( $term );
$post_type = get_post_type( $post_id );