forked from thinkswan/facebook-comments-for-wordpress
/
facebook-comments-combinecomments.php
110 lines (89 loc) · 3.79 KB
/
facebook-comments-combinecomments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**********************************
Combined comment counts (and caching)
**********************************/
function fbComments_getCachedCommentCount($xid, $wpCommentCount) {
fbComments_log('In ' . __FUNCTION__ . "(xid=$xid, wpCommentCount=$wpCommentCount)");
global $fbc_options;
$fbCommentCount = get_option("fbComments_commentCount_$xid");
return fbComments_getProperCommentCount($fbCommentCount, $wpCommentCount);
}
function fbComments_getProperCommentCount($fbCommentCount=0, $wpCommentCount=0) {
fbComments_log('In ' . __FUNCTION__ . "(fbCommentCount=$fbCommentCount, wpCommentCount=$wpCommentCount)");
global $fbc_options, $wp_query, $wpdb;
$postId = $wp_query->post->ID;
// $comments = $wpdb->get_row("SELECT comment_count as count FROM wp_posts WHERE ID = '$postId'");
// $commentcount = $comments->count;
// wp_reset_query();
# If the WordPress comments are hidden, just return the Facebook comments count
if ($fbc_options['hideWpComments']) {
// if (!comments_open()) return $wpCommentCount;
fbComments_log(" Returning a Facebook comment count of $fbCommentCount");
return $fbCommentCount;
# If commenting is closed on this post or we shouldn't be displaying Facebook comments due to settings, just return the WordPress comments count
} elseif (!comments_open() ||
($fbc_options['displayPagesOrPosts'] == 'pages') && (!is_page()) ||
($fbc_options['displayPagesOrPosts'] == 'posts') && (!is_single())) {
return $wpCommentCount;
} else {
fbComments_log(sprintf(' Returning a combined comment count of %d', $fbCommentCount+$wpCommentCount));
return $fbCommentCount + $wpCommentCount;
}
}
/*
* Loops through all posts and caches the Facebook comment count
*
* Thanks to Almog Baku for help with the Facebook API calls (http://www.almogbaku.com/)
*/
function fbComments_cacheAllCommentCounts() {
global $fbc_options;
fbComments_log('In ' . __FUNCTION__ . '()');
$fb = fbComments_getFbApi();
$posts = get_posts(array('numberposts' => -1)); // Retrieve all posts
if ($posts) {
fbComments_log(sprintf(' Looping through %d posts', count($posts)));
foreach ($posts as $post) {
$xid = $fbc_options['xid'] . "_post{$post->ID}";
$query = array(
'method' => 'fql.query',
'query' => 'SELECT count FROM comments_info WHERE app_id="' . $fb->getAppId() . '" AND xid="' . $xid . '"'
);
try {
fbComments_log(" Retrieving Facebook comment count for post with xid=$xid");
$result = $fb->api($query);
if ($result) {
update_option("fbComments_commentCount_$xid", $result[0]['count']);
}
} catch (FacebookApiException $e) {
fbComments_log(" FAILED to retrieve Facebook comment count for post with xid=$xid error: $e");
}
}
}
}
function fbComments_combineCommentCounts($value) {
fbComments_log('In ' . __FUNCTION__ . "(value=$value)");
global $fbc_options, $wp_query;
$postId = $wp_query->post->ID;
$xid = $fbc_options['xid'] . "_post$postId";
// Return the cached comment count (if it exists)
if (get_option("fbComments_commentCount_$xid") !== null) {
return fbComments_getCachedCommentCount($xid, $value);
}
$fb = fbComments_getFbApi();
$query = array(
'method' => 'fql.query',
'query' => 'SELECT count FROM comments_info WHERE app_id="' . $fb->getAppId() . '" AND xid="' . $xid . '"'
);
try {
fbComments_log(" Comment count wasn't cached. Retrieving Facebook comment count for post with xid=$xid");
$result = $fb->api($query);
if ($result) {
// Cache the Facebook comment count
update_option("fbComments_commentCount_$xid", $result[0]['count']);
return fbComments_getProperCommentCount($result[0]['count'], $value);
}
} catch (FacebookApiException $e) {}
fbComments_log(" FAILED to retrieve Facebook comment count for post with xid=$xid");
return fbComments_getProperCommentCount(0, $value);
}
?>