This commit is contained in:
emmymayo
2025-02-05 23:15:46 +01:00
commit 7269c99357
16995 changed files with 3389680 additions and 0 deletions
@@ -0,0 +1,61 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\Twig;
if (!defined('ABSPATH')) exit;
use MailPoet\Analytics\Analytics as AnalyticsGenerator;
use MailPoet\DI\ContainerWrapper;
use MailPoet\InvalidStateException;
use MailPoetVendor\Twig\Extension\AbstractExtension;
use MailPoetVendor\Twig\TwigFunction;
class Analytics extends AbstractExtension {
/** @var AnalyticsGenerator */
private $analytics;
public function getFunctions() {
return [
new TwigFunction(
'is_analytics_enabled',
[$this, 'isEnabled'],
['is_safe' => ['all']]
),
new TwigFunction(
'get_analytics_public_id',
[$this, 'getPublicId'],
['is_safe' => ['all']]
),
new TwigFunction(
'is_analytics_public_id_new',
[$this, 'isPublicIdNew'],
['is_safe' => ['all']]
),
];
}
private function getAnalytics() {
if ($this->analytics === null) {
$this->analytics = ContainerWrapper::getInstance()->get(AnalyticsGenerator::class);
}
if (!$this->analytics instanceof AnalyticsGenerator) {
throw new InvalidStateException('AnalyticsGenerator service was not registered!');
}
return $this->analytics;
}
public function isEnabled() {
return $this->getAnalytics()->isEnabled();
}
public function getPublicId() {
return $this->getAnalytics()->getPublicId();
}
public function isPublicIdNew() {
return $this->getAnalytics()->isPublicIdNew();
}
}
@@ -0,0 +1,96 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\Twig;
if (!defined('ABSPATH')) exit;
use MailPoet\Config\Env;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Util\CdnAssetUrl;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Twig\Extension\AbstractExtension;
use MailPoetVendor\Twig\TwigFunction;
class Assets extends AbstractExtension {
private $globals;
/** @var WPFunctions */
private $wp;
/** @var CdnAssetUrl|null */
private $cdnAssetsUrl;
public function __construct(
array $globals,
WPFunctions $wp,
CdnAssetUrl $cdnAssetsUrl = null
) {
$this->globals = $globals;
$this->wp = $wp;
$this->cdnAssetsUrl = $cdnAssetsUrl;
}
public function getFunctions() {
return [
new TwigFunction(
'getJavascriptScriptUrl',
[$this, 'getJavascriptScriptUrl'],
['is_safe' => ['all']]
),
new TwigFunction(
'image_url',
[$this, 'generateImageUrl'],
['is_safe' => ['all']]
),
new TwigFunction(
'cdn_url',
[$this, 'generateCdnUrl'],
['is_safe' => ['all']]
),
new TwigFunction(
'language',
[$this, 'language'],
['is_safe' => ['all']]
),
];
}
/**
* Returns the language, which is currently loaded.
* This function is used to add the language tag for our system emails like stats notifications.
*/
public function language() {
// If we do not have a translation, the language of the mail will be English.
if (!is_textdomain_loaded('mailpoet')) {
return 'en';
}
return (string)$this->wp->getBlogInfo('language');
}
public function getJavascriptScriptUrl($script) {
return sprintf(
'%s/%s/%s?ver=%s',
$this->globals['assets_url'],
strpos($script, 'lib/') === 0 ? 'js' : 'dist/js',
$this->getAssetFileName($this->globals['assets_manifest_js'], $script),
Env::$version
);
}
public function generateImageUrl($path) {
return $this->globals['assets_url'] . '/img/' . $path;
}
public function getAssetFileName($manifest, $asset) {
return (!empty($manifest[$asset])) ? $manifest[$asset] : $asset;
}
public function generateCdnUrl($path) {
if ($this->cdnAssetsUrl === null) {
$this->cdnAssetsUrl = ContainerWrapper::getInstance()->get(CdnAssetUrl::class);
}
return $this->cdnAssetsUrl->generateCdnUrl($path);
}
}
@@ -0,0 +1,39 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\Twig;
if (!defined('ABSPATH')) exit;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Twig\Extension\AbstractExtension;
use MailPoetVendor\Twig\TwigFilter;
class Filters extends AbstractExtension {
public function getName() {
return 'filters';
}
public function getFilters() {
return [
new TwigFilter(
'intval',
'intval'
),
new TwigFilter(
'replaceLinkTags',
'MailPoet\Util\Helpers::replaceLinkTags'
),
new TwigFilter(
'wpKses',
[$this, 'wpKses'],
['is_safe' => ['html']]
),
];
}
public function wpKses($content, $allowedHtml) {
$wp = WPFunctions::get();
return $wp->wpKses($content, $allowedHtml);
}
}
@@ -0,0 +1,383 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\Twig;
if (!defined('ABSPATH')) exit;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Referrals\UrlDecorator;
use MailPoet\Settings\SettingsController;
use MailPoet\Util\FreeDomains;
use MailPoet\Util\Notices\PendingApprovalNotice;
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
use MailPoet\WP\Functions as WPFunctions;
use MailPoet\WPCOM\DotcomHelperFunctions;
use MailPoetVendor\Carbon\Carbon;
use MailPoetVendor\Twig\Extension\AbstractExtension;
use MailPoetVendor\Twig\TwigFunction;
class Functions extends AbstractExtension {
/** @var SettingsController */
private $settings = null;
/** @var WooCommerceHelper */
private $woocommerceHelper = null;
/** @var WPFunctions */
private $wp = null;
/** @var UrlDecorator */
private $referralUrlDecorator = null;
/** @var PendingApprovalNotice */
private $pendingApprovalNotice = null;
private function getWooCommerceHelper(): WooCommerceHelper {
if ($this->woocommerceHelper === null) {
$this->woocommerceHelper = new WooCommerceHelper($this->getWp());
}
return $this->woocommerceHelper;
}
private function getreferralUrlDecorator(): UrlDecorator {
if ($this->referralUrlDecorator === null) {
$this->referralUrlDecorator = new UrlDecorator($this->getWp(), $this->getSettings());
}
return $this->referralUrlDecorator;
}
private function getSettings(): SettingsController {
if ($this->settings === null) {
$this->settings = SettingsController::getInstance();
}
return $this->settings;
}
private function getDotcomHelperFunctions(): DotcomHelperFunctions {
return ContainerWrapper::getInstance()->get(DotcomHelperFunctions::class);
}
private function getWp(): WPFunctions {
if ($this->wp === null) {
$this->wp = WPFunctions::get();
}
return $this->wp;
}
private function getPendingApprovalNotice(): PendingApprovalNotice {
if ($this->pendingApprovalNotice === null) {
$this->pendingApprovalNotice = ContainerWrapper::getInstance()->get(PendingApprovalNotice::class);
}
return $this->pendingApprovalNotice;
}
public function getFunctions() {
return [
new TwigFunction(
'json_encode',
'json_encode',
['is_safe' => ['all']]
),
new TwigFunction(
'json_decode',
'json_decode',
['is_safe' => ['all']]
),
new TwigFunction(
'wp_nonce_field',
'wp_nonce_field',
['is_safe' => ['all']]
),
new TwigFunction(
'params',
[$this, 'params'],
['is_safe' => ['all']]
),
new TwigFunction(
'admin_url',
'admin_url',
['is_safe' => ['all']]
),
new TwigFunction(
'get_option',
'get_option',
['is_safe' => ['all']]
),
new TwigFunction(
'sending_frequency',
[$this, 'getSendingFrequency'],
['is_safe' => ['all']]
),
new TwigFunction(
'wp_date_format',
[$this, 'getWPDateFormat'],
['is_safe' => ['all']]
),
new TwigFunction(
'mailpoet_version',
[$this, 'getMailPoetVersion'],
['is_safe' => ['all']]
),
new TwigFunction(
'mailpoet_premium_version',
[$this, 'getMailPoetPremiumVersion'],
['is_safe' => ['all']]
),
new TwigFunction(
'wp_date_format',
[$this, 'getWPDateFormat'],
['is_safe' => ['all']]
),
new TwigFunction(
'wp_time_format',
[$this, 'getWPTimeFormat'],
['is_safe' => ['all']]
),
new TwigFunction(
'wp_datetime_format',
[$this, 'getWPDateTimeFormat'],
['is_safe' => ['all']]
),
new TwigFunction(
'do_action',
'do_action',
['is_safe' => ['all']]
),
new TwigFunction(
'is_rtl',
[$this, 'isRtl'],
['is_safe' => ['all']]
),
new TwigFunction(
'number_format_i18n',
'number_format_i18n',
['is_safe' => ['all']]
),
new TwigFunction(
'mailpoet_locale',
[$this, 'getTwoLettersLocale'],
['is_safe' => ['all']]
),
new TwigFunction(
'mailpoet_free_domains',
[$this, 'getFreeDomains'],
['is_safe' => ['all']]
),
new TwigFunction(
'is_woocommerce_active',
[$this, 'isWoocommerceActive'],
['is_safe' => ['all']]
),
new TwigFunction(
'get_woocommerce_version',
[$this, 'getWooCommerceVersion'],
['is_safe' => ['all']]
),
new TwigFunction(
'wp_start_of_week',
[$this, 'getWPStartOfWeek'],
['is_safe' => ['all']]
),
new TwigFunction(
'stats_color',
[$this, 'statsColor'],
['is_safe' => ['all']]
),
new TwigFunction(
'clicked_stats_text',
[$this, 'clickedStatsText'],
['is_safe' => ['all']]
),
new TwigFunction(
'stats_number_format_i18n',
[$this, 'statsNumberFormatI18n'],
['is_safe' => ['all']]
),
new TwigFunction(
'add_referral_id',
[$this, 'addReferralId'],
['is_safe' => ['all']]
),
new TwigFunction(
'is_loading_3rd_party_enabled',
[$this, 'libs3rdPartyEnabled'],
['is_safe' => ['all']]
),
new TwigFunction(
'is_dotcom_ecommerce_plan',
[$this, 'isDotcomEcommercePlan'],
['is_safe' => ['all']]
),
new TwigFunction(
'is_dotcom',
[$this, 'isDotcom'],
['is_safe' => ['all']]
),
new TwigFunction(
'pending_approval_message',
[$this, 'pendingApprovalMessage'],
['is_safe' => ['html']]
),
];
}
public function getSendingFrequency() {
/** @var string[] $args */
$args = func_get_args();
$value = (int)array_shift($args);
$label = null;
$labels = [
'minute' => __('every minute', 'mailpoet'),
// translators: %1$d is the amount of minutes.
'minutes' => __('every %1$d minutes', 'mailpoet'),
'hour' => __('every hour', 'mailpoet'),
// translators: %1$d is the amount of hours.
'hours' => __('every %1$d hours', 'mailpoet'),
];
if ($value >= 60) {
// we're dealing with hours
if ($value === 60) {
$label = $labels['hour'];
} else {
$label = $labels['hours'];
}
$value /= 60;
} else {
// we're dealing with minutes
if ($value === 1) {
$label = $labels['minute'];
} else {
$label = $labels['minutes'];
}
}
return sprintf($label, $value);
}
public function getWPDateFormat() {
return $this->getWp()->getOption('date_format') ?: 'F j, Y';
}
public function getWPStartOfWeek() {
return $this->getWp()->getOption('start_of_week') ?: 0;
}
public function getMailPoetVersion() {
return MAILPOET_VERSION;
}
public function getMailPoetPremiumVersion() {
return (defined('MAILPOET_PREMIUM_VERSION')) ? MAILPOET_PREMIUM_VERSION : false;
}
public function getWPTimeFormat() {
return $this->getWp()->getOption('time_format') ?: 'g:i a';
}
public function getWPDateTimeFormat() {
return sprintf('%s %s', $this->getWPDateFormat(), $this->getWPTimeFormat());
}
public function params($key = null) {
$args = $this->getWp()->stripslashesDeep($_GET);
if (array_key_exists($key, $args)) {
return $args[$key];
}
return null;
}
public function installedInLastTwoWeeks() {
$maxNumberOfWeeks = 2;
$installedAt = Carbon::createFromFormat('Y-m-d H:i:s', $this->getSettings()->get('installed_at'));
if ($installedAt === false) {
return false;
}
return $installedAt->diffInWeeks(Carbon::now()) < $maxNumberOfWeeks;
}
public function isRtl() {
return $this->getWp()->isRtl();
}
public function getTwoLettersLocale() {
return explode('_', $this->getWp()->getLocale())[0];
}
public function getFreeDomains() {
return FreeDomains::FREE_DOMAINS;
}
public function isWoocommerceActive() {
return $this->getWooCommerceHelper()->isWooCommerceActive();
}
public function getWooCommerceVersion() {
return $this->getWooCommerceHelper()->getWooCommerceVersion();
}
public function statsColor($percentage) {
if ($percentage > 3) {
return '#7ed321';
} elseif ($percentage > 1) {
return '#ff9f00';
} else {
return '#f559c3';
}
}
public function clickedStatsText($clicked) {
if ($clicked > 3) {
return __('Excellent', 'mailpoet');
} elseif ($clicked > 1) {
return __('Good', 'mailpoet');
} else {
return __('Average', 'mailpoet');
}
}
/**
* Wrapper around number_format_i18n() to return two decimals digits if the number
* is smaller than 0.1 and one decimal digit if the number is equal or greater
* than 0.1.
*
* @param int|float $number
*
* @return string
*/
public function statsNumberFormatI18n($number) {
if ($number < 0.1) {
$decimals = 2;
} else {
$decimals = 1;
}
return number_format_i18n($number, $decimals);
}
public function addReferralId($url) {
return $this->getreferralUrlDecorator()->decorate($url);
}
public function libs3rdPartyEnabled(): bool {
return $this->getSettings()->get('3rd_party_libs.enabled') === '1';
}
public function isDotcomEcommercePlan(): bool {
if (function_exists('wc_calypso_bridge_is_ecommerce_plan')) {
return wc_calypso_bridge_is_ecommerce_plan();
}
return false;
}
public function isDotcom(): bool {
return $this->getDotcomHelperFunctions()->isDotcom();
}
public function pendingApprovalMessage(): string {
return $this->getPendingApprovalNotice()->getPendingApprovalMessage();
}
}
@@ -0,0 +1,74 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\Twig;
if (!defined('ABSPATH')) exit;
use MailPoetVendor\Twig\Extension\AbstractExtension;
use MailPoetVendor\Twig\Extension\CoreExtension;
use MailPoetVendor\Twig\TwigFunction;
class Handlebars extends AbstractExtension {
public function getFunctions() {
return [
new TwigFunction(
'partial',
[
$this,
'generatePartial',
],
[
'needs_environment' => true,
'needs_context' => true,
'is_safe' => ['all'],
]
),
];
}
public function generatePartial($env, $context) {
// get arguments (minus env & $context)
/** @var array{0:string, 1:array|string, 2:string} $args */
$args = array_slice(func_get_args(), 2);
$argsCount = count($args);
// default values
$alias = null;
switch ($argsCount) {
case 2:
list($id, $file) = $args;
break;
case 3:
list($id, $file, $alias) = $args;
break;
default:
return;
}
$renderedTemplate = CoreExtension::include($env, $context, $file);
$output = <<<EOL
<script id="$id" type="text/x-handlebars-template">
$renderedTemplate
</script>
EOL;
if ($alias !== null) {
$output .= <<<EOL
<script type="text/javascript">
jQuery(function($) {
$(function() {
Handlebars.registerPartial(
'$alias',
jQuery('#$id').html()
);
});
});
</script>
EOL;
}
return $output;
}
}
@@ -0,0 +1,127 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\Twig;
if (!defined('ABSPATH')) exit;
use MailPoet\Config\Localizer;
use MailPoet\InvalidStateException;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Twig\Extension\AbstractExtension;
use MailPoetVendor\Twig\TwigFunction;
class I18n extends AbstractExtension {
private $textDomains;
public function __construct(
$textDomain
) {
// set text domain
$this->textDomains = [$textDomain, 'woocommerce'];
}
public function getFunctions() {
// twig custom functions
$twigFunctions = [];
// list of WP functions to map
$functions = [
'localize' => 'localize',
'__' => 'translate',
'esc_html__' => 'translateEscHTML',
'esc_attr__' => 'translateEscAttr',
'_n' => 'pluralize',
'_x' => 'translateWithContext',
'get_locale' => 'getLocale',
'date' => 'date',
];
foreach ($functions as $twigFunction => $function) {
$callable = [$this, $function];
if (!is_callable($callable)) {
throw new InvalidStateException('Trying to register non-existing function to Twig.');
}
$twigFunctions[] = new TwigFunction(
$twigFunction,
$callable,
['is_safe' => ['all']]
);
}
return $twigFunctions;
}
public function localize() {
$args = func_get_args();
/** @var array $translations */
$translations = array_shift($args);
$output = [];
foreach ($translations as $key => $translation) {
$output[] =
'MailPoet.I18n.add("' . $key . '", "' . str_replace(['"', "\n", "\r"], ['\"', " ", ""], $translation ?? '') . '");';
}
WPFunctions::get()->wpAddInlineScript('mailpoet_mailpoet', join("\n", $output));
}
public function translate() {
$args = func_get_args();
return call_user_func_array('__', $this->setTextDomain($args));
}
public function translateEscHTML() {
$args = func_get_args();
return call_user_func_array('esc_html__', $this->setTextDomain($args));
}
public function translateEscAttr() {
$args = func_get_args();
return call_user_func_array('esc_attr__', $this->setTextDomain($args));
}
public function pluralize() {
$args = func_get_args();
return call_user_func_array('_n', $this->setTextDomain($args));
}
public function translateWithContext() {
$args = func_get_args();
return call_user_func_array('_x', $this->setTextDomain($args));
}
public function getLocale() {
$localizer = new Localizer;
return $localizer->locale();
}
public function date() {
$args = func_get_args();
/** @var int|null $date */
$date = (isset($args[0])) ? $args[0] : null;
$dateFormat = (isset($args[1])) ? $args[1] : WPFunctions::get()->getOption('date_format');
if (empty($date)) return;
// check if it's an int passed as a string
if ((string)(int)$date === $date) {
$date = (int)$date;
} else if (!is_int($date)) {
$date = strtotime($date);
}
return WPFunctions::get()->getDateFromGmt(date('Y-m-d H:i:s', (int)$date), $dateFormat);
}
private function setTextDomain($args = []) {
// make sure that the last argument is our text domain
if (!in_array($args[count($args) - 1], $this->textDomains)) {
// otherwise add it to the list of arguments
$args[] = $this->textDomains[0];
}
return $args;
}
}
@@ -0,0 +1 @@
<?php