WordPressを読む 47-4 /blog/wp-includes/rewrite.php 4
2015/01/29
目次
/blog/wp-includes/rewrite.php 4
クラス WP_Rewrite::generate_rewrite_rule()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Generate Rewrite rules with permalink structure and walking directory only. * * Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that * allows for shorter list of parameters. See the method for longer * description of what generating rewrite rules does. * * @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters. * @since 1.5.0 * @access public * * @param string $permalink_structure The permalink structure to generate rules. * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over. * @return array */ public function generate_rewrite_rule($permalink_structure, $walk_dirs = false) { return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs); } |
クラス WP_Rewrite::rewrite_rules()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | /** * Construct rewrite matches and queries from permalink structure. * * Runs the action 'generate_rewrite_rules' with the parameter that is an * reference to the current WP_Rewrite instance to further manipulate the * permalink structures and rewrite rules. Runs the 'rewrite_rules_array' * filter on the full rewrite rule array. * * There are two ways to manipulate the rewrite rules, one by hooking into * the 'generate_rewrite_rules' action and gaining full control of the * object or just manipulating the rewrite rule array before it is passed * from the function. * * @since 1.5.0 * @access public * * @return array An associate array of matches and queries. */ public function rewrite_rules() { $rewrite = array(); if ( empty($this->permalink_structure) ) return $rewrite; // robots.txt -only if installed at the root $home_path = parse_url( home_url() ); $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array(); // Old feed and service files $deprecated_files = array( '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old', '.*wp-app\.php(/.*)?$' => $this->index . '?error=403', ); // Registration rules $registration_pages = array(); if ( is_multisite() && is_main_site() ) { $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true'; $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true'; } $registration_pages['.*wp-register.php$'] = $this->index . '?register=true'; // Deprecated // Post rewrite rules. $post_rewrite = $this->generate_rewrite_rules( $this->permalink_structure, EP_PERMALINK ); /** * Filter rewrite rules used for "post" archives. * * @since 1.5.0 * * @param array $post_rewrite The rewrite rules for posts. */ $post_rewrite = apply_filters( 'post_rewrite_rules', $post_rewrite ); // Date rewrite rules. $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE); /** * Filter rewrite rules used for date archives. * * Likely date archives would include /yyyy/, /yyyy/mm/, and /yyyy/mm/dd/. * * @since 1.5.0 * * @param array $date_rewrite The rewrite rules for date archives. */ $date_rewrite = apply_filters( 'date_rewrite_rules', $date_rewrite ); // Root-level rewrite rules. $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT); /** * Filter rewrite rules used for root-level archives. * * Likely root-level archives would include pagination rules for the homepage * as well as site-wide post feeds (e.g. /feed/, and /feed/atom/). * * @since 1.5.0 * * @param array $root_rewrite The root-level rewrite rules. */ $root_rewrite = apply_filters( 'root_rewrite_rules', $root_rewrite ); // Comments rewrite rules. $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, false, true, true, false); /** * Filter rewrite rules used for comment feed archives. * * Likely comments feed archives include /comments/feed/, and /comments/feed/atom/. * * @since 1.5.0 * * @param array $comments_rewrite The rewrite rules for the site-wide comments feeds. */ $comments_rewrite = apply_filters( 'comments_rewrite_rules', $comments_rewrite ); // Search rewrite rules. $search_structure = $this->get_search_permastruct(); $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH); /** * Filter rewrite rules used for search archives. * * Likely search-related archives include /search/search+query/ as well as * pagination and feed paths for a search. * * @since 1.5.0 * * @param array $search_rewrite The rewrite rules for search queries. */ $search_rewrite = apply_filters( 'search_rewrite_rules', $search_rewrite ); // Author rewrite rules. $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS); /** * Filter rewrite rules used for author archives. * * Likely author archives would include /author/author-name/, as well as * pagination and feed paths for author archives. * * @since 1.5.0 * * @param array $author_rewrite The rewrite rules for author archives. */ $author_rewrite = apply_filters( 'author_rewrite_rules', $author_rewrite ); // Pages rewrite rules. $page_rewrite = $this->page_rewrite_rules(); /** * Filter rewrite rules used for "page" post type archives. * * @since 1.5.0 * * @param array $page_rewrite The rewrite rules for the "page" post type. */ $page_rewrite = apply_filters( 'page_rewrite_rules', $page_rewrite ); // Extra permastructs. foreach ( $this->extra_permastructs as $permastructname => $struct ) { if ( is_array( $struct ) ) { if ( count( $struct ) == 2 ) $rules = $this->generate_rewrite_rules( $struct[0], $struct[1] ); else $rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] ); } else { $rules = $this->generate_rewrite_rules( $struct ); } /** * Filter rewrite rules used for individual permastructs. * * The dynamic portion of the hook name, $permastructname, refers * to the name of the registered permastruct, e.g. 'post_tag' (tags), * 'category' (categories), etc. * * @since 3.1.0 * * @param array $rules The rewrite rules generated for the current permastruct. */ $rules = apply_filters( $permastructname . '_rewrite_rules', $rules ); if ( 'post_tag' == $permastructname ) { /** * Filter rewrite rules used specifically for Tags. * * @since 2.3.0 * @deprecated 3.1.0 Use 'post_tag_rewrite_rules' instead * * @param array $rules The rewrite rules generated for tags. */ $rules = apply_filters( 'tag_rewrite_rules', $rules ); } $this->extra_rules_top = array_merge($this->extra_rules_top, $rules); } // Put them together. if ( $this->use_verbose_page_rules ) $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules); else $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules); /** * Fires after the rewrite rules are generated. * * @since 1.5.0 * * @param WP_Rewrite $this Current WP_Rewrite instance, passed by reference. */ do_action_ref_array( 'generate_rewrite_rules', array( &$this ) ); /** * Filter the full set of generated rewrite rules. * * @since 1.5.0 * * @param array $this->rules The compiled array of rewrite rules. */ $this->rules = apply_filters( 'rewrite_rules_array', $this->rules ); return $this->rules; } |
クラス WP_Rewrite::wp_rewrite_rules()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Retrieve the rewrite rules. * * The difference between this method and {@link * WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules * in the 'rewrite_rules' option and retrieves it. This prevents having to * process all of the permalinks to get the rewrite rules in the form of * caching. * * @since 1.5.0 * @access public * * @return array Rewrite rules. */ public function wp_rewrite_rules() { $this->rules = get_option('rewrite_rules'); if ( empty($this->rules) ) { $this->matches = 'matches'; $this->rewrite_rules(); update_option('rewrite_rules', $this->rules); } return $this->rules; } |
クラス WP_Rewrite::mod_rewrite_rules()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | /** * Retrieve mod_rewrite formatted rewrite rules to write to .htaccess. * * Does not actually write to the .htaccess file, but creates the rules for * the process that will. * * Will add the non_wp_rules property rules to the .htaccess file before * the WordPress rewrite rules one. * * @since 1.5.0 * @access public * * @return string */ public function mod_rewrite_rules() { if ( ! $this->using_permalinks() ) return ''; $site_root = parse_url( site_url() ); if ( isset( $site_root['path'] ) ) $site_root = trailingslashit($site_root['path']); $home_root = parse_url(home_url()); if ( isset( $home_root['path'] ) ) $home_root = trailingslashit($home_root['path']); else $home_root = '/'; $rules = "<IfModule mod_rewrite.c>\n"; $rules .= "RewriteEngine On\n"; $rules .= "RewriteBase $home_root\n"; $rules .= "RewriteRule ^index\.php$ - [L]\n"; // Prevent -f checks on index.php. //add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all) foreach ( (array) $this->non_wp_rules as $match => $query) { // Apache 1.3 does not support the reluctant (non-greedy) modifier. $match = str_replace('.+?', '.+', $match); // If the match is unanchored and greedy, prepend rewrite conditions // to avoid infinite redirects and eclipsing of real files. //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { //nada. //} $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; } if ( $this->use_verbose_rules ) { $this->matches = ''; $rewrite = $this->rewrite_rules(); $num_rules = count($rewrite); $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . "RewriteCond %{REQUEST_FILENAME} -d\n" . "RewriteRule ^.*$ - [S=$num_rules]\n"; foreach ( (array) $rewrite as $match => $query) { // Apache 1.3 does not support the reluctant (non-greedy) modifier. $match = str_replace('.+?', '.+', $match); // If the match is unanchored and greedy, prepend rewrite conditions // to avoid infinite redirects and eclipsing of real files. //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { //nada. //} if ( strpos($query, $this->index) !== false ) $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; else $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; } } else { $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" . "RewriteCond %{REQUEST_FILENAME} !-d\n" . "RewriteRule . {$home_root}{$this->index} [L]\n"; } $rules .= "</IfModule>\n"; /** * * Filter the list of rewrite rules formatted for output to an .htaccess file. * * @since 1.5.0 * * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess. */ $rules = apply_filters( 'mod_rewrite_rules', $rules ); /** * Filter the list of rewrite rules formatted for output to an .htaccess file. * * @since 1.5.0 * @deprecated 1.5.0 Use the mod_rewrite_rules filter instead. * * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess. */ $rules = apply_filters( 'rewrite_rules', $rules ); // Deprecated return $rules; } |