[A-Za-z0-9]{20,64})', array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'handle' ), // Authenticated by the site's secret key in the URL. 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => array( 'key' => array( 'type' => 'string', 'required' => true, ), ), ) ); } /** * Permission check: the key in the address must be this site's webhook key. * * @param WP_REST_Request $request Request. * @return true|WP_Error */ public static function allowed( $request ) { $stored = (string) get_option( self::OPTION, '' ); if ( '' === $stored || ! hash_equals( $stored, (string) $request->get_param( 'key' ) ) ) { return new WP_Error( 'nodebook_forbidden', __( 'Invalid key.', 'nodebook' ), array( 'status' => 403 ) ); } return true; } /** @param WP_REST_Request $request */ public static function handle( $request ) { $body = $request->get_json_params(); if ( ! is_array( $body ) || ! $body ) { $body = $request->get_body_params(); } if ( ! is_array( $body ) || ! $body ) { return new WP_Error( 'nodebook_empty', __( 'No data received.', 'nodebook' ), array( 'status' => 400 ) ); } $result = self::process( $body ); if ( ! $result ) { return new WP_Error( 'nodebook_no_contact', __( 'No email address found in the payload.', 'nodebook' ), array( 'status' => 422 ) ); } return rest_ensure_response( array( 'ok' => true, 'contact_id' => $result, ) ); } /** Turn a payload into a conversion. Returns the contact ID or 0. */ public static function process( array $body ) { $flat = Nodebook_Bookings::flatten( $body ); $lower = array(); foreach ( $flat as $k => $v ) { if ( is_scalar( $v ) ) { $lower[ strtolower( str_replace( array( '-', ' ' ), '_', (string) $k ) ) ] = (string) $v; } } $pick = static function ( array $keys ) use ( $lower ) { foreach ( $keys as $k ) { if ( isset( $lower[ $k ] ) && '' !== trim( $lower[ $k ] ) ) { return trim( $lower[ $k ] ); } } return ''; }; $person = Nodebook_Bookings::person( $body ); if ( empty( $person['email'] ) ) { return 0; } // Email consent: "email_status": "unsubscribed" (a record from another system) or "newsletter": true (the person's choice). $status = $pick( array( 'email_status', 'subscription_status', 'marketing_status' ) ); if ( '' !== $status && null !== Nodebook_Subscription::normalize( $status ) ) { $person['email_status'] = $status; } foreach ( array( 'email_consent', 'marketing_consent', 'accepts_marketing', 'newsletter', 'subscribed', 'subscribe', 'opt_in', 'optin' ) as $k ) { if ( isset( $lower[ $k ] ) && '' !== trim( $lower[ $k ] ) ) { $person['email_consent'] = array( 'agreed' => (bool) preg_match( '/^(1|yes|true|on|y|s[ií]|ja|subscribed|checked|accepted)$/i', trim( $lower[ $k ] ) ), 'text' => $k, ); break; } } // A custom event ("track": "webinar_attended") instead of a conversion. $event = $pick( array( 'track', 'event_name' ) ); if ( '' !== $event ) { $src = $pick( array( 'source', 'platform', 'channel' ) ); return Nodebook_Events::track( $person, $event, array( 'value' => $pick( array( 'value', 'amount' ) ), 'label' => $pick( array( 'event_label', 'title' ) ), 'create' => true, 'source' => $src ? mb_substr( sanitize_text_field( $src ), 0, 60 ) : __( 'Webhook', 'nodebook' ), ) ) ? (int) Nodebook_Contacts::find_by_email( $person['email'] )['id'] : 0; } $type = sanitize_key( $pick( array( 'type', 'event', 'conversion', 'kind' ) ) ); $in = Nodebook_Conversions::ymd( $pick( array( 'check_in', 'checkin', 'arrival', 'start_date', 'start', 'date', 'appointment_date', 'from' ) ) ); $out = Nodebook_Conversions::ymd( $pick( array( 'check_out', 'checkout', 'departure', 'end_date', 'end', 'to' ) ) ); if ( ! in_array( $type, Nodebook_Conversions::types(), true ) ) { $type = $out ? 'booking' : ( $in ? 'appointment' : 'form' ); } $what = $pick( array( 'title', 'property', 'listing', 'listing_name', 'property_name', 'service', 'service_name', 'product', 'subject' ) ); $src = $pick( array( 'source', 'platform', 'channel' ) ); $src = $src ? mb_substr( sanitize_text_field( $src ), 0, 60 ) : __( 'Webhook', 'nodebook' ); $range = Nodebook_Conversions::range_label( $in, $out ); if ( 'booking' === $type ) { /* translators: %s: what was booked */ $title = sprintf( __( 'Booked %s', 'nodebook' ), $what ? $what : __( 'a stay', 'nodebook' ) ); } elseif ( 'appointment' === $type ) { /* translators: %s: service */ $title = sprintf( __( 'Appointment: %s', 'nodebook' ), $what ? $what : __( 'an appointment', 'nodebook' ) ); } elseif ( $what ) { $title = sprintf( (string) Nodebook_Niche::get( 'inquiry' ), $what ); } else { /* translators: %s: source, e.g. Zapier */ $title = sprintf( __( 'Inquiry via %s', 'nodebook' ), $src ); } if ( $range ) { $title .= ' · ' . $range; } $title = mb_substr( sanitize_text_field( $title ), 0, 250 ); $ext = $pick( array( 'id', 'booking_id', 'reservation_id', 'lead_id', 'external_id', 'appointment_id', 'order_id' ) ); return Nodebook_Conversions::record( $person, $type, $title, array( 'source' => $src, 'external_id' => ctype_digit( $ext ) ? (int) $ext : ( $ext ? absint( crc32( $ext ) ) : 0 ), 'value' => is_numeric( $pick( array( 'value', 'total', 'amount', 'price' ) ) ) ? $pick( array( 'value', 'total', 'amount', 'price' ) ) : '', 'currency' => $pick( array( 'currency', 'currency_code' ) ), 'dates' => array( $in, $out ), 'object_id' => $what ? self::item( $what ) : 0, ) ); } private static function item( $title ) { $q = new WP_Query( array( 'post_type' => Nodebook_Interests::item_types(), 'title' => $title, 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); return $q->posts ? (int) $q->posts[0] : 0; } }