<?php


define('SC_VERSION', '3.4.1');
define('SC_AUTHOR', 'Aakash Chakravarthy');
define('SC_URL', plugins_url('',__FILE__) );
define('SC_ADMIN', admin_url( 'options-general.php?page=shortcoder' ) );

$sc_donate_link = 'http://bit.ly/scDonate';




function sc_setup() {
    $shortcoder_data = get_option('shortcoder_data');
    $sc_flags = get_option('shortcoder_flags');
    // Move the flag version fix to sc_flags option
    if(isset($shortcoder_data['_version_fix'])){
        unset($shortcoder_data['_version_fix']);
    }
    $sc_flags['version'] = SC_VERSION;
    update_option('shortcoder_flags', $sc_flags);



    //re-generate
    if(!empty($shortcoder_data)) {
        foreach($shortcoder_data as $k => $v){
            if(!empty($shortcoder_data[$k]['autoupdate'])) {
                unset($shortcoder_data[$k]);
            }
        }
    }


    $theme = wp_get_theme();


    if (empty($shortcoder_data['io_cart']) ) {
        $shortcoder_data['io_cart']['autoupdate'] = 1;
        $io_cart_shortcode_content = file_get_contents("includes/io_cart_shortcode.html", true);
        $shortcoder_data['io_cart']['content'] = $io_cart_shortcode_content;
    } else {
        //correct issue in io_cart sc for existing installs
        if (!strpos($shortcoder_data['io_cart']['content'],'setTimeout(simpleCart.init, 100);')) {
            $shortcoder_data['io_cart']['content'] .= '<script>jQuery(document).ready(function () {setTimeout(simpleCart.init, 100);});</script>';
        }
        if (strpos($shortcoder_data['io_cart']['content'],'[sc name="io_cart" page="%%page%%"]') !==FALSE) {
            $shortcoder_data['io_cart']['content'] = str_replace('[sc name="io_cart" page="%%page%%"]','',$shortcoder_data['io_cart']['content']);
        }
    }
    if (empty($shortcoder_data['io_category']) ) {
        $shortcoder_data['io_category']['autoupdate'] = 1;
        $io_category_shortcode_content = file_get_contents("includes/io_category_shortcode.html", true);
        $shortcoder_data['io_category']['content'] = $io_category_shortcode_content;
    }
    if (empty($shortcoder_data['io_all_cat']) ) {
        $shortcoder_data['io_all_cat']['autoupdate'] = 1;
        $io_all_cat_shortcode_content = file_get_contents("includes/io_all_cat_shortcode.html", true);
        $shortcoder_data['io_all_cat']['content'] = $io_all_cat_shortcode_content;
    }
    if (empty($shortcoder_data['io_all_rental']) ) {
        $shortcoder_data['io_all_rental']['autoupdate'] = 1;
        $io_all_rental_shortcode_content = file_get_contents("includes/io_all_rental_shortcode.html", true);
        $shortcoder_data['io_all_rental']['content'] = $io_all_rental_shortcode_content;
    }
    if (empty($shortcoder_data['io_rental']) ) {
        $shortcoder_data['io_rental']['autoupdate'] = 1;
        $io_rental_shortcode_content = file_get_contents("includes/io_rental_shortcode.html", true);
        $shortcoder_data['io_rental']['content'] = $io_rental_shortcode_content;
    }else{
        //correct issue in io_rental sc
        if(!empty($shortcoder_data['io_rental'])) {
            $shortcoder_data['io_rental'] = str_replace('this.title = (this.title ? \'\' + this.title + \'\' : \'\') + \'Image \' + (this.index + 1) + \' of \' + this.group.length;', 'this.title = (this.title ? "" + this.title + "" : "") + \'Image \' + (this.index + 1) + \' of \' + this.group.length;', $shortcoder_data['io_rental']);
        }
        if (strpos($shortcoder_data['io_rental']['content'],'[sc name="io_cart" page="%%page%%"]') !==FALSE) {
            $shortcoder_data['io_rental']['content'] = str_replace('[sc name="io_cart" page="%%page%%"]','',$shortcoder_data['io_rental']['content']);
        }
    }
    if (empty($shortcoder_data['io_category_cart']) ) {
        $shortcoder_data['io_category_cart']['autoupdate'] = 1;
        $io_category_cart_shortcode_content = file_get_contents("includes/io_category_cart_shortcode.html", true);
        $shortcoder_data['io_category_cart']['content'] = $io_category_cart_shortcode_content;
    }else{
        if (strpos($shortcoder_data['io_category_cart']['content'],'[sc name="io_cart" page="%%page%%"]') !==FALSE) {
            $shortcoder_data['io_category_cart']['content'] = str_replace('[sc name="io_cart" page="%%page%%"]','',$shortcoder_data['io_category_cart']['content']);
        }
    }

    if (empty($shortcoder_data['io_city_landing_page_links']) ) {
        $shortcoder_data['io_city_landing_page_links']['autoupdate'] = 0;//off bc we want the setting to control it
        if(get_option('io_brand') === "PW"){
            $shortcoder_data['io_city_landing_page_links']['content'] = 'Professional service for <span class="io_city_list">%%landing_pages%%</span> and surrounding areas. Please submit a quote or contact us to be sure we service your area.';
        }else{
            $shortcoder_data['io_city_landing_page_links']['content'] = 'Professional delivery to <span class="io_city_list">%%landing_pages%%</span> and surrounding areas. Please submit a quote or contact us to be sure we service your area.';
        }
    }

    if (empty($shortcoder_data['eh_additional_description']) ) {
        $shortcoder_data['eh_additional_description']['autoupdate'] = 1;
        $shortcoder_data['eh_additional_description']['content'] = '
<div class="eh_additional_description" style="display:none">
    <ul>
    <li><strong>DELIVERY:</strong> We deliver to %%landing_pages%% and surrounding areas (delivery fees calculated at checkout).</li>
    <li><strong>COUPONS:</strong><a href="/coupon-form/">Click here to check for coupons.</a></li>
    <li><strong>RAIN POLICY:</strong> You can reschedule your rental due to rain at no extra cost as long as 24 hour notice is given.</li>
    <li><strong>OCCUPANCY:</strong> A maximum of 5-6 kids of equal size at one time for bounce houses, and 1 person at a time for slides is what most inflatable manufacturers recommend.</li>
    <li><strong>CLEANING:</strong> All units are cleaned and sanitized before use.</li>
    <li><strong>WHAT\'S INCLUDED:</strong>
        <ul class="square">
        <li>Extension Cord</li>
        <li>Blower</li>
        <li>Safety Stakes to secure to ground</li>
        </ul>
    </li>
    <li><strong>WHAT YOU\'LL NEED:</strong>
        <ul class="square">
        <li>Enough space for us to setup your rental</li>
        <li>Electric outlet within 50 feet of setup area</li>
        <li>Water source and water hose if renting a water slide</li>
        </ul>
    </li>    
    </ul>
</div>
';
    }

    if (empty($shortcoder_data['io_form_delivery']) ) {
        $shortcoder_data['io_form_delivery']['autoupdate'] = 1;
        $io_form_delivery_shortcode_content = file_get_contents("includes/io_form_delivery_shortcode.html", true);
        $shortcoder_data['io_form_delivery']['content'] = $io_form_delivery_shortcode_content;
    }

    if (empty($shortcoder_data['io_map_container']) ) {
        $shortcoder_data['io_map_container']['autoupdate'] = 1;
        $io_map_container_shortcode_content = file_get_contents("includes/io_map_container_shortcode.html", true);
        $shortcoder_data['io_map_container']['content'] = $io_map_container_shortcode_content;
    }

// Register the geolocation shortcode
    if (empty($shortcoder_data['io_geolocation']) )
    {
        $shortcoder_data['io_geolocation']['autoupdate'] = 1;
        $io_geolocation_shortcode_content = file_get_contents("includes/io_customer_geolocation_shortcode.html", true);
        $shortcoder_data['io_geolocation']['content'] = $io_geolocation_shortcode_content;
    }

    update_option( "shortcoder_data", $shortcoder_data );

}

//register_activation_hook( __FILE__, 'sc_setup' );




// Load languages
load_plugin_textdomain('shortcoder', false, basename(dirname(__FILE__)) . '/languages/');


// Add admin menu
/*
function sc_add_menu() {
   add_options_page( 'Shortcoder', 'Shortcoder', 'manage_options', 'shortcoder', 'sc_admin_page' );
}

add_action('admin_menu','sc_add_menu');
*/


// Load the Javascripts
function sc_admin_js(){
    // Check whether the page is the Shortcoder admin page.
    if (isset($_GET['page']) && $_GET['page'] == 'shortcoder'){
        wp_enqueue_script(array(
            'jquery',
            'jquery-ui-core',
            'jquery-ui-draggable',
            'jquery-ui-droppable'
        ));
        wp_enqueue_script('shortcoder-admin-js', SC_URL . '/sc-admin-js.js?v=' . SC_VERSION);
    }
}
add_action('admin_print_scripts', 'sc_admin_js');



// Load the CSS
function sc_admin_css(){
    if (isset($_GET['page']) && $_GET['page'] == 'shortcoder') {
        wp_enqueue_style('shortcoder-admin-css', SC_URL . '/sc-admin-css.css?v=' . SC_VERSION);
    }
}
add_action('admin_print_styles', 'sc_admin_css');



// Plugin on activate fixes
/*
function sc_onactivate(){
   $sc_options = get_option('shortcoder_data');
   $sc_flags = get_option('shortcoder_flags');

   // Move the flag version fix to sc_flags option
   if(isset($sc_options['_version_fix'])){
      unset($sc_options['_version_fix']);
      update_option('shortcoder_data', $sc_options);
   }

   $sc_flags['version'] = SC_VERSION;
   update_option('shortcoder_flags', $sc_flags);

}
register_activation_hook(__FILE__, 'sc_onactivate');
*/



// Register the shortcode
add_shortcode('sc', 'shortcoder');

function shortcoder_all_ok($name){

    $sc_options = get_option('shortcoder_data');
    if(!isset($sc_options[$name]['disabled'])) $sc_options[$name]['disabled'] = 0;

    if($sc_options[$name]['disabled'] == 0){
        // Ensure 'hide_admin' key is set
        if(isset($sc_options[$name]['hide_admin'])) {
            if (current_user_can('level_10') && $sc_options[$name]['hide_admin'] == 1) {
                return false;
            } else {
                return true;
            }
        }else{
            return true;
        }
    }else{
        return false;
    }
}

// Main function
function shortcoder($atts, $content) {
    foreach($atts as $att){
        $att = str_replace('<image', '<img', $att);
    }

    $sc_options = get_option('shortcoder_data');

    // Get the Shortcode name
    if(isset($atts[0])){
        $sc_name = str_replace(array('"', "'", ":"), '', $atts[0]);
        unset($atts[0]);
    }else{
        // Old version with "name" param support
        if(array_key_exists("name", $atts)){
            $tVal = $atts['name'];
            if(array_key_exists($tVal, $sc_options)){
                $sc_name = $tVal;
                unset($atts['name']);
            }
        }
    }

    if(!isset($sc_name)){
        return '';
    }

    // Check whether shortcoder can execute
    if(shortcoder_all_ok($sc_name)){

        $sc_content_final = '';

        // If SC has parameters, then replace it
        if(!empty($atts)){
            $keys = array();
            $values = array();
            $i = 0;

            // Seperate Key and value from atts
            foreach($atts as $k=>$v){
                if($k !== 0){
                    //replace first any where the variable is in a dbl quote contained string and remove quotes
                    $keys[$i] = '"%%' . $k . '%%"';
                    $values[$i] = '"'.str_replace("''",'',$v).'"';
                    $i++;

                    //normal variable replace
                    $keys[$i] = "%%" . $k . "%%";
                    $values[$i] = $v;
                }
                $i++;
            }

            // Replace the params
            if($sc_name != "io") {
                $sc_content = $sc_options[$sc_name]['content'];
                $sc_content_rep1 = str_replace($keys, $values, $sc_content);
                $sc_content_final = preg_replace('/%%[^%\s]+%%/', '', $sc_content_rep1);
            }

        }
        else{

            // If the SC has no params, then replace the %vars%
            if($sc_name != "io"){
                $sc_content = $sc_options[$sc_name]['content'];
                $sc_content_final = preg_replace('/%%[^%\s]+%%/', '', $sc_content);
            }
        }
        $inline_script = "";
        if($sc_name === "io_category"){
            $rental_id = $atts["rental_id"];

            $category_data = array(
                'indoor_outdoor' => isset($atts["indoor_outdoor"]) ? $atts["indoor_outdoor"] : "",
                'staff' => isset($atts["staff"]) ? $atts["staff"] : "",
                'electric' => isset($atts["electric"]) ? $atts["electric"] : "",
                'dimensions' => isset($atts["dimensions"]) ? $atts["dimensions"] : "",
                'rental_id' => isset($atts["rental_id"]) ? $atts["rental_id"] : "",
                'rental_name' => isset($atts["rental_name"]) ? $atts["rental_name"] : "",
                'images_arr' => isset($atts["images_arr"]) ? str_replace("''", '"', $atts["images_arr"]) : ""
            );

            $inline_script = sprintf(
                "<script>window.io_category_data = window.io_category_data || {}; window.io_category_data['%s'] = %s;</script>",
                $rental_id,
                wp_json_encode($category_data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS)
            );
        }
        $sc_content_final = $inline_script.$sc_content_final;
        $sc_content_final = do_shortcode( $sc_content_final );
        $sc_content_final = sc_replace_dblqt_content( $sc_content_final );//#4235

        return "<!-- Start Shortcoder content -->" . ( $sc_content_final ) . "<!-- End Shortcoder content -->";

    }else{
        return '';
    }
}
// Hooks into pre_render_block filter in blocks.php to prevent it from adding p tags to inventory pages.
add_filter( 'pre_render_block', function( $pre_render,  $parsed_block, $parent_block ) {
    if ( 'core/shortcode' === $parsed_block['blockName'] &&
        ( strpos( $parsed_block['innerHTML'], 'io_product_container' ) !== false ||
         strpos( $parsed_block['innerHTML'], 'io_category_wrapper' ) !== false) ) {
        return $parsed_block['innerHTML'];
    }
    return null;
}, 10, 3 );


// Workaround for shortcoder not working in WordPress 4.4
function sc_replace_colon_content( $content ){
    return str_replace( '[sc:', '[sc name=', $content );
}
add_filter( 'the_content', 'sc_replace_colon_content', 5 );

function sc_img_change( $content ){
    if(strpos($content, "[sc :io_") !== false){
        return str_replace( '<img', '<image', $content );
    }else{
        return $content;
    }
}
add_filter( 'the_content', 'sc_img_change', 6 );


// jpa #4235 issue we replace " with &quote; and now need to switch it back only on output
function sc_replace_dblqt_content( $content ){
    return str_replace( "''", '"', $content );
}
//add_filter( 'the_content', 'sc_replace_dblqt_content', 6 );//don't need this bc it's called above in fenc shortcoder()

// Shortcoder admin page
function sc_admin_page(){

    $sc_updated = false;
    $sc_options = get_option('shortcoder_data');
    $sc_flags = get_option('shortcoder_flags');

    $title = __( "Create a Shortcode", 'shortcoder' );
    $button = __( "Create Shortcode", 'shortcoder' );
    $edit = 0;
    $sc_content = '';
    $sc_disable = 0;
    $sc_hide_admin = 0;
    $sc_autoupdate = 0;

    // Insert shortcode
    if (isset($_POST["sc_form_main"]) && $_POST["sc_form_main"] == '1' && check_admin_referer('shortcoder_create_form')){
        $sc_options = get_option('shortcoder_data');
        $sc_name = stripslashes($_POST['sc_name']);

        $io_sc_history = get_option('io_sc_history');

        $sc_post_disabled = isset( $_POST['sc_disable'] ) ? intval( $_POST['sc_disable'] ) : 0;
        $sc_post_hideadmin = isset( $_POST['sc_hide_admin'] ) ? intval( $_POST['sc_hide_admin'] ) : 0;
        $sc_post_autoupdate = isset( $_POST['sc_autoupdate'] ) ? intval( $_POST['sc_autoupdate'] ) : 0;
        $content = str_replace('\"','"',$_POST['sc_content']);//fixed slashes from post
        $content = str_replace("\\\\","\\",$content);//fixes slashes being increased
        $content = str_replace("\'","'",$content);//fixes single quote slash issue
        if($io_sc_history){
            if($io_sc_history[$sc_name]){
                if(end($io_sc_history[$sc_name])['content'] != $content){
                    array_unshift($io_sc_history[$sc_name], Array('content'=>$sc_options[$sc_name]['content'], 'date'=>time()));
                }
            }else{
                $io_sc_history[$sc_name] = Array();
                array_unshift($io_sc_history[$sc_name], Array('content'=>$sc_options[$sc_name]['content'], 'date'=>time()));
            }
        }else{
            $io_sc_history = Array($sc_name => Array());
            array_unshift($io_sc_history[$sc_name], Array('content'=>$sc_options[$sc_name]['content'], 'date'=>time()));
        }
        $sc_options[$sc_name] = array(
            'content' => $content,
            'disabled' => $sc_post_disabled,
            'hide_admin' => $sc_post_hideadmin,
            'autoupdate' => $sc_post_autoupdate
        );

        // Updating the DB
        update_option("shortcoder_data", $sc_options);
        update_option("io_sc_history", $io_sc_history, false);
        $sc_updated = true;

        // Insert Message
        if($sc_updated == 'true'){
            echo '<div class="message updated fade"><p>' . __('Shortcode updated successfully !', 'shortcoder') . '</p></div>';

            //jpa - let's keep it in the editor take make it easier to continue editing
            $sc_name_edit = $_POST['sc_name'];
            $sc_content = ($sc_options[$sc_name_edit]['content']);
            if(isset($sc_options[$sc_name_edit]['disabled'])) $sc_disable = $sc_options[$sc_name_edit]['disabled'];
            if(isset($sc_options[$sc_name_edit]['hide_admin'])) $sc_hide_admin = $sc_options[$sc_name_edit]['hide_admin'];
            $sc_autoupdate = $sc_options[$sc_name_edit]['autoupdate'];
        }else{
            echo '<div class="message error fade"><p>' . __('Unable to create shortcode !', 'shortcoder') . '</p></div>';
        }
    }

    // Edit shortcode
    if (isset($_POST["sc_form_edit"]) && $_POST["sc_form_edit"] == '1' && check_admin_referer('shortcoder_edit_form')){
        $sc_options = get_option('shortcoder_data');
        $sc_name_edit = stripslashes($_POST['sc_name_edit']);

        if($_POST["sc_form_action"] == "edit"){
            //$sc_content = stripslashes($sc_options[$sc_name_edit]['content']);
            $sc_content = ($sc_options[$sc_name_edit]['content']);
            if(isset($sc_options[$sc_name_edit]['disabled'])) $sc_disable = $sc_options[$sc_name_edit]['disabled'];
            if(isset($sc_options[$sc_name_edit]['hide_admin'])) $sc_hide_admin = $sc_options[$sc_name_edit]['hide_admin'];
            $sc_autoupdate = $sc_options[$sc_name_edit]['autoupdate'];

            $title = __( 'Edit this Shortcode - ', 'shortcoder' ) . '<small>' . $sc_name_edit . '</small>';
            $button = __( 'Update Shortcode', 'shortcoder' );
            $edit = 1;
        }else{
            unset($sc_options[$sc_name_edit]);
            unset($sc_name_edit);
            update_option("shortcoder_data", $sc_options);
            echo '<div class="message updated fade"><p>' . __('Shortcode deleted successfully !', 'shortcoder') . '</p></div>';
        }
    }
    if(!isset($sc_name_edit)) $sc_name_edit = '';

    add_thickbox()
    ?>

    <!-- Shortcoder Admin page -->

    <div class="sc_modal">
        <span class="sc_modal_close"></span>
        <div></div>
    </div>

    <div class="wrap">
        <?php sc_admin_buttons('fbrec'); ?>
        <h2>
            <img width="32" height="32" src="<?php echo SC_URL; ?>/images/shortcoder.png" align="absmiddle" /> Shortcoder
            <sup class="smallText"> v
                <?php echo SC_VERSION; ?>
            </sup>
        </h2>

        <ul class="sc_share_wrap" style="display:none">
            <li class="sc_donate" data-width="300" data-height="220" data-url="<?php echo SC_URL . '/js/share.php?i=1'; ?>">
                <a href="#"></a>
            </li>
            <li class="sc_share" data-width="350" data-height="85" data-url="<?php echo SC_URL . '/js/share.php?i=2'; ?>">
                <a href="#"></a>
            </li>
        </ul>

        <div id="content">

            <h3>
                <?php echo $title; ?>
                <?php if($edit == 1) echo '<span class="button sc_back">&lt;&lt; ' . __( "Back", 'shortcoder' ) . '</span>'; ?> </h3>

            <form method="post" id="sc_form">

                <div class="sc_section">
                    <label for="sc_name" class="sc_fld_title">
                        <?php _e( "Title:", 'shortcoder' ); ?>
                    </label>
                    <span class="sc_name_wrap">
                        <input type="text" name="sc_name" id="sc_name" value="<?php echo $sc_name_edit; ?>"
                               placeholder="Enter a shortcode name" class="widefat" required="required" />
                        <div id="sc_code"></div>
                    </span>
                </div>


                <div class="sc_section">
                    <label for="sc_content" class="sc_fld_title">
                        <?php _e( "Content:", 'shortcoder' ); ?>
                    </label>
                    <textarea rows="18" cols="80" name="sc_content" id="sc_content" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"><?php echo $sc_content ?></textarea>
                </div>


                <div class="sc_section">
                    <p>
                        <b>Restore Default:</b> Check box below, hit update, and then re-load the page.
                    </p>
                </div>
                <?php
                    $io_sc_history = get_option('io_sc_history');
                    if($io_sc_history[$sc_name_edit]){
                        $sc_revision_select = "<label for='io_sc_history_select'>History</label>";
                        $sc_revision_select .= build_revision_select("io_sc_history_select", $io_sc_history, $sc_name_edit);
                        $io_sc_compare = $io_sc_history;

                        array_unshift($io_sc_compare[$sc_name_edit], array('date'=>"Up to Date Version", 'content'=>file_get_contents("includes/".$sc_name_edit."_shortcode.html", true)));
                        array_unshift($io_sc_compare[$sc_name_edit], array('date'=>"Current Version", 'content'=>$sc_options[$sc_name_edit]['content']));
                        $sc_revision_select_left = "<label for='io_sc_history_comp_left'>Compare</label>";
                        $sc_revision_select_left .= build_revision_select("io_sc_history_comp_left", $io_sc_compare, $sc_name_edit);
                        $sc_revision_select_right = "<label for='io_sc_history_comp_right'>To</label>";
                        $sc_revision_select_right .= build_revision_select("io_sc_history_comp_right", $io_sc_compare, $sc_name_edit);
                        $sc_revision_script ="
                            <script src='../wp-content/plugins/inflatableoffice/diff.js'></script>
                            <script>
                                var io_sc_history = ".json_encode($io_sc_history).";
                                var io_sc_name = '".$sc_name_edit."';
                                jQuery(document).ready(function() {
                                   jQuery('#io_sc_history_select').on('change', function (){
                                       selected_revision = jQuery(this).find('option:selected');
                                       selected_revision_arr = io_sc_history[io_sc_name].filter(function(revision){
                                          if(selected_revision.val() == revision['date']) return true;
                                       });
                                       selected_revision_content = selected_revision_arr[0]['content']
                                       jQuery('#sc_content').val(selected_revision_content);
                                   });
                                });
                            </script>
                        ";
                        $sc_revision_compare = '
                            <a href="#TB_inline?width=600&height=550&inlineId=modal-window-id" class="thickbox">Compare Revisions</a>
                            <div id="modal-window-id" style="display:none;">
                                '.$sc_revision_select_left.$sc_revision_select_right.'
                                <br>
                                <div style="width:100%; height:100%;" id="sc_compare_result"></div>
                            </div>
                            <script>
                            var io_sc_compare = '.json_encode($io_sc_compare).';
                            var io_sc_name = "'.$sc_name_edit.'";
                            function compare_sc_versions(){
                                var left_value = jQuery("#io_sc_history_comp_left").val();
                                var right_value = jQuery("#io_sc_history_comp_right").val();
                                selected_left_arr = io_sc_compare[io_sc_name].filter(function(revision){
                                    if(left_value == revision["date"]) return true;
                                });
                                selected_right_arr = io_sc_compare[io_sc_name].filter(function(revision){
                                    if(right_value == revision["date"]) return true;
                                });
                                if(selected_left_arr.length>0 && selected_right_arr.length > 0){
                                    let left_content = selected_left_arr[0]["content"];
                                    let right_content = selected_right_arr[0]["content"];
                                    
                                    var diff = JsDiff.diffLines(left_content, right_content),
                                    display = document.getElementById("sc_compare_result");
                                    fragment = document.createDocumentFragment();
                                    display.innerHTML = "";
                                    diff.forEach((part) => {                                        
                                        // green for additions, red for deletions
                                        // grey for common parts
                                        const color = part.added ? "green" :
                                        part.removed ? "red" : "grey";
                                        // if its grey grab first 50 chars and last 50 then replace rest with ...
                                        let split_part = part["value"].split(/\r?\n|\r|\n/g);
                                        if(split_part.length > 20 && color == "grey"){
                                            debugger;
                                            let temp_arr = split_part.slice(0,5);
                                            temp_arr.push("...")
                                            temp_arr = temp_arr.concat(split_part.slice(-5));
                                            split_part = temp_arr;
                                        }
                                        split_part.forEach((line) => {
                                            debugger;
                                            span = document.createElement("span");
                                            span.style.color = color;
                                            span.appendChild(document
                                                .createTextNode(line));
                                            fragment.appendChild(span);
                                            fragment.appendChild(document.createElement("br"));
                                            
                                        });
                                    });
                                    display.appendChild(fragment);
                                }
                            }
                                
                            jQuery(document).ready(function (){
                                jQuery("#io_sc_history_comp_left").on("change", function (){
                                       compare_sc_versions()
                                });
                                jQuery("#io_sc_history_comp_right").on("change", function (){
                                       compare_sc_versions()
                                });
                            });
                            </script>
                        ';
                        echo $sc_revision_select;
                        echo $sc_revision_script;
                        echo $sc_revision_compare;
                    }
                ?>
                <div class="sc_section">

                    <table width="100%">
                        <tr>

                            <td width="50%" class="sc_settings">
                                <!--
                                <label>
                                    <input name="sc_disable" type="checkbox" value="1" <?php echo $sc_disable=="1" ?
                                    'checked="checked"' : ""; ?>/>
                                    <?php _e( "Temporarily disable this shortcode", 'shortcoder' ); ?>
                                </label>
                                <label>
                                    <input name="sc_hide_admin" type="checkbox" value="1" <?php echo $sc_hide_admin=="1" ?
                                    'checked="checked"' : ""; ?>/>
                                    <?php _e( "Disable this Shortcode to admins", 'shortcoder' ); ?>
                                </label>
                                -->
                                <label>
                                    <input name="sc_autoupdate" type="checkbox" value="1" <?php echo $sc_autoupdate=="1" ?
                                        'checked="checked"' : ""; ?>/>
                                    <?php _e( "Keep this shortcode updated to latest defaults (will overwrite any edits)", 'shortcoder' ); ?>
                                    <script>
                                        jQuery("#sc_content").change(function(){
                                            jQuery("[name=sc_autoupdate]").prop('checked','');
                                        });
                                    </script>
                                </label>
                            </td>

                            <td>
                                <p align="right">
                                    <input type="submit" name="sc_submit" id="sc_submit" class="button-primary" value="<?php echo $button; ?>"
                                    />
                                </p>
                            </td>

                        </tr>
                    </table>

                </div>

                <?php wp_nonce_field('shortcoder_create_form'); ?>
                <input name="sc_form_main" type="hidden" value="1" />
            </form>

            <h3>
                <?php _e( "Created Shortcodes", 'shortcoder' ); ?>
                <small>(
                    <?php _e( "Click to edit", 'shortcoder' ); ?>)</small>
            </h3>
            <form method="post" id="sc_edit_form">
                <ul id="sc_list" class="clearfix">
                    <?php
                    $sc_options = get_option('shortcoder_data');
                    if(is_array($sc_options)){
                        foreach($sc_options as $key=>$value){
                            if($key != "io_city_landing_page_links") echo '<li>' . $key . '</li>';
                        }
                    }
                    ?>
                </ul>

                <?php wp_nonce_field('shortcoder_edit_form'); ?>
                <input name="sc_form_edit" type="hidden" value="1" />
                <input name="sc_form_action" id="sc_form_action" type="hidden" value="edit" />
                <input name="sc_name_edit" id="sc_name_edit" type="hidden" />
            </form>

            <div id="sc_delete" title="Drag & drop shortcodes to delete"></div>

        </div>
        <!-- Content -->

        <br/>
        <p align="center" style="display:none">
            <a href="http://www.aakashweb.com/forum/" target="_blank">Report bugs</a> |
            <a href="http://www.aakashweb.com/forum/" target="_blank">Support Forum</a> |
            <a href="http://www.aakashweb.com/wordpress-plugins/shortcoder/" target="_blank">Help</a> |
            <a href="http://bit.ly/scdonate" target="_blank">Donate</a>
            <br/>
            <br/>
            <a href="#" class="sc_open_video">(Demo video)</a>
            <br />
            <br />
            <a href="https://twitter.com/vaakash" target="_blank">Follow @vaakash</a>
            <br/>
            <br/>
            <a href="http://www.aakashweb.com/" target="_blank" class="sc_credits">a plugin from Aakash Web</a>
        </p>

        <script>
            var sc_version = '<?php echo SC_VERSION; ?>';
        </script>

    </div>
    <!-- Wrap -->

    <?php
}



// Helper for SC TinyMCE button
function sc_admin_footer(){

    if (isset($_GET['page']) && $_GET['page'] == 'shortcoder')
        return;

    echo "
<script>
window.onload = function(){
   if( typeof QTags === 'function' )
      QTags.addButton( 'QT_sc_insert', 'Shortcoder', sc_show_insert );
}
function sc_show_insert(){
   tb_show('Insert a Shortcode', '" . SC_URL . "/sc-insert.php?TB_iframe=true');
}
</script>
";
}
add_action('admin_footer', 'sc_admin_footer');




function sc_admin_buttons($type){
    switch($type){
        case 'fbrec':
            //echo '<iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Ffacebook.com%2Faakashweb&amp;send=false&amp;layout=button_count&amp;width=130&amp;show_faces=false&amp;action=recommend&amp;colorscheme=light&amp;font=arial&amp;height=21&amp;appId=106994469342299" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width: 130px; height:21px;float: right;margin-top: 15px;" allowtransparency="true"></iframe>';
            break;
    }
}




// Action Links
function sc_plugin_actions($links, $file){
    static $this_plugin;
    global $sc_donate_link;

    if(!$this_plugin) $this_plugin = plugin_basename(__FILE__);
    if( $file == $this_plugin ){
        $settings_link = "<a href='$sc_donate_link' title='If you like this plugin, then just make a small Donation to continue this plugin development.' target='_blank'>" . __('Make Donations', 'shortcoder') . '</a> ';
        $links = array_merge(array($settings_link), $links);
    }
    return $links;
}
//add_filter('plugin_action_links', 'sc_plugin_actions', 10, 2);




// Shortcoder tinyMCE buttons
function sc_addbuttons() {
    if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') )
        return;

    if (isset($_GET['page']) && $_GET['page'] == 'shortcoder')
        return;

    if ( get_user_option('rich_editing') == 'true') {
        add_filter("mce_external_plugins", "sc_add_tinymce_plugin");
        add_filter('mce_buttons', 'sc_register_button');
    }
}

function sc_register_button($buttons) {
    array_push($buttons, "separator", "scbutton");
    return $buttons;
}

function sc_add_tinymce_plugin($plugin_array) {
    $plugin_array['scbutton'] = SC_URL . '/js/tinymce/editor_plugin.js';
    return $plugin_array;
}

function build_revision_select($id, $io_sc_history, $sc_name_edit){
    $sc_revision_select = "<select style='margin-left:5px' id='".$id."'><option value='' style='display:none' disabled selected>Select version to load...</option>";
    foreach($io_sc_history[$sc_name_edit] as $revision){
        if(gettype($revision['date']) == "string"){
            $sc_revision_select .= "<option value='".$revision['date']."'>".$revision['date']."</option>";
        }else{
            $sc_revision_select .= "<option value=".$revision['date'].">".date('m/d/Y h:i', $revision['date'])."</option>";
        }
    }
    return $sc_revision_select .= "</select>";
}

add_action('init', 'sc_addbuttons'); // init process for button control