WordPress

php – Jak prawidłowo filtrować listę żądanych przez API zdarzeń?

  • 15 lipca, 2024
  • 3 min read
php – Jak prawidłowo filtrować listę żądanych przez API zdarzeń?


Jestem dość nowy w rozwoju WordPressa i zacząłem tworzyć własną wtyczkę do przeszukiwania bazy danych, a następnie zwracania wyników z zapytania wyszukiwania i wyświetlania listy nadchodzących wydarzeń w sekcji „sugerowane”. Lista wydarzeń pochodzi z API Corsizio. Mój problem polega na tym, aby wyświetlić prawidłowe wydarzenia.

Wydarzenia, które wyświetlają się w mojej sekcji „sugerowane”, są zaplanowane na koniec roku. Powinny wyświetlać się wydarzenia bliżej bieżącej daty, lipca/sierpnia. Wszystko, co próbowałem, wydaje się zawodzić, więc jestem tutaj, aby uzyskać pomoc i oto, co mam do tej pory:

// Function to fetch suggested courses from Corsizio API
function get_suggested_courses($license_number) {
    $api_url="
    $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>

To jest tylko część kodu wtyczki. Jeśli muszę pokazać całość, aby lepiej zrozumieć, proszę pytać.

Warto przeczytać!  Witamy BuddyBoss w rodzinie produktów WPBeginner


Źródło