<?php
function freshop_enqueue_scripts() {

    if (!is_admin()) {
        
        clearstatcache();

        // Deregister Bootstrap jquery
        wp_deregister_script('jquery');
        wp_deregister_script('jquery-migrate');

        enqueue_and_version_script('jquery', '/resources/vendor/jquery/jquery-3.5.1.min.js');
        enqueue_and_version_script('jquery-migrate', '/resources/vendor/jquery/jquery-migrate-3.3.1.min.js');
        
        // Greensock animations
        enqueue_and_version_script('gsap-cssplugin', '/resources/vendor/gsap/CSSPlugin.min.js');
        enqueue_and_version_script('gsap-easepack', '/resources/vendor/gsap/EasePack.min.js');
        enqueue_and_version_script('gsap-tweenlite', '/resources/vendor/gsap/TweenLite.min.js');
        enqueue_and_version_script('jquery-gsap', '/resources/vendor/gsap/jquery.gsap.min.js');

        // Bootstrap
        enqueue_and_version_script('bootstrap', '/resources/vendor/bootstrap/js/bootstrap.min.js');

        enqueue_and_version_script('off-canvas-menu', '/resources/vendor/off-canvas/hc-offcanvas-nav.js');

        // Carousel, Menus, jquery plugins
        enqueue_and_version_script('jquery-cookie', '/resources/vendor/jquery/jquery.cookie.js');
        
        // Underscore
        enqueue_and_version_script("underscore", "/resources/vendor/underscore/underscore-min.js");
        
        // Foundation (scripts)
        enqueue_and_version_script('config-js', "/resources/scripts/config.js");
        enqueue_and_version_script_child('config-js-child', "/resources/scripts/config.js");

        enqueue_and_version_style('style-css-print', '/resources/stylesheets/css/print.css', 'print');

        enqueue_and_version_style('blog-simple', '/resources/stylesheets/css/blog-simple.css');
        enqueue_and_version_style('off-canvas-core', '/resources/vendor/off-canvas/off-canvas-core.css');
        enqueue_and_version_style('hc-offcanvas-nav-carbon', '/resources/vendor/off-canvas/hc-offcanvas-nav.carbon.css');
        enqueue_and_version_style('header-gold', '/resources/stylesheets/css/header-gold.css');
        enqueue_and_version_style('footer-gold', '/resources/stylesheets/css/footer-gold.css');

        // Foundation (styles)
        enqueue_and_version_style("style-css", "/style.css");
        enqueue_and_version_style('screen-css', '/resources/stylesheets/css/screen.css');

        enqueue_and_version_script("fp-client-js", "/resources/scripts/fpClientUtil.js");
        enqueue_and_version_script("fp-client-util-js", "/resources/scripts/fpClient.js");
        enqueue_and_version_script("screen-js", "/resources/scripts/screen.js");
        enqueue_and_version_script("loading-js", "/resources/scripts/loading.js");

        // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
        wp_localize_script(
            'fp-client-js',
            'ajax_objectClient',
            array(
                'ajax_url' => admin_url( 'admin-ajax.php' ),
                'rest_url'  => get_bloginfo('url') . '/wp-json'
            )
        );
    }
}
function freshop_admin_enqueue_scripts() {
    enqueue_and_version_style("fp-admin-screen-css", "/resources/stylesheets/css/admin-screen.css");
    enqueue_and_version_script('fp-admin-js', "/resources/scripts/fpClientAdmin.js");

    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script(
        'fp-admin-js',
        'ajax_objectClient',
        array(
            'ajax_url'  => admin_url( 'admin-ajax.php' ),
            'rest_url'  => get_bloginfo('url') . '/wp-json'
        )
    );
}


function admin_enqueue_scripts() {
    enqueue_and_version_style("fp-admin-screen-css", "/resources/stylesheets/css/admin-screen.css");
    enqueue_and_version_script('fp-admin-js', "/resources/scripts/fpClientAdmin.js");

    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script(
        'fp-admin-js',
        'ajax_objectClient',
        array(
            'ajax_url'  => admin_url( 'admin-ajax.php' ),
            'rest_url'  => get_bloginfo('url') . '/wp-json'
        )
    );
}

add_action( 'wp_enqueue_scripts', 'freshop_enqueue_scripts', 0 );
add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts', 0 );

function enqueue_and_version_script($handle, $path, $in_footer = false) {
  wp_enqueue_script($handle, get_template_directory_uri() . $path, false, fp_get_theme_version(), $in_footer);
}

function enqueue_and_version_script_child($handle, $path, $in_footer = false) {
  wp_enqueue_script($handle, get_stylesheet_directory_uri() . $path, false, fp_get_theme_version(), $in_footer);
}

function enqueue_and_version_style($handle, $path, $media = 'all') {
  wp_enqueue_style ($handle, get_template_directory_uri() . $path, false, fp_get_theme_version(), $media);
}

add_filter('rocket_css_url', 'fp_rocket_css_url');
function fp_rocket_css_url($url) {
  $version = fp_get_theme_version();
  return str_replace('?f=', '?' . (isset($version) ? $version . '&' : '') . 'f=', $url);
}

function social_media_urls() {
    $social_media_urls = array(
        'facebook' => null,
        'instagram' => null,        
        'twitter' => null,
        'pinterest' => null,
        'youtube' => null,
        'googleplus' => null,
        'rss' => null,
        'android' => null,
        'ios' => null
    );
    
    // global store settings
    $args = array(
        'post_type' => 'stores',
        'meta_query' => array(array('key' => 'wpcf-store-name', 'value' => 'no-store-selected'))
    );

    $posts = get_posts($args);

    if (count($posts) > 0) {
        foreach ($social_media_urls as $key => $value) {
            $social_media_urls[$key] = get_post_meta($posts[0]->ID, 'wpcf-store-' . $key . '-url', true);
        }
    }
    
    $fpPreferences = null;

    if(isset($_COOKIE['fp-pref'])) { 
        $fpPreferences = $_COOKIE['fp-pref'];
    }

    if ($fpPreferences != null) {
        $preferences = json_decode(stripslashes($fpPreferences));

        if ($preferences->store_id) {
            // override with specific store if available in session
            $args = array(
                'post_type' => 'stores',
                'meta_query' => array(array('key' => 'wpcf-store-id', 'value' => $preferences->store_id))
            );

            $posts = get_posts($args);

            if (count($posts) > 0) {

                $url = null;
                foreach ($social_media_urls as $key => $value) {
                    $url = get_post_meta($posts[0]->ID, 'wpcf-store-' . $key . '-url', true);
                    if(!empty($url)) $social_media_urls[$key] = $url;
                }
            }
        }
    }

    return $social_media_urls;
}

add_action('rest_api_init', 'load_freshop_wp_api');
add_filter('allowed_http_origin', '__return_true');
function load_freshop_wp_api() {
    include_once(get_template_directory() . '/functions/rest/freshop.rest.class.php');
    $fprest = new FreshopREST();
}

// clear REST API cache when any post is saved
add_action('save_post', 'fp_clear_wp_rest_cache');
add_action('after_rocket_clean_domain', 'fp_clear_wp_rest_cache');

add_action('soliloquy_flush_caches', function() {
    if (function_exists('rocket_clean_post')) {
        $posts = get_posts(array(
            'post_type' => 'home-page',
            'numberposts' => -1
        ));

        foreach ($posts as $post) {
            rocket_clean_post($post->ID);
        }
    }

    fp_clear_wp_rest_cache();
});

function fp_clear_wp_rest_cache() {
    if (class_exists('WP_REST_Cache')) {
        WP_REST_Cache::empty_cache();
    }
}

add_action('wp_ajax_fp_slider_shortcode', 'load_slider_ajax');
add_action('wp_ajax_nopriv_fp_slider_shortcode', 'load_slider_ajax');

function load_slider_ajax() {
    $store_id = $_POST['store_id'];
    $is_home = $_POST['is_home'];
    $content = '';

    //we have no way of passing store id or group id to soliloquy slider so set a global
    global $fp_selected_store;
    $fp_selected_store['id'] = $store_id;
    $fp_selected_store['group_id'] = get_store_group($store_id);

    if($is_home == 'true') {
        $content = do_shortcode('[soliloquy slug="homepage-slider"]');
    }

    wp_reset_postdata();
    wp_die($content);
}
if (class_exists('Soliloquy')) {
    add_action('rest_api_init', 'load_freshop_wp_api_soliloquy');
    function load_freshop_wp_api_soliloquy() {
        include_once(get_template_directory() . '/functions/rest/freshop.rest.soliloquy.php');
        $fprestsoliloquy = new FreshopRESTSoliloquy();
    }
}