Add WP_Query_State class. Introduce is_single(), is_archive(), and friends.
git-svn-id: http://svn.automattic.com/wordpress/trunk@1449 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
83844c9b8b
commit
8b44ee9d93
|
@ -147,6 +147,8 @@ foreach (array_merge($wpvarstoreset, $more_wpvars) as $wpvar) {
|
|||
|
||||
$query_string = apply_filters('query_string', $query_string);
|
||||
|
||||
$wp_query_state->parse_query($query_string);
|
||||
|
||||
// Update some caches.
|
||||
update_category_cache();
|
||||
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
<?php
|
||||
|
||||
class WP_Query_State {
|
||||
var $single = false;
|
||||
var $archive = false;
|
||||
var $date = false;
|
||||
var $author = false;
|
||||
var $category = false;
|
||||
var $search = false;
|
||||
var $feed = false;
|
||||
var $home = false;
|
||||
|
||||
function init () {
|
||||
$this->single = false;
|
||||
$this->archive = false;
|
||||
$this->date = false;
|
||||
$this->author = false;
|
||||
$this->category = false;
|
||||
$this->search = false;
|
||||
$this->feed = false;
|
||||
$this->home = false;
|
||||
}
|
||||
|
||||
function parse_query ($query) {
|
||||
parse_str($query);
|
||||
$this->init();
|
||||
|
||||
if ('' != $m) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $hour) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $minute) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $second) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $year) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $monthnum) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $day) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $w) {
|
||||
$this->date = true;
|
||||
}
|
||||
|
||||
if ('' != $name) {
|
||||
$this->single = true;
|
||||
}
|
||||
|
||||
if (($p != '') && ($p != 'all')) {
|
||||
$this->single = true;
|
||||
}
|
||||
|
||||
if (!empty($s)) {
|
||||
$this->search = true;
|
||||
}
|
||||
|
||||
if ((empty($cat)) || ($cat == 'all') || ($cat == '0') ||
|
||||
// Bypass cat checks if fetching specific posts
|
||||
(
|
||||
intval($year) || intval($monthnum) || intval($day) || intval($w) ||
|
||||
intval($p) || !empty($name) || !empty($s)
|
||||
)
|
||||
) {
|
||||
$this->category = false;
|
||||
} else {
|
||||
if (stristr($cat,'-')) {
|
||||
$this->category = false;
|
||||
} else {
|
||||
$this->category = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ('' != $category_name) {
|
||||
$this->category = true;
|
||||
}
|
||||
|
||||
if ((empty($author)) || ($author == 'all') || ($author == '0')) {
|
||||
$this->author = false;
|
||||
} else {
|
||||
$this->author = true;
|
||||
}
|
||||
|
||||
if ('' != $author_name) {
|
||||
$this->author = true;
|
||||
}
|
||||
|
||||
if ('' != $feed) {
|
||||
$this->feed = true;
|
||||
}
|
||||
|
||||
if ( ($this->date || $this->author || $this->category)
|
||||
&& (! $this->single)) {
|
||||
$this->archive = true;
|
||||
}
|
||||
|
||||
if ( ! ($this->archive || $this->single || $this->search || $this->feed)) {
|
||||
$this->home = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function WP_Query_State ($query = '') {
|
||||
if (! empty($query)) {
|
||||
$this->parse_query($query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Make a global instance.
|
||||
if (! isset($wp_query_state)) {
|
||||
$wp_query_state = new WP_Query_State();
|
||||
}
|
||||
|
||||
?>
|
|
@ -1805,4 +1805,52 @@ function wp_head() {
|
|||
do_action('wp_head', '');
|
||||
}
|
||||
|
||||
function is_single () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->single;
|
||||
}
|
||||
|
||||
function is_archive () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->archive;
|
||||
}
|
||||
|
||||
function is_date () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->date;
|
||||
}
|
||||
|
||||
function is_author () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->author;
|
||||
}
|
||||
|
||||
function is_category () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->category;
|
||||
}
|
||||
|
||||
function is_search () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->search;
|
||||
}
|
||||
|
||||
function is_feed () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->search;
|
||||
}
|
||||
|
||||
function is_home () {
|
||||
global $wp_query_state;
|
||||
|
||||
return $wp_query_state->home;
|
||||
}
|
||||
|
||||
?>
|
|
@ -47,6 +47,7 @@ if ( !(phpversion() >= '4.1') )
|
|||
die( 'Your server is running PHP version ' . phpversion() . ' but WordPress requires at least 4.1' );
|
||||
|
||||
|
||||
require (ABSPATH . WPINC . '/classes.php');
|
||||
require (ABSPATH . WPINC . '/functions.php');
|
||||
timer_start();
|
||||
require (ABSPATH . WPINC . '/functions-formatting.php');
|
||||
|
|
Loading…
Reference in New Issue