PluginProbe
MaxButtons – Create buttons / 7.1
MaxButtons – Create buttons v7.1
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
maxbuttons / classes / block.php

block.php in MaxButtons – Create buttons 7.1, at classes/block.php

254 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MaxButtons;
3 defined('ABSPATH') or die('No direct access permitted');
4
5 /** A block is a combination of related settings.
6 *
7 * Blocks are grouped and put into the same Database table row. This way related data, it's executing, display and other decision
8 * making is seperate from other blocks improving realiability and readability of the code.
9 */
10 use MaxButtons\maxBlocks as maxBlocks;
11 use MaxButtons\maxField as maxField;
12
13 abstract class maxBlock
14 {
15 protected $data = array();
16
17 /** Block constructor
18 *
19 * Constructor for a button block. Hooks up all needed filters and inits data for a block.
20 *
21 */
22 function __construct($priority = 10)
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);
27 //add_action('mb-admin-fields', array($this,'admin_fields' ) );
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
38 $this->fields = apply_filters($this->blockname. "-block-fields",$this->fields);
39 $this->data[$this->blockname] = array(); //empty init
40
41 }
42
43 /** Save fields runs the POST variable through all blocks
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 *
48 * @param $data Array Data in blockname - field logic format
49 * @param $post Array $_POST style data
50 *
51 * @return $data Array
52 */
53 public function save_fields($data, $post)
54 {
55 $block = isset($this->data[$this->blockname]) ? $this->data[$this->blockname] : array();
56
57 foreach($this->fields as $field => $options)
58 {
59 $default = (isset($options["default"])) ? $options["default"] : '';
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]))
63 {
64 $block[$field] = $post[$field];
65 }
66 else
67 $block[$field] = (isset($post[$field])) ? sanitize_text_field($post[$field]) : $default;
68 }
69
70 $data[$this->blockname] = $block;
71 return $data;
72
73 }
74
75 /** Return fields of current block
76 *
77 * Will return fields of current block only
78 * @return Array $fields
79 */
80 public function get_fields()
81 {
82 return $this->fields;
83 }
84
85 /** Returns Blockname of current block
86 *
87 *
88 * @return $string | boolean Name of the block, if set, otherwise false
89 */
90 public function get_name()
91 {
92 if (isset($this->blockname))
93 return $this->blockname;
94
95 return false;
96 }
97
98
99 /* Display Block admin interface
100 *
101 * Writes admin interface to output.
102 * @abstract
103 */
104 abstract public function admin_fields();
105
106 /** Parse HTML portion of button
107 *
108 * This filter is passed through to modify the HTML parts of the button.
109 *
110 * Note: When changing parts of the DomObj writing new tags / DOM to elements, it's needed to regenerate the Object.
111 *
112 * @param $button DomObj SimpleDOMObject
113 * @param $mode String[normal|preview] Flag to check if loading in preview
114 *
115 * @return DomObj
116 */
117 public function parse_button($button, $mode) { return $button; }
118
119 /* Parse CSS of the button
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 *
124 * @param $css Array [normal|hover|other][cssline] = css declaration
125 * @param $mode String [normal|preview]
126 *
127 * @return $css Array
128 */
129 public function parse_css($css, $mode = 'normal') {
130
131 $data = $this->data[$this->blockname];
132
133 // get all fields from this block
134 foreach($this->fields as $field => $field_data)
135 {
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
140 // if this field has a css property
141 if (isset($field_data["css"]))
142 {
143 // get the property value from the data
144 $value = isset($data[$field]) ? $data[$field] : '';
145 $value = str_replace(array(";"), '', $value); //sanitize
146
147 if (isset($field_data["default"]) && strpos($field_data["default"],"px") && ! strpos($value,"px"))
148 {
149 if ($value == '') $value = 0; // pixel values, no empty but 0
150 $value .= "px";
151 }
152 if (isset($data[$field]))
153 {
154 foreach($csspart as $part)
155 {
156 if (is_array($csspseudo))
157 {
158 foreach($csspseudo as $pseudo)
159 $css[$part][$pseudo][$field_data["css"]] = $value ;
160 }
161 else
162 $css[$part][$csspseudo][$field_data["css"]] = $value ;
163 }
164 }
165 }
166
167 }
168
169 return $css;
170 }
171
172 /* Ability to output custom JS for each button */
173 public function parse_js($js, $mode = 'normal')
174 {
175 return $js;
176 }
177
178
179 /** Map the Block fields
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 *
184 * @param $map Array [$field_id][css|attr|func|] = property/function
185 *
186 * @return Array
187 */
188 public function map_fields($map)
189 {
190 foreach($this->fields as $field => $field_data)
191 {
192 if (isset($field_data["css"]))
193 {
194 $cssdef = $field_data["css"];
195 $multidef = explode('-',$cssdef);
196 if ( count($multidef) > 1)
197 {
198 $cssdef = "";
199 for($i = 0; $i < count($multidef); $i++)
200 {
201 if ($i == 0)
202 $cssdef .= $multidef[$i];
203 else
204 $cssdef .= ucfirst($multidef[$i]);
205 //$multidef[$i] . ucfirst($multidef[1]);
206 }
207 }
208 $map[$field]["css"] = $cssdef;
209 if ( isset($field_data["default"]) && strpos($field_data["default"],"px") != false )
210 $map[$field]["css_unit"] = 'px';
211
212 }
213 if (isset($field_data["csspart"]))
214 $map[$field]["csspart"] = $field_data["csspart"];
215 }
216 return $map;
217
218 }
219
220 /** Sets the data
221 *
222 * This action is called from button class when data is pulled from the database and populates the dataArray to all blocks
223 *
224 */
225 function set($dataArray)
226 {
227
228 $this->data = $dataArray;
229 }
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
252 }
253 ?>
254