Fix dashboard news widget and add caching (#223)

Signed-off-by: Andy Fragen <andy@thefragens.com>
This commit is contained in:
Andy Fragen 2025-08-16 18:16:47 -07:00 committed by GitHub
parent 6ca4e6e73b
commit 44e23518ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,8 @@
namespace FAIR\Dashboard_Widgets;
use const FAIR\Packages\CACHE_LIFETIME;
use WP_Error;
const EVENTS_API = 'https://api.fair.pm/fair/v1/events';
@ -15,10 +17,6 @@ const EVENTS_API = 'https://api.fair.pm/fair/v1/events';
* Bootstrap.
*/
function bootstrap() {
// Support existing software like ClassicPress, which removes this feature.
if ( ! function_exists( 'wp_print_community_events_markup' ) ) {
return;
}
add_action( 'wp_ajax_get-community-events', __NAMESPACE__ . '\\get_community_events_ajax', 0 );
remove_action( 'wp_ajax_get-community-events', 'wp_ajax_get_community_events', 1 );
@ -86,9 +84,13 @@ function get_community_events_ajax() : void {
* @return array|WP_Error List of events or WP_Error on failure.
*/
function get_community_events() {
$response = wp_remote_get( EVENTS_API );
if ( is_wp_error( $response ) ) {
return $response;
$response = get_transient( EVENTS_API );
if ( ! $response ) {
$response = wp_remote_get( EVENTS_API );
if ( is_wp_error( $response ) ) {
return $response;
}
set_transient( EVENTS_API, $response, CACHE_LIFETIME );
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
@ -161,6 +163,11 @@ function get_community_events() {
* otherwise not filterable.
*/
function render_news_widget() : void {
// Support existing software like ClassicPress, which removes this feature.
if ( ! function_exists( 'wp_print_community_events_markup' ) ) {
return;
}
wp_print_community_events_markup();
?>