PluginProbe
MaxButtons – Create buttons / 6.28
MaxButtons – Create buttons v6.28
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
← All changes | classes/block.php +131 -110 6.266.28 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 -namespace MaxButtons;
2 +namespace MaxButtons;
3 3 defined('ABSPATH') or die('No direct access permitted');
4 4
5 -/** A block is a combination of related settings.
5 +/** A block is a combination of related settings.
6 6 *
7 7 * Blocks are grouped and put into the same Database table row. This way related data, it's executing, display and other decision
8 8 * making is seperate from other blocks improving realiability and readability of the code.
9 9 */
@@ -8,73 +8,73 @@
8 8 * making is seperate from other blocks improving realiability and readability of the code.
9 9 */
10 10 use MaxButtons\maxBlocks as maxBlocks;
11 11 use MaxButtons\maxField as maxField;
12 -
12 +
13 13 abstract class maxBlock
14 14 {
15 - protected $data = array();
16 -
17 - /** Block constructor
15 + protected $data = array();
16 +
17 + /** Block constructor
18 18 *
19 - * Constructor for a button block. Hooks up all needed filters and inits data for a block.
20 - *
19 + * Constructor for a button block. Hooks up all needed filters and inits data for a block.
20 + *
21 21 */
22 22 function __construct($priority = 10)
23 23 {
24 -
25 - // filters for save_post, display etc. Buttons class will the filters.
26 - //add_filter('mb-save-fields', array($this, 'save_fields'),10,2);
24 +
25 + // filters for save_post, display etc. Buttons class will the filters.
26 + //add_filter('mb-save-fields', array($this, 'save_fields'),10,2);
27 27 //add_action('mb-admin-fields', array($this,'admin_fields' ) );
28 28 //add_action('mb-data-load', array($this,'set') );
29 -
30 - //add_filter('mb-parse-button', array($this, 'parse_button'),10,2 );
31 - //add_filter('mb-js-blocks', array($this, 'parse_js'), 10, 2);
32 - //add_filter('mb-parse-element-preview', array($this,'parse_element'), 10,2);
33 -
34 - //add_filter('mb-css-blocks', array($this, 'parse_css'),10,2 );
35 - //add_filter('mb-field-map', array($this, 'map_fields') );
36 -
37 29
38 - $this->fields = apply_filters($this->blockname. "-block-fields",$this->fields);
30 + //add_filter('mb-parse-button', array($this, 'parse_button'),10,2 );
31 + //add_filter('mb-js-blocks', array($this, 'parse_js'), 10, 2);
32 + //add_filter('mb-parse-element-preview', array($this,'parse_element'), 10,2);
33 +
34 + //add_filter('mb-css-blocks', array($this, 'parse_css'),10,2 );
35 + //add_filter('mb-field-map', array($this, 'map_fields') );
36 +
37 +
38 + $this->fields = apply_filters($this->blockname. "-block-fields",$this->fields);
39 39 $this->data[$this->blockname] = array(); //empty init
40 -
40 +
41 41 }
42 -
42 +
43 43 /** Save fields runs the POST variable through all blocks
44 44 *
45 - * Taking the post variable from the form, the function will attach the submitted data to the block - field logic and
46 - * return a data object to save to the Database. If no value is submitted, the default will be loaded.
47 - *
45 + * Taking the post variable from the form, the function will attach the submitted data to the block - field logic and
46 + * return a data object to save to the Database. If no value is submitted, the default will be loaded.
47 + *
48 48 * @param $data Array Data in blockname - field logic format
49 49 * @param $post Array $_POST style data
50 - *
50 + *
51 51 * @return $data Array
52 52 */
53 53 public function save_fields($data, $post)
54 - {
55 - $block = isset($this->data[$this->blockname]) ? $this->data[$this->blockname] : array();
54 + {
55 + $block = isset($this->data[$this->blockname]) ? $this->data[$this->blockname] : array();
56 56
57 - foreach($this->fields as $field => $options)
57 + foreach($this->fields as $field => $options)
58 58 {
59 - $default = (isset($options["default"])) ? $options["default"] : '';
59 + $default = (isset($options["default"])) ? $options["default"] : '';
60 60 if (is_string($default) && strpos($default,"px") !== false)
61 - $block[$field] = (isset($post[$field]) ) ? intval($post[$field]) : $default;
62 - elseif( isset($post[$field]) && is_array($post[$field]))
61 + $block[$field] = (isset($post[$field]) ) ? intval($post[$field]) : $default;
62 + elseif( isset($post[$field]) && is_array($post[$field]))
63 63 {
64 64 $block[$field] = $post[$field];
65 65 }
66 - else
67 - $block[$field] = (isset($post[$field])) ? sanitize_text_field($post[$field]) : $default;
66 + else
67 + $block[$field] = (isset($post[$field])) ? sanitize_text_field($post[$field]) : $default;
68 68 }
69 69
70 - $data[$this->blockname] = $block;
71 - return $data;
72 -
70 + $data[$this->blockname] = $block;
71 + return $data;
72 +
73 73 }
74 -
74 +
75 75 /** Return fields of current block
76 - *
76 + *
77 77 * Will return fields of current block only
78 78 * @return Array $fields
79 79 */
80 80 public function get_fields()
@@ -80,84 +80,84 @@
80 80 public function get_fields()
81 81 {
82 82 return $this->fields;
83 83 }
84 -
84 +
85 85 /** Returns Blockname of current block
86 - *
87 - *
86 + *
87 + *
88 88 * @return $string | boolean Name of the block, if set, otherwise false
89 89 */
90 - public function get_name()
90 + public function get_name()
91 91 {
92 - if (isset($this->blockname))
93 - return $this->blockname;
94 -
92 + if (isset($this->blockname))
93 + return $this->blockname;
94 +
95 95 return false;
96 96 }
97 -
98 -
97 +
98 +
99 99 /* Display Block admin interface
100 - *
100 + *
101 101 * Writes admin interface to output.
102 102 * @abstract
103 103 */
104 104 abstract public function admin_fields();
105 -
105 +
106 106 /** Parse HTML portion of button
107 107 *
108 - * This filter is passed through to modify the HTML parts of the button.
109 - *
108 + * This filter is passed through to modify the HTML parts of the button.
109 + *
110 110 * Note: When changing parts of the DomObj writing new tags / DOM to elements, it's needed to regenerate the Object.
111 - *
111 + *
112 112 * @param $button DomObj SimpleDOMObject
113 - * @param $mode String[normal|preview] Flag to check if loading in preview
114 - *
113 + * @param $mode String[normal|preview] Flag to check if loading in preview
114 + *
115 115 * @return DomObj
116 116 */
117 - public function parse_button($button, $mode) { return $button; }
118 -
117 + public function parse_button($button, $mode) { return $button; }
118 +
119 119 /* Parse CSS of the button
120 120 *
121 - * This function will go through the blocks, matching the $css definitions of a fields and putting them in the
122 - * correct tags ( partly using csspart ) .
123 - *
121 + * This function will go through the blocks, matching the $css definitions of a fields and putting them in the
122 + * correct tags ( partly using csspart ) .
123 + *
124 124 * @param $css Array [normal|hover|other][cssline] = css declaration
125 - * @param $mode String [normal|preview]
126 - *
127 - * @return $css Array
125 + * @param $mode String [normal|preview]
126 + *
127 + * @return $css Array
128 128 */
129 - public function parse_css($css, $mode = 'normal') {
129 + public function parse_css($css, $mode = 'normal') {
130 130
131 - $data = $this->data[$this->blockname];
131 + $data = $this->data[$this->blockname];
132 132
133 133 // get all fields from this block
134 134 foreach($this->fields as $field => $field_data)
135 135 {
136 136 // get cssparts, can be comma-seperated value
137 - $csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton');
138 - $csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal';
139 -
137 + $csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton');
138 + $csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal';
139 +
140 140 // if this field has a css property
141 - if (isset($field_data["css"]))
141 + if (isset($field_data["css"]))
142 142 {
143 143 // get the property value from the data
144 - $value = isset($data[$field]) ? $data[$field] : '';
144 + $value = isset($data[$field]) ? $data[$field] : '';
145 145 $value = str_replace(array(";"), '', $value); //sanitize
146 -
146 +
147 147 if (isset($field_data["default"]) && strpos($field_data["default"],"px") && ! strpos($value,"px"))
148 148 {
149 - if ($value == '') $value = 0; // pixel values, no empty but 0
150 - $value .= "px";
149 + if ($value == '') $value = 0; // pixel values, no empty but 0
150 + $value .= "px";
151 151 }
152 - if (isset($data[$field]))
152 + if (isset($data[$field]))
153 153 {
154 154 foreach($csspart as $part)
155 155 {
156 - if (is_array($csspseudo))
156 + if (is_array($csspseudo))
157 157 {
158 158 foreach($csspseudo as $pseudo)
159 - $css[$part][$pseudo][$field_data["css"]] = $value ;
159 + $css[$part][$pseudo][$field_data["css"]] = $value ;
160 160 }
161 161 else
162 162 $css[$part][$csspseudo][$field_data["css"]] = $value ;
163 163 }
@@ -162,71 +162,92 @@
162 162 $css[$part][$csspseudo][$field_data["css"]] = $value ;
163 163 }
164 164 }
165 165 }
166 -
167 - }
168 166
169 - return $css;
167 + }
168 +
169 + return $css;
170 170 }
171 -
172 - /* Ability to output custom JS for each button */
171 +
172 + /* Ability to output custom JS for each button */
173 173 public function parse_js($js, $mode = 'normal')
174 174 {
175 - return $js;
175 + return $js;
176 176 }
177 -
178 -
179 - /** Map the Block fields
177 +
178 +
179 + /** Map the Block fields
180 180 *
181 - * This function will take the field name and link it to the defined CSS definition to use in providing the live preview in the
182 - * button editor. I.e. a field with name x will be linked to CSS-property X . Or to a custom Javascript function.
183 - *
181 + * This function will take the field name and link it to the defined CSS definition to use in providing the live preview in the
182 + * button editor. I.e. a field with name x will be linked to CSS-property X . Or to a custom Javascript function.
183 + *
184 184 * @param $map Array [$field_id][css|attr|func|] = property/function
185 - *
185 + *
186 186 * @return Array
187 187 */
188 - public function map_fields($map)
188 + public function map_fields($map)
189 189 {
190 190 foreach($this->fields as $field => $field_data)
191 191 {
192 - if (isset($field_data["css"]))
192 + if (isset($field_data["css"]))
193 193 {
194 - $cssdef = $field_data["css"];
195 - $multidef = explode('-',$cssdef);
194 + $cssdef = $field_data["css"];
195 + $multidef = explode('-',$cssdef);
196 196 if ( count($multidef) > 1)
197 197 {
198 - $cssdef = "";
198 + $cssdef = "";
199 199 for($i = 0; $i < count($multidef); $i++)
200 - {
201 - if ($i == 0)
200 + {
201 + if ($i == 0)
202 202 $cssdef .= $multidef[$i];
203 203 else
204 - $cssdef .= ucfirst($multidef[$i]);
205 - //$multidef[$i] . ucfirst($multidef[1]);
204 + $cssdef .= ucfirst($multidef[$i]);
205 + //$multidef[$i] . ucfirst($multidef[1]);
206 206 }
207 - }
208 - $map[$field]["css"] = $cssdef;
207 + }
208 + $map[$field]["css"] = $cssdef;
209 209 if ( isset($field_data["default"]) && strpos($field_data["default"],"px") != false )
210 - $map[$field]["css_unit"] = 'px';
211 -
210 + $map[$field]["css_unit"] = 'px';
211 +
212 212 }
213 - if (isset($field_data["csspart"]))
214 - $map[$field]["csspart"] = $field_data["csspart"];
213 + if (isset($field_data["csspart"]))
214 + $map[$field]["csspart"] = $field_data["csspart"];
215 215 }
216 - return $map;
217 -
216 + return $map;
217 +
218 218 }
219 -
219 +
220 220 /** Sets the data
221 221 *
222 222 * This action is called from button class when data is pulled from the database and populates the dataArray to all blocks
223 - *
223 + *
224 224 */
225 225 function set($dataArray)
226 226 {
227 -
227 +
228 228 $this->data = $dataArray;
229 229 }
230 -
230 +
231 + public function getValue($field)
232 + {
233 + $data = $this->data;
234 + /*foreach($data as $blockname => $fieldname)
235 + {
236 + if ($fieldname == $field)
237 + return $data[$blockname][$fieldname];
238 + }*/
239 +
240 + if (isset($data[$this->blockname][$field]))
241 + {
242 + return $data[$this->blockname][$field];
243 + }
244 +
245 + if (isset($this->fields[$field]['default']))
246 + return $this->fields[$field]['default'];
247 +
248 + // if not found return default
249 + return false;
250 + }
251 +
231 252 }
232 253 ?>