<?php
/**
 * Plugin shortcode
 *
 * @package     Wow_Plugin
 * @subpackage  Public/Shortcode
 * @author      Dmytro Lobov <i@lobov.dev>
 * @copyright   2019 Wow-Company
 * @license     GNU Public License
 * @version     1.0
 */

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

extract( shortcode_atts( array( 'id' => "" ), $atts ) );

global $wpdb;
$table  = $wpdb->prefix . 'wow_' . $this->plugin['prefix'];
$sSQL   = $wpdb->prepare( "select * from $table WHERE id = %d", $id );
$result = $wpdb->get_results( $sSQL );

if ( count( $result ) > 0 ) {

	foreach ( $result as $key => $val ) {
		$param = unserialize( $val->param );

		if ( $param['item_user'] == 1 || $param['item_user'] == '' ) {
			$user = true;
		} elseif ( $param['item_user'] == 2 ) {
			if ( is_user_logged_in() ) {
				if ( $param['user_role'] == 'all' ) {
					$user = true;
				} else {
					$current_user = wp_get_current_user();
					if ( $param['user_role'] == $current_user->roles[0] ) {
						$user = true;
					} else {
						$user = false;
					}
				}
			} else {
				$user = false;
			}
		} elseif ( $param['item_user'] == 3 ) {
			$user = ! is_user_logged_in();
		}
		if ( ! empty( $param['depending_language'] ) ) {
			$item_lang = $param['lang'];
			$site_lang = get_locale();
			if ( substr( $site_lang, 0, 2 ) == $item_lang ) {
				$lang = true;
			} else {
				$lang = false;
			}
		} elseif ( empty( $param['depending_language'] ) ) {
			$lang = true;
		}

		if ( $user == true && $lang == true ) {

			ob_start();
			include( 'partials/public.php' );
			ob_end_clean();

			$time = ! empty( $param['time'] ) ? $param['time'] : '';

			$slug    = $this->plugin['slug'];
			$version = $this->plugin['version'];

			$url_style = $this->plugin['url'] . 'assets/css/style.min.css';
			wp_enqueue_style( $slug, $url_style, null, $version );

			$inline_style = self::style( $param, $id );
			wp_add_inline_style( $slug, $inline_style );

			if ( empty( $param['disable_fontawesome'] ) ) {
				$url_icons = $this->plugin['url'] . 'assets/vendors/fontawesome/css/fontawesome-all.min.css';
				wp_enqueue_style( $slug . '-fontawesome', $url_icons, null, '5.6.3' );
			}

			$url_script = $this->plugin['url'] . 'assets/js/script.min.js';
			wp_enqueue_script( $slug, $url_script, array( 'jquery' ), $version );
		}
	}
}
