10-settings.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace cx_newsletter;
  3. use \get_option;
  4. use \update_option;
  5. use \delete_option;
  6. use \exception;
  7. abstract class settings
  8. implements settings_interface {
  9. public function __construct() {
  10. $this->is_initializated();
  11. }
  12. public abstract function get_plugin_version() : int;
  13. protected abstract function get_options() : array;
  14. public function get(string $name) : string {
  15. if (!$this->exists($name)) {
  16. throw $this->not_exists($name);
  17. }
  18. return get_option($this->esc_name($name), $this->get_default($name));
  19. }
  20. public function save(string $name, string $content) : self {
  21. if (!$this->exists($name)) {
  22. throw $this->not_exists($name);
  23. }
  24. update_option($this->esc_name($name), $content);
  25. return $this;
  26. }
  27. public function init() : void {
  28. foreach ($this->get_options_names() as $name) {
  29. $this->save($name, $this->get_default($name));
  30. }
  31. $this->set_initialization(true);
  32. }
  33. public function clean() : void {
  34. foreach ($this->get_options_names() as $name) {
  35. $this->drop($name);
  36. }
  37. $this->set_initialization(false);
  38. }
  39. private function drop(string $name) : void {
  40. delete_option($this->esc_name($name));
  41. }
  42. public function get_default(string $name) : string {
  43. if (!$this->exists($name)) {
  44. throw $this->not_exists($name);
  45. }
  46. return $this->get_options()[$name];
  47. }
  48. private function get_options_names() : array {
  49. return array_keys($this->get_options());
  50. }
  51. private function exists(string $name) : bool {
  52. return array_key_exists($name, $this->get_options());
  53. }
  54. private function not_exists(string $name) : \exception {
  55. return new exception('Setting with name '.$name.' not exists.');
  56. }
  57. private function esc_name(string $name) : string {
  58. return 'cx_newsletter_option_'.$name;
  59. }
  60. public function is_initializated() : bool {
  61. if ($this->initialization !== null) {
  62. return $this->initialization;
  63. }
  64. $state = get_option($this->initialization_name(), null);
  65. $this->initialization = ($state === 'yes');
  66. return $this->is_initializated();
  67. }
  68. private function set_initialization(bool $state) : void {
  69. update_option($this->initialization_name(), 'yes');
  70. $this->initialization = true;
  71. if (!$state) {
  72. delete_option($this->initialization_name());
  73. $this->initialization = false;
  74. }
  75. }
  76. private function initialization_name() : string {
  77. return 'cx_newsletter_options_initialization';
  78. }
  79. protected array $options;
  80. protected ?bool $initialization = null;
  81. }