From 0dfbe8473d366b19224073dbf478bf29413d3b35 Mon Sep 17 00:00:00 2001 From: Jitendra Banjara Date: Wed, 12 Feb 2025 18:15:13 +0530 Subject: [PATCH 1/4] PROD-8402 - Fixed - Group directory search/global search in other languages (WPML) not working --- .../compatibility/class-bb-wpml-helpers.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/bp-core/compatibility/class-bb-wpml-helpers.php b/src/bp-core/compatibility/class-bb-wpml-helpers.php index d51d5150898..632c2d9afe0 100644 --- a/src/bp-core/compatibility/class-bb-wpml-helpers.php +++ b/src/bp-core/compatibility/class-bb-wpml-helpers.php @@ -74,6 +74,9 @@ public function compatibility_init() { add_filter( 'Bp_Search_Posts_sql', array( $this, 'bb_wpml_search_posts_sql' ), 10, 2 ); add_action( 'bb_get_the_profile_field_options_select_html', array( $this, 'bb_wpml_profile_field_options_order' ), 10, 2 ); + + add_filter( 'bp_groups_get_where_conditions', array( $this, 'bb_wpml_groups_dir_search_where_conditions' ), 10, 2 ); + add_filter( 'Bp_Search_Groups_sql', array( $this, 'bb_wpml_groups_search_global_sql' ), 10, 2 ); } /** @@ -335,6 +338,77 @@ public function bb_wpml_profile_field_options_order( $html, $field_obj ) { return $html; } + /** + * Retrieves the translated group name from WPML. + * + * @since BuddyBoss [BBVERSION] + * + * @param string $search_term The original search term. + * + * @return string|false The translated group name if found, otherwise false. + */ + public static function bb_get_wpml_translated_group_name( $search_term ) { + if ( ! class_exists( 'Sitepress' ) || empty( $search_term ) ) { + return false; + } + + global $wpdb; + + return $wpdb->get_var( + $wpdb->prepare( + "SELECT s.value + FROM wp_icl_string_translations st + JOIN wp_icl_strings s ON st.string_id = s.id + WHERE s.context = %s + AND st.value LIKE %s + AND st.language = %s", + 'Buddypress Multilingual', '%' . $wpdb->esc_like( $search_term ) . '%', ICL_LANGUAGE_CODE + ) + ); + } + + /** + * Modifies the WHERE conditions in BuddyPress group search to + * include translated names. + * + * @param array $where The existing WHERE conditions. + * @param array $r The search query arguments. + * + * @return array Modified WHERE conditions. + */ + public function bb_wpml_groups_dir_search_where_conditions( $where, $r ) { + if ( isset( $r['search_terms'] ) && $r['search_terms'] ) { + $translated_name = self::bb_get_wpml_translated_group_name( $r['search_terms'] ); + + if ( ! empty( $translated_name ) ) { + $where['search'] = str_replace( $r['search_terms'], $translated_name, $where['search'] ); + } + } + + return $where; + } + + /** + * Modifies the SQL query for searching BuddyPress groups by replacing + * the search term with its translated version. + * + * @param string $sql_query The original SQL query. + * @param array $args The search query arguments. + * + * @return string Modified SQL query. + */ + public function bb_wpml_groups_search_global_sql( $sql_query, $args ) { + if ( isset( $args['search_term'] ) && $args['search_term'] ) { + $translated_name = self::bb_get_wpml_translated_group_name( $args['search_term'] ); + + if ( ! empty( $translated_name ) ) { + $sql_query = str_replace( $args['search_term'], $translated_name, $sql_query ); + } + } + + return $sql_query; + } + } BB_WPML_Helpers::instance(); From 6b0bea6a1df19f5d1a3021743b2cb9001cac32d9 Mon Sep 17 00:00:00 2001 From: Jitendra Banjara Date: Thu, 13 Feb 2025 14:57:39 +0530 Subject: [PATCH 2/4] PROD-8402 - Update - Dynamic table name use prefix --- src/bp-core/compatibility/class-bb-wpml-helpers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bp-core/compatibility/class-bb-wpml-helpers.php b/src/bp-core/compatibility/class-bb-wpml-helpers.php index 632c2d9afe0..35bd93d1120 100644 --- a/src/bp-core/compatibility/class-bb-wpml-helpers.php +++ b/src/bp-core/compatibility/class-bb-wpml-helpers.php @@ -357,8 +357,8 @@ public static function bb_get_wpml_translated_group_name( $search_term ) { return $wpdb->get_var( $wpdb->prepare( "SELECT s.value - FROM wp_icl_string_translations st - JOIN wp_icl_strings s ON st.string_id = s.id + FROM {$wpdb->prefix}icl_string_translations st + JOIN {$wpdb->prefix}icl_strings s ON st.string_id = s.id WHERE s.context = %s AND st.value LIKE %s AND st.language = %s", From 08bf624dbbc9dd14656672c886273a8624bbc49f Mon Sep 17 00:00:00 2001 From: Kartik Suthar Date: Thu, 13 Feb 2025 15:09:57 +0530 Subject: [PATCH 3/4] PROD-8402 - phpcbf fixes --- .../compatibility/class-bb-wpml-helpers.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/bp-core/compatibility/class-bb-wpml-helpers.php b/src/bp-core/compatibility/class-bb-wpml-helpers.php index 35bd93d1120..6e3d59c2921 100644 --- a/src/bp-core/compatibility/class-bb-wpml-helpers.php +++ b/src/bp-core/compatibility/class-bb-wpml-helpers.php @@ -69,7 +69,7 @@ public function compatibility_init() { // Forum/Topic. add_filter( 'bbp_after_has_topics_parse_args', array( $this, 'bb_wpml_member_profile_topic_reply' ) ); add_filter( 'bbp_after_has_replies_parse_args', array( $this, 'bb_wpml_member_profile_topic_reply' ) ); - + // Fix incorrect search results for Global Search for translated/non-translated posts. add_filter( 'Bp_Search_Posts_sql', array( $this, 'bb_wpml_search_posts_sql' ), 10, 2 ); @@ -268,9 +268,9 @@ public function bb_wpml_search_posts_sql( $sql_query, $args ) { defined( 'ICL_LANGUAGE_CODE' ) && $sitepress->is_translated_post_type( $args['post_type'] ) ) { - global $wpdb; + global $wpdb; $sql_query .= " AND EXISTS ( - SELECT 1 + SELECT 1 FROM {$wpdb->prefix}icl_translations t WHERE t.element_type = CONCAT('post_', %s) AND t.language_code = %s @@ -316,9 +316,12 @@ public function bb_wpml_profile_field_options_order( $html, $field_obj ) { } // Sort the array by the 'text' element. - usort( $options, function ( $a, $b ) { - return strcmp( $a['text'], $b['text'] ); - } ); + usort( + $options, + function ( $a, $b ) { + return strcmp( $a['text'], $b['text'] ); + } + ); if ( 'desc' === $order_by ) { $first = array_shift( $options ); @@ -356,13 +359,15 @@ public static function bb_get_wpml_translated_group_name( $search_term ) { return $wpdb->get_var( $wpdb->prepare( - "SELECT s.value + "SELECT s.value FROM {$wpdb->prefix}icl_string_translations st JOIN {$wpdb->prefix}icl_strings s ON st.string_id = s.id WHERE s.context = %s AND st.value LIKE %s AND st.language = %s", - 'Buddypress Multilingual', '%' . $wpdb->esc_like( $search_term ) . '%', ICL_LANGUAGE_CODE + 'Buddypress Multilingual', + '%' . $wpdb->esc_like( $search_term ) . '%', + ICL_LANGUAGE_CODE ) ); } @@ -408,7 +413,6 @@ public function bb_wpml_groups_search_global_sql( $sql_query, $args ) { return $sql_query; } - } BB_WPML_Helpers::instance(); From 17d040ddc3b5537d0c7351ef7a2aa39c48998c21 Mon Sep 17 00:00:00 2001 From: Kartik Suthar Date: Thu, 13 Feb 2025 15:10:52 +0530 Subject: [PATCH 4/4] PROD-8402 - phpcbf fixes --- src/bp-core/compatibility/class-bb-wpml-helpers.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bp-core/compatibility/class-bb-wpml-helpers.php b/src/bp-core/compatibility/class-bb-wpml-helpers.php index 6e3d59c2921..6f673600c29 100644 --- a/src/bp-core/compatibility/class-bb-wpml-helpers.php +++ b/src/bp-core/compatibility/class-bb-wpml-helpers.php @@ -268,7 +268,8 @@ public function bb_wpml_search_posts_sql( $sql_query, $args ) { defined( 'ICL_LANGUAGE_CODE' ) && $sitepress->is_translated_post_type( $args['post_type'] ) ) { - global $wpdb; + global $wpdb; + $sql_query .= " AND EXISTS ( SELECT 1 FROM {$wpdb->prefix}icl_translations t