forked from ptz0n/wordpress-google-cse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-cse.php
202 lines (180 loc) · 5.24 KB
/
google-cse.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/*
Plugin Name: Google CSE
Plugin URI: http://wordpress.org/extend/plugins/google-cse/
Description: Google powered search for your WordPress site or blog.
Version: 1.0.4
Author: Erik Eng
Author URI: http://erikeng.se/
License: GPLv2 or later
*/
/**
* Define constants
*
* @constant string GCSE_VERSION Plugin version
*/
define('GCSE_VERSION', '1.0.3');
/**
* Security
*
*/
if(!function_exists('add_action')) {
echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
exit;
}
/**
* Admin
*
*/
if(is_admin()) {
require_once dirname( __FILE__ ) . '/admin.php';
}
/**
* TODO: Localization
*
*/
/**
* Google API Request
*
* @see wp_remote_get
*
* @param boolean $test
*
* @since 1.0
*
* @return array
*/
function gcse_request($test = false)
{
global $wp_query;
$options = get_option('gcse_options');
if(isset($options['key']) && $options['key'] &&
isset($options['id']) && $options['id']) {
// Build URL
$num = isset($wp_query->query_vars['posts_per_page']) &&
$wp_query->query_vars['posts_per_page'] < 11 ?
$wp_query->query_vars['posts_per_page'] : 10;
$start = isset($wp_query->query_vars['paged']) &&
$wp_query->query_vars['paged'] ?
($wp_query->query_vars['paged']-1)*$num+1 : 1;
$params = http_build_query(array(
'key' => trim($options['key']),
'cx' => trim($options['id']),
'alt' => 'json',
'num' => $num,
'start' => $start,
'prettyPrint' => 'false',
'q' => get_search_query()));
$url = 'https://www.googleapis.com/customsearch/v1?'.$params;
// Check for and return cached response
if($response = get_transient('gcse_'.md5($url))) {
return json_decode($response, true);
}
// Request response
if(is_wp_error($response = wp_remote_get($url, array('sslverify' => false)))) {
return array('error' => array('errors' =>
array(array('reason' => $response->get_error_message()))));
}
}
// Save and return new response
if(isset($response['body'])) {
set_transient('gcse_'.md5($url), $response['body'], 3600);
return json_decode($response['body'], true);
}
else {
return array();
}
}
/**
* Search Results
*
* @since 1.0
*
*/
function gcse_results($posts, $q) {
if($q->is_single == 1 && $q->is_single != 1) {
global $wp_query;
$response = gcse_request();
if(isset($response['items']) && $response['items']) {
$results = array();
foreach($response['items'] as $result) {
if($id = gcse_url_to_postid($result['link'])) {
$post = get_post($id);
}
else {
$post = (object)array(
'post_title' => $result['title'],
'post_author' => '',
'post_date' => '',
'post_status' => 'published',
'post_excerpt' => $result['snippet'],
'post_content' => $result['htmlSnippet'],
'guid' => $result['link'],
'post_type' => 'search',
'ID' => 0,
);
// Adding in the featured image. You can use it if you'd like.
if(isset($result['pagemap']) && isset($result['pagemap']['cse_image']['0'])) {
$post->cse_img = $result['pagemap']['cse_image'][0]['src'];
}
}
$results[] = $post;
}
$post = '';
// Set results as posts
$posts = $results;
$results = '';
// Update post count
$wp_query->post_count = count($posts);
$wp_query->found_posts = $response['searchInformation']['totalResults'];
// Pagination
$posts_per_page = $wp_query->query_vars['posts_per_page'] < 11 ?
$wp_query->query_vars['posts_per_page'] : 10;
$wp_query->max_num_pages = ceil(
$response['searchInformation']['totalResults'] /
$posts_per_page);
// Apply filters
add_filter('the_permalink', 'gcse_permalink');
}
}
return $posts;
}
//add_action('wp_head', 'gcse_results'); // Old Method didn't modify query results in the right place
add_filter('posts_results', 'gcse_results', 99, 2); // Modifies results directly after query is made
/**
* URL to Post ID
*
* @param string $url
*
* @since 1.0
*
* @see url_to_postid
* @see http://betterwp.net/wordpress-tips/url_to_postid-for-custom-post-types/
*
* @return int
*/
function gcse_url_to_postid($url)
{
// TODO: Check url to post id map cache
return url_to_postid($url);
}
/**
* Permalink Filter
*
* @since 1.0
*
* @param string $the_permalink
*
* @return string
*
*/
function gcse_permalink($the_permalink)
{
if(function_exists('is_main_query') && is_main_query() && $the_permalink == '') {
global $post;
return $post->guid;
}
else {
return $the_permalink;
}
}