I'm pretty new to PHP and WordPress development, and I started creating my own plugin to search a database then return results from the search query and show a list of upcoming events in a "suggested" section. The list of events comes from the Corsizio API. My problem is getting to show the correct events.
The events that show in my "suggested" section are planned for the end of the year. It should be showing events closer to the current date, July/August. Everything I've tried seems to fail so I'm here for some help and this is what I have so far:
// Function to fetch suggested courses from Corsizio API
function get_suggested_courses($license_number) {
$api_url = 'https://api.corsizio.com/v1/events';
$api_key = 'API_KEY';
error_log('Fetching events from Corsizio API...');
$response = wp_remote_get($api_url, array(
'headers' => array(
'Authorization' => 'Bearer ' . $api_key,
),
));
if (is_wp_error($response)) {
error_log('API request failed: ' . $response->get_error_message());
return [];
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
$events = $data->list ?? [];
// Log the fetched events
error_log('Fetched events: ' . print_r($events, true));
error_log('Number of events fetched: ' . count($events));
// If license number is provided, filter the events
if (!empty($license_number)) {
$license_prefix = substr($license_number, 0, 2);
$events = array_filter($events, function($event) use ($license_prefix) {
return strpos($event->summary, $license_prefix) !== false;
});
// Log the events after filtering by license prefix
error_log('Events after filtering by license prefix: ' . print_r($events, true));
error_log('Number of events after filtering by license prefix: ' . count($events));
}
// Filter events to only include the 5 most recent ones
$filtered_events = array_slice($events, 0, 5);
// Log the filtered events
error_log('Filtered events: ' . print_r($filtered_events, true));
error_log('Number of filtered events: ' . count($filtered_events));
return $filtered_events;
}
// Insert suggested courses here
$first_license_number = !empty($license_results) ? $license_results[0]->license_number : null;
$suggested_courses = get_suggested_courses($first_license_number);
if (!empty($suggested_courses)) {
?>
<h3>Suggested Courses</h3>
<table>
<thead>
<tr>
<th>Course Title</th>
<th>Start Date</th>
<th>Location</th>
<th>Register</th>
</tr>
</thead>
<tbody>
<?php foreach ($suggested_courses as $event) :
// Check if the necessary properties exist before accessing them
$course_title = isset($event->name) ? $event->name : 'N/A';
$summary = isset($event->summary) ? $event->summary : 'N/A';
$start_date = isset($event->startDate) ? (new DateTime($event->startDate))->format('m/d/Y') : 'N/A';
$location = isset($event->location) ? $event->location : 'N/A';
$register_url = isset($event->formUrl) ? $event->formUrl : '#';
?>
<tr>
<td><?php echo esc_html($course_title); ?></td>
<td><?php echo esc_html($start_date); ?></td>
<td><?php echo esc_html($location); ?></td>
<td><a href="<?php echo esc_url($register_url); ?>" target="_blank">Click Here to Register</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This is just part of the plugin code. If I need to show all of it to give a better understanding, please ask.
