weedops-theme/functions.php
John Mizerek 2db3c63e5a Initial commit: WeedOps WordPress theme
Custom theme for weedops.site — front page, header, footer,
functions, styles, and images.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 19:28:20 -07:00

130 lines
4.5 KiB
PHP

<?php
/**
* WeedOps Theme Functions
*/
// Theme setup
function weedops_setup() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption'));
}
add_action('after_setup_theme', 'weedops_setup');
// Enqueue styles
function weedops_scripts() {
wp_enqueue_style('weedops-style', get_stylesheet_uri(), array(), '1.0');
}
add_action('wp_enqueue_scripts', 'weedops_scripts');
// Remove WordPress junk from head
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
// Remove WP block library CSS (not using Gutenberg blocks)
function weedops_remove_block_css() {
wp_dequeue_style('wp-block-library');
wp_dequeue_style('wp-block-library-theme');
wp_dequeue_style('classic-theme-styles');
wp_dequeue_style('global-styles');
}
add_action('wp_enqueue_scripts', 'weedops_remove_block_css', 100);
// Disable Gutenberg for pages
function weedops_disable_gutenberg($current_status, $post_type) {
if ($post_type === 'page') return false;
return $current_status;
}
add_filter('use_block_editor_for_post_type', 'weedops_disable_gutenberg', 10, 2);
// Cloudflare Turnstile (for deck request form)
define('WEEDOPS_TURNSTILE_SITE_KEY', '');
define('WEEDOPS_TURNSTILE_SECRET_KEY', '');
function weedops_turnstile_scripts() {
if (WEEDOPS_TURNSTILE_SITE_KEY) {
wp_enqueue_script('turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js', array(), null, true);
}
}
add_action('wp_enqueue_scripts', 'weedops_turnstile_scripts');
function weedops_verify_turnstile($token) {
if (!WEEDOPS_TURNSTILE_SECRET_KEY || empty($token)) return !WEEDOPS_TURNSTILE_SECRET_KEY;
$response = wp_remote_post('https://challenges.cloudflare.com/turnstile/v0/siteverify', array(
'body' => array(
'secret' => WEEDOPS_TURNSTILE_SECRET_KEY,
'response' => $token,
'remoteip' => $_SERVER['REMOTE_ADDR'] ?? '',
),
));
if (is_wp_error($response)) return false;
$body = json_decode(wp_remote_retrieve_body($response), true);
return !empty($body['success']);
}
// Handle deck request form submission (early, before headers sent)
function weedops_handle_deck_request_init() {
if (!isset($_POST['weedops_deck_request'])) return;
// Verify nonce
if (!wp_verify_nonce($_POST['_wpnonce'] ?? '', 'weedops_deck_request')) {
set_transient('weedops_form_error_' . $_SERVER['REMOTE_ADDR'], 'Invalid request.', 60);
wp_redirect(home_url('/#deck'));
exit;
}
// Verify Turnstile if configured
if (WEEDOPS_TURNSTILE_SITE_KEY && !weedops_verify_turnstile($_POST['cf-turnstile-response'] ?? '')) {
set_transient('weedops_form_error_' . $_SERVER['REMOTE_ADDR'], 'Verification failed. Please try again.', 60);
wp_redirect(home_url('/#deck'));
exit;
}
$name = sanitize_text_field($_POST['name'] ?? '');
$email = sanitize_email($_POST['email'] ?? '');
$note = sanitize_textarea_field($_POST['note'] ?? '');
if (empty($name) || empty($email)) {
set_transient('weedops_form_error_' . $_SERVER['REMOTE_ADDR'], 'Name and email are required.', 60);
wp_redirect(home_url('/#deck'));
exit;
}
// Send notification email
$to = 'john@weedops.site';
$subject = 'WeedOps Deck Request: ' . $name;
$body = "Name: {$name}\nEmail: {$email}\n\nNote:\n{$note}";
$headers = array('Reply-To: ' . $name . ' <' . $email . '>');
wp_mail($to, $subject, $body, $headers);
set_transient('weedops_form_success_' . $_SERVER['REMOTE_ADDR'], true, 60);
wp_redirect(home_url('/#deck'));
exit;
}
add_action('template_redirect', 'weedops_handle_deck_request_init');
// Get form result from transient (after redirect)
function weedops_get_form_result() {
$ip = $_SERVER['REMOTE_ADDR'];
$success = get_transient('weedops_form_success_' . $ip);
if ($success) {
delete_transient('weedops_form_success_' . $ip);
return true;
}
$error = get_transient('weedops_form_error_' . $ip);
if ($error) {
delete_transient('weedops_form_error_' . $ip);
return $error;
}
return null;
}