From 195257e4111253a1ae1918194afd42fac016b45c Mon Sep 17 00:00:00 2001 From: Shady Sharaf Date: Wed, 18 Aug 2021 12:17:31 +0000 Subject: [PATCH 1/2] Pass-through session cookies to global content requests --- inc/global_content/namespace.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/inc/global_content/namespace.php b/inc/global_content/namespace.php index c19685d..835d0f2 100644 --- a/inc/global_content/namespace.php +++ b/inc/global_content/namespace.php @@ -10,6 +10,7 @@ use Exception; use WP_Admin_Bar; use WP_CLI; +use WP_Http_Cookie; use WP_Site; /** @@ -61,6 +62,9 @@ function bootstrap_site() : void { // Handle network admin sites list. add_filter( 'manage_sites_action_links', __NAMESPACE__ . '\\sites_list_row_actions', 10, 2 ); add_filter( 'display_site_states', __NAMESPACE__ . '\\add_global_site_state', 10, 2 ); + + // Handles passing current user cookies to global content requests to allow authenticated requests. + add_filter( 'http_request_args', __NAMESPACE__ . '\\filter_global_content_requests_for_auth', 10, 2 ); } /** @@ -366,3 +370,30 @@ function prevent_site_deletion( array $caps, string $cap, int $user_id, array $a return [ 'do_not_allow' ]; } + +/** + * Filter all requests to the global content repo to pass-through current user cookies + * + * @param array $parsed_args Parsed request arguments. + * @param string $url Request URL. + * + * @filters http_request_args + * + * @return array Parsed request arguments including current session cookies. + */ +function filter_global_content_requests_for_auth( array $parsed_args, string $url ) : array { + $global_site_url = get_site_url(); + + if ( strpos( $url, $global_site_url ) !== 0 ) { + return $parsed_args; + } + + $parsed_args['cookies'] = array_map( function( $value, $name ) { + return new WP_Http_Cookie( [ + 'name' => $name, + 'value' => $value, + ] ); + }, $_COOKIE, array_keys( $_COOKIE ) ); + + return $parsed_args; +} From ed66c37186eacb93217ea203fe18820d00140837 Mon Sep 17 00:00:00 2001 From: Shady Sharaf Date: Wed, 18 Aug 2021 13:37:39 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=92=85=20Spacing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ryan McCue --- inc/global_content/namespace.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/global_content/namespace.php b/inc/global_content/namespace.php index 835d0f2..9cfd4c8 100644 --- a/inc/global_content/namespace.php +++ b/inc/global_content/namespace.php @@ -388,7 +388,7 @@ function filter_global_content_requests_for_auth( array $parsed_args, string $ur return $parsed_args; } - $parsed_args['cookies'] = array_map( function( $value, $name ) { + $parsed_args['cookies'] = array_map( function ( $value, $name ) { return new WP_Http_Cookie( [ 'name' => $name, 'value' => $value,