root / drupal7 / sites / all / modules / addthis / classes / Services / AddThisScriptManager.php @ a6e869e4
1 | 2c8c2b87 | Assos Assos | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * @file
|
||
4 | * Class definition of a script manager.
|
||
5 | *
|
||
6 | * This class will be used on different places. The result of the attachJsToElement()
|
||
7 | * should be the same in every situation within one request and throughout the
|
||
8 | * loading of the site.
|
||
9 | *
|
||
10 | * When manipulating the configuration do this very early in the request. This
|
||
11 | * could be hook_init() for example. Any other method should be before hook_page_build().
|
||
12 | * The implementation of addthis_page_build() is the first known instance where
|
||
13 | * this class might get used based on the configuration.
|
||
14 | */
|
||
15 | |||
16 | class AddThisScriptManager { |
||
17 | |||
18 | private $addthis = NULL; |
||
19 | private $async = NULL; |
||
20 | private $domready = NULL; |
||
21 | |||
22 | /**
|
||
23 | * Construct method.
|
||
24 | */
|
||
25 | private function __construct() { |
||
26 | $this->addthis = AddThis::getInstance(); |
||
27 | |||
28 | $this->async = $this->addthis->getWidgetJsAsync(); |
||
29 | $this->domready = $this->addthis->getWidgetJsDomReady(); |
||
30 | } |
||
31 | |||
32 | /**
|
||
33 | * Return a single instance of the AddThisScriptManager.
|
||
34 | *
|
||
35 | * @return AddThisScriptManager
|
||
36 | */
|
||
37 | public static function getInstance() { |
||
38 | static $manager; |
||
39 | |||
40 | if (!isset($manager)) { |
||
41 | $manager = new AddThisScriptManager(); |
||
42 | } |
||
43 | return $manager; |
||
44 | } |
||
45 | |||
46 | /**
|
||
47 | * Get the current widget js url.
|
||
48 | *
|
||
49 | * @return string
|
||
50 | * A url reference to the widget js.
|
||
51 | */
|
||
52 | public function getWidgetJsUrl() { |
||
53 | return check_url(variable_get(AddThis::WIDGET_JS_URL_KEY, AddThis::DEFAULT_WIDGET_JS_URL)); |
||
54 | } |
||
55 | |||
56 | /**
|
||
57 | * Return if we are on https connection.
|
||
58 | *
|
||
59 | * @return bool
|
||
60 | * TRUE if the current request is on https.
|
||
61 | */
|
||
62 | public function isHttps() { |
||
63 | global $is_https; |
||
64 | |||
65 | return $is_https; |
||
66 | } |
||
67 | |||
68 | /**
|
||
69 | * Change the schema from http to https if we are on https.
|
||
70 | *
|
||
71 | * @param string $url
|
||
72 | * A full url.
|
||
73 | *
|
||
74 | * @return string
|
||
75 | * The changed url.
|
||
76 | */
|
||
77 | public function correctSchemaIfHttps($url) { |
||
78 | if (is_string($url) && $this->isHttps()) { |
||
79 | return str_replace('http://', 'https://', $url); |
||
80 | } |
||
81 | else {
|
||
82 | return $url; |
||
83 | } |
||
84 | throw new InvalidArgumentException('The argument was not a string value'); |
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * Attach the widget js to the element.
|
||
89 | *
|
||
90 | * @todo Change the scope of the addthis.js.
|
||
91 | * See if we can get the scope of the addthis.js into the header
|
||
92 | * just below the settings so that the settings can be used in the loaded
|
||
93 | * addthis.js of our module.
|
||
94 | *
|
||
95 | * @param array $element
|
||
96 | * The element to attach the JavaScript to.
|
||
97 | */
|
||
98 | public function attachJsToElement(&$element) { |
||
99 | |||
100 | if ($this->addthis->getWidgetJsInclude() != AddThis::WIDGET_JS_INCLUDE_NONE) { |
||
101 | $widget_js = new AddThisWidgetJsUrl($this->getWidgetJsUrl()); |
||
102 | |||
103 | $pubid = $this->addthis->getProfileId(); |
||
104 | if (isset($pubid) && !empty($pubid) && is_string($pubid)) { |
||
105 | $widget_js->addAttribute('pubid', $pubid); |
||
106 | } |
||
107 | |||
108 | $async = $this->async; |
||
109 | if ($async) { |
||
110 | $widget_js->addAttribute('async', 1); |
||
111 | } |
||
112 | |||
113 | $domready = $this->domready; |
||
114 | if ($domready) { |
||
115 | $widget_js->addAttribute('domready', 1); |
||
116 | } |
||
117 | |||
118 | // Only when the script is not loaded after the DOM is ready we include
|
||
119 | // the script with #attached.
|
||
120 | if (!$domready) { |
||
121 | $element['#attached']['js'][$this->getWidgetJsUrl()] = array( |
||
122 | 'type' => 'external', |
||
123 | 'scope' => 'footer', |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | // Every setting value passed here overrides previously set values but
|
||
128 | // leaves the values that are already set somewhere else and that are not
|
||
129 | // passed here.
|
||
130 | $element['#attached']['js'][] = array( |
||
131 | 'type' => 'setting', |
||
132 | 'data' => array( |
||
133 | 'addthis' => array( |
||
134 | 'async' => $async, |
||
135 | 'domready' => $domready, |
||
136 | 'widget_url' => $this->getWidgetJsUrl(), |
||
137 | |||
138 | 'addthis_config' => $this->getJsAddThisConfig(), |
||
139 | 'addthis_share' => $this->getJsAddThisShare(), |
||
140 | ) |
||
141 | ) |
||
142 | ); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * Enable / disable domready loading.
|
||
148 | *
|
||
149 | * @param bool $enabled
|
||
150 | * TRUE to enabled domready loading.
|
||
151 | */
|
||
152 | function setDomReady($enabled) { |
||
153 | $this->domready = $enabled; |
||
154 | } |
||
155 | |||
156 | /**
|
||
157 | * Enable / disable async loading.
|
||
158 | *
|
||
159 | * @param bool $enabled
|
||
160 | * TRUE to enabled async loading.
|
||
161 | */
|
||
162 | function setAsync($enabled) { |
||
163 | $this->async = $enabled; |
||
164 | } |
||
165 | |||
166 | /**
|
||
167 | * Get a array with all addthis_config values.
|
||
168 | *
|
||
169 | * Allow alter through 'addthis_configuration'.
|
||
170 | *
|
||
171 | * @todo Add static cache.
|
||
172 | *
|
||
173 | * @todo Make the adding of configuration dynamic.
|
||
174 | * SRP is lost here.
|
||
175 | */
|
||
176 | private function getJsAddThisConfig() { |
||
177 | global $language; |
||
178 | |||
179 | $enabled_services = $this->addthis->getServiceNamesAsCommaSeparatedString($this->addthis->getEnabledServices()) . 'more'; |
||
180 | $excluded_services = $this->addthis->getServiceNamesAsCommaSeparatedString($this->addthis->getExcludedServices()); |
||
181 | |||
182 | $configuration = array( |
||
183 | 'pubid' => $this->addthis->getProfileId(), |
||
184 | 'services_compact' => $enabled_services, |
||
185 | 'services_exclude' => $excluded_services, |
||
186 | 'data_track_clickback' => $this->addthis->isClickbackTrackingEnabled(), |
||
187 | 'ui_508_compliant' => $this->addthis->get508Compliant(), |
||
188 | 'ui_click' => $this->addthis->isClickToOpenCompactMenuEnabled(), |
||
189 | 'ui_cobrand' => $this->addthis->getCoBrand(), |
||
190 | 'ui_delay' => $this->addthis->getUiDelay(), |
||
191 | 'ui_header_background' => $this->addthis->getUiHeaderBackgroundColor(), |
||
192 | 'ui_header_color' => $this->addthis->getUiHeaderColor(), |
||
193 | 'ui_open_windows' => $this->addthis->isOpenWindowsEnabled(), |
||
194 | 'ui_use_css' => $this->addthis->isStandardCssEnabled(), |
||
195 | 'ui_use_addressbook' => $this->addthis->isAddressbookEnabled(), |
||
196 | 'ui_language' => $language->language, |
||
197 | ); |
||
198 | if (module_exists('googleanalytics')) { |
||
199 | if ($this->addthis->isGoogleAnalyticsTrackingEnabled()) { |
||
200 | $configuration['data_ga_property'] = variable_get('googleanalytics_account', ''); |
||
201 | $configuration['data_ga_social'] = $this->addthis->isGoogleAnalyticsSocialTrackingEnabled(); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | drupal_alter('addthis_configuration', $configuration); |
||
206 | return $configuration; |
||
207 | } |
||
208 | |||
209 | /**
|
||
210 | * Get a array with all addthis_share values.
|
||
211 | *
|
||
212 | * Allow alter through 'addthis_configuration_share'.
|
||
213 | *
|
||
214 | * @todo Add static cache.
|
||
215 | *
|
||
216 | * @todo Make the adding of configuration dynamic.
|
||
217 | * SRP is lost here.
|
||
218 | */
|
||
219 | private function getJsAddThisShare() { |
||
220 | |||
221 | $configuration = $this->getJsAddThisConfig(); |
||
222 | |||
223 | if (isset($configuration['templates'])) { |
||
224 | $addthis_share = array( |
||
225 | 'templates' => $configuration['templates'], |
||
226 | ); |
||
227 | } |
||
228 | $addthis_share['templates']['twitter'] = $this->addthis->getTwitterTemplate(); |
||
229 | |||
230 | drupal_alter('addthis_configuration_share', $configuration); |
||
231 | return $addthis_share; |
||
232 | } |
||
233 | |||
234 | } |