class-activation.php
4 months ago
class-admin-menu-organizer.php
4 months ago
class-admin-menu-svg-icon-mask.php
4 months ago
class-auto-publish-posts-with-missed-schedule.php
4 months ago
class-avif-upload.php
4 months ago
class-captcha-protection.php
4 months ago
class-change-login-url.php
4 months ago
class-cleanup-admin-bar.php
4 months ago
class-common-methods.php
4 months ago
class-content-duplication.php
4 months ago
class-content-order.php
4 months ago
class-custom-admin-footer-text.php
4 months ago
class-custom-body-class.php
4 months ago
class-custom-css.php
4 months ago
class-custom-nav-menu-items-in-new-tab.php
4 months ago
class-deactivation.php
4 months ago
class-disable-author-archives.php
4 months ago
class-disable-comments.php
4 months ago
class-disable-dashboard-widgets.php
4 months ago
class-disable-embeds.php
4 months ago
class-disable-feeds.php
4 months ago
class-disable-gutenberg.php
4 months ago
class-disable-rest-api.php
4 months ago
class-disable-smaller-components.php
4 months ago
class-disable-updates.php
4 months ago
class-disable-xml-rpc.php
4 months ago
class-display-system-summary.php
4 months ago
class-email-address-obfuscator.php
4 months ago
class-email-delivery.php
4 months ago
class-enhance-list-tables.php
4 months ago
class-external-permalinks.php
4 months ago
class-heartbeat-control.php
4 months ago
class-hide-admin-bar.php
4 months ago
class-hide-admin-notices.php
4 months ago
class-image-sizes-panel.php
4 months ago
class-image-upload-control.php
4 months ago
class-insert-head-body-footer-code.php
4 months ago
class-last-login-column.php
4 months ago
class-limit-login-attempts.php
4 months ago
class-login-id-type.php
4 months ago
class-login-logout-menu.php
4 months ago
class-maintenance-mode.php
4 months ago
class-manage-ads-appads-txt.php
4 months ago
class-manage-robots-txt.php
4 months ago
class-media-files-visibility-control.php
4 months ago
class-media-replacement.php
4 months ago
class-multiple-user-roles.php
4 months ago
class-obfuscate-author-slugs.php
4 months ago
class-open-external-links-in-new-tab.php
4 months ago
class-password-protection.php
4 months ago
class-redirect-after-login.php
4 months ago
class-redirect-after-logout.php
4 months ago
class-redirect-fourofour.php
4 months ago
class-registration-date-column.php
4 months ago
class-revisions-control.php
4 months ago
class-search-engines-visibility.php
4 months ago
class-settings-fields-render.php
4 months ago
class-settings-sanitization.php
4 months ago
class-settings-sections-fields.php
4 months ago
class-show-custom-taxonomy-filters.php
4 months ago
class-site-identity-on-login-page.php
4 months ago
class-svg-upload.php
4 months ago
class-various-admin-ui-enhancements.php
4 months ago
class-view-admin-as-role.php
4 months ago
class-wider-admin-menu.php
4 months ago
class-wp-config-transformer.php
4 months ago
class-disable-comments.php
311 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for Disable Comments module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class Disable_Comments { |
| 11 | /** |
| 12 | * Public post types that should never have comments disabled by this module. |
| 13 | * |
| 14 | * @since 8.5.2 |
| 15 | * |
| 16 | * @var string[] |
| 17 | */ |
| 18 | public $inapplicable_post_types = array('kt_gallery'); |
| 19 | |
| 20 | /** |
| 21 | * Get the disable comments mode. |
| 22 | * |
| 23 | * @since 8.5.2 |
| 24 | * |
| 25 | * @param array $options Module options. |
| 26 | * @return string |
| 27 | */ |
| 28 | private function get_disable_comments_type( $options ) { |
| 29 | return 'only-on'; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get the post types selected in the Disable Comments settings. |
| 34 | * |
| 35 | * @since 8.5.2 |
| 36 | * |
| 37 | * @param array $options Module options. |
| 38 | * @return string[] |
| 39 | */ |
| 40 | private function get_disabled_comments_post_types( $options ) { |
| 41 | $disable_comments_for = ( isset( $options['disable_comments_for'] ) ? $options['disable_comments_for'] : array() ); |
| 42 | if ( empty( $disable_comments_for ) || !is_array( $disable_comments_for ) ) { |
| 43 | return array(); |
| 44 | } |
| 45 | $common_methods = new Common_Methods(); |
| 46 | $disable_comments_for = $common_methods->get_array_of_keys_with_true_value( $disable_comments_for ); |
| 47 | return array_values( array_diff( $disable_comments_for, $this->inapplicable_post_types ) ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get public post types that support comments and can be managed by this module. |
| 52 | * |
| 53 | * @since 8.5.2 |
| 54 | * |
| 55 | * @return string[] |
| 56 | */ |
| 57 | private function get_applicable_public_post_types( $options = array() ) { |
| 58 | if ( empty( $options ) ) { |
| 59 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 60 | } |
| 61 | $public_post_types = get_post_types( array( |
| 62 | 'public' => true, |
| 63 | ), 'names' ); |
| 64 | $configured_post_types = ( isset( $options['disable_comments_for'] ) && is_array( $options['disable_comments_for'] ) ? array_keys( $options['disable_comments_for'] ) : array() ); |
| 65 | $candidate_post_types = array_unique( array_merge( $public_post_types, $configured_post_types ) ); |
| 66 | $applicable_post_types = array(); |
| 67 | if ( is_array( $candidate_post_types ) ) { |
| 68 | foreach ( $candidate_post_types as $post_type ) { |
| 69 | $post_type_object = get_post_type_object( $post_type ); |
| 70 | if ( !is_object( $post_type_object ) || !$post_type_object->public || $this->is_post_type_inapplicable( $post_type ) ) { |
| 71 | continue; |
| 72 | } |
| 73 | if ( post_type_supports( $post_type, 'comments' ) || array_key_exists( $post_type, (array) $options['disable_comments_for'] ) ) { |
| 74 | $applicable_post_types[] = $post_type; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | return $applicable_post_types; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Check if the post type should never be affected by Disable Comments. |
| 83 | * |
| 84 | * @since 8.5.2 |
| 85 | * |
| 86 | * @param string $post_type Post type slug. |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function is_post_type_inapplicable( $post_type ) { |
| 90 | return in_array( $post_type, $this->inapplicable_post_types, true ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Check if comments are disabled for all applicable post types. |
| 95 | * |
| 96 | * @since 8.5.2 |
| 97 | * |
| 98 | * @param array $options Optional module options. |
| 99 | * @return bool |
| 100 | */ |
| 101 | public function are_comments_disabled_for_all_applicable_post_types( $options = array() ) { |
| 102 | if ( empty( $options ) ) { |
| 103 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 104 | } |
| 105 | if ( !isset( $options['disable_comments'] ) || !$options['disable_comments'] ) { |
| 106 | return false; |
| 107 | } |
| 108 | $applicable_post_types = $this->get_applicable_public_post_types( $options ); |
| 109 | if ( empty( $applicable_post_types ) ) { |
| 110 | return false; |
| 111 | } |
| 112 | foreach ( $applicable_post_types as $post_type ) { |
| 113 | if ( !$this->is_commenting_disabled_for_post_type( $post_type, $options ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Disable comments for post types |
| 122 | * |
| 123 | * @since 2.7.0 |
| 124 | */ |
| 125 | public function disable_comments_for_post_types_edit() { |
| 126 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 127 | foreach ( $this->get_applicable_public_post_types( $options ) as $post_type_slug ) { |
| 128 | if ( $this->is_commenting_disabled_for_post_type( $post_type_slug, $options ) ) { |
| 129 | remove_post_type_support( $post_type_slug, 'comments' ); |
| 130 | remove_post_type_support( $post_type_slug, 'trackbacks' ); |
| 131 | remove_meta_box( 'commentstatusdiv', $post_type_slug, 'normal' ); |
| 132 | remove_meta_box( 'commentstatusdiv', $post_type_slug, 'side' ); |
| 133 | remove_meta_box( 'commentsdiv', $post_type_slug, 'normal' ); |
| 134 | remove_meta_box( 'commentsdiv', $post_type_slug, 'side' ); |
| 135 | remove_meta_box( 'trackbacksdiv', $post_type_slug, 'normal' ); |
| 136 | remove_meta_box( 'trackbacksdiv', $post_type_slug, 'side' ); |
| 137 | // edit-comments.js |
| 138 | wp_dequeue_script( 'admin-comments' ); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Hide existing comments from the frontend post |
| 145 | * |
| 146 | * @since 6.2.1 |
| 147 | */ |
| 148 | public function hide_existing_comments_on_frontend() { |
| 149 | $current_post_type = get_post_type(); |
| 150 | if ( $this->is_commenting_disabled_for_post_type( $current_post_type ) ) { |
| 151 | add_filter( |
| 152 | 'comments_array', |
| 153 | '__return_empty_array', |
| 154 | 10, |
| 155 | 2 |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Return empty comments array for comment templates |
| 162 | * |
| 163 | * @since 6.3.1 |
| 164 | */ |
| 165 | public function maybe_return_empty_comments( $comments, $post_id ) { |
| 166 | $post = get_post( $post_id ); |
| 167 | if ( !is_object( $post ) || !property_exists( $post, 'post_type' ) ) { |
| 168 | return $comments; |
| 169 | } |
| 170 | if ( $this->is_commenting_disabled_for_post_type( $post->post_type ) ) { |
| 171 | return array(); |
| 172 | } |
| 173 | return $comments; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Close commenting on the frontend |
| 178 | * |
| 179 | * @since 2.7.0 |
| 180 | */ |
| 181 | public function close_comments_pings_on_frontend( $comments_pings_open, $post_id ) { |
| 182 | // If commenting or pinging is not open, let's keep it that way |
| 183 | if ( !$comments_pings_open ) { |
| 184 | return $comments_pings_open; |
| 185 | } |
| 186 | // Commenting or pinging is open for the post ID, let's decide if we should close it |
| 187 | $post = get_post( $post_id ); |
| 188 | if ( is_object( $post ) && property_exists( $post, 'post_type' ) && $this->is_commenting_disabled_for_post_type( $post->post_type ) ) { |
| 189 | return false; |
| 190 | } |
| 191 | return $comments_pings_open; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Always return zero for comments count on a post where the post type has commenting disabled |
| 196 | * |
| 197 | * @since 6.2.7 |
| 198 | */ |
| 199 | public function return_zero_comments_count( $comments_number, $post_id ) { |
| 200 | $post = get_post( $post_id ); |
| 201 | if ( is_object( $post ) && property_exists( $post, 'post_type' ) ) { |
| 202 | if ( $this->is_commenting_disabled_for_post_type( $post->post_type ) ) { |
| 203 | return 0; |
| 204 | } |
| 205 | } |
| 206 | return $comments_number; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Disable commenting via XML-RPC |
| 211 | * |
| 212 | * @link https://plugins.trac.wordpress.org/browser/disable-comments/tags/2.4.5/disable-comments.php |
| 213 | * @since 6.3.1 |
| 214 | */ |
| 215 | public function disable_xmlrpc_comments( $methods ) { |
| 216 | if ( $this->are_comments_disabled_for_all_applicable_post_types() ) { |
| 217 | unset($methods['wp.newComment']); |
| 218 | } |
| 219 | return $methods; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Disables comments endpoint in REST API |
| 224 | * |
| 225 | * @link https://plugins.trac.wordpress.org/browser/disable-comments/tags/2.4.5/disable-comments.php |
| 226 | * @since 6.3.1 |
| 227 | */ |
| 228 | public function disable_rest_api_comments_endpoints( $endpoints ) { |
| 229 | if ( $this->are_comments_disabled_for_all_applicable_post_types() ) { |
| 230 | if ( isset( $endpoints['comments'] ) ) { |
| 231 | unset($endpoints['comments']); |
| 232 | } |
| 233 | if ( isset( $endpoints['/wp/v2/comments'] ) ) { |
| 234 | unset($endpoints['/wp/v2/comments']); |
| 235 | } |
| 236 | if ( isset( $endpoints['/wp/v2/comments/(?P<id>[\\d]+)'] ) ) { |
| 237 | unset($endpoints['/wp/v2/comments/(?P<id>[\\d]+)']); |
| 238 | } |
| 239 | } |
| 240 | return $endpoints; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Return blank comment before inserting to DB |
| 245 | * |
| 246 | * @link https://plugins.trac.wordpress.org/browser/disable-comments/tags/2.4.5/disable-comments.php |
| 247 | * @since 6.3.1 |
| 248 | */ |
| 249 | public function return_blank_comment( $prepared_comment, $request ) { |
| 250 | $post_id = absint( $request->get_param( 'post' ) ); |
| 251 | if ( empty( $post_id ) ) { |
| 252 | return $prepared_comment; |
| 253 | } |
| 254 | $post = get_post( $post_id ); |
| 255 | if ( !is_object( $post ) || !property_exists( $post, 'post_type' ) ) { |
| 256 | return $prepared_comment; |
| 257 | } |
| 258 | if ( !$this->is_commenting_disabled_for_post_type( $post->post_type ) ) { |
| 259 | return $prepared_comment; |
| 260 | } |
| 261 | return new \WP_Error('asenha_commenting_disabled', __( 'Comments are disabled for this content type.', 'admin-site-enhancements' ), array( |
| 262 | 'status' => 403, |
| 263 | )); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Show blank template on singular views when comment is disabled |
| 268 | * |
| 269 | * @since 4.9.2 |
| 270 | */ |
| 271 | public function show_blank_comment_template() { |
| 272 | $current_post_type = get_post_type(); |
| 273 | if ( is_singular() && $this->is_commenting_disabled_for_post_type( $current_post_type ) ) { |
| 274 | add_filter( 'comments_template', [$this, 'load_blank_comment_template'], 20 ); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Load the actual blank comment template |
| 280 | * |
| 281 | * @since 4.9.2 |
| 282 | */ |
| 283 | public function load_blank_comment_template() { |
| 284 | return ASENHA_PATH . 'includes/blank-comment-template.php'; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Check if commenting is disabled for the post type |
| 289 | * |
| 290 | * @since 7.8.8 |
| 291 | */ |
| 292 | public function is_commenting_disabled_for_post_type( $post_type, $options = array() ) { |
| 293 | if ( empty( $post_type ) || $this->is_post_type_inapplicable( $post_type ) ) { |
| 294 | return false; |
| 295 | } |
| 296 | if ( empty( $options ) ) { |
| 297 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 298 | } |
| 299 | $comment_is_disabled_for_post_type = false; |
| 300 | if ( array_key_exists( 'disable_comments', $options ) && $options['disable_comments'] ) { |
| 301 | $disable_comments_for = $this->get_disabled_comments_post_types( $options ); |
| 302 | $disable_comments_type = $this->get_disable_comments_type( $options ); |
| 303 | if ( 'only-on' === $disable_comments_type && in_array( $post_type, $disable_comments_for, true ) || 'except-on' === $disable_comments_type && !in_array( $post_type, $disable_comments_for, true ) || 'all-post-types' === $disable_comments_type ) { |
| 304 | $comment_is_disabled_for_post_type = true; |
| 305 | } |
| 306 | } |
| 307 | return $comment_is_disabled_for_post_type; |
| 308 | } |
| 309 | |
| 310 | } |
| 311 |