<?php
/**
 * Admin: "Settings > Pages" business logic
 *
 *
 */
if (!defined('ABSPATH')) {
    exit;
}
if (!current_user_can('manage_options')) {
    return;
}
$redirect_url = admin_url('admin.php?page=chama-settings&tab=pages');
// Create account disconnect URL
$regenerate_pages_url = esc_url(add_query_arg(
    array(
        'chama_nonce' => wp_create_nonce('chama_regenerate_nonce'),
        'regenerate' => true,
    ),
    $redirect_url
));

// Page regeneration
if ( isset($_GET['chama_nonce']) && isset($_GET['regenerate']) ) {
    $chama_nonce = sanitize_key( wp_unslash( $_GET['chama_nonce'] ) );
    $regenerate = sanitize_text_field( wp_unslash( $_GET['regenerate'] ) );

    if ( wp_verify_nonce( $chama_nonce, 'chama_regenerate_nonce' ) && ! empty( $regenerate ) ) {
        $Chama_Plugin = new Chama_Plugin;
        $Chama_Plugin->chama_generate_pages();

        $message = 'The pages have been successfully regenerated';
        echo '<div class="notice-success notice"><p>', esc_html( $message ), '</p></div>';
    }
}
