functions.php 14 KB

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