Make WordPress Core

Opened 22 months ago

Closed 5 months ago

Last modified 5 months ago

#58265 closed enhancement (fixed)

Show defined but empty values for constants in Site Health

Reported by: presskopp's profile Presskopp Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.7 Priority: normal
Severity: normal Version: 5.2
Component: Site Health Keywords: has-screenshots good-first-bug has-patch
Focuses: Cc:

Description

Right now, for example if DB_COLLATE is defined as '', the value will not be shown in the WordPress Constants list, there's just an empty space. What about exchanging empty, but defined values for "Defined, but empty" or "empty value" or something similar for more clarity?

Patching class-wp-debug-data.php, ~L. 333 does the job:

'value' => ( defined( 'DB_COLLATE' ) ? ( DB_COLLATE === '' ? __( 'Defined as empty' ) : DB_COLLATE ) : __( 'Undefined' ) ),


Attachments (1)

empty.png (14.8 KB) - added by Presskopp 22 months ago.

Download all attachments as: .zip

Change History (14)

@Presskopp
22 months ago

#1 @Presskopp
22 months ago

Of course if this would be welcomed, debug should also be altered

#2 @Presskopp
20 months ago

  • Keywords good-first-bug added

This ticket was mentioned in PR #4765 on WordPress/wordpress-develop by @Presskopp.


20 months ago
#3

  • Keywords has-patch added

This patch is mainly to test providing patches using github

#4 @iflairwebtechnologies
7 months ago

Hello @Presskopp

Displaying a message like "Defined, but empty" or "empty value" for constants with empty but defined values would definitely improve clarity. This way, users can distinguish between undefined constants and those that are defined but empty. Here’s how you might implement this in a WordPress environment:

Example Code Implementation
Find the code responsible for displaying the constants:

Typically, this would be in a debugging or settings page, such as in a custom admin page or a debugging plugin.
Modify the display logic:

Add a check to see if the constant is defined and if its value is an empty string. If so, set the display value to "Defined, but empty" or similar.
Here's an example of how you might modify the code to achieve this:

<?php
// Function to display WordPress constants
function display_wp_constants() {
    $constants = [
        'DB_COLLATE',
        'DB_CHARSET',
        'WP_DEBUG',
        // Add other constants as needed
    ];

    echo '<table>';
    echo '<tr><th>Constant</th><th>Value</th></tr>';

    foreach ($constants as $constant) {
        if (defined($constant)) {
            $value = constant($constant);
            if ($value === '') {
                $display_value = 'Defined, but empty';
            } else {
                $display_value = $value;
            }
        } else {
            $display_value = 'Not defined';
        }

        echo '<tr><td>' . $constant . '</td><td>' . $display_value . '</td></tr>';
    }

    echo '</table>';
}

Steps to Implement
Create a Custom Plugin or Theme Function:

If this is for a custom plugin or theme, you can add the display_wp_constants function to the appropriate file (e.g., functions.php in a theme or the main plugin file).
Hook into the Admin Page:

Hook this function into the appropriate admin page or section where you want to display the constants.

<?php
add_action('admin_menu', 'add_constants_display_page');

function add_constants_display_page() {
    add_menu_page(
        'WP Constants',
        'WP Constants',
        'manage_options',
        'wp-constants',
        'display_wp_constants',
        'dashicons-admin-generic'
    );
}

#5 @Clorith
6 months ago

  • Keywords has-patch removed
  • Milestone changed from Awaiting Review to Future Release
  • Version set to 5.2

Good point, and would indeed help avoid users thinking something is wrong.

I wonder if perhaps, to not cause confusion, the "Defined, but empty" message should have emphasis, so it's easier to distinguish from a string that may hold "Empty" as it's value (unlikely scenario, but we don't know what strange strings may be used in constants out there by plugins or themes or custom setups).

I don't think the debug value should be modified though, it should be the actual contents of the constant, and empty if it is indeed empty, as the debug data that's copied and shared should represent the raw value, and does not need to be in a user-readable format.

Marking this for future release, as it does not currently have a valid patch, but happy to move forward if and when that happens.

This ticket was mentioned in PR #7214 on WordPress/wordpress-develop by @brobken.


6 months ago
#6

  • Keywords has-patch added

Added additional information about the constant DB_COLLATE as per request in the Trac ticket.

#7 @brobken
6 months ago

@Clorith I added the requested information.
If the constant wasn't set, it was already displaying 'Undefined'.

The problem was only when the constant was set, but empty. This PR should resolve this issue and won't show empty content anymore.

Regarding styling: wouldn't do that as this is a debug section which needs clean output for copy paste as well.

#8 @SergeyBiryukov
6 months ago

  • Milestone changed from Future Release to 6.7
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

Related: #58484

#9 @desrosj
5 months ago

@SergeyBiryukov are you able to get this reviewed prior to beta 1?

This ticket was mentioned in Slack in #core by jorbin. View the logs.


5 months ago

#11 @jorbin
5 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 59147:

Site Health: Note when DB_COLLATE is defined but empty.

Right now, for example if DB_COLLATE is defined as , the value will not be shown in the WordPress Constants list, there's just an empty space. This adds a message so it's clearer when the constant is empty.

Props Presskopp, brobken, Clorith.
Fixes #58265.

#13 @SergeyBiryukov
5 months ago

In 59155:

Site Health: Adjust display of the DB_COLLATE and WP_ENVIRONMENT_TYPE constants.

Includes:

  • Simplifying the logic and bringing some consistency to how the values are checked and displayed.
  • Correcting the debug value for DB_COLLATE. This should be the actual contents of the constant, and empty if it is indeed empty, as the debug data that's copied and shared should represent the raw value, and does not need to be in a user-readable format.

Follow-up to [45782], [52021], [54239], [59147].

Props Clorith, SergeyBiryukov.
See #58265.

Note: See TracTickets for help on using tickets.