<?php
/**
 * Theme Functions.
 */

/* Theme Setup */
if (!function_exists('modern_construction_setup')):

function modern_construction_setup() {

	$GLOBALS['content_width'] = apply_filters('modern_construction_content_width', 640);

	add_theme_support('automatic-feed-links');
	add_theme_support('post-thumbnails');
	add_theme_support('title-tag');
	add_theme_support( 'align-wide' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'html5', array( 'comment-list', 'search-form', 'comment-form', ) );
	add_theme_support( 'wp-block-styles' );
	add_theme_support('custom-logo', array(
		'height'      => 240,
		'width'       => 240,
		'flex-height' => true,
	));

	add_theme_support('custom-background', array(
		'default-color' => 'f1f1f1',
	));

	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, icons, and column width.
	 */
	add_editor_style(array('css/editor-style.css', mega_construction_font_url()));
	// Theme Activation Notice
	global $pagenow;
	if (
		is_admin()
		&&
		('themes.php' == $pagenow)
		// &&
		// isset( $_GET['activated'] )
	) {
		add_action('admin_notices', 'modern_construction_activation_notice');
	}
}
endif;
add_action('after_setup_theme', 'modern_construction_setup');

function modern_construction_activation_notice() {
	$modern_construction_meta = get_option( 'modern_construction_admin_notice' );

	if (!$modern_construction_meta) {
	echo '<div id="modern-construction-welcome-notice" class="notice notice-success is-dismissible activation-notice">';
		echo '<div class="content">';
			echo '<h2>' . esc_html__( 'Thank You for Choosing Modern Construction by Themesglance!', 'modern-construction' ) . '</h2>';
			echo '<p class="notice-para">' . esc_html__( 'We’re thrilled to have you on board. To help you get started quickly and make the most out of your theme, check out the resources below:', 'modern-construction' ) . '</p>';
			echo '<p>';
				echo '<a href="' . esc_url( admin_url( 'themes.php?page=modern_construction_guide' ) ) . '" class="button">' . esc_html__( 'Get Started', 'modern-construction' ) . '</a>';
				echo '<a href="' . esc_url( 'https://preview.themesglance.com/mega-construction-pro/' ) . '" class="button" target="_blank">' . esc_html__( 'Demo', 'modern-construction' ) . '</a>';
				echo '<a href="' . esc_url( 'https://www.themesglance.com/products/premium-construction-wordpress-theme' ) . '" class="button" target="_blank">' . esc_html__( 'Get Premium', 'modern-construction' ) . '</a>';
				echo '<a href="' . esc_url( 'https://preview.themesglance.com/demo/docs/free-mega-construction/' ) . '" class="button" target="_blank">' . esc_html__( 'Documentation', 'modern-construction' ) . '</a>';
			echo '</p>';
		echo '</div>';
		echo '<div class="image-preview">';
                echo '<a  href="https://www.themesglance.com/products/wp-theme-bundle" target="_blank" class="pre-img rel="noopener noreferrer"">';
                echo '<img src="' . esc_url( get_template_directory_uri() . '/images/notice-img.png' ) . '" alt="Notice Image" />';
        echo '</a>';
		echo '</div>';
	echo '</div>';
}
}

// Admin notice code START
function modern_construction_dismissed_notice() {
	update_option( 'modern_construction_admin_notice', true );
}
add_action( 'wp_ajax_modern_construction_dismissed_notice', 'modern_construction_dismissed_notice' );


//After Switch theme function
add_action('after_switch_theme', 'modern_construction_getstart_setup_options');
function modern_construction_getstart_setup_options () {
    update_option('modern_construction_admin_notice', false );
}

add_action( 'wp_enqueue_scripts', 'modern_construction_enqueue_styles' );
function modern_construction_enqueue_styles() {
	$parent_style = 'mega-construction-basic-style'; // Style handle of parent theme.
	wp_enqueue_style( 'bootstrap-css', get_template_directory_uri().'/css/bootstrap.css' );
	wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
	wp_enqueue_style( 'modern-construction-style', get_stylesheet_uri(), array( $parent_style ) );
	if (is_singular() && comments_open() && get_option('thread_comments')) {
		wp_enqueue_script('comment-reply');
	}
	wp_enqueue_style( 'modern-construction-block-patterns-style-frontend', get_theme_file_uri('/block-patterns/css/block-frontend.css') );

	require get_theme_file_path( '/inc/color-option.php' );
	wp_add_inline_style( 'modern-construction-style',$modern_construction_custom_css);
}

function modern_construction_remove_parent_function() {
	remove_action( 'admin_notices', 'mega_construction_activation_notice' );
	remove_action( 'admin_menu', 'mega_construction_gettingstarted' );
}
add_action( 'init', 'modern_construction_remove_parent_function');

// Enqueue Customizer JS
function modern_construction_enqueue_customizer_js() {
	wp_enqueue_script('modern_construction-customizer-control', get_stylesheet_directory_uri() . '/js/customize-controls.js', array( 'jquery', 'customize-controls' ), false, true
	);
}
add_action( 'customize_controls_enqueue_scripts', 'modern_construction_enqueue_customizer_js' );

function modern_construction_enqueue_customizer_css() {
	wp_enqueue_style( 'modern_construction-customizer-main-css', get_stylesheet_directory_uri() . '/css/customize-controls.css' );
	}
add_action( 'customize_controls_print_styles', 'modern_construction_enqueue_customizer_css' );

function my_customize_register() {     
	global $wp_customize;
	$wp_customize->remove_setting( 'mega_construction_first_theme_color' );
	$wp_customize->remove_control( 'mega_construction_first_theme_color' );	
	$wp_customize->remove_setting( 'mega_construction_second_theme_color' );
	$wp_customize->remove_control( 'mega_construction_second_theme_color' );
	$wp_customize->remove_setting( 'mega_construction_reset_color' );
	$wp_customize->remove_control( 'mega_construction_reset_color' );
	$wp_customize->remove_setting( 'mega_construction_reset_demo_import_settings' );
	$wp_customize->remove_control( 'mega_construction_reset_demo_import_settings' );

	//Reset Global Color
	$wp_customize->add_setting('modern_construction_reset_color', array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_text_field',
	));
	
	$wp_customize->add_control('modern_construction_reset_color', array(
		'type'    => 'button',
		'label'   => __('Reset Global Color', 'modern-construction'),
		'section' => 'mega_construction_reset_section',
		'input_attrs' => array(
			'onclick' => 'ResetChildGlobalColor()',
		),
	));	
} 
add_action( 'customize_register', 'my_customize_register', 11 );

function modern_construction_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'modern-construction' ),
		'description'   => __( 'Appears on blog page sidebar', 'modern-construction' ),
		'id'            => 'sidebar-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s p-2 mb-4">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title pb-2 mb-2 pt-0">',
		'after_title'   => '</h3>',
	) );
	
	register_sidebar( array(
		'name'          => __( 'Page Sidebar', 'modern-construction' ),
		'description'   => __( 'Appears on page sidebar', 'modern-construction' ),
		'id'            => 'sidebar-2',
		'before_widget' => '<aside id="%1$s" class="widget %2$s p-2 mb-4">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title pb-2 mb-2 pt-0">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Third Column Sidebar', 'modern-construction' ),
		'description'   => __( 'Appears on page sidebar', 'modern-construction' ),
		'id'            => 'sidebar-3',
		'before_widget' => '<aside id="%1$s" class="widget %2$s p-2 mb-4">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title pb-2 mb-2 pt-0">',
		'after_title'   => '</h3>',
	) );

	$modern_construction_footer_columns = get_theme_mod('modern_construction_footer_widget', '4');
	for ($i=1; $i<=$modern_construction_footer_columns; $i++) {
		register_sidebar( array(
			'name'          => __( 'Footer ', 'modern-construction' ) . $i,
			'id'            => 'footer-' . $i,
			'description'   => '',
			'before_widget' => '<aside id="%1$s" class="widget %2$s py-4">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );
	}
}
add_action( 'widgets_init', 'modern_construction_widgets_init' );

function modern_construction_mysetup() {
	/* Theme Credit link */
	define('MODERN_CONSTRUCTION_PRO_THEME_URL',__('https://www.themesglance.com/products/premium-construction-wordpress-theme','modern-construction'));
	define('MODERN_CONSTRUCTION_THEME_DOC',__('https://preview.themesglance.com/demo/docs/mega-construction-pro/','modern-construction'));
	define('MODERN_CONSTRUCTION_LIVE_DEMO',__('https://preview.themesglance.com/mega-construction-pro/','modern-construction'));
	define('MODERN_CONSTRUCTION_FREE_THEME_DOC',__('http://preview.themesglance.com/demo/docs/free-mega-construction/','modern-construction'));
	define('MODERN_CONSTRUCTION_SUPPORT',__('https://wordpress.org/support/theme/modern-construction/','modern-construction'));
	define('MODERN_CONSTRUCTION_REVIEW',__('https://wordpress.org/support/theme/modern-construction/reviews/','modern-construction'));
	define('MODERN_CONSTRUCTION_SITE_URL',__('https://www.themesglance.com/products/free-construction-wordpress-theme','modern-construction'));
	define('MODERN_CONSTRUCTION_BUNDLE_URL',__('https://www.themesglance.com/products/wp-theme-bundle','modern-construction'));

	if ( ! defined( 'MODERN_CONSTRUCTION_PRO_NAME' ) ) {
		define( 'MODERN_CONSTRUCTION_PRO_NAME', __( 'Modern Construction Pro', 'modern-construction' ));
	}
	
	if ( ! defined( 'MODERN_CONSTRUCTION_DOC_NAME' ) ) {
		define( 'MODERN_CONSTRUCTION_DOC_NAME', __( 'Modern Construction Doc', 'modern-construction' ));
	}
	
	if ( ! defined( 'MODERN_CONSTRUCTION_DEMO_NAME' ) ) {
		define( 'MODERN_CONSTRUCTION_DEMO_NAME', __( 'Modern Construction Demo', 'modern-construction' ));
	}
	/* Block Pattern */
	require get_theme_file_path('/block-patterns/block-patterns.php');
}
add_action( 'after_setup_theme', 'modern_construction_mysetup' );

/**
 * Enqueue block editor style
 */
function modern_construction_block_editor_styles() {
	wp_enqueue_style( 'modern-construction-font', mega_construction_font_url(), array() );
    wp_enqueue_style( 'modern-construction-block-patterns-style-editor', get_theme_file_uri( '/block-patterns/css/block-editor.css' ), false, '1.0', 'all' );
    wp_enqueue_style( 'font-awesome-css', get_template_directory_uri().'/css/fontawesome-all.css' );
}
add_action( 'enqueue_block_editor_assets', 'modern_construction_block_editor_styles' );

// Customizer Section
function modern_construction_customizer ( $wp_customize ) {

	$wp_customize->add_section( 'modern_construction_service_section' , array(
    	'title'      => __( 'Our Services', 'modern-construction' ),
		'priority'   => null,
		'panel' => 'mega_construction_panel_id'
	) );

	$wp_customize->add_setting('modern_construction_service_title',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('modern_construction_service_title',array(
		'label'	=> __('Section Title','modern-construction'),
		'section'=> 'modern_construction_service_section',
		'setting'=> 'modern_construction_service_title',
		'type'=> 'text'
	));	

	$wp_customize->add_setting('modern_construction_service_text',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('modern_construction_service_text',array(
		'label'	=> __('Add Text','modern-construction'),
		'section'=> 'modern_construction_service_section',
		'setting'=> 'modern_construction_service_text',
		'type'=> 'text'
	));	

	$wp_customize->add_setting( 'modern_construction_slider_button_label', array(
		'default' => __('READ MORE','modern-construction'),
		'sanitize_callback'	=> 'sanitize_text_field'
	) );
	$wp_customize->add_control( 'modern_construction_slider_button_label', array(
		'label' => esc_html__( 'Slider Button Label','modern-construction' ),
		'section' => 'mega_construction_slidersettings',
		'type'    => 'text',
		'settings'    => 'modern_construction_slider_button_label'
	) );

	$wp_customize->add_setting('modern_construction_slider_button_link',array(
        'default'=> '',
        'sanitize_callback' => 'esc_url_raw'
    ));
    $wp_customize->add_control('modern_construction_slider_button_link',array(
        'label' => esc_html__('Add Button Link','modern-construction'),
        'section'=> 'mega_construction_slidersettings',
        'type'=> 'url'
    ));

	$categories = get_categories();
	$cats = array();
	$i = 0;
	$cat_pst[]= 'select';
	foreach($categories as $category){
		if($i==0){
			$default = $category->slug;
			$i++;
		}
		$cat_pst[$category->slug] = $category->name;
	}

	$wp_customize->add_setting('modern_construction_category',array(
		'default' => 'select',
		'sanitize_callback' => 'mega_construction_sanitize_choices',
	));
	$wp_customize->add_control('modern_construction_category',array(
		'type'    => 'select',
		'choices' => $cat_pst,
		'label' => __('Select Category to display Latest Post','modern-construction'),
		'section' => 'modern_construction_service_section',
	));

	$args = array('numberposts' => -1);
	$post_list = get_posts($args);
	$i = 0;
	$pst[]='Select';	
	foreach($post_list as $post){
		$pst[$post->post_title] = $post->post_title;
	}

	$wp_customize->add_setting('modern_construction_post',array(
		'sanitize_callback' => 'mega_construction_sanitize_choices',
	));
	$wp_customize->add_control('modern_construction_post',array(
		'type'    => 'select',
		'choices' => $pst,
		'label' => __('Select post','modern-construction'),
		'section' => 'modern_construction_service_section',
	));

	$wp_customize->add_setting( 'modern_construction_first_theme_color', array(
	    'default' => '',
	    'sanitize_callback' => 'sanitize_hex_color'
  	));
  	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'modern_construction_first_theme_color', array(
  		'label' => 'Color Option',
	    'description' => __('One can change complete theme global color settings on just one click.', 'modern-construction'),
	    'section' => 'mega_construction_theme_color_option',
	    'settings' => 'modern_construction_first_theme_color',
  	)));

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

/* Implement the get started page */
require get_theme_file_path() . '/inc/getting-started/getting-started.php';
