Ticket #966: patch2

File patch2, 3.4 KB (added by excalin, 7 years ago)
Line 
1Index: template-functions-links.php
2===================================================================
3--- template-functions-links.php        (revision 2428)
4+++ template-functions-links.php        (working copy)
5@@ -448,17 +448,14 @@
6 }
7 
8 function next_posts_link($label='Next Page »', $max_page=0) {
9-       global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages;
10+    global $paged, $max_num_pages;
11     if (!$max_page) {
12-                       if ( isset($max_num_pages) ) {
13-                               $max_page = $max_num_pages;
14-                       } else {
15-        preg_match('#FROM (.*) GROUP BY#', $request, $matches);
16-        $fromwhere = $matches[1];
17-        $numposts = $wpdb->get_var("SELECT COUNT(ID) FROM $fromwhere");
18-        $max_page = $max_num_pages = ceil($numposts / $posts_per_page);
19-                       }
20-    }
21+      if ( isset($max_num_pages) ) {
22+        $max_page = $max_num_pages;
23+      } else {
24+        count_pages();
25+        $max_page = $max_num_pages;
26+    }}
27     if (!$paged)
28         $paged = 1;
29     $nextpage = intval($paged) + 1;
30@@ -489,27 +486,47 @@
31     }
32 }
33 
34+function count_pages() {
35+  global $max_num_pages;
36+
37+  if (! is_single()) {
38+    switch (get_query_var('what_to_show')) {
39+    case 'posts':
40+      aux_count_pages('ID');
41+      break;
42+
43+    case 'days':
44+      aux_count_pages('DISTINCT DATE_FORMAT(post_date, \'%d %m %Y\')');
45+      break;
46+
47+    default:
48+      $max_num_pages = 999999;
49+    }
50+  }
51+}
52+
53+function aux_count_pages($what_to_count) {
54+  global $request, $posts_per_page, $wpdb, $max_num_pages;
55+
56+  if ( ! isset($max_num_pages) ) {
57+    preg_match('#FROM (.*) GROUP BY#', $request, $matches);
58+    $fromwhere = $matches[1];
59+    $fromwhere = preg_replace("(/\*TAG\*/.*/\*TAG\*/)","",$fromwhere);
60+    $numposts = $wpdb->get_var("SELECT COUNT($what_to_count) FROM $fromwhere");
61+    $max_num_pages = ceil($numposts / $posts_per_page);
62+  }
63+}
64+
65 function posts_nav_link($sep=' — ', $prelabel='« Previous Page', $nxtlabel='Next Page »') {
66-       global $request, $posts_per_page, $wpdb, $max_num_pages;
67-       if (! is_single()) {
68+       global $max_num_pages;
69 
70-               if (get_query_var('what_to_show') == 'posts') {
71-                       if ( ! isset($max_num_pages) ) {
72-                               preg_match('#FROM (.*) GROUP BY#', $request, $matches);
73-                               $fromwhere = $matches[1];
74-                               $numposts = $wpdb->get_var("SELECT COUNT(ID) FROM $fromwhere");
75-                               $max_num_pages = ceil($numposts / $posts_per_page);
76-                       }
77-               } else {
78-                       $max_num_pages = 999999;
79-               }
80+        count_pages();
81 
82         if ($max_num_pages > 1) {
83             previous_posts_link($prelabel);
84             echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
85-            next_posts_link($nxtlabel, $max_page);
86+            next_posts_link($nxtlabel, $max_num_pages);
87         }
88-    }
89 }
90 
91 ?>
92\ No newline at end of file
93Index: classes.php
94===================================================================
95--- classes.php (revision 2428)
96+++ classes.php (working copy)
97@@ -525,9 +525,9 @@
98                                $end_date = $wpdb->get_var("SELECT min(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $endrow,1");
99 
100                                if ($page > 1) {
101-                                       $where .= " AND post_date >= '$end_date' AND post_date <= '$start_date'";
102+                                       $where .= " /*TAG*/ AND post_date >= '$end_date' AND post_date <= '$start_date' /*TAG*/";
103                                } else {
104-                                       $where .= " AND post_date >= '$end_date'";
105+                                       $where .= " /*TAG*/ AND post_date >= '$end_date' /*TAG*/";
106                                }
107                        }
108                }