<?php
/**
 * GreyboxPro functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package GreyboxPro
 */

if ( ! function_exists( 'greyboxpro_setup' ) ) :
/**
 * Sets 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 for post thumbnails.
 */
function greyboxpro_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on GreyboxPro, use a find and replace
	 * to change 'greyboxpro' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'greyboxpro', get_template_directory() . '/languages' );


	

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


/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function greyboxpro_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'greyboxpro_content_width', 1170 );
}
add_action( 'after_setup_theme', 'greyboxpro_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function greyboxpro_widgets_init() {
    register_sidebar( array(
        'name'          => esc_html__( 'Sidebar', 'greyboxpro' ),
        'id'            => 'sidebar-1',
        'description'   => esc_html__( 'Add widgets here.', 'greyboxpro' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
    register_sidebar( array(
        'name'          => esc_html__( 'Footer 1', 'greyboxpro' ),
        'id'            => 'footer-1',
        'description'   => esc_html__( 'Add widgets here.', 'greyboxpro' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
    register_sidebar( array(
        'name'          => esc_html__( 'Footer 2', 'greyboxpro' ),
        'id'            => 'footer-2',
        'description'   => esc_html__( 'Add widgets here.', 'greyboxpro' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
    register_sidebar( array(
        'name'          => esc_html__( 'Footer 3', 'greyboxpro' ),
        'id'            => 'footer-3',
        'description'   => esc_html__( 'Add widgets here.', 'greyboxpro' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
}
add_action( 'widgets_init', 'greyboxpro_widgets_init' );


/**
 * Enqueue scripts and styles.
 */
function greyboxpro_scripts() {
	// load bootstrap css
   
    wp_enqueue_style( 'greyboxpro-bootstrap-css', get_template_directory_uri() . '/inc/assets/css/bootstrap.min.css' );
    wp_enqueue_style( 'greyboxpro-fontawesome-cdn', get_template_directory_uri() . '/inc/assets/css/fontawesome.min.css' );
 
	// load bootstrap css
	// load AItheme styles
	// load GreyboxPro styles
	wp_enqueue_style( 'greyboxpro-style', get_stylesheet_uri() );
    if(get_theme_mod( 'theme_option_setting' ) && get_theme_mod( 'theme_option_setting' ) !== 'default') {
        wp_enqueue_style( 'greyboxpro-'.get_theme_mod( 'theme_option_setting' ), get_template_directory_uri() . '/inc/assets/css/presets/theme-option/'.get_theme_mod( 'theme_option_setting' ).'.css', false, '' );
    }
    
	wp_enqueue_script('jquery');

    // Internet Explorer HTML5 support
    wp_enqueue_script( 'gbphtml5hiv',get_template_directory_uri().'/inc/assets/js/html5.js', array(), '3.7.0', false );
    wp_script_add_data( 'gbphtml5hiv', 'conditional', 'lt IE 9' );

	// load bootstrap js

    wp_enqueue_script('greyboxpro-popper', get_template_directory_uri() . '/inc/assets/js/popper.min.js', array(), '', true );
    wp_enqueue_script('greyboxpro-bootstrapjs', get_template_directory_uri() . '/inc/assets/js/bootstrap.min.js', array(), '', true );
   
    wp_enqueue_script('greyboxpro-themejs', get_template_directory_uri() . '/inc/assets/js/theme-script.min.js', array(), '', true );
	wp_enqueue_script( 'greyboxpro-skip-link-focus-fix', get_template_directory_uri() . '/inc/assets/js/skip-link-focus-fix.min.js', array(), '20151215', true );

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



function greyboxpro_password_form() {
    global $post;
    $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    $o = '<form action="' . esc_url( home_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
    <div class="d-block mb-3">' . __( "To view this protected post, enter the password below:", "greyboxpro" ) . '</div>
    <div class="form-group form-inline"><label for="' . $label . '" class="mr-2">' . __( "Password:", "greyboxpro" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" class="form-control mr-2" /> <input type="submit" name="Submit" value="' . esc_attr__( "Submit", "greyboxpro" ) . '" class="btn btn-primary"/></div>
    </form>';
    return $o;
}
add_filter( 'the_password_form', 'greyboxpro_password_form' );


/* Remove parent theme page templates */

function child_remove_page_templates( $page_templates ) {
	unset( $page_templates['fullwidth.php'] );
	unset( $page_templates['no-featureimage.php'] );
	unset( $page_templates['page.php'] );
	return $page_templates;
  }
  add_filter( 'theme_page_templates', 'child_remove_page_templates' );


add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
    if ( is_page_template( 'blog-article-no-banner.php' ) ) {
        $classes[] = 'post-template-default';
    }
	if ( is_page_template( 'light-mode.php' ) ) {
        $classes[] = 'page-template-default';
    }
    return $classes;
}

add_filter( 'body_class', function( $classes ) {
	if ( is_page_template( 'blog-article-no-banner.php' ) ) {
		foreach($classes as $key => $class) {
			if( $class == "post-template" ){
				unset($classes[$key]);
			}
		}
	}
	if ( is_page_template( 'light-mode.php' ) ) {
		foreach($classes as $key => $class) {
			if( $class == "page-template" ){
				unset($classes[$key]);
			}
		}
	}
    return $classes;
}, 1000);


/**
 * Get information about available image sizes
 */
function get_image_sizes( $size = '' ) {
    $wp_additional_image_sizes = wp_get_additional_image_sizes();
 
    $sizes = array();
    $get_intermediate_image_sizes = get_intermediate_image_sizes();
 
    // Create the full array with sizes and crop info
    foreach( $get_intermediate_image_sizes as $_size ) {
        if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
            $sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
            $sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
            $sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
        } elseif ( isset( $wp_additional_image_sizes[ $_size ] ) ) {
            $sizes[ $_size ] = array( 
                'width' => $wp_additional_image_sizes[ $_size ]['width'],
                'height' => $wp_additional_image_sizes[ $_size ]['height'],
                'crop' =>  $wp_additional_image_sizes[ $_size ]['crop']
            );
        }
    }
 
    // Get only 1 size if found
    if ( $size ) {
        if( isset( $sizes[ $size ] ) ) {
            return $sizes[ $size ];
        } else {
            return false;
        }
    }
    return $sizes;
}

// Remove custom image sizes
function remove_extra_image_sizes() {
    $wp_additional_image_sizes = wp_get_additional_image_sizes();
  foreach ( get_intermediate_image_sizes() as $size ) {
      remove_image_size( $size );
  }

  add_image_size( 'Small', 375); 
  add_image_size( 'Extra Large', 1920); 
}
add_action('init', 'remove_extra_image_sizes');


add_action('admin_head', 'remove_medium_large_size');

function remove_medium_large_size() {
  echo '<style>
  #elementor-panel .elementor-group-control-image-size select option[value="medium_large"]{
	display: none;
}
  </style>';
}


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

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';

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



function gbp_admin_login_logo() { 
    $logo_url = get_template_directory_uri() . '/assets/images/GreyboxPro-logo-1.svg';
?> 
    <style type="text/css"> 
        body.login div#login{
            padding: 12% 0 0;
        }
        body.login div#login h1 a {
            background-image: url(<?php echo $logo_url; ?>);
            background-size: 260px;
            width: 100%;
            height: 50px;
        } 
    </style>
<?php 
} 
add_action( 'login_enqueue_scripts', 'gbp_admin_login_logo' );

function wpb_login_logo_url() {
    return home_url();
}
add_filter( 'login_headerurl', 'wpb_login_logo_url' );
  
function wpb_login_logo_url_title() {
    return 'GreyboxPro';
}
add_filter( 'login_headertitle', 'wpb_login_logo_url_title' );

// disable srcset on frontend
function disable_wp_responsive_images() {
	return 1;
}
add_filter('max_srcset_image_width', 'disable_wp_responsive_images');

function shapeSpace_customize_image_sizes($sizes) {
	unset($sizes['medium_large']); // 768px
	return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'shapeSpace_customize_image_sizes');


/** Media Settings Override **/
//Thumbnail Size
update_option( 'thumbnail_size_w', 150 );
update_option( 'thumbnail_size_h', 150 );
update_option( 'thumbnail_crop', 1 );
//Medium Size
update_option( 'medium_size_w', 768 );
update_option( 'medium_size_h', 0 );
update_option( 'medium_crop', 1 );
//Large Size
update_option( 'large_size_w', 1024 );
update_option( 'large_size_h', 0 );
update_option( 'large_crop', 1 );


add_filter( 'body_class', function( $classes ) {
    if ( is_page_template( 'blog-article-sidebar.php' ) || is_page_template( 'blog-article.php' ) ) {
      $classes[] = 'page-template';
    } 
  return array_merge( $classes, array( 'custom' ) );
} );


/**
 * Set up the WordPress core custom header feature.
 *
 * @uses greyboxpro_header_style()
 */
function greyboxpro_custom_header_setup() {
	add_theme_support( 'custom-header', apply_filters( 'greyboxpro_custom_header_args', array(
		'default-image'          => '',
		'default-text-color'     => 'fff',
		'width'                  => 1000,
		'height'                 => 250,
		'flex-height'            => true,
		'wp-head-callback'       => '',
	) ) );
}
add_action( 'after_setup_theme', 'greyboxpro_custom_header_setup' );

if ( function_exists( 'register_block_style' ) ) {
    register_block_style(
        'core/quote',
        array(
            'name'         => 'blue-quote',
            'label'        => __( 'Blue Quote', 'greyboxpro' ),
            'is_default'   => true,
            'inline_style' => '.wp-block-quote.is-style-blue-quote { color: blue; }',
        )
    );
}

function wpdocs_register_block_patterns() {
    register_block_pattern(
        'wpdocs/my-example',
        array(
            'title'         => __( 'My First Block Pattern', 'greyboxpro' ),
            'description'   => _x( 'This is my first block pattern', 'Block pattern description', 'greyboxpro' ),
            'content'       => '<!-- wp:paragraph --><p>A single paragraph block style</p><!-- /wp:paragraph -->',
            'categories'    => array( 'text' ),
            'keywords'      => array( 'cta', 'demo', 'example' ),
            'viewportWidth' => 800,
        )
    );
}
add_action( 'init', 'wpdocs_register_block_patterns' );

/***
 * Add theme supports
 */

add_theme_support( 'custom-logo', array(
    'height' => 53,
    'width'  => 300,
) );

add_theme_support( 'title-tag' );
add_theme_support( 'align-wide' );
add_theme_support( 'wp-block-styles' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
add_theme_support( 'custom-background' );
/** post thumbnail **/
add_theme_support( 'post-thumbnails' );

/**
 * Registers an editor stylesheet for the theme.
 */
function gbp_wpdocs_theme_add_editor_styles() {
    add_editor_style( 'custom-editor-style.css' );
}
add_action( 'admin_init', 'gbp_wpdocs_theme_add_editor_styles' );