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 +113 -106 7.136.28 View file →
@@ -2,8 +2,9 @@
2 2 namespace MaxButtons;
3 3 defined('ABSPATH') or die('No direct access permitted');
4 4
5 5 /* Datamodel and base functionality for a button
6 +
6 7 */
7 8
8 9 use \simple_html_dom as simple_html_dom;
9 10
@@ -14,9 +15,8 @@
14 15 protected $name = '';
15 16 protected $status = '';
16 17 protected $description = '';
17 18 protected $cache = '';
18 - protected $updated = 0;
19 19
20 20 protected $button_loaded = false;
21 21
22 22 protected $data = array();
@@ -45,9 +45,9 @@
45 45 // the parser
46 46
47 47 // get all files from blocks map
48 48
49 - // get all blocks classes, do init on the blocks. Init should not load anything big.
49 + // get all blocks via apply filters, do init. Init should not load anything big.
50 50 $this->loadBlockClasses();
51 51
52 52 }
53 53
@@ -59,8 +59,9 @@
59 59
60 60 // set blocks to the 'block name' that survived.
61 61 maxUtils::addTime("Load Block classes");
62 62
63 + //$classes = apply_filters("mb_blockclassesload", $classes);
63 64 $class_array = maxBlocks::getBlockClasses();
64 65 $classes = array_map(maxUtils::namespaceit('maxUtils::array_namespace'), $class_array );
65 66
66 67 foreach($classes as $block => $class)
@@ -69,15 +70,16 @@
69 70
70 71 $this->blocks[] = $block;
71 72 if (is_admin())
72 73 {
73 - // used for getValue() operators in the Admin Section
74 -
75 74 maxBlocks::add($block); // block collection.
76 75 }
77 76 }
78 77
78 +
79 79 $this->clear(); // init
80 + do_action("mb\blockclasses", $class_array);
81 +
80 82 }
81 83
82 84 /** Simple function to retrieve loaded blocks - * Used by install class */
83 85 public function getBlocks()
@@ -109,16 +111,12 @@
109 111 } else {
110 112 $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . maxUtils::get_table_name() . " WHERE id = %d and status ='%s'", $id, $status), ARRAY_A);
111 113 }
112 114
113 - if (! is_array($row)) // no db row results in null / false
115 + if (count($row) == 0)
114 116 {
115 117 return false;
116 118 }
117 - elseif (count($row) == 0)
118 - {
119 - return false;
120 - }
121 119
122 120 /* Take the id from the query, otherwise ( when button shortcode called by name ) id might not be present properly' */
123 121 $button_id = $row["id"];
124 122 maxButtons::buttonLoad(array("button_id" => $button_id)); // central registration
@@ -138,9 +136,8 @@
138 136 $this->button_css = array();
139 137 $this->button_js = array();
140 138 $this->data = array('id' => 0);
141 139 $this->data = $this->save(array(),false);
142 - $this->updated = 0;
143 140
144 141 $this->cache = '';
145 142 $this->button_loaded = false;
146 143
@@ -147,9 +144,9 @@
147 144 foreach($this->blocks as $block)
148 145 {
149 146 $block->set($this->data); // reset blocks
150 147 }
151 - maxBlocks::setData($this->data);
148 +
152 149 }
153 150
154 151 function setupData($data)
155 152 {
@@ -179,17 +176,16 @@
179 176 $this->data["id"] = $this->id; // needed for certain blocks, to be button aware.
180 177 $this->data["document_id"] = $this->document_id; // bound to JS and others.
181 178 $this->name = $data["name"];
182 179 $this->status = $data["status"];
183 - $this->updated = isset($data['updated']) ? $data['updated'] : 0;
184 180 $this->description = $this->data["basic"]["description"];
185 181
182 + //do_action('mb-data-load', $this->data);
186 183 foreach($this->blocks as $block)
187 184 {
188 185 $block->set($this->data);
189 186 }
190 187
191 -
192 188 maxBlocks::setData($this->data);
193 189
194 190
195 191 return true;
@@ -201,46 +197,36 @@
201 197 return $this->data;
202 198
203 199 }
204 200
205 - public function getID()
201 + function getID()
206 202 {
207 203 return $this->id;
208 204 }
209 -
210 - public function getDocumentID()
205 + function getDocumentID()
211 206 {
212 207 return $this->document_id;
213 208 }
214 - public function getName()
209 + function getName()
215 210 {
216 211 return $this->name;
217 212 }
218 -
219 - public function getDescription()
213 + function getDescription()
220 214 {
221 215 return $this->description;
222 216 }
223 217
224 - public function getStatus()
218 + function getStatus()
225 219 {
226 220 return $this->status;
227 221 }
228 222
229 - public function getUpdated($forDisplay = true)
223 + function getParsedCSS()
230 224 {
231 - if ($forDisplay)
232 - return date_i18n( get_option( 'date_format' ), strtotime($this->updated) );
233 - else
234 - return strtotime($this->updated);
235 - }
236 -
237 - public function getParsedCSS()
238 - {
239 225 return $this->parsed_css;
240 226 }
241 227
242 - public function getCSSArray()
228 + function getCSSArray()
243 229 {
244 230
245 231 return $this->button_css;
246 232 }
@@ -273,8 +259,21 @@
273 259
274 260 }
275 261 }
276 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 + */
269 + function reloadData()
270 + {
271 + exit('reload Data - do not use');
272 + //do_action('mb-data-load', $this->data);
273 +
274 + }
275 +
277 276 /* Parse CSS from the elements
278 277
279 278 @param string $mode [normal,preview,editor] - The view needed.
280 279 @param string $forceCompile Recompile the CSS in any case
@@ -303,19 +302,20 @@
303 302 foreach($this->blocks as $block)
304 303 {
305 304 $css = $block->parse_css($css, $mode);
306 305 }
306 + //$css = apply_filters('mb-css-blocks', $css, $mode);
307 307
308 308 /* Filter the raw CSS array before compile
309 309
310 - This filters passes an array with all CSS elements before compile time. This should be CSS elements that can be understood by the CSS parser.
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 311 @since 4.20
312 312 @param $css CSS Array - split by element and pseudo (normal/hover)
313 +
313 314 */
314 315
315 316 $css = apply_filters('mb/button/rawcss', $css, $mode);
316 317
317 -//echo "<PRE>"; print_r($css); echo "</PRE>";
318 318 $this->button_css = $css;
319 319
320 320 $css = $this->getCSSParser()->parse($this->button_css);
321 321 $css = apply_filters('mb/button/compiledcss', $css, $mode); // the final result.
@@ -362,9 +362,8 @@
362 362 $name = $this->name;
363 363 // non-latin breaks CSS / ID's - so move to latin.
364 364 $name = maxUtils::translit($name);
365 365 $name = sanitize_title($name);
366 - $name = str_replace('%', '', $name);
367 366
368 367 $classes = array("maxbutton-" . $this->id,
369 368 "maxbutton");
370 369
@@ -371,8 +370,9 @@
371 370
372 371 if ($name != '')
373 372 $classes[] = "maxbutton-" . $name;
374 373
374 + $classes = apply_filters('mb-mainclasses', $classes);
375 375 $classes = implode(' ', $classes);
376 376
377 377 $domObj = new simple_html_dom();
378 378 $domObj->load('<a class="' . $classes . '"></a>');
@@ -396,20 +396,11 @@
396 396 {
397 397 foreach($this->blocks as $block)
398 398 {
399 399 $block->admin_fields();
400 - $block->display_fields();
401 - }
402 400
403 - }
404 -
405 - protected function post_parse_button($domObj, $mode)
406 - {
407 - foreach($this->blocks as $block)
408 - {
409 - $domObj = $block->post_parse_button($domObj, $mode);
410 - $domObj->load($domObj->save());
411 401 }
402 + //do_action('mb-admin-fields' );
412 403
413 404 }
414 405
415 406 /* Display the button */
@@ -450,9 +441,9 @@
450 441 $compile = false;
451 442 break;
452 443 case "editor":
453 444 $preview = true;
454 - $compile = true;
445 + $compile = true;
455 446 // editor is both compile and preview.
456 447 break;
457 448 break;
458 449 case "normal":
@@ -462,10 +453,13 @@
462 453 }
463 454
464 455
465 456 // Apply filters for general data override
457 +
466 458 $this->data = apply_filters('mb/button/data_before_display', $this->data, $mode, array('preview' => $preview, 'compile' => $compile) ); // hooks
467 459
460 +
461 +
468 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.
469 463 $compile = true;
470 464
471 465 }
@@ -478,21 +472,14 @@
478 472 {
479 473 $block->set($this->data);
480 474 }
481 475
482 - // create the button
483 476 $domObj = $this->parse_button($mode);
484 477
485 - // create CSS
486 478 maxUtils::startTime('button-parse-css-'. $this->id);
487 479 $this->parse_css($mode, $compile);
488 480 maxUtils::endTime('button-parse-css-'. $this->id);
489 481
490 -
491 - // operation after creating CSS model. Mainly adding extra classes
492 - $this->post_parse_button($domObj, $mode);
493 -
494 -
495 482 if (! $preview) // no js on previews
496 483 $this->parse_js($mode);
497 484
498 485 if ($preview) // mark it preview
@@ -519,9 +506,8 @@
519 506 }
520 507 elseif ($this->load_css == 'footer')
521 508 {
522 509 $css = $this->display_css(false, false);
523 -
524 510 do_action('mb-footer',$this->id, $css);
525 511
526 512 if (! $preview)
527 513 {
@@ -538,8 +524,9 @@
538 524 elseif ($this->load_css == 'element') // not possible to load both normal and hover to an element.
539 525 {
540 526 $domObj->find('a',0)->class .= ' normal';
541 527 $domObj = $this->cssParser->outputInline($domObj);
528 + //$this->get_element_css($domObj, 'normal');
542 529 }
543 530
544 531
545 532 $output .= $domObj->save();
@@ -566,9 +553,11 @@
566 553 foreach($this->blocks as $block)
567 554 {
568 555 $map = $block->map_fields($map);
569 556 }
557 + //$map = apply_filters("mb-field-map",$map);
570 558
559 +
571 560 echo "<script language='javascript'>";
572 561 echo "var buttonFieldMap = '" . json_encode($map) . "';" ;
573 562 echo "</script>";
574 563
@@ -652,11 +641,9 @@
652 641
653 642 return $this->update($data);
654 643
655 644 }
656 - /* Remove the button from database
657 - * @todo implement, this function could be safer.
658 - */
645 + /* Remove the button from database */
659 646 public function delete($id)
660 647 {
661 648 global $wpdb;
662 649 $wpdb->query($wpdb->prepare("DELETE FROM " . maxUtils::get_table_name() . " WHERE id = %d", $id));
@@ -671,8 +658,9 @@
671 658 */
672 659 public function save($post, $savedb = true)
673 660 {
674 661 $post = stripslashes_deep($post); // don't multiply slashes please.
662 + //$data = apply_filters('mb-save-fields',$this->data, $post);
675 663 $data = $this->data;
676 664
677 665 foreach($this->blocks as $block)
678 666 {
@@ -728,9 +716,8 @@
728 716 $return = $id;
729 717
730 718 }
731 719
732 - do_action('mb/button/save', $this->id);
733 720
734 721 if ($result === false)
735 722 {
736 723 $error = "Database error " . $wpdb->last_error;
@@ -760,9 +747,9 @@
760 747 global $wpdb;
761 748
762 749 if ($this->id > 0)
763 750 {
764 - $fields = array("cache" => $css, 'updated' => $this->updated); // try not to update timestamp on recache.
751 + $fields = array("cache" => $css);
765 752 $where = array('id' => $this->id);
766 753 $where_format = array('%d');
767 754 $wpdb->update(maxUtils::get_table_name(), $fields, $where, null, $where_format);
768 755 $return = true;
@@ -774,9 +761,9 @@
774 761 // Resets all of the button caches.
775 762 public function reset_cache()
776 763 {
777 764 global $wpdb;
778 - $fields = array("cache" => null, 'updated' => 'updated');
765 + $fields = array("cache" => null);
779 766 $where = array(1 => 1);
780 767 //$where_format = array('%d');
781 768 $sql = "UPDATE " . maxUtils::get_table_name() . " SET cache = NULL ";
782 769 $wpdb->query($sql);
@@ -791,32 +778,32 @@
791 778 @param $atts array Shortcode Atts
792 779 @return string HTML presentation of button
793 780
794 781 */
795 - public function shortcode($button_atts)
782 + public function shortcode($atts)
796 783 {
797 - $atts = shortcode_atts(array(
798 - 'id' => null,
799 - 'name' => null,
800 - 'text' => null,
801 - 'url' => null,
802 - 'linktitle' => null,
803 - 'window' => null,
804 - 'nofollow' => null,
805 - 'nocache' => null,
806 - 'extraclass' => null,
784 + extract(shortcode_atts(array(
785 + 'id' => '',
786 + 'name' => '',
787 + 'text' => '',
788 + 'url' => '',
789 + 'linktitle' => '',
790 + 'window' => '',
791 + 'nofollow' => '',
792 + 'nocache' => false,
807 793 'style' => 'footer',
794 + 'exclude' => ''
808 795
809 - ), $button_atts, 'maxbutton');
796 + ), $atts));
810 797
811 - $button_id = $atts['id'];
812 - $button_name = $atts['name'];
798 + $button_id = $id;
799 + $button_name = $name;
813 800
814 - if (! is_null($button_id) && $button_id > 0)
801 + if ($button_id > 0)
815 802 $result = $this->set($button_id);
816 - elseif (! is_null($button_name))
803 + elseif ($button_name != '')
817 804 $result = $this->set(0, $button_name);
818 - else return; // no button id or name given
805 + else return; // no button id / name
819 806
820 807 /* Shortcode cache control
821 808
822 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!
@@ -822,60 +809,67 @@
822 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!
823 810
824 811 @param boolean $nocache True / False
825 812 */
826 - $compile = apply_filters("mb/shortcode/nocache", $atts['nocache'] );
813 + $compile = apply_filters("mb/shortcode/nocache", $nocache);
827 814
828 815 if (! $result)
829 - return; // button doesn't exist
816 + return; // shortcode doesn't exist
830 817
831 818 // If we're not in the admin and the button is in the trash, just return nothing
832 - if (!is_admin() && $this->status == 'trash') {
833 - return;
819 + if (!is_admin() && $this->status == 'trash') {
820 + return '';
821 + }
822 + // Check to handle excludes
823 + if ("{$exclude}" != '') {
824 + global $post;
825 +
826 + // Don't render the button if excluded from the current post/page
827 + $exclude = explode(',', "{$exclude}");
828 + if (in_array($post->ID, $exclude)) {
829 + return '';
830 + }
834 831 }
835 832
836 833 // Override shortcode options comparing to default button data.
837 - //$overrides = false;
838 -
839 - if (! is_null($atts['text']) && $atts['text'] !== '')
834 + $overrides = false;
835 + if ($text != '')
840 836 {
841 - $this->data["text"]["text"] = $atts['text'];
837 + $this->data["text"]["text"] = $text;
838 + $overrides = true;
842 839 }
840 + if ($url != '')
841 + {
843 842
844 - if (! is_null($atts['url']) && $atts['url'] !== '')
843 + $this->data["basic"]["url"] = $url;
844 + //$compile = true; // css change forces recompile
845 + $overrides = true;
846 + }
847 + if ($window != '' && $window =='new')
845 848 {
846 - $this->data["basic"]["url"] = $atts['url'];
849 + $this->data["basic"]["new_window"] = 1;
850 + $overrides = true;
847 851 }
848 -
849 - if (! is_null($atts['window']) )
852 + elseif ($window != '' && $window == 'same')
850 853 {
851 - if ($atts['window'] == 'new' )
852 - $this->data["basic"]["new_window"] = 1;
853 - elseif ($atts['window'] == 'same')
854 - $this->data["basic"]["new_window"] = 0;
854 + $this->data["basic"]["new_window"] = 0;
855 + $overrides = true;
855 856 }
856 857
857 - if (! is_null($atts['nofollow']) )
858 + if ($nofollow != '' && $nofollow == 'true')
858 859 {
859 - if ($atts['nofollow'] == 'true')
860 - $this->data["basic"]["nofollow"] = 1;
861 - elseif ($atts['nofollow'] == 'false')
862 - $this->data["basic"]["nofollow"] = 0;
860 + $this->data["basic"]["nofollow"] = 1;
861 + $overrides = true;
863 862 }
864 863
865 - if (! is_null($atts['linktitle']) )
864 + if ($linktitle != '')
866 865 {
867 - $this->data['basic']['link_title'] = $atts['linktitle'];
866 + $this->data['basic']['link_title'] = $linktitle;
867 + $overrides = true;
868 868 }
869 869
870 - if (! is_null($atts['extraclass']))
870 + switch($style)
871 871 {
872 -
873 - $this->data['advanced']['extra_classes'] .= ' ' . $atts['extraclass'];
874 - }
875 -
876 - switch($atts['style'])
877 - {
878 872 case "inline":
879 873 $load_css = 'inline';
880 874 break;
881 875 default:
@@ -883,11 +877,22 @@
883 877 break;
884 878 }
885 879
886 880 // allow for more flexible changes and data manipulation.
887 - $this->data = $this->shortcode_overrides($this->data, $button_atts);
881 + $data = $this->data;
882 + $this->data = $this->shortcode_overrides($this->data, $atts);
888 883 $this->data = apply_filters('mb/shortcode/data', $this->data, $atts);
889 884
885 +/* if ($data !== $this->data)
886 + {
887 + $overrides = true;
888 + }
889 +*/
890 +/* if ($overrides)
891 + {
892 + do_action('mb-data-load', $this->data);
893 + }
894 +*/
890 895 // if there are no reasons not to display; display
891 896 $args = array("echo" => false,
892 897 "load_css" => $load_css,
893 898 "compile" => $compile,
@@ -893,11 +898,13 @@
893 898 "compile" => $compile,
894 899 );
895 900 $args = apply_filters('mb_shortcode_display_args', $args);
896 901
902 +
897 903 $output = $this->display($args);
898 904
899 905 return $output;
906 +
900 907 }
901 908
902 909
903 910 public function shortcode_overrides($data, $atts)