root/wp-grins/trunk/wp-grins.php

Revision 9059, 4.1 kB (checked in by alexkingorg, 1 year ago)

version 1.5

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 // WP Grins
4 //
5 // Copyright (c) 2004-2007 Alex King
6 // http://alexking.org/projects/wordpress
7 //
8 // This is an add-on for WordPress
9 // http://wordpress.org/
10 //
11 // **********************************************************************
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 // *****************************************************************
16
17 /*
18 Plugin Name: WP Grins
19 Plugin URI: http://alexking.org/projects/wordpress
20 Description: A Clickable Smilies hack for WordPress.
21 Version: 1.5
22 Author: Alex King
23 Author URI: http://alexking.org
24 */
25
26 function wp_grins() { // left in for legacy reasons
27     print('');
28 }
29
30 if (!function_exists('ak_prototype')) {
31     function ak_prototype() {
32         if (!function_exists('wp_enqueue_script')) {
33             global $ak_prototype;
34             if (!isset($ak_prototype) || !$ak_prototype) {
35                 print('
36         <script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-includes/js/prototype.js"></script>
37                 ');
38             }
39             $ak_prototype = true;
40         }
41     }
42 }
43
44 function wp_grins_head() {
45     print('<script type="text/javascript" src="'.get_bloginfo('wpurl').'/index.php?ak_action=wp_grins_js"></script>'."\n");
46     ak_prototype();
47     print('
48 <style type="text/css">
49 #wp_grins img {
50     cursor: pointer;
51 }
52 </style>
53 <!--[if IE]>
54 <style type="text/css">
55 #wp_grins img {
56     cursor: hand;
57 }
58 </style>
59 <![endif]-->
60     ');
61 }
62
63 function wp_grins_js() {
64     if (function_exists('wp_enqueue_script')) {
65         wp_enqueue_script('prototype');
66     }
67     if (isset($_GET['ak_action']) && $_GET['ak_action'] == 'wp_grins_js') {
68         global $wpsmiliestrans;
69
70         header("Content-type: text/javascript");
71     
72         $grins = '';
73         $smiled = array();
74         foreach ($wpsmiliestrans as $tag => $grin) {
75             if (!in_array($grin, $smiled)) {
76                 $smiled[] = $grin;
77                 $tag = str_replace(' ', '', $tag);
78                 $grins .= '<img src="'.get_bloginfo('wpurl').'/wp-includes/images/smilies/'.$grin.'" alt="'.$tag.'" onclick="grin(\''.$tag.'\');"/> ';
79             }
80         }
81 ?>
82 function insertAfter(node, referenceNode) {
83     referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
84 }
85 function loadGrins() {
86     var grinsDiv = document.createElement('div');
87     grinsDiv.id = 'wp_grins';
88     grinsDiv.innerHTML = '<?php print(str_replace("'", "\'", $grins)); ?>';
89     if ($('postdiv')) {
90         var type = 'child';
91         var node = $('postdiv');
92     }
93     else if (document.getElementById('postdivrich')) {
94         var type = 'child';
95         var node = $('postdivrich');
96     }
97     else if (document.getElementById('comment')) {
98         var type = 'before';
99         var node = $('comment');
100     }
101     else {
102         return;
103     }
104     switch (type) {
105         case 'child':
106             grinsDiv.style.paddingTop = '5px';
107             node.appendChild(grinsDiv);
108             break;
109         case 'before':
110             node.parentNode.insertBefore(grinsDiv, node);
111             break;
112     }
113 }
114 Event.observe(window, 'load', loadGrins, false);
115 function grin(tag) {
116     var myField;
117     if ($('content') && $('content').type == 'textarea') {
118         myField = document.getElementById('content');
119         if ($('postdivrich') && typeof tinyMCE != 'undefined' && (!$('edButtons') || $('quicktags').style.display == 'none')) {
120             tinyMCE.execInstanceCommand('mce_editor_0', 'mceInsertContent', false, '&nbsp;' + tag + '&nbsp;');
121             tinyMCE.selectedInstance.repaint();
122             return;
123         }
124     }
125     else if ($('comment') && $('comment').type == 'textarea') {
126         myField = $('comment');
127     }
128     else {
129         return false;
130     }
131     if (document.selection) {
132         myField.focus();
133         sel = document.selection.createRange();
134         sel.text = ' ' + tag + ' ';
135         myField.focus();
136     }
137     else if (myField.selectionStart || myField.selectionStart == '0') {
138         var startPos = myField.selectionStart;
139         var endPos = myField.selectionEnd;
140         var cursorPos = endPos;
141         myField.value = myField.value.substring(0, startPos)
142                       + ' ' + tag + ' '
143                       + myField.value.substring(endPos, myField.value.length);
144         cursorPos += tag.length + 2;
145         myField.focus();
146         myField.selectionStart = cursorPos;
147         myField.selectionEnd = cursorPos;
148     }
149     else {
150         myField.value += tag;
151         myField.focus();
152     }
153 }
154 <?php
155         die();
156     }
157 }
158
159 add_action('init', 'wp_grins_js');
160 add_action('wp_head', 'wp_grins_head');
161 add_action('admin_head', 'wp_grins_head');
162
163 ?>
Note: See TracBrowser for help on using the browser.