PluginProbe
MaxButtons – Create buttons / 7.13.1
MaxButtons – Create buttons v7.13.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.13.1, at classes/block.php

244 lines 6.7 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 $this->fields = apply_filters($this->blockname. "-block-fields",$this->fields);
25 $this->data[$this->blockname] = array(); //empty init
26 }
27
28 /** Save fields runs the POST variable through all blocks
29 *
30 * Taking the post variable from the form, the function will attach the submitted data to the block - field logic and
31 * return a data object to save to the Database. If no value is submitted, the default will be loaded.
32 *
33 * @param $data Array Data in blockname - field logic format
34 * @param $post Array $_POST style data
35 *
36 * @return $data Array
37 */
38 public function save_fields($data, $post)
39 {
40 $block = isset($this->data[$this->blockname]) ? $this->data[$this->blockname] : array();
41
42 foreach($this->fields as $field => $options)
43 {
44 $default = (isset($options["default"])) ? $options["default"] : '';
45 if (is_string($default) && strpos($default,"px") !== false)
46 $block[$field] = (isset($post[$field]) ) ? intval($post[$field]) : $default;
47 elseif( isset($post[$field]) && is_array($post[$field]))
48 {
49 $block[$field] = $post[$field];
50 }
51 else
52 $block[$field] = (isset($post[$field])) ? sanitize_text_field($post[$field]) : $default;
53 }
54
55 $data[$this->blockname] = $block;
56 return $data;
57
58 }
59
60 /** Return fields of current block
61 *
62 * Will return fields of current block only
63 * @return Array $fields
64 */
65 public function get_fields()
66 {
67 return $this->fields;
68 }
69
70 /** Returns Blockname of current block
71 *
72 *
73 * @return $string | boolean Name of the block, if set, otherwise false
74 */
75 public function get_name()
76 {
77 if (isset($this->blockname))
78 return $this->blockname;
79
80 return false;
81 }
82
83
84 /* Build the Block admin interface
85 *
86 * Builds admin interface via Admin class ( addfield ). After building, display fields should be called.
87 * @abstract
88 */
89 abstract public function admin_fields();
90 public function display_fields()
91 {
92 $admin = MB()->getClass('admin');
93 $admin->display_fields();
94
95 }
96
97 /** Adding a sidebar **
98 *
99 * This function can be used to add a sidebar to the block. The sidebar should be called before display_fields is put out but after the last field
100 *
101 * */
102 public function sidebar()
103 {
104 return;
105 }
106
107 /** Parse HTML portion of button
108 *
109 * This filter is passed through to modify the HTML parts of the button.
110 *
111 * Note: When changing parts of the DomObj writing new tags / DOM to elements, it's needed to regenerate the Object.
112 *
113 * @param $button DomObj SimpleDOMObject
114 * @param $mode String[normal|preview] Flag to check if loading in preview
115 *
116 * @return DomObj
117 */
118 public function parse_button($button, $mode) { return $button; }
119 public function post_parse_button($domObj, $mode) { return $domObj; }
120
121 /* Parse CSS of the button
122 *
123 * This function will go through the blocks, matching the $css definitions of a fields and putting them in the
124 * correct tags ( partly using csspart ) .
125 *
126 * @param $css Array [normal|hover|other][cssline] = css declaration
127 * @param $mode String [normal|preview]
128 *
129 * @return $css Array
130 */
131 public function parse_css($css, $mode = 'normal') {
132
133 $data = $this->data[$this->blockname];
134
135 // get all fields from this block
136 foreach($this->fields as $field => $field_data)
137 {
138 // get cssparts, can be comma-seperated value
139 $csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton');
140 $csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal';
141
142 // if this field has a css property
143 if (isset($field_data["css"]))
144 {
145 // get the property value from the data
146 $value = isset($data[$field]) ? $data[$field] : '';
147 $value = str_replace(array(";"), '', $value); //sanitize
148
149 if (isset($field_data["default"]) && strpos($field_data["default"],"px") && ! strpos($value,"px"))
150 {
151 if ($value == '') $value = 0; // pixel values, no empty but 0
152 $value .= "px";
153 }
154 elseif (isset($field_data["default"]) && strpos($field_data["default"],"%") && ! strpos($value,"%"))
155 {
156 if ($value == '') $value = 0; // pixel values, no empty but 0
157 $value .= "%";
158 }
159 if (isset($data[$field]))
160 {
161 foreach($csspart as $part)
162 {
163 if (is_array($csspseudo))
164 {
165 foreach($csspseudo as $pseudo)
166 $css[$part][$pseudo][$field_data["css"]] = $value ;
167 }
168 else
169 $css[$part][$csspseudo][$field_data["css"]] = $value ;
170 }
171 }
172 }
173
174 }
175
176 return $css;
177 }
178
179 /* Ability to output custom JS for each button */
180 public function parse_js($js, $mode = 'normal')
181 {
182 return $js;
183 }
184
185
186 /** Map the Block fields
187 *
188 * This function will take the field name and link it to the defined CSS definition to use in providing the live preview in the
189 * button editor. I.e. a field with name x will be linked to CSS-property X . Or to a custom Javascript function.
190 *
191 * @param $map Array [$field_id][css|attr|func|] = property/function
192 *
193 * @return Array
194 */
195 public function map_fields($map)
196 {
197 foreach($this->fields as $field => $field_data)
198 {
199 if (isset($field_data["css"]))
200 {
201 $cssdef = $field_data["css"];
202 $multidef = explode('-',$cssdef);
203 if ( count($multidef) > 1)
204 {
205 $cssdef = "";
206 for($i = 0; $i < count($multidef); $i++)
207 {
208 if ($i == 0)
209 $cssdef .= $multidef[$i];
210 else
211 $cssdef .= ucfirst($multidef[$i]);
212 //$multidef[$i] . ucfirst($multidef[1]);
213 }
214 }
215 $map[$field]["css"] = $cssdef;
216 if ( isset($field_data["default"]) && strpos($field_data["default"],"px") != false )
217 $map[$field]["css_unit"] = 'px';
218 else if ( isset($field_data["default"]) && strpos($field_data["default"],"%") != false )
219 $map[$field]["css_unit"] = '%';
220
221 }
222 if (isset($field_data["csspart"]))
223 $map[$field]["csspart"] = $field_data["csspart"];
224 if (isset($field_data['csspseudo']))
225 $map[$field]['pseudo'] = $field_data['csspseudo'];
226
227 }
228 return $map;
229
230 }
231
232 /** Sets the data
233 *
234 * This action is called from button class when data is pulled from the database and populates the dataArray to all blocks
235 *
236 */
237 function set($dataArray)
238 {
239
240 $this->data = $dataArray;
241 }
242
243 }
244