Ticket #3563: buttonsnap.php

File buttonsnap.php, 14.2 kB (added by ryan, 2 years ago)

buttonsnap plugin

Line 
1 <?php
2 /*******************************************************************************
3 BUTTONSNAP CLASS LIBRARY By Owen Winkler
4 http://asymptomatic.net
5 WordPress Downloads are at http://redalt.com/downloads
6 Version: 1.3.1
7 *******************************************************************************/
8
9 if (!class_exists('buttonsnap')) :
10 class buttonsnap
11 {
12     var $script_output = false;
13     var $buttons = array('post'=>array(),'page'=>array(),'any'=>array());
14     var $markers = array();
15     
16     function sink_hooks()
17     {
18         add_action('edit_form_advanced', array(&$this, 'edit_form_advanced'));
19         add_action('edit_page_form', array(&$this, 'edit_page_form'));
20         add_filter('mce_plugins', array(&$this, 'mce_plugins'));
21     }
22     
23     function go_solo()
24     {
25         $dispatch = isset($_POST['buttonsnapdispatch']) ? $_POST['buttonsnapdispatch'] : @$_GET['buttonsnapdispatch'];
26         if($dispatch != '') {
27             auth_redirect();
28             $selection = isset($_POST['selection']) ? $_POST['selection'] : @$_GET['selection'];
29             $selection = apply_filters($dispatch, $selection);
30             die($selection);
31         }
32         if(isset($_GET['docss'])) {
33             auth_redirect();
34             do_action('marker_css');
35             die();
36         }
37     }
38     
39     function edit_form_advanced()
40     {
41         if (!$this->script_output) {
42             $this->output_script('post');
43             $this->script_output = true;
44         }
45     }
46     
47     function edit_page_form()
48     {
49         if (!$this->script_output) {
50             $this->output_script('page');
51             $this->script_output = true;
52         }
53     }
54     
55     function mce_plugins($plugins)
56     {
57         if (count($this->markers) > 0) {
58         
59             echo "var buttonsnap_markers = new Array(\n";
60             $comma = '';
61             foreach ($this->markers as $k => $v) {
62                 echo "{$comma}\"{$k}\"";
63                 $comma = "\n,";
64             }
65             echo "\n);\n";
66             echo "var buttonsnap_classes = new Array(\n";
67             $comma = '';
68             foreach ($this->markers as $k => $v) {
69                 echo "{$comma}\"{$v}\"";
70                 $comma = "\n,";
71             }
72             echo "\n);\n";
73             
74 ?>
75
76 function TinyMCE_buttonsnap_initInstance(inst) {
77     tinyMCE.importCSS(inst.getDoc(), "<?php echo $this->plugin_uri(); ?>?docss=true");
78 }
79
80 function TinyMCE_buttonsnap_parseAttributes(attribute_string) {
81     var attributeName = "";
82     var attributeValue = "";
83     var withInName;
84     var withInValue;
85     var attributes = new Array();
86     var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');
87     var titleText = tinyMCE.getLang('lang_buttonsnap_more');
88     var titleTextPage = tinyMCE.getLang('lang_buttonsnap_page');
89
90     if (attribute_string == null || attribute_string.length < 2)
91         return null;
92
93     withInName = withInValue = false;
94
95     for (var i=0; i<attribute_string.length; i++) {
96         var chr = attribute_string.charAt(i);
97
98         if ((chr == '"' || chr == "'") && !withInValue)
99             withInValue = true;
100         else if ((chr == '"' || chr == "'") && withInValue) {
101             withInValue = false;
102
103             var pos = attributeName.lastIndexOf(' ');
104             if (pos != -1)
105                 attributeName = attributeName.substring(pos+1);
106
107             attributes[attributeName.toLowerCase()] = attributeValue.substring(1).toLowerCase();
108
109             attributeName = "";
110             attributeValue = "";
111         } else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)
112             withInName = true;
113
114         if (chr == '=' && withInName)
115             withInName = false;
116
117         if (withInName)
118             attributeName += chr;
119
120         if (withInValue)
121             attributeValue += chr;
122     }
123
124     return attributes;
125 }
126
127 function TinyMCE_buttonsnap_cleanup(type, content) {
128     switch (type) {
129         case "initial_editor_insert":
130             content = TinyMCE_buttonsnap_cleanup("insert_to_editor", content);
131             alert('foo');
132            
133             break;
134    
135         case "insert_to_editor":
136             var startPos = 0;
137
138             for(z=0;z<buttonsnap_markers.length;z++) {
139                 var startPos = 0;
140                 while ((startPos = content.indexOf('<!--' + buttonsnap_markers[z] + '-->', startPos)) != -1) {
141                     // Insert image
142                     var contentAfter = content.substring(startPos + 7 + buttonsnap_markers[z].length);
143                     content = content.substring(0, startPos);
144                     content += '<img src="' + (tinyMCE.getParam("theme_href") + "/images/spacer.gif") + '" ';
145                     content += ' width="100%" height="10px" ';
146                     content += 'alt="" class="' + buttonsnap_classes[z] + '" />';
147                     content += contentAfter;
148    
149                     startPos++;
150                 }
151             }
152             break;
153
154         case "get_from_editor":
155             var startPos = -1;
156             while ((startPos = content.indexOf('<img', startPos+1)) != -1) {
157                 var endPos = content.indexOf('/>', startPos);
158                 var attribs = TinyMCE_buttonsnap_parseAttributes(content.substring(startPos + 4, endPos));
159
160                 for(z=0;z<buttonsnap_classes.length;z++) {
161                     if (attribs['class'] == buttonsnap_classes[z]) {
162                         endPos += 2;
163        
164                         var embedHTML = '<!--' + buttonsnap_markers[z] + '-->';
165        
166                         // Insert embed/object chunk
167                         chunkBefore = content.substring(0, startPos);
168                         chunkAfter = content.substring(endPos);
169                         content = chunkBefore + embedHTML + chunkAfter;
170                         break;
171                     }
172                 }
173             }
174             break;
175     }
176
177     return content;
178 }
179
180 <?php
181             $plugins[] = 'buttonsnap';
182         }
183         return $plugins;
184     }
185     
186     function output_script($type = 'any')
187     {
188         echo '<script type="text/javascript">
189         var buttonsnap_request_uri = "' . $this->plugin_uri() . '";
190         var buttonsnap_wproot = "' . get_settings('siteurl') . '";
191         </script>' . "\n";
192 echo <<< ENDSCRIPT
193
194 <script type="text/javascript">
195 addLoadEvent(function () { window.setTimeout('buttonsnap_addbuttons()',1000); });
196 var buttonsnap_mozilla = document.getElementById&&!document.all;
197 function buttonsnap_safeclick(e)
198 {
199     if(!buttonsnap_mozilla) {
200         e.returnValue = false;
201         e.cancelBubble = true;
202     }
203 }
204 function buttonsnap_addEvent( obj, type, fn )
205 {
206     if (obj.addEventListener)
207         obj.addEventListener( type, fn, false );
208     else if (obj.attachEvent)
209     {
210         obj["e"+type+fn] = fn;
211         obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
212         obj.attachEvent( "on"+type, obj[type+fn] );
213     }
214 }
215 function buttonsnap_newbutton(src, alt) {
216     if(window.tinyMCE) {
217         var anchor = document.createElement('A');
218         anchor.setAttribute('href', 'javascript:;');
219         anchor.setAttribute('title', alt);
220         var newimage = document.createElement('IMG');
221         newimage.setAttribute('src', src);
222         newimage.setAttribute('alt', alt);
223         newimage.setAttribute('class', 'mceButtonNormal');
224         buttonsnap_addEvent(newimage, 'mouseover', function() {tinyMCE.switchClass(this,'mceButtonOver');});
225         buttonsnap_addEvent(newimage, 'mouseout', function() {tinyMCE.switchClass(this,'mceButtonNormal');}); //restoreClass(this)
226         buttonsnap_addEvent(newimage, 'mousedown', function() {tinyMCE.restoreAndSwitchClass(this,'mceButtonDown');});
227         anchor.appendChild(newimage);
228         brs = mcetoolbar.getElementsByTagName('BR');
229         if(brs.length > 0)
230             mcetoolbar.insertBefore(anchor, brs[0]);
231         else
232             mcetoolbar.appendChild(anchor);
233     }
234     else if(window.qttoolbar)
235     {
236         var anchor = document.createElement('input');
237         anchor.type = 'button';
238         anchor.value = alt;
239         anchor.className = 'ed_button';
240         anchor.title = alt;
241         anchor.id = 'ed_' + alt;
242         qttoolbar.appendChild(anchor);
243     }   
244     return anchor;
245 }
246 function buttonsnap_newseparator() {
247     if(window.tinyMCE) {
248         var sep = document.createElement('IMG');
249         
250         sep.setAttribute('src', buttonsnap_wproot + '/wp-includes/js/tinymce/themes/advanced/images/spacer.gif');
251         sep.className = 'mceSeparatorLine';
252         sep.setAttribute('class', 'mceSeparatorLine');
253         sep.setAttribute('height', '16');
254         sep.setAttribute('width', '1');
255         brs = mcetoolbar.getElementsByTagName('BR');
256         if(brs.length > 0)
257             mcetoolbar.insertBefore(sep, brs[0]);
258         else
259             mcetoolbar.appendChild(sep);
260     }
261 }
262 function buttonsnap_settext(text) {
263     if(window.tinyMCE) {
264         window.tinyMCE.execInstanceCommand('content', 'mceInsertContent', false, text);
265         tinyMCE.execCommand("mceCleanup");
266     } else {
267         edInsertContent(edCanvas, text);
268     }
269 }
270 function buttonsnap_ajax(dispatch) {
271     if(window.tinyMCE)     {
272         selection = tinyMCE.getInstanceById('content').getSelectedText();
273     }
274     else     {   
275         if (edCanvas.selectionStart || edCanvas.selectionStart == '0') {
276             var startPos = edCanvas.selectionStart;
277             var endPos = edCanvas.selectionEnd;
278     
279             if (startPos != endPos) {
280                 selection = edCanvas.value.substring(startPos, endPos);
281             }
282         }
283         else if (document.getSelection)        {
284             selection = 'document.getSelection';
285         }
286         else if (document.selection)         {
287             document.getElementById('content').focus();
288               sel = document.selection.createRange();
289             if (sel.text.length > 0)             {
290                 selection = sel.text;
291             }
292             else             {
293                 selection = '';
294             }
295         }
296         else         {
297             selection = 'JAVASCRIPT ERROR FAILED TO GET SELECTED TEXT!';
298         }
299     }
300
301     var ajax = new sack(buttonsnap_request_uri);
302     ajax.setVar('buttonsnapdispatch', dispatch);
303     ajax.setVar('selection', selection);
304     ajax.onCompletion = function () {buttonsnap_settext(this.response);};
305     ajax.runAJAX();
306 }
307 var mcetoolbar;
308 var qttoolbar = document.getElementById("ed_toolbar");
309 function buttonsnap_addbuttons () {
310     if(window.tinyMCE) {
311         try {
312             var edit = document.getElementById(window.tinyMCE.getEditorId('content'));
313             for(table = edit;table.tagName != 'TABLE';table = table.parentNode);
314             mcetoolbar = table.rows[0].firstChild;
315         }
316         catch(e) {
317             setTimeout('buttonsnap_addbuttons()', 5000);
318             return;
319         }
320     }
321     try {
322 ENDSCRIPT;
323         
324         switch($type) {
325         case 'any':
326             $this->buttons['any'] = array_merge($this->buttons['post'], $this->buttons['page'], $this->buttons['any']);
327             break;
328         default:
329             $this->buttons[$type] = array_merge($this->buttons[$type], $this->buttons['any']);
330         }
331         $usebuttons = $this->buttons[$type];
332         
333         foreach ($usebuttons as $button) {
334             if($button['type'] == 'separator') {
335                 echo "buttonsnap_newseparator();\n";
336             }
337             else {
338                 echo "newbtn = buttonsnap_newbutton('{$button['src']}', '{$button['alt']}');\n";
339                 switch($button['type']) {
340                 case 'text':
341                     echo "buttonsnap_addEvent(newbtn, 'click', function(e) {buttonsnap_settext(\"{$button['text']}\");buttonsnap_safeclick(e);});\n";
342                     break;
343                 case 'js':
344                     echo "buttonsnap_addEvent(newbtn, 'click', function(e) {" . $button['js'] . "buttonsnap_safeclick(e);});\n";
345                     break;
346                 case 'ajax':
347                     echo "buttonsnap_addEvent(newbtn, 'click', function(e) {buttonsnap_ajax(\"{$button['hook']}\");buttonsnap_safeclick(e);});\n";
348                     break;
349                 default:
350                     echo "buttonsnap_addEvent(newbtn, 'click', function(e) {alert(\"The :{$button->type}: button is an invalid type\");buttonsnap_safeclick(e);});\n";
351                 }
352             }
353         }
354 echo <<< MORESCRIPT
355     }
356     catch(e) {
357         setTimeout('buttonsnap_addbuttons()', 5000);
358     }
359 }
360 </script>
361
362 MORESCRIPT;
363     }
364     
365     function textbutton($imgsrc, $alttext, $inserted, $type="any")
366     {
367         $this->buttons[$type][] = array('type'=>'text', 'src'=>$imgsrc, 'alt'=>$alttext, 'text'=>$inserted);
368         return $this->buttons;
369     }
370     
371     function jsbutton($imgsrc, $alttext, $js, $type="any")
372     {
373         $this->buttons[$type][] = array('type'=>'js', 'src'=>$imgsrc, 'alt'=>$alttext, 'js'=>$js);
374         return $this->buttons;
375     }
376
377     function ajaxbutton($imgsrc, $alttext, $hook, $type="any")
378     {
379         $this->buttons[$type][] = array('type'=>'ajax', 'src'=>$imgsrc, 'alt'=>$alttext, 'hook'=>$hook);
380         return $this->buttons;
381     }
382     
383     function separator($type="any")
384     {
385         $this->buttons[$type][] = array('type'=>'separator');
386         return $this->buttons;
387     }
388     
389     function register_marker($marker, $cssclass)
390     {
391         $this->markers[$marker] = $cssclass;
392     }
393     
394     function basename($src='')
395     {
396         if($src == '') $src = __FILE__;
397         $name = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $src);
398         return str_replace('\\', '/', $name);
399     }
400     
401     function plugin_uri($src = '')
402     {
403         return get_settings('siteurl') . '/wp-content/plugins/' . $this->basename($src);
404     }
405     
406     function include_up($filename) {
407         $c=0;
408         while(!is_file($filename)) {
409             $filename = '../' . $filename;
410             $c++;
411             if($c==30) {
412                 echo 'Could not find ' . basename($filename) . '.'; return '';
413             }
414         }
415         return $filename;
416     }
417
418     function debug($foo)
419     {
420         $args = func_get_args();
421         echo "<pre style=\"background-color:#ffeeee;border:1px solid red;\">";
422         foreach($args as $arg1)
423         {
424             echo htmlentities(print_r($arg1, 1)) . "<br/>";
425         }
426         echo "</pre>";
427     }
428 }
429 $buttonsnap = new buttonsnap();
430 function buttonsnap_textbutton($imgsrc, $alttext, $inserted, $type="any") { global $buttonsnap; return $buttonsnap->textbutton($imgsrc, $alttext, $inserted, $type);}
431 function buttonsnap_jsbutton($imgsrc, $alttext, $js, $type="any") { global $buttonsnap; return $buttonsnap->jsbutton($imgsrc, $alttext, $js, $type);}
432 function buttonsnap_ajaxbutton($imgsrc, $alttext, $hook, $type="any") { global $buttonsnap; return $buttonsnap->ajaxbutton($imgsrc, $alttext, $hook, $type);}
433 function buttonsnap_separator($type="any") { global $buttonsnap; return $buttonsnap->separator($type);}
434
435 function buttonsnap_textbutton_post($imgsrc, $alttext, $inserted) { global $buttonsnap; return $buttonsnap->textbutton($imgsrc, $alttext, $inserted, 'post');}
436 function buttonsnap_jsbutton_post($imgsrc, $alttext, $js) { global $buttonsnap; return $buttonsnap->jsbutton($imgsrc, $alttext, $js, 'post');}
437 function buttonsnap_ajaxbutton_post($imgsrc, $alttext, $hook) { global $buttonsnap; return $buttonsnap->ajaxbutton($imgsrc, $alttext, $hook, 'post');}
438 function buttonsnap_separator_post() { global $buttonsnap; return $buttonsnap->separator('post');}
439
440 function buttonsnap_textbutton_page($imgsrc, $alttext, $inserted) { global $buttonsnap; return $buttonsnap->textbutton($imgsrc, $alttext, $inserted, 'page');}
441 function buttonsnap_jsbutton_page($imgsrc, $alttext, $js) { global $buttonsnap; return $buttonsnap->jsbutton($imgsrc, $alttext, $js, 'page');}
442 function buttonsnap_ajaxbutton_page($imgsrc, $alttext, $hook) { global $buttonsnap; return $buttonsnap->ajaxbutton($imgsrc, $alttext, $hook, 'page');}
443 function buttonsnap_separator_page() { global $buttonsnap; return $buttonsnap->separator('page');}
444
445 function buttonsnap_dirname($src = '') {global $buttonsnap; return dirname($buttonsnap->plugin_uri($src));}
446 function buttonsnap_register_marker($marker, $cssclass) {global $buttonsnap; return $buttonsnap->register_marker($marker, $cssclass);}
447 endif;
448 if (!defined('ABSPATH')) {
449   require_once($buttonsnap->include_up('wp-config.php'));
450   $buttonsnap->go_solo();
451 }
452 else {
453     $buttonsnap->sink_hooks();
454 }
455
456 ?>
457