functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /**
  3. * Seedlet functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Seedlet
  8. * @since 1.0.0
  9. */
  10. /**
  11. * seedlet only works in WordPress 4.7 or later.
  12. */
  13. if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
  14. require get_template_directory() . '/inc/back-compat.php';
  15. return;
  16. }
  17. /**
  18. * Determine whether the site is being requested from IE.
  19. */
  20. $is_ie = false;
  21. if ( preg_match( '~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false ) ) {
  22. $is_ie = true;
  23. }
  24. if ( ! function_exists( 'seedlet_setup' ) ) :
  25. /**
  26. * Sets up theme defaults and registers support for various WordPress features.
  27. *
  28. * Note that this function is hooked into the after_setup_theme hook, which
  29. * runs before the init hook. The init hook is too late for some features, such
  30. * as indicating support for post thumbnails.
  31. */
  32. function seedlet_setup() {
  33. /*
  34. * Make theme available for translation.
  35. * Translations can be filed in the /languages/ directory.
  36. * If you're building a theme based on seedlet, use a find and replace
  37. * to change 'seedlet' to the name of your theme in all the template files.
  38. */
  39. load_theme_textdomain( 'seedlet', get_template_directory() . '/languages' );
  40. // Add default posts and comments RSS feed links to head.
  41. add_theme_support( 'automatic-feed-links' );
  42. /*
  43. * Let WordPress manage the document title.
  44. * By adding theme support, we declare that this theme does not use a
  45. * hard-coded <title> tag in the document head, and expect WordPress to
  46. * provide it for us.
  47. */
  48. add_theme_support( 'title-tag' );
  49. /*
  50. * Enable support for Post Thumbnails on posts and pages.
  51. *
  52. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  53. */
  54. add_theme_support( 'post-thumbnails' );
  55. set_post_thumbnail_size( 1568, 9999 );
  56. // This theme uses wp_nav_menu() in two locations.
  57. register_nav_menus(
  58. array(
  59. 'primary' => __( 'Primary Navigation', 'seedlet' ),
  60. 'footer' => __( 'Footer Navigation', 'seedlet' ),
  61. 'social' => __( 'Social Links Navigation', 'seedlet' ),
  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(
  69. 'html5',
  70. array(
  71. 'search-form',
  72. 'comment-form',
  73. 'comment-list',
  74. 'gallery',
  75. 'caption',
  76. 'style',
  77. 'script',
  78. )
  79. );
  80. /**
  81. * Add support for core custom logo.
  82. *
  83. * @link https://codex.wordpress.org/Theme_Logo
  84. */
  85. add_theme_support(
  86. 'custom-logo',
  87. array(
  88. 'height' => 240,
  89. 'width' => 240,
  90. 'flex-width' => false,
  91. 'flex-height' => false,
  92. )
  93. );
  94. // Add theme support for selective refresh for widgets.
  95. add_theme_support( 'customize-selective-refresh-widgets' );
  96. // Add support for Block Styles.
  97. add_theme_support( 'wp-block-styles' );
  98. // Add support for full and wide align images.
  99. add_theme_support( 'align-wide' );
  100. // Add support for editor styles.
  101. add_theme_support( 'editor-styles' );
  102. $editor_stylesheet_path = './assets/css/style-editor.css';
  103. if ( $is_ie ) {
  104. $editor_stylesheet_path = './assets/css/ie-editor.css';
  105. }
  106. // Enqueue editor styles.
  107. add_editor_style(
  108. array(
  109. seedlet_fonts_url(),
  110. $editor_stylesheet_path,
  111. ) );
  112. // Add custom editor font sizes.
  113. add_theme_support(
  114. 'editor-font-sizes',
  115. array(
  116. array(
  117. 'name' => __( 'Tiny', 'seedlet' ),
  118. 'shortName' => __( 'XS', 'seedlet' ),
  119. 'size' => 14,
  120. 'slug' => 'tiny',
  121. ),
  122. array(
  123. 'name' => __( 'Small', 'seedlet' ),
  124. 'shortName' => __( 'S', 'seedlet' ),
  125. 'size' => 16,
  126. 'slug' => 'small',
  127. ),
  128. array(
  129. 'name' => __( 'Normal', 'seedlet' ),
  130. 'shortName' => __( 'M', 'seedlet' ),
  131. 'size' => 18,
  132. 'slug' => 'normal',
  133. ),
  134. array(
  135. 'name' => __( 'Large', 'seedlet' ),
  136. 'shortName' => __( 'L', 'seedlet' ),
  137. 'size' => 24,
  138. 'slug' => 'large',
  139. ),
  140. array(
  141. 'name' => __( 'Huge', 'seedlet' ),
  142. 'shortName' => __( 'XL', 'seedlet' ),
  143. 'size' => 28,
  144. 'slug' => 'huge',
  145. ),
  146. )
  147. );
  148. // Editor color palette.
  149. $colors_theme_mod = get_theme_mod( 'custom_colors_active' );
  150. $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' );
  151. $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' );
  152. $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' );
  153. $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' );
  154. $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' );
  155. add_theme_support(
  156. 'editor-color-palette',
  157. array(
  158. array(
  159. 'name' => __( 'Primary', 'seedlet' ),
  160. 'slug' => 'primary',
  161. 'color' => $primary
  162. ),
  163. array(
  164. 'name' => __( 'Secondary', 'seedlet' ),
  165. 'slug' => 'secondary',
  166. 'color' => $secondary
  167. ),
  168. array(
  169. 'name' => __( 'Foreground', 'seedlet' ),
  170. 'slug' => 'foreground',
  171. 'color' => $foreground
  172. ),
  173. array(
  174. 'name' => __( 'Tertiary', 'seedlet' ),
  175. 'slug' => 'tertiary',
  176. 'color' => $tertiary
  177. ),
  178. array(
  179. 'name' => __( 'Background', 'seedlet' ),
  180. 'slug' => 'background',
  181. 'color' => $background
  182. ),
  183. )
  184. );
  185. $gradient_color_a = $secondary;
  186. $gradient_color_b = $tertiary;
  187. add_theme_support(
  188. 'editor-gradient-presets',
  189. array(
  190. array(
  191. 'name' => __( 'Diagonal', 'seedlet' ),
  192. 'gradient' => 'linear-gradient(to bottom right, ' . $gradient_color_a . ' 49.9%, ' . $gradient_color_b . ' 50%)',
  193. 'slug' => 'hard-diagonal',
  194. ),
  195. array(
  196. 'name' => __( 'Diagonal inverted', 'seedlet' ),
  197. 'gradient' => 'linear-gradient(to top left, ' . $gradient_color_a . ' 49.9%, ' . $gradient_color_b . ' 50%)',
  198. 'slug' => 'hard-diagonal-inverted',
  199. ),
  200. array(
  201. 'name' => __( 'Horizontal', 'seedlet' ),
  202. 'gradient' => 'linear-gradient(to bottom, ' . $gradient_color_a . ' 50%, ' . $gradient_color_b . ' 50%)',
  203. 'slug' => 'hard-horizontal',
  204. ),
  205. array(
  206. 'name' => __( 'Horizontal inverted', 'seedlet' ),
  207. 'gradient' => 'linear-gradient(to top, ' . $gradient_color_a . ' 50%, ' . $gradient_color_b . ' 50%)',
  208. 'slug' => 'hard-horizontal-inverted',
  209. ),
  210. array(
  211. 'name' => __( 'Diagonal gradient', 'seedlet' ),
  212. 'gradient' => 'linear-gradient(to bottom right, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  213. 'slug' => 'diagonal',
  214. ),
  215. array(
  216. 'name' => __( 'Diagonal inverted gradient', 'seedlet' ),
  217. 'gradient' => 'linear-gradient(to top left, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  218. 'slug' => 'diagonal-inverted',
  219. ),
  220. array(
  221. 'name' => __( 'Horizontal gradient', 'seedlet' ),
  222. 'gradient' => 'linear-gradient(to bottom, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  223. 'slug' => 'horizontal',
  224. ),
  225. array(
  226. 'name' => __( 'Horizontal inverted gradient', 'seedlet' ),
  227. 'gradient' => 'linear-gradient(to top, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  228. 'slug' => 'horizontal-inverted',
  229. ),
  230. array(
  231. 'name' => __( 'Stripe', 'seedlet' ),
  232. 'gradient' => 'linear-gradient(to bottom, transparent 20%, ' . $gradient_color_a . ' 20%, ' . $gradient_color_a . ' 80%, transparent 80%)',
  233. 'slug' => 'stripe',
  234. ),
  235. )
  236. );
  237. // Add support for responsive embedded content.
  238. add_theme_support( 'responsive-embeds' );
  239. // Add support for custom line height controls.
  240. add_theme_support( 'custom-line-height' );
  241. // Add support for experimental link color control.
  242. add_theme_support( 'experimental-link-color' );
  243. // Add support for experimental cover block spacing.
  244. add_theme_support( 'experimental-custom-spacing' );
  245. // Add support for WordPress.com Global Styles.
  246. add_theme_support(
  247. 'jetpack-global-styles',
  248. [
  249. 'enable_theme_default' => true,
  250. ]
  251. );
  252. }
  253. endif;
  254. add_action( 'after_setup_theme', 'seedlet_setup' );
  255. /**
  256. * Add Google webfonts, if necessary
  257. *
  258. * - See: http://themeshaper.com/2014/08/13/how-to-add-google-fonts-to-wordpress-themes/
  259. */
  260. function seedlet_fonts_url() {
  261. $fonts_url = '';
  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. $fira_sans = esc_html_x( 'on', 'Fira Sans: on or off', 'seedlet' );
  267. /* Translators: If there are characters in your language that are not
  268. * supported by Roboto Sans, translate this to 'off'. Do not translate
  269. * into your own language.
  270. */
  271. $playfair_display = esc_html_x( 'on', 'Playfair Display: on or off', 'seedlet' );
  272. if ( 'off' !== $fira_sans || 'off' !== $playfair_display ) {
  273. $font_families = array();
  274. if ( 'off' !== $fira_sans ) {
  275. $font_families[] = 'Fira Sans:ital,wght@0,400;0,500;1,400';
  276. }
  277. if ( 'off' !== $playfair_display ) {
  278. $font_families[] = 'Playfair Display:ital,wght@0,400;0,700;1,400';
  279. }
  280. $query_args = array(
  281. 'family' => urlencode( implode( '|', $font_families ) ),
  282. 'subset' => urlencode( 'latin,latin-ext' ),
  283. );
  284. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  285. }
  286. return esc_url_raw( $fonts_url );
  287. }
  288. /**
  289. * Register widget area.
  290. *
  291. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  292. */
  293. function seedlet_widgets_init() {
  294. register_sidebar(
  295. array(
  296. 'name' => __( 'Footer', 'seedlet' ),
  297. 'id' => 'sidebar-1',
  298. 'description' => __( 'Add widgets here to appear in your footer.', 'seedlet' ),
  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. }
  306. add_action( 'widgets_init', 'seedlet_widgets_init' );
  307. /**
  308. * Set the content width in pixels, based on the theme's design and stylesheet.
  309. *
  310. * Priority 0 to make it available to lower priority callbacks.
  311. *
  312. * @global int $content_width Content width.
  313. */
  314. function seedlet_content_width() {
  315. // This variable is intended to be overruled from themes.
  316. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  317. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  318. $GLOBALS['content_width'] = apply_filters( 'seedlet_content_width', 750 );
  319. }
  320. add_action( 'after_setup_theme', 'seedlet_content_width', 0 );
  321. /**
  322. * Enqueue scripts and styles.
  323. */
  324. function seedlet_scripts() {
  325. // Enqueue Google fonts
  326. wp_enqueue_style( 'seedlet-fonts', seedlet_fonts_url(), array(), null );
  327. // Theme styles
  328. if ( $is_ie ) {
  329. // If IE 11 or below, use a flattened stylesheet with static values replacing CSS Variables
  330. wp_enqueue_style( 'seedlet-style', get_template_directory_uri() . '/assets/css/ie.css', array(), wp_get_theme()->get( 'Version' ) );
  331. } else {
  332. // If not IE, use the standard stylesheet
  333. wp_enqueue_style( 'seedlet-style', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) );
  334. }
  335. // RTL styles
  336. wp_style_add_data( 'seedlet-style', 'rtl', 'replace' );
  337. // Print styles
  338. wp_enqueue_style( 'seedlet-print-style', get_template_directory_uri() . '/assets/css/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  339. // Threaded comment reply styles
  340. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  341. wp_enqueue_script( 'comment-reply' );
  342. }
  343. // Main navigation scripts
  344. wp_enqueue_script( 'seedlet-primary-navigation-script', get_template_directory_uri() . '/assets/js/primary-navigation.js', array(), wp_get_theme()->get( 'Version' ), true );
  345. }
  346. add_action( 'wp_enqueue_scripts', 'seedlet_scripts' );
  347. /**
  348. * Fix skip link focus in IE11.
  349. *
  350. * This does not enqueue the script because it is tiny and because it is only for IE11,
  351. * thus it does not warrant having an entire dedicated blocking script being loaded.
  352. *
  353. * @link https://git.io/vWdr2
  354. */
  355. function seedlet_skip_link_focus_fix() {
  356. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  357. ?>
  358. <script>
  359. /(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);
  360. </script>
  361. <?php
  362. }
  363. add_action( 'wp_print_footer_scripts', 'seedlet_skip_link_focus_fix' );
  364. /**
  365. * SVG Icons class.
  366. */
  367. require get_template_directory() . '/classes/class-seedlet-svg-icons.php';
  368. /**
  369. * Custom colors class.
  370. */
  371. if ( empty( get_theme_mod( 'colors_manager' ) ) ) { // If the theme is on wpcom, we bypass the theme's built in custom colors, because wpcom uses a different custom color implementation.
  372. require get_template_directory() . '/classes/class-seedlet-custom-colors.php';
  373. }
  374. /**
  375. * Enhance the theme by hooking into WordPress.
  376. */
  377. require get_template_directory() . '/inc/template-functions.php';
  378. /**
  379. * SVG Icons related functions.
  380. */
  381. require get_template_directory() . '/inc/icon-functions.php';
  382. /**
  383. * Custom template tags for the theme.
  384. */
  385. require get_template_directory() . '/inc/template-tags.php';
  386. /**
  387. * Customizer additions.
  388. */
  389. require get_template_directory() . '/inc/customizer.php';
  390. /**
  391. * Block Patterns.
  392. */
  393. require get_template_directory() . '/inc/block-patterns.php';
  394. /**
  395. * Load WooCommerce compatibility file.
  396. */
  397. if ( class_exists( 'WooCommerce' ) ) {
  398. require get_template_directory() . '/inc/woocommerce.php';
  399. }