1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nexcess.net Turpentine Extension for Magento |
5
|
|
|
* Copyright (C) 2012 Nexcess.net L.L.C. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Nexcessnet_Turpentine_Model_Observer_Esi extends Varien_Event_Observer { |
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set a cookie with the customer group id when customer logs in |
25
|
|
|
* |
26
|
|
|
* Events: customer_login |
27
|
|
|
* |
28
|
|
|
* @param Varien_Object $eventObject |
29
|
|
|
* @return null |
30
|
|
|
*/ |
31
|
|
|
public function setCustomerGroupCookie($eventObject) { |
32
|
|
|
$customer = $eventObject->getCustomer(); |
33
|
|
|
$cookie = Mage::getSingleton('core/cookie'); |
34
|
|
|
if (Mage::getStoreConfig('persistent/options/enabled')) { |
35
|
|
|
$cookie->set('customer_group', $customer->getGroupId(), Mage::getStoreConfig('persistent/options/lifetime')); |
36
|
|
|
} else { |
37
|
|
|
$cookie->set('customer_group', $customer->getGroupId()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Destroy the cookie with the customer group when customer logs out |
43
|
|
|
* |
44
|
|
|
* Events: customer_logout |
45
|
|
|
* |
46
|
|
|
* @param Varien_Object $eventObject |
47
|
|
|
* @return null |
48
|
|
|
*/ |
49
|
|
|
public function removeCustomerGroupCookie($eventObject) { |
50
|
|
|
Mage::getSingleton('core/cookie')->delete('customer_group'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check the ESI flag and set the ESI header if needed |
55
|
|
|
* |
56
|
|
|
* Events: http_response_send_before |
57
|
|
|
* |
58
|
|
|
* @param Varien_Object $eventObject |
59
|
|
|
* @return null |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function setFlagHeaders($eventObject) { |
|
|
|
|
62
|
|
|
$response = $eventObject->getResponse(); |
63
|
|
|
if (Mage::helper('turpentine/esi')->shouldResponseUseEsi()) { |
64
|
|
|
$response->setHeader('X-Turpentine-Esi', |
65
|
|
|
Mage::registry('turpentine_esi_flag') ? '1' : '0'); |
66
|
|
|
Mage::helper('turpentine/debug')->logDebug( |
67
|
|
|
'Set ESI flag header to: %s', |
68
|
|
|
(Mage::registry('turpentine_esi_flag') ? '1' : '0') ); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Allows disabling page-caching by setting the cache flag on a controller |
74
|
|
|
* |
75
|
|
|
* <customer_account> |
76
|
|
|
* <turpentine_cache_flag value="0" /> |
77
|
|
|
* </customer_account> |
78
|
|
|
* |
79
|
|
|
* Events: controller_action_layout_generate_blocks_after |
80
|
|
|
* |
81
|
|
|
* @param Varien_Object $eventObject |
82
|
|
|
* @return null |
83
|
|
|
*/ |
84
|
|
|
public function checkCacheFlag($eventObject) { |
85
|
|
|
if (Mage::helper('turpentine/varnish')->shouldResponseUseVarnish()) { |
86
|
|
|
$layoutXml = $eventObject->getLayout()->getUpdate()->asSimplexml(); |
87
|
|
|
foreach ($layoutXml->xpath('//turpentine_cache_flag') as $node) { |
88
|
|
|
foreach ($node->attributes() as $attr => $value) { |
89
|
|
|
if ($attr == 'value') { |
90
|
|
|
if ( ! (string) $value) { |
91
|
|
|
Mage::register('turpentine_nocache_flag', true, true); |
92
|
|
|
return; //only need to set the flag once |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* On controller redirects, check the target URL and set to home page |
102
|
|
|
* if it would otherwise go to a getBlock URL |
103
|
|
|
* |
104
|
|
|
* @param Varien_Object $eventObject |
105
|
|
|
* @return null |
106
|
|
|
*/ |
107
|
|
|
public function checkRedirectUrl($eventObject) { |
108
|
|
|
$esiHelper = Mage::helper('turpentine/esi'); |
109
|
|
|
$url = $eventObject->getTransport()->getUrl(); |
110
|
|
|
$referer = Mage::helper('core/http')->getHttpReferer(); |
111
|
|
|
$dummyUrl = $esiHelper->getDummyUrl(); |
112
|
|
|
$reqUenc = Mage::helper('core')->urlDecode( |
113
|
|
|
Mage::app()->getRequest()->getParam('uenc') ); |
114
|
|
|
|
115
|
|
|
if ($this->_checkIsEsiUrl($url)) { |
116
|
|
|
if ($this->_checkIsNotEsiUrl($reqUenc) && |
117
|
|
|
Mage::getBaseUrl() == $url) { |
118
|
|
|
$newUrl = $this->_fixupUencUrl($reqUenc); |
119
|
|
|
} elseif ($this->_checkIsNotEsiUrl($referer)) { |
120
|
|
|
$newUrl = $referer; |
121
|
|
|
} else { |
122
|
|
|
$newUrl = $dummyUrl; |
123
|
|
|
} |
124
|
|
|
// TODO: make sure this actually looks like a URL |
125
|
|
|
$eventObject->getTransport()->setUrl($newUrl); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($eventObject->getTransport()->getUrl() != $url) { |
129
|
|
|
Mage::helper('turpentine/debug')->logDebug( |
130
|
|
|
'Detected redirect to ESI URL, changing: %s => %s', |
131
|
|
|
$url, $eventObject->getTransport()->getUrl() ); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Load the cache clear events from stored config |
137
|
|
|
* |
138
|
|
|
* @param Varien_Object $eventObject |
139
|
|
|
* @return null |
140
|
|
|
*/ |
141
|
|
|
public function loadCacheClearEvents($eventObject) { |
142
|
|
|
Varien_Profiler::start('turpentine::observer::esi::loadCacheClearEvents'); |
143
|
|
|
$events = Mage::helper('turpentine/esi')->getCacheClearEvents(); |
144
|
|
|
$appShim = Mage::getSingleton('turpentine/shim_mage_core_app'); |
145
|
|
|
foreach ($events as $ccEvent) { |
146
|
|
|
$appShim->shim_addEventObserver('global', $ccEvent, |
147
|
|
|
'turpentine_ban_'.$ccEvent, 'singleton', |
148
|
|
|
'turpentine/observer_ban', 'banClientEsiCache'); |
149
|
|
|
} |
150
|
|
|
Varien_Profiler::stop('turpentine::observer::esi::loadCacheClearEvents'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Add the core/messages block rewrite if the flash message fix is enabled |
155
|
|
|
* |
156
|
|
|
* The core/messages block is rewritten because it doesn't use a template |
157
|
|
|
* we can replace with an ESI include tag, just dumps out a block of |
158
|
|
|
* hard-coded HTML and also frequently skips the toHtml method |
159
|
|
|
* |
160
|
|
|
* @param Varien_Object $eventObject |
161
|
|
|
* @return null |
162
|
|
|
*/ |
163
|
|
|
public function addMessagesBlockRewrite($eventObject) { |
164
|
|
|
if (Mage::helper('turpentine/esi')->shouldFixFlashMessages()) { |
165
|
|
|
Varien_Profiler::start('turpentine::observer::esi::addMessagesBlockRewrite'); |
166
|
|
|
Mage::getSingleton('turpentine/shim_mage_core_app') |
167
|
|
|
->shim_addClassRewrite('block', 'core', 'messages', |
168
|
|
|
'Nexcessnet_Turpentine_Block_Core_Messages'); |
169
|
|
|
Varien_Profiler::stop('turpentine::observer::esi::addMessagesBlockRewrite'); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Check the magento version and runtime env and set the replace_form_key |
175
|
|
|
* flag if needed |
176
|
|
|
* |
177
|
|
|
* @param Varien_Object $eventObject |
178
|
|
|
* @return null |
179
|
|
|
*/ |
180
|
|
|
public function setReplaceFormKeyFlag($eventObject) { |
181
|
|
|
$esiHelper = Mage::helper('turpentine/esi'); |
182
|
|
|
$varnishHelper = Mage::helper('turpentine/varnish'); |
183
|
|
|
$request = Mage::app()->getRequest(); |
184
|
|
|
if ($esiHelper->shouldResponseUseEsi() && |
185
|
|
|
$varnishHelper->csrfFixupNeeded() && |
186
|
|
|
! $request->isPost()) { |
187
|
|
|
Mage::register('replace_form_key', true); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Replace the form key placeholder with the ESI include fragment |
193
|
|
|
* |
194
|
|
|
* @param Varien_Object $eventObject |
195
|
|
|
* @return null |
196
|
|
|
*/ |
197
|
|
|
public function replaceFormKeyPlaceholder($eventObject) { |
198
|
|
|
if (Mage::registry('replace_form_key')) { |
199
|
|
|
$esiHelper = Mage::helper('turpentine/esi'); |
200
|
|
|
$response = $eventObject->getResponse(); |
201
|
|
|
$responseBody = $response->getBody(); |
202
|
|
|
$responseBody = str_replace('{{form_key_esi_placeholder}}', |
203
|
|
|
$esiHelper->buildEsiIncludeFragment( |
204
|
|
|
$esiHelper->getFormKeyEsiUrl() ), |
205
|
|
|
$responseBody); |
206
|
|
|
$response->setBody($responseBody); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Encode block data in URL then replace with ESI template |
212
|
|
|
* |
213
|
|
|
* @link https://github.com/nexcess/magento-turpentine/wiki/ESI_Cache_Policy |
214
|
|
|
* |
215
|
|
|
* Events: core_block_abstract_to_html_before |
216
|
|
|
* |
217
|
|
|
* @param Varien_Object $eventObject |
218
|
|
|
* @return null |
219
|
|
|
*/ |
220
|
|
|
public function injectEsi($eventObject) { |
221
|
|
|
$blockObject = $eventObject->getBlock(); |
222
|
|
|
$dataHelper = Mage::helper('turpentine/data'); |
223
|
|
|
$esiHelper = Mage::helper('turpentine/esi'); |
224
|
|
|
$debugHelper = Mage::helper('turpentine/debug'); |
225
|
|
|
if ($esiHelper->getEsiBlockLogEnabled()) { |
226
|
|
|
$debugHelper->logInfo( |
227
|
|
|
'Checking ESI block candidate: %s', |
228
|
|
|
$blockObject->getNameInLayout() ? $blockObject->getNameInLayout() : $blockObject->getModuleName() ); |
229
|
|
|
} |
230
|
|
|
if ($esiHelper->shouldResponseUseEsi() && |
231
|
|
|
$blockObject instanceof Mage_Core_Block_Template && |
|
|
|
|
232
|
|
|
$esiOptions = $blockObject->getEsiOptions()) { |
233
|
|
|
|
234
|
|
|
if ((isset($esiOptions['disableEsiInjection'])) && ($esiOptions['disableEsiInjection'] == 1)) { |
235
|
|
|
return; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if (Mage::app()->getStore()->getCode() == 'admin') { |
239
|
|
|
// admin blocks are not allowed to be cached for now |
240
|
|
|
$debugHelper->logWarn( |
241
|
|
|
'Ignoring attempt to inject adminhtml block: %s', |
242
|
|
|
$blockObject->getNameInLayout() ? $blockObject->getNameInLayout() : $blockObject->getModuleName() ); |
243
|
|
|
return; |
244
|
|
|
} elseif ($esiHelper->getEsiBlockLogEnabled()) { |
245
|
|
|
$debugHelper->logInfo('Block check passed, injecting block: %s', |
246
|
|
|
$blockObject->getNameInLayout() ? $blockObject->getNameInLayout() : $blockObject->getModuleName()); |
247
|
|
|
} |
248
|
|
|
Varien_Profiler::start('turpentine::observer::esi::injectEsi'); |
249
|
|
|
$ttlParam = $esiHelper->getEsiTtlParam(); |
250
|
|
|
$cacheTypeParam = $esiHelper->getEsiCacheTypeParam(); |
251
|
|
|
$dataParam = $esiHelper->getEsiDataParam(); |
252
|
|
|
$methodParam = $esiHelper->getEsiMethodParam(); |
253
|
|
|
$hmacParam = $esiHelper->getEsiHmacParam(); |
254
|
|
|
$scopeParam = $esiHelper->getEsiScopeParam(); |
255
|
|
|
$referrerParam = $esiHelper->getEsiReferrerParam(); |
256
|
|
|
|
257
|
|
|
$esiOptions = $this->_getDefaultEsiOptions($esiOptions); |
258
|
|
|
|
259
|
|
|
// change the block's template to the stripped down ESI template |
260
|
|
|
switch ($esiOptions[$methodParam]) { |
261
|
|
|
case 'ajax': |
262
|
|
|
$blockObject->setTemplate('turpentine/ajax.phtml'); |
263
|
|
|
break; |
264
|
|
|
|
265
|
|
|
case 'esi': |
266
|
|
|
default: |
267
|
|
|
$blockObject->setTemplate('turpentine/esi.phtml'); |
268
|
|
|
// flag request for ESI processing |
269
|
|
|
Mage::register('turpentine_esi_flag', true, true); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
// esi data is the data needed to regenerate the ESI'd block |
273
|
|
|
$esiData = $this->_getEsiData($blockObject, $esiOptions)->toArray(); |
274
|
|
|
ksort($esiData); |
275
|
|
|
$frozenData = $dataHelper->freeze($esiData); |
276
|
|
|
$urlOptions = array( |
277
|
|
|
$methodParam => $esiOptions[$methodParam], |
278
|
|
|
$cacheTypeParam => $esiOptions[$cacheTypeParam], |
279
|
|
|
$ttlParam => $esiOptions[$ttlParam], |
280
|
|
|
$hmacParam => $dataHelper->getHmac($frozenData), |
281
|
|
|
$dataParam => $frozenData, |
282
|
|
|
); |
283
|
|
|
if ($esiOptions[$methodParam] == 'ajax') { |
284
|
|
|
$urlOptions['_secure'] = Mage::app()->getStore() |
285
|
|
|
->isCurrentlySecure(); |
286
|
|
|
} |
287
|
|
|
if ($esiOptions[$scopeParam] == 'page') { |
288
|
|
|
$urlOptions[$referrerParam] = Mage::helper('core')->urlEncode( |
289
|
|
|
Mage::getUrl('*/*/*', array('_use_rewrite' => true, '_current' => true)) |
290
|
|
|
); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$esiUrl = Mage::getUrl('turpentine/esi/getBlock', $urlOptions); |
294
|
|
|
if ($esiOptions[$methodParam] == 'esi') { |
295
|
|
|
// setting [web/unsecure/base_url] can be https://... but ESI can never be HTTPS |
296
|
|
|
$esiUrl = preg_replace('|^https://|i', 'http://', $esiUrl); |
297
|
|
|
} |
298
|
|
|
$blockObject->setEsiUrl($esiUrl); |
299
|
|
|
// avoid caching the ESI template output to prevent the double-esi- |
300
|
|
|
// include/"ESI processing not enabled" bug |
301
|
|
|
foreach (array('lifetime', 'tags', 'key') as $dataKey) { |
302
|
|
|
$blockObject->unsetData('cache_'.$dataKey); |
303
|
|
|
} |
304
|
|
|
if (strlen($esiUrl) > 2047) { |
305
|
|
|
Mage::helper('turpentine/debug')->logWarn( |
306
|
|
|
'ESI url is probably too long (%d > 2047 characters): %s', |
307
|
|
|
strlen($esiUrl), $esiUrl ); |
308
|
|
|
} |
309
|
|
|
Varien_Profiler::stop('turpentine::observer::esi::injectEsi'); |
310
|
|
|
} // else handle the block like normal and cache it inline with the page |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Generate ESI data to be encoded in URL |
315
|
|
|
* |
316
|
|
|
* @param Mage_Core_Block_Template $blockObject |
317
|
|
|
* @param array $esiOptions |
318
|
|
|
* @return Varien_Object |
319
|
|
|
*/ |
320
|
|
|
protected function _getEsiData($blockObject, $esiOptions) { |
321
|
|
|
Varien_Profiler::start('turpentine::observer::esi::_getEsiData'); |
322
|
|
|
$esiHelper = Mage::helper('turpentine/esi'); |
323
|
|
|
$cacheTypeParam = $esiHelper->getEsiCacheTypeParam(); |
324
|
|
|
$scopeParam = $esiHelper->getEsiScopeParam(); |
325
|
|
|
$methodParam = $esiHelper->getEsiMethodParam(); |
326
|
|
|
$esiData = new Varien_Object(); |
327
|
|
|
$esiData->setStoreId(Mage::app()->getStore()->getId()); |
328
|
|
|
$esiData->setNameInLayout($blockObject->getNameInLayout()); |
329
|
|
|
$esiData->setBlockType(get_class($blockObject)); |
330
|
|
|
$esiData->setLayoutHandles($this->_getBlockLayoutHandles($blockObject)); |
331
|
|
|
$esiData->setEsiMethod($esiOptions[$methodParam]); |
332
|
|
|
if ($esiOptions[$cacheTypeParam] == 'private' || $esiOptions[$cacheTypeParam] == 'customer_group') { |
333
|
|
|
if (is_array(@$esiOptions['flush_events'])) { |
334
|
|
|
$esiData->setFlushEvents(array_merge( |
335
|
|
|
$esiHelper->getDefaultCacheClearEvents(), |
336
|
|
|
array_keys($esiOptions['flush_events']) )); |
337
|
|
|
} else { |
338
|
|
|
$esiData->setFlushEvents( |
339
|
|
|
$esiHelper->getDefaultCacheClearEvents() ); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
if ($esiOptions[$scopeParam] == 'page') { |
343
|
|
|
$esiData->setParentUrl(Mage::app()->getRequest()->getRequestString()); |
344
|
|
|
} |
345
|
|
|
if (is_array($esiOptions['dummy_blocks'])) { |
346
|
|
|
$dummyBlocks = array(); |
347
|
|
|
foreach ($esiOptions['dummy_blocks'] as $key => $value) { |
348
|
|
|
$dummyBlocks[] = (empty($value) && ! is_numeric($key)) ? $key : $value; |
349
|
|
|
} |
350
|
|
|
$esiData->setDummyBlocks($dummyBlocks); |
351
|
|
|
} else { |
352
|
|
|
Mage::helper('turpentine/debug')->logWarn( |
353
|
|
|
'Invalid dummy_blocks for block: %s', |
354
|
|
|
$blockObject->getNameInLayout() ); |
355
|
|
|
} |
356
|
|
|
$simpleRegistry = array(); |
357
|
|
|
$complexRegistry = array(); |
358
|
|
|
if (is_array($esiOptions['registry_keys'])) { |
359
|
|
|
foreach ($esiOptions['registry_keys'] as $key => $options) { |
360
|
|
|
$value = Mage::registry($key); |
361
|
|
|
if ($value) { |
362
|
|
|
if (is_object($value) && |
363
|
|
|
$value instanceof Mage_Core_Model_Abstract) { |
|
|
|
|
364
|
|
|
$complexRegistry[$key] = |
365
|
|
|
$this->_getComplexRegistryData($options, $value); |
366
|
|
|
} else { |
367
|
|
|
$simpleRegistry[$key] = $value; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
} else { |
372
|
|
|
Mage::helper('turpentine/debug')->logWarn( |
373
|
|
|
'Invalid registry_keys for block: %s', |
374
|
|
|
$blockObject->getNameInLayout() ); |
375
|
|
|
} |
376
|
|
|
$esiData->setSimpleRegistry($simpleRegistry); |
377
|
|
|
$esiData->setComplexRegistry($complexRegistry); |
378
|
|
|
Varien_Profiler::stop('turpentine::observer::esi::_getEsiData'); |
379
|
|
|
return $esiData; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Get the active layout handles for this block and any child blocks |
384
|
|
|
* |
385
|
|
|
* This is probably kind of slow since it uses a bunch of xpath searches |
386
|
|
|
* but this was the easiest way to get the info needed. Should be a target |
387
|
|
|
* for future optimization |
388
|
|
|
* |
389
|
|
|
* There is an issue with encoding the used handles in the URL, if the used |
390
|
|
|
* handles change (ex customer logs in), the cached version of the page will |
391
|
|
|
* still have the old handles encoded in it's ESI url. This can lead to |
392
|
|
|
* weirdness like the "Log in" link displaying for already logged in |
393
|
|
|
* visitors on pages that were initially visited by not-logged-in visitors. |
394
|
|
|
* Not sure of a solution for this yet. |
395
|
|
|
* |
396
|
|
|
* Above problem is currently solved by EsiController::_swapCustomerHandles() |
397
|
|
|
* but it would be best to find a more general solution to this. |
398
|
|
|
* |
399
|
|
|
* @param Mage_Core_Block_Template $block |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
protected function _getBlockLayoutHandles($block) { |
403
|
|
|
Varien_Profiler::start('turpentine::observer::esi::_getBlockLayoutHandles'); |
404
|
|
|
$layout = $block->getLayout(); |
405
|
|
|
$layoutXml = Mage::helper('turpentine/esi')->getLayoutXml(); |
406
|
|
|
$activeHandles = array(); |
407
|
|
|
// get the xml node representing the block we're working on (from the |
408
|
|
|
// default handle probably) |
409
|
|
|
$blockNode = current($layout->getNode()->xpath(sprintf( |
410
|
|
|
'//block[@name=\'%s\']', |
411
|
|
|
$block->getNameInLayout() ))); |
412
|
|
|
$childBlocks = Mage::helper('turpentine/data') |
413
|
|
|
->getChildBlockNames($blockNode); |
414
|
|
|
foreach ($childBlocks as $blockName) { |
415
|
|
|
foreach ($layout->getUpdate()->getHandles() as $handle) { |
416
|
|
|
// check if this handle has any block or reference tags that |
417
|
|
|
// refer to this block or a child block, unless the handle name |
418
|
|
|
// is blank |
419
|
|
|
if ($handle !== '' && (strpos($handle, 'THEME') === 0 || |
420
|
|
|
$layoutXml->xpath(sprintf( |
421
|
|
|
'//%s//*[@name=\'%s\']', $handle, $blockName )))) { |
422
|
|
|
$activeHandles[] = $handle; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
if ( ! $activeHandles) { |
|
|
|
|
427
|
|
|
$activeHandles[] = 'default'; |
428
|
|
|
} |
429
|
|
|
Varien_Profiler::stop('turpentine::observer::esi::_getBlockLayoutHandles'); |
430
|
|
|
return array_unique($activeHandles); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Get the default ESI options |
435
|
|
|
* |
436
|
|
|
* @return array |
437
|
|
|
*/ |
438
|
|
|
protected function _getDefaultEsiOptions($options) { |
439
|
|
|
$esiHelper = Mage::helper('turpentine/esi'); |
440
|
|
|
$ttlParam = $esiHelper->getEsiTtlParam(); |
441
|
|
|
$methodParam = $esiHelper->getEsiMethodParam(); |
442
|
|
|
$cacheTypeParam = $esiHelper->getEsiCacheTypeParam(); |
443
|
|
|
$defaults = array( |
444
|
|
|
$esiHelper->getEsiMethodParam() => 'esi', |
445
|
|
|
$esiHelper->getEsiScopeParam() => 'global', |
446
|
|
|
$esiHelper->getEsiCacheTypeParam() => 'public', |
447
|
|
|
'dummy_blocks' => array(), |
448
|
|
|
'registry_keys' => array(), |
449
|
|
|
); |
450
|
|
|
$options = array_merge($defaults, $options); |
451
|
|
|
|
452
|
|
|
// set the default TTL |
453
|
|
|
if ( ! isset($options[$ttlParam])) { |
454
|
|
|
if ($options[$cacheTypeParam] == 'private' || $options[$cacheTypeParam] == 'customer_group') { |
455
|
|
|
switch ($options[$methodParam]) { |
456
|
|
|
case 'ajax': |
457
|
|
|
$options[$ttlParam] = '0'; |
458
|
|
|
break; |
459
|
|
|
|
460
|
|
|
case 'esi': |
461
|
|
|
default: |
462
|
|
|
$options[$ttlParam] = $esiHelper->getDefaultEsiTtl(); |
463
|
|
|
break; |
464
|
|
|
} |
465
|
|
|
} else { |
466
|
|
|
$options[$ttlParam] = Mage::helper('turpentine/varnish') |
467
|
|
|
->getDefaultTtl(); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
return $options; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Get the complex registry entry data |
476
|
|
|
* |
477
|
|
|
* @param array $valueOptions |
478
|
|
|
* @param mixed $value |
479
|
|
|
* @return array |
480
|
|
|
*/ |
481
|
|
|
protected function _getComplexRegistryData($valueOptions, $value) { |
482
|
|
|
$idMethod = @$valueOptions['id_method'] ? |
483
|
|
|
$valueOptions['id_method'] : 'getId'; |
484
|
|
|
$model = @$valueOptions['model'] ? |
485
|
|
|
$valueOptions['model'] : Mage::helper('turpentine/data') |
486
|
|
|
->getModelName($value); |
487
|
|
|
$data = array( |
488
|
|
|
'model' => $model, |
489
|
|
|
'id' => $value->{$idMethod}(), |
490
|
|
|
); |
491
|
|
|
return $data; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Fix a URL to ensure it uses Magento's base URL instead of the backend |
496
|
|
|
* URL |
497
|
|
|
* |
498
|
|
|
* @param string $uencUrl |
499
|
|
|
* @return string |
500
|
|
|
*/ |
501
|
|
|
protected function _fixupUencUrl($uencUrl) { |
502
|
|
|
$esiHelper = Mage::helper('turpentine/esi'); |
503
|
|
|
$corsOrigin = $esiHelper->getCorsOrigin(); |
504
|
|
|
if ($corsOrigin != $esiHelper->getCorsOrigin($uencUrl)) { |
505
|
|
|
return $corsOrigin.parse_url($uencUrl, PHP_URL_PATH); |
506
|
|
|
} else { |
507
|
|
|
return $uencUrl; |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Check if a URL *is not* for the /turpentine/esi/getBlock/ action |
513
|
|
|
* |
514
|
|
|
* @param string $url |
515
|
|
|
* @return bool |
516
|
|
|
*/ |
517
|
|
|
protected function _checkIsNotEsiUrl($url) { |
518
|
|
|
return $url && ! preg_match('~/turpentine/esi/getBlock/~', $url); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Check if a URL *is* for the /turpentine/esi/getBlock/ action |
523
|
|
|
* |
524
|
|
|
* @param string $url |
525
|
|
|
* @return bool |
526
|
|
|
*/ |
527
|
|
|
protected function _checkIsEsiUrl($url) { |
528
|
|
|
return ! $this->_checkIsNotEsiUrl($url); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
public function hookToControllerActionPreDispatch($observer) { |
532
|
|
View Code Duplication |
if (Mage::helper('turpentine/data')->getVclFix() == 0 && $observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add') { |
|
|
|
|
533
|
|
|
Mage::dispatchEvent("add_to_cart_before", array('request' => $observer->getControllerAction()->getRequest())); |
534
|
|
|
} |
535
|
|
View Code Duplication |
if ($observer->getEvent()->getControllerAction()->getFullActionName() == 'wishlist_index_index') { |
|
|
|
|
536
|
|
|
Mage::dispatchEvent('wishlist_index_index_before', array('request' => $observer->getControllerAction()->getRequest())); |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
public function hookToControllerActionPostDispatch($observer) { |
541
|
|
View Code Duplication |
if ($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add') { |
|
|
|
|
542
|
|
|
Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest())); |
543
|
|
|
} |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
public function hookToAddToCartBefore($observer) { |
547
|
|
|
//Mage::log("hookToAddToCartBefore-antes ".print_r($observer->getEvent()->getRequest()->getParams(),true)." will be added to cart.", null, 'carrinho.log', true); |
|
|
|
|
548
|
|
|
$key = Mage::getSingleton('core/session')->getFormKey(); |
549
|
|
|
$observer->getEvent()->getRequest()->setParam('form_key', $key); |
550
|
|
|
$request = $observer->getEvent()->getRequest()->getParams(); |
|
|
|
|
551
|
|
|
//Mage::log("hookToAddToCartBefore ".print_r($request,true)." will be added to cart.", null, 'carrinho.log', true); |
|
|
|
|
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
public function hookToAddToCartAfter($observer) { |
555
|
|
|
$request = $observer->getEvent()->getRequest()->getParams(); |
|
|
|
|
556
|
|
|
//Mage::log("hookToAddToCartAfter ".print_r($request,true)." is added to cart.", null, 'carrinho.log', true); |
|
|
|
|
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Set the form key on the add to wishlist request |
561
|
|
|
* |
562
|
|
|
* @param $observer |
563
|
|
|
* |
564
|
|
|
* @return Nexcessnet_Turpentine_Model_Observer_Esi |
565
|
|
|
*/ |
566
|
|
|
public function hookToAddToWishlistBefore($observer) |
567
|
|
|
{ |
568
|
|
|
$key = Mage::getSingleton('core/session')->getFormKey(); |
569
|
|
|
$observer->getEvent()->getRequest()->setParam('form_key', $key); |
570
|
|
|
return $this; |
571
|
|
|
} |
572
|
|
|
} |
573
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.