| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class configures the parameters advanced |
| 10 |
* |
| 11 |
* @author Alex Kovalev <alex.kovalevv@gmail.com>, Github: https://github.com/alexkovalevv |
| 12 |
* |
| 13 |
* @copyright (c) 2017 Webraftic Ltd |
| 14 |
*/ |
| 15 |
class WbcrCmp_ConfigComments extends WBCR\Factory_Templates_134\Configurate { |
| 16 |
|
| 17 |
private $modified_types = []; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param Wbcr_Factory480_Plugin $plugin |
| 21 |
*/ |
| 22 |
public function __construct( Wbcr_Factory480_Plugin $plugin ) { |
| 23 |
parent::__construct( $plugin ); |
| 24 |
$this->plugin = $plugin; |
| 25 |
} |
| 26 |
|
| 27 |
public function registerActionsAndFilters() { |
| 28 |
// These need to happen now |
| 29 |
if ( $this->isDisabledAllPosts() ) { |
| 30 |
add_action( 'widgets_init', [ $this, 'disableRcWidget' ] ); |
| 31 |
add_action( 'template_redirect', [ $this, 'filterQuery' ], 9 ); // before redirect_canonical |
| 32 |
|
| 33 |
// Admin bar filtering has to happen here since WP 3.6 |
| 34 |
add_action( 'template_redirect', [ $this, 'filterAdminBar' ] ); |
| 35 |
add_action( 'admin_init', [ $this, 'filterAdminBar' ] ); |
| 36 |
} else { |
| 37 |
|
| 38 |
if ( $this->getPopulateOption( 'comment_text_convert_links_pseudo' ) || $this->getPopulateOption( 'pseudo_comment_author_link' ) ) { |
| 39 |
add_action( 'wp_enqueue_scripts', [ $this, 'assetsUrlSpanScripts' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( $this->getPopulateOption( 'comment_text_convert_links_pseudo' ) ) { |
| 43 |
add_filter( 'comment_text', [ $this, 'commentTextConvertLinksPseudo' ] ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( $this->getPopulateOption( 'pseudo_comment_author_link' ) ) { |
| 47 |
add_filter( 'get_comment_author_link', [ $this, 'pseudoCommentAuthorLink' ], 100, 3 ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( $this->getPopulateOption( 'remove_url_from_comment_form' ) ) { |
| 51 |
add_filter( 'comment_form_default_fields', [ $this, 'removeUrlFromCommentForm' ] ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// These can happen later |
| 56 |
//add_action('plugins_loaded', array($this, 'register_text_domain')); |
| 57 |
add_action( 'wp_loaded', [ $this, 'initWploadedFilters' ] ); |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* Remove comment links from the admin bar in a multisite network. |
| 62 |
*/ |
| 63 |
public function removeNetworkCommentLinks( $wp_admin_bar ) { |
| 64 |
if ( $this->plugin->isNetworkActive() && is_user_logged_in() ) { |
| 65 |
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) { |
| 66 |
$wp_admin_bar->remove_menu( 'blog-' . $blog->userblog_id . '-c' ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
// We have no way to know whether the plugin is active on other sites, so only remove this one |
| 70 |
$wp_admin_bar->remove_menu( 'blog-' . get_current_blog_id() . '-c' ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
private function isDisabledAllPosts() { |
| 75 |
return $this->getPopulateOption( 'disable_comments', 'enable_comments' ) == 'disable_comments'; |
| 76 |
} |
| 77 |
|
| 78 |
private function isDisabledCertainPostTypes() { |
| 79 |
return $this->getPopulateOption( 'disable_comments', 'enable_comments' ) == 'disable_certain_post_types_comments'; |
| 80 |
} |
| 81 |
|
| 82 |
private function isEnabledComments() { |
| 83 |
return $this->getPopulateOption( 'disable_comments', 'enable_comments' ) == 'enable_comments'; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* Get an array of disabled post type. |
| 88 |
*/ |
| 89 |
private function getDisabledPostTypes() { |
| 90 |
return wbcr_cmp_get_disabled_post_types(); |
| 91 |
} |
| 92 |
|
| 93 |
/* |
| 94 |
* Check whether comments have been disabled on a given post type. |
| 95 |
*/ |
| 96 |
private function isPostTypeDisabled( $type ) { |
| 97 |
return $this->isDisabledCertainPostTypes() && in_array( $type, $this->getDisabledPostTypes() ); |
| 98 |
} |
| 99 |
|
| 100 |
public function initWploadedFilters() { |
| 101 |
$disabled_post_types = $this->getDisabledPostTypes(); |
| 102 |
|
| 103 |
if ( ! empty( $disabled_post_types ) && ! $this->isEnabledComments() ) { |
| 104 |
foreach ( $disabled_post_types as $type ) { |
| 105 |
// we need to know what native support was for later |
| 106 |
if ( post_type_supports( $type, 'comments' ) ) { |
| 107 |
$this->modified_types[] = $type; |
| 108 |
remove_post_type_support( $type, 'comments' ); |
| 109 |
remove_post_type_support( $type, 'trackbacks' ); |
| 110 |
} |
| 111 |
} |
| 112 |
add_filter( 'comments_array', [ $this, 'filterExistingComments' ], 20, 2 ); |
| 113 |
add_filter( 'comments_open', [ $this, 'filterCommentStatus' ], 20, 2 ); |
| 114 |
add_filter( 'pings_open', [ $this, 'filterCommentStatus' ], 20, 2 ); |
| 115 |
} |
| 116 |
|
| 117 |
// Filters for the admin only |
| 118 |
if ( is_admin() ) { |
| 119 |
add_action( 'admin_print_footer_scripts', [ $this, 'discussionNotice' ] ); |
| 120 |
|
| 121 |
// if only certain types are disabled, remember the original post status |
| 122 |
if ( ! $this->isDisabledAllPosts() ) { |
| 123 |
add_action( 'edit_form_advanced', [ $this, 'editFormInputs' ] ); |
| 124 |
add_action( 'edit_page_form', [ $this, 'editFormInputs' ] ); |
| 125 |
} else { |
| 126 |
add_action( 'admin_menu', [ $this, 'filterAdminMenu' ], 9999 ); // do this as late as possible |
| 127 |
add_action( 'admin_print_footer_scripts-index.php', [ $this, 'dashboardJs' ] ); |
| 128 |
add_action( 'wp_dashboard_setup', [ $this, 'filterDashboard' ] ); |
| 129 |
add_filter( 'pre_option_default_pingback_flag', '__return_zero' ); |
| 130 |
} |
| 131 |
} // Filters for front end only |
| 132 |
else { |
| 133 |
add_action( 'template_redirect', [ $this, 'checkCommentTemplate' ] ); |
| 134 |
|
| 135 |
if ( $this->isDisabledAllPosts() ) { |
| 136 |
add_filter( 'feed_links_show_comments_feed', '__return_false' ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
* Replace the theme's comment template with a blank one. |
| 143 |
* To prevent this, define DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE |
| 144 |
* and set it to True |
| 145 |
*/ |
| 146 |
public function checkCommentTemplate() { |
| 147 |
if ( is_singular() && ( $this->isDisabledAllPosts() || $this->isPostTypeDisabled( get_post_type() ) ) ) { |
| 148 |
if ( ! defined( 'DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE' ) || DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE == true ) { |
| 149 |
// Kill the comments template. |
| 150 |
add_filter( 'comments_template', [ $this, 'dummyCommentsTemplate' ], 20 ); |
| 151 |
} |
| 152 |
// Remove comment-reply script for themes that include it indiscriminately |
| 153 |
wp_deregister_script( 'comment-reply' ); |
| 154 |
// feed_links_extra inserts a comments RSS link |
| 155 |
remove_action( 'wp_head', 'feed_links_extra', 3 ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public function dummyCommentsTemplate() { |
| 160 |
return WCM_PLUGIN_DIR . '/includes/comments-template.php'; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Issue a 403 for all comment feed requests. |
| 165 |
*/ |
| 166 |
public function filterQuery() { |
| 167 |
if ( is_comment_feed() ) { |
| 168 |
wp_die( __( 'Comments are closed.' ), '', [ 'response' => 403 ] ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Remove comment links from the admin bar. |
| 174 |
*/ |
| 175 |
public function filterAdminBar() { |
| 176 |
if ( is_admin_bar_showing() ) { |
| 177 |
// Remove comments links from admin bar |
| 178 |
remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( is_multisite() ) { |
| 182 |
add_action( 'admin_bar_menu', [ $this, 'removeNetworkCommentLinks' ], 500 ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
public function editFormInputs() { |
| 187 |
global $post; |
| 188 |
// Without a dicussion meta box, comment_status will be set to closed on new/updated posts |
| 189 |
if ( in_array( $post->post_type, $this->modified_types ) ) { |
| 190 |
echo '<input type="hidden" name="comment_status" value="' . esc_attr( $post->comment_status ) . '" /><input type="hidden" name="ping_status" value="' . esc_attr( $post->ping_status ) . '" />'; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public function discussionNotice() { |
| 195 |
$disabled_post_types = $this->getDisabledPostTypes(); |
| 196 |
if ( get_current_screen()->id == 'options-discussion' && ! empty( $disabled_post_types ) ) { |
| 197 |
$names = []; |
| 198 |
foreach ( $disabled_post_types as $type ) { |
| 199 |
$type_object = get_post_type_object( $type ); |
| 200 |
if ( empty( $type_object ) ) { |
| 201 |
continue; |
| 202 |
} |
| 203 |
$names[ $type ] = $type_object->labels->name; |
| 204 |
} |
| 205 |
|
| 206 |
?> |
| 207 |
<script> |
| 208 |
jQuery(document).ready(function($) { |
| 209 |
$(".wrap h2").first().after(<?php echo json_encode( '<div style="color: #900"><p>' . sprintf( __( 'Note: The <em>%s</em> plugin is currently active, and comments are completely disabled on: %s. Many of the settings below will not be applicable for those post types.', 'comments-plus' ), $this->plugin->getPluginTitle(), implode( ', ', $names ) ) . '</p></div>' );?>); |
| 210 |
}); |
| 211 |
</script> |
| 212 |
<?php |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
public function filterAdminMenu() { |
| 217 |
global $pagenow; |
| 218 |
|
| 219 |
if ( $pagenow == 'comment.php' || $pagenow == 'edit-comments.php' || $pagenow == 'options-discussion.php' ) { |
| 220 |
wp_die( __( 'Comments are closed.' ), '', [ 'response' => 403 ] ); |
| 221 |
} |
| 222 |
|
| 223 |
remove_menu_page( 'edit-comments.php' ); |
| 224 |
remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
| 225 |
} |
| 226 |
|
| 227 |
public function filterDashboard() { |
| 228 |
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); |
| 229 |
} |
| 230 |
|
| 231 |
public function dashboardJs() { |
| 232 |
echo '<script> |
| 233 |
jQuery(function($){ |
| 234 |
$("#dashboard_right_now .comment-count, #latest-comments").hide(); |
| 235 |
$("#welcome-panel .welcome-comments").parent().hide(); |
| 236 |
}); |
| 237 |
</script>'; |
| 238 |
} |
| 239 |
|
| 240 |
public function filterExistingComments( $comments, $post_id ) { |
| 241 |
$post = get_post( $post_id ); |
| 242 |
|
| 243 |
return ( $this->isDisabledAllPosts() || $this->isPostTypeDisabled( $post->post_type ) ) ? [] : $comments; |
| 244 |
} |
| 245 |
|
| 246 |
public function filterCommentStatus( $open, $post_id ) { |
| 247 |
$post = get_post( $post_id ); |
| 248 |
|
| 249 |
return ( $this->isDisabledAllPosts() || $this->isPostTypeDisabled( $post->post_type ) ) ? false : $open; |
| 250 |
} |
| 251 |
|
| 252 |
public function disableRcWidget() { |
| 253 |
unregister_widget( 'WP_Widget_Recent_Comments' ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Convert links in comment text into span pseudo links |
| 258 |
* |
| 259 |
* @param $comment_text |
| 260 |
* |
| 261 |
* @return mixed |
| 262 |
*/ |
| 263 |
|
| 264 |
public function commentTextConvertLinksPseudo( $comment_text ) { |
| 265 |
|
| 266 |
return $this->convertLinksPseudo( $comment_text ); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Convert links into span pseudo links |
| 271 |
* |
| 272 |
* @param $text |
| 273 |
* |
| 274 |
* @return mixed |
| 275 |
*/ |
| 276 |
|
| 277 |
public function convertLinksPseudo( $text ) { |
| 278 |
|
| 279 |
return preg_replace_callback( '/<a[^>]+href=[\'"](https?:\/\/[^"\']+)[\'"][^>]+>(.*?)<\/a>/i', [ |
| 280 |
$this, |
| 281 |
'replaceLinks' |
| 282 |
], $text ); |
| 283 |
} |
| 284 |
|
| 285 |
public function replaceLinks( $matches ) { |
| 286 |
if ( $matches[1] == get_home_url() ) { |
| 287 |
return $matches[0]; |
| 288 |
} |
| 289 |
|
| 290 |
return '<span class="wbcr-clearfy-pseudo-link" data-uri="' . esc_url( $matches[1] ) . '" > ' . esc_html( $matches[2] ) . '</span>'; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Convert author link to pseudo link |
| 295 |
* |
| 296 |
* @return string |
| 297 |
*/ |
| 298 |
|
| 299 |
public function pseudoCommentAuthorLink( $return, $author, $comment_ID ) { |
| 300 |
$url = get_comment_author_url( $comment_ID ); |
| 301 |
$author = get_comment_author( $comment_ID ); |
| 302 |
|
| 303 |
if ( empty( $url ) || 'http://' == $url ) { |
| 304 |
$return = esc_html( $author ); |
| 305 |
} else { |
| 306 |
$return = '<span class="wbcr-clearfy-pseudo-link" data-uri="' . esc_url( $url ) . '">' . esc_html( $author ) . '</span>'; |
| 307 |
} |
| 308 |
|
| 309 |
return $return; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Remove url field from comment form |
| 314 |
* |
| 315 |
* @param $fields |
| 316 |
* |
| 317 |
* @return mixed |
| 318 |
*/ |
| 319 |
|
| 320 |
public function removeUrlFromCommentForm( $fields ) { |
| 321 |
if ( isset( $fields['url'] ) ) { |
| 322 |
unset( $fields['url'] ); |
| 323 |
} |
| 324 |
|
| 325 |
return $fields; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
// todo: Убрать это грязное решение со скриптами. |
| 330 |
public function assetsUrlSpanScripts() { |
| 331 |
if ( ! is_singular() ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
wp_enqueue_style( 'wbcr-comments-plus-url-span', WCM_PLUGIN_URL . '/assets/css/url-span.css', [], $this->plugin->getPluginVersion() ); |
| 336 |
wp_enqueue_script( 'wbcr-comments-plus-url-span', WCM_PLUGIN_URL . '/assets/js/url-span.js', [ 'jquery' ], $this->plugin->getPluginVersion(), true ); |
| 337 |
} |
| 338 |
} |
| 339 |
|