]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/themes/twentytwelve/functions.php
Wordpress 3.6
[autoinstalls/wordpress.git] / wp-content / themes / twentytwelve / functions.php
1 <?php
2 /**
3  * Twenty Twelve functions and definitions.
4  *
5  * Sets up the theme and provides some helper functions, which are used
6  * in the theme as custom template tags. Others are attached to action and
7  * filter hooks in WordPress to change core functionality.
8  *
9  * When using a child theme (see http://codex.wordpress.org/Theme_Development and
10  * http://codex.wordpress.org/Child_Themes), you can override certain functions
11  * (those wrapped in a function_exists() call) by defining them first in your child theme's
12  * functions.php file. The child theme's functions.php file is included before the parent
13  * theme's file, so the child theme functions would be used.
14  *
15  * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
16  * to a filter or action hook.
17  *
18  * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
19  *
20  * @package WordPress
21  * @subpackage Twenty_Twelve
22  * @since Twenty Twelve 1.0
23  */
24
25 /**
26  * Sets up the content width value based on the theme's design and stylesheet.
27  */
28 if ( ! isset( $content_width ) )
29         $content_width = 625;
30
31 /**
32  * Sets up theme defaults and registers the various WordPress features that
33  * Twenty Twelve supports.
34  *
35  * @uses load_theme_textdomain() For translation/localization support.
36  * @uses add_editor_style() To add a Visual Editor stylesheet.
37  * @uses add_theme_support() To add support for post thumbnails, automatic feed links,
38  *      custom background, and post formats.
39  * @uses register_nav_menu() To add support for navigation menus.
40  * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
41  *
42  * @since Twenty Twelve 1.0
43  */
44 function twentytwelve_setup() {
45         /*
46          * Makes Twenty Twelve available for translation.
47          *
48          * Translations can be added to the /languages/ directory.
49          * If you're building a theme based on Twenty Twelve, use a find and replace
50          * to change 'twentytwelve' to the name of your theme in all the template files.
51          */
52         load_theme_textdomain( 'twentytwelve', get_template_directory() . '/languages' );
53
54         // This theme styles the visual editor with editor-style.css to match the theme style.
55         add_editor_style();
56
57         // Adds RSS feed links to <head> for posts and comments.
58         add_theme_support( 'automatic-feed-links' );
59
60         // This theme supports a variety of post formats.
61         add_theme_support( 'post-formats', array( 'aside', 'image', 'link', 'quote', 'status' ) );
62
63         // This theme uses wp_nav_menu() in one location.
64         register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ) );
65
66         /*
67          * This theme supports custom background color and image, and here
68          * we also set up the default background color.
69          */
70         add_theme_support( 'custom-background', array(
71                 'default-color' => 'e6e6e6',
72         ) );
73
74         // This theme uses a custom image size for featured images, displayed on "standard" posts.
75         add_theme_support( 'post-thumbnails' );
76         set_post_thumbnail_size( 624, 9999 ); // Unlimited height, soft crop
77 }
78 add_action( 'after_setup_theme', 'twentytwelve_setup' );
79
80 /**
81  * Adds support for a custom header image.
82  */
83 require( get_template_directory() . '/inc/custom-header.php' );
84
85 /**
86  * Returns the Google font stylesheet URL if available.
87  *
88  * The use of Open Sans by default is localized. For languages that use
89  * characters not supported by the font, the font can be disabled.
90  *
91  * @since Twenty Twelve 1.2
92  *
93  * @return string Font stylesheet or empty string if disabled.
94  */
95 function twentytwelve_get_font_url() {
96         $font_url = '';
97
98         /* translators: If there are characters in your language that are not supported
99          by Open Sans, translate this to 'off'. Do not translate into your own language. */
100         if ( 'off' !== _x( 'on', 'Open Sans font: on or off', 'twentytwelve' ) ) {
101                 $subsets = 'latin,latin-ext';
102
103                 /* translators: To add an additional Open Sans character subset specific to your language, translate
104                  this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language. */
105                 $subset = _x( 'no-subset', 'Open Sans font: add new subset (greek, cyrillic, vietnamese)', 'twentytwelve' );
106
107                 if ( 'cyrillic' == $subset )
108                         $subsets .= ',cyrillic,cyrillic-ext';
109                 elseif ( 'greek' == $subset )
110                         $subsets .= ',greek,greek-ext';
111                 elseif ( 'vietnamese' == $subset )
112                         $subsets .= ',vietnamese';
113
114                 $protocol = is_ssl() ? 'https' : 'http';
115                 $query_args = array(
116                         'family' => 'Open+Sans:400italic,700italic,400,700',
117                         'subset' => $subsets,
118                 );
119                 $font_url = add_query_arg( $query_args, "$protocol://fonts.googleapis.com/css" );
120         }
121
122         return $font_url;
123 }
124
125 /**
126  * Enqueues scripts and styles for front-end.
127  *
128  * @since Twenty Twelve 1.0
129  */
130 function twentytwelve_scripts_styles() {
131         global $wp_styles;
132
133         /*
134          * Adds JavaScript to pages with the comment form to support
135          * sites with threaded comments (when in use).
136          */
137         if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
138                 wp_enqueue_script( 'comment-reply' );
139
140         /*
141          * Adds JavaScript for handling the navigation menu hide-and-show behavior.
142          */
143         wp_enqueue_script( 'twentytwelve-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '1.0', true );
144
145         $font_url = twentytwelve_get_font_url();
146         if ( ! empty( $font_url ) )
147                 wp_enqueue_style( 'twentytwelve-fonts', esc_url_raw( $font_url ), array(), null );
148
149         /*
150          * Loads our main stylesheet.
151          */
152         wp_enqueue_style( 'twentytwelve-style', get_stylesheet_uri() );
153
154         /*
155          * Loads the Internet Explorer specific stylesheet.
156          */
157         wp_enqueue_style( 'twentytwelve-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentytwelve-style' ), '20121010' );
158         $wp_styles->add_data( 'twentytwelve-ie', 'conditional', 'lt IE 9' );
159 }
160 add_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' );
161
162 /**
163  * Adds additional stylesheets to the TinyMCE editor if needed.
164  *
165  * @uses twentytwelve_get_font_url() To get the Google Font stylesheet URL.
166  *
167  * @since Twenty Twelve 1.2
168  *
169  * @param string $mce_css CSS path to load in TinyMCE.
170  * @return string
171  */
172 function twentytwelve_mce_css( $mce_css ) {
173         $font_url = twentytwelve_get_font_url();
174
175         if ( empty( $font_url ) )
176                 return $mce_css;
177
178         if ( ! empty( $mce_css ) )
179                 $mce_css .= ',';
180
181         $mce_css .= esc_url_raw( str_replace( ',', '%2C', $font_url ) );
182
183         return $mce_css;
184 }
185 add_filter( 'mce_css', 'twentytwelve_mce_css' );
186
187 /**
188  * Creates a nicely formatted and more specific title element text
189  * for output in head of document, based on current view.
190  *
191  * @since Twenty Twelve 1.0
192  *
193  * @param string $title Default title text for current view.
194  * @param string $sep Optional separator.
195  * @return string Filtered title.
196  */
197 function twentytwelve_wp_title( $title, $sep ) {
198         global $paged, $page;
199
200         if ( is_feed() )
201                 return $title;
202
203         // Add the site name.
204         $title .= get_bloginfo( 'name' );
205
206         // Add the site description for the home/front page.
207         $site_description = get_bloginfo( 'description', 'display' );
208         if ( $site_description && ( is_home() || is_front_page() ) )
209                 $title = "$title $sep $site_description";
210
211         // Add a page number if necessary.
212         if ( $paged >= 2 || $page >= 2 )
213                 $title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );
214
215         return $title;
216 }
217 add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
218
219 /**
220  * Makes our wp_nav_menu() fallback -- wp_page_menu() -- show a home link.
221  *
222  * @since Twenty Twelve 1.0
223  */
224 function twentytwelve_page_menu_args( $args ) {
225         if ( ! isset( $args['show_home'] ) )
226                 $args['show_home'] = true;
227         return $args;
228 }
229 add_filter( 'wp_page_menu_args', 'twentytwelve_page_menu_args' );
230
231 /**
232  * Registers our main widget area and the front page widget areas.
233  *
234  * @since Twenty Twelve 1.0
235  */
236 function twentytwelve_widgets_init() {
237         register_sidebar( array(
238                 'name' => __( 'Main Sidebar', 'twentytwelve' ),
239                 'id' => 'sidebar-1',
240                 'description' => __( 'Appears on posts and pages except the optional Front Page template, which has its own widgets', 'twentytwelve' ),
241                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
242                 'after_widget' => '</aside>',
243                 'before_title' => '<h3 class="widget-title">',
244                 'after_title' => '</h3>',
245         ) );
246
247         register_sidebar( array(
248                 'name' => __( 'First Front Page Widget Area', 'twentytwelve' ),
249                 'id' => 'sidebar-2',
250                 'description' => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
251                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
252                 'after_widget' => '</aside>',
253                 'before_title' => '<h3 class="widget-title">',
254                 'after_title' => '</h3>',
255         ) );
256
257         register_sidebar( array(
258                 'name' => __( 'Second Front Page Widget Area', 'twentytwelve' ),
259                 'id' => 'sidebar-3',
260                 'description' => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
261                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
262                 'after_widget' => '</aside>',
263                 'before_title' => '<h3 class="widget-title">',
264                 'after_title' => '</h3>',
265         ) );
266 }
267 add_action( 'widgets_init', 'twentytwelve_widgets_init' );
268
269 if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
270 /**
271  * Displays navigation to next/previous pages when applicable.
272  *
273  * @since Twenty Twelve 1.0
274  */
275 function twentytwelve_content_nav( $html_id ) {
276         global $wp_query;
277
278         $html_id = esc_attr( $html_id );
279
280         if ( $wp_query->max_num_pages > 1 ) : ?>
281                 <nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
282                         <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
283                         <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentytwelve' ) ); ?></div>
284                         <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?></div>
285                 </nav><!-- #<?php echo $html_id; ?> .navigation -->
286         <?php endif;
287 }
288 endif;
289
290 if ( ! function_exists( 'twentytwelve_comment' ) ) :
291 /**
292  * Template for comments and pingbacks.
293  *
294  * To override this walker in a child theme without modifying the comments template
295  * simply create your own twentytwelve_comment(), and that function will be used instead.
296  *
297  * Used as a callback by wp_list_comments() for displaying the comments.
298  *
299  * @since Twenty Twelve 1.0
300  */
301 function twentytwelve_comment( $comment, $args, $depth ) {
302         $GLOBALS['comment'] = $comment;
303         switch ( $comment->comment_type ) :
304                 case 'pingback' :
305                 case 'trackback' :
306                 // Display trackbacks differently than normal comments.
307         ?>
308         <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
309                 <p><?php _e( 'Pingback:', 'twentytwelve' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></p>
310         <?php
311                         break;
312                 default :
313                 // Proceed with normal comments.
314                 global $post;
315         ?>
316         <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
317                 <article id="comment-<?php comment_ID(); ?>" class="comment">
318                         <header class="comment-meta comment-author vcard">
319                                 <?php
320                                         echo get_avatar( $comment, 44 );
321                                         printf( '<cite><b class="fn">%1$s</b> %2$s</cite>',
322                                                 get_comment_author_link(),
323                                                 // If current post author is also comment author, make it known visually.
324                                                 ( $comment->user_id === $post->post_author ) ? '<span>' . __( 'Post author', 'twentytwelve' ) . '</span>' : ''
325                                         );
326                                         printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
327                                                 esc_url( get_comment_link( $comment->comment_ID ) ),
328                                                 get_comment_time( 'c' ),
329                                                 /* translators: 1: date, 2: time */
330                                                 sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
331                                         );
332                                 ?>
333                         </header><!-- .comment-meta -->
334
335                         <?php if ( '0' == $comment->comment_approved ) : ?>
336                                 <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwelve' ); ?></p>
337                         <?php endif; ?>
338
339                         <section class="comment-content comment">
340                                 <?php comment_text(); ?>
341                                 <?php edit_comment_link( __( 'Edit', 'twentytwelve' ), '<p class="edit-link">', '</p>' ); ?>
342                         </section><!-- .comment-content -->
343
344                         <div class="reply">
345                                 <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'twentytwelve' ), 'after' => ' <span>&darr;</span>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
346                         </div><!-- .reply -->
347                 </article><!-- #comment-## -->
348         <?php
349                 break;
350         endswitch; // end comment_type check
351 }
352 endif;
353
354 if ( ! function_exists( 'twentytwelve_entry_meta' ) ) :
355 /**
356  * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
357  *
358  * Create your own twentytwelve_entry_meta() to override in a child theme.
359  *
360  * @since Twenty Twelve 1.0
361  */
362 function twentytwelve_entry_meta() {
363         // Translators: used between list items, there is a space after the comma.
364         $categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) );
365
366         // Translators: used between list items, there is a space after the comma.
367         $tag_list = get_the_tag_list( '', __( ', ', 'twentytwelve' ) );
368
369         $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
370                 esc_url( get_permalink() ),
371                 esc_attr( get_the_time() ),
372                 esc_attr( get_the_date( 'c' ) ),
373                 esc_html( get_the_date() )
374         );
375
376         $author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
377                 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
378                 esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
379                 get_the_author()
380         );
381
382         // Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
383         if ( $tag_list ) {
384                 $utility_text = __( 'This entry was posted in %1$s and tagged %2$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
385         } elseif ( $categories_list ) {
386                 $utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
387         } else {
388                 $utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
389         }
390
391         printf(
392                 $utility_text,
393                 $categories_list,
394                 $tag_list,
395                 $date,
396                 $author
397         );
398 }
399 endif;
400
401 /**
402  * Extends the default WordPress body class to denote:
403  * 1. Using a full-width layout, when no active widgets in the sidebar
404  *    or full-width template.
405  * 2. Front Page template: thumbnail in use and number of sidebars for
406  *    widget areas.
407  * 3. White or empty background color to change the layout and spacing.
408  * 4. Custom fonts enabled.
409  * 5. Single or multiple authors.
410  *
411  * @since Twenty Twelve 1.0
412  *
413  * @param array Existing class values.
414  * @return array Filtered class values.
415  */
416 function twentytwelve_body_class( $classes ) {
417         $background_color = get_background_color();
418         $background_image = get_background_image();
419
420         if ( ! is_active_sidebar( 'sidebar-1' ) || is_page_template( 'page-templates/full-width.php' ) )
421                 $classes[] = 'full-width';
422
423         if ( is_page_template( 'page-templates/front-page.php' ) ) {
424                 $classes[] = 'template-front-page';
425                 if ( has_post_thumbnail() )
426                         $classes[] = 'has-post-thumbnail';
427                 if ( is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) )
428                         $classes[] = 'two-sidebars';
429         }
430
431         if ( empty( $background_image ) ) {
432                 if ( empty( $background_color ) )
433                         $classes[] = 'custom-background-empty';
434                 elseif ( in_array( $background_color, array( 'fff', 'ffffff' ) ) )
435                         $classes[] = 'custom-background-white';
436         }
437
438         // Enable custom font class only if the font CSS is queued to load.
439         if ( wp_style_is( 'twentytwelve-fonts', 'queue' ) )
440                 $classes[] = 'custom-font-enabled';
441
442         if ( ! is_multi_author() )
443                 $classes[] = 'single-author';
444
445         return $classes;
446 }
447 add_filter( 'body_class', 'twentytwelve_body_class' );
448
449 /**
450  * Adjusts content_width value for full-width and single image attachment
451  * templates, and when there are no active widgets in the sidebar.
452  *
453  * @since Twenty Twelve 1.0
454  */
455 function twentytwelve_content_width() {
456         if ( is_page_template( 'page-templates/full-width.php' ) || is_attachment() || ! is_active_sidebar( 'sidebar-1' ) ) {
457                 global $content_width;
458                 $content_width = 960;
459         }
460 }
461 add_action( 'template_redirect', 'twentytwelve_content_width' );
462
463 /**
464  * Add postMessage support for site title and description for the Theme Customizer.
465  *
466  * @since Twenty Twelve 1.0
467  *
468  * @param WP_Customize_Manager $wp_customize Theme Customizer object.
469  * @return void
470  */
471 function twentytwelve_customize_register( $wp_customize ) {
472         $wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
473         $wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
474         $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
475 }
476 add_action( 'customize_register', 'twentytwelve_customize_register' );
477
478 /**
479  * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
480  *
481  * @since Twenty Twelve 1.0
482  */
483 function twentytwelve_customize_preview_js() {
484         wp_enqueue_script( 'twentytwelve-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20130301', true );
485 }
486 add_action( 'customize_preview_init', 'twentytwelve_customize_preview_js' );