Nav apraksta

SettingsController.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace MailPoet\Settings;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Cron\CronTrigger;
  5. use MailPoet\DI\ContainerWrapper;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. class SettingsController {
  8. const DEFAULT_SENDING_METHOD_GROUP = 'website';
  9. const DEFAULT_SENDING_METHOD = 'PHPMail';
  10. const DEFAULT_SENDING_FREQUENCY_EMAILS = 25;
  11. const DEFAULT_SENDING_FREQUENCY_INTERVAL = 5; // in minutes
  12. const DEFAULT_DEACTIVATE_SUBSCRIBER_AFTER_INACTIVE_DAYS = 180;
  13. private $loaded = false;
  14. private $settings = [];
  15. private $defaults = null;
  16. /** @var SettingsRepository */
  17. private $settingsRepository;
  18. private static $instance;
  19. public function __construct(
  20. SettingsRepository $settingsRepository
  21. ) {
  22. $this->settingsRepository = $settingsRepository;
  23. }
  24. public function get($key, $default = null) {
  25. $this->ensureLoaded();
  26. $keyParts = explode('.', $key);
  27. $setting = $this->settings;
  28. if ($default === null) {
  29. $default = $this->getDefaultValue($keyParts);
  30. }
  31. foreach ($keyParts as $keyPart) {
  32. if (is_array($setting) && array_key_exists($keyPart, $setting)) {
  33. $setting = $setting[$keyPart];
  34. } else {
  35. return $default;
  36. }
  37. }
  38. if (is_array($setting) && is_array($default)) {
  39. return array_replace_recursive($default, $setting);
  40. }
  41. return $setting;
  42. }
  43. public function getAllDefaults() {
  44. if ($this->defaults === null) {
  45. $this->defaults = [
  46. 'mta_group' => self::DEFAULT_SENDING_METHOD_GROUP,
  47. 'mta' => [
  48. 'method' => self::DEFAULT_SENDING_METHOD,
  49. 'frequency' => [
  50. 'emails' => self::DEFAULT_SENDING_FREQUENCY_EMAILS,
  51. 'interval' => self::DEFAULT_SENDING_FREQUENCY_INTERVAL,
  52. ],
  53. ],
  54. CronTrigger::SETTING_NAME => [
  55. 'method' => CronTrigger::DEFAULT_METHOD,
  56. ],
  57. 'signup_confirmation' => [
  58. 'enabled' => true,
  59. 'subject' => sprintf(__('Confirm your subscription to %1$s', 'mailpoet'), WPFunctions::get()->getOption('blogname')),
  60. 'body' => WPFunctions::get()->__("Hello,\n\nWelcome to our newsletter!\n\nPlease confirm your subscription to our list by clicking the link below: \n\n[activation_link]I confirm my subscription![/activation_link]\n\nThank you,\n\nThe Team", 'mailpoet'),
  61. ],
  62. 'tracking' => [
  63. 'enabled' => true,
  64. ],
  65. 'analytics' => [
  66. 'enabled' => false,
  67. ],
  68. 'display_nps_poll' => true,
  69. 'deactivate_subscriber_after_inactive_days' => self::DEFAULT_DEACTIVATE_SUBSCRIBER_AFTER_INACTIVE_DAYS,
  70. ];
  71. }
  72. return $this->defaults;
  73. }
  74. /**
  75. * Fetches the value from DB and update in cache
  76. * This is required for sync settings between parallel processes e.g. cron
  77. */
  78. public function fetch($key, $default = null) {
  79. $keys = explode('.', $key);
  80. $mainKey = $keys[0];
  81. $this->settings[$mainKey] = $this->fetchValue($mainKey);
  82. return $this->get($key, $default);
  83. }
  84. public function getAll() {
  85. $this->ensureLoaded();
  86. return array_replace_recursive($this->getAllDefaults(), $this->settings);
  87. }
  88. public function set($key, $value) {
  89. $this->ensureLoaded();
  90. $keyParts = explode('.', $key);
  91. $mainKey = $keyParts[0];
  92. $lastKey = array_pop($keyParts);
  93. $setting =& $this->settings;
  94. foreach ($keyParts as $keyPart) {
  95. $setting =& $setting[$keyPart];
  96. if (!is_array($setting)) {
  97. $setting = [];
  98. }
  99. }
  100. $setting[$lastKey] = $value;
  101. $this->settingsRepository->createOrUpdateByName($mainKey, $this->settings[$mainKey]);
  102. }
  103. public function delete($key) {
  104. $setting = $this->settingsRepository->findOneByName($key);
  105. if ($setting) {
  106. $this->settingsRepository->remove($setting);
  107. $this->settingsRepository->flush();
  108. }
  109. unset($this->settings[$key]);
  110. }
  111. private function ensureLoaded() {
  112. if ($this->loaded) {
  113. return;
  114. }
  115. $this->settings = [];
  116. foreach ($this->settingsRepository->findAll() as $setting) {
  117. $this->settings[$setting->getName()] = $setting->getValue();
  118. }
  119. $this->loaded = true;
  120. }
  121. private function getDefaultValue($keys) {
  122. $default = $this->getAllDefaults();
  123. foreach ($keys as $key) {
  124. if (array_key_exists($key, $default)) {
  125. $default = $default[$key];
  126. } else {
  127. return null;
  128. }
  129. }
  130. return $default;
  131. }
  132. private function fetchValue($key) {
  133. $setting = $this->settingsRepository->findOneByName($key);
  134. return $setting ? $setting->getValue() : null;
  135. }
  136. public function resetCache() {
  137. $this->settings = [];
  138. $this->loaded = false;
  139. }
  140. public static function setInstance($instance) {
  141. self::$instance = $instance;
  142. }
  143. /** @return SettingsController */
  144. public static function getInstance() {
  145. if (isset(self::$instance)) return self::$instance;
  146. return ContainerWrapper::getInstance()->get(SettingsController::class);
  147. }
  148. }