Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 376 lines (319 sloc) 13.863 kB
3d79827 Added version 3.1.3 from http://plugins.svn.wordpress.org/facebook-co…
Steven Mathias authored
1 <?php
2
3 /**********************************
4 Dashboard recent comments widget
5 **********************************/
6 /**
7 * Display recent comments from facebook in a dashboard widget.
8 *
9 * @since 3.0.0
10 */
11 function fbcomments_dashboard_widget_function() {
12 global $fbc_options;
13
14 wp_register_style('widgets', FBCOMMENTS_CSS_WIDGETS, array(), FBCOMMENTS_VER);
15 wp_enqueue_style('widgets');
16
17 // needed for fb api call? excep 104 without it
18 fbComments_storeAccessToken();
19
20 $atoken = $fbc_options['accessToken'];
21 $fb = fbComments_getFbApi();
22
23
24 $commentsq = "SELECT fromid, text, id, time, username, xid, object_id ".
25 "FROM comment WHERE xid IN (SELECT xid FROM comments_info WHERE app_id={$fbc_options['appId']})".
26 "ORDER BY time desc";
27 $usersq = "SELECT id, name, url, pic_square FROM profile ".
28 "WHERE id IN (SELECT fromid FROM #comments)";
29
30 $query = '{
31 "comments": "' . $commentsq . '",
32 "users": "' . $usersq . '"
33 }';
34
35 $query = array("method"=>"fql.multiquery","queries"=>$query,'access_token'=>$atoken);
36 $result = $fb->api($query);
37
38 $comments = $result;
39 if (!is_array($comments)) { print_r('No&nbsp;response&nbsp;from&nbsp;facebook.com'); return; }
40 $ncomms = sizeof($comments[0]['fql_result_set']);
41 $dcomms = $ncomms < $fbc_options['dashNumComments'] ? $ncomms : $fbc_options['dashNumComments'];
42
43 if ($ncomms == 0) { echo "<div style='text-align:right; font-size:.8em'>
44 <a href='https://developers.facebook.com/tools/comments?id={$fbc_options['appId']}'>Administer Comments</a></div><hr />"
45 .'No Comments!';
46 } else {
47 // $ncomms = $ncomms < 10 ? $ncomms : 10;
48
49 $htmlout =
50 // using the old api to make calling from js easier
51 // the new graph api method is much cleaner, but it causes problems in opera
52 // since it returns a value, which opera then prompts the user to open or save
53 "<div id=\"fb-root\"></div>
54 <script>
55 window.fbAsyncInit = function() {
56 FB.init({appId: '{$fbc_options['appId']}', status: true, cookie: true, xfbml: true});
57 };
58 (function() {
59 var e = document.createElement('script'); e.async = true;
60 e.src = document.location.protocol +
61 '//connect.facebook.net/en_US/all.js';
62 document.getElementById('fb-root').appendChild(e);
63 }());
64 </script>"
65 ."<div style='text-align:right; font-size:.8em'>
66 <a href='https://developers.facebook.com/tools/comments?id={$fbc_options['appId']}'>Administer Comments</a></div><hr />"
67 // should probably change this to class so that it validates
68 .'<div id="the-comment-list" class="list:comment" style="margin-top: -1em">';
69
70 $parity = '';
71 $users = $comments[1]['fql_result_set'];
72 $comments = $comments[0]['fql_result_set'];
73 for ($i=0,$par=0;$i<$dcomms;$i++,$par++) {
74 // for people who use the same app id for more than one site,
75 // only return results unique to this xid
76 if ( strncmp($comments[$i]['xid'],$fbc_options['xid'],15) ) { $par--; continue; }
77
78 // find matching user
79 for ($j=0;$j<count($users);$j++) {
80 if ($comments[$i]['fromid'] == $users[$j]['id']) {
81 $index=$j;
82 break;
83 }
84 }
85
86 // Comment username and Link
87 $username = $comments[$i]['fromid'];
88 if ($username == '1309634065') { // if anon user
89 $username = '<span class="aname">$comments[$i][username]</span>';
90 } else {
91 $username = '<a target="_blank" href="https://www.facebook.com/profile.php?id='
92 . $username
93 .'">'. $users[$index]['name'] .'</a>';
94 }
95
96 // make pretty
97 $commenttext = $comments[$i]['text'];
98 $order = array("\r\n", "\n", "\r");
99 $replace = '<br />';
100
101 // Processes \r\n's first so they aren't converted twice.
102 $commenttext = str_replace($order, $replace, $commenttext);
103
104 // url of post/page on which comment was made
105 $post_id = substr($comments[$i]['xid'],20);
106 $commenturl = get_permalink($post_id);
107
108 // make pretty alternations
109 $parity = ($par&1)
110 ? "comment byuser comment-author-admin odd alt thread-odd thread-alt depth-1 comment-item approved":
111 "comment byuser comment-author-admin even thread-even depth-1 comment-item approved";
112
113 $imgurl = $users[$index]['pic_square'];
114
115 // what will be written to the dashboard widget
116 $htmlout .=
117 '<div class="'.$parity.'">'.
118 '<img alt="" src="'.$imgurl.'" class="avatar avatar-50 photo" height="50" width="50"/>'.
119 '<div class="dashboard-comment-wrap">'.
120 '<h4 class="comment-meta"> From '.
121 '<cite class="comment-author"><a href="https://www.facebook.com/profile.php?id='.$comments[$i]['fromid'].'">'.$username.'</a></cite> on '.
122 '<a href="'.$commenturl.'">'.get_post($post_id)->post_title.'</a>
123 <abbr style="font-size:.8em" title="'.date('r',$comments[$i]['time']).'"> '.date('d M Y',$comments[$i]['time']).'</abbr>
124 <span class="approve">[Pending]</span>'.
125 '</h4>
126 <blockquote>
127 <p>'.$commenttext.'</p>
128 </blockquote>
129 <p class="row-actions">
130 <span class="trash wallkit_actionset">
131
132 <a id="deletecomm'.$i.'" href="#" class="delete vim-d vim-destructive" title="Delete this comment">
133 delete
134 </a>
135
136 </span>'.
137 "<script>
138 jQuery('#deletecomm".$i."').click(function(data) {
139 FB.api({
140 method: 'comments.remove',"
141 .'comment_id: "'.$comments[$i]['id'].'", '
142 .'xid: "'.$comments[$i]['xid'].'", '
143 .'access_token: "'.$atoken.'", '
144 ."},
145 function(response) {
146 if (!response || response.error_code) {
147 alert('ERROR: Failed to delete comment.');
148 } else {
149 alert('Comment Deleted');
150 window.location.reload();
151 }
152 });
153 });
154 </script>
155 </p>
156 </div>
157 </div>";
158 }
159 $htmlout .= '</div>';
160 print_r($htmlout);
161 }
162 }
163
164 // Create the function used in the action hook
165 function fbcomments_add_dashboard_widgets() {
166 global $fbc_options;
167 if ($fbc_options['showDBWidget'] == true)
168 wp_add_dashboard_widget('dashboard_widget', 'Recent Facebook comments', 'fbcomments_dashboard_widget_function');
169 }
170
171
172 /**********************************
173 Page recent comments widget
174 **********************************/
175 /* add styles before wp_head is loaded
176 see: http://bit.ly/igYFYu
177 */
178 function conditionally_add_scripts_and_styles($posts){
179 if (empty($posts)) return $posts;
180
181 // enqueue here
182 wp_enqueue_style('fbc_rc_widgets-style', FBCOMMENTS_CSS_WIDGETS);
183
184 return $posts;
185 }
186
187 /**
188 * Recent_Comments widget class
189 *
190 * @since 3.0.0
191 */
192 class FBCRC_Widget extends WP_Widget {
193 /** constructor */
194 function FBCRC_Widget() {
195 $widget_ops = array( 'description' => __('The most recent Facebook comments.') );
196 parent::WP_Widget(false, $name = 'Recent Facebook Comments', $widget_ops);
197 }
198
199 /** @see WP_Widget::widget */
200 function widget($args, $instance) {
201 extract( $args );
202 $title = apply_filters('widget_title', $instance['title']);
203
204 global $fbc_options;
205 $atoken = $fbc_options['accessToken'];
206
207 $fb = fbComments_getFbApi();
208 /*
209 select post_id, fromid, time, text, post_fbid
210 from comment
211 where object_id
212 in (select comments_fbid from link_stat where url="http://developers.facebook.com/blog/post/472")'
213 */
214 $commentsq = "SELECT fromid, text, id, time, username, xid, object_id ".
215 "FROM comment WHERE xid IN (SELECT xid FROM comments_info WHERE app_id={$fbc_options['appId']})".
216 "ORDER BY time desc";
217 // $commurl = urlencode('');
218 // $commentsq = "SELECT fromid, text, id, time, username, xid, post_id, post_fbid, object_id ".
219 // "FROM comment ".
220 // "WHERE (xid IN (SELECT xid FROM comments_info WHERE app_id={$fbc_options['appId']})) ".
221 // "OR object_id IN (SELECT comments_fbid FROM link_stat WHERE url='$commurl') ".
222 // "ORDER BY time desc";
223
224 // $commentsq = "SELECT post_id, fromid, time, text, post_fbid ".
225 // 'FROM comment'.
226 // "ORDER BY time desc";
227 $usersq = "SELECT id, name, url, pic_square FROM profile ".
228 "WHERE id IN (SELECT fromid FROM #comments)";
229
230 $query = '{
231 "comments": "' . $commentsq . '",
232 "users": "' . $usersq . '"
233 }';
234
235
236 $query = array("method"=>"fql.multiquery","queries"=>$query,'access_token'=>$atoken);
237
238 $comments = $fb->api($query);
239
240 if ( ! $number = (int) $instance['number'] )
241 $number = 5;
242 else if ( $number < 1 )
243 $number = 1;
244 $ncomms = sizeof($comments[0]['fql_result_set']);
245
246 // if no comments, display no comments; otherwise display the greater of $ncomms and $number comments
247 $ncomms = $ncomms == 0 ? 0 : ($ncomms < $number ? $ncomms : $number);
248 $output = '<ul id="fbc_rc_widget">';
249
250 $parity = '';
251 $users = $comments[1]['fql_result_set'];
252 $comments = $comments[0]['fql_result_set'];
253
254 $show_avatar = isset($instance['show_avatar']) ? $instance['show_avatar'] : true;
255
256 for ($i=0,$par=0;$i<$ncomms;$i++,$par++) {
257 if ( strncmp($comments[$i]['xid'],$fbc_options['xid'],15) ) { $par--; continue; }
258 // find matching user
259 for ($j=0;$j<count($users);$j++) {
260 if ($comments[$i]['fromid'] == $users[$j]['id']) {
261 $index=$j;
262 break;
263 }
264 }
265
266
267 // Comment meta
268 $username = $comments[$i]['fromid'];
269 if ($username == '1309634065') { // if anon user
270 $username = $comments[$i]['username'];
271 } else {
272 $username = '<a target="_blank" href="https://www.facebook.com/profile.php?id='.$username.'">'.$users[$index]['name'].'</a>';
273 }
274
275 // print user defined number of words, if there are less than this, don't trim
276 $commenttext = trim($comments[$i]['text'], ' ');
277 $nwords = count(explode(" ",$commenttext));
278 $dwords = $nwords <= $instance['word_count'] ? $nwords : $instance['word_count'];
279 if ($nwords > $dwords) {
280 preg_match("/^(\S+\s+){0,$dwords}/", $commenttext, $matches); // match spaces (nth space will be at end of nth word)
281 $commenttext = trim($matches[0]) . '[...]';
282 }
283
284 // print line breaks as such
285 $order = array("\r\n", "\n", "\r"); // Processes \r\n's first so they aren't converted twice.
286 $replace = '<br />';
287 $commenttext = str_replace($order, $replace, $commenttext);
288
289 // url of post/page on which comment was made
290 $post_id = substr($comments[$i]['xid'],20);
291 $commenturl = get_permalink($post_id);
292
293 // to allow alternating styles on comments
294 $parity = ($par&1) ? "odd": "even";
295
296 // display avatar only if option is checked
297 if ($show_avatar) {
298 $imgurl = $users[$index]['pic_square'];
299 $imgclass = '';
300 } else {
301 $imgurl = $users[$index]['pic_square'];
302 $imgclass = 'style="display:none"';
303 }
304
305 // what will be written to the widget
306 $output .=
307 '<li class="fbc_rc_comment '.$parity.'">
308 <div class="fbc_rc_comment-meta">
309 <cite class="fbc_rc_comment-author"><a href="https://www.facebook.com/profile.php?id='.$comments[$i]['fromid'].'">'.$username.'</a></cite>
310 <abbr class="fbc_rc_date" title="'.date('r',$comments[$i]['time']).'">'.date('d M Y',$comments[$i]['time']).'</abbr>
311 </div>
312 <img alt="" src="'.$imgurl.'" class="avatar" height="50" width="50" '.$imgclass.' />
313 <div class="fbc_rc_text">'.$commenttext.'</div>
314 <div class="fbc_rc_permalink"><a href="'.$commenturl.'"> '.get_post($post_id)->post_title.'</a></div>
315 </li>';
316 }
317 $output .= '</ul>';
318
319 // print everything out
320 echo $before_widget;
321 if ( $title )
322 echo $before_title . $title . $after_title;
323 echo $output;
324 echo $after_widget;
325 }
326
327 /** @see WP_Widget::update */
328 function update($new_instance, $old_instance) {
329 $instance = $old_instance;
330 $instance['title'] = strip_tags($new_instance['title']);
331 $instance['number'] = (int) $new_instance['number'];
332 $instance['word_count'] = (int) $new_instance['word_count'];
333 if ( isset($new_instance['show_avatar']) )
334 $instance['show_avatar'] = 1;
335 else
336 $instance['show_avatar'] = 0;
337 return $instance;
338 }
339
340 /** @see WP_Widget::form */
341 function form($instance) {
342 $instance = wp_parse_args( (array) $instance, array( 'show_avatar' => true ) );
343 $title = isset($instance['title']) ? esc_attr($instance['title']) : 'Recent Comments';
344 $number = isset($instance['number']) ? absint($instance['number']) : 5;
345 $word_count = isset($instance['word_count']) ? absint($instance['word_count']) : 50;
346
347 ?>
348 <p>
349 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
350 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
351 </p>
352 <p>
353 <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Max number of comments to show:'); ?></label>
354 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" />
355 </p>
356 <p>
357 <label for="<?php echo $this->get_field_id('word_count'); ?>"><?php _e('Max number of words per comment to show:'); ?></label>
358 <input id="<?php echo $this->get_field_id('word_count'); ?>" name="<?php echo $this->get_field_name('word_count'); ?>" type="text" value="<?php echo $word_count; ?>" size="3" />
359 </p>
360 <p>
361 <input class="checkbox" type="checkbox" <?php checked( $instance['show_avatar'], true ); ?> id="<?php echo $this->get_field_id( 'show_avatar' ); ?>" name="<?php echo $this->get_field_name( 'show_avatar' ); ?>" />
362 <label for="<?php echo $this->get_field_id( 'show_avatar' ); ?>">Check to display avatar</label>
363 </p>
364 <?php
365 }
366
367 } // class FBCRC_Widget
368
369 function fbComments_dashboard_widget_init() {
370 if (is_admin()) {
371 wp_enqueue_script('jquery');
372 }
373 }
374
375 ?>
Something went wrong with that request. Please try again.