Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -12,7 +12,7 @@ namespace WP_Ultimo\Domain_Mapping;
use Psr\Log\LogLevel;
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
defined('ABSPATH') || exit;
/**
* Helper class for domain mapping functionality.
@ -35,7 +35,7 @@ class Helper {
/**
* Static-only class.
*/
private function __construct() {} // end __construct;
private function __construct() {}
/**
* Checks if we are in development mode.
@ -48,7 +48,7 @@ class Helper {
$site_url = site_url();
$is_development_mode = preg_match( '#(localhost|staging.*\.|\.local|\.test)#', $site_url );
$is_development_mode = preg_match('#(localhost|staging.*\.|\.local|\.test)#', $site_url);
/**
* Allow plugin developers to add additional tests
@ -60,8 +60,8 @@ class Helper {
* @param string $site_url The site URL.
* @return bool
*/
return apply_filters( 'wu_is_development_mode', $is_development_mode, $site_url );
} // end is_development_mode;
return apply_filters('wu_is_development_mode', $is_development_mode, $site_url);
}
/**
* Gets the local IP address of the network.
*
@ -72,8 +72,8 @@ class Helper {
*/
public static function get_local_network_ip() {
return isset( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : false;
} // end get_local_network_ip;
return isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : false;
}
/**
* Gets the public IP address of the network using an external HTTP call.
@ -93,10 +93,10 @@ class Helper {
/**
* See more about this filter below, on this same method.
*/
return apply_filters( 'wu_get_network_public_ip', $local_ip, true );
} // end if;
return apply_filters('wu_get_network_public_ip', $local_ip, true);
}
$_ip_address = get_site_transient( 'wu_public_network_ip' );
$_ip_address = get_site_transient('wu_public_network_ip');
if ( ! $_ip_address ) {
$ip_address = false;
@ -109,17 +109,17 @@ class Helper {
)
);
if ( ! is_wp_error( $response ) ) {
$ip_address = trim( wp_remote_retrieve_body( $response ) );
if ( ! is_wp_error($response) ) {
$ip_address = trim(wp_remote_retrieve_body($response));
continue;
} // end if;
} // end foreach;
}
}
set_site_transient( 'wu_public_network_ip', $ip_address, 10 * DAY_IN_SECONDS );
set_site_transient('wu_public_network_ip', $ip_address, 10 * DAY_IN_SECONDS);
$_ip_address = $ip_address;
} // end if;
}
/**
* Allow developers to change the public IP address of the network.
@ -135,8 +135,8 @@ class Helper {
* @param bool $local True if this is a local network (localhost, .dev, etc.), false otherwise.
* @return string The new IP address.
*/
return apply_filters( 'wu_get_network_public_ip', $_ip_address, false );
} // end get_network_public_ip;
return apply_filters('wu_get_network_public_ip', $_ip_address, false);
}
/**
* Checks if a given domain name has a valid associated SSL certificate.
*
@ -177,7 +177,7 @@ class Helper {
);
// If stream could not be established, SSL is invalid.
if (!$stream) {
if ( ! $stream) {
throw new \Exception($errstr);
}
@ -190,8 +190,8 @@ class Helper {
if ($cert) {
// Verify the certificate's validity period.
$current_time = time();
$valid_from = $cert['validFrom_time_t'] ?? 0;
$valid_to = $cert['validTo_time_t'] ?? 0;
$valid_from = $cert['validFrom_time_t'] ?? 0;
$valid_to = $cert['validTo_time_t'] ?? 0;
// Check if the certificate is currently valid.
if ($current_time >= $valid_from && $current_time <= $valid_to) {
@ -199,20 +199,20 @@ class Helper {
// Check that the domain matches the certificate.
$common_name = $cert['subject']['CN'] ?? ''; // Common Name (CN)
$alt_names = $cert['extensions']['subjectAltName'] ?? ''; // Subject Alternative Names (SAN)
$alt_names = $cert['extensions']['subjectAltName'] ?? ''; // Subject Alternative Names (SAN)
// Parse SAN into an array if present.
$alt_names_array = array_filter(array_map('trim', explode(',', str_replace('DNS:', '', $alt_names))));
$alt_names_array = array_filter(array_map('trim', explode(',', str_replace('DNS:', '', $alt_names))));
$alt_names_array[] = $common_name;
// Check if the host matches either the CN, any SAN entry, or supports a wildcard match.
if (
$host === $common_name ||
in_array( $host, $alt_names_array, true )
in_array($host, $alt_names_array, true)
) {
$is_valid = true;
} else {
foreach ($alt_names_array as $alt_name) {
if ( strpos($alt_name, '*.') === 0 && str_ends_with( $host, substr($alt_name, 1) )) {
if ( strpos($alt_name, '*.') === 0 && str_ends_with($host, substr($alt_name, 1))) {
$is_valid = true;
break;
}
@ -234,5 +234,5 @@ class Helper {
}
return $is_valid;
} // end has_valid_ssl_certificate;
} // end class Helper;
}
}

View File

@ -32,8 +32,7 @@ class Primary_Domain {
public function init() {
add_action('wu_domain_mapping_load', array($this, 'add_hooks'), -20);
} // end init;
}
/**
* Adds the necessary hooks.
@ -48,8 +47,7 @@ class Primary_Domain {
add_action('admin_init', array($this, 'maybe_redirect_to_mapped_or_network_domain'));
add_action('login_init', array($this, 'maybe_redirect_to_mapped_or_network_domain'));
} // end add_hooks;
}
/**
* Redirects the site to its primary mapped domain, if any.
@ -62,16 +60,12 @@ class Primary_Domain {
$should_redirect = true;
if (is_preview()) {
$should_redirect = false;
} // end if;
}
if (is_customize_preview()) {
$should_redirect = false;
} // end if;
}
/**
* Allow developers to short-circuit the redirection, preventing it
@ -83,34 +77,29 @@ class Primary_Domain {
* @return bool
*/
if (apply_filters('wu_should_redirect_to_primary_domain', $should_redirect) === false) {
return;
}
} // end if;
if (!function_exists('wu_get_domains')) {
if ( ! function_exists('wu_get_domains')) {
return;
}
} // end if;
$domains = wu_get_domains(array(
'blog_id' => get_current_blog_id(),
'primary_domain' => 1,
'active' => 1,
'domain__not_in' => array($_SERVER['HTTP_HOST']),
));
$domains = wu_get_domains(
array(
'blog_id' => get_current_blog_id(),
'primary_domain' => 1,
'active' => 1,
'domain__not_in' => array($_SERVER['HTTP_HOST']),
)
);
if (empty($domains)) {
return;
} // end if;
}
$primary_domain = $domains[0];
if ($_SERVER['HTTP_HOST'] !== $primary_domain->get_domain() && $primary_domain->is_active()) {
$url = wu_get_current_url();
$new_url = Domain_Mapping::get_instance()->replace_url($url, $primary_domain);
@ -118,10 +107,8 @@ class Primary_Domain {
wp_redirect(set_url_scheme($new_url));
exit;
} // end if;
} // end redirect_to_primary_domain;
}
}
/**
* Handles redirects to mapped ot network domain for the admin panel.
@ -132,37 +119,29 @@ class Primary_Domain {
public function maybe_redirect_to_mapped_or_network_domain() {
if ($_SERVER['REQUEST_METHOD'] !== 'GET' || wp_doing_ajax()) {
return;
} // end if;
}
/*
* The visitor is actively trying to logout. Let them do it!
*/
if (wu_request('action', 'nothing') === 'logout' || wu_request('loggedout')) {
return;
} // end if;
}
$site = wu_get_current_site();
$mapped_domain = $site->get_primary_mapped_domain();
if (!$mapped_domain) {
if ( ! $mapped_domain) {
return;
} // end if;
}
$redirect_settings = wu_get_setting('force_admin_redirect', 'both');
if ($redirect_settings === 'both') {
return;
} // end if;
}
$current_url = wp_parse_url(wu_get_current_url());
@ -175,35 +154,25 @@ class Primary_Domain {
$query_args = array();
if (wu_get_isset($current_url, 'query')) {
wp_parse_str($current_url['query'], $query_args);
} // end if;
}
$redirect_url = false;
if ($redirect_settings === 'force_map' && $current_url_to_compare !== $mapped_url_to_compare) {
$redirect_url = Domain_Mapping::get_instance()->replace_url(wu_get_current_url(), $mapped_domain);
$query_args = array_map(fn($value) => Domain_Mapping::get_instance()->replace_url($value, $mapped_domain), $query_args);
} elseif ($redirect_settings === 'force_network' && $current_url_to_compare === $mapped_url_to_compare) {
$redirect_url = wu_restore_original_url(wu_get_current_url(), $site->get_id());
$query_args = array_map(fn($value) => wu_restore_original_url($value, $site->get_id()), $query_args);
} // end if;
}
if ($redirect_url) {
wp_redirect(add_query_arg($query_args, $redirect_url));
exit;
} // end if;
} // end maybe_redirect_to_mapped_or_network_domain;
} // end class Primary_Domain;
}
}
}

View File

@ -26,5 +26,4 @@ defined('ABSPATH') || exit;
class SSO {
use \WP_Ultimo\Traits\Singleton;
} // end class SSO;
}