source: google-highlight/trunk/google-hilite.php @ 2302

Revision 2302, 3.4 KB checked in by ryan, 7 years ago (diff)

Bump version

Line 
1<?php
2/*
3Plugin Name: Search Hilite
4Plugin URI: http://dev.wp-plugins.org/browser/google-highlight/
5Description: When someone is referred from a search engine like Google, Yahoo, or WordPress' own, the terms they search for are highlighted with this plugin. Packaged by <a href="http://photomatt.net/">Matt</a>.
6Version: 1.5
7Author: Ryan Boren
8Author URI: http://boren.nu
9*/
10
11function get_search_query_terms($engine = 'google') {
12        $referer = urldecode($_SERVER['HTTP_REFERER']);
13        $query_array = array();
14        switch ($engine) {
15        case 'google':
16                // Google query parsing code adapted from Dean Allen's
17                // Google Hilite 0.3. http://textism.com
18                $query_terms = preg_replace('/^.*q=([^&]+)&?.*$/i','$1', $referer);
19                $query_terms = preg_replace('/\'|"/', '', $query_terms);
20                $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
21                break;
22
23        case 'lycos':
24                $query_terms = preg_replace('/^.*query=([^&]+)&?.*$/i','$1', $referer);
25                $query_terms = preg_replace('/\'|"/', '', $query_terms);
26                $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
27                break;
28
29        case 'yahoo':
30                $query_terms = preg_replace('/^.*p=([^&]+)&?.*$/i','$1', $referer);
31                $query_terms = preg_replace('/\'|"/', '', $query_terms);
32                $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
33                break;
34               
35        case 'wordpress':
36                $search = get_query_var('s');
37                $search_terms = get_query_var('search_terms');
38
39                if (!empty($search_terms)) {
40                        $query_array = $search_terms;
41                } else if (!empty($search)) {
42                        $query_array = array($search);
43                } else {
44                        $query_terms = preg_replace('/^.*s=([^&]+)&?.*$/i','$1', $referer);
45                        $query_terms = preg_replace('/\'|"/', '', $query_terms);
46                        $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
47                }
48        }
49       
50        return $query_array;
51}
52
53function is_referer_search_engine($engine = 'google') {
54        if( empty($_SERVER['HTTP_REFERER']) && 'wordpress' != $engine ) {
55                return false;
56        }
57
58        $referer = urldecode($_SERVER['HTTP_REFERER']);
59
60        if ( ! $engine ) {
61                return false;
62        }
63
64        switch ($engine) {
65        case 'google':
66                if (preg_match('|^http://(www)?\.?google.*|i', $referer)) {
67                        return true;
68                }
69                break;
70
71        case 'lycos':
72                if (preg_match('|^http://search\.lycos.*|i', $referer)) {
73                        return true;
74                }
75                break;
76
77        case 'yahoo':
78                if (preg_match('|^http://search\.yahoo.*|i', $referer)) {
79                        return true;
80                }
81                break;
82
83        case 'wordpress':
84                if ( is_search() )
85                        return true;
86
87                $siteurl = get_option('home');
88                if (preg_match("#^$siteurl#i", $referer))
89                        return true;
90
91                break;
92        }
93
94        return false;
95}
96
97function hilite($text) {
98        $search_engines = array('wordpress', 'google', 'lycos', 'yahoo');
99
100        foreach ($search_engines as $engine) {
101                if ( is_referer_search_engine($engine)) {
102                        $query_terms = get_search_query_terms($engine);
103                        foreach ($query_terms as $term) {
104                                if (!empty($term) && $term != ' ') {
105                    $term = preg_quote($term, '/');
106                                        if (!preg_match('/<.+>/',$text)) {
107                                                $text = preg_replace('/(\b'.$term.'\b)/i','<span class="hilite">$1</span>',$text);
108                                        } else {
109                                                $text = preg_replace('/(?<=>)([^<]+)?(\b'.$term.'\b)/i','$1<span class="hilite">$2</span>',$text);
110                                        }
111                                }
112                        }
113                        break;
114                }
115        }
116
117        return $text;
118}
119
120function hilite_head() {
121        echo "
122<style type='text/css'>
123.hilite {
124        color: #fff;
125        background-color: #f93;
126}
127</style>
128";
129}
130
131// Highlight text and comments:
132add_filter('the_content', 'hilite');
133add_filter('the_excerpt', 'hilite');
134add_filter('comment_text', 'hilite');
135add_action('wp_head', 'hilite_head');
136
137?>
Note: See TracBrowser for help on using the repository browser.