functions.php 12 KB

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