functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * Independent Publisher 2 functions and definitions
  4. *
  5. * @package Independent_Publisher_2
  6. */
  7. if ( ! function_exists( 'independent_publisher_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 independent_publisher_2_setup() {
  16. /*
  17. * Make theme available for translation.
  18. * Translations can be filed in the /languages/ directory.
  19. * If you're building a theme based on Independent Publisher 2, use a find and replace
  20. * to change 'independent-publisher-2' to the name of your theme in all the template files
  21. */
  22. load_theme_textdomain( 'independent-publisher-2', get_template_directory() . '/languages' );
  23. // Add default posts and comments RSS feed links to head.
  24. add_theme_support( 'automatic-feed-links' );
  25. /*
  26. * Let WordPress manage the document title.
  27. * By adding theme support, we declare that this theme does not use a
  28. * hard-coded <title> tag in the document head, and expect WordPress to
  29. * provide it for us.
  30. */
  31. add_theme_support( 'title-tag' );
  32. add_theme_support( 'customize-selective-refresh-widgets' );
  33. /*
  34. * Enable support for Post Thumbnails on posts and pages.
  35. *
  36. * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  37. */
  38. add_theme_support( 'post-thumbnails' );
  39. set_post_thumbnail_size( 740, 430, true );
  40. add_image_size( 'independent-publisher-2-banner', 1440, 600, true );
  41. add_image_size( 'independent-publisher-2-full-width', 1100, 9999 );
  42. // This theme uses wp_nav_menu() in one location.
  43. register_nav_menus(
  44. array(
  45. 'menu-1' => esc_html__( 'Header', 'independent-publisher-2' ),
  46. )
  47. );
  48. /**
  49. * Add custom logo support
  50. */
  51. add_theme_support(
  52. 'custom-logo',
  53. array(
  54. 'height' => 300,
  55. 'width' => 1500,
  56. 'flex-width' => true,
  57. )
  58. );
  59. /*
  60. * Switch default core markup for search form, comment form, and comments
  61. * to output valid HTML5.
  62. */
  63. add_theme_support(
  64. 'html5',
  65. array(
  66. 'search-form',
  67. 'comment-form',
  68. 'comment-list',
  69. 'gallery',
  70. 'caption',
  71. )
  72. );
  73. // Set up the WordPress core custom background feature.
  74. add_theme_support(
  75. 'custom-background',
  76. apply_filters(
  77. 'independent_publisher_2_custom_background_args',
  78. array(
  79. 'default-color' => 'ffffff',
  80. )
  81. )
  82. );
  83. // Load regular editor styles into the new block-based editor.
  84. add_theme_support( 'editor-styles' );
  85. // Add support for Block Styles.
  86. add_theme_support( 'wp-block-styles' );
  87. // Add support for responsive embeds.
  88. add_theme_support( 'responsive-embeds' );
  89. // Add support for full and wide align images.
  90. add_theme_support( 'align-wide' );
  91. // Add support for custom line height controls.
  92. add_theme_support( 'custom-line-height' );
  93. // Add support for experimental cover block spacing.
  94. add_theme_support( 'custom-spacing' );
  95. // Add support for custom color scheme.
  96. add_theme_support(
  97. 'editor-color-palette',
  98. array(
  99. array(
  100. 'name' => __( 'Medium Blue', 'independent-publisher-2' ),
  101. 'slug' => 'medium-blue',
  102. 'color' => '#0087be',
  103. ),
  104. array(
  105. 'name' => __( 'Bright Blue', 'independent-publisher-2' ),
  106. 'slug' => 'bright-blue',
  107. 'color' => '#00aadc',
  108. ),
  109. array(
  110. 'name' => __( 'Dark Gray', 'independent-publisher-2' ),
  111. 'slug' => 'dark-gray',
  112. 'color' => '#4d4d4b',
  113. ),
  114. array(
  115. 'name' => __( 'Light Gray', 'independent-publisher-2' ),
  116. 'slug' => 'light-gray',
  117. 'color' => '#b3b3b1',
  118. ),
  119. array(
  120. 'name' => __( 'White', 'independent-publisher-2' ),
  121. 'slug' => 'white',
  122. 'color' => '#fff',
  123. ),
  124. )
  125. );
  126. }
  127. endif; // independent_publisher_2_setup
  128. add_action( 'after_setup_theme', 'independent_publisher_2_setup' );
  129. if ( ! function_exists( 'independent_publisher_2_word_count' ) ) :
  130. /**
  131. * Gets the number of words in the post content.
  132. *
  133. * @link http://php.net/str_word_count
  134. * @link http://php.net/number_format
  135. */
  136. function independent_publisher_2_word_count() {
  137. $content = get_post_field( 'post_content', get_the_ID() );
  138. $count = str_word_count( strip_tags( $content ) );
  139. $time = $count / 250; //Roughly 250 wpm reading time
  140. // if time less than 1 explicitly set to 1.
  141. if ( $time < 1 ) {
  142. $time = 1;
  143. }
  144. return number_format( $time );
  145. }
  146. endif; // independent_publisher_2_word_count
  147. /**
  148. * Set the content width in pixels, based on the theme's design and stylesheet.
  149. *
  150. * Priority 0 to make it available to lower priority callbacks.
  151. *
  152. * @global int $content_width
  153. */
  154. function independent_publisher_2_content_width() {
  155. $GLOBALS['content_width'] = apply_filters( 'independent_publisher_2_content_width', 1100 );
  156. }
  157. add_action( 'after_setup_theme', 'independent_publisher_2_content_width', 0 );
  158. /**
  159. * Register widget area.
  160. *
  161. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  162. */
  163. function independent_publisher_2_widgets_init() {
  164. register_sidebar(
  165. array(
  166. 'name' => esc_html__( 'Sidebar', 'independent-publisher-2' ),
  167. 'id' => 'sidebar-1',
  168. 'description' => '',
  169. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  170. 'after_widget' => '</aside>',
  171. 'before_title' => '<h1 class="widget-title">',
  172. 'after_title' => '</h1>',
  173. )
  174. );
  175. register_sidebar(
  176. array(
  177. 'name' => esc_html__( 'Footer 1', 'independent-publisher-2' ),
  178. 'id' => 'sidebar-2',
  179. 'description' => '',
  180. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  181. 'after_widget' => '</aside>',
  182. 'before_title' => '<h1 class="widget-title">',
  183. 'after_title' => '</h1>',
  184. )
  185. );
  186. register_sidebar(
  187. array(
  188. 'name' => esc_html__( 'Footer 2', 'independent-publisher-2' ),
  189. 'id' => 'sidebar-3',
  190. 'description' => '',
  191. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  192. 'after_widget' => '</aside>',
  193. 'before_title' => '<h1 class="widget-title">',
  194. 'after_title' => '</h1>',
  195. )
  196. );
  197. register_sidebar(
  198. array(
  199. 'name' => esc_html__( 'Footer 3', 'independent-publisher-2' ),
  200. 'id' => 'sidebar-4',
  201. 'description' => '',
  202. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  203. 'after_widget' => '</aside>',
  204. 'before_title' => '<h1 class="widget-title">',
  205. 'after_title' => '</h1>',
  206. )
  207. );
  208. }
  209. add_action( 'widgets_init', 'independent_publisher_2_widgets_init' );
  210. /**
  211. * Enqueue scripts and styles.
  212. */
  213. function independent_publisher_2_scripts() {
  214. // Add Genericons, used in the main stylesheet.
  215. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.3.1' );
  216. wp_enqueue_style( 'independent-publisher-2-style', get_stylesheet_uri() );
  217. // Theme block stylesheet.
  218. wp_enqueue_style( 'independent-publisher-2-block-style', get_theme_file_uri( '/css/blocks.css' ), array( 'independent-publisher-2-style' ), '1.0' );
  219. wp_enqueue_script( 'independent-publisher-2-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20170317', true );
  220. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  221. wp_enqueue_script( 'independent-publisher-2-images', get_template_directory_uri() . '/js/independent-publisher-2.js', array(), '20210118', true );
  222. }
  223. // If there's an active Video widget, and it's (hopefully) in the footer widget area
  224. if ( is_active_widget( '', '', 'media_video' ) && ( is_active_sidebar( 'sidebar-2' ) || is_active_sidebar( 'sidebar-3' ) || is_active_sidebar( 'sidebar-4' ) ) ) {
  225. wp_enqueue_script( 'independent-publisher-2-video-widget', get_template_directory_uri() . '/js/video-widget.js', array(), '20210111', true );
  226. }
  227. wp_enqueue_script( 'independent-publisher-2-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20170315', true );
  228. if ( independent_publisher_2_has_header_image() ) {
  229. wp_add_inline_style(
  230. 'independent-publisher-2-style',
  231. sprintf(
  232. '#hero-header { background: url("%s") no-repeat center; background-size: cover; background-attachment: scroll; }',
  233. esc_url( get_header_image() )
  234. )
  235. );
  236. }
  237. if ( is_singular() ) {
  238. if ( comments_open() && get_option( 'thread_comments' ) ) {
  239. wp_enqueue_script( 'comment-reply' );
  240. }
  241. $is_wc_product_page = false;
  242. if ( class_exists( 'WooCommerce' ) && is_product() ) {
  243. $is_wc_product_page = true;
  244. }
  245. if ( independent_publisher_2_has_cover_image() && independent_publisher_2_jetpack_featured_image_display() && ! $is_wc_product_page ) {
  246. $banner_src = get_the_post_thumbnail( get_the_ID(), 'independent-publisher-2-banner' );
  247. //Look for URLs in the image HTML
  248. preg_match_all( '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $banner_src, $banner_matches );
  249. //Grab the first URL
  250. $banner = $banner_matches[0][0];
  251. wp_add_inline_style(
  252. 'independent-publisher-2-style',
  253. sprintf(
  254. '#hero-header { background: url("%s") no-repeat center; background-size: cover; background-attachment: scroll; }',
  255. esc_url( $banner )
  256. )
  257. );
  258. }
  259. }
  260. }
  261. add_action( 'wp_enqueue_scripts', 'independent_publisher_2_scripts' );
  262. /**
  263. * Enqueue editor styles for Gutenberg
  264. */
  265. function independent_publisher_2_block_editor_styles() {
  266. // Block styles.
  267. wp_enqueue_style( 'independent-pub-block-editor-style', get_theme_file_uri( '/css/editor-blocks.css' ) );
  268. }
  269. add_action( 'enqueue_block_editor_assets', 'independent_publisher_2_block_editor_styles' );
  270. /**
  271. * Returns true if a post Featured Image can be displayed.
  272. *
  273. * @return bool
  274. */
  275. function independent_publisher_2_has_cover_image() {
  276. if ( is_singular() && ! post_password_required() && ! is_attachment() && independent_publisher_2_has_post_thumbnail() ) {
  277. return true;
  278. } else {
  279. return false;
  280. }
  281. }
  282. /**
  283. * Returns true if there's a header image set.
  284. *
  285. * @return bool
  286. */
  287. function independent_publisher_2_has_header_image() {
  288. return (bool) get_header_image();
  289. }
  290. /**
  291. * Returns true if the word count can be displayed in posts.
  292. *
  293. * @return bool
  294. */
  295. function independent_publisher_2_show_word_count() {
  296. $content = get_post_field( 'post_content', get_the_ID() );
  297. return in_array( get_post_type(), array( 'post' ) ) && ! empty( $content ) && (bool) 1 === (bool) get_theme_mod( 'independent_publisher_2_display_reading_time', 1 );
  298. }
  299. /**
  300. * Implement the Custom Header feature.
  301. */
  302. require get_template_directory() . '/inc/custom-header.php';
  303. /**
  304. * Custom template tags for this theme.
  305. */
  306. require get_template_directory() . '/inc/template-tags.php';
  307. /**
  308. * Custom functions that act independently of the theme templates.
  309. */
  310. require get_template_directory() . '/inc/extras.php';
  311. /**
  312. * Customizer additions.
  313. */
  314. require get_template_directory() . '/inc/customizer.php';
  315. /**
  316. * Load Jetpack compatibility file.
  317. */
  318. require get_template_directory() . '/inc/jetpack.php';
  319. /**
  320. * Load WooCommerce compatibility file.
  321. */
  322. if ( class_exists( 'WooCommerce' ) ) {
  323. require get_template_directory() . '/inc/woocommerce.php';
  324. }