<?php
/**
 * Theme Functions
 *
 * @package Language_Tutor
 * @version GIT: 1.0.3
 *
 * @phpcs:disable
 * (c) 2024 Language Tutor. All rights reserved.
 */

// Theme Setup
if (!function_exists('Language_Tutor_setup')) {
    /**
     * Theme setup
     *
     * @return void
     */
    function Language_Tutor_setup()
    {
        // Add theme support
        add_theme_support('custom-logo');
        add_theme_support('post-thumbnails');

        // Load text domain for translation
        load_theme_textdomain('language-tutor', get_template_directory() . '/languages');
    }
}
add_action('after_setup_theme', 'Language_Tutor_setup');

// Enqueue Styles
if (!function_exists('Language_Tutor_Enqueue_style')) {
    /**
     * Enqueue style.
     *
     * @return void
     */
    function Language_Tutor_Enqueue_style()
    {
        // Enqueue main stylesheet
        wp_enqueue_style('language-tutor-style', get_stylesheet_uri(), array(), filemtime(get_stylesheet_directory() . '/style.css'));
    }
}
add_action('wp_enqueue_scripts', 'Language_Tutor_Enqueue_style');

/**
 * Load Dashicons
 *
 * @return void
 */
function Language_Tutor_Load_Dashicons(){
     wp_enqueue_style('dashicons');
 }
add_action('wp_enqueue_scripts', 'Language_Tutor_Load_Dashicons');

// Block Styles and Patterns
/**
 * Register custom block styles
 *
 * @return void
 */
function language_tutor_register_block_styles() {
    register_block_style(
        'core/button',
        array(
            'name'  => 'language-tutor-button',
            'label' => __( 'Language Tutor Button', 'language-tutor' ),
        )
    );
}
add_action( 'init', 'language_tutor_register_block_styles' );

/**
 * Register custom block patterns
 *
 * @return void
 */
function language_tutor_register_block_patterns() {
    register_block_pattern(
        'language-tutor/custom-pattern',
        array(
            'title'       => __( 'Language Tutor Custom Pattern', 'language-tutor' ),
            'description' => _x( 'A custom pattern for Language Tutor theme', 'Block pattern description', 'language-tutor' ),
            'content'     => '<!-- wp:group --><div class="wp-block-group"><div class="wp-block-group__inner-container"><!-- wp:paragraph --><p>' . esc_html__( 'This is a custom block pattern for Language Tutor theme.', 'language-tutor' ) . '</p><!-- /wp:paragraph --></div></div><!-- /wp:group -->',
        )
    );
}
add_action( 'init', 'language_tutor_register_block_patterns' );

/**
 * Register legacy pattern slug aliases for backward compatibility.
 *
 * This keeps previously saved templates (using older slugs) working after
 * pattern slug renames.
 *
 * @return void
 */
function language_tutor_register_legacy_pattern_aliases() {
    if ( ! class_exists( 'WP_Block_Patterns_Registry' ) ) {
        return;
    }

    $aliases = array(
        'language-tutor/header-section' => 'language-tutor/site-header',
        'language-tutor/footer-section' => 'language-tutor/site-footer',
        'language-tutor/hero-section'   => 'language-tutor/hero-banner',
        'language-tutor/first-section'  => 'language-tutor/learning-benefits',
        'language-tutor/second-section' => 'language-tutor/teaching-approach',
        'language-tutor/third-section'  => 'language-tutor/how-it-works',
        'language-tutor/fourth-section' => 'language-tutor/learning-stats',
        'language-tutor/fifth-section'  => 'language-tutor/student-testimonial',
        'language-tutor/sixth-section'  => 'language-tutor/final-cta',
        'language-tutor/blog-section'   => 'language-tutor/blog-post-grid',
    );

    $registry = WP_Block_Patterns_Registry::get_instance();

    foreach ( $aliases as $legacy_slug => $current_slug ) {
        if ( $registry->is_registered( $legacy_slug ) || ! $registry->is_registered( $current_slug ) ) {
            continue;
        }

        $pattern = $registry->get_registered( $current_slug );
        if ( empty( $pattern['content'] ) ) {
            continue;
        }

        $legacy_pattern = array(
            'title'   => $pattern['title'],
            'content' => $pattern['content'],
        );

        if ( ! empty( $pattern['description'] ) ) {
            $legacy_pattern['description'] = $pattern['description'];
        }
        if ( ! empty( $pattern['categories'] ) ) {
            $legacy_pattern['categories'] = $pattern['categories'];
        }
        if ( ! empty( $pattern['keywords'] ) ) {
            $legacy_pattern['keywords'] = $pattern['keywords'];
        }
        if ( isset( $pattern['inserter'] ) ) {
            $legacy_pattern['inserter'] = (bool) $pattern['inserter'];
        }
        if ( ! empty( $pattern['blockTypes'] ) ) {
            $legacy_pattern['blockTypes'] = $pattern['blockTypes'];
        }
        if ( ! empty( $pattern['postTypes'] ) ) {
            $legacy_pattern['postTypes'] = $pattern['postTypes'];
        }
        if ( ! empty( $pattern['templateTypes'] ) ) {
            $legacy_pattern['templateTypes'] = $pattern['templateTypes'];
        }
        if ( ! empty( $pattern['viewportWidth'] ) ) {
            $legacy_pattern['viewportWidth'] = $pattern['viewportWidth'];
        }

        register_block_pattern( $legacy_slug, $legacy_pattern );
    }
}
add_action( 'init', 'language_tutor_register_legacy_pattern_aliases', 20 );
