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

@ -25,7 +25,9 @@ defined('ABSPATH') || exit;
*/
class Domain_Manager extends Base_Manager {
use \WP_Ultimo\Apis\Rest_Api, \WP_Ultimo\Apis\WP_CLI, \WP_Ultimo\Traits\Singleton;
use \WP_Ultimo\Apis\Rest_Api;
use \WP_Ultimo\Apis\WP_CLI;
use \WP_Ultimo\Traits\Singleton;
/**
* The manager slug.
@ -62,8 +64,7 @@ class Domain_Manager extends Base_Manager {
public function get_integrations() {
return apply_filters('wu_domain_manager_get_integrations', $this->integrations, $this);
} // end get_integrations;
}
/**
* Get the instance of one of the integrations classes.
@ -77,17 +78,14 @@ class Domain_Manager extends Base_Manager {
$integrations = $this->get_integrations();
if (isset($integrations[$id])) {
$class_name = $integrations[$id];
if (isset($integrations[ $id ])) {
$class_name = $integrations[ $id ];
return $class_name::get_instance();
} // end if;
}
return false;
} // end get_integration_instance;
}
/**
* Instantiate the necessary hooks.
@ -134,8 +132,7 @@ class Domain_Manager extends Base_Manager {
add_action('wp_insert_site', array($this, 'handle_site_created'));
add_action('wp_delete_site', array($this, 'handle_site_deleted'));
} // end init;
}
/**
* Set COOKIE_DOMAIN if not defined in sites with mapped domains.
@ -146,13 +143,10 @@ class Domain_Manager extends Base_Manager {
*/
protected function set_cookie_domain() {
if (defined('DOMAIN_CURRENT_SITE') && !defined('COOKIE_DOMAIN') && !preg_match('/' . DOMAIN_CURRENT_SITE . '$/', '.' . $_SERVER['HTTP_HOST'])) {
define( 'COOKIE_DOMAIN', '.' . $_SERVER['HTTP_HOST'] );
} // end if;
} // end set_cookie_domain;
if (defined('DOMAIN_CURRENT_SITE') && ! defined('COOKIE_DOMAIN') && ! preg_match('/' . DOMAIN_CURRENT_SITE . '$/', '.' . $_SERVER['HTTP_HOST'])) {
define('COOKIE_DOMAIN', '.' . $_SERVER['HTTP_HOST']);
}
}
/**
* Triggers subdomain mapping events on site creation.
@ -168,11 +162,9 @@ class Domain_Manager extends Base_Manager {
$has_subdomain = str_replace($current_site->domain, '', $site->domain);
if (!$has_subdomain) {
if ( ! $has_subdomain) {
return;
} // end if;
}
$args = array(
'subdomain' => $site->domain,
@ -180,8 +172,7 @@ class Domain_Manager extends Base_Manager {
);
wu_enqueue_async_action('wu_add_subdomain', $args, 'domain');
} // end handle_site_created;
}
/**
* Triggers subdomain mapping events on site deletion.
@ -197,11 +188,9 @@ class Domain_Manager extends Base_Manager {
$has_subdomain = str_replace($current_site->domain, '', $site->domain);
if (!$has_subdomain) {
if ( ! $has_subdomain) {
return;
} // end if;
}
$args = array(
'subdomain' => $site->domain,
@ -209,8 +198,7 @@ class Domain_Manager extends Base_Manager {
);
wu_enqueue_async_action('wu_remove_subdomain', $args, 'domain');
} // end handle_site_deleted;
}
/**
* Triggers the do_event of the payment successful.
@ -232,8 +220,7 @@ class Domain_Manager extends Base_Manager {
);
wu_do_event('domain_created', $payload);
} // end handle_domain_created;
}
/**
* Remove send domain removal event.
@ -247,17 +234,14 @@ class Domain_Manager extends Base_Manager {
public function handle_domain_deleted($result, $domain) {
if ($result) {
$args = array(
'domain' => $domain->get_domain(),
'site_id' => $domain->get_site_id(),
);
wu_enqueue_async_action('wu_remove_domain', $args, 'domain');
} // end if;
} // end handle_domain_deleted;
}
}
/**
* Add all domain mapping settings.
@ -267,59 +251,78 @@ class Domain_Manager extends Base_Manager {
*/
public function add_domain_mapping_settings() {
wu_register_settings_field('domain-mapping', 'domain_mapping_header', array(
'title' => __('Domain Mapping Settings', 'wp-ultimo'),
'desc' => __('Define the domain mapping settings for your network.', 'wp-ultimo'),
'type' => 'header',
));
wu_register_settings_field(
'domain-mapping',
'domain_mapping_header',
array(
'title' => __('Domain Mapping Settings', 'wp-ultimo'),
'desc' => __('Define the domain mapping settings for your network.', 'wp-ultimo'),
'type' => 'header',
)
);
wu_register_settings_field('domain-mapping', 'enable_domain_mapping', array(
'title' => __('Enable Domain Mapping?', 'wp-ultimo'),
'desc' => __('Do you want to enable domain mapping?', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
));
wu_register_settings_field(
'domain-mapping',
'enable_domain_mapping',
array(
'title' => __('Enable Domain Mapping?', 'wp-ultimo'),
'desc' => __('Do you want to enable domain mapping?', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
)
);
wu_register_settings_field('domain-mapping', 'force_admin_redirect', array(
'title' => __('Force Admin Redirect', 'wp-ultimo'),
'desc' => __('Select how you want your users to access the admin panel if they have mapped domains.', 'wp-ultimo') . '<br><br>' . __('Force Redirect to Mapped Domain: your users with mapped domains will be redirected to theirdomain.com/wp-admin, even if they access using yournetworkdomain.com/wp-admin.', 'wp-ultimo') . '<br><br>' . __('Force Redirect to Network Domain: your users with mapped domains will be redirect to yournetworkdomain.com/wp-admin, even if they access using theirdomain.com/wp-admin.', 'wp-ultimo'),
'tooltip' => '',
'type' => 'select',
'default' => 'both',
'require' => array('enable_domain_mapping' => 1),
'options' => array(
'both' => __('Allow access to the admin by both mapped domain and network domain', 'wp-ultimo'),
'force_map' => __('Force Redirect to Mapped Domain', 'wp-ultimo'),
'force_network' => __('Force Redirect to Network Domain', 'wp-ultimo'),
),
));
wu_register_settings_field(
'domain-mapping',
'force_admin_redirect',
array(
'title' => __('Force Admin Redirect', 'wp-ultimo'),
'desc' => __('Select how you want your users to access the admin panel if they have mapped domains.', 'wp-ultimo') . '<br><br>' . __('Force Redirect to Mapped Domain: your users with mapped domains will be redirected to theirdomain.com/wp-admin, even if they access using yournetworkdomain.com/wp-admin.', 'wp-ultimo') . '<br><br>' . __('Force Redirect to Network Domain: your users with mapped domains will be redirect to yournetworkdomain.com/wp-admin, even if they access using theirdomain.com/wp-admin.', 'wp-ultimo'),
'tooltip' => '',
'type' => 'select',
'default' => 'both',
'require' => array('enable_domain_mapping' => 1),
'options' => array(
'both' => __('Allow access to the admin by both mapped domain and network domain', 'wp-ultimo'),
'force_map' => __('Force Redirect to Mapped Domain', 'wp-ultimo'),
'force_network' => __('Force Redirect to Network Domain', 'wp-ultimo'),
),
)
);
wu_register_settings_field('domain-mapping', 'custom_domains', array(
'title' => __('Enable Custom Domains?', 'wp-ultimo'),
'desc' => __('Toggle this option if you wish to allow end-customers to add their own domains. This can be controlled on a plan per plan basis.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
'require' => array(
'enable_domain_mapping' => true,
),
));
wu_register_settings_field(
'domain-mapping',
'custom_domains',
array(
'title' => __('Enable Custom Domains?', 'wp-ultimo'),
'desc' => __('Toggle this option if you wish to allow end-customers to add their own domains. This can be controlled on a plan per plan basis.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
'require' => array(
'enable_domain_mapping' => true,
),
)
);
wu_register_settings_field('domain-mapping', 'domain_mapping_instructions', array(
'title' => __('Add New Domain Instructions', 'wp-ultimo'),
'tooltip' => __('Display a customized message with instructions for the mapping and alerting the end-user of the risks of mapping a misconfigured domain.', 'wp-ultimo'),
'desc' => __('You can use the placeholder <code>%NETWORK_DOMAIN%</code> and <code>%NETWORK_IP%</code>.', 'wp-ultimo'),
'type' => 'textarea',
'default' => array($this, 'default_domain_mapping_instructions'),
'html_attr' => array(
'rows' => 8,
),
'require' => array(
'enable_domain_mapping' => true,
'custom_domains' => true,
),
));
} // end add_domain_mapping_settings;
wu_register_settings_field(
'domain-mapping',
'domain_mapping_instructions',
array(
'title' => __('Add New Domain Instructions', 'wp-ultimo'),
'tooltip' => __('Display a customized message with instructions for the mapping and alerting the end-user of the risks of mapping a misconfigured domain.', 'wp-ultimo'),
'desc' => __('You can use the placeholder <code>%NETWORK_DOMAIN%</code> and <code>%NETWORK_IP%</code>.', 'wp-ultimo'),
'type' => 'textarea',
'default' => array($this, 'default_domain_mapping_instructions'),
'html_attr' => array(
'rows' => 8,
),
'require' => array(
'enable_domain_mapping' => true,
'custom_domains' => true,
),
)
);
}
/**
* Add all SSO settings.
@ -329,40 +332,55 @@ class Domain_Manager extends Base_Manager {
*/
public function add_sso_settings() {
wu_register_settings_field('sso', 'sso_header', array(
'title' => __('Single Sign-On Settings', 'wp-ultimo'),
'desc' => __('Settings to configure the Single Sign-On functionality of WP Multisite WaaS, responsible for keeping customers and admins logged in across all network domains.', 'wp-ultimo'),
'type' => 'header',
));
wu_register_settings_field(
'sso',
'sso_header',
array(
'title' => __('Single Sign-On Settings', 'wp-ultimo'),
'desc' => __('Settings to configure the Single Sign-On functionality of WP Multisite WaaS, responsible for keeping customers and admins logged in across all network domains.', 'wp-ultimo'),
'type' => 'header',
)
);
wu_register_settings_field('sso', 'enable_sso', array(
'title' => __('Enable Single Sign-On', 'wp-ultimo'),
'desc' => __('Enables the Single Sign-on functionality.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
));
wu_register_settings_field(
'sso',
'enable_sso',
array(
'title' => __('Enable Single Sign-On', 'wp-ultimo'),
'desc' => __('Enables the Single Sign-on functionality.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
)
);
wu_register_settings_field('sso', 'restrict_sso_to_login_pages', array(
'title' => __('Restrict SSO Checks to Login Pages', 'wp-ultimo'),
'desc' => __('The Single Sign-on feature adds one extra ajax calls to every page load on sites with custom domains active to check if it should perform an auth loopback. You can restrict these extra calls to the login pages of sub-sites using this option. If enabled, SSO will only work on login pages.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 0,
'require' => array(
'enable_sso' => true,
),
));
wu_register_settings_field(
'sso',
'restrict_sso_to_login_pages',
array(
'title' => __('Restrict SSO Checks to Login Pages', 'wp-ultimo'),
'desc' => __('The Single Sign-on feature adds one extra ajax calls to every page load on sites with custom domains active to check if it should perform an auth loopback. You can restrict these extra calls to the login pages of sub-sites using this option. If enabled, SSO will only work on login pages.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 0,
'require' => array(
'enable_sso' => true,
),
)
);
wu_register_settings_field('sso', 'enable_sso_loading_overlay', array(
'title' => __('Enable SSO Loading Overlay', 'wp-ultimo'),
'desc' => __('When active, a loading overlay will be added on-top of the site currently being viewed while the SSO auth loopback is performed on the background.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
'require' => array(
'enable_sso' => true,
),
));
} // end add_sso_settings;
wu_register_settings_field(
'sso',
'enable_sso_loading_overlay',
array(
'title' => __('Enable SSO Loading Overlay', 'wp-ultimo'),
'desc' => __('When active, a loading overlay will be added on-top of the site currently being viewed while the SSO auth loopback is performed on the background.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
'require' => array(
'enable_sso' => true,
),
)
);
}
/**
* Returns the default instructions for domain mapping.
*
@ -379,8 +397,7 @@ class Domain_Manager extends Base_Manager {
$instructions[] = __('After you finish that step, come back to this screen and click the button below.', 'wp-ultimo');
return implode(PHP_EOL . PHP_EOL, $instructions);
} // end default_domain_mapping_instructions;
}
/**
* Gets the instructions, filtered and without the shortcodes.
@ -394,11 +411,9 @@ class Domain_Manager extends Base_Manager {
$instructions = wu_get_setting('domain_mapping_instructions');
if (!$instructions) {
if ( ! $instructions) {
$instructions = $this->default_domain_mapping_instructions();
} // end if;
}
$domain = $current_site->domain;
$ip = Helper::get_network_public_ip();
@ -410,8 +425,7 @@ class Domain_Manager extends Base_Manager {
$instructions = str_replace('%NETWORK_IP%', $ip, $instructions);
return apply_filters('wu_get_domain_mapping_instructions', $instructions, $domain, $ip);
} // end get_domain_mapping_instructions;
}
/**
* Creates the event to save the transition.
@ -426,7 +440,6 @@ class Domain_Manager extends Base_Manager {
public function send_domain_to_host($old_value, $new_value, $item_id) {
if ($old_value !== $new_value) {
$domain = wu_get_domain($item_id);
$args = array(
@ -435,10 +448,8 @@ class Domain_Manager extends Base_Manager {
);
wu_enqueue_async_action('wu_add_domain', $args, 'domain');
} // end if;
} // end send_domain_to_host;
}
}
/**
* Checks the DNS and SSL status of a domain.
@ -453,17 +464,15 @@ class Domain_Manager extends Base_Manager {
$domain = wu_get_domain($domain_id);
if (!$domain) {
if ( ! $domain) {
return;
} // end if;
}
$max_tries = apply_filters('wu_async_process_domain_stage_max_tries', 5, $domain);
$try_again_time = apply_filters('wu_async_process_domains_try_again_time', 5, $domain); // minutes
$tries++;
++$tries;
$stage = $domain->get_stage();
@ -473,9 +482,7 @@ class Domain_Manager extends Base_Manager {
wu_log_add("domain-{$domain_url}", sprintf(__('Starting Check for %s', 'wp-ultimo'), $domain_url));
if ($stage === 'checking-dns') {
if ($domain->has_correct_dns()) {
$domain->set_stage('checking-ssl-cert');
$domain->save();
@ -485,18 +492,23 @@ class Domain_Manager extends Base_Manager {
__('- DNS propagation finished, advancing domain to next step...', 'wp-ultimo')
);
wu_enqueue_async_action('wu_async_process_domain_stage', array('domain_id' => $domain_id, 'tries' => 0), 'domain');
wu_enqueue_async_action(
'wu_async_process_domain_stage',
array(
'domain_id' => $domain_id,
'tries' => 0,
),
'domain'
);
do_action('wu_domain_manager_dns_propagation_finished', $domain);
return;
} else {
/*
* Max attempts
*/
if ($tries > $max_tries) {
$domain->set_stage('failed');
$domain->save();
@ -508,8 +520,7 @@ class Domain_Manager extends Base_Manager {
);
return;
} // end if;
}
wu_log_add(
"domain-{$domain_url}",
@ -528,13 +539,9 @@ class Domain_Manager extends Base_Manager {
);
return;
} // end if;
}
} elseif ($stage === 'checking-ssl-cert') {
if ($domain->has_valid_ssl_certificate()) {
$domain->set_stage('done');
$domain->set_secure(true);
@ -547,13 +554,11 @@ class Domain_Manager extends Base_Manager {
);
return;
} else {
/*
* Max attempts
*/
if ($tries > $max_tries) {
$domain->set_stage('done-without-ssl');
$domain->save();
@ -565,8 +570,7 @@ class Domain_Manager extends Base_Manager {
);
return;
} // end if;
}
wu_log_add(
"domain-{$domain_url}",
@ -574,15 +578,20 @@ class Domain_Manager extends Base_Manager {
sprintf(__('- SSL Cert not found, retrying in %d minute(s)...', 'wp-ultimo'), $try_again_time)
);
wu_schedule_single_action(strtotime("+{$try_again_time} minutes"), 'wu_async_process_domain_stage', array('domain_id' => $domain_id, 'tries' => $tries), 'domain');
wu_schedule_single_action(
strtotime("+{$try_again_time} minutes"),
'wu_async_process_domain_stage',
array(
'domain_id' => $domain_id,
'tries' => $tries,
),
'domain'
);
return;
} // end if;
} // end if;
} // end async_process_domain_stage;
}
}
}
/**
* Alternative implementation for PHP's native dns_get_record.
@ -606,7 +615,6 @@ class Domain_Manager extends Base_Manager {
);
foreach ($record_types as $record_type) {
$chain = new \RemotelyLiving\PHPDNS\Resolvers\Chain(
new \RemotelyLiving\PHPDNS\Resolvers\CloudFlare(),
new \RemotelyLiving\PHPDNS\Resolvers\GoogleDNS(),
@ -617,7 +625,6 @@ class Domain_Manager extends Base_Manager {
$records = $chain->getRecords($domain, $record_type);
foreach ($records as $record_data) {
$record = array();
$record['type'] = $record_type;
@ -625,10 +632,8 @@ class Domain_Manager extends Base_Manager {
$record['data'] = (string) $record_data->getData();
if (empty($record['data'])) {
$record['data'] = (string) $record_data->getIPAddress();
} // end if;
}
// Some DNS providers return a trailing dot.
$record['data'] = rtrim($record['data'], '.');
@ -642,14 +647,11 @@ class Domain_Manager extends Base_Manager {
$record['tag'] = ''; // Used by integrations.
$results[] = $record;
} // end foreach;
} // end foreach;
}
}
return apply_filters('wu_domain_dns_get_record', $results, $domain);
} // end dns_get_record;
}
/**
* Get the DNS records for a given domain.
@ -661,42 +663,41 @@ class Domain_Manager extends Base_Manager {
$domain = wu_request('domain');
if (!$domain) {
if ( ! $domain) {
wp_send_json_error(new \WP_Error('domain-missing', __('A valid domain was not passed.', 'wp-ultimo')));
} // end if;
}
$auth_ns = array();
$additional = array();
try {
$result = self::dns_get_record($domain);
} catch (\Throwable $e) {
wp_send_json_error(new \WP_Error('error', __('Not able to fetch DNS entries.', 'wp-ultimo'), array(
'exception' => $e->getMessage(),
)));
} // end try;
wp_send_json_error(
new \WP_Error(
'error',
__('Not able to fetch DNS entries.', 'wp-ultimo'),
array(
'exception' => $e->getMessage(),
)
)
);
}
if ($result === false) {
wp_send_json_error(new \WP_Error('error', __('Not able to fetch DNS entries.', 'wp-ultimo')));
}
} // end if;
wp_send_json_success(array(
'entries' => $result,
'auth' => $auth_ns,
'additional' => $additional,
'network_ip' => Helper::get_network_public_ip(),
));
} // end get_dns_records;
wp_send_json_success(
array(
'entries' => $result,
'auth' => $auth_ns,
'additional' => $additional,
'network_ip' => Helper::get_network_public_ip(),
)
);
}
/**
* Takes the list of domains and set them to non-primary when a new primary is added.
@ -711,20 +712,15 @@ class Domain_Manager extends Base_Manager {
public function async_remove_old_primary_domains($domains) {
foreach ($domains as $domain_id) {
$domain = wu_get_domain($domain_id);
if ($domain) {
$domain->set_primary_domain(false);
$domain->save();
} // end if;
} // end foreach;
} // end async_remove_old_primary_domains;
}
}
}
/**
* Tests the integration in the Wizard context.
@ -738,31 +734,30 @@ class Domain_Manager extends Base_Manager {
$integration = $this->get_integration_instance($integration_id);
if (!$integration) {
wp_send_json_error(array(
'message' => __('Invalid Integration ID', 'wp-ultimo'),
));
} // end if;
if ( ! $integration) {
wp_send_json_error(
array(
'message' => __('Invalid Integration ID', 'wp-ultimo'),
)
);
}
/*
* Checks for the constants...
*/
if (!$integration->is_setup()) {
wp_send_json_error(array(
'message' => sprintf(
__('The necessary constants were not found on your wp-config.php file: %s', 'wp-ultimo'),
implode(', ', $integration->get_missing_constants())
),
));
} // end if;
if ( ! $integration->is_setup()) {
wp_send_json_error(
array(
'message' => sprintf(
__('The necessary constants were not found on your wp-config.php file: %s', 'wp-ultimo'),
implode(', ', $integration->get_missing_constants())
),
)
);
}
return $integration->test_connection();
} // end test_integration;
}
/**
* Loads all the host provider integrations we have available.
@ -818,11 +813,9 @@ class Domain_Manager extends Base_Manager {
/**
* Allow developers to add their own host provider integrations via wp plugins.
*
*
* @since 2.0.0
*/
do_action('wp_ultimo_host_providers_load');
} // end load_integrations;
} // end class Domain_Manager;
}
}