-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-load-more-for-searchwp.php
143 lines (127 loc) · 3.93 KB
/
ajax-load-more-for-searchwp.php
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
<?php
/**
* Plugin Name: Ajax Load More for SearchWP
* Plugin URI: http://connekthq.com/plugins/ajax-load-more/extensions/searchwp/
* Description: An Ajax Load More extension that adds compatibility with SearchWP
* Text Domain: ajax-load-more-for-searchwp
* Author: Darren Cooney
* Author URI: https://connekthq.com
* Version: 1.0.4
* License: GPL
* Copyright: Connekt Media & Darren Cooney
*
* @package ALM_SearchWP
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Installation hook.
*/
function alm_searchwp_install() {
if ( ! is_plugin_active( 'ajax-load-more/ajax-load-more.php' ) ) {
set_transient( 'alm_searchwp_admin_notice', true, 5 );
}
}
register_activation_hook( __FILE__, 'alm_searchwp_install' );
/**
* Display admin notice if plugin does not meet the requirements.
*/
function alm_searchwp_admin_notice() {
$slug = 'ajax-load-more';
// Ajax Load More Notice.
if ( get_transient( 'alm_searchwp_admin_notice' ) ) {
$install_url = get_admin_url() . '/update.php?action=install-plugin&plugin=' . $slug . '&_wpnonce=' . wp_create_nonce( 'install-plugin_' . $slug );
$message = '<div class="error">';
$message .= '<p>You must install and activate the core Ajax Load More plugin before using the Ajax Load More SearchWP extension.</p>';
$message .= '<p>' . sprintf( '<a href="%s" class="button-primary">%s</a>', $install_url, 'Install Ajax Load More Now' ) . '</p>';
$message .= '</div>';
echo wp_kses_post( $message );
delete_transient( 'alm_searchwp_admin_notice' );
}
}
add_action( 'admin_notices', 'alm_searchwp_admin_notice' );
if ( ! class_exists( 'ALM_SearchWP' ) ) :
/**
* ALM SearchWP Class.
*
* @author ConnektMedia <[email protected]>
* @since 1.0
*/
class ALM_SearchWP {
/**
* Construct class.
*
* @author ConnektMedia <[email protected]>
* @since 1.0
*/
public function __construct() {
add_filter( 'alm_searchwp', [ &$this, 'alm_searchwp_get_posts' ], 10, 2 );
}
/**
* Get searchwp search results and return post ids in post__in wp_query param
*
* @param array $args The current query arguments.
* @param string $engine The search engine slug.
* @return array $args
* @author ConnektMedia <[email protected]>
* @since 1.0
*/
public function alm_searchwp_get_posts( $args, $engine ) {
if ( class_exists( '\SearchWP\Query' ) ) {
if ( empty( $engine ) || ! isset( $engine ) ) {
$engine = 'default'; // set search engine.
}
$term = sanitize_text_field( $args['s'] );
$swp_query = new \SearchWP\Query(
$term,
[
'engine' => $engine,
'per_page' => -1,
'fields' => 'ids',
]
);
if ( ! empty( $swp_query->results ) ) {
$args['post__in'] = $swp_query->results;
$args['orderby'] = 'post__in'; // override orderby to relevance.
$args['search'] = $term; // Reset 's' term value.
$args['s'] = ''; // Reset 's' term value.
}
return $args;
}
}
}
/**
* Highlight a search term using SearchWP Highlighter.
*
* @param string $haystack The string to search through.
* @param array $args The ALM arguements.
* @return string
* @author ConnektMedia <[email protected]>
* @since 1.1
*/
function alm_searchwp_highlight( $haystack = '', $args = [] ) {
if ( ! class_exists( '\SearchWP\Highlighter' ) || empty( $haystack ) || empty( $args ) ) {
return;
}
global $post;
$highlighter = new \SearchWP\Highlighter();
$needle = isset( $args['search'] ) ? sanitize_text_field( $args['search'] ) : '';
return $highlighter->apply( $haystack, $needle );
}
/**
* The main function responsible for returning the one true ALM_SearchWP Instance.
*
* @return object $alm_searchwp
* @author ConnektMedia <[email protected]>
* @since 1.0
*/
function alm_searchwp() {
global $alm_searchwp;
if ( ! isset( $alm_searchwp ) ) {
$alm_searchwp = new ALM_SearchWP();
}
return $alm_searchwp;
}
alm_searchwp();
endif;