functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * Libre 2 functions and definitions
  4. *
  5. * @package Libre 2
  6. */
  7. if ( ! function_exists( 'libre_2_setup' ) ) :
  8. /**
  9. * Sets up theme defaults and registers support for various WordPress features.
  10. *
  11. * Note that this function is hooked into the after_setup_theme hook, which
  12. * runs before the init hook. The init hook is too late for some features, such
  13. * as indicating support for post thumbnails.
  14. */
  15. function libre_2_setup() {
  16. /*
  17. * Make theme available for translation.
  18. * Translations can be filed in the /languages/ directory.
  19. */
  20. load_theme_textdomain( 'libre-2', get_template_directory() . '/languages' );
  21. // Add default posts and comments RSS feed links to head.
  22. add_theme_support( 'automatic-feed-links' );
  23. /*
  24. * Let WordPress manage the document title.
  25. * By adding theme support, we declare that this theme does not use a
  26. * hard-coded <title> tag in the document head, and expect WordPress to
  27. * provide it for us.
  28. */
  29. add_theme_support( 'title-tag' );
  30. /* Add support for editor styles */
  31. add_editor_style( array( 'editor-style.css', libre_2_fonts_url() ) );
  32. // This theme uses wp_nav_menu() in one location.
  33. register_nav_menus(
  34. array(
  35. 'menu-1' => esc_html__( 'Header', 'libre-2' ),
  36. )
  37. );
  38. /*
  39. * Add support for Featured Images
  40. */
  41. add_theme_support( 'post-thumbnails' );
  42. add_image_size( 'libre-2-post-thumbnail', '1088', '9999' );
  43. /*
  44. * Add support for core Custom Logos
  45. */
  46. add_theme_support(
  47. 'custom-logo',
  48. array(
  49. 'height' => 300,
  50. 'width' => 300,
  51. 'flex-width' => true,
  52. )
  53. );
  54. // Add theme support for selective refresh for widgets.
  55. add_theme_support( 'customize-selective-refresh-widgets' );
  56. /*
  57. * Switch default core markup for search form, comment form, and comments
  58. * to output valid HTML5.
  59. */
  60. add_theme_support(
  61. 'html5',
  62. array(
  63. 'search-form',
  64. 'comment-form',
  65. 'gallery',
  66. 'caption',
  67. )
  68. );
  69. // Set up the WordPress core custom background feature.
  70. add_theme_support(
  71. 'custom-background',
  72. apply_filters(
  73. 'libre_2_custom_background_args',
  74. array(
  75. 'default-color' => 'ffffff',
  76. )
  77. )
  78. );
  79. // Load regular editor styles into the new block-based editor.
  80. add_theme_support( 'editor-styles' );
  81. // Add support for responsive embeds.
  82. add_theme_support( 'responsive-embeds' );
  83. // Add support for full and wide align images.
  84. add_theme_support( 'align-wide' );
  85. }
  86. endif; // libre_2_setup
  87. add_action( 'after_setup_theme', 'libre_2_setup' );
  88. /**
  89. * Set the content width in pixels, based on the theme's design and stylesheet.
  90. *
  91. * Priority 0 to make it available to lower priority callbacks.
  92. *
  93. * @global int $content_width
  94. */
  95. function libre_2_content_width() {
  96. $GLOBALS['content_width'] = apply_filters( 'libre_2_content_width', 739 );
  97. }
  98. add_action( 'after_setup_theme', 'libre_2_content_width', 0 );
  99. /*
  100. * Adjust $content_width for full-width page template
  101. */
  102. if ( ! function_exists( 'libre_2_content_width' ) ) :
  103. function libre_2_content_width() {
  104. global $content_width;
  105. if ( is_page_template( 'templates/full-width-page.php' ) ) {
  106. $content_width = 1088; //pixels
  107. }
  108. }
  109. add_action( 'template_redirect', 'libre_2_content_width' );
  110. endif; // if ! function_exists( 'libre_2_content_width' )
  111. /**
  112. * Register widget area.
  113. *
  114. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  115. */
  116. function libre_2_widgets_init() {
  117. register_sidebar(
  118. array(
  119. 'name' => esc_html__( 'Sidebar', 'libre-2' ),
  120. 'id' => 'sidebar-1',
  121. 'description' => '',
  122. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  123. 'after_widget' => '</aside>',
  124. 'before_title' => '<h2 class="widget-title">',
  125. 'after_title' => '</h2>',
  126. )
  127. );
  128. register_sidebar(
  129. array(
  130. 'name' => esc_html__( 'Footer 1', 'libre-2' ),
  131. 'id' => 'sidebar-2',
  132. 'description' => '',
  133. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  134. 'after_widget' => '</aside>',
  135. 'before_title' => '<h2 class="widget-title">',
  136. 'after_title' => '</h2>',
  137. )
  138. );
  139. register_sidebar(
  140. array(
  141. 'name' => esc_html__( 'Footer 2', 'libre-2' ),
  142. 'id' => 'sidebar-3',
  143. 'description' => '',
  144. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  145. 'after_widget' => '</aside>',
  146. 'before_title' => '<h2 class="widget-title">',
  147. 'after_title' => '</h2>',
  148. )
  149. );
  150. register_sidebar(
  151. array(
  152. 'name' => esc_html__( 'Footer 3', 'libre-2' ),
  153. 'id' => 'sidebar-4',
  154. 'description' => '',
  155. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  156. 'after_widget' => '</aside>',
  157. 'before_title' => '<h2 class="widget-title">',
  158. 'after_title' => '</h2>',
  159. )
  160. );
  161. }
  162. add_action( 'widgets_init', 'libre_2_widgets_init' );
  163. /**
  164. * Register Google Fonts
  165. */
  166. function libre_2_fonts_url() {
  167. $fonts_url = '';
  168. /* Translators: If there are characters in your language that are not
  169. * supported by Libre Baskerville, translate this to 'off'. Do not translate
  170. * into your own language.
  171. */
  172. $libre = esc_html_x( 'on', 'Libre Baskerville font: on or off', 'libre-2' );
  173. if ( 'off' !== $libre ) {
  174. $font_families = array();
  175. $font_families[] = 'Libre Baskerville:400,400italic,700';
  176. $query_args = array(
  177. 'family' => urlencode( implode( '|', $font_families ) ),
  178. 'subset' => urlencode( 'latin,latin-ext' ),
  179. );
  180. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  181. }
  182. return esc_url_raw( $fonts_url );
  183. }
  184. /**
  185. * Add preconnect for Google Fonts.
  186. *
  187. * @param array $urls URLs to print for resource hints.
  188. * @param string $relation_type The relation type the URLs are printed.
  189. * @return array $urls URLs to print for resource hints.
  190. */
  191. /*
  192. function libre_2_resource_hints( $urls, $relation_type ) {
  193. if ( wp_style_is( 'libre-2-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
  194. $urls[] = array(
  195. 'href' => 'https://fonts.gstatic.com',
  196. 'crossorigin',
  197. );
  198. }
  199. return $urls;
  200. }
  201. add_filter( 'wp_resource_hints', 'libre_2_resource_hints', 10, 2 );
  202. */
  203. /**
  204. * Enqueue scripts and styles.
  205. */
  206. function libre_2_scripts() {
  207. wp_enqueue_style( 'libre-2-style', get_stylesheet_uri() );
  208. // Theme block stylesheet.
  209. wp_enqueue_style( 'libre-2-block-style', get_theme_file_uri( '/css/blocks.css' ), array( 'libre-2-style' ), '1.0' );
  210. wp_enqueue_style( 'libre-2-fonts', libre_2_fonts_url(), array(), null );
  211. wp_enqueue_script( 'libre-2-script', get_template_directory_uri() . '/js/libre.js', array( 'jquery' ), '20150623', true );
  212. $adminbar = is_admin_bar_showing();
  213. wp_localize_script( 'libre-2-script', 'libreadminbar', array( $adminbar ) );
  214. wp_enqueue_script( 'libre-2-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );
  215. wp_enqueue_script( 'libre-2-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
  216. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  217. wp_enqueue_script( 'comment-reply' );
  218. }
  219. }
  220. add_action( 'wp_enqueue_scripts', 'libre_2_scripts' );
  221. /**
  222. * Enqueue editor styles for Gutenberg
  223. */
  224. function libre_2_block_editor_styles() {
  225. // Block styles.
  226. wp_enqueue_style( 'libre-2-block-editor-style', get_theme_file_uri( '/css/editor-blocks.css' ) );
  227. // Fonts.
  228. wp_enqueue_style( 'libre-2-fonts', libre_2_fonts_url(), array(), null );
  229. }
  230. add_action( 'enqueue_block_editor_assets', 'libre_2_block_editor_styles' );
  231. /*
  232. * Filters the Categories archive widget to add a span around the post count
  233. */
  234. function libre_2_cat_count_span( $links ) {
  235. $links = str_replace( '</a> (', '</a><span class="post-count">(', $links );
  236. $links = str_replace( ')', ')</span>', $links );
  237. return $links;
  238. }
  239. add_filter( 'wp_list_categories', 'libre_2_cat_count_span' );
  240. /*
  241. * Add a span around the post count in the Archives widget
  242. */
  243. function libre_2_archive_count_span( $links ) {
  244. $links = str_replace( '</a>&nbsp;(', '</a><span class="post-count">(', $links );
  245. $links = str_replace( ')', ')</span>', $links );
  246. return $links;
  247. }
  248. add_filter( 'get_archives_link', 'libre_2_archive_count_span' );
  249. if ( ! function_exists( 'libre_2_continue_reading_link' ) ) :
  250. /**
  251. * Returns an ellipsis and "Continue reading" plus off-screen title link for excerpts
  252. */
  253. function libre_2_continue_reading_link() {
  254. return '&hellip; <a class="more-link" href="' . esc_url( get_permalink() ) . '">' . sprintf( wp_kses_post( __( 'Continue reading <span class="screen-reader-text">%1$s</span> <span class="meta-nav" aria-hidden="true">&rarr;</span>', 'libre-2' ) ), esc_attr( strip_tags( get_the_title() ) ) ) . '</a>';
  255. }
  256. endif; // libre_2_continue_reading_link
  257. /**
  258. * Replaces "[...]" (appended to automatically generated excerpts) with libre_2_continue_reading_link().
  259. *
  260. * To override this in a child theme, remove the filter and add your own
  261. * function tied to the excerpt_more filter hook.
  262. */
  263. function libre_2_auto_excerpt_more( $more ) {
  264. return libre_2_continue_reading_link();
  265. }
  266. add_filter( 'excerpt_more', 'libre_2_auto_excerpt_more' );
  267. /**
  268. * Adds a pretty "Continue Reading" link to custom post excerpts.
  269. *
  270. * To override this link in a child theme, remove the filter and add your own
  271. * function tied to the get_the_excerpt filter hook.
  272. */
  273. function libre_2_custom_excerpt_more( $output ) {
  274. if ( has_excerpt() && ! is_attachment() ) {
  275. $output .= libre_2_continue_reading_link();
  276. }
  277. return $output;
  278. }
  279. add_filter( 'get_the_excerpt', 'libre_2_custom_excerpt_more' );
  280. /*
  281. * Custom comments display to move Reply link,
  282. * used in comments.php
  283. */
  284. function libre_2_comments( $comment, $args, $depth ) {
  285. ?>
  286. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
  287. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  288. <footer class="comment-meta">
  289. <div class="comment-metadata">
  290. <span class="comment-author vcard">
  291. <?php
  292. if ( 0 != $args['avatar_size'] ) {
  293. echo get_avatar( $comment, $args['avatar_size'] );}
  294. ?>
  295. <?php printf( '<b class="fn">%s</b>', get_comment_author_link() ); ?>
  296. </span>
  297. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
  298. <time datetime="<?php comment_time( 'c' ); ?>">
  299. <?php printf( '<span class="comment-date">%1$s</span><span class="comment-time screen-reader-text">%2$s</span>', get_comment_date(), get_comment_time() ); ?>
  300. </time>
  301. </a>
  302. <?php
  303. comment_reply_link(
  304. array_merge(
  305. $args,
  306. array(
  307. 'add_below' => 'div-comment',
  308. 'depth' => $depth,
  309. 'max_depth' => $args['max_depth'],
  310. 'before' => '<span class="reply">',
  311. 'after' => '</span>',
  312. )
  313. )
  314. );
  315. ?>
  316. <?php edit_comment_link( esc_html__( 'Edit', 'libre-2' ), '<span class="edit-link">', '</span>' ); ?>
  317. </div><!-- .comment-metadata -->
  318. <?php if ( '0' == $comment->comment_approved ) : ?>
  319. <p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'libre-2' ); ?></p>
  320. <?php endif; ?>
  321. </footer><!-- .comment-meta -->
  322. <div class="comment-content">
  323. <?php comment_text(); ?>
  324. </div><!-- .comment-content -->
  325. </article><!-- .comment-body -->
  326. <?php
  327. }
  328. /**
  329. * Implement the Custom Header feature.
  330. */
  331. require get_template_directory() . '/inc/custom-header.php';
  332. /**
  333. * Custom template tags for this theme.
  334. */
  335. require get_template_directory() . '/inc/template-tags.php';
  336. /**
  337. * Custom functions that act independently of the theme templates.
  338. */
  339. require get_template_directory() . '/inc/extras.php';
  340. /**
  341. * Customizer additions.
  342. */
  343. require get_template_directory() . '/inc/customizer.php';
  344. /**
  345. * Load Jetpack compatibility file.
  346. */
  347. require get_template_directory() . '/inc/jetpack.php';
  348. /**
  349. * Load WooCommerce compatibility file.
  350. */
  351. if ( class_exists( 'WooCommerce' ) ) {
  352. require get_template_directory() . '/inc/woocommerce.php';
  353. }