web/wp-content/themes/thematic/library/extensions/dynamic-classes.php
changeset 1 0d28b7c10758
equal deleted inserted replaced
0:0d9a58d2c515 1:0d28b7c10758
       
     1 <?php
       
     2 
       
     3 // Generates semantic classes for BODY element
       
     4 function thematic_body_class( $print = true ) {
       
     5 	global $wp_query, $current_user;
       
     6     
       
     7     $c = '';
       
     8 
       
     9 	if (apply_filters('thematic_show_bc_wordpress', TRUE)) {
       
    10         // It's surely a WordPress blog, right?
       
    11         $c = array('wordpress');
       
    12     }
       
    13 
       
    14 	if (apply_filters('thematic_show_bc_datetime', TRUE)) {
       
    15         // Applies the time- and date-based classes (below) to BODY element
       
    16         thematic_date_classes( time(), $c );
       
    17     }
       
    18 
       
    19     if (apply_filters('thematic_show_bc_contenttype', TRUE)) {
       
    20         // Generic semantic classes for what type of content is displayed
       
    21         is_front_page()  ? $c[] = 'home'       : null; // For the front page, if set
       
    22         is_home()        ? $c[] = 'blog'       : null; // For the blog posts page, if set
       
    23         is_archive()     ? $c[] = 'archive'    : null;
       
    24         is_date()        ? $c[] = 'date'       : null;
       
    25         is_search()      ? $c[] = 'search'     : null;
       
    26         is_paged()       ? $c[] = 'paged'      : null;
       
    27         is_attachment()  ? $c[] = 'attachment' : null;
       
    28         is_404()         ? $c[] = 'four04'     : null; // CSS does not allow a digit as first character
       
    29     }
       
    30 
       
    31     if (apply_filters('thematic_show_bc_singular', TRUE)) {
       
    32         // Special classes for BODY element when a singular post
       
    33         if ( is_singular() ) {
       
    34             $c[] = 'singular';
       
    35         } else {
       
    36             $c[] = 'not-singular';
       
    37         }
       
    38     }
       
    39 
       
    40 	// Special classes for BODY element when a single post
       
    41 	if ( is_single() && apply_filters('thematic_show_bc_singlepost', TRUE)) {
       
    42 		$postID = $wp_query->post->ID;
       
    43 		the_post();
       
    44 
       
    45         // Adds post slug class, prefixed by 'slug-'
       
    46         $c[] = 'slug-' . $wp_query->post->post_name;
       
    47 
       
    48 		// Adds 'single' class and class with the post ID
       
    49 		$c[] = 'single postid-' . $postID;
       
    50 
       
    51 		// Adds classes for the month, day, and hour when the post was published
       
    52 		if ( isset( $wp_query->post->post_date ) )
       
    53 			thematic_date_classes( mysql2date( 'U', $wp_query->post->post_date ), $c, 's-' );
       
    54 
       
    55 		// Adds category classes for each category on single posts
       
    56 		if ( $cats = get_the_category() )
       
    57 			foreach ( $cats as $cat )
       
    58 				$c[] = 's-category-' . $cat->slug;
       
    59 
       
    60 		// Adds tag classes for each tags on single posts
       
    61 		if ( $tags = get_the_tags() )
       
    62 			foreach ( $tags as $tag )
       
    63 				$c[] = 's-tag-' . $tag->slug;
       
    64 
       
    65 		// Adds MIME-specific classes for attachments
       
    66 		if ( is_attachment() ) {
       
    67 			$mime_type = get_post_mime_type();
       
    68 			$mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
       
    69 				$c[] = 'attachmentid-' . $postID . ' attachment-' . str_replace( $mime_prefix, "", "$mime_type" );
       
    70 		}
       
    71 
       
    72 		// Adds author class for the post author
       
    73 		$c[] = 's-author-' . sanitize_title_with_dashes(strtolower(get_the_author_meta('user_nicename', $post->post_author)));
       
    74 		rewind_posts();
       
    75 		
       
    76 		// For posts with excerpts
       
    77 		if (has_excerpt())
       
    78 			$c[] = 's-has-excerpt';
       
    79 			
       
    80 		// For posts with comments open or closed
       
    81 		if (comments_open()) {
       
    82 			$c[] = 's-comments-open';		
       
    83 		} else {
       
    84 			$c[] = 's-comments-closed';
       
    85 		}
       
    86 	
       
    87 		// For posts with pings open or closed
       
    88 		if (pings_open()) {
       
    89 			$c[] = 's-pings-open';
       
    90 		} else {
       
    91 			$c[] = 's-pings-closed';
       
    92 		}
       
    93 	
       
    94 		// For password-protected posts
       
    95 		if ( $post->post_password )
       
    96 			$c[] = 's-protected';
       
    97 	
       
    98 		// For sticky posts
       
    99 		if (is_sticky())
       
   100 		   $c[] = 's-sticky';		
       
   101 		
       
   102 	}
       
   103 
       
   104 	// Author name classes for BODY on author archives
       
   105 	elseif ( is_author() && apply_filters('thematic_show_bc_authorarchives', TRUE)) {
       
   106 		$author = $wp_query->get_queried_object();
       
   107 		$c[] = 'author';
       
   108 		$c[] = 'author-' . $author->user_nicename;
       
   109 	}
       
   110 
       
   111 	// Category name classes for BODY on category archvies
       
   112 	elseif ( is_category() && apply_filters('thematic_show_bc_categoryarchives', TRUE)) {
       
   113 		$cat = $wp_query->get_queried_object();
       
   114 		$c[] = 'category';
       
   115 		$c[] = 'category-' . $cat->slug;
       
   116 	}
       
   117 
       
   118 	// Tag name classes for BODY on tag archives
       
   119 	elseif ( is_tag() && apply_filters('thematic_show_bc_tagarchives', TRUE)) {
       
   120 		$tags = $wp_query->get_queried_object();
       
   121 		$c[] = 'tag';
       
   122 		$c[] = 'tag-' . $tags->slug;
       
   123 	}
       
   124 
       
   125 	// Page author for BODY on 'pages'
       
   126 	elseif ( is_page() && apply_filters('thematic_show_bc_pages', TRUE)) {
       
   127 		$pageID = $wp_query->post->ID;
       
   128 		$page_children = wp_list_pages("child_of=$pageID&echo=0");
       
   129 		the_post();
       
   130 
       
   131         // Adds post slug class, prefixed by 'slug-'
       
   132         $c[] = 'slug-' . $wp_query->post->post_name;
       
   133 
       
   134 		$c[] = 'page pageid-' . $pageID;
       
   135 		
       
   136 		$c[] = 'page-author-' . sanitize_title_with_dashes(strtolower(get_the_author_meta('user_nicename', $post->post_author)));
       
   137 		
       
   138 		// Checks to see if the page has children and/or is a child page; props to Adam
       
   139 		if ( $page_children )
       
   140 			$c[] = 'page-parent';
       
   141 		if ( $wp_query->post->post_parent )
       
   142 			$c[] = 'page-child parent-pageid-' . $wp_query->post->post_parent;
       
   143 			
       
   144 		// For pages with excerpts
       
   145 		if (has_excerpt())
       
   146 			$c[] = 'page-has-excerpt';
       
   147 			
       
   148 		// For pages with comments open or closed
       
   149 		if (comments_open()) {
       
   150 			$c[] = 'page-comments-open';		
       
   151 		} else {
       
   152 			$c[] = 'page-comments-closed';
       
   153 		}
       
   154 	
       
   155 		// For pages with pings open or closed
       
   156 		if (pings_open()) {
       
   157 			$c[] = 'page-pings-open';
       
   158 		} else {
       
   159 			$c[] = 'page-pings-closed';
       
   160 		}
       
   161 	
       
   162 		// For password-protected pages
       
   163 		if ( $post->post_password )
       
   164 			$c[] = 'page-protected';			
       
   165 			
       
   166 		// Checks to see if the page is using a template	
       
   167 		if ( is_page_template() & !is_page_template('default') )
       
   168 			$c[] = 'page-template page-template-' . str_replace( '.php', '-php', get_post_meta( $pageID, '_wp_page_template', true ) );
       
   169 		rewind_posts();
       
   170 	}
       
   171 
       
   172 	// Search classes for results or no results
       
   173 	elseif ( is_search() && apply_filters('thematic_show_bc_search', TRUE)) {
       
   174 		the_post();
       
   175 		if ( $wp_query->found_posts > 0 ) {
       
   176 			$c[] = 'search-results';
       
   177 		} else {
       
   178 			$c[] = 'search-no-results';
       
   179 		}
       
   180 		rewind_posts();
       
   181 	}
       
   182 
       
   183 	if (apply_filters('thematic_show_bc_loggedin', TRUE)) {
       
   184         // For when a visitor is logged in while browsing
       
   185         if ( $current_user->ID )
       
   186             $c[] = 'loggedin';
       
   187     }
       
   188 
       
   189 	// Paged classes; for 'page X' classes of index, single, etc.
       
   190 	if ( (( ( $page = $wp_query->get('paged') ) || ( $page = $wp_query->get('page') ) ) && $page > 1) && apply_filters('thematic_show_bc_pagex', TRUE)) {
       
   191 	// Thanks to Prentiss Riddle, twitter.com/pzriddle, for the security fix below. 
       
   192  			$page = intval($page); // Ensures that an integer (not some dangerous script) is passed for the variable
       
   193 		$c[] = 'paged-' . $page;
       
   194 		if ( is_single() ) {
       
   195 			$c[] = 'single-paged-' . $page;
       
   196 		} elseif ( is_page() ) {
       
   197 			$c[] = 'page-paged-' . $page;
       
   198 		} elseif ( is_category() ) {
       
   199 			$c[] = 'category-paged-' . $page;
       
   200 		} elseif ( is_tag() ) {
       
   201 			$c[] = 'tag-paged-' . $page;
       
   202 		} elseif ( is_date() ) {
       
   203 			$c[] = 'date-paged-' . $page;
       
   204 		} elseif ( is_author() ) {
       
   205 			$c[] = 'author-paged-' . $page;
       
   206 		} elseif ( is_search() ) {
       
   207 			$c[] = 'search-paged-' . $page;
       
   208 		}
       
   209 	}
       
   210 	
       
   211 	if (apply_filters('thematic_show_bc_browser', TRUE)) {
       
   212         // A little Browser detection shall we?
       
   213         $browser = $_SERVER[ 'HTTP_USER_AGENT' ];
       
   214 	
       
   215         // Mac, PC ...or Linux
       
   216         if ( preg_match( "/Mac/", $browser ) ){
       
   217 			$c[] = 'mac';
       
   218 		
       
   219         } elseif ( preg_match( "/Windows/", $browser ) ){
       
   220 			$c[] = 'windows';
       
   221 		
       
   222         } elseif ( preg_match( "/Linux/", $browser ) ) {
       
   223 			$c[] = 'linux';
       
   224 
       
   225         } else {
       
   226 			$c[] = 'unknown-os';
       
   227         }
       
   228 	
       
   229         // Checks browsers in this order: Chrome, Safari, Opera, MSIE, FF
       
   230         if ( preg_match( "/Chrome/", $browser ) ) {
       
   231 			$c[] = 'chrome';
       
   232 
       
   233 			preg_match( "/Chrome\/(\d.\d)/si", $browser, $matches);
       
   234 			$ch_version = 'ch' . str_replace( '.', '-', $matches[1] );      
       
   235 			$c[] = $ch_version;
       
   236 
       
   237         } elseif ( preg_match( "/Safari/", $browser ) ) {
       
   238 			$c[] = 'safari';
       
   239 			
       
   240 			preg_match( "/Version\/(\d.\d)/si", $browser, $matches);
       
   241 			$sf_version = 'sf' . str_replace( '.', '-', $matches[1] );      
       
   242 			$c[] = $sf_version;
       
   243 			
       
   244         } elseif ( preg_match( "/Opera/", $browser ) ) {
       
   245 			$c[] = 'opera';
       
   246 			
       
   247 			preg_match( "/Opera\/(\d.\d)/si", $browser, $matches);
       
   248 			$op_version = 'op' . str_replace( '.', '-', $matches[1] );      
       
   249 			$c[] = $op_version;
       
   250 			
       
   251         } elseif ( preg_match( "/MSIE/", $browser ) ) {
       
   252 			$c[] = 'msie';
       
   253 			
       
   254 			if( preg_match( "/MSIE 6.0/", $browser ) ) {
       
   255 					$c[] = 'ie6';
       
   256 			} elseif ( preg_match( "/MSIE 7.0/", $browser ) ){
       
   257 					$c[] = 'ie7';
       
   258 			} elseif ( preg_match( "/MSIE 8.0/", $browser ) ){
       
   259 					$c[] = 'ie8';
       
   260 			}
       
   261 			
       
   262         } elseif ( preg_match( "/Firefox/", $browser ) && preg_match( "/Gecko/", $browser ) ) {
       
   263 			$c[] = 'firefox';
       
   264 			
       
   265 			preg_match( "/Firefox\/(\d)/si", $browser, $matches);
       
   266 			$ff_version = 'ff' . str_replace( '.', '-', $matches[1] );      
       
   267 			$c[] = $ff_version;
       
   268 			
       
   269         } else {
       
   270 			$c[] = 'unknown-browser';
       
   271         }
       
   272 	}
       
   273 	
       
   274 
       
   275 	// Separates classes with a single space, collates classes for BODY
       
   276 	$c = join( ' ', apply_filters( 'body_class',  $c ) ); // Available filter: body_class
       
   277 
       
   278 	// And tada!
       
   279 	return $print ? print($c) : $c;
       
   280 }
       
   281 
       
   282 // Generates semantic classes for each post DIV element
       
   283 function thematic_post_class( $print = true ) {
       
   284 	global $post, $thematic_post_alt;
       
   285 
       
   286 	// hentry for hAtom compliace, gets 'alt' for every other post DIV, describes the post type and p[n]
       
   287 	$c = array( 'hentry', "p$thematic_post_alt", $post->post_type, $post->post_status );
       
   288 
       
   289 	// Author for the post queried
       
   290 	$c[] = 'author-' . sanitize_title_with_dashes(strtolower(get_the_author('login')));
       
   291 
       
   292 	// Category for the post queried
       
   293 	foreach ( (array) get_the_category() as $cat )
       
   294 		$c[] = 'category-' . $cat->slug;
       
   295 
       
   296 	// Tags for the post queried; if not tagged, use .untagged
       
   297 	if ( get_the_tags() == null ) {
       
   298 		$c[] = 'untagged';
       
   299 	} else {
       
   300 		foreach ( (array) get_the_tags() as $tag )
       
   301 			$c[] = 'tag-' . $tag->slug;
       
   302 	}
       
   303 
       
   304 	// For posts with excerpts
       
   305 	if (has_excerpt())
       
   306 		$c[] = 'has-excerpt';
       
   307 		
       
   308 	// For posts with comments open or closed
       
   309 	if (comments_open()) {
       
   310 		$c[] = 'comments-open';		
       
   311 	} else {
       
   312 		$c[] = 'comments-closed';
       
   313 	}
       
   314 
       
   315 	// For posts with pings open or closed
       
   316 	if (pings_open()) {
       
   317 		$c[] = 'pings-open';
       
   318 	} else {
       
   319 		$c[] = 'pings-closed';
       
   320 	}
       
   321 
       
   322 	// For password-protected posts
       
   323 	if ( $post->post_password )
       
   324 		$c[] = 'protected';
       
   325 
       
   326 	// For sticky posts
       
   327 	if (is_sticky())
       
   328 	   $c[] = 'sticky';
       
   329 
       
   330 	// Applies the time- and date-based classes (below) to post DIV
       
   331 	thematic_date_classes( mysql2date( 'U', $post->post_date ), $c );
       
   332 
       
   333 	// If it's the other to the every, then add 'alt' class
       
   334 	if ( ++$thematic_post_alt % 2 )
       
   335 		$c[] = 'alt';
       
   336 
       
   337     // Adds post slug class, prefixed by 'slug-'
       
   338     $c[] = 'slug-' . $post->post_name;
       
   339 
       
   340 	// Separates classes with a single space, collates classes for post DIV
       
   341 	$c = join( ' ', apply_filters( 'post_class', $c ) ); // Available filter: post_class
       
   342 
       
   343 	// And tada!
       
   344 	return $print ? print($c) : $c;
       
   345 }
       
   346 
       
   347 // Define the num val for 'alt' classes (in post DIV and comment LI)
       
   348 $thematic_post_alt = 1;
       
   349 
       
   350 // Generates semantic classes for each comment LI element
       
   351 function thematic_comment_class( $print = true ) {
       
   352 	global $comment, $post, $thematic_comment_alt, $comment_depth, $comment_thread_alt;
       
   353 
       
   354 	// Collects the comment type (comment, trackback),
       
   355 	$c = array( $comment->comment_type );
       
   356 
       
   357 	// Counts trackbacks (t[n]) or comments (c[n])
       
   358 	if ( $comment->comment_type == 'comment' ) {
       
   359 		$c[] = "c$thematic_comment_alt";
       
   360 	} else {
       
   361 		$c[] = "t$thematic_comment_alt";
       
   362 	}
       
   363 
       
   364 	// If the comment author has an id (registered), then print the log in name
       
   365 	if ( $comment->user_id > 0 ) {
       
   366 		$user = get_userdata($comment->user_id);
       
   367 		// For all registered users, 'byuser'; to specificy the registered user, 'commentauthor+[log in name]'
       
   368 		$c[] = 'byuser comment-author-' . sanitize_title_with_dashes(strtolower( $user->user_login ));
       
   369 		// For comment authors who are the author of the post
       
   370 		if ( $comment->user_id === $post->post_author )
       
   371 			$c[] = 'bypostauthor';
       
   372 	}
       
   373 
       
   374 	// If it's the other to the every, then add 'alt' class; collects time- and date-based classes
       
   375 	thematic_date_classes( mysql2date( 'U', $comment->comment_date ), $c, 'c-' );
       
   376 	if ( ++$thematic_comment_alt % 2 )
       
   377 		$c[] = 'alt';
       
   378 
       
   379 	// Comment depth
       
   380 	$c[] = "depth-$comment_depth";
       
   381 
       
   382 	// Separates classes with a single space, collates classes for comment LI
       
   383 	$c = join( ' ', apply_filters( 'comment_class', $c ) ); // Available filter: comment_class
       
   384 
       
   385 	// Tada again!
       
   386 	return $print ? print($c) : $c;
       
   387 }
       
   388 
       
   389 // Generates time- and date-based classes for BODY, post DIVs, and comment LIs; relative to GMT (UTC)
       
   390 function thematic_date_classes( $t, &$c, $p = '' ) {
       
   391 	$t = $t + ( get_option('gmt_offset') * 3600 );
       
   392 	$c[] = $p . 'y' . gmdate( 'Y', $t ); // Year
       
   393 	$c[] = $p . 'm' . gmdate( 'm', $t ); // Month
       
   394 	$c[] = $p . 'd' . gmdate( 'd', $t ); // Day
       
   395 	$c[] = $p . 'h' . gmdate( 'H', $t ); // Hour
       
   396 }
       
   397 
       
   398 // Remember: Thematic, like The Sandbox, is for play.
       
   399 ?>