<?php
/*
 * Plugin Name: Qcart
 * Version: 22.01.251001
 * Author: ComoQuiero
 * Author URI: https://qcart.app
 * Plugin URI: https://qcart.app
 * Description: Your plugin to automatically allow your users to add a shopping list cart for your recipes, display nutrition, and more!
 * Text Domain: comoquiero
 */

if (is_admin()) {
    include_once 'settings.php';

} else {

    // HANDLE SHORTCODE [qcart]
    add_shortcode('qcart', function ($atts) {
        $q = $atts["q"] ? "qcart-" . $atts["q"] : "cq-parent";
        return "<div class='$q'></div>";
    });

    // DEPRECATED SHORTCODE [comoquiero]:
    add_shortcode('comoquiero', function ($atts) {
        $q = $atts["q"] ? "qcart-" . $atts["q"] : "cq-parent";
        return "<div class='$q'></div>";
    });

    // LOCATION.SEARCH
    $q = "";

    $key = get_option('qcart_key');
    if (!empty($key)) {

        $supermarket = get_option('qcart_supermarket');
        $brands = get_option('qcart_brands');

        // KEY
        $q .= "?key=$key";

        // SUPERMARKET
        if (!empty($supermarket)) {
            $q .= "&smkt=$supermarket";
        }

        // BRANDS
        if (!empty($brands)) {
            $q .= "&brands=$brands";
        }
    }

    // INJECT SCRIPT
    if ($_GET['test']) {
        wp_enqueue_script('QCART_qrecipes', "https://s.comoquiero.abc/qrecipes-test.js$q", array(), '', true);
    } else {
        wp_enqueue_script('QCART_qrecipes', "https://s.comoquiero.net/qrecipes.js$q", array(), '', true);
    }
}
