functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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( array(
  44. 'menu-1' => esc_html__( 'Header', 'independent-publisher-2' ),
  45. ) );
  46. /**
  47. * Add custom logo support
  48. */
  49. add_theme_support( 'custom-logo', array(
  50. 'height' => 300,
  51. 'width' => 1500,
  52. 'flex-width' => true,
  53. ) );
  54. /*
  55. * Switch default core markup for search form, comment form, and comments
  56. * to output valid HTML5.
  57. */
  58. add_theme_support( 'html5', array(
  59. 'search-form',
  60. 'comment-form',
  61. 'comment-list',
  62. 'gallery',
  63. 'caption',
  64. ) );
  65. // Set up the WordPress core custom background feature.
  66. add_theme_support( 'custom-background', apply_filters( 'independent_publisher_2_custom_background_args', array(
  67. 'default-color' => 'ffffff',
  68. ) ) );
  69. // Load regular editor styles into the new block-based editor.
  70. add_theme_support( 'editor-styles' );
  71. // Add support for responsive embeds.
  72. add_theme_support( 'responsive-embeds' );
  73. // Add support for full and wide align images.
  74. add_theme_support( 'align-wide' );
  75. // Add support for custom color scheme.
  76. add_theme_support( 'editor-color-palette', array(
  77. array(
  78. 'name' => __( 'Medium Blue', 'independent-publisher-2' ),
  79. 'slug' => 'medium-blue',
  80. 'color' => '#0087be',
  81. ),
  82. array(
  83. 'name' => __( 'Bright Blue', 'independent-publisher-2' ),
  84. 'slug' => 'bright-blue',
  85. 'color' => '#00aadc',
  86. ),
  87. array(
  88. 'name' => __( 'Dark Gray', 'independent-publisher-2' ),
  89. 'slug' => 'dark-gray',
  90. 'color' => '#4d4d4b',
  91. ),
  92. array(
  93. 'name' => __( 'Light Gray', 'independent-publisher-2' ),
  94. 'slug' => 'light-gray',
  95. 'color' => '#b3b3b1',
  96. ),
  97. array(
  98. 'name' => __( 'White', 'independent-publisher-2' ),
  99. 'slug' => 'white',
  100. 'color' => '#fff',
  101. ),
  102. ) );
  103. }
  104. endif; // independent_publisher_2_setup
  105. add_action( 'after_setup_theme', 'independent_publisher_2_setup' );
  106. if ( ! function_exists( 'independent_publisher_2_word_count' ) ) :
  107. /**
  108. * Gets the number of words in the post content.
  109. *
  110. * @link http://php.net/str_word_count
  111. * @link http://php.net/number_format
  112. */
  113. function independent_publisher_2_word_count() {
  114. $content = get_post_field( 'post_content', get_the_ID() );
  115. $count = str_word_count( strip_tags( $content ) );
  116. $time = $count / 250; //Roughly 250 wpm reading time
  117. return number_format( $time );
  118. }
  119. endif; // independent_publisher_2_word_count
  120. /**
  121. * Set the content width in pixels, based on the theme's design and stylesheet.
  122. *
  123. * Priority 0 to make it available to lower priority callbacks.
  124. *
  125. * @global int $content_width
  126. */
  127. function independent_publisher_2_content_width() {
  128. $GLOBALS['content_width'] = apply_filters( 'independent_publisher_2_content_width', 1100 );
  129. }
  130. add_action( 'after_setup_theme', 'independent_publisher_2_content_width', 0 );
  131. /**
  132. * Register widget area.
  133. *
  134. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  135. */
  136. function independent_publisher_2_widgets_init() {
  137. register_sidebar( array(
  138. 'name' => esc_html__( 'Sidebar', 'independent-publisher-2' ),
  139. 'id' => 'sidebar-1',
  140. 'description' => '',
  141. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  142. 'after_widget' => '</aside>',
  143. 'before_title' => '<h1 class="widget-title">',
  144. 'after_title' => '</h1>',
  145. ) );
  146. register_sidebar( array(
  147. 'name' => esc_html__( 'Footer 1', 'independent-publisher-2' ),
  148. 'id' => 'sidebar-2',
  149. 'description' => '',
  150. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  151. 'after_widget' => '</aside>',
  152. 'before_title' => '<h1 class="widget-title">',
  153. 'after_title' => '</h1>',
  154. ) );
  155. register_sidebar( array(
  156. 'name' => esc_html__( 'Footer 2', 'independent-publisher-2' ),
  157. 'id' => 'sidebar-3',
  158. 'description' => '',
  159. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  160. 'after_widget' => '</aside>',
  161. 'before_title' => '<h1 class="widget-title">',
  162. 'after_title' => '</h1>',
  163. ) );
  164. register_sidebar( array(
  165. 'name' => esc_html__( 'Footer 3', 'independent-publisher-2' ),
  166. 'id' => 'sidebar-4',
  167. 'description' => '',
  168. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  169. 'after_widget' => '</aside>',
  170. 'before_title' => '<h1 class="widget-title">',
  171. 'after_title' => '</h1>',
  172. ) );
  173. }
  174. add_action( 'widgets_init', 'independent_publisher_2_widgets_init' );
  175. /**
  176. * Enqueue scripts and styles.
  177. */
  178. function independent_publisher_2_scripts() {
  179. // Add Genericons, used in the main stylesheet.
  180. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.3.1' );
  181. wp_enqueue_style( 'independent-publisher-2-style', get_stylesheet_uri() );
  182. // Theme block stylesheet.
  183. wp_enqueue_style( 'independent-publisher-2-block-style', get_theme_file_uri( '/css/blocks.css' ), array( 'independent-publisher-2-style' ), '1.0' );
  184. wp_enqueue_script( 'independent-publisher-2-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20170317', true );
  185. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  186. wp_enqueue_script( 'independent-publisher-2-images', get_template_directory_uri() . '/js/independent-publisher-2.js', array( 'jquery' ), '20170406', true );
  187. }
  188. // If there's an active Video widget, and it's (hopefully) in the footer widget area
  189. if ( is_active_widget( '','', 'media_video' ) && ( is_active_sidebar( 'sidebar-2' ) || is_active_sidebar( 'sidebar-3' ) || is_active_sidebar( 'sidebar-4' ) ) ) {
  190. wp_enqueue_script( 'independent-publisher-2-video-widget', get_template_directory_uri() . '/js/video-widget.js', array( 'jquery' ), '20170608', true );
  191. }
  192. wp_enqueue_script( 'independent-publisher-2-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20170315', true );
  193. if ( independent_publisher_2_has_header_image() ) {
  194. wp_add_inline_style( 'independent-publisher-2-style', sprintf(
  195. '#hero-header { background: url("%s") no-repeat center; background-size: cover; background-attachment: scroll; }',
  196. esc_url( get_header_image() )
  197. ) );
  198. }
  199. if ( is_singular() ) {
  200. if ( comments_open() && get_option( 'thread_comments' ) ) {
  201. wp_enqueue_script( 'comment-reply' );
  202. }
  203. $is_wc_product_page = false;
  204. if ( class_exists( 'WooCommerce' ) && is_product() ) {
  205. $is_wc_product_page = true;
  206. }
  207. if ( independent_publisher_2_has_cover_image() && independent_publisher_2_jetpack_featured_image_display() && ! $is_wc_product_page ) {
  208. $banner_src = get_the_post_thumbnail( get_the_ID(), 'independent-publisher-2-banner' );
  209. //Look for URLs in the image HTML
  210. preg_match_all( '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $banner_src, $banner_matches );
  211. //Grab the first URL
  212. $banner = $banner_matches[0][0];
  213. wp_add_inline_style( 'independent-publisher-2-style', sprintf(
  214. '#hero-header { background: url("%s") no-repeat center; background-size: cover; background-attachment: scroll; }',
  215. esc_url( $banner )
  216. ) );
  217. }
  218. }
  219. }
  220. add_action( 'wp_enqueue_scripts', 'independent_publisher_2_scripts' );
  221. /**
  222. * Enqueue editor styles for Gutenberg
  223. */
  224. function independent_publisher_2_block_editor_styles() {
  225. // Block styles.
  226. wp_enqueue_style( 'independent-pub-block-editor-style', get_theme_file_uri( '/css/editor-blocks.css' ) );
  227. }
  228. add_action( 'enqueue_block_editor_assets', 'independent_publisher_2_block_editor_styles' );
  229. /**
  230. * Returns true if a post Featured Image can be displayed.
  231. *
  232. * @return bool
  233. */
  234. function independent_publisher_2_has_cover_image() {
  235. if ( is_singular() && ! post_password_required() && ! is_attachment() && independent_publisher_2_has_post_thumbnail() ) {
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. }
  241. /**
  242. * Returns true if there's a header image set.
  243. *
  244. * @return bool
  245. */
  246. function independent_publisher_2_has_header_image() {
  247. return (bool) get_header_image();
  248. }
  249. /**
  250. * Returns true if the word count can be displayed in posts.
  251. *
  252. * @return bool
  253. */
  254. function independent_publisher_2_show_word_count() {
  255. $content = get_post_field( 'post_content', get_the_ID() );
  256. return in_array( get_post_type(), array( 'post' ) ) && ! empty( $content ) && (bool) 1 === (bool) get_theme_mod( 'independent_publisher_2_display_reading_time', 1 );
  257. }
  258. /**
  259. * Implement the Custom Header feature.
  260. */
  261. require get_template_directory() . '/inc/custom-header.php';
  262. /**
  263. * Custom template tags for this theme.
  264. */
  265. require get_template_directory() . '/inc/template-tags.php';
  266. /**
  267. * Custom functions that act independently of the theme templates.
  268. */
  269. require get_template_directory() . '/inc/extras.php';
  270. /**
  271. * Customizer additions.
  272. */
  273. require get_template_directory() . '/inc/customizer.php';
  274. /**
  275. * Load Jetpack compatibility file.
  276. */
  277. require get_template_directory() . '/inc/jetpack.php';
  278. /**
  279. * Load WooCommerce compatibility file.
  280. */
  281. if ( class_exists( 'WooCommerce' ) ) {
  282. require get_template_directory() . '/inc/woocommerce.php';
  283. }