functions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * Varia functions and definitions
  5. *
  6. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  7. *
  8. * @package WordPress
  9. * @subpackage Varia
  10. * @since 1.0.0
  11. */
  12. if ( ! function_exists( 'varia_default_colors' ) ) {
  13. function varia_default_colors() {
  14. return array(
  15. 'background' => '#FFFFFF', //bg
  16. 'foreground' => '#444444', //txt
  17. 'primary' => '#0000ff', //link
  18. 'secondary' => '#ff0000', //fg1
  19. 'tertiary' => null, //fg2
  20. );
  21. }
  22. }
  23. /**
  24. * Varia only works in WordPress 4.7 or later.
  25. */
  26. if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
  27. require get_template_directory() . '/inc/back-compat.php';
  28. return;
  29. }
  30. if ( ! function_exists( 'varia_setup' ) ) :
  31. /**
  32. * Sets up theme defaults and registers support for various WordPress features.
  33. *
  34. * Note that this function is hooked into the after_setup_theme hook, which
  35. * runs before the init hook. The init hook is too late for some features, such
  36. * as indicating support for post thumbnails.
  37. */
  38. function varia_setup() {
  39. /*
  40. * Make theme available for translation.
  41. * Translations can be filed in the /languages/ directory.
  42. * If you're building a theme based on Varia, use a find and replace
  43. * to change 'varia' to the name of your theme in all the template files.
  44. */
  45. load_theme_textdomain( 'varia', get_template_directory() . '/languages' );
  46. // Add default posts and comments RSS feed links to head.
  47. add_theme_support( 'automatic-feed-links' );
  48. // Enable appearance tools for Block Editor.
  49. add_theme_support( 'appearance-tools' );
  50. /*
  51. * Let WordPress manage the document title.
  52. * By adding theme support, we declare that this theme does not use a
  53. * hard-coded <title> tag in the document head, and expect WordPress to
  54. * provide it for us.
  55. */
  56. add_theme_support( 'title-tag' );
  57. /*
  58. * Enable support for Post Thumbnails on posts and pages.
  59. *
  60. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  61. */
  62. add_theme_support( 'post-thumbnails' );
  63. set_post_thumbnail_size( 1568, 9999 );
  64. // This theme uses wp_nav_menu() in two locations.
  65. register_nav_menus(
  66. array(
  67. 'menu-1' => __( 'Primary', 'varia' ),
  68. 'menu-2' => __( 'Footer Menu', 'varia' ),
  69. 'social' => __( 'Social Links Menu', 'varia' ),
  70. )
  71. );
  72. /*
  73. * Switch default core markup for search form, comment form, and comments
  74. * to output valid HTML5.
  75. */
  76. add_theme_support(
  77. 'html5',
  78. array(
  79. 'search-form',
  80. 'comment-form',
  81. 'comment-list',
  82. 'gallery',
  83. 'caption',
  84. )
  85. );
  86. /**
  87. * Add support for core custom logo.
  88. *
  89. * @link https://codex.wordpress.org/Theme_Logo
  90. */
  91. add_theme_support(
  92. 'custom-logo',
  93. array(
  94. 'height' => 96,
  95. 'width' => 100,
  96. 'flex-width' => true,
  97. 'flex-height' => true,
  98. 'header-text' => array( 'site-title', 'site-description' ),
  99. )
  100. );
  101. // Add theme support for selective refresh for widgets.
  102. add_theme_support( 'customize-selective-refresh-widgets' );
  103. // Add support for Block Styles.
  104. add_theme_support( 'wp-block-styles' );
  105. // Add support for full and wide align images.
  106. add_theme_support( 'align-wide' );
  107. // Add support for editor styles.
  108. add_theme_support( 'editor-styles' );
  109. // Enqueue editor styles.
  110. add_editor_style( 'style-editor.css' );
  111. // add_editor_style( 'child-theme-alt-style.css' ); // this will get loaded in a child theme
  112. // Add custom editor font sizes.
  113. add_theme_support(
  114. 'editor-font-sizes',
  115. array(
  116. array(
  117. 'name' => __( 'Small', 'varia' ),
  118. 'shortName' => __( 'S', 'varia' ),
  119. 'size' => 15,
  120. 'slug' => 'small',
  121. ),
  122. array(
  123. 'name' => __( 'Normal', 'varia' ),
  124. 'shortName' => __( 'M', 'varia' ),
  125. 'size' => 18,
  126. 'slug' => 'normal',
  127. ),
  128. array(
  129. 'name' => __( 'Large', 'varia' ),
  130. 'shortName' => __( 'L', 'varia' ),
  131. 'size' => 25.92,
  132. 'slug' => 'large',
  133. ),
  134. array(
  135. 'name' => __( 'Huge', 'varia' ),
  136. 'shortName' => __( 'XL', 'varia' ),
  137. 'size' => 31.105,
  138. 'slug' => 'huge',
  139. ),
  140. )
  141. );
  142. /*
  143. * Get customizer colors and add them to the editor color palettes
  144. *
  145. * - if the customizer color is empty, use the default
  146. */
  147. $colors_array = get_theme_mod( 'colors_manager' ); // color annotations array()
  148. $default_colors = varia_default_colors();
  149. $primary = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['link'] : $default_colors['primary']; // $config-global--color-primary-default;
  150. $secondary = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['fg1'] : $default_colors['secondary']; // $config-global--color-secondary-default;
  151. $tertiary = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? ( $colors_array['colors']['fg2'] ?? '' ) : $default_colors['tertiary']; // $config-global--color-tertiary-default;
  152. $foreground = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['txt'] : $default_colors['foreground']; // $config-global--color-foreground-default;
  153. $background = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['bg'] : $default_colors['background']; // $config-global--color-background-default;
  154. $editor_colors_array = array(
  155. array(
  156. 'name' => __( 'Primary', 'varia' ),
  157. 'slug' => 'primary',
  158. 'color' => $primary,
  159. ),
  160. array(
  161. 'name' => __( 'Secondary', 'varia' ),
  162. 'slug' => 'secondary',
  163. 'color' => $secondary,
  164. ),
  165. array(
  166. 'name' => __( 'Foreground', 'varia' ),
  167. 'slug' => 'foreground',
  168. 'color' => $foreground,
  169. ),
  170. array(
  171. 'name' => __( 'Background', 'varia' ),
  172. 'slug' => 'background',
  173. 'color' => $background,
  174. ),
  175. );
  176. if ( $tertiary ) {
  177. $editor_colors_array[] = array(
  178. 'name' => __( 'Tertiary', 'varia' ),
  179. 'slug' => 'tertiary',
  180. 'color' => $tertiary,
  181. );
  182. }
  183. $editor_colors_array = apply_filters( 'varia_editor_color_palette', $editor_colors_array );
  184. // Editor color palette.
  185. add_theme_support( 'editor-color-palette', $editor_colors_array );
  186. // Add support for responsive embedded content.
  187. add_theme_support( 'responsive-embeds' );
  188. // Add support for custom line height controls.
  189. add_theme_support( 'custom-line-height' );
  190. // Add support for experimental cover block spacing.
  191. add_theme_support( 'custom-spacing' );
  192. // Add support for custom units.
  193. add_theme_support( 'custom-units' );
  194. // Add support for Global Styles.
  195. add_theme_support(
  196. 'jetpack-global-styles',
  197. array(
  198. 'enable_theme_default' => true,
  199. )
  200. );
  201. // Add support for Content Options.
  202. add_theme_support( 'jetpack-content-options', array(
  203. 'blog-display' => 'content',
  204. 'featured-images' => array(
  205. 'archive' => true,
  206. 'archive-default' => true,
  207. 'post' => true,
  208. 'post-default' => true,
  209. 'page' => true,
  210. //featured images on pages boolean was previously managed by Varia rather than jetpack. If that value has been set default to that.
  211. 'page-default' => get_theme_mod( 'show_featured_image_on_pages', false ),
  212. ),
  213. ) );
  214. }
  215. endif;
  216. add_action( 'after_setup_theme', 'varia_setup' );
  217. /**
  218. * Register widget area.
  219. *
  220. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  221. */
  222. function varia_widgets_init() {
  223. register_sidebar(
  224. array(
  225. 'name' => __( 'Footer', 'varia' ),
  226. 'id' => 'sidebar-1',
  227. 'description' => __( 'Add widgets here to appear in your footer.', 'varia' ),
  228. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  229. 'after_widget' => '</section>',
  230. 'before_title' => '<h2 class="widget-title">',
  231. 'after_title' => '</h2>',
  232. )
  233. );
  234. }
  235. add_action( 'widgets_init', 'varia_widgets_init' );
  236. /**
  237. * Set the content width in pixels, based on the theme's design and stylesheet.
  238. *
  239. * Priority 0 to make it available to lower priority callbacks.
  240. *
  241. * @global int $content_width Content width.
  242. */
  243. function varia_content_width() {
  244. // This variable is intended to be overruled from themes.
  245. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  246. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  247. $GLOBALS['content_width'] = apply_filters( 'varia_content_width', 750 );
  248. }
  249. add_action( 'after_setup_theme', 'varia_content_width', 0 );
  250. /**
  251. * Enqueue scripts and styles.
  252. */
  253. function varia_scripts() {
  254. // Theme styles
  255. wp_enqueue_style( 'varia-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  256. // RTL styles
  257. wp_style_add_data( 'varia-style', 'rtl', 'replace' );
  258. // Print styles
  259. wp_enqueue_style( 'varia-print-style', get_template_directory_uri() . '/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  260. // Threaded comment reply styles
  261. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  262. wp_enqueue_script( 'comment-reply' );
  263. }
  264. // Note, the is_IE global variable is defined by WordPress and is used
  265. // to detect if the current browser is internet explorer.
  266. global $is_IE;
  267. if ( $is_IE ) {
  268. // If IE 11 or below, use a ponyfill to add CSS Variable support
  269. wp_register_script( 'css-vars-ponyfill', get_template_directory_uri() . '/js/css-vars-ponyfill2.js' );
  270. wp_enqueue_script(
  271. 'ie11-fix',
  272. get_template_directory_uri() . '/js/ie11-fix.js',
  273. array( 'css-vars-ponyfill' ),
  274. wp_get_theme()->get( 'Version' )
  275. );
  276. wp_enqueue_style( 'varia-ie-styles', get_template_directory_uri() . '/ie.css', array(), wp_get_theme()->get( 'Version' ) );
  277. }
  278. }
  279. add_action( 'wp_enqueue_scripts', 'varia_scripts' );
  280. function varia_add_mobile_nav_on_side_scripts() {
  281. if ( get_theme_mod( 'enable_side_menu' ) !== 1 ) {
  282. return;
  283. }
  284. // Main navigation scripts
  285. wp_enqueue_script( 'varia-primary-navigation-script', get_template_directory_uri() . '/js/primary-navigation.js', array(), wp_get_theme()->get( 'Version' ), true );
  286. }
  287. /**
  288. * Fix skip link focus in IE11.
  289. *
  290. * This does not enqueue the script because it is tiny and because it is only for IE11,
  291. * thus it does not warrant having an entire dedicated blocking script being loaded.
  292. *
  293. * @link https://git.io/vWdr2
  294. */
  295. function varia_skip_link_focus_fix() {
  296. // Prevent outputting skip-link-focus-fix in AMP since the AMP framework has it built-in,
  297. // per <https://github.com/ampproject/amphtml/issues/18671>.
  298. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
  299. return;
  300. }
  301. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  302. ?>
  303. <script>
  304. /(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);
  305. </script>
  306. <?php
  307. }
  308. add_action( 'wp_print_footer_scripts', 'varia_skip_link_focus_fix' );
  309. /**
  310. * Enqueue block editor content-width styles.
  311. * - These need to be enqueued separately to include the selectors
  312. * that live outside of `.editor-styles-wrapper`
  313. *
  314. */
  315. function varia_editor_content_width() {
  316. wp_enqueue_style( 'varia-editor-content-width-style', get_theme_file_uri( '/style-editor-content-width.css' ), false, wp_get_theme()->get( 'Version' ), 'all' );
  317. }
  318. add_action( 'enqueue_block_editor_assets', 'varia_editor_content_width' );
  319. // This makes it possible to define the function in earlier to alter in one way or another.
  320. if ( ! function_exists( 'varia_mobile_nav_on_side' ) ) {
  321. function varia_mobile_nav_on_side( $classes ) {
  322. if ( get_theme_mod( 'enable_side_menu' ) === 1 ) {
  323. return array_merge( $classes, array( 'mobile-nav-side' ) );
  324. }
  325. return $classes;
  326. }
  327. }
  328. if ( ! function_exists( 'varia_enable_mobile_nav_on_side' ) ) {
  329. function varia_enable_mobile_nav_on_side() {
  330. set_theme_mod( 'enable_side_menu', 1 );
  331. }
  332. }
  333. if ( ! function_exists( 'varia_register_mobile_nav_on_side_customizer_control' ) ) {
  334. function varia_register_mobile_nav_on_side_customizer_control( $wp_customize ) {
  335. $wp_customize->add_setting(
  336. 'enable_side_menu',
  337. array(
  338. 'default' => 1,
  339. 'sanitize_callback' => 'absint',
  340. )
  341. );
  342. $wp_customize->add_section(
  343. 'nav_menus_mobile',
  344. array(
  345. 'title' => __( 'Mobile Settings', 'varia' ),
  346. 'panel' => 'nav_menus',
  347. )
  348. );
  349. $wp_customize->add_control(
  350. 'enable_side_menu',
  351. array(
  352. 'label' => __( 'Display mobile menu on the side', 'varia' ),
  353. 'section' => 'nav_menus_mobile',
  354. 'settings' => 'enable_side_menu',
  355. 'type' => 'checkbox',
  356. )
  357. );
  358. }
  359. }
  360. function varia_mobile_nav_on_side_setup() {
  361. // Add .mobile-nav-side body class.
  362. add_filter( 'body_class', 'varia_mobile_nav_on_side' );
  363. // Enable the mobile nav on side on theme switch.
  364. add_action( 'after_switch_theme', 'varia_enable_mobile_nav_on_side' );
  365. // Enable the customizer control toggle for the mobile nav on the side.
  366. add_action( 'customize_register', 'varia_register_mobile_nav_on_side_customizer_control' );
  367. // Adds the script that help toggle the mobile nav.
  368. add_action( 'wp_enqueue_scripts', 'varia_add_mobile_nav_on_side_scripts' );
  369. }
  370. /**
  371. * Sanitize the checkbox.
  372. *
  373. * @param boolean $input.
  374. *
  375. * @return boolean true if is 1 or '1', false if anything else
  376. */
  377. function varia_sanitize_checkbox( $input ) {
  378. if ( 1 == $input ) {
  379. return true;
  380. } else {
  381. return false;
  382. }
  383. }
  384. /**
  385. * Add ability to show or hide header and footer elements on the homepage.
  386. */
  387. function varia_customize_header_footer( $wp_customize ) {
  388. // Add setting to hide the site header on the homepage.
  389. $wp_customize->add_setting(
  390. 'hide_site_header',
  391. array(
  392. 'default' => false,
  393. 'type' => 'theme_mod',
  394. 'transport' => 'refresh',
  395. 'sanitize_callback' => 'varia_sanitize_checkbox',
  396. )
  397. );
  398. // Add control to hide the site header on the homepage.
  399. $wp_customize->add_control(
  400. 'hide_site_header',
  401. array(
  402. 'label' => esc_html__( 'Hide the Site Header', 'varia' ),
  403. 'description' => esc_html__( 'Check to hide the site header, if your homepage is set to display a static page.', 'varia' ),
  404. 'section' => 'static_front_page',
  405. 'priority' => 10,
  406. 'type' => 'checkbox',
  407. 'settings' => 'hide_site_header',
  408. )
  409. );
  410. // Add setting to hide footer elements on the homepage.
  411. $wp_customize->add_setting(
  412. 'hide_site_footer',
  413. array(
  414. 'default' => false,
  415. 'type' => 'theme_mod',
  416. 'transport' => 'refresh',
  417. 'sanitize_callback' => 'varia_sanitize_checkbox',
  418. )
  419. );
  420. // Add control to hide footer elements on the homepage.
  421. $wp_customize->add_control(
  422. 'hide_site_footer',
  423. array(
  424. 'label' => esc_html__( 'Hide the Site Footer Menu & Widgets', 'varia' ),
  425. 'description' => esc_html__( 'Check to hide the site menu & widgets in the footer, if your homepage is set to display a static page.', 'varia' ),
  426. 'section' => 'static_front_page',
  427. 'priority' => 10,
  428. 'type' => 'checkbox',
  429. 'settings' => 'hide_site_footer',
  430. )
  431. );
  432. }
  433. add_action( 'customize_register', 'varia_customize_header_footer' );
  434. /**
  435. * SVG Icons class.
  436. */
  437. require get_template_directory() . '/classes/class-varia-svg-icons.php';
  438. /**
  439. * Enhance the theme by hooking into WordPress.
  440. */
  441. require get_template_directory() . '/inc/template-functions.php';
  442. /**
  443. * SVG Icons related functions.
  444. */
  445. require get_template_directory() . '/inc/icon-functions.php';
  446. /**
  447. * Custom template tags for the theme.
  448. */
  449. require get_template_directory() . '/inc/template-tags.php';
  450. /**
  451. * Load WooCommerce compatibility file.
  452. */
  453. if ( class_exists( 'WooCommerce' ) ) {
  454. require get_template_directory() . '/inc/woocommerce.php';
  455. }