Skip to content

Instantly share code, notes, and snippets.

@crstauf
Last active October 22, 2023 03:50
Show Gist options
  • Save crstauf/793d8d70dd6d2572e698f8b46ab6316b to your computer and use it in GitHub Desktop.
Save crstauf/793d8d70dd6d2572e698f8b46ab6316b to your computer and use it in GitHub Desktop.
MU-plugin to prevent production functionality on development sites.
<?php
/**
* Plugin Name: Development Environment Preventions
* Description: MU-plugin to prevent production functionality on development sites.
* Plugin URI: https://gist.github.com/crstauf/793d8d70dd6d2572e698f8b46ab6316b
* Author: Caleb Stauffer
* Author URI: https://develop.calebstauffer.com
*/
$production_site_url = '{PRODUCTION SITE URL}';
$dev_email_address = '{DEV EMAIL ADDRESS}';
$apply_preventions = false;
if ( '{PRODUCTION SITE URL}' === $production_site_url )
trigger_error( 'Update $production_site_url in ' . __FILE__ );
if ( '{DEV EMAIL ADDRESS}' === $dev_email_address )
trigger_error( 'Update $dev_email_address in ' . __FILE__ );
if ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) $apply_preventions = true;
if ( defined( 'WP_DEVELOP' ) && WP_DEVELOP ) $apply_preventions = true;
if ( get_site_url() !== $production_site_url ) $apply_preventions = true;
if ( !$apply_preventions ) {
/**
* Add inactive status indication to mu-plugin row meta.
*/
add_filter( 'plugin_row_meta', function ( $meta, $plugin_file ) {
if ( basename( __FILE__ ) !== $plugin_file )
return $meta;
array_unshift( $meta, '<strong>Inactive</strong>' );
return $meta;
}, 10, 2 );
return;
}
/*
## ## ####### ######## #### ###### ######## ######
### ## ## ## ## ## ## ## ## ## ##
#### ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ###### ######
## #### ## ## ## ## ## ## ##
## ### ## ## ## ## ## ## ## ## ##
## ## ####### ## #### ###### ######## ######
*/
/**
* Add a notice in admin so we're aware of the preventions.
*/
add_action( 'admin_notices', function() {
echo '<div class="notice notice-warning">' .
'<p>Behavior modification by <code>' . __FILE__ . '</code>; site is identified as development environment.</p>' .
'</div>';
}, -999 );
/**
* Add active status indication to mu-plugin row meta.
*/
add_filter( 'plugin_row_meta', function ( $meta, $plugin_file ) {
if ( basename( __FILE__ ) !== $plugin_file )
return $meta;
array_unshift( $meta, '<strong style="color: red;">!!! ACTIVE !!!</strong>' );
return $meta;
}, 10, 2 );
/*
######## ######## ######## ## ## ######## ## ## ######## #### ####### ## ## ######
## ## ## ## ## ## ## ## ### ## ## ## ## ## ### ## ## ##
## ## ## ## ## ## ## ## #### ## ## ## ## ## #### ## ##
######## ######## ###### ## ## ###### ## ## ## ## ## ## ## ## ## ## ######
## ## ## ## ## ## ## ## #### ## ## ## ## ## #### ##
## ## ## ## ## ## ## ## ### ## ## ## ## ## ### ## ##
## ## ## ######## ### ######## ## ## ## #### ####### ## ## ######
*/
// set staging constant for Square payment gateway
define( 'WC_SQUARE_ENABLE_STAGING', true );
/**
* Override WordPress admin email.
*/
add_filter( 'pre_option_admin_email', function ( $email_address ) use ( $dev_email_address ) { return $dev_email_address; } );
add_filter( 'pre_site_option_admin_email', function ( $email_address ) use ( $dev_email_address ) { return $dev_email_address; } );
/**
* Direct all emails to development (prevent emails to customers).
*/
add_filter( 'wp_mail', function ( $args ) use ( $dev_email_address ) { $args['to'] = $dev_email_address; return $args; }, 999 );
/**
* Prevent running of AutomateWoo workflows.
* @link https://automatewoo.com/ Plugin site.
* @see AutomateWoo\Workflow::run()
* @since AutomateWoo 2.6.6
*/
define( 'AW_PREVENT_WORKFLOWS', true );
/**
* Prevent WooCommerce Subscription renewals.
* @link https://woocommerce.com/products/woocommerce-subscriptions/ Plugin site.
* @see WC_Subscriptions::is_duplicate_site()
*/
add_filter( 'woocommerce_subscriptions_is_duplicate_site', '__return_true' );
/**
* Prevent processing of AutomateWoo workflows.
* @link https://automatewoo.com/ Plugin site.
* @see AutomateWoo\Workflow::validate_workflow()
*/
add_filter( 'automatewoo_custom_validate_workflow', '__return_false' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment