Skip to content

Commit

Permalink
Add maybe_switch_site method (#12932)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Birchler <pascalb@google.com>
  • Loading branch information
spacedmonkey and swissspidy committed Jan 13, 2023
1 parent d450d60 commit 6571c9a
Showing 1 changed file with 68 additions and 55 deletions.
123 changes: 68 additions & 55 deletions includes/REST_API/Embed_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,6 @@ private function get_data_from_post( string $url ) {
* Checks are supposedly from the hosted site blog.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @since 1.2.0
*
Expand All @@ -357,61 +355,10 @@ private function get_data_from_post( string $url ) {
* @param string $url Permalink to check.
* @return WP_Post|null Post object on success, null otherwise.
*/
private function url_to_post( $url ): ?WP_Post { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
private function url_to_post( $url ): ?WP_Post {
$post = null;
$switched_blog = false;

if ( is_multisite() ) {
/**
* URL parts.
*
* @var array<string, string>|false $url_parts
*/
$url_parts = wp_parse_url( $url );
if ( ! $url_parts ) {
$url_parts = [];
}

$url_parts = wp_parse_args(
$url_parts,
[
'host' => '',
'path' => '/',
]
);

$qv = [
'domain' => $url_parts['host'],
'path' => '/',
'number' => 1,
'update_site_cache' => false,
'update_site_meta_cache' => false,
];

// In case of subdirectory configs, set the path.
if ( ! is_subdomain_install() ) {
// Get "sub-site" part of "http://example.org/sub-site/web-stories/my-story/".
// But given just "http://example.org/web-stories/my-story/", don't treat "web-stories" as site path.
// This differs from the logic in get_oembed_response_data_for_url() which does not do this.
// TODO: Investigate possible core bug in get_oembed_response_data_for_url()?
$path = explode( '/', ltrim( $url_parts['path'], '/' ) );
$path = \count( $path ) > 2 ? reset( $path ) : false;
$network = get_network();
if ( $path && $network instanceof WP_Network ) {
$qv['path'] = $network->path . $path . '/';
}
}

$sites = (array) get_sites( $qv );
$site = reset( $sites );
$switched_blog = $this->maybe_switch_site( $url );

if ( $site && get_current_blog_id() !== (int) $site->blog_id ) {
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.switch_to_blog_switch_to_blog
switch_to_blog( (int) $site->blog_id );

$switched_blog = true;
}
}

if ( function_exists( 'wpcom_vip_url_to_postid' ) ) {
$post_id = wpcom_vip_url_to_postid( $url );
Expand Down Expand Up @@ -480,6 +427,72 @@ private function url_to_post( $url ): ?WP_Post { // phpcs:ignore SlevomatCodingS
return $post;
}

/**
* Maybe switch to site.
*
* @since 1.29.0
*
* @param string $url Permalink to check.
*/
private function maybe_switch_site( $url ): bool {
if ( ! is_multisite() ) {
return false;
}
$switched_blog = false;

/**
* URL parts.
*
* @var array<string, string>|false $url_parts
*/
$url_parts = wp_parse_url( $url );
if ( ! $url_parts ) {
$url_parts = [];
}

$url_parts = wp_parse_args(
$url_parts,
[
'host' => '',
'path' => '/',
]
);

$qv = [
'domain' => $url_parts['host'],
'path' => '/',
'number' => 1,
'update_site_cache' => false,
'update_site_meta_cache' => false,
];

// In case of subdirectory configs, set the path.
if ( ! is_subdomain_install() ) {
// Get "sub-site" part of "http://example.org/sub-site/web-stories/my-story/".
// But given just "http://example.org/web-stories/my-story/", don't treat "web-stories" as site path.
// This differs from the logic in get_oembed_response_data_for_url() which does not do this.
// TODO: Investigate possible core bug in get_oembed_response_data_for_url()?
$path = explode( '/', ltrim( $url_parts['path'], '/' ) );
$path = \count( $path ) > 2 ? reset( $path ) : false;
$network = get_network();
if ( $path && $network instanceof WP_Network ) {
$qv['path'] = $network->path . $path . '/';
}
}

$sites = (array) get_sites( $qv );
$site = reset( $sites );

if ( $site && get_current_blog_id() !== (int) $site->blog_id ) {
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.switch_to_blog_switch_to_blog
switch_to_blog( (int) $site->blog_id );

$switched_blog = true;
}

return $switched_blog;
}

/**
* Parses an HTML document to and returns the story's title and poster.
*
Expand Down

0 comments on commit 6571c9a

Please sign in to comment.