functions.php 14 KB

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