WordPress.org

Make WordPress Core

Ticket #10425: 10425.patch

File 10425.patch, 6.3 KB (added by ruslany, 9 years ago)

Patch for the $non_wp_rules support

  • wp-admin/includes/misc.php

     
    154154        // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
    155155        if ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) {
    156156                if ( iis7_supports_permalinks() ) {
    157                         $rule = $wp_rewrite->iis7_url_rewrite_rules();
    158                         if ( ! empty($rule) ) {
    159                                 return iis7_add_rewrite_rule($web_config_file, $rule);
     157                        $rules = $wp_rewrite->iis7_url_rewrite_rules();
     158                        if ( ! empty($rules) ) {
     159                                return iis7_add_rewrite_rules($web_config_file, $rules);
    160160                        } else {
    161                                 return iis7_delete_rewrite_rule($web_config_file);
     161                                return iis7_delete_rewrite_rules($web_config_file);
    162162                        }
    163163                }
    164164        }
     
    457457}
    458458
    459459/**
    460  * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
     460 * Delete WordPress rewrite rules from web.config file if it exists there
    461461 *
    462462 * @since 2.8.0
    463463 *
    464  * @return bool
    465  * @param string $filename The file path to the configuration file
    466  */
    467 function iis7_rewrite_rule_exists($filename) {
    468         if ( ! file_exists($filename) )
    469                 return false;
    470         if ( ! class_exists('DOMDocument') )
    471                 return false;
    472 
    473         $doc = new DOMDocument();
    474         if ( $doc->load($filename) === false )
    475                 return false;
    476         $xpath = new DOMXPath($doc);
    477         $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
    478         if ( $rules->length == 0 )
    479                 return false;
    480         else
    481                 return true;
    482 }
    483 
    484 /**
    485  * Delete WordPress rewrite rule from web.config file if it exists there
    486  *
    487  * @since 2.8.0
    488  *
    489464 * @param string $filename Name of the configuration file
    490465 * @return bool
    491466 */
    492 function iis7_delete_rewrite_rule($filename) {
     467function iis7_delete_rewrite_rules( $filename ) {
    493468        // If configuration file does not exist then rules also do not exist so there is nothing to delete
    494469        if ( ! file_exists($filename) )
    495470                return true;
     
    503478        if ( $doc -> load($filename) === false )
    504479                return false;
    505480        $xpath = new DOMXPath($doc);
    506         $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
     481        $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[contains(@name,\'wordpress\')]');
    507482        if ( $rules->length > 0 ) {
    508                 $child = $rules->item(0);
    509                 $parent = $child->parentNode;
    510                 $parent->removeChild($child);
     483                foreach ( $rules as $child ) {
     484                        $parent = $child->parentNode;
     485                        $parent->removeChild($child);           
     486                }
    511487                $doc->formatOutput = true;
    512488                saveDomDocument($doc, $filename);
    513489        }
     
    515491}
    516492
    517493/**
    518  * Add WordPress rewrite rule to the IIS 7 configuration file.
     494 * Add WordPress rewrite rules to the IIS 7 configuration file.
    519495 *
    520496 * @since 2.8.0
    521497 *
    522498 * @param string $filename The file path to the configuration file
    523  * @param string $rewrite_rule The XML fragment with URL Rewrite rule
     499 * @param string $rewrite_rules The XML fragment with URL Rewrite rule
    524500 * @return bool
    525501 */
    526 function iis7_add_rewrite_rule($filename, $rewrite_rule) {
     502function iis7_add_rewrite_rules($filename, $rewrite_rules) {
    527503        if ( ! class_exists('DOMDocument') )
    528504                return false;
    529505
     
    542518
    543519        $xpath = new DOMXPath($doc);
    544520
    545         // First check if the rule already exists as in that case there is no need to re-add it
    546         $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
    547         if ( $wordpress_rules->length > 0 )
    548                 return true;
     521        // First check if the rules already exist as in that case we need to remove them before writing the updated ones.
     522        $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[contains(@name,\'wordpress\')]');
     523        if ( $wordpress_rules->length > 0 ) {
     524                foreach ( $wordpress_rules as $child ) {
     525                        $parent = $child->parentNode;
     526                        $parent->removeChild($child);           
     527                }
     528        }
    549529
    550530        // Check the XPath to the rewrite rule and create XML nodes if they do not exist
    551531        $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
     
    584564        }
    585565
    586566        $rule_fragment = $doc->createDocumentFragment();
    587         $rule_fragment->appendXML($rewrite_rule);
     567        $rule_fragment->appendXML($rewrite_rules);
    588568        $rules_node->appendChild($rule_fragment);
    589569
    590570        $doc->formatOutput = true;
  • wp-includes/rewrite.php

     
    17201720                if ( ! $this->using_permalinks()) {
    17211721                        return '';
    17221722                }
    1723                 $rules  = "<rule name=\"wordpress\" patternSyntax=\"Wildcard\">\n";
    1724                 $rules .= "     <match url=\"*\" />\n";
    1725                 $rules .= "     <conditions>\n";
    1726                 $rules .= "             <add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" negate=\"true\" />\n";
    1727                 $rules .= "             <add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" negate=\"true\" />\n";
    1728                 $rules .= "     </conditions>\n";
    1729                 $rules .= "     <action type=\"Rewrite\" url=\"index.php\" />\n";
    1730                 $rules .= "</rule>";
    17311723
     1724                $rules = "\n";
     1725                $counter = 0;
     1726                //add in the rules that don't rewrite to WP's index.php (and thus shouldn't be handled by WP at all)
     1727                foreach ( (array) $this->non_wp_rules as $match => $query ) {
     1728                        $rules .= '<rule name="non_wordpress_rule_' . $counter++ . '" stopProcessing="true">' . "\n";
     1729                        $rules .= ' <match url="' . $match . '" />' . "\n";
     1730                        // Apache uses $N (N=0,..,9) for back-references. IIS URL rewrite uses {R:N} (N=0,...,9)
     1731                        // We need to replaces all the back-referenses
     1732                        $query = preg_replace('/\$(\d)/i', '{R:$1}', $query);
     1733                        $rules .= ' <action type="Rewrite" url="' . $query . '" />' . "\n";
     1734                        $rules .= '</rule>' . "\n";
     1735                }
     1736
     1737                $rules .= '<rule name="wordpress" patternSyntax="Wildcard">'."\n";
     1738                $rules .= ' <match url="*" />'."\n";
     1739                $rules .= ' <conditions>'."\n";
     1740                $rules .= '  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />'."\n";
     1741                $rules .= '  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />'."\n";
     1742                $rules .= ' </conditions>'."\n";
     1743                $rules .= ' <action type="Rewrite" url="index.php" />'."\n";
     1744                $rules .= '</rule>';
     1745
    17321746                $rules = apply_filters('iis7_url_rewrite_rules', $rules);
    17331747
    17341748                return $rules;