You are here

function noreferrer_subscribe in No Referrer 8

Same name and namespace in other branches
  1. 7 noreferrer.admin.inc \noreferrer_subscribe()

Retrieves whitelist from external URL.

2 calls to noreferrer_subscribe()
NoReferrerSettingsForm::submitForm in src/Form/NoReferrerSettingsForm.php
Form submission handler.
noreferrer_cron in ./noreferrer.module
Implements hook_cron().

File

./noreferrer.module, line 70
No Referrer module.

Code

function noreferrer_subscribe($url) {
  try {
    $response = \Drupal::httpClient()
      ->get($url);
  } catch (Exception $e) {
    \Drupal::logger('noreferrer')
      ->error('Error received at %url while retrieving domain whitelist: %message.', [
      '%url' => $url,
      '%message' => $e
        ->getMessage(),
    ]);
    return;
  }
  $whitelist = json_decode((string) $response
    ->getBody());
  if (is_array($whitelist)) {
    \Drupal::configFactory()
      ->getEditable('noreferrer.settings')
      ->set('whitelisted_domains', implode(' ', $whitelist))
      ->save();
  }
  else {
    \Drupal::logger('noreferrer')
      ->error('Unable to extract valid data from %url while retrieving domain whitelist.', [
      '%url' => $url,
    ]);
  }
}