Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/functions/legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Exit if accessed directly
defined('ABSPATH') || exit;

// phpcs:disable
/**
* Return the instance of the function
*/
Expand Down
90 changes: 87 additions & 3 deletions inc/managers/class-domain-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public function handle_site_created($site): void {
$has_subdomain = str_replace($current_site->domain, '', $site->domain);

if ( ! $has_subdomain) {
// Create a domain record for the site
$this->create_domain_record_for_site($site);
return;
}

Expand All @@ -171,6 +173,56 @@ public function handle_site_created($site): void {
];

wu_enqueue_async_action('wu_add_subdomain', $args, 'domain');

// Create a domain record for the site
$this->create_domain_record_for_site($site);
}

/**
* Creates a domain record for a site.
*
* @since 2.4.0
*
* @param \WP_Site $site The site to create a domain record for.
* @return \WP_Error|\WP_Ultimo\Models\Domain
*/
public function create_domain_record_for_site($site) {

// Check if a domain record already exists for this site
$existing_domains = wu_get_domains(
[
'blog_id' => $site->blog_id,
'number' => 1,
]
);

if ( ! empty($existing_domains)) {
return $existing_domains[0];
}

// Create a new domain record
$domain = wu_create_domain(
[
'blog_id' => $site->blog_id,
'domain' => $site->domain,
'active' => true,
'primary_domain' => true,
'secure' => false,
'stage' => 'checking-dns',
]
);

if (is_wp_error($domain)) {
wu_log_add('domain-creation', sprintf('Failed to create domain record for site %d: %s', $site->blog_id, $domain->get_error_message()), LogLevel::ERROR);
return $domain;
}

wu_log_add('domain-creation', sprintf('Created domain record for site %d: %s', $site->blog_id, $site->domain));

// Process the domain stage asynchronously
wu_enqueue_async_action('wu_async_process_domain_stage', ['domain_id' => $domain->get_id()], 'domain');

return $domain;
}

/**
Expand Down Expand Up @@ -321,6 +373,26 @@ public function add_domain_mapping_settings(): void {
],
]
);

wu_register_settings_field(
'domain-mapping',
'dns_check_interval',
[
'title' => __('DNS Check Interval', 'wp-multisite-waas'),
'tooltip' => __('Set the interval in seconds between DNS and SSL certificate checks for domains.', 'wp-multisite-waas'),
'desc' => __('Minimum: 10 seconds, Maximum: 300 seconds (5 minutes). Default: 300 seconds.', 'wp-multisite-waas'),
'type' => 'number',
'default' => 300,
'min' => 10,
'max' => 300,
'html_attr' => [
'step' => 1,
],
'require' => [
'enable_domain_mapping' => true,
],
]
);
}

/**
Expand Down Expand Up @@ -470,7 +542,19 @@ public function async_process_domain_stage($domain_id, $tries = 0): void {

$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
// Get the DNS check interval from settings (in seconds)
$dns_check_interval = wu_get_setting('dns_check_interval', 300);

// Ensure the interval is within the allowed range (10-300 seconds)
$dns_check_interval = max(10, min(300, (int) $dns_check_interval));

// Convert seconds to minutes for the schedule
$try_again_time = ceil($dns_check_interval / 60);

// Ensure we have at least 1 minute
$try_again_time = max(1, $try_again_time);

$try_again_time = apply_filters('wu_async_process_domains_try_again_time', $try_again_time, $domain); // minutes

++$tries;

Expand Down Expand Up @@ -529,7 +613,7 @@ public function async_process_domain_stage($domain_id, $tries = 0): void {
);

wu_schedule_single_action(
strtotime("+{$try_again_time} minutes"),
time() + $dns_check_interval,
'wu_async_process_domain_stage',
[
'domain_id' => $domain_id,
Expand Down Expand Up @@ -579,7 +663,7 @@ public function async_process_domain_stage($domain_id, $tries = 0): void {
);

wu_schedule_single_action(
strtotime("+{$try_again_time} minutes"),
time() + $dns_check_interval,
'wu_async_process_domain_stage',
[
'domain_id' => $domain_id,
Expand Down