<?php
/**
 * Versatile Corporate Theme Options Panel
 *
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Start Class
if ( ! class_exists( 'Versatile_Corporate_Theme_Options' ) ) {

	class Versatile_Corporate_Theme_Options {

		/**
		 * Returns single theme option
		 *
		 * @since 1.0.0
		 */
		public static function get_theme_option( $id ) {

			if ( get_theme_mod($id) != '' ) {
				return get_theme_mod($id);
			}
			else {
				return null;
			}
		}

		/**
		 * Register Theme settings on Customizer
		 *
		 * @since 1.0.0
		 */
		public static function register( $wp_customize ) {

			$wp_customize->add_section( 'versatile_corporate_theme_options', 
				array(
					'title'       => __( 'Theme Options', 'versatile-corporate' ),
					'priority'    => 35,
					'capability'  => 'edit_theme_options',
					'description' => __('Footer Content and Social Media Links', 'versatile-corporate'),
				)
			);

			$wp_customize->add_setting( 'versatile_corporate_email_text_block', array(
				'sanitize_callback' => 'sanitize_text_field'
			) );

			$wp_customize->add_control( new WP_Customize_Control(
			    $wp_customize,
			    'versatile_corporate_email_text',
			        array(
			            'label'    => __( 'Email Address', 'versatile-corporate' ),
			            'section'  => 'versatile_corporate_theme_options',
			            'settings' => 'versatile_corporate_email_text_block',
			            'type'     => 'text'
			        )
			    )
			);

			$wp_customize->add_setting( 'versatile_corporate_phone_text_block', array(
				'sanitize_callback' => 'sanitize_text_field'
			) );

			$wp_customize->add_control( new WP_Customize_Control(
			    $wp_customize,
			    'versatile_corporate_phone_text',
			        array(
			            'label'    => __( 'Phone Number', 'versatile-corporate' ),
			            'section'  => 'versatile_corporate_theme_options',
			            'settings' => 'versatile_corporate_phone_text_block',
			            'type'     => 'text'
			        )
			    )
			);

			$wp_customize->add_setting( 'versatile_corporate_address_text_block', array(
				'capability' => 'edit_theme_options',
				'sanitize_callback' => 'sanitize_textarea_field',
			) );

			$wp_customize->add_control(  new WP_Customize_Control(
				$wp_customize,
				'versatile_corporate_address_text',
					array(
			  			'type' 			=> 'textarea',
			  			'section' 		=> 'versatile_corporate_theme_options',
			  			'settings' 		=> 'versatile_corporate_address_text_block',
			  			'label' 		=> __( 'Address', 'versatile-corporate' ),
					)
				)
			);

			$wp_customize->add_setting( 'versatile_corporate_credits_block', array(
				'capability' => 'edit_theme_options',
				'sanitize_callback' => 'sanitize_textarea_field',
			) );

			$wp_customize->add_control(  new WP_Customize_Control(
				$wp_customize,
				'versatile_corporate_credits_text',
					array(
			  			'type' 			=> 'textarea',
			  			'section' 		=> 'versatile_corporate_theme_options',
			  			'settings' 		=> 'versatile_corporate_credits_block',
			  			'label' 		=> __( 'Footer Credits', 'versatile-corporate' ),
					)
				)
			);

			// Social Media links
			$wp_customize->add_setting( 'versatile_corporate_facebook_text_block', array(
				'sanitize_callback' => 'sanitize_text_field'
			) );

			$wp_customize->add_control( new WP_Customize_Control(
			    $wp_customize,
			    'versatile_corporate_facebook_text',
			        array(
			            'label'    => __( 'Facebook Link', 'versatile-corporate' ),
			            'section'  => 'versatile_corporate_theme_options',
			            'settings' => 'versatile_corporate_facebook_text_block',
			            'type'     => 'text'
			        )
			    )
			);

			$wp_customize->add_setting( 'versatile_corporate_instagram_text_block', array(
				'sanitize_callback' => 'sanitize_text_field'
			) );

			$wp_customize->add_control( new WP_Customize_Control(
			    $wp_customize,
			    'versatile_corporate_instagram_text',
			        array(
			            'label'    => __( 'Instagram Link', 'versatile-corporate' ),
			            'section'  => 'versatile_corporate_theme_options',
			            'settings' => 'versatile_corporate_instagram_text_block',
			            'type'     => 'text'
			        )
			    )
			);

			$wp_customize->add_setting( 'versatile_corporate_twitter_text_block', array(
				'sanitize_callback' => 'sanitize_text_field'
			) );

			$wp_customize->add_control( new WP_Customize_Control(
			    $wp_customize,
			    'versatile_corporate_twitter_text',
			        array(
			            'label'    => __( 'Twitter Link', 'versatile-corporate' ),
			            'section'  => 'versatile_corporate_theme_options',
			            'settings' => 'versatile_corporate_twitter_text_block',
			            'type'     => 'text'
			        )
			    )
			);

			$wp_customize->add_setting( 'versatile_corporate_google_text_block', array(
				'sanitize_callback' => 'sanitize_text_field'
			) );

			$wp_customize->add_control( new WP_Customize_Control(
			    $wp_customize,
			    'versatile_corporate_google_text',
			        array(
			            'label'    => __( 'Google Plus link', 'versatile-corporate' ),
			            'section'  => 'versatile_corporate_theme_options',
			            'settings' => 'versatile_corporate_google_text_block',
			            'type'     => 'text'
			        )
			    )
			);

			$wp_customize->get_setting( 'versatile_corporate_email_text_block' )->transport = 'postMessage';
			$wp_customize->get_setting( 'versatile_corporate_phone_text_block' )->transport = 'postMessage';
			$wp_customize->get_setting( 'versatile_corporate_address_text_block' )->transport = 'postMessage';
			$wp_customize->get_setting( 'versatile_corporate_credits_block' )->transport = 'postMessage';
		}
	}	
}

// Setup the Theme Customizer settings and controls...
add_action( 'customize_register' , array( 'Versatile_Corporate_Theme_Options' , 'register' ) );


// Helper function to use in theme to return a theme option value
function vcto_get_theme_option( $id = '' , $is_link = false) {

	if($is_link) {
		$chars = array('-', ' ', '(', ')');
		return str_replace($chars, "", Versatile_Corporate_Theme_Options::get_theme_option( $id ));
	}
	else {
		return Versatile_Corporate_Theme_Options::get_theme_option( $id );
	}

	return false;
}

// Helper function to use in theme to check if any of the option has value
function vcto_check_options( $ids = array() ) {
	$has_value = 0;

	foreach($ids as $id) {

		if( Versatile_Corporate_Theme_Options::get_theme_option( $id ) ){
			$has_value++;
		}
	}

	if($has_value == 0) {
		return false;
	}
	else {
		return true;
	}
}