<?php
/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @package           YT Evo
 *
 * @wordpress-plugin
 * Plugin Name:       YT Evo
 * Description: The YT Evo Plugin for WordPress makes it easy to create posts from YouTube channels, and works seamlessly with most themes and custom post types.
 * Author:            Chris Derenberger
 * License:           GPL-2.0+
 * Version:           2.0.3
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       yt-evolution
 * Domain Path:       /languages
 */

global $tube_video_curator;
$tube_video_curator = Yt_Evolution::init();

require_once YT_EVOLUTION_DIR. '/lib/plugin-update-checker/plugin-update-checker.php';
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
  'https://plexvids.com/wp-content/uploads/yt-evolution/yt-evolution.json',
  __FILE__, //Full path to the main plugin file or functions.php.
  'yt-evolution'
);

class Yt_Evolution {    

  public static $tube_dashboard;  
  
  public static $tube_settings;  
  
  public static $tube_settings_api_keys;  
  
  public static $tube_api_key_nag;  
  
  public static $tube_videos_list; 
  
  public static $tube_find_videos;  
  
  public static $tube_find_sources;  
  
  public static $tube_source_manager;  
  
  public static $tube_youtube_videos;   
  
  public static $tube_vimeo_videos;   
  
  public static $tube_twitch_videos;   
  
  public static $tube_skipped_videos;  
  
  public static $tube_embed;
    
  public static $tube_linkify;  
  
  public static $tube_import_cron;  
  
  public static $ytevo_ez; 
  
  //public static $tube_imgur_tools;  
  
  //public static $tube_go_live; 
  
  //public static $tube_add_via_imgur; 
  
  public static $tube_add_via_url; 
  
  public static $tube_theme_filters;   
  
  public static $instance;
  
  public static function init() {
    
    if ( is_null( self::$instance ) )
        self::$instance = new Yt_Evolution();
    
    return self::$instance;
      
  }
  
  
  // Constructor  
  function __construct() {
    
    // set constant variables for the plugin
    self::set_constants();
    
    add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
    
    // create all the classes used by the plugin
    self::create_classes();
    
    // load the text-domain
    add_action( 'admin_init', array( $this, 'load_tube_vc_textdomain') );
    
    // create the Admin menu
    add_action( 'admin_menu', array( $this, 'create_tube_vc_admin_menu'), 9 );
    
    // customize the admin menu
    add_action( 'admin_menu', array( $this, 'customize_tube_vc_admin_menu'), 99 );
    
    // add action links to the Plugins List page
    add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array( $this, 'add_tube_vc_action_links' ) );
    
    // Init action using for testing autosync capability
    //add_action('init', array( $this, 'test_autosync') );    
    
    // enque the styles for the plugin
    add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_tube_vc_admin_styles') );    
    
    // Activation hook to set the default settings
    register_activation_hook( __FILE__, array( self::$tube_settings, 'set_default_plugin_settings') );
        
    // Activation hook to set the default Find Videos settings
    register_activation_hook( __FILE__, array( self::$tube_find_videos, 'set_default_query_options') );
        
    // Activation hook to set the default Find Sources settings
    register_activation_hook( __FILE__, array( self::$tube_find_sources, 'set_default_query_options') ); 
       // Activation hook to set the default settings
    register_activation_hook( __FILE__, array( self::$tube_settings, 'ytevo_on_activation') );
        
    // Activation hook to set the default Find Videos settings
    register_activation_hook( __FILE__, array( self::$tube_settings, 'ytevo_install_data') );
        
   
    // Deactivation hook to remove databse tables
    register_deactivation_hook( __FILE__, array( self::$tube_settings, 'ytevo_on_deactivation') );
  
     
    
    // Deactivation hook to clear the autosync cron
    register_deactivation_hook( __FILE__, array( self::$tube_import_cron, 'clear_scheduled_autosync_hook') );
  
     
      
    //  WP Ajax stuff
    
    // Create video post
    add_action('wp_ajax_create_tube_video_post_via_ajax', array( $this, 'create_tube_video_post_via_ajax') );
    add_action('wp_ajax_nopriv_create_tube_video_post_via_ajax', array( $this, 'create_tube_video_post_via_ajax') );
    
    // Get all terms for post type
    add_action('wp_ajax_get_all_terms_for_tube_video_post_type_via_ajax', array( $this, 'get_all_terms_for_tube_video_post_type_via_ajax') );
    add_action('wp_ajax_nopriv_get_all_terms_for_tube_video_post_type_via_ajax', array( $this, 'get_all_terms_for_tube_video_post_type_via_ajax') );
    
    // get all taxonomies for post type
    add_action('wp_ajax_get_all_taxonomies_for_tube_video_post_type_via_ajax', array( $this, 'get_all_taxonomies_for_tube_video_post_type_via_ajax') );
    add_action('wp_ajax_nopriv_get_all_taxonomies_for_tube_video_post_type_via_ajax', array( $this, 'get_all_taxonomies_for_tube_video_post_type_via_ajax') );
    
    // Create skipped Video post
    add_action('wp_ajax_create_skipped_tube_video_post_via_ajax', array( $this, 'create_skipped_tube_video_post_via_ajax') );
    add_action('wp_ajax_nopriv_create_skipped_tube_video_post_via_ajax', array( $this, 'create_skipped_tube_video_post_via_ajax') );
    
    // Restore skipped video
    add_action('wp_ajax_restore_skipped_tube_video_post_via_ajax', array( $this, 'restore_skipped_tube_video_post_via_ajax') );
    add_action('wp_ajax_nopriv_restore_skipped_tube_video_post_via_ajax', array( $this, 'restore_skipped_tube_video_post_via_ajax') );

        
  
    if ( is_admin() ) {
        add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
        add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );

        add_filter('plugin_row_meta', array( $this, 'fun_hide_view_details' ),10,4);


    }
  }  

  function fun_hide_view_details($plugin_meta, $plugin_file, $plugin_data, $status)
  {
    if($plugin_data['slug'] == 'yt-evolution')
      unset($plugin_meta[2]);
      return $plugin_meta;
  }


  public function init_metabox() {
        add_action( 'add_meta_boxes', array( $this, 'add_metabox'  )        );
        add_action( 'save_post',      array( $this, 'save_metabox' ), 10, 2 );
    }


    public function add_metabox() {
        add_meta_box(
            'my-meta-box',
            __( 'Video Transcript', 'ytevolution' ),
            array( $this, 'render_metabox' ),
            'post',
            'advanced',
            'default'
        );
 
    }
 
    /**
     * Renders the meta box.
     */
    public function render_metabox( $post ) {
        // Add nonce for security and authentication.
        wp_nonce_field( 'custom_nonce_action', 'custom_nonce' );
         // Add an nonce field so we can check for it later.
  
        // Use get_post_meta to retrieve an existing value from the database.
        $value = get_post_meta( $post->ID, 'video_transcript', true );
  
        // Display the form, using the current value.
        ?>
        <label for="myplugin_new_field">
            <?php //_e( 'Tra', 'textdomain' ); ?>
        </label>
 
         
        <textarea style="width:100%;min-height:200px;" type="textarea" class="form-control" name="video_transcript" ><?php echo esc_attr( $value ); ?></textarea> 
         
        <?php 
    }
 
    
    public function save_metabox( $post_id, $post ) {
        // Add nonce for security and authentication.
        $nonce_name   = isset( $_POST['custom_nonce'] ) ? $_POST['custom_nonce'] : '';
        $nonce_action = 'custom_nonce_action';
 
        // Check if nonce is valid.
        if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) ) {
            return;
        }
 
        // Check if user has permissions to save data.
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
 
        // Check if not an autosave.
        if ( wp_is_post_autosave( $post_id ) ) {
            return;
        }
 
        // Check if not a revision.
        if ( wp_is_post_revision( $post_id ) ) {
            return;
        }

        $video_transcript = filter_var($_POST['video_transcript'], FILTER_SANITIZE_STRING);
        update_post_meta($post_id, 'video_transcript', $video_transcript);
    }
  
  
  // Set up a bunch of constants used by the plugin
  private static function set_constants() {
  
    $constants = array(
      'YT_EVOLUTION_DIR' => plugin_dir_path( __FILE__ ),
      'YT_EVOLUTION_URL' => plugin_dir_url( __FILE__ ),
      'YT_EVOLUTION_SLUG' => 'yt-evolution',
      'YT_EVOLUTION_DASH_URL' => admin_url('admin.php?page=yt-evolution'),
      'YT_EVOLUTION_SETTINGS_URL' => admin_url('admin.php?page=yt-evolution-settings'),
      'YT_EVOLUTION_IMPORT_SETTINGS_URL' => admin_url('admin.php?page=yt-evolution-settings&tab=import'),
      'YT_EVOLUTION_API_KEYS_URL' => admin_url('admin.php?page=yt-evolution-api-keys'),
      'YT_EVOLUTION_VIMEO_API_KEYS_URL' => admin_url('admin.php?page=yt-evolution-api-keys&tab=vimeo-api'),
      'YT_EVOLUTION_TWITCH_API_KEYS_URL' => admin_url('admin.php?page=yt-evolution-api-keys&tab=twitch-api'),
      'YT_EVOLUTION_VIEW_SOURCES_URL' => admin_url( 'edit.php?post_type=tube_source' ),
      'YT_EVOLUTION_FIND_SOURCES_URL' => admin_url( 'admin.php?page=yt-evolution-find-sources' ),
      'YT_EVOLUTION_ADD_VIA_URL_URL' => admin_url( 'admin.php?page=yt-evolution-add-via-url' ),
      'YT_EVOLUTION_FIND_VIDEOS_URL' => admin_url( 'admin.php?page=yt-evolution-find-videos' ),
      'YT_EVOLUTION_SKIPPED_VIDEOS_URL' => admin_url( 'edit.php?post_type=tube_skipped_video' ),
      'YT_EVOLUTION_LOADER' => plugin_dir_url( __FILE__ ) . '/images/tube-loader.gif',
      'YTEVO_CRUATOR_DIR' => plugin_dir_path( __FILE__ ),
      'YTEVO_CRUATOR_URL' => plugin_dir_url( __FILE__ ),
      'YTEVO_CRUATOR_SLUG' => 'ytevo-cruator',
      'YTEVO_CRUATOR_DASH_URL' => admin_url('admin.php?page=ytevo-cruator'),
      'YTEVO_CRUATOR_SETTINGS_URL' => admin_url('admin.php?page=ytevo-cruator'),
      'YTEVO_CRUATOR_IMPORT_SETTINGS_URL' => admin_url('admin.php?page=ytevo-cruator-settings&tab=import'),
      'YTEVO_CRUATOR_API_KEYS_URL' => admin_url('admin.php?page=ytevo-cruator-api-keys'),
      
      'YTEVO_CRUATOR_VIEW_SOURCES_URL' => admin_url( 'edit.php?post_type=ytevo_source' ),
      'YTEVO_CRUATOR_FIND_SOURCES_URL' => admin_url( 'admin.php?page=yt-evolution-find-sources' ),
      'YTEVO_CRUATOR_ADD_VIA_URL_URL' => admin_url( 'admin.php?page=yt-evolution-add-via-url' ),
      'YTEVO_CRUATOR_FIND_VIDEOS_URL' => admin_url( 'admin.php?page=yt-evolution-find-videos' ),
      'YTEVO_CRUATOR_SKIPPED_VIDEOS_URL' => admin_url( 'edit.php?post_type=ytevo_skipped_video' ),
      'YTEVO_CRUATOR_LOADER' => plugin_dir_url( __FILE__ ) . '/images/tube-loader.gif',
    );
    
    foreach ( $constants as $key => $value):
      
      if ( !defined( $key ) ) {
        define( $key, $value );
      }
      
    endforeach;
  }

  function load_textdomain() {
    load_plugin_textdomain( YT_EVOLUTION_SLUG, false, dirname( plugin_basename(__FILE__) ) . '/languages' );
  }
  
  
  private static function create_classes() {
    
    // Get the tubeYouTubeVideos instance
      require_once YT_EVOLUTION_DIR . '/classes/ytevo-curator.php';
      
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-youtube-videos.php';
    self::$tube_youtube_videos = new Yt_EVL_YouTube_Videos();
        
    // Get the Tube_Vimeo_Videos instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-vimeo-videos.php';
    self::$tube_vimeo_videos = new Yt_EVL_Vimeo_Videos();     
        
    // Get the Tube_Twitch_Videos instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-twitch-videos.php';
    self::$tube_twitch_videos = new Yt_EVL_Twitch_Videos();      
    
    // Get the Tube_Settings instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-settings.php';
    self::$tube_settings = new Yt_EVL_Settings();
    
    // Get the Tube_Settings_Api_Keys instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-settings-api-keys.php';
    self::$tube_settings_api_keys = new Yt_EVL_Settings_Api_Keys();
    
    // Get the Tube_Api_Key_Nag instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-api-key-nag.php';
    self::$tube_api_key_nag = new Yt_EVL_Api_Key_Nag();

    // Get the Tube_Dashboard instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-dashboard.php';    
    self::$tube_dashboard = new Yt_EVL_Dashboard();

    // Get the Tube_Linkify instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-linkify.php';
    self::$tube_linkify = new Yt_EVL_Linkify();
    
    // Get the Tube_Videos_List instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-videos-list.php';    
    self::$tube_videos_list = new Yt_EVL_Videos_List();
    
    // Get the Tube_Find_Videos instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-find-videos.php';    
    self::$tube_find_videos = new Yt_EVL_Find_Videos();
    
    // Get the $tube_find_sources instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-find-sources.php';    
    self::$tube_find_sources = new Yt_EVL_Find_Sources();
    
    // Get the tubeSources instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-source-manager.php';    
    self::$tube_source_manager = new Yt_EVL_Source_Manager(); 
       
    // Get the Tube_Skipped_Videos instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-skipped-videos.php';
    self::$tube_skipped_videos = new Yt_EVL_Skipped_Videos();
    
    // Get the Tube_Embed instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-embed.php';
    self::$tube_embed = new Yt_EVL_Embed();
    
    // Get the Tube_Import_Cron instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-import-cron.php';
    self::$tube_import_cron = new Yt_EVL_Import_Cron();
    
    
    // Get the Tube_YouTube_Duration instance [FUTURE]
    // require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-youtube-duration.php';
    
    // Get the Tube_Imgur_Tools instance [FUTURE]
    //require_once YT_EVOLUTION_DIR . '/classes/FUTURE-class-tube-imgur-tools.php';
    //self::$tube_imgur_tools = new Tube_Imgur_Tools();
    
    // Get the Tube_Add_Via_Imgur instance [FUTURE]
    //require_once YT_EVOLUTION_DIR . '/classes/FUTURE-class-tube-add-via-imgur.php';
    //self::$tube_add_via_imgur = new Tube_Add_Via_Imgur();
    
    // Get the Tube_Go_Live instance [FUTURE]
    //require_once YT_EVOLUTION_DIR . '/classes/class-tube-go-live.php';
    //self::$tube_go_live = new Tube_Go_Live();
    
    // Get the Tube_Add_Via_Url instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-add-via-url.php';
    self::$tube_add_via_url = new Yt_EVL_Add_Via_Url();
    
    // Get the Tube_Theme_Filters instance
    require_once YT_EVOLUTION_DIR . '/classes/class-tube-vc-theme-filters.php';
    self::$tube_theme_filters = new Yt_EVL_Theme_Filters();
        
  }

  // function to test the autosycn
  function test_autosync() {
  
    self::$tube_import_cron -> do_autosync();
  
  }
  
  
  // Create the Admin Menu for the plugin
  function create_tube_vc_admin_menu() {

    // Main Menu Page
    $menu_page = add_menu_page(
      __('YT Evolution', 'yt-evolution'), // The Page title
      __('YT Evolution', 'yt-evolution'), // The Menu Title
      'publish_posts', // The capability required for access to this item
      YT_EVOLUTION_SLUG, // the slug to use for the page in the URL
      array( $this::$tube_dashboard, 'render_options_page_dashboard' ),  // The function to call to render the page
      "dashicons-format-video",
      58 // Position
    );      
  
    // Load the Styes / JS conditionally
    add_action( 'load-' . $menu_page, array( $this, 'load_tube_vc_admin_styles') );
              
        
    // Submenu Page: Settings
    $submenu_page_settings = add_submenu_page( 
      YT_EVOLUTION_SLUG, 
      'YT Evolution Settings',
       'Settings',
      'manage_options', 
      YT_EVOLUTION_SLUG .'-settings', 
      array( $this::$tube_settings, 'render_options_page_settings' )
    );     
    // Load the Styes / JS conditionally
    add_action( 'load-' . $submenu_page_settings, array( $this, 'load_tube_vc_admin_styles') );
    add_action( 'load-' . $submenu_page_settings, array( $this::$tube_settings, 'load_tube_vc_settings_scripts') );
  add_action( 'load-' . $submenu_page_settings, array( $this::$tube_settings, 'ytevo_scripts') );
  
      
    // Submenu Page: API Settings
    $submenu_page_api_settings = add_submenu_page( 
      YT_EVOLUTION_SLUG, 
      'Your Youtube API Keys',
       'Youtube API Key',
        'manage_options', 
        YT_EVOLUTION_SLUG .'-api-keys', 
        array( $this::$tube_settings_api_keys, 'render_options_page_api_settings' )
    );     
    
    // Load the Styes / JS conditionally
    add_action( 'load-' . $submenu_page_api_settings, array( $this, 'load_tube_vc_admin_styles') );
    add_action( 'load-' . $submenu_page_api_settings, array( $this::$tube_settings, 'ytevo_scripts') );
    add_action( 'load-' . $submenu_page_api_settings, array( $this::$tube_settings, 'ytapi_scripts') );
      
      
    /*     
    // Submenu Page: Go Live      
    $submenu_page_live_stream = add_submenu_page(
      YT_EVOLUTION_SLUG, 
      'YT Plugin: Go Live',
      'Go Live',
      'manage_options', 
      YT_EVOLUTION_SLUG .'-live-stream', 
      array( $this::$tube_go_live, 'render_options_page_live_stream' )
    );     
    
    
    // Submenu Page: Add Via Imgur
    $submenu_page_add_via_imgur = add_submenu_page( 
      YT_EVOLUTION_SLUG, 
      'YT Plugin: Add Via Imgur',
      'Add Via Imgur',
      'manage_options', 
      YT_EVOLUTION_SLUG .'-add-via-imgur', 
      array( $this::$tube_add_via_imgur, 'render_options_page_add_via_imgur' )
    );    
    */
      
      
    // Submenu Page: Add Via Url
    $submenu_page_add_via_url = add_submenu_page(
      YT_EVOLUTION_SLUG, 
      'YT Evolution: Add Via Url',
      'Add Video Via Url',
      'publish_posts', 
      YT_EVOLUTION_SLUG .'-add-via-url', 
      array( $this::$tube_add_via_url, 'render_options_page_add_via_url' )
    );     
    // Using registered $page handle to hook script load
    add_action( 'load-' . $submenu_page_add_via_url, array( $this, 'load_tube_vc_admin_styles') );
    add_action( 'load-' . $submenu_page_add_via_url, array( $this, 'load_tube_vc_find_videos_scripts') );
    add_action( 'load-' . $submenu_page_add_via_url, array( $this::$tube_add_via_url, 'load_tube_vc_add_via_url_scripts') );

      
    // Submenu Page: Find Videos
    $submenu_page_find_videos = add_submenu_page( 
      //$option_page, 
      YT_EVOLUTION_SLUG, 
      'YT Evolution: Search for Videos',
       'Search for Videos',
      'publish_posts', 
      YT_EVOLUTION_SLUG .'-find-videos', 
      array( $this::$tube_find_videos, 'render_options_page_find_videos' )
    );     
    
    // Using registered $page handle to hook script load
    add_action( 'load-' . $submenu_page_find_videos, array( $this, 'load_tube_vc_admin_styles') );
    add_action( 'load-' . $submenu_page_find_videos, array( $this, 'load_tube_vc_find_videos_scripts') );
    
 
   
    // Submenu Page: Add Channels & Playlists
    $submenu_page_find_sources = add_submenu_page( 
      //$option_page, 
      YT_EVOLUTION_SLUG, // page is "hidden" from menus
      'YT Evolution: Add New Channels &amp; Playlists',
       'Add Channels &amp; Playlists',
      'manage_options', 
      YT_EVOLUTION_SLUG .'-find-sources', 
      array( $this::$tube_find_sources, 'render_options_page_find_sources' )
    );     
    
    // Using registered $page handle to hook script load
    add_action( 'load-' . $submenu_page_find_sources, array( $this, 'load_tube_vc_admin_styles') );
    add_action( 'load-' . $submenu_page_find_sources, array( $this::$tube_find_sources, 'load_tube_vc_find_sources_scripts') );
    add_action( 'load-' . $submenu_page_find_sources, array( $this::$tube_settings, 'ytevo_scripts') );
  }
  
  
  // Customize the Admin menu
  function customize_tube_vc_admin_menu(){    
      
      // customize the menu
      global $submenu;   
     
      if ( 
        isset( $submenu[YT_EVOLUTION_SLUG] )
         && ( current_user_can( 'manage_options' ) ||  current_user_can( 'publish_posts' ) )
       
       ):
        
        $curator_submenu = $submenu[YT_EVOLUTION_SLUG];
        
        // create a separator device
        $separator = array(
          '<span class="tube-vc-menu-separator"></span>',
          'publish_posts',
          '#',
          NULL        
        );
        
          
        // change the name of first menu item
        $curator_submenu[0][0] = __( 'Dashboard', 'yt-revolution' );
          
        if( current_user_can( 'manage_options' ) ):
        
          // Do something, for admins only
          
          $revised_menu[] = $curator_submenu[0]; // Dashboard
          
          $revised_menu[] = $separator;     
          
          $revised_menu[] = $curator_submenu[6]; // Your Channels
          
          $revised_menu[] = $curator_submenu[5]; // Add Channels
          
          $revised_menu[] = $separator;     
          
          $revised_menu[] = $curator_submenu[3]; // Add via URL
          
          $revised_menu[] = $curator_submenu[4]; // Search Videos
          
          $revised_menu[] = $curator_submenu[7]; // Skipped Videos
          
          $revised_menu[] = $separator;     
          
          $revised_menu[] = $curator_submenu[1];  // Settings
          
          $revised_menu[] = $curator_submenu[2];  // API Keys 
        
        elseif ( current_user_can( 'publish_posts' ) ):
          
          $revised_menu[] = $curator_submenu[0]; // Dashboard
          
          $revised_menu[] = $separator;     
          
          $revised_menu[] = $curator_submenu[3]; // Your Channels
          
          $revised_menu[] = $separator;     
          
          $revised_menu[] = $curator_submenu[1]; // Add via URL
          
          $revised_menu[] = $curator_submenu[2]; // Search Videos
          
          $revised_menu[] = $curator_submenu[4]; // Skipped Videos
          
        
        endif;
          
        $submenu[YT_EVOLUTION_SLUG] = $revised_menu;

      endif;
      
  }

  // Returns an array of all External Video IDs (to check dupes)
  function get_external_video_ids($site) { 
    
    $args = array( 
      'posts_per_page' => -1,
      'post_type' => get_post_types(),
      'meta_query' => array(
        array(
          'key'     => 'tube_video_id',
          'compare' => 'EXISTS',
        ),
        array(
          'key'     => 'tube_video_site',
          'compare' => '=',
          'value' => $site,
        ),
      ),
    );
    
    
    $posts_query = new WP_Query($args);
    
    $ids = array();
    
    foreach ($posts_query->posts as $post) {
      if($post->tube_video_id != ""){
          
        if ( $post->post_type == 'tube_skipped_video' ):
          $ids[$post->tube_video_id]['status'] = 'skipped';
        else:
          $ids[$post->tube_video_id]['status'] = $post->post_status;
        endif;
         
          $ids[$post->tube_video_id]['id'] = $post->ID;
          $ids[$post->tube_video_id]['permalink'] = get_permalink($post->ID);
          $ids[$post->tube_video_id]['edit'] = get_edit_post_link($post->ID);
        
        
      }      
    }
    
    return $ids;
    
  }
  
  
  //  Get the videos pending review (used in dashboard)
  function get_videos_pending_review( $args = NULL ) {
    
    $args = array( 
      'posts_per_page' => -1,
      'post_status' => 'pending',
      'post_type' => get_option( 'tube_vc_default_import_post_type' ),
      'meta_query' => array(
        array(
          'key'     => 'tube_video_id',
          'compare' => 'EXISTS',
        ),
      ),
    );
    
    
    $posts_query = new WP_Query($args);
    
    if ( ! $posts_query->have_posts() ):
      return NULL;      
    endif;
    
    return $posts_query;
    
    
  }
  
  
    
  // Get all the taxonomies for the tube video post type (via AJAX)
  function get_all_taxonomies_for_tube_video_post_type_via_ajax(  ) {
    
    $taxonomies = $this -> get_all_taxonomies_for_tube_video_post_type(  );    
  
    $taxonomies = json_encode($taxonomies);      


    echo filter_var($taxonomies);
    
    die();
    
  }
  
  // Get all the taxonomies for the tube video post type
  function get_all_taxonomies_for_tube_video_post_type( $type = NULL ) {
  
    // make sure there's a post type
    if ( ! $type && ! array_key_exists('post_type', $_POST) ):
      return;
    endif;
    
    // if no type via argument, use the post_type from $_POST
    if ( ! $type  ):
        $type = filter_var($_POST['post_type'], FILTER_SANITIZE_STRING);
    endif;
    
    // Get post type taxonomies.
    $taxonomies = get_object_taxonomies($type);
    
    // don't include the Nav Menu, Link Cat, or Post Format taxonomies
    $ignored = array( 'nav_menu', 'link_category', 'post_format' );
    
    if ( ! $taxonomies || ( count( $taxonomies ) == 0 ) ):
      return NULL;
    endif;
    
    foreach ( $taxonomies as $index => $taxonomy ):
      
      if ( in_array( $taxonomy, $ignored ) ):
        unset( $taxonomies[$index] );
      endif;
        
    endforeach;
    
    // allow taxonomies to be filtered
    $taxonomies = apply_filters( 'tube_vc_filter_importable_taxonomies', $taxonomies);
    
    if ( ! $taxonomies || ( count( $taxonomies ) == 0 ) ):
      return NULL;
    endif;
    
    // create data about the taxonomies
    
    foreach ( $taxonomies as $taxonomy ):
      
      $tax_obj = get_taxonomy( $taxonomy );
      
      $object = new stdClass();
      
      $object->taxonomy_slug = $tax_obj->name;
      
      $object->taxonomy_name = $tax_obj->labels->name;
            
      $taxonomies_data[] = $object;
      
    endforeach;      
    
    // return the taxonomies
    return $taxonomies_data;
    
  }
  
  
  // Get all the terms for the tube video post type (via AJAX)
  function get_all_terms_for_tube_video_post_type_via_ajax( ) { // Returns an array of categories for a post type
  
    $terms = $this -> get_all_terms_for_tube_video_post_type(  );
    
    $terms = json_encode($terms);      
    echo filter_var($terms);
    
    die();
    
  }
  
  
  // Get all the terms for the tube video post type
  function get_all_terms_for_tube_video_post_type( $type = NULL ) { // Returns an array of categories for a post type
      
    // make sure there's a post type
    if ( ! $type && ! array_key_exists('post_type', $_POST) ):
      return;
    endif;
    
    // if no type via argument, use the post_type from $_POST
    if ( ! $type  ):
        $type = filter_var($_POST['post_type'], FILTER_SANITIZE_STRING);
    endif;
    
    // Get all possible taxonomies
    $taxonomies = $this -> get_all_taxonomies_for_tube_video_post_type( $type );
            
    // make sure there are taxonomies, or do nothing
    if ( ! $taxonomies || count($taxonomies) == 0 ):
      
      return;
      
    endif;
    
    // placeholder for the results
    $results = array();
    
    // loop through the taxonomies
    foreach ( $taxonomies as $taxonomy ):
            
        $args = array(
            'orderby'           => 'name', 
            'order'             => 'ASC',
            'hide_empty'        => false, 
            'exclude'           => array(), 
            'exclude_tree'      => array(), 
            'include'           => array(),
            'number'            => '', 
            'fields'            => 'all', 
            'slug'              => '',
            'parent'            => '',
            'hierarchical'      => true, 
            'child_of'          => 0, 
            'get'               => '', 
            'name__like'        => '',
            'description__like' => '',
            'pad_counts'        => false, 
            'offset'            => '', 
            'search'            => '', 
            'cache_domain'      => 'core'
        );
        
        // Get the taxonomy terms
        $tax_terms = get_terms( $taxonomy->taxonomy_slug, $args );
        
        // loop through the terms
        foreach ($tax_terms as $tax_term):
          
          if($tax_term):
            
            // create data object about the term
            $object = new stdClass();
            
            $object->id = $tax_term->term_id;
            
            $object->name = $tax_term->name;
            
            $object->taxonomy_slug = $tax_term->taxonomy;
            
            $object->taxonomy_name = $taxonomy->taxonomy_name;

             // add teh term to the results
            $results[] = $object;
            
          endif;
         
        endforeach;

      
      
    endforeach;
    
    return $results;
    
  }
  
  
  
  // Create Tube Video post (via AJAX)
  function create_tube_video_post_via_ajax(){
    

    //remove any link and it's text
    $skip_link = get_option('tube_vc_default_restrict_link');
    if($skip_link){
      $_POST['content'] = preg_replace("@<(a)[^>]*?>.*?</\\1>@si", "", $_POST['content']);
    }

    $post_id = $this ->  create_tube_video_post( $_POST );

    // make sure we got a result
    if ( is_numeric($post_id) ):
      
      // add the post links to the result
      $results['view'] = get_permalink( $post_id );
      
      $results['edit'] = get_edit_post_link( $post_id );

      $results = json_encode($results);
      
      echo filter_var($results);
        
    else:
      
      echo 'error';
      
    endif;
       
    die();
    
  }
  
  function render_ez_content(){

    global $wpdb;
    $table_name_articles = $wpdb->prefix . 'ytevo_ez_articles';
    $article = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name_articles WHERE used=0 ;"));

    if(!$article){
      $ezp = get_option('ez_paging');
      $ezp = $ezp+1;

      $key = get_option('ytevo_ezinearticles_api_key');
      $cat = get_option('ytevo_default_ezinearticles_category');
      $subcat = get_option('ytevo_default_ezinearticles_subcategory');

      $apiEndpoint = 'http://api.ezinearticles.com/api.php?search=articles&category='.$cat.'&subcategory='.$subcat.'&page='.$ezp.'&response_format=json&key='.$key;
      $apiResult = wp_remote_get($apiEndpoint, array('timeout' =>30));

        if (is_wp_error($apiResult))
        {
            return false;
        }

        $jsonResult = json_decode($apiResult['body']);
        
        if (isset($jsonResult->error))
        {
          return false;
        }
        
        $articles = $jsonResult->articles;
        if(!empty($articles)){
            $table  = $wpdb->prefix . 'ytevo_ez_articles';
            $delete = $wpdb->query("TRUNCATE TABLE $table");
            $table_name = $wpdb->prefix . 'ytevo_ez_articles';
            foreach($articles as $article){
                $wpdb->insert( $table_name, 
		            array( 
              			'aid' => $article->article->id, 
                    'title' => $article->article->title, 
                    'url' => $article->article->url, 
                    'summery' => $article->article->summary, 
              		) 
              	);
            }
        }
      }
      
      $ezcontent = "     <!-- wp:paragraph -->
<p>{$article->summery}</p>
<!-- /wp:paragraph -->";
}
  
function generate_links_content(){
       $links = get_option( 'tube_vc_default_links' );
       $linksarray = json_decode($links);
       $content = '';
       if(isset($linksarray->dt) && !empty($linksarray->dt)){
           $linknum = count($linksarray->dt);
          $content = '<!-- addlinks --><ul>';
           for($i=0; $i< $linknum ; $i++){
               $content .= "<li><a href='{$linksarray->url[$i]}'>{$linksarray->dt[$i]}</a></li>";
           }
           $content .= '</ul><!-- addlinks -->';
       }
       
       return $content;
}
  
  // Create Tube Video post  
  function create_tube_video_post( $args ){ // Setting and calling wp_insert_post();
       // echo "Ron Preview <pre>";print_r($args);exit;
      $title = $args['title'];

      //$channel_id = $args['channel_id'];
      $skip_link = get_option('tube_vc_default_restrict_link');
      
      $content = nl2br($args['content']);
      
      if($skip_link){
          $content = preg_replace('/ <a.*a>/', '', $content);
      }
     // $skip_desc = get_option('tube_vc_default_restrict_desc');
     // if($skip_desc) $content = '';
      $id = $args['id'];
       
      $creator_name = ($args['creator_name'] === '') ? 'unknown user':$args['creator_name'];
      
      $image_url = $args['image_url'];
      
      $media_url = $args['media_url'];
      $ytpid = $args['ytpid'];
       
      $source_term_ids = $args['term_ids'];
          
      $tags = $args['tags'];
      // $transcript = $args['transcript'];
      
      $date = $args['date'];
      
      $source_site = $args['site'];
      
      $status = $args['status'];
      
      $author = ( ! array_key_exists('author', $args) ) ? get_option( 'tube_vc_default_import_author') : $args['author'];
      
      $author = apply_filters( 'tube_vc_filter_import_author', $author, $source_site, $creator_name );
      
      //$descrContent = $content;
      
      $excerpt = wp_trim_words( $content, 40 );

      $iframe = '<div><iframe width="100%" height="400" src="https://www.youtube.com/embed/'.$id.'" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>';

      //$iframe = '[iframe url="https://www.youtube.com/embed/'.$id.'"]';

      $content = $iframe.'<br>'.$content;

  
        // Integrate EZ
     
      $auto_imported = ( array_key_exists('auto_imported', $args) ) ? 1 : NULL;
      
      
      $date_gmt_formatted = date('Y-m-d H:i:s',strtotime($date));
  
      $replacers = array('/%title%/', '/%description%/', '/%embed%/', '/%media_url%/', '/%thumbnail%/', '/%query%/', '/%user%/', '/%date%/');
            
      $post_type = get_option( 'tube_vc_default_import_post_type');
      
      $post_type = apply_filters( 'tube_vc_filter_import_post_type', $post_type, $source_site, $creator_name );

      $default_import_term_name =  get_option( 'tube_vc_default_import_term') ;
      if(empty($default_import_term_name)){
        $default_import_term_name = 'category';
      }


      
      $unserize_arr = @unserialize($source_term_ids);
      $cats_ids = array();
      if ($unserize_arr !== false) {
          foreach ($unserize_arr as $key => $cat_id) {
            $cat = get_term_by('id', $cat_id, 'category');
            $cats_ids[] = $cat->slug;
          }

      } else {
          $source_term_ids_arr = explode(',', $source_term_ids);
          foreach ($source_term_ids_arr as $key => $cat_id) {
            $cat = get_term_by('id', $cat_id, 'category');
            $cats_ids[] = $cat->slug;
          }
      }

      

      // Creating new post
      $my_post = array(
        'post_title'    => $title,
        'post_content'  => $content,
        'post_excerpt'  => $excerpt,
        'post_date_gmt' => $date_gmt_formatted,
        'post_status'   => $status,
        'post_author'   => $author,
        'post_type'     => $post_type
       
      );

      $en_ez =  get_option('ytevo_ezinearticles_enable');
      $en_artf =  get_option('ytevo_articleforge_enable');


      if($en_ez){
        $my_post = apply_filters( 'tube_vc_filter_post_pre_insert', $my_post, $source_site, $creator_name, $ytpid, $id );

      }
      else if($en_artf){
        $my_post = apply_filters( 'artf_tube_vc_filter_post_pre_insert', $my_post, $source_site, $creator_name, $ytpid, $id );
        // print_r($my_post);
        // exit;
      }

      remove_filter('content_save_pre', 'wp_filter_post_kses');
      remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');

      $my_post['post_content'] = str_replace(array('{', '}'), '', $my_post['post_content']);
      $my_post['post_content'] = apply_filters('the_content', $my_post['post_content']);

      $post_ID = wp_insert_post($my_post);
      
      add_filter('content_save_pre', 'wp_filter_post_kses');
      add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
      
      // updating post meta
      // if a media is detected, add its source url
      

      // if(!empty($transcript)){
      //   add_post_meta($post_ID, 'video_transcript',$transcript );
      // }
      
      
      // terms came in as a string
      if ( $source_term_ids && ! is_array( $source_term_ids ) ):        
        
        // explode them to an array
        $default_import_term_ids = explode( ',', $source_term_ids );
          
      else:
        
        // get the default from plugin settings
        $default_import_term_ids = array( intval( get_option( 'tube_vc_default_import_term') ) );
       
      endif;
      
      $import_term_ids = apply_filters( 'tube_vc_filter_import_term', $default_import_term_ids, $source_site, $creator_name );

      //print_r($import_term_ids); exit;

      
      
      //if ( $import_term_ids && is_array($import_term_ids) ):
        
      //   $import_terms_sanitized = array();
          
      //   // sanitize all the the terms
      //   foreach ( $import_term_ids as $import_term_id ):
          
      //     $import_term = get_term( intval( trim( $import_term_id ) ) );
          
      //     if ( $import_term ):
            
      //       $import_terms_sanitized[$import_term->taxonomy][] = $import_term->term_id;    
          
      //     endif;
                  
      //   endforeach;        
        
      //endif;                



      // set the post terms
      if(!empty($cats_ids)){
        wp_set_object_terms( $post_ID, $cats_ids , $default_import_term_name, true );
      }

      if( $default_import_term_name == 'category' ){
        $default_category = get_option('default_category');
        wp_remove_object_terms($post_ID, 'uncategorized', 'category');
      }
        
        
      // deal with tag importing  
      if ( $tags ):
        
        $tag_import_taxonomy = get_option( 'tube_vc_default_tag_import_taxonomy');
        
        $tag_import_taxonomy = apply_filters( 'tube_vc_filter_tag_import_taxonomy', $tag_import_taxonomy, $source_site, $creator_name );
        
        if ( $tag_import_taxonomy ):
          
          // create an array from the tags,if it's not an array already
          $tags_array = is_array( $tags ) ? $tags : explode(',', $tags);
          
          $tags_sanitized = array();
          
          // sanitize all the the tags
          foreach ( $tags_array as $tag ):
            
            $tags_sanitized[] = esc_html( $tag );
            
          endforeach;
          
          wp_set_post_terms( $post_ID, $tags_sanitized, $tag_import_taxonomy ); 
        
        endif;
        
      endif;
        
      // add all the post meta  
      add_post_meta( $post_ID, 'tube_video_oembed_url', $media_url, true );
      
      add_post_meta( $post_ID, 'tube_video_date', $date, true );
      
      add_post_meta( $post_ID, 'tube_video_id', $id, true );
      
      add_post_meta( $post_ID, 'tube_video_site', $source_site, true );
      
      add_post_meta( $post_ID, 'tube_video_creator_name', $creator_name, true );
      
      if ($auto_imported):
        add_post_meta( $post_ID, 'tube_video_auto_imported', $auto_imported, true );
      endif;
      
      if( $image_url ):
        
        add_post_meta( $post_ID, 'tube_video_image_url', $image_url, true ) || update_post_meta( $post_ID, 'tube_video_image_url', $image_url ); 
        
        // Create and upload thumbnail 
        $upload_dir = wp_upload_dir();
        
        // try to get image via wp_remote_get
        $image_data_get = wp_remote_get($image_url);
        
        // see if there's image data
        if ( $image_data_get && ! is_wp_error( $image_data_get ) ):
          
          // get the body of the response
          $image_data = $image_data_get['body'];          
          
          // generate a title
          $title_sanitized = substr(sanitize_title(stripslashes($title)), -70);
          
          // generate a filename
          $filename = $title_sanitized . '-' . $id.basename($image_url);
          
          // replace stuff we don't need
          $filename = str_replace( array('%','maxresdefault'),'', $filename);
     
          // generate the file path
          if(wp_mkdir_p($upload_dir['path'])):
              $file = $upload_dir['path'] . '/' . $filename;
          else:
              $file = $upload_dir['basedir'] . '/' . $filename;
          endif;
          
          // upload the file contents
          $put_bytes = file_put_contents($file, $image_data);
      
          if ( false !== $put_bytes ):
            
            $wp_filetype = wp_check_filetype($filename, null );
  
            $caption = '<a href="'.$media_url.'" target="_blank">' . _x('Source', 'Text of source link in image caption', 'yt-revolution') . '</a>';
            
            $attachment = array(
                'post_mime_type' => $wp_filetype['type'],
                'post_title' => esc_html($title),
                'post_excerpt' => wp_kses_post($caption),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            
            $attach_id = wp_insert_attachment( $attachment, $file, $post_ID );
            
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
            
            wp_update_attachment_metadata( $attach_id, $attach_data );
        
            update_post_meta($attach_id, '_wp_attachment_image_alt', esc_html($title));
            
            set_post_thumbnail( $post_ID, $attach_id );
            
          endif;

        endif;
        
      endif;
      
      return $post_ID;
      
  }
  
  
  
  // Create the skipped video post (via AJAX)
  function create_skipped_tube_video_post_via_ajax(){ // Reject post : insert
      
      $title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
      
      $content = filter_var($_POST['content'], FILTER_SANITIZE_STRING);
      
      $creator_name = filter_var($_POST['creator_name'], FILTER_SANITIZE_STRING);
      
      $id = filter_var($_POST['id'], FILTER_VALIDATE_INT);
      
      $date = filter_var($_POST['date'], FILTER_SANITIZE_STRING);
      
      $date_gmt_formatted = date('Y-m-d H:i:s',strtotime($date));
      
      $media_url = filter_var($_POST['media_url'], FILTER_SANITIZE_STRING);
      
      $source_site = filter_var($_POST['site'], FILTER_SANITIZE_STRING);
      
      $author = ( ! array_key_exists('author', $_POST) ) ? get_option( 'tube_vc_default_import_author') : filter_var($_POST['author'], FILTER_SANITIZE_STRING);;
  
  
      $skipped_video_post_args = array(
        'post_name'     => $source_site . '-' . $id,
        'post_title'    => $title . ' [' . $creator_name . ' | ' . $source_site . ']',
        'post_content'  => $content,
        'post_date_gmt' => $date_gmt_formatted,
        'post_status'   => 'publish',
        //'tags_input'    => $author,
        'post_author'   => $author,
        'post_type'     => 'tube_skipped_video'
      );
  
      $skipped_video_post = wp_insert_post($skipped_video_post_args);
      
      add_post_meta( $skipped_video_post, 'tube_video_oembed_url', $media_url, true );
      add_post_meta( $skipped_video_post, 'tube_video_date', $date, true );
      add_post_meta( $skipped_video_post, 'tube_video_id', $id, true );
      add_post_meta( $skipped_video_post, 'tube_video_site', $source_site, true );
      add_post_meta( $skipped_video_post, 'tube_video_creator_name', $creator_name, true );
      //add_post_meta( $skipped_video_post, 'tube_video_channel_id', $channel_id, true );
      

      if ( is_numeric($skipped_video_post) ):
        
        $results['id'] =  $skipped_video_post ;
        
        $results['view'] = get_permalink( $skipped_video_post );
        
        $results['edit'] = get_edit_post_link( $skipped_video_post );

        $results = json_encode($results);
        
        echo esc_html($results);
          
      else:
        
        echo 'error';
        
      endif;
         
      die();
  }
  
  
  
  // Restore the skipped video post (via AJAX)
  function restore_skipped_tube_video_post_via_ajax(){ // Reject post : insert
      
      $post_id = filter_var($_POST['postid'], FILTER_VALIDATE_INT);
  
      $restore_post = wp_delete_post($post_id);
      
      if ( $restore_post ):
        echo 'success';          
      else:
        echo 'error';
      endif;
         
      die();
  }


  // Load the text domain
  function load_tube_vc_textdomain() {
      
    load_plugin_textdomain('yt-revolution', false, basename( dirname( __FILE__ ) ) . '/i18n' );
    
  }
  
  
  
  // Main Tube Admin CSS
  function load_tube_vc_admin_styles(){
      add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_tube_vc_admin_styles') );
  }
      
  function enqueue_tube_vc_admin_styles() {
      wp_register_style(
        YT_EVOLUTION_SLUG, 
        plugins_url('/css/style.css', __FILE__), 
        array(), 
        '1.0.0'
     );
      wp_register_style(
        'chosen', 
        plugins_url('/js/chosen/chosen.min.css', __FILE__), 
        array(), 
        '1.0.0'
     );
      
       wp_register_style(
        YTEVO_CRUATOR_SLUG, 
        plugins_url('/css/style2.css', __FILE__), 
        array(), 
        '1.0.0'
     );
     wp_enqueue_style(YTEVO_CRUATOR_SLUG);
       wp_enqueue_style('chosen');
       wp_enqueue_script( 'chosen', plugins_url('/js/chosen/chosen.jquery.min.js', __FILE__), array ( 'jquery' ), 1.1, true);
     wp_enqueue_style(YT_EVOLUTION_SLUG);
     
  }
  
  
  
  function load_tube_vc_find_videos_scripts(){
    
      add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_find_videos_scripts') );      
      
  }
  
      
  function enqueue_find_videos_scripts() {
    
      wp_register_script(
        'tube_vc_find_videos', 
        plugins_url('/js/tube-vc-find-videos.js', __FILE__), 
        array(), 
        '1.0.1',
        //rand(50000, 150000), 
        false
      );
      wp_enqueue_script('tube_vc_find_videos');
      
      add_action( 'admin_print_scripts', array(  $this, 'print_find_videos_scripts'), 99 );
      
  }

      
  function print_find_videos_scripts() {    
    $arr_term = get_option( 'tube_vc_default_import_term');
    ?>
    <script type="text/javascript">
      var pluginDirectory = '<?php echo plugins_url('', __FILE__); ?>';
      Yt_EVL_Ajax_Videos.query = '<?php echo get_option( 'tube_vc_find_videos_query', '' ); ?>';
      Yt_EVL_Ajax_Videos.queryType = '<?php echo get_option( 'tube_vc_find_videos_query_type', '' ); ?>';
      Yt_EVL_Ajax_Videos.number = '<?php echo get_option( 'tube_vc_find_videos_max_results', '' ); ?>';


      var arr_term =  '<?php echo json_encode($arr_term); ?>';
      var json_p = jQuery.parseJSON(arr_term);
      
      Yt_EVL_Ajax_Videos.currentCat = arr_term;

      Yt_EVL_Ajax_Videos.currentTagImportTax = '<?php echo get_option( 'tube_vc_default_tag_import_taxonomy', '' ); ?>';
      Yt_EVL_Ajax_Videos.trans.approve = '<?php _e('Approve', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.deny = '<?php _e('Deny', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.needUpdate = '<?php _e('You have changed the query parameters, remember to click on Save Changes to update the results.', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.apiWrong = '<?php _e('There is an error with your API key, it has been rejected by Google API Service.', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.loading = '<?php _e('Loading...', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.mediaFailed = '<?php _e('Video import failed. Please try again.', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.mediaAdded = '<?php _e('Video added successfully.', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.mediaRejected = '<?php _e('Video skipped successfully.', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.restoreFailed = '<?php _e('Video restore failed. Please try again.', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.mediaRestored = '<?php _e('Video restored successfully.', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.videoPublishedFlag = '<?php _e('PUBLISHED', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.videoDraftFlag = '<?php _e('DRAFT', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.videoPendingFlag = '<?php _e('PENDING', 'yt-revolution'); ?>';
      Yt_EVL_Ajax_Videos.trans.videoSkippedFlag = '<?php _e('SKIPPED', 'yt-revolution'); ?>';
     </script>
    
    <?php
  }



  

  
  /* -----------------------------------------------------------------------------
   *  Render the admin page
    ---------------------------------------------------------------------------- */
  
  function render_options_page_find_videos() {
      
      require_once(dirname(__FILE__) . '/TOSS-page-find-videos.php');
       
  }
  
  
  
  
  
  // add action links to the Plugins List page
  function add_tube_vc_action_links ( $links ) {
      
     $mylinks = array(
       '<a href="' . YT_EVOLUTION_DASH_URL . '">Dashboard</a>',
       '<a href="' . YT_EVOLUTION_SETTINGS_URL . '">Settings</a>'
       );
       
      return array_merge( $links, $mylinks );
      
  }


  /* -----------------------------------------------------------------------------
   *  Get settings values for sites & autosycn
    ---------------------------------------------------------------------------- */
  
  
  function get_external_site_options( $active_only = false ){
        
     $sites = array();
     
     $sites['youtube'] = 'YouTube';
     
  //     $sites['vimeo'] = 'Vimeo';
  //     
  //     $sites['twitch'] = 'Twitch';
     
     if ( ! $active_only ):
       
        return $sites;
       
     endif;
     
      
    // get the search site value
    $search_site = get_option( 'tube_vc_find_videos_site' );
  
     
     $youtube_api_key = Yt_Evolution::$tube_youtube_videos->get_youtube_api_key();  
     
     $vimeo_api_key = Yt_Evolution::$tube_vimeo_videos->get_vimeo_api_key();  
     
     $twitch_api_key = Yt_Evolution::$tube_twitch_videos->get_twitch_api_key(); 
      
     if ( ! $youtube_api_key ):
       
       // Remove YouTube from search options
       unset( $sites['youtube'] );
       
       // make sure it's not using YouTube as search site
       if ( $search_site == 'youtube' ):
       
          delete_option( 'tube_vc_find_videos_site' );
        
       endif;       
     
        // check if there's a Vimeo key and update the site option if so
       if ( $vimeo_api_key ):
         
         update_option( 'tube_vc_find_videos_site', 'vimeo' );
         
       endif;
     
     endif;
     
     
     if ( ! $vimeo_api_key):       
       
       // Remove Vimeo from search options
       unset( $sites['vimeo'] );
       
       // make sure it's not using Vimeo as search site     
       if ( $search_site == 'vimeo' ):
         
        delete_option( 'tube_vc_find_videos_site' );
         
       endif;
     
        // check if there's a YouTube key and update the site option if so   
       if ( $youtube_api_key ):
         
         update_option( 'tube_vc_find_videos_site', 'youtube' );
         
       endif;
       
     endif;
     
     /*
     
     if ( ! $twitch_api_key):       
       
       // Remove Twitch from search options
       unset( $sites['twitch'] );
       
       // make sure it's not using Twitch as search site     
       if ( $search_site == 'twitch' ):
         
        delete_option( 'tube_vc_find_videos_site' );
         
       endif;
     
        // check if there's a YouTube key and update the site option if so   
       if ( $youtube_api_key ):
         
         update_option( 'tube_vc_find_videos_site', 'youtube' );
         
       endif;
       
     endif;
     */
     
     return $sites;
   
  }
  
  function get_external_site_label( $key ){
        
     $options = $this->get_external_site_options();
     
     if ( ! array_key_exists($key, $options) ):
       return NULL;
     endif;
     
     return $options[$key];
     
  }
  
  
  function get_feed_type_options($require_api = false){
        
     $options = array();
     
     $youtube_api_key = Yt_Evolution::$tube_youtube_videos->get_youtube_api_key();
      
     if ($youtube_api_key || ! $require_api):
       
       $options['youtube-channel'] = 'YouTube Channel';
       
       $options['youtube-playlist'] = 'YouTube Playlist';
       
     endif;
     
     $vimeo_api_key = Yt_Evolution::$tube_vimeo_videos->get_vimeo_api_key();
     
     if ($vimeo_api_key || ! $require_api):
       
       $options['vimeo-username'] = 'Vimeo User';
       
       $options['vimeo-channel'] = 'Vimeo Channel';
       
     endif;
     
     
     $twitch_api_key = Yt_Evolution::$tube_twitch_videos->get_twitch_api_key();
     
     if ($twitch_api_key || ! $require_api):
       
       $options['twitch-channel'] = 'Twitch Channel';
       
     endif;
     
     
     return $options;
     
  }
  
  
  
  function get_feed_type_label( $feed_type ){
        
     $options = $this->get_feed_type_options();
     
     if ( ! array_key_exists( $feed_type, $options) ):
       return NULL;
     endif;
     
     return $options[$feed_type];
     
  }
  
  
  function get_auto_import_status( $video_data ){        
    
    $auto_import_status = get_option( 'tube_vc_default_auto_import_status');
    
    $auto_import_status = apply_filters( 'tube_vc_filter_auto_import_status', $auto_import_status, $video_data );
     
    return $auto_import_status;
     
  }
    
  
  public static function has_api_keys( $required = NULL ){        
    
    
    $api_keys['youtube'] = Yt_Evolution::$tube_youtube_videos->get_youtube_api_key();
    
    $api_keys['vimeo'] = Yt_Evolution::$tube_vimeo_videos->get_vimeo_api_key();
    
    $api_keys['twitch'] = Yt_Evolution::$tube_twitch_videos->get_twitch_api_key();
    
    if ( $required ):
      
      foreach ( $required as $is_required ):
        
        // only need one match to be true
        if ( $api_keys[$is_required] ) return true;
        
      endforeach;
    
    else:      
      
      foreach ( $api_keys as $key => $key_value ):
        
        // only need one match to be true
        if ( $key_value ) return true;
        
      endforeach;
          
    endif;
    
    return false;
     
  }
      
  
  public static function no_api_keys_message(  ){        
    
    $message = sprintf(
      __('Sorry, you must have a <a href="%1$s">YouTube</a>, <a href="%2$s">Vimeo</a>, or <a href="%3$s">Twitch</a> API key to use this feature.', 'yt-revolution'), 
      YT_EVOLUTION_API_KEYS_URL, 
      YT_EVOLUTION_VIMEO_API_KEYS_URL, 
      YT_EVOLUTION_TWITCH_API_KEYS_URL
    );
    
    echo wp_kses_post( '<p>' . $message . '</p>' );
    
    return;
     
  }
  
  public static function no_youtube_or_vimeo_api_keys_message(  ){        
    
    $message = sprintf(
    __('Sorry, you must have a <a href="%1$s">YouTube</a> or <a href="%2$s">Vimeo</a> API key to use this feature.', 'yt-revolution'), 
      YT_EVOLUTION_API_KEYS_URL, 
      YT_EVOLUTION_VIMEO_API_KEYS_URL
    );
    
    echo wp_kses_post( '<p>' . $message . '</p>' );
    
    return;
     
  }
    
  public static function swapapi(){
    global $wpdb;
    $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}yt_apikeys WHERE DATE(today) = DATE(NOW())" );
    $today = current_time('Y-m-d');
      
    if(!$count){
        $wpdb->update( 
      	"{$wpdb->prefix}yt_apikeys", 
      	array(
      		'used' => 0,	// string
      		'today' => $today	// integer (number) 
      	),
                      array('status'=>1),
                      array(
                          '%d',	// value1
      		'%s'	// value2
                      ),
                      array(
                        '%d'  
                      )
      	
        );
    }
       
       
    $currentapikey = get_option( 'tube_vc_youtube_api_key' );
   // SET current as used 
  
    $wpdb->update( 
            "{$wpdb->prefix}yt_apikeys", 
            array( 
                    'used' => 1,	// string

            ),
            array('apikey'=>$currentapikey),
            array(
                '%d',	// value1

            ),
            array(
              '%s'  
            )
    );
                            
    // Get next apikey                        
    $newapikey = $wpdb->get_var( "SELECT apikey FROM {$wpdb->prefix}yt_apikeys WHERE DATE(today) = '$today' AND used=0 " );                
    if($newapikey){
           
      update_option('tube_vc_youtube_api_key', $newapikey);
      $wpdb->update( 
            "{$wpdb->prefix}yt_apikeys", 
            array( 
                    'used' => 1,	// string

            ),
            array('apikey'=>$newapikey),
            array(
                '%d',	// value1

            ),
            array(
              '%s'  
            )
      );
      
      
      $to = get_option('admin_email');

      $subject = 'Your youtube API key swaped';
      $body = " The API key just swaped. The last used API was $currentapikey and current API key is $newapikey";
      $headers = array('Content-Type: text/html; charset=UTF-8');
       
      wp_mail( $to, $subject, $body, $headers );
    }
     // $all[self::$opt_apikey] = '';
     // update_option(self::$opt_alloptions, $all);
  }  


}