HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /var/www/kevin-demo/wp-content/themes/fitmencook/functions.php
<?php
/**
 * fitmencook functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package fitmencook
 */

if ( ! defined( '_S_VERSION' ) ) {
	define( '_S_VERSION', '1.5.0' );
}

/**
 * -------------------------------------------------------------------------
 * CLONE SAFETY FLAGS
 * -------------------------------------------------------------------------
 * Override in wp-config.php if needed:
 *   define('FMC_DISABLE_WOO', true/false);
 *   define('FMC_DISABLE_ADS', true/false);
 */
if ( ! defined('FMC_DISABLE_WOO') ) define('FMC_DISABLE_WOO', true);
if ( ! defined('FMC_DISABLE_ADS') ) define('FMC_DISABLE_ADS', true);

/**
 * Helpers
 */
function fmc_has_woo(): bool {
	if ( defined('FMC_DISABLE_WOO') && FMC_DISABLE_WOO ) return false;
	return class_exists('WooCommerce');
}

function fmc_has_ads(): bool {
	if ( defined('FMC_DISABLE_ADS') && FMC_DISABLE_ADS ) return false;
	return true;
}

function fmc_file_version( string $abs_path ): string {
	// Prevent filemtime() warnings/fatals if file missing in clone
	if ( @file_exists($abs_path) ) {
		$ts = @filemtime($abs_path);
		if ( $ts ) return date('YmdHi', $ts);
	}
	return (string) _S_VERSION;
}

function fmc_safe_require( string $abs_path ): void {
	if ( file_exists($abs_path) ) require $abs_path;
}

function fmc_safe_require_once( string $abs_path ): void {
	if ( file_exists($abs_path) ) require_once $abs_path;
}

function fmc_post_type_exists_safe( string $pt ): bool {
	return post_type_exists($pt);
}

/**
 * Sets up theme defaults and registers support for various WordPress features.
 */
function fmc_setup() {
	load_theme_textdomain( 'fmc', get_template_directory() . '/languages' );

	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'post-thumbnails' );

	register_nav_menus(
		array(
			'primary' => __( 'Primary', 'fmc' ),
			'menu_404' => __( '404 Page', 'fmc' ),
		)
	);

	add_theme_support(
		'html5',
		array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
		)
	);

	add_theme_support( 'customize-selective-refresh-widgets' );

	add_theme_support(
		'custom-logo',
		array(
			'height'      => 160,
			'width'       => 210,
			'flex-width'  => true,
			'flex-height' => true,
		)
	);
}
add_action( 'after_setup_theme', 'fmc_setup' );

/**
 * Set the content width in pixels.
 */
function fmc_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'fmc_content_width', 640 );
}
add_action( 'after_setup_theme', 'fmc_content_width', 0 );

/**
 * Enqueue scripts and styles.
 */
function fmc_scripts() {
	$main_css_abs   = get_stylesheet_directory() . '/main.css';
	$custom_js_abs  = get_stylesheet_directory() . '/js/custom.js';

	$css_cache_buster = fmc_file_version($main_css_abs);
	$js_cache_buster  = fmc_file_version($custom_js_abs);

	wp_enqueue_style( 'fmc-style', get_stylesheet_uri(), array(), _S_VERSION );
	wp_enqueue_style( 'main', get_stylesheet_directory_uri() . '/main.css', array(), $css_cache_buster, 'all' );

	// GSAP
	wp_enqueue_script(
		'gsap',
		'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js',
		array(),
		'3.12.2',
		true
	);
	wp_enqueue_script(
		'gsap-scrolltrigger',
		'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/ScrollTrigger.min.js',
		array('gsap'),
		'3.12.2',
		true
	);

	wp_enqueue_script(
		'custom',
		get_template_directory_uri() . '/js/custom.js',
		array('jquery','gsap','gsap-scrolltrigger'),
		$js_cache_buster,
		true
	);

	// Font
	wp_enqueue_style(
		'sunfish-original-font',
		'https://fonts.cdnfonts.com/css/sunfish-original',
		array(),
		null
	);

	$theme_vars = array(
		'ajaxUrl' => admin_url('admin-ajax.php'),
		'formID'  => 'UNLNpK',
		'cartUrl' => ( fmc_has_woo() && function_exists('wc_get_cart_url') ) ? wc_get_cart_url() : '',
		'nonce'   => wp_create_nonce('nonce-security'),
	);

	if ( is_singular('recipes') ) {
		$theme_vars['recipeID'] = get_the_ID();
	}
	wp_localize_script('custom', 'theme', $theme_vars);

	// Vendors
	wp_enqueue_script( 'flickity', get_template_directory_uri() . '/js/vendor/flickity.js', array('jquery'), _S_VERSION, true );
	wp_enqueue_script( 'smart-banner', get_template_directory_uri() . '/js/vendor/smartbanner.js', array('jquery'), _S_VERSION, true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}

	// Guard "product" queries if Woo is off or CPT doesn't exist
	$is_product_single  = fmc_post_type_exists_safe('product') && is_singular('product');
	$is_product_archive = fmc_post_type_exists_safe('product') && is_post_type_archive('product');

	if ( is_singular('recipes') || $is_product_single || is_singular('meal-plans') || is_post_type_archive('recipes') ) {
		wp_enqueue_script( 'rateit-script', get_template_directory_uri() . '/js/vendor/rateit.min.js', array('jquery'), _S_VERSION, true );
	}

	if ( is_singular('recipes') || is_singular('meal-plans') || $is_product_single ) {
		wp_enqueue_script( 'validate', get_template_directory_uri() . '/js/vendor/jquery.validate.min.js', array('jquery'), _S_VERSION, true );
	}
}
add_action( 'wp_enqueue_scripts', 'fmc_scripts' );

/**
 * Woo mini-cart AJAX (guarded)
 */
if ( fmc_has_woo() ) {
	fmc_safe_require_once( get_template_directory() . '/includes/woo-minicart-ajax.php' );
}

/**
 * Admin styles
 */
function fmc_admin_styles() {
	wp_enqueue_style( 'backend-styles', get_template_directory_uri() . '/admin.css' );
}
add_action( 'admin_enqueue_scripts', 'fmc_admin_styles' );

/**
 * Login styles
 */
function stier_login_styles() {
	wp_enqueue_style( 'login-style', get_template_directory_uri() . '/wp-login.css' );
}
add_action('login_head', 'stier_login_styles');

/**
 * Includes (safe requires)
 */
fmc_safe_require( get_template_directory() . '/includes/template-tags.php' );
fmc_safe_require( get_template_directory() . '/includes/template-functions.php' );

// Ads/widgets (guarded for clone safety)
if ( fmc_has_ads() ) {
	fmc_safe_require( get_template_directory() . '/includes/widgets-ads.php' );
}

fmc_safe_require( get_template_directory() . '/includes/customizer.php' );
fmc_safe_require( get_template_directory() . '/includes/comments.php' );
fmc_safe_require( get_template_directory() . '/includes/yoast/yoast.php' );

fmc_safe_require_once( get_template_directory() . '/includes/post-types.php' );
fmc_safe_require_once( get_template_directory() . '/includes/klaviyoEmailSendAjax.php' );
fmc_safe_require_once( get_template_directory() . '/includes/brevoEmailSendAjax.php' );

if ( is_admin() ) {
	fmc_safe_require( get_template_directory() . '/includes/admin-time-modified.php' );
}

/**
 * Load WooCommerce compatibility file (guarded)
 */
if ( fmc_has_woo() ) {
	fmc_safe_require( get_template_directory() . '/includes/woocommerce.php' );
}

/**
 * Remove URL from comments
 */
add_filter('comment_form_default_fields', 'unset_url_field');
function unset_url_field($fields){
	if ( isset($fields['url']) ) unset($fields['url']);
	return $fields;
}

/**
 * Blocks
 */
add_action( 'init', 'register_acf_blocks' );
function register_acf_blocks() {
	register_block_type( __DIR__ . '/blocks/hero' );
	register_block_type( __DIR__ . '/blocks/counters' );
	register_block_type( __DIR__ . '/blocks/philosophy' );
	register_block_type( __DIR__ . '/blocks/app-cta' );
	register_block_type( __DIR__ . '/blocks/blurbs' );
	register_block_type( __DIR__ . '/blocks/order' );
	register_block_type( __DIR__ . '/blocks/button' );
	register_block_type( __DIR__ . '/blocks/section' );
	register_block_type( __DIR__ . '/blocks/logo-slide' );
	register_block_type( __DIR__ . '/blocks/fmc-media' );
	register_block_type( __DIR__ . '/blocks/cta-banner' );
	register_block_type( __DIR__ . '/blocks/pi-intro' );
}

/**
 * Comment form placeholders
 */
function placeholder_author_email_url_form_fields( $fields ) {
	foreach( $fields as &$field ) {
		$field = str_replace( 'id="author"', 'id="author" placeholder="Your Name*"', $field );
		$field = str_replace( 'id="email"',  'id="email" placeholder="Your Email*"', $field );
		$field = str_replace( 'id="url"',    'id="url" placeholder="website"', $field );
	}
	return $fields;
}
add_filter( 'comment_form_default_fields', 'placeholder_author_email_url_form_fields' );

function placeholder_comment_form_field($fields) {
	$replace_comment = __("Share your thoughts on the recipe here! Rate below if you've made it", 'fmc');
	$fields['comment_field'] = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) .
	'</label><textarea id="comment" name="comment" cols="45" rows="8" placeholder="'.$replace_comment.'" aria-required="true"></textarea></p>';
	return $fields;
}
add_filter( 'comment_form_defaults', 'placeholder_comment_form_field' );

add_filter( 'comment_form_fields', 'move_comment_field' );
function move_comment_field( $fields ) {
	if ( isset($fields['comment']) ) {
		$comment_field = $fields['comment'];
		unset( $fields['comment'] );
		$fields['comment'] = $comment_field;
	}
	return $fields;
}

/**
 * Allow iFrames
 */
function allow_iframes( $allowedposttags ){
	$allowedposttags['iframe'] = array(
		'align' => true,
		'allow' => true,
		'allowfullscreen' => true,
		'class' => true,
		'frameborder' => true,
		'height' => true,
		'id' => true,
		'marginheight' => true,
		'marginwidth' => true,
		'name' => true,
		'scrolling' => true,
		'src' => true,
		'style' => true,
		'width' => true,
		'allowFullScreen' => true,
		'mozallowfullscreen' => true,
		'title' => true,
		'webkitAllowFullScreen' => true,
	);
	return $allowedposttags;
}
add_filter( 'wp_kses_allowed_html', 'allow_iframes', 1 );

/**
 * TinyMCE editor button to highlight text
 */
add_action( 'after_setup_theme', 'fitmencook_theme_setup' );
if ( ! function_exists( 'fitmencook_theme_setup' ) ) {
	function fitmencook_theme_setup(){
		add_action( 'admin_init', 'fitmencook_theme_add_editor_styles' );
		add_action( 'init', 'fitmencook_buttons' );
	}
}

if ( ! function_exists( 'fitmencook_theme_add_editor_styles' ) ) {
	function fitmencook_theme_add_editor_styles() {
		add_editor_style( 'admin.css' );
	}
}

if ( ! function_exists( 'fitmencook_buttons' ) ) {
	function fitmencook_buttons() {
		if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) return;
		if ( get_user_option( 'rich_editing' ) !== 'true' ) return;
		add_filter( 'mce_external_plugins', 'fitmencook_add_buttons' );
		add_filter( 'mce_buttons', 'fitmencook_register_buttons' );
	}
}

if ( ! function_exists( 'fitmencook_add_buttons' ) ) {
	function fitmencook_add_buttons( $plugin_array ) {
		$plugin_array['highlight'] = get_template_directory_uri().'/js/tinymce_buttons.js';
		return $plugin_array;
	}
}

if ( ! function_exists( 'fitmencook_register_buttons' ) ) {
	function fitmencook_register_buttons( $buttons ) {
		array_push( $buttons, 'highlight' );
		return $buttons;
	}
}

/**
 * Nutrition scripts include (safe)
 */
fmc_safe_require_once( get_template_directory() . '/includes/enqueue-scripts-nutrition.php' );

/**
 * Coach page assets
 */
function enqueue_coach_page_styles() {
	if ( is_page_template( 'coach.php' ) ) {
		wp_enqueue_style( 'coach-style', get_template_directory_uri() . '/assets/css/coach.css' );
	}
}
add_action( 'wp_enqueue_scripts', 'enqueue_coach_page_styles' );

function enqueue_coach_slider_script() {
	wp_enqueue_script(
		'coach-slider-script',
		get_template_directory_uri() . '/js/coach-slider.js',
		array('jquery'),
		null,
		true
	);
}
add_action('wp_enqueue_scripts', 'enqueue_coach_slider_script');

/**
 * Woo-only assets (guarded)
 */
function custom_add_to_cart_script() {
	if ( ! fmc_has_woo() ) return;
	wp_enqueue_script(
		'custom-add-to-cart',
		get_template_directory_uri() . '/js/custom-add-to-cart.js',
		array('jquery'),
		'',
		true
	);
}
add_action('wp_enqueue_scripts', 'custom_add_to_cart_script');

function enqueue_shop_page_styles() {
	if ( ! fmc_has_woo() ) return;
	if ( fmc_post_type_exists_safe('product') && is_post_type_archive('product') ) {
		$css_abs = get_template_directory() . '/assets/css/shop-page.css';
		wp_enqueue_style(
			'shop-page-style',
			get_template_directory_uri() . '/assets/css/shop-page.css',
			array(),
			fmc_file_version($css_abs),
			'all'
		);
	}
}
add_action('wp_enqueue_scripts', 'enqueue_shop_page_styles');

function fmc_enqueue_single_product_assets(){
	if ( ! fmc_has_woo() ) return;
	if ( function_exists('is_product') && is_product() ) {
		$css_abs = get_template_directory() . '/assets/css/single-product.css';
		$js_abs  = get_template_directory() . '/js/single-product-toggle.js';

		wp_enqueue_style(
			'fmc-single-product',
			get_template_directory_uri() . '/assets/css/single-product.css',
			array(),
			fmc_file_version($css_abs),
			'all'
		);

		wp_enqueue_script(
			'fmc-single-product-toggle',
			get_template_directory_uri() . '/js/single-product-toggle.js',
			array(),
			fmc_file_version($js_abs),
			true
		);
	}
}
add_action( 'wp_enqueue_scripts', 'fmc_enqueue_single_product_assets' );

/**
 * Dynamic year shortcode + replacements
 */
function dynamic_year_shortcode() {
	return date('Y');
}
add_shortcode('year', 'dynamic_year_shortcode');

add_filter('the_title', function($title) {
	return str_replace('[year]', date('Y'), $title);
});

add_filter('the_content', function($content) {
	$year = date('Y');
	$content = str_replace('[year]', $year, $content);
	$content = str_replace('%%currentyear%%', $year, $content);
	return $content;
});

/**
 * Woo cart discount (guarded)
 */
function apply_quantity_discount( $cart ) {
	if ( ! fmc_has_woo() ) return;
	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

	// ACF option reads (if ACF missing, get_field will be undefined)
	if ( ! function_exists('get_field') ) return;

	$enable_offer = get_field( 'global_enable_special_offer', 'option' );
	$discount_qty = get_field( 'global_discount_quantity', 'option' ) ?: 3;
	$discount_pct = get_field( 'global_discount_percent', 'option' ) ?: 10;

	if ( ! $enable_offer ) return;

	$discount = 0;
	foreach ( $cart->get_cart() as $cart_item ) {
		$qty   = $cart_item['quantity'];
		$price = $cart_item['data']->get_price();
		if ( $qty >= $discount_qty ) {
			$discount += $price * $qty * ( $discount_pct / 100 );
		}
	}

	if ( $discount > 0 ) {
		$cart->add_fee(
			sprintf( __( 'Buy %d+ Get %d%% Off', 'woocommerce' ), $discount_qty, $discount_pct ),
			-$discount
		);
	}
}
if ( fmc_has_woo() ) {
	add_action( 'woocommerce_cart_calculate_fees', 'apply_quantity_discount', 20, 1 );
}

/**
 * Random recipe mega menu shortcode
 */
function random_recipe_mega_menu() {
	$args = array(
		'post_type'      => 'recipes',
		'posts_per_page' => 1,
		'orderby'        => 'rand'
	);

	$query = new WP_Query($args);
	ob_start();

	if ($query->have_posts()) {
		while ($query->have_posts()) {
			$query->the_post();
			$image_url = get_the_post_thumbnail_url(get_the_ID(), 'medium_large');
			$permalink = get_permalink();
			$title = get_the_title();
			?>
			<div style="padding-top: 0px;">
				<a href="<?php echo esc_url($permalink); ?>">
					<img class='img__recipe-m'
						src="<?php echo esc_url($image_url); ?>"
						alt="<?php echo esc_attr(get_the_title()); ?>"
						style="width: 100%; height: auto; aspect-ratio: 16/9; object-fit: cover;"
					/>
				</a>
			</div>

			<p style="font-size: 13px;">
				<a href="<?php echo esc_url($permalink); ?>"><?php echo esc_html($title); ?></a>
			</p>
			<?php
		}
		wp_reset_postdata();
	}

	return ob_get_clean();
}
add_shortcode('random_recipe_menu', 'random_recipe_mega_menu');

/**
 * Brevo scripts
 */
function enqueue_brevo_form_scripts() {
	wp_enqueue_script(
		'brevo-main-js',
		'https://sibforms.com/forms/end-form/build/main.js',
		array(),
		null,
		true
	);

	$inline_js = <<<JS
window.REQUIRED_CODE_ERROR_MESSAGE = 'Please choose a country code';
window.LOCALE = 'en';
window.EMAIL_INVALID_MESSAGE = window.SMS_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";
window.REQUIRED_ERROR_MESSAGE = "This field cannot be left blank.";
window.GENERIC_INVALID_MESSAGE = "The information provided is invalid. Please review the field format and try again.";

window.translation = {
  common: {
    selectedList: '{quantity} list selected',
    selectedLists: '{quantity} lists selected',
    selectedOption: '{quantity} selected',
    selectedOptions: '{quantity} selected'
  }
};

var AUTOHIDE = Boolean(1);
JS;

	wp_add_inline_script('brevo-main-js', $inline_js, 'before');
}
add_action('wp_enqueue_scripts', 'enqueue_brevo_form_scripts');

/**
 * Newsletter email send script
 */
function enqueue_newsletter_email_script() {
	if ( is_singular('recipes') ) {
		$js_abs = get_template_directory() . '/js/custom/newsletterEmailSend.js';
		wp_enqueue_script(
			'newsletter-email-send',
			get_template_directory_uri() . '/js/custom/newsletterEmailSend.js',
			array('jquery'),
			fmc_file_version($js_abs),
			true
		);

		// NOTE: This uses the same global var name "theme" as the main script.
		// Keeping it as-is to preserve behavior, but it will overwrite if both run.
		wp_localize_script('newsletter-email-send', 'theme', array(
			'ajaxUrl'  => admin_url('admin-ajax.php'),
			'recipeID' => get_the_ID()
		));
	}
}
add_action('wp_enqueue_scripts', 'enqueue_newsletter_email_script');

/**
 * Recipe v2 styles
 */
function enqueue_recipe_v2_styles() {
	if ( is_singular('recipes') ) {
		$template_slug = get_page_template_slug();
		if ($template_slug === 'template-single-recipe-v2.php') {
			$css_abs = get_template_directory() . '/assets/css/recipe-v2.css';
			wp_enqueue_style(
				'recipe-v2-style',
				get_template_directory_uri() . '/assets/css/recipe-v2.css',
				array(),
				fmc_file_version($css_abs)
			);
		}
	}
}
add_action('wp_enqueue_scripts', 'enqueue_recipe_v2_styles');

/**
 * Recipe FAQ script
 */
function enqueue_recipe_faq_script() {
	if ( is_singular('recipes') ) {
		wp_enqueue_script(
			'recipe-faq',
			get_stylesheet_directory_uri() . '/js/recipe-faq.js',
			array(),
			'1.0',
			true
		);
	}
}
add_action('wp_enqueue_scripts', 'enqueue_recipe_faq_script');

/**
 * Admin footer tweak for product screen (Woo-only)
 */
add_action( 'admin_footer', function() {
	if ( ! fmc_has_woo() ) return;
	$screen = function_exists('get_current_screen') ? get_current_screen() : null;
	if ( $screen && $screen->id === 'product' ) : ?>
		<script>
		document.addEventListener('DOMContentLoaded', function() {
			function changeLabel() {
				const headers = document.querySelectorAll('h2.postbox-header');
				headers.forEach(h2 => {
					const label = h2.querySelector('label');
					const target = label ? label : h2;
					if (target && target.textContent && target.textContent.trim() === 'Product description') {
						target.textContent = 'Product description (What does it taste like?)';
					}
				});
			}
			changeLabel();
			setTimeout(changeLabel, 800);
		});
		</script>
	<?php endif;
});