Skip to content

Commit cba2459

Browse files
committed
Improve event location fallback logic
Adds fallback to default US location if user location is empty and initial event fetch fails. Also uses geo-IP lookup via wp_get_user_location when location is unavailable, enhancing reliability of event retrieval.
1 parent 236710f commit cba2459

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

includes/adapters/class-wordpress.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,27 @@ protected function get_events_items( $limit ) {
206206

207207
$user_id = \get_current_user_id();
208208

209+
$location = $this->get_events_location( $user_id );
210+
209211
$events_response = \wp_get_community_events(
210212
array(
211-
'number' => $limit,
212-
'location' => $this->get_events_location( $user_id ),
213+
'number' => $limit,
214+
'location' => $location,
213215
)
214216
);
215217

218+
if ( \is_wp_error( $events_response ) || empty( $events_response['events'] ) ) {
219+
// If location was empty and the first call failed, try again with the default WP.org news locale.
220+
if ( empty( $location ) ) {
221+
$events_response = \wp_get_community_events(
222+
array(
223+
'number' => $limit,
224+
'location' => array( 'country' => 'US' ),
225+
)
226+
);
227+
}
228+
}
229+
216230
if ( \is_wp_error( $events_response ) || empty( $events_response['events'] ) ) {
217231
return array();
218232
}
@@ -254,6 +268,11 @@ protected function get_events_location( $user_id ) {
254268
}
255269
}
256270

271+
if ( empty( $location ) && \function_exists( 'wp_get_user_location' ) ) {
272+
// Falls back to the same geo-IP lookup core uses for the dashboard widget.
273+
$location = \wp_get_user_location();
274+
}
275+
257276
/**
258277
* Filter the location used for WordPress Events.
259278
*

0 commit comments

Comments
 (0)