functions.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * Seedlet functions and definitions
  5. *
  6. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  7. *
  8. * @package Seedlet
  9. * @since 1.1.0
  10. */
  11. /**
  12. * seedlet 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( 'seedlet_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 seedlet_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 seedlet, use a find and replace
  31. * to change 'seedlet' to the name of your theme in all the template files.
  32. */
  33. load_theme_textdomain( 'seedlet', 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. 'primary' => __( 'Primary Navigation', 'seedlet' ),
  54. 'footer' => __( 'Footer Navigation', 'seedlet' ),
  55. 'social' => __( 'Social Links Navigation', 'seedlet' ),
  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. 'style',
  71. 'script',
  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' => 240,
  84. 'width' => 240,
  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. $editor_stylesheet_path = './assets/css/style-editor.css';
  98. // Enqueue editor styles.
  99. add_editor_style(
  100. array(
  101. seedlet_fonts_url(),
  102. './assets/css/style-editor.css',
  103. )
  104. );
  105. // Add custom editor font sizes.
  106. add_theme_support(
  107. 'editor-font-sizes',
  108. array(
  109. array(
  110. 'name' => __( 'Tiny', 'seedlet' ),
  111. 'shortName' => __( 'XS', 'seedlet' ),
  112. 'size' => 14,
  113. 'slug' => 'tiny',
  114. ),
  115. array(
  116. 'name' => __( 'Small', 'seedlet' ),
  117. 'shortName' => __( 'S', 'seedlet' ),
  118. 'size' => 16,
  119. 'slug' => 'small',
  120. ),
  121. array(
  122. 'name' => __( 'Normal', 'seedlet' ),
  123. 'shortName' => __( 'M', 'seedlet' ),
  124. 'size' => 18,
  125. 'slug' => 'normal',
  126. ),
  127. array(
  128. 'name' => __( 'Large', 'seedlet' ),
  129. 'shortName' => __( 'L', 'seedlet' ),
  130. 'size' => 24,
  131. 'slug' => 'large',
  132. ),
  133. array(
  134. 'name' => __( 'Huge', 'seedlet' ),
  135. 'shortName' => __( 'XL', 'seedlet' ),
  136. 'size' => 28,
  137. 'slug' => 'huge',
  138. ),
  139. )
  140. );
  141. // Editor color palette.
  142. $colors_theme_mod = get_theme_mod( 'custom_colors_active' );
  143. $primary = ( ! empty( $colors_theme_mod ) && 'default' === $colors_theme_mod || empty( get_theme_mod( 'seedlet_--global--color-primary' ) ) ) ? '#000000' : get_theme_mod( 'seedlet_--global--color-primary' );
  144. $secondary = ( ! empty( $colors_theme_mod ) && 'default' === $colors_theme_mod || empty( get_theme_mod( 'seedlet_--global--color-secondary' ) ) ) ? '#3C8067' : get_theme_mod( 'seedlet_--global--color-secondary' );
  145. $foreground = ( ! empty( $colors_theme_mod ) && 'default' === $colors_theme_mod || empty( get_theme_mod( 'seedlet_--global--color-foreground' ) ) ) ? '#333333' : get_theme_mod( 'seedlet_--global--color-foreground' );
  146. $tertiary = ( ! empty( $colors_theme_mod ) && 'default' === $colors_theme_mod || empty( get_theme_mod( 'seedlet_--global--color-tertiary' ) ) ) ? '#FAFBF6' : get_theme_mod( 'seedlet_--global--color-tertiary' );
  147. $background = ( ! empty( $colors_theme_mod ) && 'default' === $colors_theme_mod || empty( get_theme_mod( 'seedlet_--global--color-background' ) ) ) ? '#FFFFFF' : get_theme_mod( 'seedlet_--global--color-background' );
  148. add_theme_support(
  149. 'editor-color-palette',
  150. array(
  151. array(
  152. 'name' => __( 'Primary', 'seedlet' ),
  153. 'slug' => 'primary',
  154. 'color' => $primary,
  155. ),
  156. array(
  157. 'name' => __( 'Secondary', 'seedlet' ),
  158. 'slug' => 'secondary',
  159. 'color' => $secondary,
  160. ),
  161. array(
  162. 'name' => __( 'Foreground', 'seedlet' ),
  163. 'slug' => 'foreground',
  164. 'color' => $foreground,
  165. ),
  166. array(
  167. 'name' => __( 'Tertiary', 'seedlet' ),
  168. 'slug' => 'tertiary',
  169. 'color' => $tertiary,
  170. ),
  171. array(
  172. 'name' => __( 'Background', 'seedlet' ),
  173. 'slug' => 'background',
  174. 'color' => $background,
  175. ),
  176. )
  177. );
  178. $gradient_color_a = $secondary;
  179. $gradient_color_b = $tertiary;
  180. add_theme_support(
  181. 'editor-gradient-presets',
  182. array(
  183. array(
  184. 'name' => __( 'Diagonal', 'seedlet' ),
  185. 'gradient' => 'linear-gradient(to bottom right, ' . $gradient_color_a . ' 49.9%, ' . $gradient_color_b . ' 50%)',
  186. 'slug' => 'hard-diagonal',
  187. ),
  188. array(
  189. 'name' => __( 'Diagonal inverted', 'seedlet' ),
  190. 'gradient' => 'linear-gradient(to top left, ' . $gradient_color_a . ' 49.9%, ' . $gradient_color_b . ' 50%)',
  191. 'slug' => 'hard-diagonal-inverted',
  192. ),
  193. array(
  194. 'name' => __( 'Horizontal', 'seedlet' ),
  195. 'gradient' => 'linear-gradient(to bottom, ' . $gradient_color_a . ' 50%, ' . $gradient_color_b . ' 50%)',
  196. 'slug' => 'hard-horizontal',
  197. ),
  198. array(
  199. 'name' => __( 'Horizontal inverted', 'seedlet' ),
  200. 'gradient' => 'linear-gradient(to top, ' . $gradient_color_a . ' 50%, ' . $gradient_color_b . ' 50%)',
  201. 'slug' => 'hard-horizontal-inverted',
  202. ),
  203. array(
  204. 'name' => __( 'Diagonal gradient', 'seedlet' ),
  205. 'gradient' => 'linear-gradient(to bottom right, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  206. 'slug' => 'diagonal',
  207. ),
  208. array(
  209. 'name' => __( 'Diagonal inverted gradient', 'seedlet' ),
  210. 'gradient' => 'linear-gradient(to top left, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  211. 'slug' => 'diagonal-inverted',
  212. ),
  213. array(
  214. 'name' => __( 'Horizontal gradient', 'seedlet' ),
  215. 'gradient' => 'linear-gradient(to bottom, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  216. 'slug' => 'horizontal',
  217. ),
  218. array(
  219. 'name' => __( 'Horizontal inverted gradient', 'seedlet' ),
  220. 'gradient' => 'linear-gradient(to top, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  221. 'slug' => 'horizontal-inverted',
  222. ),
  223. array(
  224. 'name' => __( 'Stripe', 'seedlet' ),
  225. 'gradient' => 'linear-gradient(to bottom, transparent 20%, ' . $gradient_color_a . ' 20%, ' . $gradient_color_a . ' 80%, transparent 80%)',
  226. 'slug' => 'stripe',
  227. ),
  228. )
  229. );
  230. // Add support for responsive embedded content.
  231. add_theme_support( 'responsive-embeds' );
  232. // Add support for custom line height controls.
  233. add_theme_support( 'custom-line-height' );
  234. // Add support for link color control.
  235. add_theme_support( 'link-color' );
  236. // Add support for experimental cover block spacing.
  237. add_theme_support( 'custom-spacing' );
  238. // Add support for custom units.
  239. add_theme_support( 'custom-units' );
  240. // Add support for WordPress.com Global Styles.
  241. add_theme_support(
  242. 'jetpack-global-styles',
  243. array(
  244. 'enable_theme_default' => true,
  245. )
  246. );
  247. }
  248. endif;
  249. add_action( 'after_setup_theme', 'seedlet_setup' );
  250. /**
  251. * Add Google webfonts, if necessary
  252. *
  253. * - See: http://themeshaper.com/2014/08/13/how-to-add-google-fonts-to-wordpress-themes/
  254. */
  255. function seedlet_fonts_url() {
  256. $fonts_url = '';
  257. /* Translators: If there are characters in your language that are not
  258. * supported by Fira Sans, translate this to 'off'. Do not translate
  259. * into your own language.
  260. */
  261. $fira_sans = esc_html_x( 'on', 'Fira Sans: on or off', 'seedlet' );
  262. /* Translators: If there are characters in your language that are not
  263. * supported by Playfair Display, translate this to 'off'. Do not translate
  264. * into your own language.
  265. */
  266. $playfair_display = esc_html_x( 'on', 'Playfair Display: on or off', 'seedlet' );
  267. if ( 'off' !== $fira_sans || 'off' !== $playfair_display ) {
  268. $font_families = array();
  269. if ( 'off' !== $fira_sans ) {
  270. $font_families[] = 'Fira Sans:ital,wght@0,400;0,500;1,400';
  271. }
  272. if ( 'off' !== $playfair_display ) {
  273. $font_families[] = 'Playfair Display:ital,wght@0,400;0,700;1,400';
  274. }
  275. /**
  276. * A filter to enable child themes to add/change/omit font families.
  277. *
  278. * @param array $font_families An array of font families to be imploded for the Google Font API
  279. */
  280. $font_families = apply_filters( 'included_google_font_families', $font_families );
  281. $query_args = array(
  282. 'family' => urlencode( implode( '|', $font_families ) ),
  283. 'subset' => urlencode( 'latin,latin-ext' ),
  284. );
  285. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  286. }
  287. return esc_url_raw( $fonts_url );
  288. }
  289. /**
  290. * Register widget area.
  291. *
  292. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  293. */
  294. function seedlet_widgets_init() {
  295. register_sidebar(
  296. array(
  297. 'name' => __( 'Footer', 'seedlet' ),
  298. 'id' => 'sidebar-1',
  299. 'description' => __( 'Add widgets here to appear in your footer.', 'seedlet' ),
  300. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  301. 'after_widget' => '</section>',
  302. 'before_title' => '<h2 class="widget-title">',
  303. 'after_title' => '</h2>',
  304. )
  305. );
  306. }
  307. add_action( 'widgets_init', 'seedlet_widgets_init' );
  308. /**
  309. * Set the content width in pixels, based on the theme's design and stylesheet.
  310. *
  311. * Priority 0 to make it available to lower priority callbacks.
  312. *
  313. * @global int $content_width Content width.
  314. */
  315. function seedlet_content_width() {
  316. // This variable is intended to be overruled from themes.
  317. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  318. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  319. $GLOBALS['content_width'] = apply_filters( 'seedlet_content_width', 620 );
  320. }
  321. add_action( 'after_setup_theme', 'seedlet_content_width', 0 );
  322. /**
  323. * Enqueue scripts and styles.
  324. */
  325. function seedlet_scripts() {
  326. // Enqueue Google fonts
  327. wp_enqueue_style( 'seedlet-fonts', seedlet_fonts_url(), array(), null );
  328. // Theme styles
  329. wp_enqueue_style( 'seedlet-style', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) );
  330. // Navigation styles
  331. wp_enqueue_style( 'seedlet-style-navigation', get_template_directory_uri() . '/assets/css/style-navigation.css', array(), wp_get_theme()->get( 'Version' ) );
  332. // RTL styles
  333. wp_style_add_data( 'seedlet-style', 'rtl', 'replace' );
  334. wp_style_add_data( 'seedlet-style-navigation', 'rtl', 'replace' );
  335. // Print styles
  336. wp_enqueue_style( 'seedlet-print-style', get_template_directory_uri() . '/assets/css/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  337. // Threaded comment reply styles
  338. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  339. wp_enqueue_script( 'comment-reply' );
  340. }
  341. // Main navigation scripts
  342. wp_enqueue_script( 'seedlet-primary-navigation-script', get_template_directory_uri() . '/assets/js/primary-navigation.js', array(), wp_get_theme()->get( 'Version' ), true );
  343. // Note, the is_IE global variable is defined by WordPress and is used
  344. // to detect if the current browser is internet explorer.
  345. global $is_IE;
  346. if ( $is_IE ) {
  347. // If IE 11 or below, use a ponyfill to add CSS Variable support
  348. wp_register_script( 'css-vars-ponyfill', get_template_directory_uri() . '/assets/js/css-vars-ponyfill2.js' );
  349. wp_enqueue_script(
  350. 'ie11-fix',
  351. get_template_directory_uri() . '/assets/js/ie11-fix.js',
  352. array( 'css-vars-ponyfill' ),
  353. '1.0'
  354. );
  355. }
  356. }
  357. add_action( 'wp_enqueue_scripts', 'seedlet_scripts' );
  358. /**
  359. * Fix skip link focus in IE11.
  360. *
  361. * This does not enqueue the script because it is tiny and because it is only for IE11,
  362. * thus it does not warrant having an entire dedicated blocking script being loaded.
  363. *
  364. * @link https://git.io/vWdr2
  365. */
  366. function seedlet_skip_link_focus_fix() {
  367. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  368. ?>
  369. <script>
  370. /(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);
  371. </script>
  372. <?php
  373. }
  374. add_action( 'wp_print_footer_scripts', 'seedlet_skip_link_focus_fix' );
  375. if ( ! function_exists( 'seedlet_author_bio' ) ) {
  376. /**
  377. * Implements the Jetpack Author bio
  378. */
  379. function seedlet_author_bio() {
  380. if ( ! function_exists( 'jetpack_author_bio' ) ) {
  381. if ( ! is_singular( 'attachment' ) ) {
  382. get_template_part( 'template-parts/post/author-bio' );
  383. }
  384. } else {
  385. jetpack_author_bio();
  386. }
  387. }
  388. }
  389. /**
  390. * SVG Icons class.
  391. */
  392. require get_template_directory() . '/classes/class-seedlet-svg-icons.php';
  393. /**
  394. * Custom colors class.
  395. */
  396. if ( ! class_exists( 'Colors_Manager' ) ) { // Check for presence of wpcom color manager to avoid duplicate color customization functionality.
  397. require get_template_directory() . '/classes/class-seedlet-custom-colors.php';
  398. }
  399. /**
  400. * Enhance the theme by hooking into WordPress.
  401. */
  402. require get_template_directory() . '/inc/template-functions.php';
  403. /**
  404. * SVG Icons related functions.
  405. */
  406. require get_template_directory() . '/inc/icon-functions.php';
  407. /**
  408. * Custom template tags for the theme.
  409. */
  410. require get_template_directory() . '/inc/template-tags.php';
  411. /**
  412. * Customizer additions.
  413. */
  414. require get_template_directory() . '/inc/customizer.php';
  415. /**
  416. * Block Patterns.
  417. */
  418. require get_template_directory() . '/inc/block-patterns.php';
  419. /**
  420. * Block Styles.
  421. */
  422. require get_template_directory() . '/inc/block-styles.php';
  423. /**
  424. * Load WooCommerce compatibility file.
  425. */
  426. if ( class_exists( 'WooCommerce' ) ) {
  427. require get_template_directory() . '/inc/woocommerce.php';
  428. }
  429. /**
  430. * Load Jetpack compatibility file.
  431. */
  432. require get_template_directory() . '/inc/jetpack.php';