<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       tarokkina_free/fer
 * @since      1.0.0
 *
 * @package    Tarokkina_free
 * @subpackage Tarokkina_free/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 *
 * @package    Tarokkina_free
 * @subpackage Tarokkina_free/admin
 * @author     tarokkina_free <tarokkina_free@gmailcom>
 */
class Tarokkina_free_Admin {


	private $plugin_name;
	private $version;

	/**
	 * Initialize the class and set its freeperties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function TkNa_enqueue_styles_free() {

		if (get_current_screen()->id == 'tarokkina_free_page_crb_carbon_fields_container_tarot_free') {
			$css_back = 'back_tarots';
			wp_enqueue_style( 'tarokkina_free_'.$css_back, plugin_dir_url( __DIR__ ) . 'css/'.$css_back.'.css', array(), $this->version, 'all');
		}elseif (get_current_screen()->id == 'edit-tarokkina_free-cat' || get_current_screen()->id == 'tarokkina_free_page_tarokina_free_license') {
			$css_back = 'back_barajas';
			wp_enqueue_style( 'tarokkina_free_'.$css_back, plugin_dir_url( __DIR__ ) . 'css/'.$css_back.'.css', array(), $this->version, 'all');
		}elseif (get_current_screen()->id == 'edit-tarokkina_free') {
			$css_back = 'back_cartas';
			wp_enqueue_style( 'tarokkina_free_'.$css_back, plugin_dir_url( __DIR__ ) . 'css/'.$css_back.'.css', array(), $this->version, 'all');
		}elseif (get_current_screen()->id == 'tarokkina_free') {
			$css_back = 'back_carta';
			wp_enqueue_style( 'tarokkina_free_'.$css_back, plugin_dir_url( __DIR__ ) . 'css/'.$css_back.'.css', array(), $this->version, 'all');
		}else{

		}

	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function TkNa_enqueue_scripts_free() {
		if (get_current_screen()->id == 'tarokkina_free_page_crb_carbon_fields_container_tarot_free') {
			$js_back = 'js_back_tarots';
			wp_enqueue_script( 'tarokkina_free_'.$js_back, plugin_dir_url( __DIR__ ) . 'admin/js/'.$js_back.'.js', array( 'jquery' ), $this->version, false);
		}elseif (get_current_screen()->id == 'edit-tarokkina_free-cat') {
			$js_back = 'js_back_barajas';
			wp_enqueue_script( 'tarokkina_free_'.$js_back, plugin_dir_url( __DIR__ ) . 'admin/js/'.$js_back.'.js', array( 'jquery' ), $this->version, false);
		}elseif (get_current_screen()->id == 'edit-tarokkina_free') {
			$js_back = 'js_back_cartas';
			wp_enqueue_script( 'tarokkina_free_'.$js_back, plugin_dir_url( __DIR__ ) . 'admin/js/'.$js_back.'.js', array( 'jquery' ), $this->version, false);
		}elseif (get_current_screen()->id == 'tarokkina_free') {
			$js_back = 'js_back_carta';
			wp_enqueue_script( 'tarokkina_free_'.$js_back, plugin_dir_url( __DIR__ ) . 'admin/js/'.$js_back.'.js', array( 'jquery' ), $this->version, false);
		}else{
			wp_enqueue_script( 'tarokkina_free_js_admin', plugin_dir_url( __DIR__ ) . 'admin/js/js_admin.js', array( 'jquery' ), $this->version, false);
		}

	}



	// =============================================================
	//       CARBON FIEDS
	// =============================================================
			public function TkNaP_free_carbon_fields(){

				if ( file_exists( plugin_dir_path( dirname( __FILE__ ) ) . 'lib/carbon-fields/vendor/autoload.php' ) ) {
					require(  plugin_dir_path( dirname( __FILE__ ) ) . 'lib/carbon-fields/vendor/autoload.php' );
					\Carbon_Fields\Carbon_Fields::boot();
				}
			}




	// =============================================================
	//       LOGO
	// =============================================================
			public function TkNaP_free_cabecera(){
				$cabecera = '
				<div class="cabecera_head">
					<div class="cabecera_logo">
						<img src="'.TAROKINA_URL_FREE.'img/logo.svg">
						<div class="name">arnelio</div>
						<div class="bloq_title">
						<span class="logoTitle">TAROKINA <span class="textFree">FREE</span></span>
						<span class="plugV">'.TAROKKINA_FREE_VERSION.'</span>
						</div>
					</div>
					<div class="cabecera_info">

						<span>
						<a data-text="'.
						esc_html__('Regenerate images and clear the tarot cache.','tarokina-free').'" class="btnCardIds clear_tooltip" href="'.admin_url( 'edit.php?post_type=tarokkina_free&page=crb_carbon_fields_container_tarot_free.php&cids=2' ).'"><svg class="svg-icon" style="width:25px; height:25px; vertical-align:bottom;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M837.818182 915.549091h-131.956364c-12.8 0-23.272727-10.472727-23.272727-23.272727v-58.181819c0-4.189091-8.843636-11.636364-23.272727-11.636363s-23.272727 7.447273-23.272728 11.636363v58.181819c0 12.8-10.472727 23.272727-23.272727 23.272727H465.454545c-12.8 0-23.272727-10.472727-23.272727-23.272727s10.472727-23.272727 23.272727-23.272728h124.043637v-34.909091c0-32.581818 30.72-58.181818 69.818182-58.181818s69.818182 25.6 69.818181 58.181818v34.909091H814.545455v-232.727272H209.454545v232.727272h93.789091c12.8 0 23.272727 10.472727 23.272728 23.272728s-10.472727 23.272727-23.272728 23.272727H186.181818c-12.8 0-23.272727-10.472727-23.272727-23.272727v-279.272728c0-12.8 10.472727-23.272727 23.272727-23.272727h651.636364c12.8 0 23.272727 10.472727 23.272727 23.272727v279.272728c0 12.8-10.472727 23.272727-23.272727 23.272727z" /><path d="M837.818182 636.276364H186.181818c-12.8 0-23.272727-10.472727-23.272727-23.272728v-69.818181c0-11.403636 8.145455-20.945455 19.316364-22.807273l244.596363-43.054546V124.276364c0-26.065455 23.970909-46.545455 54.225455-46.545455h62.138182c30.487273 0 54.225455 20.48 54.225454 46.545455V477.090909l244.596364 43.054546c11.170909 1.861818 19.316364 11.636364 19.316363 22.807272v69.818182A23.738182 23.738182 0 0 1 837.818182 636.276364z m-628.363637-46.545455h605.09091v-26.996364l-244.596364-43.054545a23.202909 23.202909 0 0 1-19.316364-22.807273V126.138182a13.172364 13.172364 0 0 0-7.68-2.094546h-62.138182c-3.723636 0-6.516364 1.163636-7.68 2.094546v370.269091c0 11.403636-8.145455 20.945455-19.316363 22.807272L209.454545 562.734545v26.996364z" /></svg>&nbsp;'.
						esc_html__('Clear cache','tarokina-free').'</a>
					</span>


						<span><a href="https://arnelio.com/downloads/tarokina-pro/" target="_blank">Tarokina Pro</a></span>
						<span><a href="https://arnelio.com/contact/" target="_blank">Contact</a></span>
					</div>
				</div>
				';
				echo $cabecera;
			}



	// =============================================================
	//       Links en plugins.php
	// =============================================================
	public function TkNa_free_links_plugin( array $actions ) {
		if (is_admin()) {
			return array_merge( array(
				'pro' => '<img style="width:57px;height:27px" src="'.esc_url(TAROKINA_URL_FREE).'img/icon_double.svg"><a href="https://arnelio.com/downloads/tarokina-pro/">' . esc_html__( 'Pro version', 'tarokina-free' ) . '</a>',
			), $actions );
		}
	}


	// Per page tarokina Free
	public function TkNa_free_per_page ( $per_page ) {
		if ($per_page < 50) {
			$num = 50;
		}else{
           $num = $per_page;
		}
		return $num;
	}


			// =============================================================
			//       Creando el option del tarot free
			// =============================================================
			public function TkNa_free_option(){
				require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/option_tarot.php';
			}



		// Borrando el check de la tabla de las cartas en bulk actions 
		public function TkNaP_array_cartas_free(){
			global $current_screen;
			
			if ( $current_screen->id == 'edit-tarokkina_free' && isset($_GET['trashed']) || $current_screen->id == 'edit-tarokkina_free' && isset($_GET['updated'])) {

				require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/cards_ids.php';
				delete_option('_tarokki_checkCartas_free' );
				
			}
		}


	// =============================================================
	//       Instalador de tarokina free
	// =============================================================
	public function TkNa_free_install(){
		if ( isset($_POST['freeinstall']) && $_POST['freeinstall']=='yes') {
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/install.php';
				update_option( 'tarokki_install_free', 'yes' );
			}
		} 



    // =============================================================
	//       admin/tarots/escritor.php
	// =============================================================
	public function TkNa_free_Escritor(){
		require_once TAROKINA_ADMIN_PATH_FREE . 'tarots/escritor.php';
	}


	// =============================================================
	//       Link to tarokina pro in cards
	// =============================================================
	public function TkNa_free_link_card(){
		echo '<div id="proBtn"><a class="button-secondary" style="color: #fff;background: #742c7a;border-color: #742c7a;margin: 5px 0;padding: 3px 6px" target="_blank" href="https://arnelio.com/downloads/tarokina-pro/">Tarokina pro&nbsp;&nbsp;<svg style="width: 22px;vertical-align:bottom" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><path d="M3 4a1 1 0 011-1h12a1 1 0 011 1v2a1 1 0 01-1 1H4a1 1 0 01-1-1V4zM3 10a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H4a1 1 0 01-1-1v-6zM14 9a1 1 0 00-1 1v6a1 1 0 001 1h2a1 1 0 001-1v-6a1 1 0 00-1-1h-2z"></path> </svg></a></div>';
	}


	// =============================================================
	//       Links top in cards
	// =============================================================
	public function TkNa_GridAdonsCards(){
		?>
		<div class="textEditCard"><span class="dashicons dashicons-edit"></span><?php echo esc_html__('Edit title', 'tarokina-free') ?></div>
		<div class="grid_bloq">
		<div class="addons_bloq">
			<div><a href="https://arnelio.com/downloads/tarokina-pro/" target="_blank"><img class="som" style="width:100%;height:100%" src="<?php echo esc_url(plugin_dir_url(dirname( __FILE__ )))?>img/tarokina-pro_plugin.svg" alt="tarokina pro"><div style="text-align:center">Pro</div></a></div>
		</div></div>
		<?php
	}

	// =============================================================
	//       Grid con los addons
	// =============================================================
	public function TkNa_Grid_addons(){
		?>
	<div id="grid_bloq" class="grid_bloq">
		<div class="addons_bloq">

		<a href="https://wordpress.org/support/plugin/tarokina-free/reviews/?filter=5" target="_blank" rel="noopener noreferrer">
				<div class="item som itRev">
					<h3><?php esc_html_e('Review','tarokina-free')?></h3>
					<div class="stars">&#9733;&#9733;&#9733;&#9733;&#9733;</div>
					<p><?php esc_html_e('Please rate Tarokina Free on WordPress.org to help us spread the word. Thank you from the Arnelio team!','tarokina-free')?></p>
				</div>
			</a>

			<a href="https://arnelio.us1.list-manage.com/subscribe?u=a15a475afbc487f524b220d70&id=7aacc08df4" target="_blank" rel="noopener noreferrer">
				<div class="item som itEma">
					<h3><?php esc_html_e('Arnelio.com','tarokina-free')?></h3>
					<div class="email"><img src="<?php echo esc_url(plugin_dir_url(dirname( __FILE__ )))?>img/email.svg" alt="email" /></div>
					<p><?php esc_html_e('We notify you about discounts and updates.','tarokina-free')?></p>
				</div>
			</a>

			<a href="https://arnelio.com/downloads/tarokina-pro/" target="_blank"><div class="item som"><img style="width:100%;height:100%" src="<?php echo esc_attr(plugin_dir_url(dirname( __FILE__ )))?>img/tarokina-pro_plugin.svg" alt="tarokina pro"><div class="name">Tarokina Pro</div></div></a>

			<a href="https://arnelio.com/downloads/classic-tarot-spreads-for-tarokina/" target="_blank"><div class="item som"><img style="width:100%;height:100%" src="<?php echo esc_url(plugin_dir_url(dirname( __FILE__ )))?>img/classic_spreads.svg" alt="Classic spreads"><div class="name">Classic Spreads</div></div></a>

			<a href="https://arnelio.com/downloads/custom-tarot-spreads-for-tarokina/" target="_blank"><div class="item som"><img style="width:100%;height:100%" src="<?php echo esc_url(plugin_dir_url(dirname( __FILE__ )))?>img/custom_spreads.svg" alt="Custom spreads"><div class="name">Custom Spreads</div></div></a>

		</div>
	</div>
		<?php
		}
		
    // =============================================================
	//       LINK Review in web for Tarokina Free
	// =============================================================
	public function TkNa_free_admin_footer( $text ) {

		global $current_screen;

		if ($current_screen->id !== 'tarokkina_free_page_crb_carbon_fields_container_tarot_free') {
			$url  = 'https://wordpress.org/support/plugin/tarokina-free/reviews/?filter=5';
			$text = sprintf(
				wp_kses(
					__( 'Please rate %1$s <a href="%2$s" target="_blank" rel="noopener noreferrer">&#9733;&#9733;&#9733;&#9733;&#9733;</a> on <a href="%3$s" target="_blank" rel="noopener">WordPress.org</a> to help us spread the word. Thank you from the Arnelio team!', 'tarokina-free' ),
					[
						'a' => [
							'href'   => [],
							'target' => [],
							'rel'    => [],
						],
					]
				),
				'<strong>Tarokina Free</strong>',
				$url,
				$url
			);

			return '<div class="footerReview">'.$text.'</div>';
		
		}
	}


	// =============================================================
	//       Add admin notice to rate plugin
	// =============================================================
	public function TkNa_rate_notice(){
		$settings = get_option('TkNa_settings');

		if(!empty($settings['installed_on'])){
			$ignore_rating = empty($settings['ignore_rating']) ? "" : $settings['ignore_rating'];
			if($ignore_rating != "yes"){
				$date1 = $settings['installed_on'];
				$date2 = date("Y/m/d");
				$diff = abs(strtotime($date2) - strtotime($date1));
				$days = floor($diff / (60*60*24));
				if($days >= 7){
					$TkNa_new_URI = $_SERVER['REQUEST_URI'];
					$TkNa_new_URI = add_query_arg('tkna-ignore-notice', '0', $TkNa_new_URI);
					echo '<div style="min-height:80px;margin:10px 0;padding:10px;display: -webkit-box;display: -ms-flexbox;display: flex;-webkit-box-align: center;-ms-flex-align: center;
					align-items: center;" class="notice notice-success is-dismissible"><img style="width: 80px;margin-right: 8px;border-radius: 4px;float: left;" src="'.esc_url(plugin_dir_url(dirname( __FILE__ ) )).'img/icon-128x128.png" alt="tarokina free"><p style="margin-right:30px;">';
					printf(__('Awesome! You have been using <a style="display: inline-block" href="edit.php?post_type=tarokkina_free&page=crb_carbon_fields_container_tarot_free.php">Tarokina Free</a> for more than 1 week. Would you mind taking a few seconds to give it a 5-star rating on WordPress? Thank you in advance 🙂 | <a style="display: inline-block" href="%2$s" target="_blank">Ok, you deserved it</a> | <a style="display: inline-block" href="%1$s">I already did</a> | <a style="display: inline-block" href="%1$s">No, not good enough</a>', 'tarokina-free'), $TkNa_new_URI,
					'https://wordpress.org/support/plugin/tarokina-free/reviews/?filter=5');
					echo "</p></div>";
				}
			}
		}
	}
	public function TkNa_ignore_notice(){
		if(isset($_GET['tkna-ignore-notice']) && $_GET['tkna-ignore-notice'] == "0"){
			$settings = get_option('TkNa_settings');
			$settings['ignore_rating'] = "yes";
			update_option('TkNa_settings', $settings, "no");
		}
	}


	
	// =============================================================
	//      Notice Borrar cache despues de actualizar
	// =============================================================

	// Notice Clear cache button
	public function TkNaP_notice_clear_cache_free(){
		?>
		 <div style="margin-left:0;margin-right:22px" class="notice notice-success settings-success is-dismissible"> 
				<p style="padding: 8px">
				<svg class="svg-icon" style="width:25px; height:25px; vertical-align:bottom;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M837.818182 915.549091h-131.956364c-12.8 0-23.272727-10.472727-23.272727-23.272727v-58.181819c0-4.189091-8.843636-11.636364-23.272727-11.636363s-23.272727 7.447273-23.272728 11.636363v58.181819c0 12.8-10.472727 23.272727-23.272727 23.272727H465.454545c-12.8 0-23.272727-10.472727-23.272727-23.272727s10.472727-23.272727 23.272727-23.272728h124.043637v-34.909091c0-32.581818 30.72-58.181818 69.818182-58.181818s69.818182 25.6 69.818181 58.181818v34.909091H814.545455v-232.727272H209.454545v232.727272h93.789091c12.8 0 23.272727 10.472727 23.272728 23.272728s-10.472727 23.272727-23.272728 23.272727H186.181818c-12.8 0-23.272727-10.472727-23.272727-23.272727v-279.272728c0-12.8 10.472727-23.272727 23.272727-23.272727h651.636364c12.8 0 23.272727 10.472727 23.272727 23.272727v279.272728c0 12.8-10.472727 23.272727-23.272727 23.272727z" fill="#040000" /><path d="M837.818182 636.276364H186.181818c-12.8 0-23.272727-10.472727-23.272727-23.272728v-69.818181c0-11.403636 8.145455-20.945455 19.316364-22.807273l244.596363-43.054546V124.276364c0-26.065455 23.970909-46.545455 54.225455-46.545455h62.138182c30.487273 0 54.225455 20.48 54.225454 46.545455V477.090909l244.596364 43.054546c11.170909 1.861818 19.316364 11.636364 19.316363 22.807272v69.818182A23.738182 23.738182 0 0 1 837.818182 636.276364z m-628.363637-46.545455h605.09091v-26.996364l-244.596364-43.054545a23.202909 23.202909 0 0 1-19.316364-22.807273V126.138182a13.172364 13.172364 0 0 0-7.68-2.094546h-62.138182c-3.723636 0-6.516364 1.163636-7.68 2.094546v370.269091c0 11.403636-8.145455 20.945455-19.316363 22.807272L209.454545 562.734545v26.996364z" fill="#040000" /></svg>
				<strong><?php echo esc_html('Cache cleared','tarokina-free'); ?>.</strong>
			</p>
			</div>
		<?php
		 delete_option( 'tkna_notice_updatePlugin_clear_cache_free' );
	}




		// If update plugin... update tarots
		public function TkNaP_pro_create_tarots_update_free( $upgrader_object, $options ) {
			$current_plugin_path_name = 'tarokina-free/tarokina-free.php';	
			if ($options['action'] == 'update' && $options['type'] == 'plugin' ) {
				
				foreach($options['plugins'] as $each_plugin) {
					if ( $each_plugin==$current_plugin_path_name ) {
						update_option( 'tkna_notice_updatePlugin_clear_cache_free', 'yes','yes' );
						delete_option('_tarokki_checkCartas_free' );	

					}
				}

			}
		}


}
