-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdaves-wordpress-live-search.php
More file actions
133 lines (111 loc) · 4.8 KB
/
daves-wordpress-live-search.php
File metadata and controls
133 lines (111 loc) · 4.8 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
<?php
/*
Plugin Name: Dave's WordPress Live Search
Description: Adds "live search" functionality to your WordPress site. Uses the built-in search and jQuery.
Version: 4.6
Author: Dave Ross
Author URI: http://davidmichaelross.com/
Plugin URI: http://wordpress.org/extend/plugins/daves-wordpress-live-search/
Text Domain: daves-wordpress-live-search
*/
/**
* Copyright (c) 2014 Dave Ross <dave@csixty4.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* */
add_action( 'init', 'daves_wp_live_search_init' );
function daves_wp_live_search_init() {
if ( defined( 'DOING_AJAX' ) ) {
include_once "class-daves-wordpress-live-search-results.php";
}
else {
include_once "class-daves-wordpress-live-search.php";
add_action( 'admin_notices', array( 'DavesWordPressLiveSearch', 'admin_notices' ) );
// Register hooks
add_action( 'admin_menu', array( 'DavesWordPressLiveSearch', 'admin_menu' ) );
add_action( 'wp_head', array( 'DavesWordPressLiveSearch', 'head' ) );
add_action( 'admin_head', array( 'DavesWordPressLiveSearch', 'head' ) );
register_activation_hook( __FILE__, array( 'DavesWordPressLiveSearch', 'activate' ) );
add_action( 'admin_enqueue_scripts', array( 'DavesWordPressLiveSearch', 'admin_enqueue_scripts' ) );
DavesWordPressLiveSearch::advanced_search_init();
}
}
// Provide a json_encode implementation if none exists (PHP < 5.2.0)
if ( !function_exists( 'json_encode' ) ) {
require ABSPATH . "/wp-includes/compat.php" ;
}
// Relevanssi "bridge" plugin
class DWLS_Relevanssi_Bridge {
static function init() {
if ( function_exists( 'relevanssi_do_query' ) ) {
add_filter( 'dwls_alter_results', array( 'DWLS_Relevanssi_Bridge', 'dwls_alter_results' ), 10, 3 );
}
}
static function dwls_alter_results( $wpQueryResults, $maxResults, $results ) {
global $wp_query;
// WP_Query::query() set everything up
// Now have Relevanssi do the query over again
// but do it in its own way
// Thanks Mikko!
relevanssi_do_query( $wp_query );
$results->relevanssi = true;
// Mikko says Relevanssi 2.5 doesn't handle limits
// when it queries, so I need to handle them on my own.
$wpQueryResults = array_slice( $wp_query->posts, 0, $maxResults );
return $wpQueryResults;
}
}
add_action( 'init', array( 'DWLS_Relevanssi_Bridge', 'init' ) );
class DWLS_Util {
public static function updateFirstImagePostmeta( $post_id, $post ) {
$parent_post = wp_is_post_revision( $post_id );
if ( false !== $parent_post ) {
$post_id = $parent_post;
$post = get_post( $parent_post, OBJECT );
}
$applyContentFilter = get_option( 'daves-wordpress-live-search_apply_content_filter', false );
$content = $post->post_content;
if ( $applyContentFilter ) {
$content = apply_filters( 'the_content', $content );
}
$content = str_replace( ']]>', ']]>', $content );
$attachment_thumbnail = self::firstImg( $content );
update_post_meta( $post_id, '_dwls_first_image', $attachment_thumbnail );
return $attachment_thumbnail;
}
public static function firstImg( $post_content ) {
$matches = array();
$output = preg_match_all( '/<img [^>]*src=["|\']([^"|\']+)/i', $post_content, $matches );
if ( isset( $matches[1][0] ) ) {
$first_img = $matches[1][0];
}
if ( empty( $first_img ) ) {
return '';
}
return $first_img;
}
/**
* Match an array key or return a default value.
*
* @param array $options Associated array of options to match against.
* @param string|int $requested Value to match against $options array keys.
* @param string|int $default Default value if there isn't a match.
* @return string|int Matching key or default.
*/
function match_array_key_or_default( $options, $requested, $default = null ) {
return array_key_exists( $requested, $options ) ?
$requested : $default;
}
}
add_action( "save_post", array( "DWLS_Util", "updateFirstImagePostmeta" ), 10, 2 );