/** * Method for template_post_parse hook * * @param string Parsed template string * @param bool Whether an embed or not * @param integer Site ID * @return string Template string */ public function template_post_parse($template, $sub, $site_id) { // play nice with other extensions on this hook if (isset($this->EE->extensions->last_call) && $this->EE->extensions->last_call) { $template = $this->EE->extensions->last_call; } // is this the final template? if ($sub == FALSE) { // check the cache for postponed tags if (!isset($this->EE->session->cache['stash']['__template_post_parse__'])) { $this->EE->session->cache['stash']['__template_post_parse__'] = array(); } // an array of tags needing to be post-parsed $cache = $this->EE->session->cache['stash']['__template_post_parse__']; // are we capturing the final output of the rendered EE host template? $save_output = FALSE; // run any postponed stash tags if (!empty($cache)) { $context = ''; if (!class_exists('Stash')) { include_once PATH_THIRD . 'stash/mod.stash.php'; } else { // get static context if it has been set $context = Stash::$context; } // save TMPL values for later $tagparams = isset($this->EE->TMPL->tagparams) ? $this->EE->TMPL->tagparams : array(); $tagdata = isset($this->EE->TMPL->tagdata) ? $this->EE->TMPL->tagdata : ''; // reset tagparams so Stash is instantiated with default values $this->EE->TMPL->tagparams = array(); // instantiate but don't initialise $s = new Stash(TRUE); // sort by priority $cache = $s->sort_by_key($cache, 'priority', 'sort_by_integer'); // loop through, prep the Stash instance, call the postponed tag and replace output into the placeholder foreach ($cache as $placeholder => $tag) { // make sure there is a placeholder in the template // it may have been removed by advanced conditional processing if (strpos($template, $placeholder) !== FALSE) { $this->EE->TMPL->log_item("Stash: post-processing tag: " . $tag['tagproper'] . " will be replaced into " . LD . $placeholder . RD); $this->EE->TMPL->tagparams = $tag['tagparams']; $this->EE->TMPL->tagdata = $tag['tagdata']; // restore context @ pointer in context parameter if (isset($this->EE->TMPL->tagparams['context']) && $this->EE->TMPL->tagparams['context'] == '@') { $this->EE->TMPL->tagparams['context'] = $context; } // restore context @ pointer if hardcoded in name parameter if (isset($this->EE->TMPL->tagparams['name']) && strncmp($this->EE->TMPL->tagparams['name'], '@:', 2) == 0) { $this->EE->TMPL->tagparams['name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['name']); } // restore context @ pointer if hardcoded in file_name parameter if (isset($this->EE->TMPL->tagparams['file_name']) && strncmp($this->EE->TMPL->tagparams['file_name'], '@:', 2) == 0) { $this->EE->TMPL->tagparams['file_name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['file_name']); } // has the save_output tag been called? if ($tag['method'] === 'save_output') { $save_output = $tag; $save_output['placeholder'] = $placeholder; } else { // initialise Stash with our custom tagparams $s->init(TRUE); $out = $s->{$tag['method']}(); $template = str_replace(LD . $placeholder . RD, $out, $template); // remove the placeholder from the cache so we don't iterate over it in future calls of this hook unset($this->EE->session->cache['stash']['__template_post_parse__'][$placeholder]); } } } // restore original TMPL values $this->EE->TMPL->tagparams = $tagparams; $this->EE->TMPL->tagdata = $tagdata; } // cache output to a static file if ($save_output) { $this->EE->TMPL->tagparams = $save_output['tagparams']; $s->init(TRUE); $template = str_replace(LD . $save_output['placeholder'] . RD, '', $template); $s->{$save_output['method']}($template); // restore original TMPL values $this->EE->TMPL->tagparams = $tagparams; } // cleanup unset($cache); } return $template; }
/** * Method for template_post_parse hook * * @param string Parsed template string * @param bool Whether an embed or not * @param integer Site ID * @param bool Has the extension been called by Stash rather than EE? * @param bool Final call of this extension * @return string Template string */ public function template_post_parse($template, $sub, $site_id, $from_stash = FALSE, $final = FALSE) { // play nice with other extensions on this hook if (isset($this->EE->extensions->last_call) && $this->EE->extensions->last_call) { $template = $this->EE->extensions->last_call; } // is this the final template? if ($sub == FALSE && $final == FALSE) { // check the cache for postponed tags if (!isset($this->EE->session->cache['stash']['__template_post_parse__'])) { $this->EE->session->cache['stash']['__template_post_parse__'] = array(); } // an array of tags needing to be post-parsed $cache = $this->EE->session->cache['stash']['__template_post_parse__']; // are we capturing the final output of the rendered EE host template? $save_output = FALSE; // run any postponed stash tags if (!empty($cache)) { $context = ''; if (!class_exists('Stash')) { include_once PATH_THIRD . 'stash/mod.stash.php'; } else { // get static context if it has been set $context = Stash::$context; } // save TMPL values for later $tagparams = isset($this->EE->TMPL->tagparams) ? $this->EE->TMPL->tagparams : array(); $tagdata = isset($this->EE->TMPL->tagdata) ? $this->EE->TMPL->tagdata : ''; // reset tagparams so Stash is instantiated with default values $this->EE->TMPL->tagparams = array(); // instantiate but don't initialise $s = new Stash(TRUE); // sort by priority $cache = $s->sort_by_key($cache, 'priority', 'sort_by_integer'); // loop through, prep the Stash instance, call the postponed tag and replace output into the placeholder foreach ($cache as $placeholder => $tag) { if (strpos($template, $placeholder) !== FALSE) { // make sure there is a placeholder in the template // it may have been removed by advanced conditional processing $this->EE->TMPL->log_item("Stash: post-processing tag: " . $tag['tagproper'] . " will be replaced into " . LD . $placeholder . RD); $this->EE->TMPL->tagparams = $tag['tagparams']; $this->EE->TMPL->tagdata = $tag['tagdata']; // restore context @ pointer in context parameter if (isset($this->EE->TMPL->tagparams['context']) && $this->EE->TMPL->tagparams['context'] == '@') { $this->EE->TMPL->tagparams['context'] = $context; } // restore context @ pointer if hardcoded in name parameter if (isset($this->EE->TMPL->tagparams['name']) && strncmp($this->EE->TMPL->tagparams['name'], '@:', 2) == 0) { $this->EE->TMPL->tagparams['name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['name']); } // restore context @ pointer if hardcoded in file_name parameter if (isset($this->EE->TMPL->tagparams['file_name']) && strncmp($this->EE->TMPL->tagparams['file_name'], '@:', 2) == 0) { $this->EE->TMPL->tagparams['file_name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['file_name']); } // initialise Stash with our custom tagparams $s->init(TRUE); // has the save_output or final_output tags been called? if ($tag['method'] === 'save_output' || $tag['method'] === 'final_output') { // remove placeholder from the template $template = str_replace(LD . $placeholder . RD, '', $template); // allow the called method to alter/cache the entire template $template = $s->{$tag['method']}($template); } else { // call the tag $out = $s->{$tag['method']}(); // replace the output of our tag into the template placeholder $template = str_replace(LD . $placeholder . RD, $out, $template); } // remove the placeholder from the cache so we don't iterate over it in future calls of this hook unset($this->EE->session->cache['stash']['__template_post_parse__'][$placeholder]); } } // restore original TMPL values $this->EE->TMPL->tagparams = $tagparams; $this->EE->TMPL->tagdata = $tagdata; } // cleanup unset($cache); // just before the template is sent to output if (FALSE == $from_stash) { // batch processing of cached variables $this->EE->load->model('stash_model'); // get the query queue by reference $queue =& $this->EE->stash_model->get_queue(); // we need to flatten the data in the queue to a string, so we can parse it // first, let's extract the data into a simple indexed array... $data = array(); foreach ($queue->inserts as $table => $inserts) { foreach ($inserts as $query) { $data[] = $query['parameters']; // will always exist } } foreach ($queue->updates as $table => $updates) { foreach ($updates as $query) { if (isset($query['parameters'])) { $data[] = $query['parameters']; } } } if (count($data) > 0) { // flatten data so we can parse it $delim = '|' . $this->EE->functions->random() . '|'; $data = (string) implode($delim, $data); // Run template_post_parse on the flattened data. // We need to disable the in_progress recursion check in EE_Extensions::universal_call // don't even think about making this private, @pkriete ! $this->EE->extensions->in_progress = ''; $data = $this->EE->extensions->call('template_post_parse', $data, FALSE, $this->EE->config->item('site_id'), TRUE, TRUE); $this->EE->extensions->in_progress = 'template_post_parse'; // restore recursion check // explode the data back into an array $data = (array) explode($delim, $data); // update the queues with the parsed parameter values foreach ($queue->inserts as $table => $inserts) { foreach ($inserts as $cache_key => $query) { $queue->inserts[$table][$cache_key]['parameters'] = array_shift($data); } } foreach ($queue->updates as $table => $updates) { foreach ($updates as $cache_key => $query) { if (isset($query['parameters'])) { $queue->updates[$table][$cache_key]['parameters'] = array_shift($data); } } } unset($data); } // process inserts/updates queue $this->EE->TMPL->log_item("Stash: batch processing queued queries"); $this->EE->stash_model->process_queue(); } } return $template; }