<?php

/* ==========================================================================
 *  Customizer
 * ========================================================================== */

function realizer_customize_register( $realizer_customize_register ) {

	// REALIZER panel
	$realizer_customize_register->add_panel( 'realizer_panel', array(
		'title'			=> __( 'Theme settings', 'realizer' ),
		'description'	=> '',
		'priority'		=> 160,
	) );

		// Default post thumbnail setting
		$realizer_customize_register->add_section( 'realizer_default_post_thumbnail', array(
			'title'			=> __( 'Default post thumbnail settings', 'realizer' ),
			'description'	=> '',
			'panel'			=> 'realizer_panel',
			'priority'		=> 0,
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
		) );
		$realizer_customize_register->add_setting( 'realizer_default_post_thumbnail_setting', array(
			'type'				=> 'theme_mod',
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
			'default'			=> esc_url( get_template_directory_uri() ) . '/assets/images/realizer_default-post-thumbnail.png',
			'transport'			=> 'refresh',
			'sanitize_callback'	=> 'realizer_sanitize_image',
		) );
		$realizer_customize_register->add_control(
			new WP_Customize_Image_Control(
				$realizer_customize_register,
				'realizer_default_post_thumbnail_control',
				array(
					'priority'		=> 0,
					'section'		=> 'realizer_default_post_thumbnail',
					'settings'		=> 'realizer_default_post_thumbnail_setting',
					'label'			=> __( 'Default post thumbnail settings', 'realizer' ),
					'description'	=> __( 'Specifies the image to display for posts or static pages if no featured image is registered. If not specified, the image bundled with the REALIZER will be displayed.', 'realizer' ),
				)
			)
		);

		// Core WordPress patterns
		$realizer_customize_register->add_section( 'realizer_core_patterns', array(
			'title'			=> __( 'Core WordPress patterns settings', 'realizer' ),
			'description'	=> '',
			'panel'			=> 'realizer_panel',
			'priority'		=> 0,
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
		) );
		$realizer_customize_register->add_setting( 'realizer_core_patterns_setting', array(
			'type'				=> 'theme_mod',
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
			'default'			=> true,
			'transport'			=> 'refresh',
			'sanitize_callback'	=> 'realizer_sanitize_checkbox',
		) );
		$realizer_customize_register->add_control(
			new WP_Customize_Control(
				$realizer_customize_register,
				'realizer_core_patterns_control',
				array(
					'type'			=> 'checkbox',
					'priority'		=> 0,
					'section'		=> 'realizer_core_patterns',
					'settings'		=> 'realizer_core_patterns_setting',
					'label'			=> __( 'Disable core WordPress patterns', 'realizer' ),
					'description'	=> __( 'Disables WordPress core patterns and remote patterns that are automatically loaded from the official WordPress.org pattern directory.', 'realizer' ),
				)
			)
		);

		// Font Awesome
		$realizer_customize_register->add_section( 'realizer_fontawesome', array(
			'title'			=> __( 'Font Awesome settings', 'realizer' ),
			'description'	=> '',
			'panel'			=> 'realizer_panel',
			'priority'		=> 0,
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
		) );
		$realizer_customize_register->add_setting( 'realizer_fontawesome_setting', array(
			'type'				=> 'theme_mod',
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
			'default'			=> true,
			'transport'			=> 'refresh',
			'sanitize_callback'	=> 'realizer_sanitize_checkbox',
		) );
		$realizer_customize_register->add_control(
			new WP_Customize_Control(
				$realizer_customize_register,
				'realizer_fontawesome_control',
				array(
					'type'			=> 'checkbox',
					'priority'		=> 0,
					'section'		=> 'realizer_fontawesome',
					'settings'		=> 'realizer_fontawesome_setting',
					'label'			=> __( 'Enable Font Awesome (Recommended)', 'realizer' ),
					'description'	=> __( 'Enable Font Awesome. The version to load is "Font Awesome Free 6.7.2".', 'realizer' ),
				)
			)
		);

		// Pagetop button
		$realizer_customize_register->add_section( 'realizer_pagetop_button', array(
			'title'				=> __( 'Pagetop button settings', 'realizer' ),
			'description'		=> '',
			'panel'				=> 'realizer_panel',
			'priority'			=> 0,
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
		) );
		$realizer_customize_register->add_setting( 'realizer_pagetop_button_setting', array(
			'type'				=> 'theme_mod',
			'capability'		=> 'edit_theme_options',
			'theme_supports'	=> '',
			'default'			=> true,
			'transport'			=> 'refresh',
			'sanitize_callback'	=> 'realizer_sanitize_checkbox',
		) );
		$realizer_customize_register->add_control(
			new WP_Customize_Control(
				$realizer_customize_register,
				'realizer_pagetop_button_control',
				array(
					'type'			=> 'checkbox',
					'priority'		=> 0,
					'section'		=> 'realizer_pagetop_button',
					'settings'		=> 'realizer_pagetop_button_setting',
					'label'			=> __( 'Enable Pagetop button (Recommended)', 'realizer' ),
					'description'	=> __( 'Enable Pagetop button. Font Awesome is used for display, so please enable Font Awesome as well. Also, when you enable the page top button, a smooth scrolling effect is applied to the in-page links.', 'realizer' ),
				)
			)
		);

		function realizer_sanitize_image( $file, $setting ) {
			$mimes = array(
				'jpg|jpeg|jpe'	=> 'image/jpeg',
				'gif'			=> 'image/gif',
				'png'			=> 'image/png',
				'bmp'			=> 'image/bmp',
				'tif|tiff'		=> 'image/tiff',
				'ico'			=> 'image/x-icon'
			);
			//check file type from file name
			$file_ext = wp_check_filetype( $file, $mimes );
			//if file has a valid mime type return it, otherwise return default
			return ( $file_ext['ext'] ? $file : $setting->default );
		}

		function realizer_sanitize_checkbox( $checked ) {
			return ( ( isset( $checked ) && true == $checked ) ? true : false );
		}

}
add_action( 'customize_register', 'realizer_customize_register' );

/* ==========================================================================
 *  Output settings
 * ========================================================================== */

// Default post thumbnail
function realizer_post_thumbnail_html ( $html ) {
	$media = get_theme_mod ( 'realizer_default_post_thumbnail_setting' );
	if ( $html ) {
	} elseif ( $media ) {
		$html = '<img src="' . esc_url( $media ) . '" class="dafault-post-thumbnail" alt="' . esc_attr( get_the_title() ) . '" style="width:100%;height:100%;object-fit:cover;" decoding="async">';
	}
	return $html;
}
add_filter( 'post_thumbnail_html', 'realizer_post_thumbnail_html' );

// Core WordPress patterns
if ( get_theme_mod ( 'realizer_core_patterns_setting', true ) == "is-on" ) {
	remove_theme_support( 'core-block-patterns' );
}

// Font Awesome
if ( get_theme_mod ( 'realizer_fontawesome_setting', true ) == "is-on" ) {
	// See the function 'realizer_wp_enqueue_scripts' in function.php.
	function realizer_enqueue_editor_assets() {
		wp_enqueue_style( 'fontawesome-free-6.7.2-web', get_theme_file_uri( '/assets/fontawesome-free-6.7.2-web/css/all.min.css' ) );
	}
	add_action( 'enqueue_block_assets', 'realizer_enqueue_editor_assets' );
}

// Pagetop button
if ( get_theme_mod ( 'realizer_pagetop_button_setting', true ) == "is-on" ) {
	function realizer_pagetop_button() {
		echo '<div class="realizer-pagetop"><a aria-label="Pagetop" href="#wp-skip-link"><i class="fas fa-angle-up"></i></a></div>';
	}
	add_action( 'wp_footer', 'realizer_pagetop_button');
}