<?php
/**
 * Vir Za functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage vir_za
 * @since Vir Za 1.0
 */
// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}
// Adds theme support for post formats.
if ( ! function_exists( 'virza_post_format_setup' ) ) :
	/**
	 * Adds theme support for post formats.
	 *
	 * @since Vir Za 1.0
	 *
	 * @return void
	 */
	function virza_post_format_setup() {
		add_theme_support( 'post-formats', array( 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video' ) );
	}
endif;
add_action( 'after_setup_theme', 'virza_post_format_setup' );

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

/**
 * Enqueue theme styles.
 */
function virza_master_enqueue_styles() {
    wp_enqueue_style(
        'virza-main-style',
        get_stylesheet_uri(),
        array(),
        filemtime(get_stylesheet_directory() . '/style.css')
    );

    $css_path = get_theme_file_path('assets/css/virza.css');
    if (file_exists($css_path)) {
        wp_enqueue_style(
            'virza-style',
            get_theme_file_uri('assets/css/virza.css'),
            array(),
            filemtime($css_path)
        );
    }
    $css_path = get_theme_file_path('assets/css/extra.css');
    if (file_exists($css_path)) {
        wp_enqueue_style(
            'extra-style',
            get_theme_file_uri('assets/css/extra.css'),
            array(),
            filemtime($css_path)
        );
    }
    
    $js_path = get_theme_file_path('assets/js/index.js');
    if (file_exists($js_path)){
        wp_enqueue_script( 'virza-theme-js', get_theme_file_uri('assets/js/index.js'), array(), filemtime($js_path), true );
    }
}
add_action('wp_enqueue_scripts', 'virza_master_enqueue_styles');


// Registers custom block styles.
if ( ! function_exists( 'virza_block_styles' ) ) :
	/**
	 * Registers custom block styles.
	 *
	 * @since Vir Za 1.0
	 *
	 * @return void
	 */
	function virza_block_styles() {
		register_block_style(
			'core/list',
			array(
				'name'         => 'checkmark-list',
				'label'        => __( 'Checkmark', 'virza' ),
				'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', 'virza_block_styles' );

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

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

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

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

// Registers block binding callback function for the post format name.
if ( ! function_exists( 'virza_format_binding' ) ) :
	/**
	 * Callback function for the post format name block binding source.
	 *
	 * @since Vir Za 1.0
	 *
	 * @return string|void Post format name, or nothing if the format is 'standard'.
	 */
	function virza_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;
