<?php
namespace Wpupgrader\WordPress\Plugins\GdprConsentLite\Hooks\Shortcodes;

use Wpupgrader\WordPress\Plugins\GdprConsentLite\Consents;
use Wpupgrader\WordPress\Plugins\GdprConsentPlugin\GdprConsentPlugin;

class embed extends \Wpupgrader\Libraries\WordPress\Plugins\Core\Hooks\AbstractShortcode
{
    static $embedDisabledText = '';
    static $embedCount = 0;

    /**
     * This method is invoked by Wordpress
     *
     * @param string $atts
     * @param string $content
     * @return string
     */
    static public function invoke($atts = null, $content = null)
    {
        $attributes = \shortcode_atts([
            'title'  => null,
            'id'     => null,
            'ids'    => null,
            'widget' => true,
            'suffix' => '',
        ], $atts);

        if (!empty($attributes['id'])) {
            $consent_ids = [$attributes['id']];
        }
        else if (!empty($attributes['ids'])) {
            $consent_ids = \explode(',', $attributes['ids']);
        }

        $content = \do_shortcode($content);

        if ($hasPlaceholder = \preg_match('~\[\s*gdpr_consent_inactive_content\s*\]~i', $content)) {
            $content = \preg_split('~\[\s*gdpr_consent_inactive_content\s*\]~i', $content);
        }
        else {
            $content = [$content];
        }

        $options = \get_option('gdpr-consent-settings');
        if (!empty($consent_ids) && (\gdprc_user_is_logged_in() || empty($options['gdpr_consent_maintenance_mode']))) {

            $gdprcCookie = \gdprc_consent_cookie_get();
            $consent_ids_authorized = \explode(',', \str_replace(GDPRC_COOKIE_DELIMITER, ',', $gdprcCookie));

            /** @var bool $authorized If this shortcode is authorized by the users consents or not */
            $authorized = true;
            foreach ($consent_ids as $consent_id) {
                if (!\in_array($consent_id, $consent_ids_authorized)) {
                    $authorized = false;
                }
            }

            if (!$authorized) {
                if ($attributes['widget'] && $attributes['widget'] !== 'false' && $attributes['widget'] !== '0') {
                    /** If consent shortcode was not authorized, embed give consent widget! */
                    $plugin = GdprConsentPlugin::getInstance();
                    $block = $plugin->getBlock('Embed');
                    $block->setConsents(static::getConsents($consent_ids));
                    $block->setSuffix($attributes['suffix']);
                    $block->setEmbedCount((string)static::$embedCount);

                    if (!empty($attributes['title'])) {
                        $block->setCustomTitle($attributes['title']);
                    }

                    if ($hasPlaceholder) {
                        $content[0] = \preg_replace('~^([^<]*</[^>]+>[^<]*|[^<]*)<([^>]+)>~i', '\1<div class="gdprc_consent_interface_embed_popup'.($block->getEffectsClasses()).'" id="gdprc_consent_embed_popup_'.static::$embedCount.'">'.$block->render().'</div><\2 gdprc_consent_placeholder="gdprc_consent_embed_popup_'.static::$embedCount.'">', $content[1]);
                    }
                    else {
                        $content[0] = $block->render();
                    }

                    static::$embedCount++;
                }
                else {
                    if (empty(static::$embedDisabledText) && !empty($settings = \get_option('gdpr-consent-settings'))) {
                        if (!empty($settings['gdprc_consent_interface_embed_disabled'])) {
                            static::$embedDisabledText = $settings['gdprc_consent_interface_embed_disabled'];
                        }
                    }
                    $content[0] = \do_shortcode(static::$embedDisabledText); // Remove content
                }
            }
        }

        /**
         * Return the content
         */
        return $content[0];
    }

    static protected function getConsents($consent_ids)
    {
        $consentClass = new Consents();
        return $consentClass->getByIds($consent_ids);
    }
}
