-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeom-live-search.php
More file actions
183 lines (160 loc) · 5.39 KB
/
meom-live-search.php
File metadata and controls
183 lines (160 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Plugin Name: MEOM Live Search
* Plugin Uri: https://github.com/MEOM/meom-live-search
* Description: WordPress plugin for showing live search results. Compatible with Polylang and Relevanssi plugins.
* Version: 1.0.5
* Author: MEOM
* Author URI: https://www.meom.fi
* Text Domain: meom-live-search
* Domain Path: /languages
*/
/*
MEOM Live Search
Copyright © 2019 MEOM Oy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
define( 'MEOM_LIVE_SEARCH_VERSION', '1.0.5' );
/**
* MEOM Live Search class.
*
* @class MEOM_Live_Search
* @since 1.0.0
* @version 1.0.0
*/
class MEOM_Live_Search {
/**
* Constructor
*
* @since 1.0.0
* @version 1.0.0
*/
public function __construct() {
// Add actions
add_action( 'rest_api_init', array( $this, 'register_live_search_rest_route' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'mls_enqueue_scripts' ) );
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
}
/**
* Initialize rest api point
*
* @since 1.0.0
* @version 1.0.0
*/
public function register_live_search_rest_route() {
register_rest_route('meom-live-search/v1', '/search', array(
'methods' => 'GET',
'callback' => function () {
$default_args = array(
'post_status' => 'publish',
'post_type' => get_post_types( array(
'public' => true,
'exclude_from_search' => false,
) ),
);
$args = apply_filters( 'meom_live_search_args', array_merge( $default_args, $_GET ), $_GET );
ob_start();
if ( function_exists( 'relevanssi_do_query' ) ) {
$query_object = new WP_Query( $args );
$search_results = relevanssi_do_query( $query_object );
} else {
$search_results = new WP_Query( $args );
if ( ! empty( $search_results->posts ) ) {
$search_results = $search_results->posts;
} else {
$search_results = [];
}
}
$this->mls_render_template( [ 'search_results' => $search_results, 's' => $args[ 's' ] ] );
return array( 'resultHTML' => ob_get_clean() );
},
'permission_callback' => '__return_true',
));
}
/**
* Render template
*
* @since 1.0.0
* @version 1.0.0
*/
public function mls_render_template( $args = [] ) {
$template = locate_template( '/meom-live-search/search-results.php' );
if ( ! $template ) {
$template = __DIR__ . '/templates/search-results.php';
}
extract( $args );
include $template;
}
/**
* Enqueue scripts
*
* @since 1.0.0
* @version 1.0.0
*/
public function mls_enqueue_scripts() {
wp_enqueue_script(
'meom-live-search',
plugins_url( '/js/meom-live-search.js', __FILE__ ),
array( 'jquery' ),
MEOM_LIVE_SEARCH_VERSION,
true
);
$live_search = array(
'lang' => ( function_exists( 'pll_current_language' ) ) ? pll_current_language( 'slug' ) : get_locale(),
'locale' => ( function_exists( 'pll_current_language' ) ) ? pll_current_language( 'locale' ) : get_locale(),
'resultsElement' => esc_attr( apply_filters( 'meom_live_search_results_element', '.meom-live-search' ) ),
'searchInput' => esc_attr( apply_filters( 'meom_live_search_input', '[name="s"]' ) ),
);
wp_localize_script(
'meom-live-search',
'liveSearch',
$live_search
);
wp_enqueue_style(
'meom-live-search-style',
plugins_url( 'css/meom-live-search.css', __FILE__ ),
array(),
MEOM_LIVE_SEARCH_VERSION
);
}
/**
* Load plugin textdomain
*
* @since 1.0.0
* @version 1.0.0
*/
public function load_textdomain() {
load_plugin_textdomain(
'meom-live-search',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
}
}
$meom_live_search = new MEOM_Live_Search();
/**
* Change locale to match mls_locale parameter
*
* @since 1.0.5
* @param string $locale Locale.
* @return string
*/
function mls_change_locale_for_rest( $locale ) {
if ( isset( $_GET['mls_locale'] ) ) {
return $_GET['mls_locale'];
}
return $locale;
}
add_filter( 'determine_locale', 'mls_change_locale_for_rest', 9999 );