From 7f6c10059b4e9aae8a5e472a472e99de0ead5799 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 24 Feb 2025 15:25:04 +0100 Subject: [PATCH] Extension: Allow moz-extension as a URL protocol (#477) --- includes/class-rest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/includes/class-rest.php b/includes/class-rest.php index bf42aebb..c70db804 100644 --- a/includes/class-rest.php +++ b/includes/class-rest.php @@ -44,6 +44,21 @@ private function register_hooks() { add_action( 'wp_trash_post', array( $this, 'notify_remote_friend_post_deleted' ) ); add_action( 'before_delete_post', array( $this, 'notify_remote_friend_post_deleted' ) ); add_action( 'set_user_role', array( $this, 'notify_remote_friend_request_accepted' ), 20, 3 ); + add_action( 'rest_pre_serve_request', array( $this, 'send_rest_origin' ), 20, 3 ); + } + + public function send_rest_origin( $ret, $response, $request ) { + if ( strpos( $request->get_route(), '/' . self::PREFIX . '/extension' ) !== 0 ) { + return $ret; + } + + if ( $request->get_header( 'origin' ) ) { + $scheme = wp_parse_url( $request->get_header( 'origin' ), PHP_URL_SCHEME ); + if ( 'moz-extension' === $scheme ) { + header( 'access-control-allow-origin: ' . $request->get_header( 'origin' ) ); + } + } + return $ret; } /**