functions.php 16 KB

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