Skip to content

Commit b8548f5

Browse files
committed
Simplified body class filter
Let core do the heavy lifting on this. This is a breaking change.
1 parent e912bb7 commit b8548f5

2 files changed

Lines changed: 13 additions & 139 deletions

File tree

includes/context.php

Lines changed: 12 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -10,159 +10,33 @@
1010
*/
1111

1212
/**
13-
* CareLib's main contextual function.
14-
*
15-
* This allows code to be used more than once without running hundreds of
16-
* conditional checks within the theme. It returns an array of contexts
17-
* based on what page a visitor is currently viewing on the site.
18-
*
19-
* @since 1.0.0
20-
* @access protected
21-
* @return array
22-
*/
23-
function _carelib_get_context() {
24-
$context = array();
25-
$object = get_queried_object();
26-
$object_id = get_queried_object_id();
27-
28-
if ( is_front_page() ) {
29-
$context[] = 'home';
30-
31-
if ( ! is_home() ) {
32-
$context[] = 'static-home';
33-
}
34-
} elseif ( is_home() ) {
35-
$context[] = 'blog';
36-
}
37-
38-
if ( carelib_is_plural() ) {
39-
$context[] = 'plural';
40-
41-
if ( is_search() ) {
42-
$context[] = 'search';
43-
} elseif ( is_archive() ) {
44-
$context[] = 'archive';
45-
46-
if ( is_post_type_archive() ) {
47-
$post_type = get_query_var( 'post_type' );
48-
49-
if ( is_array( $post_type ) ) {
50-
reset( $post_type );
51-
}
52-
53-
$context[] = "archive-{$post_type}";
54-
} elseif ( is_tax() || is_category() || is_tag() ) {
55-
$context[] = 'taxonomy';
56-
$context[] = "taxonomy-{$object->taxonomy}";
57-
$context[] = "taxonomy-{$object->taxonomy}-" . sanitize_html_class( $object->slug, $object->term_id );
58-
} elseif ( is_author() ) {
59-
$context[] = 'user';
60-
} elseif ( is_date() ) {
61-
$context[] = 'date';
62-
} elseif ( is_time() ) {
63-
$context[] = 'time';
64-
}
65-
}
66-
} else {
67-
if ( is_singular() ) {
68-
$context[] = 'singular';
69-
$context[] = "singular-{$object->post_type}";
70-
$context[] = "singular-{$object->post_type}-{$object_id}";
71-
} elseif ( is_404() ) {
72-
$context[] = 'error-404';
73-
}
74-
}
75-
76-
$context = (array) apply_filters( 'carelib_context', $context );
77-
78-
return array_map( 'esc_attr', array_unique( $context ) );
79-
}
80-
81-
/**
82-
* Filter the WordPress body class with a better set of default classes.
83-
*
84-
* The goal of this is to create classes which are more consistently handled
85-
* and are backwards compatible with the original body class functionality
86-
* that existed prior to WordPress core adopting this feature.
13+
* Filter the WordPress body class with extra default classes.
8714
*
8815
* @since 1.0.0
8916
* @access public
90-
* @param array $classes
91-
* @param string|array $class
17+
* @param array $classes
9218
* @return array
9319
*/
94-
function carelib_body_class_filter( $classes, $class ) {
95-
// WordPress class for uses when WordPress isn't always the only system on the site.
96-
$classes = array( 'wordpress' );
97-
98-
// Text direction.
99-
$classes[] = is_rtl() ? 'rtl' : 'ltr';
100-
101-
// Locale and language.
102-
$locale = get_locale();
103-
$lang = carelib_get_language( $locale );
104-
105-
if ( $locale !== $lang ) {
106-
$classes[] = $lang;
107-
}
108-
109-
$classes[] = strtolower( str_replace( '_', '-', $locale ) );
110-
111-
// Check if the current theme is a parent or child theme.
112-
$classes[] = is_child_theme() ? 'child-theme' : 'parent-theme';
113-
114-
// Multisite check adds the 'multisite' class and the blog ID.
115-
if ( is_multisite() ) {
116-
$classes[] = 'multisite';
117-
$classes[] = 'blog-' . get_current_blog_id();
20+
function carelib_body_class_filter( $classes ) {
21+
if ( is_front_page() && ! is_home() ) {
22+
$classes[] = 'static-home';
11823
}
11924

120-
// Is the current user logged in.
121-
$classes[] = is_user_logged_in() ? 'logged-in' : 'logged-out';
122-
123-
// WP admin bar.
124-
if ( is_admin_bar_showing() ) {
125-
$classes[] = 'admin-bar';
126-
}
127-
128-
$context = _carelib_get_context();
129-
130-
if ( in_array( 'singular', $context, true ) ) {
131-
132-
// Get the queried post object.
133-
$post = get_queried_object();
25+
if ( carelib_is_plural() ) {
26+
$classes[] = 'plural';
13427

135-
// Checks for custom template.
136-
$template = str_replace(
137-
array(
138-
"{$post->post_type}-template-",
139-
"{$post->post_type}-",
140-
),
141-
'',
142-
basename( get_page_template_slug( $post ), '.php' )
143-
);
144-
if ( $template ) {
145-
$classes[] = "{$post->post_type}-template-{$template}";
146-
} else {
147-
$classes[] = "{$post->post_type}-template-default";
28+
if ( is_tax() || is_category() || is_tag() ) {
29+
$classes[] = 'taxonomy';
14830
}
31+
} elseif ( is_singular() ) {
32+
$classes[] = 'singular';
14933
}
15034

151-
// Merge base contextual classes with $classes.
152-
$classes = array_merge( $classes, $context );
153-
154-
// Theme layouts.
15535
if ( carelib_has_layouts() ) {
15636
$classes[] = sanitize_html_class( 'layout-' . carelib_get_theme_layout() );
15737
}
15838

159-
// Input class.
160-
if ( ! empty( $class ) ) {
161-
$class = is_array( $class ) ? $class : preg_split( '#\s+#', $class );
162-
$classes = array_merge( $classes, $class );
163-
}
164-
165-
return array_map( 'esc_attr', $classes );
39+
return $classes;
16640
}
16741

16842
/**

includes/filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
*
274274
* @see carelib_body_class_filter
275275
*/
276-
add_filter( 'body_class', 'carelib_body_class_filter', 0, 2 );
276+
add_filter( 'body_class', 'carelib_body_class_filter', 0 );
277277

278278
/**
279279
* Callback defined in includes/context.php

0 commit comments

Comments
 (0)