<?php if (!defined('ABSPATH')) die('No direct access allowed'); ?>
<?php
Thememakers_Application_Shortcodes::register();

//*******

add_action('admin_head', 'thememakers_app_shortcodes_admin', 1);

function thememakers_app_shortcodes_admin() {
    wp_enqueue_script('thememakers_app_shortcodes_js', THEMEMAKERS_APPLICATION_URI . '/shortcodes/js/general.js', array('jquery', 'jquery-ui-core', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-dialog'));
    wp_enqueue_style('thememakers_app_shortcodes_css', THEMEMAKERS_APPLICATION_URI . '/shortcodes/css/styles.css');
    ?>
    <script type="text/javascript">
        var thememakers_app_shortcodes_app_link = "<?php echo Thememakers_Application_Shortcodes::get_application_uri() ?>";
        var thememakers_app_shortcodes_items = [];

    <?php foreach (Thememakers_Application_Shortcodes::$autoshortcodes as $shortcode_key => $shortcode_name) : ?>
            thememakers_app_shortcodes_items.push({'key': '<?php echo $shortcode_key ?>', 'name': '<?php echo $shortcode_name ?>'});
    <?php endforeach; ?>


    </script>
    <?php
}

add_filter('mce_buttons_2', array('Thememakers_Application_Shortcodes', 'mce_buttons'));
add_filter('mce_external_plugins', array('Thememakers_Application_Shortcodes', 'mce_add_rich_plugins'));

//AJAX callback
add_action('wp_ajax_app_shortcodes_get_shortcode_template', array('Thememakers_Application_Shortcodes', 'get_shortcode_template'));
add_action('wp_ajax_app_shortcodes_base64_encode', array('Thememakers_Application_Shortcodes', 'base64_encode'));

class Thememakers_Application_Shortcodes {

    public static $shortcodes = array();
    public static $autoshortcodes = array();

    //*****************************

    public static function get_application_path() {
        return THEMEMAKERS_APPLICATION_PATH . '/shortcodes';
    }

    public static function get_application_uri() {
        return THEMEMAKERS_APPLICATION_URI . '/shortcodes';
    }

    //realmag777 dev
    public static function register() {
        /*
          self::$shortcodes = array(
          'alert' => "Alerts",
          'quotes' => "Quotes",
          );
         */

        $shortcodes = self::get_shortcodes_array();
        if (!empty($shortcodes)) {
            foreach ($shortcodes as $value) {
                $name = ucfirst(trim($value));
                $name = str_replace("_", " ", $name);
                self::$autoshortcodes[$value] = $name;
            }
        }

        foreach (self::$autoshortcodes as $shortcode_key => $shortcode_name) {

            $code = 'function ' . $shortcode_key . '_action($attributes = array(), $content = "") {
        $attributes["content"] = $content;
        return Thememakers_Application_Shortcodes::draw_html("' . $shortcode_key . '", $attributes);}';

            eval($code);

            add_shortcode($shortcode_key, $shortcode_key . '_action');
        }
    }

    public static function draw_html($shortcode_key, $attributes = array()) {
        return ThemeMakersThemeView::draw_free_page(self::get_application_path() . "/views/autoshortcodes/" . $shortcode_key . ".php", $attributes);
    }

    public static function get_shortcodes_array() {
        $results = array();
        $handler = opendir(self::get_application_path() . "/views/autoshortcodes/popups/");
        while ($file = readdir($handler)) {
            if ($file != "." AND $file != "..") {
															 
							
				$results[] = $file;
								
            }
        }
        //****
        foreach ($results as $key => $value) {
				
            $value = explode(".", $value);
            if (!empty($value[0])) {
               		
				 $results[$key] = $value[0];
            }
        }

        sort($results);

        return $results;
    }

    //*****************************


    function mce_buttons($buttons) {
        $buttons[] = 'thememakers_shortcodes';
        return $buttons;
    }

    function mce_add_rich_plugins($plugin_array) {
        $plugin_array['thememakers_tiny_shortcodes'] = self::get_application_uri() . '/js/shortcodes.js';
        return $plugin_array;
    }

    //ajax
    public function get_shortcode_template() {
        echo ThemeMakersThemeView::draw_free_page(self::get_application_path() . '/views/autoshortcodes/popups/' . $_REQUEST['shortcode_name'] . ".php", array());
        exit;
    }

    //ajax
    public static function base64_encode() {
        echo base64_encode($_REQUEST['string']);
        exit;
    }

}

