From c2c982162c9b7dbe7d3b212cfce2dafe5585e313 Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Thu, 15 Feb 2024 14:44:50 -0500 Subject: [PATCH 1/3] feat(subscriptions): add parse_query filter --- includes/hub/admin/class-woo.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/includes/hub/admin/class-woo.php b/includes/hub/admin/class-woo.php index a3798593..11c8c477 100644 --- a/includes/hub/admin/class-woo.php +++ b/includes/hub/admin/class-woo.php @@ -46,6 +46,7 @@ public static function init() { add_filter( 'manage_' . $db_class_name::POST_TYPE_SLUG . '_posts_columns', [ $class_name, 'posts_columns' ] ); add_action( 'manage_' . $db_class_name::POST_TYPE_SLUG . '_posts_custom_column', [ $class_name, 'posts_columns_values' ], 10, 2 ); + add_action( 'parse_query', [ $class_name, 'parse_query' ] ); add_filter( 'get_edit_post_link', [ $class_name, 'get_edit_post_link' ], 10, 2 ); @@ -170,6 +171,25 @@ public static function pre_get_posts( $query ) { return null; } + /** + * Filters search query to include custom fields + * + * @param \WP_Query $query The Query object. + * @return void + */ + public static function parse_query( $query ) { + global $pagenow; + + if ( ! is_admin() || 'edit.php' !== $pagenow || ! $query->is_main_query() || ! in_array( $query->query_vars['post_type'], self::$post_types, true ) ) { + return; + } + + error_log( 'Query: ' . $query->query_vars['s'] ); + + // Get Post IDs by query. + } + + /** * Modify columns on post type table * From ef704246029e28a3d01bb13d6ac4f239834595c9 Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Thu, 15 Feb 2024 15:54:16 -0500 Subject: [PATCH 2/3] feat: add name and email query logic --- includes/hub/admin/class-woo.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/includes/hub/admin/class-woo.php b/includes/hub/admin/class-woo.php index 11c8c477..9b5c4b79 100644 --- a/includes/hub/admin/class-woo.php +++ b/includes/hub/admin/class-woo.php @@ -36,7 +36,6 @@ abstract class Woo { * Runs the initialization. */ public static function init() { - $class_name = get_called_class(); $db_class_name = str_replace( 'Admin', 'Database', $class_name ); self::$post_types[] = $db_class_name::POST_TYPE_SLUG; @@ -180,13 +179,30 @@ public static function pre_get_posts( $query ) { public static function parse_query( $query ) { global $pagenow; - if ( ! is_admin() || 'edit.php' !== $pagenow || ! $query->is_main_query() || ! in_array( $query->query_vars['post_type'], self::$post_types, true ) ) { + if ( ! is_admin() || 'edit.php' !== $pagenow || empty( $query->query_vars['s'] ) || ! in_array( $query->query_vars['post_type'], self::$post_types, true ) ) { return; } - error_log( 'Query: ' . $query->query_vars['s'] ); + $search_term = $query->query_vars['s']; + + // Query by name and/or email meta. + $meta_query = [ + 'relation' => 'OR', + [ + 'key' => 'user_name', + 'value' => sanitize_text_field( $search_term ), + 'compare' => 'LIKE', + ], + [ + 'key' => 'user_email', + 'value' => sanitize_text_field( $search_term ), + 'compare' => 'LIKE', + ] + ]; + + $query->set( 'meta_query', $meta_query ); - // Get Post IDs by query. + unset( $query->query_vars['s'] ); } From 881cc5ba43d7e8453045083f74fc5118b60ffa49 Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Thu, 15 Feb 2024 16:04:54 -0500 Subject: [PATCH 3/3] fix: account for search by order number --- includes/hub/admin/class-woo.php | 39 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/includes/hub/admin/class-woo.php b/includes/hub/admin/class-woo.php index 9b5c4b79..6ebbe860 100644 --- a/includes/hub/admin/class-woo.php +++ b/includes/hub/admin/class-woo.php @@ -185,27 +185,28 @@ public static function parse_query( $query ) { $search_term = $query->query_vars['s']; - // Query by name and/or email meta. - $meta_query = [ - 'relation' => 'OR', - [ - 'key' => 'user_name', - 'value' => sanitize_text_field( $search_term ), - 'compare' => 'LIKE', - ], - [ - 'key' => 'user_email', - 'value' => sanitize_text_field( $search_term ), - 'compare' => 'LIKE', - ] - ]; - - $query->set( 'meta_query', $meta_query ); - - unset( $query->query_vars['s'] ); + if ( ! is_numeric( $search_term ) ) { + // Query by name and/or email meta. + $meta_query = [ + 'relation' => 'OR', + [ + 'key' => 'user_name', + 'value' => sanitize_text_field( $search_term ), + 'compare' => 'LIKE', + ], + [ + 'key' => 'user_email', + 'value' => sanitize_text_field( $search_term ), + 'compare' => 'LIKE', + ], + ]; + + $query->set( 'meta_query', $meta_query ); + + unset( $query->query_vars['s'] ); + } } - /** * Modify columns on post type table *