| 1 |
<?php |
| 2 |
class SyndicationDataQueries { |
| 3 |
function SyndicationDataQueries () { |
| 4 |
add_action('init', array(&$this, 'init')); |
| 5 |
add_action('parse_query', array(&$this, 'parse_query'), 10, 1); |
| 6 |
add_filter('posts_search', array(&$this, 'posts_search'), 10, 2); |
| 7 |
add_filter('posts_where', array(&$this, 'posts_where'), 10, 2); |
| 8 |
add_filter('posts_fields', array(&$this, 'posts_fields'), 10, 2); |
| 9 |
add_filter('posts_request', array(&$this, 'posts_request'), 10, 2); |
| 10 |
} |
| 11 |
|
| 12 |
function init () { |
| 13 |
global $wp; |
| 14 |
$wp->add_query_var('guid'); |
| 15 |
} |
| 16 |
|
| 17 |
function parse_query (&$q) { |
| 18 |
if ($q->get('guid')) : |
| 19 |
$q->is_single = false; // Causes nasty side-effects. |
| 20 |
$q->is_singular = true; // Doesn't? |
| 21 |
endif; |
| 22 |
|
| 23 |
if ($q->get('fields') == '_synfresh') : |
| 24 |
$q->query_vars['cache_results'] = false; // Not suitable. |
| 25 |
endif; |
| 26 |
} /* SyndicationDataQueries::parse_query () */ |
| 27 |
|
| 28 |
function pre_get_posts (&$q) { |
| 29 |
// |
| 30 |
} |
| 31 |
|
| 32 |
function posts_request ($sql, &$query) { |
| 33 |
if ($query->get('fields') == '_synfresh') : |
| 34 |
FeedWordPress::diagnostic('feed_items:freshness:sql', "SQL: ".$sql); |
| 35 |
endif; |
| 36 |
return $sql; |
| 37 |
} |
| 38 |
|
| 39 |
function posts_search ($search, &$query) { |
| 40 |
global $wpdb; |
| 41 |
if ($guid = $query->get('guid')) : |
| 42 |
if (strlen(trim($guid)) > 0) : |
| 43 |
$seek = array($guid); |
| 44 |
|
| 45 |
// MD5 hashes |
| 46 |
if (preg_match('/^[0-9a-f]{32}$/i', $guid)) : |
| 47 |
$seek[] = SyndicatedPost::normalize_guid_prefix().$guid; |
| 48 |
endif; |
| 49 |
|
| 50 |
// Invalid URIs, URIs that WordPress just doesn't like, and URIs |
| 51 |
// that WordPress decides to munge. |
| 52 |
$nGuid = SyndicatedPost::normalize_guid($guid); |
| 53 |
if ($guid != $nGuid) : |
| 54 |
$seek[] = $nGuid; |
| 55 |
endif; |
| 56 |
|
| 57 |
// Escape to prevent frak-ups, injections, etc. |
| 58 |
$seek = array_map('esc_sql', $seek); |
| 59 |
|
| 60 |
// Assemble |
| 61 |
$guidMatch = "(guid = '".implode("') OR (guid = '", $seek)."')"; |
| 62 |
$search .= " AND ($guidMatch)"; |
| 63 |
endif; |
| 64 |
endif; |
| 65 |
|
| 66 |
if ($query->get('fields')=='_synfresh') : |
| 67 |
// Ugly hack to ensure we ONLY check by guid in syndicated freshness |
| 68 |
// checks -- for reasons of both performance and correctness. Pitch: |
| 69 |
$search .= " -- '"; |
| 70 |
endif; |
| 71 |
return $search; |
| 72 |
} /* SyndicationDataQueries::posts_search () */ |
| 73 |
|
| 74 |
function posts_where ($where, &$q) { |
| 75 |
// Ugly hack to ensure we ONLY check by guid in syndicated freshness |
| 76 |
// checks -- for reasons of both performance and correctness. Catch: |
| 77 |
if (strpos($where, " -- '") !== false) : |
| 78 |
$bits = explode(" -- '", $where, 2); |
| 79 |
$where = $bits[0]; |
| 80 |
endif; |
| 81 |
return $where; |
| 82 |
} /* SyndicationDataQueries::post_where () */ |
| 83 |
|
| 84 |
function posts_fields ($fields, &$query) { |
| 85 |
global $wpdb; |
| 86 |
if ($f = $query->get('fields')) : |
| 87 |
switch ($f) : |
| 88 |
case '_synfresh' : |
| 89 |
$fields = "{$wpdb->posts}.ID, {$wpdb->posts}.guid, {$wpdb->posts}.post_modified_gmt"; |
| 90 |
break; |
| 91 |
default : |
| 92 |
// Do nothing. |
| 93 |
endswitch; |
| 94 |
endif; |
| 95 |
return $fields; |
| 96 |
} /* SyndicationDataQueries::posts_fields () */ |
| 97 |
} |
| 98 |
|
| 99 |
$SDQ = new SyndicationDataQueries; |
| 100 |
|
| 101 |
|