You are here

class AddThisScriptManager in AddThis 7.4

@file Class definition of a script manager.

This class will be used on different places. The result of the attachJsToElement() should be the same in every situation within one request and throughout the loading of the site.

When manipulating the configuration do this very early in the request. This could be hook_init() for example. Any other method should be before hook_page_build(). The implementation of addthis_page_build() is the first known instance where this class might get used based on the configuration.

Hierarchy

Expanded class hierarchy of AddThisScriptManager

File

classes/Services/AddThisScriptManager.php, line 16
Class definition of a script manager.

View source
class AddThisScriptManager {
  private $addthis = NULL;
  private $async = NULL;
  private $domready = NULL;

  /**
   * Construct method.
   */
  private function __construct() {
    $this->addthis = AddThis::getInstance();
    $this->async = $this->addthis
      ->getWidgetJsAsync();
    $this->domready = $this->addthis
      ->getWidgetJsDomReady();
  }

  /**
   * Return a single instance of the AddThisScriptManager.
   *
   * @return AddThisScriptManager
   */
  public static function getInstance() {
    static $manager;
    if (!isset($manager)) {
      $manager = new AddThisScriptManager();
    }
    return $manager;
  }

  /**
   * Get the current widget js url.
   *
   * @return string
   *   A url reference to the widget js.
   */
  public function getWidgetJsUrl() {
    return check_url(variable_get(AddThis::WIDGET_JS_URL_KEY, AddThis::DEFAULT_WIDGET_JS_URL));
  }

  /**
   * Return if we are on https connection.
   *
   * @return bool
   *   TRUE if the current request is on https.
   */
  public function isHttps() {
    global $is_https;
    return $is_https;
  }

  /**
   * Change the schema from http to https if we are on https.
   *
   * @param  string $url
   *   A full url.
   *
   * @return string
   *   The changed url.
   */
  public function correctSchemaIfHttps($url) {
    if (is_string($url) && $this
      ->isHttps()) {
      return str_replace('http://', 'https://', $url);
    }
    else {
      return $url;
    }
    throw new InvalidArgumentException('The argument was not a string value');
  }

  /**
   * Attach the widget js to the element.
   *
   * @todo Change the scope of the addthis.js.
   *   See if we can get the scope of the addthis.js into the header
   *   just below the settings so that the settings can be used in the loaded
   *   addthis.js of our module.
   *
   * @param array $element
   *   The element to attach the JavaScript to.
   */
  public function attachJsToElement(&$element) {
    if ($this->addthis
      ->getWidgetJsInclude() != AddThis::WIDGET_JS_INCLUDE_NONE) {
      $widget_js = new AddThisWidgetJsUrl($this
        ->getWidgetJsUrl());
      $pubid = $this->addthis
        ->getProfileId();
      if (!empty($pubid) && is_string($pubid)) {
        $widget_js
          ->addAttribute('pubid', $pubid);
      }
      $async = $this->async;
      if ($async) {
        $widget_js
          ->addAttribute('async', 1);
      }
      $domready = $this->domready;
      if ($domready) {
        $widget_js
          ->addAttribute('domready', 1);
      }

      // Only when the script is not loaded after the DOM is ready we include
      // the script with #attached.
      if (!$domready) {
        $element['#attached']['js'][$widget_js
          ->getFullUrl()] = array(
          'type' => 'external',
          'scope' => 'footer',
        );
      }

      // Every setting value passed here overrides previously set values but
      // leaves the values that are already set somewhere else and that are not
      // passed here.
      $element['#attached']['js'][] = array(
        'type' => 'setting',
        'data' => array(
          'addthis' => array(
            'async' => $async,
            'domready' => $domready,
            'widget_url' => $widget_js
              ->getFullUrl(),
            'addthis_config' => $this
              ->getJsAddThisConfig(),
            'addthis_share' => $this
              ->getJsAddThisShare(),
          ),
        ),
      );
    }
  }

  /**
   * Enable / disable domready loading.
   *
   * @param bool $enabled
   *   TRUE to enabled domready loading.
   */
  function setDomReady($enabled) {
    $this->domready = $enabled;
  }

  /**
   * Enable / disable async loading.
   *
   * @param bool $enabled
   *   TRUE to enabled async loading.
   */
  function setAsync($enabled) {
    $this->async = $enabled;
  }

  /**
   * Get a array with all addthis_config values.
   *
   * Allow alter through 'addthis_configuration'.
   *
   * @todo Add static cache.
   *
   * @todo Make the adding of configuration dynamic.
   *   SRP is lost here.
   */
  private function getJsAddThisConfig() {
    global $language;
    $enabled_services = $this->addthis
      ->getServiceNamesAsCommaSeparatedString($this->addthis
      ->getEnabledServices()) . 'more';
    $excluded_services = $this->addthis
      ->getServiceNamesAsCommaSeparatedString($this->addthis
      ->getExcludedServices());
    $configuration = array(
      'pubid' => $this->addthis
        ->getProfileId(),
      'services_compact' => $enabled_services,
      'services_exclude' => $excluded_services,
      'data_track_clickback' => $this->addthis
        ->isClickbackTrackingEnabled(),
      'ui_508_compliant' => $this->addthis
        ->get508Compliant(),
      'ui_click' => $this->addthis
        ->isClickToOpenCompactMenuEnabled(),
      'ui_cobrand' => $this->addthis
        ->getCoBrand(),
      'ui_delay' => $this->addthis
        ->getUiDelay(),
      'ui_header_background' => $this->addthis
        ->getUiHeaderBackgroundColor(),
      'ui_header_color' => $this->addthis
        ->getUiHeaderColor(),
      'ui_open_windows' => $this->addthis
        ->isOpenWindowsEnabled(),
      'ui_use_css' => $this->addthis
        ->isStandardCssEnabled(),
      'ui_use_addressbook' => $this->addthis
        ->isAddressbookEnabled(),
      'ui_language' => $language->language,
    );
    if (module_exists('googleanalytics')) {
      if ($this->addthis
        ->isGoogleAnalyticsTrackingEnabled()) {
        $configuration['data_ga_property'] = variable_get('googleanalytics_account', '');
        $configuration['data_ga_social'] = $this->addthis
          ->isGoogleAnalyticsSocialTrackingEnabled();
      }
    }
    drupal_alter('addthis_configuration', $configuration);
    return $configuration;
  }

  /**
   * Get a array with all addthis_share values.
   *
   * Allow alter through 'addthis_configuration_share'.
   *
   * @todo Add static cache.
   *
   * @todo Make the adding of configuration dynamic.
   *   SRP is lost here.
   */
  private function getJsAddThisShare() {
    $configuration = $this
      ->getJsAddThisConfig();
    if (isset($configuration['templates'])) {
      $addthis_share = array(
        'templates' => $configuration['templates'],
      );
    }
    $twitter_via = $this->addthis
      ->getTwitterVia();
    if (!empty($twitter_via)) {
      $addthis_share['passthrough']['twitter']['via'] = $twitter_via;
    }
    $twitter_text = $this->addthis
      ->getTwitterText();
    if (!empty($twitter_text)) {
      $addthis_share['passthrough']['twitter']['text'] = $twitter_text;
    }
    drupal_alter('addthis_configuration_share', $configuration);
    return $addthis_share;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddThisScriptManager::$addthis private property
AddThisScriptManager::$async private property
AddThisScriptManager::$domready private property
AddThisScriptManager::attachJsToElement public function Attach the widget js to the element.
AddThisScriptManager::correctSchemaIfHttps public function Change the schema from http to https if we are on https.
AddThisScriptManager::getInstance public static function Return a single instance of the AddThisScriptManager.
AddThisScriptManager::getJsAddThisConfig private function Get a array with all addthis_config values.
AddThisScriptManager::getJsAddThisShare private function Get a array with all addthis_share values.
AddThisScriptManager::getWidgetJsUrl public function Get the current widget js url.
AddThisScriptManager::isHttps public function Return if we are on https connection.
AddThisScriptManager::setAsync function Enable / disable async loading.
AddThisScriptManager::setDomReady function Enable / disable domready loading.
AddThisScriptManager::__construct private function Construct method.