PluginProbe
Social Share Buttons / 1.19
Social Share Buttons v1.19
trunk 0.9 1.0 1.1 1.1.1 1.1.2 1.10 1.11 1.12 1.15 1.16 1.17 1.18 1.19 1.2 1.20 1.3 1.30 1.4 1.5 1.6 1.7 1.8 1.9
share-button / classes / class-block.php

class-block.php in Social Share Buttons 1.19, at classes/class-block.php

278 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MBSocial;
3 defined('ABSPATH') or die('No direct access permitted');
4
5 abstract class block
6 {
7 protected $data = array();
8 protected $collection = null;
9 protected $fields = array();
10 protected $fielddata = array();
11 protected $blockname;
12 protected $is_preview = false;
13
14 //protected static $eventInit = false;
15 protected $network;
16
17
18 public function __construct()
19 {
20 $w = MBSocial()->whistle();
21 $w->listen('display/parse/network', array($this, 'set_network'), 'tell');
22 }
23
24 public function set_network($network)
25 {
26 $this->network = $network;
27 }
28 function setCollection($collection)
29 {
30 $this->collection = $collection;
31 }
32
33
34 public function set($data)
35 {
36 $this->data = $data;
37 if (! isset($this->data[$this->blockname]) )
38 $this->data[$this->blockname] = array();
39
40 return true;
41
42 }
43
44 // get the block data
45 public function get()
46 {
47 return $this->data;
48
49 }
50
51
52 public function get_name()
53 {
54 return $this->blockname;
55 }
56
57 public function get_fields()
58 {
59 return $this->fields;
60 }
61
62 public function getValue($field)
63 {
64 $data = $this->data;
65
66 if (isset($data[$this->blockname][$field]))
67 {
68 return $data[$this->blockname][$field];
69 }
70
71 if (isset($this->fields[$field]['default']))
72 return $this->fields[$field]['default'];
73
74
75 // if not found return default
76 return false;
77 }
78
79 public function getColorValue($fieldname)
80 {
81 $value = self::getValue($fieldname);
82 if ($this->isrgba($value))
83 return $value;
84
85 if (! $value )
86 return false;
87 if (substr($value,0,1) !== '#')
88 {
89 $value = '#' . $value;
90 }
91 return $value;
92 }
93
94 private function isrgba($value)
95 {
96 if (strpos($value, 'rgb') >= 0 )
97 return true;
98 else {
99 return false;
100 }
101 }
102
103
104
105 public function setPreview($preview = true)
106 {
107 $this->is_preview = $preview;
108
109 }
110
111 /* Save fields on a per block data
112 *
113 * Post data is sent unfiltered so sanitization must be done here!
114 * @param $data Array with all collection block data
115 * @param $post Array|null The post array or null. Null in case of loading defaults, not a post.
116 */
117 public function save_fields($data, $post)
118 {
119 $blockdata = array();
120
121 //if (isset($this->data[$this->blockname]))
122 // $blockdata = $this->data;
123 //$blockdata = $data;
124
125 foreach($this->fields as $field => $options)
126 {
127
128 if ($field == "multifields") // standardize multi fields
129 {
130 $mf_id = $options["id"];
131 $mf_fields = $options["fields"];
132
133 $multidata = array();
134
135 if (isset($post[$mf_id])) // the collection with the id's.
136 {
137 $i = 0; // id's might be duplicate i.e. two similar buttons.
138 foreach ($post[$mf_id] as $id) // id is button-id
139 {
140
141 foreach($mf_fields as $mf_field => $options)
142 {
143 $default = (isset($options["default"])) ? $options["default"] : '';
144 // POST[ field_name - $id ]
145 //
146 $multidata[$i][$id][$mf_field] = isset($post[$mf_field . "-" . $id . "-" . $i ]) ? $post[$mf_field . "-" . $id . "-" . $i] : $default;
147 }
148 $i++;
149 }
150 }
151 $blockdata[$mf_id] = $multidata;
152 }
153 else
154 {
155 $default = (isset($options["default"])) ? $options["default"] : '';
156 // stripslashes since the WP post var adds them.
157
158 $blockdata[$field] = (isset($post[$field])) ? stripslashes(sanitize_text_field($post[$field])) : $default;
159
160 }
161 }
162
163 $data[$this->blockname] = $blockdata;
164 return $data;
165
166 }
167
168 public function parse($domObj, $args = array() )
169 {
170
171 return $domObj; // nothing to do by default
172 }
173
174 public function parseButtons($buttons)
175 {
176 return $buttons;
177 }
178
179 // Adepted from block class - maxbuttons
180 public function parseCSS($css, $args)
181 {
182 $data = $this->data[$this->blockname];
183
184 // get all fields from this block
185 foreach($this->fields as $field => $field_data)
186 {
187 // get cssparts, can be comma-seperated value
188 $csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton');
189 $csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal';
190
191 // if this field has a css property
192 if (isset($field_data["css"]))
193 {
194
195
196 // get the property value from the data
197 $value = isset($data[$field]) ? $data[$field] : $field_data['default'];
198 $value = str_replace(array(";"), '', $value); //sanitize
199
200 if ( strpos($field_data["default"],"px") && ! strpos($value,"px"))
201 {
202 if ($value == '') $value = 0; // pixel values, no empty but 0
203 $value .= "px";
204 }
205
206 if (isset($data[$field]))
207 {
208
209 foreach($csspart as $part)
210 {
211 if (is_array($csspseudo))
212 {
213 foreach($csspseudo as $pseudo)
214 $css[$part][$pseudo][$field_data["css"]] = $value ;
215 }
216 else
217 $css[$part][$csspseudo][$field_data["css"]] = $value ;
218 }
219 }
220 }
221
222 }
223
224 return $css;
225 }
226
227
228 // default function
229 public function parseJS($js)
230 {
231 return $js;
232 }
233
234 /** Get the post metadata of a certain block
235 * Used in post specific settings
236 * @param $post_id int Post id
237 * @return Array | Boolean . Array of setting data, or false if not metadata present
238 */
239 public function get_block_meta_data($post_id)
240 {
241 $meta = $this->collection->get_metadata($post_id);
242
243 if ( isset($meta[$this->blockname]))
244 return $meta[$this->blockname];
245 else
246 return false;
247
248 }
249
250 public function save_meta_boxes($metadata, $post_id, $post)
251 {
252 if (! isset($this->meta_fields))
253 return $metadata;
254
255 $blockmeta = array();
256
257 foreach($this->meta_fields as $field => $options)
258 {
259 $default = (isset($options["default"])) ? $options["default"] : '';
260 // stripslashes since the WP post var adds them.
261 $blockmeta[$field] = (isset($post[$field])) ? stripslashes(sanitize_text_field($post[$field])) : $default;
262 }
263
264 $metadata[$this->blockname] = $blockmeta;
265
266 return $metadata;
267 }
268
269 public function do_meta_boxes($array, $post)
270 {
271 return $array;
272 }
273
274
275 abstract function admin();
276
277 } // class
278