| 1 |
<?php |
| 2 |
define('FEEDWORDPRESS_OPTIMIZE_IN_CLAUSES', get_option('feedwordpress_optimize_in_clauses', false)); |
| 3 |
|
| 4 |
class SyndicationDataQueries { |
| 5 |
function SyndicationDataQueries () { |
| 6 |
add_action('init', array($this, 'init')); |
| 7 |
add_filter('query', array($this, 'optimize_in_clauses')); |
| 8 |
add_action('parse_query', array($this, 'parse_query'), 10, 1); |
| 9 |
add_filter('posts_search', array($this, 'posts_search'), 10, 2); |
| 10 |
add_filter('posts_where', array($this, 'posts_where'), 10, 2); |
| 11 |
add_filter('posts_fields', array($this, 'posts_fields'), 10, 2); |
| 12 |
add_filter('posts_request', array($this, 'posts_request'), 10, 2); |
| 13 |
} |
| 14 |
|
| 15 |
function init () { |
| 16 |
global $wp; |
| 17 |
$wp->add_query_var('guid'); |
| 18 |
} |
| 19 |
|
| 20 |
function optimize_in_clauses ($q) { |
| 21 |
// This is kind of a dicey, low-level thing to do, and Christ, |
| 22 |
// this is something WordPress should be doing on its own, |
| 23 |
// so it's disabled by default. But you can enable it in |
| 24 |
// Performance --> Optimize IN clauses |
| 25 |
if (FEEDWORDPRESS_OPTIMIZE_IN_CLAUSES) : |
| 26 |
if (preg_match_all('/ \s+ IN \s* \((\s*([0-9]+)\s*)\)/x', $q, $r, PREG_OFFSET_CAPTURE)) : |
| 27 |
$from = 0; $nq = ''; |
| 28 |
foreach ($r[0] as $idx => $ref) : |
| 29 |
$len = $ref[1] - $from; |
| 30 |
$nq .= substr($q, $from, $len); |
| 31 |
$nq .= ' = ' . $r[1][$idx][0]; |
| 32 |
$from = $ref[1] + strlen($ref[0]); |
| 33 |
endforeach; |
| 34 |
|
| 35 |
$q = $nq; |
| 36 |
endif; |
| 37 |
endif; |
| 38 |
|
| 39 |
return $q; |
| 40 |
} |
| 41 |
|
| 42 |
function parse_query (&$q) { |
| 43 |
if ($q->get('guid')) : |
| 44 |
$q->is_single = false; // Causes nasty side-effects. |
| 45 |
$q->is_singular = true; // Doesn't? |
| 46 |
endif; |
| 47 |
|
| 48 |
$ff = $q->get('fields'); |
| 49 |
if ($ff == '_synfresh' or $ff == '_synfrom') : |
| 50 |
$q->query_vars['cache_results'] = false; // Not suitable. |
| 51 |
endif; |
| 52 |
} /* SyndicationDataQueries::parse_query () */ |
| 53 |
|
| 54 |
function pre_get_posts (&$q) { |
| 55 |
// |
| 56 |
} |
| 57 |
|
| 58 |
function posts_request ($sql, &$query) { |
| 59 |
if ($query->get('fields') == '_synfresh') : |
| 60 |
FeedWordPress::diagnostic('feed_items:freshness:sql', "SQL: ".$sql); |
| 61 |
endif; |
| 62 |
return $sql; |
| 63 |
} |
| 64 |
|
| 65 |
function posts_search ($search, &$query) { |
| 66 |
global $wpdb; |
| 67 |
if ($guid = $query->get('guid')) : |
| 68 |
if (strlen(trim($guid)) > 0) : |
| 69 |
$seek = array($guid); |
| 70 |
|
| 71 |
// MD5 hashes |
| 72 |
if (preg_match('/^[0-9a-f]{32}$/i', $guid)) : |
| 73 |
$seek[] = SyndicatedPost::normalize_guid_prefix().$guid; |
| 74 |
endif; |
| 75 |
|
| 76 |
// Invalid URIs, URIs that WordPress just doesn't like, and URIs |
| 77 |
// that WordPress decides to munge. |
| 78 |
$nGuid = SyndicatedPost::normalize_guid($guid); |
| 79 |
if ($guid != $nGuid) : |
| 80 |
$seek[] = $nGuid; |
| 81 |
endif; |
| 82 |
|
| 83 |
// Escape to prevent frak-ups, injections, etc. |
| 84 |
$seek = array_map('esc_sql', $seek); |
| 85 |
|
| 86 |
// Assemble |
| 87 |
$guidMatch = "(guid = '".implode("') OR (guid = '", $seek)."')"; |
| 88 |
$search .= " AND ($guidMatch)"; |
| 89 |
endif; |
| 90 |
endif; |
| 91 |
|
| 92 |
if ($query->get('fields')=='_synfresh') : |
| 93 |
// Ugly hack to ensure we ONLY check by guid in syndicated freshness |
| 94 |
// checks -- for reasons of both performance and correctness. Pitch: |
| 95 |
$search .= " -- '"; |
| 96 |
elseif ($query->get('fields')=='_synfrom') : |
| 97 |
$search .= " AND ({$wpdb->postmeta}.meta_key = '".$query->get('meta_key')."' AND {$wpdb->postmeta}.meta_value = '".$query->get('meta_value')."') -- '"; |
| 98 |
endif; |
| 99 |
return $search; |
| 100 |
} /* SyndicationDataQueries::posts_search () */ |
| 101 |
|
| 102 |
function posts_where ($where, &$q) { |
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
// Ugly hack to ensure we ONLY check by guid in syndicated freshness |
| 106 |
// checks -- for reasons of both performance and correctness. Catch: |
| 107 |
if (strpos($where, " -- '") !== false) : |
| 108 |
$bits = explode(" -- '", $where, 2); |
| 109 |
$where = $bits[0]; |
| 110 |
endif; |
| 111 |
|
| 112 |
if ($psn = $q->get('post_status__not')) : |
| 113 |
$where .= " AND ({$wpdb->posts}.post_status <> '".$wpdb->escape($psn)."')"; |
| 114 |
endif; |
| 115 |
|
| 116 |
return $where; |
| 117 |
} /* SyndicationDataQueries::post_where () */ |
| 118 |
|
| 119 |
function posts_fields ($fields, &$query) { |
| 120 |
global $wpdb; |
| 121 |
if ($f = $query->get('fields')) : |
| 122 |
switch ($f) : |
| 123 |
case '_synfresh' : |
| 124 |
$fields = "{$wpdb->posts}.ID, {$wpdb->posts}.guid, {$wpdb->posts}.post_modified_gmt, {$wpdb->posts}.post_name"; |
| 125 |
break; |
| 126 |
case '_synfrom' : |
| 127 |
$fields = "{$wpdb->posts}.ID, {$wpdb->posts}.guid, {$wpdb->posts}.post_title, {$wpdb->postmeta}.meta_value"; |
| 128 |
break; |
| 129 |
default : |
| 130 |
// Do nothing. |
| 131 |
endswitch; |
| 132 |
endif; |
| 133 |
return $fields; |
| 134 |
} /* SyndicationDataQueries::posts_fields () */ |
| 135 |
} |
| 136 |
|
| 137 |
$SDQ = new SyndicationDataQueries; |
| 138 |
|
| 139 |
|