Skip to content

Commit f16cee5

Browse files
committed
Add comment status functions and Site Health integration
- Add get_current_comment_status() method returning simple status string - Add get_detailed_comment_status() method returning comprehensive status array - Integrate plugin status into WordPress Site Health Info panel - Add comprehensive error handling with try-catch blocks and safe defaults - Include total comments count, network settings, and role exclusion data - Update README.md with complete documentation and usage examples - Add docs directory to .distignore for distribution exclusion Features: - Simple status: 'all', 'posts', 'pages', 'posts,pages', 'multiple', 'none' - Detailed status: 14 data points including API restrictions and role exclusions - Automatic Site Health integration in Tools > Site Health > Info - Robust error handling for edge cases and uninitialized states - Performance optimized with single data collection method
1 parent e55e22d commit f16cee5

File tree

4 files changed

+452
-1
lines changed

4 files changed

+452
-1
lines changed

.distignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ wp-cli.local.yml
3030
tests
3131
vendor
3232
node_modules
33+
docs
3334
*.sql
3435
*.tar.gz
3536
*.zip

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,64 @@ Version and compatibility information can be found in the plugin [readme](https:
88

99
License: [GPLv3+](https://www.gnu.org/licenses/gpl-3.0.html)
1010

11+
## Comment Status Functions
12+
13+
The plugin provides two methods to programmatically check the current comment disable configuration:
14+
15+
### `get_current_comment_status()`
16+
17+
Returns a simple string indicating the current comment disable status.
18+
19+
**Return Values:**
20+
21+
- `'all'` - Comments disabled site-wide for all content types
22+
- `'posts'` - Comments disabled only for blog posts
23+
- `'pages'` - Comments disabled only for pages
24+
- `'posts,pages'` - Comments disabled for both posts and pages
25+
- `'multiple'` - Comments disabled for multiple specific content types
26+
- `'none'` - Comments enabled everywhere
27+
- Custom post type name (e.g., `'product'`) - Comments disabled for that specific post type
28+
29+
### `get_detailed_comment_status()`
30+
31+
Returns a comprehensive array with complete plugin configuration details including disabled post types, API restrictions, network settings, role exclusions, and comment counts.
32+
33+
**Key Array Elements:**
34+
35+
- `status` - Main status string (same as above method)
36+
- `disabled_post_types` - Array of disabled post type slugs
37+
- `total_comments` - Total number of comments in database
38+
- `network_active` - Whether plugin is network activated
39+
- `xmlrpc_disabled` / `rest_api_disabled` - API restriction status
40+
- `role_exclusion_enabled` - Whether role-based exclusions are active
41+
42+
## Usage Examples
43+
44+
```php
45+
// Get simple status
46+
$instance = Disable_Comments::get_instance();
47+
$status = $instance->get_current_comment_status();
48+
49+
if ($status === 'all') {
50+
echo 'Comments are disabled everywhere';
51+
} elseif ($status === 'none') {
52+
echo 'Comments are enabled';
53+
}
54+
55+
// Get detailed information
56+
$details = $instance->get_detailed_comment_status();
57+
echo 'Total comments: ' . $details['total_comments'];
58+
echo 'Plugin configured: ' . ($details['is_configured'] ? 'Yes' : 'No');
59+
```
60+
61+
**Note:** Call these methods on or after the `init` hook to ensure proper plugin initialization.
62+
63+
## Site Health Integration
64+
65+
Plugin status is automatically displayed in **Tools > Site Health > Info > Disable Comments** section, providing administrators with a complete overview of all plugin settings including comment counts, disabled post types, API restrictions, and network configuration. No additional setup required.
66+
1167
## Must-Use version
1268

1369
A [must-use version](https://github.com/WPDevelopers/disable-comments-mu) of the plugin is also available.
1470

15-
### This plugin is maintained by the [WPDeveloper](https://wpdeveloper.com/).
71+
### This plugin is maintained by the [WPDeveloper](https://wpdeveloper.com/)

disable-comments.php

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ function __construct() {
108108

109109
add_action('plugins_loaded', [$this, 'init_filters']);
110110
add_action('wp_loaded', [$this, 'start_plugin_usage_tracking']);
111+
112+
// Add Site Health integration
113+
add_filter('debug_information', array($this, 'add_site_health_info'));
111114
}
112115

113116
public function is_network_admin() {
@@ -1208,6 +1211,300 @@ protected function truncate_table($table_name) {
12081211
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
12091212
return $wpdb->query( "TRUNCATE TABLE " . esc_sql( $table_name ) );
12101213
}
1214+
1215+
/**
1216+
* Get the current site-wide comment status as a descriptive string.
1217+
*
1218+
* This function analyzes the current Disable Comments plugin configuration
1219+
* and returns a string describing which content types have comments disabled.
1220+
*
1221+
* @return string The current comment status:
1222+
* - 'all' if comments are disabled site-wide for all content types
1223+
* - 'posts' if comments are disabled only for posts
1224+
* - 'pages' if comments are disabled only for pages
1225+
* - 'posts,pages' if comments are disabled for both posts and pages
1226+
* - 'custom_type_name' for other specific content types
1227+
* - 'multiple' if multiple specific types are disabled (not all)
1228+
* - 'none' if comments are not disabled anywhere
1229+
*
1230+
* @since 2.5.2
1231+
*/
1232+
public function get_current_comment_status() {
1233+
try {
1234+
// Handle case where plugin is not properly initialized
1235+
if (empty($this->options)) {
1236+
return 'none';
1237+
}
1238+
1239+
// Check if comments are disabled everywhere
1240+
if ($this->is_remove_everywhere()) {
1241+
return 'all';
1242+
}
1243+
1244+
// Get disabled post types
1245+
$disabled_post_types = $this->get_disabled_post_types();
1246+
1247+
// If no post types are disabled, comments are enabled everywhere
1248+
if (empty($disabled_post_types)) {
1249+
return 'none';
1250+
}
1251+
1252+
// Get all available post types that support comments
1253+
$all_post_types = $this->get_all_post_types();
1254+
$all_post_type_keys = array_keys($all_post_types);
1255+
1256+
// Check if all available post types are disabled
1257+
if (count($disabled_post_types) >= count($all_post_type_keys)) {
1258+
$missing_types = array_diff($all_post_type_keys, $disabled_post_types);
1259+
if (empty($missing_types)) {
1260+
return 'all';
1261+
}
1262+
}
1263+
1264+
// Handle specific common cases
1265+
if (count($disabled_post_types) === 1) {
1266+
$disabled_type = $disabled_post_types[0];
1267+
1268+
// Return the specific post type name for single disabled types
1269+
switch ($disabled_type) {
1270+
case 'post':
1271+
return 'posts';
1272+
case 'page':
1273+
return 'pages';
1274+
default:
1275+
// For custom post types, return the post type slug
1276+
return $disabled_type;
1277+
}
1278+
}
1279+
1280+
// Handle multiple specific post types
1281+
if (count($disabled_post_types) === 2 &&
1282+
in_array('post', $disabled_post_types) &&
1283+
in_array('page', $disabled_post_types)) {
1284+
return 'posts,pages';
1285+
}
1286+
1287+
// For other combinations, return 'multiple' to indicate partial disabling
1288+
return 'multiple';
1289+
1290+
} catch (Exception $e) {
1291+
// Error handling - return safe default
1292+
error_log('Disable Comments: Error in get_current_comment_status() - ' . $e->getMessage());
1293+
return 'none';
1294+
}
1295+
}
1296+
1297+
/**
1298+
* Get detailed comment status information including API restrictions.
1299+
*
1300+
* This function provides comprehensive information about comment restrictions
1301+
* including post type restrictions, API-level restrictions, network settings,
1302+
* role exclusions, and comment counts.
1303+
*
1304+
* @return array Associative array with detailed status information:
1305+
* - 'status' => Main status (same as get_current_comment_status())
1306+
* - 'disabled_post_types' => Array of disabled post type slugs
1307+
* - 'disabled_post_type_labels' => Array of disabled post type labels
1308+
* - 'remove_everywhere' => Boolean indicating global disable
1309+
* - 'xmlrpc_disabled' => Boolean indicating XML-RPC comments disabled
1310+
* - 'rest_api_disabled' => Boolean indicating REST API comments disabled
1311+
* - 'total_post_types' => Total number of available post types
1312+
* - 'is_configured' => Boolean indicating if plugin is configured
1313+
* - 'total_comments' => Total number of comments in database
1314+
* - 'network_active' => Boolean indicating if plugin is network activated
1315+
* - 'sitewide_settings' => Site-wide settings status
1316+
* - 'role_exclusion_enabled' => Boolean indicating if role exclusions are enabled
1317+
* - 'excluded_roles' => Array of excluded role slugs
1318+
* - 'excluded_role_labels' => Array of human-readable excluded role names
1319+
*
1320+
* @since 2.5.2
1321+
*/
1322+
public function get_detailed_comment_status() {
1323+
try {
1324+
$status = $this->get_current_comment_status();
1325+
$disabled_post_types = $this->get_disabled_post_types();
1326+
$all_post_types = $this->get_all_post_types();
1327+
1328+
// Get human-readable labels for disabled post types
1329+
$disabled_labels = array();
1330+
foreach ($disabled_post_types as $post_type) {
1331+
if (isset($all_post_types[$post_type])) {
1332+
$disabled_labels[] = $all_post_types[$post_type]->labels->name;
1333+
} else {
1334+
// Fallback for custom post types not in the main list
1335+
$post_type_obj = get_post_type_object($post_type);
1336+
$disabled_labels[] = $post_type_obj ? $post_type_obj->labels->name : $post_type;
1337+
}
1338+
}
1339+
1340+
// Get total comments count
1341+
$total_comments = $this->get_all_comments_number();
1342+
1343+
// Determine site-wide settings status
1344+
$sitewide_settings = 'not_applicable';
1345+
if ($this->networkactive) {
1346+
$sitewide_settings = isset($this->options['sitewide_settings']) && $this->options['sitewide_settings'] ?
1347+
'enabled' : 'disabled';
1348+
}
1349+
1350+
// Process role-based exclusion information
1351+
$role_exclusion_enabled = isset($this->options['enable_exclude_by_role']) && $this->options['enable_exclude_by_role'];
1352+
$excluded_roles = isset($this->options['exclude_by_role']) ? $this->options['exclude_by_role'] : array();
1353+
1354+
// Get human-readable role names
1355+
$excluded_role_labels = array();
1356+
if ($role_exclusion_enabled && !empty($excluded_roles)) {
1357+
$editable_roles = get_editable_roles();
1358+
1359+
foreach ($excluded_roles as $role) {
1360+
if ($role === 'logged-out-users') {
1361+
$excluded_role_labels[] = __('Logged out users', 'disable-comments');
1362+
} elseif (isset($editable_roles[$role])) {
1363+
$excluded_role_labels[] = translate_user_role($editable_roles[$role]['name']);
1364+
} else {
1365+
$excluded_role_labels[] = $role;
1366+
}
1367+
}
1368+
}
1369+
1370+
return array(
1371+
'status' => $status,
1372+
'disabled_post_types' => $disabled_post_types,
1373+
'disabled_post_type_labels' => $disabled_labels,
1374+
'remove_everywhere' => $this->is_remove_everywhere(),
1375+
'xmlrpc_disabled' => !empty($this->options['remove_xmlrpc_comments']),
1376+
'rest_api_disabled' => !empty($this->options['remove_rest_API_comments']),
1377+
'total_post_types' => count($all_post_types),
1378+
'is_configured' => $this->is_configured(),
1379+
'total_comments' => $total_comments,
1380+
'network_active' => $this->networkactive,
1381+
'sitewide_settings' => $sitewide_settings,
1382+
'role_exclusion_enabled' => $role_exclusion_enabled,
1383+
'excluded_roles' => $excluded_roles,
1384+
'excluded_role_labels' => $excluded_role_labels
1385+
);
1386+
1387+
} catch (Exception $e) {
1388+
// Error handling - return safe defaults
1389+
error_log('Disable Comments: Error in get_detailed_comment_status() - ' . $e->getMessage());
1390+
return array(
1391+
'status' => 'none',
1392+
'disabled_post_types' => array(),
1393+
'disabled_post_type_labels' => array(),
1394+
'remove_everywhere' => false,
1395+
'xmlrpc_disabled' => false,
1396+
'rest_api_disabled' => false,
1397+
'total_post_types' => 0,
1398+
'is_configured' => false,
1399+
'total_comments' => 0,
1400+
'network_active' => false,
1401+
'sitewide_settings' => 'not_applicable',
1402+
'role_exclusion_enabled' => false,
1403+
'excluded_roles' => array(),
1404+
'excluded_role_labels' => array()
1405+
);
1406+
}
1407+
}
1408+
/**
1409+
* Add Disable Comments information to WordPress Site Health Info panel.
1410+
*
1411+
* This method integrates the plugin's status information into WordPress's
1412+
* built-in Site Health system for easy debugging and site overview.
1413+
*
1414+
* @param array $debug_info The debug information array.
1415+
* @return array Modified debug information array.
1416+
*
1417+
* @since 2.5.2
1418+
*/
1419+
public function add_site_health_info($debug_info) {
1420+
$data = $this->get_detailed_comment_status();
1421+
1422+
// Create the main status description
1423+
$status_descriptions = array(
1424+
'all' => __('Comments are disabled site-wide for all content types', 'disable-comments'),
1425+
'posts' => __('Comments are disabled only for blog posts', 'disable-comments'),
1426+
'pages' => __('Comments are disabled only for pages', 'disable-comments'),
1427+
'posts,pages' => __('Comments are disabled for both posts and pages', 'disable-comments'),
1428+
'multiple' => __('Comments are disabled for multiple specific content types', 'disable-comments'),
1429+
'none' => __('Comments are enabled everywhere', 'disable-comments'),
1430+
);
1431+
1432+
$status_description = isset($status_descriptions[$data['status']]) ?
1433+
$status_descriptions[$data['status']] :
1434+
sprintf(__('Comments are disabled for: %s', 'disable-comments'), $data['status']);
1435+
1436+
// Format site-wide settings value
1437+
$sitewide_settings_labels = array(
1438+
'enabled' => __('Enabled', 'disable-comments'),
1439+
'disabled' => __('Disabled', 'disable-comments'),
1440+
'not_applicable' => __('Not applicable', 'disable-comments'),
1441+
);
1442+
1443+
// Build the fields array using data from get_detailed_comment_status()
1444+
$fields = array(
1445+
'status' => array(
1446+
'label' => __('Comment Status', 'disable-comments'),
1447+
'value' => $status_description,
1448+
),
1449+
'plugin_configured' => array(
1450+
'label' => __('Plugin Configured', 'disable-comments'),
1451+
'value' => $data['is_configured'] ? __('Yes', 'disable-comments') : __('No', 'disable-comments'),
1452+
),
1453+
'total_comments' => array(
1454+
'label' => __('Total Comments', 'disable-comments'),
1455+
'value' => number_format_i18n($data['total_comments']),
1456+
),
1457+
'global_disable' => array(
1458+
'label' => __('Global Disable Active', 'disable-comments'),
1459+
'value' => $data['remove_everywhere'] ? __('Yes', 'disable-comments') : __('No', 'disable-comments'),
1460+
),
1461+
'disabled_post_type_count' => array(
1462+
'label' => __('Disabled Post Types Count', 'disable-comments'),
1463+
'value' => sprintf('%d of %d', count($data['disabled_post_types']), $data['total_post_types']),
1464+
),
1465+
'disabled_post_types' => array(
1466+
'label' => __('Disabled Post Types', 'disable-comments'),
1467+
'value' => !empty($data['disabled_post_type_labels']) ?
1468+
implode(', ', $data['disabled_post_type_labels']) :
1469+
__('None', 'disable-comments'),
1470+
),
1471+
'xmlrpc_comments' => array(
1472+
'label' => __('XML-RPC Comments', 'disable-comments'),
1473+
'value' => $data['xmlrpc_disabled'] ? __('Disabled', 'disable-comments') : __('Enabled', 'disable-comments'),
1474+
),
1475+
'rest_api_comments' => array(
1476+
'label' => __('REST API Comments', 'disable-comments'),
1477+
'value' => $data['rest_api_disabled'] ? __('Disabled', 'disable-comments') : __('Enabled', 'disable-comments'),
1478+
),
1479+
'network_active' => array(
1480+
'label' => __('Network Active', 'disable-comments'),
1481+
'value' => $data['network_active'] ? __('Yes', 'disable-comments') : __('No', 'disable-comments'),
1482+
),
1483+
'sitewide_settings' => array(
1484+
'label' => __('Site-wide Settings', 'disable-comments'),
1485+
'value' => $sitewide_settings_labels[$data['sitewide_settings']],
1486+
),
1487+
'role_exclusion_enabled' => array(
1488+
'label' => __('Role-based Exclusions', 'disable-comments'),
1489+
'value' => $data['role_exclusion_enabled'] ? __('Enabled', 'disable-comments') : __('Disabled', 'disable-comments'),
1490+
),
1491+
'excluded_roles' => array(
1492+
'label' => __('Excluded Roles', 'disable-comments'),
1493+
'value' => !empty($data['excluded_role_labels']) ?
1494+
implode(', ', $data['excluded_role_labels']) :
1495+
__('None', 'disable-comments'),
1496+
),
1497+
);
1498+
1499+
// Add the section to Site Health
1500+
$debug_info['disable-comments'] = array(
1501+
'label' => __('Disable Comments', 'disable-comments'),
1502+
'description' => __('Complete overview of comment disable settings and configuration.', 'disable-comments'),
1503+
'fields' => $fields,
1504+
);
1505+
1506+
return $debug_info;
1507+
}
12111508
}
12121509

12131510
Disable_Comments::get_instance();

0 commit comments

Comments
 (0)