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