<?php
/**
 * Plugin Name: Blazemedia Affiliation
 * Plugin URI: https://www.blazemedia.it/
 * Description: This is the ultimate Blazemedia plugin for links affiliation management.
 * Version: 2.0.1
 * Author: Blazemedia
 * Author URI: https://blazemedia.it/
 * License: http://www.apache.org/licenses/LICENSE-2.0
 * Text Domain: blz-affiliation
 * Network: false
 *
 * @package blz-affiliation
 *
 * Copyright 2023 Blazemedia (email: techteam@blazemedia.it)
 *
 */
define( 'BLAZE_AFFILIATION_PATH'   , plugin_dir_path( __FILE__ ) );
define( 'BLAZE_AFFILIATION_URI'    , plugin_dir_url( __FILE__ ) );
define( 'BLAZE_AFFILIATION_VERSION', "2.0.3");

if( defined('DOING_CRON') ) return;

$options = get_option('affiliation_settings_option_name');

$env   = !isset( $options['ambiente'] ) ? 'staging' : [
	'prod'    => 'production',
	'staging' => 'staging',
	'local'   => 'local',
	'newlp'   => 'newlp',
][ $options['ambiente'] ];

$cache = isset( $options['cache']    ) ? ( $options['cache'] == '0' ? false : '1' ) : true;

define( 'BLAZE_AFFILIATION_ENV',   $env   );
define( 'BLAZE_AFFILIATION_CACHE', $cache );

switch( BLAZE_AFFILIATION_ENV ) {

	case 'production' :

		define( 'BLZ_API_RENDERING_MANAGER', "https://rendering-manager.blazemedia.it/api/");
		define( 'BLZ_API_AUTHOR_MANAGER',    "https://author-manager.blazemedia.it/wp-json/api/v1/");
		define( 'BLZ_API_LINK_PROVIDER',     "https://stored-links-provider.blazemedia.it/wp-json/api/v1/");
		define( 'BLZ_API_FORMAT_PROVIDER',   "https://format-provider.blazemedia.it/wp-json/api/v1/");
		define( 'BLZ_FORMAT_PROVIDER',       "https://format-provider.blazemedia.it/wp/wp-admin");
		define( 'BLZ_API_BRANDED_CONTENT',   "https://branded-content-tracking-manager.blazemedia.it/api/");

		break;

 	case 'local' :

		define( 'BLZ_API_RENDERING_MANAGER', "http://rendering-manager.test/api/");
		define( 'BLZ_API_AUTHOR_MANAGER',    "http://author_manager.test/wp-json/api/v1/");
		define( 'BLZ_API_LINK_PROVIDER',     "http://stored-links-provider.test/api/");
		define( 'BLZ_API_FORMAT_PROVIDER',   "http://format_provider.test/wp-json/api/v1/");
		define( 'BLZ_FORMAT_PROVIDER',       "http://format_provider.test/wp/wp-admin");
		define( 'BLZ_API_BRANDED_CONTENT',   "http://branded-content-tracking-manager.test/api/");

		break;

 	case 'newlp' :
		define( 'BLZ_API_RENDERING_MANAGER', "https://rendering-manager.blazemedia.it/api/");
		define( 'BLZ_API_AUTHOR_MANAGER',    "https://author-manager.blazemedia.it/wp-json/api/v1/");
		define( 'BLZ_API_LINK_PROVIDER',     "https://stored-links-provider.blazemedia.it/api/");
		define( 'BLZ_API_FORMAT_PROVIDER',   "https://format-provider.blazemedia.it/wp-json/api/v1/");
		define( 'BLZ_FORMAT_PROVIDER',       "https://format-provider.blazemedia.it/wp/wp-admin");
		define( 'BLZ_API_BRANDED_CONTENT',   "https://branded-content-tracking-manager.blazemedia.it/api/");

		break;

	default : 
		define( 'BLZ_API_RENDERING_MANAGER', "https://laravel-rendering.test.blazemedia.it/api/");
		define( 'BLZ_API_AUTHOR_MANAGER',    "https://authors-manager.test.blazemedia.it/wp-json/api/v1/");
		define( 'BLZ_API_LINK_PROVIDER',     "https://stored-links-provider.test.blazemedia.it/api/");
		define( 'BLZ_API_FORMAT_PROVIDER',   "https://format-provider.test.blazemedia.it/wp-json/api/v1/");
		define( 'BLZ_FORMAT_PROVIDER',       "https://format-provider.test.blazemedia.it/wp/wp-admin");
		define( 'BLZ_API_BRANDED_CONTENT',   "https://branded-content-tracking-manager.test.blazemedia.it/api/");

		break; 
}
require_once BLAZE_AFFILIATION_PATH . '/vendor/autoload.php';

use Blazemedia\Affiliation\CustomPostTypes;
use Blazemedia\Affiliation\API;
use Blazemedia\Affiliation\Rendering;
use Blazemedia\Affiliation\CustomTaxonomies;
use Blazemedia\Affiliation\AdminUserInterface\Buttons;
use Blazemedia\Affiliation\PostAttributesCacheBuilder;
use Blazemedia\Affiliation\AdminUserInterface\Settings;
use Blazemedia\Affiliation\AdminUserInterface\MetaBoxes;
use Blazemedia\Affiliation\AdminUserInterface\Content;


class Affiliation {

	
	private $cacheBuilder;
	
	public function __construct() {

		CustomPostTypes\Zone::init();

		if( is_admin() || $this->is_wp_rest_api() ) {

			/// mostra le impostazioni in amministrazione
			$this->admin();

		} else {

			/// effettua il rendering dei formati in pagina
			add_action( 'wp', [ $this, 'render' ] );

			/// attiva le actions su save post per popolare le cache 
			$this->cacheBuilder = new PostAttributesCacheBuilder();

		}
		
	}


	function is_wp_rest_api() {

		return str_contains( $_SERVER['REQUEST_URI'], 'wp-json' );
	}


	function render() {

		/// verifica che il post type corrente sia abilitato
		/// per funzionare deve essre già pronta la query
		if ( !Settings\PostTypes::isPostTypeEnabled() ) return;
		
		/// ricava (probabilmente dalle cache) 
		/// tutti i dati dei formati in pagina
		$this->cacheBuilder->enableFormatsRendering();

		/// ricava i dati da assegnare alla singola pagina
		new Rendering\PageLinksData();

		/// ricava gli stili per il sito
		new Rendering\Styles();

		/// controlla se un post è Paid e carica le dipendenze
		/// new Rendering\PrintPaidJs(); -- rimosso per campagne google ads che non utilizzano il gclid
		//  new Rendering\PrintEbayDisclaimer();
	}


	private function admin(){

		/// aggiunge la pagina dei settings del plugin backend
		new Settings\SettingPage;

		/// gestione purge cache e transient
		new Settings\CacheAndTransient;
		/// aggiunge i bottoni per i link di affiliazione			
		new Buttons\AffiliateParseButton;

		// Aggiunge pulsante per l'aggiunta di un card
		new Buttons\AffiliateParseCard;
		
		new Buttons\AffiliateParseImage; 
		new Buttons\AffiliateButton;
		new Buttons\AffiliateLink;
		new Buttons\AffiliatePrice;
		new Buttons\AffiliateTable;
		new Buttons\AffiliateZone;
		new Buttons\AffiliateWidget;
		new Buttons\AffiliatePromo;
		new Buttons\AffiliateBranded;
		new Buttons\AffiliateParseCardAwin;
		new MetaBoxes\DisclaimerSettings;
		new MetaBoxes\PaidSettings;
		new Content\AsinChecker;
	}
}


/// Imposta ruolo Affiliation Manager e assegna le capability
/// per quel ruolo e per gli amministratori
Settings\Capability::getInstance( 'restrizioni_1' );
Settings\PostTypes::getInstance();

// Custom service taxonomy
$blz_tax          		= new CustomTaxonomies\BlzAffiliationAttributes;
$blz_tracking_customer  = new CustomTaxonomies\TrackingCustomer;

$articlesByAsin   = new API\ArticlesByAsin;
$articlesByITM    = new API\ArticlesByITM;
$proxy            = new API\Proxy;
//$render         = new API\Render;


$blzAffiliation = new Affiliation();