<?php
/**
 * License: GNU General Public License v2 or later
 * Description: Core functionality file for WordPress themes by Themes Machine. This file contains theme setup functions, enqueue scripts/styles, admin notices, and custom logic.
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 */


function markegol_add_welcome_page() {
    add_theme_page(
        __('Welcome to Theme', 'markegol'),
        __('Welcome to Theme', 'markegol'),
        'edit_theme_options',
        'theme-welcome',
        'markegol_render_welcome_page'
    );
}
add_action('admin_menu', 'markegol_add_welcome_page');

function markegol_render_welcome_page() {
    include get_template_directory() . '/theme-welcome/welcome-theme.php';
}


function markegol_admin_notice() {
    if (!is_admin()) {
        return;
    }

    global $pagenow;

    if ($pagenow === 'themes.php' && isset($_GET['activated'])) {
        $theme = wp_get_theme();
        $theme_name = strtoupper($theme->get('Name'));

        $theme_url = esc_url('https://themesmachine.com/index.php/product/markegol/');
        $welcome_url = esc_url(admin_url('themes.php?page=theme-welcome'));

        echo '<div class="notice notice-success is-dismissible">
            <p><strong>Thanks for activating <strong>' . esc_html($theme_name) . '</strong>!</strong></p>
            <p>' . esc_html__('Check out the Welcome Page to see the theme basic functionalities.', 'markegol') . '</p>
            <p>
                 <a href="' . $welcome_url . '" class="button button-primary">' . esc_html__('Welcome Page', 'markegol') . '</a>
                <a href="' . $theme_url. '" class="button" target="_blank" rel="noopener">' . esc_html__('Theme Page', 'markegol') . '</a>
            </p>
        </div>';
    }
}
add_action('admin_notices', 'markegol_admin_notice');


function markegol_theme_enqueue_styles()
{
    wp_enqueue_style(
        'markegol-theme-css',
        get_template_directory_uri() . '/source/css/theme.css',
        array(),
        wp_get_theme()->get('Version'),
        'all'
    );
}
add_action('wp_enqueue_scripts', 'markegol_theme_enqueue_styles');

function markegol_theme_enqueue_scripts() {
    wp_enqueue_script(
        'markegol-theme-js',
        get_template_directory_uri() . '/source/js/theme.js',
        array(),
        false, 
        true
    );
}
add_action('wp_enqueue_scripts', 'markegol_theme_enqueue_scripts');

function markegol_set_default_block_lock() {
    if (get_option('themes_machine_lock_all_blocks') === false) {
        update_option('themes_machine_lock_all_blocks', 1);
    }
}
add_action('after_switch_theme', 'markegol_set_default_block_lock');


add_action('admin_init', function () {
    if (!isset($_POST['themes_machine_lock_toggle'])) {
        return;
    }

    if (!check_admin_referer('themes_machine_lock_toggle')) {
        return;
    }

    $current = get_option('themes_machine_lock_all_blocks', 0);
    $new = $current ? 0 : 1;
    update_option('themes_machine_lock_all_blocks', $new);

    add_action('admin_notices', function () use ($new) {
        echo '<div class="notice notice-success is-dismissible"><p>'
            . ($new ? '🔒 All blocks have been disabled.' : '🔓 All blocks have been enabled.')
            . '</p></div>';
    });
});


add_action('enqueue_block_editor_assets', function () {
    $theme = wp_get_theme();
    $theme_name = $theme->get('Name');
    $theme_url  = $theme->get('ThemeURI') ?: admin_url('themes.php');
    $locked = get_option('themes_machine_lock_all_blocks', 0);

    $data = [
        'theme_name' => $theme_name,
        'theme_uri'  => $theme_url,
        'settings_theme_uri'  => admin_url('themes.php?page=theme-welcome'),
        'lock_move' => $locked,
    ];


    $handle = 'themes-machine-lock-blocks';
    $path   = get_stylesheet_directory() . '/theme-welcome/welcome-theme-scripts.js';
    $uri    = get_stylesheet_directory_uri() . '/theme-welcome/welcome-theme-scripts.js';

    wp_enqueue_script(
        $handle,
        $uri,
        ['wp-plugins', 'wp-edit-post', 'wp-components', 'wp-element', 'wp-hooks', 'wp-block-editor', 'wp-data'],
        filemtime($path),
        true
    );

    wp_localize_script($handle, 'Theme', $data);
});




