Skip to content

Instantly share code, notes, and snippets.

@spacedmonkey
Created March 30, 2022 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacedmonkey/bf9f9cacb4d3aa8e99c1faaa4e51a447 to your computer and use it in GitHub Desktop.
Save spacedmonkey/bf9f9cacb4d3aa8e99c1faaa4e51a447 to your computer and use it in GitHub Desktop.
Add custom port support for multisite.
<?php
/**
* Plugin Name: Multisite custom port support
* Plugin URI: https://www.spacedmonkey.com
* Description: Enable custom port support for multisite.
* Version: 1.0
* Requires at least: 5.6
* Requires PHP: 4.0
* Author: Jonathan Harris
* Author URI: https://www.spacedmonkey.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Hook into `pre_get_site_by_path` and look up site by port.
*
* @param null|false|WP_Site $site Site value to return by path. Default null
* to continue retrieving the site.
* @param null|false|WP_Site $site Site value to return by path. Default null
* to continue retrieving the site.
* @param string $domain The requested domain.
* @param string $path The requested path, in full.
* @param int|null $segments The suggested number of paths to consult.
* Default null, meaning the entire path was to be consulted.
* @param string[] $paths The paths to search for, based on $path and $segments.
*
* @return null|false|WP_Site $site New site.
*/
function pre_get_site_by_path_with_port( $site, $domain, $path, $segments, $paths ) {
$domain_pieces = parse_url( $domain );
$domain = $domain_pieces['host'];
$domains = array( $domain );
if ( 'www.' === substr( $domain, 0, 4 ) ) {
$domains[] = substr( $domain, 4 );
}
$args = array(
'number' => 1,
'update_site_meta_cache' => false,
);
if ( count( $domains ) > 1 ) {
$args['domain__in'] = $domains;
$args['orderby']['domain_length'] = 'DESC';
} else {
$args['domain'] = array_shift( $domains );
}
if ( count( $paths ) > 1 ) {
$args['path__in'] = $paths;
$args['orderby']['path_length'] = 'DESC';
} else {
$args['path'] = array_shift( $paths );
}
if ( $domain_pieces['port'] ) {
$args['meta_query'] = array(
array(
'key' => 'port',
'value' => $domain_pieces['port']
),
);
}
$result = get_sites( $args );
$site = array_shift( $result );
if ( $site ) {
return $site;
}
return false;
}
add_filter( 'pre_get_site_by_path', 'pre_get_site_by_path_with_port', 10, 5 );
/**
* Add form input to site edit form.
*
* @param int $site_id Site id.
*
* @return void
*/
function add_site_info_port( $site_id ) {
$port = (string) get_site_meta( $site_id, 'port', true );
?>
<table class="form-table" role="presentation">
<tr class="form-field">
<th scope="row"><label for="url"><?php _e( 'Port' ); ?></label></th>
<td><input name="port" type="number" id="port" value="<?php echo esc_attr( $port ); ?>"/></td>
</tr>
</table>
<?php
}
add_action( 'network_site_info_form', 'add_site_info_port' );
/**
* Hook into saving edit site to save port number.
*
* @param string $action
* @param int $result
*
* @return void
*/
function edit_site_save_port( $action, $result ) {
if ( $action === 'edit-site' && $result === 1 ) {
$site_id = isset( $_POST['id'] ) ? wp_unslash( sanitize_text_field( $_POST['id'] ) ) : 0;
$port = isset( $_POST['port'] ) ? wp_unslash( sanitize_text_field( $_POST['port'] ) ) : '';
update_site_meta( $site_id, 'port', $port );
}
}
add_action( 'check_admin_referer', 'edit_site_save_port', 10, 2 );
/**
* Hook into domain exists and lookup via port as well.
*
* @param int|null $old_result The site ID if the site name exists, null otherwise.
* @param string $domain Domain to be checked.
* @param string $path Path to be checked.
* @param int $network_id Network ID. Relevant only on multi-network installations.
*
* @return mixed|null
*/
function domain_exists_port( $old_result, $domain, $path, $network_id ) {
$port = isset( $_POST['port'] ) ? wp_unslash( sanitize_text_field( $_POST['port'] ) ) : 0;
if ( ! $port ) {
return $old_result;
}
$path = trailingslashit( $path );
$args = array(
'network_id' => $network_id,
'domain' => $domain,
'path' => $path,
'fields' => 'ids',
'number' => 1,
'update_site_meta_cache' => false,
'meta_query' => array(
array(
'key' => 'port',
'value' => $port
),
)
);
$result = get_sites( $args );
$result = array_shift( $result );
return $result;
}
add_filter( 'domain_exists', 'domain_exists_port', 10, 4 );
add_filter( 'redirect_network_admin_request', '__return_false' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment