<?php
/*ccccccccccccc*/
add_action("add_meta_boxes", "legel_eagel_main");

function legel_eagel_main() {
    add_meta_box("legal_eagel_main", "Legal Eagle", "legel_eagel_function", array("post" , "page"), "side", "high", null);
}
function legel_eagel_function($object){
?>
    <button class="collapsible">Legel Eagle Popups 
      <span class="collapsible-arrow">
        <svg class="arrow-open" width="24px" height="24px"><g><path d="M7.41,8.59L12,13.17l4.59-4.58L18,10l-6,6l-6-6L7.41,8.59z"></path></g></svg> 
        <svg class="arrow-close" width="24px" height="24px"><g><path d="M12,8l-6,6l1.41,1.41L12,10.83l4.59,4.58L18,14L12,8z"></path></g></svg>
      </span>      
    </button>

    <div class="content">
      <p><?php require_once('popups_meta_box_shortcode.php');?></p>
    </div>
    <button class="collapsible">Legel Eagle Template
      <span class="collapsible-arrow">
        <svg class="arrow-open" width="24px" height="24px"><g><path d="M7.41,8.59L12,13.17l4.59-4.58L18,10l-6,6l-6-6L7.41,8.59z"></path></g></svg> 
        <svg class="arrow-close" width="24px" height="24px"><g><path d="M12,8l-6,6l1.41,1.41L12,10.83l4.59,4.58L18,14L12,8z"></path></g></svg>
      </span> 
    </button>
    <div class="content">
      <p><?php custom_legel_eagel_short_code();?></p>
    </div>
    <button class="collapsible">Legal Eagle General
      <span class="collapsible-arrow">
        <svg class="arrow-open" width="24px" height="24px"><g><path d="M7.41,8.59L12,13.17l4.59-4.58L18,10l-6,6l-6-6L7.41,8.59z"></path></g></svg> 
        <svg class="arrow-close" width="24px" height="24px"><g><path d="M12,8l-6,6l1.41,1.41L12,10.83l4.59,4.58L18,14L12,8z"></path></g></svg>
      </span> 
    </button>
    <div class="content">
      <p><?php custom_legel_eagel_short_code_general();?></p>
    </div>
    <button class="collapsible">Legal Eagle Page Setttings
      <span class="collapsible-arrow">
        <svg class="arrow-open" width="24px" height="24px"><g><path d="M7.41,8.59L12,13.17l4.59-4.58L18,10l-6,6l-6-6L7.41,8.59z"></path></g></svg> 
        <svg class="arrow-close" width="24px" height="24px"><g><path d="M12,8l-6,6l1.41,1.41L12,10.83l4.59,4.58L18,14L12,8z"></path></g></svg>
      </span> 
    </button>
    <div class="content">
      <p><?php require_once('meta_tmp.php');?></p>
    </div>
      <?php
}
/*cccccccccccc*/

/* ----------------- LEGEL EAGLE short code start --------------------- */

function custom_legel_eagel_short_code()
{
    global $shortcode_tags;
    global $wpdb;

    $result = $wpdb->get_results("SELECT * FROM `wp_legal_pages`");
    $count = 0;
    ?>
    <div class='copied'> Copied to clipboard</div>
    <select class="Legal_Eagel_select_box" id="custom_legel_eagel_select_box">
    <?php
    foreach($result as $tag => $code)
    {
        $count++;
        ?>
        <option value="[wp-legalpage tid=<?php echo $count;?>]"><?php echo $code->title;?></option>
        <?php
    }
    ?>
    </select>
    <span class="le-shortcode-area">
      <!-- The text field -->
      <input type="text" id="legel_eagel_shortcode_input">
      <!-- The button used to copy the text -->
      <button class="legal-button btn-blk" id="legel_eagel_CpyBtn">
          
          <span>COPY</span>
      </button>
    </span>
    <?php
}
/* ----------------- LEGEL EAGLE short code end --------------------- */


/* ----------------- LEGEL EAGLE General short code start --------------------- */

function custom_legel_eagel_short_code_general()
{
    global $shortcode_tags;
    global $wpdb;

    $result = $wpdb->get_results("SELECT * FROM `wp_legal_pages`");
    $count = 0;
    ?>
    <div class='copied1'> Copied to clipboard</div>

    <select class="Legal_Eagel_General_select_box" id="custom_legel_eagel_general_select_box">
      <option value="[Domain]">Domain Name</option>
      <option value="[Business Name]">Business Name</option>
      <option value="[Phone]">Phone</option>
      <option value="[Street]">Street</option>
      <option value="[City, State, Zip code]">City, State, Zip code</option>
      <option value="[Country]">Country</option>
      <option value="[Email]">Email</option>
      <option value="[Address]">Full Mailing Address</option>
      <option value="[Niche]">Niche</option>
      <option value="[Facebook-url]">Facebook URL</option>
      <option value="[Google-url]">Google URL</option>
      <option value="[Twitter-url]">Twitter URL</option>
      <option value="[LinkedIn-url]">LinkedIn URL</option>
    </select>
    <span class="le-general-shortcode-area">
      <!-- The text field -->
      <input type="text" id="legel_eagel_general_shortcode_input">
      <!-- The button used to copy the text -->
      <button class="legal-button btn-blk" id="legel_eagel_general_CpyBtn">
         
          <span>COPY</span>
      </button>
    </span>
    <?php
}
/* ----------------- LEGEL EAGLE General short code end --------------------- */

/*function custom_meta_box_markup_eu_cookies($object) { 
         require_once('meta_tmp.php');
}

function add_custom_meta_box() {
    add_meta_box("eu_cookies", "Wp Legal Eagle Page Setttings", "custom_meta_box_markup_eu_cookies", array("post" , "page"), "side", "high", null);
}

$lp_eu_get_visibility   = get_option('lp_eu_cookie_enable');*/

//if ($lp_eu_get_visibility =='ON') {
    //add_action("add_meta_boxes", "add_custom_meta_box");
//}

new Shortcode_Tinymce();
class Shortcode_Tinymce
{
    public function __construct()
    {
        add_action('admin_init', array($this, 'pu_shortcode_button'));
        add_action('admin_footer', array($this, 'pu_get_shortcodes'));
    }

  public function pu_shortcode_button()
    {
        if( current_user_can('edit_posts') &&  current_user_can('edit_pages') )
        {
            add_filter( 'mce_external_plugins', array($this, 'pu_add_buttons' ));
            add_filter( 'mce_buttons', array($this, 'pu_register_buttons' ));
        }
    }

   public function pu_add_buttons( $plugin_array )
    {
        $plugin_array['pushortcodes'] = plugin_dir_url( __FILE__ ) . 'js/shortcode-tinymce-button.js';
        return $plugin_array;
    }

   public function pu_register_buttons( $buttons )
    {
        array_push( $buttons, 'separator', 'pushortcodes' );
        return $buttons;
    }

   public function pu_get_shortcodes()
    {
        global $shortcode_tags;
        global $wpdb;

        $result = $wpdb->get_results("SELECT * FROM `wp_legal_pages`");
        echo '<script type="text/javascript">
        var shortcodes_button = new Array();';
        $count = 0;

        foreach($result as $tag => $code)
        {
            $c = json_encode($code);
            echo "shortcodes_button[{$code->id}] = '{$code->title}';";
            $count++;
        }
        echo '</script>';
    }
}



add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

add_action( 'wp_ajax_uc_c_my_action', 'uc_c_my_action' );


add_action( 'wp_ajax_uc_c_my_adult', 'uc_c_my_adult' );

function uc_c_my_action() {
    global $wpdb;
    $checkpost =  $_POST['checkbox'];
    $post_id = $_POST['post_id'];
    if (trim($checkpost) == "lp_eu_status=ON") {
      update_option('lp_eu_cookie_enable_' . $post_id  , 'ON');        
      setcookie('lp_eu_agree_'. $post_id, 'NOT_ACCEPTED', -1, '/');
    } else {
        update_option('lp_eu_cookie_enable_' . $post_id  ,'OFF');
    }
    wp_die();
}

function uc_c_my_adult() {
  
    global $wpdb;

    $checkpost =  $_POST['checkbox'];
    $post_id = $_POST['post_id'];
    if (trim($checkpost) == "lp-is_adult_page=ON") {
        update_option('lp_eu_adult_page_enable_' . $post_id  , 'ON');
    } else {
        update_option('lp_eu_adult_page_enable_' . $post_id  ,'OFF');
    }
    $host = $_SERVER['HTTP_HOST'];
    $parmalink = get_permalink( $post_id );
    $parmalink = rtrim($parmalink,"/");
    $link = explode($host, $parmalink);
    setcookie('is_user_adult', null, -1, $link[1]);
    wp_die();
}


add_action( 'wp_ajax_uc_c_my_action_remove_popup_single_page', 'uc_c_my_action_remove_popup_single_page' );

function uc_c_my_action_remove_popup_single_page() {
    global $wpdb;
   
        $result = $wpdb->get_results("SELECT * FROM `wp_lp_popups`");

        $string  = "";
         $string .= '<select  id="popup_select_field">';
          $string .=  '<option>Legal Eagle Popups</option>';
 
  foreach ($result as $tag => $code) {
     $string .= '<option    value="'. $code->id .'">'. $code->popupName .'</option>';
  }

 $string .= '</select>';

 $string .=  '<input id="submit_popup_button" value="Add Popup" type="submit" class="legal-button btn-blk" />';

   // $field_value = $_POST["field_value"];
    $post_id = $_POST["post_id"];
    setcookie('lp_eu_terms_condition__popup_'.$post_id, null, -1,'/');
   $result = delete_option("popups_shortcode_" . $post_id );
   $arr = array('result' => $result , 'content' =>  $string);

echo json_encode($arr);
   // echo $result;
    wp_die();
}



add_action( 'wp_ajax_uc_c_my_action_single_page', 'uc_c_my_action_single_page' );

function uc_c_my_action_single_page() {
    global $wpdb;
    $field_value = $_POST["field_value"];

     $result = $wpdb->get_results("SELECT * FROM `wp_lp_popups`");

        $string  = "";
         $string .= '<select disabled id="popup_select_field">';
          $string .=  '<option>Legal Eagle Popups</option>';
 
  foreach ($result as $tag => $code) {
     $string .= '<option  '. ($field_value == $code->id ? " selected " : "") .'  value="'. $code->id .'">'. $code->popupName .'</option>';
  }

 $string .= '</select>';

 $string .=  '<input id="submit_popup_button" value="Remove Popup" type="submit" class="legal-button btn-blk"/>';


    
    $post_id = $_POST["post_id"];
    $result = add_option( 'popups_shortcode_' . $post_id , $field_value, '', 'yes' );

    $arr = array('result' => $result , 'content' =>  $string);

    echo json_encode($arr);

    wp_die();
}



function wpb_after_post_content($content){
if (is_single() || is_page()) {  

  // if (get_the_ID() == "120") {
    
    $post_value = get_option("popups_shortcode_" . get_the_ID());
    if ( shortcode_exists( 'wp-legalpopup' ) ) {

   if ($post_value != "") {
     $content .= "[wp-legalpopup pid={$post_value}]";
   }
     
     } 
    
  // }    
}
   // return do_shortcode($content . " " . $shot );
    return $content;
}
add_filter( "the_content", "wpb_after_post_content" );


// hooks your functions into the correct filters
function wdm_add_mce_button() {
            // check user permissions
            if ( !current_user_can( 'edit_posts' ) &&  !current_user_can( 'edit_pages' ) ) {
                       return;
               }
           // check if WYSIWYG is enabled
           if ( 'true' == get_user_option( 'rich_editing' ) ) {
               add_filter( 'mce_external_plugins', 'wdm_add_tinymce_plugin' );
               add_filter( 'mce_buttons', 'wdm_register_mce_button' );
               }
}
add_action('admin_head', 'wdm_add_mce_button');

// register new button in the editor
function wdm_register_mce_button( $buttons ) {
            array_push( $buttons, 'wdm_mce_button' );
            return $buttons;
}


// declare a script for the new button
// the script will insert the shortcode on the click event
function wdm_add_tinymce_plugin( $plugin_array ) {
          $plugin_array['wdm_mce_button'] = plugin_dir_url(__FILE__) .'/js/wdm-mce-button.js';
          return $plugin_array;
}
