[wp-hackers] Missing Hooks for Widget Filters

Gregory Wood greg at wood.name
Tue Sep 20 19:20:27 GMT 2005


Hello all,

I'm new to WP, so I hope this is going through the proper procedures. 
I'm trying to add a psuedo permissions plugin where I can restrict 
certain posts from being viewed by certain users based on category (for 
which I've added a table). This was very, very easy to implement with 
the main loop by creating a 'posts_join' and 'posts_where'. 
Unfortunately it's not so easy to filter out results of various other 
'widgets' on the blog.

I see that a 'list_cats_exclusions' filter was added to list_cats() with 
1.5.2(?), allowing me to filter out the categories. I've patched my 
1.5.1.3 install and this works beautifully.

Unfortunately I don't see any methods to filter out get_archives() or 
get_calendar(), two widgets that would otherwise display post titles and 
provide links to 'hidden' posts. I don't see any mechanism to plug into, 
so I would propose adding 4 filters to template-functions-general.php: 
archives_join, archives_where, calendar_join, calendar_where.

I've also taken a look at get_next_post() and get_previous_post() in 
template-functions-links.php and I see a place to hook into for 
excluding categories. Unfortunately, as currently implemented, this 
functionality doesn't appear to work. Specifically, this is trying to 
exclude based on post_category in the posts table, which doesn't appear 
to be set (I'm guessing that when the posts2cat table was added, this 
field was deprecated).

To 'fix' get_next_post() and get_previous_post() (and add the filtering 
I need for my plugin), I propose moving the INNER JOIN out of the 
$in_same_cat block and changing the exclusions to exclude the field 
"category_id" instead of "post_category". Then after the loop that 
generates $sql_exclude_cats, add a filter for "next_previous_exclusions".

I've attached a patch for both of the files. Please let me know if you 
think these changes are possible.

Gregory
-------------- next part --------------
--- template-functions-links.php.old	Tue Sep 20 15:08:43 2005
+++ template-functions-links.php.new	Tue Sep 20 15:17:55 2005
@@ -206,9 +206,8 @@
     
 	$current_post_date = $post->post_date;
     
-	$join = '';
+	$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
 	if ($in_same_cat) {
-		$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
 		$cat_array = get_the_category($post->ID);
 	 	$join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
 		for ($i = 1; $i < (count($cat_array)); $i++) {
@@ -222,9 +221,10 @@
 		$blah = explode('and', $excluded_categories);
 		foreach($blah as $category) {
 			$category = intval($category);
-			$sql_exclude_cats .= " AND post_category != $category";
+			$sql_exclude_cats .= " AND category_id != $category";
 		}
 	}
+	$sql_exclude_cats = apply_filters('next_previous_exclusions', $sql_exclude_cats);
 
 	return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
 }
@@ -238,9 +238,8 @@
 
 	$current_post_date = $post->post_date;
     
-	$join = '';
+	$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
 	if ($in_same_cat) {
-		$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
 		$cat_array = get_the_category($post->ID);
 	 	$join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
 		for ($i = 1; $i < (count($cat_array)); $i++) {
@@ -254,12 +253,12 @@
 		$blah = explode('and', $excluded_categories);
 		foreach($blah as $category) {
 			$category = intval($category);
-			$sql_exclude_cats .= " AND post_category != $category";
+			$sql_exclude_cats .= " AND category_id != $category";
 		}
 	}
+	$sql_exclude_cats = apply_filters('next_previous_exclusions', $sql_exclude_cats);
 
 	$now = current_time('mysql');
-    
 	return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
 }
 
-------------- next part --------------
--- template-functions-general.php.old	Tue Sep 20 15:07:20 2005
+++ template-functions-general.php.new	Tue Sep 20 15:06:56 2005
@@ -296,8 +296,10 @@
 
     $now = current_time('mysql');
 
+    $join = apply_filters('archives_join', '');
+    $where = apply_filters('archives_where', '');
     if ('monthly' == $type) {
-        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" . $limit);
+        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join WHERE post_date < '$now' AND post_status = 'publish' $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" . $limit);
         if ($arcresults) {
             $afterafter = $after;
             foreach ($arcresults as $arcresult) {
@@ -312,7 +314,7 @@
             }
         }
     } elseif ('daily' == $type) {
-        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth` FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);
+        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth` FROM $wpdb->posts $join WHERE post_date < '$now' AND post_status = 'publish' $where ORDER BY post_date DESC" . $limit);
         if ($arcresults) {
             foreach ($arcresults as $arcresult) {
                 $url  = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
@@ -323,7 +325,7 @@
         }
     } elseif ('weekly' == $type) {
 	$start_of_week = get_settings('start_of_week');
-        $arcresults = $wpdb->get_results("SELECT DISTINCT WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);
+        $arcresults = $wpdb->get_results("SELECT DISTINCT WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd FROM $wpdb->posts $join WHERE post_date < '$now' AND post_status = 'publish' $where ORDER BY post_date DESC" . $limit);
         $arc_w_last = '';
         if ($arcresults) {
             foreach ($arcresults as $arcresult) {
@@ -342,7 +344,7 @@
             }
         }
     } elseif ('postbypost' == $type) {
-        $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);
+        $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts $join WHERE post_date < '$now' AND post_status = 'publish' $where ORDER BY post_date DESC" . $limit);
         if ($arcresults) {
             foreach ($arcresults as $arcresult) {
                 if ($arcresult->post_date != '0000-00-00 00:00:00') {
@@ -410,17 +412,19 @@
     $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
 
     // Get the next and previous month and year with at least one post
+    $join = apply_filters('calendar_join', '');
+    $where = apply_filters('calendar_where', '');
     $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
-            FROM $wpdb->posts
+            FROM $wpdb->posts $join
             WHERE post_date < '$thisyear-$thismonth-01'
-            AND post_status = 'publish'
+            AND post_status = 'publish' $where
                               ORDER BY post_date DESC
                               LIMIT 1");
     $next = $wpdb->get_row("SELECT  DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
-            FROM $wpdb->posts
+            FROM $wpdb->posts $join
             WHERE post_date >  '$thisyear-$thismonth-01'
             AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
-            AND post_status = 'publish'
+            AND post_status = 'publish' $where
                               ORDER  BY post_date ASC
                               LIMIT 1");
 
@@ -477,10 +481,10 @@
 
     // Get days with posts
     $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
-            FROM $wpdb->posts WHERE MONTH(post_date) = $thismonth
+            FROM $wpdb->posts $join WHERE MONTH(post_date) = $thismonth
             AND YEAR(post_date) = $thisyear
             AND post_status = 'publish'
-            AND post_date < '" . current_time('mysql') . '\'', ARRAY_N);
+            AND post_date < '" . current_time('mysql') . '\' '.$where, ARRAY_N);
     if ($dayswithposts) {
         foreach ($dayswithposts as $daywith) {
             $daywithpost[] = $daywith[0];
@@ -501,11 +505,11 @@
 
     $ak_titles_for_day = array();
     $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
-                                         ."FROM $wpdb->posts "
+                                         ."FROM $wpdb->posts $join"
                                          ."WHERE YEAR(post_date) = '$thisyear' "
                                          ."AND MONTH(post_date) = '$thismonth' "
                                          ."AND post_date < '".current_time('mysql')."' "
-                                         ."AND post_status = 'publish'"
+                                         ."AND post_status = 'publish' $where"
                                         );
     if ($ak_post_titles) {
         foreach ($ak_post_titles as $ak_post_title) {


More information about the wp-hackers mailing list