Making WordPress.org

Changeset 11952


Ignore:
Timestamp:
07/08/2022 03:53:55 AM (4 years ago)
Author:
dd32
Message:

API: Community Calendly calendar notifications: Catch all of the relevant bookings, and handle different forms different questions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/dotorg/slack/community-deputies-calendly-webhook.php

    r11941 r11952  
    1414 */
    1515function api_request( $url ) {
     16    if ( ! $url ) {
     17        return false;
     18    }
     19
    1620    $req = wp_remote_get(
    1721        $url,
     
    2226        ]
    2327    );
     28
    2429    return json_decode( wp_remote_retrieve_body( $req ) );
    2530}
    2631
    2732// Check the request is valid.
    28 if ( $_GET['secret'] !== COMMUNITY_CALENDLY_SECRET ) {
     33if ( empty( $_GET['secret'] ) || $_GET['secret'] !== COMMUNITY_CALENDLY_SECRET ) {
    2934    die();
    3035}
     
    4247
    4348// Check it's a valid and expected meeting type..
     49$valid               = false;
    4450$valid_meeting_names = [
    45     'wordcamp orientation',
    46     'wordcamp budget review',
     51    'Meetup',
     52    'WordCamp',
     53    'do_action',
     54    'Orientation',
     55    'Budget Review',
    4756];
    48 if ( ! in_array( strtolower( $event_name ), $valid_meeting_names ) ) {
     57
     58foreach ( $valid_meeting_names as $name ) {
     59    if ( false !== stripos( $event_name, $name ) ) {
     60        $valid = true;
     61        break;
     62    }
     63}
     64
     65if ( ! $valid ) {
    4966    die();
    5067}
     
    5875// Compile the questions..
    5976$questions_and_answers = array_merge(
    60     wp_list_pluck( $routing_form_submission->questions_and_answers, 'answer', 'question' ),
    61     wp_list_pluck( $request_body_parsed->payload->questions_and_answers, 'answer', 'question' ),
     77    wp_list_pluck( $routing_form_submission->questions_and_answers ?? [], 'answer', 'question' ),
     78    wp_list_pluck( $request_body_parsed->payload->questions_and_answers ?? [], 'answer', 'question' ),
    6279);
    6380
    64 // Finally, localize the date and let Slack know.
     81// Finally, compile some phrases needed, localised dates, event names, etc.
    6582$timezone      = $request_body_parsed->payload->timezone;
    6683$date_time     = new DateTime( "now", new DateTimeZone( $timezone ) );
    6784$date_time->setTimestamp( strtotime( $event_details->start_time ) );
    6885$localized_time = $date_time->format( 'g:ia l, F jS, Y' ); // 10:30am Thursday, July 7th, 2022.
     86
     87// Suffix the timezone, as the localized time is timezone dependant.
     88$localized_time .= " ({$timezone})";
     89
     90$location = '';
     91// Use the first question found that contains 'location' or 'WordCamp'.
     92// Forms use 'Location', 'Your WordCamp Location', and 'WordCamp Name'.
     93foreach ( $questions_and_answers as $question => $answer ) {
     94    if (
     95        false !== stripos( $question, 'Location' ) ||
     96        false !== stripos( $question, 'WordCamp' )
     97    ) {
     98        $location = $answer;
     99        break;
     100    }
     101}
     102
     103// If the location isn't specified, use their Name instead, as it's likely an individual-specific meeting.
     104$location       = $location ?: ( $questions_and_answers['Name'] ?? '' );
     105$event_with_loc = $location ? "{$location} {$event_name}" : $event_name;
    69106
    70107$send = new Send( SLACK_MESSAGE_WEBHOOK_URL );
     
    74111if ( 'invitee.created' === $event ) {
    75112    $send->set_text(
    76         "*{$questions_and_answers['Your WordCamp Location']} {$event_name} is scheduled!*\n" .
    77         "Assigned to {$assigned_to}. Starts at {$localized_time} ({$timezone})."
     113        "*{$event_with_loc} is scheduled!*\n" .
     114        "Assigned to {$assigned_to}. Starts at {$localized_time}."
    78115    );
    79116} elseif ( 'invitee.canceled' === $event ) {
    80117    $reason_given = $request_body_parsed->payload->cancellation->reason ?: 'No reason provided';
    81118    $send->set_text(
    82         "*{$questions_and_answers['Your WordCamp Location']} {$event_name} has been canceled.*\n" .
    83         "Canceled by {$request_body_parsed->payload->cancellation->canceled_by}: {$reason_given}, was scheduled for {$localized_time} ($timezone)."
     119        "*{$event_with_loc} has been canceled.*\n" .
     120        "Canceled by {$request_body_parsed->payload->cancellation->canceled_by}: {$reason_given}, was scheduled for {$localized_time}."
    84121    );
    85122} else {
Note: See TracChangeset for help on using the changeset viewer.