admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('addresswise_nonce'), // Generate nonce )); } } // Enqueue the JavaScript file for address autofill on the checkout page add_action('wp_enqueue_scripts', 'addresswise_enqueue_scripts'); require_once plugin_dir_path(__FILE__) . 'settings/settings.php'; // AJAX action handler to get client token add_action('wp_ajax_get_client_token', 'addresswise_get_client_token'); add_action('wp_ajax_nopriv_get_client_token', 'addresswise_get_client_token'); // Allow non-logged-in users to use the AJAX action function addresswise_get_client_token() { // Verify nonce if ( ! check_ajax_referer( 'addresswise_nonce', 'nonce', false ) ) { wp_send_json_error( 'Invalid nonce' ); } // Check if WordPress is loaded if ( ! function_exists( 'get_option' ) || ! function_exists( 'wp_json_encode' ) ) { status_header(500); wp_send_json_error(array('error' => 'WordPress environment is not properly loaded.')); wp_die(); } // Retrieve the client token from options $client_token = get_option('addresswise_settings_input_field'); // Check if the client token is retrieved successfully if ( ! $client_token ) { status_header(500); wp_send_json_error(array('error' => 'Client token not found.')); wp_die(); } // Return the token as JSON echo wp_json_encode(array('client_token' => esc_html($client_token))); wp_die(); }