-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastcomments-wordpress-plugin.php
More file actions
160 lines (137 loc) · 5.71 KB
/
fastcomments-wordpress-plugin.php
File metadata and controls
160 lines (137 loc) · 5.71 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
<?php
/*
Plugin Name: FastComments
Plugin URI: https://fastcomments.com
Description: A live, fast, privacy-focused commenting system with advanced spam prevention capabilities.
Version: 3.18.0
Author: winrid @ FastComments
License: GPL-2.0+
*/
// Safe guard to prevent loading this from outside wordpress.
if (!defined('WPINC')) {
die;
}
$FASTCOMMENTS_VERSION = 3.180;
require_once plugin_dir_path(__FILE__) . 'admin/fastcomments-admin.php';
require_once plugin_dir_path(__FILE__) . 'public/fastcomments-public.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');
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');