| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Ajaxify Comments - Ajax and Lazy Loading Comments |
| 4 |
Plugin URI: https://dlxplugins.com/plugins/ajaxify-comments/ |
| 5 |
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. |
| 6 |
Author: DLX Plugins |
| 7 |
Author URI: https://dlxplugins.com/plugins/ajaxify-comments/ |
| 8 |
Version: 3.0.2 |
| 9 |
License: GPLv2 |
| 10 |
Text Domain: wp-ajaxify-comments |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace DLXPlugins\WPAC; |
| 14 |
|
| 15 |
define( 'WPAC_VERSION', '3.0.2' ); |
| 16 |
define( 'WPAC_PLUGIN_NAME', 'WP Ajaxify Comments' ); |
| 17 |
define( 'WPAC_SETTINGS_URL', 'admin.php?page=wp-ajaxify-comments' ); |
| 18 |
define( 'WPAC_DOMAIN', 'wpac' ); |
| 19 |
define( 'WPAC_OPTION_PREFIX', WPAC_DOMAIN . '_' ); // used to save options in version <=0.8.0 |
| 20 |
define( 'WPAC_OPTION_KEY', WPAC_DOMAIN ); // used to save options in version >= 0.9.0 |
| 21 |
define( 'WPAC_FILE', __FILE__ ); |
| 22 |
|
| 23 |
/** |
| 24 |
* The next group of constants are used for matching strings and should not be changed. |
| 25 |
*/ |
| 26 |
if ( function_exists( 'is_wp_version_compatible' ) && is_wp_version_compatible( '5.5' ) ) { |
| 27 |
define( 'WPAC_WP_ERROR_PLEASE_TYPE_COMMENT', '<strong>Error:</strong> Please type your comment text.' ); |
| 28 |
} else { |
| 29 |
define( 'WPAC_WP_ERROR_PLEASE_TYPE_COMMENT', '<strong>Error:</strong> Please type a comment.' ); |
| 30 |
} |
| 31 |
define( 'WPAC_WP_ERROR_COMMENTS_CLOSED', 'Sorry, comments are closed for this item.' ); |
| 32 |
define( 'WPAC_WP_ERROR_MUST_BE_LOGGED_IN', 'Sorry, you must be logged in to post a comment.' ); |
| 33 |
define( 'WPAC_WP_ERROR_FILL_REQUIRED_FIELDS', '<strong>Error:</strong> Please fill the required fields (name, email).' ); |
| 34 |
define( 'WPAC_WP_ERROR_INVALID_EMAIL_ADDRESS', '<strong>Error:</strong> Please enter a valid email address.' ); |
| 35 |
define( 'WPAC_WP_ERROR_POST_TOO_QUICKLY', 'You are posting comments too quickly. Slow down.' ); |
| 36 |
define( 'WPAC_WP_ERROR_DUPLICATE_COMMENT', 'Duplicate comment detected; it looks as though you’ve already said that!' ); |
| 37 |
|
| 38 |
// Support for site-level autoloading. |
| 39 |
if ( file_exists( __DIR__ . '/lib/autoload.php' ) ) { |
| 40 |
require_once __DIR__ . '/lib/autoload.php'; |
| 41 |
} |
| 42 |
|
| 43 |
require_once Functions::get_plugin_dir( 'functions.php' ); |
| 44 |
|
| 45 |
/** |
| 46 |
* Run when the plugin is loaded. |
| 47 |
*/ |
| 48 |
function plugins_loaded() { |
| 49 |
|
| 50 |
if ( is_admin() ) { |
| 51 |
$admin = new Admin\Init(); |
| 52 |
$admin->init(); |
| 53 |
} |
| 54 |
|
| 55 |
// Init menu helper. |
| 56 |
$menu_helper = new Menu_Helper(); |
| 57 |
$menu_helper->run(); |
| 58 |
|
| 59 |
// Init lazy load. |
| 60 |
$lazy_load = new Lazy_Load(); |
| 61 |
$lazy_load->run(); |
| 62 |
|
| 63 |
// Load die handler. This should only affect if `HTTP_X_WPAC_REQUEST` server var is set. |
| 64 |
add_filter( 'wp_die_handler', 'wpac_wp_die_handler' ); |
| 65 |
|
| 66 |
// For checking when a theme has changed. |
| 67 |
add_action( 'switch_theme', __NAMESPACE__ . '\wpac_has_switched_theme' ); |
| 68 |
} |
| 69 |
add_action( 'plugins_loaded', __NAMESPACE__ . '\plugins_loaded' ); |
| 70 |
|
| 71 |
/** |
| 72 |
* Run when the theme has been switched. |
| 73 |
* |
| 74 |
* We'll use this to alert the user to re-save settings. |
| 75 |
*/ |
| 76 |
function wpac_has_switched_theme() { |
| 77 |
update_option( 'wpac_theme_has_changed', true ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Run when WP has finished loading in. |
| 82 |
* |
| 83 |
* Queries should be initialized by now, so we can check for post variables. |
| 84 |
*/ |
| 85 |
function wp() { |
| 86 |
/** |
| 87 |
* Fires before the main WPAC actions/filters. Useful for hooking in early. |
| 88 |
*/ |
| 89 |
do_action( 'dlxplugins/ajaxify/comments/wp' ); |
| 90 |
|
| 91 |
// If Ajaxify enabled, run the plugin. |
| 92 |
if ( Functions::is_ajaxify_enabled() ) { |
| 93 |
add_filter( 'the_comments', 'wpac_comments_template_query_args_filter' ); |
| 94 |
add_action( 'wp_enqueue_scripts', 'wpac_initialize', 9 ); |
| 95 |
add_action( 'wp_enqueue_scripts', 'wpac_enqueue_scripts' ); |
| 96 |
add_filter( 'option_page_comments', 'wpac_option_page_comments' ); |
| 97 |
add_filter( 'option_comments_per_page', 'wpac_option_comments_per_page' ); |
| 98 |
|
| 99 |
// For Genesis compatibility. |
| 100 |
add_filter( 'genesis_before_comments', 'wpac_genesis_before_comments' ); |
| 101 |
} |
| 102 |
} |
| 103 |
add_action( 'wp', __NAMESPACE__ . '\wp' ); |
| 104 |
|
| 105 |
/** |
| 106 |
* Fires before a comment is posted. |
| 107 |
* |
| 108 |
* @param int $comment_post_id The post ID the comment will be posted to. |
| 109 |
*/ |
| 110 |
global $wpac_global_comment_post_id; |
| 111 |
function pre_comment_on_post( $comment_post_id ) { |
| 112 |
global $wpac_global_comment_post_id; |
| 113 |
$wpac_global_comment_post_id = $comment_post_id; |
| 114 |
add_filter( 'gettext', 'wpac_filter_gettext', 20, 3 ); // todo - need to run earlier. |
| 115 |
} |
| 116 |
add_action( 'pre_comment_on_post', __NAMESPACE__ . '\pre_comment_on_post' ); |
| 117 |
|
| 118 |
/* Setup plugin activation and redirection */ |
| 119 |
register_activation_hook( __FILE__, __NAMESPACE__ . '\on_plugin_activation' ); |
| 120 |
add_action( 'admin_init', __NAMESPACE__ . '\activate_redirect' ); |
| 121 |
|
| 122 |
/** |
| 123 |
* Determine if a user can be redirected or not. |
| 124 |
* |
| 125 |
* @return true if the user can be redirected. false if not. |
| 126 |
*/ |
| 127 |
function can_redirect_on_activation() { |
| 128 |
/** |
| 129 |
* Filter whether to redirect on plugin activation. |
| 130 |
* |
| 131 |
* @param bool $can_redirect Whether to redirect on plugin activation. Pass `false` to prevent redirect. |
| 132 |
*/ |
| 133 |
$can_redirect = apply_filters( 'dlxplugins/ajaxify/comments/activation/can_redirect', true ); |
| 134 |
if ( false === $can_redirect ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
// If plugin is activated in network admin options, skip redirect. |
| 139 |
if ( is_network_admin() ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
// Skip redirect if WP_DEBUG is enabled. |
| 144 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
// Check for dev mode. |
| 149 |
if ( function_exists( 'wp_get_development_mode' ) ) { |
| 150 |
if ( ! empty( wp_get_development_mode() ) ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Determine if multi-activation is enabled. |
| 156 |
$maybe_multi = filter_input( INPUT_GET, 'activate-multi', FILTER_VALIDATE_BOOLEAN ); |
| 157 |
if ( $maybe_multi ) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
// All is well. Can redirect. |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Callback when the plugin is activated. |
| 167 |
*/ |
| 168 |
function on_plugin_activation() { |
| 169 |
if ( can_redirect_on_activation() ) { |
| 170 |
// Add option for whether to redirect. |
| 171 |
add_option( 'wpac_do_activation_redirect', sanitize_text_field( __FILE__ ) ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Redirect in the admin upon plugin activation. |
| 177 |
*/ |
| 178 |
function activate_redirect() { |
| 179 |
if ( can_redirect_on_activation() && is_admin() ) { |
| 180 |
if ( __FILE__ === get_option( 'wpac_do_activation_redirect' ) ) { |
| 181 |
delete_option( 'wpac_do_activation_redirect' ); |
| 182 |
wp_safe_redirect( esc_url_raw( add_query_arg( array( 'first_time_install' => true ), Functions::get_settings_url() ) ) ); |
| 183 |
exit; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|