本文整理汇总了PHP中WP_MatchesMapRegex类的典型用法代码示例。如果您正苦于以下问题:PHP WP_MatchesMapRegex类的具体用法?PHP WP_MatchesMapRegex怎么用?PHP WP_MatchesMapRegex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WP_MatchesMapRegex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parse_request
//.........这里部分代码省略.........
$this->request = $request;
// Look for matches.
$request_match = $request;
if (empty($request_match)) {
// An empty request could only match against ^$ regex
if (isset($rewrite['$'])) {
$this->matched_rule = '$';
$query = $rewrite['$'];
$matches = array('');
}
} else {
foreach ((array) $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)) {
if ($wp_rewrite->use_verbose_page_rules && preg_match('/pagename=\\$matches\\[([0-9]+)\\]/', $query, $varmatch)) {
// this is a verbose page match, lets check to be sure about it
if (!get_page_by_path($matches[$varmatch[1]])) {
continue;
}
}
// Got a match.
$this->matched_rule = $match;
break;
}
}
}
if (isset($this->matched_rule)) {
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
$this->matched_query = $query;
// Parse the query.
parse_str($query, $perma_query_vars);
// If we're processing a 404 request, clear the error var since we found something.
if ('404' == $error) {
unset($error, $_GET['error']);
}
}
// If req_uri is empty or if it is a request for ourself, unset error.
if (empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
unset($error, $_GET['error']);
if (isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
unset($perma_query_vars);
}
$this->did_permalink = false;
}
}
/**
* Filter the query variables whitelist before processing.
*
* Allows (publicly allowed) query vars to be added, removed, or changed prior
* to executing the query. Needed to allow custom rewrite rules using your own arguments
* to work, or any other custom query variables you want to be publicly available.
*
* @since 1.5.2
*
* @param array $public_query_vars The array of whitelisted query variables.
*/
$this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
foreach (get_post_types(array(), 'objects') as $post_type => $t) {
if ($t->query_var) {
$post_type_query_vars[$t->query_var] = $post_type;
示例2: url_to_postid
/**
* Examine a url and try to determine the post ID it represents.
*
* Checks are supposedly from the hosted site blog.
*
* @since 1.0.0
*
* @param string $url Permalink to check.
* @return int Post ID, or 0 on failure.
*/
function url_to_postid($url)
{
global $wp_rewrite;
$url = apply_filters('url_to_postid', $url);
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if (preg_match('#[?&](p|page_id|attachment_id)=(\\d+)#', $url, $values)) {
$id = absint($values[2]);
if ($id) {
return $id;
}
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if (empty($rewrite)) {
return 0;
}
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
// Get rid of URL ?query=string
$url_split = explode('?', $url);
$url = $url_split[0];
// Add 'www.' if it is absent and should be there
if (false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.')) {
$url = str_replace('://', '://www.', $url);
}
// Strip 'www.' if it is present and shouldn't be
if (false === strpos(home_url(), '://www.')) {
$url = str_replace('://www.', '://', $url);
}
// Strip 'index.php/' if we're not using path info permalinks
if (!$wp_rewrite->using_index_permalinks()) {
$url = str_replace('index.php/', '', $url);
}
if (false !== strpos($url, home_url())) {
// Chop off http://domain.com
$url = str_replace(home_url(), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url(home_url());
$home_path = isset($home_path['path']) ? $home_path['path'] : '';
$url = str_replace($home_path, '', $url);
}
// Trim leading and lagging slashes
$url = trim($url, '/');
$request = $url;
// Look for matches.
$request_match = $request;
foreach ((array) $rewrite as $match => $query) {
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if (!empty($url) && $url != $request && strpos($match, $url) === 0) {
$request_match = $url . '/' . $request;
}
if (preg_match("!^{$match}!", $request_match, $matches)) {
if ($wp_rewrite->use_verbose_page_rules && preg_match('/pagename=\\$matches\\[([0-9]+)\\]/', $query, $varmatch)) {
// this is a verbose page match, lets check to be sure about it
if (!get_page_by_path($matches[$varmatch[1]])) {
continue;
}
}
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Filter out non-public query vars
global $wp;
parse_str($query, $query_vars);
$query = array();
foreach ((array) $query_vars as $key => $value) {
if (in_array($key, $wp->public_query_vars)) {
$query[$key] = $value;
}
}
// Do the query
$query = new WP_Query($query);
if (!empty($query->posts) && $query->is_singular) {
return $query->post->ID;
} else {
return 0;
}
}
}
return 0;
}
示例3: parse_request
//.........这里部分代码省略.........
$this->request = $request;
// Look for matches.
$request_match = $request;
if (empty($request_match)) {
// An empty request could only match against ^$ regex
if (isset($rewrite['$'])) {
$this->matched_rule = '$';
$query = $rewrite['$'];
$matches = array('');
}
} else {
foreach ((array) $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)) {
if ($wp_rewrite->use_verbose_page_rules && preg_match('/pagename=\\$matches\\[([0-9]+)\\]/', $query, $varmatch)) {
// this is a verbose page match, lets check to be sure about it
if (!get_page_by_path($matches[$varmatch[1]])) {
continue;
}
}
// Got a match.
$this->matched_rule = $match;
break;
}
}
}
if (isset($this->matched_rule)) {
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
$this->matched_query = $query;
// Parse the query.
parse_str($query, $perma_query_vars);
// If we're processing a 404 request, clear the error var since we found something.
if ('404' == $error) {
unset($error, $_GET['error']);
}
}
// If req_uri is empty or if it is a request for ourself, unset error.
if (empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
unset($error, $_GET['error']);
if (isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
unset($perma_query_vars);
}
$this->did_permalink = false;
}
}
$this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
foreach ($GLOBALS['wp_post_types'] as $post_type => $t) {
if ($t->query_var) {
$post_type_query_vars[$t->query_var] = $post_type;
}
}
foreach ($this->public_query_vars as $wpvar) {
if (isset($this->extra_query_vars[$wpvar])) {
$this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
} elseif (isset($_POST[$wpvar])) {
$this->query_vars[$wpvar] = $_POST[$wpvar];
} elseif (isset($_GET[$wpvar])) {
$this->query_vars[$wpvar] = $_GET[$wpvar];
} elseif (isset($perma_query_vars[$wpvar])) {
$this->query_vars[$wpvar] = $perma_query_vars[$wpvar];
示例4: _process_generic_text
//.........这里部分代码省略.........
} else {
$home_path = '';
}
$home_path = trim($home_path, '/');
$req_uri = str_replace($pathinfo, '', rawurldecode($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, '/');
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;
}
$request_match = $request;
$permalink_query_vars = array();
foreach ((array) $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.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Parse the query.
parse_str($query, $permalink_query_vars);
break;
}
}
$post_name = $category_name = $tax_name = false;
if (isset($permalink_query_vars['pagename'])) {
$icl_post_lang = isset($_POST['icl_post_language']) ? $_POST['icl_post_language'] : $current_language;
$sitepress->switch_lang($icl_post_lang);
$page_by_path = get_page_by_path($permalink_query_vars['pagename']);
$sitepress->switch_lang($current_language);
if (!empty($page_by_path->post_type)) {
$post_name = $permalink_query_vars['pagename'];
$post_type = 'page';
} else {
$post_name = $permalink_query_vars['pagename'];
$post_type = 'post';
}
} elseif (isset($permalink_query_vars['name'])) {
$post_name = $permalink_query_vars['name'];
$post_type = 'post';
} elseif (isset($permalink_query_vars['category_name'])) {
$category_name = $permalink_query_vars['category_name'];
} elseif (isset($permalink_query_vars['p'])) {
// case or /archives/%post_id
$post_data_prepared = $wpdb->prepare("SELECT post_type, post_name FROM {$wpdb->posts} WHERE id=%d", $permalink_query_vars['p']);
list($post_type, $post_name) = $wpdb->get_row($post_data_prepared, ARRAY_N);
} else {
if (empty($this->custom_post_query_vars) or empty($this->taxonomies_query_vars)) {
$this->init_query_vars();
}
foreach ($this->custom_post_query_vars as $query_vars_key => $query_vars_value) {
示例5: wpl_url_to_postid
function wpl_url_to_postid($url)
{
global $wp_rewrite;
$url = apply_filters('url_to_postid', $url);
$id = url_to_postid($url);
if (isset($id) && $id > 0) {
return $id;
}
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if (preg_match('#[?&](p|page_id|attachment_id)=(\\d+)#', $url, $values)) {
$id = absint($values[2]);
if ($id) {
return $id;
}
}
//first check if URL is homepage
$wordpress_url = get_bloginfo('url');
if (substr($wordpress_url, -1, -1) != '/') {
$wordpress_url = $wordpress_url . "/";
}
if (str_replace('/', '', $url) == str_replace('/', '', $wordpress_url)) {
return get_option('page_on_front');
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if (empty($rewrite)) {
return 0;
}
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
// Get rid of URL ?query=string
$url_split = explode('?', $url);
$url = $url_split[0];
// Add 'www.' if it is absent and should be there
if (false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.')) {
$url = str_replace('://', '://www.', $url);
}
// Strip 'www.' if it is present and shouldn't be
if (false === strpos(home_url(), '://www.')) {
$url = str_replace('://www.', '://', $url);
}
// Strip 'index.php/' if we're not using path info permalinks
if (!$wp_rewrite->using_index_permalinks()) {
$url = str_replace('index.php/', '', $url);
}
if (false !== strpos($url, home_url())) {
// Chop off http://domain.com
$url = str_replace(home_url(), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url(home_url());
$home_path = isset($home_path['path']) ? $home_path['path'] : '';
$url = str_replace($home_path, '', $url);
}
// Trim leading and lagging slashes
$url = trim($url, '/');
$request = $url;
// Look for matches.
$request_match = $request;
foreach ((array) $rewrite as $match => $query) {
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if (!empty($url) && $url != $request && strpos($match, $url) === 0) {
$request_match = $url . '/' . $request;
}
if (preg_match("!^{$match}!", $request_match, $matches)) {
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Filter out non-public query vars
global $wp;
parse_str($query, $query_vars);
$query = array();
foreach ((array) $query_vars as $key => $value) {
if (in_array($key, $wp->public_query_vars)) {
$query[$key] = $value;
}
}
// Taken from class-wp.php
foreach ($GLOBALS['wp_post_types'] as $post_type => $t) {
if ($t->query_var) {
$post_type_query_vars[$t->query_var] = $post_type;
}
}
foreach ($wp->public_query_vars as $wpvar) {
if (isset($wp->extra_query_vars[$wpvar])) {
$query[$wpvar] = $wp->extra_query_vars[$wpvar];
} elseif (isset($_POST[$wpvar])) {
$query[$wpvar] = $_POST[$wpvar];
} elseif (isset($_GET[$wpvar])) {
$query[$wpvar] = $_GET[$wpvar];
} elseif (isset($query_vars[$wpvar])) {
$query[$wpvar] = $query_vars[$wpvar];
}
if (!empty($query[$wpvar])) {
if (!is_array($query[$wpvar])) {
//.........这里部分代码省略.........
示例6: query_vars
//.........这里部分代码省略.........
} else {
$pathinfo = '';
}
$pathinfo_array = explode('?', $pathinfo);
$pathinfo = str_replace("%", "%25", $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(home_url());
if (isset($home_path['path'])) {
$home_path = $home_path['path'];
} else {
$home_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 = 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;
}
$wp->request = $request;
// Look for matches.
$request_match = $request;
if (empty($request_match)) {
// An empty request could only match against ^$ regex
if (isset($rewrite['$'])) {
$wp->matched_rule = '$';
$query = $rewrite['$'];
$matches = array('');
}
} else {
if ($req_uri != 'wp-app.php') {
foreach ((array) $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)) {
if ($wp_rewrite->use_verbose_page_rules && preg_match('/pagename=\\$matches\\[([0-9]+)\\]/', $query, $varmatch)) {
// this is a verbose page match, lets check to be sure about it
if (!($page_foundid = $this->get_page_by_path($matches[$varmatch[1]]))) {
continue;
} else {
wp_cache_set('qts_page_request', $page_foundid);
// caching query :)
}
}
// Got a match.
$wp->matched_rule = $match;
break;
}
}
}
}
if (isset($wp->matched_rule)) {
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
$wp->matched_query = $query;
// Parse the query.
parse_str($query, $perma_query_vars);
// If we're processing a 404 request, clear the error var
// since we found something.
unset($_GET['error']);
unset($error);
}
// If req_uri is empty or if it is a request for ourself, unset error.
if (empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
unset($_GET['error']);
unset($error);
if (isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
unset($perma_query_vars);
}
$wp->did_permalink = false;
}
}
return $wp->public_query_vars;
}
示例7: url_to_postid
/**
* Examine a url and try to determine the post ID it represents.
*
* Checks are supposedly from the hosted site blog.
*
* @since 1.0.0
*
* @param string $url Permalink to check.
* @return int Post ID, or 0 on failure.
*/
function url_to_postid($url)
{
global $wp_rewrite;
$url = apply_filters('url_to_postid', $url);
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if (preg_match('#[?&](p|page_id|attachment_id)=(\\d+)#', $url, $values)) {
$id = absint($values[2]);
if ($id) {
return $id;
}
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if (empty($rewrite)) {
return 0;
}
// $url cleanup by Mark Jaquith
// This fixes things like #anchors, ?query=strings, missing 'www.',
// added 'www.', or added 'index.php/' that will mess up our WP_Query
// and return a false negative
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
// Get rid of URL ?query=string
$url_split = explode('?', $url);
$url = $url_split[0];
// Add 'www.' if it is absent and should be there
if (false !== strpos(get_option('home'), '://www.') && false === strpos($url, '://www.')) {
$url = str_replace('://', '://www.', $url);
}
// Strip 'www.' if it is present and shouldn't be
if (false === strpos(get_option('home'), '://www.')) {
$url = str_replace('://www.', '://', $url);
}
// Strip 'index.php/' if we're not using path info permalinks
if (!$wp_rewrite->using_index_permalinks()) {
$url = str_replace('index.php/', '', $url);
}
if (false !== strpos($url, get_option('home'))) {
// Chop off http://domain.com
$url = str_replace(get_option('home'), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url(get_option('home'));
$home_path = $home_path['path'];
$url = str_replace($home_path, '', $url);
}
// Trim leading and lagging slashes
$url = trim($url, '/');
$request = $url;
// Done with cleanup
// 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($url) && strpos($match, $url) === 0 && $url != $request) {
$request_match = $url . '/' . $request;
}
if (preg_match("!^{$match}!", $request_match, $matches)) {
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Filter out non-public query vars
global $wp;
parse_str($query, $query_vars);
$query = array();
foreach ((array) $query_vars as $key => $value) {
if (in_array($key, $wp->public_query_vars)) {
$query[$key] = $value;
}
}
// Do the query
$query = new WP_Query($query);
if ($query->is_single || $query->is_page) {
return $query->post->ID;
} else {
return 0;
}
}
}
return 0;
}
示例8: url_to_postid
//.........这里部分代码省略.........
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if ( empty($rewrite) )
return 0;
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
// Get rid of URL ?query=string
$url_split = explode('?', $url);
$url = $url_split[0];
// Add 'www.' if it is absent and should be there
if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
$url = str_replace('://', '://www.', $url);
// Strip 'www.' if it is present and shouldn't be
if ( false === strpos(home_url(), '://www.') )
$url = str_replace('://www.', '://', $url);
// Strip 'index.php/' if we're not using path info permalinks
if ( !$wp_rewrite->using_index_permalinks() )
$url = str_replace( $wp_rewrite->index . '/', '', $url );
if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
// Chop off http://domain.com/[path]
$url = str_replace(home_url(), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url( home_url( '/' ) );
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
}
// Trim leading and lagging slashes
$url = trim($url, '/');
$request = $url;
$post_type_query_vars = array();
foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
if ( ! empty( $t->query_var ) )
$post_type_query_vars[ $t->query_var ] = $post_type;
}
// Look for matches.
$request_match = $request;
foreach ( (array)$rewrite as $match => $query) {
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
$request_match = $url . '/' . $request;
if ( preg_match("#^$match#", $request_match, $matches) ) {
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
// This is a verbose page match, let's check to be sure about it.
if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
continue;
}
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Filter out non-public query vars
global $wp;
parse_str( $query, $query_vars );
$query = array();
foreach ( (array) $query_vars as $key => $value ) {
if ( in_array( $key, $wp->public_query_vars ) ){
$query[$key] = $value;
if ( isset( $post_type_query_vars[$key] ) ) {
$query['post_type'] = $post_type_query_vars[$key];
$query['name'] = $value;
}
}
}
// Resolve conflicts between posts with numeric slugs and date archive queries.
$query = wp_resolve_numeric_slug_conflicts( $query );
// Do the query
$query = new WP_Query( $query );
if ( ! empty( $query->posts ) && $query->is_singular )
return $query->post->ID;
else
return 0;
}
}
return 0;
}
示例9: change_rules_array
/**
* Change Rules
* Replace rewrite rules if subdomain is category
* @param array $rules wordpress from get_option('rewrite_rules');
* @return array Final rewrite rules
*/
public function change_rules_array($rules)
{
if (is_array($rules)) {
if ($this->settings['remove_category_permalink']) {
/*
Now We have to check that current request
using rules without category or not,
And the request what we are talking about should be "single" and its descendants (attachment, comment page, etc)
by checking the real rules
*/
$without_category = true;
if (2 == $this->settings['remove_category_permalink']) {
$request_match = $_SERVER['REQUEST_URI'];
$request_match = trim($request_match, '/');
$permalink_structure = get_option('permalink_structure');
$permalink_structure = trim($permalink_structure, '/');
/*
* only for single post, and there's no way 'slash' count would less than permalink structure
*/
if (substr_count($request_match, '/') >= substr_count($permalink_structure, '/')) {
/*
* the code below copied from class.wp.php
*/
foreach ($rules as $match => $query) {
if (stripos($query, 'category_name') !== false) {
if (preg_match("#^{$match}#", $request_match, $matches) || preg_match("#^{$match}#", urldecode($request_match), $matches)) {
// Trim the query of everything up to the '?'.
$query_result = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query_result = addslashes(WP_MatchesMapRegex::apply($query_result, $matches));
// Parse the query.
parse_str($query_result, $perma_query_vars);
if ((isset($perma_query_vars['name']) || isset($perma_query_vars['p'])) && isset($perma_query_vars['category_name'])) {
$without_category = false;
break;
}
}
}
}
}
}
/*
*update rules if we need to using rules without %category%
*/
if ($without_category) {
$rules_removed_category = $this->get_rules_array_removed_category_permalink();
if ($rules_removed_category) {
$rules = $rules_removed_category;
}
}
}
//replace all feed rules
$rules["feed/(feed|rdf|rss|rss2|atom)/?\$"] = "index.php?category_name=" . $this->subdomain->slug . "&feed=\$matches[1]";
$rules["(feed|rdf|rss|rss2|atom)/?\$"] = "index.php?category_name=" . $this->subdomain->slug . "&feed=\$matches[1]";
if (0 == $this->settings['using_index']) {
$rules2 = array();
$rules2["\$"] = "index.php?category_name=" . $this->subdomain->slug;
$rules2["page/?([0-9]{1,})/?\$"] = "index.php?category_name=" . $this->subdomain->slug . "&paged=\$matches[1]";
$rules = $rules2 + $rules;
}
}
return $rules;
}
示例10: parse_request
/**
* Parse request to find correct WordPress query.
*
* Sets up the query variables based on the request. There are also many
* filters and actions that can be used to further manipulate the result.
*
* @since 2.0.0
*
* @param array|string $extra_query_vars Set the extra query variables.
*/
function parse_request($extra_query_vars = '')
{
global $wp_rewrite;
$this->query_vars = array();
$taxonomy_query_vars = array();
if (is_array($extra_query_vars)) {
$this->extra_query_vars =& $extra_query_vars;
} else {
if (!empty($extra_query_vars)) {
parse_str($extra_query_vars, $this->extra_query_vars);
}
}
// 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;
if (isset($_SERVER['PATH_INFO'])) {
$pathinfo = $_SERVER['PATH_INFO'];
} else {
$pathinfo = '';
}
$pathinfo_array = explode('?', $pathinfo);
$pathinfo = str_replace("%", "%25", $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_option('home'));
if (isset($home_path['path'])) {
$home_path = $home_path['path'];
} else {
$home_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, '', rawurldecode($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 = 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 ((array) $rewrite as $match => $query) {
// Don't try to match against AtomPub calls
if ($req_uri == 'wp-app.php') {
break;
}
// 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.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
$this->matched_query = $query;
// Parse the query.
parse_str($query, $perma_query_vars);
// If we're processing a 404 request, clear the error var
// since we found something.
if (isset($_GET['error'])) {
unset($_GET['error']);
}
//.........这里部分代码省略.........
示例11: url_to_postid
//.........这里部分代码省略.........
}
// Strip 'index.php/' if we're not using path info permalinks
if (isset($wp_rewrite) && !$wp_rewrite->using_index_permalinks()) {
$url = str_replace('index.php/', '', $url);
}
if (false !== strpos($url, home_url())) {
// Chop off http://domain.com
$url = str_replace(home_url(), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url(home_url());
$home_path = isset($home_path['path']) ? $home_path['path'] : '';
$url = str_replace($home_path, '', $url);
}
// Trim leading and lagging slashes
$url = trim($url, '/');
$request = $url;
if (empty($request) && (!isset($_GET) || empty($_GET))) {
return get_option('page_on_front');
}
// Look for matches.
$request_match = $request;
foreach ((array) $rewrite as $match => $query) {
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if (!empty($url) && $url != $request && strpos($match, $url) === 0) {
$request_match = $url . '/' . $request;
}
if (preg_match("!^{$match}!", $request_match, $matches)) {
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Filter out non-public query vars
global $wp;
parse_str($query, $query_vars);
$query = array();
foreach ((array) $query_vars as $key => $value) {
if (in_array($key, $wp->public_query_vars)) {
$query[$key] = $value;
}
}
/************************************************************************
* ADDED: $GLOBALS['wp_post_types'] doesn't seem to have custom postypes
* Trying below to find posttypes in $rewrite rules
*************************************************************************/
// PostType Array
$custom_post_type = false;
$post_types = array();
foreach ($rewrite as $key => $value) {
if (preg_match('/post_type=([^&]+)/i', $value, $matched)) {
if (isset($matched[1]) && !in_array($matched[1], $post_types)) {
$post_types[] = $matched[1];
}
}
}
foreach ((array) $query_vars as $key => $value) {
if (in_array($key, $post_types)) {
$custom_post_type = true;
$query['post_type'] = $key;
$query['postname'] = $value;
}
}
// print_r($post_types);
/************************************************************************
示例12: url_to_custompostid
/**
* Custom post type rewrite function
* @param unknown $url
* @return number|Ambigous <string, NULL>
*/
function url_to_custompostid($url)
{
global $wp_rewrite, $wpdb;
$moreName = '';
$url = apply_filters('url_to_postid', $url);
$rewrite = $wp_rewrite->wp_rewrite_rules();
if (empty($rewrite)) {
return 0;
}
$url_split = explode('#', $url);
$url = $url_split[0];
$url_split = explode('?', $url);
$url = $url_split[0];
if (false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.')) {
$url = str_replace('://', '://www.', $url);
}
// Add 'www.' if it is absent and should be there
if (false === strpos(home_url(), '://www.')) {
$url = str_replace('://www.', '://', $url);
}
// Strip 'www.' if it is present and shouldn't be
if (!$wp_rewrite->using_index_permalinks()) {
$url = str_replace('index.php/', '', $url);
}
// Strip 'index.php/' if we're not using path info permalinks
if (false !== strpos($url, home_url())) {
$url = str_replace(home_url(), '', $url);
// Chop off http://domain.com
} else {
$home_path = parse_url(home_url());
// Chop off /path/to/blog
$home_path = isset($home_path['path']) ? $home_path['path'] : '';
$url = str_replace($home_path, '', $url);
}
$url = trim($url, '/');
// Trim leading and lagging slashes
$request = $url;
$request_match = $request;
// Look for matches.
foreach ((array) $rewrite as $match => $query) {
if (!empty($url) && $url != $request && strpos($match, $url) === 0) {
$request_match = $url . '/' . $request;
}
if (preg_match("!^{$match}!", $request_match, $matches)) {
if ($wp_rewrite->use_verbose_page_rules && preg_match('/pagename=\\$matches\\[([0-9]+)\\]/', $query, $varmatch)) {
if (!get_page_by_path($matches[$varmatch[1]])) {
continue;
}
}
$query = preg_replace('!^.+\\?!', '', $query);
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
global $wp;
global $wpdb;
parse_str($query, $query_vars);
$query = array();
foreach ((array) $query_vars as $key => $value) {
if (in_array($key, $wp->public_query_vars)) {
$query[$key] = $value;
}
}
if (!empty($query['videogallery'])) {
$moreName = $wpdb->get_var('SELECT ID FROM ' . $wpdb->prefix . 'posts WHERE post_name="' . $query['videogallery'] . '" LIMIT 1');
}
return $moreName;
}
}
return 0;
}
示例13: getPageObject
/**
* Parse which page we are on using URL
*/
public function getPageObject($pageUrl)
{
global $wp_rewrite;
// If post type, we are using url_to_postid function
$postId = url_to_postid($pageUrl);
if ($postId) {
$postType = get_post_type_object(get_post($postId)->post_type);
return array('value' => $postId, 'title' => get_the_title($postId), 'type' => get_post($postId)->post_type, 'label' => is_array($postType->labels) ? $postType->labels['name'] : $postType->labels->name);
}
$path = str_replace(get_site_url(), '', $pageUrl);
$path = trim($path, '/');
// If path is empty, then it is front page
if (empty($path)) {
return array('value' => get_option('page_on_front') ? get_option('page_on_front') : '', 'title' => '', 'type' => 'front_page', 'label' => __('Home Page'));
}
// Otherwise, we will try to match through rewrite or by query
$rewrite = $wp_rewrite->wp_rewrite_rules();
if (is_array($rewrite) && count($rewrite) > 0) {
foreach ($rewrite as $match => $query) {
if (preg_match("#^{$match}#", $path, $matches) || preg_match("#^{$match}#", urldecode($path), $matches)) {
$query = preg_replace("!^.*\\?!", '', $query);
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
parse_str($query, $query_vars);
break;
}
}
} else {
$query = preg_replace("!^.*\\?!", '', $path);
parse_str($query, $query_vars);
}
// Workaround for fail pagename rewrite match
if (isset($query_vars['pagename']) && strpos($query_vars['pagename'], '?') !== false) {
$query = preg_replace("!^.*\\?!", '', $query_vars['pagename']);
parse_str($query, $query_vars);
}
$querypost = new WP_Query($query_vars);
if ($querypost->is_date()) {
if ($querypost->query_vars['m']) {
$date = $querypost->query_vars['m'];
} else {
if ($querypost->is_day()) {
$date = $querypost->query_vars['year'] . zeroise($querypost->query_vars['monthnum'], 2) . zeroise($querypost->query_vars['day'], 2);
} else {
if ($querypost->is_month()) {
$date = $querypost->query_vars['year'] . zeroise($querypost->query_vars['monthnum'], 2);
} else {
if ($querypost->is_year()) {
$date = $querypost->query_vars['year'];
}
}
}
}
return array('value' => $date, 'title' => '', 'type' => 'archive', 'label' => __("Archive"));
} else {
if ($querypost->is_category() || $querypost->is_tag() || $querypost->is_tax()) {
$tax_query = $querypost->tax_query->queries;
$taxonomy = get_taxonomy($tax_query[0]['taxonomy']);
if ($tax_query[0]['field'] == 'term_id') {
$term_id = $tax_query[0]['terms'][0];
} else {
if ($tax_query[0]['field'] == 'slug') {
$term_id = get_term_by('slug', $tax_query[0]['terms'][0], $taxonomy->name)->term_id;
}
}
return array('value' => $term_id, 'title' => get_term($term_id, $taxonomy->name)->name, 'type' => $taxonomy->name, 'label' => is_array($taxonomy->labels->name) ? $taxonomy->labels['name'] : $taxonomy->labels->name);
} else {
if ($querypost->is_search()) {
return array('value' => $querypost->query_vars['s'], 'title' => '', 'type' => 'search', 'label' => __("Search"));
} else {
if ($querypost->is_home()) {
return array('value' => '', 'title' => '', 'type' => 'home', 'label' => __("Blog Home Page"));
}
}
}
}
}
示例14: _process_generic_text
//.........这里部分代码省略.........
$pathinfo = '';
$req_uri = str_replace($pathinfo, '', rawurldecode($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, '/');
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;
}
if (!$request) {
continue;
}
$request_match = $request;
$permalink_query_vars = array();
foreach ((array) $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.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Parse the query.
parse_str($query, $permalink_query_vars);
break;
}
}
$post_name = $category_name = $tax_name = false;
if (isset($permalink_query_vars['pagename'])) {
$get_page_by_path = new WPML_Get_Page_By_Path($wpdb, $sitepress);
$page_by_path = $get_page_by_path->get($permalink_query_vars['pagename'], $test_language);
$post_name = $permalink_query_vars['pagename'];
if (!empty($page_by_path->post_type)) {
$post_type = 'page';
} else {
$post_type = 'post';
}
} elseif (isset($permalink_query_vars['name'])) {
$post_name = $permalink_query_vars['name'];
$post_type = 'post';
} elseif (isset($permalink_query_vars['category_name'])) {
$category_name = $permalink_query_vars['category_name'];
} elseif (isset($permalink_query_vars['p'])) {
// case or /archives/%post_id
$post_data_prepared = $wpdb->prepare("SELECT post_type, post_name FROM {$wpdb->posts} WHERE id=%d", $permalink_query_vars['p']);
list($post_type, $post_name) = $wpdb->get_row($post_data_prepared, ARRAY_N);
} else {
if (empty($this->custom_post_query_vars) or empty($this->taxonomies_query_vars)) {
$this->init_query_vars();
}
foreach ($this->custom_post_query_vars as $query_vars_key => $query_vars_value) {
if (isset($permalink_query_vars[$query_vars_value])) {
$post_name = $permalink_query_vars[$query_vars_value];
$post_type = $query_vars_key;
示例15: convert_link2json
/**
* 链接转换
* src:外链,直接pass
* 内链,通过url路由规则,检测出具体参数
* type:1,外链
* 2,文章链接
* 3,菜单链接
* return :json api link
*/
function convert_link2json($src, $type = 0)
{
$dest = $src;
switch ($type) {
case 1:
return $dest;
break;
case 2:
case 3:
global $wp_rewrite;
$rewrite = $wp_rewrite->wp_rewrite_rules();
if ($rewrite != false) {
//自定义链接
$home_path = trim(parse_url(home_url(), PHP_URL_PATH), '/');
$req_uri = trim(parse_url($src, PHP_URL_PATH), '/');
$req_uri = preg_replace("|^{$home_path}|i", '', $req_uri);
$req_uri = trim($req_uri, '/');
$matches = null;
foreach ((array) $rewrite as $match => $query) {
if (preg_match("#^{$match}#", $req_uri, $matches) || preg_match("#^{$match}#", urldecode($req_uri), $matches)) {
break;
}
}
if ($matches) {
$query = preg_replace("!^.+\\?!", '', $query);
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
parse_str($query, $param);
$filter = array();
foreach ($param as $key => $value) {
$filter["filter[{$key}]"] = $value;
}
$filter["filter[only_one]"] = 1;
$dest = get_json_url_posts_list(0, $filter);
}
}
//默认链接
$match = "p=([0-9]{1,})";
if (preg_match("#{$match}#", $src, $matches)) {
$dest = get_json_url_posts_list($matches[1]);
}
break;
default:
}
return $dest;
}