functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. /**
  12. * Varia 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( 'varia_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 varia_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 Varia, use a find and replace
  31. * to change 'varia' to the name of your theme in all the template files.
  32. */
  33. load_theme_textdomain( 'varia', 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. 'menu-1' => __( 'Primary', 'varia' ),
  54. 'menu-2' => __( 'Footer Menu', 'varia' ),
  55. 'social' => __( 'Social Links Menu', 'varia' ),
  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. )
  71. );
  72. /**
  73. * Add support for core custom logo.
  74. *
  75. * @link https://codex.wordpress.org/Theme_Logo
  76. */
  77. add_theme_support(
  78. 'custom-logo',
  79. array(
  80. 'height' => 96,
  81. 'width' => 100,
  82. 'flex-width' => true,
  83. 'flex-height' => true,
  84. 'header-text' => array( 'site-title', 'site-description' ),
  85. )
  86. );
  87. // Add theme support for selective refresh for widgets.
  88. add_theme_support( 'customize-selective-refresh-widgets' );
  89. // Add support for Block Styles.
  90. add_theme_support( 'wp-block-styles' );
  91. // Add support for full and wide align images.
  92. add_theme_support( 'align-wide' );
  93. // Add support for editor styles.
  94. add_theme_support( 'editor-styles' );
  95. // Enqueue editor styles.
  96. add_editor_style( 'style-editor.css' );
  97. // add_editor_style( 'child-theme-alt-style.css' ); // this will get loaded in a child theme
  98. // Add custom editor font sizes.
  99. add_theme_support(
  100. 'editor-font-sizes',
  101. array(
  102. array(
  103. 'name' => __( 'Small', 'varia' ),
  104. 'shortName' => __( 'S', 'varia' ),
  105. 'size' => 15,
  106. 'slug' => 'small',
  107. ),
  108. array(
  109. 'name' => __( 'Normal', 'varia' ),
  110. 'shortName' => __( 'M', 'varia' ),
  111. 'size' => 18,
  112. 'slug' => 'normal',
  113. ),
  114. array(
  115. 'name' => __( 'Large', 'varia' ),
  116. 'shortName' => __( 'L', 'varia' ),
  117. 'size' => 25.92,
  118. 'slug' => 'large',
  119. ),
  120. array(
  121. 'name' => __( 'Huge', 'varia' ),
  122. 'shortName' => __( 'XL', 'varia' ),
  123. 'size' => 31.105,
  124. 'slug' => 'huge',
  125. ),
  126. )
  127. );
  128. /*
  129. * Get customizer colors and add them to the editor color palettes
  130. *
  131. * - if the customizer color is empty, use the default
  132. */
  133. $colors_array = get_theme_mod( 'colors_manager' ); // color annotations array()
  134. $primary = ! empty( $colors_array ) ? $colors_array['colors']['link'] : '#0000FF'; // $config-global--color-primary-default;
  135. $secondary = ! empty( $colors_array ) ? $colors_array['colors']['fg1'] : '#FF0000'; // $config-global--color-secondary-default;
  136. $foreground = ! empty( $colors_array ) ? $colors_array['colors']['txt'] : '#444444'; // $config-global--color-foreground-default;
  137. $background = ! empty( $colors_array ) ? $colors_array['colors']['bg'] : '#FFFFFF'; // $config-global--color-background-default;
  138. // Editor color palette.
  139. add_theme_support(
  140. 'editor-color-palette',
  141. array(
  142. array(
  143. 'name' => __( 'Primary', 'varia' ),
  144. 'slug' => 'primary',
  145. 'color' => $primary,
  146. ),
  147. array(
  148. 'name' => __( 'Secondary', 'varia' ),
  149. 'slug' => 'secondary',
  150. 'color' => $secondary,
  151. ),
  152. array(
  153. 'name' => __( 'Foreground', 'varia' ),
  154. 'slug' => 'foreground',
  155. 'color' => $foreground,
  156. ),
  157. array(
  158. 'name' => __( 'Background', 'varia' ),
  159. 'slug' => 'background',
  160. 'color' => $background,
  161. ),
  162. )
  163. );
  164. // Add support for responsive embedded content.
  165. add_theme_support( 'responsive-embeds' );
  166. // Add support for Global Styles.
  167. add_theme_support(
  168. 'jetpack-global-styles',
  169. array(
  170. 'enable_theme_default' => true,
  171. )
  172. );
  173. }
  174. endif;
  175. add_action( 'after_setup_theme', 'varia_setup' );
  176. /**
  177. * Register widget area.
  178. *
  179. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  180. */
  181. function varia_widgets_init() {
  182. register_sidebar(
  183. array(
  184. 'name' => __( 'Footer', 'varia' ),
  185. 'id' => 'sidebar-1',
  186. 'description' => __( 'Add widgets here to appear in your footer.', 'varia' ),
  187. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  188. 'after_widget' => '</section>',
  189. 'before_title' => '<h2 class="widget-title">',
  190. 'after_title' => '</h2>',
  191. )
  192. );
  193. }
  194. add_action( 'widgets_init', 'varia_widgets_init' );
  195. /**
  196. * Set the content width in pixels, based on the theme's design and stylesheet.
  197. *
  198. * Priority 0 to make it available to lower priority callbacks.
  199. *
  200. * @global int $content_width Content width.
  201. */
  202. function varia_content_width() {
  203. // This variable is intended to be overruled from themes.
  204. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  205. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  206. $GLOBALS['content_width'] = apply_filters( 'varia_content_width', 750 );
  207. }
  208. add_action( 'after_setup_theme', 'varia_content_width', 0 );
  209. /**
  210. * Enqueue scripts and styles.
  211. */
  212. function varia_scripts() {
  213. // Theme styles
  214. wp_enqueue_style( 'varia-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  215. // RTL styles
  216. wp_style_add_data( 'varia-style', 'rtl', 'replace' );
  217. // Print styles
  218. wp_enqueue_style( 'varia-print-style', get_template_directory_uri() . '/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  219. // Threaded comment reply styles
  220. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  221. wp_enqueue_script( 'comment-reply' );
  222. }
  223. }
  224. add_action( 'wp_enqueue_scripts', 'varia_scripts' );
  225. function varia_add_mobile_nav_on_side_scripts() {
  226. if ( get_theme_mod( 'enable_side_menu' ) !== 1 ) {
  227. return;
  228. }
  229. // Main navigation scripts
  230. wp_enqueue_script( 'varia-primary-navigation-script', get_template_directory_uri() . '/js/primary-navigation.js', array(), wp_get_theme()->get( 'Version' ), true );
  231. }
  232. /**
  233. * Fix skip link focus in IE11.
  234. *
  235. * This does not enqueue the script because it is tiny and because it is only for IE11,
  236. * thus it does not warrant having an entire dedicated blocking script being loaded.
  237. *
  238. * @link https://git.io/vWdr2
  239. */
  240. function varia_skip_link_focus_fix() {
  241. // Prevent outputting skip-link-focus-fix in AMP since the AMP framework has it built-in,
  242. // per <https://github.com/ampproject/amphtml/issues/18671>.
  243. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
  244. return;
  245. }
  246. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  247. ?>
  248. <script>
  249. /(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);
  250. </script>
  251. <?php
  252. }
  253. add_action( 'wp_print_footer_scripts', 'varia_skip_link_focus_fix' );
  254. /**
  255. * Enqueue block editor content-width styles.
  256. * - These need to be enqueued separately to include the selectors
  257. * that live outside of `.editor-styles-wrapper`
  258. *
  259. */
  260. function varia_editor_content_width() {
  261. wp_enqueue_style( 'varia-editor-content-width-style', get_theme_file_uri( '/style-editor-content-width.css' ), false, wp_get_theme()->get( 'Version' ), 'all' );
  262. }
  263. add_action( 'enqueue_block_editor_assets', 'varia_editor_content_width' );
  264. // This makes it possible to define the function in earlier to alter in one way or another.
  265. if ( ! function_exists( 'varia_mobile_nav_on_side' ) ) {
  266. function varia_mobile_nav_on_side( $classes ) {
  267. if ( get_theme_mod( 'enable_side_menu' ) === 1 ) {
  268. return array_merge( $classes, array( 'mobile-nav-side' ) );
  269. }
  270. return $classes;
  271. }
  272. }
  273. if ( ! function_exists( 'varia_enable_mobile_nav_on_side' ) ) {
  274. function varia_enable_mobile_nav_on_side() {
  275. set_theme_mod( 'enable_side_menu', 1 );
  276. }
  277. }
  278. if ( ! function_exists( 'varia_register_mobile_nav_on_side_customizer_control' ) ) {
  279. function varia_register_mobile_nav_on_side_customizer_control( $wp_customize ) {
  280. $wp_customize->add_setting(
  281. 'enable_side_menu',
  282. array(
  283. 'default' => 1,
  284. 'sanitize_callback' => 'absint',
  285. )
  286. );
  287. $wp_customize->add_section(
  288. 'nav_menus_mobile',
  289. array(
  290. 'title' => __( 'Mobile Settings', 'varia' ),
  291. 'panel' => 'nav_menus',
  292. )
  293. );
  294. $wp_customize->add_control(
  295. 'enable_side_menu',
  296. array(
  297. 'label' => __( 'Display mobile menu on the side', 'varia' ),
  298. 'section' => 'nav_menus_mobile',
  299. 'settings' => 'enable_side_menu',
  300. 'type' => 'checkbox',
  301. )
  302. );
  303. }
  304. }
  305. function varia_mobile_nav_on_side_setup() {
  306. // Add .mobile-nav-side body class.
  307. add_filter( 'body_class', 'varia_mobile_nav_on_side' );
  308. // Enable the mobile nav on side on theme switch.
  309. add_action( 'after_switch_theme', 'varia_enable_mobile_nav_on_side' );
  310. // Enable the customizer control toggle for the mobile nav on the side.
  311. add_action( 'customize_register', 'varia_register_mobile_nav_on_side_customizer_control' );
  312. // Adds the script that help toggle the mobile nav.
  313. add_action( 'wp_enqueue_scripts', 'varia_add_mobile_nav_on_side_scripts' );
  314. }
  315. /**
  316. * Sanitize the checkbox.
  317. *
  318. * @param boolean $input.
  319. *
  320. * @return boolean true if is 1 or '1', false if anything else
  321. */
  322. function varia_sanitize_checkbox( $input ) {
  323. if ( 1 == $input ) {
  324. return true;
  325. } else {
  326. return false;
  327. }
  328. }
  329. /**
  330. * Add ability to show or hide header and footer elements on the homepage.
  331. */
  332. function varia_customize_header_footer( $wp_customize ) {
  333. // Add setting to hide the site header on the homepage.
  334. $wp_customize->add_setting(
  335. 'hide_site_header',
  336. array(
  337. 'default' => false,
  338. 'type' => 'theme_mod',
  339. 'transport' => 'refresh',
  340. 'sanitize_callback' => 'varia_sanitize_checkbox',
  341. )
  342. );
  343. // Add control to hide the site header on the homepage.
  344. $wp_customize->add_control(
  345. 'hide_site_header',
  346. array(
  347. 'label' => esc_html__( 'Hide the Site Header', 'seedlet' ),
  348. 'description' => esc_html__( 'Check to hide the site header, if your homepage is set to display a static page.', 'seedlet' ),
  349. 'section' => 'static_front_page',
  350. 'priority' => 10,
  351. 'type' => 'checkbox',
  352. 'settings' => 'hide_site_header',
  353. )
  354. );
  355. // Add setting to hide footer elements on the homepage.
  356. $wp_customize->add_setting(
  357. 'hide_site_footer',
  358. array(
  359. 'default' => false,
  360. 'type' => 'theme_mod',
  361. 'transport' => 'refresh',
  362. 'sanitize_callback' => 'varia_sanitize_checkbox',
  363. )
  364. );
  365. // Add control to hide footer elements on the homepage.
  366. $wp_customize->add_control(
  367. 'hide_site_footer',
  368. array(
  369. 'label' => esc_html__( 'Hide the Site Footer Menu & Widgets', 'seedlet' ),
  370. 'description' => esc_html__( 'Check to hide the site menu & widgets in the footer, if your homepage is set to display a static page.', 'seedlet' ),
  371. 'section' => 'static_front_page',
  372. 'priority' => 10,
  373. 'type' => 'checkbox',
  374. 'settings' => 'hide_site_footer',
  375. )
  376. );
  377. }
  378. add_action( 'customize_register', 'varia_customize_header_footer' );
  379. /**
  380. * SVG Icons class.
  381. */
  382. require get_template_directory() . '/classes/class-varia-svg-icons.php';
  383. /**
  384. * Custom Comment Walker template.
  385. */
  386. require get_template_directory() . '/classes/class-varia-walker-comment.php';
  387. /**
  388. * Enhance the theme by hooking into WordPress.
  389. */
  390. require get_template_directory() . '/inc/template-functions.php';
  391. /**
  392. * SVG Icons related functions.
  393. */
  394. require get_template_directory() . '/inc/icon-functions.php';
  395. /**
  396. * Custom template tags for the theme.
  397. */
  398. require get_template_directory() . '/inc/template-tags.php';
  399. /**
  400. * Load WooCommerce compatibility file.
  401. */
  402. if ( class_exists( 'WooCommerce' ) ) {
  403. require get_template_directory() . '/inc/woocommerce.php';
  404. }