PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/extensions/dynamic-classes.php

https://github.com/middlesister/Thematic
PHP | 638 lines | 391 code | 105 blank | 142 comment | 110 complexity | 61b0c3b04baa436ac224b75e5c4b0d9f MD5 | raw file
  1. <?php
  2. /**
  3. * Dynamic Classes
  4. *
  5. * @package ThematicCoreLibrary
  6. * @subpackage DynamicClasses
  7. */
  8. if ( function_exists( 'childtheme_override_body_class' ) ) {
  9. /**
  10. * @ignore
  11. */
  12. function thematic_body_class() {
  13. childtheme_override_body_class();
  14. }
  15. } else {
  16. /**
  17. * Generates semantic classes for BODY element
  18. *
  19. * @param array $classes body classes
  20. */
  21. function thematic_body_class( $classes ) {
  22. /**
  23. * Filter to control the theme layout
  24. *
  25. * Accepts any string that is part of thematic_available_theme_layouts(). Note that
  26. * the filter overrides the layout defined in the Theme Customizer. Any invalid
  27. * layout string will be ignored and the theme's default layout will be used.
  28. *
  29. * @see thematic_available_theme_layouts()
  30. *
  31. * @since 2.0
  32. *
  33. * @param string $current_layout
  34. */
  35. $current_layout = apply_filters( 'thematic_current_theme_layout', thematic_get_theme_opt( 'layout' ) );
  36. if ( is_page_template( 'template-page-fullwidth.php' ) ) {
  37. $classes[] = 'full-width';
  38. } elseif( in_array( $current_layout, thematic_available_layout_slugs() ) ) {
  39. $classes[] = $current_layout;
  40. } else {
  41. $classes[] = thematic_default_theme_layout();
  42. }
  43. if( thematic_is_legacy_xhtml() ) {
  44. $classes[] = 'thematic-xhtml';
  45. }
  46. /**
  47. * Filter the body classes
  48. *
  49. * @param array $classes
  50. */
  51. return apply_filters( 'thematic_body_class', $classes );
  52. }
  53. }
  54. if ( function_exists( 'childtheme_override_legacy_body_class' ) ) {
  55. /**
  56. * @ignore
  57. */
  58. function thematic_legacy_body_class() {
  59. childtheme_override_legacy_body_class();
  60. }
  61. } else {
  62. /**
  63. * Generates semantic classes for BODY element
  64. *
  65. * @param array $c body classes
  66. */
  67. function thematic_legacy_body_class( $c ) {
  68. global $wp_query, $current_user, $blog_id, $post, $taxonomy;
  69. if ( apply_filters('thematic_show_bc_wordpress', TRUE ) ) {
  70. // It's surely a WordPress blog, right?
  71. $c[] = 'wordpress';
  72. }
  73. if ( apply_filters( 'thematic_show_bc_blogid', TRUE) ) {
  74. // Applies the blog id to BODY element .. blog-id1 for WordPress < 3.0
  75. $c[] = 'blogid-' . $blog_id;
  76. }
  77. if ( apply_filters( 'thematic_show_bc_datetime', TRUE) ) {
  78. // Applies the time- and date-based classes (below) to BODY element
  79. thematic_date_classes( time(), $c );
  80. }
  81. if ( apply_filters( 'thematic_show_bc_contenttype', TRUE ) ) {
  82. // Generic semantic classes for what type of content is displayed
  83. is_front_page() ? $c[] = 'home' : null; // For the front page, if set
  84. is_home() ? $c[] = 'blog' : null; // For the blog posts page, if set
  85. is_archive() ? $c[] = 'archive' : null;
  86. is_date() ? $c[] = 'date' : null;
  87. is_search() ? $c[] = 'search' : null;
  88. is_paged() ? $c[] = 'paged' : null;
  89. is_attachment() ? $c[] = 'attachment' : null;
  90. is_404() ? $c[] = 'four04' : null; // CSS does not allow a digit as first character
  91. }
  92. if ( apply_filters( 'thematic_show_bc_singular', TRUE) ) {
  93. // Special classes for BODY element when a singular post
  94. if ( is_singular() ) {
  95. $c[] = 'singular';
  96. } else {
  97. $c[] = 'not-singular';
  98. }
  99. }
  100. if ( is_single() && apply_filters( 'thematic_show_bc_singlepost', TRUE ) ) {
  101. $postID = $wp_query->post->ID;
  102. the_post();
  103. // Adds post slug class, prefixed by 'slug-'
  104. $c[] = 'slug-' . $wp_query->post->post_name;
  105. // Adds 'single' class and class with the post ID
  106. $c[] = 'single';
  107. $c[] = 'postid-' . $postID;
  108. // Adds classes for the month, day, and hour when the post was published
  109. if ( isset( $wp_query->post->post_date ) ) {
  110. thematic_date_classes( mysql2date( 'U', $wp_query->post->post_date ), $c, 's-' );
  111. }
  112. // Special classes for BODY element when a single post
  113. // Adds category classes for each category on single posts
  114. if ( $cats = get_the_category() ) {
  115. foreach ( $cats as $cat ) {
  116. $c[] = 's-category-' . $cat->slug;
  117. }
  118. }
  119. // Adds tag classes for each tag on single posts
  120. if ( $tags = get_the_tags() ) {
  121. foreach ( $tags as $tag ) {
  122. $c[] = 's-tag-' . $tag->slug;
  123. }
  124. }
  125. // Adds taxonomy classes for each term on single posts
  126. $single_post_type = get_post_type_object( get_post_type( $post->ID ) );
  127. // Check for post types without taxonomy inclusion
  128. if ( isset( $single_post_type->taxonomy ) ) {
  129. if ( $tax = get_the_terms( $post->ID, get_post_taxonomies() ) ) {
  130. foreach ( $tax as $term ) {
  131. // Remove tags and categories from results
  132. if ( $term->taxonomy != 'post_tag' ) {
  133. if ( $term->taxonomy != 'category' ) {
  134. $c[] = 's-tax-' . $term->taxonomy;
  135. $c[] = 's-' . $term->taxonomy . '-' . $term->slug;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. // Adds MIME-specific classes for attachments
  142. if ( is_attachment() ) {
  143. $mime_type = get_post_mime_type();
  144. $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
  145. $c[] = 'attachmentid-' . $postID . ' attachment-' . str_replace( $mime_prefix, "", "$mime_type" );
  146. }
  147. // Adds author class for the post author
  148. $c[] = 's-author-' . sanitize_title_with_dashes( strtolower( get_the_author_meta( 'user_nicename', $post->post_author ) ) );
  149. rewind_posts();
  150. // For posts with excerpts
  151. if ( has_excerpt() ) {
  152. $c[] = 's-has-excerpt';
  153. }
  154. // For posts with comments open or closed
  155. if ( comments_open() ) {
  156. $c[] = 's-comments-open';
  157. } else {
  158. $c[] = 's-comments-closed';
  159. }
  160. // For posts with pings open or closed
  161. if ( pings_open() ) {
  162. $c[] = 's-pings-open';
  163. } else {
  164. $c[] = 's-pings-closed';
  165. }
  166. // For password-protected posts
  167. if ( $post->post_password ) {
  168. $c[] = 's-protected';
  169. }
  170. // For sticky posts
  171. if ( is_sticky() ) {
  172. $c[] = 's-sticky';
  173. }
  174. }
  175. // Author name classes for BODY on author archives
  176. elseif ( is_author() && apply_filters( 'thematic_show_bc_authorarchives', TRUE ) ) {
  177. $author = $wp_query->get_queried_object();
  178. $c[] = 'author';
  179. $c[] = 'author-' . $author->user_nicename;
  180. }
  181. // Category name classes for BODY on category archvies
  182. elseif ( is_category() && apply_filters( 'thematic_show_bc_categoryarchives', TRUE ) ) {
  183. $cat = $wp_query->get_queried_object();
  184. $c[] = 'category';
  185. $c[] = 'category-' . $cat->slug;
  186. }
  187. // Tag name classes for BODY on tag archives
  188. elseif ( is_tag() && apply_filters('thematic_show_bc_tagarchives', TRUE ) ) {
  189. $tags = $wp_query->get_queried_object();
  190. $c[] = 'tag';
  191. $c[] = 'tag-' . $tags->slug;
  192. }
  193. // Taxonomy name classes for BODY on tag archives
  194. elseif ( is_tax() && apply_filters( 'thematic_show_bc_taxonomyarchives', TRUE) ) {
  195. $c[] = 'taxonomy';
  196. $c[] = 'tax-' . $taxonomy;
  197. $c[] = $taxonomy . '-' . strtolower(thematic_get_term_name());
  198. }
  199. // Page author for BODY on 'pages'
  200. elseif ( is_page() && apply_filters( 'thematic_show_bc_pages', TRUE ) ) {
  201. $pageID = $wp_query->post->ID;
  202. $page_children = wp_list_pages( "child_of=$pageID&echo=0" );
  203. the_post();
  204. // Adds post slug class, prefixed by 'slug-'
  205. $c[] = 'slug-' . $wp_query->post->post_name;
  206. $c[] = 'page';
  207. $c[] = 'pageid-' . $pageID;
  208. $c[] = 'page-author-' . sanitize_title_with_dashes( strtolower( get_the_author_meta( 'user_nicename', $post->post_author) ) );
  209. // Checks to see if the page has children and/or is a child page; props to Adam
  210. if ( $page_children ) {
  211. $c[] = 'page-parent';
  212. }
  213. if ( $wp_query->post->post_parent ) {
  214. $c[] = 'page-child parent-pageid-' . $wp_query->post->post_parent;
  215. }
  216. // For pages with excerpts
  217. if ( has_excerpt() ) {
  218. $c[] = 'page-has-excerpt';
  219. }
  220. // For pages with comments open or closed
  221. if ( comments_open() ) {
  222. $c[] = 'page-comments-open';
  223. } else {
  224. $c[] = 'page-comments-closed';
  225. }
  226. // For pages with pings open or closed
  227. if ( pings_open() ) {
  228. $c[] = 'page-pings-open';
  229. } else {
  230. $c[] = 'page-pings-closed';
  231. }
  232. // For password-protected pages
  233. if ( $post->post_password ) {
  234. $c[] = 'page-protected';
  235. }
  236. // Checks to see if the page is using a template
  237. if ( is_page_template() & !is_page_template('default') ) {
  238. $c[] = 'page-template page-template-' . str_replace( '.php', '-php', get_post_meta( $pageID, '_wp_page_template', true ) );
  239. }
  240. rewind_posts();
  241. }
  242. // Search classes for results or no results
  243. elseif ( is_search() && apply_filters( 'thematic_show_bc_search', TRUE ) ) {
  244. the_post();
  245. if ( $wp_query->found_posts > 0 ) {
  246. $c[] = 'search-results';
  247. } else {
  248. $c[] = 'search-no-results';
  249. }
  250. rewind_posts();
  251. }
  252. if ( apply_filters( 'thematic_show_bc_loggedin', TRUE ) ) {
  253. // For when a visitor is logged in while browsing
  254. if ( $current_user->ID ) {
  255. $c[] = 'loggedin';
  256. }
  257. }
  258. // Paged classes; for page x > 1 classes of index and all post types etc.
  259. if ( isset( $post ) && apply_filters( 'thematic_show_bc_pagex', TRUE ) ) {
  260. if ( ( ( ( $page = $wp_query->get( 'paged' ) ) || ( $page = $wp_query->get('page') ) ) && $page > 1 ) ) {
  261. // Thanks to Prentiss Riddle, twitter.com/pzriddle, for the security fix below.
  262. $page = intval( $page ); // Ensures that an integer (not some dangerous script) is passed for the variable
  263. $c[] = 'paged-' . $page;
  264. if ( thematic_is_custom_post_type() ) {
  265. $c[] = str_replace( '_','-',$post->post_type ) . '-paged-' . $page;
  266. } elseif ( is_single() && $post->post_type=="post" ) {
  267. $c[] = 'single-paged-' . $page;
  268. } elseif ( is_page() ) {
  269. $c[] = 'page-paged-' . $page;
  270. } elseif ( is_category() ) {
  271. $c[] = 'category-paged-' . $page;
  272. } elseif ( is_tag() ) {
  273. $c[] = 'tag-paged-' . $page;
  274. } elseif ( is_tax() ) {
  275. $c[] = 'taxonomy-paged-' . $page;
  276. } elseif ( is_date() ) {
  277. $c[] = 'date-paged-' . $page;
  278. } elseif ( is_author() ) {
  279. $c[] = 'author-paged-' . $page;
  280. } elseif ( is_search() ) {
  281. $c[] = 'search-paged-' . $page;
  282. }
  283. // Paged classes; for page x = 1 For all post types
  284. } elseif ( preg_match( '/<!--nextpage(.*?)-->/', $post->post_content ) ) {
  285. if ( thematic_is_custom_post_type() ) {
  286. $c[] = str_replace( '_','-',$post->post_type ) . '-paged-1';
  287. } elseif (is_page()) {
  288. $c[] = 'page-paged-1';
  289. } elseif (is_single()) {
  290. $c[] = 'single-paged-1';
  291. }
  292. }
  293. }
  294. // And tada!
  295. return array_unique(apply_filters( 'thematic_legacy_body_class', $c )); // Available filter: thematic_legacy_body_class
  296. }
  297. }
  298. /**
  299. * Add thematic body classes
  300. *
  301. * Child themes can add the legacy bodyclasses if they need using
  302. * add_theme_support('thematic_legacy_body_class')
  303. */
  304. function thematic_activate_body_classes() {
  305. add_filter( 'body_class', 'thematic_body_class' );
  306. if ( current_theme_supports ( 'thematic_legacy_body_class' ) ) {
  307. add_filter( 'body_class', 'thematic_legacy_body_class', 20 );
  308. }
  309. // Add browser CSS class to the end (queuing through priority) of the body classes
  310. if ( apply_filters( 'thematic_show_bc_browser', false ) ) {
  311. add_filter( 'body_class', 'thematic_browser_class_names', 30 );
  312. }
  313. }
  314. add_action( 'init', 'thematic_activate_body_classes' );
  315. /**
  316. * thematic_browser_class_names function.
  317. */
  318. function thematic_browser_class_names($classes) {
  319. // add 'class-name' to the $classes array
  320. // $classes[] = 'class-name';
  321. $browser = $_SERVER[ 'HTTP_USER_AGENT' ];
  322. // Mac, PC ...or Linux
  323. if ( preg_match( "/Mac/", $browser ) ) {
  324. $classes[] = 'mac';
  325. } elseif ( preg_match( "/Windows/", $browser ) ) {
  326. $classes[] = 'windows';
  327. } elseif ( preg_match( "/Linux/", $browser ) ) {
  328. $classes[] = 'linux';
  329. } else {
  330. $classes[] = 'unknown-os';
  331. }
  332. // Checks browsers in this order: Chrome, Safari, Opera, MSIE, FF
  333. if ( preg_match( "/Chrome/", $browser ) ) {
  334. $classes[] = 'chrome';
  335. if ( ( current_theme_supports( 'minorbrowserversion_all' )) || ( current_theme_supports( 'minorbrowserversion_ch' ) ) ) {
  336. preg_match( "/Chrome\/(\d+.\d+)/si", $browser, $matches );
  337. $ch_version = 'ch' . str_replace( '.', '-', ( isset ( $matches[1] ) ? $matches[1] : null ) );
  338. } else {
  339. preg_match( "/Chrome\/(\d+)/si", $browser, $matches );
  340. $ch_version = 'ch' . ( isset ( $matches[1] ) ? $matches[1] : null );
  341. }
  342. $classes[] = $ch_version;
  343. } elseif ( preg_match( "/Safari/", $browser ) ) {
  344. $classes[] = 'safari';
  345. if ( ( current_theme_supports( 'minorbrowserversion_all' )) || ( current_theme_supports( 'minorbrowserversion_sf' ) ) ) {
  346. preg_match( "/Version\/(\d+.\d+)/si", $browser, $matches );
  347. $sf_version = 'sf' . str_replace( '.', '-', ( isset ( $matches[1] ) ? $matches[1] : null ) );
  348. } else {
  349. preg_match( "/Version\/(\d+)/si", $browser, $matches );
  350. $sf_version = 'sf' . ( isset ( $matches[1] ) ? $matches[1] : null );
  351. }
  352. $classes[] = $sf_version;
  353. } elseif ( preg_match( "/Opera/", $browser ) ) {
  354. $classes[] = 'opera';
  355. if ( ( current_theme_supports( 'minorbrowserversion_all' ) ) || ( current_theme_supports( 'minorbrowserversion_op' ) ) ) {
  356. preg_match( "/Version\/(\d+.\d+)/si", $browser, $matches );
  357. $op_version = 'op' . str_replace( '.', '-', ( isset ( $matches[1] ) ? $matches[1] : null ) );
  358. } else {
  359. preg_match( "/Version\/(\d+)/si", $browser, $matches );
  360. $op_version = 'op' . ( isset ( $matches[1] ) ? $matches[1] : null );
  361. }
  362. $classes[] = $op_version;
  363. } elseif ( preg_match( "/MSIE/", $browser ) ) {
  364. $classes[] = 'msie';
  365. if ( ( current_theme_supports( 'minorbrowserversion_all' )) || ( current_theme_supports( 'minorbrowserversion_ie' ) ) ) {
  366. preg_match( "/MSIE (\d+.\d+)/si", $browser, $matches );
  367. $ie_version = 'ie' . str_replace( '.', '-', ( isset ( $matches[1] ) ? $matches[1] : null ) );
  368. } else {
  369. preg_match( "/MSIE (\d+)/si", $browser, $matches );
  370. $ie_version = 'ie' . ( isset ( $matches[1] ) ? $matches[1] : null );
  371. }
  372. $classes[] = $ie_version;
  373. } elseif ( preg_match( "/Firefox/", $browser ) && preg_match( "/Gecko/", $browser ) ) {
  374. $classes[] = 'firefox';
  375. if ( ( current_theme_supports( 'minorbrowserversion_all' ) ) || ( current_theme_supports( 'minorbrowserversion_ff' ) ) ) {
  376. preg_match( "/Firefox\/(\d+.\d+)/si", $browser, $matches );
  377. $ff_version = 'ff' . str_replace( '.', '-', ( isset ( $matches[1] ) ? $matches[1] : null ) );
  378. } else {
  379. preg_match( "/Firefox\/(\d+)/si", $browser, $matches );
  380. $ff_version = 'ff' . ( isset ( $matches[1] ) ? $matches[1] : null );
  381. }
  382. $classes[] = $ff_version;
  383. } else {
  384. $classes[] = 'unknown-browser';
  385. }
  386. // return the $classes array
  387. return $classes;
  388. }
  389. if (function_exists('childtheme_override_post_class')) {
  390. /**
  391. * @ignore
  392. */
  393. function thematic_post_class() {
  394. childtheme_override_post_class();
  395. }
  396. } else {
  397. /**
  398. * Generates semantic classes for each post DIV element
  399. */
  400. function thematic_post_class( $c ) {
  401. global $post, $thematic_post_alt, $thematic_content_length, $taxonomy;
  402. // hentry for hAtom compliace, gets 'alt' for every other post DIV, describes the post type and p[n]
  403. $c[] = 'hentry';
  404. $c[] = "p$thematic_post_alt";
  405. $c[] = str_replace( '_', '-', $post->post_type );
  406. $c[] = $post->post_status ;
  407. // Author for the post queried
  408. $c[] = 'author-' . sanitize_title_with_dashes( strtolower( get_the_author_meta( 'user_login' ) ) );
  409. // Category for the post queried
  410. foreach ( ( array ) get_the_category() as $cat )
  411. $c[] = 'category-' . $cat->slug;
  412. // Tags for the post queried; if not tagged, use .untagged
  413. if ( get_the_tags() == null ) {
  414. $c[] = 'untagged';
  415. } else {
  416. foreach ( ( array ) get_the_tags() as $tag )
  417. $c[] = 'tag-' . $tag->slug;
  418. }
  419. if (function_exists('get_post_type_object')) {
  420. // Taxonomies and terms for the post queried
  421. $single_post_type = get_post_type_object( get_post_type( $post->ID ) );
  422. // Check for post types without taxonomy inclusion
  423. if ( isset($single_post_type->taxonomy) ) {
  424. foreach ( ( array ) get_the_terms( $post->ID, get_post_taxonomies() ) as $term ) {
  425. // Remove tags and categories from results
  426. if ( $term->taxonomy != 'category' ) {
  427. if ( $term->taxonomy != 'post_tag' ) {
  428. $c[] = 'p-tax-' . $term->taxonomy;
  429. $c[] = 'p-' . $term->taxonomy . '-' . $term->slug;
  430. }
  431. }
  432. }
  433. }
  434. }
  435. $thematic_excerpt_more = preg_match( '/<!--more(.*?)-->/', $post->post_content );
  436. // For posts displayed as excerpts
  437. if ( $thematic_content_length == 'excerpt' || ( !is_single() && $thematic_excerpt_more ) ) {
  438. $c[] = 'is-excerpt';
  439. if ( has_excerpt() ) {
  440. // For wp-admin Write Page generated excerpts
  441. $c[] = 'custom-excerpt';
  442. } elseif ( $thematic_excerpt_more ) {
  443. // For more tag
  444. $c[] = 'moretag-excerpt';
  445. } else {
  446. // For auto generated excerpts
  447. $c[] = 'auto-excerpt';
  448. }
  449. // For posts displayed as full content
  450. } elseif ( $thematic_content_length == 'full' ) {
  451. $c[] = 'is-full';
  452. }
  453. // For posts with comments open or closed
  454. if ( comments_open() ) {
  455. $c[] = 'comments-open';
  456. } else {
  457. $c[] = 'comments-closed';
  458. }
  459. // For posts with pings open or closed
  460. if (pings_open()) {
  461. $c[] = 'pings-open';
  462. } else {
  463. $c[] = 'pings-closed';
  464. }
  465. // For password-protected posts
  466. if ( $post->post_password ) {
  467. $c[] = 'protected';
  468. }
  469. // For sticky posts
  470. if (is_sticky()) {
  471. $c[] = 'sticky';
  472. }
  473. // Applies the time- and date-based classes (below) to post DIV
  474. thematic_date_classes( mysql2date( 'U', $post->post_date ), $c );
  475. // If it's the other to the every, then add 'alt' class
  476. if ( ++$thematic_post_alt % 2 ) {
  477. $c[] = 'alt';
  478. }
  479. // Adds post slug class, prefixed by 'slug-'
  480. $c[] = 'slug-' . $post->post_name;
  481. // And tada!
  482. return array_unique(apply_filters( 'thematic_post_class', $c )); // Available filter: thematic_post_class
  483. }
  484. }
  485. /**
  486. * Add thematic post classes if child theme activates it
  487. */
  488. function thematic_activate_post_classes() {
  489. if ( current_theme_supports ( 'thematic_legacy_post_class' ) ) {
  490. add_filter( 'post_class', 'thematic_post_class', 20 );
  491. }
  492. }
  493. add_action( 'init', 'thematic_activate_post_classes' );
  494. /**
  495. * Define the num val for 'alt' classes (in post DIV and comment LI)
  496. *
  497. * @var int (default value: 1)
  498. */
  499. $thematic_post_alt = 1;
  500. /**
  501. * Adds classes to commment li's using the WordPress comment_class filter
  502. *
  503. * @since 1.0
  504. */
  505. function thematic_add_comment_class($classes) {
  506. global $comment, $post;
  507. // Add time and date based classes
  508. thematic_date_classes( mysql2date( 'U', $comment->comment_date ), $classes, 'thm-c-' );
  509. // Do not duplicate values
  510. return array_unique( $classes );
  511. }
  512. add_filter( 'comment_class', 'thematic_add_comment_class', 20 );
  513. if ( function_exists( 'childtheme_override_date_classes' ) ) {
  514. /**
  515. * @ignore
  516. */
  517. function thematic_date_classes() {
  518. childtheme_override_date_classes();
  519. }
  520. } else {
  521. /**
  522. * Generates time and date based classes relative to GMT (UTC)
  523. */
  524. function thematic_date_classes( $t, &$c, $p = '' ) {
  525. $t = $t + ( get_option('gmt_offset') * 3600 );
  526. $c[] = $p . 'y' . gmdate( 'Y', $t ); // Year
  527. $c[] = $p . 'm' . gmdate( 'm', $t ); // Month
  528. $c[] = $p . 'd' . gmdate( 'd', $t ); // Day
  529. $c[] = $p . 'h' . gmdate( 'H', $t ); // Hour
  530. }
  531. }
  532. // Remember: Thematic, like The Sandbox, is for play.
  533. ?>