PluginProbe
Social Share Buttons / 1.2
Social Share Buttons v1.2
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.2, at classes/class-block.php

266 lines 5.6 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 = $this->getValue($fieldname);
82 if (! $value )
83 return false;
84
85 if (substr($value,0,1) !== '#')
86 {
87 $value = '#' . $value;
88 }
89
90 return $value;
91
92 }
93
94
95 public function setPreview($preview = true)
96 {
97 $this->is_preview = $preview;
98
99 }
100
101 /* Save fields on a per block data
102 *
103 * Post data is sent unfiltered so sanitization must be done here!
104 * @param $data Array with all collection block data
105 * @param $post Array|null The post array or null. Null in case of loading defaults, not a post.
106 */
107 public function save_fields($data, $post)
108 {
109 $blockdata = array();
110
111 //if (isset($this->data[$this->blockname]))
112 // $blockdata = $this->data;
113 //$blockdata = $data;
114
115 foreach($this->fields as $field => $options)
116 {
117
118 if ($field == "multifields") // standardize multi fields
119 {
120 $mf_id = $options["id"];
121 $mf_fields = $options["fields"];
122
123 $multidata = array();
124
125 if (isset($post[$mf_id])) // the collection with the id's.
126 {
127 $i = 0; // id's might be duplicate i.e. two similar buttons.
128 foreach ($post[$mf_id] as $id) // id is button-id
129 {
130
131 foreach($mf_fields as $mf_field => $options)
132 {
133 $default = (isset($options["default"])) ? $options["default"] : '';
134 // POST[ field_name - $id ]
135 //
136 $multidata[$i][$id][$mf_field] = isset($post[$mf_field . "-" . $id . "-" . $i ]) ? $post[$mf_field . "-" . $id . "-" . $i] : $default;
137 }
138 $i++;
139 }
140 }
141 $blockdata[$mf_id] = $multidata;
142 }
143 else
144 {
145 $default = (isset($options["default"])) ? $options["default"] : '';
146 // stripslashes since the WP post var adds them.
147 $blockdata[$field] = (isset($post[$field])) ? stripslashes(sanitize_text_field($post[$field])) : $default;
148 }
149 }
150
151 $data[$this->blockname] = $blockdata;
152 return $data;
153
154 }
155
156 public function parse($domObj, $args = array() )
157 {
158
159 return $domObj; // nothing to do by default
160 }
161
162 public function parseButtons($buttons)
163 {
164 return $buttons;
165 }
166
167 // Adepted from block class - maxbuttons
168 public function parseCSS($css, $args)
169 {
170 $data = $this->data[$this->blockname];
171
172 // get all fields from this block
173 foreach($this->fields as $field => $field_data)
174 {
175 // get cssparts, can be comma-seperated value
176 $csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton');
177 $csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal';
178
179 // if this field has a css property
180 if (isset($field_data["css"]))
181 {
182
183
184 // get the property value from the data
185 $value = isset($data[$field]) ? $data[$field] : $field_data['default'];
186 $value = str_replace(array(";"), '', $value); //sanitize
187
188 if ( strpos($field_data["default"],"px") && ! strpos($value,"px"))
189 {
190 if ($value == '') $value = 0; // pixel values, no empty but 0
191 $value .= "px";
192 }
193
194 if (isset($data[$field]))
195 {
196
197 foreach($csspart as $part)
198 {
199 if (is_array($csspseudo))
200 {
201 foreach($csspseudo as $pseudo)
202 $css[$part][$pseudo][$field_data["css"]] = $value ;
203 }
204 else
205 $css[$part][$csspseudo][$field_data["css"]] = $value ;
206 }
207 }
208 }
209
210 }
211
212 return $css;
213 }
214
215
216 // default function
217 public function parseJS($js)
218 {
219 return $js;
220 }
221
222 /** Get the post metadata of a certain block
223 * Used in post specific settings
224 * @param $post_id int Post id
225 * @return Array | Boolean . Array of setting data, or false if not metadata present
226 */
227 public function get_block_meta_data($post_id)
228 {
229 $meta = $this->collection->get_metadata($post_id);
230
231 if ( isset($meta[$this->blockname]))
232 return $meta[$this->blockname];
233 else
234 return false;
235
236 }
237
238 public function save_meta_boxes($metadata, $post_id, $post)
239 {
240 if (! isset($this->meta_fields))
241 return $metadata;
242
243 $blockmeta = array();
244
245 foreach($this->meta_fields as $field => $options)
246 {
247 $default = (isset($options["default"])) ? $options["default"] : '';
248 // stripslashes since the WP post var adds them.
249 $blockmeta[$field] = (isset($post[$field])) ? stripslashes(sanitize_text_field($post[$field])) : $default;
250 }
251
252 $metadata[$this->blockname] = $blockmeta;
253
254 return $metadata;
255 }
256
257 public function do_meta_boxes($array, $post)
258 {
259 return $array;
260 }
261
262
263 abstract function admin();
264
265 } // class
266