Index: region_visibility.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/region_visibility/region_visibility.module,v
retrieving revision 1.1
diff -u -p -r1.1 region_visibility.module
--- region_visibility.module	11 May 2009 17:48:24 -0000	1.1
+++ region_visibility.module	13 May 2009 00:42:36 -0000
@@ -52,70 +52,86 @@ function region_visibility_menu() {
  * Implementation of hook_perm().
  */
 function region_visibility_perm() {
-  return array('administer region visibility', 'view all regions', 'use PHP for region visibility');
+  return array('administer region visibility', 'use PHP for region visibility');
 }
 
 /**
  * Implementation of hook_theme_registry_alter().
  */
 function region_visibility_theme_registry_alter(&$theme_registry) {
-  if (isset($theme_registry['page'])) {
-    // If region_visibility's preprocess function is there already, remove it.
-    if ($key = array_search('region_visibility_preprocess_page', $theme_registry['page']['preprocess functions'])) {
-      unset($theme_registry['page']['preprocess functions'][$key]);
-    }
-    // Now tack it on at the end so it runs after everything else.
-    $theme_registry['page']['preprocess functions'][] = 'region_visibility_preprocess_page';
-  } 
+  if (isset($theme_registry['blocks'])) {
+    $theme_registry['blocks']['function'] = 'region_visibility_blocks';
+    $theme_registry['blocks']['theme paths'][] = drupal_get_path('module', 'region_visibility');
+  }
 }
 
 /**
- * Implementation of hook_preprocess_page().
+ * Return a set of blocks available for the current user if the region is visible.
+ *
+ * @param $region
+ *   Which set of blocks to retrieve.
+ * @return
+ *   A string containing the themed blocks for this region.
  */
-function region_visibility_preprocess_page(&$vars) {
-  if (!user_access('view all regions')) {
-    $regions = array();
-    global $theme_key;
+function region_visibility_blocks($region) {
+  $output = '';
 
-    if (empty($theme_key)) {
-      init_theme();
+  if (_region_visibility($region)) {
+    if ($list = block_list($region)) {
+      foreach ($list as $key => $block) {
+        // $key == <i>module</i>_<i>delta</i>
+        $output .= theme('block', $block);
+      }
     }
+  
+    // Add any content assigned to this region through drupal_set_content() calls.
+    $output .= drupal_get_content($region);
+  }
 
-    $records = region_visibility_regions_load($theme_key);
+  return $output;
+}
 
-    if (!empty($records)) {
-      foreach(array_keys(system_region_list($theme_key)) as $region) {
-        if ($vars[$region]) {
-          $record = $records[$region];
-          // Match path if necessary
-          if ($record['pages']) {
-            if ($record['visibility'] < 2) {
-              $path = drupal_get_path_alias($_GET['q']);
-              // Compare with the internal and path alias (if any).
-              $page_match = drupal_match_path($path, $record['pages']);
-              if ($path != $_GET['q']) {
-                $page_match = $page_match || drupal_match_path($_GET['q'], $record['pages']);
-              }
-              // When $record['visibility'] has a value of 0, the region is displayed on
-              // all pages except those listed in $record['pages']. When set to 1, it
-              // is displayed only on those pages listed in $record['pages'].
-              $page_match = !($record['visibility'] xor $page_match);
-            }
-            else {
-              $page_match = drupal_eval($record['pages']);
-            }
-          }
-          else {
-            $page_match = TRUE;
-          }
-
-          if (!$page_match) {
-            unset($vars[$region]);
-          }
+/**
+ * Determine the visibility of a region.
+ *
+ * @param $region
+ *   A string containing the name of the region.
+ * @return
+ *   Boolean TRUE if region is visible, FALSE otherwise.
+ */
+function _region_visibility($region) {
+  global $theme_key;
+
+  if (empty($theme_key)) {
+    init_theme();
+  }
+
+  $records = region_visibility_regions_load($theme_key);
+
+  $page_match = TRUE;
+
+  if ($record = $records[$region]) {
+    // Match path if necessary
+    if ($record['pages']) {
+      if ($record['visibility'] < 2) {
+        $path = drupal_get_path_alias($_GET['q']);
+        // Compare with the internal and path alias (if any).
+        $page_match = drupal_match_path($path, $record['pages']);
+        if ($path != $_GET['q']) {
+          $page_match = $page_match || drupal_match_path($_GET['q'], $record['pages']);
         }
+        // When $record['visibility'] has a value of 0, the region is displayed on
+        // all pages except those listed in $record['pages']. When set to 1, it
+        // is displayed only on those pages listed in $record['pages'].
+        $page_match = !($record['visibility'] xor $page_match);
+      }
+      else {
+        $page_match = drupal_eval($record['pages']);
       }
     }
   }
+
+  return $page_match;
 }
 
 /**
@@ -210,7 +226,7 @@ function region_visibility_regions_load(
     return $records;
   }
 
-  if (($cache = cache_get($cache_key, 'cache') && is_array($cache->data))) {
+  if (($cache = cache_get($cache_key, 'cache')) && is_array($cache->data)) {
     $records = $cache->data;
   }
   else {
