functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. $query_args = array(
  184. 'family' => urlencode( implode( '|', $font_families ) ),
  185. 'subset' => urlencode( 'latin,latin-ext' ),
  186. );
  187. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  188. }
  189. return esc_url_raw( $fonts_url );
  190. }
  191. /**
  192. * Add preconnect for Google Fonts.
  193. *
  194. * @param array $urls URLs to print for resource hints.
  195. * @param string $relation_type The relation type the URLs are printed.
  196. * @return array $urls URLs to print for resource hints.
  197. */
  198. /*
  199. function libre_2_resource_hints( $urls, $relation_type ) {
  200. if ( wp_style_is( 'libre-2-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
  201. $urls[] = array(
  202. 'href' => 'https://fonts.gstatic.com',
  203. 'crossorigin',
  204. );
  205. }
  206. return $urls;
  207. }
  208. add_filter( 'wp_resource_hints', 'libre_2_resource_hints', 10, 2 );
  209. */
  210. /**
  211. * Enqueue scripts and styles.
  212. */
  213. function libre_2_scripts() {
  214. wp_enqueue_style( 'libre-2-style', get_stylesheet_uri() );
  215. // Theme block stylesheet.
  216. wp_enqueue_style( 'libre-2-block-style', get_theme_file_uri( '/css/blocks.css' ), array( 'libre-2-style' ), '1.0' );
  217. wp_enqueue_style( 'libre-2-fonts', libre_2_fonts_url(), array(), null );
  218. wp_enqueue_script( 'libre-2-script', get_template_directory_uri() . '/js/libre.js', array( 'jquery' ), '20150623', true );
  219. $adminbar = is_admin_bar_showing();
  220. wp_localize_script( 'libre-2-script', 'libreadminbar', array( $adminbar ) );
  221. wp_enqueue_script( 'libre-2-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );
  222. wp_enqueue_script( 'libre-2-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
  223. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  224. wp_enqueue_script( 'comment-reply' );
  225. }
  226. }
  227. add_action( 'wp_enqueue_scripts', 'libre_2_scripts' );
  228. /*
  229. * Filters the Categories archive widget to add a span around the post count
  230. */
  231. function libre_2_cat_count_span( $links ) {
  232. $links = str_replace( '</a> (', '</a><span class="post-count">(', $links );
  233. $links = str_replace( ')', ')</span>', $links );
  234. return $links;
  235. }
  236. add_filter( 'wp_list_categories', 'libre_2_cat_count_span' );
  237. /*
  238. * Add a span around the post count in the Archives widget
  239. */
  240. function libre_2_archive_count_span( $links ) {
  241. $links = str_replace( '</a>&nbsp;(', '</a><span class="post-count">(', $links );
  242. $links = str_replace( ')', ')</span>', $links );
  243. return $links;
  244. }
  245. add_filter( 'get_archives_link', 'libre_2_archive_count_span' );
  246. if ( ! function_exists( 'libre_2_continue_reading_link' ) ) :
  247. /**
  248. * Returns an ellipsis and "Continue reading" plus off-screen title link for excerpts
  249. */
  250. function libre_2_continue_reading_link() {
  251. 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>';
  252. }
  253. endif; // libre_2_continue_reading_link
  254. /**
  255. * Replaces "[...]" (appended to automatically generated excerpts) with libre_2_continue_reading_link().
  256. *
  257. * To override this in a child theme, remove the filter and add your own
  258. * function tied to the excerpt_more filter hook.
  259. */
  260. function libre_2_auto_excerpt_more( $more ) {
  261. return libre_2_continue_reading_link();
  262. }
  263. add_filter( 'excerpt_more', 'libre_2_auto_excerpt_more' );
  264. /**
  265. * Adds a pretty "Continue Reading" link to custom post excerpts.
  266. *
  267. * To override this link in a child theme, remove the filter and add your own
  268. * function tied to the get_the_excerpt filter hook.
  269. */
  270. function libre_2_custom_excerpt_more( $output ) {
  271. if ( has_excerpt() && ! is_attachment() ) {
  272. $output .= libre_2_continue_reading_link();
  273. }
  274. return $output;
  275. }
  276. add_filter( 'get_the_excerpt', 'libre_2_custom_excerpt_more' );
  277. /*
  278. * Custom comments display to move Reply link,
  279. * used in comments.php
  280. */
  281. function libre_2_comments( $comment, $args, $depth ) {
  282. ?>
  283. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
  284. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  285. <footer class="comment-meta">
  286. <div class="comment-metadata">
  287. <span class="comment-author vcard">
  288. <?php
  289. if ( 0 != $args['avatar_size'] ) {
  290. echo get_avatar( $comment, $args['avatar_size'] );}
  291. ?>
  292. <?php printf( '<b class="fn">%s</b>', get_comment_author_link() ); ?>
  293. </span>
  294. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
  295. <time datetime="<?php comment_time( 'c' ); ?>">
  296. <?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() ); ?>
  297. </time>
  298. </a>
  299. <?php
  300. comment_reply_link(
  301. array_merge(
  302. $args,
  303. array(
  304. 'add_below' => 'div-comment',
  305. 'depth' => $depth,
  306. 'max_depth' => $args['max_depth'],
  307. 'before' => '<span class="reply">',
  308. 'after' => '</span>',
  309. )
  310. )
  311. );
  312. ?>
  313. <?php edit_comment_link( esc_html__( 'Edit', 'libre-2' ), '<span class="edit-link">', '</span>' ); ?>
  314. </div><!-- .comment-metadata -->
  315. <?php if ( '0' == $comment->comment_approved ) : ?>
  316. <p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'libre-2' ); ?></p>
  317. <?php endif; ?>
  318. </footer><!-- .comment-meta -->
  319. <div class="comment-content">
  320. <?php comment_text(); ?>
  321. </div><!-- .comment-content -->
  322. </article><!-- .comment-body -->
  323. <?php
  324. }
  325. /**
  326. * Implement the Custom Header feature.
  327. */
  328. require get_template_directory() . '/inc/custom-header.php';
  329. /**
  330. * Custom template tags for this theme.
  331. */
  332. require get_template_directory() . '/inc/template-tags.php';
  333. /**
  334. * Custom functions that act independently of the theme templates.
  335. */
  336. require get_template_directory() . '/inc/extras.php';
  337. /**
  338. * Customizer additions.
  339. */
  340. require get_template_directory() . '/inc/customizer.php';
  341. /**
  342. * Load Jetpack compatibility file.
  343. */
  344. require get_template_directory() . '/inc/jetpack.php';
  345. /**
  346. * Load WooCommerce compatibility file.
  347. */
  348. if ( class_exists( 'WooCommerce' ) ) {
  349. require get_template_directory() . '/inc/woocommerce.php';
  350. }