WordPressを読む 47-2 /blog/wp-includes/rewrite.php 2
2015/01/14
目次
/blog/wp-includes/rewrite.php 2
クラス WP_Rewrite::クラス変数
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | /** * WordPress Rewrite Component. * * The WordPress Rewrite class writes the rewrite module rules to the .htaccess * file. It also handles parsing the request to get the correct setup for the * WordPress Query class. * * The Rewrite along with WP class function as a front controller for WordPress. * You can add rules to trigger your page view and processing using this * component. The full functionality of a front controller does not exist, * meaning you can't define how the template files load based on the rewrite * rules. * * @since 1.5.0 */ class WP_Rewrite { /** * Permalink structure for posts. * * @since 1.5.0 * @access private * @var string */ var $permalink_structure; /** * Whether to add trailing slashes. * * @since 2.2.0 * @access private * @var bool */ var $use_trailing_slashes; /** * Base for the author permalink structure (example.com/$author_base/authorname). * * @since 1.5.0 * @access private * @var string */ var $author_base = 'author'; /** * Permalink structure for author archives. * * @since 1.5.0 * @access private * @var string */ var $author_structure; /** * Permalink structure for date archives. * * @since 1.5.0 * @access private * @var string */ var $date_structure; /** * Permalink structure for pages. * * @since 1.5.0 * @access private * @var string */ var $page_structure; /** * Base of the search permalink structure (example.com/$search_base/query). * * @since 1.5.0 * @access private * @var string */ var $search_base = 'search'; /** * Permalink structure for searches. * * @since 1.5.0 * @access private * @var string */ var $search_structure; /** * Comments permalink base. * * @since 1.5.0 * @access private * @var string */ var $comments_base = 'comments'; /** * Pagination permalink base. * * @since 3.1.0 * @access private * @var string */ var $pagination_base = 'page'; /** * Feed permalink base. * * @since 1.5.0 * @access private * @var string */ var $feed_base = 'feed'; /** * Comments feed permalink structure. * * @since 1.5.0 * @access private * @var string */ var $comments_feed_structure; /** * Feed request permalink structure. * * @since 1.5.0 * @access private * @var string */ var $feed_structure; /** * The static portion of the post permalink structure. * * If the permalink structure is "/archive/%post_id%" then the front * is "/archive/". If the permalink structure is "/%year%/%postname%/" * then the front is "/". * * @see WP_Rewrite::init() * @since 1.5.0 * @access private * @var string */ var $front; /** * The prefix for all permalink structures. * * If PATHINFO/index permalinks are in use then the root is the value of * {@link WP_Rewrite::$index} with a trailing slash appended. Otherwise * the root will be empty. * * @see WP_Rewrite::init() * @see WP_Rewrite::using_index_permalinks() * @since 1.5.0 * @access private * @var string */ var $root = ''; /** * The name of the index file which is the entry point to all requests. * * @since 1.5.0 * @access public * @var string */ public $index = 'index.php'; /** * Variable name to use for regex matches in the rewritten query. * * @since 1.5.0 * @access private * @var string */ var $matches = ''; /** * Rewrite rules to match against the request to find the redirect or query. * * @since 1.5.0 * @access private * @var array */ var $rules; /** * Additional rules added external to the rewrite class. * * Those not generated by the class, see add_rewrite_rule(). * * @since 2.1.0 * @access private * @var array */ var $extra_rules = array(); /** * Additional rules that belong at the beginning to match first. * * Those not generated by the class, see add_rewrite_rule(). * * @since 2.3.0 * @access private * @var array */ var $extra_rules_top = array(); /** * Rules that don't redirect to WordPress' index.php. * * These rules are written to the mod_rewrite portion of the .htaccess, * and are added by {@link add_external_rule()}. * * @since 2.1.0 * @access private * @var array */ var $non_wp_rules = array(); /** * Extra permalink structures, e.g. categories, added by {@link add_permastruct()}. * * @since 2.1.0 * @access private * @var array */ var $extra_permastructs = array(); /** * Endpoints (like /trackback/) added by {@link add_rewrite_endpoint()}. * * @since 2.1.0 * @access private * @var array */ var $endpoints; /** * Whether to write every mod_rewrite rule for WordPress into the .htaccess file. * * This is off by default, turning it on might print a lot of rewrite rules * to the .htaccess file. * * @see WP_Rewrite::mod_rewrite_rules() * @since 2.0.0 * @access public * @var bool */ public $use_verbose_rules = false; /** * Could post permalinks be confused with those of pages? * * If the first rewrite tag in the post permalink structure is one that could * also match a page name (e.g. %postname% or %author%) then this flag is * set to true. Prior to WordPress 3.3 this flag indicated that every page * would have a set of rules added to the top of the rewrite rules array. * Now it tells {@link WP::parse_request()} to check if a URL matching the * page permastruct is actually a page before accepting it. * * @link http://core.trac.wordpress.org/ticket/16687 * @see WP_Rewrite::init() * @since 2.5.0 * @access public * @var bool */ public $use_verbose_page_rules = true; /** * Rewrite tags that can be used in permalink structures. * * These are translated into the regular expressions stored in * {@link WP_Rewrite::$rewritereplace} and are rewritten to the * query variables listed in {@link WP_Rewrite::$queryreplace}. * * Additional tags can be added with {@link add_rewrite_tag()}. * * @since 1.5.0 * @access private * @var array */ var $rewritecode = array( '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%postname%', '%post_id%', '%author%', '%pagename%', '%search%' ); /** * Regular expressions to be substituted into rewrite rules in place * of rewrite tags, see {@link WP_Rewrite::$rewritecode}. * * @since 1.5.0 * @access private * @var array */ var $rewritereplace = array( '([0-9]{4})', '([0-9]{1,2})', '([0-9]{1,2})', '([0-9]{1,2})', '([0-9]{1,2})', '([0-9]{1,2})', '([^/]+)', '([0-9]+)', '([^/]+)', '([^/]+?)', '(.+)' ); /** * Query variables that rewrite tags map to, see {@link WP_Rewrite::$rewritecode}. * * @since 1.5.0 * @access private * @var array */ var $queryreplace = array( 'year=', 'monthnum=', 'day=', 'hour=', 'minute=', 'second=', 'name=', 'p=', 'author_name=', 'pagename=', 's=' ); /** * Supported default feeds. * * @since 1.5.0 * @access private * @var array */ var $feeds = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' ); |
クラス WP_Rewrite::using_permalinks()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Whether permalinks are being used. * * This can be either rewrite module or permalink in the HTTP query string. * * @since 1.5.0 * @access public * * @return bool True, if permalinks are enabled. */ public function using_permalinks() { return ! empty($this->permalink_structure); } |
クラス WP_Rewrite::using_index_permalinks()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Whether permalinks are being used and rewrite module is not enabled. * * Means that permalink links are enabled and index.php is in the URL. * * @since 1.5.0 * @access public * * @return bool */ public function using_index_permalinks() { if ( empty($this->permalink_structure) ) return false; // If the index is not in the permalink, we're using mod_rewrite. if ( preg_match('#^/*' . $this->index . '#', $this->permalink_structure) ) return true; return false; } |
クラス WP_Rewrite::using_mod_rewrite_permalinks()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Whether permalinks are being used and rewrite module is enabled. * * Using permalinks and index.php is not in the URL. * * @since 1.5.0 * @access public * * @return bool */ public function using_mod_rewrite_permalinks() { if ( $this->using_permalinks() && ! $this->using_index_permalinks() ) return true; else return false; } |
クラス WP_Rewrite::preg_index()
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 | /** * Index for matches for usage in preg_*() functions. * * The format of the string is, with empty matches property value, '$NUM'. * The 'NUM' will be replaced with the value in the $number parameter. With * the matches property not empty, the value of the returned string will * contain that value of the matches property. The format then will be * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the * value of the $number parameter. * * @since 1.5.0 * @access public * * @param int $number Index number. * @return string */ public function preg_index($number) { $match_prefix = '$'; $match_suffix = ''; if ( ! empty($this->matches) ) { $match_prefix = '$' . $this->matches . '['; $match_suffix = ']'; } return "$match_prefix$number$match_suffix"; } |
クラス WP_Rewrite::page_uri_index()
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 | /** * Retrieve all page and attachments for pages URIs. * * The attachments are for those that have pages as parents and will be * retrieved. * * @since 2.5.0 * @access public * * @return array Array of page URIs as first element and attachment URIs as second element. */ public function page_uri_index() { global $wpdb; //get pages in order of hierarchy, i.e. children after parents $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'"); $posts = get_page_hierarchy( $pages ); // If we have no pages get out quick if ( !$posts ) return array( array(), array() ); //now reverse it, because we need parents after children for rewrite rules to work properly $posts = array_reverse($posts, true); $page_uris = array(); $page_attachment_uris = array(); foreach ( $posts as $id => $post ) { // URL => page name $uri = get_page_uri($id); $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id )); if ( !empty($attachments) ) { foreach ( $attachments as $attachment ) { $attach_uri = get_page_uri($attachment->ID); $page_attachment_uris[$attach_uri] = $attachment->ID; } } $page_uris[$uri] = $id; } return array( $page_uris, $page_attachment_uris ); } |