-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-ajaxify-comments.php
186 lines (161 loc) · 6.24 KB
/
wp-ajaxify-comments.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
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
<?php
/*
Plugin Name: Ajaxify Comments - Ajax and Lazy Loading Comments
Plugin URI: https://dlxplugins.com/plugins/ajaxify-comments/
Description: Ajaxify Comments hooks into your current theme and adds AJAX functionality to the comment form. It also supports lazy loading of the comments section.
Author: DLX Plugins
Author URI: https://dlxplugins.com/plugins/ajaxify-comments/
Version: 3.0.3
License: GPLv2
Text Domain: wp-ajaxify-comments
*/
namespace DLXPlugins\WPAC;
define( 'WPAC_VERSION', '3.0.3' );
define( 'WPAC_PLUGIN_NAME', 'WP Ajaxify Comments' );
define( 'WPAC_SETTINGS_URL', 'admin.php?page=wp-ajaxify-comments' );
define( 'WPAC_DOMAIN', 'wpac' );
define( 'WPAC_OPTION_PREFIX', WPAC_DOMAIN . '_' ); // used to save options in version <=0.8.0
define( 'WPAC_OPTION_KEY', WPAC_DOMAIN ); // used to save options in version >= 0.9.0
define( 'WPAC_FILE', __FILE__ );
/**
* The next group of constants are used for matching strings and should not be changed.
*/
if ( function_exists( 'is_wp_version_compatible' ) && is_wp_version_compatible( '5.5' ) ) {
define( 'WPAC_WP_ERROR_PLEASE_TYPE_COMMENT', '<strong>Error:</strong> Please type your comment text.' );
} else {
define( 'WPAC_WP_ERROR_PLEASE_TYPE_COMMENT', '<strong>Error:</strong> Please type a comment.' );
}
define( 'WPAC_WP_ERROR_COMMENTS_CLOSED', 'Sorry, comments are closed for this item.' );
define( 'WPAC_WP_ERROR_MUST_BE_LOGGED_IN', 'Sorry, you must be logged in to post a comment.' );
define( 'WPAC_WP_ERROR_FILL_REQUIRED_FIELDS', '<strong>Error:</strong> Please fill the required fields (name, email).' );
define( 'WPAC_WP_ERROR_INVALID_EMAIL_ADDRESS', '<strong>Error:</strong> Please enter a valid email address.' );
define( 'WPAC_WP_ERROR_POST_TOO_QUICKLY', 'You are posting comments too quickly. Slow down.' );
define( 'WPAC_WP_ERROR_DUPLICATE_COMMENT', 'Duplicate comment detected; it looks as though you’ve already said that!' );
// Support for site-level autoloading.
if ( file_exists( __DIR__ . '/lib/autoload.php' ) ) {
require_once __DIR__ . '/lib/autoload.php';
}
require_once Functions::get_plugin_dir( 'functions.php' );
/**
* Run when the plugin is loaded.
*/
function plugins_loaded() {
if ( is_admin() ) {
$admin = new Admin\Init();
$admin->init();
}
// Init menu helper.
$menu_helper = new Menu_Helper();
$menu_helper->run();
// Init lazy load.
$lazy_load = new Lazy_Load();
$lazy_load->run();
// Load die handler. This should only affect if `HTTP_X_WPAC_REQUEST` server var is set.
add_filter( 'wp_die_handler', 'wpac_wp_die_handler' );
// For checking when a theme has changed.
add_action( 'switch_theme', __NAMESPACE__ . '\wpac_has_switched_theme' );
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\plugins_loaded' );
/**
* Run when the theme has been switched.
*
* We'll use this to alert the user to re-save settings.
*/
function wpac_has_switched_theme() {
update_option( 'wpac_theme_has_changed', true );
}
/**
* Run when WP has finished loading in.
*
* Queries should be initialized by now, so we can check for post variables.
*/
function wp() {
/**
* Fires before the main WPAC actions/filters. Useful for hooking in early.
*/
do_action( 'dlxplugins/ajaxify/comments/wp' );
// If Ajaxify enabled, run the plugin.
if ( Functions::is_ajaxify_enabled() ) {
add_filter( 'the_comments', 'wpac_comments_template_query_args_filter' );
add_action( 'wp_enqueue_scripts', 'wpac_initialize', 9 );
add_action( 'wp_enqueue_scripts', 'wpac_enqueue_scripts' );
add_filter( 'option_page_comments', 'wpac_option_page_comments' );
add_filter( 'option_comments_per_page', 'wpac_option_comments_per_page' );
// For Genesis compatibility.
add_filter( 'genesis_before_comments', 'wpac_genesis_before_comments' );
}
}
add_action( 'wp', __NAMESPACE__ . '\wp' );
/**
* Fires before a comment is posted.
*
* @param int $comment_post_id The post ID the comment will be posted to.
*/
global $wpac_global_comment_post_id;
function pre_comment_on_post( $comment_post_id ) {
global $wpac_global_comment_post_id;
$wpac_global_comment_post_id = $comment_post_id;
add_filter( 'gettext', 'wpac_filter_gettext', 20, 3 ); // todo - need to run earlier.
}
add_action( 'pre_comment_on_post', __NAMESPACE__ . '\pre_comment_on_post' );
/* Setup plugin activation and redirection */
register_activation_hook( __FILE__, __NAMESPACE__ . '\on_plugin_activation' );
add_action( 'admin_init', __NAMESPACE__ . '\activate_redirect' );
/**
* Determine if a user can be redirected or not.
*
* @return true if the user can be redirected. false if not.
*/
function can_redirect_on_activation() {
/**
* Filter whether to redirect on plugin activation.
*
* @param bool $can_redirect Whether to redirect on plugin activation. Pass `false` to prevent redirect.
*/
$can_redirect = apply_filters( 'dlxplugins/ajaxify/comments/activation/can_redirect', true );
if ( false === $can_redirect ) {
return false;
}
// If plugin is activated in network admin options, skip redirect.
if ( is_network_admin() ) {
return false;
}
// Skip redirect if WP_DEBUG is enabled.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
return false;
}
// Check for dev mode.
if ( function_exists( 'wp_get_development_mode' ) ) {
if ( ! empty( wp_get_development_mode() ) ) {
return false;
}
}
// Determine if multi-activation is enabled.
$maybe_multi = filter_input( INPUT_GET, 'activate-multi', FILTER_VALIDATE_BOOLEAN );
if ( $maybe_multi ) {
return false;
}
// All is well. Can redirect.
return true;
}
/**
* Callback when the plugin is activated.
*/
function on_plugin_activation() {
if ( can_redirect_on_activation() ) {
// Add option for whether to redirect.
add_option( 'wpac_do_activation_redirect', sanitize_text_field( __FILE__ ) );
}
}
/**
* Redirect in the admin upon plugin activation.
*/
function activate_redirect() {
if ( can_redirect_on_activation() && is_admin() ) {
if ( __FILE__ === get_option( 'wpac_do_activation_redirect' ) ) {
delete_option( 'wpac_do_activation_redirect' );
wp_safe_redirect( esc_url_raw( add_query_arg( array( 'first_time_install' => true ), Functions::get_settings_url() ) ) );
exit;
}
}
}