<?php

/*
*	Admin functions and definitions
*
* 	@version	1.0
* 	@author		Euthemians Team
* 	@URI		http://euthemians.com
*/

/**
 * Default hidden metaboxes for portfolio
 */
function engic_eutf_change_default_hidden( $hidden, $screen ) {
    if ( 'portfolio' == $screen->id ) {
        $hidden = array_flip( $hidden );
        unset( $hidden['portfolio_categorydiv'] ); //show portfolio category box
        $hidden = array_flip( $hidden );
        $hidden[] = 'postexcerpt';
		$hidden[] = 'postcustom';
		$hidden[] = 'commentstatusdiv';
		$hidden[] = 'commentsdiv';
		$hidden[] = 'authordiv';
    }
    return $hidden;
}
add_filter( 'default_hidden_meta_boxes', 'engic_eutf_change_default_hidden', 10, 2 );


/**
 * Enqueue scripts and styles for the back end.
 */
function engic_eutf_backend_scripts( $hook ) {
	global $post;

	wp_register_style( 'engic-eutf-page-feature-section', get_template_directory_uri() . '/includes/css/eut-page-feature-section.css', array(), '1.0.2' );
	wp_register_style( 'engic-eutf-admin-meta', get_template_directory_uri() . '/includes/css/eut-admin-meta.css', array(), '1.0' );
	wp_register_style( 'engic-eutf-custom-sidebars', get_template_directory_uri() . '/includes/css/eut-custom-sidebars.css' );


	$engic_eutf_upload_slider_texts = array(
		'modal_title' => esc_html__( 'Insert Images', 'engic' ),
		'modal_button_title' => esc_html__( 'Insert Images', 'engic' ),
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
	);

	$engic_eutf_upload_image_replace_texts = array(
		'modal_title' => esc_html__( 'Replace Image', 'engic' ),
		'modal_button_title' => esc_html__( 'Replace Image', 'engic' ),
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
	);

	$engic_eutf_upload_media_texts = array(
		'modal_title' => esc_html__( 'Insert Media', 'engic' ),
		'modal_button_title' => esc_html__( 'Insert Media', 'engic' ),
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
	);

	$engic_eutf_upload_image_texts = array(
		'modal_title' => esc_html__( 'Insert Image', 'engic' ),
		'modal_button_title' => esc_html__( 'Insert Image', 'engic' ),
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
	);

	$engic_eutf_feature_section_texts = array(
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
	);

	$engic_eutf_custom_sidebar_texts = array(
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
	);

	wp_register_script( 'engic-eutf-custom-sidebars', get_template_directory_uri() . '/includes/js/eut-custom-sidebars.js', array( 'jquery'), false, '2.0.0' );
	wp_localize_script( 'engic-eutf-custom-sidebars', 'engic_eutf_custom_sidebar_texts', $engic_eutf_custom_sidebar_texts );

	wp_register_script( 'engic-eutf-upload-slider-script', get_template_directory_uri() . '/includes/js/eut-upload-slider.js', array( 'jquery'), false, '2.0.0' );
	wp_localize_script( 'engic-eutf-upload-slider-script', 'engic_eutf_upload_slider_texts', $engic_eutf_upload_slider_texts );

	wp_register_script( 'engic-eutf-upload-feature-slider-script', get_template_directory_uri() . '/includes/js/eut-upload-feature-slider.js', array( 'jquery'), false, '2.0.0' );
	wp_localize_script( 'engic-eutf-upload-feature-slider-script', 'engic_eutf_upload_feature_slider_texts', $engic_eutf_upload_slider_texts );

	wp_register_script( 'engic-eutf-upload-image-replace-script', get_template_directory_uri() . '/includes/js/eut-upload-image-replace.js', array( 'jquery'), false, '2.0.0' );
	wp_localize_script( 'engic-eutf-upload-image-replace-script', 'engic_eutf_upload_image_replace_texts', $engic_eutf_upload_image_replace_texts );

	wp_register_script( 'engic-eutf-upload-simple-media-script', get_template_directory_uri() . '/includes/js/eut-upload-simple.js', array( 'jquery'), false, '2.0.0' );
	wp_localize_script( 'engic-eutf-upload-simple-media-script', 'engic_eutf_upload_media_texts', $engic_eutf_upload_media_texts );

	wp_register_script( 'engic-eutf-upload-image-script', get_template_directory_uri() . '/includes/js/eut-upload-image.js', array( 'jquery'), false, '2.0.0' );
	wp_localize_script( 'engic-eutf-upload-image-script', 'engic_eutf_upload_image_texts', $engic_eutf_upload_image_texts );

	wp_register_script( 'engic-eutf-page-feature-section-script', get_template_directory_uri() . '/includes/js/eut-page-feature-section.js', array( 'jquery', 'wp-color-picker' ), false, '2.0.0' );
	wp_localize_script( 'engic-eutf-page-feature-section-script', 'engic_eutf_feature_section_texts', $engic_eutf_feature_section_texts );

	wp_register_script( 'engic-eutf-post-options-script', get_template_directory_uri() . '/includes/js/eut-post-options.js', array( 'jquery'), false, '2.0.0' );
	wp_register_script( 'engic-eutf-portfolio-options-script', get_template_directory_uri() . '/includes/js/eut-portfolio-options.js', array( 'jquery'), false, '2.0.0' );



	if ( $hook == 'post-new.php' || $hook == 'post.php' ) {

        if ( 'post' === $post->post_type ) {

			wp_enqueue_style( 'engic-eutf-admin-meta' );
            wp_enqueue_style( 'engic-eutf-page-feature-section' );

			wp_enqueue_script( 'engic-eutf-upload-simple-media-script' );
			wp_enqueue_script( 'engic-eutf-upload-image-script' );
			wp_enqueue_script( 'engic-eutf-upload-slider-script' );
			wp_enqueue_script( 'engic-eutf-upload-feature-slider-script' );
			wp_enqueue_script( 'engic-eutf-upload-image-replace-script' );
			wp_enqueue_script( 'engic-eutf-page-feature-section-script' );
			wp_enqueue_script( 'engic-eutf-post-options-script' );

        } else if ( 'page' === $post->post_type || 'portfolio' === $post->post_type) {

			wp_enqueue_style( 'engic-eutf-admin-meta' );
			wp_enqueue_style( 'wp-color-picker' );
            wp_enqueue_style( 'engic-eutf-page-feature-section' );

			wp_enqueue_script( 'engic-eutf-upload-simple-media-script' );
			wp_enqueue_script( 'engic-eutf-upload-image-script' );
			wp_enqueue_script( 'engic-eutf-upload-slider-script' );
			wp_enqueue_script( 'engic-eutf-upload-feature-slider-script' );
			wp_enqueue_script( 'engic-eutf-upload-image-replace-script' );
			wp_enqueue_script( 'engic-eutf-page-feature-section-script' );
			if ( 'portfolio' === $post->post_type) {
				wp_enqueue_script( 'engic-eutf-portfolio-options-script' );
			}

        } else if ( 'testimonial' === $post->post_type || 'product' === $post->post_type) {

			wp_enqueue_style( 'engic-eutf-admin-meta' );

        }
    }


	if ( isset( $_GET['page'] ) && ( 'engic-eutf-custom-sidebar-settings' == $_GET['page'] ) ) {

		wp_enqueue_style( 'engic-eutf-custom-sidebars' );
		wp_enqueue_script( 'jquery-ui-sortable' );
		wp_enqueue_script( 'engic-eutf-custom-sidebars' );
	}

	wp_register_style(
		'redux-custom-css',
		get_template_directory_uri() . '/includes/css/eut-redux-panel.css',
		array(),
		time(),
		'all'
	);
	wp_enqueue_style( 'redux-custom-css' );

}
add_action( 'admin_enqueue_scripts', 'engic_eutf_backend_scripts', 10, 1 );

/**
 * Helper function to get custom fields with fallback
 */
function engic_eutf_post_meta( $id, $fallback = false ) {
	global $post;
	$post_id = $post->ID;
	if ( $fallback == false ) $fallback = '';
	$post_meta = get_post_meta( $post_id, $id, true );
	$output = ( $post_meta !== '' ) ? $post_meta : $fallback;
	return $output;
}

function engic_eutf_admin_post_meta( $post_id, $id, $fallback = false ) {
	if ( $fallback == false ) $fallback = '';
	$post_meta = get_post_meta( $post_id, $id, true );
	$output = ( $post_meta !== '' ) ? $post_meta : $fallback;
	return $output;
}

/**
 * Helper function to get theme options with fallback
 */
function engic_eutf_option( $id, $fallback = false, $param = false ) {
	global $engic_eutf_options;
	if ( $fallback == false ) $fallback = '';
	$output = ( isset($engic_eutf_options[$id]) && $engic_eutf_options[$id] !== '' ) ? $engic_eutf_options[$id] : $fallback;
	if ( !empty($engic_eutf_options[$id]) && $param ) {
		$output = ( isset($engic_eutf_options[$id][$param]) && $engic_eutf_options[$id][$param] !== '' ) ? $engic_eutf_options[$id][$param] : $fallback;
		if ( 'font-family' == $param ) {
			$output = urldecode( $output );
			if ( strpos($output, ' ') && !strpos($output, ',') ) {
				$output = '"' . $output . '"';
			}
		}
	}
	return $output;
}

/**
 * Helper function to print css code if not empty
 */
function engic_eutf_css_option( $id, $fallback = false, $param = false ) {
	$option = engic_eutf_option( $id, $fallback, $param );
	if ( !empty( $option ) && 0 != $option && $param ) {
		return $param . ': ' . $option . ';';
	}
}

/**
 * Helper function to get array value with fallback
 */
function engic_eutf_array_value( $input_array, $id, $fallback = false, $param = false ) {

	if ( $fallback == false ) $fallback = '';
	$output = ( isset($input_array[$id]) && $input_array[$id] !== '' ) ? $input_array[$id] : $fallback;
	if ( !empty($input_array[$id]) && $param ) {
		$output = ( isset($input_array[$id][$param]) && $input_array[$id][$param] !== '' ) ? $input_array[$id][$param] : $fallback;
	}
	return $output;
}

/**
 * Helper function to return trimmed css code
 */
function engic_eutf_get_css_output( $css ) {
	/* Trim css for speed */
	$css_trim =  preg_replace( '/\s+/', ' ', $css );

	/* Add stylesheet Tag */
	return "<!-- Dynamic css -->\n<style type=\"text/css\">\n" . $css_trim . "\n</style>";
}

/**
 * Helper functions to set/get current template
 */
function engic_eutf_set_current_view( $id ) {
	global $engic_eutf_options;
	$engic_eutf_options['current_view'] = $id;
}
function engic_eutf_get_current_view( $fallback = '' ) {
	global $engic_eutf_options;
	if ( $fallback == false ) $fallback = '';
	$output = ( isset($engic_eutf_options['current_view']) && $engic_eutf_options['current_view'] !== '' ) ? $engic_eutf_options['current_view'] : $fallback;
	return $output;
}

/**
 * Helper function for strings
 */
function engic_eutf_starts_with( $haystack, $needle ) {
     $length = strlen($needle);
     return (substr($haystack, 0, $length) === $needle);
}

/**
 * Helper function convert hex to rgb
 */
function engic_eutf_hex2rgb( $hex ) {
	$hex = str_replace("#", "", $hex);

	if(strlen($hex) == 3) {
		$r = hexdec( substr( $hex, 0, 1 ).substr( $hex, 0, 1) );
		$g = hexdec( substr( $hex, 1, 1 ).substr( $hex, 1, 1) );
		$b = hexdec( substr( $hex, 2, 1 ).substr( $hex, 2, 1) );
	} else {
		$r = hexdec( substr( $hex, 0, 2) );
		$g = hexdec( substr( $hex, 2, 2) );
		$b = hexdec( substr( $hex, 4, 2) );
	}
	$rgb = array($r, $g, $b);
	return implode(",", $rgb);
}

/**
 * Helper function to get theme visibility options
 */
function engic_eutf_visibility( $id, $fallback = '' ) {
	$visibility = engic_eutf_option( $id, $fallback  );
	if ( '1' == $visibility ) {
		return true;
	}
	return false;
}

/**
 * Backend Theme Activation Actions
 */
function engic_eutf_backend_theme_activation() {
	global $pagenow;

	if ( is_admin() && isset( $_GET['activated'] ) && $pagenow == 'themes.php' ) {
		//Redirect to Theme Options
		header( 'Location: ' . admin_url() . 'admin.php?page=engic_eutf_options&tab=0' ) ;
	}
}

add_action('admin_init','engic_eutf_backend_theme_activation');

/**
 * Check if Revolution slider is active
 */
function engic_eutf_is_revslider_active() {

	if ( class_exists('RevSlider') ) {
		return true;
	}
	return false;
}

/**
 * Check if to replace Backend Logo
 */
function engic_eutf_admin_login_logo() {

	$replace_logo = engic_eutf_option( 'replace_admin_logo' );
	if ( $replace_logo ) {
		$engic_eutf_logo = engic_eutf_option( 'logo','','url' );
		$engic_eutf_logo_height = engic_eutf_option( 'logo', '84', 'height' );
		if ( $engic_eutf_logo_height > 84 ) {
			$engic_eutf_logo_height = 84;
		}
		$engic_eutf_logo_width = engic_eutf_option( 'logo', '300', 'width' );
		if ( $engic_eutf_logo_width > 300 ) {
			$engic_eutf_logo_width = 300;
		}
		if ( !empty( $engic_eutf_logo ) ) {
			echo "
			<style>
			.login h1 a {
				background-image: url('" . $engic_eutf_logo . "');
				width: 100%;
				height: " . $engic_eutf_logo_height . "px;
				background-size: " . $engic_eutf_logo_width . "px auto;
			}
			</style>
			";
		}
	}
}
add_action( 'login_head', 'engic_eutf_admin_login_logo' );

/**
 * Disable SEO Page Analysis
 */
function engic_eutf_disable_page_analysis( $bool ) {
	if( '1' == engic_eutf_option( 'disable_seo_page_analysis', '0' ) ) {
		return false;
	}
	return $bool;
}
add_filter( 'wpseo_use_page_analysis', 'engic_eutf_disable_page_analysis' );

/**
 * Get Browser Platform
 */
function engic_eutf_browser_webkit_check() {

	if ( empty($_SERVER['HTTP_USER_AGENT'] ) ) {
		return false;
	}

	$u_agent = $_SERVER['HTTP_USER_AGENT'];

	if (
		( preg_match( '!linux!i', $u_agent ) || preg_match( '!windows|win32!i', $u_agent ) ) && preg_match( '!webkit!i', $u_agent )
	) {
		return true;
	}

	return false;
}

//Omit closing PHP tag to avoid accidental whitespace output errors.
