| 1 |
<?php |
| 2 |
class SyndicationDataQueries { |
| 3 |
function SyndicationDataQueries () { |
| 4 |
add_action('init', array(&$this, 'init')); |
| 5 |
add_filter('pre_get_posts', array(&$this, 'pre_get_posts'), 10, 1); |
| 6 |
add_filter('posts_search', array(&$this, 'posts_search'), 10, 2); |
| 7 |
add_filter('posts_fields', array(&$this, 'posts_fields'), 10, 2); |
| 8 |
add_filter('posts_request', array(&$this, 'posts_request'), 10, 2); |
| 9 |
} |
| 10 |
|
| 11 |
function init () { |
| 12 |
global $wp; |
| 13 |
$wp->add_query_var('guid'); |
| 14 |
} |
| 15 |
|
| 16 |
function pre_get_posts (&$q) { |
| 17 |
if ($q->get('guid')) : |
| 18 |
$q->query_vars['post_type'] = get_post_types(); |
| 19 |
$q->query_vars['post_status'] = implode(",", get_post_stati()); |
| 20 |
endif; |
| 21 |
|
| 22 |
if ($q->get('fields') == '_synfresh') : |
| 23 |
$q->query_vars['cache_results'] = false; // Not suitable for caching. |
| 24 |
endif; |
| 25 |
} |
| 26 |
|
| 27 |
function posts_request ($sql, &$query) { |
| 28 |
if ($query->get('fields') == '_synfresh') : |
| 29 |
FeedWordPress::diagnostic('feed_items:freshness:sql', "SQL: ".$sql); |
| 30 |
endif; |
| 31 |
return $sql; |
| 32 |
} |
| 33 |
|
| 34 |
function posts_search ($search, &$query) { |
| 35 |
global $wpdb; |
| 36 |
if ($guid = $query->get('guid')) : |
| 37 |
if (strlen(trim($guid)) > 0) : |
| 38 |
$seek = array($guid); |
| 39 |
$md5Seek = array(); |
| 40 |
|
| 41 |
// MD5 hashes |
| 42 |
if (preg_match('/^[0-9a-f]{32}$/i', $guid)) : |
| 43 |
$md5Seek = array($guid); |
| 44 |
$seek[] = SyndicatedPost::normalize_guid_prefix().$guid; |
| 45 |
endif; |
| 46 |
|
| 47 |
// URLs that are invalid, or that WordPress just doesn't like |
| 48 |
$nGuid = SyndicatedPost::normalize_guid($guid); |
| 49 |
if ($guid != $nGuid) : |
| 50 |
$seek[] = $nGuid; |
| 51 |
endif; |
| 52 |
|
| 53 |
// Assemble |
| 54 |
$guidMatch = "(guid = '".implode("') OR (guid = '", $seek)."')"; |
| 55 |
if (count($md5Seek) > 0) : |
| 56 |
$guidMatch .= " OR (MD5(guid) = '".implode("') OR (MD5(guid) = '", $md5Seek)."')"; |
| 57 |
endif; |
| 58 |
|
| 59 |
$search .= $wpdb->prepare(" AND ($guidMatch)"); |
| 60 |
endif; |
| 61 |
endif; |
| 62 |
return $search; |
| 63 |
} /* SyndicationDataQueries::posts_where () */ |
| 64 |
|
| 65 |
function posts_fields ($fields, &$query) { |
| 66 |
global $wpdb; |
| 67 |
if ($f = $query->get('fields')) : |
| 68 |
switch ($f) : |
| 69 |
case '_synfresh' : |
| 70 |
$fields = "{$wpdb->posts}.ID, {$wpdb->posts}.guid, {$wpdb->posts}.post_modified_gmt"; |
| 71 |
break; |
| 72 |
default : |
| 73 |
// Do nothing. |
| 74 |
endswitch; |
| 75 |
endif; |
| 76 |
return $fields; |
| 77 |
} /* SyndicationDataQueries::posts_fields () */ |
| 78 |
} |
| 79 |
|
| 80 |
$SDQ = new SyndicationDataQueries; |
| 81 |
|
| 82 |
|