You are here

pwa.install in Progressive Web App 7

Same filename and directory in other branches
  1. 8 pwa.install
  2. 7.2 pwa.install
  3. 2.x pwa.install

File

pwa.install
View source
<?php

/**
 * @file
 */

/**
 * Implements hook_requirements().
 */
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'],
      ]),
    );
  }
  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.'),
    );
  }
  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.'),
    );
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function pwa_install() {
  $directory = file_default_scheme() . '://pwa';
  file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  variable_set('pwa_sw_cache_exclude', implode("\n", PWA_SW_CACHE_EXCLUDE));
  variable_set('pwa_sw_cache_urls', implode("\n", [
    '/',
    '/offline',
    variable_get('pwa_start_url', ''),
  ]));
  pwa_flush_caches();

  // By default give permissions to anonymous users.
  //
  // @TODO: set a status message prompting admins to enable functionality for
  //        other roles themselves.
  //
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, [
    'access pwa',
  ]);
}

/**
 * Implements hook_uninstall().
 */
function pwa_uninstall() {

  // Remove variables.
  db_delete('variable')
    ->condition('name', 'pwa_%', 'LIKE')
    ->execute();
  cache_clear_all('variables', 'cache_bootstrap');

  // Delete manifest.json
  $scheme = file_default_scheme();
  $directory = $scheme . '://pwa';
  $path = $directory . '/manifest.json';
  file_unmanaged_delete($path);
}

Functions

Namesort descending Description
pwa_install Implements hook_install().
pwa_requirements Implements hook_requirements().
pwa_uninstall Implements hook_uninstall().