/**
* Omega's main contextual function. This allows code to be used more than once without running
* hundreds of conditional checks within the theme. It returns an array of contexts based on what
* page a visitor is currently viewing on the site. This function is useful for making dynamic/contextual
* classes, action and filter hooks, and handling the templating system.
*
* Note that time and date can be tricky because any of the conditionals may be true on time-/date-
* based archives depending on several factors. For example, one could load an archive for a specific
* second during a specific minute within a specific hour on a specific day and so on.
*
* @since 0.7.0
* @access public
* @global $wp_query The current page's query object.
* @global $omega The global Omega object.
* @return array $omega->context Several contexts based on the current page.
*/
function omega_get_context()
{
global $omega;
/* If $omega->context has been set, don't run through the conditionals again. Just return the variable. */
if (isset($omega->context)) {
return apply_filters('omega_context', $omega->context);
}
/* Set some variables for use within the function. */
$omega->context = array();
$object = get_queried_object();
$object_id = get_queried_object_id();
/* Front page of the site. */
if (is_front_page()) {
$omega->context[] = 'home';
}
/* Blog page. */
if (is_home()) {
$omega->context[] = 'blog';
} elseif (is_singular()) {
$omega->context[] = 'singular';
$omega->context[] = "singular-{$object->post_type}";
$omega->context[] = "singular-{$object->post_type}-{$object_id}";
} elseif (is_archive()) {
$omega->context[] = 'archive';
/* Post type archives. */
if (is_post_type_archive()) {
$post_type = get_post_type_object(get_query_var('post_type'));
$omega->context[] = "archive-{$post_type->name}";
}
/* Taxonomy archives. */
if (is_tax() || is_category() || is_tag()) {
$omega->context[] = 'taxonomy';
$omega->context[] = "taxonomy-{$object->taxonomy}";
$slug = 'post_format' == $object->taxonomy ? str_replace('post-format-', '', $object->slug) : $object->slug;
$omega->context[] = "taxonomy-{$object->taxonomy}-" . sanitize_html_class($slug, $object->term_id);
}
/* User/author archives. */
if (is_author()) {
$user_id = get_query_var('author');
$omega->context[] = 'user';
$omega->context[] = 'user-' . sanitize_html_class(get_the_author_meta('user_nicename', $user_id), $user_id);
}
/* Date archives. */
if (is_date()) {
$omega->context[] = 'date';
if (is_year()) {
$omega->context[] = 'year';
}
if (is_month()) {
$omega->context[] = 'month';
}
if (get_query_var('w')) {
$omega->context[] = 'week';
}
if (is_day()) {
$omega->context[] = 'day';
}
}
/* Time archives. */
if (is_time()) {
$omega->context[] = 'time';
if (get_query_var('hour')) {
$omega->context[] = 'hour';
}
if (get_query_var('minute')) {
$omega->context[] = 'minute';
}
}
} elseif (is_search()) {
$omega->context[] = 'search';
} elseif (is_404()) {
$omega->context[] = 'error-404';
}
return array_map('esc_attr', apply_filters('omega_context', array_unique($omega->context)));
}
/**
* Hybrid's main contextual function. This allows code to be used more than once without running
* hundreds of conditional checks within the theme. It returns an array of contexts based on what
* page a visitor is currently viewing on the site. This function is useful for making dynamic/contextual
* classes, action and filter hooks, and handling the templating system.
*
* Note that time and date can be tricky because any of the conditionals may be true on time-/date-
* based archives depending on several factors. For example, one could load an archive for a specific
* second during a specific minute within a specific hour on a specific day and so on.
*
* @since 0.7.0
* @global $wp_query The current page's query object.
* @global $hybrid The global Hybrid object.
* @return array $hybrid->context Several contexts based on the current page.
*/
function hybrid_get_context()
{
global $hybrid;
/* If $hybrid->context has been set, don't run through the conditionals again. Just return the variable. */
if (isset($hybrid->context)) {
return $hybrid->context;
}
/* Set some variables for use within the function. */
$hybrid->context = array();
$object = get_queried_object();
$object_id = get_queried_object_id();
/* Front page of the site. */
if (is_front_page()) {
$hybrid->context[] = 'home';
}
/* Blog page. */
if (is_home()) {
$hybrid->context[] = 'blog';
} elseif (is_singular()) {
$hybrid->context[] = 'singular';
$hybrid->context[] = "singular-{$object->post_type}";
$hybrid->context[] = "singular-{$object->post_type}-{$object_id}";
} elseif (is_archive()) {
$hybrid->context[] = 'archive';
/* Taxonomy archives. */
if (is_tax() || is_category() || is_tag()) {
$hybrid->context[] = 'taxonomy';
$hybrid->context[] = "taxonomy-{$object->taxonomy}";
$hybrid->context[] = "taxonomy-{$object->taxonomy}-" . sanitize_html_class($object->slug, $object->term_id);
} elseif (is_post_type_archive()) {
$post_type = get_post_type_object(get_query_var('post_type'));
$hybrid->context[] = "archive-{$post_type->name}";
} elseif (is_author()) {
$hybrid->context[] = 'user';
$hybrid->context[] = 'user-' . sanitize_html_class(get_the_author_meta('user_nicename', $object_id), $object_id);
} else {
if (is_date()) {
$hybrid->context[] = 'date';
if (is_year()) {
$hybrid->context[] = 'year';
}
if (is_month()) {
$hybrid->context[] = 'month';
}
if (get_query_var('w')) {
$hybrid->context[] = 'week';
}
if (is_day()) {
$hybrid->context[] = 'day';
}
}
if (is_time()) {
$hybrid->context[] = 'time';
if (get_query_var('hour')) {
$hybrid->context[] = 'hour';
}
if (get_query_var('minute')) {
$hybrid->context[] = 'minute';
}
}
}
} elseif (is_search()) {
$hybrid->context[] = 'search';
} elseif (is_404()) {
$hybrid->context[] = 'error-404';
}
return array_map('esc_attr', $hybrid->context);
}
/**
* Hybrid's main contextual function. This allows code to be used more than once without running
* hundreds of conditional checks within the theme. It returns an array of contexts based on what
* page a visitor is currently viewing on the site. This function is useful for making dynamic/contextual
* classes, action and filter hooks, and handling the templating system.
*
* Note that time and date can be tricky because any of the conditionals may be true on time-/date-
* based archives depending on several factors. For example, one could load an archive for a specific
* second during a specific minute within a specific hour on a specific day and so on.
*
* @since 0.7.0
* @global $wp_query The current page's query object.
* @global $hybrid The global Hybrid object.
* @return array $hybrid->context Several contexts based on the current page.
*/
function hybrid_get_context() {
global $wp_query, $hybrid;
/* If $hybrid->context has been set, don't run through the conditionals again. Just return the variable. */
if ( isset( $hybrid->context ) )
return $hybrid->context;
$hybrid->context = array();
/* Front page of the site. */
if ( is_front_page() )
$hybrid->context[] = 'home';
/* Blog page. */
if ( is_home() ) {
$hybrid->context[] = 'blog';
}
/* Singular views. */
elseif ( is_singular() ) {
$hybrid->context[] = 'singular';
$hybrid->context[] = "singular-{$wp_query->post->post_type}";
$hybrid->context[] = "singular-{$wp_query->post->post_type}-{$wp_query->post->ID}";
}
/* Archive views. */
elseif ( is_archive() ) {
$hybrid->context[] = 'archive';
/* Taxonomy archives. */
if ( is_tax() || is_category() || is_tag() ) {
$term = $wp_query->get_queried_object();
$hybrid->context[] = 'taxonomy';
$hybrid->context[] = "taxonomy-{$term->taxonomy}";
$hybrid->context[] = "taxonomy-{$term->taxonomy}-" . sanitize_html_class( $term->slug, $term->term_id );
}
/* Post type archives. */
elseif ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
$post_type = get_post_type_object( get_query_var( 'post_type' ) );
$hybrid->context[] = "archive-{$post_type->name}";
}
/* User/author archives. */
elseif ( is_author() ) {
$hybrid->context[] = 'user';
$hybrid->context[] = 'user-' . sanitize_html_class( get_the_author_meta( 'user_nicename', get_query_var( 'author' ) ), $wp_query->get_queried_object_id() );
}
/* Time/Date archives. */
else {
if ( is_date() ) {
$hybrid->context[] = 'date';
if ( is_year() )
$hybrid->context[] = 'year';
if ( is_month() )
$hybrid->context[] = 'month';
if ( get_query_var( 'w' ) )
$hybrid->context[] = 'week';
if ( is_day() )
$hybrid->context[] = 'day';
}
if ( is_time() ) {
$hybrid->context[] = 'time';
if ( get_query_var( 'hour' ) )
$hybrid->context[] = 'hour';
if ( get_query_var( 'minute' ) )
$hybrid->context[] = 'minute';
}
}
}
/* Search results. */
elseif ( is_search() ) {
$hybrid->context[] = 'search';
}
/* Error 404 pages. */
elseif ( is_404() ) {
$hybrid->context[] = 'error-404';
}
return array_map( 'esc_attr', $hybrid->context );
}
/**
* Checks if the current page can be accessed by the specified rules
*
* @since 1.0.0
*
* @param array $rules List of allowed pages.
* @return bool
*/
protected function check_current_page($rules)
{
$result = false;
foreach ($rules as $key => $active) {
if (!$active) {
continue;
}
/*
* The item order is critical, in case a page has multiple flags
* like "Front" and "Home" and "Archive".
* In this example "Archive" might be denied but "Front" allowed,
* so we have to define a hierarchy which flag is actually used.
*/
switch ($key) {
case 'front':
$result = is_front_page();
break;
case 'home':
$result = is_home();
break;
case 'notfound':
$result = is_404();
break;
case 'search':
$result = is_search();
break;
case 'attachment':
$result = is_attachment();
break;
case 'single':
$result = is_singular();
break;
case 'archive':
$result = is_archive();
break;
case 'author':
$result = is_author();
break;
case 'date':
$result = is_date();
break;
case 'year':
$result = is_year();
break;
case 'month':
$result = is_month();
break;
case 'day':
$result = is_day();
break;
case 'time':
$result = is_time();
break;
}
if ($result) {
$this->matched_type = $key;
break;
}
}
return apply_filters('ms_rule_special_model_check_current_page', $result, $rules);
}
请发表评论