weedops-theme/functions.php
Alex 8eddfee34b Initial commit: Weedops WordPress theme
Includes front-page, strain archive/single templates, header/footer,
functions.php with custom post types, and base styles.
2026-03-23 22:54:38 +00:00

192 lines
9.3 KiB
PHP

<?php
/**
* Weedops Theme Functions
*
* Cannabis industry platform — theme setup, enqueues, custom post types,
* taxonomies, REST API extensions, and helper utilities.
*
* @package Weedops
* @version 1.0.0
*/
defined( 'ABSPATH' ) || exit;
define( 'WEEDOPS_VERSION', '1.0.0' );
define( 'WEEDOPS_DIR', get_template_directory() );
define( 'WEEDOPS_URI', get_template_directory_uri() );
// ─────────────────────────────────────────────────────────────────────────────
// Theme Setup
// ─────────────────────────────────────────────────────────────────────────────
function weedops_setup(): void {
load_theme_textdomain( 'weedops', WEEDOPS_DIR . '/languages' );
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'html5', [ 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script' ] );
add_theme_support( 'customize-selective-refresh-widgets' );
add_theme_support( 'woocommerce' );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
register_nav_menus( [
'primary' => __( 'Primary Navigation', 'weedops' ),
'footer' => __( 'Footer Navigation', 'weedops' ),
'account' => __( 'Account Menu', 'weedops' ),
] );
}
add_action( 'after_setup_theme', 'weedops_setup' );
// ─────────────────────────────────────────────────────────────────────────────
// Scripts & Styles
// ─────────────────────────────────────────────────────────────────────────────
function weedops_enqueue_assets(): void {
// Google Fonts — Inter
wp_enqueue_style(
'weedops-fonts',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap',
[],
null
);
// Main stylesheet
wp_enqueue_style(
'weedops-style',
get_stylesheet_uri(),
[ 'weedops-fonts' ],
WEEDOPS_VERSION
);
// Main JS bundle
wp_enqueue_script(
'weedops-main',
WEEDOPS_URI . '/assets/js/main.js',
[],
WEEDOPS_VERSION,
true
);
// Pass data to JS
wp_localize_script( 'weedops-main', 'WeedopsData', [
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'restUrl' => esc_url_raw( rest_url( 'weedops/v1/' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'siteUrl' => get_site_url(),
'isLoggedIn'=> is_user_logged_in(),
] );
}
add_action( 'wp_enqueue_scripts', 'weedops_enqueue_assets' );
// ─────────────────────────────────────────────────────────────────────────────
// Custom Post Types, Taxonomies & Meta Fields
// ─────────────────────────────────────────────────────────────────────────────
// Post types, taxonomies, and meta field registration live in their own file.
require_once WEEDOPS_DIR . '/inc/custom-post-types.php';
// ─────────────────────────────────────────────────────────────────────────────
// Widget Areas
// ─────────────────────────────────────────────────────────────────────────────
function weedops_register_sidebars(): void {
$sidebars = [
[ 'id' => 'sidebar-main', 'name' => __( 'Main Sidebar', 'weedops' ) ],
[ 'id' => 'sidebar-catalog', 'name' => __( 'Catalog Sidebar', 'weedops' ) ],
[ 'id' => 'footer-col-1', 'name' => __( 'Footer Column 1', 'weedops' ) ],
[ 'id' => 'footer-col-2', 'name' => __( 'Footer Column 2', 'weedops' ) ],
[ 'id' => 'footer-col-3', 'name' => __( 'Footer Column 3', 'weedops' ) ],
];
foreach ( $sidebars as $s ) {
register_sidebar( [
'id' => $s['id'],
'name' => $s['name'],
'before_widget' => '<div id="%1$s" class="wo-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="wo-widget__title">',
'after_title' => '</h4>',
] );
}
}
add_action( 'widgets_init', 'weedops_register_sidebars' );
// ─────────────────────────────────────────────────────────────────────────────
// Helper Functions
// ─────────────────────────────────────────────────────────────────────────────
/**
* Render a strain type badge.
*
* @param string $type indica | sativa | hybrid | cbd
* @param bool $echo Echo or return.
*/
function weedops_strain_badge( string $type, bool $echo = true ): string {
$label = ucfirst( $type );
$class = 'wo-badge wo-badge--' . sanitize_html_class( strtolower( $type ) );
$html = sprintf( '<span class="%s">%s</span>', esc_attr( $class ), esc_html( $label ) );
if ( $echo ) echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return $html;
}
/**
* Return formatted THC/CBD string for a strain.
*
* @param int $post_id
*/
function weedops_get_cannabinoid_str( int $post_id ): string {
$thc = get_post_meta( $post_id, 'wo_thc_percent', true );
$cbd = get_post_meta( $post_id, 'wo_cbd_percent', true );
$parts = [];
if ( $thc !== '' ) $parts[] = 'THC ' . number_format( (float) $thc, 1 ) . '%';
if ( $cbd !== '' ) $parts[] = 'CBD ' . number_format( (float) $cbd, 1 ) . '%';
return implode( ' · ', $parts );
}
/**
* Check if a product has a valid COA (lab test certificate).
*/
function weedops_has_valid_coa( int $post_id ): bool {
$coa = get_post_meta( $post_id, 'wo_coa_url', true );
$tested = get_post_meta( $post_id, 'wo_lab_tested', true );
return ! empty( $coa ) && (bool) $tested;
}
/**
* Age-gate: returns true if the user has confirmed they are 21+.
* Stored in a session cookie.
*/
function weedops_age_verified(): bool {
return ! empty( $_COOKIE['wo_age_verified'] );
}
// ─────────────────────────────────────────────────────────────────────────────
// Age Verification AJAX Handler
// ─────────────────────────────────────────────────────────────────────────────
function weedops_ajax_age_verify(): void {
check_ajax_referer( 'wo_age_verify', 'nonce' );
setcookie( 'wo_age_verified', '1', time() + ( 30 * DAY_IN_SECONDS ), COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
wp_send_json_success( [ 'verified' => true ] );
}
add_action( 'wp_ajax_nopriv_wo_age_verify', 'weedops_ajax_age_verify' );
add_action( 'wp_ajax_wo_age_verify', 'weedops_ajax_age_verify' );
// ─────────────────────────────────────────────────────────────────────────────
// Title Tag
// ─────────────────────────────────────────────────────────────────────────────
function weedops_document_title_parts( array $parts ): array {
$parts['tagline'] = get_bloginfo( 'description' );
return $parts;
}
add_filter( 'document_title_parts', 'weedops_document_title_parts' );
// ─────────────────────────────────────────────────────────────────────────────
// Excerpt length
// ─────────────────────────────────────────────────────────────────────────────
add_filter( 'excerpt_length', fn() => 30 );
add_filter( 'excerpt_more', fn() => '&hellip;' );