<?php
/**
 * Function describe for All1 
 * 
 * @package all1
 */

/**
 * Set a constant that holds the theme's minimum supported PHP version.
 */
define( 'ALL1_MIN_PHP_VERSION', '7.0' );

/**
 * Immediately after theme switch is fired we we want to check php version and
 * revert to previously active theme if version is below our minimum.
 */
add_action( 'after_switch_theme', 'all1_test_for_min_php' );

/**
 * Switches back to the previous theme if the minimum PHP version is not met.
 */
function all1_test_for_min_php() {

	// Compare versions.
	if ( version_compare( PHP_VERSION, ALL1_MIN_PHP_VERSION, '<' ) ) {
		// Site doesn't meet themes min php requirements, add notice...
		add_action( 'admin_notices', 'all1_min_php_not_met_notice' );
		// ... and switch back to previous theme.
		switch_theme( get_option( 'theme_switched' ) );
		return false;

	};
}

if ( ! function_exists( 'wp_body_open' ) ) {
        function wp_body_open() {
                do_action( 'wp_body_open' );
        }
}

/**
 * An error notice that can be displayed if the Minimum PHP version is not met.
 */
function all1_min_php_not_met_notice() {
	?>
	<div class="notice notice-error is_dismissable">
		<p>
			<?php esc_html_e( 'You need to update your PHP version to run this theme.', 'all1' ); ?> <br />
			<?php
			printf(
				/* translators: 1 is the current PHP version string, 2 is the minmum supported php version string of the theme */
				esc_html__( 'Actual version is: %1$s, required version is: %2$s.', 'all1' ),
				PHP_VERSION,
				ALL1_MIN_PHP_VERSION
			); // phpcs: XSS ok.
			?>
		</p>
	</div>
	<?php
}

if ( ! function_exists( 'all1_setup' ) ) :
	/**
	 * all1 setup.
	 *
	 * Set up theme defaults and registers support for various WordPress features.
	 *
	 * Note that this function is hooked into the after_setup_theme hook, which
	 * runs before the init hook. The init hook is too late for some features, such
	 * as indicating support post thumbnails.
	 *
	 */
	function all1_setup() {

		// Define and register starter content to showcase the theme on new sites.
		$starter_content = array(

			'widgets' => array(

				'footer-column-1-widget-area' => array(
					'recent-comments'
				),

				'footer-column-2-widget-area' => array(
					'recent-posts'
				),

				'footer-column-3-widget-area' => array(
					'calendar'
				),
			),

			'posts' => array(
				'home',
				'blog',
				'about',
				'contact'
			),

			// Default to a static front page and assign the front and posts pages.
			'options' => array(
				'show_on_front' => 'page',
				'page_on_front' => '{{home}}',
				'page_for_posts' => '{{blog}}',
			),

			// Set the front page section theme mods to the IDs of the core-registered pages.
			'theme_mods' => array(
				'all1_slider_display' => 1,
				'all1_slide1_image' => esc_url( get_stylesheet_directory_uri() . '/images/slides/1.jpg' ),
				'all1_slide1_content' => _x( '<h2>Slide 1 Title</h2><div class="slide-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div><a href="#" class="btn" title="Read more">Read more</a>', 'Theme starter content', 'all1' ),
				'all1_slide2_image' => esc_url( get_stylesheet_directory_uri() . '/images/slides/2.jpg' ),
				'all1_slide2_content' => _x( '<h2>Slide 2 Title</h2><div class="slide-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div><a href="#" class="btn" title="Read more">Read more</a>', 'Theme starter content', 'all1' ),
				'all1_slide3_image' => esc_url( get_stylesheet_directory_uri() . '/images/slides/3.jpg' ),
				'all1_slide3_content' => _x( '<h2>Slide 3 Title</h2><div class="slide-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div><a href="#" class="btn" title="Read more">Read more</a>', 'Theme starter content', 'all1' ),
			),

			'nav_menus' => array(
				// Assign a menu to the "primary" location.
				'primary' => array(
					'name' => __( 'Primary Menu', 'all1' ),
					'items' => array(
						'link_home',
						'page_blog',
						'page_contact',
						'page_about',
					),
				),

				// Assign a menu to the "footer" location.
				'footer' => array(
					'name' => __( 'Footer Menu', 'all1' ),
					'items' => array(
						'link_home',
						'page_about',
						'page_blog',
						'page_contact',
					),
				),
			),
		);

		$starter_content = apply_filters( 'all1_starter_content', $starter_content );
		add_theme_support( 'starter-content', $starter_content );
	}
endif; // all1_setup
add_action( 'after_setup_theme', 'all1_setup' );

add_action( 'wp_enqueue_scripts', 'all1_enqueue_styles' );
function all1_enqueue_styles() {

  	wp_enqueue_style( 'allingrid-stylesheet', get_template_directory_uri() . '/style.css' );
	wp_enqueue_style( 'all1-child-style', get_stylesheet_uri(), array( 'all1-stylesheet' ) );

	if ( is_front_page()
		&& get_option( 'show_on_front' ) == 'page'
		&& get_theme_mod('all1_slider_display', 0) == 1 ) {

		$customCSS = '';

		$customCSS .= '@media screen and (min-width: 800px) {';

		$customCSS .= 'body.home #header-main-fixed:not(.header-main-in-scroll) {background-color:transparent;}';

		$customCSS .= 'body.home #header-main-fixed:not(.header-main-in-scroll) #navmain > div > ul > li > a,body.home #header-main-fixed:not(.header-main-in-scroll) h1,body.home #header-main-fixed:not(.header-main-in-scroll) strong {color:#FFF;}';

		$customCSS .= 'body.home #header-spacer:not(.header-spacer-in-scroll) {display:none;}';

		$customCSS .= '#slider-wrapper {width:100vw;margin-left:calc(-50vw + 50%);margin-top:-3px;}';

		$customCSS .= '}';

		wp_add_inline_style( 'all1-inline-style', $customCSS );
	}
}

function all1_load_scripts() {

	wp_enqueue_script( 'slippry', get_stylesheet_directory_uri() . '/js/slippry.js',
			array( 'jquery' ) );

	wp_enqueue_script( 'all1-js', get_stylesheet_directory_uri() . '/js/all1.js',
			array( 'jquery', 'slippry' ) );
}
add_action( 'wp_enqueue_scripts', 'all1_load_scripts' );

if ( ! function_exists( 'all1_display_slider' ) ) :
	/**
	 * Displays the slider
	 */
	function all1_display_slider() { ?>
		<div id="slider-wrapper">
			<ul id="slider">
				<?php
					// display slides
					for ( $i = 1; $i <= 3; ++$i ) {
							
							$defaultSlideImage = get_stylesheet_directory_uri().'/images/slides/' . $i .'.jpg';

							$slideContent = get_theme_mod( 'all1_slide'.$i.'_content' );
							$slideImage = get_theme_mod( 'all1_slide'.$i.'_image', $defaultSlideImage );

						?>
							
						<?php if ( $slideImage ) : ?>
								<li>
									<a href="#slide<?php echo esc_attr($i); ?>">
									<img src="<?php echo esc_attr($slideImage); ?>" width="100%" alt="<?php echo str_replace('"', "'", $slideContent); ?>" class="slider-img" />
									</a>
								</li>
						<?php endif; ?>
		<?php		} ?>
			</ul>
		</div><!-- #slider-wrapper -->
	<?php 
	}
endif; // all1_display_slider

if ( ! function_exists( 'all1_sanitize_checkbox' ) ) :
	/**
	 * Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked`
	 * as a boolean value, either TRUE or FALSE.
	 *
	 * @param bool $checked Whether the checkbox is checked.
	 * @return bool Whether the checkbox is checked.
	 */
	function all1_sanitize_checkbox( $checked ) {
		// Boolean check.
		return ( ( isset( $checked ) && true == $checked ) ? true : false );
	}
endif; // all1_sanitize_checkbox

if ( ! function_exists( 'all1_sanitize_html' ) ) :

	function all1_sanitize_html( $html ) {
		return wp_kses_post( $html );
	}

endif; // all1_sanitize_html

if ( ! function_exists( 'all1_sanitize_url' ) ) :

	function all1_sanitize_url( $url ) {
		return esc_url_raw( $url );
	}

endif; // all1_sanitize_url

if ( ! function_exists( 'all1_customize_register' ) ) :
	/**
	 * Register theme settings in the customizer
	 */
	function all1_customize_register( $wp_customize ) {

		/**
		 * Add Slider Section
		 */
		$wp_customize->add_section(
			'all1_slider_section',
			array(
				'title'       => __( 'Slider', 'all1' ),
				'capability'  => 'edit_theme_options',
			)
		);

		// Add display slider option
		$wp_customize->add_setting(
				'all1_slider_display',
				array(
						'default'           => 0,
						'sanitize_callback' => 'all1_sanitize_checkbox',
				)
		);

		$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'all1_slider_display',
								array(
									'label'          => __( 'Display Slider on a Static Front Page', 'all1' ),
									'section'        => 'all1_slider_section',
									'settings'       => 'all1_slider_display',
									'type'           => 'checkbox',
								)
							)
		);
		
		for ($i = 1; $i <= 3; ++$i) {
		
			$slideContentId = 'all1_slide'.$i.'_content';
			$slideImageId = 'all1_slide'.$i.'_image';
			$defaultSliderImagePath = get_stylesheet_directory_uri().'/images/slides/'.$i.'.jpg';
		
			// Add Slide Content
			$wp_customize->add_setting(
				$slideContentId,
				array(
					'sanitize_callback' => 'all1_sanitize_html',
				)
			);
			
			$wp_customize->add_control( new WP_Customize_Control( $wp_customize, $slideContentId,
										array(
											'label'          => sprintf( esc_html__( 'Slide #%s Content', 'all1' ), $i ),
											'section'        => 'all1_slider_section',
											'settings'       => $slideContentId,
											'type'           => 'textarea',
											)
										)
			);
			
			// Add Slide Background Image
			$wp_customize->add_setting( $slideImageId,
				array(
					'default' => $defaultSliderImagePath,
					'sanitize_callback' => 'all1_sanitize_url'
				)
			);

			$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, $slideImageId,
					array(
						'label'   	 => sprintf( esc_html__( 'Slide #%s Image', 'all1' ), $i ),
						'section' 	 => 'all1_slider_section',
						'settings'   => $slideImageId,
					) 
				)
			);
		}
	}
endif; // all1_customize_register
add_action('customize_register', 'all1_customize_register');