/static/wp-content/plugins/simple-facebook-connect/sfc-getcomm.php

https://github.com/elleeott/WPOC-boilerplate · PHP · 150 lines · 116 code · 26 blank · 8 comment · 22 complexity · 80e88204fdeafffed1ac8e94b7f804ee MD5 · raw file

  1. <?php
  2. // gets comments from FB for the post, based on the auto-published post
  3. function sfc_getcomm_check($comments, $id) {
  4. $fbpagepost = get_post_meta($id,'_fb_post_id_app',true);
  5. $fbprofpost = get_post_meta($id,'_fb_post_id_profile',true);
  6. if (!$fbpagepost && !$fbprofpost) return $comments;
  7. $options = get_option('sfc_options');
  8. if ($fbpagepost || $fbprofpost) {
  9. if ( false === ( $newcomms = get_transient('sfcgetcomm-'.$id) ) ) {
  10. $newcomms = array();
  11. // get comments from the app or page post
  12. if ($fbpagepost && !empty($options['fanpage'])) {
  13. $token = $options['page_access_token'];
  14. $fbresp = sfc_remote($fbpagepost, 'comments', array('access_token'=>$token));
  15. if (!empty($fbresp['data'])) {
  16. foreach ($fbresp['data'] as $fbcomm) {
  17. $nc=null;
  18. $nc->comment_ID = $fbcomm['id'];
  19. $nc->fbid = $fbcomm['from']['id'];
  20. $nc->comment_post_ID = $id;
  21. $nc->comment_author = $fbcomm['from']['name'];
  22. $nc->comment_author_email = '';
  23. $nc->comment_author_url = 'http://www.facebook.com/profile.php?id='.$fbcomm['from']['id'];
  24. $nc->comment_author_IP = '';
  25. $time = strtotime ($fbcomm['created_time']);
  26. $nc->comment_date = date('Y-m-d H:i:s', $time);
  27. $nc->comment_date_gmt = gmdate('Y-m-d H:i:s', $time);;
  28. $nc->comment_content = $fbcomm['message'];
  29. $nc->comment_karma = 0;
  30. $nc->comment_approved = 1;
  31. $nc->comment_agent = 'SFC/1.0 (WordPress; en-US) SFC-GetComments/1.0';
  32. $nc->comment_type = '';
  33. $nc->comment_parent = 0;
  34. $nc->user_id = 0;
  35. wp_cache_add($nc->comment_ID, $nc, 'comment');
  36. $newcomms[] = $nc;
  37. }
  38. }
  39. }
  40. // get comments from the profile post
  41. if ($fbprofpost) {
  42. $token = $options['access_token'];
  43. $fbresp = sfc_remote($fbprofpost, 'comments', array('access_token'=>$token));
  44. if (!empty($fbresp['data'])) {
  45. foreach ($fbresp['data'] as $fbcomm) {
  46. $nc=null;
  47. $nc->comment_ID = $fbcomm['id'];
  48. $nc->fbid = $fbcomm['from']['id'];
  49. $nc->comment_post_ID = $id;
  50. $nc->comment_author = $fbcomm['from']['name'];
  51. $nc->comment_author_email = '';
  52. $nc->comment_author_url = 'http://www.facebook.com/profile.php?id='.$fbcomm['from']['id'];
  53. $nc->comment_author_IP = '';
  54. $time = strtotime ($fbcomm['created_time']);
  55. $nc->comment_date = date('Y-m-d H:i:s', $time);
  56. $nc->comment_date_gmt = gmdate('Y-m-d H:i:s', $time);;
  57. $nc->comment_content = $fbcomm['message'];
  58. $nc->comment_karma = 0;
  59. $nc->comment_approved = 1;
  60. $nc->comment_agent = 'SFC/1.0 (WordPress; en-US) SFC-GetComments/1.0';
  61. $nc->comment_type = '';
  62. $nc->comment_parent = 0;
  63. $nc->user_id = 0;
  64. wp_cache_add($nc->comment_ID, $nc, 'comment');
  65. $newcomms[] = $nc;
  66. }
  67. }
  68. }
  69. set_transient('sfcgetcomm-'.$id, $newcomms, 6*60*60); // 6 hours seems reasonable
  70. }
  71. global $sfc_getcomm_counts;
  72. $sfc_getcomm_counts[$id] = count($newcomms);
  73. // build a new array based on the two existing arrays
  74. if ( !empty($newcomms) ) {
  75. $finalcomm=array();
  76. while ( !empty($comments) || !empty($newcomms) ) {
  77. if (empty($comments)) {
  78. $finalcomm = array_merge($finalcomm, $newcomms);
  79. $newcomms = array();
  80. }
  81. if (empty($newcomms)) {
  82. $finalcomm = array_merge($finalcomm, $comments);
  83. $comments = array();
  84. }
  85. if ( strtotime($comments[0]->comment_date) < strtotime($newcomms[0]->comment_date) ) {
  86. $finalcomm[] = array_shift($comments);
  87. } else {
  88. $finalcomm[] = array_shift($newcomms);
  89. }
  90. }
  91. $comments = $finalcomm;
  92. }
  93. }
  94. return $comments;
  95. }
  96. add_filter('comments_array', 'sfc_getcomm_check', 10, 2);
  97. // fix the comment count to have the FB-imported comments
  98. function sfc_getcomm_correct_count($count, $id) {
  99. global $sfc_getcomm_counts;
  100. if ( $sfc_getcomm_counts[$id] ) $count += $sfc_getcomm_counts[$id];
  101. return $count;
  102. }
  103. add_filter('get_comments_number','sfc_getcomm_correct_count', 10, 2);
  104. // add a "facebook" class to FB-imported comments
  105. function sfc_getcomm_class($classes) {
  106. if ( !empty($GLOBALS['comment']->fbid) ) {
  107. $classes[] = 'facebook';
  108. }
  109. return $classes;
  110. }
  111. add_filter('comment_class','sfc_getcomm_class', 10, 1);
  112. // show FB avatars for FB-imported comments
  113. function sfc_getcomm_avatar($avatar, $id_or_email, $size = '96', $default = '', $alt = false) {
  114. if ( !is_object($id_or_email) || !isset($id_or_email->fbid))
  115. return $avatar;
  116. return "<img width='{$size}' height='{$size}' class='avatar avatar-{$size} fbavatar' src='http://graph.facebook.com/{$id_or_email->fbid}/picture?type=square' />";
  117. }
  118. add_filter('get_avatar','sfc_getcomm_avatar', 10, 5);
  119. // remove the reply link fron FB-imported comments
  120. function sfc_getcomm_reply_link($link, $args, $comment, $post) {
  121. if (!empty($comment->fbid)) $link = '';
  122. return $link;
  123. }
  124. add_filter('comment_reply_link','sfc_getcomm_reply_link',10,4);