<?php

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

add_action( 'admin_post_reeid_tools_map_repair', 'reeid_tools_map_repair_handler' );

function reeid_tools_map_repair_handler() {

    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'Unauthorized', 403 );
    }

    check_admin_referer( 'reeid_tools_tab_action', 'reeid_tools_tab_nonce' );

    $repaired = 0;

    // Repair ALL maps
    if ( isset( $_POST['reeid_maprepair_all'] ) ) {

     $sources = get_posts(
    array(
        'post_type'      => 'any',
        'post_status'    => 'any',
        'posts_per_page' => -1,
        // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- required admin repair scan across translation source posts
        'meta_key'       => '_reeid_translation_map',
    )
);

        foreach ( $sources as $post ) {

            $map = (array) get_post_meta( $post->ID, '_reeid_translation_map', true );
            if ( empty( $map ) ) {
                continue;
            }

            // Normalize + re-save (forces repair)
            update_post_meta( $post->ID, '_reeid_translation_map', $map );
            $repaired++;
        }

    // Repair SINGLE map
    } else {

        $post_id = isset( $_POST['reeid_maprepair_post_id'] )
            ? absint( $_POST['reeid_maprepair_post_id'] )
            : 0;

        if ( $post_id ) {
            $map = (array) get_post_meta( $post_id, '_reeid_translation_map', true );
            if ( ! empty( $map ) ) {
                update_post_meta( $post_id, '_reeid_translation_map', $map );
                $repaired = 1;
            }
        }
    }

    wp_safe_redirect(
        add_query_arg(
            array(
                'page'  => 'reeid-translate-settings',
                'tab'   => 'tools',
                'fixed' => $repaired,
            ),
            admin_url( 'admin.php' )
        )
    );
    exit;
}
add_action( 'admin_post_reeid_tools', function () {

    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__( 'Unauthorized.', 'reeid-translate' ) );
    }

    // Verify nonce (Tools tab form)
    if (
        ! isset( $_POST['reeid_tools_tab_nonce'] ) ||
        ! wp_verify_nonce(
            sanitize_text_field( wp_unslash( $_POST['reeid_tools_tab_nonce'] ) ),
            'reeid_tools_tab_action'
        )
    ) {
        wp_die( esc_html__( 'Security check failed.', 'reeid-translate' ) );
    }

   /* ===============================
   PURGE ALL TRANSLATION CACHE
   =============================== */

$purge = filter_input( INPUT_POST, 'reeid_purge_all_cache', FILTER_UNSAFE_RAW );

if ( ! empty( $purge ) ) {

    global $wpdb;

    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- DELETE operation, caching not applicable
    $wpdb->query(
        "DELETE FROM {$wpdb->options}
         WHERE option_name LIKE '_transient_reeid_%'
            OR option_name LIKE '_transient_timeout_reeid_%'"
    );

    do_action( 'reeid_purge_translation_cache' );

    wp_safe_redirect(
        add_query_arg(
            array(
                'page'   => 'reeid-translate-settings',
                'tab'    => 'tools',
                'purged' => 1,
            ),
            admin_url( 'admin.php' )
        )
    );

    exit;
}

    // Default redirect (no action matched)
    wp_safe_redirect(
        admin_url( 'admin.php?page=reeid-translate-settings&tab=tools' )
    );

    exit;
});