<?php

// This will add the theme options panel if the theme has this support

/*
*	TO DO :
*	Separate theme page css from HTML class css
*
*/

class ZnAdmin{

	public 	$theme_pages = array();
	public 	$data  = array();

	private $is_setup = false;
	private $is_update = false;

	function __construct() {

		$this->theme_data = ZN()->theme_data;

		add_action( 'admin_menu', 				array( &$this, 'zn_add_admin_pages'));
		add_action( 'admin_enqueue_scripts', 	array( &$this, 'zn_print_scripts') );
		add_action( 'admin_menu', 				array( &$this,  'edit_admin_menus' ) );
		add_action( 'current_screen', 			array( &$this, 'remove_actions' ) );

		add_action( 'admin_init', 				array( &$this, 'zn_permalink_settings_init') );
		add_action( 'admin_init', 				array( &$this, 'zn_permalink_settings_save') );

		// Check server connection
		add_action( 'admin_init', 				array( &$this, 'zn_check_server_connection') );

		add_action( 'zn_theme_installed', 		array( &$this, 'redirect_theme_install' ) );

		$this->is_setup = get_option( 'zn_theme_first_install', false );
		delete_option( 'zn_theme_first_install' );
	}

	// Check a connection to the server
	function zn_check_server_connection(){

		if( ! isset( $_GET['check_connection'] ) ) { return false; }

		global $wp_version;

		$response = wp_remote_get(ZN()->theme_data['server_url']);
		$response_code = wp_remote_retrieve_response_code( $response );

		if ( $response_code != 200 ) {
			set_transient( 'zn_server_connection_check', 'notok', 172800 );
		}else{
			set_transient( 'zn_server_connection_check', 'ok', 172800 );
		}

	}

	function redirect_theme_install(){
		wp_redirect( admin_url( 'admin.php?page=zn-about' ) );
		update_option( 'zn_theme_first_install', true, false );
		exit;
	}

	/*--------------------------------------------------------------------------------------------------
		Save the permalinks options
	--------------------------------------------------------------------------------------------------*/
	function zn_permalink_settings_save() {
		if ( ! is_admin() )
			return;

		// We need to save the options ourselves; settings api does not trigger save for the permalinks page
		if ( isset( $_POST['zn_permalinks'] ) /*|| isset( $_POST['zn_portfolio_item_slug_input'] ) */ ) {
			$permalinks = $_POST['zn_permalinks'];
			update_option( 'zn_permalinks', $permalinks );
			flush_rewrite_rules();
		}
	}

	/*--------------------------------------------------------------------------------------------------
		Add options for the portfolio and Documentation
	--------------------------------------------------------------------------------------------------*/
	static public function permalink_callback( $field ) {

		$permalinks = get_option( 'zn_permalinks' );

		?>
			<input name="zn_permalinks[<?php echo $field['id']; ?>]" type="text" class="regular-text code" value="<?php if ( isset( $permalinks[$field['id']] ) ) echo esc_attr( $permalinks[$field['id']] ); ?>" placeholder="<?php echo $field['id']; ?>" />
		<?php
	}

	function zn_permalink_settings_init() {

		$post_types = array();
		$taxonomies = array();
		$this->zn_allowed_post_types = apply_filters( 'zn_allowed_post_types', $post_types );
		$this->zn_allowed_taxonomies = apply_filters( 'zn_allowed_taxonomies', $taxonomies );

		foreach ( $this->zn_allowed_post_types as $id => $name) {

			$post_type_section_id = 'zn-'.$id.'-permalink';

			// SECTION : UNIQUE ID, NAME, CALLBACK, SETTINGS PAGE
			add_settings_section( $post_type_section_id, $name.' Slugs', '', 'permalink' );

			$this->add_settings_field( $id, $name, $post_type_section_id );

			if ( !empty( $this->zn_allowed_taxonomies[$id] ) ) {

				$current_taxonomies = $this->zn_allowed_taxonomies[$id];
				foreach ( $current_taxonomies as $key => $taxonomy) {
					$this->add_settings_field( $taxonomy['id'], $taxonomy['name'], $post_type_section_id );
				}

			}

		}
	}

	function add_settings_field( $id, $name, $section ) {

		// Add Slug option
		add_settings_field(
			$id,      	// id
			$name .' item slug', 	// setting title
			array(&$this,'permalink_callback'),  // display callback
			'permalink',                 				// settings page
			$section,                 				// settings section
			array(
				'id'	=> $id
			)
		);
	}

	function get_theme_options_pages(){

		if ( !file_exists(THEME_BASE.'/template_helpers/options/theme-pages.php') ) { return array(); }
		include( THEME_BASE.'/template_helpers/options/theme-pages.php');
		return apply_filters( 'zn_theme_pages', $admin_pages );
	}

	function get_theme_options(){
		include( THEME_BASE.'/template_helpers/options/theme-options.php' );
		return apply_filters( 'zn_theme_options', $admin_options );
	}


	/**
	 * Add all framework admin pages
	 * @return null
	 */
	function zn_add_admin_pages(){

		// Add the main page
		$this->data['theme_pages'] = $this->get_theme_options_pages();
		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LjYyNCAxNS4xODQsNTUuMzI4IDIxLjM1NSw2MC40MTggTDI4LjA3NywxMy40ODkgQzE4LjE4MywxNy43MzEgMTEuMjUsMjcuNTU0IDExLjI1LDM5IEwxMS4yNSwzOSBaIE00MS42MDQsMTEuMzgyIEwzNy4xNzQsNDIuMzQ1IEwzOC4wODEsNDIuMzQ1IEMzOC44Myw0Mi4zNDUgMzkuNDM1LDQyLjI0NiAzOS44OTcsNDIuMDQ4IEM0MC4zNTcsNDEuODUxIDQwLjgxOCw0MS40OTIgNDEuMjgsNDAuOTczIEw1MC41MjksMzAuMTQ0IEM1MS4wNzYsMjkuNTAxIDUxLjY4MSwyOS4wMzEgNTIuMzQ0LDI4LjczNSBDNTMuMDA2LDI4LjQzOCA1My44MTMsMjguMjg5IDU0Ljc2NCwyOC4yODkgTDY0LjYwMywyOC4yODkgQzYwLjczMSwxOS4wNDUgNTIsMTIuMzUgNDEuNjA0LDExLjM4MiBMNDEuNjA0LDExLjM4MiBaIiBpZD0iU2hhcGUiIHNrZXRjaDp0eXBlPSJNU1NoYXBlR3JvdXAiPjwvcGF0aD4gICAgICAgIDwvZz4gICAgPC9nPjwvc3ZnPg==";
		$page = add_menu_page( ZN()->theme_data['name'] .' Theme', ZN()->theme_data['name'] .' Theme', 'manage_options', 'zn-about', array(&$this, 'about_screen'), $icon );

		// Add all subpages
		foreach ( $this->data['theme_pages'] as $key => $value ) {

			/* CREATE THE SUBPAGES */
			$this->theme_pages[] = add_submenu_page(
				'zn-about',
				$value['title'],
				$value['title'],
				'manage_options',
				'zn_tp_'.$key,
				array(&$this, 'zn_render_page')
			);

		}
	}

	/**
	 * Replace the first menu title quick setup / update screen / dashboard
	 * @return [type] [description]
	 */
	function edit_admin_menus() {
		global $submenu;

		$menu_name = 'Dashboard';
		if ( $this->is_setup ){
			$menu_name = 'Quick setup';
		}

		if ( current_user_can( 'manage_options' ) ) {
			$submenu['zn-about'][0][0] = $menu_name;
		}
	}

	/**
	 * Removes all WP actions so we can have a clean page
	 * @return null
	 */
	function remove_actions(){

		$screen = get_current_screen();

		if ( in_array( $screen->id, $this->theme_pages ) ) {
			remove_all_actions( 'admin_notices' );
		}

		return false;
	}


	function zn_print_scripts( $hook ){

		/* Set default theme pages where the js and css should be loaded */
		$this->theme_pages[] = 'post.php';
		$this->theme_pages[] = 'post-new.php';
		$this->theme_pages[] = 'widgets.php';
		$this->theme_pages   = apply_filters( 'zn_theme_pages', $this->theme_pages );

		// Load about page scripts
		if( $hook === 'toplevel_page_zn-about' ) {

			wp_enqueue_style( 'zn_about_style', FW_URL .'/admin/assets/css/zn_about.css', array(), ZN()->version );
			wp_enqueue_script( 'jquery-ui-tooltip');
			wp_enqueue_script( 'zn_about_script', FW_URL .'/admin/assets/js/zn_about.js', array('jquery'), ZN()->version );
		}

		if ( ! in_array( $hook, $this->theme_pages ) ) {
			return;
		}

		// Don't allow heartbeat in the advanced page
		if( $hook == 'kallyas-options_page_zn_tp_advanced_options' ){
			wp_deregister_script('heartbeat');
		}

		// LOAD CUSTOM SCRIPTS
		wp_enqueue_script( 'zn_theme_ajax_callback', FW_URL .'/assets/js/zn_theme_ajax_callback.js', 'jquery','',true );
		add_action('admin_print_styles', array( &$this, 'admin_css' ) );

		ZN()->load_html_scripts();
	}

	function admin_css(){
		echo '<!-- ICON FONTS CSS -->';
		echo '<style type="text/css">';
			echo ZN()->icon_manager->set_css( '' );
		echo '</style>';
	}

	function zn_render_page() {

		// Get the curent slug
		$slug = $_GET['page'];
		$slug = str_replace( 'zn_tp_', '', $slug );
		$this->data['slug'] = $slug;


		$this->data['theme_options'] = $this->get_theme_options();
		ZN()->html()->zn_set_data( $this->data );

		echo ZN()->html()->zn_page_start();

		echo ZN()->html()->zn_render_page_options();

		echo ZN()->html()->zn_page_end();
	}

	/**
	 * Renders the admin pages
	 * @return [type] [description]
	 */
	function about_screen(){
		include( dirname(__FILE__) .'/tmpl/header-tmpl.php' );
		include( dirname(__FILE__) .'/tmpl/content-tmpl.php' );
		include( dirname(__FILE__) .'/tmpl/footer-tmpl.php' );
	}

}


?>
