You are here

class DisqusCommentManager in Disqus 8

It contains common functions to manage disqus_comment fields.

Hierarchy

Expanded class hierarchy of DisqusCommentManager

2 files declare their use of DisqusCommentManager
DisqusBaseBlock.php in src/Plugin/Block/DisqusBaseBlock.php
DisqusCommentCount.php in src/Plugin/views/field/DisqusCommentCount.php
1 string reference to 'DisqusCommentManager'
disqus.services.yml in ./disqus.services.yml
disqus.services.yml
1 service uses DisqusCommentManager
disqus.manager in ./disqus.services.yml
Drupal\disqus\DisqusCommentManager

File

src/DisqusCommentManager.php, line 18

Namespace

Drupal\disqus
View source
class DisqusCommentManager implements DisqusCommentManagerInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity field manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Configuration Factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Constructs the DisqusCommentManager object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   A module handler.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   A module handler.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AccountInterface $current_user, ModuleHandlerInterface $module_handler, ConfigFactory $config_factory) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->currentUser = $current_user;
    $this->moduleHandler = $module_handler;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function getFields($entity_type_id) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    if (!$entity_type
      ->entityClassImplements('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {
      return [];
    }
    $map = $this
      ->getAllFields();
    return isset($map[$entity_type_id]) ? $map[$entity_type_id] : [];
  }

  /**
   * {@inheritdoc}
   */
  public function getAllFields() {
    $map = $this->entityFieldManager
      ->getFieldMap();

    // Build a list of disqus comment fields only.
    $disqus_comment_fields = [];
    foreach ($map as $entity_type => $data) {
      foreach ($data as $field_name => $field_info) {
        if ($field_info['type'] == 'disqus_comment') {
          $disqus_comment_fields[$entity_type][$field_name] = $field_info;
        }
      }
    }
    return $disqus_comment_fields;
  }

  /**
   * {@inheritdoc}
   */
  public function ssoSettings() {
    $disqus['sso'] = [
      'name' => $this->configFactory
        ->get('system.site')
        ->get('name'),
      // The login window must be closed once the user logs in.
      'url' => Url::fromRoute('user.login', [], [
        'query' => [
          'destination' => Url::fromRoute('disqus.close_window')
            ->toString(),
        ],
        'absolute' => TRUE,
      ])
        ->toString(),
      // The logout link must redirect back to the original page.
      'logout' => Url::fromRoute('user.logout', [], [
        'query' => [
          'destination' => Url::fromRoute('<current>')
            ->toString(),
        ],
        'absolute' => TRUE,
      ])
        ->toString(),
      'width' => 800,
      'height' => 600,
    ];
    $managed_logo = $this->configFactory
      ->get('disqus.settings')
      ->get('advanced.sso.disqus_logo');
    $use_site_logo = $this->configFactory
      ->get('disqus.settings')
      ->get('advanced.sso.disqus_use_site_logo');
    if (!$use_site_logo && !empty($managed_logo)) {
      $disqus['sso']['button'] = $this->entityTypeManager
        ->getStorage('file')
        ->load($managed_logo)
        ->url();
    }
    elseif ($logo = theme_get_setting('logo')) {
      $url = $logo['url'];
      if (!UrlHelper::isExternal($url)) {
        $url = Url::fromUri('internal:' . $url, [
          'absolute' => TRUE,
        ])
          ->toString();
      }
      $disqus['sso']['button'] = $url;
    }
    else {
      $disqus['sso']['button'] = Url::fromUri('base://core/misc/druplicon.png', [
        'absolute' => TRUE,
      ])
        ->toString();
    }
    if ($favicon = theme_get_setting('favicon')) {
      $disqus['sso']['icon'] = $favicon['url'];
    }

    // Stick the authentication requirements and data in the settings.
    $disqus['api_key'] = $this->configFactory
      ->get('disqus.settings')
      ->get('advanced.disqus_publickey');
    $disqus['remote_auth_s3'] = $this
      ->ssoKeyEncode($this
      ->ssoUserData());
    return $disqus;
  }

  /**
   * Assembles the full private key for use in SSO authentication.
   *
   * @param array $data
   *   An array contating data.
   *
   * @return string
   *   The String containing message, timestamp.
   */
  protected function ssoKeyEncode(array $data) {

    // Encode the data to be sent off to Disqus.
    $message = base64_encode(json_encode($data));
    $timestamp = time();
    $hmac = hash_hmac('sha1', "{$message} {$timestamp}", $this->configFactory
      ->get('disqus.settings')
      ->get('advanced.disqus_secretkey'));
    return "{$message} {$hmac} {$timestamp}";
  }

  /**
   * Assembles user-specific data used by Disqus SSO.
   *
   * @return array
   *   An array containing sso user data.
   */
  protected function ssoUserData() {
    $account = $this->currentUser;
    $data = [];
    if (!$account
      ->isAnonymous()) {
      $data['id'] = $account
        ->id();
      $data['username'] = $account
        ->getAccountName();
      $data['email'] = $account
        ->getEmail();
      $data['url'] = Url::fromRoute('entity.user.canonical', [
        'user' => $account
          ->id(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString();

      // Load the user's avatar.
      $user_picture_default = $this->configFactory
        ->get('field.instance.user.user.user_picture')
        ->get('settings.default_image');
      $user = $this->entityTypeManager
        ->getStorage('user')
        ->load($account
        ->id());
      if (isset($user->user_picture->target_id) && !empty($user->user_picture
        ->isEmpty()) && ($file = $this->entityTypeManager
        ->getStorage('file')
        ->load($user->user_picture->entity
        ->getFileUri()))) {
        $file_uri = $file
          ->getFileUri();
        $data['avatar'] = !empty($file_uri) ? $file_uri : NULL;
      }
      elseif (!empty($user_picture_default['fid']) && ($file = $this->entityTypeManager
        ->getStorage('file')
        ->load($user_picture_default['fid']))) {
        $file_uri = $file
          ->getFileUri();
        $data['avatar'] = !empty($file_uri) ? $file_uri : NULL;
      }
      if (isset($data['avatar'])) {
        $data['avatar'] = file_create_url($data['avatar']);
      }
    }
    $this->moduleHandler
      ->alter('disqus_user_data', $data);
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DisqusCommentManager::$configFactory protected property Configuration Factory.
DisqusCommentManager::$currentUser protected property The current user.
DisqusCommentManager::$entityFieldManager protected property The entity field manager service.
DisqusCommentManager::$entityTypeManager protected property The entity type manager service.
DisqusCommentManager::$moduleHandler protected property The module handler service.
DisqusCommentManager::getAllFields public function Utility function to return all disqus_comment fields. Overrides DisqusCommentManagerInterface::getAllFields
DisqusCommentManager::getFields public function Utility function to return an array of disqus_comment fields. Overrides DisqusCommentManagerInterface::getFields
DisqusCommentManager::ssoKeyEncode protected function Assembles the full private key for use in SSO authentication.
DisqusCommentManager::ssoSettings public function Computes the full settings associated with Disqus SSO. Overrides DisqusCommentManagerInterface::ssoSettings
DisqusCommentManager::ssoUserData protected function Assembles user-specific data used by Disqus SSO.
DisqusCommentManager::__construct public function Constructs the DisqusCommentManager object.
DisqusCommentManagerInterface::DISQUS_API_CLOSE constant API Close on entity delete.
DisqusCommentManagerInterface::DISQUS_API_NO_ACTION constant API No action on entity delete.
DisqusCommentManagerInterface::DISQUS_API_REMOVE constant API Remove on entity delete.