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