<?php
/**
 * Flavor – Restaurant Menu
 *
 * Review request admin notice.
 *
 * Politely invites engaged users to leave a review on WordPress.org once they
 * have published at least five dishes. The notice appears only on Flavor admin
 * screens and can be snoozed for two weeks ("Maybe later") or dismissed for good.
 *
 * Identical in the Free and Pro plugins — both point to the public WordPress.org
 * listing, the only place where Flavor collects public reviews.
 *
 * @package    FlavorMenu
 * @copyright  Copyright (c) 2024 Flavor Studio
 * @license    GPL-2.0-or-later
 * @link       https://flavor-menu.com
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Flavor_Review_Notice {

    /** Public WordPress.org review form (pre-selects 5 stars). */
    const REVIEW_URL = 'https://wordpress.org/support/plugin/flavor-restaurant-menu/reviews/?rate=5#new-post';

    /** Minimum number of published dishes before the notice appears. */
    const MIN_DISHES = 5;

    /** How long "Maybe later" hides the notice, in days. */
    const SNOOZE_DAYS = 14;

    const OPT_DISMISSED = 'flavor_review_notice_dismissed';
    const OPT_SNOOZE    = 'flavor_review_notice_snooze';
    const ACTION        = 'flavor_review_notice';

    public static function init() {
        add_action( 'admin_notices', array( __CLASS__, 'maybe_render' ) );
        add_action( 'admin_post_' . self::ACTION, array( __CLASS__, 'handle_action' ) );
    }

    /**
     * Render the notice only when every condition is met.
     */
    public static function maybe_render() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        // Permanently dismissed?
        if ( get_option( self::OPT_DISMISSED ) ) {
            return;
        }
        // Snoozed and still inside the snooze window?
        $snooze = (int) get_option( self::OPT_SNOOZE, 0 );
        if ( $snooze && time() < $snooze ) {
            return;
        }
        // Only on Flavor screens (dish list, dish editor, plugin submenu pages).
        if ( ! self::is_flavor_screen() ) {
            return;
        }
        // Success moment: at least MIN_DISHES published dishes.
        $counts = wp_count_posts( 'flavor_menu_item' );
        if ( ! $counts || (int) $counts->publish < self::MIN_DISHES ) {
            return;
        }

        self::render();
    }

    /**
     * True on the dish list, the dish editor and any Flavor submenu page.
     */
    private static function is_flavor_screen() {
        if ( ! function_exists( 'get_current_screen' ) ) {
            return false;
        }
        $screen = get_current_screen();
        return ( $screen && isset( $screen->post_type ) && 'flavor_menu_item' === $screen->post_type );
    }

    private static function render() {
        $review_url  = wp_nonce_url( admin_url( 'admin-post.php?action=' . self::ACTION . '&do=review' ), self::ACTION );
        $later_url   = wp_nonce_url( admin_url( 'admin-post.php?action=' . self::ACTION . '&do=later' ), self::ACTION );
        $dismiss_url = wp_nonce_url( admin_url( 'admin-post.php?action=' . self::ACTION . '&do=dismiss' ), self::ACTION );
        ?>
        <div class="notice notice-info">
            <p style="font-size:13px;line-height:1.6;margin:.7em 0;">
                <strong><?php esc_html_e( 'Enjoying Flavor?', 'flavor-restaurant-menu' ); ?></strong>
                <?php esc_html_e( 'A quick, honest review on WordPress.org helps us a lot — and helps other restaurants discover the plugin. It only takes a minute.', 'flavor-restaurant-menu' ); ?>
            </p>
            <p style="margin:.7em 0;">
                <a href="<?php echo esc_url( $review_url ); ?>" class="button button-primary">
                    <?php esc_html_e( 'Sure, leave a review →', 'flavor-restaurant-menu' ); ?>
                </a>
                <a href="<?php echo esc_url( $later_url ); ?>" class="button button-secondary" style="margin-left:6px;">
                    <?php esc_html_e( 'Maybe later', 'flavor-restaurant-menu' ); ?>
                </a>
                <a href="<?php echo esc_url( $dismiss_url ); ?>" style="margin-left:12px;color:#646970;text-decoration:none;line-height:28px;">
                    <?php esc_html_e( 'I already did / No thanks', 'flavor-restaurant-menu' ); ?>
                </a>
            </p>
        </div>
        <?php
    }

    /**
     * Handle the three notice actions: review / later / dismiss.
     */
    public static function handle_action() {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html__( 'You do not have permission to do this.', 'flavor-restaurant-menu' ) );
        }
        check_admin_referer( self::ACTION );

        $do = isset( $_GET['do'] ) ? sanitize_key( wp_unslash( $_GET['do'] ) ) : '';

        if ( 'later' === $do ) {
            update_option( self::OPT_SNOOZE, time() + ( self::SNOOZE_DAYS * DAY_IN_SECONDS ) );
        } elseif ( 'review' === $do ) {
            update_option( self::OPT_DISMISSED, 1 );
            // External destination is intentional (public WordPress.org review form).
            // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
            wp_redirect( self::REVIEW_URL );
            exit;
        } else {
            update_option( self::OPT_DISMISSED, 1 );
        }

        $back = wp_get_referer();
        if ( ! $back ) {
            $back = admin_url( 'edit.php?post_type=flavor_menu_item' );
        }
        wp_safe_redirect( $back );
        exit;
    }
}
