<?php

/**
 * Implements hook_init().
 */
function mkbh_paywall_init()
{
    module_load_include('inc', 'mkbh_paywall', 'mkbh_paywall.helpers');
    module_load_include('inc', 'mkbh_paywall', 'mkbh_paywall.forms');
    module_load_include('inc', 'mkbh_paywall', 'mkbh_paywall.fields');

    if (user_is_logged_in()) {
        drupal_page_is_cacheable(false);

        drupal_add_http_header('Cache-Control', 'no-cache, no-store, must-revalidate');
        drupal_add_http_header('Pragma', 'no-cache');
        drupal_add_http_header('Expires', '0');
    }
}

/**
 * Implements hook_menu().
 */
function mkbh_paywall_menu()
{
    // General admin settings page.
    $items['admin/config/paywall'] = [
        'title' => 'Configure Paywall',
        'description' => 'Configure content paywall module settings.',
        'page callback' => 'drupal_get_form',
        'page arguments' => ['mkbh_paywall_settings_form'],
        'access arguments' => ['access administration pages'],
        'file' => 'mkbh_paywall.forms.inc',
        'file path' => drupal_get_path('module', 'mkbh_paywall'),
        'type' => MENU_NORMAL_ITEM,
    ];

    return $items;
}

/**
 * Implements hook_metatag_metatags_view_alter().
 */
function mkbh_paywall_metatag_metatags_view_alter(&$output, $instance, $options) {
  if (isset($options['entity'])) {
    $entity = $options['entity'];

    if (property_exists($entity, 'type') && $entity->type == 'article') {
      $existsPaidOption = database()->table('mkbh_paywall_node_options')
        ->where('nid', $entity->nid)
        ->exists();

      if ($existsPaidOption) {
        return;
      }
    }
  }

  // If there are no subscription settings, then we delete parts of the markup for paid content.
  unset($output['schema_article.hasPart']);
  unset($output['schema_article.isAccessibleForFree']);
}
