Skip to content

Commit 0a230d2

Browse files
committed
v2.6.4 - caching/nocache imrovements
1 parent f2cd0a0 commit 0a230d2

File tree

4 files changed

+149
-80
lines changed

4 files changed

+149
-80
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* Author: [Mark Croxton](http://hallmark-design.co.uk/)
44

5-
### Version 2.6.3
5+
### Version 2.6.4
66

77
This is the development version of Stash. Test thoroughly before using in production.
88

system/expressionengine/third_party/stash/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
if (! defined('STASH_VER'))
33
{
44
define('STASH_NAME', 'Stash');
5-
define('STASH_VER', '2.6.3');
5+
define('STASH_VER', '2.6.4');
66
define('STASH_AUTHOR', 'Mark Croxton');
77
define('STASH_DOCS', 'http://github.com/croxton/Stash/');
88
define('STASH_DESC', 'Stash: save text and code snippets for reuse throughout your templates.');

system/expressionengine/third_party/stash/ext.stash.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ public function template_fetch_template($row)
251251
$this->EE->session->cache['stash'] = array_merge($this->EE->session->cache['stash'], $embed_vars);
252252
}
253253

254-
// instantiate and initialize Stash
255-
$s = new Stash();
254+
// instantiate Stash without initialising
255+
$s = new Stash(TRUE);
256256

257257
// get the file
258258
$out = $s->get($param);

system/expressionengine/third_party/stash/mod.stash.php

Lines changed: 145 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,64 +1175,87 @@ public function extend()
11751175
11761176
--------------------------------------------------------- */
11771177

1178-
if ( FALSE !== $with = $this->EE->TMPL->fetch_param('with', FALSE))
1179-
{
1180-
$embed_vars = array();
1181-
unset($this->EE->TMPL->tagparams['with']);
1178+
if ( FALSE === $with = $this->EE->TMPL->fetch_param('with', FALSE)) return;
1179+
1180+
$embed_params = array();
1181+
unset($this->EE->TMPL->tagparams['with']);
11821182

1183-
// embed vars passed as params
1184-
foreach ($this->EE->TMPL->tagparams as $key => $val)
1183+
// have values other than embed name been passed in the "with" param? These should be passed as parameters to the embed:
1184+
$with = explode(' ', $with, 2);
1185+
1186+
// extract embed vars passed as params
1187+
foreach ($this->EE->TMPL->tagparams as $key => $val)
1188+
{
1189+
if (strncmp($key, 'stash:', 6) == 0)
11851190
{
1186-
if (strncmp($key, 'stash:', 6) == 0)
1187-
{
1188-
$embed_vars[substr($key, 6)] = $val;
1189-
unset($this->EE->TMPL->tagparams[$key]);
1190-
}
1191+
$embed_params[] = $key . '="'. $val .'"';
11911192
}
1193+
}
1194+
1195+
// if this is a tag pair, construct an embed_extend tag and pass data enclosed by {stash:...} pairs to it
1196+
if ($this->EE->TMPL->tagdata)
1197+
{
1198+
// construct the embed_extend tag
1199+
$this->EE->TMPL->tagdata = LD . 'exp:stash:embed_extend name="' . $with[0] . '"' . (isset($with[1]) ? ' ' . $with[1] : '') . ' ' . implode(" ", $embed_params) . RD . $this->EE->TMPL->tagdata . LD . '/exp:stash:embed_extend' . RD;
1200+
}
1201+
else
1202+
{
1203+
// construct the embed tag
1204+
$this->EE->TMPL->tagdata = LD . 'exp:stash:embed name="' . $with[0] . '"' . (isset($with[1]) ? ' ' . $with[1] : '') . ' ' . implode(" ", $embed_params) . RD;
1205+
}
1206+
1207+
// escape it?
1208+
if ( (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('escape')) )
1209+
{
1210+
$this->EE->TMPL->tagdata = LD .'stash:nocache'. RD . $this->EE->TMPL->tagdata . LD .'/stash:nocache'. RD;
1211+
}
1212+
1213+
// inject the embed into a variable / block
1214+
return $this->set();
1215+
1216+
}
1217+
1218+
// ---------------------------------------------------------
1219+
1220+
/**
1221+
* Pass variables to an embed via variable pairs in tagdata
1222+
*
1223+
* @access public
1224+
* @return string
1225+
*/
1226+
public function embed_extend()
1227+
{
1228+
if ($this->EE->TMPL->tagdata)
1229+
{
1230+
$embed_vars = array();
11921231

1193-
// if this is a tag pair, capture data enclosed by {stash:...} pairs
1194-
if ($this->EE->TMPL->tagdata)
1232+
foreach($this->EE->TMPL->var_pair as $key => $val)
11951233
{
1196-
foreach($this->EE->TMPL->var_pair as $key => $val)
1234+
if (strncmp($key, 'stash:', 6) == 0)
11971235
{
1198-
if (strncmp($key, 'stash:', 6) == 0)
1199-
{
1200-
$pattern = '/'.LD.$key.RD.'(.*)'.LD.'\/'.$key.RD.'/Usi';
1201-
preg_match($pattern, $this->EE->TMPL->tagdata, $matches);
1236+
$pattern = '/'.LD.$key.RD.'(.*)'.LD.'\/'.$key.RD.'/Usi';
1237+
preg_match($pattern, $this->EE->TMPL->tagdata, $matches);
12021238

1203-
if ( ! empty($matches))
1204-
{
1205-
$embed_vars[substr($key, 6)] = $matches[1];
1206-
}
1207-
}
1239+
if ( ! empty($matches))
1240+
{
1241+
$embed_vars[$key] = $matches[1];
1242+
}
12081243
}
12091244
}
1210-
1211-
// add embed vars directly to the stash session cache
1212-
#$this->EE->session->cache['stash'] = array_merge($this->EE->session->cache['stash'], $embed_vars);
12131245

1214-
// add embed vars to the static cache for parsing at the point the extended embed is included into the page
1215-
if (count($embed_vars) > 0)
1246+
if (is_array($this->EE->TMPL->tagparams))
12161247
{
1217-
if ( ! isset(self::$_cache['embed_vars']))
1218-
{
1219-
self::$_cache['embed_vars'] = array();
1220-
}
1221-
1222-
// generate a unique id to identify this embed instance
1223-
$id = $this->EE->functions->random();
1224-
1225-
// cache the variables for later
1226-
self::$_cache['embed_vars'][$id] = $embed_vars;
1227-
1228-
// add id as a parameter
1229-
$with .= ' id="' . $id .'"';
1248+
$this->EE->TMPL->tagparams = array_merge($embed_vars, $this->EE->TMPL->tagparams);
1249+
}
1250+
else
1251+
{
1252+
$this->EE->TMPL->tagparams = $embed_vars;
12301253
}
12311254

1232-
// now inject the embed into the named block/variable
1233-
$this->EE->TMPL->tagdata = LD . 'exp:stash:embed:' . $with . RD;
1234-
return $this->set();
1235-
}
1255+
$this->EE->TMPL->tagdata = '';
1256+
}
1257+
1258+
return $this->embed();
12361259
}
12371260

12381261
// ---------------------------------------------------------
@@ -2499,6 +2522,7 @@ public function embed()
24992522
if ( ! $this->_is_cacheable())
25002523
{
25012524
$this->EE->TMPL->tagparams['save'] = 'no';
2525+
self::$_nocache = FALSE; // remove {stash:nocache} pairs
25022526
}
25032527
}
25042528

@@ -2539,18 +2563,6 @@ public function embed()
25392563
}
25402564
}
25412565

2542-
// merge any cached embed vars if this embed was injected via an extend
2543-
$id = $this->EE->TMPL->fetch_param('id');
2544-
2545-
if ( $id && isset(self::$_cache['embed_vars']))
2546-
{
2547-
if (isset(self::$_cache['embed_vars'][$id]))
2548-
{
2549-
$this->EE->TMPL->tagparams['embed_vars'] = array_merge(self::$_cache['embed_vars'][$id], $this->EE->TMPL->tagparams['embed_vars']);
2550-
unset(self::$_cache['embed_vars'][$id]);
2551-
}
2552-
}
2553-
25542566
// permitted parameters for embeds
25552567
$reserved_vars = array(
25562568
'name',
@@ -2601,19 +2613,31 @@ public function cache()
26012613
...
26022614
{/exp:stash:cache}
26032615
*/
2616+
2617+
// process as a static cache?
2618+
if ( $this->EE->TMPL->fetch_param('process') == 'static')
2619+
{
2620+
return $this->static_cache($this->EE->TMPL->tagdata);
2621+
}
2622+
2623+
// Is this page really cacheable? (Note: allow all request types to be cached)
2624+
if ( ! $this->_is_cacheable(FALSE))
2625+
{
2626+
self::$_nocache = FALSE; // remove {stash:nocache} pairs
2627+
$this->EE->TMPL->tagparams['replace'] = 'yes';
2628+
$this->EE->TMPL->tagparams['save'] = 'no'; // disable caching
2629+
}
2630+
else
2631+
{
2632+
$this->EE->TMPL->tagparams['save'] = 'yes';
2633+
}
26042634

26052635
// Unprefix common variables in wrapped tags
26062636
if($unprefix = $this->EE->TMPL->fetch_param('unprefix'))
26072637
{
26082638
$this->EE->TMPL->tagdata = $this->_un_prefix($unprefix, $this->EE->TMPL->tagdata);
26092639
}
26102640

2611-
// process as a static cache?
2612-
if ( $this->EE->TMPL->fetch_param('process') == 'static')
2613-
{
2614-
return $this->static_cache($this->EE->TMPL->tagdata);
2615-
}
2616-
26172641
// default name for cached items is 'cache'
26182642
$this->EE->TMPL->tagparams['name'] = $this->EE->TMPL->fetch_param('name', 'cache');
26192643

@@ -2644,7 +2668,6 @@ public function cache()
26442668

26452669
// mandatory parameter values for cached items
26462670
$this->EE->TMPL->tagparams['scope'] = 'site';
2647-
$this->EE->TMPL->tagparams['save'] = 'yes';
26482671
$this->EE->TMPL->tagparams['parse_tags'] = 'yes';
26492672
$this->EE->TMPL->tagparams['parse_vars'] = 'yes';
26502673
$this->EE->TMPL->tagparams['parse_conditionals'] = 'yes';
@@ -2726,10 +2749,12 @@ public function static_cache($output='')
27262749
$this->process = 'end';
27272750
$this->priority = '999999'; // should be the last thing post-processed (by Stash)
27282751

2729-
// has the tag been used as a tag pair? If so, just return to the template so it can be parsed naturally
2752+
// has the tag been used as a tag pair? If so, we'll parse the tagdata to remove {stash:nocache} pairs
27302753
if ($this->EE->TMPL->tagdata)
27312754
{
2732-
$output = $this->EE->TMPL->tagdata;
2755+
// parse the tagdata
2756+
self::$_nocache = FALSE; // remove {stash:nocache} pairs
2757+
$output = $this->parse();
27332758
}
27342759

27352760
if ($out = $this->_post_parse('save_output')) return $out . $output;
@@ -2750,11 +2775,12 @@ public function save_output($output='')
27502775
// mandatory parameter values for cached output
27512776
$this->EE->TMPL->tagparams['context'] = NULL;
27522777
$this->EE->TMPL->tagparams['scope'] = 'site';
2753-
$this->EE->TMPL->tagparams['save'] = 'yes';
2754-
$this->EE->TMPL->tagparams['refresh'] = "0"; // static cached items can't expire
27552778
$this->EE->TMPL->tagparams['replace'] = "no"; // static cached items cannot be replaced
27562779
$this->EE->TMPL->tagparams['bundle'] = 'static'; // cached pages in the static bundle are saved to file automatically by the model
27572780

2781+
// optional parameters
2782+
$this->EE->TMPL->tagparams['refresh'] = $this->EE->TMPL->fetch_param('refresh', 0); // by default static cached items won't expire
2783+
27582784
// bundle determines the cache driver
27592785
$this->bundle_id = $this->EE->stash_model->get_bundle_by_name($this->EE->TMPL->tagparams['bundle']);
27602786

@@ -2770,6 +2796,10 @@ public function save_output($output='')
27702796
{
27712797
$this->EE->TMPL->tagparams['save'] = 'no';
27722798
}
2799+
else
2800+
{
2801+
$this->EE->TMPL->tagparams['save'] = 'yes';
2802+
}
27732803

27742804
// permitted parameters for cached
27752805
$reserved_vars = array(
@@ -2871,19 +2901,47 @@ public function not_found()
28712901
* Check to see if a template (not a fragment) is suitable for caching
28722902
*
28732903
* @access public
2904+
* @param boolean $check_request_type
28742905
* @return string
28752906
*/
2876-
private function _is_cacheable()
2907+
private function _is_cacheable($check_request_type=TRUE)
28772908
{
28782909
// Check if we should cache this URI
2879-
if ($_SERVER['REQUEST_METHOD'] == 'POST' // … POST request
2880-
|| $this->EE->input->get('ACT') // … ACT request
2881-
|| $this->EE->input->get('css') // … css request
2882-
|| $this->EE->input->get('URL') // … URL request
2883-
)
2910+
if ($check_request_type)
2911+
{
2912+
if ($_SERVER['REQUEST_METHOD'] == 'POST' // … POST request
2913+
|| $this->EE->input->get('ACT') // … ACT request
2914+
|| $this->EE->input->get('css') // … css request
2915+
|| $this->EE->input->get('URL') // … URL request
2916+
)
2917+
{
2918+
return FALSE;
2919+
}
2920+
}
2921+
2922+
// has caching been deliberately disabled?
2923+
if ( FALSE === (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('save', 'yes')) )
28842924
{
28852925
return FALSE;
28862926
}
2927+
2928+
// logged_in_only: only cache if the page visitor is logged in
2929+
if ( (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('logged_in_only')) )
2930+
{
2931+
if ($this->EE->session->userdata['member_id'] == 0)
2932+
{
2933+
return FALSE; // don't cache
2934+
}
2935+
}
2936+
2937+
// logged_out_only: only cache if the page visitor is logged out
2938+
if ((bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('logged_out_only')) )
2939+
{
2940+
if ($this->EE->session->userdata['member_id'] != 0)
2941+
{
2942+
return FALSE; // don't cache
2943+
}
2944+
}
28872945

28882946
return TRUE;
28892947
}
@@ -2989,7 +3047,7 @@ public function parse($params = array(), $value=NULL)
29893047
$this->EE->TMPL->tagparams['parse_tags'] = $this->EE->TMPL->fetch_param('parse_tags', 'yes');
29903048
$this->EE->TMPL->tagparams['parse_vars'] = $this->EE->TMPL->fetch_param('parse_vars', 'yes');
29913049
$this->EE->TMPL->tagparams['parse_conditionals'] = $this->EE->TMPL->fetch_param('parse_conditionals', 'yes');
2992-
$this->EE->TMPL->tagparams['parse_depth'] = $this->EE->TMPL->fetch_param('parse_depth', 3);
3050+
$this->EE->TMPL->tagparams['parse_depth'] = $this->EE->TMPL->fetch_param('parse_depth', 4);
29933051

29943052
// postpone tag processing?
29953053
if ( $this->process !== 'inline')
@@ -4142,6 +4200,11 @@ private function _parse_sub_template($tags = TRUE, $vars = TRUE, $conditionals =
41424200
// protect content inside {stash:nocache} tags, or {[prefix]:nocache} tags
41434201
$this->EE->TMPL->tagdata = preg_replace_callback($nocache_pattern, array($this, '_placeholders'), $this->EE->TMPL->tagdata);
41444202
}
4203+
else
4204+
{
4205+
// remove extraneous {stash:nocache} tags, or {[prefix]:nocache} tags
4206+
$this->EE->TMPL->tagdata = str_replace(array(LD.$nocache.RD, LD.'/'.$nocache.RD), '', $this->EE->TMPL->tagdata);
4207+
}
41454208

41464209
// parse variables
41474210
if ($vars)
@@ -4154,6 +4217,11 @@ private function _parse_sub_template($tags = TRUE, $vars = TRUE, $conditionals =
41544217
// protect content inside {stash:nocache} tags that might have been exposed by parse_vars
41554218
$this->EE->TMPL->tagdata = preg_replace_callback($nocache_pattern, array($this, '_placeholders'), $this->EE->TMPL->tagdata);
41564219
}
4220+
else
4221+
{
4222+
// remove extraneous {stash:nocache} tags, or {[prefix]:nocache} tags
4223+
$this->EE->TMPL->tagdata = str_replace(array(LD.$nocache.RD, LD.'/'.$nocache.RD), '', $this->EE->TMPL->tagdata);
4224+
}
41574225
}
41584226

41594227
// parse conditionals?
@@ -4894,7 +4962,8 @@ private function _api_call($method, $params, $type='variable', $scope='user', $v
48944962
{
48954963
$this->EE->TMPL->tagdata = $value;
48964964
}
4897-
4965+
4966+
$this->init(); // re-initilize Stash
48984967
$result = $this->{$method}();
48994968

49004969
// restore original template params and tagdata

0 commit comments

Comments
 (0)