Skip to content

Commit

Permalink
All: Factor out jquery_site_expand() and jquery_site_extract()
Browse files Browse the repository at this point in the history
Avoid direct use JQUERY_STAGING_PREFIX so that the next commit
can replace that with a different system that can accomodate a port
number for jquery-wp-docker.
  • Loading branch information
Krinkle committed Oct 7, 2023
1 parent ea82eea commit 06431b5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
7 changes: 2 additions & 5 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
}
// Strip custom port number or staging prefix
// e.g. local.jquery.com:8080 -> jquery.com
$live_site = preg_replace( '/:\d+$/', '', strtolower( $_SERVER['HTTP_HOST'] ) );
if ( JQUERY_STAGING ) {
$live_site = strtr( $live_site, [ JQUERY_STAGING_PREFIX => '' ] );
}
$live_site = jquery_site_extract( $_SERVER['HTTP_HOST'] );
if ( !isset( $sites[ $live_site ] ) ) {
header( "400 Invalid Request" );
header( "Content-Type: text/plain" );
Expand All @@ -61,7 +58,7 @@
define( 'MULTISITE', true );
define( 'SUNRISE', true );
define( 'SUBDOMAIN_INSTALL', true );
define( 'DOMAIN_CURRENT_SITE', JQUERY_STAGING_PREFIX . 'jquery.com' );
define( 'DOMAIN_CURRENT_SITE', jquery_site_expand( 'jquery.com' ) );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );
Expand Down
4 changes: 2 additions & 2 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated
global $wpdb;

$base = '/';
$domain = JQUERY_STAGING_PREFIX . 'jquery.com';
$domain = jquery_site_expand( 'jquery.com' );

wp_check_mysql_version();
wp_cache_flush();
Expand Down Expand Up @@ -82,7 +82,7 @@ function jquery_install_site( $site, $user ) {
// sites in the exact order that they are defined, and without any gaps, as otherwise
// the "next" inserted ID would not match what we declare in jquery_sites()
//
// $blog_id = insert_blog( JQUERY_STAGING_PREFIX . $domain, $path, 1 );
// $blog_id = insert_blog( jquery_site_expand( $domain ), $path, 1 );
//
// WordPress 5.1, deprecates insert_blog() in favour of a new wp_insert_site() function,
// which does accept a custom 'blog_id' to be set up front.
Expand Down
6 changes: 3 additions & 3 deletions plugins/jquery-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ function strip_https($url) {
return $post_data;
}, 10, 2 );

if ( JQUERY_STAGING_PREFIX === 'local.' && !defined( 'XMLRPC_REQUEST' ) ) {
if ( JQUERY_STAGING && !defined( 'XMLRPC_REQUEST' ) ) {
ob_start( 'jquery_com_ob_local_urls' );
}
function jquery_com_ob_local_urls( $content ) {
$pairs = [];
foreach ( jquery_sites() as $site => $_ ) {
// Replace HTTPS with protocol-relative so navigation stays within HTTP locally.
$pairs[ 'https://' . $site ] = '//' . JQUERY_STAGING_PREFIX . $site;
$pairs[ 'https://' . $site ] = '//' . jquery_site_expand( $site );
// Update any remaining HTTP or protocol-relative urls.
$pairs[ '//' . $site ] = '//' . JQUERY_STAGING_PREFIX . $site;
$pairs[ '//' . $site ] = '//' . jquery_site_expand( $site );
}
return strtr( $content, $pairs );
}
33 changes: 31 additions & 2 deletions sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,35 @@ function jquery_sites() {
return $sites;
}

/**
* Resolve a canonical site (e.g. JQUERY_LIVE_SITE) into one for
* the current environment. This exists to automatically change
* the site hostname if JQUERY_STAGING is true.
*
* This is cheap and can be applied at the last minute as-needed.
*
* @param string $site
* @return string
*/
function jquery_site_expand( $site ) {
if ( JQUERY_STAGING ) {
return JQUERY_STAGING_PREFIX . $site;
}
return $site;
}

/**
* @param string $site E.g. `$_SERVER['HTTP_HOST']`
* @return string
*/
function jquery_site_extract( $hostname ) {
$live_site = preg_replace( '/:\d+$/', '', strtolower( $hostname ) );
if ( JQUERY_STAGING ) {
$live_site = strtr( $live_site, [ JQUERY_STAGING_PREFIX => '' ] );
}
return $live_site;
}

function jquery_default_site_options() {
$defaults = array(
'enable_xmlrpc' => 1,
Expand All @@ -383,8 +412,8 @@ function jquery_default_site_options() {
// to redirect to live sites in local development. This filter does not
// prevent the redirect, but changes the redirect to the local site.
if ( JQUERY_STAGING ) {
$defaults['home'] = '//' . JQUERY_STAGING_PREFIX . JQUERY_LIVE_SITE;
$defaults['siteurl'] = '//' . JQUERY_STAGING_PREFIX . JQUERY_LIVE_SITE;
$defaults['home'] = '//' . jquery_site_expand( JQUERY_LIVE_SITE );
$defaults['siteurl'] = '//' . jquery_site_expand( JQUERY_LIVE_SITE );
}
return $defaults;

Expand Down
4 changes: 2 additions & 2 deletions sunrise.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
}
require ABSPATH . 'wp-admin/includes/upgrade.php';
$sites = jquery_sites();
$site = str_replace( JQUERY_STAGING_PREFIX, '', $_SERVER['HTTP_HOST'] );
$site = jquery_site_extract( $_SERVER['HTTP_HOST'] );
if ( ! empty( $sites[ $site ]['subsites'] ) ) {
list( $first_path_segment ) = explode( '/', trim( $_SERVER['REQUEST_URI'], '/' ), 2 );
if ( $first_path_segment && isset( $sites[ $site . '/' . $first_path_segment ] ) )
$site .= '/' . $first_path_segment;
}

jquery_install_site( $site, $super_admin );
wp_safe_redirect( 'http://' . JQUERY_STAGING_PREFIX . $site );
wp_safe_redirect( 'http://' . jquery_site_expand( $site ) );
exit;
} );
}
Expand Down

0 comments on commit 06431b5

Please sign in to comment.