<?php
/**
 * Medo Pixel functions and definitions
 *
 * @package Medo Pixel
 */

if ( ! function_exists( 'medo_pixel_setup' ) ) :
	function medo_pixel_setup() {
		
		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		add_theme_support( 'woocommerce' );

		load_theme_textdomain( 'medo-pixel', get_template_directory() . '/languages' );

		/*
		 * Let WordPress manage the document title.
		 * By adding theme support, we declare that this theme does not use a
		 * hard-coded <title> tag in the document head, and expect WordPress to
		 * provide it for us.
		 */
		add_theme_support( 'title-tag' );

		/*
		 * Post thumbnail support should be enabled for pages and posts.
		 */
		add_theme_support( 'post-thumbnails' );

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus( array(
			'menu-1' => esc_html__( 'Primary', 'medo-pixel' ),
			'menu-2' => esc_html__( 'Footer', 'medo-pixel' ),
		) );

		/*
		 * To produce valid HTML5, change the default core markup for the comments, search form, and search form.
		 */
		add_theme_support( 'html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		) );
		
		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		add_theme_support( 'custom-logo', array(
			'width'       => 270,
			'height'      => 80,
			'flex-height' => true,
			'flex-width'  => true,
		) );

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		// Add custom image size.
		add_image_size( 'medo-pixel-1920-550', 1920, 550, true );
		add_image_size( 'medo-pixel-1370-550', 1370, 550, true );
		add_image_size( 'medo-pixel-590-310', 590, 310, true );
		add_image_size( 'medo-pixel-420-380', 420, 380, true );
		add_image_size( 'medo-pixel-420-300', 420, 300, true );
		add_image_size( 'medo-pixel-420-200', 420, 200, true );
		add_image_size( 'medo-pixel-290-150', 290, 150, true );
		add_image_size( 'medo-pixel-80-60', 80, 60, true );
		
		add_editor_style( array( '/assets/css/editor-style.min.css') );

		add_theme_support( 'align-wide' );
		add_theme_support( 'editor-styles' );
		add_theme_support( 'wp-block-styles' );

        add_theme_support( 'custom-background', apply_filters( 'medo_pixel_custom_background', array(
            'default-color' => 'ffffff',
            'default-image' => '',
        )));
        
        add_theme_support( 'responsive-embeds' );

		add_theme_support('custom-header', array(
			'default-image'      => '',
			'width'              => 1920,
			'height'             => 200,
			'flex-height'        => true,
			'flex-width'         => true,
			'uploads'            => true,
		));
		
		define('MEDO_PIXEL_BUY_NOW',__('https://www.themepixels.net/products/wp-theme-bundle/','medo-pixel'));
		define('MEDO_PIXEL_LIVE_DEMO',__('https://themepixels.net/','medo-pixel'));
		define('MEDO_PIXEL_FREE_DOC',__('https://www.themepixels.net/docs/medo-pixel-free/','medo-pixel'));
		define('MEDO_PIXEL_BUNDLE',__('https://www.themepixels.net/products/wp-theme-bundle/','medo-pixel'));
		define('MEDO_PIXEL_THEME_SUPPORT',__('https://wordpress.org/support/theme/medo-pixel','medo-pixel'));
		
		/**
		* FREE DEMO CONTENT.
		*/
		require get_template_directory() . '/inc/free-demo-content/medo_pixel_config_file.php';

	}
endif;
add_action( 'after_setup_theme', 'medo_pixel_setup' );

/**
 * Enqueue scripts and styles.
 */
function medo_pixel_scripts() {

	wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/bootstrap/css/bootstrap.min.css' );
	if ( is_rtl() ){
		wp_enqueue_style( 'bootstrap-rtl', get_template_directory_uri() . '/assets/bootstrap/css/rtl/bootstrap.min.css' );

		wp_enqueue_style( 'medo-pixel-rtl-style', get_template_directory_uri() . '/rtl.css' );

	}

	// Theme stylesheet.
	wp_enqueue_style( 'medo-pixel-style', get_stylesheet_uri() );
	require get_parent_theme_file_path( '/extra-pixel-customize.php' );
	wp_add_inline_style( 'medo-pixel-style',$medo_pixel_pix_theme_css );
	
	wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/assets/font-awesome/css/all.min.css' );
	wp_enqueue_style( 'medo-pixel-blocks', get_template_directory_uri() . '/assets/css/blocks.min.css' );
	wp_enqueue_style( 'medo-pixel-style', get_stylesheet_uri() );
	wp_enqueue_style( 'medo-pixel-google-font', 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,400i,600,700,800|Poppins:300,400,400i,500,600,700,800,900&display=swap', false );

	$scripts = array(
		array(
			'id'     => 'bootstrap',
			'url'    => get_template_directory_uri() . '/assets/bootstrap/js/bootstrap.min.js',
			'footer' => true
		),
		array(
			'id'     => 'medo-pixel-custom',
			'url'    => get_template_directory_uri() . '/assets/js/custom.js',
			'footer' => true
		)
	);

	medo_pixel_add_scripts( $scripts );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'medo_pixel_scripts' );

	// Change number or products per row to 6
	add_filter('loop_shop_columns', 'medo_pixel_loop_columns');
	if (!function_exists('medo_pixel_loop_columns')) {
		function medo_pixel_loop_columns() {
			$medo_pixel_columns = get_theme_mod( 'medo_pixel_per_columns', 3 );
			return $medo_pixel_columns;
		}
	}

	//Change number of products that are displayed per page (shop page)
	add_filter( 'loop_shop_per_page', 'medo_pixel_per_page', 10 );
	function medo_pixel_per_page( $medo_pixel_cols ) {
	  	$medo_pixel_cols = get_theme_mod( 'medo_pixel_product_per_page', 10 );
		return $medo_pixel_cols;
	}

	function medo_pixel_sanitize_number_absint( $number, $setting ) {
		// Ensure $number is an absolute integer (whole number, zero or greater).
		$number = absint( $number );

		// If the input is an absolute integer, return it; otherwise, return the default
		return ( $number ? $number : $setting->default );
	}

function medo_pixel_sanitize_number_range( $number, $setting ) {

	// Ensure input is an absolute integer.
	$number = absint( $number );

	// Get the input attributes associated with the setting.
	$atts = $setting->manager->get_control( $setting->id )->input_attrs;

	// Get minimum number in the range.
	$min = ( isset( $atts['min'] ) ? $atts['min'] : $number );

	// Get maximum number in the range.
	$max = ( isset( $atts['max'] ) ? $atts['max'] : $number );

	// Get step.
	$step = ( isset( $atts['step'] ) ? $atts['step'] : 1 );

	// If the number is within the valid range, return it; otherwise, return the default
	return ( $min <= $number && $number <= $max && is_int( $number / $step ) ? $number : $setting->default );
}

function medo_pixel_sanitize_checkbox( $input ) {
    // Boolean check
    return ( ( isset( $input ) && true == $input ) ? true : false );
}

/*radio button sanitization*/
function medo_pixel_sanitize_choices( $input, $setting ) {
    global $wp_customize;
    $control = $wp_customize->get_control( $setting->id );
    if ( array_key_exists( $input, $control->choices ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

/**
* Add script
* 
* @since Medo Pixel 1.0.0
*/
function medo_pixel_add_scripts( $scripts ){
	foreach ( $scripts as $key => $value ) {
		wp_enqueue_script( $value['id'] , $value['url'] , array( 'jquery', 'jquery-masonry' ), 0.8, $value['footer'] );
	}
}

/**
 * Sanitizes Image Upload.
 *
 * @param string $input potentially dangerous data.
 */
function medo_pixel_sanitize_image( $input ) {
	$filetype = wp_check_filetype( $input );
	if ( $filetype['ext'] && wp_ext2type( $filetype['ext'] ) === 'image' ) {
		return esc_url( $input );
	}
	return '';
}

/**
* Enqueue editor styles for Gutenberg
* 
* @since Medo Pixel 1.0.0
*/
function medo_pixel_block_editor_styles() {
	// Block styles.
	wp_enqueue_style( 'medo-pixel-block-editor-style', get_theme_file_uri( '/assets/css/editor-blocks.min.css' ) );
	// Google Font
	wp_enqueue_style( 'medo-pixel-google-font', 'https://fonts.googleapis.com/css?family=Poppins:300,400,400i,500,600,700,700i', false );
}
add_action( 'enqueue_block_editor_assets', 'medo_pixel_block_editor_styles' );

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer/customizer.php';

/**
 * Dynamic CSS.
 */
require get_template_directory() . '/inc/customizer/loader.php';

/**
* GET START.
*/
require get_template_directory() . '/themeinfo/medo_pixel_themeinfo_page.php';


// NOTICE FUNCTION
function medo_pixel_activation_notice() {

    if ( get_option( 'medo_pixel_notice_dismissed' ) ) {
        return;
    }

    if ( isset( $_GET['page'] ) && $_GET['page'] === 'medo-pixel-themeinfo-page' ) {
        return;
    }
    medo_pixel_theme_notice_content( true, false, true );
}

function medo_pixel_theme_notice_content(
    $medo_pixel_show_theme_info = true,
    $medo_pixel_show_box_1 = false,
    $medo_pixel_show_dismiss = true
) {
?>
	<div class="updated notice notice-theme-info-class <?php echo $medo_pixel_show_dismiss ? 'is-dismissible' : ''; ?>" data-notice="theme_info">
        <div class="medo-pixel-theme-info-notice clearfix">
            <div class="medo-pixel-theme-notice-content">
				<div class="notice-content">
					<div class="inner-notice-contetn">
						<h4 class="best-value"><?php esc_html_e( 'Best Value', 'medo-pixel' ); ?></h4>
						<h2 class="medo-pixel-notice-h2">
							<?php
							printf(
								/* translators: 1: Theme name */
								esc_html__('Get 30+ Premium WordPress Themes in One Bundle', 'medo-pixel'), '<strong>' . esc_html(wp_get_theme()->get('Name')) . '</strong>'
							);
							?>
						</h2>

						<p class="medo-pixel-notice-p">
							<?php
							printf(
								/* translators: 1: Theme name */
								esc_html__('Premium WordPress Themes for Business, Ecommerce, Blogs, Portfolio and more. Our themes are light & easy to customize ', 'medo-pixel'), '<strong>' . esc_html(wp_get_theme()->get('Name')) . '</strong>'
							);
							?>
						</p>
					</div>
					<div class="inner-notice-buttons">
						<?php if ( $medo_pixel_show_theme_info ) : ?>
							<a class="medo-pixel-btn-theme-info button button-primary bundlee"
								href="<?php echo esc_url( admin_url( 'themes.php?page=medo-pixel-themeinfo-page' ) ); ?>">
								<?php esc_html_e( 'Theme Info', 'medo-pixel' ); ?>
							</a>
						<?php endif; ?>
						<a class="medo-pixel-btn-theme-info button button-primary live-demoo" target="_blank" href="<?php echo esc_url(MEDO_PIXEL_BUY_NOW); ?>" id="medo-pixel-bundle-button"> <?php esc_html_e('Buy Now', 'medo-pixel') ?></a>
						<a class="medo-pixel-btn-theme-info button button-primary bundlee" target="_blank" href="<?php echo esc_url(MEDO_PIXEL_LIVE_DEMO); ?>" id="medo-pixel-collection-button"> <?php esc_html_e('Live Demo', 'medo-pixel') ?></a>
						<a class="medo-pixel-btn-theme-info button button-primary bundlee" target="_blank" href="<?php echo esc_url(MEDO_PIXEL_BUNDLE); ?>" 
						id="medo-pixel-collection-button"> <?php esc_html_e('Get Bundle', 'medo-pixel') ?></a>
					</div>
				</div>
				<div class="middle-box">
					<?php if ( $medo_pixel_show_box_1 ) : ?>
						<div class="box-1">
							<div class="box-info">
								<span class="dashicons dashicons-yes-alt"></span>
								<div>
									<p><?php esc_html_e( '30+ Premium', 'medo-pixel' ); ?></p>
									<p><?php esc_html_e( 'WordPress Themes', 'medo-pixel' ); ?></p>
								</div>
							</div>
							<div class="box-info">
								<span class="dashicons dashicons-yes-alt"></span>
								<div>
									<p><?php esc_html_e( 'Single Theme', 'medo-pixel' ); ?></p>
									<p><?php esc_html_e( 'Starting at $40', 'medo-pixel' ); ?></p>
								</div>
							</div>
							<div class="box-info">
								<span class="dashicons dashicons-yes-alt"></span>
								<div>
									<p><?php esc_html_e( 'One Time Payment -', 'medo-pixel' ); ?></p>
									<p><?php esc_html_e( 'no hidden charges', 'medo-pixel' ); ?></p>
								</div>
							</div>
						</div>
					<?php endif; ?>
					<div class="box-2">
						<p class="box-2-para"><?php esc_html_e( 'Total Value', 'medo-pixel' ); ?></p>
						<h4><?php esc_html_e( '$2499', 'medo-pixel' ); ?></h4>
						<p><?php esc_html_e( 'Bundle Price', 'medo-pixel' ); ?></p>
						<span><sup><?php esc_html_e( '$', 'medo-pixel' ); ?></sup><h2><?php esc_html_e( '59', 'medo-pixel' ); ?></h2></span>
						<p class="box-para"><?php esc_html_e( 'One Time Payement', 'medo-pixel' ); ?></p>
						<p class="box-btn"><?php esc_html_e( 'You Save $2430', 'medo-pixel' ); ?></p>
					</div>
				</div>
				<div class="notice-image">
					<a href="<?php echo esc_url( MEDO_PIXEL_BUNDLE ); ?>" target="_blank" rel="noopener noreferrer">
						<img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/assets/images/bundle-img.png' ); ?>" alt="<?php esc_attr_e( 'Theme Screenshot', 'medo-pixel' ); ?>">
					</a>
				</div>
            </div>
        </div>
    </div>
<?php
}

add_action('admin_notices', 'medo_pixel_activation_notice');

add_action('wp_ajax_medo_pixel_dismiss_notice', 'medo_pixel_dismiss_notice');

function medo_pixel_notice_status() {
    delete_option('medo_pixel_notice_dismissed');
}
add_action('after_switch_theme', 'medo_pixel_notice_status');

function medo_pixel_dismiss_notice() {
    update_option('medo_pixel_notice_dismissed', true);
    wp_send_json_success();
}

function medo_pixel_admin_enqueue_scripts(){
	wp_enqueue_style('medo-pixel-admin-style', esc_url( get_template_directory_uri() ) . '/assets/css/medo-pixel-notice.css');
	wp_enqueue_script('medo-pixel-dismiss-notice-script', get_stylesheet_directory_uri() . '/assets/js/medo-pixel-notice.js', array('jquery'), null, true);
}
add_action( 'admin_enqueue_scripts', 'medo_pixel_admin_enqueue_scripts' );

function medo_pixel_remove_customize_register() {
    global $wp_customize;

    $wp_customize->remove_setting( 'display_header_text' );
    $wp_customize->remove_control( 'display_header_text' );

}

add_action( 'customize_register', 'medo_pixel_remove_customize_register', 11 );