<?php
/**
 * Yt_EVL_Api_Key_Nag
 * 
 * Show an admin notice if the user hasn't set up any ALPI keys
 * 
 * @package Yt_Evolution
 * @subpackage Classes
 * @author  Chris Derenberger <https://www.ytevolution.com>
 * @copyright Copyright 2020, YTEVOLUTION®
 * @link https://www.ytevolution.com
 * @since 1.0.0
 */
 
class Yt_EVL_Api_Key_Nag {
  
  public static $instance;
  
  public static function init() {
      if ( is_null( self::$instance ) )
          self::$instance = new Yt_EVL_Api_Key_Nag();
      return self::$instance;
  }
    
  
  // Constructor    
  function __construct() {    
    
    // check if request to ignore the nag  
    add_action('admin_init', array( $this, 'ignore_api_key_nag' ) );
    
    // try to display the nag
    add_action('admin_notices', array( $this, 'display_api_key_nag' ) );

  }
  
 
  // Display a notice that can be dismissed
  function display_api_key_nag() {
    
    // if any key is set, do nothing
    if ( Yt_Evolution::has_api_keys() ):
      return;     
    endif;
     
    // Check that the user hasn't already clicked to ignore the message
    
    // get current User ID
    $user_id = get_current_user_id();
    
    // check if we should ignore the API Key nag
    if ( get_user_meta($user_id, 'ignore_api_key_nag') ) :
      return;
    endif;
    
    
    // make sure we're not on a page to input the settings
    global $pagenow;
    
    if ( $pagenow == 'admin.php' ):

      $page = filter_var($_GET['page'], FILTER_SANITIZE_STRING);

      if ( ( array_key_exists('page', $_GET) ) && ($page == 'yt-revolution-api-keys' ||  $page == YT_EVOLUTION_SLUG ) ):
        return;
      endif;
        
    endif;
    
    // Output the nag  
    ?>
    <div class="update-nag api-key-nag clearfix" style="display:block; margin-top:35px; position:relative;">
        <a href="<?php echo esc_url( add_query_arg('ignore_api_key_nag', '1') ); ?>" class="notice-dismiss"><?php _e('Dismiss', YT_EVOLUTION_SLUG); ?></a>
      <p>
        <strong><?php echo __('You&rsquo;re almost ready to create videos&hellip;', 'yt-revolution'); ?></strong>
      </p>
        <a href="<?php echo esc_url(YT_EVOLUTION_DASH_URL); ?>" class="button button-primary button-tube"><?php _e('Set up the YT Evolution', 'yt-revolution'); ?></a>
    </div>
    <?php

  }
  
  
 
  // Check if there's a request to ignore the API Key nag  
  function ignore_api_key_nag() {
    
    // check for the ignore querystring
    if(isset($_GET['ignore_api_key_nag'])){
      $ignore_api_key_nag = filter_var($_GET['ignore_api_key_nag'], FILTER_SANITIZE_STRING);
      if ( isset($ignore_api_key_nag) && '1' == $ignore_api_key_nag ):
        
        // get current User ID
        $user_id = get_current_user_id();
        
        // add some user meta to ignore the API GET nag
        add_user_meta($user_id, 'ignore_api_key_nag', 'true', true);
        
      endif;
    }
  }
}