<?php
/**
 * Theme sprecific functions and definitions
 */


/* Theme setup section
------------------------------------------------------------------- */

// Set the content width based on the theme's design and stylesheet.
if ( ! isset( $content_width ) ) $content_width = 1170; /* pixels */

// Add theme specific actions and filters
// Attention! Function were add theme specific actions and filters handlers must have priority 1
if ( !function_exists( 'writer_ancora_theme_setup' ) ) {
	add_action( 'writer_ancora_action_before_init_theme', 'writer_ancora_theme_setup', 1 );
	function writer_ancora_theme_setup() {

        // Add default posts and comments RSS feed links to head
        add_theme_support( 'automatic-feed-links' );

        // Enable support for Post Thumbnails
        add_theme_support( 'post-thumbnails' );

        // Custom header setup
        add_theme_support( 'custom-header', array('header-text'=>false));

        // Custom backgrounds setup
        add_theme_support( 'custom-background');

        // Supported posts formats
        add_theme_support( 'post-formats', array('gallery', 'video', 'audio', 'link', 'quote', 'image', 'status', 'aside', 'chat') );

        // Autogenerate title tag
        add_theme_support('title-tag');

        // Add user menu
        add_theme_support('nav-menus');

        // WooCommerce Support
        add_theme_support( 'woocommerce' );

		// Register theme menus
		add_filter( 'writer_ancora_filter_add_theme_menus',		'writer_ancora_add_theme_menus' );

		// Register theme sidebars
		add_filter( 'writer_ancora_filter_add_theme_sidebars',	'writer_ancora_add_theme_sidebars' );

		// Set options for importer
		add_filter( 'writer_ancora_filter_importer_options',		'writer_ancora_set_importer_options' );

		// Add theme specified classes into the body
		add_filter( 'body_class', 'writer_ancora_body_classes' );

		// Set list of the theme required plugins
		writer_ancora_storage_set('required_plugins', array(
			'essgrids',
			'trx_socials',
			'revslider',
			'trx_utils',
			'visual_composer',
			'woocommerce',
			'contact-form-7',
			'mailchimp',
			'elegro-payment',
			'trx_updater',
			'gdpr-compliance'
			)
		);

		// Set list of the theme required custom fonts from folder /css/font-faces
        // Attention! Font's folder must have name equal to the font's name
        writer_ancora_storage_set('required_custom_fonts', array(
                'Amadeus'
            )
        );

        // Gutenberg support
        add_theme_support( 'align-wide' );

        writer_ancora_storage_set('demo_data_url',  esc_url(writer_ancora_get_protocol() . '://writer.ancorathemes.com/demo/'));
	}
}


// Add/Remove theme nav menus
if ( !function_exists( 'writer_ancora_add_theme_menus' ) ) {
	function writer_ancora_add_theme_menus($menus) {
		return $menus;
	}
}


// Add theme specific widgetized areas
if ( !function_exists( 'writer_ancora_add_theme_sidebars' ) ) {
	function writer_ancora_add_theme_sidebars($sidebars=array()) {
		if (is_array($sidebars)) {
			$theme_sidebars = array(
				'sidebar_main'		=> esc_html__( 'Main Sidebar', 'writer-ancora' ),
				'sidebar_footer'	=> esc_html__( 'Footer Sidebar', 'writer-ancora' )
			);
			if (function_exists('writer_ancora_exists_woocommerce') && writer_ancora_exists_woocommerce()) {
				$theme_sidebars['sidebar_cart']  = esc_html__( 'WooCommerce Cart Sidebar', 'writer-ancora' );
			}
			$sidebars = array_merge($theme_sidebars, $sidebars);
		}
		return $sidebars;
	}
}


// Add theme specified classes into the body
if ( !function_exists('writer_ancora_body_classes') ) {
	function writer_ancora_body_classes( $classes ) {

		$classes[] = 'writer_ancora_body';
		$classes[] = 'body_style_' . trim(writer_ancora_get_custom_option('body_style'));
		$classes[] = 'body_' . (writer_ancora_get_custom_option('body_filled')=='yes' ? 'filled' : 'transparent');
		$classes[] = 'theme_skin_' . trim(writer_ancora_get_custom_option('theme_skin'));
		$classes[] = 'article_style_' . trim(writer_ancora_get_custom_option('article_style'));
		
		$blog_style = writer_ancora_get_custom_option(is_singular() && !writer_ancora_storage_get('blog_streampage') ? 'single_style' : 'blog_style');
		$classes[] = 'layout_' . trim($blog_style);
		$classes[] = 'template_' . trim(writer_ancora_get_template_name($blog_style));
		
		$body_scheme = writer_ancora_get_custom_option('body_scheme');
		if (empty($body_scheme)  || writer_ancora_is_inherit_option($body_scheme)) $body_scheme = 'original';
		$classes[] = 'scheme_' . $body_scheme;

		$top_panel_position = writer_ancora_get_custom_option('top_panel_position');
		if (!writer_ancora_param_is_off($top_panel_position)) {
			$classes[] = 'top_panel_show';
			$classes[] = 'top_panel_' . trim($top_panel_position);
		} else 
			$classes[] = 'top_panel_hide';
		$classes[] = writer_ancora_get_sidebar_class();

		if (writer_ancora_get_custom_option('show_video_bg')=='yes' && (writer_ancora_get_custom_option('video_bg_youtube_code')!='' || writer_ancora_get_custom_option('video_bg_url')!=''))
			$classes[] = 'video_bg_show';

		if (writer_ancora_get_theme_option('page_preloader')!='')
			$classes[] = 'preloader';

		return $classes;

	}
}


// Add theme specified classes into the body
if ( !function_exists('writer_ancora_body_wrap_classes') ) {
	function writer_ancora_body_wrap_classes() {
		$body_style  = writer_ancora_get_custom_option('body_style');
		$class = $style = '';
		if ($body_style=='boxed' || writer_ancora_get_custom_option('bg_image_load')=='always') {
			if (($img = (int) writer_ancora_get_custom_option('bg_image', 0)) > 0)
				$class = 'bg_image_'.($img);
			else if (($img = (int) writer_ancora_get_custom_option('bg_pattern', 0)) > 0)
				$class = 'bg_pattern_'.($img);
			else if (($img = writer_ancora_get_custom_option('bg_color', '')) != '')
				$style = 'background-color: '.($img).';';
			else if (writer_ancora_get_custom_option('bg_custom')=='yes') {
				if (($img = writer_ancora_get_custom_option('bg_image_custom')) != '')
					$style = 'background: url('.esc_url($img).') ' . str_replace('_', ' ', writer_ancora_get_custom_option('bg_image_custom_position')) . ' no-repeat fixed;';
				else if (($img = writer_ancora_get_custom_option('bg_pattern_custom')) != '')
					$style = 'background: url('.esc_url($img).') 0 0 repeat fixed;';
				else if (($img = writer_ancora_get_custom_option('bg_image')) > 0)
					$class = 'bg_image_'.($img);
				else if (($img = writer_ancora_get_custom_option('bg_pattern')) > 0)
					$class = 'bg_pattern_'.($img);
				if (($img = writer_ancora_get_custom_option('bg_color')) != '')
					$style .= 'background-color: '.($img).';';
			}
		}

		$body_classes = array('class' => $class, 'style' => $style );

		return $body_classes;

	}
}


// Add TOC items 'Home' and "To top"
if ( !function_exists('writer_ancora_toc_home') ) {
	function writer_ancora_toc_home( ) {
		if (writer_ancora_get_custom_option('menu_toc_home')=='yes' && function_exists('writer_ancora_sc_anchor'))
				writer_ancora_show_layout(writer_ancora_sc_anchor(array(
					'id' => "toc_home",
					'title' => esc_html__('Home', 'writer-ancora'),
					'description' => esc_html__('{{Return to Home}} - ||navigate to home page of the site', 'writer-ancora'),
					'icon' => "icon-home",
					'separator' => "yes",
					'url' => esc_url(home_url('/'))
					)
				)); 
		if (writer_ancora_get_custom_option('menu_toc_top')=='yes' && function_exists('writer_ancora_sc_anchor'))
			writer_ancora_show_layout(writer_ancora_sc_anchor(array(
				'id' => "toc_top",
				'title' => esc_html__('To Top', 'writer-ancora'),
				'description' => esc_html__('{{Back to top}} - ||scroll to top of the page', 'writer-ancora'),
				'icon' => "icon-double-up",
				'separator' => "yes")
				)); 
	}
}

// Add page meta to the head
if (!function_exists('writer_ancora_head_add_page_meta')) {
    add_action('wp_head', 'writer_ancora_head_add_page_meta', 1);
    function writer_ancora_head_add_page_meta() {
        ?>
        <meta charset="<?php bloginfo( 'charset' ); ?>" />
        <meta name="viewport" content="width=device-width, initial-scale=1<?php if (writer_ancora_get_theme_option('responsive_layouts')=='yes') echo ', maximum-scale=1'; ?>">
        <meta name="format-detection" content="telephone=no">

        <link rel="profile" href="//gmpg.org/xfn/11" />
        <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
        <?php
    }
}

// Return text for the Privacy Policy checkbox
if ( ! function_exists('writer_ancora_get_privacy_text' ) ) {
    function writer_ancora_get_privacy_text() {
        $page = get_option( 'wp_page_for_privacy_policy' );
        $privacy_text = writer_ancora_get_theme_option( 'privacy_text' );
        return apply_filters( 'writer_ancora_filter_privacy_text', wp_kses_post(
                $privacy_text
                . ( ! empty( $page ) && ! empty( $privacy_text )
                    // Translators: Add url to the Privacy Policy page
                    ? ' ' . sprintf( __( 'For further details on handling user data, see our %s', 'writer-ancora' ),
                        '<a href="' . esc_url( get_permalink( $page ) ) . '" target="_blank">'
                        . __( 'Privacy Policy', 'writer-ancora' )
                        . '</a>' )
                    : ''
                )
            )
        );
    }
}

// Return text for the "I agree ..." checkbox
if ( ! function_exists( 'writer_ancora_trx_utils_privacy_text' ) ) {
    add_filter( 'trx_utils_filter_privacy_text', 'writer_ancora_trx_utils_privacy_text' );
    function writer_ancora_trx_utils_privacy_text( $text='' ) {
        return writer_ancora_get_privacy_text();
    }
}

// Add theme required plugins
if ( !function_exists( 'writer_ancora_add_trx_utils' ) ) {
    add_filter( 'trx_utils_active', 'writer_ancora_add_trx_utils' );
    function writer_ancora_add_trx_utils($enable=true) {
        return true;
    }
}

/**
 * Fire the wp_body_open action.
 *
 * Added for backwards compatibility to support pre 5.2.0 WordPress versions.
 */
if ( ! function_exists( 'wp_body_open' ) ) {
    function wp_body_open() {
        /**
         * Triggered after the opening <body> tag.
         */
        do_action('wp_body_open');
    }
}


//------------------------------------------------------------------------
// One-click import support
//------------------------------------------------------------------------

// Set theme specific importer options
if ( ! function_exists( 'writer_ancora_importer_set_options' ) ) {
    add_filter( 'trx_utils_filter_importer_options', 'writer_ancora_importer_set_options', 9 );
    function writer_ancora_importer_set_options( $options=array() ) {
        if ( is_array( $options ) ) {
            // Save or not installer's messages to the log-file
            $options['debug'] = false;
            // Prepare demo data
            if ( is_dir( WRITER_ANCORA_THEME_PATH . 'demo/' ) ) {
                $options['demo_url'] = WRITER_ANCORA_THEME_PATH . 'demo/';
            } else {
                $options['demo_url'] = esc_url( writer_ancora_get_protocol().'://demofiles.ancorathemes.com/writer-ancora/' ); // Demo-site domain
            }

            // Required plugins
            $options['required_plugins'] =  array(
                'essential-grid',
                'revslider',
                'js_composer',
                'mailchimp-for-wp',
                'woocommerce'
            );

            $options['theme_slug'] = 'writer_ancora';

            // Set number of thumbnails to regenerate when its imported (if demo data was zipped without cropped images)
            // Set 0 to prevent regenerate thumbnails (if demo data archive is already contain cropped images)
            $options['regenerate_thumbnails'] = 3;
            // Default demo
            $options['files']['default']['title'] = esc_html__( 'Writer Ancora Demo', 'writer-ancora' );
            $options['files']['default']['domain_dev'] = esc_url(writer_ancora_get_protocol().'://writer.upd.ancorathemes.com'); // Developers domain
            $options['files']['default']['domain_demo']= esc_url(writer_ancora_get_protocol().'://writer.ancorathemes.com'); // Demo-site domain

        }
        return $options;
    }
}


/* Include framework core files
------------------------------------------------------------------- */
// If now is WP Heartbeat call - skip loading theme core files
if (!isset($_POST['action']) || $_POST['action']!="heartbeat") {
	require_once get_template_directory().'/fw/loader.php';
}
?>