'GET', 'callback' => array( $this, 'verify' ), 'permission_callback' => '__return_true', ), array( 'methods' => 'POST', 'callback' => array( $this, 'receive' ), 'permission_callback' => array( $this, 'signed_by_meta' ), ), ) ); } /** Meta calls this once when you save the webhook, and expects the challenge back as plain text. */ public function verify( WP_REST_Request $request ) { $mode = (string) $request->get_param( 'hub_mode' ); $token = (string) $request->get_param( 'hub_verify_token' ); $challenge = (string) $request->get_param( 'hub_challenge' ); $expected = (string) get_option( 'afpp_webhook_token' ); if ( 'subscribe' === $mode && '' !== $expected && hash_equals( $expected, $token ) ) { header( 'Content-Type: text/plain; charset=utf-8' ); echo esc_html( preg_replace( '/[^A-Za-z0-9_\-]/', '', $challenge ) ); exit; } return new WP_REST_Response( array( 'error' => 'forbidden' ), 403 ); } /** * Only Meta may post to the webhook: every call must carry a valid X-Hub-Signature-256 made with * the app secret. Without an app secret saved, incoming calls are refused. * * @return true|WP_Error */ public function signed_by_meta( WP_REST_Request $request ) { $secret = (string) AFPP_Settings::get( 'afpp_app_secret' ); if ( '' === $secret ) { return new WP_Error( 'afpp_no_app_secret', 'Add the app secret in New Post Alerts settings to receive webhook calls.', array( 'status' => 403 ) ); } $sig = (string) $request->get_header( 'x_hub_signature_256' ); $expected = 'sha256=' . hash_hmac( 'sha256', $request->get_body(), $secret ); if ( '' === $sig || ! hash_equals( $expected, $sig ) ) { return new WP_Error( 'afpp_bad_signature', 'Invalid signature.', array( 'status' => 401 ) ); } return true; } public function receive( WP_REST_Request $request ) { $data = json_decode( $request->get_body(), true ); if ( ! is_array( $data ) || empty( $data['entry'] ) ) { return new WP_REST_Response( array( 'ok' => true ), 200 ); } foreach ( (array) $data['entry'] as $entry ) { foreach ( (array) ( isset( $entry['changes'] ) ? $entry['changes'] : array() ) as $change ) { $value = isset( $change['value'] ) ? $change['value'] : array(); foreach ( (array) ( isset( $value['messages'] ) ? $value['messages'] : array() ) as $message ) { $this->handle_message( $message ); } foreach ( (array) ( isset( $value['statuses'] ) ? $value['statuses'] : array() ) as $status ) { $this->handle_status( $status ); } } } update_option( 'afpp_webhook_last_seen', time(), false ); return new WP_REST_Response( array( 'ok' => true ), 200 ); } private function handle_message( array $message ) { $from = isset( $message['from'] ) ? preg_replace( '/\D+/', '', $message['from'] ) : ''; if ( ! $from ) { return; } $text = ''; if ( isset( $message['text']['body'] ) ) { $text = $message['text']['body']; } elseif ( isset( $message['button']['text'] ) ) { $text = $message['button']['text']; } elseif ( isset( $message['interactive']['button_reply']['title'] ) ) { $text = $message['interactive']['button_reply']['title']; } $text = strtolower( trim( (string) $text ) ); $row = AFPP_Subscribers::by_phone( $from ); if ( $row ) { if ( in_array( $text, self::STOP_WORDS, true ) ) { AFPP_Subscribers::set_status( $row->id, 'unsubscribed' ); } elseif ( in_array( $text, self::START_WORDS, true ) && in_array( $row->status, array( 'unsubscribed', 'bounced' ), true ) ) { AFPP_Subscribers::set_status( $row->id, 'active' ); } } do_action( 'afpp_webhook_message', $message, $row ); } private function handle_status( array $status ) { global $wpdb; $wamid = isset( $status['id'] ) ? sanitize_text_field( $status['id'] ) : ''; $state = isset( $status['status'] ) ? sanitize_key( $status['status'] ) : ''; if ( ! $wamid || ! in_array( $state, array( 'sent', 'delivered', 'read', 'failed' ), true ) ) { return; } $q = AFPP_DB::queue(); // Never move backwards (a late "delivered" must not overwrite "read"). $rank = array( 'sent' => 1, 'delivered' => 2, 'read' => 3, 'failed' => 4 ); $wpdb->query( // phpcs:ignore WordPress.DB $wpdb->prepare( "UPDATE %i SET delivery = %s WHERE wamid = %s AND ( CASE delivery WHEN 'sent' THEN 1 WHEN 'delivered' THEN 2 WHEN 'read' THEN 3 WHEN 'failed' THEN 4 ELSE 0 END ) < %d", $q, $state, $wamid, $rank[ $state ] ) ); if ( 'failed' === $state && ! empty( $status['errors'][0]['title'] ) ) { $wpdb->update( $q, array( 'error' => substr( sanitize_text_field( $status['errors'][0]['title'] ), 0, 250 ) ), array( 'wamid' => $wamid ) ); // phpcs:ignore WordPress.DB } do_action( 'afpp_webhook_status', $wamid, $state, $status ); } }