Updated some coding style typos.
[project/addthis.git] / classes / AddThis.php
CommitLineData
63315932
JP
1<?php
2/**
3 * @file
4 * An AddThis-class.
63315932
JP
5 */
6
7class AddThis {
8
591b038e 9 const BLOCK_NAME = 'addthis_block';
460f1e3c 10 const DEFAULT_CUSTOM_CONFIGURATION_CODE = 'var addthis_config = {}';
06a60498 11 const DEFAULT_FORMATTER = 'addthis_default_formatter';
e3447863 12 const DEFAULT_NUMBER_OF_PREFERRED_SERVICES = 4;
43a336b9 13 const FIELD_TYPE = 'addthis';
9268db58 14 const MODULE_NAME = 'addthis';
460f1e3c
JP
15 const PERMISSION_ADMINISTER_ADDTHIS = 'administer addthis';
16 const PERMISSION_ADMINISTER_ADVANCED_ADDTHIS = 'administer advanced addthis';
9268db58 17 const STYLE_KEY = 'addthis_style';
43a336b9 18 const WIDGET_TYPE = 'addthis_button_widget';
9268db58
JP
19
20 // AddThis attribute and parameter names (as defined in AddThis APIs)
21 const PROFILE_ID_QUERY_PARAMETER = 'pubid';
22 const TITLE_ATTRIBUTE = 'addthis:title';
23
24 // Persistent variable keys
79751477 25 const ADDRESSBOOK_ENABLED_KEY = 'addthis_addressbook_enabled';
65e6db6f 26 const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type';
5fc529ad 27 const BOOKMARK_URL_KEY = 'addthis_bookmark_url';
5ce0cb2b 28 const CLICKBACK_TRACKING_ENABLED_KEY = 'addthis_clickback_tracking_enabled';
4aa15ce9 29 const CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY = 'addthis_click_to_open_compact_menu_enabled';
dc19bfa2 30 const CO_BRAND_KEY = 'addthis_co_brand';
90e669c3 31 const COMPLIANT_508_KEY = 'addthis_508_compliant';
460f1e3c
JP
32 const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY = 'addthis_custom_configuration_code_enabled';
33 const CUSTOM_CONFIGURATION_CODE_KEY = 'addthis_custom_configuration_code';
65e6db6f 34 const ENABLED_SERVICES_KEY = 'addthis_enabled_services';
59bb6deb 35 const FACEBOOK_LIKE_ENABLED_KEY = 'addthis_facebook_like_enabled';
421589c9 36 const GOOGLE_PLUS_ONE_ENABLED_KEY = 'addthis_google_plus_one_enabled';
460f1e3c 37 const LARGE_ICONS_ENABLED_KEY = 'addthis_large_icons_enabled';
e3447863 38 const NUMBER_OF_PREFERRED_SERVICES_KEY = 'addthis_number_of_preferred_services';
4aa15ce9 39 const OPEN_WINDOWS_ENABLED_KEY = 'addthis_open_windows_enabled';
cfab0d33 40 const PROFILE_ID_KEY = 'addthis_profile_id';
8109364c 41 const SERVICES_CSS_URL_KEY = 'addthis_services_css_url';
fc3ad5d3 42 const SERVICES_JSON_URL_KEY = 'addthis_services_json_url';
4aa15ce9 43 const STANDARD_CSS_ENABLED_KEY = 'addthis_standard_css_enabled';
e1345397 44 const TWITTER_ENABLED_KEY = 'addthis_twitter_enabled';
4aa15ce9 45 const UI_DELAY_KEY = 'addthis_ui_delay';
57765925
JP
46 const UI_HEADER_BACKGROUND_COLOR_KEY = 'addthis_ui_header_background_color';
47 const UI_HEADER_COLOR_KEY = 'addthis_ui_header_color';
8109364c 48 const WIDGET_JS_URL_KEY = 'addthis_widget_js_url';
9268db58
JP
49
50 // External resources
51 const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=250';
8109364c 52 const DEFAULT_SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
be7a8d4b 53 const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
8109364c 54 const DEFAULT_WIDGET_JS_URL = 'http://s7.addthis.com/js/250/addthis_widget.js';
9268db58
JP
55
56 // Internal resources
57 const ADMIN_CSS_FILE = 'addthis.admin.css';
211164bd 58 const ADMIN_INCLUDE_FILE = 'includes/addthis.admin.inc';
9268db58
JP
59
60 // Widget types
b6608a16
MG
61 const WIDGET_TYPE_COMPACT_BUTTON = 'addthis_compact_button';
62 const WIDGET_TYPE_DISABLED = 'addthis_disabled';
63 const WIDGET_TYPE_LARGE_BUTTON = 'addthis_large_button';
64 const WIDGET_TYPE_SHARECOUNT = 'addthis_sharecount';
65 const WIDGET_TYPE_TOOLBOX = 'addthis_toolbox';
9268db58 66
2ee37e69
JP
67 private static $instance;
68
20f92648 69 /* @var AddThisJson */
45d4a8eb 70 private $json;
45d4a8eb 71
ab677dcc
JP
72 /**
73 * @return AddThis
74 */
2ee37e69
JP
75 public static function getInstance() {
76 if (!isset(self::$instance)) {
ab677dcc 77 $addThis = new AddThis();
20f92648 78 $addThis->setJson(new AddThisJson());
ab677dcc 79 self::$instance = $addThis;
2ee37e69
JP
80 }
81 return self::$instance;
82 }
83
20f92648 84 public function setJson(AddThisJson $json) {
ab677dcc
JP
85 $this->json = $json;
86 }
87
b6608a16
MG
88 //@TODO: Refactor all WidgetType names into DisplayType
89 // A widget type is the element shown to edit a value.
39839324 90 // We use the widget as a type way to define the way to Display.
b6608a16 91 // Therefore we need to keep a good descriptive name as DisplayType.
39839324 92
b6608a16
MG
93 /*
94 * Get all the DisplayTypes that are available.
95 */
2ee37e69 96 public function getWidgetTypes() {
63315932
JP
97 return array(
98 self::WIDGET_TYPE_DISABLED => t('Disabled'),
99 self::WIDGET_TYPE_COMPACT_BUTTON => t('Compact button'),
100 self::WIDGET_TYPE_LARGE_BUTTON => t('Large button'),
101 self::WIDGET_TYPE_TOOLBOX => t('Toolbox'),
102 self::WIDGET_TYPE_SHARECOUNT => t('Sharecount'),
103 );
104 }
105
b6608a16
MG
106 /*
107 * Return me the markup for a certain display type.
108 *
109 * Variables contains #entity and #settings as keys when applicable.
110 * When #entity is not there we link to the current url. When #settings
111 * is not there we use the default settings.
112 */
113 public function getDisplayMarkup($display, $variables = NULL) {
114 $formatters = addthis_field_info_formatter_field_type();
115 if (array_key_exists($display, $formatters)) {
116 // The display type is found. Now get it and get the markup.
117 $display_inf = $formatters[$display];
63315932 118
b6608a16
MG
119 // Get all hook implementation to verify later if we can call it.
120 $implementations = module_implements('addthis_display_markup');
121
122 $markup = array();
123 // First we look for a targeted implementation to call.
124 if (function_exists($display_inf['module'] . '_addthis_display_markup__' . $display)) {
125 $markup = call_user_func_array($display_inf['module'] . '_addthis_display_markup__' . $display, array(NULL));
126
127 // This should be the default implementation that is called.
128 } elseif (in_array($display_inf['module'], $implementations)) {
129 $markup = module_invoke($display_inf['module'], 'addthis_display_markup', $display);
130
131 // When we end up here somebody did something wrong in there module.
132 }
133 return $markup;
134
135 } else {
136 // Return empty
137 return array();
39839324 138 }
b6608a16
MG
139 // If no display is found or something went wrong we go here.
140 return array();
141 }
39839324 142
b6608a16
MG
143 /*
144 * Get the type DisplayType used for our AddThis block.
145 */
146 public function getBlockDisplayType() {
147 return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
63315932
JP
148 }
149
2ee37e69 150 public function getProfileId() {
e960315c 151 return check_plain(variable_get(AddThis::PROFILE_ID_KEY));
18feba68
JP
152 }
153
2ee37e69 154 public function getServicesCssUrl() {
e960315c 155 return check_url(variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL));
8109364c
JP
156 }
157
2ee37e69 158 public function getServicesJsonUrl() {
e960315c 159 return check_url(variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL));
fc3ad5d3
JP
160 }
161
ab677dcc
JP
162 public function getServices() {
163 $rows = array();
164 $services = $this->json->decode($this->getServicesJsonUrl());
f75fabc2
JP
165 if (empty($services)) {
166 drupal_set_message(t('AddThis services could not be loaded from ' . $this->getServicesJsonUrl()), 'warning');
167 } else {
ab677dcc
JP
168 foreach ($services['data'] AS $service) {
169 $serviceCode = check_plain($service['code']);
170 $serviceName = check_plain($service['name']);
171 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName;
172 }
173 }
174 return $rows;
65e6db6f
JP
175 }
176
ab677dcc
JP
177 public function getEnabledServices() {
178 return variable_get(self::ENABLED_SERVICES_KEY, array());
65e6db6f
JP
179 }
180
2ee37e69 181 public function addStylesheets() {
45d4a8eb
JP
182 drupal_add_css($this->getServicesCssUrl(), 'external');
183 drupal_add_css($this->getAdminCssFilePath(), 'file');
2e4987e2
JP
184 }
185
4c82025d
JP
186 public function addWidgetJs() {
187 drupal_add_js(self::getWidgetUrl(), array('type' => 'external', 'scope' => 'footer'));
188 }
189
2ee37e69 190 public function addConfigurationOptionsJs() {
45d4a8eb
JP
191 if ($this->isCustomConfigurationCodeEnabled()) {
192 $javascript = $this->getCustomConfigurationCode();
460f1e3c 193 } else {
8e927e58 194 $enabledServices = $this->getServiceNamesAsCommaSeparatedString() . 'more';
945a965d 195
196 $configuration = array(
197 'services_compact' => $enabledServices,
198 'data_track_clickback' => $this->isClickbackTrackingEnabled(),
199 'ui_508_compliant' => $this->get508Compliant(),
200 'ui_click' => $this->isClickToOpenCompactMenuEnabled(),
201 'ui_cobrand' => $this->getCoBrand(),
202 'ui_delay' => $this->getUiDelay(),
203 'ui_header_background' => $this->getUiHeaderBackgroundColor(),
204 'ui_header_color' => $this->getUiHeaderColor(),
205 'ui_open_windows' => $this->isOpenWindowsEnabled(),
206 'ui_use_css' => $this->isStandardCssEnabled(),
207 'ui_use_addressbook' => $this->isAddressbookEnabled(),
208 );
209 // @todo provide hook to alter the default configuration.
210
211 $javascript = 'var addthis_config = ' . drupal_json_encode($configuration);
460f1e3c 212 }
4c82025d 213 drupal_add_js($javascript, array('type' => 'inline'));
87bc3b42
JP
214 }
215
2ee37e69 216 public function areLargeIconsEnabled() {
2ecdf543 217 return (boolean) variable_get(self::LARGE_ICONS_ENABLED_KEY, TRUE);
8abc0f61
JP
218 }
219
4aa15ce9
JP
220 public function isClickToOpenCompactMenuEnabled() {
221 return (boolean) variable_get(self::CLICK_TO_OPEN_COMPACT_MENU_ENABLED_KEY, FALSE);
222 }
223
224 public function isOpenWindowsEnabled() {
225 return (boolean) variable_get(self::OPEN_WINDOWS_ENABLED_KEY, FALSE);
226 }
227
59bb6deb
JP
228 public function isFacebookLikeEnabled() {
229 return (boolean) variable_get(self::FACEBOOK_LIKE_ENABLED_KEY, FALSE);
230 }
231
421589c9
JP
232 public function isGooglePlusOneEnabled() {
233 return (boolean) variable_get(self::GOOGLE_PLUS_ONE_ENABLED_KEY, FALSE);
234 }
235
e1345397
JP
236 public function isTwitterEnabled() {
237 return (boolean) variable_get(self::TWITTER_ENABLED_KEY, FALSE);
238 }
239
4aa15ce9
JP
240 public function getUiDelay() {
241 return (int) check_plain(variable_get(self::UI_DELAY_KEY));
242 }
243
2ee37e69 244 public function getUiHeaderColor() {
e960315c 245 return check_plain(variable_get(self::UI_HEADER_COLOR_KEY));
57765925
JP
246 }
247
2ee37e69 248 public function getUiHeaderBackgroundColor() {
e960315c 249 return check_plain(variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY));
57765925
JP
250 }
251
4aa15ce9
JP
252 public function isStandardCssEnabled() {
253 return (boolean) variable_get(self::STANDARD_CSS_ENABLED_KEY, TRUE);
254 }
255
2ee37e69 256 public function getCustomConfigurationCode() {
460f1e3c
JP
257 return variable_get(self::CUSTOM_CONFIGURATION_CODE_KEY, self::DEFAULT_CUSTOM_CONFIGURATION_CODE);
258 }
259
2ee37e69 260 public function isCustomConfigurationCodeEnabled() {
2ecdf543 261 return (boolean) variable_get(self::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY, FALSE);
460f1e3c
JP
262 }
263
2ee37e69
JP
264 public function getBaseWidgetJsUrl() {
265 return check_url(variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL));
266 }
267
268 public function getBaseBookmarkUrl() {
269 return check_url(variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL));
270 }
271
e3447863
JP
272 public function getNumberOfPreferredServices() {
273 return variable_get(self::NUMBER_OF_PREFERRED_SERVICES_KEY, self::DEFAULT_NUMBER_OF_PREFERRED_SERVICES);
274 }
275
dc19bfa2
JP
276 public function getCoBrand() {
277 return variable_get(self::CO_BRAND_KEY);
278 }
279
90e669c3 280 public function get508Compliant() {
2ecdf543 281 return (boolean) variable_get(self::COMPLIANT_508_KEY, FALSE);
90e669c3
JP
282 }
283
08ff2c73 284 public function isClickbackTrackingEnabled() {
2ecdf543 285 return (boolean) variable_get(self::CLICKBACK_TRACKING_ENABLED_KEY, FALSE);
08ff2c73
JP
286 }
287
79751477 288 public function isAddressbookEnabled() {
2ecdf543 289 return (boolean) variable_get(self::ADDRESSBOOK_ENABLED_KEY, FALSE);
79751477
JP
290 }
291
b6608a16 292 public function getAddThisAttributesMarkup($entity) {
ab677dcc 293 if (is_object($entity)) {
5b48493d
JP
294 $attributes = array();
295
296 // Add title
297 $attributes += $this->getAddThisTitleAttributeMarkup($entity);
298
299 // Return the array with attributes
300 return $attributes;
ab677dcc 301 }
5b48493d 302 return array();
ab677dcc
JP
303 }
304
305 private function getAddThisTitleAttributeMarkup($entity) {
5b48493d 306 return array(
b345e9b6
JP
307 self::TITLE_ATTRIBUTE => (check_plain($entity->title) . ' - ' . variable_get('site_name'))
308 );
ab677dcc
JP
309 }
310
b6608a16 311 public function getLargeButtonsClass() {
45d4a8eb 312 return $this->areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
8abc0f61
JP
313 }
314
b6608a16 315 public function getTwitterButtonMarkup() {
5b48493d 316 $element = NULL;
e1345397 317 if ($this->isTwitterEnabled()) {
5b48493d 318 $element = array(
25b6bbc6 319 '#theme' => 'addthis_element',
5b48493d 320 '#tag' => 'a',
25b6bbc6
MG
321 '#value' => '',
322 '#attributes' => array(
323 'class' => array('addthis_button_tweet')
5b48493d
JP
324 ),
325 );
e1345397 326 }
5b48493d 327 return $element;
e1345397
JP
328 }
329
b6608a16 330 public function getFacebookLikeButtonMarkup() {
5b48493d 331 $element = NULL;
59bb6deb 332 if ($this->isFacebookLikeEnabled()) {
5b48493d
JP
333 $element = array(
334 '#theme' => 'addthis_element',
335 '#tag' => 'a',
25b6bbc6 336 '#value' => '',
5b48493d 337 '#attributes' => array(
25b6bbc6
MG
338 'class' => array(
339 'addthis_button_facebook_like'
340 ),
5b48493d
JP
341 'fb:like:layout' => 'button_count'
342 )
343 );
59bb6deb 344 }
5b48493d 345 return $element;
59bb6deb
JP
346 }
347
b6608a16 348 public function getGooglePlusOneButtonMarkup() {
5b48493d 349 $element = NULL;
421589c9 350 if ($this->isGooglePlusOneEnabled()) {
5b48493d
JP
351 $element = array(
352 '#theme' => 'addthis_element',
353 '#tag' => 'a',
25b6bbc6
MG
354 '#value' => '',
355 '#attributes' => array(
356 'class' => array('addthis_button_google_plusone')
5b48493d
JP
357 )
358 );
421589c9 359 }
5b48493d 360 return $element;
421589c9
JP
361 }
362
2ee37e69 363 private function getServiceNamesAsCommaSeparatedString() {
45d4a8eb 364 $enabledServiceNames = array_values($this->getEnabledServices());
87bc3b42
JP
365 $enabledServicesAsCommaSeparatedString = '';
366 foreach ($enabledServiceNames as $enabledServiceName) {
367 if ($enabledServiceName != '0') {
368 $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
369 }
370 }
371 return $enabledServicesAsCommaSeparatedString;
372 }
373
2ee37e69 374 private function getAdminCssFilePath() {
65e6db6f
JP
375 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
376 }
377
b6608a16
MG
378 /*
379 * Helper function. Get a bookmark url appended with the ProfileId
380 */
381 public function getFullBookmarkUrl() {
5b48493d 382 return $this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp();
9cbc2da3
JP
383 }
384
2ee37e69 385 private function getProfileIdQueryParameter($prefix) {
45d4a8eb 386 $profileId = $this->getProfileId();
e8f5baaa 387 return !empty($profileId) ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
22526524
JP
388 }
389
2ee37e69 390 private function getProfileIdQueryParameterPrefixedWithAmp() {
45d4a8eb 391 return $this->getProfileIdQueryParameter('&');
22526524
JP
392 }
393
2ee37e69 394 private function getProfileIdQueryParameterPrefixedWithHash() {
45d4a8eb 395 return $this->getProfileIdQueryParameter('#');
22526524
JP
396 }
397
2ee37e69 398 private function getWidgetUrl() {
45d4a8eb 399 return check_url($this->getBaseWidgetJsUrl() . $this->getProfileIdQueryParameterPrefixedWithHash());
63315932
JP
400 }
401}