| @@ -1,9 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | +define('FEEDWORDPRESS_OPTIMIZE_IN_CLAUSES', get_option('feedwordpress_optimize_in_clauses', false)); | |
| 2 | 3 | |
| 3 | 4 | class SyndicationDataQueries { |
| 4 | - public function __construct () { | |
| 5 | + function SyndicationDataQueries () { | |
| 5 | 6 | add_action('init', array($this, 'init')); |
| 7 | + add_filter('query', array($this, 'optimize_in_clauses')); | |
| 6 | 8 | add_action('parse_query', array($this, 'parse_query'), 10, 1); |
| 7 | 9 | add_filter('posts_search', array($this, 'posts_search'), 10, 2); |
| 8 | 10 | add_filter('posts_where', array($this, 'posts_where'), 10, 2); |
| 9 | 11 | add_filter('posts_fields', array($this, 'posts_fields'), 10, 2); |
| @@ -10,28 +12,51 @@ | ||
| 10 | 12 | add_filter('posts_request', array($this, 'posts_request'), 10, 2); |
| 11 | 13 | } |
| 12 | 14 | |
| 13 | 15 | function init () { |
| 14 | - // NOOP | |
| 16 | + global $wp; | |
| 17 | + $wp->add_query_var('guid'); | |
| 15 | 18 | } |
| 16 | 19 | |
| 17 | - function parse_query ($q) { | |
| 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) { | |
| 18 | 43 | if ($q->get('guid')) : |
| 19 | 44 | $q->is_single = false; // Causes nasty side-effects. |
| 20 | 45 | $q->is_singular = true; // Doesn't? |
| 21 | 46 | endif; |
| 22 | - | |
| 47 | + | |
| 23 | 48 | $ff = $q->get('fields'); |
| 24 | 49 | if ($ff == '_synfresh' or $ff == '_synfrom') : |
| 25 | 50 | $q->query_vars['cache_results'] = false; // Not suitable. |
| 26 | 51 | endif; |
| 27 | 52 | } /* SyndicationDataQueries::parse_query () */ |
| 28 | - | |
| 29 | - function pre_get_posts ($q) { | |
| 30 | - // Is this a stub? Nothing is used... (gwyneth 20230920) | |
| 53 | + | |
| 54 | + function pre_get_posts (&$q) { | |
| 55 | + // | |
| 31 | 56 | } |
| 32 | - | |
| 33 | - function posts_request ($sql, $query) { | |
| 57 | + | |
| 58 | + function posts_request ($sql, &$query) { | |
| 34 | 59 | if ($query->get('fields') == '_synfresh') : |
| 35 | 60 | FeedWordPress::diagnostic('feed_items:freshness:sql', "SQL: ".$sql); |
| 36 | 61 | endif; |
| 37 | 62 | return $sql; |
| @@ -36,18 +61,17 @@ | ||
| 36 | 61 | endif; |
| 37 | 62 | return $sql; |
| 38 | 63 | } |
| 39 | 64 | |
| 40 | - function posts_search ($search, $query) { | |
| 65 | + function posts_search ($search, &$query) { | |
| 41 | 66 | global $wpdb; |
| 42 | 67 | if ($guid = $query->get('guid')) : |
| 43 | 68 | if (strlen(trim($guid)) > 0) : |
| 44 | 69 | $seek = array($guid); |
| 45 | - | |
| 70 | + | |
| 46 | 71 | // MD5 hashes |
| 47 | 72 | if (preg_match('/^[0-9a-f]{32}$/i', $guid)) : |
| 48 | 73 | $seek[] = SyndicatedPost::normalize_guid_prefix().$guid; |
| 49 | - $seek[] = SyndicatedPost::alternative_guid_prefix().$guid; | |
| 50 | 74 | endif; |
| 51 | 75 | |
| 52 | 76 | // Invalid URIs, URIs that WordPress just doesn't like, and URIs |
| 53 | 77 | // that WordPress decides to munge. |
| @@ -53,33 +77,32 @@ | ||
| 53 | 77 | // that WordPress decides to munge. |
| 54 | 78 | $nGuid = SyndicatedPost::normalize_guid($guid); |
| 55 | 79 | if ($guid != $nGuid) : |
| 56 | 80 | $seek[] = $nGuid; |
| 57 | - $seek[] = SyndicatedPost::alternative_guid($guid); | |
| 58 | 81 | endif; |
| 59 | - | |
| 82 | + | |
| 60 | 83 | // Escape to prevent frak-ups, injections, etc. |
| 61 | 84 | $seek = array_map('esc_sql', $seek); |
| 62 | - | |
| 85 | + | |
| 63 | 86 | // Assemble |
| 64 | 87 | $guidMatch = "(guid = '".implode("') OR (guid = '", $seek)."')"; |
| 65 | 88 | $search .= " AND ($guidMatch)"; |
| 66 | 89 | endif; |
| 67 | 90 | endif; |
| 68 | - | |
| 91 | + | |
| 69 | 92 | if ($query->get('fields')=='_synfresh') : |
| 70 | 93 | // Ugly hack to ensure we ONLY check by guid in syndicated freshness |
| 71 | 94 | // checks -- for reasons of both performance and correctness. Pitch: |
| 72 | 95 | $search .= " -- '"; |
| 73 | 96 | elseif ($query->get('fields')=='_synfrom') : |
| 74 | - $search .= " AND ({$wpdb->postmeta}.meta_key = '".$query->get('meta_key')."' AND {$wpdb->postmeta}.meta_value = '".$query->get('meta_value')."') -- '"; | |
| 97 | + $search .= " AND ({$wpdb->postmeta}.meta_key = '".$query->get('meta_key')."' AND wp_postmeta.meta_value = '".$query->get('meta_value')."') -- '"; | |
| 75 | 98 | endif; |
| 76 | 99 | return $search; |
| 77 | 100 | } /* SyndicationDataQueries::posts_search () */ |
| 78 | - | |
| 79 | - function posts_where ($where, $q) { | |
| 101 | + | |
| 102 | + function posts_where ($where, &$q) { | |
| 80 | 103 | global $wpdb; |
| 81 | - | |
| 104 | + | |
| 82 | 105 | // Ugly hack to ensure we ONLY check by guid in syndicated freshness |
| 83 | 106 | // checks -- for reasons of both performance and correctness. Catch: |
| 84 | 107 | if (strpos($where, " -- '") !== false) : |
| 85 | 108 | $bits = explode(" -- '", $where, 2); |
| @@ -84,17 +107,17 @@ | ||
| 84 | 107 | if (strpos($where, " -- '") !== false) : |
| 85 | 108 | $bits = explode(" -- '", $where, 2); |
| 86 | 109 | $where = $bits[0]; |
| 87 | 110 | endif; |
| 88 | - | |
| 111 | + | |
| 89 | 112 | if ($psn = $q->get('post_status__not')) : |
| 90 | - $where .= " AND ({$wpdb->posts}.post_status <> '".esc_sql($psn)."')"; | |
| 113 | + $where .= " AND ({$wpdb->posts}.post_status <> '".$wpdb->escape($psn)."')"; | |
| 91 | 114 | endif; |
| 92 | - | |
| 115 | + | |
| 93 | 116 | return $where; |
| 94 | 117 | } /* SyndicationDataQueries::post_where () */ |
| 95 | - | |
| 96 | - function posts_fields ($fields, $query) { | |
| 118 | + | |
| 119 | + function posts_fields ($fields, &$query) { | |
| 97 | 120 | global $wpdb; |
| 98 | 121 | if ($f = $query->get('fields')) : |
| 99 | 122 | switch ($f) : |
| 100 | 123 | case '_synfresh' : |