Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added ToppaSettings interface and abstract class, for facilitating ma…

…nagement of plugin settings
  • Loading branch information...
commit 07cb7652503e8d959586a2c9a17d55a152d02da5 1 parent ac1e00b
@toppa authored
Showing with 74 additions and 0 deletions.
  1. +8 −0 ToppaSettings.php
  2. +66 −0 ToppaSettingsWp.php
View
8 ToppaSettings.php
@@ -0,0 +1,8 @@
+<?php
+
+interface ToppaSettings {
+ public function __get($name);
+ public function refresh();
+ public function set(array $newSettings, $preferExisting = false);
+ public function delete();
+}
View
66 ToppaSettingsWp.php
@@ -0,0 +1,66 @@
+<?php
+
+abstract class ToppaSettingsWp implements ToppaSettings {
+ private $functionsFacade;
+ private $name;
+ private $data = array();
+
+ public function __construct($name, ToppaFunctionsFacade $functionsFacade) {
+ $this->name = $name;
+ $this->functionsFacade = $functionsFacade;
+ }
+
+ public function __get($name) {
+ if (array_key_exists($name, $this->data)) {
+ return $this->data[$name];
+ }
+
+ else {
+ $this->refresh();
+ }
+
+ // and try again...
+ if (array_key_exists($name, $this->data)) {
+ return $this->data[$name];
+ }
+
+ throw New Exception(__('Invalid data property __get for ', 'toppalibs') . htmlentities($name));
+ }
+
+ public function refresh() {
+ $oldSettings = $this->functionsFacade->getSetting($this->name);
+
+ if (is_array($oldSettings)) {
+ $this->data = $oldSettings;
+ }
+
+ return $this->data;
+ }
+
+ public function set(array $newSettings, $preferExisting = false) {
+ $this->refresh();
+
+ if ($preferExisting) {
+ $this->data = array_merge($newSettings, $this->data);
+ }
+
+ else {
+ $this->data = array_merge($this->data, $newSettings);
+ }
+
+ $this->functionsFacade->setSetting($this->name, $this->data);
+ return $this->data;
+ }
+
+ public function delete() {
+ $this->functionsFacade->deleteSetting($this->name);
+
+ if ($this->functionsFacade->getSetting($this->name)) {
+ throw new Exception(__('Failed to delete settings', 'toppalibs'));
+ }
+
+ return true;
+ }
+}
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.