| 1 |
<?php |
| 2 |
namespace maxButtons; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
abstract class collectionBlock |
| 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 |
|
| 15 |
|
| 16 |
function setCollection($collection) |
| 17 |
{ |
| 18 |
$this->collection = $collection; |
| 19 |
} |
| 20 |
|
| 21 |
function set($data) |
| 22 |
{ |
| 23 |
//$blockname = $this->blockname; |
| 24 |
|
| 25 |
if (! isset($data[$this->blockname])) |
| 26 |
$blockdata = array(); |
| 27 |
else |
| 28 |
$blockdata = $data[$this->blockname]; |
| 29 |
|
| 30 |
|
| 31 |
foreach($this->fields as $field => $options) |
| 32 |
{ |
| 33 |
if ($field == 'multifields') // multifields can't set default since amount is unknown |
| 34 |
{ |
| 35 |
$mf_id = $options["id"]; |
| 36 |
$mf_fields = $options["fields"]; |
| 37 |
if (isset($blockdata[$mf_id])) |
| 38 |
{ |
| 39 |
$this->data[$mf_id] = $blockdata[$mf_id]; // why not. |
| 40 |
} |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
if (isset($blockdata[$field])) |
| 45 |
$this->data[$field] = $blockdata[$field]; |
| 46 |
elseif (isset($options["default"])) |
| 47 |
$this->data[$field] = $options["default"]; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return true; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
// get the block data |
| 56 |
function get() |
| 57 |
{ |
| 58 |
return $this->data; |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
function setPreview($preview = true) |
| 63 |
{ |
| 64 |
$this->is_preview = $preview; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/* Save fields on a per block data |
| 69 |
|
| 70 |
Post data is sent unfiltered so sanitization must be done here! |
| 71 |
*/ |
| 72 |
function save_fields($data, $post) |
| 73 |
{ |
| 74 |
$blockdata = array(); |
| 75 |
|
| 76 |
//if (isset($this->data[$this->blockname])) |
| 77 |
$blockdata = $this->data; |
| 78 |
|
| 79 |
foreach($this->fields as $field => $options) |
| 80 |
{ |
| 81 |
|
| 82 |
if ($field == "multifields") // standardize multi fields |
| 83 |
{ |
| 84 |
$mf_id = $options["id"]; |
| 85 |
$mf_fields = $options["fields"]; |
| 86 |
|
| 87 |
$multidata = array(); |
| 88 |
|
| 89 |
if (isset($post[$mf_id])) // the collection with the id's. |
| 90 |
{ |
| 91 |
$i = 0; // id's might be duplicate i.e. two similar buttons. |
| 92 |
foreach ($post[$mf_id] as $id) // id is button-id |
| 93 |
{ |
| 94 |
|
| 95 |
foreach($mf_fields as $mf_field => $options) |
| 96 |
{ |
| 97 |
$default = (isset($options["default"])) ? $options["default"] : ''; |
| 98 |
// POST[ field_name - $id ] |
| 99 |
// |
| 100 |
$multidata[$i][$id][$mf_field] = isset($post[$mf_field . "-" . $id . "-" . $i ]) ? $post[$mf_field . "-" . $id . "-" . $i] : $default; |
| 101 |
} |
| 102 |
$i++; |
| 103 |
} |
| 104 |
} |
| 105 |
$blockdata[$mf_id] = $multidata; |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
$default = (isset($options["default"])) ? $options["default"] : ''; |
| 110 |
// stripslashes since the WP post var adds them. |
| 111 |
$blockdata[$field] = (isset($post[$field])) ? stripslashes(sanitize_text_field($post[$field])) : $default; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
$data[$this->blockname] = $blockdata; |
| 117 |
return $data; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
function parse($domObj, $args) |
| 122 |
{ |
| 123 |
|
| 124 |
return $domObj; // nothing to do by default |
| 125 |
} |
| 126 |
|
| 127 |
function parseButtons($buttons) |
| 128 |
{ |
| 129 |
return $buttons; |
| 130 |
} |
| 131 |
|
| 132 |
// Adepted from block class - maxbuttons |
| 133 |
function parseCSS($css, $args) |
| 134 |
{ |
| 135 |
$data = $this->data; |
| 136 |
|
| 137 |
// get all fields from this block |
| 138 |
foreach($this->fields as $field => $field_data) |
| 139 |
{ |
| 140 |
// get cssparts, can be comma-seperated value |
| 141 |
$csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton'); |
| 142 |
$csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal'; |
| 143 |
|
| 144 |
// if this field has a css property |
| 145 |
if (isset($field_data["css"])) |
| 146 |
{ |
| 147 |
// get the property value from the data |
| 148 |
$value = isset($data[$field]) ? $data[$field] : ''; |
| 149 |
$value = str_replace(array(";"), '', $value); //sanitize |
| 150 |
|
| 151 |
if ( strpos($field_data["default"],"px") && ! strpos($value,"px")) |
| 152 |
{ |
| 153 |
if ($value == '') $value = 0; // pixel values, no empty but 0 |
| 154 |
$value .= "px"; |
| 155 |
} |
| 156 |
if (isset($data[$field])) |
| 157 |
{ |
| 158 |
foreach($csspart as $part) |
| 159 |
{ |
| 160 |
if (is_array($csspseudo)) |
| 161 |
{ |
| 162 |
foreach($csspseudo as $pseudo) |
| 163 |
$css[$part][$pseudo][$field_data["css"]] = $value ; |
| 164 |
} |
| 165 |
else |
| 166 |
$css[$part][$csspseudo][$field_data["css"]] = $value ; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
return $css; |
| 174 |
} |
| 175 |
|
| 176 |
// default function |
| 177 |
function parseJS($js) |
| 178 |
{ |
| 179 |
return $js; |
| 180 |
} |
| 181 |
|
| 182 |
// for the preview |
| 183 |
public function map_fields($map) |
| 184 |
{ |
| 185 |
foreach($this->fields as $field => $field_data) |
| 186 |
{ |
| 187 |
if (isset($field_data["css"])) |
| 188 |
{ |
| 189 |
$cssdef = $field_data["css"]; |
| 190 |
$multidef = explode('-',$cssdef); |
| 191 |
if ( count($multidef) > 1) |
| 192 |
{ |
| 193 |
$cssdef = ""; |
| 194 |
for($i = 0; $i < count($multidef); $i++) |
| 195 |
{ |
| 196 |
if ($i == 0) |
| 197 |
$cssdef .= $multidef[$i]; |
| 198 |
else |
| 199 |
$cssdef .= ucfirst($multidef[$i]); |
| 200 |
//$multidef[$i] . ucfirst($multidef[1]); |
| 201 |
} |
| 202 |
} |
| 203 |
$map[$field]["css"] = $cssdef; |
| 204 |
if ( isset($field_data["default"]) && strpos($field_data["default"],"px") != false ) |
| 205 |
$map[$field]["css_unit"] = 'px'; |
| 206 |
|
| 207 |
} |
| 208 |
if (isset($field_data["csspart"])) |
| 209 |
$map[$field]["csspart"] = $field_data["csspart"]; |
| 210 |
} |
| 211 |
return $map; |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
abstract function admin_fields(); |
| 216 |
|
| 217 |
} // class |
| 218 |
|