Projet

Général

Profil

Paste
Télécharger (15,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / addthis / classes / AddThis.php @ fc3d89c3

1 85ad3d82 Assos Assos
<?php
2
/**
3
 * @file
4
 * An AddThis-class.
5
 */
6
7
class AddThis {
8
9
  const BLOCK_NAME = 'addthis_block';
10
  const DEFAULT_CUSTOM_CONFIGURATION_CODE = 'var addthis_config = {}';
11
  const DEFAULT_FORMATTER = 'addthis_default_formatter';
12
  const DEFAULT_NUMBER_OF_PREFERRED_SERVICES = 4;
13
  const FIELD_TYPE = 'addthis';
14
  const MODULE_NAME = 'addthis';
15
  const PERMISSION_ADMINISTER_ADDTHIS = 'administer addthis';
16
  const PERMISSION_ADMINISTER_ADVANCED_ADDTHIS = 'administer advanced addthis';
17
  const STYLE_KEY = 'addthis_style';
18
  const WIDGET_TYPE = 'addthis_button_widget';
19
20 4543c6c7 Assos Assos
  // AddThis attribute and parameter names (as defined in AddThis APIs).
21 85ad3d82 Assos Assos
  const PROFILE_ID_QUERY_PARAMETER = 'pubid';
22
  const TITLE_ATTRIBUTE = 'addthis:title';
23
  const URL_ATTRIBUTE = 'addthis:url';
24
25 4543c6c7 Assos Assos
  // Persistent variable keys.
26 85ad3d82 Assos Assos
  const ADDRESSBOOK_ENABLED_KEY = 'addthis_addressbook_enabled';
27
  const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type';
28 4543c6c7 Assos Assos
  const BLOCK_WIDGET_SETTINGS_KEY = 'addthis_block_widget_settings';
29 85ad3d82 Assos Assos
  const BOOKMARK_URL_KEY = 'addthis_bookmark_url';
30
  const CLICKBACK_TRACKING_ENABLED_KEY = 'addthis_clickback_tracking_enabled';
31
  const CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY = 'addthis_click_to_open_compact_menu_enabled';
32
  const CO_BRAND_KEY = 'addthis_co_brand';
33
  const COMPLIANT_508_KEY = 'addthis_508_compliant';
34
  const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY = 'addthis_custom_configuration_code_enabled';
35
  const CUSTOM_CONFIGURATION_CODE_KEY = 'addthis_custom_configuration_code';
36
  const ENABLED_SERVICES_KEY = 'addthis_enabled_services';
37 2c8c2b87 Assos Assos
  const EXCLUDED_SERVICES_KEY = 'addthis_excluded_services';
38 85ad3d82 Assos Assos
  const GOOGLE_ANALYTICS_TRACKING_ENABLED_KEY = 'addthis_google_analytics_tracking_enabled';
39
  const GOOGLE_ANALYTICS_SOCIAL_TRACKING_ENABLED_KEY = 'addthis_google_analytics_social_tracking_enabled';
40
  const FACEBOOK_LIKE_COUNT_SUPPORT_ENABLED = 'addthis_facebook_like_count_support_enabled';
41
  const OPEN_WINDOWS_ENABLED_KEY = 'addthis_open_windows_enabled';
42
  const PROFILE_ID_KEY = 'addthis_profile_id';
43
  const SERVICES_CSS_URL_KEY = 'addthis_services_css_url';
44
  const SERVICES_JSON_URL_KEY = 'addthis_services_json_url';
45
  const STANDARD_CSS_ENABLED_KEY = 'addthis_standard_css_enabled';
46
  const UI_DELAY_KEY = 'addthis_ui_delay';
47
  const UI_HEADER_BACKGROUND_COLOR_KEY = 'addthis_ui_header_background_color';
48
  const UI_HEADER_COLOR_KEY = 'addthis_ui_header_color';
49
  const WIDGET_JS_URL_KEY = 'addthis_widget_js_url';
50 2c8c2b87 Assos Assos
  const WIDGET_JS_LOAD_DOMREADY = 'addthis_widget_load_domready';
51
  const WIDGET_JS_LOAD_ASYNC = 'addthis_widget_load_async';
52
  const WIDGET_JS_INCLUDE = 'addthis_widget_include';
53 85ad3d82 Assos Assos
54 4543c6c7 Assos Assos
  // Twitter.
55 85ad3d82 Assos Assos
  const TWITTER_VIA_KEY = 'addthis_twitter_via';
56
  const TWITTER_VIA_DEFAULT = 'AddThis';
57
  const TWITTER_TEMPLATE_KEY = 'addthis_twitter_template';
58
  const TWITTER_TEMPLATE_DEFAULT = '{{title}} {{url}} via @AddThis';
59
60 4543c6c7 Assos Assos
  // External resources.
61
  const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=300';
62 85ad3d82 Assos Assos
  const DEFAULT_SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
63
  const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
64 4543c6c7 Assos Assos
  const DEFAULT_WIDGET_JS_URL = 'http://s7.addthis.com/js/300/addthis_widget.js';
65 2c8c2b87 Assos Assos
  const DEFAULT_WIDGET_JS_LOAD_DOMREADY = TRUE;
66
  const DEFAULT_WIDGET_JS_LOAD_ASYNC = FALSE;
67
68
  // Type of inclusion.
69
  // 0 = don't include, 1 = pages no admin, 2 = on usages only.
70
  const DEFAULT_WIDGET_JS_INCLUDE = 2;
71
  const WIDGET_JS_INCLUDE_NONE = 0;
72
  const WIDGET_JS_INCLUDE_PAGE = 1;
73
  const WIDGET_JS_INCLUDE_USAGE = 2;
74
75 85ad3d82 Assos Assos
76 4543c6c7 Assos Assos
  // Internal resources.
77 85ad3d82 Assos Assos
  const ADMIN_CSS_FILE = 'addthis.admin.css';
78
  const ADMIN_INCLUDE_FILE = 'includes/addthis.admin.inc';
79
80 4543c6c7 Assos Assos
  // Widget types.
81 85ad3d82 Assos Assos
  const WIDGET_TYPE_DISABLED = 'addthis_disabled';
82
83 4543c6c7 Assos Assos
  // Styles.
84 85ad3d82 Assos Assos
  const CSS_32x32 = 'addthis_32x32_style';
85
  const CSS_16x16 = 'addthis_16x16_style';
86
87
  private static $instance;
88
89
  /* @var AddThisJson */
90
  private $json;
91
92
  /**
93 4543c6c7 Assos Assos
   * Get the singleton instance of the AddThis class.
94
   *
95 85ad3d82 Assos Assos
   * @return AddThis
96 4543c6c7 Assos Assos
   *   Instance of AddThis.
97 85ad3d82 Assos Assos
   */
98
  public static function getInstance() {
99 4543c6c7 Assos Assos
100 85ad3d82 Assos Assos
    if (!isset(self::$instance)) {
101 4543c6c7 Assos Assos
      $add_this = new AddThis();
102
      $add_this->setJson(new AddThisJson());
103
      self::$instance = $add_this;
104 85ad3d82 Assos Assos
    }
105 4543c6c7 Assos Assos
106 85ad3d82 Assos Assos
    return self::$instance;
107
  }
108
109 4543c6c7 Assos Assos
  /**
110
   * Set the json object.
111
   */
112 85ad3d82 Assos Assos
  public function setJson(AddThisJson $json) {
113
    $this->json = $json;
114
  }
115
116
  public function getDefaultFormatterTypes() {
117
    return array(
118
      self::WIDGET_TYPE_DISABLED => t('Disabled'),
119
    );
120
  }
121
122
  public function getDisplayTypes() {
123
    $displays = array();
124
    foreach ($display_impl = _addthis_field_info_formatter_field_type() as $key => $display) {
125
      $displays[$key] = t(check_plain($display['label']));
126
    }
127
    return $displays;
128
  }
129
130
  /*
131
   * Get markup for a given display type.
132
   *
133
   * When $options does not contain #entity, link to the current URL.
134
   * When $options does not contain #display, use default settings.
135
   */
136
  public function getDisplayMarkup($display, $options = array()) {
137
    if (empty($display)) {
138
      return array();
139
    }
140
141
    $formatters = _addthis_field_info_formatter_field_type();
142
143
    if (!array_key_exists($display, $formatters)) {
144
      return array();
145
    }
146
147
    // The display type exists. Now get it and get the markup.
148
    $display_information = $formatters[$display];
149
150
    // Theme function might only give a display name and
151
    // render on default implementation.
152
    if (!isset($options['#display']) ||
153
        (isset($options['#display']['type']) && $options['#display']['type'] != $display)) {
154
155
      $options['#display'] = isset($options['#display']) ? $options['#display'] : array();
156
      $options['#display'] = array_merge($options['#display'], $display_information);
157
      $options['#display']['type'] = $display;
158
159
    }
160
161
    // When #entity and #entity_type exist, use the entity's URL.
162
    if (isset($options['#entity']) && isset($options['#entity_type'])) {
163
      $uri = entity_uri($options['#entity_type'], $options['#entity']);
164
      $uri['options'] += array(
165
        'absolute' => TRUE,
166
      );
167
168
      // @todo Add a hook to alter the uri also based on fields from the
169
      // entity (such as custom share link). Pass $options and $uri. Return
170
      // a uri object to which we can reset it. Maybe use the alter structure.
171
172
      $options['#url'] = url($uri['path'], $uri['options']);
173
    }
174
    // @todo Hash the options array and cache the markup.
175
    // This will save all the extra calls to modules and alters.
176
177
    // Allow other modules to alter markup options.
178
    drupal_alter('addthis_markup_options', $options);
179
180
    $markup = array(
181
      '#display' => $options['#display'],
182
    );
183
    // Get all hook implementation to verify later if we can call it.
184
    $addthis_display_markup_implementations = module_implements('addthis_display_markup');
185
186
    // Look for a targeted implementation to call.
187
    // This should be the default implementation that is called.
188
    if (function_exists($display_information['module'] . '_addthis_display_markup__' . $display)) {
189
      $markup += call_user_func_array($display_information['module'] . '_addthis_display_markup__' . $display, array($options));
190
    }
191
    elseif (in_array($display_information['module'], $addthis_display_markup_implementations)) {
192
      $markup += module_invoke($display_information['module'], 'addthis_display_markup', $display, $options);
193
    }
194
    // Allow other modules to alter markup.
195
    drupal_alter('addthis_markup', $markup);
196
    return $markup;
197
  }
198
199
  public function getServices() {
200
    $rows = array();
201
    $services = $this->json->decode($this->getServicesJsonUrl());
202
    if (empty($services)) {
203
      drupal_set_message(t('AddThis services could not be loaded from @service_url', array('@service_url', $this->getServicesJsonUrl())), 'warning');
204
    }
205
    else {
206
      foreach ($services['data'] as $service) {
207
        $serviceCode = check_plain($service['code']);
208
        $serviceName = check_plain($service['name']);
209
        $rows[$serviceCode] = '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName;
210
      }
211
    }
212
    return $rows;
213
  }
214
215
  public function getAddThisAttributesMarkup($options) {
216
    if (isset($options)) {
217
      $attributes = array();
218
219
      if (isset($options['#entity'])) {
220
        $attributes += $this->getAttributeTitle($options['#entity']);
221
      }
222
      $attributes += $this->getAttributeUrl($options);
223
224
      return $attributes;
225
    }
226
    return array();
227
  }
228
229 c12e7e6a Assos Assos
  /**
230
   * Get the type used for the block.
231
   */
232 85ad3d82 Assos Assos
  public function getBlockDisplayType() {
233
    return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_DISABLED);
234
  }
235
236 c12e7e6a Assos Assos
  /**
237
   * Get the settings used by the block display.
238
   */
239 4543c6c7 Assos Assos
  public function getBlockDisplaySettings() {
240 c12e7e6a Assos Assos
    $settings = variable_get(self::BLOCK_WIDGET_SETTINGS_KEY, NULL);
241
242
    if ($settings == NULL && $this->getBlockDisplayType() != self::WIDGET_TYPE_DISABLED) {
243
      $settings = field_info_formatter_settings($this->getBlockDisplayType());
244
    }
245
246
    return $settings;
247 4543c6c7 Assos Assos
  }
248
249 85ad3d82 Assos Assos
  public function getProfileId() {
250
    return check_plain(variable_get(AddThis::PROFILE_ID_KEY));
251
  }
252
253
  public function getServicesCssUrl() {
254
    return check_url(variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL));
255
  }
256
257
  public function getServicesJsonUrl() {
258
    return check_url(variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL));
259
  }
260
261
  public function getEnabledServices() {
262
    return variable_get(self::ENABLED_SERVICES_KEY, array());
263
  }
264
265 2c8c2b87 Assos Assos
  public function getExcludedServices() {
266
    return variable_get(self::EXCLUDED_SERVICES_KEY, array());
267
  }
268
269 4543c6c7 Assos Assos
  /**
270 2c8c2b87 Assos Assos
   * Return the type of inclusion.
271 4543c6c7 Assos Assos
   *
272
   * @return string
273
   *   Retuns domready or async.
274
   */
275 2c8c2b87 Assos Assos
  public function getWidgetJsInclude() {
276
    return variable_get(self::WIDGET_JS_INCLUDE, self::DEFAULT_WIDGET_JS_INCLUDE);
277
  }
278
279
  /**
280
   * Return if domready loading should be active.
281
   *
282
   * @return bool
283
   *   Returns TRUE if domready is enabled.
284
   */
285
  public function getWidgetJsDomReady() {
286
    return variable_get(self::WIDGET_JS_LOAD_DOMREADY, self::DEFAULT_WIDGET_JS_LOAD_DOMREADY);
287
  }
288
289
  /**
290
   * Return if async initialization should be active.
291
   *
292
   * @return bool
293
   *   Returns TRUE if async is enabled.
294
   */
295
  public function getWidgetJsAsync() {
296
    return variable_get(self::WIDGET_JS_LOAD_ASYNC, self::DEFAULT_WIDGET_JS_LOAD_ASYNC);
297 85ad3d82 Assos Assos
  }
298
299
  public function isClickToOpenCompactMenuEnabled() {
300
    return (boolean) variable_get(self::CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY, FALSE);
301
  }
302
303
  public function isOpenWindowsEnabled() {
304
    return (boolean) variable_get(self::OPEN_WINDOWS_ENABLED_KEY, FALSE);
305
  }
306
307
  public function getUiDelay() {
308
    return (int) check_plain(variable_get(self::UI_DELAY_KEY));
309
  }
310
311
  public function getUiHeaderColor() {
312
    return check_plain(variable_get(self::UI_HEADER_COLOR_KEY));
313
  }
314
315
  public function getUiHeaderBackgroundColor() {
316
    return check_plain(variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY));
317
  }
318
319
  public function isStandardCssEnabled() {
320
    return (boolean) variable_get(self::STANDARD_CSS_ENABLED_KEY, TRUE);
321
  }
322
323
  public function getCustomConfigurationCode() {
324
    return variable_get(self::CUSTOM_CONFIGURATION_CODE_KEY, self::DEFAULT_CUSTOM_CONFIGURATION_CODE);
325
  }
326
327
  public function isCustomConfigurationCodeEnabled() {
328
    return (boolean) variable_get(self::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY, FALSE);
329
  }
330
331
  public function getBaseWidgetJsUrl() {
332
    return check_url(variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL));
333
  }
334
335
  public function getBaseBookmarkUrl() {
336
    return check_url(variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL));
337
  }
338
339
  public function getCoBrand() {
340
    return variable_get(self::CO_BRAND_KEY, '');
341
  }
342
343
  public function get508Compliant() {
344
    return (boolean) variable_get(self::COMPLIANT_508_KEY, FALSE);
345
  }
346
347
  public function getTwitterVia() {
348
    return variable_get(self::TWITTER_VIA_KEY, self::TWITTER_VIA_DEFAULT);
349
  }
350
351
  public function getTwitterTemplate() {
352
    return variable_get(self::TWITTER_TEMPLATE_KEY, self::TWITTER_TEMPLATE_DEFAULT);
353
  }
354
355
  public function isClickbackTrackingEnabled() {
356
    return (boolean) variable_get(self::CLICKBACK_TRACKING_ENABLED_KEY, FALSE);
357
  }
358
359
  public function isAddressbookEnabled() {
360
    return (boolean) variable_get(self::ADDRESSBOOK_ENABLED_KEY, FALSE);
361
  }
362
363
  public function isGoogleAnalyticsTrackingEnabled() {
364
    return (boolean) variable_get(self::GOOGLE_ANALYTICS_TRACKING_ENABLED_KEY, FALSE);
365
  }
366
367
  public function isGoogleAnalyticsSocialTrackingEnabled() {
368
    return (boolean) variable_get(self::GOOGLE_ANALYTICS_SOCIAL_TRACKING_ENABLED_KEY, FALSE);
369
  }
370
371
  public function isFacebookLikeCountSupportEnabled() {
372
    return (boolean) variable_get(self::FACEBOOK_LIKE_COUNT_SUPPORT_ENABLED, TRUE);
373
  }
374
375
  public function addStylesheets() {
376
    drupal_add_css($this->getServicesCssUrl(), 'external');
377
    drupal_add_css($this->getAdminCssFilePath(), 'file');
378
  }
379
380
  public function getFullBookmarkUrl() {
381
    return $this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp();
382
  }
383
384 2c8c2b87 Assos Assos
  /**
385
   * Transform the entity title to a attribute.
386
   *
387
   * @remarks
388
   *   The title of the entity and site can not contain double-qoutes. These are
389
   *   encoded into html chars.
390
   */
391 85ad3d82 Assos Assos
  private function getAttributeTitle($entity) {
392
    if (isset($entity->title)) {
393
      return array(
394 2c8c2b87 Assos Assos
        self::TITLE_ATTRIBUTE => htmlentities($entity->title . ' - ' . variable_get('site_name'), ENT_COMPAT),
395 85ad3d82 Assos Assos
      );
396
    }
397
    return array();
398
  }
399
400
  private function getAttributeUrl($options) {
401
    if (isset($options['#url'])) {
402
      return array(
403
        self::URL_ATTRIBUTE => $options['#url'],
404
      );
405
    }
406
    return array();
407
  }
408
409 2c8c2b87 Assos Assos
  public function getServiceNamesAsCommaSeparatedString($services) {
410
    $serviceNames = array_values($services);
411
    $servicesAsCommaSeparatedString = '';
412
    foreach ($serviceNames as $serviceName) {
413
      if ($serviceName != '0') {
414
        $servicesAsCommaSeparatedString .= $serviceName . ',';
415 85ad3d82 Assos Assos
      }
416
    }
417 2c8c2b87 Assos Assos
    return $servicesAsCommaSeparatedString;
418 85ad3d82 Assos Assos
  }
419
420
  private function getAdminCssFilePath() {
421
    return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
422
  }
423
424
  private function getProfileIdQueryParameter($prefix) {
425
    $profileId = $this->getProfileId();
426
    return !empty($profileId) ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
427
  }
428
429
  private function getProfileIdQueryParameterPrefixedWithAmp() {
430
    return $this->getProfileIdQueryParameter('&');
431
  }
432
433
  private function getProfileIdQueryParameterPrefixedWithHash() {
434
    return $this->getProfileIdQueryParameter('#');
435
  }
436
437 4543c6c7 Assos Assos
  /**
438
   * Get the url for the AddThis Widget.
439
   */
440 85ad3d82 Assos Assos
  private function getWidgetUrl() {
441 4543c6c7 Assos Assos
    $url = ($this->currentlyOnHttps() ?
442
      $this->getBaseWidgetJsUrl() : // Not https url.
443
      $this->transformToSecureUrl($this->getBaseWidgetJsUrl()) // Transformed to https url.
444
    );
445
    return check_url($url);
446 85ad3d82 Assos Assos
  }
447
448 4543c6c7 Assos Assos
  /**
449
   * Request if we are currently on a https connection.
450
   *
451
   * @return True if we are currently on a https connection.
452
   */
453
  public function currentlyOnHttps() {
454 85ad3d82 Assos Assos
    global $base_root;
455 4543c6c7 Assos Assos
    return (strpos($base_root, 'https://') !== FALSE) ? TRUE : FALSE;
456
  }
457
458
  /**
459
   * Transform a url to secure url with https prefix.
460
   */
461
  public function transformToSecureUrl($url) {
462
    if ($this->currentlyOnHttps()) {
463 85ad3d82 Assos Assos
      $url = (strpos($url, 'http://') === 0 ? 'https://' . substr($url, 7) : $url);
464
    }
465
    return $url;
466
  }
467
}