Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for environment types and fix description of plugin #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mcaskill
Copy link

Fixed the description of the plugin in the README to indicate that it relies on the value of a DISALLOW_INDEXING constant and not WP_ENV. The latter constant only being used as a reference in an dashboard notice.

Also added support for WP_ENVIRONMENT_TYPE and support for omitting WP_ENV. Proposed behaviour:

  1. Resolve the environment type:
    1. Use the value of the WP_ENV constant if defined and palpable.
    2. Otherwise, use the value of the wp_get_environment_type() function if defined (introduced in WordPress 5.0.0).
    3. Otherwise, use NULL.
  2. Filter the environment type with the following hook:
    • roots/bedrock/disallow_indexing_environment_type
  3. Display the notice:
    1. If the environment type is palpable, display the notice with the filtered environment type.
    2. Otherwise, display a shorter notice that does not reference the environment type.

@retlehs
Copy link
Member

retlehs commented Jan 23, 2025

Thank you for the PR! My apologies that this slipped through the cracks.

Thanks for the README clarification about DISALLOW_INDEXING being the key constant. The new filter hook is also a good addition.

I'm not a fan of if / else's, what about something like this?

add_action('admin_notices', function () {
    $env = defined('WP_ENV') && WP_ENV ? WP_ENV : null;
    
    if (!$env && function_exists('wp_get_environment_type')) {
        $env = wp_get_environment_type();
    }

    $env = apply_filters('roots/bedrock/disallow_indexing_environment_type', $env);
    
    if (!$env) {
        echo "<div class='notice notice-warning'><p><strong>Bedrock:</strong> " . 
             __('Search engine indexing has been discouraged.', 'roots') . 
             "</p></div>";
        return;
    }

    printf(
        "<div class='notice notice-warning'><p>%s</p></div>",
        sprintf(
            __('%1$s Search engine indexing has been discouraged because the current environment is %2$s.', 'roots'),
            '<strong>Bedrock:</strong>',
            '<code>' . $env . '</code>'
        )
    );
});

Also noting that Bedrock added support for WP_ENVIRONMENT_TYPE a bit after this PR: roots/bedrock#668

mcaskill added a commit to mcaskill/bedrock-disallow-indexing that referenced this pull request Jan 23, 2025
Prefer fewer else statements and early returns.

See: roots#1 (comment)
@mcaskill
Copy link
Author

@retlehs Thanks for your suggestion. I've included it in my changeset.

New behaviour:

1. Resolve the environment type:
	1. Use the value of the `WP_ENV` constant if defined and palpable.
	2. Otherwise, use the value of the `wp_get_environment_type()` function if defined (introduced in WordPress 5.0.0).
	3. Otherwise, use `NULL`.
2. Filter the environment type with the following hook:
	* `roots/bedrock/disallow_indexing_environment_type`
3. Display the notice:
	1. If the environment type is palpable, display the notice with the filtered environment type.
	2. Otherwise, display a shorter notice that does not reference the environment type.
Fixed the description of the plugin to indicate that it relies on the value of a `DISALLOW_INDEXING` constant and not `WP_ENV`. The latter constant only being used as a reference in an dashboard notice.
Prefer fewer else statements and early returns.

See: roots#1 (comment)
@mcaskill mcaskill force-pushed the feature/wp-environment-type branch from 7c07dd8 to 31a1cf7 Compare January 23, 2025 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants