| 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 |
$blockdata[$field] = (isset($post[$field])) ? stripslashes(sanitize_text_field($post[$field])) : $default; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
$data[$this->blockname] = $blockdata; |
| 162 |
return $data; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
public function parse($domObj, $args = array() ) |
| 167 |
{ |
| 168 |
|
| 169 |
return $domObj; // nothing to do by default |
| 170 |
} |
| 171 |
|
| 172 |
public function parseButtons($buttons) |
| 173 |
{ |
| 174 |
return $buttons; |
| 175 |
} |
| 176 |
|
| 177 |
// Adepted from block class - maxbuttons |
| 178 |
public function parseCSS($css, $args) |
| 179 |
{ |
| 180 |
$data = $this->data[$this->blockname]; |
| 181 |
|
| 182 |
// get all fields from this block |
| 183 |
foreach($this->fields as $field => $field_data) |
| 184 |
{ |
| 185 |
// get cssparts, can be comma-seperated value |
| 186 |
$csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton'); |
| 187 |
$csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal'; |
| 188 |
|
| 189 |
// if this field has a css property |
| 190 |
if (isset($field_data["css"])) |
| 191 |
{ |
| 192 |
|
| 193 |
|
| 194 |
// get the property value from the data |
| 195 |
$value = isset($data[$field]) ? $data[$field] : $field_data['default']; |
| 196 |
$value = str_replace(array(";"), '', $value); //sanitize |
| 197 |
|
| 198 |
if ( strpos($field_data["default"],"px") && ! strpos($value,"px")) |
| 199 |
{ |
| 200 |
if ($value == '') $value = 0; // pixel values, no empty but 0 |
| 201 |
$value .= "px"; |
| 202 |
} |
| 203 |
|
| 204 |
if (isset($data[$field])) |
| 205 |
{ |
| 206 |
|
| 207 |
foreach($csspart as $part) |
| 208 |
{ |
| 209 |
if (is_array($csspseudo)) |
| 210 |
{ |
| 211 |
foreach($csspseudo as $pseudo) |
| 212 |
$css[$part][$pseudo][$field_data["css"]] = $value ; |
| 213 |
} |
| 214 |
else |
| 215 |
$css[$part][$csspseudo][$field_data["css"]] = $value ; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
return $css; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
// default function |
| 227 |
public function parseJS($js) |
| 228 |
{ |
| 229 |
return $js; |
| 230 |
} |
| 231 |
|
| 232 |
/** Get the post metadata of a certain block |
| 233 |
* Used in post specific settings |
| 234 |
* @param $post_id int Post id |
| 235 |
* @return Array | Boolean . Array of setting data, or false if not metadata present |
| 236 |
*/ |
| 237 |
public function get_block_meta_data($post_id) |
| 238 |
{ |
| 239 |
$meta = $this->collection->get_metadata($post_id); |
| 240 |
|
| 241 |
if ( isset($meta[$this->blockname])) |
| 242 |
return $meta[$this->blockname]; |
| 243 |
else |
| 244 |
return false; |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
public function save_meta_boxes($metadata, $post_id, $post) |
| 249 |
{ |
| 250 |
if (! isset($this->meta_fields)) |
| 251 |
return $metadata; |
| 252 |
|
| 253 |
$blockmeta = array(); |
| 254 |
|
| 255 |
foreach($this->meta_fields as $field => $options) |
| 256 |
{ |
| 257 |
$default = (isset($options["default"])) ? $options["default"] : ''; |
| 258 |
// stripslashes since the WP post var adds them. |
| 259 |
$blockmeta[$field] = (isset($post[$field])) ? stripslashes(sanitize_text_field($post[$field])) : $default; |
| 260 |
} |
| 261 |
|
| 262 |
$metadata[$this->blockname] = $blockmeta; |
| 263 |
|
| 264 |
return $metadata; |
| 265 |
} |
| 266 |
|
| 267 |
public function do_meta_boxes($array, $post) |
| 268 |
{ |
| 269 |
return $array; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
abstract function admin(); |
| 274 |
|
| 275 |
} // class |
| 276 |
|