<?php
/**
 * Getting Started Help Notice
 *
 * @package Author Portfolio
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

/**
 * Render Welcome Admin Notice
 */
function author_portfolio_general_admin_notice() {
    if ( ! current_user_can( 'edit_theme_options' ) ) {
        return;
    }

    $screen = get_current_screen();
    if ( $screen && ( 'appearance_page_advanced-import' === $screen->id || 'appearance_page_author-portfolio-getting-started' === $screen->id ) ) {
        return;
    }

    if ( get_transient( 'author_portfolio_welcome_notice_dismissed' ) ) {
        return;
    }

    $dismiss_url = wp_nonce_url(
        add_query_arg( 'author_portfolio_action', 'dismiss_welcome_notice' ),
        'author_portfolio_dismiss_notice_nonce',
        '_author_portfolio_notice_nonce'
    );
    ?>
    <div class="notice notice-info author-portfolio-welcome-notice is-dismissible">
        <div class="author-portfolio-notice-wrapper">
            <div class="author-portfolio-notice-inner">
                <div class="notice-thumbnail-col">
                    <img src="<?php echo esc_url( get_theme_file_uri( 'assets/img/author-portfolio-pro-thumb.png' ) ); ?>" alt="<?php esc_attr_e( 'Author Portfolio Pro', 'author-portfolio' ); ?>">
                </div>
                <div class="notice-content-col">
                    <h3><?php esc_html_e( 'Thank you for installing the Author Portfolio WordPress Theme.', 'author-portfolio' ); ?></h3>
                    <p class="notice-desc">
                        <?php esc_html_e( 'Ready to create a stunning author website? Click the Install Starter Author Website Templates button, and you\'ll be redirected to our demo page to get started.', 'author-portfolio' ); ?>
                    </p>
                    <p>
                        <a class="author-portfolio-btn-get-started button button-primary author-portfolio-button-padding" href="#"><?php esc_html_e( 'Install Starter Author Website Templates', 'author-portfolio' ); ?></a>
                        <a target="_blank" href="<?php echo esc_url( 'https://rswpthemes.com/demo-page/?demo=author-portfolio-demo-selector' ); ?>" class="button button-highlight upgrade-to-pro button-primary"><?php esc_html_e( 'Live Demos', 'author-portfolio' ); ?></a>
                        <a href="<?php echo esc_url( admin_url( 'themes.php?page=author-portfolio-getting-started' ) ); ?>" class="button button-highlight btn-doc button-primary" style="color:#fff;"><span style="margin-top:4px;margin-right:5px;" class="dashicons dashicons-video-alt3"></span><?php esc_html_e( ' Getting Started Videos', 'author-portfolio' ); ?></a>
                        <a target="_blank" href="<?php echo esc_url( author_portfolio_utm_url( 'welcome_notice' ) ); ?>" class="button button-highlight upgrade-to-pro button-primary"><?php esc_html_e( 'Upgrade To Pro', 'author-portfolio' ); ?></a>
                        <a href="<?php echo esc_url( $dismiss_url ); ?>" style="text-decoration: none; float: right;"><?php esc_html_e( 'Dismiss Notice', 'author-portfolio' ); ?></a>
                    </p>
                </div>
            </div>
        </div>
    </div>
    <?php
}
add_action( 'admin_notices', 'author_portfolio_general_admin_notice' );

/**
 * Handle Notice Dismissal Action
 */
function author_portfolio_dismiss_welcome_notice() {
    if ( ! isset( $_GET['author_portfolio_action'] ) || 'dismiss_welcome_notice' !== $_GET['author_portfolio_action'] ) {
        return;
    }

    if ( ! current_user_can( 'edit_theme_options' ) ) {
        return;
    }

    $nonce = isset( $_GET['_author_portfolio_notice_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_author_portfolio_notice_nonce'] ) ) : '';

    if ( ! wp_verify_nonce( $nonce, 'author_portfolio_dismiss_notice_nonce' ) ) {
        return;
    }

    // Hide notice for 24 hours via Transient
    set_transient( 'author_portfolio_welcome_notice_dismissed', true, DAY_IN_SECONDS );

    wp_safe_redirect( remove_query_arg( array( 'author_portfolio_action', '_author_portfolio_notice_nonce' ) ) );
    exit;
}
add_action( 'admin_init', 'author_portfolio_dismiss_welcome_notice' );