<?php
/**
 * Hostiko functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage Hostiko
 * @since Hostiko 1.0
 */

require_once(__DIR__ . '/welcome.php');

// Adds theme support for post formats.
if ( ! function_exists( 'hostiko_block_post_format_setup' ) ) :
	/**
	 * Adds theme support for post formats.
	 *
	 * @since Hostiko 1.0
	 *
	 * @return void
	 */
	function hostiko_block_post_format_setup() {
		add_theme_support( 'post-formats', array( 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video' ) );
	}
endif;
add_action( 'after_setup_theme', 'hostiko_block_post_format_setup' );

// Enqueues editor-style.css in the editors.
if ( ! function_exists( 'hostiko_block_editor_style' ) ) :
	/**
	 * Enqueues editor-style.css in the editors.
	 *
	 * @since Hostiko 1.0
	 *
	 * @return void
	 */
	function hostiko_block_editor_style() {
		add_editor_style( 'assets/css/editor-style.css' );
	}
endif;
add_action( 'after_setup_theme', 'hostiko_block_editor_style' );

// Enqueues style.css on the front.
if ( ! function_exists( 'hostiko_block_enqueue_styles' ) ) :
	/**
	 * Enqueues style.css on the front.
	 *
	 * @since Hostiko 1.0
	 *
	 * @return void
	 */
	function hostiko_block_enqueue_styles() {
		wp_enqueue_style(
			'hostiko-block-style',
			get_parent_theme_file_uri( 'style.css' ),
			array(),
			wp_get_theme()->get( 'Version' )
		);
	}
endif;
add_action( 'wp_enqueue_scripts', 'hostiko_block_enqueue_styles' );

// Registers custom block styles.
if ( ! function_exists( 'hostiko_block_block_styles' ) ) :
	/**
	 * Registers custom block styles.
	 *
	 * @since Hostiko 1.0
	 *
	 * @return void
	 */
	function hostiko_block_block_styles() {
		register_block_style(
			'core/list',
			array(
				'name'         => 'checkmark-list',
				'label'        => __( 'Checkmark', 'hostiko' ),
				'inline_style' => '
				ul.is-style-checkmark-list {
					list-style-type: "\2713";
				}

				ul.is-style-checkmark-list li {
					padding-inline-start: 1ch;
				}',
			)
		);
	}
endif;
add_action( 'init', 'hostiko_block_block_styles' );

// Registers pattern categories.
if ( ! function_exists( 'hostiko_block_pattern_categories' ) ) :
	/**
	 * Registers pattern categories.
	 *
	 * @since Hostiko 1.0
	 *
	 * @return void
	 */
	function hostiko_block_pattern_categories() {

		register_block_pattern_category(
			'hostiko_block_page',
			array(
				'label'       => __( 'Pages', 'hostiko' ),
				'description' => __( 'A collection of full page layouts.', 'hostiko' ),
			)
		);

		register_block_pattern_category(
			'hostiko_block_post-format',
			array(
				'label'       => __( 'Post formats', 'hostiko' ),
				'description' => __( 'A collection of post format patterns.', 'hostiko' ),
			)
		);
	}
endif;
add_action( 'init', 'hostiko_block_pattern_categories' );

// Registers block binding sources.
if ( ! function_exists( 'hostiko_block_register_block_bindings' ) ) :
	/**
	 * Registers the post format block binding source.
	 *
	 * @since Hostiko 1.0
	 *
	 * @return void
	 */
	function hostiko_block_register_block_bindings() {
		register_block_bindings_source(
			'hostiko-block/format',
			array(
				'label'              => _x( 'Post format name', 'Label for the block binding placeholder in the editor', 'hostiko' ),
				'get_value_callback' => 'hostiko_block_format_binding',
			)
		);
	}
endif;
add_action( 'init', 'hostiko_block_register_block_bindings' );

// Registers block binding callback function for the post format name.
if ( ! function_exists( 'hostiko_block_format_binding' ) ) :
	/**
	 * Callback function for the post format name block binding source.
	 *
	 * @since Hostiko 1.0
	 *
	 * @return string|void Post format name, or nothing if the format is 'standard'.
	 */
	function hostiko_block_format_binding() {
		$post_format_slug = get_post_format();

		if ( $post_format_slug && 'standard' !== $post_format_slug ) {
			return get_post_format_string( $post_format_slug );
		}
	}
endif;

// Add Font Awesome for frontend and editor
function hostiko_block_enqueue_fontawesome() {
    // Load Font Awesome on frontend
    wp_enqueue_style(
        'font-awesome', get_template_directory_uri() . '/assets/css/all.min.css', array(), filemtime(get_template_directory() . '/assets/css/all.min.css')
    );
}
add_action('wp_enqueue_scripts', 'hostiko_block_enqueue_fontawesome');

// Load Font Awesome inside block editor
function hostiko_block_enqueue_fontawesome_editor() {
    wp_enqueue_style(
        'font-awesome-editor',
        get_template_directory_uri() . '/assets/css/all.min.css',
        array(),
        filemtime(get_template_directory() . '/assets/css/all.min.css'),
        'all'
    );
}
add_action('enqueue_block_assets', 'hostiko_block_enqueue_fontawesome_editor');


// Load custom CSS in WordPress Admin (not frontend and not limited to editor only)
function hostiko_block_admin_custom_styles() {
    wp_enqueue_style(
        'hostiko-admin-style',
        get_template_directory_uri() . '/assets/css/custom-style.css',
        array(),
        wp_get_theme()->get( 'Version' ),
        'all'
    );
}
add_action( 'admin_enqueue_scripts', 'hostiko_block_admin_custom_styles' );

/**
 * Show only blog posts in search results
 */
function hostiko_block_search_only_posts( $query ) {
    if ( $query->is_search() && $query->is_main_query() && !is_admin() ) {
        $query->set( 'post_type', 'post' );  // Only show Posts
        $query->set( 'post_status', 'publish' ); // Hide drafts/private
    }
}
add_action( 'pre_get_posts', 'hostiko_block_search_only_posts' );


// Enqueue layout-specific CSS on frontend and in block editor
add_action( 'wp_enqueue_scripts', 'hostiko_enqueue_layout_css' );
add_action( 'enqueue_block_editor_assets', 'hostiko_enqueue_layout_css' );

function hostiko_enqueue_layout_css() {
    $theme_version = wp_get_theme()->get( 'Version' );

    // 🔹 Change this line depending on which layout you’re building
    $default_layout = isset($_SESSION['imported_layout']) ? 'layout-'.$_SESSION['imported_layout'].'' : 'layout-91';

    // If a layout is stored in options (from demo import), use it
    $layout = get_option( 'hostiko_active_layout', $default_layout );

    $layout_file = get_template_directory() . '/assets/css/' . $layout . '.css';
    $layout_url  = get_template_directory_uri() . '/assets/css/' . $layout . '.css';

    if ( file_exists( $layout_file ) ) {
        wp_enqueue_style(
            'hostiko-' . esc_attr( $layout ),
            $layout_url,
            array(),
            $theme_version
        );
    }
}

