<?php

/*
Plugin Name: EZ Horoscope Professional
Plugin URI: https://ezhoroscope.com/
Description: EZ Horoscope allows you to create beautiful, engaging horoscopes with daily updates, including advice, guidance, and accurate cosmic influences, all crafted to keep your readers coming back for more.
Version: 2.2.2.44
Author: EZHoroscope
Author URI: https://ezhoroscope.com/
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/*
EZ Horoscope is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

EZ Horoscope is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this plugin. If not, see https://www.gnu.org/licenses/gpl-2.0.html.
Text Domain: ez-horoscope
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
add_action( 'plugins_loaded', function () {
    $locale = determine_locale();
    $mofile = plugin_dir_path( __FILE__ ) . 'languages/ez-horoscope-' . $locale . '.mo';
    if ( file_exists( $mofile ) ) {
        load_textdomain( 'ez-horoscope', $mofile );
    }
} );
// ✅ Define the plugin version as a constant so it’s accessible everywhere
if ( !defined( 'EZHP_PLUGIN_VERSION' ) ) {
    define( 'EZHP_PLUGIN_VERSION', '2.2.2.44' );
}
if ( !defined( 'EZHP_PLUGIN_URL' ) ) {
    define( 'EZHP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Include Automation functionality (must be before settings page)
require_once plugin_dir_path( __FILE__ ) . 'admin/ezhp-automation.php';
// Include the tabbed settings page
require_once plugin_dir_path( __FILE__ ) . 'admin/ezhp-settings-page.php';
// Include Voice Agent functionality
require_once plugin_dir_path( __FILE__ ) . 'admin/ezhp-voice-agent.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/api-chinese-horoscope.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/api-numerology.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/api-zodiac-compatibility.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/api-birth-chart.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/api-synastry-chart.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/api-moon-phase.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/summaries-rest.php';
require_once plugin_dir_path( __FILE__ ) . 'inc/api-zodiac-profile.php';
require_once plugin_dir_path( __FILE__ ) . 'shortcodes/shortcode-horoscope-summaries.php';
require_once plugin_dir_path( __FILE__ ) . 'shortcodes/shortcode-weekly-section.php';
if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
    require_once dirname( __FILE__ ) . '/vendor/autoload.php';
}
if ( function_exists( 'ezhp_fs' ) ) {
    ezhp_fs()->set_basename( false, __FILE__ );
} else {
    function ezhp_horoscope_get_license_key() {
        if ( function_exists( 'ezhp_fs' ) && ezhp_fs()->is_premium() ) {
            $fs = ezhp_fs();
            $license = null;
            // ✅ Only call _get_license() if it exists (Pro SDK)
            if ( method_exists( $fs, '_get_license' ) ) {
                $license = $fs->_get_license();
            }
            if ( $license && isset( $license->id ) ) {
                $license_id = $license->id;
                $license_status = ( property_exists( $license, 'status' ) ? $license->status : null );
                $license_expiration = ( property_exists( $license, 'expiration' ) ? $license->expiration : null );
                return $license_id;
            }
        }
        return null;
    }

    if ( !function_exists( 'ezhp_fs' ) ) {
        // Create a helper function for easy SDK access.
        function ezhp_fs() {
            global $ezhp_fs;
            if ( !isset( $ezhp_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $ezhp_fs = fs_dynamic_init( array(
                    'id'               => '16865',
                    'slug'             => 'ez-horoscope',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_ebe38fc821365ad3ae083dd5a6efc',
                    'is_premium'       => false,
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'trial'            => array(
                        'days'               => 14,
                        'is_require_payment' => true,
                    ),
                    'menu'             => array(
                        'slug'    => 'ez-horoscope-settings',
                        'support' => false,
                        'parent'  => array(
                            'slug' => 'options-general.php',
                        ),
                    ),
                    'is_live'          => true,
                    'is_org_compliant' => true,
                ) );
            }
            return $ezhp_fs;
        }

        // Init Freemius.
        ezhp_fs();
        // Signal that SDK was initiated.
        do_action( 'ezhp_fs_loaded' );
    }
    function ezhp_check_for_shortcode(  $posts  ) {
        if ( empty( $posts ) ) {
            return $posts;
        }
        $shortcode_found = false;
        foreach ( $posts as $post ) {
            if ( stripos( $post->post_content, '[ezhp_ezhoroscope ' ) !== false || stripos( $post->post_content, '[ezhp_ezweeklyhoroscope' ) !== false || stripos( $post->post_content, '[ezhp_eznumerology' ) !== false || stripos( $post->post_content, '[ezhp_ezsunsigncompatibility' ) !== false || stripos( $post->post_content, '[ezhp_ezchinesehoroscope' ) !== false || stripos( $post->post_content, '[ezhp_eztarot_3cardspread' ) !== false || stripos( $post->post_content, '[ezhp_ezmoonphase' ) !== false ) {
                $shortcode_found = true;
                break;
            }
        }
        if ( $shortcode_found ) {
            ezhp_enqueue_scripts_and_styles();
            // Ensure JS and CSS are enqueued
        }
        return $posts;
    }

    add_filter( 'the_posts', 'ezhp_check_for_shortcode' );
    function ezhp_enqueue_scripts_and_styles() {
        wp_enqueue_style(
            'ez-horoscope-style',
            plugins_url( '/css/horoscope.css', __FILE__ ),
            array(),
            EZHP_PLUGIN_VERSION
        );
        wp_enqueue_script(
            'ez-horoscope-script',
            plugins_url( '/js/horoscope.js', __FILE__ ),
            array('jquery'),
            EZHP_PLUGIN_VERSION,
            true
        );
        wp_localize_script( 'ez-horoscope-script', 'ezhpAjax', array(
            'ajaxurl'  => admin_url( 'admin-ajax.php' ),
            'security' => wp_create_nonce( 'ezhp_generate_voice_shortcode' ),
        ) );
    }

    function ezhp_admin_enqueue_styles(  $hook_suffix  ) {
        if ( 'settings_page_ez-horoscope-settings' === $hook_suffix ) {
            ezhp_enqueue_scripts_and_styles();
            wp_enqueue_style(
                'ezhp-admin-style',
                plugin_dir_url( __FILE__ ) . 'css/ezhp-admin.css',
                [],
                EZHP_PLUGIN_VERSION
            );
            // Brilyi Voice Agent
            // Enqueue voice agent admin JS
            wp_enqueue_script(
                'ezhp-voice-agent-admin',
                plugins_url( '/js/voice-agent.js', __FILE__ ),
                array('jquery'),
                EZHP_PLUGIN_VERSION,
                true
            );
            // Pass saved agentId to JS
            wp_localize_script( 'ezhp-voice-agent-admin', 'ezhpVoiceAgent', array(
                'savedAgentId' => get_option( 'ezhp_voice_agent_id', '' ),
            ) );
            // Automation tab JS
            wp_enqueue_script(
                'ezhp-automation-admin',
                plugins_url( '/js/automation.js', __FILE__ ),
                array('jquery'),
                EZHP_PLUGIN_VERSION,
                true
            );
            // Pass AJAX URL, nonce, and translatable strings to JS
            /* translators: %s: page title */
            $ezhp_slug_required_text = __( 'Slug is required for: %s', 'ez-horoscope' );
            /* translators: %s: page slug */
            $ezhp_title_required_text = __( 'Title is required for: %s', 'ez-horoscope' );
            /* translators: %d: number of pages created */
            $ezhp_pages_created_text = __( '%d page(s) created successfully.', 'ez-horoscope' );
            // Get current language from URL parameter
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading URL parameter for display purposes only, not processing form data
            $ezhp_current_lang = ( isset( $_GET['lang'] ) ? sanitize_text_field( wp_unslash( $_GET['lang'] ) ) : 'en' );
            wp_localize_script( 'ezhp-automation-admin', 'ezhpAutomation', array(
                'ajaxurl'  => admin_url( 'admin-ajax.php' ),
                'nonce'    => wp_create_nonce( 'ezhp_automation_nonce' ),
                'language' => $ezhp_current_lang,
                'strings'  => array(
                    'slugRequired'  => $ezhp_slug_required_text,
                    'titleRequired' => $ezhp_title_required_text,
                    'confirmCreate' => __( 'Are you sure you want to create the selected pages?', 'ez-horoscope' ),
                    'pagesCreated'  => $ezhp_pages_created_text,
                    'unknownError'  => __( 'An unknown error occurred.', 'ez-horoscope' ),
                    'ajaxError'     => __( 'AJAX request failed', 'ez-horoscope' ),
                    'resultsTitle'  => __( 'Results', 'ez-horoscope' ),
                    'created'       => __( 'Created', 'ez-horoscope' ),
                    'errors'        => __( 'Errors', 'ez-horoscope' ),
                    'pageTitle'     => __( 'Page Title', 'ez-horoscope' ),
                    'status'        => __( 'Status', 'ez-horoscope' ),
                    'actions'       => __( 'Actions', 'ez-horoscope' ),
                    'statusCreated' => __( 'Created', 'ez-horoscope' ),
                    'statusError'   => __( 'Error', 'ez-horoscope' ),
                    'view'          => __( 'View', 'ez-horoscope' ),
                    'edit'          => __( 'Edit', 'ez-horoscope' ),
                ),
            ) );
        }
    }

    add_action( 'admin_enqueue_scripts', 'ezhp_admin_enqueue_styles' );
    // Add a "Settings" link in the plugins page next to the plugin name
    add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'ezhp_horoscope_add_action_links' );
    function ezhp_horoscope_add_action_links(  $links  ) {
        $settings_link = '<a href="options-general.php?page=ez-horoscope-settings">Settings</a>';
        array_push( $links, $settings_link );
        return $links;
    }

    // Hook to add the plugin settings page to the WordPress admin menu
    add_action( 'admin_menu', 'ezhp_horoscope_add_settings_page' );
    // Function to add the settings page under the "Settings" menu
    function ezhp_horoscope_add_settings_page() {
        add_options_page(
            'EZ Horoscope Settings',
            'Horoscope Settings',
            'manage_options',
            'ez-horoscope-settings',
            'ezhp_settings_page'
        );
    }

    // Function to render the settings page content
    function ezhp_horoscope_render_settings_page() {
        ?>
        <div class="wrap">
            <h1>EZ Horoscope Settings</h1>
            <p>
                <strong>Instructions:</strong> Modify your settings then place the shortcode
                <code>[ezhp_ezhoroscope sign="zodiacsign" language="languageCode"]</code> wherever you want the horoscope to appear.
                <br><strong>Example:</strong> <code>[ezhp_ezhoroscope sign="leo" language="en"]</code>
                <br>For additional instructions, visit:
                <a href="https://ezhoroscope.com/documentation/" target="_blank">
                    https://ezhoroscope.com/documentation/
                </a>
            </p>
            <form method="post" action="options.php" class="settings-form">
                <?php 
        // Output security fields for the registered setting
        settings_fields( 'ezhp_horoscope_settings_group' );
        // Output the settings sections and fields
        do_settings_sections( 'ez-horoscope-settings' );
        // Output save settings button
        submit_button();
        ?>
            </form>
        </div>
    <?php 
    }

    // Hook to initialize the plugin settings
    add_action( 'admin_init', 'ezhp_horoscope_register_settings' );
    function ezhp_horoscope_register_settings() {
        // Register settings
        register_setting( 'ezhp_horoscope_settings_group', 'ezhp_horoscope_background_color', 'ezhp_horoscope_sanitize_hex_color' );
        register_setting( 'ezhp_horoscope_settings_group', 'ezhp_horoscope_font', 'sanitize_text_field' );
        register_setting( 'ezhp_horoscope_settings_group', 'ezhp_horoscope_font_size', 'ezhp_horoscope_sanitize_font_size' );
        register_setting( 'ezhp_horoscope_settings_group', 'ezhp_horoscope_font_color', 'ezhp_horoscope_sanitize_hex_color' );
        register_setting( 'ezhp_horoscope_settings_group', 'ezhp_horoscope_include_backlink', 'ezhp_horoscope_sanitize_checkbox' );
        // Add the settings section
        add_settings_section(
            'ezhp_horoscope_settings_section',
            'Customization Settings',
            null,
            'ez-horoscope-settings'
        );
        // Add settings fields
        add_settings_field(
            'ezhp_horoscope_background_color_field',
            'Background Color',
            'ezhp_horoscope_background_color_field_render',
            'ez-horoscope-settings',
            'ezhp_horoscope_settings_section'
        );
        add_settings_field(
            'ezhp_horoscope_font_field',
            'Font',
            'ezhp_horoscope_font_field_render',
            'ez-horoscope-settings',
            'ezhp_horoscope_settings_section'
        );
        add_settings_field(
            'ezhp_horoscope_font_size_field',
            'Font Size',
            'ezhp_horoscope_font_size_field_render',
            'ez-horoscope-settings',
            'ezhp_horoscope_settings_section'
        );
        // Add settings field for font color
        add_settings_field(
            'ezhp_horoscope_font_color_field',
            'Font Color',
            'ezhp_horoscope_font_color_field_render',
            'ez-horoscope-settings',
            'ezhp_horoscope_settings_section'
        );
        // Sanitization callback for hex color
        function ezhp_horoscope_sanitize_hex_color(  $color  ) {
            // Check if the color is a valid hex code
            if ( preg_match( '/^#[a-fA-F0-9]{6}$/', $color ) ) {
                return $color;
            } else {
                // Return a default color if the input is invalid
                return '#ffffff';
            }
        }

        // Sanitization callback for font size
        function ezhp_horoscope_sanitize_font_size(  $font_size  ) {
            // Allow only numbers and specific units (px, em, %)
            if ( preg_match( '/^\\d+(px|em|%)$/', $font_size ) ) {
                return $font_size;
            } else {
                // Return a default size if the input is invalid
                return '14px';
            }
        }

        // Sanitization callback for checkbox
        function ezhp_horoscope_sanitize_checkbox(  $input  ) {
            // Return true if checkbox is checked, false otherwise
            return ( $input ? true : false );
        }

        add_settings_field(
            'ezhp_horoscope_include_backlink_field',
            'Include Backlink',
            'ezhp_horoscope_include_backlink_field_render',
            'ez-horoscope-settings',
            'ezhp_horoscope_settings_section'
        );
    }

    // Render the "Include Backlink" field in settings
    function ezhp_horoscope_include_backlink_field_render() {
        $include_backlink = get_option( 'ezhp_horoscope_include_backlink', false );
        ?>
        <input type="checkbox" id="ezhp_horoscope_include_backlink" name="ezhp_horoscope_include_backlink" value="1" <?php 
        checked( 1, $include_backlink );
        ?>>
        <label for="ezhp_horoscope_include_backlink">Include a backlink to enneagramzoom.com</label>
        <?php 
    }

    // Render the font field in settings
    function ezhp_horoscope_font_field_render() {
        $font = get_option( 'ezhp_horoscope_font', 'Arial' );
        ?>
        <input type="text" id="ezhp_horoscope_font" name="ezhp_horoscope_font" value="<?php 
        echo esc_attr( $font );
        ?>" class="regular-text">
        <p>Enter the font name for the horoscope text (e.g., Arial, Georgia, Times New Roman).</p>
    <?php 
    }

    // Render the font size field in settings
    function ezhp_horoscope_font_size_field_render() {
        $font_size = get_option( 'ezhp_horoscope_font_size', '14px' );
        ?>
        <input type="text" id="ezhp_horoscope_font_size" name="ezhp_horoscope_font_size" value="<?php 
        echo esc_attr( $font_size );
        ?>" class="regular-text">
        <p>Enter the font size for the horoscope text (e.g., 14px, 16px, 1.2em).</p>
    <?php 
    }

    function ezhp_horoscope_font_color_field_render() {
        $color = get_option( 'ezhp_horoscope_font_color', '#000000' );
        // Default to black if not set
        ?>
        <input type="text" id="ezhp_horoscope_font_color" name="ezhp_horoscope_font_color" value="<?php 
        echo esc_attr( $color );
        ?>" class="color-field">
        <p>Pick a font color using the color picker or enter a hex code (e.g., #000000). <strong>Font colors are only applied to areas with background color other than white.</strong></p>
        <?php 
    }

    // Enqueue the WordPress color picker
    add_action( 'admin_enqueue_scripts', 'ezhp_horoscope_enqueue_color_picker' );
    function ezhp_horoscope_enqueue_color_picker(  $hook_suffix  ) {
        if ( $hook_suffix !== 'settings_page_ez-horoscope-settings' ) {
            return;
        }
        wp_enqueue_style( 'wp-color-picker' );
        wp_enqueue_script(
            'ez-horoscope-color-picker',
            plugins_url( 'color-picker.js', __FILE__ ),
            array('wp-color-picker'),
            EZHP_PLUGIN_VERSION,
            true
        );
    }

    // Function to render the background color input field with a color picker
    function ezhp_horoscope_background_color_field_render() {
        $color = get_option( 'ezhp_horoscope_background_color', '#f5eae4' );
        ?>
        <input type="text" id="ezhp_horoscope_background_color" name="ezhp_horoscope_background_color" value="<?php 
        echo esc_attr( $color );
        ?>" class="regular-text color-field">
        <p>Pick a background color using the color picker or enter a hex code (e.g., #f5eae4).</p>
<?php 
    }

    /// Function to send activation data to the Web API
    function ezhp_track_plugin_activation() {
        // Get the plugin name
        $plugin_name = 'EZ Horoscope Premium';
        // The name of your plugin
        // Get the full site URL (this will be sent as "DomainName" in the payload)
        $site_url = get_site_url();
        // Prepare the data to send to the Web API
        $activation_data = array(
            'PluginName'        => $plugin_name,
            'PluginVersion'     => EZHP_PLUGIN_VERSION,
            'DomainName'        => $site_url,
            'DateTimeActivated' => current_time( 'Y-m-d\\TH:i:s' ),
        );
        // Send the data to your .NET Web API endpoint
        $response = wp_remote_post( 'https://www.enneagramzoom.com/api/PluginActivation', array(
            'method'  => 'POST',
            'body'    => wp_json_encode( $activation_data ),
            'headers' => array(
                'Content-Type' => 'application/json',
            ),
            'timeout' => 20,
        ) );
        // Check if the API request was successful
        if ( is_wp_error( $response ) ) {
            // Log the error or handle it accordingly
            //error_log('Plugin activation tracking failed: ' . $response->get_error_message());
        } else {
            // Handle the API response
            $api_response_body = wp_remote_retrieve_body( $response );
            $api_response = json_decode( $api_response_body, true );
            // Check if the response contains the ActivationGuid
            if ( isset( $api_response['activationGuid'] ) ) {
                // Sanitize the ActivationGuid before saving it to the options table
                $sanitized_activation_guid = sanitize_text_field( $api_response['activationGuid'] );
                // Save the sanitized ActivationGuid to the WordPress options table
                update_option( 'ezhp_horoscope_activation_guid', $sanitized_activation_guid );
                // Optionally, you can log or use the ActivationGuid for debugging
                //error_log('Activation GUID saved: ' . $api_response['activationGuid']);
            } else {
                //error_log('Activation GUID not found in the API response.');
            }
        }
    }

    // Register the plugin activation hook
    register_activation_hook( __FILE__, 'ezhp_track_plugin_activation' );
    // Register the plugin deactivation hook
    register_deactivation_hook( __FILE__, 'ezhp_track_plugin_deactivation' );
    /**
     * Handles plugin deactivation tracking
     */
    function ezhp_track_plugin_deactivation() {
        // Retrieve the stored Activation GUID
        $activation_guid = get_option( 'ezhp_horoscope_activation_guid', '' );
        // If no activation GUID is found, skip the call
        if ( empty( $activation_guid ) ) {
            return;
        }
        // Prepare the payload
        $payload = array(
            'activationGuid' => $activation_guid,
        );
        // Send the deactivation event to your .NET API endpoint
        $response = wp_remote_post( 'https://www.enneagramzoom.com/api/PluginActivation/Deactivate', array(
            'method'  => 'POST',
            'body'    => wp_json_encode( $payload ),
            'headers' => array(
                'Content-Type' => 'application/json',
            ),
            'timeout' => 20,
        ) );
        // Optionally handle errors (commented out for now)
        /*
        if (is_wp_error($response)) {
            error_log('Plugin deactivation tracking failed: ' . $response->get_error_message());
        }
        */
    }

    // Function to display the daily horoscope content via iframe with a dynamic background color, font, font size, and promotional content visibility
    function ezhp_display_horoscope_section(  $atts  ) {
        $atts = shortcode_atts( array(
            'sign'               => 'aquarius',
            'language'           => 'en',
            'top_headline_level' => 'h1',
        ), $atts );
        $zodiac_sign = sanitize_text_field( strtolower( trim( $atts['sign'] ) ) );
        $language = sanitize_text_field( strtolower( trim( $atts['language'] ) ) );
        $top_headline_level = sanitize_text_field( strtolower( trim( $atts['top_headline_level'] ) ) );
        $background_color = get_option( 'ezhp_horoscope_background_color', '' );
        $include_backlink = get_option( 'ezhp_horoscope_include_backlink', false );
        $font = get_option( 'ezhp_horoscope_font', 'Arial' );
        $font_size = get_option( 'ezhp_horoscope_font_size', '14px' );
        $font_color = get_option( 'ezhp_horoscope_font_color', '#000000' );
        // Assuming this is defined elsewhere
        // Freemius license and other checks
        $license_key = ezhp_horoscope_get_license_key();
        $iframe_url = 'https://www.enneagramzoom.com/syndicated/' . $zodiac_sign . '-daily-horoscope';
        // Retrieve the activation GUID from the WordPress options table
        $activation_guid = get_option( 'ezhp_horoscope_activation_guid', '' );
        // Build query parameters
        $query_params = array(
            'language'       => $language,
            'color'          => $background_color,
            'font'           => $font,
            'fontSize'       => $font_size,
            'fontColor'      => $font_color,
            'activationGuid' => $activation_guid,
            'licenseKey'     => $license_key,
        );
        // Append all query parameters to the URL
        foreach ( $query_params as $key => $value ) {
            if ( !empty( $value ) ) {
                $iframe_url = add_query_arg( $key, urlencode( $value ), $iframe_url );
            }
        }
        // Prepare the backlink HTML
        $link_url = 'https://www.enneagramzoom.com/horoscope/' . $zodiac_sign . '-horoscope-today/';
        $link_text = ucfirst( $zodiac_sign ) . ' daily horoscope';
        $backlink_html = '';
        if ( $include_backlink ) {
            $backlink_html = '<div class="backlink-container">
                <a href="' . esc_url( $link_url ) . '" target="_blank">' . esc_html( $link_text ) . '</a> by enneagramzoom.com
            </div>';
        }
        // Return the iframe and any additional HTML
        return '
            <iframe id="syndicated-horoscope-iframe" src="' . esc_url( $iframe_url ) . '" width="100%" frameborder="0"></iframe>
            ' . $backlink_html;
    }

    // Register the shortcode [ezhp_ezhoroscope sign="zodiac_sign"]
    add_shortcode( 'ezhp_ezhoroscope', 'ezhp_display_horoscope_section' );
    // Function to display the weekly horoscope content via iframe with a dynamic background color, font, font size, and promotional content visibility
    function ezhp_display_weekly_horoscope_section(  $atts  ) {
        $atts = shortcode_atts( array(
            'sign'     => 'aquarius',
            'language' => 'en',
        ), $atts );
        $zodiac_sign = sanitize_text_field( strtolower( trim( $atts['sign'] ) ) );
        $language = sanitize_text_field( strtolower( trim( $atts['language'] ) ) );
        $background_color = get_option( 'ezhp_horoscope_background_color', '' );
        $include_backlink = get_option( 'ezhp_horoscope_include_backlink', false );
        $font = get_option( 'ezhp_horoscope_font', 'Arial' );
        $font_size = get_option( 'ezhp_horoscope_font_size', '14px' );
        // Retrieve the Freemius license key using the helper function
        $license_key = ezhp_horoscope_get_license_key();
        $iframe_url = 'https://www.enneagramzoom.com/syndicated/' . $zodiac_sign . '-weekly-horoscope-syndicated';
        // Retrieve the activation GUID from the WordPress options table
        $activation_guid = get_option( 'ezhp_horoscope_activation_guid', '' );
        // Add the license key as a query parameter if it exists
        if ( $license_key ) {
            $iframe_url = add_query_arg( 'licenseKey', urlencode( $license_key ), $iframe_url );
        }
        // Add the activation GUID as a query parameter if it exists
        if ( $activation_guid ) {
            $iframe_url = add_query_arg( 'activationGuid', urlencode( $activation_guid ), $iframe_url );
        }
        // Add the language as a query parameter if specified
        if ( !empty( $language ) ) {
            $iframe_url = add_query_arg( 'language', urlencode( $language ), $iframe_url );
        }
        if ( !empty( $background_color ) ) {
            $iframe_url = add_query_arg( 'color', urlencode( $background_color ), $iframe_url );
        }
        if ( !empty( $font ) ) {
            $iframe_url = add_query_arg( 'font', urlencode( $font ), $iframe_url );
        }
        if ( !empty( $font_size ) ) {
            $iframe_url = add_query_arg( 'fontSize', urlencode( $font_size ), $iframe_url );
        }
        $link_url = 'https://www.enneagramzoom.com/horoscope/' . $zodiac_sign . '-horoscope-today/';
        $link_text = ucfirst( $zodiac_sign ) . ' weekly horoscope';
        $backlink_html = '';
        if ( $include_backlink ) {
            $backlink_html = '<div class="backlink-container">
                <a href="' . esc_url( $link_url ) . '" target="_blank">' . esc_html( $link_text ) . '</a> by enneagramzoom.com
            </div>';
        }
        return '
        <iframe id="syndicated-horoscope-iframe" src="' . esc_url( $iframe_url ) . '" width="100%" frameborder="0"></iframe>
        ' . $backlink_html;
    }

    //end weekly horoscope function
    add_shortcode( 'ezhp_ezweeklyhoroscope', 'ezhp_display_weekly_horoscope_section' );
    add_shortcode( 'ezhp_ezhoroscope_section', 'ezhp_display_horoscope_section_section' );
    function ezhp_display_horoscope_section_section(  $atts  ) {
        $atts = shortcode_atts( array(
            'sign'     => 'aquarius',
            'section'  => 'guidance',
            'language' => 'en',
        ), $atts );
        $ezhp_zodiac_sign = sanitize_text_field( strtolower( trim( $atts['sign'] ) ) );
        $ezhp_language = sanitize_text_field( strtolower( trim( $atts['language'] ) ) );
        $ezhp_content_section = sanitize_text_field( strtolower( trim( $atts['section'] ) ) );
        $ezhp_api_key = sanitize_text_field( get_option( 'ezhp_horoscope_activation_guid', '' ) );
        $ezhp_api_url = sprintf(
            'https://api.ezhoroscope.com/api/Horoscope/western-section?zodiacSign=%s&content=%s&apikey=%s&language=%s',
            rawurlencode( ucfirst( $ezhp_zodiac_sign ) ),
            rawurlencode( $ezhp_content_section ),
            rawurlencode( $ezhp_api_key ),
            rawurlencode( $ezhp_language )
        );
        $ezhp_response = wp_remote_get( $ezhp_api_url, [
            'timeout' => 20,
            'headers' => [
                'Accept' => 'application/json',
            ],
        ] );
        if ( is_wp_error( $ezhp_response ) ) {
            return 'Unable to retrieve horoscope section. Please try again later.';
        }
        $ezhp_body = wp_remote_retrieve_body( $ezhp_response );
        $ezhp_data = json_decode( $ezhp_body, true );
        if ( empty( $ezhp_data ) || empty( $ezhp_data['sectionHtml'] ) ) {
            return 'Invalid response from the horoscope API.';
        }
        return wp_kses_post( $ezhp_data['sectionHtml'] );
    }

    /**
     * Section Title Shortcode
     * Usage: [ezhp_ezhoroscope_section_title sign="aries" section="moon-item" language="en"]
     * Usage: [ezhp_ezhoroscope_section_title sign="aries" section="moon-item" tag="h3" class="planet-title"]
     *
     * @param array $atts Shortcode attributes.
     * @return string The section title HTML.
     */
    add_shortcode( 'ezhp_ezhoroscope_section_title', 'ezhp_display_horoscope_section_title' );
    function ezhp_display_horoscope_section_title(  $atts  ) {
        $ezhp_atts = shortcode_atts( array(
            'sign'     => 'aries',
            'section'  => 'sun-item',
            'language' => 'en',
            'tag'      => '',
            'class'    => '',
        ), $atts, 'ezhp_ezhoroscope_section_title' );
        $ezhp_zodiac_sign = sanitize_text_field( strtolower( trim( $ezhp_atts['sign'] ) ) );
        $ezhp_language = sanitize_text_field( strtolower( trim( $ezhp_atts['language'] ) ) );
        $ezhp_content_section = sanitize_text_field( strtolower( trim( $ezhp_atts['section'] ) ) );
        $ezhp_tag = sanitize_key( $ezhp_atts['tag'] );
        $ezhp_class = sanitize_html_class( $ezhp_atts['class'] );
        $ezhp_api_key = sanitize_text_field( get_option( 'ezhp_horoscope_activation_guid', '' ) );
        $ezhp_api_url = sprintf(
            'https://api.ezhoroscope.com/api/Horoscope/western-section?zodiacSign=%s&content=%s&apikey=%s&language=%s',
            rawurlencode( ucfirst( $ezhp_zodiac_sign ) ),
            rawurlencode( $ezhp_content_section ),
            rawurlencode( $ezhp_api_key ),
            rawurlencode( $ezhp_language )
        );
        $ezhp_response = wp_remote_get( $ezhp_api_url, array(
            'timeout' => 20,
            'headers' => array(
                'Accept' => 'application/json',
            ),
        ) );
        if ( is_wp_error( $ezhp_response ) ) {
            return '';
        }
        $ezhp_body = wp_remote_retrieve_body( $ezhp_response );
        $ezhp_data = json_decode( $ezhp_body, true );
        if ( empty( $ezhp_data ) || empty( $ezhp_data['sectionTitle'] ) ) {
            return '';
        }
        $ezhp_title = sanitize_text_field( $ezhp_data['sectionTitle'] );
        if ( empty( $ezhp_tag ) ) {
            return esc_html( $ezhp_title );
        }
        $ezhp_allowed_tags = array(
            'h1',
            'h2',
            'h3',
            'h4',
            'h5',
            'h6',
            'p',
            'span',
            'div',
            'strong',
            'em'
        );
        if ( !in_array( $ezhp_tag, $ezhp_allowed_tags, true ) ) {
            return esc_html( $ezhp_title );
        }
        $ezhp_class_attr = ( !empty( $ezhp_class ) ? ' class="' . esc_attr( $ezhp_class ) . '"' : '' );
        return '<' . esc_attr( $ezhp_tag ) . $ezhp_class_attr . '>' . esc_html( $ezhp_title ) . '</' . esc_attr( $ezhp_tag ) . '>';
    }

    //Begin Tarot Block
    add_shortcode( 'ezhp_eztarot_3cardspread', 'ezhp_display_tarot_3cardspread' );
    function ezhp_display_tarot_3cardspread(  $atts  ) {
        // Set default attributes
        $atts = shortcode_atts( array(
            'language'           => 'en',
            'top_headline_level' => 'h2',
        ), $atts );
        $language = sanitize_text_field( strtolower( trim( $atts['language'] ) ) );
        // Retrieve the Freemius license key
        $license_key = ezhp_horoscope_get_license_key();
        // Retrieve the activation GUID from the WordPress options table
        $activation_guid = get_option( 'ezhp_horoscope_activation_guid', '' );
        $top_headline_level = sanitize_text_field( strtolower( trim( $atts['top_headline_level'] ) ) );
        $background_color = get_option( 'ezhp_horoscope_background_color', '' );
        $include_backlink = get_option( 'ezhp_horoscope_include_backlink', false );
        $font = get_option( 'ezhp_horoscope_font', 'Arial' );
        $font_size = get_option( 'ezhp_horoscope_font_size', '14px' );
        $font_color = get_option( 'ezhp_horoscope_font_color', '#000000' );
        // Base iframe URL
        $iframe_url = 'https://www.enneagramzoom.com/syndicated/tarot/3-card-spread';
        // Build query parameters
        $query_params = array(
            'language'       => $language,
            'activationGuid' => $activation_guid,
            'licenseKey'     => $license_key,
            'color'          => $background_color,
            'font'           => $font,
            'fontSize'       => $font_size,
            'fontColor'      => $font_color,
        );
        // Append all query parameters to the URL
        foreach ( $query_params as $key => $value ) {
            if ( !empty( $value ) ) {
                $iframe_url = add_query_arg( $key, urlencode( $value ), $iframe_url );
            }
        }
        // Return the iframe HTML
        return '<div id="ez-iframe-container" style="overflow: hidden;"><iframe id="syndicated-horoscope-iframe" src="' . esc_url( $iframe_url ) . '" width="100%"  frameborder="0"></iframe></div>';
    }

    //End Tarot Block
    //Begin Numerology Block
    add_shortcode( 'ezhp_eznumerology', 'ezhp_display_numerology' );
    function ezhp_display_numerology(  $atts  ) {
        // Set default attributes
        $atts = shortcode_atts( array(
            'language'           => 'en',
            'top_headline_level' => 'h2',
        ), $atts );
        $language = sanitize_text_field( strtolower( trim( $atts['language'] ) ) );
        // Retrieve the Freemius license key
        $license_key = ezhp_horoscope_get_license_key();
        // Retrieve the activation GUID from the WordPress options table
        $activation_guid = get_option( 'ezhp_horoscope_activation_guid', '' );
        $top_headline_level = sanitize_text_field( strtolower( trim( $atts['top_headline_level'] ) ) );
        $background_color = get_option( 'ezhp_horoscope_background_color', '' );
        $include_backlink = get_option( 'ezhp_horoscope_include_backlink', false );
        $font = get_option( 'ezhp_horoscope_font', 'Arial' );
        $font_size = get_option( 'ezhp_horoscope_font_size', '14px' );
        $font_color = get_option( 'ezhp_horoscope_font_color', '#000000' );
        // Base iframe URL
        $iframe_url = 'https://www.enneagramzoom.com/syndicated/numerology';
        // Build query parameters
        $query_params = array(
            'language'       => $language,
            'activationGuid' => $activation_guid,
            'licenseKey'     => $license_key,
            'color'          => $background_color,
            'font'           => $font,
            'fontSize'       => $font_size,
            'fontColor'      => $font_color,
        );
        // Append all query parameters to the URL
        foreach ( $query_params as $key => $value ) {
            if ( !empty( $value ) ) {
                $iframe_url = add_query_arg( $key, urlencode( $value ), $iframe_url );
            }
        }
        // Return the iframe HTML
        return '<div id="ez-iframe-container" style="overflow: hidden;"><iframe id="syndicated-horoscope-iframe" src="' . esc_url( $iframe_url ) . '" width="100%"  frameborder="0"></iframe></div>';
    }

    //End Numerology Block
    //Begin NumeSunSignCompatibilityrology Block
    add_shortcode( 'ezhp_ezsunsigncompatibility', 'ezhp_display_sunsigncompatibility' );
    function ezhp_display_sunsigncompatibility(  $atts  ) {
        // Set default attributes
        $atts = shortcode_atts( array(
            'language'           => 'en',
            'top_headline_level' => 'h2',
        ), $atts );
        $language = sanitize_text_field( strtolower( trim( $atts['language'] ) ) );
        // Retrieve the Freemius license key
        $license_key = ezhp_horoscope_get_license_key();
        // Retrieve the activation GUID from the WordPress options table
        $activation_guid = get_option( 'ezhp_horoscope_activation_guid', '' );
        $top_headline_level = sanitize_text_field( strtolower( trim( $atts['top_headline_level'] ) ) );
        $background_color = get_option( 'ezhp_horoscope_background_color', '' );
        $include_backlink = get_option( 'ezhp_horoscope_include_backlink', false );
        $font = get_option( 'ezhp_horoscope_font', 'Arial' );
        $font_size = get_option( 'ezhp_horoscope_font_size', '14px' );
        $font_color = get_option( 'ezhp_horoscope_font_color', '#000000' );
        // Base iframe URL
        $iframe_url = 'https://www.enneagramzoom.com/syndicated/zodiaccompatibility';
        //zodiaccompatibility
        // Build query parameters
        $query_params = array(
            'language'       => $language,
            'activationGuid' => $activation_guid,
            'licenseKey'     => $license_key,
            'color'          => $background_color,
            'font'           => $font,
            'fontSize'       => $font_size,
            'fontColor'      => $font_color,
        );
        // Append all query parameters to the URL
        foreach ( $query_params as $key => $value ) {
            if ( !empty( $value ) ) {
                $iframe_url = add_query_arg( $key, urlencode( $value ), $iframe_url );
            }
        }
        // Return the iframe HTML
        return '<div id="ez-iframe-container" style="overflow: hidden;"><iframe id="syndicated-horoscope-iframe" src="' . esc_url( $iframe_url ) . '" width="100%"  frameborder="0"></iframe></div>';
    }

    //End SunSignCompatibility Block
    //Begin Chinese Horoscope Block
    add_shortcode( 'ezhp_ezchinesehoroscope', 'ezhp_display_chinesehoroscope' );
    function ezhp_display_chinesehoroscope(  $atts  ) {
        // Set default attributes
        $atts = shortcode_atts( array(
            'language'           => 'en',
            'top_headline_level' => 'h2',
        ), $atts );
        $language = sanitize_text_field( strtolower( trim( $atts['language'] ) ) );
        // Retrieve the Freemius license key
        $license_key = ezhp_horoscope_get_license_key();
        // Retrieve the activation GUID from the WordPress options table
        $activation_guid = get_option( 'ezhp_horoscope_activation_guid', '' );
        $top_headline_level = sanitize_text_field( strtolower( trim( $atts['top_headline_level'] ) ) );
        $background_color = get_option( 'ezhp_horoscope_background_color', '' );
        $include_backlink = get_option( 'ezhp_horoscope_include_backlink', false );
        $font = get_option( 'ezhp_horoscope_font', 'Arial' );
        $font_size = get_option( 'ezhp_horoscope_font_size', '14px' );
        $font_color = get_option( 'ezhp_horoscope_font_color', '#000000' );
        // Base iframe URL
        $iframe_url = 'https://www.enneagramzoom.com/syndicated/chinesehoroscope';
        //chinesehoroscope
        // Build query parameters
        $query_params = array(
            'language'       => $language,
            'activationGuid' => $activation_guid,
            'licenseKey'     => $license_key,
            'color'          => $background_color,
            'font'           => $font,
            'fontSize'       => $font_size,
            'fontColor'      => $font_color,
        );
        // Append all query parameters to the URL
        foreach ( $query_params as $key => $value ) {
            if ( !empty( $value ) ) {
                $iframe_url = add_query_arg( $key, urlencode( $value ), $iframe_url );
            }
        }
        // Return the iframe HTML
        return '<div id="ez-iframe-container" style="overflow: hidden;"><iframe id="syndicated-horoscope-iframe" src="' . esc_url( $iframe_url ) . '" width="100%"  frameborder="0"></iframe></div>';
    }

    //End Chinese Horoscope Block
}
//Block code
add_action( 'init', function () {
    $block_path = __DIR__ . '/build/blocks/daily-horoscope';
    if ( file_exists( $block_path . '/block.json' ) ) {
        // ✅ Register shared stylesheet for both frontend & editor preview iframe
        wp_register_style(
            'ezhoroscope-daily-horoscope-style',
            plugins_url( 'build/blocks/daily-horoscope/style-index.css', __FILE__ ),
            [],
            filemtime( $block_path . '/style-index.css' )
        );
        register_block_type( $block_path, [
            'style'         => 'ezhoroscope-daily-horoscope-style',
            'editor_style'  => 'ezhoroscope-daily-horoscope-style',
            'editor_script' => 'ezhoroscope-daily-horoscope-editor',
        ] );
        //error_log('✅ Registered ezhoroscope/daily-horoscope block with editor script');
    } else {
        //error_log('❌ Missing block.json in ' . $block_path);
    }
} );
//Includes code so only premium users have access to professional daily version of horoscope
add_action( 'enqueue_block_editor_assets', function () {
    $handle = 'ezhoroscope-daily-horoscope-editor';
    $path = plugin_dir_path( __FILE__ ) . 'build/blocks/daily-horoscope/index.js';
    $url = plugins_url( 'build/blocks/daily-horoscope/index.js', __FILE__ );
    if ( file_exists( $path ) ) {
        wp_enqueue_script(
            $handle,
            $url,
            [
                'wp-blocks',
                'wp-element',
                'wp-editor',
                'wp-components',
                'wp-i18n'
            ],
            filemtime( $path ),
            true
        );
        // Premium-only: Pass premium flag to block editor (stripped from free version)
        if ( function_exists( 'ezhp_fs' ) && ezhp_fs()->can_use_premium_code__premium_only() ) {
            wp_localize_script( $handle, 'ezhpBlockData', array(
                'isPremium' => true,
            ) );
        }
        //error_log('✅ Enqueued Daily Horoscope block editor script');
    } else {
        //error_log('❌ Missing Daily Horoscope block editor script');
    }
} );
// Register Monthly Horoscope Block.
add_action( 'init', function () {
    $ezhp_block_path = __DIR__ . '/build/blocks/monthly-horoscope';
    if ( file_exists( $ezhp_block_path . '/block.json' ) ) {
        // Register shared stylesheet for both frontend & editor preview iframe.
        wp_register_style(
            'ezhoroscope-monthly-horoscope-style',
            plugins_url( 'build/blocks/monthly-horoscope/style-index.css', __FILE__ ),
            array(),
            ( file_exists( $ezhp_block_path . '/style-index.css' ) ? filemtime( $ezhp_block_path . '/style-index.css' ) : EZHP_PLUGIN_VERSION )
        );
        register_block_type( $ezhp_block_path, array(
            'style'         => 'ezhoroscope-monthly-horoscope-style',
            'editor_style'  => 'ezhoroscope-monthly-horoscope-style',
            'editor_script' => 'ezhoroscope-monthly-horoscope-editor',
        ) );
    }
} );
// Enqueue Monthly Horoscope Block Editor Script.
add_action( 'enqueue_block_editor_assets', function () {
    $ezhp_handle = 'ezhoroscope-monthly-horoscope-editor';
    $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/monthly-horoscope/index.js';
    $ezhp_url = plugins_url( 'build/blocks/monthly-horoscope/index.js', __FILE__ );
    if ( file_exists( $ezhp_path ) ) {
        wp_enqueue_script(
            $ezhp_handle,
            $ezhp_url,
            array(
                'wp-blocks',
                'wp-element',
                'wp-editor',
                'wp-components',
                'wp-i18n',
                'wp-server-side-render'
            ),
            filemtime( $ezhp_path ),
            true
        );
    }
} );
// Enqueue Monthly Horoscope Frontend Script.
add_action( 'wp_enqueue_scripts', function () {
    if ( has_block( 'ezhoroscope/monthly-horoscope' ) ) {
        $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/monthly-horoscope/frontend.js';
        $ezhp_url = plugins_url( 'build/blocks/monthly-horoscope/frontend.js', __FILE__ );
        if ( file_exists( $ezhp_path ) ) {
            wp_enqueue_script(
                'ezhoroscope-monthly-horoscope-frontend',
                $ezhp_url,
                array(),
                filemtime( $ezhp_path ),
                true
            );
        }
    }
} );
add_action( 'init', function () {
    //error_log('✅ ezhoroscope block registration running'); // debug marker
    $block_path = __DIR__ . '/build/blocks/weekly-horoscope';
    if ( file_exists( $block_path . '/block.json' ) ) {
        // ✅ Register shared stylesheet for both frontend & editor preview iframe
        wp_register_style(
            'ezhoroscope-weekly-horoscope-style',
            plugins_url( 'build/blocks/weekly-horoscope/style-index.css', __FILE__ ),
            [],
            filemtime( $block_path . '/style-index.css' )
        );
        register_block_type( $block_path, [
            'style'           => 'ezhoroscope-weekly-horoscope-style',
            'editor_style'    => 'ezhoroscope-weekly-horoscope-style',
            'editor_script'   => 'ezhoroscope-weekly-horoscope-editor',
            'render_callback' => function ( $attributes, $content ) {
                ob_start();
                include __DIR__ . '/build/blocks/weekly-horoscope/render.php';
                return ob_get_clean();
            },
        ] );
        //error_log('✅ Registered ezhoroscope/daily-horoscope block with editor script');
    } else {
        //error_log('❌ Missing block.json in ' . $block_path);
    }
} );
add_action( 'enqueue_block_editor_assets', function () {
    $handle = 'ezhoroscope-weekly-horoscope-editor';
    $path = plugin_dir_path( __FILE__ ) . 'build/blocks/weekly-horoscope/index.js';
    $url = plugins_url( 'build/blocks/weekly-horoscope/index.js', __FILE__ );
    if ( file_exists( $path ) ) {
        wp_enqueue_script(
            $handle,
            $url,
            [
                'wp-blocks',
                'wp-element',
                'wp-editor',
                'wp-components',
                'wp-i18n'
            ],
            filemtime( $path ),
            true
        );
        // ADD THIS HERE - Pass default images URL to JavaScript
        wp_localize_script( $handle, 'ezWeeklyHoroscopeData', [
            'defaultImagesUrl' => plugins_url( 'assets/images/days-of-week/', __FILE__ ),
        ] );
    }
} );
// Professional Weekly Horoscope Block
add_action( 'init', function () {
    $ezhp_block_path = __DIR__ . '/build/blocks/weekly-horoscope-professional';
    if ( file_exists( $ezhp_block_path . '/block.json' ) ) {
        // Register shared stylesheet for both frontend & editor preview iframe
        wp_register_style(
            'ezhoroscope-weekly-horoscope-professional-style',
            plugins_url( 'build/blocks/weekly-horoscope-professional/style-index.css', __FILE__ ),
            [],
            ( file_exists( $ezhp_block_path . '/style-index.css' ) ? filemtime( $ezhp_block_path . '/style-index.css' ) : EZHP_PLUGIN_VERSION )
        );
        register_block_type( $ezhp_block_path, [
            'style'           => 'ezhoroscope-weekly-horoscope-professional-style',
            'editor_style'    => 'ezhoroscope-weekly-horoscope-professional-style',
            'editor_script'   => 'ezhoroscope-weekly-horoscope-professional-editor',
            'render_callback' => function ( $attributes, $content ) {
                ob_start();
                include __DIR__ . '/build/blocks/weekly-horoscope-professional/render.php';
                return ob_get_clean();
            },
        ] );
    }
} );
add_action( 'enqueue_block_editor_assets', function () {
    $ezhp_handle = 'ezhoroscope-weekly-horoscope-professional-editor';
    $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/weekly-horoscope-professional/index.js';
    $ezhp_url = plugins_url( 'build/blocks/weekly-horoscope-professional/index.js', __FILE__ );
    if ( file_exists( $ezhp_path ) ) {
        wp_enqueue_script(
            $ezhp_handle,
            $ezhp_url,
            [
                'wp-blocks',
                'wp-element',
                'wp-editor',
                'wp-components',
                'wp-i18n',
                'wp-server-side-render'
            ],
            filemtime( $ezhp_path ),
            true
        );
        // Pass zodiac images URL and premium status to JavaScript
        $ezhp_localize_data = [
            'zodiacImagesUrl' => plugins_url( 'assets/images/zodiac/', __FILE__ ),
        ];
        // Premium-only: Pass premium flag to block editor (stripped from free version)
        if ( function_exists( 'ezhp_fs' ) && ezhp_fs()->can_use_premium_code__premium_only() ) {
            $ezhp_localize_data['isPremium'] = true;
        }
        wp_localize_script( $ezhp_handle, 'ezWeeklyHoroscopeProfessionalData', $ezhp_localize_data );
    }
} );
// Zodiac Profile Block
add_action( 'init', function () {
    $block_path = __DIR__ . '/build/blocks/zodiac-profile';
    if ( file_exists( $block_path . '/block.json' ) ) {
        register_block_type( $block_path );
    }
} );
add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_script(
        'ezhp-zodiac-profile-frontend',
        plugin_dir_url( __FILE__ ) . 'build/blocks/zodiac-profile/frontend.js',
        ['wp-element'],
        filemtime( plugin_dir_path( __FILE__ ) . 'build/blocks/zodiac-profile/frontend.js' ),
        true
    );
} );
add_action( 'enqueue_block_assets', function () {
    if ( !is_admin() ) {
        wp_enqueue_script(
            'ezhp-zodiac-profile-frontend',
            plugin_dir_url( __FILE__ ) . 'build/blocks/zodiac-profile/frontend.js',
            ['wp-element'],
            filemtime( plugin_dir_path( __FILE__ ) . 'build/blocks/zodiac-profile/frontend.js' ),
            true
        );
    }
} );
// Enqueue daily-horoscope frontend.js in editor for CTA preview
add_action( 'enqueue_block_editor_assets', function () {
    $ezhp_frontend_path = plugin_dir_path( __FILE__ ) . 'build/blocks/daily-horoscope/frontend.js';
    if ( file_exists( $ezhp_frontend_path ) ) {
        wp_enqueue_script(
            'ezhp-daily-horoscope-frontend',
            plugin_dir_url( __FILE__ ) . 'build/blocks/daily-horoscope/frontend.js',
            [],
            filemtime( $ezhp_frontend_path ),
            true
        );
    }
} );
// Chinese Horoscope Block
add_action( 'init', function () {
    $block_path = __DIR__ . '/build/blocks/chinese-horoscope';
    if ( file_exists( $block_path . '/block.json' ) ) {
        register_block_type( $block_path );
    }
} );
add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_script(
        'ezhp-chinese-horoscope-frontend',
        plugin_dir_url( __FILE__ ) . 'build/blocks/chinese-horoscope/frontend.js',
        ['wp-element'],
        filemtime( plugin_dir_path( __FILE__ ) . 'build/blocks/chinese-horoscope/frontend.js' ),
        true
    );
} );
add_action( 'enqueue_block_assets', function () {
    if ( !is_admin() ) {
        wp_enqueue_script(
            'ezhp-chinese-horoscope-frontend',
            plugin_dir_url( __FILE__ ) . 'build/blocks/chinese-horoscope/frontend.js',
            ['wp-element'],
            filemtime( plugin_dir_path( __FILE__ ) . 'build/blocks/chinese-horoscope/frontend.js' ),
            true
        );
    }
} );
add_action( 'enqueue_block_editor_assets', function () {
    $handle = 'ezhoroscope-daily-horoscope-sections-editor';
    $path = plugin_dir_path( __FILE__ ) . 'build/blocks/daily-horoscope-sections/index.js';
    $url = plugins_url( 'build/blocks/daily-horoscope-sections/index.js', __FILE__ );
    if ( file_exists( $path ) ) {
        wp_enqueue_script(
            $handle,
            $url,
            [
                'wp-blocks',
                'wp-element',
                'wp-editor',
                'wp-components',
                'wp-i18n'
            ],
            filemtime( $path ),
            true
        );
    }
} );
// Register the "Daily Horoscope Sections" block
add_action( 'init', function () {
    $block_path = __DIR__ . '/build/blocks/daily-horoscope-sections';
    if ( file_exists( $block_path . '/block.json' ) ) {
        register_block_type( $block_path, [
            'editor_script'   => 'ezhoroscope-daily-horoscope-sections-editor',
            'render_callback' => function ( $attributes, $content ) {
                ob_start();
                include __DIR__ . '/build/blocks/daily-horoscope-sections/render.php';
                return ob_get_clean();
            },
        ] );
    }
} );
// Numerology Block
add_action( 'init', function () {
    $block_path = __DIR__ . '/build/blocks/numerology';
    if ( file_exists( $block_path . '/block.json' ) ) {
        register_block_type( $block_path );
    }
} );
add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_script(
        'ezhp-numerology-frontend',
        plugin_dir_url( __FILE__ ) . 'build/blocks/numerology/frontend.js',
        ['wp-element'],
        filemtime( plugin_dir_path( __FILE__ ) . 'build/blocks/numerology/frontend.js' ),
        true
    );
} );
add_action( 'enqueue_block_assets', function () {
    if ( !is_admin() ) {
        wp_enqueue_script(
            'ezhp-numerology-frontend',
            plugin_dir_url( __FILE__ ) . 'build/blocks/numerology/frontend.js',
            ['wp-element'],
            filemtime( plugin_dir_path( __FILE__ ) . 'build/blocks/numerology/frontend.js' ),
            true
        );
    }
} );
// Zodiac Compatibility Block
add_action( 'init', function () {
    $ezhp_block_path = __DIR__ . '/build/blocks/zodiac-compatibility';
    if ( file_exists( $ezhp_block_path . '/block.json' ) ) {
        register_block_type( $ezhp_block_path );
    }
} );
/**
 * Tarot JS + global variables (only register, enqueue from block render.php)
 */
add_action( 'wp_enqueue_scripts', function () {
    // Register (not enqueue) main tarot script
    wp_register_script(
        'ezhp-tarot-js',
        plugins_url( 'js/tarot.js', __FILE__ ),
        array(),
        EZHP_PLUGIN_VERSION,
        true
    );
    // Keep original global variables (tarot.js depends on these)
    wp_localize_script( 'ezhp-tarot-js', 'EZHP_TAROT_DECK_BASE', plugin_dir_url( __FILE__ ) . 'js/tarot/decks/' );
    wp_localize_script( 'ezhp-tarot-js', 'EZHP_TAROT_IMAGE_BASE', plugin_dir_url( __FILE__ ) . 'images/tarot/' );
} );
// Tarot 3-Card Spread Block
add_action( 'init', function () {
    $block_path = __DIR__ . '/build/blocks/tarot-3card-spread';
    if ( file_exists( $block_path . '/block.json' ) ) {
        register_block_type( $block_path );
    }
} );
// Horoscope Summaries Block
add_action( 'init', function () {
    $ezhp_block_path = __DIR__ . '/build/blocks/horoscope-summaries';
    if ( file_exists( $ezhp_block_path . '/block.json' ) ) {
        // Shared stylesheet for both frontend & editor preview iframe
        wp_register_style(
            'ezhoroscope-horoscope-summaries-style',
            plugins_url( 'build/blocks/horoscope-summaries/style-index.css', __FILE__ ),
            [],
            filemtime( $ezhp_block_path . '/style-index.css' )
        );
        register_block_type( $ezhp_block_path, [
            'style'           => 'ezhoroscope-horoscope-summaries-style',
            'editor_style'    => 'ezhoroscope-horoscope-summaries-style',
            'editor_script'   => 'ezhoroscope-horoscope-summaries-editor',
            'render_callback' => function ( $attributes, $content ) {
                ob_start();
                include __DIR__ . '/build/blocks/horoscope-summaries/render.php';
                return ob_get_clean();
            },
        ] );
        //error_log('✅ Registered ezhoroscope/horoscope-summaries block');
    }
} );
add_action( 'enqueue_block_editor_assets', function () {
    $ezhp_handle = 'ezhoroscope-horoscope-summaries-editor';
    $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/horoscope-summaries/index.js';
    $ezhp_url = plugins_url( 'build/blocks/horoscope-summaries/index.js', __FILE__ );
    if ( file_exists( $ezhp_path ) ) {
        wp_enqueue_script(
            $ezhp_handle,
            $ezhp_url,
            [
                'wp-blocks',
                'wp-element',
                'wp-editor',
                'wp-components',
                'wp-i18n'
            ],
            filemtime( $ezhp_path ),
            true
        );
        //error_log('✅ Enqueued Horoscope Summaries block editor script');
    }
} );
// Birth Chart Block
add_action( 'init', function () {
    $ezhp_block_path = __DIR__ . '/build/blocks/birth-chart';
    if ( file_exists( $ezhp_block_path . '/block.json' ) ) {
        register_block_type( $ezhp_block_path );
    }
} );
add_action( 'wp_enqueue_scripts', function () {
    $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/birth-chart/frontend.js';
    if ( file_exists( $ezhp_path ) ) {
        wp_enqueue_script(
            'ezhp-birth-chart-frontend',
            plugin_dir_url( __FILE__ ) . 'build/blocks/birth-chart/frontend.js',
            ['wp-element'],
            filemtime( $ezhp_path ),
            true
        );
    }
} );
add_action( 'enqueue_block_assets', function () {
    if ( !is_admin() ) {
        $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/birth-chart/frontend.js';
        if ( file_exists( $ezhp_path ) ) {
            wp_enqueue_script(
                'ezhp-birth-chart-frontend',
                plugin_dir_url( __FILE__ ) . 'build/blocks/birth-chart/frontend.js',
                ['wp-element'],
                filemtime( $ezhp_path ),
                true
            );
        }
    }
} );
// Synastry Chart Block
add_action( 'init', function () {
    $ezhp_block_path = __DIR__ . '/build/blocks/synastry-chart';
    if ( file_exists( $ezhp_block_path . '/block.json' ) ) {
        register_block_type( $ezhp_block_path );
    }
} );
add_action( 'wp_enqueue_scripts', function () {
    $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/synastry-chart/frontend.js';
    if ( file_exists( $ezhp_path ) ) {
        wp_enqueue_script(
            'ezhp-synastry-chart-frontend',
            plugin_dir_url( __FILE__ ) . 'build/blocks/synastry-chart/frontend.js',
            ['wp-element'],
            filemtime( $ezhp_path ),
            true
        );
    }
} );
add_action( 'enqueue_block_assets', function () {
    if ( !is_admin() ) {
        $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/synastry-chart/frontend.js';
        if ( file_exists( $ezhp_path ) ) {
            wp_enqueue_script(
                'ezhp-synastry-chart-frontend',
                plugin_dir_url( __FILE__ ) . 'build/blocks/synastry-chart/frontend.js',
                ['wp-element'],
                filemtime( $ezhp_path ),
                true
            );
        }
    }
} );
// Moon Phase Block
add_action( 'init', function () {
    $ezhp_block_path = __DIR__ . '/build/blocks/moon-phase';
    if ( file_exists( $ezhp_block_path . '/block.json' ) ) {
        register_block_type( $ezhp_block_path );
    }
} );
add_action( 'enqueue_block_editor_assets', function () {
    $ezhp_handle = 'ezhoroscope-moon-phase-editor';
    $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/moon-phase/index.js';
    $ezhp_url = plugins_url( 'build/blocks/moon-phase/index.js', __FILE__ );
    if ( file_exists( $ezhp_path ) ) {
        wp_enqueue_script(
            $ezhp_handle,
            $ezhp_url,
            [
                'wp-blocks',
                'wp-element',
                'wp-editor',
                'wp-components',
                'wp-i18n',
                'wp-server-side-render'
            ],
            filemtime( $ezhp_path ),
            true
        );
    }
    // Also load frontend.js in editor
    $ezhp_frontend_path = plugin_dir_path( __FILE__ ) . 'build/blocks/moon-phase/frontend.js';
    if ( file_exists( $ezhp_frontend_path ) ) {
        wp_enqueue_script(
            'ezhp-moon-phase-frontend',
            plugin_dir_url( __FILE__ ) . 'build/blocks/moon-phase/frontend.js',
            [],
            filemtime( $ezhp_frontend_path ),
            true
        );
    }
} );
add_action( 'wp_enqueue_scripts', function () {
    if ( has_block( 'ezhoroscope/moon-phase' ) || ezhp_has_moon_phase_shortcode() ) {
        $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/moon-phase/frontend.js';
        if ( file_exists( $ezhp_path ) ) {
            wp_enqueue_script(
                'ezhp-moon-phase-frontend',
                plugin_dir_url( __FILE__ ) . 'build/blocks/moon-phase/frontend.js',
                [],
                filemtime( $ezhp_path ),
                true
            );
        }
    }
} );
add_action( 'enqueue_block_assets', function () {
    if ( !is_admin() && (has_block( 'ezhoroscope/moon-phase' ) || ezhp_has_moon_phase_shortcode()) ) {
        $ezhp_path = plugin_dir_path( __FILE__ ) . 'build/blocks/moon-phase/frontend.js';
        if ( file_exists( $ezhp_path ) ) {
            wp_enqueue_script(
                'ezhp-moon-phase-frontend',
                plugin_dir_url( __FILE__ ) . 'build/blocks/moon-phase/frontend.js',
                [],
                filemtime( $ezhp_path ),
                true
            );
        }
    }
} );
/**
 * Check if current post/page has moon phase shortcode
 *
 * @return bool
 */
function ezhp_has_moon_phase_shortcode() {
    global $post;
    if ( !is_a( $post, 'WP_Post' ) ) {
        return false;
    }
    return has_shortcode( $post->post_content, 'ezhp_ezmoonphase' );
}

/**
 * Moon Phase Shortcode
 * Usage: [ezhp_ezmoonphase language="en" image_size="100"]
 */
add_shortcode( 'ezhp_ezmoonphase', 'ezhp_moon_phase_shortcode_render' );
function ezhp_moon_phase_shortcode_render(  $atts  ) {
    $ezhp_atts = shortcode_atts( array(
        'language'   => 'en',
        'image_size' => 100,
    ), $atts, 'ezhp_ezmoonphase' );
    $ezhp_attributes = array(
        'language'            => sanitize_text_field( $ezhp_atts['language'] ),
        'imageSize'           => absint( $ezhp_atts['image_size'] ),
        'newMoonImage'        => '',
        'waxingCrescentImage' => '',
        'firstQuarterImage'   => '',
        'waxingGibbousImage'  => '',
        'fullMoonImage'       => '',
        'waningGibbousImage'  => '',
        'lastQuarterImage'    => '',
        'waningCrescentImage' => '',
    );
    ob_start();
    $attributes = $ezhp_attributes;
    include plugin_dir_path( __FILE__ ) . 'build/blocks/moon-phase/render.php';
    return ob_get_clean();
}
