functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. if ( ! function_exists( 'dw_setup' ) ):
  3. function dw_setup() {
  4. require( get_template_directory() . '/inc/utils.php' );
  5. require( get_template_directory() . '/inc/template.php' );
  6. require( get_template_directory() . '/inc/tweaks.php' );
  7. if ( ! isset( $content_width ) ) $content_width = 1310;
  8. add_theme_support( 'title-tag' );
  9. add_theme_support( 'wp-block-styles' );
  10. add_theme_support( 'automatic-feed-links' );
  11. add_theme_support( 'responsive-embeds' );
  12. add_theme_support( 'menus' );
  13. add_theme_support( 'post-thumbnails' );
  14. add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ) );
  15. add_theme_support( 'post-formats', array( 'aside', 'gallery', 'image', 'video', 'audio', 'quote', 'link', 'status', 'chat' ) );
  16. set_post_thumbnail_size( 150, 150, true );
  17. add_image_size( 'post-large', 770, 577, true );
  18. add_image_size( 'studio', 400, 300, true );
  19. add_filter( 'big_image_size_threshold', '__return_false' );
  20. add_theme_support( 'align-wide' );
  21. // Load the front-end stylesheet into the block editor so editor typography/sizing
  22. // matches the front end (was missing — only a 2-line editor.css was enqueued).
  23. add_theme_support( 'editor-styles' );
  24. add_editor_style( array( 'v4-style.min.css', 'css/editor.css' ) );
  25. remove_action( 'init', 'wp_sitemaps_get_server' );
  26. // added this to remove depreciation warning in v6.4.1 b/c front page doesn't have wp_header
  27. remove_action( 'wp_footer', 'the_block_template_skip_link' );
  28. // see changes @ https://core.trac.wordpress.org/changeset/56932
  29. }
  30. endif;
  31. add_action( 'after_setup_theme', 'dw_setup' );
  32. if ( ! function_exists( 'dw_plugs' ) ):
  33. function dw_plugs() {
  34. require( get_template_directory() . '/inc/smtp.php');
  35. //echo wp_mail( 'david@davidawindham.com', 'WP Mail Test', 'Mail is working' );
  36. if (!class_exists('dw_crumbs')) {
  37. //include_once(TEMPLATEPATH.'/inc/crumbs.php');
  38. }
  39. }
  40. endif;
  41. add_action( 'after_setup_theme', 'dw_plugs' );
  42. // Keep login sessions alive for a year (default is 2 days, 14 with "Remember Me").
  43. // There's no WP constant for this — it's the auth_cookie_expiration filter, so it
  44. // can't live in wp-config.php. Lower YEAR_IN_SECONDS if you want a shorter session.
  45. function dw_login_session( $length, $user_id, $remember ) {
  46. return YEAR_IN_SECONDS;
  47. }
  48. add_filter( 'auth_cookie_expiration', 'dw_login_session', 10, 3 );
  49. function dw_remove_block_library_css(){
  50. wp_dequeue_style( 'wp-block-library' );
  51. }
  52. //add_action( 'wp_enqueue_scripts', 'dw_remove_block_library_css' );
  53. /* Guest Book — publish the "Guest" Gravity Form (id 2) entries below the form on
  54. /contact/guests: 4 Name · 6 Location · 9 Website · 10 Message (Email (7) is never
  55. shown; active entries only). Registered here rather than in the late-loaded
  56. inc/form.php because this block theme renders the page content BEFORE
  57. wp_enqueue_scripts, so the render_block hook has to be attached early. */
  58. function dw_guestbook() {
  59. if ( ! class_exists( 'GFAPI' ) ) {
  60. return '';
  61. }
  62. $entries = GFAPI::get_entries(
  63. 2,
  64. array( 'status' => 'active' ),
  65. array( 'key' => 'date_created', 'direction' => 'DESC' ),
  66. array( 'offset' => 0, 'page_size' => 200 )
  67. );
  68. if ( is_wp_error( $entries ) || empty( $entries ) ) {
  69. return '';
  70. }
  71. $rows = '';
  72. foreach ( $entries as $e ) {
  73. $name = trim( rgar( $e, '4' ) );
  74. $loc = trim( rgar( $e, '6' ) );
  75. $web = trim( rgar( $e, '9' ) );
  76. $msg = trim( rgar( $e, '10' ) );
  77. if ( $name === '' && $msg === '' ) {
  78. continue;
  79. }
  80. $weblink = '';
  81. if ( $web !== '' ) {
  82. $href = esc_url( $web );
  83. if ( $href !== '' ) {
  84. $label = preg_replace( '#^https?://(www\.)?#i', '', untrailingslashit( $web ) );
  85. $weblink = '<a href="' . $href . '" target="_blank" rel="nofollow ugc noopener">' . esc_html( $label ) . '</a>';
  86. }
  87. }
  88. $rows .= '<tr>'
  89. . '<td data-label="Name">' . esc_html( $name ) . '</td>'
  90. . '<td data-label="Location">' . esc_html( $loc ) . '</td>'
  91. . '<td data-label="Website">' . $weblink . '</td>'
  92. . '<td data-label="Message">' . nl2br( esc_html( $msg ) ) . '</td>'
  93. . '<td data-label="Date">' . esc_html( mysql2date( 'M Y', $e['date_created'] ) ) . '</td>'
  94. . '</tr>';
  95. }
  96. if ( $rows === '' ) {
  97. return '';
  98. }
  99. return '<div class="dw-guestbook">'
  100. . '<h3 class="dw-guestbook-title">Guest Book</h3>'
  101. . '<figure class="wp-block-table is-style-stripes"><table>'
  102. . '<thead><tr>'
  103. . '<th scope="col">Name</th><th scope="col">Location</th><th scope="col">Website</th>'
  104. . '<th scope="col">Message</th><th scope="col">Date</th>'
  105. . '</tr></thead>'
  106. . '<tbody>' . $rows . '</tbody>'
  107. . '</table></figure></div>';
  108. }
  109. function dw_guestbook_once() {
  110. static $done = false;
  111. if ( $done ) {
  112. return '';
  113. }
  114. $done = true;
  115. return dw_guestbook();
  116. }
  117. add_shortcode( 'dw_guestbook', 'dw_guestbook' );
  118. add_filter( 'render_block', 'dw_guestbook_render_block', 10, 2 );
  119. function dw_guestbook_render_block( $block_content, $block ) {
  120. if ( ! empty( $block['blockName'] ) && 'core/post-content' === $block['blockName'] && is_page( 'guests' ) ) {
  121. $block_content .= dw_guestbook_once();
  122. }
  123. return $block_content;
  124. }
  125. add_action( 'wp_enqueue_scripts', 'dw_scripts' );
  126. function dw_scripts() {
  127. global $post;
  128. wp_enqueue_style( 'style-min', get_template_directory_uri() . '/v4-style.min.css', array(), filemtime( get_template_directory() . '/v4-style.min.css' ) );
  129. wp_deregister_script('jquery');
  130. if ( is_page('studio') ) {
  131. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  132. wp_enqueue_script( 'init', get_template_directory_uri() . '/js/studio.js', '', '', true );
  133. }
  134. elseif ( is_page('music') ) {
  135. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  136. wp_enqueue_script( 'last-fm', get_template_directory_uri() . '/js/music.js', '', '', true );
  137. }
  138. elseif ( is_page('art') ) {
  139. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  140. wp_enqueue_script( 'art', get_template_directory_uri() . '/js/art.js', '', '', true );
  141. }
  142. elseif ( is_page('contact') ) {
  143. require( get_template_directory() . '/inc/form.php' );
  144. //gravity_form_enqueue_scripts(1, false);
  145. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  146. wp_enqueue_style( 'gforms', get_template_directory_uri() . '/css/form.css');
  147. wp_enqueue_script('jquery', get_template_directory_uri() . '/js/jquery-3.1.0.min.js', array(), false, true);
  148. wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', array(), '3', true);
  149. wp_enqueue_script( 'form', get_template_directory_uri() . '/js/contact.js', '', '', true );
  150. }
  151. elseif ( is_page('guests') ) {
  152. require( get_template_directory() . '/inc/form.php' );
  153. //gravity_form_enqueue_scripts(1, false);
  154. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  155. wp_enqueue_style( 'gforms', get_template_directory_uri() . '/css/form.css');
  156. wp_enqueue_script('jquery', get_template_directory_uri() . '/js/jquery-3.1.0.min.js', array(), false, true);
  157. wp_enqueue_script( 'form', get_template_directory_uri() . '/js/contact.js', '', '', true );
  158. }
  159. elseif ( is_page('contract') ) {
  160. require( get_template_directory() . '/inc/form.php' );
  161. //gravity_form_enqueue_scripts(1, false);
  162. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  163. wp_enqueue_style( 'gforms', get_template_directory_uri() . '/css/form.css');
  164. wp_enqueue_script('jquery', get_template_directory_uri() . '/js/jquery-3.1.0.min.js', array(), false, true);
  165. wp_enqueue_script( 'form', get_template_directory_uri() . '/js/contact.js', '', '', true );
  166. }
  167. elseif ( is_page('pay') ) {
  168. require( get_template_directory() . '/inc/form.php' );
  169. //gravity_form_enqueue_scripts(1, false);
  170. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  171. wp_enqueue_style( 'gforms', get_template_directory_uri() . '/css/form.css');
  172. wp_enqueue_script('jquery', get_template_directory_uri() . '/js/jquery-3.1.0.min.js', array(), false, true);
  173. }
  174. elseif ( is_page('analytics') ) {
  175. require( get_template_directory() . '/inc/analytics.php' );
  176. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  177. wp_enqueue_script( 'dashboardjs', get_template_directory_uri() . '/js/analytics.js', '', '', true );
  178. }
  179. elseif ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  180. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  181. wp_enqueue_script( 'single', get_template_directory_uri() . '/js/single.js', '', '', true );
  182. wp_enqueue_script( 'comment-reply' );
  183. }
  184. elseif ( is_page('desk') || is_archive() || is_search() ) {
  185. global $wp_rewrite;
  186. wp_enqueue_script( 'jquery', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  187. wp_enqueue_script( 'desk', get_template_directory_uri() . '/js/desk.js', '', '', true );
  188. wp_enqueue_script( '_s_backbone-loop', get_template_directory_uri() . '/js/loop.js', array( 'jquery', 'backbone', 'underscore', 'wp-api' ), '1.0', true );
  189. $queried_object = get_queried_object();
  190. $local = array(
  191. 'loopType' => 'home',
  192. 'queriedObject' => $queried_object,
  193. 'pathInfo' => array(
  194. 'author_permastruct' => $wp_rewrite->get_author_permastruct(),
  195. 'host' => preg_replace( '#^http(s)?://#i', '', untrailingslashit( home_url() ) ),
  196. 'path' => _s_backbone_get_request_path(),
  197. 'use_trailing_slashes' => $wp_rewrite->use_trailing_slashes,
  198. 'parameters' => _s_backbone_get_request_parameters(),
  199. ),
  200. );
  201. if ( is_category() || is_tag() || is_tax() ) {
  202. $local['loopType'] = 'archive';
  203. $local['taxonomy'] = get_taxonomy( $queried_object->taxonomy );
  204. } elseif ( is_search() ) {
  205. $local['loopType'] = 'search';
  206. $local['searchQuery'] = get_search_query();
  207. } elseif ( is_author() ) {
  208. $local['loopType'] = 'author';
  209. }
  210. if ( is_paged() ) {
  211. $local['page'] = absint( get_query_var( 'paged' ) ) + 1;
  212. }
  213. wp_localize_script( '_s_backbone-loop', 'settings', $local );
  214. }
  215. else {
  216. //wp_deregister_script('mediaelement');
  217. wp_enqueue_script( 'scriptmin', get_template_directory_uri() . '/js/v4-script.min.js', '', '', true );
  218. wp_enqueue_script( 'single', get_template_directory_uri() . '/js/single.js', '', '', true );
  219. wp_enqueue_script( 'mediaelement-me', get_template_directory_uri() .'/js/mediaelement-and-player.min.js', '', '', true);
  220. wp_enqueue_script( 'mediaelement-wp', get_template_directory_uri() .'/js/wp-mediaelement.min.js', '', '', true);
  221. }
  222. }
  223. // davo-bot 2000 — AI ask widget. Printed directly on wp_footer (like dw_analytics)
  224. // rather than enqueued, because front-page.php skips wp_head() so wp_enqueue_scripts
  225. // never fires there. The loader pulls /ask/widget.js (proxied same-origin to ralph).
  226. // On the homepage, window.dwAsk.open = 'once' auto-opens the terminal on first visit.
  227. function dw_ask_widget() {
  228. if ( is_admin() ) return;
  229. $src = get_template_directory_uri() . '/js/ask-widget.js';
  230. $ver = filemtime( get_template_directory() . '/js/ask-widget.js' );
  231. $open = is_front_page() ? 'once' : '';
  232. ?>
  233. <script>window.dwAsk = {open:"<?php echo esc_js( $open ); ?>"};</script>
  234. <script src="<?php echo esc_url( $src . '?ver=' . $ver ); ?>"></script>
  235. <?php
  236. }
  237. add_action( 'wp_footer', 'dw_ask_widget' );
  238. // front-page.php is fully self-contained (no wp_head, hand-rolled critical CSS, zero
  239. // blocks/presets), but WordPress still injects theme.json's global styles — and its
  240. // body/h1-h6 element rules override the homepage's intended typography. Strip them on
  241. // the front page only; the rest of the site keeps them. (front-page.php skips wp_head,
  242. // so the global styles print during wp_footer — dequeue before that late-print runs.)
  243. function dw_no_global_styles_on_home() {
  244. if ( is_front_page() ) {
  245. wp_dequeue_style( 'global-styles' );
  246. wp_dequeue_style( 'global-styles-css-custom-properties' );
  247. }
  248. }
  249. add_action( 'wp_enqueue_scripts', 'dw_no_global_styles_on_home', 100 );
  250. add_action( 'wp_footer', 'dw_no_global_styles_on_home', 1 );
  251. // Dev only: on a real content save, ping BrowserSync's reload endpoint so the front-end
  252. // preview refreshes — DB content changes aren't visible to a filesystem watcher otherwise.
  253. // Hooks the REST save events the block editor actually fires (+ customizer + nav menus).
  254. // Gated to WP_DEBUG → no-op in production.
  255. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  256. function dw_bs_reload_on_save( $post = null ) {
  257. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  258. if ( $post && is_object( $post ) && $post->post_status === 'auto-draft' ) return;
  259. wp_remote_get( 'https://daw.stu:81/__browser_sync__?method=reload', array(
  260. 'blocking' => false,
  261. 'sslverify' => false,
  262. ) );
  263. }
  264. add_action( 'rest_after_insert_page', 'dw_bs_reload_on_save', 10, 3 );
  265. add_action( 'rest_after_insert_post', 'dw_bs_reload_on_save', 10, 3 );
  266. add_action( 'customize_save_after', 'dw_bs_reload_on_save', 10, 3 );
  267. add_action( 'wp_update_nav_menu', 'dw_bs_reload_on_save', 10, 3 );
  268. }
  269. add_action('wp_footer', 'dw_analytics', 22);
  270. function dw_analytics() { ?>
  271. <script>
  272. var _paq = window._paq = window._paq || [];
  273. /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  274. _paq.push(['trackPageView']);
  275. _paq.push(['enableLinkTracking']);
  276. (function() {
  277. var u="https://davidawindham.com/wik/";
  278. _paq.push(['setTrackerUrl', u+'matomo.php']);
  279. _paq.push(['setSiteId', '1']);
  280. var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
  281. g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  282. })();
  283. </script>
  284. <noscript><p><img src="https://davidawindham.com/wik/matomo.php?idsite=1&amp;rec=1" style="border:0;" alt="" /></p></noscript>
  285. <?php }
  286. ?>