Comment 3 for bug 343821

Revision history for this message
Alex Stansfield (casualgenius) wrote :

Hi Paul,

I've replicated and, hopefully, fixed this bug. I'm a little worried about knock on problems as it required a change to the rewrite rules which are basically a kind of voodoo magic.

If you wish to test it out then edit the plugin/filters.php file. Find the function "wps_post_rewrite_rules" and replace it with the one below.

function wps_post_rewrite_rules( $rules ) {
 global $wps_this_subdomain, $wp_rewrite;

 // If we have %category% in the permalink we also need to create rules without it.
 // This is because the category might be the subdomain and so wouldn't be in the url
 if ( strstr( $wp_rewrite->permalink_structure, '%category%' ) && $wps_this_subdomain && ($wps_this_subdomain->type == WPS_TYPE_CAT) ) {
  // Grab the permalink structure
  $perma_tmp = $wp_rewrite->permalink_structure;

  // Remove the /%category section
  $perma_tmp = str_replace('/%category%','',$perma_tmp);

  // Create the extra rules using this new structure
  $extra_rules = $wp_rewrite->generate_rewrite_rules($perma_tmp, EP_PERMALINK);

  // Now we have to remove the rule that matches a category on it's own
  // this is reinstated later but just can't come before the extra rules
  $unset_key = array_search('index.php?category_name=$matches[1]', $extra_rules);

  if ($unset_key) {
   unset($extra_rules[$unset_key]);
  }

  // Check for the problem attachment rules and remove them.
  // Pray this doesn't break anything ;)
  foreach ($extra_rules as $regexp => $url) {
   if (strpos($url, 'attachment=$matches') && (strpos($regexp, 'attachment') === false)) {
    unset($extra_rules[$regexp]);
   }
  }

  // merge to two rule sets into one
  $rules = array_merge($extra_rules, $rules);
 }

 // Check if the permalink structure has any date parts
 $has_date = false;
 foreach ( array( '%year%', '%monthnum%', '%day%' ) as $datepart ) {
  if ( strstr( $wp_rewrite->permalink_structure, $datepart ) ) {
   $has_date = true;
  }
 }

 // If there is a date part in the permalink structure filter by the subdomain we're on
 // This is incase we're actually looking at an date archive rather than a post
 if ( $has_date && $wps_this_subdomain) {
  $rules = $wps_this_subdomain->addRewriteFilter($rules);
 }

 //print('<pre>'.print_r($rules,true).'</pre>');

 return $rules;
}

Let me know how you get on and if you notice anything else go wrong. If it's ok for you I will release an 0.6.1-rc2 with the fix and see if anyone else find issues with it.

Cheers,

Alex