/*
 Theme Name:   Woodmart Child
 Description:  Woodmart Child Theme
 Author:       XTemos
 Author URI:   http://xtemos.com
 Template:     woodmart
 Version:      1.0.0
 Text Domain:  woodmart
*/
/**
 * FIDAREX - Permanent WooCommerce Reviews & Empty Stars
 * One-time database update.
 */

add_action('admin_init', function () {

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

    // Run only once.
    $flag = 'fidarex_reviews_setup_v3';

    if ( get_option($flag) === 'done' ) {
        return;
    }

    global $wpdb;

    // 1. Enable WooCommerce product reviews.
    update_option('woocommerce_enable_reviews', 'yes');

    // 2. Enable star ratings.
    update_option('woocommerce_enable_review_rating', 'yes');

    // 3. Require rating when submitting a review.
    update_option('woocommerce_review_rating_required', 'yes');

    // 4. Enable empty star ratings in WoodMart product grids.
    $woodmart_options = get_option('xts-woodmart-options', array());

    if ( ! is_array($woodmart_options) ) {
        update_option(
            'fidarex_reviews_setup_v3_error',
            'WoodMart settings could not be read.'
        );
        return;
    }

    $woodmart_options['show_empty_star_rating'] = '1';

    update_option('xts-woodmart-options', $woodmart_options);

    // 5. Permanently enable comments for all existing products.
    $updated = $wpdb->query("
        UPDATE {$wpdb->posts}
        SET comment_status = 'open'
        WHERE post_type = 'product'
        AND post_status NOT IN ('trash', 'auto-draft')
        AND comment_status <> 'open'
    ");

    // Do not mark as completed if database update failed.
    if ( $updated === false ) {
        update_option(
            'fidarex_reviews_setup_v3_error',
            $wpdb->last_error
        );
        return;
    }

    // Clear product caches.
    $product_ids = $wpdb->get_col("
        SELECT ID
        FROM {$wpdb->posts}
        WHERE post_type = 'product'
        AND post_status NOT IN ('trash', 'auto-draft')
    ");

    foreach ( $product_ids as $product_id ) {
        clean_post_cache((int) $product_id);
    }

    // Mark the operation as completed permanently.
    update_option($flag, 'done', false);

    update_option(
        'fidarex_reviews_setup_v3_count',
        (int) $updated,
        false
    );

}, 20);


// Show execution result in WordPress dashboard.
add_action('admin_notices', function () {

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

    $error = get_option('fidarex_reviews_setup_v3_error');

    if ( $error ) {
        echo '<div class="notice notice-error"><p>';
        echo 'FIDAREX Reviews Error: ' . esc_html($error);
        echo '</p></div>';
        return;
    }

    $count = get_option('fidarex_reviews_setup_v3_count', null);

    if ( $count !== null ) {
        echo '<div class="notice notice-success"><p>';
        echo 'FIDAREX: Product reviews and empty star ratings have been enabled successfully. ';
        echo 'Updated products: ' . esc_html($count);
        echo '</p></div>';

        delete_option('fidarex_reviews_setup_v3_count');
    }

});