'POST', 'callback' => 'AdvancedSteamWidget_rest_refresh', 'permission_callback' => function() { return current_user_can('edit_theme_options'); }, 'args' => array( 'id' => array( 'required' => true, 'validate_callback' => function($v) { return is_numeric($v); }, 'sanitize_callback' => 'absint', ), 'profile_url' => array( 'required' => false, 'sanitize_callback' => 'sanitize_text_field', ), ), )); //block refresh endpoint - normalizes the URL and returns it so the editor can round-trip the canonical form back into the block attribute register_rest_route('advanced-steam-widget/v1', '/block-refresh', array( 'methods' => 'POST', 'callback' => 'AdvancedSteamWidget_rest_block_refresh', 'permission_callback' => function() { return current_user_can('edit_theme_options'); }, 'args' => array( 'id' => array( 'required' => true, //block ids are b_; keep the character class permissive but strip anything unexpected 'sanitize_callback' => function($v) { return preg_replace('/[^A-Za-z0-9_-]/', '', (string)$v); }, ), 'profile_url' => array( 'required' => false, 'sanitize_callback' => 'sanitize_text_field', ), ), )); //template list endpoint - lets the block's SelectControl reflect any theme overrides without hardcoding register_rest_route('advanced-steam-widget/v1', '/templates', array( 'methods' => 'GET', 'callback' => function() { return AdvancedSteamWidget_get_templates(); }, 'permission_callback' => function() { return current_user_can('edit_theme_options'); }, )); }); //classic widget refresh callback - refreshes cache and resets the recurring cron so the next scheduled fetch isn't imminent function AdvancedSteamWidget_rest_refresh($request) { $id = (int)$request->get_param('id'); $profile_url = $request->get_param('profile_url') ?? ''; //refresh_instance normalizes internally; widget-stored URLs are already canonical anyway $result = AdvancedSteamWidget_refresh_instance($id, $profile_url); //reset the recurring cron timer from now $next_update = 0; if (wp_next_scheduled('advanced_steam_widget_update', array($id)) !== false) { wp_clear_scheduled_hook('advanced_steam_widget_update', array($id)); wp_reschedule_event(time(), 'advanced_steam_widget_interval_' . $id, 'advanced_steam_widget_update', array($id)); $next_update = (int)wp_next_scheduled('advanced_steam_widget_update', array($id)); } $cache = AdvancedSteamWidget_get_cache($id); return array( 'success' => ($result === true), 'message' => ($result === true) ? 'Updated successfully' : $result, 'fetched_at' => $cache ? (int)$cache['fetched_at'] : 0, 'fetched_ago' => $cache ? human_time_diff($cache['fetched_at'], time()) : '', 'next_update' => $next_update, 'next_update_in' => $next_update ? human_time_diff(time(), $next_update) : '', ); } //block refresh callback - refreshes cache and returns the normalized URL so the editor can persist the canonical form function AdvancedSteamWidget_rest_block_refresh($request) { $id = $request->get_param('id'); $raw_url = $request->get_param('profile_url') ?? ''; //compute normalized URL for the response; refresh_instance also normalizes internally so fetch gets a clean URL $normalized_url = AdvancedSteamWidget_normalize_profile_input($raw_url); $result = AdvancedSteamWidget_refresh_instance($id, $raw_url); return array( 'success' => ($result === true), 'message' => ($result === true) ? 'Updated successfully' : $result, 'normalized_url' => $normalized_url, ); }