Advertisement
Guest User

Untitled

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