Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 43 additions & 30 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,42 @@ public static function set_custom_columns($columns) {
];
}

private static $service_body_map = null;

public static function get_service_body_map() {
if (self::$service_body_map !== null) {
return self::$service_body_map;
}

$cached = get_transient('mayo_service_body_map');
if ($cached !== false) {
self::$service_body_map = $cached;
return self::$service_body_map;
}

$map = [];
$settings = get_option('mayo_settings', []);
$bmlt_root_server = $settings['bmlt_root_server'] ?? '';

if (!empty($bmlt_root_server)) {
$response = wp_remote_get($bmlt_root_server . '/client_interface/json/?switcher=GetServiceBodies');

if (!is_wp_error($response)) {
$service_bodies = json_decode(wp_remote_retrieve_body($response), true);

if (is_array($service_bodies)) {
foreach ($service_bodies as $body) {
$map[$body['id']] = $body['name'];
}
}
}
}

set_transient('mayo_service_body_map', $map, 600);
self::$service_body_map = $map;
return self::$service_body_map;
}

public static function render_custom_columns($column, $post_id) {
switch ($column) {
case 'event_type':
Expand All @@ -226,37 +262,14 @@ public static function render_custom_columns($column, $post_id) {
$service_body_id = get_post_meta($post_id, 'service_body', true);
if ($service_body_id === '0') {
echo esc_html('Unaffiliated (0)');
} elseif (empty($service_body_id)) {
echo esc_html('—');
} else {
// Get the service body name from the BMLT root server
$settings = get_option('mayo_settings', []);
$bmlt_root_server = $settings['bmlt_root_server'] ?? '';
$found = false;

if (!empty($bmlt_root_server)) {
$response = wp_remote_get($bmlt_root_server . '/client_interface/json/?switcher=GetServiceBodies');

if (!is_wp_error($response)) {
$service_bodies = json_decode(wp_remote_retrieve_body($response), true);

if (is_array($service_bodies)) {
foreach ($service_bodies as $body) {
if ($body['id'] == $service_body_id) {
echo esc_html($body['name'] . ' (' . $body['id'] . ')');
$found = true;
break;
}
}
}
}
}

// Fallback if we couldn't get the name
if (!$found) {
if (empty($service_body_id)) {
echo esc_html('—');
} else {
echo esc_html('Service Body (' . $service_body_id . ')');
}
$map = self::get_service_body_map();
if (isset($map[$service_body_id])) {
echo esc_html($map[$service_body_id] . ' (' . $service_body_id . ')');
} else {
echo esc_html('Service Body (' . $service_body_id . ')');
}
}
break;
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ This project is licensed under the GPL v2 or later.
* Improved performance of external source fetching by parallelizing HTTP requests. Sites with multiple external sources will see significantly faster load times.
* Changed external source event type dropdown from "Select an event type" to "All Event Types" to clarify that leaving it blank fetches all types.
* Added support for comma-separated event types in the REST API (e.g., `event_type=Activity,Service`), enabling a single external source to fetch multiple event types in one request.
* Fixed admin events list page taking minutes to load when many events exist by caching service body lookups instead of making an HTTP request per row.

= 1.8.7 =
* Fixed announcement form flyer upload field never appearing even when `show_flyer="true"` shortcode attribute was set. [#252]
Expand Down
31 changes: 24 additions & 7 deletions tests/Unit/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,18 @@ public function testRenderCustomColumnsOutputsServiceBodyUnaffiliated(): void {
* Test render_custom_columns outputs service body fallback
*/
public function testRenderCustomColumnsOutputsServiceBodyFallback(): void {
// Reset static cache from prior tests
$ref = new \ReflectionProperty(Admin::class, 'service_body_map');
$ref->setAccessible(true);
$ref->setValue(null, null);

$this->setPostMeta(402, [
'service_body' => '999'
]);

Functions\when('get_transient')->justReturn(false);
Functions\when('set_transient')->justReturn(true);

$this->mockWpRemoteGet([
'GetServiceBodies' => [
'code' => 200,
Expand All @@ -305,13 +313,6 @@ public function testRenderCustomColumnsOutputsEmptyServiceBody(): void {
'service_body' => ''
]);

$this->mockWpRemoteGet([
'GetServiceBodies' => [
'code' => 200,
'body' => []
]
]);

ob_start();
Admin::render_custom_columns('service_body', 403);
$output = ob_get_clean();
Expand Down Expand Up @@ -591,10 +592,18 @@ public function testRenderCustomColumnsOutputsRecurringIndicator(): void {
* Test render_custom_columns outputs service body with BMLT lookup
*/
public function testRenderCustomColumnsOutputsServiceBodyFromBmlt(): void {
// Reset static cache from prior tests
$ref = new \ReflectionProperty(Admin::class, 'service_body_map');
$ref->setAccessible(true);
$ref->setValue(null, null);

$this->setPostMeta(412, [
'service_body' => '5'
]);

Functions\when('get_transient')->justReturn(false);
Functions\when('set_transient')->justReturn(true);

$this->mockWpRemoteGet([
'GetServiceBodies' => [
'code' => 200,
Expand Down Expand Up @@ -1046,10 +1055,18 @@ public function testRenderCustomColumnsWithNoTimezone(): void {
* Test render_custom_columns service body with BMLT error response
*/
public function testRenderCustomColumnsServiceBodyWithBmltError(): void {
// Reset static cache from prior tests
$ref = new \ReflectionProperty(Admin::class, 'service_body_map');
$ref->setAccessible(true);
$ref->setValue(null, null);

$this->setPostMeta(422, [
'service_body' => '123'
]);

Functions\when('get_transient')->justReturn(false);
Functions\when('set_transient')->justReturn(true);

// wp_remote_get returns WP_Error which is_wp_error() will detect
Functions\when('wp_remote_get')->justReturn(new \WP_Error('http_error', 'Connection failed'));

Expand Down