Няма описание

functions.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * Twenty Nineteen functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package WordPress
  8. * @subpackage Twenty_Nineteen
  9. * @since Twenty Nineteen 1.0
  10. */
  11. /**
  12. * Twenty Nineteen only works in WordPress 4.7 or later.
  13. */
  14. if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
  15. require get_template_directory() . '/inc/back-compat.php';
  16. return;
  17. }
  18. if ( ! function_exists( 'twentynineteen_setup' ) ) :
  19. /**
  20. * Sets up theme defaults and registers support for various WordPress features.
  21. *
  22. * Note that this function is hooked into the after_setup_theme hook, which
  23. * runs before the init hook. The init hook is too late for some features, such
  24. * as indicating support for post thumbnails.
  25. */
  26. function twentynineteen_setup() {
  27. /*
  28. * Make theme available for translation.
  29. * Translations can be filed in the /languages/ directory.
  30. * If you're building a theme based on Twenty Nineteen, use a find and replace
  31. * to change 'twentynineteen' to the name of your theme in all the template files.
  32. */
  33. load_theme_textdomain( 'twentynineteen', get_template_directory() . '/languages' );
  34. // Add default posts and comments RSS feed links to head.
  35. add_theme_support( 'automatic-feed-links' );
  36. /*
  37. * Let WordPress manage the document title.
  38. * By adding theme support, we declare that this theme does not use a
  39. * hard-coded <title> tag in the document head, and expect WordPress to
  40. * provide it for us.
  41. */
  42. add_theme_support( 'title-tag' );
  43. /*
  44. * Enable support for Post Thumbnails on posts and pages.
  45. *
  46. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  47. */
  48. add_theme_support( 'post-thumbnails' );
  49. set_post_thumbnail_size( 1568, 9999 );
  50. // This theme uses wp_nav_menu() in two locations.
  51. register_nav_menus(
  52. array(
  53. 'menu-1' => __( 'Primary', 'twentynineteen' ),
  54. 'footer' => __( 'Footer Menu', 'twentynineteen' ),
  55. 'social' => __( 'Social Links Menu', 'twentynineteen' ),
  56. )
  57. );
  58. /*
  59. * Switch default core markup for search form, comment form, and comments
  60. * to output valid HTML5.
  61. */
  62. add_theme_support(
  63. 'html5',
  64. array(
  65. 'search-form',
  66. 'comment-form',
  67. 'comment-list',
  68. 'gallery',
  69. 'caption',
  70. 'script',
  71. 'style',
  72. 'navigation-widgets',
  73. )
  74. );
  75. /**
  76. * Add support for core custom logo.
  77. *
  78. * @link https://codex.wordpress.org/Theme_Logo
  79. */
  80. add_theme_support(
  81. 'custom-logo',
  82. array(
  83. 'height' => 190,
  84. 'width' => 190,
  85. 'flex-width' => false,
  86. 'flex-height' => false,
  87. )
  88. );
  89. // Add theme support for selective refresh for widgets.
  90. add_theme_support( 'customize-selective-refresh-widgets' );
  91. // Add support for Block Styles.
  92. add_theme_support( 'wp-block-styles' );
  93. // Add support for full and wide align images.
  94. add_theme_support( 'align-wide' );
  95. // Add support for editor styles.
  96. add_theme_support( 'editor-styles' );
  97. // Enqueue editor styles.
  98. add_editor_style( 'style-editor.css' );
  99. // Add custom editor font sizes.
  100. add_theme_support(
  101. 'editor-font-sizes',
  102. array(
  103. array(
  104. 'name' => __( 'Small', 'twentynineteen' ),
  105. 'shortName' => __( 'S', 'twentynineteen' ),
  106. 'size' => 19.5,
  107. 'slug' => 'small',
  108. ),
  109. array(
  110. 'name' => __( 'Normal', 'twentynineteen' ),
  111. 'shortName' => __( 'M', 'twentynineteen' ),
  112. 'size' => 22,
  113. 'slug' => 'normal',
  114. ),
  115. array(
  116. 'name' => __( 'Large', 'twentynineteen' ),
  117. 'shortName' => __( 'L', 'twentynineteen' ),
  118. 'size' => 36.5,
  119. 'slug' => 'large',
  120. ),
  121. array(
  122. 'name' => __( 'Huge', 'twentynineteen' ),
  123. 'shortName' => __( 'XL', 'twentynineteen' ),
  124. 'size' => 49.5,
  125. 'slug' => 'huge',
  126. ),
  127. )
  128. );
  129. // Editor color palette.
  130. add_theme_support(
  131. 'editor-color-palette',
  132. array(
  133. array(
  134. 'name' => 'default' === get_theme_mod( 'primary_color' ) ? __( 'Blue', 'twentynineteen' ) : null,
  135. 'slug' => 'primary',
  136. 'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 33 ),
  137. ),
  138. array(
  139. 'name' => 'default' === get_theme_mod( 'primary_color' ) ? __( 'Dark Blue', 'twentynineteen' ) : null,
  140. 'slug' => 'secondary',
  141. 'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 23 ),
  142. ),
  143. array(
  144. 'name' => __( 'Dark Gray', 'twentynineteen' ),
  145. 'slug' => 'dark-gray',
  146. 'color' => '#111',
  147. ),
  148. array(
  149. 'name' => __( 'Light Gray', 'twentynineteen' ),
  150. 'slug' => 'light-gray',
  151. 'color' => '#767676',
  152. ),
  153. array(
  154. 'name' => __( 'White', 'twentynineteen' ),
  155. 'slug' => 'white',
  156. 'color' => '#FFF',
  157. ),
  158. )
  159. );
  160. // Add support for responsive embedded content.
  161. add_theme_support( 'responsive-embeds' );
  162. // Add support for custom line height.
  163. add_theme_support( 'custom-line-height' );
  164. }
  165. endif;
  166. add_action( 'after_setup_theme', 'twentynineteen_setup' );
  167. /**
  168. * Register widget area.
  169. *
  170. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  171. */
  172. function twentynineteen_widgets_init() {
  173. register_sidebar(
  174. array(
  175. 'name' => __( 'Footer', 'twentynineteen' ),
  176. 'id' => 'sidebar-1',
  177. 'description' => __( 'Add widgets here to appear in your footer.', 'twentynineteen' ),
  178. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  179. 'after_widget' => '</section>',
  180. 'before_title' => '<h2 class="widget-title">',
  181. 'after_title' => '</h2>',
  182. )
  183. );
  184. }
  185. add_action( 'widgets_init', 'twentynineteen_widgets_init' );
  186. /**
  187. * Replaces "[...]" (appended to automatically generated excerpts) with ... and
  188. * a 'Continue reading' link.
  189. *
  190. * @since Twenty Nineteen 2.0
  191. *
  192. * @param string $link Link to single post/page.
  193. * @return string 'Continue reading' link prepended with an ellipsis.
  194. */
  195. function twentynineteen_excerpt_more( $link ) {
  196. if ( is_admin() ) {
  197. return $link;
  198. }
  199. $link = sprintf(
  200. '<p class="link-more"><a href="%1$s" class="more-link">%2$s</a></p>',
  201. esc_url( get_permalink( get_the_ID() ) ),
  202. /* translators: %s: Post title. */
  203. sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentynineteen' ), get_the_title( get_the_ID() ) )
  204. );
  205. return ' &hellip; ' . $link;
  206. }
  207. add_filter( 'excerpt_more', 'twentynineteen_excerpt_more' );
  208. /**
  209. * Set the content width in pixels, based on the theme's design and stylesheet.
  210. *
  211. * Priority 0 to make it available to lower priority callbacks.
  212. *
  213. * @global int $content_width Content width.
  214. */
  215. function twentynineteen_content_width() {
  216. // This variable is intended to be overruled from themes.
  217. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  218. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  219. $GLOBALS['content_width'] = apply_filters( 'twentynineteen_content_width', 640 );
  220. }
  221. add_action( 'after_setup_theme', 'twentynineteen_content_width', 0 );
  222. /**
  223. * Enqueue scripts and styles.
  224. */
  225. function twentynineteen_scripts() {
  226. wp_enqueue_style( 'twentynineteen-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  227. wp_style_add_data( 'twentynineteen-style', 'rtl', 'replace' );
  228. if ( has_nav_menu( 'menu-1' ) ) {
  229. wp_enqueue_script( 'twentynineteen-priority-menu', get_theme_file_uri( '/js/priority-menu.js' ), array(), '20181214', true );
  230. wp_enqueue_script( 'twentynineteen-touch-navigation', get_theme_file_uri( '/js/touch-keyboard-navigation.js' ), array(), '20181231', true );
  231. }
  232. wp_enqueue_style( 'twentynineteen-print-style', get_template_directory_uri() . '/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  233. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  234. wp_enqueue_script( 'comment-reply' );
  235. }
  236. }
  237. add_action( 'wp_enqueue_scripts', 'twentynineteen_scripts' );
  238. /**
  239. * Fix skip link focus in IE11.
  240. *
  241. * This does not enqueue the script because it is tiny and because it is only for IE11,
  242. * thus it does not warrant having an entire dedicated blocking script being loaded.
  243. *
  244. * @link https://git.io/vWdr2
  245. */
  246. function twentynineteen_skip_link_focus_fix() {
  247. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  248. ?>
  249. <script>
  250. /(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
  251. </script>
  252. <?php
  253. }
  254. add_action( 'wp_print_footer_scripts', 'twentynineteen_skip_link_focus_fix' );
  255. /**
  256. * Enqueue supplemental block editor styles.
  257. */
  258. function twentynineteen_editor_customizer_styles() {
  259. wp_enqueue_style( 'twentynineteen-editor-customizer-styles', get_theme_file_uri( '/style-editor-customizer.css' ), false, '1.1', 'all' );
  260. if ( 'custom' === get_theme_mod( 'primary_color' ) ) {
  261. // Include color patterns.
  262. require_once get_parent_theme_file_path( '/inc/color-patterns.php' );
  263. wp_add_inline_style( 'twentynineteen-editor-customizer-styles', twentynineteen_custom_colors_css() );
  264. }
  265. }
  266. add_action( 'enqueue_block_editor_assets', 'twentynineteen_editor_customizer_styles' );
  267. /**
  268. * Display custom color CSS in customizer and on frontend.
  269. */
  270. function twentynineteen_colors_css_wrap() {
  271. // Only include custom colors in customizer or frontend.
  272. if ( ( ! is_customize_preview() && 'default' === get_theme_mod( 'primary_color', 'default' ) ) || is_admin() ) {
  273. return;
  274. }
  275. require_once get_parent_theme_file_path( '/inc/color-patterns.php' );
  276. $primary_color = 199;
  277. if ( 'default' !== get_theme_mod( 'primary_color', 'default' ) ) {
  278. $primary_color = get_theme_mod( 'primary_color_hue', 199 );
  279. }
  280. ?>
  281. <style type="text/css" id="custom-theme-colors" <?php echo is_customize_preview() ? 'data-hue="' . absint( $primary_color ) . '"' : ''; ?>>
  282. <?php echo twentynineteen_custom_colors_css(); ?>
  283. </style>
  284. <?php
  285. }
  286. add_action( 'wp_head', 'twentynineteen_colors_css_wrap' );
  287. /**
  288. * SVG Icons class.
  289. */
  290. require get_template_directory() . '/classes/class-twentynineteen-svg-icons.php';
  291. /**
  292. * Custom Comment Walker template.
  293. */
  294. require get_template_directory() . '/classes/class-twentynineteen-walker-comment.php';
  295. /**
  296. * Common theme functions.
  297. */
  298. require get_template_directory() . '/inc/helper-functions.php';
  299. /**
  300. * SVG Icons related functions.
  301. */
  302. require get_template_directory() . '/inc/icon-functions.php';
  303. /**
  304. * Enhance the theme by hooking into WordPress.
  305. */
  306. require get_template_directory() . '/inc/template-functions.php';
  307. /**
  308. * Custom template tags for the theme.
  309. */
  310. require get_template_directory() . '/inc/template-tags.php';
  311. /**
  312. * Customizer additions.
  313. */
  314. require get_template_directory() . '/inc/customizer.php';
  315. /**
  316. * Block Patterns.
  317. */
  318. require get_template_directory() . '/inc/block-patterns.php';