| @@ -1,42 +1,85 @@ | ||
| 1 | 1 | <div class="betterdocs-search-result-wrap"> |
| 2 | - <ul class="docs-search-result"> | |
| 3 | - <?php | |
| 4 | - if ( $search_results->have_posts() ) { | |
| 5 | - $input_not_found = ''; | |
| 6 | - while ( $search_results->have_posts() ): $search_results->the_post(); | |
| 7 | - preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_the_content(), $matches ); | |
| 2 | + <ul class="docs-search-result"> | |
| 3 | + <?php | |
| 4 | +// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- view template receives variables via extract(); prefixing is impractical. | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | +if ( $search_results->have_posts() ) { | |
| 9 | + $input_not_found = ''; | |
| 10 | + while ( $search_results->have_posts() ) : | |
| 11 | + $search_results->the_post(); | |
| 12 | + preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_the_content(), $matches ); | |
| 8 | 13 | |
| 9 | - if ( $matches[1] ) { | |
| 10 | - $first_img = $matches[1][0]; | |
| 11 | - } else { | |
| 12 | - $first_img = ''; | |
| 13 | - } | |
| 14 | + if ( $matches[1] ) { | |
| 15 | + $first_img = $matches[1][0]; | |
| 16 | + } else { | |
| 17 | + $first_img = ''; | |
| 18 | + } | |
| 14 | 19 | |
| 15 | - $terms = get_the_terms( get_the_ID(), 'doc_category' ); | |
| 16 | - $terms_name = []; | |
| 20 | + $terms = get_the_terms( get_the_ID(), 'doc_category' ); | |
| 21 | + $terms_name = []; | |
| 17 | 22 | |
| 18 | - if ( $terms && ! is_wp_error( $terms ) ) { | |
| 19 | - foreach ( $terms as $term ) { | |
| 20 | - $terms_name[] = $term->name; | |
| 21 | - } | |
| 22 | - } | |
| 23 | + if ( $terms && ! is_wp_error( $terms ) ) { | |
| 24 | + foreach ( $terms as $term ) { | |
| 25 | + $terms_name[] = $term->name; | |
| 26 | + } | |
| 27 | + } | |
| 23 | 28 | |
| 24 | - $all_terms = join( ", ", $terms_name ); | |
| 25 | - $icon = ''; | |
| 26 | - $search_result_image = betterdocs()->settings->get( 'search_result_image' ); | |
| 29 | + $all_terms = join( ', ', $terms_name ); | |
| 30 | + $icon = ''; | |
| 31 | + $search_result_image = betterdocs()->settings->get( 'search_result_image' ); | |
| 27 | 32 | |
| 28 | - if ( $search_result_image == 1 && has_post_thumbnail() ): | |
| 29 | - $icon = get_the_post_thumbnail(); | |
| 30 | - elseif ( $search_result_image == 1 && ! empty( $first_img ) ): | |
| 31 | - $icon = '<img src="' . $first_img . '" alt="">'; | |
| 32 | - endif; | |
| 33 | + if ( $search_result_image == 1 && has_post_thumbnail() ) : | |
| 34 | + $icon = get_the_post_thumbnail(); | |
| 35 | + elseif ( $search_result_image == 1 && ! empty( $first_img ) ) : | |
| 36 | + $icon = '<img src="' . $first_img . '" alt="">'; | |
| 37 | + endif; | |
| 33 | 38 | |
| 34 | - echo '<li>' . $icon . '<a href="' . get_permalink() . '"><span class="betterdocs-search-title">' . betterdocs()->template_helper->kses( get_the_title() ) . '</span><br><span class="betterdocs-search-category">' . $all_terms . '</span></a></li>'; | |
| 35 | - endwhile; | |
| 36 | - } else { | |
| 37 | - $input_not_found = $search_input; | |
| 38 | - echo '<li>' . stripslashes( betterdocs()->settings->get( 'search_not_found_text' ) ) . '</li>'; | |
| 39 | - } | |
| 40 | - ?> | |
| 41 | - </ul> | |
| 39 | + // Get the correct permalink for the post's language | |
| 40 | + $post_id = get_the_ID(); | |
| 41 | + $permalink = get_permalink( $post_id ); | |
| 42 | + | |
| 43 | + // Handle WPML permalinks | |
| 44 | + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) { | |
| 45 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML public integration filter. | |
| 46 | + $post_language = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $post_id, 'element_type' => 'post_docs' ) ); | |
| 47 | + | |
| 48 | + if ( $post_language ) { | |
| 49 | + global $sitepress; | |
| 50 | + $current_lang = $sitepress ? $sitepress->get_current_language() : ''; | |
| 51 | + | |
| 52 | + if ( $post_language !== $current_lang ) { | |
| 53 | + $permalink = add_query_arg( 'lang', $post_language, $permalink ); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + } | |
| 57 | + // Handle TranslatePress permalinks | |
| 58 | + elseif ( class_exists( '\TRP_Translate_Press' ) ) { | |
| 59 | + global $TRP_LANGUAGE; | |
| 60 | + $trp = \TRP_Translate_Press::get_trp_instance(); | |
| 61 | + if ( isset( $trp ) && method_exists( $trp, 'get_component' ) ) { | |
| 62 | + $trp_settings = $trp->get_component( 'settings' ); | |
| 63 | + $trp_url_converter = $trp->get_component( 'url_converter' ); | |
| 64 | + | |
| 65 | + if ( $trp_settings && $trp_url_converter && isset( $TRP_LANGUAGE ) ) { | |
| 66 | + $settings = $trp_settings->get_settings(); | |
| 67 | + $default_lang = isset( $settings['default-language'] ) ? $settings['default-language'] : 'en_US'; | |
| 68 | + | |
| 69 | + // If we're not on the default language, ensure the URL has the language prefix | |
| 70 | + if ( $TRP_LANGUAGE && $TRP_LANGUAGE !== $default_lang ) { | |
| 71 | + $permalink = $trp_url_converter->get_url_for_language( $TRP_LANGUAGE, $permalink ); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + echo '<li>' . $icon . '<a href="' . $permalink . '"><span class="betterdocs-search-title">' . betterdocs()->template_helper->kses( get_the_title() ) . '</span><br><span class="betterdocs-search-category">' . $all_terms . '</span></a></li>'; //phpcs:ignore | |
| 78 | + endwhile; | |
| 79 | + } else { | |
| 80 | + $input_not_found = $search_input; | |
| 81 | + echo '<li>' . esc_html( betterdocs()->settings->get( 'search_not_found_text' ) ) . '</li>'; | |
| 82 | + } | |
| 83 | + ?> | |
| 84 | + </ul> | |
| 42 | 85 | </div> |