Created
October 7, 2013 00:38
-
-
Save justintadlock/6860964 to your computer and use it in GitHub Desktop.
Hybrid Core framework attributes proposal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* HTML attribute functions and filters. The purposes of this is to provide a way for theme/plugin devs | |
* to hook into the attributes for specific HTML elements and create new or modify existing attributes. | |
* This is sort of like `body_class()`, `post_class()`, and `comment_class()` on steroids. Plus, it | |
* handles attributes for many more elements. The biggest benefit of using this is to provide richer | |
* microdata while being forward compatible with the ever-changing Web. Currently, the default microdata | |
* vocabulary supported is Schema.org. | |
*/ | |
/* Attributes for major structural elements. */ | |
add_filter( 'hybrid_attr_body', 'hybrid_attr_body', 5 ); | |
add_filter( 'hybrid_attr_header', 'hybrid_attr_header', 5 ); | |
add_filter( 'hybrid_attr_footer', 'hybrid_attr_footer', 5 ); | |
add_filter( 'hybrid_attr_content', 'hybrid_attr_content', 5 ); | |
add_filter( 'hybrid_attr_comment', 'hybrid_attr_comment', 5 ); | |
add_filter( 'hybrid_attr_post', 'hybrid_attr_post', 5 ); | |
add_filter( 'hybrid_attr_sidebar', 'hybrid_attr_sidebar', 5, 2 ); | |
add_filter( 'hybrid_attr_menu', 'hybrid_attr_menu', 5, 2 ); | |
/* Post-specific attributes. */ | |
add_filter( 'hybrid_attr_entry-title', 'hybrid_attr_entry_title', 5 ); | |
add_filter( 'hybrid_attr_entry-author', 'hybrid_attr_entry_author', 5 ); | |
add_filter( 'hybrid_attr_entry-published', 'hybrid_attr_entry_published', 5 ); | |
add_filter( 'hybrid_attr_entry-content', 'hybrid_attr_entry_content', 5 ); | |
add_filter( 'hybrid_attr_entry-summary', 'hybrid_attr_entry_summary', 5 ); | |
add_filter( 'hybrid_attr_entry-terms', 'hybrid_attr_entry_terms', 5, 2 ); | |
/* Comment specific attributes. */ | |
add_filter( 'hybrid_attr_comment-author', 'hybrid_attr_comment_author', 5 ); | |
add_filter( 'hybrid_attr_comment-published', 'hybrid_attr_comment_published', 5 ); | |
add_filter( 'hybrid_attr_comment-content', 'hybrid_attr_comment_content', 5 ); | |
/** | |
* @since 1.7.0 | |
* @access public | |
* @param string $slug The slug/ID of the element (e.g., 'sidebar'). | |
* @param string $context A specific context (e.g., 'primary'). | |
* @param array $attributes Custom attributes to pass in. | |
* @return void | |
*/ | |
function hybrid_attr( $slug, $context = '', $attributes = array() ) { | |
echo hybrid_get_attr( $slug, $context, $attributes ); | |
} | |
/** | |
* @since 1.7.0 | |
* @access public | |
* @param string $slug The slug/ID of the element (e.g., 'sidebar'). | |
* @param string $context A specific context (e.g., 'primary'). | |
* @param array $attributes Custom attributes to pass in. | |
* @return string | |
*/ | |
function hybrid_get_attr( $slug, $context = '', $attributes = array() ) { | |
$out = ''; | |
$attr = apply_filters( "hybrid_attr_{$slug}", $attributes, $context ); | |
if ( empty( $attr ) ) | |
return; | |
foreach ( $attr as $name => $value ) | |
$out .= !empty( $value ) ? sprintf( ' %s="%s"', esc_html( $name ), esc_attr( $value ) ) : esc_html( " {$name}" ); | |
return trim( $out ); | |
} | |
/* === Structural === */ | |
/** | |
* <body> element attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_body( $attr ) { | |
$attr['class'] = join( ' ', hybrid_get_body_class() ); | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/WebPage'; | |
return $attr; | |
} | |
/** | |
* Page <header> element attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_header( $attr ) { | |
$attr['id'] = 'header'; | |
$attr['role'] = 'banner'; | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/WPHeader'; | |
return $attr; | |
} | |
/** | |
* Page <footer> element attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_footer( $attr ) { | |
$attr['id'] = 'footer'; | |
$attr['role'] = 'contentinfo'; | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/WPFooter'; | |
return $attr; | |
} | |
/** | |
* Main content container of the page attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_content( $attr ) { | |
$attr['id'] = 'content'; | |
$attr['class'] = 'content'; | |
$attr['role'] = 'main'; | |
$attr['itemprop'] = 'mainContentOfPage'; | |
if ( is_singular( 'post' ) || is_home() || is_archive() ) { | |
$attr['itemscope'] = ''; | |
$attr['itemtype'] = 'http://schema.org/Blog'; | |
} | |
else if ( is_search() ) { | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/SearchResultsPage'; | |
} | |
return $attr; | |
} | |
/** | |
* Post <article> element attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_post( $attr ) { | |
$attr['id'] = 'post-' . get_the_ID(); | |
$attr['class'] = str_replace( 'hentry ', 'entry ', join( ' ', hybrid_get_post_class() ) ); | |
if ( 'post' === get_post_type() ) { | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/BlogPosting'; | |
$attr['itemprop'] = 'blogPost'; | |
// $attr['itemtype'] = 'http://schema.org/Article'; | |
} else { | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/CreativeWork'; | |
} | |
return $attr; | |
} | |
/** | |
* Sidebar attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @param string $context | |
* @return array | |
*/ | |
function hybrid_attr_sidebar( $attr, $context ) { | |
if ( !empty( $context ) ) | |
$attr['id'] = "sidebar-{$context}"; | |
$attr['class'] = 'sidebar'; | |
$attr['role'] = 'complementary'; | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/WPSideBar'; | |
return $attr; | |
} | |
/** | |
* Nav menu attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @param string $context | |
* @return array | |
*/ | |
function hybrid_attr_menu( $attr, $context ) { | |
if ( !empty( $context ) ) | |
$attr['id'] = "menu-{$context}"; | |
$attr['class'] = 'menu'; | |
$attr['role'] = 'navigation'; | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/SiteNavigationElement'; | |
return $attr; | |
} | |
/* === posts === */ | |
/** | |
* Post title attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_entry_title( $attr ) { | |
$attr['class'] = 'entry-title'; | |
$attr['itemprop'] = 'headline'; | |
return $attr; | |
} | |
/** | |
* Post author attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_entry_author( $attr ) { | |
$attr['class'] = 'entry-author author'; | |
$attr['itemprop'] = 'author'; | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/Person'; | |
return $attr; | |
} | |
/** | |
* Post time/published attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_entry_published( $attr ) { | |
$attr['class'] = 'published updated'; | |
$attr['datetime'] = get_the_time( 'Y-m-d\TH:i:sP' ); | |
$attr['title'] = get_the_time( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid' ) ); | |
return $attr; | |
} | |
/** | |
* Post content (not excerpt) attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_entry_content( $attr ) { | |
$attr['class'] = 'entry-content'; | |
$attr['itemprop'] = 'articleBody'; | |
return $attr; | |
} | |
/** | |
* Post summary/excerpt attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_entry_summary( $attr ) { | |
$attr['class'] = 'entry-summary'; | |
$attr['itemprop'] = 'description'; | |
return $attr; | |
} | |
/** | |
* Post terms (tags, categories, etc.) attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @param string $context | |
* @return array | |
*/ | |
function hybrid_attr_entry_terms( $attr, $context ) { | |
if ( !empty( $context ) ) { | |
$attr['class'] = $context; | |
if ( 'category' === $context ) | |
$attr['itemprop'] = 'articleSection'; | |
else if ( 'post_tag' === $context ) | |
$attr['itemprop'] = 'keywords'; | |
} | |
return $attr; | |
} | |
/* === Comment elements === */ | |
/** | |
* Comment wrapper attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_comment( $attr ) { | |
$attr['id'] = 'comment-' . get_comment_ID(); | |
$attr['class'] = join( ' ', hybrid_get_comment_class() ); | |
if ( in_array( get_comment_type(), array( '', 'comment' ) ) ) { | |
$attr['itemprop'] = 'comment'; | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/UserComments'; | |
} | |
return $attr; | |
} | |
/** | |
* Comment author attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_comment_author( $attr ) { | |
$attr['class'] = 'comment-author'; | |
$attr['itemprop'] = 'creator'; | |
$attr['itemscope'] = 'itemscope'; | |
$attr['itemtype'] = 'http://schema.org/Person'; | |
return $attr; | |
} | |
/** | |
* Comment time/published attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_comment_published( $attr ) { | |
$attr['class'] = 'published'; | |
$attr['datetime'] = get_comment_time( 'Y-m-d\TH:i:sP' ); | |
$attr['title'] = get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid' ) ); | |
$attr['itemprop'] = 'commentTime'; | |
return $attr; | |
} | |
/** | |
* Comment permalink attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_comment_permalink( $attr ) { | |
return $attr; | |
} | |
/** | |
* Comment content/text attributes. | |
* | |
* @since 1.7.0 | |
* @access public | |
* @param array $attr | |
* @return array | |
*/ | |
function hybrid_attr_comment_content( $attr ) { | |
$attr['class'] = 'comment-content'; | |
$attr['itemprop'] = 'commentText'; | |
return $attr; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment