PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/news/library/functions/context.php

https://bitbucket.org/lgorence/quickpress
PHP | 472 lines | 233 code | 97 blank | 142 comment | 76 complexity | 4ab78b0dda5189d04fa5295e2a508052 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Functions for making various theme elements context-aware. Controls things such as the smart
  4. * and logical body, post, and comment CSS classes as well as context-based action and filter hooks.
  5. * The functions also integrate with WordPress' implementations of body_class, post_class, and
  6. * comment_class, so your theme won't have any trouble with plugin integration.
  7. *
  8. * @package HybridCore
  9. * @subpackage Functions
  10. * @author Justin Tadlock <justin@justintadlock.com>
  11. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  12. * @link http://themehybrid.com/hybrid-core
  13. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  14. */
  15. /**
  16. * Hybrid's main contextual function. This allows code to be used more than once without running
  17. * hundreds of conditional checks within the theme. It returns an array of contexts based on what
  18. * page a visitor is currently viewing on the site. This function is useful for making dynamic/contextual
  19. * classes, action and filter hooks, and handling the templating system.
  20. *
  21. * Note that time and date can be tricky because any of the conditionals may be true on time-/date-
  22. * based archives depending on several factors. For example, one could load an archive for a specific
  23. * second during a specific minute within a specific hour on a specific day and so on.
  24. *
  25. * @since 0.7.0
  26. * @access public
  27. * @global $wp_query The current page's query object.
  28. * @global $hybrid The global Hybrid object.
  29. * @return array $hybrid->context Several contexts based on the current page.
  30. */
  31. function hybrid_get_context() {
  32. global $hybrid;
  33. /* If $hybrid->context has been set, don't run through the conditionals again. Just return the variable. */
  34. if ( isset( $hybrid->context ) )
  35. return $hybrid->context;
  36. /* Set some variables for use within the function. */
  37. $hybrid->context = array();
  38. $object = get_queried_object();
  39. $object_id = get_queried_object_id();
  40. /* Front page of the site. */
  41. if ( is_front_page() )
  42. $hybrid->context[] = 'home';
  43. /* Blog page. */
  44. if ( is_home() ) {
  45. $hybrid->context[] = 'blog';
  46. }
  47. /* Singular views. */
  48. elseif ( is_singular() ) {
  49. $hybrid->context[] = 'singular';
  50. $hybrid->context[] = "singular-{$object->post_type}";
  51. $hybrid->context[] = "singular-{$object->post_type}-{$object_id}";
  52. }
  53. /* Archive views. */
  54. elseif ( is_archive() ) {
  55. $hybrid->context[] = 'archive';
  56. /* Taxonomy archives. */
  57. if ( is_tax() || is_category() || is_tag() ) {
  58. $hybrid->context[] = 'taxonomy';
  59. $hybrid->context[] = "taxonomy-{$object->taxonomy}";
  60. $slug = ( ( 'post_format' == $object->taxonomy ) ? str_replace( 'post-format-', '', $object->slug ) : $object->slug );
  61. $hybrid->context[] = "taxonomy-{$object->taxonomy}-" . sanitize_html_class( $slug, $object->term_id );
  62. }
  63. /* Post type archives. */
  64. elseif ( is_post_type_archive() ) {
  65. $post_type = get_post_type_object( get_query_var( 'post_type' ) );
  66. $hybrid->context[] = "archive-{$post_type->name}";
  67. }
  68. /* User/author archives. */
  69. elseif ( is_author() ) {
  70. $hybrid->context[] = 'user';
  71. $hybrid->context[] = 'user-' . sanitize_html_class( get_the_author_meta( 'user_nicename', $object_id ), $object_id );
  72. }
  73. /* Time/Date archives. */
  74. else {
  75. if ( is_date() ) {
  76. $hybrid->context[] = 'date';
  77. if ( is_year() )
  78. $hybrid->context[] = 'year';
  79. if ( is_month() )
  80. $hybrid->context[] = 'month';
  81. if ( get_query_var( 'w' ) )
  82. $hybrid->context[] = 'week';
  83. if ( is_day() )
  84. $hybrid->context[] = 'day';
  85. }
  86. if ( is_time() ) {
  87. $hybrid->context[] = 'time';
  88. if ( get_query_var( 'hour' ) )
  89. $hybrid->context[] = 'hour';
  90. if ( get_query_var( 'minute' ) )
  91. $hybrid->context[] = 'minute';
  92. }
  93. }
  94. }
  95. /* Search results. */
  96. elseif ( is_search() ) {
  97. $hybrid->context[] = 'search';
  98. }
  99. /* Error 404 pages. */
  100. elseif ( is_404() ) {
  101. $hybrid->context[] = 'error-404';
  102. }
  103. return array_map( 'esc_attr', $hybrid->context );
  104. }
  105. /**
  106. * Creates a set of classes for each site entry upon display. Each entry is given the class of
  107. * 'hentry'. Posts are given category, tag, and author classes. Alternate post classes of odd,
  108. * even, and alt are added.
  109. *
  110. * @since 0.5.0
  111. * @access public
  112. * @global $post The current post's DB object.
  113. * @param string|array $class Additional classes for more control.
  114. * @return void
  115. */
  116. function hybrid_entry_class( $class = '', $post_id = null ) {
  117. static $post_alt;
  118. $post = get_post( $post_id );
  119. /* Make sure we have a real post first. */
  120. if ( !empty( $post ) ) {
  121. $post_id = $post->ID;
  122. /* Add hentry for microformats compliance, the post type, and post status. */
  123. $classes = array( 'hentry', $post->post_type, $post->post_status );
  124. /* Post alt class. */
  125. $classes[] = 'post-' . ++$post_alt;
  126. $classes[] = ( $post_alt % 2 ) ? 'odd' : 'even alt';
  127. /* Author class. */
  128. $classes[] = 'author-' . sanitize_html_class( get_the_author_meta( 'user_nicename' ), get_the_author_meta( 'ID' ) );
  129. /* Sticky class (only on home/blog page). */
  130. if ( is_home() && is_sticky() && !is_paged() )
  131. $classes[] = 'sticky';
  132. /* Password-protected posts. */
  133. if ( post_password_required() )
  134. $classes[] = 'protected';
  135. /* Has excerpt. */
  136. if ( post_type_supports( $post->post_type, 'excerpt' ) && has_excerpt() )
  137. $classes[] = 'has-excerpt';
  138. /* Has <!--more--> link. */
  139. if ( !is_singular() && false !== strpos( $post->post_content, '<!--more-->' ) )
  140. $classes[] = 'has-more-link';
  141. /* Post format. */
  142. if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) {
  143. $post_format = get_post_format( $post_id );
  144. $classes[] = ( ( empty( $post_format ) || is_wp_error( $post_format ) ) ? 'format-standard' : "format-{$post_format}" );
  145. }
  146. /* Add category and post tag terms as classes. */
  147. if ( 'post' == $post->post_type ) {
  148. foreach ( array( 'category', 'post_tag' ) as $tax ) {
  149. foreach ( (array)get_the_terms( $post->ID, $tax ) as $term ) {
  150. if ( !empty( $term->slug ) )
  151. $classes[] = $tax . '-' . sanitize_html_class( $term->slug, $term->term_id );
  152. }
  153. }
  154. }
  155. }
  156. /* If not a post. */
  157. else {
  158. $classes = array( 'hentry', 'error' );
  159. }
  160. /* User-created classes. */
  161. if ( !empty( $class ) ) {
  162. if ( !is_array( $class ) )
  163. $class = preg_split( '#\s+#', $class );
  164. $classes = array_merge( $classes, $class );
  165. }
  166. /* Apply the filters for WP's 'post_class'. */
  167. $classes = apply_filters( 'post_class', $classes, $class, $post_id );
  168. /* Join all the classes into one string and echo them. */
  169. $class = join( ' ', $classes );
  170. echo apply_atomic( 'entry_class', $class );
  171. }
  172. /**
  173. * Sets a class for each comment. Sets alt, odd/even, and author/user classes. Adds author, user,
  174. * and reader classes. Needs more work because WP, by default, assigns even/odd backwards
  175. * (Odd should come first, even second).
  176. *
  177. * @since 0.2.0
  178. * @access public
  179. * @global $wpdb WordPress DB access object.
  180. * @global $comment The current comment's DB object.
  181. * @return void
  182. */
  183. function hybrid_comment_class( $class = '' ) {
  184. global $post, $comment, $hybrid;
  185. /* Gets default WP comment classes. */
  186. $classes = get_comment_class( $class );
  187. /* Get the comment type. */
  188. $comment_type = get_comment_type();
  189. /* If the comment type is 'pingback' or 'trackback', add the 'ping' comment class. */
  190. if ( 'pingback' == $comment_type || 'trackback' == $comment_type )
  191. $classes[] = 'ping';
  192. /* User classes to match user role and user. */
  193. if ( $comment->user_id > 0 ) {
  194. /* Create new user object. */
  195. $user = new WP_User( $comment->user_id );
  196. /* Set a class with the user's role(s). */
  197. if ( is_array( $user->roles ) ) {
  198. foreach ( $user->roles as $role )
  199. $classes[] = sanitize_html_class( "role-{$role}" );
  200. }
  201. /* Set a class with the user's name. */
  202. $classes[] = sanitize_html_class( "user-{$user->user_nicename}", "user-{$user->ID}" );
  203. }
  204. /* If not a registered user */
  205. else {
  206. $classes[] = 'reader';
  207. }
  208. /* Comment by the entry/post author. */
  209. if ( $post = get_post( $post_id ) ) {
  210. if ( $comment->user_id === $post->post_author )
  211. $classes[] = 'entry-author';
  212. }
  213. /* Get comment types that are allowed to have an avatar. */
  214. $avatar_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
  215. /* If avatars are enabled and the comment types can display avatars, add the 'has-avatar' class. */
  216. if ( get_option( 'show_avatars' ) && in_array( $comment->comment_type, $avatar_comment_types ) )
  217. $classes[] = 'has-avatar';
  218. /* Make sure comment classes doesn't have any duplicates. */
  219. $classes = array_unique( $classes );
  220. /* Join all the classes into one string and echo them. */
  221. $class = join( ' ', $classes );
  222. echo apply_filters( "{$hybrid->prefix}_comment_class", $class );
  223. }
  224. /**
  225. * Provides classes for the <body> element depending on page context.
  226. *
  227. * @since 0.1.0
  228. * @access public
  229. * @uses $wp_query
  230. * @param string|array $class Additional classes for more control.
  231. * @return void
  232. */
  233. function hybrid_body_class( $class = '' ) {
  234. global $wp_query;
  235. /* Text direction (which direction does the text flow). */
  236. $classes = array( 'wordpress', get_bloginfo( 'text_direction' ), get_locale() );
  237. /* Check if the current theme is a parent or child theme. */
  238. $classes[] = ( is_child_theme() ? 'child-theme' : 'parent-theme' );
  239. /* Multisite check adds the 'multisite' class and the blog ID. */
  240. if ( is_multisite() ) {
  241. $classes[] = 'multisite';
  242. $classes[] = 'blog-' . get_current_blog_id();
  243. }
  244. /* Date classes. */
  245. $time = time() + ( get_option( 'gmt_offset' ) * 3600 );
  246. $classes[] = strtolower( gmdate( '\yY \mm \dd \hH l', $time ) );
  247. /* Is the current user logged in. */
  248. $classes[] = ( is_user_logged_in() ) ? 'logged-in' : 'logged-out';
  249. /* WP admin bar. */
  250. if ( is_admin_bar_showing() )
  251. $classes[] = 'admin-bar';
  252. /* Use the '.custom-background' class to integrate with the WP background feature. */
  253. if ( get_background_image() || get_background_color() )
  254. $classes[] = 'custom-background';
  255. /* Add the '.custom-header' class if the user is using a custom header. */
  256. if ( get_header_image() )
  257. $classes[] = 'custom-header';
  258. /* Merge base contextual classes with $classes. */
  259. $classes = array_merge( $classes, hybrid_get_context() );
  260. /* Singular post (post_type) classes. */
  261. if ( is_singular() ) {
  262. /* Get the queried post object. */
  263. $post = get_queried_object();
  264. /* Checks for custom template. */
  265. $template = str_replace( array ( "{$post->post_type}-template-", "{$post->post_type}-" ), '', basename( get_post_meta( get_queried_object_id(), "_wp_{$post->post_type}_template", true ), '.php' ) );
  266. if ( !empty( $template ) )
  267. $classes[] = "{$post->post_type}-template-{$template}";
  268. /* Post format. */
  269. if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) {
  270. $post_format = get_post_format( get_queried_object_id() );
  271. $classes[] = ( ( empty( $post_format ) || is_wp_error( $post_format ) ) ? "{$post->post_type}-format-standard" : "{$post->post_type}-format-{$post_format}" );
  272. }
  273. /* Attachment mime types. */
  274. if ( is_attachment() ) {
  275. foreach ( explode( '/', get_post_mime_type() ) as $type )
  276. $classes[] = "attachment-{$type}";
  277. }
  278. }
  279. /* Paged views. */
  280. if ( ( ( $page = $wp_query->get( 'paged' ) ) || ( $page = $wp_query->get( 'page' ) ) ) && $page > 1 )
  281. $classes[] = 'paged paged-' . intval( $page );
  282. /* Input class. */
  283. if ( !empty( $class ) ) {
  284. if ( !is_array( $class ) )
  285. $class = preg_split( '#\s+#', $class );
  286. $classes = array_merge( $classes, $class );
  287. }
  288. /* Apply the filters for WP's 'body_class'. */
  289. $classes = apply_filters( 'body_class', $classes, $class );
  290. /* Join all the classes into one string. */
  291. $class = join( ' ', $classes );
  292. /* Print the body class. */
  293. echo apply_atomic( 'body_class', $class );
  294. }
  295. /**
  296. * Function for handling what the browser/search engine title should be. Attempts to handle every
  297. * possible situation WordPress throws at it for the best optimization.
  298. *
  299. * @since 0.1.0
  300. * @access public
  301. * @global $wp_query
  302. * @return void
  303. */
  304. function hybrid_document_title() {
  305. global $wp_query;
  306. /* Set up some default variables. */
  307. $doctitle = '';
  308. $separator = ':';
  309. /* If viewing the front page and posts page of the site. */
  310. if ( is_front_page() && is_home() )
  311. $doctitle = get_bloginfo( 'name' ) . $separator . ' ' . get_bloginfo( 'description' );
  312. /* If viewing the posts page or a singular post. */
  313. elseif ( is_home() || is_singular() ) {
  314. $doctitle = get_post_meta( get_queried_object_id(), 'Title', true );
  315. if ( empty( $doctitle ) && is_front_page() )
  316. $doctitle = get_bloginfo( 'name' ) . $separator . ' ' . get_bloginfo( 'description' );
  317. elseif ( empty( $doctitle ) )
  318. $doctitle = single_post_title( '', false );
  319. }
  320. /* If viewing any type of archive page. */
  321. elseif ( is_archive() ) {
  322. /* If viewing a taxonomy term archive. */
  323. if ( is_category() || is_tag() || is_tax() ) {
  324. $doctitle = single_term_title( '', false );
  325. }
  326. /* If viewing a post type archive. */
  327. elseif ( is_post_type_archive() ) {
  328. $post_type = get_post_type_object( get_query_var( 'post_type' ) );
  329. $doctitle = $post_type->labels->name;
  330. }
  331. /* If viewing an author/user archive. */
  332. elseif ( is_author() ) {
  333. $doctitle = get_user_meta( get_query_var( 'author' ), 'Title', true );
  334. if ( empty( $doctitle ) )
  335. $doctitle = get_the_author_meta( 'display_name', get_query_var( 'author' ) );
  336. }
  337. /* If viewing a date-/time-based archive. */
  338. elseif ( is_date () ) {
  339. if ( get_query_var( 'minute' ) && get_query_var( 'hour' ) )
  340. $doctitle = sprintf( __( 'Archive for %1$s', 'hybrid-core' ), get_the_time( __( 'g:i a', 'hybrid-core' ) ) );
  341. elseif ( get_query_var( 'minute' ) )
  342. $doctitle = sprintf( __( 'Archive for minute %1$s', 'hybrid-core' ), get_the_time( __( 'i', 'hybrid-core' ) ) );
  343. elseif ( get_query_var( 'hour' ) )
  344. $doctitle = sprintf( __( 'Archive for %1$s', 'hybrid-core' ), get_the_time( __( 'g a', 'hybrid-core' ) ) );
  345. elseif ( is_day() )
  346. $doctitle = sprintf( __( 'Archive for %1$s', 'hybrid-core' ), get_the_time( __( 'F jS, Y', 'hybrid-core' ) ) );
  347. elseif ( get_query_var( 'w' ) )
  348. $doctitle = sprintf( __( 'Archive for week %1$s of %2$s', 'hybrid-core' ), get_the_time( __( 'W', 'hybrid-core' ) ), get_the_time( __( 'Y', 'hybrid-core' ) ) );
  349. elseif ( is_month() )
  350. $doctitle = sprintf( __( 'Archive for %1$s', 'hybrid-core' ), single_month_title( ' ', false) );
  351. elseif ( is_year() )
  352. $doctitle = sprintf( __( 'Archive for %1$s', 'hybrid-core' ), get_the_time( __( 'Y', 'hybrid-core' ) ) );
  353. }
  354. /* For any other archives. */
  355. else {
  356. $doctitle = __( 'Archives', 'hybrid-core' );
  357. }
  358. }
  359. /* If viewing a search results page. */
  360. elseif ( is_search() )
  361. $doctitle = sprintf( __( 'Search results for &quot;%1$s&quot;', 'hybrid-core' ), esc_attr( get_search_query() ) );
  362. /* If viewing a 404 not found page. */
  363. elseif ( is_404() )
  364. $doctitle = __( '404 Not Found', 'hybrid-core' );
  365. /* If the current page is a paged page. */
  366. if ( ( ( $page = $wp_query->get( 'paged' ) ) || ( $page = $wp_query->get( 'page' ) ) ) && $page > 1 )
  367. $doctitle = sprintf( __( '%1$s Page %2$s', 'hybrid-core' ), $doctitle . $separator, number_format_i18n( $page ) );
  368. /* Apply the wp_title filters so we're compatible with plugins. */
  369. $doctitle = apply_filters( 'wp_title', $doctitle, $separator, '' );
  370. /* Trim separator + space from beginning and end in case a plugin adds it. */
  371. $doctitle = trim( $doctitle, "{$separator} " );
  372. /* Print the title to the screen. */
  373. echo apply_atomic( 'document_title', esc_attr( $doctitle ) );
  374. }
  375. ?>