root/live-comment-preview/trunk/live-comment-preview.php

Revision 41680, 8.7 kB (checked in by bradt, 3 months ago)

Updating version numbers.

Line 
1<?php
2/*
3Plugin Name: Live Comment Preview
4Plugin URI: http://wordpress.org/extend/plugins/live-comment-preview/
5Description: Supply users with a live comment preview. Use the function &lt;?php live_preview() ?&gt; to display the live preview in a different location. Based on version 1.7 by <a href="http://jm.cc/">Jeff Minard</a>.
6Author: Brad Touesnard
7Author URI: http://bradt.ca/
8Version: 1.9
9
10        Copyright 2007  Brad Touesnard  (http://bradt.ca/)
11
12        This program is free software; you can redistribute it and/or modify
13        it under the terms of the GNU General Public License as published by
14        the Free Software Foundation; either version 2 of the License, or
15        (at your option) any later version.
16
17        This program is distributed in the hope that it will be useful,
18        but WITHOUT ANY WARRANTY; without even the implied warranty of
19        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20        GNU General Public License for more details.
21
22        You should have received a copy of the GNU General Public License
23        along with this program; if not, write to the Free Software
24        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25*/ 
26
27function lcp_output_js() {
28        global $user_ID, $user_identity;
29
30        // Gravatar settings
31        $gravatar_size = 32;
32        $gravatar_default = 'http://www.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536';
33        $gravatar_rating = get_option('avatar_rating');
34
35        // Customize this string if you want to modify the preview output
36        // %1 - author's name (as hyperlink if available)
37        // %2 - comment text
38        // %3 - gravatar image url
39        $previewFormat = '
40                <ol class="commentlist" style="clear: both; margin-top: 3em;">
41                        <li id="comment-preview" class="alt" style="overflow: hidden;">
42                                <img src="%3" alt="" class="avatar avatar-' . $gravatar_size . '" width="' . $gravatar_size . '" height="' . $gravatar_size . '"/>
43                                <cite>%1</cite> Says:
44                                <br />
45                                %2
46                        </li>
47                </ol>';
48
49        // If you have changed the ID's on your form field elements
50        // You should make them match here
51        $commentFrom_commentID = 'comment';
52        $commentFrom_authorID  = 'author';
53        $commentFrom_urlID     = 'url';
54        $commentFrom_emailID     = 'email';
55
56        $user_gravatar = '';
57        // Default name
58        if ($user_ID) {
59                $default_name = $user_identity;
60
61                $user = get_userdata($user_ID);
62                if ($user) {
63                        $user_gravatar = 'http://www.gravatar.com/avatar/' . md5(strtolower($user->user_email));
64                }
65        }
66        else {
67                $default_name = 'Anonymous';
68        }
69
70        // You shouldn't need to edit anything else.
71
72        header('Content-type: text/javascript');
73        ?>
74
75function wptexturize(text) {
76        text = ' '+text+' ';
77        var next        = true;
78        var output      = '';
79        var prev        = 0;
80        var length      = text.length;
81        while ( prev < length ) {
82                var index = text.indexOf('<', prev);
83                if ( index > -1 ) {
84                        if ( index == prev ) {
85                                index = text.indexOf('>', prev);
86                        }
87                        index++;
88                } else {
89                        index = length;
90                }
91                var s = text.substring(prev, index);
92                prev = index;
93                if ( s.substr(0,1) != '<' && next == true ) {
94                        s = s.replace(/---/g, '&#8212;');
95                        s = s.replace(/--/g, '&#8211;');
96                        s = s.replace(/\.{3}/g, '&#8230;');
97                        s = s.replace(/``/g, '&#8220;');
98                        s = s.replace(/'s/g, '&#8217;s');
99                        s = s.replace(/'(\d\d(?:&#8217;|')?s)/g, '&#8217;$1');
100                        s = s.replace(/([\s"])'/g, '$1&#8216;');
101                        s = s.replace(/(\d+)"/g, '$1&Prime;');
102                        s = s.replace(/(\d+)'/g, '$1&prime;');
103                        s = s.replace(/([^\s])'([^'\s])/g, '$1&#8217;$2');
104                        s = s.replace(/(\s)"([^\s])/g, '$1&#8220;$2');
105                        s = s.replace(/"(\s)/g, '&#8221;$1');
106                        s = s.replace(/'(\s|.)/g, '&#8217;$1');
107                        s = s.replace(/\(tm\)/ig, '&#8482;');
108                        s = s.replace(/\(c\)/ig, '&#169;');
109                        s = s.replace(/\(r\)/ig, '&#174;');
110                        s = s.replace(/''/g, '&#8221;');
111                        s = s.replace(/(\d+)x(\d+)/g, '$1&#215;$2');
112                } else if ( s.substr(0,5) == '<code' ) {
113                        next = false;
114                } else {
115                        next = true;
116                }
117                output += s;
118        }
119        return output.substr(1, output.length-2);       
120}
121
122function wpautop(p) {
123        p = p + '\n\n';
124        p = p.replace(/(<blockquote[^>]*>)/g, '\n$1');
125        p = p.replace(/(<\/blockquote[^>]*>)/g, '$1\n');
126        p = p.replace(/\r\n/g, '\n');
127        p = p.replace(/\r/g, '\n');
128        p = p.replace(/\n\n+/g, '\n\n');
129        p = p.replace(/\n?(.+?)(?:\n\s*\n)/g, '<p>$1</p>');
130        p = p.replace(/<p>\s*?<\/p>/g, '');
131        p = p.replace(/<p>\s*(<\/?blockquote[^>]*>)\s*<\/p>/g, '$1');
132        p = p.replace(/<p><blockquote([^>]*)>/ig, '<blockquote$1><p>');
133        p = p.replace(/<\/blockquote><\/p>/ig, '<p></blockquote>');     
134        p = p.replace(/<p>\s*<blockquote([^>]*)>/ig, '<blockquote$1>');
135        p = p.replace(/<\/blockquote>\s*<\/p>/ig, '</blockquote>');     
136        p = p.replace(/\s*\n\s*/g, '<br />');
137        return p;
138}
139
140function updateLivePreview() {
141       
142        var cmntArea = document.getElementById('<?php echo $commentFrom_commentID ?>');
143        var pnmeArea = document.getElementById('<?php echo $commentFrom_authorID ?>');
144        var purlArea = document.getElementById('<?php echo $commentFrom_urlID ?>');
145        var emlArea = document.getElementById('<?php echo $commentFrom_emailID ?>');
146       
147        if( cmntArea != null )
148                var cmnt = wpautop(wptexturize(cmntArea.value));
149        else
150                var cmnt = '';
151
152        if( pnmeArea != null )
153                var pnme = pnmeArea.value;
154        else
155                var pnme = '';
156       
157        if( purlArea != null )
158                var purl = purlArea.value;
159        else
160                var purl = '';
161               
162        if ( emlArea != null )
163                var eml = emlArea.value;
164        else
165                var eml = '';
166               
167        if(purl && pnme) {
168                var name = '<a href="' + purl + '">' + pnme + '</a>';
169        } else if(!purl && pnme) {
170                var name = pnme;
171        } else if(purl && !pnme) {
172                var name = '<a href="' + purl + '"><?php echo addslashes($default_name); ?></a>';
173        } else {
174                var name = "<?php echo addslashes($default_name); ?>";
175        }       
176       
177        var user_gravatar = '<?php echo addslashes($user_gravatar); ?>';
178        var gravatar = '<?php echo addslashes($gravatar_default); ?>?';
179        if (eml != '') {
180                gravatar = 'http://www.gravatar.com/avatar/' + hex_md5(eml) + '?d=<?php echo urlencode($gravatar_default); ?>&amp;';
181        }
182        else if (user_gravatar != '') {
183                gravatar = user_gravatar + '?d=<?php echo urlencode($gravatar_default); ?>&amp;';
184        }
185       
186        gravatar += 's=<?php echo $gravatar_size; ?>';
187       
188    <?php
189        if (!empty($gravatar_rating)) {
190                ?>
191                gravatar += '&amp;r=<?php echo urlencode($gravatar_rating) ?>';
192                <?php
193        }
194
195    $previewFormat = str_replace("\r", "", $previewFormat);
196    $previewFormat = str_replace("\n", "", $previewFormat);
197    $previewFormat = str_replace("'", "\'", $previewFormat);
198    $previewFormat = str_replace("%1", "' + name + '", $previewFormat);
199    $previewFormat = str_replace("%2", "' + cmnt + '", $previewFormat);
200    $previewFormat = str_replace("%3", "' + gravatar + '", $previewFormat);
201    $previewFormat = "'" . $previewFormat . "';\n";
202    ?>
203    document.getElementById('commentPreview').innerHTML = <?php echo $previewFormat; ?>
204}
205
206function initLivePreview() {
207        if(!document.getElementById)
208                return false;
209
210        var cmntArea = document.getElementById('<?php echo $commentFrom_commentID ?>');
211        var pnmeArea = document.getElementById('<?php echo $commentFrom_authorID ?>');
212        var purlArea = document.getElementById('<?php echo $commentFrom_urlID ?>');
213       
214        if ( cmntArea )
215                cmntArea.onkeyup = updateLivePreview;
216       
217        if ( pnmeArea )
218                pnmeArea.onkeyup = updateLivePreview;
219       
220        if ( purlArea )
221                purlArea.onkeyup = updateLivePreview;   
222}
223
224//========================================================
225// Event Listener by Scott Andrew - http://scottandrew.com
226// edited by Mark Wubben, <useCapture> is now set to false
227//========================================================
228function addEvent(obj, evType, fn){
229        if(obj.addEventListener){
230                obj.addEventListener(evType, fn, false);
231                return true;
232        } else if (obj.attachEvent){
233                var r = obj.attachEvent('on'+evType, fn);
234                return r;
235        } else {
236                return false;
237        }
238}
239
240addEvent(window, "load", initLivePreview);
241
242        <?php
243        // Add the MD5 functions using PHP so we only
244        // need to make 1 request to the web server for JS
245        $plugin_path = dirname(__FILE__);
246        $md5_file = $plugin_path . '/md5.js';
247        @include($md5_file);
248       
249        // We're done outputting JS
250        die();
251}
252
253function live_preview($before='', $after='') {
254        global $livePreviewDivAdded;
255        if($livePreviewDivAdded == false) {
256                // We don't want this included in every page
257                // so we add it here instead of using the wphead filter
258                echo '<script src="' . get_option('home') . '/?live-comment-preview.js" type="text/javascript"></script>';
259                echo $before.'<div id="commentPreview"></div>'.$after;
260                $livePreviewDivAdded = true;
261        }
262}
263
264function lcp_add_preview_div($post_id) {
265        live_preview();
266        return $post_id;
267}
268
269$livePreviewDivAdded == false;
270
271if( stristr($_SERVER['REQUEST_URI'], 'live-comment-preview.js') ) {
272        add_action('template_redirect', 'lcp_output_js');
273}
274
275add_action('comment_form', 'lcp_add_preview_div');
276?>
Note: See TracBrowser for help on using the browser.