WordPress 4.7
[autoinstalls/wordpress.git] / wp-content / themes / twentyseventeen / functions.php
1 <?php
2 /**
3  * Twenty Seventeen functions and definitions
4  *
5  * @link https://developer.wordpress.org/themes/basics/theme-functions/
6  *
7  * @package WordPress
8  * @subpackage Twenty_Seventeen
9  * @since 1.0
10  */
11
12 /**
13  * Twenty Seventeen only works in WordPress 4.7 or later.
14  */
15 if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '<' ) ) {
16         require get_template_directory() . '/inc/back-compat.php';
17         return;
18 }
19
20 /**
21  * Sets up theme defaults and registers support for various WordPress features.
22  *
23  * Note that this function is hooked into the after_setup_theme hook, which
24  * runs before the init hook. The init hook is too late for some features, such
25  * as indicating support for post thumbnails.
26  */
27 function twentyseventeen_setup() {
28         /*
29          * Make theme available for translation.
30          * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentyseventeen
31          * If you're building a theme based on Twenty Seventeen, use a find and replace
32          * to change 'twentyseventeen' to the name of your theme in all the template files.
33          */
34         load_theme_textdomain( 'twentyseventeen' );
35
36         // Add default posts and comments RSS feed links to head.
37         add_theme_support( 'automatic-feed-links' );
38
39         /*
40          * Let WordPress manage the document title.
41          * By adding theme support, we declare that this theme does not use a
42          * hard-coded <title> tag in the document head, and expect WordPress to
43          * provide it for us.
44          */
45         add_theme_support( 'title-tag' );
46
47         /*
48          * Enable support for Post Thumbnails on posts and pages.
49          *
50          * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
51          */
52         add_theme_support( 'post-thumbnails' );
53
54         add_image_size( 'twentyseventeen-featured-image', 2000, 1200, true );
55
56         add_image_size( 'twentyseventeen-thumbnail-avatar', 100, 100, true );
57
58         // This theme uses wp_nav_menu() in two locations.
59         register_nav_menus( array(
60                 'top'    => __( 'Top Menu', 'twentyseventeen' ),
61                 'social' => __( 'Social Links Menu', 'twentyseventeen' ),
62         ) );
63
64         /*
65          * Switch default core markup for search form, comment form, and comments
66          * to output valid HTML5.
67          */
68         add_theme_support( 'html5', array(
69                 'comment-form',
70                 'comment-list',
71                 'gallery',
72                 'caption',
73         ) );
74
75         /*
76          * Enable support for Post Formats.
77          *
78          * See: https://codex.wordpress.org/Post_Formats
79          */
80         add_theme_support( 'post-formats', array(
81                 'aside',
82                 'image',
83                 'video',
84                 'quote',
85                 'link',
86                 'gallery',
87                 'audio',
88         ) );
89
90         // Add theme support for Custom Logo.
91         add_theme_support( 'custom-logo', array(
92                 'width'       => 250,
93                 'height'      => 250,
94                 'flex-width'  => true,
95         ) );
96
97         // Add theme support for selective refresh for widgets.
98         add_theme_support( 'customize-selective-refresh-widgets' );
99
100         /*
101          * This theme styles the visual editor to resemble the theme style,
102          * specifically font, colors, and column width.
103          */
104         add_editor_style( array( 'assets/css/editor-style.css', twentyseventeen_fonts_url() ) );
105
106         add_theme_support( 'starter-content', array(
107                 'widgets' => array(
108                         'sidebar-1' => array(
109                                 'text_business_info',
110                                 'search',
111                                 'text_about',
112                         ),
113
114                         'sidebar-2' => array(
115                                 'text_business_info',
116                         ),
117
118                         'sidebar-3' => array(
119                                 'text_about',
120                                 'search',
121                         ),
122                 ),
123
124                 'posts' => array(
125                         'home',
126                         'about' => array(
127                                 'thumbnail' => '{{image-sandwich}}',
128                         ),
129                         'contact' => array(
130                                 'thumbnail' => '{{image-espresso}}',
131                         ),
132                         'blog' => array(
133                                 'thumbnail' => '{{image-coffee}}',
134                         ),
135                         'homepage-section' => array(
136                                 'thumbnail' => '{{image-espresso}}',
137                         ),
138                 ),
139
140                 'attachments' => array(
141                         'image-espresso' => array(
142                                 'post_title' => _x( 'Espresso', 'Theme starter content', 'twentyseventeen' ),
143                                 'file' => 'assets/images/espresso.jpg',
144                         ),
145                         'image-sandwich' => array(
146                                 'post_title' => _x( 'Sandwich', 'Theme starter content', 'twentyseventeen' ),
147                                 'file' => 'assets/images/sandwich.jpg',
148                         ),
149                         'image-coffee' => array(
150                                 'post_title' => _x( 'Coffee', 'Theme starter content', 'twentyseventeen' ),
151                                 'file' => 'assets/images/coffee.jpg',
152                         ),
153                 ),
154
155                 'options' => array(
156                         'show_on_front' => 'page',
157                         'page_on_front' => '{{home}}',
158                         'page_for_posts' => '{{blog}}',
159                 ),
160
161                 'theme_mods' => array(
162                         'panel_1' => '{{homepage-section}}',
163                         'panel_2' => '{{about}}',
164                         'panel_3' => '{{blog}}',
165                         'panel_4' => '{{contact}}',
166                 ),
167
168                 'nav_menus' => array(
169                         'top' => array(
170                                 'name' => __( 'Top Menu', 'twentyseventeen' ),
171                                 'items' => array(
172                                         'page_home',
173                                         'page_about',
174                                         'page_blog',
175                                         'page_contact',
176                                 ),
177                         ),
178                         'social' => array(
179                                 'name' => __( 'Social Links Menu', 'twentyseventeen' ),
180                                 'items' => array(
181                                         'link_yelp',
182                                         'link_facebook',
183                                         'link_twitter',
184                                         'link_instagram',
185                                         'link_email',
186                                 ),
187                         ),
188                 ),
189         ) );
190 }
191 add_action( 'after_setup_theme', 'twentyseventeen_setup' );
192
193 /**
194  * Set the content width in pixels, based on the theme's design and stylesheet.
195  *
196  * Priority 0 to make it available to lower priority callbacks.
197  *
198  * @global int $content_width
199  */
200 function twentyseventeen_content_width() {
201
202         $content_width = 700;
203
204         if ( twentyseventeen_is_frontpage() ) {
205                 $content_width = 1120;
206         }
207
208         /**
209          * Filter Twenty Seventeen content width of the theme.
210          *
211          * @since Twenty Seventeen 1.0
212          *
213          * @param $content_width integer
214          */
215         $GLOBALS['content_width'] = apply_filters( 'twentyseventeen_content_width', $content_width );
216 }
217 add_action( 'after_setup_theme', 'twentyseventeen_content_width', 0 );
218
219 /**
220  * Register custom fonts.
221  */
222 function twentyseventeen_fonts_url() {
223         $fonts_url = '';
224
225         /**
226          * Translators: If there are characters in your language that are not
227          * supported by Libre Franklin, translate this to 'off'. Do not translate
228          * into your own language.
229          */
230         $libre_franklin = _x( 'on', 'Libre Franklin font: on or off', 'twentyseventeen' );
231
232         if ( 'off' !== $libre_franklin ) {
233                 $font_families = array();
234
235                 $font_families[] = 'Libre Franklin:300,300i,400,400i,600,600i,800,800i';
236
237                 $query_args = array(
238                         'family' => urlencode( implode( '|', $font_families ) ),
239                         'subset' => urlencode( 'latin,latin-ext' ),
240                 );
241
242                 $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
243         }
244
245         return esc_url_raw( $fonts_url );
246 }
247
248 /**
249  * Add preconnect for Google Fonts.
250  *
251  * @since Twenty Seventeen 1.0
252  *
253  * @param array  $urls           URLs to print for resource hints.
254  * @param string $relation_type  The relation type the URLs are printed.
255  * @return array $urls           URLs to print for resource hints.
256  */
257 function twentyseventeen_resource_hints( $urls, $relation_type ) {
258         if ( wp_style_is( 'twentyseventeen-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
259                 $urls[] = array(
260                         'href' => 'https://fonts.gstatic.com',
261                         'crossorigin',
262                 );
263         }
264
265         return $urls;
266 }
267 add_filter( 'wp_resource_hints', 'twentyseventeen_resource_hints', 10, 2 );
268
269 /**
270  * Register widget area.
271  *
272  * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
273  */
274 function twentyseventeen_widgets_init() {
275         register_sidebar( array(
276                 'name'          => __( 'Sidebar', 'twentyseventeen' ),
277                 'id'            => 'sidebar-1',
278                 'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentyseventeen' ),
279                 'before_widget' => '<section id="%1$s" class="widget %2$s">',
280                 'after_widget'  => '</section>',
281                 'before_title'  => '<h2 class="widget-title">',
282                 'after_title'   => '</h2>',
283         ) );
284
285         register_sidebar( array(
286                 'name'          => __( 'Footer 1', 'twentyseventeen' ),
287                 'id'            => 'sidebar-2',
288                 'description'   => __( 'Add widgets here to appear in your footer.', 'twentyseventeen' ),
289                 'before_widget' => '<section id="%1$s" class="widget %2$s">',
290                 'after_widget'  => '</section>',
291                 'before_title'  => '<h2 class="widget-title">',
292                 'after_title'   => '</h2>',
293         ) );
294
295         register_sidebar( array(
296                 'name'          => __( 'Footer 2', 'twentyseventeen' ),
297                 'id'            => 'sidebar-3',
298                 'description'   => __( 'Add widgets here to appear in your footer.', 'twentyseventeen' ),
299                 'before_widget' => '<section id="%1$s" class="widget %2$s">',
300                 'after_widget'  => '</section>',
301                 'before_title'  => '<h2 class="widget-title">',
302                 'after_title'   => '</h2>',
303         ) );
304 }
305 add_action( 'widgets_init', 'twentyseventeen_widgets_init' );
306
307 /**
308  * Replaces "[...]" (appended to automatically generated excerpts) with ... and
309  * a 'Continue reading' link.
310  *
311  * @since Twenty Seventeen 1.0
312  *
313  * @return string 'Continue reading' link prepended with an ellipsis.
314  */
315 function twentyseventeen_excerpt_more( $link ) {
316         if ( is_admin() ) {
317                 return $link;
318         }
319
320         $link = sprintf( '<p class="link-more"><a href="%1$s" class="more-link">%2$s</a></p>',
321                 esc_url( get_permalink( get_the_ID() ) ),
322                 /* translators: %s: Name of current post */
323                 sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ), get_the_title( get_the_ID() ) )
324         );
325         return ' &hellip; ' . $link;
326 }
327 add_filter( 'excerpt_more', 'twentyseventeen_excerpt_more' );
328
329 /**
330  * Handles JavaScript detection.
331  *
332  * Adds a `js` class to the root `<html>` element when JavaScript is detected.
333  *
334  * @since Twenty Seventeen 1.0
335  */
336 function twentyseventeen_javascript_detection() {
337         echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
338 }
339 add_action( 'wp_head', 'twentyseventeen_javascript_detection', 0 );
340
341 /**
342  * Add a pingback url auto-discovery header for singularly identifiable articles.
343  */
344 function twentyseventeen_pingback_header() {
345         if ( is_singular() && pings_open() ) {
346                 printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
347         }
348 }
349 add_action( 'wp_head', 'twentyseventeen_pingback_header' );
350
351 /**
352  * Display custom color CSS.
353  */
354 function twentyseventeen_colors_css_wrap() {
355         if ( 'custom' !== get_theme_mod( 'colorscheme' ) && ! is_customize_preview() ) {
356                 return;
357         }
358
359         require_once( get_parent_theme_file_path( '/inc/color-patterns.php' ) );
360         $hue = absint( get_theme_mod( 'colorscheme_hue', 250 ) );
361 ?>
362         <style type="text/css" id="custom-theme-colors" <?php if ( is_customize_preview() ) { echo 'data-hue="' . $hue . '"'; } ?>>
363                 <?php echo twentyseventeen_custom_colors_css(); ?>
364         </style>
365 <?php }
366 add_action( 'wp_head', 'twentyseventeen_colors_css_wrap' );
367
368 /**
369  * Enqueue scripts and styles.
370  */
371 function twentyseventeen_scripts() {
372         // Add custom fonts, used in the main stylesheet.
373         wp_enqueue_style( 'twentyseventeen-fonts', twentyseventeen_fonts_url(), array(), null );
374
375         // Theme stylesheet.
376         wp_enqueue_style( 'twentyseventeen-style', get_stylesheet_uri() );
377
378         // Load the dark colorscheme.
379         if ( 'dark' === get_theme_mod( 'colorscheme', 'light' ) || is_customize_preview() ) {
380                 wp_enqueue_style( 'twentyseventeen-colors-dark', get_theme_file_uri( '/assets/css/colors-dark.css' ), array( 'twentyseventeen-style' ), '1.0' );
381         }
382
383         // Load the Internet Explorer 9 specific stylesheet, to fix display issues in the Customizer.
384         if ( is_customize_preview() ) {
385                 wp_enqueue_style( 'twentyseventeen-ie9', get_theme_file_uri( '/assets/css/ie9.css' ), array( 'twentyseventeen-style' ), '1.0' );
386                 wp_style_add_data( 'twentyseventeen-ie9', 'conditional', 'IE 9' );
387         }
388
389         // Load the Internet Explorer 8 specific stylesheet.
390         wp_enqueue_style( 'twentyseventeen-ie8', get_theme_file_uri( '/assets/css/ie8.css' ), array( 'twentyseventeen-style' ), '1.0' );
391         wp_style_add_data( 'twentyseventeen-ie8', 'conditional', 'lt IE 9' );
392
393         // Load the html5 shiv.
394         wp_enqueue_script( 'html5', get_theme_file_uri( '/assets/js/html5.js' ), array(), '3.7.3' );
395         wp_script_add_data( 'html5', 'conditional', 'lt IE 9' );
396
397         wp_enqueue_script( 'twentyseventeen-skip-link-focus-fix', get_theme_file_uri( '/assets/js/skip-link-focus-fix.js' ), array(), '1.0', true );
398
399         $twentyseventeen_l10n = array(
400                 'quote'          => twentyseventeen_get_svg( array( 'icon' => 'quote-right' ) ),
401         );
402
403         if ( has_nav_menu( 'top' ) ) {
404                 wp_enqueue_script( 'twentyseventeen-navigation', get_theme_file_uri( '/assets/js/navigation.js' ), array(), '1.0', true );
405                 $twentyseventeen_l10n['expand']         = __( 'Expand child menu', 'twentyseventeen' );
406                 $twentyseventeen_l10n['collapse']       = __( 'Collapse child menu', 'twentyseventeen' );
407                 $twentyseventeen_l10n['icon']           = twentyseventeen_get_svg( array( 'icon' => 'angle-down', 'fallback' => true ) );
408         }
409
410         wp_enqueue_script( 'twentyseventeen-global', get_theme_file_uri( '/assets/js/global.js' ), array( 'jquery' ), '1.0', true );
411
412         wp_enqueue_script( 'jquery-scrollto', get_theme_file_uri( '/assets/js/jquery.scrollTo.js' ), array( 'jquery' ), '2.1.2', true );
413
414         wp_localize_script( 'twentyseventeen-skip-link-focus-fix', 'twentyseventeenScreenReaderText', $twentyseventeen_l10n );
415
416         if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
417                 wp_enqueue_script( 'comment-reply' );
418         }
419 }
420 add_action( 'wp_enqueue_scripts', 'twentyseventeen_scripts' );
421
422 /**
423  * Add custom image sizes attribute to enhance responsive image functionality
424  * for content images.
425  *
426  * @since Twenty Seventeen 1.0
427  *
428  * @param string $sizes A source size value for use in a 'sizes' attribute.
429  * @param array  $size  Image size. Accepts an array of width and height
430  *                      values in pixels (in that order).
431  * @return string A source size value for use in a content image 'sizes' attribute.
432  */
433 function twentyseventeen_content_image_sizes_attr( $sizes, $size ) {
434         $width = $size[0];
435
436         if ( 740 <= $width ) {
437                 $sizes = '(max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px';
438         }
439
440         if ( is_active_sidebar( 'sidebar-1' ) || is_archive() || is_search() || is_home() || is_page() ) {
441                 if ( ! ( is_page() && 'one-column' === get_theme_mod( 'page_options' ) ) && 767 <= $width ) {
442                          $sizes = '(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px';
443                 }
444         }
445
446         return $sizes;
447 }
448 add_filter( 'wp_calculate_image_sizes', 'twentyseventeen_content_image_sizes_attr', 10, 2 );
449
450 /**
451  * Filter the `sizes` value in the header image markup.
452  *
453  * @since Twenty Seventeen 1.0
454  *
455  * @param string $html   The HTML image tag markup being filtered.
456  * @param object $header The custom header object returned by 'get_custom_header()'.
457  * @param array  $attr   Array of the attributes for the image tag.
458  * @return string The filtered header image HTML.
459  */
460 function twentyseventeen_header_image_tag( $html, $header, $attr ) {
461         if ( isset( $attr['sizes'] ) ) {
462                 $html = str_replace( $attr['sizes'], '100vw', $html );
463         }
464         return $html;
465 }
466 add_filter( 'get_header_image_tag', 'twentyseventeen_header_image_tag', 10, 3 );
467
468 /**
469  * Add custom image sizes attribute to enhance responsive image functionality
470  * for post thumbnails.
471  *
472  * @since Twenty Seventeen 1.0
473  *
474  * @param array $attr       Attributes for the image markup.
475  * @param int   $attachment Image attachment ID.
476  * @param array $size       Registered image size or flat array of height and width dimensions.
477  * @return string A source size value for use in a post thumbnail 'sizes' attribute.
478  */
479 function twentyseventeen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
480         if ( is_archive() || is_search() || is_home() ) {
481                 $attr['sizes'] = '(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px';
482         } else {
483                 $attr['sizes'] = '100vw';
484         }
485
486         return $attr;
487 }
488 add_filter( 'wp_get_attachment_image_attributes', 'twentyseventeen_post_thumbnail_sizes_attr', 10, 3 );
489
490 /**
491  * Use front-page.php when Front page displays is set to a static page.
492  *
493  * @since Twenty Seventeen 1.0
494  *
495  * @param string $template front-page.php.
496  *
497  * @return string The template to be used: blank if is_home() is true (defaults to index.php), else $template.
498  */
499 function twentyseventeen_front_page_template( $template ) {
500         return is_home() ? '' : $template;
501 }
502 add_filter( 'frontpage_template',  'twentyseventeen_front_page_template' );
503
504 /**
505  * Implement the Custom Header feature.
506  */
507 require get_parent_theme_file_path( '/inc/custom-header.php' );
508
509 /**
510  * Custom template tags for this theme.
511  */
512 require get_parent_theme_file_path( '/inc/template-tags.php' );
513
514 /**
515  * Additional features to allow styling of the templates.
516  */
517 require get_parent_theme_file_path( '/inc/template-functions.php' );
518
519 /**
520  * Customizer additions.
521  */
522 require get_parent_theme_file_path( '/inc/customizer.php' );
523
524 /**
525  * SVG icons functions and filters.
526  */
527 require get_parent_theme_file_path( '/inc/icon-functions.php' );