Açıklama Yok

Functions.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace MailPoet\Twig;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Referrals\UrlDecorator;
  5. use MailPoet\Settings\SettingsController;
  6. use MailPoet\Util\FreeDomains;
  7. use MailPoet\WooCommerce\Helper as WooCommerceHelper;
  8. use MailPoet\WP\Functions as WPFunctions;
  9. use MailPoetVendor\Carbon\Carbon;
  10. use MailPoetVendor\Twig\Extension\AbstractExtension;
  11. use MailPoetVendor\Twig\TwigFunction;
  12. class Functions extends AbstractExtension {
  13. /** @var SettingsController */
  14. private $settings;
  15. /** @var WooCommerceHelper */
  16. private $woocommerceHelper;
  17. /** @var WPFunctions */
  18. private $wp;
  19. /** @var UrlDecorator */
  20. private $referralUrlDecorator;
  21. public function __construct() {
  22. $this->settings = SettingsController::getInstance();
  23. $this->woocommerceHelper = new WooCommerceHelper();
  24. $this->wp = WPFunctions::get();
  25. $this->referralUrlDecorator = new UrlDecorator($this->wp, $this->settings);
  26. }
  27. public function getFunctions() {
  28. return [
  29. new TwigFunction(
  30. 'json_encode',
  31. 'json_encode',
  32. ['is_safe' => ['all']]
  33. ),
  34. new TwigFunction(
  35. 'json_decode',
  36. 'json_decode',
  37. ['is_safe' => ['all']]
  38. ),
  39. new TwigFunction(
  40. 'wp_nonce_field',
  41. 'wp_nonce_field',
  42. ['is_safe' => ['all']]
  43. ),
  44. new TwigFunction(
  45. 'params',
  46. [$this, 'params'],
  47. ['is_safe' => ['all']]
  48. ),
  49. new TwigFunction(
  50. 'admin_url',
  51. 'admin_url',
  52. ['is_safe' => ['all']]
  53. ),
  54. new TwigFunction(
  55. 'get_option',
  56. 'get_option',
  57. ['is_safe' => ['all']]
  58. ),
  59. new TwigFunction(
  60. 'sending_frequency',
  61. [$this, 'getSendingFrequency'],
  62. ['is_safe' => ['all']]
  63. ),
  64. new TwigFunction(
  65. 'wp_date_format',
  66. [$this, 'getWPDateFormat'],
  67. ['is_safe' => ['all']]
  68. ),
  69. new TwigFunction(
  70. 'mailpoet_version',
  71. [$this, 'getMailPoetVersion'],
  72. ['is_safe' => ['all']]
  73. ),
  74. new TwigFunction(
  75. 'mailpoet_premium_version',
  76. [$this, 'getMailPoetPremiumVersion'],
  77. ['is_safe' => ['all']]
  78. ),
  79. new TwigFunction(
  80. 'wp_date_format',
  81. [$this, 'getWPDateFormat'],
  82. ['is_safe' => ['all']]
  83. ),
  84. new TwigFunction(
  85. 'wp_time_format',
  86. [$this, 'getWPTimeFormat'],
  87. ['is_safe' => ['all']]
  88. ),
  89. new TwigFunction(
  90. 'wp_datetime_format',
  91. [$this, 'getWPDateTimeFormat'],
  92. ['is_safe' => ['all']]
  93. ),
  94. new TwigFunction(
  95. 'do_action',
  96. 'do_action',
  97. ['is_safe' => ['all']]
  98. ),
  99. new TwigFunction(
  100. 'is_rtl',
  101. [$this, 'isRtl'],
  102. ['is_safe' => ['all']]
  103. ),
  104. new TwigFunction(
  105. 'number_format_i18n',
  106. 'number_format_i18n',
  107. ['is_safe' => ['all']]
  108. ),
  109. new TwigFunction(
  110. 'mailpoet_locale',
  111. [$this, 'getTwoLettersLocale'],
  112. ['is_safe' => ['all']]
  113. ),
  114. new TwigFunction(
  115. 'mailpoet_free_domains',
  116. [$this, 'getFreeDomains'],
  117. ['is_safe' => ['all']]
  118. ),
  119. new TwigFunction(
  120. 'is_woocommerce_active',
  121. [$this, 'isWoocommerceActive'],
  122. ['is_safe' => ['all']]
  123. ),
  124. new TwigFunction(
  125. 'wp_start_of_week',
  126. [$this, 'getWPStartOfWeek'],
  127. ['is_safe' => ['all']]
  128. ),
  129. new TwigFunction(
  130. 'opened_stats_color',
  131. [$this, 'openedStatsColor'],
  132. ['is_safe' => ['all']]
  133. ),
  134. new TwigFunction(
  135. 'clicked_stats_color',
  136. [$this, 'clickedStatsColor'],
  137. ['is_safe' => ['all']]
  138. ),
  139. new TwigFunction(
  140. 'opened_stats_text',
  141. [$this, 'openedStatsText'],
  142. ['is_safe' => ['all']]
  143. ),
  144. new TwigFunction(
  145. 'clicked_stats_text',
  146. [$this, 'clickedStatsText'],
  147. ['is_safe' => ['all']]
  148. ),
  149. new TwigFunction(
  150. 'add_referral_id',
  151. [$this, 'addReferralId'],
  152. ['is_safe' => ['all']]
  153. ),
  154. new TwigFunction(
  155. 'is_loading_3rd_party_enabled',
  156. [$this, 'libs3rdPartyEnabled'],
  157. ['is_safe' => ['all']]
  158. ),
  159. ];
  160. }
  161. public function getSendingFrequency() {
  162. $args = func_get_args();
  163. $value = (int)array_shift($args);
  164. $label = null;
  165. $labels = [
  166. 'minute' => $this->wp->__('every minute', 'mailpoet'),
  167. 'minutes' => $this->wp->__('every %1$d minutes', 'mailpoet'),
  168. 'hour' => $this->wp->__('every hour', 'mailpoet'),
  169. 'hours' => $this->wp->__('every %1$d hours', 'mailpoet'),
  170. ];
  171. if ($value >= 60) {
  172. // we're dealing with hours
  173. if ($value === 60) {
  174. $label = $labels['hour'];
  175. } else {
  176. $label = $labels['hours'];
  177. }
  178. $value /= 60;
  179. } else {
  180. // we're dealing with minutes
  181. if ($value === 1) {
  182. $label = $labels['minute'];
  183. } else {
  184. $label = $labels['minutes'];
  185. }
  186. }
  187. if ($label !== null) {
  188. return sprintf($label, $value);
  189. } else {
  190. return $value;
  191. }
  192. }
  193. public function getWPDateFormat() {
  194. return $this->wp->getOption('date_format') ?: 'F j, Y';
  195. }
  196. public function getWPStartOfWeek() {
  197. return $this->wp->getOption('start_of_week') ?: 0;
  198. }
  199. public function getMailPoetVersion() {
  200. return MAILPOET_VERSION;
  201. }
  202. public function getMailPoetPremiumVersion() {
  203. return (defined('MAILPOET_PREMIUM_VERSION')) ? MAILPOET_PREMIUM_VERSION : false;
  204. }
  205. public function getWPTimeFormat() {
  206. return $this->wp->getOption('time_format') ?: 'g:i a';
  207. }
  208. public function getWPDateTimeFormat() {
  209. return sprintf('%s %s', $this->getWPDateFormat(), $this->getWPTimeFormat());
  210. }
  211. public function params($key = null) {
  212. $args = $this->wp->stripslashesDeep($_GET);
  213. if (array_key_exists($key, $args)) {
  214. return $args[$key];
  215. }
  216. return null;
  217. }
  218. public function installedInLastTwoWeeks() {
  219. $maxNumberOfWeeks = 2;
  220. $installedAt = Carbon::createFromFormat('Y-m-d H:i:s', $this->settings->get('installed_at'));
  221. if ($installedAt === false) {
  222. return false;
  223. }
  224. return $installedAt->diffInWeeks(Carbon::now()) < $maxNumberOfWeeks;
  225. }
  226. public function isRtl() {
  227. return $this->wp->isRtl();
  228. }
  229. public function getTwoLettersLocale() {
  230. return explode('_', $this->wp->getLocale())[0];
  231. }
  232. public function getFreeDomains() {
  233. return FreeDomains::FREE_DOMAINS;
  234. }
  235. public function isWoocommerceActive() {
  236. return $this->woocommerceHelper->isWooCommerceActive();
  237. }
  238. public function openedStatsColor($opened) {
  239. if ($opened > 30) {
  240. return '#2993ab';
  241. } elseif ($opened > 10) {
  242. return '#f0b849';
  243. } else {
  244. return '#d54e21';
  245. }
  246. }
  247. public function clickedStatsColor($clicked) {
  248. if ($clicked > 3) {
  249. return '#2993ab';
  250. } elseif ($clicked > 1) {
  251. return '#f0b849';
  252. } else {
  253. return '#d54e21';
  254. }
  255. }
  256. public function openedStatsText($opened) {
  257. if ($opened > 30) {
  258. return __('EXCELLENT', 'mailpoet');
  259. } elseif ($opened > 10) {
  260. return __('GOOD', 'mailpoet');
  261. } else {
  262. return __('BAD', 'mailpoet');
  263. }
  264. }
  265. public function clickedStatsText($clicked) {
  266. if ($clicked > 3) {
  267. return __('EXCELLENT', 'mailpoet');
  268. } elseif ($clicked > 1) {
  269. return __('GOOD', 'mailpoet');
  270. } else {
  271. return __('BAD', 'mailpoet');
  272. }
  273. }
  274. public function addReferralId($url) {
  275. return $this->referralUrlDecorator->decorate($url);
  276. }
  277. public function libs3rdPartyEnabled(): bool {
  278. return $this->settings->get('3rd_party_libs.enabled') === '1';
  279. }
  280. }