<?php
if (!defined('ABSPATH')) {
    exit;
}

// Review Action For Admin
add_action('admin_notices', 'gamadsmbai_review_notice');

function gamadsmbai_review_notice()
{
    if (!current_user_can('manage_options')) {
        return;
    }

    if (get_option('gamadsmbai_review_dismissed')) {
        return;
    }

    global $wpdb;
    $gamadsmbai_table_main = $wpdb->prefix . 'gamadsmbai_ads';
    // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter -- $gamadsmbai_table_main derived from $wpdb->prefix and escaped; safe internal query; No need of cache
    $gamadsmbai_total_ads = $wpdb->get_var("SELECT count(*) FROM $gamadsmbai_table_main");
    if (!$gamadsmbai_total_ads) {
        return;
    }
?>
    <div class="notice notice-info is-dismissible gamadsmbai-review-notice">
        <p>
            ⭐ <strong>Thank you for using GAM Ad Inserter.</strong><br>
            If it’s adding value to your site, we’d truly appreciate a quick review 🚀.
            Your feedback helps us keep building better tools for publishers.
            We appreciate your support.
        </p>
        <p>
            <a href="https://wordpress.org/support/plugin/gam-ads-ad-manager-and-ad-exchange/reviews/"
                target="_blank"
                class="button button-primary">
                Leave a Review
            </a>
        </p>
    </div>
<?php
}

add_action('admin_enqueue_scripts', function () {
    wp_add_inline_script('jquery', "
        jQuery(document).on('click', '.gamadsmbai-review-notice .notice-dismiss', function() {
            jQuery.post(ajaxurl, {
                action: 'gamadsmbai_dismiss_review_notice'
            });
        });
    ");
});

add_action('wp_ajax_gamadsmbai_dismiss_review_notice', function () {
    update_option('gamadsmbai_review_dismissed', 1);
    wp_die();
});
