Gracefully catches Twig exceptions and displays error messages
MAILPOET-667 #time 3h
This commit is contained in:
@ -146,8 +146,9 @@ class Initializer {
|
||||
}
|
||||
|
||||
function setupRenderer() {
|
||||
$renderer = new Renderer();
|
||||
$this->renderer = $renderer->init();
|
||||
$caching = !WP_DEBUG;
|
||||
$debugging = WP_DEBUG;
|
||||
$this->renderer = new Renderer($caching, $debugging);
|
||||
}
|
||||
|
||||
function setupLocalizer() {
|
||||
@ -210,4 +211,4 @@ class Initializer {
|
||||
function handleFailedInitialization($message) {
|
||||
return WPNotice::displayError($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ use MailPoet\Subscribers\ImportExport\ImportExportFactory;
|
||||
use MailPoet\Listing;
|
||||
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
|
||||
use MailPoet\WP\DateTime;
|
||||
use MailPoet\WP\Notice as WPNotice;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -215,7 +216,7 @@ class Menu {
|
||||
'redirect_url' => $redirect_url,
|
||||
'sub_menu' => 'mailpoet-newsletters'
|
||||
);
|
||||
echo $this->renderer->render('welcome.html', $data);
|
||||
$this->displayPage('welcome.html', $data);
|
||||
}
|
||||
|
||||
function update() {
|
||||
@ -241,7 +242,7 @@ class Menu {
|
||||
'sub_menu' => 'mailpoet-newsletters'
|
||||
);
|
||||
|
||||
echo $this->renderer->render('update.html', $data);
|
||||
$this->displayPage('update.html', $data);
|
||||
}
|
||||
|
||||
function settings() {
|
||||
@ -263,7 +264,7 @@ class Menu {
|
||||
)
|
||||
);
|
||||
|
||||
echo $this->renderer->render('settings.html', $data);
|
||||
$this->displayPage('settings.html', $data);
|
||||
}
|
||||
|
||||
private function _getFlags() {
|
||||
@ -315,7 +316,7 @@ class Menu {
|
||||
$data['date_formats'] = Block\Date::getDateFormats();
|
||||
$data['month_names'] = Block\Date::getMonthNames();
|
||||
|
||||
echo $this->renderer->render('subscribers/subscribers.html', $data);
|
||||
$this->displayPage('subscribers/subscribers.html', $data);
|
||||
}
|
||||
|
||||
function segments() {
|
||||
@ -323,7 +324,7 @@ class Menu {
|
||||
|
||||
$data = array();
|
||||
$data['items_per_page'] = $this->getLimitPerPage('segments');
|
||||
echo $this->renderer->render('segments.html', $data);
|
||||
$this->displayPage('segments.html', $data);
|
||||
}
|
||||
|
||||
function forms() {
|
||||
@ -334,7 +335,7 @@ class Menu {
|
||||
$data['items_per_page'] = $this->getLimitPerPage('forms');
|
||||
$data['segments'] = Segment::findArray();
|
||||
|
||||
echo $this->renderer->render('forms.html', $data);
|
||||
$this->displayPage('forms.html', $data);
|
||||
}
|
||||
|
||||
function newsletters() {
|
||||
@ -364,7 +365,7 @@ class Menu {
|
||||
wp_enqueue_script('jquery-ui');
|
||||
wp_enqueue_script('jquery-ui-datepicker');
|
||||
|
||||
echo $this->renderer->render('newsletters.html', $data);
|
||||
$this->displayPage('newsletters.html', $data);
|
||||
}
|
||||
|
||||
function newletterEditor() {
|
||||
@ -377,7 +378,7 @@ class Menu {
|
||||
wp_enqueue_media();
|
||||
wp_enqueue_script('tinymce-wplink', includes_url('js/tinymce/plugins/wplink/plugin.js'));
|
||||
wp_enqueue_style('editor', includes_url('css/editor.css'));
|
||||
echo $this->renderer->render('newsletter/editor.html', $data);
|
||||
$this->displayPage('newsletter/editor.html', $data);
|
||||
}
|
||||
|
||||
function import() {
|
||||
@ -389,14 +390,14 @@ class Menu {
|
||||
'month_names' => Block\Date::getMonthNames(),
|
||||
'sub_menu' => 'mailpoet-subscribers'
|
||||
));
|
||||
echo $this->renderer->render('subscribers/importExport/import.html', $data);
|
||||
$this->displayPage('subscribers/importExport/import.html', $data);
|
||||
}
|
||||
|
||||
function export() {
|
||||
$export = new ImportExportFactory('export');
|
||||
$data = $export->bootstrap();
|
||||
$data['sub_menu'] = 'mailpoet-subscribers';
|
||||
echo $this->renderer->render('subscribers/importExport/export.html', $data);
|
||||
$this->displayPage('subscribers/importExport/export.html', $data);
|
||||
}
|
||||
|
||||
function formEditor() {
|
||||
@ -417,7 +418,7 @@ class Menu {
|
||||
'sub_menu' => 'mailpoet-forms'
|
||||
);
|
||||
|
||||
echo $this->renderer->render('form/editor.html', $data);
|
||||
$this->displayPage('form/editor.html', $data);
|
||||
}
|
||||
|
||||
function setPageTitle($title) {
|
||||
@ -428,6 +429,13 @@ class Menu {
|
||||
);
|
||||
}
|
||||
|
||||
function displaySubscriberLimitExceededTemplate() {
|
||||
$this->displayPage('limit.html', array(
|
||||
'limit' => SubscribersFeature::SUBSCRIBERS_LIMIT
|
||||
));
|
||||
exit;
|
||||
}
|
||||
|
||||
private function getLimitPerPage($model = null) {
|
||||
if($model === null) {
|
||||
return Listing\Handler::DEFAULT_LIMIT_PER_PAGE;
|
||||
@ -441,10 +449,12 @@ class Menu {
|
||||
: Listing\Handler::DEFAULT_LIMIT_PER_PAGE;
|
||||
}
|
||||
|
||||
function displaySubscriberLimitExceededTemplate() {
|
||||
echo $this->renderer->render('limit.html', array(
|
||||
'limit' => SubscribersFeature::SUBSCRIBERS_LIMIT
|
||||
));
|
||||
exit;
|
||||
private function displayPage($template, $data) {
|
||||
try {
|
||||
echo $this->renderer->render($template, $data);
|
||||
} catch (\Exception $e) {
|
||||
$notice = new WPNotice(WPNotice::TYPE_ERROR, $e->getMessage());
|
||||
$notice->displayWPNotice();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,19 +8,26 @@ use \MailPoet\Twig;
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Renderer {
|
||||
function __construct() {
|
||||
protected $cache_path;
|
||||
protected $caching_enabled;
|
||||
protected $debugging_enabled;
|
||||
protected $renderer;
|
||||
|
||||
function __construct($caching_enabled = false, $debugging_enabled = false) {
|
||||
$this->caching_enabled = $caching_enabled;
|
||||
$this->debuggin_enabled = $debugging_enabled;
|
||||
$this->cache_path = Env::$temp_path . '/cache';
|
||||
|
||||
$file_system = new TwigFileSystem(Env::$views_path);
|
||||
$this->renderer = new TwigEnv(
|
||||
$file_system,
|
||||
array(
|
||||
'cache' => $this->detectCache(),
|
||||
'debug' => WP_DEBUG,
|
||||
'debug' => $this->debugging_enabled,
|
||||
'auto_reload' => true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function init() {
|
||||
$this->setupDebug();
|
||||
$this->setupTranslations();
|
||||
$this->setupFunctions();
|
||||
@ -28,8 +35,6 @@ class Renderer {
|
||||
$this->setupHelpscout();
|
||||
$this->setupGlobalVariables();
|
||||
$this->setupSyntax();
|
||||
|
||||
return $this->renderer;
|
||||
}
|
||||
|
||||
function setupTranslations() {
|
||||
@ -66,16 +71,30 @@ class Renderer {
|
||||
}
|
||||
|
||||
function detectCache() {
|
||||
$cache_path = Env::$temp_path . '/cache';
|
||||
if(WP_DEBUG === false) {
|
||||
return $cache_path;
|
||||
}
|
||||
return false;
|
||||
return $this->caching_enabled ? $this->cache_path : false;
|
||||
}
|
||||
|
||||
function setupDebug() {
|
||||
if(WP_DEBUG === true) {
|
||||
if($this->debugging_enabled) {
|
||||
$this->renderer->addExtension(new \Twig_Extension_Debug());
|
||||
}
|
||||
}
|
||||
|
||||
function render($template, $context = array()) {
|
||||
try {
|
||||
return $this->renderer->render($template, $context);
|
||||
} catch(\RuntimeException $e) {
|
||||
throw new \Exception(sprintf(
|
||||
__('Failed to render template "%s". Please ensure the template cache folder "%s" exists and has write permissions. Terminated with error: "%s"'),
|
||||
$template,
|
||||
$this->cache_path,
|
||||
$e->getMessage()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function addGlobal($key, $value) {
|
||||
return $this->renderer->addGlobal($key, $value);
|
||||
}
|
||||
}
|
||||
|
@ -73,7 +73,11 @@ class Widget {
|
||||
)
|
||||
);
|
||||
|
||||
echo $this->renderer->render('form/iframe.html', $data);
|
||||
try {
|
||||
echo $this->renderer->render('form/iframe.html', $data);
|
||||
} catch(\Exception $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
@ -165,9 +165,12 @@ class Widget extends \WP_Widget {
|
||||
|
||||
// render form
|
||||
$renderer = new Renderer();
|
||||
$renderer = $renderer->init();
|
||||
$output = $renderer->render('form/widget.html', $data);
|
||||
$output = do_shortcode($output);
|
||||
try {
|
||||
$output = $renderer->render('form/widget.html', $data);
|
||||
$output = do_shortcode($output);
|
||||
} catch(\Exception $e) {
|
||||
$output = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
if($form_type === 'widget') {
|
||||
|
@ -11,7 +11,7 @@ class Notice {
|
||||
private $type;
|
||||
private $message;
|
||||
|
||||
protected function __construct($type, $message) {
|
||||
function __construct($type, $message) {
|
||||
$this->type = $type;
|
||||
$this->message = $message;
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
<?php
|
||||
use Codeception\Util\Stub;
|
||||
use \MailPoet\Config\Renderer;
|
||||
|
||||
class RendererTest extends MailPoetTest {
|
||||
function _before() {
|
||||
$this->renderer = new Renderer();
|
||||
$this->renderer = new Renderer($caching = false, $debugging = false);
|
||||
}
|
||||
|
||||
function testItWillNotEnableCacheWhenWpDebugIsOn() {
|
||||
@ -11,6 +12,47 @@ class RendererTest extends MailPoetTest {
|
||||
expect($result)->equals(false);
|
||||
}
|
||||
|
||||
function testItDelegatesRenderingToTwig() {
|
||||
$renderer = Stub::construct(
|
||||
$this->renderer,
|
||||
array(),
|
||||
array(
|
||||
'renderer' => Stub::makeEmpty('Twig_Environment',
|
||||
array(
|
||||
'render' => Stub::atLeastOnce(function() { return 'test render'; }),
|
||||
)
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
expect($renderer->render('non-existing-template.html', array('somekey' => 'someval')))->equals('test render');
|
||||
}
|
||||
|
||||
function testItRethrowsTwigCacheExceptions() {
|
||||
$exception_message = 'this is a test error';
|
||||
$renderer = Stub::construct(
|
||||
$this->renderer,
|
||||
array(true, false),
|
||||
array(
|
||||
'renderer' => Stub::makeEmpty('Twig_Environment',
|
||||
array(
|
||||
'render' => Stub::atLeastOnce(function() use ($exception_message) {
|
||||
throw new \RuntimeException($exception_message);
|
||||
}),
|
||||
)
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
$renderer->render('non-existing-template.html', array('somekey' => 'someval'));
|
||||
self::fail('Twig exception was not rethrown');
|
||||
} catch(\Exception $e) {
|
||||
expect($e->getMessage())->contains($exception_message);
|
||||
expect($e->getMessage())->notEquals($exception_message);
|
||||
}
|
||||
}
|
||||
|
||||
function _after() {
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user