Repository navigation
Expand file tree
/
Copy pathfastcomments-wordpress-plugin.php
More file actions
217 lines (188 loc) · 8.04 KB
/
Copy pathfastcomments-wordpress-plugin.php
File metadata and controls
217 lines (188 loc) · 8.04 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/*
Plugin Name: FastComments
Plugin URI: https://fastcomments.com
Description: A live, fast, privacy-focused commenting system with advanced spam prevention capabilities.
Version: 3.19.8
Author: winrid @ FastComments
License: GPL-2.0+
*/
// Safe guard to prevent loading this from outside wordpress.
if (!defined('WPINC')) {
die;
}
$FASTCOMMENTS_VERSION = 3.198;
require_once plugin_dir_path(__FILE__) . 'admin/fastcomments-admin.php';
require_once plugin_dir_path(__FILE__) . 'public/fastcomments-public.php';
require_once plugin_dir_path(__FILE__) . 'public/fastcomments-sidebar-widgets.php';
$fastcomments_public = new FastCommentsPublic();
$fastcomments_public->setup_api_listeners(); // TODO able to do this without new()?
// Returns the FastComments embed comments template
function fc_comments_template()
{
$path = plugin_dir_path(__FILE__) . 'public/fastcomments-widget-view.php';
if (!file_exists($path)) {
throw new Exception("Could not find file! $path");
}
return $path;
}
// Returns the FastComments embed comments template
// This method takes more arguments, like post id, but we found it not to be reliable.
function fc_comment_count_template($text_no_comments = "")
{
global $post;
$post_id = -1;
if (isset($post) && $post->ID) {
$post_id = $post->ID;
}
// we add opacity here to prevent flash of content when rendering the original text and then loading our script. We have a style override for users without JS.
return "<span class=\"fast-comments-count\" data-fast-comments-url-id=\"$post_id\" style=\"opacity: 0;\"'>$text_no_comments</span>";
}
// Sets up the FastComments embed comment count script if needed. This is done this way, with wp_footer, to prevent loading an external script.
function fc_add_comment_count_scripts()
{
global $post;
if (!isset($post) || is_singular()) {
return;
}
global $FASTCOMMENTS_VERSION;
$cdn = FastCommentsPublic::getCDN();
wp_enqueue_script('fastcomments_widget_count', "$cdn/js/embed-widget-comment-count-bulk.min.js", array(), $FASTCOMMENTS_VERSION, true);
}
// Sets up the FastComments embed comment count script if needed. This is done this way, with wp_footer, to prevent loading an external script.
function fc_add_comment_count_config()
{
global $post;
if (!isset($post) || is_singular()) {
return;
}
$jsonFcConfig = json_encode(array(
"tenantId" => get_option('fastcomments_tenant_id')
));
echo "<script>window.FastCommentsBulkCountConfig = $jsonFcConfig;</script>";
echo "<noscript><style>.fast-comments-count { opacity: 1 !important; }</style></noscript>";
}
// Comments can load as long as we have a tenant id.
if (get_option('fastcomments_tenant_id')) {
add_filter('comments_template', 'fc_comments_template', 9001);
function fc_comment_block_template($block_content, $parsed_block)
{
if ('core/comments' === $parsed_block['blockName'] && get_option('fastcomments_tenant_id')) {
$path = fc_comments_template();
ob_start();
require_once $path;
return ob_get_clean();
}
return $block_content;
}
add_filter('pre_render_block', 'fc_comment_block_template', 9002, 2);
add_filter('comments_number', 'fc_comment_count_template', 9002);
add_filter('wp_enqueue_scripts', 'fc_add_comment_count_scripts', 100);
add_filter('wp_footer', 'fc_add_comment_count_config', 100);
// Prevent spam bots from submitting to WordPress's native comment endpoints
add_action('pre_comment_on_post', 'fc_block_native_submissions');
add_filter('rest_pre_insert_comment', 'fc_block_rest_comments', 10, 2);
}
// Block submissions to wp-comments-post.php
function fc_block_native_submissions($comment_post_ID) {
// When FastComments is active, block native WordPress comment submissions
if (get_option('fastcomments_tenant_id')) {
wp_die(__('Comments must be submitted through FastComments.', 'fastcomments'), __('Comments Disabled', 'fastcomments'), array('response' => 403));
}
}
// Block REST API comment submissions
function fc_block_rest_comments($prepared_comment, $request) {
// When FastComments is active, block REST API comment submissions
if (get_option('fastcomments_tenant_id')) {
return new WP_Error(
'fastcomments_rest_blocked',
__('Comments must be submitted through FastComments.', 'fastcomments'),
array('status' => 403)
);
}
return $prepared_comment;
}
function fastcomments_cron()
{
try {
require_once plugin_dir_path(__FILE__) . 'core/FastCommentsWordPressIntegration.php';
$fastcomments = new FastCommentsWordPressIntegration();
$fastcomments->log('debug', 'Begin cron tick.');
$fastcomments->tick();
$fastcomments->log('debug', 'End cron tick.');
} catch (Exception $e) {
error_log('ERROR:::FastComments cron failed: ' . $e->getMessage());
}
}
add_action('fastcomments_cron_hook', 'fastcomments_cron');
add_action('fastcomments_send_comments_continue_hook', 'fastcomments_cron');
// Keeps the FastComments account of a user current as they change in WordPress, so the user sync only has to run once.
function fc_sync_changed_user($user_id)
{
try {
require_once plugin_dir_path(__FILE__) . 'core/FastCommentsWordPressIntegration.php';
$fastcomments = new FastCommentsWordPressIntegration();
$fastcomments->syncUser($user_id);
} catch (Exception $e) {
error_log('ERROR:::FastComments failed to sync changed user ' . $user_id . ': ' . $e->getMessage());
}
}
// Avatar plugins keep the picture in user meta, so a changed picture only shows up as a meta change.
function fc_sync_changed_user_meta($meta_id, $user_id, $meta_key)
{
global $wpdb;
$avatar_meta_keys = apply_filters('fastcomments_user_avatar_meta_keys', array('simple_local_avatar', 'basic_user_avatar', $wpdb->prefix . 'user_avatar'));
if (in_array($meta_key, $avatar_meta_keys, true)) {
fc_sync_changed_user($user_id);
}
}
// Only members of the current site count on a network. A user added to this site from the network admin is new to it.
function fc_sync_user_added_to_blog($user_id, $role, $blog_id)
{
if ((int)$blog_id === get_current_blog_id()) {
fc_sync_changed_user($user_id);
}
}
add_action('user_register', 'fc_sync_changed_user');
add_action('profile_update', 'fc_sync_changed_user');
add_action('set_user_role', 'fc_sync_changed_user');
add_action('add_user_role', 'fc_sync_changed_user');
add_action('remove_user_role', 'fc_sync_changed_user');
add_action('add_user_to_blog', 'fc_sync_user_added_to_blog', 10, 3);
add_action('added_user_meta', 'fc_sync_changed_user_meta', 10, 3);
add_action('updated_user_meta', 'fc_sync_changed_user_meta', 10, 3);
add_action('deleted_user_meta', 'fc_sync_changed_user_meta', 10, 3);
function fastcomments_cron_schedules($schedules)
{
$schedules['fastcomments_five_minutes'] = array(
'interval' => 300,
'display' => 'Every 5 minutes (FastComments)'
);
$schedules['fastcomments_fifteen_minutes'] = array(
'interval' => 900,
'display' => 'Every 15 minutes (FastComments)'
);
return $schedules;
}
add_filter('cron_schedules', 'fastcomments_cron_schedules');
function fastcomments_activate()
{
require_once plugin_dir_path(__FILE__) . 'core/FastCommentsWordPressIntegration.php';
$fastcomments = new FastCommentsWordPressIntegration();
$fastcomments->activate();
}
function fastcomments_deactivate()
{
require_once plugin_dir_path(__FILE__) . 'core/FastCommentsWordPressIntegration.php';
$fastcomments = new FastCommentsWordPressIntegration();
$fastcomments->deactivate();
}
function fastcomments_update()
{
require_once plugin_dir_path(__FILE__) . 'core/FastCommentsWordPressIntegration.php';
$fastcomments = new FastCommentsWordPressIntegration();
$fastcomments->update();
}
register_activation_hook(__FILE__, 'fastcomments_activate');
register_deactivation_hook(__FILE__, 'fastcomments_deactivate');
add_action('plugins_loaded', 'fastcomments_update');