PluginProbe
MaxButtons – Create buttons / 6.28
MaxButtons – Create buttons v6.28
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
← All changes | classes/button.php +480 -466 6.96.28 View file →
@@ -1,43 +1,42 @@
1 1 <?php
2 -
2 +namespace MaxButtons;
3 3 defined('ABSPATH') or die('No direct access permitted');
4 4
5 -/* Datamodel and base functionality for a button
5 +/* Datamodel and base functionality for a button
6 6
7 7 */
8 8
9 -use MaxButtons\maxBlocks as maxBlocks;
10 -use MaxButtons\maxField as maxField;
11 -
9 +use \simple_html_dom as simple_html_dom;
10 +
12 11 class maxButton
13 12 {
14 - protected $id = 0;
13 + protected $id = 0;
15 14 protected $document_id = 0; // an id that's not duplicated when there are multiple buttons on the same page. Preferably reliable as well.
16 - protected $name = '';
17 - protected $status = '';
18 - protected $description = '';
19 - protected $cache = '';
20 -
15 + protected $name = '';
16 + protected $status = '';
17 + protected $description = '';
18 + protected $cache = '';
19 +
21 20 protected $button_loaded = false;
22 21
23 - protected $data = array();
24 - protected $blocks; //= array('basic', 'text' , 'border', 'color', 'gradient', 'container', 'advanced', 'responsive'); // Blocks
22 + protected $data = array();
23 + protected $blocks; // Block Classes
25 24 protected $templates = array(); // .tpl files
26 25
27 - protected $button_css = array('normal' => array() ,':hover' => array() ,':visited' => array(), "responsive" => array());
28 - protected $button_js = array();
29 -
26 + protected $button_css = array();
27 + protected $button_js = array();
28 +
30 29 // output conditions
31 - protected $load_css = 'footer'; // [ footer, inline, external, element ]
32 - protected $load_js = 'footer';
33 -
34 - protected $cssParser = false;
35 - protected $parsed_css = '';
36 -
37 -
38 - /* Class constructor
39 -
30 + protected $load_css = 'footer'; // [ footer, inline, external, element ]
31 + protected $load_js = 'footer';
32 +
33 + protected $cssParser = false;
34 + protected $parsed_css = '';
35 +
36 +
37 + /* Class constructor
38 +
40 39 Get als loads the various blocks of which a button is built up. Blocks can be added and removed using the mb-init-blocks filter
41 40 */
42 41 function __construct()
43 42 {
@@ -43,112 +42,52 @@
43 42 {
44 43 maxUtils::addTime("Button construct");
45 44
46 45 // the parser
47 -
48 - // get all files from blocks map
49 -
50 - // get all blocks via apply filters, do init. Init should not load anything big.
51 -
52 46
53 - $block_paths = apply_filters('mb-block-paths', array(MB()->get_plugin_path() . "blocks/") );
54 -
55 - global $blockClass; // load requires only once
47 + // get all files from blocks map
56 48
57 - if ($blockClass == '' || count($blockClass) == 0)
58 - {
49 + // get all blocks via apply filters, do init. Init should not load anything big.
50 + $this->loadBlockClasses();
59 51
60 - $newBlocks = array();
61 -
62 - foreach($block_paths as $block_path)
63 - {
64 - $dir_iterator = new RecursiveDirectoryIterator($block_path, FilesystemIterator::SKIP_DOTS);
65 - $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
66 -
67 - foreach ($iterator as $fileinfo)
68 - {
52 + }
69 53
70 - $path = $fileinfo->getRealPath();
71 - // THIS IS PHP > 5.3.6
72 - //$extension = $fileinfo->getExtension();
73 - $extension = pathinfo($path, PATHINFO_EXTENSION);
74 -
75 - if ($fileinfo->isFile() )
76 - {
77 - if ($extension == 'php')
78 - {
79 - require_once($path);
80 - }
81 - elseif($extension == 'tpl')
82 - {
83 - $filename = $fileinfo->getBasename('.tpl');
84 - $this->templates[$filename] = array('path' => $path);
85 - }
86 - }
87 - }
54 + /* Makes overriding block features possible by subclass
88 55
89 - }
90 - ksort($blockOrder);
91 - foreach($blockOrder as $prio => $blockArray)
92 - {
93 - foreach($blockArray as $block)
94 - {
95 - if (isset($blockClass[$block]))
96 - $newBlocks[$block] = $blockClass[$block];
97 -
98 - }
99 - }
100 - $blockClass = $newBlocks;
101 - if (is_admin())
102 - {
103 - // possible issue with some hosters faking is_admin flag.
104 - if (class_exists('MaxButtons\maxBlocks') && class_exists('MaxButtons\maxBlocks') )
105 - {
106 - maxField::setTemplates($this->templates);
107 - maxBlocks::init();
108 - }
109 - else
110 - {
111 - error_log('[MaxButtons] - MaxField class is not set within admin context. This can cause issues when using button editor');
112 - }
113 - }
114 - $this->loadBlockClasses($blockClass);
115 -
116 -
117 -
118 - }
119 -
120 - $this->blocks = array_keys($blockClass);
121 - }
122 -
123 - /* Makes overriding block features possible by subclass
124 -
125 56 */
126 - private function loadBlockClasses($classes)
57 + private function loadBlockClasses()
127 58 {
128 - // set blocks to the 'block name' that survived.
129 - maxUtils::addTime("Load Block classes");
130 -
131 - $classes = apply_filters("mb_blockclassesload", $classes);
132 -
133 -
134 - foreach($classes as $block => $class)
59 +
60 + // set blocks to the 'block name' that survived.
61 + maxUtils::addTime("Load Block classes");
62 +
63 + //$classes = apply_filters("mb_blockclassesload", $classes);
64 + $class_array = maxBlocks::getBlockClasses();
65 + $classes = array_map(maxUtils::namespaceit('maxUtils::array_namespace'), $class_array );
66 +
67 + foreach($classes as $block => $class)
68 + {
69 + $block = new $class();
70 +
71 + $this->blocks[] = $block;
72 + if (is_admin())
135 73 {
136 - $block = new $class();
137 - if (is_admin())
138 - maxBlocks::add($block); // block collection.
74 + maxBlocks::add($block); // block collection.
139 75 }
76 + }
140 77
141 - do_action("mb_blockclassesloaded", $classes);
142 78
79 + $this->clear(); // init
80 + do_action("mb\blockclasses", $class_array);
81 +
143 82 }
144 -
145 - /** Simple function to retrieve loaded blocks - * Questionable if in use */
146 - public function getDefinedBlocks()
83 +
84 + /** Simple function to retrieve loaded blocks - * Used by install class */
85 + public function getBlocks()
147 86 {
148 87 return $this->blocks;
149 88 }
150 -
89 +
151 90 /** Get Data from Database and set variables
152 91 *
153 92 * You can pass either id or name to this function
154 93 *
@@ -162,100 +101,113 @@
162 101 $name = sanitize_text_field($name);
163 102 $status = sanitize_text_field($status);
164 103
165 104 global $wpdb;
166 - $this->clear(); // clear the internals of any previous work
167 -
105 + $this->clear(); // clear the internals of any previous work
106 +
168 107 // check to see if the value passed is NOT numeric. If it is, use title, else assume numeric
169 108 if($id == 0 && $name != '') {
170 109 $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . maxUtils::get_table_name() . " WHERE name = '%s' and status ='%s'", trim($name), $status ), ARRAY_A);
171 -
110 +
172 111 } else {
173 112 $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . maxUtils::get_table_name() . " WHERE id = %d and status ='%s'", $id, $status), ARRAY_A);
174 113 }
175 -
176 - if (count($row) == 0)
114 +
115 + if (count($row) == 0)
177 116 {
178 - return false;
179 - }
117 + return false;
118 + }
180 119
181 - /* Take the id from the query, otherwise ( when button shortcode called by name ) id might not be present properly' */
182 - $button_id = $row["id"];
120 + /* Take the id from the query, otherwise ( when button shortcode called by name ) id might not be present properly' */
121 + $button_id = $row["id"];
183 122 maxButtons::buttonLoad(array("button_id" => $button_id)); // central registration
184 -
123 +
185 124 return $this->setupData($row);
186 -
125 +
187 126 }
188 127 /** Clear button settings
189 128 *
190 129 * This function prevent that generated values from previous set actions will still be present.
191 - */
192 - function clear()
130 + */
131 + function clear()
193 132 {
194 133 unset($this->data);
195 134 unset($this->button_css);
196 135 $this->id = 0; // clear id
197 - $this->button_css = '';
198 - $this->button_js = array();
199 - $this->data = array();
136 + $this->button_css = array();
137 + $this->button_js = array();
138 + $this->data = array('id' => 0);
200 139 $this->data = $this->save(array(),false);
201 - $this->cache = '';
140 +
141 + $this->cache = '';
202 142 $this->button_loaded = false;
203 - $this->reloadData();
143 +
144 + foreach($this->blocks as $block)
145 + {
146 + $block->set($this->data); // reset blocks
147 + }
148 +
204 149 }
205 -
150 +
206 151 function setupData($data)
207 152 {
153 +
208 154 maxUtils::addTime("Button: Setup data");
209 155 foreach($this->blocks as $block)
210 156 {
157 + $block_name = $block->get_name();
211 158
212 - if (array_key_exists($block, $data)) // strangely isset doesn't work
159 + if (array_key_exists($block_name, $data)) // strangely isset doesn't work
213 160 {
214 - $this->data[$block] = maybe_unserialize($data[$block]); // allow to feed unserialized stuff not from dbase
215 - if (! is_array($this->data[$block]))
161 + $this->data[$block_name] = maybe_unserialize($data[$block_name]); // allow to feed unserialized stuff not from dbase
162 + if (! is_array($this->data[$block_name]))
216 163 {
217 - $this->data[$block] = json_decode($data[$block], true);
164 + $this->data[$block_name] = json_decode($data[$block_name], true);
218 165 }
219 -
166 +
220 167 }
221 - else
222 - {
223 - // else block does not exist in this dataset - ignoring.
224 - //exit("Fatal: Something wrong with provider on $block ");
225 - }
226 168 }
227 -
169 +
228 170 $this->id = $data["id"];
229 - $this->document_id = maxButtons::getDocumentID(array("button_id" => $this->id));
171 +
172 + // Because PHP 5.3
173 + $class = maxUtils::namespaceit('maxButtons');
174 + $this->document_id = $class::getDocumentID(array("button_id" => $this->id));
230 175 $this->cache = isset($data["cache"]) ? trim($data["cache"]) : ''; // not set at button packs / non-dbase buttons!
231 - $this->data["id"] = $this->id; // needed for certain blocks, to be button aware.
232 - $this->data["document_id"] = $this->document_id; // bound to JS and others.
233 - $this->name = $data["name"];
176 + $this->data["id"] = $this->id; // needed for certain blocks, to be button aware.
177 + $this->data["document_id"] = $this->document_id; // bound to JS and others.
178 + $this->name = $data["name"];
234 179 $this->status = $data["status"];
235 - $this->description = $this->data["basic"]["description"];
180 + $this->description = $this->data["basic"]["description"];
236 181
237 - do_action('mb-data-load', $this->data);
182 + //do_action('mb-data-load', $this->data);
183 + foreach($this->blocks as $block)
184 + {
185 + $block->set($this->data);
186 + }
238 187
188 + maxBlocks::setData($this->data);
189 +
190 +
239 191 return true;
240 -
192 +
241 193 }
242 -
243 - function get( )
194 +
195 + function get( )
244 196 {
245 197 return $this->data;
246 -
198 +
247 199 }
248 -
249 - function getID()
200 +
201 + function getID()
250 202 {
251 - return $this->id;
203 + return $this->id;
252 204 }
253 - function getDocumentID()
205 + function getDocumentID()
254 206 {
255 - return $this->document_id;
207 + return $this->document_id;
256 208 }
257 - function getName()
209 + function getName()
258 210 {
259 211 return $this->name;
260 212 }
261 213 function getDescription()
@@ -261,141 +213,154 @@
261 213 function getDescription()
262 214 {
263 215 return $this->description;
264 216 }
265 -
217 +
266 218 function getStatus()
267 219 {
268 - return $this->status;
220 + return $this->status;
269 221 }
270 -
271 - function getParsedCSS()
222 +
223 + function getParsedCSS()
272 224 {
273 - return $this->parsed_css;
225 + return $this->parsed_css;
274 226 }
275 -
227 +
276 228 function getCSSArray()
277 229 {
278 230
279 231 return $this->button_css;
280 232 }
281 -
233 +
282 234 function getCSSParser()
283 235 {
284 236 if (! $this->cssParser)
285 - $this->cssParser = new maxCSSParser();
286 -
237 + $this->cssParser = new maxCSSParser();
238 +
287 239 return $this->cssParser;
288 240 }
289 241 // get the cache
290 - function getCache()
242 + function getCache()
291 243 {
292 244 return $this->cache;
293 245 }
294 -
295 - // modify the cache at own risk
246 +
247 + // modify the cache at own risk
296 248 function setCache($cache)
297 249 {
298 - $this->cache = $cache;
299 - }
250 + $this->cache = $cache;
251 + }
300 252
301 - function setData($blockname, $data )
253 + /* Used by collections and import. Use sparingly. Button data is reput to blocks on display */
254 + function setData($blockname, $data )
302 255 {
303 256 foreach($data as $key => $value)
304 257 {
305 - $this->data[$blockname][$key] = $value;
306 -
258 + $this->data[$blockname][$key] = $value;
259 +
307 260 }
308 261 }
309 -
310 - /* Tell all blocks to reload the daa
311 -
312 - This function will tell all loaded blocks to reload it's data. This is needed when data is changed after the initial button load.
313 -
314 - */
262 +
263 +
264 + /* Tell all blocks to reload the data
265 +
266 + This function will tell all loaded blocks to reload it's data. This is needed when data is changed after the initial button load.
267 +
268 + */
315 269 function reloadData()
316 270 {
317 - do_action('mb-data-load', $this->data);
318 -
319 - }
271 + exit('reload Data - do not use');
272 + //do_action('mb-data-load', $this->data);
320 273
274 + }
275 +
321 276 /* Parse CSS from the elements
322 -
277 +
323 278 @param string $mode [normal,preview,editor] - The view needed.
324 - @param string $forceCompile Recompile the CSS in any case
279 + @param string $forceCompile Recompile the CSS in any case
325 280 */
326 281 function parse_css($mode = "normal", $forceCompile = false )
327 282 {
328 - $css = $this->button_css;
329 -
283 + $css = $this->button_css;
284 +
330 285 if (isset($this->cache) && $this->cache != '' && ! $forceCompile)
331 - {
332 -
286 + {
287 +
333 288 $css = $this->cache;
334 289 // kill media queries from cache
335 290 if ($mode != 'normal')
336 291 {
337 - $pattern = "/@media.*}/is";
292 + $pattern = "/@media.*}/is";
338 293 preg_match($pattern, $css, $matches);
339 - $css = preg_replace($pattern, '', $css);
294 + $css = preg_replace($pattern, '', $css);
340 295 }
341 296
342 297 maxUtils::addTime("Button: Cache loaded");
343 298 }
344 299 else
345 - {
346 - /* Internal filter, please don't use */
347 - $css = apply_filters('mb-css-blocks', $css, $mode);
348 -
300 + {
301 + /* Internal filter, please don't use */
302 + foreach($this->blocks as $block)
303 + {
304 + $css = $block->parse_css($css, $mode);
305 + }
306 + //$css = apply_filters('mb-css-blocks', $css, $mode);
307 +
349 308 /* Filter the raw CSS array before compile
350 -
351 - This filters passes an array with all CSS element before compile time. This should be CSS elements that can be understood by the CSS parser.
352 - @since 4.20
353 - @param $css CSS Array - split by element and pseudo (normal/hover)
354 -
309 +
310 + This filters passes an array with all CSS element before compile time. This should be CSS elements that can be understood by the CSS parser.
311 + @since 4.20
312 + @param $css CSS Array - split by element and pseudo (normal/hover)
313 +
355 314 */
356 - $css = apply_filters('mb/button/rawcss', $css, $mode);
357 315
316 + $css = apply_filters('mb/button/rawcss', $css, $mode);
317 +
358 318 $this->button_css = $css;
359 319
360 320 $css = $this->getCSSParser()->parse($this->button_css);
361 - $css = apply_filters('mb/button/compiledcss', $css, $mode); // the final result.
362 -
321 + $css = apply_filters('mb/button/compiledcss', $css, $mode); // the final result.
322 +
363 323 if ($mode == 'normal') // only in general mode, otherwise things go amiss.
364 324 $this->update_cache($css);
365 325
366 326 }
367 -
368 - $this->parsed_css = $css;
369 -
370 327
371 - return $css;
328 + $this->parsed_css = $css;
329 +
330 +
331 + return $css;
372 332 }
373 -
374 - /* Call blocks for javascript
375 -
376 - Function will call for all block element to crunch the required javascript for output ( if any )
377 - @param string $mode [normal, preview, editor]
378 -
333 +
334 + /* Call blocks for javascript
335 +
336 + Function will call for all block element to crunch the required javascript for output ( if any )
337 + @param string $mode [normal, preview, editor]
338 +
379 339 */
380 - function parse_js($mode = "normal")
340 + function parse_js($mode = "normal")
381 341 {
382 342 maxUtils::addTime("Button :: parse JS");
383 - $js = $this->button_js;
384 - $js = apply_filters('mb-js-blocks', $js, $mode);
385 - $this->button_js = $js;
343 + $js = $this->button_js;
344 + foreach($this->blocks as $block)
345 + {
346 + $js = $block->parse_js($js,$mode);
347 +
348 + }
349 +
350 + $this->button_js = $js;
386 351 }
387 -
352 +
388 353 /* Parse the actual button
389 -
390 - Function adds the basic button components, creates the DOM object for the button and asks all block elements to parse their additions.
391 -
392 - @param string $mode [normal, preview, editor]
354 +
355 + Function adds the basic button components, creates the DOM object for the button and asks all block elements to parse their additions.
356 +
357 + @param string $mode [normal, preview, editor]
393 358 @return Object DomObj presentation of the button
394 359 */
395 360 function parse_button($mode = 'normal')
396 361 {
397 - $name = $this->name;
362 + $name = $this->name;
398 363 // non-latin breaks CSS / ID's - so move to latin.
399 364 $name = maxUtils::translit($name);
400 365 $name = sanitize_title($name);
401 366
@@ -400,305 +365,341 @@
400 365 $name = sanitize_title($name);
401 366
402 367 $classes = array("maxbutton-" . $this->id,
403 368 "maxbutton");
404 - if ($name != '')
369 +
370 +
371 + if ($name != '')
405 372 $classes[] = "maxbutton-" . $name;
406 -
407 - $classes = apply_filters('mb-mainclasses', $classes);
408 - $classes = implode(' ', $classes);
409 -
373 +
374 + $classes = apply_filters('mb-mainclasses', $classes);
375 + $classes = implode(' ', $classes);
376 +
410 377 $domObj = new simple_html_dom();
411 - $domObj->load('<a class="' . $classes . '"></a>');
412 -
413 - $domObj = apply_filters('mb-parse-button', $domObj, $mode, $this->id);
414 -
378 + $domObj->load('<a class="' . $classes . '"></a>');
379 +
380 + foreach($this->blocks as $block)
381 + {
382 + $domObj = $block->parse_button($domObj, $mode);
383 + }
384 +
385 +
415 386 $domObj->load($domObj->save());
416 -
387 +
417 388 $cssParser = $this->getCSSParser();
418 389 $cssParser->loadDom($domObj);
419 390
420 - return $domObj;
391 + return $domObj;
421 392 }
422 393
423 - /* Display all data and html to allow users to edit button settings */
424 - public function admin_fields()
394 + /* Display all data and html to allow users to edit button settings */
395 + public function admin_fields()
425 396 {
426 - do_action('mb-admin-fields' );
427 -
397 + foreach($this->blocks as $block)
398 + {
399 + $block->admin_fields();
400 +
401 + }
402 + //do_action('mb-admin-fields' );
403 +
428 404 }
429 -
405 +
430 406 /* Display the button */
431 407 public function display($args = array() )
432 - {
408 + {
433 409 maxUtils::startTime('button-display-'. $this->id);
434 410 $defaults = array(
435 411 "mode" => 'normal',
436 412 "preview_part" => "full",
437 - "echo" => true,
438 - "load_css" => "footer", // control how css is loaded.
439 - "compile" => false, // possibility to force recompile if needed.
413 + "echo" => true,
414 + "load_css" => "footer", // control how css is loaded.
415 + "compile" => false, // possibility to force recompile if needed.
440 416 );
441 - $output = ''; // init output;
442 -
443 - $args = wp_parse_args($args, $defaults);
417 + $output = ''; // init output;
444 418
419 + $args = wp_parse_args($args, $defaults);
420 +
445 421 $cssParser = $this->getCSSParser(); // init parser
446 -
447 - $this->load_css = $args["load_css"];
448 -
422 +
423 + $this->load_css = $args["load_css"];
424 +
449 425 if ($this->id == 0) // if button doesn't exists don't display unless in editor
450 426 {
451 - if (! $args["mode"] == 'editor' )
427 + if (! $args["mode"] == 'editor' )
452 428 return;
453 -
454 - $data = apply_filters("mb-save-fields", array(), array() ); // load defaults
455 - $data["id"] = 0;
456 - do_action('mb-data-load', $data);
429 +
430 + $this->clear();
431 +
432 + $this->data["id"] = 0;
433 + //do_action('mb-data-load', $data);
457 434 }
458 -
459 - $mode = (isset($args["mode"])) ? $args["mode"] : "normal";
435 +
436 + $mode = (isset($args["mode"])) ? $args["mode"] : "normal";
460 437 switch($mode)
461 438 {
462 - case "preview":
439 + case "preview":
463 440 $preview = true;
464 441 $compile = false;
465 - break;
466 - case "editor":
467 - $preview = true;
468 - $compile = true;
469 - // editor is both compile and preview.
442 + break;
443 + case "editor":
444 + $preview = true;
445 + $compile = true;
446 + // editor is both compile and preview.
470 447 break;
471 448 break;
472 - case "normal":
473 - $preview = false;
449 + case "normal":
450 + $preview = false;
474 451 $compile = false;
475 452 break;
476 453 }
477 -
478 454
455 +
456 + // Apply filters for general data override
457 +
458 + $this->data = apply_filters('mb/button/data_before_display', $this->data, $mode, array('preview' => $preview, 'compile' => $compile) ); // hooks
459 +
460 +
461 +
479 462 if ( $this->load_css == "element" || $args["preview_part"] != "full" || $args["compile"] == true) { // if css output is on element, for to compile - otherwise inline styles will not be loaded.
480 463 $compile = true;
481 464
482 465 }
483 - else
466 + else
484 467 $compile = false;
485 468
469 + // reload the data into the blocks, might have been altered by shortcode, filters etc.
486 470
487 - $domObj = $this->parse_button($mode);
488 -
471 + foreach($this->blocks as $block)
472 + {
473 + $block->set($this->data);
474 + }
475 +
476 + $domObj = $this->parse_button($mode);
477 +
489 478 maxUtils::startTime('button-parse-css-'. $this->id);
490 - $this->parse_css($mode, $compile);
479 + $this->parse_css($mode, $compile);
491 480 maxUtils::endTime('button-parse-css-'. $this->id);
492 -
493 - if (! $preview) // no js on previews
494 - $this->parse_js($mode);
495 -
481 +
482 + if (! $preview) // no js on previews
483 + $this->parse_js($mode);
484 +
496 485 if ($preview) // mark it preview
497 486 {
498 487
499 488 $domObj->find('a',0)->class .= ' maxbutton-preview';
500 489 }
501 -
490 +
502 491 if ($preview && $args["preview_part"] != 'full')
503 492 {
504 -
493 +
505 494 if ($args["preview_part"] != 'normal')
506 495 {
507 - $domObj->find('a',0)->class .= ' hover';
496 + $domObj->find('a',0)->class .= ' hover';
508 497 $domObj = $cssParser->outputInline($domObj,'hover');
509 498
510 499 }
511 500 else
512 501 {
513 - $domObj->find('a',0)->class .= ' normal';
502 + $domObj->find('a',0)->class .= ' normal';
514 503 $domObj = $cssParser->outputInline($domObj);
515 504 }
516 505
517 506 }
518 - elseif ($this->load_css == 'footer')
507 + elseif ($this->load_css == 'footer')
519 508 {
520 - $css = $this->display_css(false, false);
521 - do_action('mb-footer',$this->id, $css);
522 -
509 + $css = $this->display_css(false, false);
510 + do_action('mb-footer',$this->id, $css);
511 +
523 512 if (! $preview)
524 513 {
525 514 $js = $this->display_js(false, true);
526 515 do_action('mb-footer', $this->document_id, $js, 'js');
527 516 }
528 - } elseif ($this->load_css == 'inline')
517 + } elseif ($this->load_css == 'inline')
529 518 {
530 - if ($args["echo"])
519 + if ($args["echo"])
531 520 $this->display_css();
532 521 else
533 522 $output .= $this->display_css(false);
534 523 }
535 - elseif ($this->load_css == 'element') // not possible to load both normal and hover to an element.
524 + elseif ($this->load_css == 'element') // not possible to load both normal and hover to an element.
536 525 {
537 - $domObj->find('a',0)->class .= ' normal';
538 - $domObj = $this->cssParser->outputInline($domObj);
539 - //$this->get_element_css($domObj, 'normal');
526 + $domObj->find('a',0)->class .= ' normal';
527 + $domObj = $this->cssParser->outputInline($domObj);
528 + //$this->get_element_css($domObj, 'normal');
540 529 }
541 -
542 530
531 +
543 532 $output .= $domObj->save();
544 -
545 - $output = apply_filters('mb-before-button-output', $output);
546 - maxButtons::buttonDone(array("button_id" => $this->id, "document_id" => $this->document_id) );
547 -
533 +
534 + $output = apply_filters('mb-before-button-output', $output);
535 + maxButtons::buttonDone(array("button_id" => $this->id, "document_id" => $this->document_id) );
536 +
548 537 maxUtils::endTime('button-display-'. $this->id);
549 -
538 +
550 539 if ($args["echo"])
551 - echo $output;
540 + echo $output;
552 541 else
553 - return $output;
542 + return $output;
554 543
555 - }
544 + }
556 545 /* Function used to map field id's to display for Frontend Javascript
557 -
558 - This function bundles all defined fields into a json encoded variable. This is used for the frontend javascript functions in the
546 +
547 + This function bundles all defined fields into a json encoded variable. This is used for the frontend javascript functions in the
559 548 administrator area like colorpickers and real-time updating of the button preview
560 549 */
561 550 public function display_field_map()
562 551 {
563 - $map = array();
564 - $map = apply_filters("mb-field-map",$map);
565 -
552 + $map = array();
553 + foreach($this->blocks as $block)
554 + {
555 + $map = $block->map_fields($map);
556 + }
557 + //$map = apply_filters("mb-field-map",$map);
566 558
567 - echo "<script language='javascript'>";
559 +
560 + echo "<script language='javascript'>";
568 561 echo "var buttonFieldMap = '" . json_encode($map) . "';" ;
569 562 echo "</script>";
570 -
563 +
571 564 }
572 -
573 - /* Write parsed CSS to output.
574 - @param echo Default true. When true, outputs directly, otherwise returns output string
565 +
566 + /* Write parsed CSS to output.
567 + @param echo Default true. When true, outputs directly, otherwise returns output string
575 568 @param style_tag Default true. When true, outputs a html <style> tags around the output.
576 569 */
577 570 public function display_css($echo = true, $style_tag = true)
578 571 {
579 - $output = '';
580 -
572 + $output = '';
573 +
581 574 if ($style_tag)
582 575 $output .= "<style type='text/css'>";
583 -
576 +
584 577 $output .= $this->parsed_css;
585 -
578 +
586 579 if ($style_tag)
587 580 $output .= "</style>";
588 -
589 -
590 - if ($echo) echo $output;
591 - else return $output;
592 -
581 +
582 +
583 + if ($echo) echo $output;
584 + else return $output;
585 +
593 586 }
594 -
587 +
595 588 /* Output Parsed Javascripting */
596 589 public function display_js($echo = true, $tag = true)
597 590 {
598 591 $output = '';
599 592
600 - if (count($this->button_js) == 0)
593 + if (count($this->button_js) == 0)
601 594 return; // no output, holiday
602 -
603 - if ($tag)
595 +
596 + if ($tag)
604 597 {
605 - $output .= "<script type='text/javascript'> ";
598 + $output .= "<script type='text/javascript'> ";
606 599 $output .= " if (typeof maxButton" . $this->document_id . " == 'undefined') { ";
607 600 $output .= " function maxButton" . $this->document_id . "() { ";
608 601 }
609 -
610 - foreach($this->button_js as $index => $code)
602 +
603 + foreach($this->button_js as $index => $code)
611 604 {
612 - $output .= $code;
613 -
605 + $output .= $code;
606 +
614 607 }
615 -
616 - if ($tag)
608 +
609 + if ($tag)
617 610 {
618 - $output .= " }
619 - }
620 - window.onload = maxButton" . $this->document_id . "();
621 - </script> ";
611 + $output .= " }
612 + }
613 + window.onload = maxButton" . $this->document_id . "();
614 + </script> ";
622 615 }
623 616
624 - if ($echo) echo $output;
625 - else return $output;
617 + if ($echo) echo $output;
618 + else return $output;
626 619 }
627 620
628 621
629 - /* Makes a copy of the current buttons.
630 -
631 - The button to be copied -must- be loaded and set
622 + /* Makes a copy of the current buttons.
623 +
624 + The button to be copied -must- be loaded and set
632 625 */
633 - function copy()
634 - {
635 - $this->id = 0;
626 + function copy()
627 + {
628 + $this->id = 0;
636 629 $data = $this->data;
637 630 $data["name"] = $this->name;
638 -
639 - return $this->update($data);
631 +
632 + return $this->update($data);
640 633 }
641 634 /* Change the publication status of the button.
642 -
635 +
643 636 */
644 - function setStatus($status = "publish")
637 + function setStatus($status = "publish")
645 638 {
646 - $data = $this->data;
647 - $data["status"] = sanitize_text_field($status);
639 + $data = $this->data;
640 + $data["status"] = sanitize_text_field($status);
648 641
649 - return $this->update($data);
650 -
651 - }
652 - /* Remove the button from database */
653 - public function delete($id)
642 + return $this->update($data);
643 +
644 + }
645 + /* Remove the button from database */
646 + public function delete($id)
654 647 {
655 648 global $wpdb;
656 649 $wpdb->query($wpdb->prepare("DELETE FROM " . maxUtils::get_table_name() . " WHERE id = %d", $id));
657 650 }
658 -
659 - /* Save changes to the button
660 -
661 - Updates or saves the button. Existing buttons must load their data and be set -first- or lose all not-passed data.
662 -
651 +
652 + /* Save changes to the button
653 +
654 + Updates or saves the button. Existing buttons must load their data and be set -first- or lose all not-passed data.
655 +
663 656 @param post Post data in field - value format (flat $_POST array)
664 - @param boolean savedb if false do not save to database
657 + @param boolean savedb if false do not save to database
665 658 */
666 659 public function save($post, $savedb = true)
667 660 {
668 661 $post = stripslashes_deep($post); // don't multiply slashes please.
669 - $data = apply_filters('mb-save-fields',$this->data, $post);
670 -
671 - if (! $savedb ) return $data;
672 - return $this->update($data); // save to db.
673 -
662 + //$data = apply_filters('mb-save-fields',$this->data, $post);
663 + $data = $this->data;
664 +
665 + foreach($this->blocks as $block)
666 + {
667 + $data = $block->save_fields($data, $post);
668 + }
669 +
670 + if (! $savedb ) return $data;
671 + return $this->update($data); // save to db.
672 +
674 673 }
675 -
674 +
676 675 /* Updates the button data to the database. Adds a button if it doesn't exist */
677 - public function update($data)
678 - {
679 - global $wpdb;
680 - $return = false;
681 -
682 - $fields = array();
676 + public function update($data)
677 + {
678 + global $wpdb;
679 + $return = false;
680 +
681 + $fields = array();
683 682 foreach($this->blocks as $block)
684 683 {
685 - if (isset($data[$block]))
684 + $block_name = $block->get_name();
685 +
686 + if (isset($data[$block_name]))
686 687 {
687 - $blockData = $data[$block];
688 - $fields[$block] = json_encode($blockData);
688 + $blockData = $data[$block_name];
689 + $fields[$block_name] = json_encode($blockData);
689 690 }
690 - }
691 - if (isset($data["name"])) { // other fields.
692 - $fields["name"] = $data["name"];
691 + }
692 + if (isset($data["name"])) { // other fields.
693 + $fields["name"] = $data["name"];
693 694 }
694 695 if (isset($data["status"])) {
695 - $fields["status"] = $data["status"];
696 + $fields["status"] = $data["status"];
696 697 }
697 -
698 -
698 +
699 +
699 700 $where = array('id' => $this->id );
700 - if ($this->id > 0)
701 + if ($this->id > 0)
701 702 {
702 703 $where = array('id' => $this->id);
703 704 $where_format = array('%d');
704 705 $result = $wpdb->update(maxUtils::get_table_name(), $fields, $where, null, $where_format);
@@ -705,115 +706,116 @@
705 706 $return = true;
706 707 }
707 708 else
708 709 {
709 - $fields['created'] = current_time('mysql',1);
710 + $fields['created'] = current_time('mysql',1);
710 711
711 712 $result = $wpdb->insert(maxUtils::get_table_name(), $fields);
712 713 $id = $wpdb->insert_id;
713 714
714 715 $this->id = $id;
715 - $return = $id;
716 -
716 + $return = $id;
717 +
717 718 }
718 -
719 -
719 +
720 +
720 721 if ($result === false)
721 722 {
722 723 $error = "Database error " . $wpdb->last_error;
723 - MB()->add_notice('error', $error);
724 - $install = MB()->getClass("install");
724 + MB()->add_notice('error', $error);
725 + $install = MB()->getClass("install");
725 726 $install::create_database_table(); // run dbdelta to try and fix.
726 -
727 +
727 728 }
728 -
729 - // update the cache
730 - $this->cache = ''; // empty cache
729 +
730 + // update the cache
731 + $this->cache = ''; // empty cache
731 732 $result = $this->set($this->id); // set the newest values
732 -
733 - if (! $result ) return false;
734 -
733 +
734 + if (! $result ) return false;
735 +
735 736 $this->display(array("echo" => false, "load_css" => "element")); // do display routing to compile.
736 - $css = $this->parsed_css;
737 - $this->update_cache($css);
737 + $css = $this->parsed_css;
738 + $this->update_cache($css);
738 739
739 740 return $return;
740 741 }
741 -
742 +
742 743 /* Updates the CSS cache. */
743 744 public function update_cache($css)
744 745 {
745 746 $return = false;
746 - global $wpdb;
747 -
748 - if ($this->id > 0)
747 + global $wpdb;
748 +
749 + if ($this->id > 0)
749 750 {
750 - $fields = array("cache" => $css);
751 + $fields = array("cache" => $css);
751 752 $where = array('id' => $this->id);
752 753 $where_format = array('%d');
753 754 $wpdb->update(maxUtils::get_table_name(), $fields, $where, null, $where_format);
754 755 $return = true;
755 -
756 +
756 757 }
757 - return $return;
758 + return $return;
758 759 }
759 -
760 +
760 761 // Resets all of the button caches.
761 762 public function reset_cache()
762 763 {
763 764 global $wpdb;
764 - $fields = array("cache" => null);
765 + $fields = array("cache" => null);
765 766 $where = array(1 => 1);
766 767 //$where_format = array('%d');
767 - $sql = "UPDATE " . maxUtils::get_table_name() . " SET cache = NULL ";
768 + $sql = "UPDATE " . maxUtils::get_table_name() . " SET cache = NULL ";
768 769 $wpdb->query($sql);
769 -
770 +
770 771 }
771 772
772 -
773 +
773 774 /* Display button via shortcode
774 -
775 +
775 776 Function that accepts WP shortcode arguments and displays or returns a button
776 -
777 - @param $atts array Shortcode Atts
777 +
778 + @param $atts array Shortcode Atts
778 779 @return string HTML presentation of button
779 -
780 +
780 781 */
781 782 public function shortcode($atts)
782 - {
783 + {
783 784 extract(shortcode_atts(array(
784 785 'id' => '',
785 786 'name' => '',
786 787 'text' => '',
787 788 'url' => '',
789 + 'linktitle' => '',
788 790 'window' => '',
789 791 'nofollow' => '',
790 - 'nocache' => false,
791 - 'style' => 'footer',
792 + 'nocache' => false,
793 + 'style' => 'footer',
792 794 'exclude' => ''
793 795
794 - ), $atts));
796 + ), $atts));
795 797
796 - $button_id = $id;
798 + $button_id = $id;
797 799 $button_name = $name;
798 -
799 - if ($button_id > 0)
800 - $result = $this->set($button_id);
801 - elseif ($button_name != '')
802 - $result = $this->set(0, $button_name);
800 +
801 + if ($button_id > 0)
802 + $result = $this->set($button_id);
803 + elseif ($button_name != '')
804 + $result = $this->set(0, $button_name);
803 805 else return; // no button id / name
804 -
806 +
805 807 /* Shortcode cache control
806 -
808 +
807 809 If true the button CSS will be recompiled again. If false the plugin will check the cache for CSS declarations. Set to true if anything is interrupting the caching mechanism. Please note, recompiling causes some load times!
808 -
809 - @param boolean $nocache True / False
810 - */
811 - $compile = apply_filters("mb/shortcode/nocache", $nocache);
812 810
813 - if (! $result)
811 + @param boolean $nocache True / False
812 + */
813 + $compile = apply_filters("mb/shortcode/nocache", $nocache);
814 +
815 + if (! $result)
814 816 return; // shortcode doesn't exist
815 -
817 +
816 818 // If we're not in the admin and the button is in the trash, just return nothing
817 819 if (!is_admin() && $this->status == 'trash') {
818 820 return '';
819 821 }
@@ -819,9 +821,9 @@
819 821 }
820 822 // Check to handle excludes
821 823 if ("{$exclude}" != '') {
822 824 global $post;
823 -
825 +
824 826 // Don't render the button if excluded from the current post/page
825 827 $exclude = explode(',', "{$exclude}");
826 828 if (in_array($post->ID, $exclude)) {
827 829 return '';
@@ -826,76 +828,88 @@
826 828 if (in_array($post->ID, $exclude)) {
827 829 return '';
828 830 }
829 831 }
830 -
832 +
831 833 // Override shortcode options comparing to default button data.
832 - $overrides = false;
833 - if ($text != '')
834 - {
835 - $this->data["text"]["text"] = $text;
834 + $overrides = false;
835 + if ($text != '')
836 + {
837 + $this->data["text"]["text"] = $text;
836 838 $overrides = true;
837 - }
838 - if ($url != '')
839 + }
840 + if ($url != '')
839 841 {
840 842
841 - $this->data["basic"]["url"] = $url;
843 + $this->data["basic"]["url"] = $url;
842 844 //$compile = true; // css change forces recompile
843 845 $overrides = true;
844 846 }
845 - if ($window != '' && $window =='new')
847 + if ($window != '' && $window =='new')
846 848 {
847 - $this->data["basic"]["new_window"] = 1;
849 + $this->data["basic"]["new_window"] = 1;
848 850 $overrides = true;
849 851 }
850 - if ($nofollow != '' && $nofollow == 'true')
852 + elseif ($window != '' && $window == 'same')
851 853 {
852 - $this->data["basic"]["nofollow"] = 1;
854 + $this->data["basic"]["new_window"] = 0;
853 855 $overrides = true;
854 856 }
855 857
858 + if ($nofollow != '' && $nofollow == 'true')
859 + {
860 + $this->data["basic"]["nofollow"] = 1;
861 + $overrides = true;
862 + }
863 +
864 + if ($linktitle != '')
865 + {
866 + $this->data['basic']['link_title'] = $linktitle;
867 + $overrides = true;
868 + }
869 +
856 870 switch($style)
857 871 {
858 - case "inline":
859 - $load_css = 'inline';
872 + case "inline":
873 + $load_css = 'inline';
860 874 break;
861 875 default:
862 - $load_css = 'footer';
876 + $load_css = 'footer';
863 877 break;
864 - }
878 + }
865 879
866 880 // allow for more flexible changes and data manipulation.
867 881 $data = $this->data;
868 - $this->data = $this->shortcode_overrides($this->data, $atts);
869 - $this->data = apply_filters('mb/shortcode/data', $this->data, $atts);
870 -
871 - if ($data !== $this->data)
882 + $this->data = $this->shortcode_overrides($this->data, $atts);
883 + $this->data = apply_filters('mb/shortcode/data', $this->data, $atts);
884 +
885 +/* if ($data !== $this->data)
872 886 {
873 - $overrides = true;
887 + $overrides = true;
874 888 }
875 -
876 - if ($overrides)
889 +*/
890 +/* if ($overrides)
877 891 {
878 892 do_action('mb-data-load', $this->data);
879 893 }
894 +*/
880 895 // if there are no reasons not to display; display
881 - $args = array("echo" => false,
882 - "load_css" => $load_css,
883 - "compile" => $compile,
896 + $args = array("echo" => false,
897 + "load_css" => $load_css,
898 + "compile" => $compile,
884 899 );
885 - $args = apply_filters('mb_shortcode_display_args', $args);
886 -
887 -
900 + $args = apply_filters('mb_shortcode_display_args', $args);
901 +
902 +
888 903 $output = $this->display($args);
889 -
904 +
890 905 return $output;
891 -
906 +
892 907 }
893 -
894 -
908 +
909 +
895 910 public function shortcode_overrides($data, $atts)
896 911 {
897 912 return $data;
898 913 }
899 -
914 +
900 915 } // class
901 -