<?php
require_once INTENSE_PLUGIN_FOLDER . '/inc/OAuth/Intense-OAuth1.php';
require_once INTENSE_PLUGIN_FOLDER . '/inc/OAuth/Intense-OAuth2.php';
require_once INTENSE_PLUGIN_FOLDER . '/inc/options/redux/intense_icon/field_intense_icon.php';

if ( !class_exists( "Intense_Options" ) ) {

	class Intense_Options {
		public $args = array();
		public $sections = array();
		public $theme;
		public $ReduxFramework;
		public $instagram_OAuth;
		public $deviantART_OAuth;
		public $facebook_OAuth;
		public $google_OAuth;
		public $px500_OAuth;
		public $flickr_OAuth;

		public function __construct( ) {
			// Set the default arguments
			$this->setArguments();
			// Create the sections and fields
			$this->setSections();

			if ( !isset( $this->args['opt_name'] ) ) { // No errors please
				return;
			}

			$intense_visions_options = get_option( 'intense_visions_options' );
			$options_are_set = !empty( $intense_visions_options );
			$intense_redux_option_set = isset( $intense_visions_options['intense_redux'] );

			if ( !$options_are_set || !$intense_redux_option_set || ( $intense_redux_option_set && $intense_visions_options['intense_redux'] )  ) {
				if ( !class_exists( 'ReduxFramework' ) && file_exists( INTENSE_PLUGIN_FOLDER . '/inc/plugins/ReduxFramework/ReduxCore/framework.php' ) ) {
					require_once INTENSE_PLUGIN_FOLDER . '/inc/plugins/ReduxFramework/ReduxCore/framework.php';
				}
			}

			if ( !class_exists( "ReduxFramework" ) ) {
				return;
			}			

			// If Redux is running as a plugin, this will remove the demo notice and links
			add_action( 'redux/plugin/hooks', array( $this, 'remove_demo' ) );

			// Dynamically add a section. Can be also used to modify sections/fields
			add_filter( 'redux/options/'.$this->args['opt_name'].'/sections', array( $this, 'dynamic_section' ) );

			// Add custom CSS for the options
			add_action( 'redux/page/'.$this->args['opt_name'].'/enqueue', array( $this, 'addPanelCSS' ) );

			// Add custom CSS for the options
			add_action( 'redux/options/'.$this->args['opt_name'].'/settings/change', array( $this, 'options_settings_change' ) );

			// run code after save
			add_action( 'redux/options/'.$this->args['opt_name'].'/saved', array( $this, 'options_saved' ) );

			add_action( 'redux/extension/customizer/control/includes', array( $this, 'customizer_fields') );

			$this->ReduxFramework = new ReduxFramework( $this->sections, $this->args );
		}

		public function customizer_fields() {
			require_once INTENSE_PLUGIN_FOLDER . '/inc/options/redux/customizer_fields.php';
		}

		public function options_saved( ) {
			//clear out cache
			$uploads = wp_upload_dir();
			$intense_uploads_dir = trailingslashit( $uploads['basedir'] . '/intense-cache/' );
			$intense_uploads_path = $uploads['baseurl'] . '/intense-cache/';

			$files = scandir( $intense_uploads_dir );
			foreach ( $files as $file ) {
				if ( $file != "." && $file != ".." ) {
					if ( filetype( $intense_uploads_dir."/".$file ) != "dir" ) {
						try {
							unlink( $intense_uploads_dir."/".$file );
						} catch ( Exception $e ) {

						}
					}
				}
			}
		}

		public function addPanelCSS() {
			$minified = ( INTENSE_DEBUG ? "" : "min." );

			wp_register_style(
				'intense-redux-custom-css',
				INTENSE_PLUGIN_URL . '/assets/css/intense/options.' . $minified . 'css',
				array(), //array( 'redux-css' ), // Be sure to include redux-css so it's appended after the core css is applied
				time(),
				'all'
			);
			wp_enqueue_style( 'intense-redux-custom-css' );
		}

		public function options_settings_change() {
			flush_rewrite_rules(); //Resets the permalinks
		}

		public function setArguments() {
			$this->args = array(
				'dev_mode' => false,
				'dev_mode_icon_class' => 'icon-large',
				'opt_name' => 'intense_visions_options',
				'output_tag' => true,
				'display_name' => "Intense WordPress Plugin by Intense Visions",
				'display_version' => 'v' . INTENSE_PLUGIN_VERSION,
				'google_api_key' => 'AIzaSyBZYcKMU9hkCC9iBnDj3kdcX6c9E571vWw',
				'import_icon_class' => 'icon-large',
				'default_icon_class' => 'icon-large',
				'menu_type'  => 'submenu',
				'page_parent'        => 'options-general.php',
				'menu_title' => __( 'Intense', 'intense' ),
				'page_title' => __( 'Intense Options', 'intense' ),
				'page_slug' => 'intense_visions_options',
				'default_show' => false,
				'default_mark' => '*',
				'allow_sub_menu' => true,
				'menu_icon'=>'dashicons-intense-intense-logo',
				'show_import_export' => false,
				'admin_bar'          => false,
				'share_icons' => array(
					'twitter' => array(
						'link' => 'https://twitter.com/IntenseVisions',
						'title' => 'Follow Intense Visions on Twitter',
						'icon'  => 'el el-icon-twitter'
					),
					'facebook' => array(
						'link' => 'https://www.facebook.com/intensevision',
						'title' => 'Like Intense Visions on Facebook',
						'icon'  => 'el el-icon-facebook'
					),
					'google' => array(
						'link' => 'https://plus.google.com/b/100661863245884531948/100661863245884531948/',
						'title' => '+1 Intense Visions on Google+',
						'icon'  => 'el el-icon-googleplus'
					)
				),
			);
		}

		public function setSections() {
			global $intense_visions_multisite_options;

			require INTENSE_PLUGIN_FOLDER . '/inc/options/shortcodes.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/skins.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/posttypes.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/widgets.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/color.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/icons.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/featured.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/snippets.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/extras.php';

			if ( !is_multisite() ) {
				require INTENSE_PLUGIN_FOLDER . '/inc/options/license.php';
			}

			require INTENSE_PLUGIN_FOLDER . '/inc/options/social.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/images.php';	
			require INTENSE_PLUGIN_FOLDER . '/inc/options/gallery.php';			
		}

		public function setOAuth() {
			global $intense_visions_options;

			$this->instagram_OAuth = new Intense_OAuth2(
				"instagram",
				"https://api.instagram.com/oauth/authorize",
				"https://api.instagram.com/oauth/access_token",
				null
			);

			// sizes?: 400,600,800,900,1024,1280,1600,original
			$this->deviantART_OAuth = new Intense_OAuth2(
				"deviantART",
				"https://www.deviantart.com/oauth2/authorize",
				"https://www.deviantart.com/oauth2/token",
				null
			);

			$this->facebook_OAuth = new Intense_OAuth2(
				"facebook",
				"https://www.facebook.com/dialog/oauth",
				"https://graph.facebook.com/oauth/access_token",
				"user_photos,user_managed_groups"
			);

			$this->google_OAuth = new Intense_OAuth2(
				"google",
				"https://accounts.google.com/o/oauth2/auth",
				"https://accounts.google.com/o/oauth2/token",
				"https://picasaweb.google.com/data/"
			);

			$this->px500_OAuth = new Intense_OAuth1(
				"px500",
				"https://api.500px.com/v1/oauth/request_token",
				"https://api.500px.com/v1/oauth/authorize",
				"https://api.500px.com/v1/oauth/access_token",
				null
			);

			$this->smugmug_OAuth = new Intense_OAuth1(
				"smugmug",
				"http://api.smugmug.com/services/oauth/1.0a/getRequestToken",
				"http://api.smugmug.com/services/oauth/1.0a/authorize",
				"http://api.smugmug.com/services/oauth/1.0a/getAccessToken",
				null//"&Access=Full&Permissions=Read"
			);

			$this->flickr_OAuth = new Intense_OAuth1(
				"flickr",
				"http://www.flickr.com/services/oauth/request_token",
				"http://www.flickr.com/services/oauth/authorize",
				"http://www.flickr.com/services/oauth/access_token",
				"&perms=read"
			);

			$this->instagram_OAuth->get_access_token();
			$this->instagram_OAuth->get_access_token();
			$this->deviantART_OAuth->get_access_token();
			$this->facebook_OAuth->get_access_token();
			$this->google_OAuth->get_access_token();
			$this->px500_OAuth->get_access_token();
			$this->flickr_OAuth->get_access_token();
			$this->smugmug_OAuth->get_access_token();

			if ( isset( $_GET['page'] ) && $_GET['page'] == 'intense_visions_options' && ( isset(  $_GET['code'] ) || isset( $_GET['OAuth1'] ) ) ) {
				echo "Check your redirect URI setting";
				die( 1 );
			}
		}

		function dynamic_section( $sections ) {
			global $intense_visions_options;

			$this->setOAuth();	
			
			require INTENSE_PLUGIN_FOLDER . '/inc/options/photosource.php';
			require INTENSE_PLUGIN_FOLDER . '/inc/options/import_export.php';

			global $intense_shortcode_list;

			foreach ($intense_shortcode_list as $shortcode_class => $shortcode_info) {				
				$title = ucwords( str_replace( '-', ' ', $shortcode_info['directory'] ) );
				$key = $shortcode_info['directory'];

				$sections['Shortcodes']['fields']['intense_active_shortcodes']['options'][ strtolower( $shortcode_class ) ] = $title;
				$sections['Shortcodes']['fields']['intense_active_shortcodes']['default'][ strtolower( $shortcode_class ) ] = 1;

				if ( isset( $shortcode_info[ 'css' ] ) ) {
					$sections['Shortcodes']['fields']['intense_shortcode_css']['options'][ $key ] = $title;
					$sections['Shortcodes']['fields']['intense_shortcode_css']['default'][ $key ] = 1;
				}
			}

			natcasesort( $sections['Shortcodes']['fields']['intense_active_shortcodes']['options'] );
			natcasesort( $sections['Shortcodes']['fields']['intense_shortcode_css']['options'] );

			return $sections;
		}

		// Remove the demo link and the notice of integrated demo from the redux-framework plugin
		function remove_demo() {

			// Used to hide the demo mode link from the plugin page. Only used when Redux is a plugin.
			if ( class_exists( 'ReduxFrameworkPlugin' ) ) {
				remove_filter( 'plugin_row_meta', array( ReduxFrameworkPlugin::get_instance(), 'plugin_meta_demo_mode_link' ), null, 2 );
			}

			// Used to hide the activation notice informing users of the demo panel. Only used when Redux is a plugin.
			remove_action( 'admin_notices', array( ReduxFrameworkPlugin::get_instance(), 'admin_notices' ) );

		}
	}
}
