| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('No direct access permitted'); |
| 3 |
|
| 4 |
/** A block is a combination of related settings. |
| 5 |
* |
| 6 |
* Blocks are grouped and put into the same Database table row. This way related data, it's executing, display and other decision |
| 7 |
* making is seperate from other blocks improving realiability and readability of the code. |
| 8 |
*/ |
| 9 |
use MaxButtons\maxBlocks as maxBlocks; |
| 10 |
use MaxButtons\maxField as maxField; |
| 11 |
|
| 12 |
abstract class maxBlock |
| 13 |
{ |
| 14 |
protected $data = array(); |
| 15 |
|
| 16 |
/** Block constructor |
| 17 |
* |
| 18 |
* Constructor for a button block. Hooks up all needed filters and inits data for a block. |
| 19 |
* |
| 20 |
*/ |
| 21 |
function __construct($priority = 10) |
| 22 |
{ |
| 23 |
|
| 24 |
// filters for save_post, display etc. Buttons class will the filters. |
| 25 |
add_filter('mb-save-fields', array($this, 'save_fields'),10,2); |
| 26 |
add_action('mb-admin-fields', array($this,'admin_fields' ) ); |
| 27 |
add_action('mb-data-load', array($this,'set') ); |
| 28 |
|
| 29 |
add_filter('mb-parse-button', array($this, 'parse_button'),10,2 ); |
| 30 |
add_filter('mb-js-blocks', array($this, 'parse_js'), 10, 2); |
| 31 |
//add_filter('mb-parse-element-preview', array($this,'parse_element'), 10,2); |
| 32 |
|
| 33 |
add_filter('mb-css-blocks', array($this, 'parse_css'),10,2 ); |
| 34 |
add_filter('mb-field-map', array($this, 'map_fields') ); |
| 35 |
|
| 36 |
|
| 37 |
$this->fields = apply_filters($this->blockname. "-block-fields",$this->fields); |
| 38 |
$this->data[$this->blockname] = array(); //empty init |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
/** Save fields runs the POST variable through all blocks |
| 43 |
* |
| 44 |
* Taking the post variable from the form, the function will attach the submitted data to the block - field logic and |
| 45 |
* return a data object to save to the Database. If no value is submitted, the default will be loaded. |
| 46 |
* |
| 47 |
* @param $data Array Data in blockname - field logic format |
| 48 |
* @param $post Array $_POST style data |
| 49 |
* |
| 50 |
* @return $data Array |
| 51 |
*/ |
| 52 |
public function save_fields($data, $post) |
| 53 |
{ |
| 54 |
$block = isset($this->data[$this->blockname]) ? $this->data[$this->blockname] : array(); |
| 55 |
|
| 56 |
foreach($this->fields as $field => $options) |
| 57 |
{ |
| 58 |
$default = (isset($options["default"])) ? $options["default"] : ''; |
| 59 |
if (is_string($default) && strpos($default,"px") !== false) |
| 60 |
$block[$field] = (isset($post[$field]) ) ? intval($post[$field]) : $default; |
| 61 |
elseif( isset($post[$field]) && is_array($post[$field])) |
| 62 |
{ |
| 63 |
$block[$field] = $post[$field]; |
| 64 |
} |
| 65 |
else |
| 66 |
$block[$field] = (isset($post[$field])) ? sanitize_text_field($post[$field]) : $default; |
| 67 |
} |
| 68 |
|
| 69 |
$data[$this->blockname] = $block; |
| 70 |
return $data; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** Return fields of current block |
| 75 |
* |
| 76 |
* Will return fields of current block only |
| 77 |
* @return Array $fields |
| 78 |
*/ |
| 79 |
public function get_fields() |
| 80 |
{ |
| 81 |
return $this->fields; |
| 82 |
} |
| 83 |
|
| 84 |
/** Returns Blockname of current block |
| 85 |
* |
| 86 |
* |
| 87 |
* @return $string | boolean Name of the block, if set, otherwise false |
| 88 |
*/ |
| 89 |
public function get_name() |
| 90 |
{ |
| 91 |
if (isset($this->blockname)) |
| 92 |
return $this->blockname; |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/* Display Block admin interface |
| 99 |
* |
| 100 |
* Writes admin interface to output. |
| 101 |
* @abstract |
| 102 |
*/ |
| 103 |
abstract public function admin_fields(); |
| 104 |
|
| 105 |
/** Parse HTML portion of button |
| 106 |
* |
| 107 |
* This filter is passed through to modify the HTML parts of the button. |
| 108 |
* |
| 109 |
* Note: When changing parts of the DomObj writing new tags / DOM to elements, it's needed to regenerate the Object. |
| 110 |
* |
| 111 |
* @param $button DomObj SimpleDOMObject |
| 112 |
* @param $mode String[normal|preview] Flag to check if loading in preview |
| 113 |
* |
| 114 |
* @return DomObj |
| 115 |
*/ |
| 116 |
public function parse_button($button, $mode) { return $button; } |
| 117 |
|
| 118 |
/* Parse CSS of the button |
| 119 |
* |
| 120 |
* This function will go through the blocks, matching the $css definitions of a fields and putting them in the |
| 121 |
* correct tags ( partly using csspart ) . |
| 122 |
* |
| 123 |
* @param $css Array [normal|hover|other][cssline] = css declaration |
| 124 |
* @param $mode String [normal|preview] |
| 125 |
* |
| 126 |
* @return $css Array |
| 127 |
*/ |
| 128 |
public function parse_css($css, $mode = 'normal') { |
| 129 |
|
| 130 |
$data = $this->data[$this->blockname]; |
| 131 |
|
| 132 |
// get all fields from this block |
| 133 |
foreach($this->fields as $field => $field_data) |
| 134 |
{ |
| 135 |
// get cssparts, can be comma-seperated value |
| 136 |
$csspart = (isset($field_data["csspart"])) ? explode(",",$field_data["csspart"]) : array('maxbutton'); |
| 137 |
$csspseudo = (isset($field_data["csspseudo"])) ? explode(",", $field_data["csspseudo"]) : 'normal'; |
| 138 |
|
| 139 |
// if this field has a css property |
| 140 |
if (isset($field_data["css"])) |
| 141 |
{ |
| 142 |
// get the property value from the data |
| 143 |
$value = isset($data[$field]) ? $data[$field] : ''; |
| 144 |
$value = str_replace(array(";"), '', $value); //sanitize |
| 145 |
|
| 146 |
if (isset($field_data["default"]) && strpos($field_data["default"],"px") && ! strpos($value,"px")) |
| 147 |
{ |
| 148 |
if ($value == '') $value = 0; // pixel values, no empty but 0 |
| 149 |
$value .= "px"; |
| 150 |
} |
| 151 |
if (isset($data[$field])) |
| 152 |
{ |
| 153 |
foreach($csspart as $part) |
| 154 |
{ |
| 155 |
if (is_array($csspseudo)) |
| 156 |
{ |
| 157 |
foreach($csspseudo as $pseudo) |
| 158 |
$css[$part][$pseudo][$field_data["css"]] = $value ; |
| 159 |
} |
| 160 |
else |
| 161 |
$css[$part][$csspseudo][$field_data["css"]] = $value ; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
return $css; |
| 169 |
} |
| 170 |
|
| 171 |
/* Ability to output custom JS for each button */ |
| 172 |
public function parse_js($js, $mode = 'normal') |
| 173 |
{ |
| 174 |
return $js; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** Map the Block fields |
| 179 |
* |
| 180 |
* This function will take the field name and link it to the defined CSS definition to use in providing the live preview in the |
| 181 |
* button editor. I.e. a field with name x will be linked to CSS-property X . Or to a custom Javascript function. |
| 182 |
* |
| 183 |
* @param $map Array [$field_id][css|attr|func|] = property/function |
| 184 |
* |
| 185 |
* @return Array |
| 186 |
*/ |
| 187 |
public function map_fields($map) |
| 188 |
{ |
| 189 |
foreach($this->fields as $field => $field_data) |
| 190 |
{ |
| 191 |
if (isset($field_data["css"])) |
| 192 |
{ |
| 193 |
$cssdef = $field_data["css"]; |
| 194 |
$multidef = explode('-',$cssdef); |
| 195 |
if ( count($multidef) > 1) |
| 196 |
{ |
| 197 |
$cssdef = ""; |
| 198 |
for($i = 0; $i < count($multidef); $i++) |
| 199 |
{ |
| 200 |
if ($i == 0) |
| 201 |
$cssdef .= $multidef[$i]; |
| 202 |
else |
| 203 |
$cssdef .= ucfirst($multidef[$i]); |
| 204 |
//$multidef[$i] . ucfirst($multidef[1]); |
| 205 |
} |
| 206 |
} |
| 207 |
$map[$field]["css"] = $cssdef; |
| 208 |
if ( isset($field_data["default"]) && strpos($field_data["default"],"px") != false ) |
| 209 |
$map[$field]["css_unit"] = 'px'; |
| 210 |
|
| 211 |
} |
| 212 |
if (isset($field_data["csspart"])) |
| 213 |
$map[$field]["csspart"] = $field_data["csspart"]; |
| 214 |
} |
| 215 |
return $map; |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/** Sets the data |
| 220 |
* |
| 221 |
* This action is called from button class when data is pulled from the database and populates the dataArray to all blocks |
| 222 |
* |
| 223 |
*/ |
| 224 |
function set($dataArray) |
| 225 |
{ |
| 226 |
|
| 227 |
$this->data = $dataArray; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
?> |
| 232 |
|