Plugin Directory

Changeset 2218814


Ignore:
Timestamp:
12/27/2019 10:38:14 PM (6 years ago)
Author:
richaber
Message:

Update to version 1.2.0

Location:
wp-search-with-algolia
Files:
2 added
10 deleted
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • wp-search-with-algolia/tags/1.2.0/README.txt

    r2194088 r2218814  
    66Tested up to: 5.3
    77Requires PHP: 7.2
    8 Stable tag: 1.1.0
     8Stable tag: 1.2.0
    99License: GNU General Public License v2.0, MIT License
    1010
     
    109109
    110110Follow along with the changelog on [Github](https://github.com/WebDevStudios/wp-search-with-algolia/releases).
     111
     112= 1.2.0 =
     113* Use filtered value of 'hitsPerPage' as 'posts_per_page' query param
     114* Fix broken SVG
     115* Add highlighting to backend search results - props @philipnewcomer
     116
     117= 1.1.0 =
     118* Minimum PHP version requirement is now PHP 7.2
     119* Minimum WordPress version requirement is now WP 5.0
     120* Internationalization/localization improvements, textdomain matches plugin slug
     121* Addressed a potential WSOD if minimum PHP and WP version requirements were not met
     122* Tested on WP 5.3
     123
     124= 1.0.0 =
     125* Initial release.
  • wp-search-with-algolia/tags/1.2.0/algolia.php

    r2194088 r2218814  
    55 * Plugin URI:        https://github.com/WebDevStudios/wp-search-with-algolia
    66 * Description:       Integrate the powerful Algolia search service with WordPress
    7  * Version:           1.1.0
     7 * Version:           1.2.0
    88 * Requires at least: 5.0
    99 * Requires PHP:      7.2
     
    2525
    2626// The Algolia Search plugin version.
    27 define( 'ALGOLIA_VERSION', '1.1.0' );
     27define( 'ALGOLIA_VERSION', '1.2.0' );
    2828
    2929// The minmum required PHP version.
  • wp-search-with-algolia/tags/1.2.0/css/algolia-instantsearch.css

    r2194088 r2218814  
    6767    overflow: hidden;
    6868    white-space: nowrap;
    69     background-image: url('data:image/svg+xml;utf8,<?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 21.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  --> <svg version="1.1" baseProfile="basic" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1366 362" xml:space="preserve"> <style type="text/css"> .st0{fill:url(#SVGID_1_);} .st1{fill:#FFFFFF;} .st2{fill:#182359;} </style> <g> <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="428.2578" y1="404.1504" x2="434.1453" y2="409.8504" gradientTransform="matrix(94.045 0 0 -94.072 -40381.5273 38479.5195)"> <stop  offset="0" style="stop-color:#00AEFF"/> <stop  offset="1" style="stop-color:#3369E7"/> </linearGradient> <path class="st0" d="M61.8,15.4h242.8c23.9,0,43.4,19.4,43.4,43.4v242.9c0,23.9-19.4,43.4-43.4,43.4H61.8 c-23.9,0-43.4-19.4-43.4-43.4V58.7C18.4,34.8,37.8,15.4,61.8,15.4z"/> <path class="st1" d="M187,98.7c-51.4,0-93.1,41.7-93.1,93.2S135.6,285,187,285s93.1-41.7,93.1-93.2S238.5,98.7,187,98.7z M187,257.5c-36.2,0-65.6-29.4-65.6-65.6s29.4-65.6,65.6-65.6s65.6,29.4,65.6,65.6S223.3,257.5,187,257.5z M187,139.7v48.9 c0,1.4,1.5,2.4,2.8,1.7l43.4-22.5c1-0.5,1.3-1.7,0.8-2.7c-9-15.8-25.7-26.6-45-27.3C188,137.8,187,138.6,187,139.7z M126.2,103.8 l-5.7-5.7c-5.6-5.6-14.6-5.6-20.2,0l-6.8,6.8c-5.6,5.6-5.6,14.6,0,20.2l5.6,5.6c0.9,0.9,2.2,0.7,3-0.2c3.3-4.5,6.9-8.8,10.9-12.8 c4.1-4.1,8.3-7.7,12.9-11C126.9,106.1,127,104.7,126.2,103.8z M217.5,89V77.7c0-7.9-6.4-14.3-14.3-14.3h-33.3 c-7.9,0-14.3,6.4-14.3,14.3v11.6c0,1.3,1.2,2.2,2.5,1.9c9.3-2.7,19.1-4.1,29-4.1c9.5,0,18.9,1.3,28,3.8 C216.3,91.2,217.5,90.3,217.5,89z"/> <g transform="translate(121 2)"> <path class="st2" d="M721.5,265.6c0,26.7-6.8,46.2-20.5,58.6c-13.7,12.4-34.6,18.6-62.8,18.6c-10.3,0-31.7-2-48.8-5.8l6.3-31 c14.3,3,33.2,3.8,43.1,3.8c15.7,0,26.9-3.2,33.6-9.6s10-15.9,10-28.5v-6.4c-3.9,1.9-9,3.8-15.3,5.8c-6.3,1.9-13.6,2.9-21.8,2.9 c-10.8,0-20.6-1.7-29.5-5.1c-8.9-3.4-16.6-8.4-22.9-15c-6.3-6.6-11.3-14.9-14.8-24.8c-3.5-9.9-5.3-27.6-5.3-40.6 c0-12.2,1.9-27.5,5.6-37.7c3.8-10.2,9.2-19,16.5-26.3c7.2-7.3,16-12.9,26.3-17c10.3-4.1,22.4-6.7,35.5-6.7 c12.7,0,24.4,1.6,35.8,3.5c11.4,1.9,21.1,3.9,29,6.1V265.6z M612.8,188.4c0,16.4,3.6,34.6,10.8,42.2c7.2,7.6,16.5,11.4,27.9,11.4 c6.2,0,12.1-0.9,17.6-2.6c5.5-1.7,9.9-3.7,13.4-6.1v-97.1c-2.8-0.6-14.5-3-25.8-3.3c-14.2-0.4-25,5.4-32.6,14.7 C616.6,156.9,612.8,173.2,612.8,188.4z M907.1,188.4c0,13.2-1.9,23.2-5.8,34.1s-9.4,20.2-16.5,27.9c-7.1,7.7-15.6,13.7-25.6,17.9 s-25.4,6.6-33.1,6.6c-7.7-0.1-23-2.3-32.9-6.6c-9.9-4.3-18.4-10.2-25.5-17.9c-7.1-7.7-12.6-17-16.6-27.9s-6-20.9-6-34.1 c0-13.2,1.8-25.9,5.8-36.7c4-10.8,9.6-20,16.8-27.7s15.8-13.6,25.6-17.8c9.9-4.2,20.8-6.2,32.6-6.2c11.8,0,22.7,2.1,32.7,6.2 c10,4.2,18.6,10.1,25.6,17.8c7.1,7.7,12.6,16.9,16.6,27.7C905,162.5,907.1,175.2,907.1,188.4z M867.1,188.5 c0-16.9-3.7-31-10.9-40.8c-7.2-9.9-17.3-14.8-30.2-14.8c-12.9,0-23,4.9-30.2,14.8c-7.2,9.9-10.7,23.9-10.7,40.8 c0,17.1,3.6,28.6,10.8,38.5c7.2,10,17.3,14.9,30.2,14.9c12.9,0,23-5,30.2-14.9C863.5,217,867.1,205.6,867.1,188.5z M994.2,274.9 c-64.1,0.3-64.1-51.8-64.1-60.1L930,30l39.1-6.2v183.6c0,4.7,0,34.5,25.1,34.6V274.9z M1063.1,274.9h-39.3V106.1l39.3-6.2V274.9z M1043.4,81.4c13.1,0,23.8-10.6,23.8-23.7s-10.6-23.7-23.8-23.7s-23.8,10.6-23.8,23.7S1030.3,81.4,1043.4,81.4z M1160.8,100 c12.9,0,23.8,1.6,32.6,4.8c8.8,3.2,15.9,7.7,21.1,13.4s8.9,13.5,11.1,21.7c2.3,8.2,3.4,17.2,3.4,27.1v100.6 c-6,1.3-15.1,2.8-27.3,4.6s-25.9,2.7-41.1,2.7c-10.1,0-19.4-1-27.7-2.9c-8.4-1.9-15.5-5-21.5-9.3c-5.9-4.3-10.5-9.8-13.9-16.6 c-3.3-6.8-5-16.4-5-26.4c0-9.6,1.9-15.7,5.6-22.3c3.8-6.6,8.9-12,15.3-16.2c6.5-4.2,13.9-7.2,22.4-9s17.4-2.7,26.6-2.7 c4.3,0,8.8,0.3,13.6,0.8s9.8,1.4,15.2,2.7v-6.4c0-4.5-0.5-8.8-1.6-12.8c-1.1-4.1-3-7.6-5.6-10.7c-2.7-3.1-6.2-5.5-10.6-7.2 s-10-3-16.7-3c-9,0-17.2,1.1-24.7,2.4c-7.5,1.3-13.7,2.8-18.4,4.5l-4.7-32.1c4.9-1.7,12.2-3.4,21.6-5.1S1150,100,1160.8,100z M1164.1,241.9c12,0,20.9-0.7,27.1-1.9v-39.8c-2.2-0.6-5.3-1.3-9.4-1.9c-4.1-0.6-8.6-1-13.6-1c-4.3,0-8.7,0.3-13.1,1 c-4.4,0.6-8.4,1.8-11.9,3.5s-6.4,4.1-8.5,7.2c-2.2,3.1-3.2,4.9-3.2,9.6c0,9.2,3.2,14.5,9,18 C1146.4,240.2,1154.2,241.9,1164.1,241.9z M391.9,101c12.9,0,23.8,1.6,32.6,4.8c8.8,3.2,15.9,7.7,21.1,13.4 c5.3,5.8,8.9,13.5,11.1,21.7c2.3,8.2,3.4,17.2,3.4,27.1v100.6c-6,1.3-15.1,2.8-27.3,4.6c-12.2,1.8-25.9,2.7-41.1,2.7 c-10.1,0-19.4-1-27.7-2.9c-8.4-1.9-15.5-5-21.5-9.3c-5.9-4.3-10.5-9.8-13.9-16.6c-3.3-6.8-5-16.4-5-26.4c0-9.6,1.9-15.7,5.6-22.3 c3.8-6.6,8.9-12,15.3-16.2c6.5-4.2,13.9-7.2,22.4-9s17.4-2.7,26.6-2.7c4.3,0,8.8,0.3,13.6,0.8c4.7,0.5,9.8,1.4,15.2,2.7v-6.4 c0-4.5-0.5-8.8-1.6-12.8c-1.1-4.1-3-7.6-5.6-10.7c-2.7-3.1-6.2-5.5-10.6-7.2c-4.4-1.7-10-3-16.7-3c-9,0-17.2,1.1-24.7,2.4 c-7.5,1.3-13.7,2.8-18.4,4.5l-4.7-32.1c4.9-1.7,12.2-3.4,21.6-5.1C371,101.8,381.1,101,391.9,101z M395.3,243 c12,0,20.9-0.7,27.1-1.9v-39.8c-2.2-0.6-5.3-1.3-9.4-1.9c-4.1-0.6-8.6-1-13.6-1c-4.3,0-8.7,0.3-13.1,1c-4.4,0.6-8.4,1.8-11.9,3.5 c-3.5,1.7-6.4,4.1-8.5,7.2c-2.2,3.1-3.2,4.9-3.2,9.6c0,9.2,3.2,14.5,9,18C377.5,241.2,385.4,243,395.3,243z M553.8,274.9 c-64.1,0.3-64.1-51.8-64.1-60.1L489.6,30l39.1-6.2v183.6c0,4.7,0,34.5,25.1,34.6V274.9z"/> </g> </g> </svg>');
     69    background-image: url('algolia-logo.svg');
    7070    background-repeat: no-repeat;
    7171    background-size: contain;
  • wp-search-with-algolia/tags/1.2.0/includes/class-algolia-search.php

    r2089442 r2218814  
    44
    55    /**
     6     * @var array
     7     */
     8    private $current_page_hits = [];
     9
     10    /**
    611     * @var int
    712     */
    8     private $nb_hits;
     13    private $total_hits;
    914
    1015    /**
     
    1924        $this->index = $index;
    2025
     26        add_action( 'loop_start', [ $this, 'begin_highlighting' ] );
    2127        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
     28        add_action( 'wp_head', [ $this, 'output_highlighting_bundled_styles' ] );
    2229    }
    2330
     
    5057        }
    5158
    52         $posts_per_page = (int) get_option( 'posts_per_page' );
    53 
     59        /**
     60         * Filters the array of parameters used in the Algolia Index search.
     61         *
     62         * @author WebDevStudios <contact@webdevstudios.com>
     63         * @since  1.0.0
     64         * @since  1.2.0 Introduced 'highlightPreTag' and 'highlightPostTag` parameters.
     65         *
     66         * @param array $params {
     67         *     Search parameters for the Algolia Index search.
     68         *
     69         *     @type string $attributesToRetrieve Which attributes to retrieve.
     70         *     @type int    $hitsPerPage          Pagination parameter. The number of hits per page to retrieve.
     71         *     @type int    $page                 Pagination parameter. The page of results to retrieve.
     72         *     @type string $highlightPreTag      HTML string to insert before highlights in result snippets.
     73         *     @type string $highlightPostTag     HTML string to insert after highlights in result snippets.
     74         * }
     75         */
    5476        $params = apply_filters(
    55             'algolia_search_params', array(
     77            'algolia_search_params',
     78            array(
    5679                'attributesToRetrieve' => 'post_id',
    57                 'hitsPerPage'          => $posts_per_page,
     80                'hitsPerPage'          => (int) get_option( 'posts_per_page' ),
    5881                'page'                 => $current_page - 1, // Algolia pages are zero indexed.
     82                'highlightPreTag'      => '<em class="algolia-search-highlight">',
     83                'highlightPostTag'     => '</em>',
    5984            )
    6085        );
     
    7499        add_filter( 'posts_search', array( $this, 'posts_search' ), 10, 2 );
    75100
     101        // Store the current page hits, so that we can use them for highlighting later on.
     102        foreach ( $results['hits'] as $hit ) {
     103            $this->current_page_hits[ $hit['post_id'] ] = $hit;
     104        }
     105
    76106        // Store the total number of its, so that we can hook into the `found_posts`.
    77107        // This is useful for pagination.
    78         $this->nb_hits = $results['nbHits'];
     108        $this->total_hits = $results['nbHits'];
    79109
    80110        $post_ids = array();
     
    90120        }
    91121
    92         $query->set( 'posts_per_page', $posts_per_page );
     122        $query->set( 'posts_per_page', $params['hitsPerPage'] );
    93123        $query->set( 'offset', 0 );
    94124
     
    118148     */
    119149    public function found_posts( $found_posts, WP_Query $query ) {
    120         return $this->should_filter_query( $query ) ? $this->nb_hits : $found_posts;
     150        return $this->should_filter_query( $query ) ? $this->total_hits : $found_posts;
    121151    }
    122152
     
    135165        return $this->should_filter_query( $query ) ? '' : $search;
    136166    }
     167
     168    /**
     169     * Output the bundled styles for highlighting search result matches, if enabled.
     170     */
     171    public function output_highlighting_bundled_styles() {
     172        if ( ! $this->highlighting_enabled() ) {
     173            return;
     174        }
     175
     176        if ( ! apply_filters( 'algolia_search_highlighting_enable_bundled_styles', true ) ) {
     177            return;
     178        }
     179
     180        ?>
     181        <style>
     182            .algolia-search-highlight {
     183                background-color: #fffbcc;
     184                border-radius: 2px;
     185                font-style: normal;
     186            }
     187        </style>
     188        <?php
     189    }
     190
     191    /**
     192     * Begin highlighting search result matches, if enabled.
     193     *
     194     * This method is called on the loop_start action, where we want to begin highlighting search result matches.
     195     *
     196     * @param WP_Query $query
     197     */
     198    public function begin_highlighting( $query ) {
     199        if ( ! $this->should_filter_query( $query ) ) {
     200            return;
     201        }
     202
     203        if ( ! $this->highlighting_enabled() ) {
     204            return;
     205        }
     206
     207        add_filter( 'the_title', [ $this, 'highlight_the_title' ], 10, 2 );
     208        add_filter( 'get_the_excerpt', [ $this, 'highlight_get_the_excerpt' ], 10, 2 );
     209
     210        add_action( 'loop_end', [ $this, 'end_highlighting' ] );
     211    }
     212
     213    /**
     214     * Stop highlighting search result matches.
     215     *
     216     * This method is called on the loop_end action, where we want to stop highlighting search result matches.
     217     *
     218     * @param WP_Query $query
     219     */
     220    public function end_highlighting( $query ) {
     221        remove_filter( 'the_title', [ $this, 'highlight_the_title' ], 10 );
     222        remove_filter( 'get_the_excerpt', [ $this, 'highlight_get_the_excerpt' ], 10 );
     223
     224        remove_action( 'loop_end', [ $this, 'end_highlighting' ] );
     225    }
     226
     227    /**
     228     * Filter the_title, replacing it with the highlighted title from the Algolia index.
     229     *
     230     * @param string $title
     231     * @param int    $post_id
     232     *
     233     * @return string
     234     */
     235    public function highlight_the_title( $title, $post_id ) {
     236        $highlighted_title = $this->current_page_hits[ $post_id ]['_highlightResult']['post_title']['value'] ?? null;
     237
     238        if ( ! empty( $highlighted_title ) ) {
     239            $title = $highlighted_title;
     240        }
     241
     242        return $title;
     243    }
     244
     245    /**
     246     * Filter get_the_excerpt, replacing it with the highlighted excerpt from the Algolia index.
     247     *
     248     * @param string  $excerpt
     249     * @param WP_Post $post
     250     *
     251     * @return string
     252     */
     253    public function highlight_get_the_excerpt( $excerpt, $post ) {
     254        $highlighted_excerpt = $this->current_page_hits[ $post->ID ]['_snippetResult']['content']['value'] ?? null;
     255
     256        if ( ! empty( $highlighted_excerpt ) ) {
     257            $excerpt = $highlighted_excerpt;
     258        }
     259
     260        return $excerpt;
     261    }
     262
     263    /**
     264     * Determine whether highlighting is enabled.
     265     *
     266     * @return bool
     267     */
     268    private function highlighting_enabled() : bool {
     269        return apply_filters( 'algolia_search_highlighting_enabled', true );
     270    }
    137271}
  • wp-search-with-algolia/tags/1.2.0/includes/indices/class-algolia-searchable-posts-index.php

    r2194088 r2218814  
    202202            'attributesToSnippet'   => array(
    203203                'post_title:30',
    204                 'content:30',
     204                'content:' . intval( apply_filters( 'excerpt_length', 55 ) ),
    205205            ),
    206206            'snippetEllipsisText'   => '…',
  • wp-search-with-algolia/trunk/README.txt

    r2194088 r2218814  
    66Tested up to: 5.3
    77Requires PHP: 7.2
    8 Stable tag: 1.1.0
     8Stable tag: 1.2.0
    99License: GNU General Public License v2.0, MIT License
    1010
     
    109109
    110110Follow along with the changelog on [Github](https://github.com/WebDevStudios/wp-search-with-algolia/releases).
     111
     112= 1.2.0 =
     113* Use filtered value of 'hitsPerPage' as 'posts_per_page' query param
     114* Fix broken SVG
     115* Add highlighting to backend search results - props @philipnewcomer
     116
     117= 1.1.0 =
     118* Minimum PHP version requirement is now PHP 7.2
     119* Minimum WordPress version requirement is now WP 5.0
     120* Internationalization/localization improvements, textdomain matches plugin slug
     121* Addressed a potential WSOD if minimum PHP and WP version requirements were not met
     122* Tested on WP 5.3
     123
     124= 1.0.0 =
     125* Initial release.
  • wp-search-with-algolia/trunk/algolia.php

    r2194088 r2218814  
    55 * Plugin URI:        https://github.com/WebDevStudios/wp-search-with-algolia
    66 * Description:       Integrate the powerful Algolia search service with WordPress
    7  * Version:           1.1.0
     7 * Version:           1.2.0
    88 * Requires at least: 5.0
    99 * Requires PHP:      7.2
     
    2525
    2626// The Algolia Search plugin version.
    27 define( 'ALGOLIA_VERSION', '1.1.0' );
     27define( 'ALGOLIA_VERSION', '1.2.0' );
    2828
    2929// The minmum required PHP version.
  • wp-search-with-algolia/trunk/css/algolia-instantsearch.css

    r2194088 r2218814  
    6767    overflow: hidden;
    6868    white-space: nowrap;
    69     background-image: url('data:image/svg+xml;utf8,<?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 21.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  --> <svg version="1.1" baseProfile="basic" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1366 362" xml:space="preserve"> <style type="text/css"> .st0{fill:url(#SVGID_1_);} .st1{fill:#FFFFFF;} .st2{fill:#182359;} </style> <g> <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="428.2578" y1="404.1504" x2="434.1453" y2="409.8504" gradientTransform="matrix(94.045 0 0 -94.072 -40381.5273 38479.5195)"> <stop  offset="0" style="stop-color:#00AEFF"/> <stop  offset="1" style="stop-color:#3369E7"/> </linearGradient> <path class="st0" d="M61.8,15.4h242.8c23.9,0,43.4,19.4,43.4,43.4v242.9c0,23.9-19.4,43.4-43.4,43.4H61.8 c-23.9,0-43.4-19.4-43.4-43.4V58.7C18.4,34.8,37.8,15.4,61.8,15.4z"/> <path class="st1" d="M187,98.7c-51.4,0-93.1,41.7-93.1,93.2S135.6,285,187,285s93.1-41.7,93.1-93.2S238.5,98.7,187,98.7z M187,257.5c-36.2,0-65.6-29.4-65.6-65.6s29.4-65.6,65.6-65.6s65.6,29.4,65.6,65.6S223.3,257.5,187,257.5z M187,139.7v48.9 c0,1.4,1.5,2.4,2.8,1.7l43.4-22.5c1-0.5,1.3-1.7,0.8-2.7c-9-15.8-25.7-26.6-45-27.3C188,137.8,187,138.6,187,139.7z M126.2,103.8 l-5.7-5.7c-5.6-5.6-14.6-5.6-20.2,0l-6.8,6.8c-5.6,5.6-5.6,14.6,0,20.2l5.6,5.6c0.9,0.9,2.2,0.7,3-0.2c3.3-4.5,6.9-8.8,10.9-12.8 c4.1-4.1,8.3-7.7,12.9-11C126.9,106.1,127,104.7,126.2,103.8z M217.5,89V77.7c0-7.9-6.4-14.3-14.3-14.3h-33.3 c-7.9,0-14.3,6.4-14.3,14.3v11.6c0,1.3,1.2,2.2,2.5,1.9c9.3-2.7,19.1-4.1,29-4.1c9.5,0,18.9,1.3,28,3.8 C216.3,91.2,217.5,90.3,217.5,89z"/> <g transform="translate(121 2)"> <path class="st2" d="M721.5,265.6c0,26.7-6.8,46.2-20.5,58.6c-13.7,12.4-34.6,18.6-62.8,18.6c-10.3,0-31.7-2-48.8-5.8l6.3-31 c14.3,3,33.2,3.8,43.1,3.8c15.7,0,26.9-3.2,33.6-9.6s10-15.9,10-28.5v-6.4c-3.9,1.9-9,3.8-15.3,5.8c-6.3,1.9-13.6,2.9-21.8,2.9 c-10.8,0-20.6-1.7-29.5-5.1c-8.9-3.4-16.6-8.4-22.9-15c-6.3-6.6-11.3-14.9-14.8-24.8c-3.5-9.9-5.3-27.6-5.3-40.6 c0-12.2,1.9-27.5,5.6-37.7c3.8-10.2,9.2-19,16.5-26.3c7.2-7.3,16-12.9,26.3-17c10.3-4.1,22.4-6.7,35.5-6.7 c12.7,0,24.4,1.6,35.8,3.5c11.4,1.9,21.1,3.9,29,6.1V265.6z M612.8,188.4c0,16.4,3.6,34.6,10.8,42.2c7.2,7.6,16.5,11.4,27.9,11.4 c6.2,0,12.1-0.9,17.6-2.6c5.5-1.7,9.9-3.7,13.4-6.1v-97.1c-2.8-0.6-14.5-3-25.8-3.3c-14.2-0.4-25,5.4-32.6,14.7 C616.6,156.9,612.8,173.2,612.8,188.4z M907.1,188.4c0,13.2-1.9,23.2-5.8,34.1s-9.4,20.2-16.5,27.9c-7.1,7.7-15.6,13.7-25.6,17.9 s-25.4,6.6-33.1,6.6c-7.7-0.1-23-2.3-32.9-6.6c-9.9-4.3-18.4-10.2-25.5-17.9c-7.1-7.7-12.6-17-16.6-27.9s-6-20.9-6-34.1 c0-13.2,1.8-25.9,5.8-36.7c4-10.8,9.6-20,16.8-27.7s15.8-13.6,25.6-17.8c9.9-4.2,20.8-6.2,32.6-6.2c11.8,0,22.7,2.1,32.7,6.2 c10,4.2,18.6,10.1,25.6,17.8c7.1,7.7,12.6,16.9,16.6,27.7C905,162.5,907.1,175.2,907.1,188.4z M867.1,188.5 c0-16.9-3.7-31-10.9-40.8c-7.2-9.9-17.3-14.8-30.2-14.8c-12.9,0-23,4.9-30.2,14.8c-7.2,9.9-10.7,23.9-10.7,40.8 c0,17.1,3.6,28.6,10.8,38.5c7.2,10,17.3,14.9,30.2,14.9c12.9,0,23-5,30.2-14.9C863.5,217,867.1,205.6,867.1,188.5z M994.2,274.9 c-64.1,0.3-64.1-51.8-64.1-60.1L930,30l39.1-6.2v183.6c0,4.7,0,34.5,25.1,34.6V274.9z M1063.1,274.9h-39.3V106.1l39.3-6.2V274.9z M1043.4,81.4c13.1,0,23.8-10.6,23.8-23.7s-10.6-23.7-23.8-23.7s-23.8,10.6-23.8,23.7S1030.3,81.4,1043.4,81.4z M1160.8,100 c12.9,0,23.8,1.6,32.6,4.8c8.8,3.2,15.9,7.7,21.1,13.4s8.9,13.5,11.1,21.7c2.3,8.2,3.4,17.2,3.4,27.1v100.6 c-6,1.3-15.1,2.8-27.3,4.6s-25.9,2.7-41.1,2.7c-10.1,0-19.4-1-27.7-2.9c-8.4-1.9-15.5-5-21.5-9.3c-5.9-4.3-10.5-9.8-13.9-16.6 c-3.3-6.8-5-16.4-5-26.4c0-9.6,1.9-15.7,5.6-22.3c3.8-6.6,8.9-12,15.3-16.2c6.5-4.2,13.9-7.2,22.4-9s17.4-2.7,26.6-2.7 c4.3,0,8.8,0.3,13.6,0.8s9.8,1.4,15.2,2.7v-6.4c0-4.5-0.5-8.8-1.6-12.8c-1.1-4.1-3-7.6-5.6-10.7c-2.7-3.1-6.2-5.5-10.6-7.2 s-10-3-16.7-3c-9,0-17.2,1.1-24.7,2.4c-7.5,1.3-13.7,2.8-18.4,4.5l-4.7-32.1c4.9-1.7,12.2-3.4,21.6-5.1S1150,100,1160.8,100z M1164.1,241.9c12,0,20.9-0.7,27.1-1.9v-39.8c-2.2-0.6-5.3-1.3-9.4-1.9c-4.1-0.6-8.6-1-13.6-1c-4.3,0-8.7,0.3-13.1,1 c-4.4,0.6-8.4,1.8-11.9,3.5s-6.4,4.1-8.5,7.2c-2.2,3.1-3.2,4.9-3.2,9.6c0,9.2,3.2,14.5,9,18 C1146.4,240.2,1154.2,241.9,1164.1,241.9z M391.9,101c12.9,0,23.8,1.6,32.6,4.8c8.8,3.2,15.9,7.7,21.1,13.4 c5.3,5.8,8.9,13.5,11.1,21.7c2.3,8.2,3.4,17.2,3.4,27.1v100.6c-6,1.3-15.1,2.8-27.3,4.6c-12.2,1.8-25.9,2.7-41.1,2.7 c-10.1,0-19.4-1-27.7-2.9c-8.4-1.9-15.5-5-21.5-9.3c-5.9-4.3-10.5-9.8-13.9-16.6c-3.3-6.8-5-16.4-5-26.4c0-9.6,1.9-15.7,5.6-22.3 c3.8-6.6,8.9-12,15.3-16.2c6.5-4.2,13.9-7.2,22.4-9s17.4-2.7,26.6-2.7c4.3,0,8.8,0.3,13.6,0.8c4.7,0.5,9.8,1.4,15.2,2.7v-6.4 c0-4.5-0.5-8.8-1.6-12.8c-1.1-4.1-3-7.6-5.6-10.7c-2.7-3.1-6.2-5.5-10.6-7.2c-4.4-1.7-10-3-16.7-3c-9,0-17.2,1.1-24.7,2.4 c-7.5,1.3-13.7,2.8-18.4,4.5l-4.7-32.1c4.9-1.7,12.2-3.4,21.6-5.1C371,101.8,381.1,101,391.9,101z M395.3,243 c12,0,20.9-0.7,27.1-1.9v-39.8c-2.2-0.6-5.3-1.3-9.4-1.9c-4.1-0.6-8.6-1-13.6-1c-4.3,0-8.7,0.3-13.1,1c-4.4,0.6-8.4,1.8-11.9,3.5 c-3.5,1.7-6.4,4.1-8.5,7.2c-2.2,3.1-3.2,4.9-3.2,9.6c0,9.2,3.2,14.5,9,18C377.5,241.2,385.4,243,395.3,243z M553.8,274.9 c-64.1,0.3-64.1-51.8-64.1-60.1L489.6,30l39.1-6.2v183.6c0,4.7,0,34.5,25.1,34.6V274.9z"/> </g> </g> </svg>');
     69    background-image: url('algolia-logo.svg');
    7070    background-repeat: no-repeat;
    7171    background-size: contain;
  • wp-search-with-algolia/trunk/includes/class-algolia-search.php

    r2089442 r2218814  
    44
    55    /**
     6     * @var array
     7     */
     8    private $current_page_hits = [];
     9
     10    /**
    611     * @var int
    712     */
    8     private $nb_hits;
     13    private $total_hits;
    914
    1015    /**
     
    1924        $this->index = $index;
    2025
     26        add_action( 'loop_start', [ $this, 'begin_highlighting' ] );
    2127        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
     28        add_action( 'wp_head', [ $this, 'output_highlighting_bundled_styles' ] );
    2229    }
    2330
     
    5057        }
    5158
    52         $posts_per_page = (int) get_option( 'posts_per_page' );
    53 
     59        /**
     60         * Filters the array of parameters used in the Algolia Index search.
     61         *
     62         * @author WebDevStudios <contact@webdevstudios.com>
     63         * @since  1.0.0
     64         * @since  1.2.0 Introduced 'highlightPreTag' and 'highlightPostTag` parameters.
     65         *
     66         * @param array $params {
     67         *     Search parameters for the Algolia Index search.
     68         *
     69         *     @type string $attributesToRetrieve Which attributes to retrieve.
     70         *     @type int    $hitsPerPage          Pagination parameter. The number of hits per page to retrieve.
     71         *     @type int    $page                 Pagination parameter. The page of results to retrieve.
     72         *     @type string $highlightPreTag      HTML string to insert before highlights in result snippets.
     73         *     @type string $highlightPostTag     HTML string to insert after highlights in result snippets.
     74         * }
     75         */
    5476        $params = apply_filters(
    55             'algolia_search_params', array(
     77            'algolia_search_params',
     78            array(
    5679                'attributesToRetrieve' => 'post_id',
    57                 'hitsPerPage'          => $posts_per_page,
     80                'hitsPerPage'          => (int) get_option( 'posts_per_page' ),
    5881                'page'                 => $current_page - 1, // Algolia pages are zero indexed.
     82                'highlightPreTag'      => '<em class="algolia-search-highlight">',
     83                'highlightPostTag'     => '</em>',
    5984            )
    6085        );
     
    7499        add_filter( 'posts_search', array( $this, 'posts_search' ), 10, 2 );
    75100
     101        // Store the current page hits, so that we can use them for highlighting later on.
     102        foreach ( $results['hits'] as $hit ) {
     103            $this->current_page_hits[ $hit['post_id'] ] = $hit;
     104        }
     105
    76106        // Store the total number of its, so that we can hook into the `found_posts`.
    77107        // This is useful for pagination.
    78         $this->nb_hits = $results['nbHits'];
     108        $this->total_hits = $results['nbHits'];
    79109
    80110        $post_ids = array();
     
    90120        }
    91121
    92         $query->set( 'posts_per_page', $posts_per_page );
     122        $query->set( 'posts_per_page', $params['hitsPerPage'] );
    93123        $query->set( 'offset', 0 );
    94124
     
    118148     */
    119149    public function found_posts( $found_posts, WP_Query $query ) {
    120         return $this->should_filter_query( $query ) ? $this->nb_hits : $found_posts;
     150        return $this->should_filter_query( $query ) ? $this->total_hits : $found_posts;
    121151    }
    122152
     
    135165        return $this->should_filter_query( $query ) ? '' : $search;
    136166    }
     167
     168    /**
     169     * Output the bundled styles for highlighting search result matches, if enabled.
     170     */
     171    public function output_highlighting_bundled_styles() {
     172        if ( ! $this->highlighting_enabled() ) {
     173            return;
     174        }
     175
     176        if ( ! apply_filters( 'algolia_search_highlighting_enable_bundled_styles', true ) ) {
     177            return;
     178        }
     179
     180        ?>
     181        <style>
     182            .algolia-search-highlight {
     183                background-color: #fffbcc;
     184                border-radius: 2px;
     185                font-style: normal;
     186            }
     187        </style>
     188        <?php
     189    }
     190
     191    /**
     192     * Begin highlighting search result matches, if enabled.
     193     *
     194     * This method is called on the loop_start action, where we want to begin highlighting search result matches.
     195     *
     196     * @param WP_Query $query
     197     */
     198    public function begin_highlighting( $query ) {
     199        if ( ! $this->should_filter_query( $query ) ) {
     200            return;
     201        }
     202
     203        if ( ! $this->highlighting_enabled() ) {
     204            return;
     205        }
     206
     207        add_filter( 'the_title', [ $this, 'highlight_the_title' ], 10, 2 );
     208        add_filter( 'get_the_excerpt', [ $this, 'highlight_get_the_excerpt' ], 10, 2 );
     209
     210        add_action( 'loop_end', [ $this, 'end_highlighting' ] );
     211    }
     212
     213    /**
     214     * Stop highlighting search result matches.
     215     *
     216     * This method is called on the loop_end action, where we want to stop highlighting search result matches.
     217     *
     218     * @param WP_Query $query
     219     */
     220    public function end_highlighting( $query ) {
     221        remove_filter( 'the_title', [ $this, 'highlight_the_title' ], 10 );
     222        remove_filter( 'get_the_excerpt', [ $this, 'highlight_get_the_excerpt' ], 10 );
     223
     224        remove_action( 'loop_end', [ $this, 'end_highlighting' ] );
     225    }
     226
     227    /**
     228     * Filter the_title, replacing it with the highlighted title from the Algolia index.
     229     *
     230     * @param string $title
     231     * @param int    $post_id
     232     *
     233     * @return string
     234     */
     235    public function highlight_the_title( $title, $post_id ) {
     236        $highlighted_title = $this->current_page_hits[ $post_id ]['_highlightResult']['post_title']['value'] ?? null;
     237
     238        if ( ! empty( $highlighted_title ) ) {
     239            $title = $highlighted_title;
     240        }
     241
     242        return $title;
     243    }
     244
     245    /**
     246     * Filter get_the_excerpt, replacing it with the highlighted excerpt from the Algolia index.
     247     *
     248     * @param string  $excerpt
     249     * @param WP_Post $post
     250     *
     251     * @return string
     252     */
     253    public function highlight_get_the_excerpt( $excerpt, $post ) {
     254        $highlighted_excerpt = $this->current_page_hits[ $post->ID ]['_snippetResult']['content']['value'] ?? null;
     255
     256        if ( ! empty( $highlighted_excerpt ) ) {
     257            $excerpt = $highlighted_excerpt;
     258        }
     259
     260        return $excerpt;
     261    }
     262
     263    /**
     264     * Determine whether highlighting is enabled.
     265     *
     266     * @return bool
     267     */
     268    private function highlighting_enabled() : bool {
     269        return apply_filters( 'algolia_search_highlighting_enabled', true );
     270    }
    137271}
  • wp-search-with-algolia/trunk/includes/indices/class-algolia-searchable-posts-index.php

    r2194088 r2218814  
    202202            'attributesToSnippet'   => array(
    203203                'post_title:30',
    204                 'content:30',
     204                'content:' . intval( apply_filters( 'excerpt_length', 55 ) ),
    205205            ),
    206206            'snippetEllipsisText'   => '…',
Note: See TracChangeset for help on using the changeset viewer.