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

include_once 'settings.php';

add_shortcode('comoquiero', function ($atts) {
    extract(shortcode_atts(array(
        'q' => '',
        'key' => '',
        'supermarket' => '',
        'brands' => '',
    ), $atts));

    $q = $q ?: "";
    $src = "https://api-v2.comoquiero.net/s/$q";

    if (!empty($key)) {

        // KEY
        $and = "?";
        if (str_contains($src, "?")) {
            $and = "&";
        }
        $src .= $and . "smkt=$supermarket";

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

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

    wp_enqueue_script("COMOQUIERO_inline", $src);

    return "<div class='cq-parent'></div>";
});
