classes.php
来自「php 开发的内容管理系统」· PHP 代码 · 共 1,728 行 · 第 1/4 页
PHP
1,728 行
} $post_rewrite = array_merge($rewrite, $post_rewrite); } return $post_rewrite; } function generate_rewrite_rule($permalink_structure, $walk_dirs = false) { return $this->generate_rewrite_rules($permalink_structure, false, false, false, $walk_dirs); } /* rewrite_rules * Construct rewrite matches and queries from permalink structure. * Returns an associate array of matches and queries. */ function rewrite_rules() { $rewrite = array(); if (empty($this->permalink_structure)) { return $rewrite; } // Post $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure); $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite); // Date $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct()); $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite); // Root $root_rewrite = $this->generate_rewrite_rules($this->root . '/'); $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite); // Comments $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, true, true, true, false); $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite); // Search $search_structure = $this->get_search_permastruct(); $search_rewrite = $this->generate_rewrite_rules($search_structure); $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); // Categories $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct()); $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite); // Authors $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct()); $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite); // Pages $page_rewrite = $this->page_rewrite_rules(); $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite); // Put them together. $this->rules = array_merge($page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $author_rewrite, $date_rewrite, $post_rewrite); do_action('generate_rewrite_rules', array(&$this)); $this->rules = apply_filters('rewrite_rules_array', $this->rules); return $this->rules; } 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; } function mod_rewrite_rules() { if ( ! $this->using_permalinks()) { return ''; } $site_root = parse_url(get_settings('siteurl')); $site_root = trailingslashit($site_root['path']); $home_root = parse_url(get_settings('home')); $home_root = trailingslashit($home_root['path']); $rules = "<IfModule mod_rewrite.c>\n"; $rules .= "RewriteEngine On\n"; $rules .= "RewriteBase $home_root\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 ($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 (strstr($query, $this->index)) { $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"; $rules = apply_filters('mod_rewrite_rules', $rules); $rules = apply_filters('rewrite_rules', $rules); // Deprecated return $rules; } function flush_rules() { generate_page_rewrite_rules(); delete_option('rewrite_rules'); $this->wp_rewrite_rules(); if ( function_exists('save_mod_rewrite_rules') ) save_mod_rewrite_rules(); } function init() { $this->permalink_structure = get_settings('permalink_structure'); $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%')); $this->root = ''; if ($this->using_index_permalinks()) { $this->root = $this->index . '/'; } $this->category_base = get_settings('category_base'); unset($this->category_structure); unset($this->author_structure); unset($this->date_structure); unset($this->page_structure); unset($this->search_structure); unset($this->feed_structure); unset($this->comment_feed_structure); } function set_permalink_structure($permalink_structure) { if ($permalink_structure != $this->permalink_structure) { update_option('permalink_structure', $permalink_structure); $this->init(); } } function set_category_base($category_base) { if ($category_base != $this->category_base) { update_option('category_base', $category_base); $this->init(); } } function WP_Rewrite() { $this->init(); }}class WP { var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview'); var $private_query_vars = array('posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'show_post_type'); var $query_vars; var $query_string; var $request; var $matched_rule; var $matched_query; var $did_permalink = false; function parse_request($extra_query_vars = '') { global $wp_rewrite; $this->query_vars = array(); if (! empty($extra_query_vars)) parse_str($extra_query_vars, $extra_query_vars); else $extra_query_vars = array(); // Process PATH_INFO, REQUEST_URI, and 404 for permalinks. // Fetch the rewrite rules. $rewrite = $wp_rewrite->wp_rewrite_rules(); if (! empty($rewrite)) { // If we match a rewrite rule, this will be cleared. $error = '404'; $this->did_permalink = true; $pathinfo = $_SERVER['PATH_INFO']; $pathinfo_array = explode('?', $pathinfo); $pathinfo = $pathinfo_array[0]; $req_uri = $_SERVER['REQUEST_URI']; $req_uri_array = explode('?', $req_uri); $req_uri = $req_uri_array[0]; $self = $_SERVER['PHP_SELF']; $home_path = parse_url(get_settings('home')); $home_path = $home_path['path']; $home_path = trim($home_path, '/'); // Trim path info from the end and the leading home path from the // front. For path info requests, this leaves us with the requesting // filename, if any. For 404 requests, this leaves us with the // requested permalink. $req_uri = str_replace($pathinfo, '', $req_uri); $req_uri = trim($req_uri, '/'); $req_uri = preg_replace("|^$home_path|", '', $req_uri); $req_uri = trim($req_uri, '/'); $pathinfo = trim($pathinfo, '/'); $pathinfo = preg_replace("|^$home_path|", '', $pathinfo); $pathinfo = trim($pathinfo, '/'); $self = trim($self, '/'); $self = preg_replace("|^$home_path|", '', $self); $self = str_replace($home_path, '', $self); $self = trim($self, '/'); // The requested permalink is in $pathinfo for path info requests and // $req_uri for other requests. if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) { $request = $pathinfo; } else { // If the request uri is the index, blank it out so that we don't try to match it against a rule. if ( $req_uri == $wp_rewrite->index ) $req_uri = ''; $request = $req_uri; } $this->request = $request; // Look for matches. $request_match = $request; foreach ($rewrite as $match => $query) { // If the requesting file is the anchor of the match, prepend it // to the path info. if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request)) { $request_match = $req_uri . '/' . $request; } if (preg_match("!^$match!", $request_match, $matches) || preg_match("!^$match!", urldecode($request_match), $matches)) { // Got a match. $this->matched_rule = $match; // Trim the query of everything up to the '?'. $query = preg_replace("!^.+\?!", '', $query); // Substitute the substring matches into the query. eval("\$query = \"$query\";"); $this->matched_query = $query; // Parse the query. parse_str($query, $query_vars); // If we're processing a 404 request, clear the error var // since we found something. if (isset($_GET['error'])) unset($_GET['error']); if (isset($error)) unset($error); break; } } // If req_uri is empty or if it is a request for ourself, unset error. if ( empty($request) || $req_uri == $self || strstr($_SERVER['PHP_SELF'], 'wp-admin/') ) { if (isset($_GET['error'])) unset($_GET['error']); if (isset($error)) unset($error); if ( isset($query_vars) && strstr($_SERVER['PHP_SELF'], 'wp-admin/') ) unset($query_vars); $this->did_permalink = false; } } $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars); for ($i=0; $i<count($this->public_query_vars); $i += 1) { $wpvar = $this->public_query_vars[$i]; if (isset($extra_query_vars[$wpvar])) $this->query_vars[$wpvar] = $extra_query_vars[$wpvar]; elseif (isset($GLOBALS[$wpvar])) $this->query_vars[$wpvar] = $GLOBALS[$wpvar]; elseif (!empty($_POST[$wpvar])) $this->query_vars[$wpvar] = $_POST[$wpvar]; elseif (!empty($_GET[$wpvar])) $this->query_vars[$wpvar] = $_GET[$wpvar]; elseif (!empty($query_vars[$wpvar])) $this->query_vars[$wpvar] = $query_vars[$wpvar]; else $this->query_vars[$wpvar] = ''; } if ( isset($error) ) $this->query_vars['error'] = $error; } function send_headers() { @header('X-Pingback: '. get_bloginfo('pingback_url')); if ( is_user_logged_in() ) nocache_headers(); if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) { status_header( 404 ); @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); } else if ( empty($this->query_vars['feed']) ) { @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); } else { // We're showing a feed, so WP is indeed the only thing that last changed if ( $this->query_vars['withcomments'] ) $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT'; else $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT'; $wp_etag = '"' . md5($wp_last_modified) . '"'; @header("Last-Modified: $wp_last_modified"); @header("ETag: $wp_etag"); // Support for Conditional GET if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']); else $client_etag = false; $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']); // If string is empty, return 0. If not, attempt to parse into a timestamp $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0; // Make a timestamp for our most recent modification... $wp_modified_timestamp = strtotime($wp_last_modified); if ( ($client_last_modified && $client_etag) ? (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) : (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) { status_header( 304 ); exit; } } } function build_query_string() { $this->query_string = ''; foreach ($this->public_query_vars as $wpvar) { if (isset($this->query_vars[$wpvar]) && '' != $this->query_vars[$wpvar]) { $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&'; $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]); } } foreach ($this->private_query_vars as $wpvar) { if (isset($GLOBALS[$wpvar]) && '' != $GLOBALS[$wpvar]) { $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&'; $this->query_string .= $wpvar . '=' . rawurlencode($GLOBALS[$wpvar]); } } $this->query_string = apply_filters('query_string', $this->query_string); } function register_globals() { global $wp_query; // Extract updated query vars back into global namespace. foreach ($wp_query->query_vars as $key => $value) { $GLOBALS[$key] = $value; } $GLOBALS['query_string'] = & $this->query_string; $GLOBALS['posts'] = & $wp_query->posts; $GLOBALS['post'] = & $wp_query->post; $GLOBALS['request'] = & $wp_query->request; if ( is_single() || is_page() ) { $GLOBALS['more'] = 1; $GLOBALS['single'] = 1; } } function init() { wp_get_current_user(); } function query_posts() { $this->build_query_string(); query_posts($this->query_string); } function handle_404() { global $wp_query; // Issue a 404 if a permalink request doesn't match any posts. Don't // issue a 404 if one was already issued, if the request was a search, // or if the request was a regular query string request rather than a // permalink request. if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) { $wp_query->set_404(); status_header( 404 ); } elseif( is_404() != true ) { status_header( 200 ); } } function main($query_args = '') { $this->init(); $this->parse_request($query_args); $this->send_headers(); $this->query_posts(); $this->handle_404(); $this->register_globals(); } function WP() { // Empty. }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?