<?php
/**
 * Business Office functions and definitions
 *
 * @link https://developer.WordPress.org/themes/basics/theme-functions/
 *
 * @package business_office
 */

if ( ! function_exists( 'business_office_setup' ) ) :
	function business_office_setup() {
		add_theme_support( 'automatic-feed-links' );

		load_theme_textdomain( 'business-office', get_template_directory() . '/languages' );

		add_theme_support( 'title-tag' );

		add_theme_support( 'post-thumbnails' );

		add_theme_support( 'woocommerce' );

		register_nav_menus( array(
			'primary_menu' 		=> esc_html__( 'Primary Menu', 'business-office' ),
		) );

		add_theme_support( 'html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		) );

		// Set up the WordPress core custom background feature.
		add_theme_support( 'custom-background', array(
			'default-color' => 'ffffff'
		) );

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

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.WordPress.org/Theme_Logo
		 */
		add_theme_support( 'custom-logo', array(
			'height'      => 250,
			'width'       => 250,
			'flex-width'  => true,
			'flex-height' => true,
		) );

		add_editor_style( array( '/assets/css/editor-style.css', business_office_get_fonts_url() ) );

		// Gutenberg support
		add_theme_support( 'editor-color-palette', array(
	       	array(
				'name' => esc_html__( 'Blue', 'business-office' ),
				'slug' => 'blue',
				'color' => '#2c7dfa',
	       	),
	       	array(
	           	'name' => esc_html__( 'Green', 'business-office' ),
	           	'slug' => 'green',
	           	'color' => '#07d79c',
	       	),
	       	array(
	           	'name' => esc_html__( 'Orange', 'business-office' ),
	           	'slug' => 'orange',
	           	'color' => '#ff8737',
	       	),
	       	array(
	           	'name' => esc_html__( 'Black', 'business-office' ),
	           	'slug' => 'black',
	           	'color' => '#2f3633',
	       	),
	       	array(
	           	'name' => esc_html__( 'Grey', 'business-office' ),
	           	'slug' => 'grey',
	           	'color' => '#82868b',
	       	),
	   	));

		add_theme_support( 'align-wide' );
		add_theme_support( 'editor-font-sizes', array(
		   	array(
		       	'name' => esc_html__( 'small', 'business-office' ),
		       	'shortName' => esc_html__( 'S', 'business-office' ),
		       	'size' => 12,
		       	'slug' => 'small'
		   	),
		   	array(
		       	'name' => esc_html__( 'regular', 'business-office' ),
		       	'shortName' => esc_html__( 'M', 'business-office' ),
		       	'size' => 16,
		       	'slug' => 'regular'
		   	),
		   	array(
		       	'name' => esc_html__( 'larger', 'business-office' ),
		       	'shortName' => esc_html__( 'L', 'business-office' ),
		       	'size' => 36,
		       	'slug' => 'larger'
		   	),
		   	array(
		       	'name' => esc_html__( 'huge', 'business-office' ),
		       	'shortName' => esc_html__( 'XL', 'business-office' ),
		       	'size' => 48,
		       	'slug' => 'huge'
		   	)
		));
		add_theme_support('editor-styles');
		add_theme_support( 'wp-block-styles' );

		/**
		 * Demo Import
		 */
		require get_template_directory() . '/apex-library/dashboard/dashboard-settings.php';
	}
endif;
add_action( 'after_setup_theme', 'business_office_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 business_office_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'business_office_content_width', 790 );
}
add_action( 'after_setup_theme', 'business_office_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.WordPress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function business_office_widgets_init() {
	register_sidebar( 
		array(
			'name'          => esc_html__( 'Sidebar', 'business-office' ),
			'id'            => 'sidebar-1',
			'description'   => esc_html__( 'Add widgets here.', 'business-office' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) 
	);

	register_sidebar(
		array(
			'name'          => __( 'Footer 1', 'business-office' ),
			'id'            => 'sidebar-2',
			'description'   => __( 'Add widgets here to appear in your footer.', 'business-office' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Footer 2', 'business-office' ),
			'id'            => 'sidebar-3',
			'description'   => __( 'Add widgets here to appear in your footer.', 'business-office' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Footer 3', 'business-office' ),
			'id'            => 'sidebar-4',
			'description'   => __( 'Add widgets here to appear in your footer.', 'business-office' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Footer 4', 'business-office' ),
			'id'            => 'sidebar-5',
			'description'   => __( 'Add widgets here to appear in your footer.', 'business-office' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);
}
add_action( 'widgets_init', 'business_office_widgets_init' );

/**
* Enqueue theme fonts.
*/
function business_office_fonts() {
	$fonts_url = business_office_get_fonts_url();

	// Load Fonts if necessary.
	if ( $fonts_url ) {
		require_once get_theme_file_path( 'apex-library/wptt-webfont-loader.php' );
		wp_enqueue_style( 'business-office-fonts', business_office_wptt_get_webfont_url( $fonts_url ), array(), null );
	}
}
add_action( 'wp_enqueue_scripts', 'business_office_fonts', 1 );
add_action( 'enqueue_block_editor_assets', 'business_office_fonts', 1 );

/**
 * Retrieve webfont URL to load fonts locally.
 */
function business_office_get_fonts_url() {
	$font_families = array(
		'Outfit:wght@100..900',
	);

	$query_args = array(
		'family'  => urlencode( implode( '|', $font_families ) ),
		'subset'  => urlencode( 'latin,latin-ext' ),
		'display' => urlencode( 'swap' ),
	);

	return apply_filters( 'business_office_get_fonts_url', add_query_arg( $query_args, 'https://fonts.googleapis.com/css2' ) );
}

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

	wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/assets/css/bootstrap.css' );

	wp_enqueue_style( 'bootstrap-icon', get_template_directory_uri() . '/assets/css/bootstrap-icons.css' );

	wp_enqueue_style( 'business-office-navigation-css', get_template_directory_uri() . '/assets/css/navigation.css' );

	wp_enqueue_style( 'business-office-blocks', get_template_directory_uri() . '/assets/css/blocks.css' );
	
	wp_enqueue_style( 'font-awesome-css', get_template_directory_uri().'/assets/css/fontawesome-all.css' );

	wp_enqueue_style( 'business-office-style', get_stylesheet_uri() );
	wp_style_add_data('business-office-style', 'rtl', 'replace');

	// Add custom inline styles safely
    $business_office_custom_style_path = get_parent_theme_file_path('/apex-library/custom-style.php');
    if (file_exists($business_office_custom_style_path)) {
        require $business_office_custom_style_path;
        if (!empty($business_office_custom_css)) {
            wp_add_inline_style('business-office-style', $business_office_custom_css);
        }
    }

	wp_enqueue_script( 'business-office-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );

	wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/assets/js/bootstrap.js', array('jquery'), '1.0', true );

	wp_enqueue_script( 'business-office-navigation-js', get_template_directory_uri() . '/assets/js/navigation.js', array('jquery'), '1.0', true );
	
	wp_enqueue_script( 'business-office-custom-script', get_template_directory_uri() . '/assets/js/theme-custom-script.js', array('jquery'), '20151215', true );

	// Localize script for AJAX
	wp_localize_script( 'business-office-custom-script', 'business_office_ajax_object', array(
		'ajax_url' => admin_url( 'admin-ajax.php' ),
		'nonce'    => wp_create_nonce( 'business_office_filter_nonce' )
	) );

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

/**
 * Enqueue editor styles for Gutenberg
 *
 * @since Business Office 1.0.0
 */
function business_office_block_editor_styles() {
	// Block styles.
	wp_enqueue_style( 'business-office-block-editor-style', get_theme_file_uri( '/assets/css/editor-blocks.css' ) );
	// Add custom fonts.
	wp_enqueue_style( 'business-office-fonts', business_office_get_fonts_url(), array(), null );
}
add_action( 'enqueue_block_editor_assets', 'business_office_block_editor_styles' );

/**
 * Checkbox sanitization callback example.
 */
function business_office_sanitize_checkbox( $checked ) {
	// Boolean check.
	return ( ( isset( $checked ) && true == $checked ) ? true : false );
}

/**
 * Removing category text from category page.
 */
function business_office_category_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    }
    return $title;
}
add_filter( 'get_the_archive_title', 'business_office_category_title' );

/*radio button sanitization*/
function business_office_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;
    }
}

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/apex-library/custom-header.php';

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

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

/**
 * Customizer additions.
 */
require get_template_directory() . '/apex-library/customizer.php';

/**
 * SVG icons functions and filters.
 */
require get_template_directory() . '/apex-library/icon-functions.php';

/**
 * Get Started Page.
 */
require get_template_directory() . '/apex-library/theme-info.php';

// Add admin notice
function business_office_admin_notice() { 
    global $pagenow;

    $business_office_theme = wp_get_theme();
    $business_office_meta  = get_option('business_office_admin_notice');
    $business_office_screen                  = get_current_screen();

    if ( !$business_office_meta ) {

        if ( is_network_admin() || ! current_user_can('manage_options') ) {
            return;
        }

        if ( $business_office_screen->base !== 'appearance_page_business-office-info' ) {

            $business_office_version = $business_office_theme->get('Version');
            ?>

            <div class="notice business-office-notice-main-div">

                <!-- Dismiss -->
				<a class="business-office-dismiss" href="<?php echo esc_url( add_query_arg( 'business_office_admin_notice', '1' ) ); ?>">
					<span class="dashicons dashicons-no-alt"></span>
				</a>

                <!-- Header -->
                <div class="business-office-notice">
                    <div class="business-office-notice-title">
                        <h1><?php echo esc_html( $business_office_theme->get('Name') ); ?></h1>
                        <span>
                            <?php esc_html_e('Version', 'business-office'); ?>
                            <?php echo esc_html($business_office_version); ?>
                        </span>
                    </div>

                    <div class="business-office-notice-buttons">
                        <a href="<?php echo esc_url( admin_url( 'themes.php?page=business-office-info' ) ); ?>" target="_blank" class="button">
                            <?php esc_html_e('Get Started', 'business-office'); ?>
                        </a>

                        <a href="<?php echo esc_url('https://www.apexthemes.net/products/business-firm-wordpress-theme/'); ?>" target="_blank" class="button button-primary">
                            <?php esc_html_e('Get Premium', 'business-office'); ?>
                        </a>

                        <a href="<?php echo esc_url('https://trial.apexthemes.net/business-office/'); ?>" target="_blank" class="button">
                            <?php esc_html_e('Live Demo', 'business-office'); ?>
                        </a>
                    </div>

					<div class="business-office-notice-logo">
						<img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/assets/images/apex-logo.png' ); ?>" alt="<?php esc_attr_e( 'Theme Screenshot', 'business-office' ); ?>">
                    </div>
                </div>

            </div>

            <?php
        }
    }
}
add_action('admin_notices', 'business_office_admin_notice');

if( ! function_exists( 'business_office_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
*/
function business_office_update_admin_notice(){
    if ( isset( $_GET['business_office_admin_notice'] ) && $_GET['business_office_admin_notice'] = '1' ) {
        update_option( 'business_office_admin_notice', true );
    }
}
endif;
add_action( 'admin_init', 'business_office_update_admin_notice' );


add_action('after_switch_theme', 'business_office_setup_options');
function business_office_setup_options () {
    update_option('business_office_admin_notice', FALSE );
}

function business_office_notice_styles() {
	wp_enqueue_style(
		'business-office-notice-function',
		get_stylesheet_directory_uri() . '/assets/css/notice-function.css',
		array(),
		'1.0.0'
	);
}
add_action( 'admin_enqueue_scripts', 'business_office_notice_styles' );