PluginProbe ʕ •ᴥ•ʔ
WPFront Scroll Top / 2.0.3
WPFront Scroll Top v2.0.3
1.5 1.6 1.6.1 1.6.2 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1 2.1.1 2.2 3.0.0 3.0.1 trunk 1.0 1.0.1 1.1 1.1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5
wpfront-scroll-top / classes / base / class-wpfront-options-base.php
wpfront-scroll-top / classes / base Last commit date
class-wpfront-options-base.php 5 years ago
class-wpfront-options-base.php
159 lines
1 <?php
2
3 /*
4 WPFront Plugins Options Base
5 Copyright (C) 2013, WPFront.com
6 Website: wpfront.com
7 Contact: syam@wpfront.com
8
9 WPFront Plugins are distributed under the GNU General Public License, Version 3,
10 June 2007. Copyright (C) 2007 Free Software Foundation, Inc., 51 Franklin
11 St, Fifth Floor, Boston, MA 02110, USA
12
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 namespace WPFront\Scroll_Top;
26
27 /**
28 * Options framework base class
29 *
30 * @author Syam Mohan <syam@wpfront.com>
31 * @copyright 2013 WPFront.com
32 */
33 class WPFront_Options_Base_ST {
34
35 //variables to hold options data
36 public $__data = array();
37 public $__optionName;
38 public $__localizeSlug;
39 public $__options;
40 private $lastOptionName;
41 private $optionNames;
42
43 function __construct($optionName, $localizeSlug) {
44 $this->__optionName = $optionName;
45 $this->__localizeSlug = $localizeSlug;
46
47 $this->__options = get_option($this->__optionName);
48
49 if (!is_array($this->__options)) {
50 $this->__options = array();
51 }
52
53 $this->optionNames = array();
54 }
55
56 //defines option with data
57 protected function addOption($name, $type, $default, $validate = NULL) {
58 if (isset($this->__data[$name])) {
59 echo 'Duplicate option ' . $name;
60 return $this;
61 }
62 $this->__data[$name]['name'] = $name;
63 $this->__data[$name]['type'] = $type;
64 $this->__data[$name]['default'] = $default;
65 $this->__data[$name]['validate'] = isset($validate) ? $validate : array(&$this, 'validate_default');
66
67 //dynamic function returning option value
68 $this->__data[$name]['func'] = function($self, $data) {
69 $value = NULL;
70 if(array_key_exists($data["name"], $self->__options))
71 $value = $self->__options[$data["name"]];
72 return $self->get_value($data["type"], $value, $data["default"], $data["validate"]);
73 };
74
75 $this->__data[$name . '_name'] = $this->__data[$name];
76 //dynamic function returning option name for settings page
77 $this->__data[$name . '_name']['func'] = function($self, $data) {
78 return $self->__optionName . "[" . $data["name"] . "]";
79 };
80
81 $this->__data[$name . '_label'] = $this->__data[$name];
82 //dynamic function returning option label for settings page
83 $this->__data[$name . '_label']['func'] = function($self, $data) {
84 return __($data["label"], $self->__localizeSlug);
85 };
86
87 $this->__data['set_' . $name] = $this->__data[$name];
88 $this->__data['set_' . $name]['func'] = function($self, $data, $value) {
89 $self->__options[$data["name"]] = $value;
90 };
91
92 $this->lastOptionName = $name;
93 array_push($this->optionNames, $name);
94
95 return $this;
96 }
97
98 //default validation function
99 private function validate_default($arg) {
100 return $arg;
101 }
102
103 //validates a zero or positive number
104 protected function validate_zero_positive($arg) {
105 if ($arg < 0) {
106 return 0;
107 }
108
109 return $arg;
110 }
111
112 //sets the label of the option, for POEDIT compatibility
113 protected function label($label) {
114 $this->__data[$this->lastOptionName . '_label']['label'] = $label;
115 }
116
117 //returns the value of option
118 public function get_value($type, $value, $default, $validate) {
119 if (!isset($value)) {
120 return $default;
121 }
122
123 switch ($type) {
124 case 'bit':
125 case 'bool':
126 return (bool) $value;
127 case 'int':
128 return call_user_func($validate, intval($value));
129 case 'float':
130 return call_user_func($validate, floatval($value));
131 case 'string':
132 return call_user_func($validate, strval($value));
133 }
134 return $value;
135 }
136
137 //returns optons array
138 public function get_options() {
139 $options = array();
140 foreach ($this->optionNames as $val) {
141 $options[$val] = $this->$val();
142 }
143 return $options;
144 }
145
146 //PHP magic function to call dynamic methods
147 public function __call($name, array $args) {
148 if (!array_key_exists($name, $this->__data)) {
149 echo '"' . $name . '" option not yet added';
150 return;
151 }
152
153 array_unshift($args, $this->__data[$name]);
154 array_unshift($args, $this);
155 return call_user_func_array($this->__data[$name]['func'], $args);
156 }
157
158 }
159