diff --git a/lib/Twig/Functions.php b/lib/Twig/Functions.php index e7ea6b2cdb..4e7fd81b7d 100644 --- a/lib/Twig/Functions.php +++ b/lib/Twig/Functions.php @@ -21,9 +21,13 @@ class Functions extends AbstractExtension { /** @var WooCommerceHelper */ private $woocommerce_helper; + /** @var WPFunctions */ + private $wp; + public function __construct() { $this->settings = new SettingsController(); $this->woocommerce_helper = new WooCommerceHelper(); + $this->wp = WPFunctions::get(); } function getFunctions() { @@ -157,10 +161,10 @@ class Functions extends AbstractExtension { $label = null; $labels = [ - 'minute' => WPFunctions::get()->__('every minute', 'mailpoet'), - 'minutes' => WPFunctions::get()->__('every %1$d minutes', 'mailpoet'), - 'hour' => WPFunctions::get()->__('every hour', 'mailpoet'), - 'hours' => WPFunctions::get()->__('every %1$d hours', 'mailpoet'), + 'minute' => $this->wp->__('every minute', 'mailpoet'), + 'minutes' => $this->wp->__('every %1$d minutes', 'mailpoet'), + 'hour' => $this->wp->__('every hour', 'mailpoet'), + 'hours' => $this->wp->__('every %1$d hours', 'mailpoet'), ]; if ($value >= 60) { @@ -188,11 +192,11 @@ class Functions extends AbstractExtension { } function getWPDateFormat() { - return WPFunctions::get()->getOption('date_format') ?: 'F j, Y'; + return $this->wp->getOption('date_format') ?: 'F j, Y'; } function getWPStartOfWeek() { - return WPFunctions::get()->getOption('start_of_week') ?: 0; + return $this->wp->getOption('start_of_week') ?: 0; } function getMailPoetVersion() { @@ -204,7 +208,7 @@ class Functions extends AbstractExtension { } function getWPTimeFormat() { - return WPFunctions::get()->getOption('time_format') ?: 'g:i a'; + return $this->wp->getOption('time_format') ?: 'g:i a'; } function getWPDateTimeFormat() { @@ -212,7 +216,7 @@ class Functions extends AbstractExtension { } function params($key = null) { - $args = WPFunctions::get()->stripslashesDeep($_GET); + $args = $this->wp->stripslashesDeep($_GET); if (array_key_exists($key, $args)) { return $args[$key]; } @@ -231,11 +235,11 @@ class Functions extends AbstractExtension { } function isRtl() { - return WPFunctions::get()->isRtl(); + return $this->wp->isRtl(); } function getTwoLettersLocale() { - return explode('_', WPFunctions::get()->getLocale())[0]; + return explode('_', $this->wp->getLocale())[0]; } function getFreeDomains() { diff --git a/tests/integration/Twig/FunctionsTest.php b/tests/integration/Twig/FunctionsTest.php index 5003940582..3389512063 100644 --- a/tests/integration/Twig/FunctionsTest.php +++ b/tests/integration/Twig/FunctionsTest.php @@ -10,19 +10,15 @@ class FunctionsTest extends \MailPoetTest { function testItExecutesIsRtlFunction() { $template = ['template' => '{% if is_rtl() %}rtl{% endif %}']; $twig = new \MailPoetVendor\Twig_Environment(new \MailPoetVendor\Twig_Loader_Array($template)); + WPFunctions::set(Stub::make(new WPFunctions, [ + 'isRtl' => Stub::consecutive(true, false), + ])); + $twig->addExtension(new Functions()); - - WPFunctions::set(Stub::make(new WPFunctions, [ - 'isRtl' => true, - ])); - $result = $twig->render('template'); - expect($result)->equals('rtl'); - - WPFunctions::set(Stub::make(new WPFunctions, [ - 'isRtl' => false, - ])); - $result = $twig->render('template'); - expect($result)->isEmpty(); + $result_rtl = $twig->render('template'); + expect($result_rtl)->equals('rtl'); + $result_no_rtl = $twig->render('template'); + expect($result_no_rtl)->isEmpty(); } function _after() {