'object', 'sanitize_callback' => 'envwise_sanitize_settings', 'default' => array( 'production_url' => 'https://www.yoursite.com/', 'disable_when_plugin_disabled' => true, ), ) ); } /** * Sanitize settings. * * @param array $input Raw settings input. * @return array Sanitized settings. */ function envwise_sanitize_settings( $input ) { $sanitized = array(); if ( isset( $input['production_url'] ) ) { $sanitized['production_url'] = esc_url_raw( trim( $input['production_url'] ) ); // Ensure URL has trailing slash for consistency. if ( ! empty( $sanitized['production_url'] ) && substr( $sanitized['production_url'], -1 ) !== '/' ) { $sanitized['production_url'] .= '/'; } } // Sanitize the disable when plugin disabled setting. $sanitized['disable_when_plugin_disabled'] = isset( $input['disable_when_plugin_disabled'] ) ? (bool) $input['disable_when_plugin_disabled'] : false; return $sanitized; } /** * Get plugin settings */ function envwise_get_settings() { $defaults = array( 'production_url' => 'https://www.yoursite.com/', 'disable_when_plugin_disabled' => true, ); $settings = get_option( ENVWISE_OPTION, array() ); return wp_parse_args( $settings, $defaults ); } /** * Get production URL from settings */ function envwise_get_production_url() { $settings = envwise_get_settings(); return $settings['production_url']; } /** * Admin page content */ function envwise_admin_page() { // Check user capabilities. if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = envwise_get_settings(); $current_url = home_url( '/' ); $is_production = envwise_is_production_environment(); ?>

Envwise automatically manages WordPress settings based on your environment (production vs non-production). This plugin serves as a framework that you can extend to implement custom environment-based controls for any WordPress setting.', 'envwise' ), array( 'strong' => array() ) ); ?>

✓ ✗
✓ ✗
✓ ⚠

  1. WP_ENV constant is defined, it takes priority over URL matching', 'envwise' ), array( 'code' => array() ) ); ?>
  2. WP_ENV is not defined, the plugin compares the current URL with the production URL', 'envwise' ), array( 'code' => array() ) ); ?>

' . __( 'Settings', 'envwise' ) . ''; array_unshift( $links, $settings_link ); return $links; } /** * Check if current environment is production * * @return boolean True if production, false otherwise */ function envwise_is_production_environment() { // Check WP_ENV if defined. if ( defined( 'WP_ENV' ) ) { return WP_ENV === 'production'; } else { // Check HOME URL against production URL from settings. $production_url = envwise_get_production_url(); $current_url = home_url( '/' ); return $current_url === $production_url; } } /** * Filter the value of the 'blog_public' option based on environment check. */ add_filter( 'pre_option_blog_public', function () { if ( ! envwise_is_production_environment() ) { return 0; // Force discourage search engines. } else { return 1; // Allow search engines in production. } } ); /** * Display a notice in the admin area if we're not in production. * This notice self-dismisses when the environment becomes production. */ add_action( 'admin_notices', function () { // Only show on plugin settings page and dashboard to avoid overwhelming users. $current_screen = get_current_screen(); if ( ! $current_screen || ! in_array( $current_screen->id, array( 'dashboard', 'tools_page_envwise' ), true ) ) { return; } if ( ! envwise_is_production_environment() ) { $message = '' . __( 'Envwise Notice:', 'envwise' ) . ' '; if ( defined( 'WP_ENV' ) && WP_ENV !== 'production' ) { /* translators: %s: The current WP_ENV value (e.g., development, staging) */ $message .= sprintf( __( 'WP_ENV is set to %s.', 'envwise' ), '' . esc_html( WP_ENV ) . '' ); } else { $message .= __( 'Current URL does not match production URL.', 'envwise' ); } $message .= ' ' . __( 'Search engine indexing is automatically disabled.', 'envwise' ); $message .= ' ' . __( 'Configure Envwise', 'envwise' ) . ''; echo '

' . wp_kses_post( $message ) . '

'; } } ); /** * Plugin activation hook */ register_activation_hook( __FILE__, 'envwise_activate' ); /** * Runs on plugin activation to apply the current environment logic immediately. */ function envwise_activate() { // Migrate settings saved by versions released under the previous name. $legacy_settings = get_option( 'env_control_settings' ); if ( false !== $legacy_settings && false === get_option( ENVWISE_OPTION ) ) { update_option( ENVWISE_OPTION, $legacy_settings ); } delete_option( 'env_control_settings' ); // When plugin is activated, apply the current environment logic. if ( envwise_is_production_environment() ) { update_option( 'blog_public', '1' ); // Allow search engines. } else { update_option( 'blog_public', '0' ); // Discourage search engines. } } /** * Plugin deactivation hook. */ register_deactivation_hook( __FILE__, 'envwise_deactivate' ); /** * Runs on plugin deactivation. * * When the "disable_when_plugin_disabled" setting is enabled, search engines are * always allowed. Otherwise blog_public is synced to the current environment, * because the pre_option filter never writes to the database and the stored value * may be stale (e.g. a production database cloned to staging). */ function envwise_deactivate() { $settings = envwise_get_settings(); if ( $settings['disable_when_plugin_disabled'] || envwise_is_production_environment() ) { update_option( 'blog_public', '1' ); // Allow search engines. } else { update_option( 'blog_public', '0' ); // Keep non-production discouraged. } }