web/wp-content/themes/twentytwelve/functions.php
changeset 204 09a1c134465b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
       
     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  * Enqueues scripts and styles for front-end.
       
    87  *
       
    88  * @since Twenty Twelve 1.0
       
    89  */
       
    90 function twentytwelve_scripts_styles() {
       
    91 	global $wp_styles;
       
    92 
       
    93 	/*
       
    94 	 * Adds JavaScript to pages with the comment form to support
       
    95 	 * sites with threaded comments (when in use).
       
    96 	 */
       
    97 	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
       
    98 		wp_enqueue_script( 'comment-reply' );
       
    99 
       
   100 	/*
       
   101 	 * Adds JavaScript for handling the navigation menu hide-and-show behavior.
       
   102 	 */
       
   103 	wp_enqueue_script( 'twentytwelve-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '1.0', true );
       
   104 
       
   105 	/*
       
   106 	 * Loads our special font CSS file.
       
   107 	 *
       
   108 	 * The use of Open Sans by default is localized. For languages that use
       
   109 	 * characters not supported by the font, the font can be disabled.
       
   110 	 *
       
   111 	 * To disable in a child theme, use wp_dequeue_style()
       
   112 	 * function mytheme_dequeue_fonts() {
       
   113 	 *     wp_dequeue_style( 'twentytwelve-fonts' );
       
   114 	 * }
       
   115 	 * add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );
       
   116 	 */
       
   117 
       
   118 	/* translators: If there are characters in your language that are not supported
       
   119 	   by Open Sans, translate this to 'off'. Do not translate into your own language. */
       
   120 	if ( 'off' !== _x( 'on', 'Open Sans font: on or off', 'twentytwelve' ) ) {
       
   121 		$subsets = 'latin,latin-ext';
       
   122 
       
   123 		/* translators: To add an additional Open Sans character subset specific to your language, translate
       
   124 		   this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language. */
       
   125 		$subset = _x( 'no-subset', 'Open Sans font: add new subset (greek, cyrillic, vietnamese)', 'twentytwelve' );
       
   126 
       
   127 		if ( 'cyrillic' == $subset )
       
   128 			$subsets .= ',cyrillic,cyrillic-ext';
       
   129 		elseif ( 'greek' == $subset )
       
   130 			$subsets .= ',greek,greek-ext';
       
   131 		elseif ( 'vietnamese' == $subset )
       
   132 			$subsets .= ',vietnamese';
       
   133 
       
   134 		$protocol = is_ssl() ? 'https' : 'http';
       
   135 		$query_args = array(
       
   136 			'family' => 'Open+Sans:400italic,700italic,400,700',
       
   137 			'subset' => $subsets,
       
   138 		);
       
   139 		wp_enqueue_style( 'twentytwelve-fonts', add_query_arg( $query_args, "$protocol://fonts.googleapis.com/css" ), array(), null );
       
   140 	}
       
   141 
       
   142 	/*
       
   143 	 * Loads our main stylesheet.
       
   144 	 */
       
   145 	wp_enqueue_style( 'twentytwelve-style', get_stylesheet_uri() );
       
   146 
       
   147 	/*
       
   148 	 * Loads the Internet Explorer specific stylesheet.
       
   149 	 */
       
   150 	wp_enqueue_style( 'twentytwelve-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentytwelve-style' ), '20121010' );
       
   151 	$wp_styles->add_data( 'twentytwelve-ie', 'conditional', 'lt IE 9' );
       
   152 }
       
   153 add_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' );
       
   154 
       
   155 /**
       
   156  * Creates a nicely formatted and more specific title element text
       
   157  * for output in head of document, based on current view.
       
   158  *
       
   159  * @since Twenty Twelve 1.0
       
   160  *
       
   161  * @param string $title Default title text for current view.
       
   162  * @param string $sep Optional separator.
       
   163  * @return string Filtered title.
       
   164  */
       
   165 function twentytwelve_wp_title( $title, $sep ) {
       
   166 	global $paged, $page;
       
   167 
       
   168 	if ( is_feed() )
       
   169 		return $title;
       
   170 
       
   171 	// Add the site name.
       
   172 	$title .= get_bloginfo( 'name' );
       
   173 
       
   174 	// Add the site description for the home/front page.
       
   175 	$site_description = get_bloginfo( 'description', 'display' );
       
   176 	if ( $site_description && ( is_home() || is_front_page() ) )
       
   177 		$title = "$title $sep $site_description";
       
   178 
       
   179 	// Add a page number if necessary.
       
   180 	if ( $paged >= 2 || $page >= 2 )
       
   181 		$title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );
       
   182 
       
   183 	return $title;
       
   184 }
       
   185 add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
       
   186 
       
   187 /**
       
   188  * Makes our wp_nav_menu() fallback -- wp_page_menu() -- show a home link.
       
   189  *
       
   190  * @since Twenty Twelve 1.0
       
   191  */
       
   192 function twentytwelve_page_menu_args( $args ) {
       
   193 	if ( ! isset( $args['show_home'] ) )
       
   194 		$args['show_home'] = true;
       
   195 	return $args;
       
   196 }
       
   197 add_filter( 'wp_page_menu_args', 'twentytwelve_page_menu_args' );
       
   198 
       
   199 /**
       
   200  * Registers our main widget area and the front page widget areas.
       
   201  *
       
   202  * @since Twenty Twelve 1.0
       
   203  */
       
   204 function twentytwelve_widgets_init() {
       
   205 	register_sidebar( array(
       
   206 		'name' => __( 'Main Sidebar', 'twentytwelve' ),
       
   207 		'id' => 'sidebar-1',
       
   208 		'description' => __( 'Appears on posts and pages except the optional Front Page template, which has its own widgets', 'twentytwelve' ),
       
   209 		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
       
   210 		'after_widget' => '</aside>',
       
   211 		'before_title' => '<h3 class="widget-title">',
       
   212 		'after_title' => '</h3>',
       
   213 	) );
       
   214 
       
   215 	register_sidebar( array(
       
   216 		'name' => __( 'First Front Page Widget Area', 'twentytwelve' ),
       
   217 		'id' => 'sidebar-2',
       
   218 		'description' => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
       
   219 		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
       
   220 		'after_widget' => '</aside>',
       
   221 		'before_title' => '<h3 class="widget-title">',
       
   222 		'after_title' => '</h3>',
       
   223 	) );
       
   224 
       
   225 	register_sidebar( array(
       
   226 		'name' => __( 'Second Front Page Widget Area', 'twentytwelve' ),
       
   227 		'id' => 'sidebar-3',
       
   228 		'description' => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
       
   229 		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
       
   230 		'after_widget' => '</aside>',
       
   231 		'before_title' => '<h3 class="widget-title">',
       
   232 		'after_title' => '</h3>',
       
   233 	) );
       
   234 }
       
   235 add_action( 'widgets_init', 'twentytwelve_widgets_init' );
       
   236 
       
   237 if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
       
   238 /**
       
   239  * Displays navigation to next/previous pages when applicable.
       
   240  *
       
   241  * @since Twenty Twelve 1.0
       
   242  */
       
   243 function twentytwelve_content_nav( $html_id ) {
       
   244 	global $wp_query;
       
   245 
       
   246 	$html_id = esc_attr( $html_id );
       
   247 
       
   248 	if ( $wp_query->max_num_pages > 1 ) : ?>
       
   249 		<nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
       
   250 			<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
       
   251 			<div class="nav-previous alignleft"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentytwelve' ) ); ?></div>
       
   252 			<div class="nav-next alignright"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?></div>
       
   253 		</nav><!-- #<?php echo $html_id; ?> .navigation -->
       
   254 	<?php endif;
       
   255 }
       
   256 endif;
       
   257 
       
   258 if ( ! function_exists( 'twentytwelve_comment' ) ) :
       
   259 /**
       
   260  * Template for comments and pingbacks.
       
   261  *
       
   262  * To override this walker in a child theme without modifying the comments template
       
   263  * simply create your own twentytwelve_comment(), and that function will be used instead.
       
   264  *
       
   265  * Used as a callback by wp_list_comments() for displaying the comments.
       
   266  *
       
   267  * @since Twenty Twelve 1.0
       
   268  */
       
   269 function twentytwelve_comment( $comment, $args, $depth ) {
       
   270 	$GLOBALS['comment'] = $comment;
       
   271 	switch ( $comment->comment_type ) :
       
   272 		case 'pingback' :
       
   273 		case 'trackback' :
       
   274 		// Display trackbacks differently than normal comments.
       
   275 	?>
       
   276 	<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
       
   277 		<p><?php _e( 'Pingback:', 'twentytwelve' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></p>
       
   278 	<?php
       
   279 			break;
       
   280 		default :
       
   281 		// Proceed with normal comments.
       
   282 		global $post;
       
   283 	?>
       
   284 	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
       
   285 		<article id="comment-<?php comment_ID(); ?>" class="comment">
       
   286 			<header class="comment-meta comment-author vcard">
       
   287 				<?php
       
   288 					echo get_avatar( $comment, 44 );
       
   289 					printf( '<cite class="fn">%1$s %2$s</cite>',
       
   290 						get_comment_author_link(),
       
   291 						// If current post author is also comment author, make it known visually.
       
   292 						( $comment->user_id === $post->post_author ) ? '<span> ' . __( 'Post author', 'twentytwelve' ) . '</span>' : ''
       
   293 					);
       
   294 					printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
       
   295 						esc_url( get_comment_link( $comment->comment_ID ) ),
       
   296 						get_comment_time( 'c' ),
       
   297 						/* translators: 1: date, 2: time */
       
   298 						sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
       
   299 					);
       
   300 				?>
       
   301 			</header><!-- .comment-meta -->
       
   302 
       
   303 			<?php if ( '0' == $comment->comment_approved ) : ?>
       
   304 				<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwelve' ); ?></p>
       
   305 			<?php endif; ?>
       
   306 
       
   307 			<section class="comment-content comment">
       
   308 				<?php comment_text(); ?>
       
   309 				<?php edit_comment_link( __( 'Edit', 'twentytwelve' ), '<p class="edit-link">', '</p>' ); ?>
       
   310 			</section><!-- .comment-content -->
       
   311 
       
   312 			<div class="reply">
       
   313 				<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'twentytwelve' ), 'after' => ' <span>&darr;</span>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
       
   314 			</div><!-- .reply -->
       
   315 		</article><!-- #comment-## -->
       
   316 	<?php
       
   317 		break;
       
   318 	endswitch; // end comment_type check
       
   319 }
       
   320 endif;
       
   321 
       
   322 if ( ! function_exists( 'twentytwelve_entry_meta' ) ) :
       
   323 /**
       
   324  * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
       
   325  *
       
   326  * Create your own twentytwelve_entry_meta() to override in a child theme.
       
   327  *
       
   328  * @since Twenty Twelve 1.0
       
   329  */
       
   330 function twentytwelve_entry_meta() {
       
   331 	// Translators: used between list items, there is a space after the comma.
       
   332 	$categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) );
       
   333 
       
   334 	// Translators: used between list items, there is a space after the comma.
       
   335 	$tag_list = get_the_tag_list( '', __( ', ', 'twentytwelve' ) );
       
   336 
       
   337 	$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
       
   338 		esc_url( get_permalink() ),
       
   339 		esc_attr( get_the_time() ),
       
   340 		esc_attr( get_the_date( 'c' ) ),
       
   341 		esc_html( get_the_date() )
       
   342 	);
       
   343 
       
   344 	$author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
       
   345 		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
       
   346 		esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
       
   347 		get_the_author()
       
   348 	);
       
   349 
       
   350 	// Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
       
   351 	if ( $tag_list ) {
       
   352 		$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' );
       
   353 	} elseif ( $categories_list ) {
       
   354 		$utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
       
   355 	} else {
       
   356 		$utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
       
   357 	}
       
   358 
       
   359 	printf(
       
   360 		$utility_text,
       
   361 		$categories_list,
       
   362 		$tag_list,
       
   363 		$date,
       
   364 		$author
       
   365 	);
       
   366 }
       
   367 endif;
       
   368 
       
   369 /**
       
   370  * Extends the default WordPress body class to denote:
       
   371  * 1. Using a full-width layout, when no active widgets in the sidebar
       
   372  *    or full-width template.
       
   373  * 2. Front Page template: thumbnail in use and number of sidebars for
       
   374  *    widget areas.
       
   375  * 3. White or empty background color to change the layout and spacing.
       
   376  * 4. Custom fonts enabled.
       
   377  * 5. Single or multiple authors.
       
   378  *
       
   379  * @since Twenty Twelve 1.0
       
   380  *
       
   381  * @param array Existing class values.
       
   382  * @return array Filtered class values.
       
   383  */
       
   384 function twentytwelve_body_class( $classes ) {
       
   385 	$background_color = get_background_color();
       
   386 
       
   387 	if ( ! is_active_sidebar( 'sidebar-1' ) || is_page_template( 'page-templates/full-width.php' ) )
       
   388 		$classes[] = 'full-width';
       
   389 
       
   390 	if ( is_page_template( 'page-templates/front-page.php' ) ) {
       
   391 		$classes[] = 'template-front-page';
       
   392 		if ( has_post_thumbnail() )
       
   393 			$classes[] = 'has-post-thumbnail';
       
   394 		if ( is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) )
       
   395 			$classes[] = 'two-sidebars';
       
   396 	}
       
   397 
       
   398 	if ( empty( $background_color ) )
       
   399 		$classes[] = 'custom-background-empty';
       
   400 	elseif ( in_array( $background_color, array( 'fff', 'ffffff' ) ) )
       
   401 		$classes[] = 'custom-background-white';
       
   402 
       
   403 	// Enable custom font class only if the font CSS is queued to load.
       
   404 	if ( wp_style_is( 'twentytwelve-fonts', 'queue' ) )
       
   405 		$classes[] = 'custom-font-enabled';
       
   406 
       
   407 	if ( ! is_multi_author() )
       
   408 		$classes[] = 'single-author';
       
   409 
       
   410 	return $classes;
       
   411 }
       
   412 add_filter( 'body_class', 'twentytwelve_body_class' );
       
   413 
       
   414 /**
       
   415  * Adjusts content_width value for full-width and single image attachment
       
   416  * templates, and when there are no active widgets in the sidebar.
       
   417  *
       
   418  * @since Twenty Twelve 1.0
       
   419  */
       
   420 function twentytwelve_content_width() {
       
   421 	if ( is_page_template( 'page-templates/full-width.php' ) || is_attachment() || ! is_active_sidebar( 'sidebar-1' ) ) {
       
   422 		global $content_width;
       
   423 		$content_width = 960;
       
   424 	}
       
   425 }
       
   426 add_action( 'template_redirect', 'twentytwelve_content_width' );
       
   427 
       
   428 /**
       
   429  * Add postMessage support for site title and description for the Theme Customizer.
       
   430  *
       
   431  * @since Twenty Twelve 1.0
       
   432  *
       
   433  * @param WP_Customize_Manager $wp_customize Theme Customizer object.
       
   434  * @return void
       
   435  */
       
   436 function twentytwelve_customize_register( $wp_customize ) {
       
   437 	$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
       
   438 	$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
       
   439 }
       
   440 add_action( 'customize_register', 'twentytwelve_customize_register' );
       
   441 
       
   442 /**
       
   443  * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
       
   444  *
       
   445  * @since Twenty Twelve 1.0
       
   446  */
       
   447 function twentytwelve_customize_preview_js() {
       
   448 	wp_enqueue_script( 'twentytwelve-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20120827', true );
       
   449 }
       
   450 add_action( 'customize_preview_init', 'twentytwelve_customize_preview_js' );