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