<?php

/**
 * Customizer fallback validation for stored theme mod values.
 *
 * @package Kubelio\Customizer
 * @since 1.0.0
 */

namespace Kubelio\Customizer;

if (! defined('ABSPATH')) {
    exit;
}

class Fallback {

    /**
     * Constructor.
     */
    public function __construct() {
        add_filter('theme_mod_product_layout', array(__CLASS__, 'product_layout'));
        add_filter('theme_mod_footer_layout', array(__CLASS__, 'footer_layout'));
        add_filter('theme_mod_shop_layout', array(__CLASS__, 'shop_layout'));
        add_filter('theme_mod_header_sticky', array(__CLASS__, 'header_sticky'));
    }

    /**
     * Validate the stored sticky-header behavior against what this theme supports.
     *
     * "none" and "down" ship with this theme, so any other stored value falls
     * back to "down". Code that registers additional sticky modes should
     * remove this filter to let its values pass through.
     *
     * @param mixed $value Stored theme mod value.
     * @return string
     */
    public static function header_sticky($value) {
        return in_array($value, array('none', 'down'), true) ? $value : 'down';
    }


    /**
     * Validate the stored single-product layout against the layouts this theme supports.
     *
     * Falls back to the default for any unsupported value. This keeps the front end
     * consistent when a value for a layout this theme doesn't ship (e.g. "modern")
     * is left stored. Code that registers extra layouts should remove this filter
     * to let its values pass through.
     *
     * @param mixed $value Stored theme mod value.
     * @return string
     */
    public static function product_layout($value) {
        return in_array($value, array('classic', 'compact'), true) ? $value : 'compact';
    }

    /**
     * Validate the stored footer layout against the layouts this theme ships.
     *
     * Falls back to the default footer for any unsupported value so the footer
     * never renders empty when a layout value this theme doesn't ship is left
     * stored. Code that registers extra footer layouts should remove this filter
     * to let its values pass through.
     *
     * @param mixed $value Stored theme mod value.
     * @return string
     */
    public static function footer_layout($value) {
        $supported = array(
            'footer-copyright-social',
            'footer-copyright-menu',
            'footer-column-3-big-first',
            'footer-column-4-equal',
        );

        return in_array($value, $supported, true) ? $value : 'footer-copyright-social';
    }

    /**
     * Validate the stored shop layout against what this theme supports.
     *
     * Only the wide shop layout ships with this theme, so any other stored
     * value falls back to "wide". Code that registers additional shop layouts
     * should remove this filter to let its values pass through.
     *
     * @param mixed $value Stored theme mod value.
     * @return string
     */
    public static function shop_layout($value) {
        return in_array($value, array('wide'), true) ? $value : 'wide';
    }
}
