functions.php 14 KB

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