function domain_301_redirect_cron in Domain 301 Redirect 8
Same name and namespace in other branches
- 7 domain_301_redirect.module \domain_301_redirect_cron()
Implements hook_cron().
File
- ./
domain_301_redirect.module, line 25 - This module allows you to 301 redirect all domains to one specific domain.
Code
function domain_301_redirect_cron() {
$config = \Drupal::config('domain_301_redirect.settings');
$enabled = $config
->get('enabled');
$domain = $config
->get('domain');
$check_period = $config
->get('check_period');
$last_checked = $config
->get('last_checked');
$reenable = $config
->get('check_reenable');
$disabled_by_check = $config
->get('disabled_by_check');
// If the redirect is enabled (or has been previously disabled) and we are
// checking for domain availability on cron, then attempt to request the test
// url using the redirect domain.
if (($enabled || $disabled_by_check && $reenable) && !empty($check_period) && $last_checked < time() - $check_period) {
if (!preg_match('|^https?://|', $domain)) {
$domain = 'http://' . $domain;
}
$domain_parts = parse_url($domain);
$domain = $domain_parts['scheme'] . '://' . $domain_parts['host'];
$manager = Drupal::service('domain_301_redirect.manager');
$config = \Drupal::configFactory()
->getEditable('domain_301_redirect.settings');
if (!$manager
->checkDomain($domain)) {
$config
->set('enabled', 0)
->set('disabled_by_check', TRUE);
\Drupal::logger('domain_301_redirect')
->error('The domain %domain no longer points to this site. Domain 301 redirection was disabled.', [
'%domain' => $domain,
]);
}
else {
\Drupal::logger('domain_301_redirect')
->info('Domain 301 redirect check passed.');
// If the redirect was disabled by cron, and it has now passed,
// re-enable it.
if (!$enabled && $reenable && $disabled_by_check) {
$config
->set('enabled', 1)
->set('disabled_by_check', FALSE);
\Drupal::logger('domain_301_redirect')
->error('The domain %domain has become available again. Domain 301 redirection was re-enabled.', [
'%domain' => $domain,
]);
}
}
$config
->set('redirect_last_checked', time())
->save();
}
}