You are here

function pwa_requirements in Progressive Web App 7.2

Same name and namespace in other branches
  1. 8 pwa.install \pwa_requirements()
  2. 7 pwa.install \pwa_requirements()
  3. 2.x pwa.install \pwa_requirements()

Implements hook_requirements().

File

./pwa.install, line 9

Code

function pwa_requirements($phase) {
  $requirements = [];
  if ($phase !== 'runtime') {
    return $requirements;
  }
  $t = get_t();
  if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' || isset($_SERVER["REQUEST_SCHEME"]) && $_SERVER["REQUEST_SCHEME"] === 'https') {
    $requirements['pwa'] = array(
      'title' => $t('Progressive Web App'),
      'value' => $t('HTTPS on'),
      'severity' => REQUIREMENT_OK,
      'description' => $t('Please make sure the certificate of %domain is valid for offline functionality to work.', [
        '%domain' => $_SERVER['HTTP_HOST'],
      ]),
      'weight' => 1,
    );
  }
  elseif (in_array($_SERVER['HTTP_HOST'], [
    'localhost',
    '127.0.0.1',
  ])) {
    $requirements['pwa'] = array(
      'title' => $t('Progressive Web App'),
      'value' => 'localhost',
      'severity' => REQUIREMENT_WARNING,
      'description' => $t('You will need to configure HTTPS on your production site for the Progressive Web App to function.'),
      'weight' => 1,
    );
  }
  else {
    $requirements['pwa'] = array(
      'title' => $t('Progressive Web App'),
      'value' => $t('HTTPS off'),
      'severity' => REQUIREMENT_ERROR,
      'description' => $t('HTTPS needs to be configured to enable your Progressive Web App. Without a secure connection, the Service Worker will not install itself.'),
      'weight' => 1,
    );
  }
  if (empty(variable_get('pwa_description', ''))) {
    $requirements['pwa_description'] = array(
      'title' => $t('PWA manifest'),
      'value' => $t('description'),
      'severity' => REQUIREMENT_WARNING,
      'description' => $t('The <a href="@manifest_url">manifest description</a> is required for the PWA to be <a href="@windows_pwa_url">automatically included</a> in the Microsoft Store.', [
        '@manifest_url' => url('/admin/config/pwa/settings', [
          'fragment' => 'edit-pwa-description',
        ]),
        '@windows_pwa_url' => 'https://docs.microsoft.com/en-us/microsoft-edge/progressive-web-apps-edgehtml/microsoft-store#criteria-for-automatic-submission',
      ]),
      'weight' => 2,
    );
  }
  $manifest = pwa_file_data('manifest');
  if (empty($manifest['icons'])) {
    $requirements['pwa_icons'] = array(
      'title' => $t('PWA manifest icons'),
      'value' => $t('No icons'),
      'severity' => REQUIREMENT_ERROR,
      'description' => $t('There are no icons declared in the manifest file. The PWA will not be installable.'),
      'weight' => 2,
    );
  }
  else {
    $has_192 = FALSE;
    $has_512 = FALSE;
    $has_monochrome = FALSE;
    foreach ($manifest['icons'] as $icon) {
      if ($icon['sizes'] === '192x192' && stripos($icon['purpose'], 'maskable') !== FALSE) {
        $has_192 = TRUE;
      }
      if ($icon['sizes'] === '512x512' && stripos($icon['purpose'], 'maskable') !== FALSE) {
        $has_512 = TRUE;
      }
      if ($icon['sizes'] === '16x16' && $icon['purpose'] === 'monochrome' && stripos($icon['type'], 'svg') !== FALSE) {
        $has_monochrome = TRUE;
      }
    }
    if (!$has_192 || !$has_512) {
      $requirements['pwa_icons'] = array(
        'title' => $t('PWA manifest icons'),
        'value' => $t('Missing %icons sizes', [
          '%icons' => trim(implode(' ', [
            $has_192 ? '' : '192x192',
            $has_512 ? '' : '512x512',
          ])),
        ]),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('Icons with these measurements are required to allow an optimal experience and a full lighthouse score.'),
        'weight' => 2,
      );
    }
    if (variable_get('pwa_apple_meta_enable') && !$has_monochrome) {
      $requirements['pwa_icons_apple'] = array(
        'title' => $t('PWA Safari pinned tab icon'),
        'value' => $t('Missing 16x16 monochrome'),
        'severity' => REQUIREMENT_WARNING,
        'description' => $t('A SVG icon with the purpose "monochrome" is missing to ensure integration with <a href="@monochrome_url">safari pinned tabs</a>. This will be used by the PWA module to add the appropriate meta tag on the page.', [
          '@monochrome_url' => 'https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/pinnedTabs/pinnedTabs.html',
        ]),
        'weight' => 2,
      );
    }
  }
  return $requirements;
}