source: live-comment-preview/trunk/live-comment-preview.php @ 435228

Revision 435228, 10.9 KB checked in by bradt, 5 months ago (diff)

Bug fix: Doesn't appear the preview in 2.0

Line 
1<?php
2/*
3Plugin Name: Live Comment Preview
4Plugin URI: http://wordpress.org/extend/plugins/live-comment-preview/
5Description: Displays a preview of the user's comment as they type it.
6Author: Brad Touesnard
7Author URI: http://bradt.ca/
8Version: 2.0.1
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23*/
24
25function lcp_output_js() {
26        global $user_ID, $user_identity;
27
28        // Avatar settings
29        $avatar_size = 32;
30        $avatar_default = get_option('avatar_default');
31        $avatar_rating = get_option('avatar_rating');
32
33        $previewFormat = '';
34
35        $file = TEMPLATEPATH . '/comment-preview.php';
36        if (file_exists($file)) {
37                ob_start();
38                include($file);
39                $previewFormat = ob_get_clean();
40
41                // Get avatar size
42                if (preg_match('@<img(.*?)class=.avatar(.*?)>@s', $previewFormat, $matches)) {
43                        $img_tag = $matches[0];
44                       
45                        if (preg_match('@width=.([0-9]+)@', $img_tag, $matches)) {
46                                $avatar_size = $matches[1];
47                        }
48                }
49        }
50
51        $file = TEMPLATEPATH . '/comments.php';
52        if (!$previewFormat && file_exists($file)) {
53                global $wp_query, $comments, $comment, $post;
54               
55                $post->comment_status = 'open';
56               
57                $comment->comment_ID = 'lcp';
58                $comment->comment_content = 'COMMENT_CONTENT';
59                $comment->comment_author = 'COMMENT_AUTHOR';
60                $comment->comment_parent = 0;
61                $comment->comment_date = time();
62               
63                $wp_query->comment = $comment;
64                $wp_query->comments = $comments = array($comment);
65                $wp_query->current_comment = -1;
66                $wp_query->comment_count = 1;
67               
68                ob_start();
69                include($file);
70                $html = ob_get_clean();
71
72                if (preg_match('@<ol(.*?)class=.commentlist(.*)</ol>@s', $html, $matches)) {
73                        $previewFormat = $matches[0];
74                       
75                        $previewFormat = preg_replace('@http://COMMENT_AUTHOR_URL@', 'COMMENT_AUTHOR_URL', $previewFormat);
76                       
77                        if (preg_match('@<img(.*?)class=.avatar(.*?)>@s', $previewFormat, $matches)) {
78                                $img_tag = $matches[0];
79                                $new_img_tag = preg_replace('@src=("|\')(.*?)("|\')@', 'src=$1AVATAR_URL$3', $img_tag);
80                                $previewFormat = str_replace($img_tag, $new_img_tag, $previewFormat);
81                               
82                                if (preg_match('@width=.([0-9]+)@', $img_tag, $matches)) {
83                                        $avatar_size = $matches[1];
84                                }
85                        }
86                }
87        }
88
89        if ( !$avatar_default )
90                $avatar_default = 'mystery';
91
92        if ( is_ssl() ) {
93                $host = 'https://secure.gravatar.com';
94        } else {
95                if ( !empty($email) )
96                        $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash{0} ) % 2 ) );
97                else
98                        $host = 'http://0.gravatar.com';
99        }
100       
101        if ( 'mystery' == $avatar_default )
102                $avatar_default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
103        elseif ( 'blank' == $avatar_default )
104                $avatar_default = includes_url('images/blank.gif');
105        elseif ( 'gravatar_default' == $avatar_default )
106                $avatar_default = "$host/avatar/";
107       
108        // Just in case the other two methods didn't work out
109        if (!$previewFormat) {
110                $previewFormat = '
111                        <ol class="commentlist">
112                                <li id="comment-preview">
113                                        <img src="' . $avatar_default . '" alt="" class="avatar avatar-' . $avatar_size . '" width="' . $avatar_size . '" height="' . $avatar_size . '"/>
114                                        <cite>COMMENT_AUTHOR</cite> Says:
115                                        <br />
116                                        COMMENT_CONTENT
117                                </li>
118                        </ol>';
119        }
120       
121        // If you have changed the ID's on your form field elements
122        // You should make them match here
123        $commentFrom_commentID = 'comment';
124        $commentFrom_authorID  = 'author';
125        $commentFrom_urlID     = 'url';
126        $commentFrom_emailID     = 'email';
127
128        $user_gravatar = '';
129        // Default name
130        if ($user_ID) {
131                $default_name = $user_identity;
132
133                $user = get_userdata($user_ID);
134                if ($user) {
135                        $user_gravatar = 'http://www.gravatar.com/avatar/' . md5(strtolower($user->user_email));
136                }
137        }
138        else {
139                $default_name = 'Anonymous';
140        }
141
142        // You shouldn't need to edit anything else.
143
144        header('Content-type: text/javascript');
145        global $allowedtags;
146        ?>
147
148var allowedtags=['<?php echo implode("', '", array_keys($allowedtags)) ?>'];
149
150function wptexturize(text) {
151        text = ' '+text+' ';
152        var next        = true;
153        var output      = '';
154        var prev        = 0;
155        var length      = text.length;
156        var tagsre = new RegExp('^/?(' + allowedtags.join('|') + ')\\b', 'i');
157        while ( prev < length ) {
158                var index = text.indexOf('<', prev);
159                if ( index > -1 ) {
160                        if ( index == prev ) {
161                                index = text.indexOf('>', prev);
162                        }
163                        index++;
164                } else {
165                        index = length;
166                }
167                var s = text.substring(prev, index);
168                prev = index;
169                if (output.match(/<$/) && !s.match(tagsre)) {
170                        // jwz: omit illegal tags
171                        output = output.replace(/<$/, ' ');
172                        s = s.replace(/^[^>]*(>|$)/, '');
173                } else if ( s.substr(0,1) != '<' && next == true ) {
174                        s = s.replace(/---/g, '&#8212;');
175                        s = s.replace(/--/g, '&#8211;');
176                        s = s.replace(/\.{3}/g, '&#8230;');
177                        s = s.replace(/``/g, '&#8220;');
178                        s = s.replace(/'s/g, '&#8217;s');
179                        s = s.replace(/'(\d\d(?:&#8217;|')?s)/g, '&#8217;$1');
180                        s = s.replace(/([\s"])'/g, '$1&#8216;');
181                        s = s.replace(/([^\s])'([^'\s])/g, '$1&#8217;$2');
182                        s = s.replace(/(\s)"([^\s])/g, '$1&#8220;$2');
183                        s = s.replace(/"(\s)/g, '&#8221;$1');
184                        s = s.replace(/'(\s|.)/g, '&#8217;$1');
185                        s = s.replace(/\(tm\)/ig, '&#8482;');
186                        s = s.replace(/\(c\)/ig, '&#169;');
187                        s = s.replace(/\(r\)/ig, '&#174;');
188                        s = s.replace(/''/g, '&#8221;');
189                        s = s.replace(/(\d+)x(\d+)/g, '$1&#215;$2');
190                } else if ( s.substr(0,5) == '<code' ) {
191                        next = false;
192                } else {
193                        next = true;
194                }
195                output += s;
196        }
197        return output.substr(1, output.length-2);       
198}
199
200function wpautop(p) {
201        p = p + '\n\n';
202        p = p.replace(/(<blockquote[^>]*>)/g, '\n$1');
203        p = p.replace(/(<\/blockquote[^>]*>)/g, '$1\n');
204        p = p.replace(/\r\n/g, '\n');
205        p = p.replace(/\r/g, '\n');
206        p = p.replace(/\n\n+/g, '\n\n');
207        p = p.replace(/\n?(.+?)(?:\n\s*\n)/g, '<p>$1</p>');
208        p = p.replace(/<p>\s*?<\/p>/g, '');
209        p = p.replace(/<p>\s*(<\/?blockquote[^>]*>)\s*<\/p>/g, '$1');
210        p = p.replace(/<p><blockquote([^>]*)>/ig, '<blockquote$1><p>');
211        p = p.replace(/<\/blockquote><\/p>/ig, '<p></blockquote>');     
212        p = p.replace(/<p>\s*<blockquote([^>]*)>/ig, '<blockquote$1>');
213        p = p.replace(/<\/blockquote>\s*<\/p>/ig, '</blockquote>');     
214        p = p.replace(/\s*\n\s*/g, '<br />');
215        return p;
216}
217
218function updateLivePreview() {
219       
220        var cmntArea = document.getElementById('<?php echo $commentFrom_commentID ?>');
221        var pnmeArea = document.getElementById('<?php echo $commentFrom_authorID ?>');
222        var purlArea = document.getElementById('<?php echo $commentFrom_urlID ?>');
223        var emlArea = document.getElementById('<?php echo $commentFrom_emailID ?>');
224       
225        if( cmntArea != null )
226                var cmnt = wpautop(wptexturize(cmntArea.value));
227        else
228                var cmnt = '';
229
230        if( pnmeArea != null )
231                var pnme = pnmeArea.value;
232        else
233                var pnme = '';
234       
235        if( purlArea != null )
236                var purl = purlArea.value;
237        else
238                var purl = '';
239               
240        if ( emlArea != null )
241                var eml = emlArea.value;
242        else
243                var eml = '';
244               
245        if(purl && pnme) {
246                var name = '<a href="' + purl + '">' + pnme + '</a>';
247        } else if(!purl && pnme) {
248                var name = pnme;
249        } else if(purl && !pnme) {
250                var name = '<a href="' + purl + '"><?php echo addslashes($default_name); ?></a>';
251        } else {
252                var name = "<?php echo addslashes($default_name); ?>";
253        }       
254       
255        var user_gravatar = '<?php echo addslashes($user_gravatar); ?>';
256        var gravatar = '<?php echo addslashes($avatar_default); ?>?';
257        if (eml != '') {
258                gravatar = 'http://www.gravatar.com/avatar/' + hex_md5(eml) + '?d=<?php echo urlencode($avatar_default); ?>&amp;';
259        }
260        else if (user_gravatar != '') {
261                gravatar = user_gravatar + '?d=<?php echo urlencode($avatar_default); ?>&amp;';
262        }
263       
264        gravatar += 's=<?php echo $avatar_size; ?>';
265       
266    <?php
267        if (!empty($avatar_rating)) {
268                ?>
269                gravatar += '&amp;r=<?php echo urlencode($avatar_rating) ?>';
270                <?php
271        }
272
273    $previewFormat = str_replace("\r", "", $previewFormat);
274    $previewFormat = str_replace("\n", "", $previewFormat);
275    $previewFormat = str_replace("'", "\'", $previewFormat);
276    $previewFormat = str_replace("COMMENT_AUTHOR", "' + name + '", $previewFormat);
277    $previewFormat = str_replace("COMMENT_CONTENT", "' + cmnt + '", $previewFormat);
278    $previewFormat = str_replace("AVATAR_URL", "' + gravatar + '", $previewFormat);
279    $previewFormat = "'" . $previewFormat . "';\n";
280    ?>
281    document.getElementById('commentPreview').innerHTML = <?php echo $previewFormat; ?>
282}
283
284function initLivePreview() {
285        if(!document.getElementById)
286                return false;
287
288        var cmntArea = document.getElementById('<?php echo $commentFrom_commentID ?>');
289        var pnmeArea = document.getElementById('<?php echo $commentFrom_authorID ?>');
290        var purlArea = document.getElementById('<?php echo $commentFrom_urlID ?>');
291       
292        if ( cmntArea )
293                cmntArea.onkeyup = updateLivePreview;
294       
295        if ( pnmeArea )
296                pnmeArea.onkeyup = updateLivePreview;
297       
298        if ( purlArea )
299                purlArea.onkeyup = updateLivePreview;   
300}
301
302//========================================================
303// Event Listener by Scott Andrew - http://scottandrew.com
304// edited by Mark Wubben, <useCapture> is now set to false
305//========================================================
306function addEvent(obj, evType, fn){
307        if(obj.addEventListener){
308                obj.addEventListener(evType, fn, false);
309                return true;
310        } else if (obj.attachEvent){
311                var r = obj.attachEvent('on'+evType, fn);
312                return r;
313        } else {
314                return false;
315        }
316}
317
318addEvent(window, "load", initLivePreview);
319
320        <?php
321        // Add the MD5 functions using PHP so we only
322        // need to make 1 request to the web server for JS
323        $plugin_path = dirname(__FILE__);
324        $md5_file = $plugin_path . '/md5.js';
325        @include($md5_file);
326       
327        // We're done outputting JS
328        die();
329}
330
331function live_preview($before='', $after='') {
332        global $livePreviewDivAdded;
333        if($livePreviewDivAdded == false) {
334                // We don't want this included in every page
335                // so we add it here instead of using the wphead filter
336                echo '<script src="' . get_option('home') . '/?live-comment-preview.js" type="text/javascript"></script>';
337                echo $before.'<div id="commentPreview"></div>'.$after;
338                $livePreviewDivAdded = true;
339        }
340}
341
342function lcp_add_preview_div($post_id) {
343        live_preview();
344        return $post_id;
345}
346
347$livePreviewDivAdded == false;
348
349if( stristr($_SERVER['REQUEST_URI'], 'live-comment-preview.js') ) {
350        add_action('template_redirect', 'lcp_output_js');
351}
352
353add_action('comment_form', 'lcp_add_preview_div');
Note: See TracBrowser for help on using the repository browser.