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

add wp_toggle_dev_plugins example #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add wp_toggle_dev_plugins example #153

wants to merge 1 commit into from

Conversation

adamedgmond
Copy link

This example will show how you can automatically activate and deactivate plugins based on environment on deploy. This will ease development and potentially improve performance on Live.

@adamedgmond
Copy link
Author

Heyo! @davidneedham @mcdwayne @ataylorme , any chance any of you fine gentlemen can take a gander?

@greg-1-anderson
Copy link
Member

Looks very useful. Maybe reduce duplication by combining case test with case dev (maybe even make it the default, so that it applies to multidevs) & use a variable for the env name in the notice.

@mcdwayne
Copy link

Good old switch case :)
Curious if there is a list of dev plugins you can reference.

@ataylorme
Copy link
Contributor

In the example, there is one plugin Hello Dolly (hello) and multiple cases (dev, test and live). What about listing the plugins to be activated and deactivated and the different environments in an array then looping through it? Something like

$plugins_to_activate_and_deactivate = array (
	'dev' => array (
		'hello' => true,
	),
	'test' => array (
		'hello' => true,
	),
	'live' => array (
		'hello' => false,
	),
);

if ( ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
	echo "Toggle developer plugins: Activating/deactivating plugins for the " . $_ENV['PANTHEON_ENVIRONMENT'] . " environment... \n";
	if( isset( $plugins_to_activate_and_deactivate[ $_ENV['PANTHEON_ENVIRONMENT'] ] ) ) {
		foreach( $plugins_to_activate_and_deactivate[ $_ENV['PANTHEON_ENVIRONMENT'] ] as $plugin_active ){
			$plugin_name = key($plugin_active);
			if( $plugin_active ) {
				echo "Toggle developer plugins: activating $plugin_name ... \n";
				passthru('wp plugin activate $plugin_name');
			} else {
				echo "Toggle developer plugins: deactivating $plugin_name ... \n";
				passthru('wp plugin deactivate $plugin_name');
			}
		}
	} else {
		echo "Toggle developer plugins: No active/deactive plugin definitions found for the " . $_ENV['PANTHEON_ENVIRONMENT'] . " environment";
	}
}

@adamedgmond
Copy link
Author

@mcdwayne debug bar plugin(s) are likely candidates for being selective of running in production, depending on how many extensions one is running.
@ataylorme nice code, i'll give it a spin ~

@greg-1-anderson
Copy link
Member

I think @ataylorme 's new scheme would actually be pretty inconvenient unless you map every multidev environment to dev:

$env_name = isset($plugins_to_activate_and_deactivate[ $_ENV['PANTHEON_ENVIRONMENT'] ]) ?  $_ENV['PANTHEON_ENVIRONMENT'] : 'dev';

Without ^^, you'd have to keep re-registering the same set of dev modules every time you made a new multidev.

@adamedgmond
Copy link
Author

thanks for the contributions @greg-1-anderson @ataylorme!

this code is close to working but won't run in live. something about the while loop and $plugin_active equalling 1.

$plugins_to_toggle = array (
	'dev' => array (
	'debug-bar' => true,
    'debug-bar-slow-actions' => true,
    'debug-bar-list-dependencies' => true
	),
	'test' => array (
    'debug-bar' => true,
    'debug-bar-slow-actions' => true,
    'debug-bar-list-dependencies' => true
	),
	'live' => array (
    'debug-bar' => false,
    'debug-bar-slow-actions' => false,
    'debug-bar-list-dependencies' => false
	),
);

if ( ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
	echo "Toggle developer plugins: Activating/deactivating plugins for the " . $_ENV['PANTHEON_ENVIRONMENT'] . " environment... \n";
  $env_name = isset($plugins_to_toggle[ $_ENV['PANTHEON_ENVIRONMENT'] ]) ?  $_ENV['PANTHEON_ENVIRONMENT'] : 'dev';
	if( isset( $plugins_to_toggle[ $env_name ] ) ) {
		while ($plugin_active = current($plugins_to_toggle[ $env_name ])) {
			$plugin_name = key($plugins_to_toggle[ $env_name ]);
			if ($plugin_active) {
				echo "Toggle developer plugins: activating $plugin_name ... \n";
				passthru('wp plugin activate '.$plugin_name);
			} else {
				echo "Toggle developer plugins: deactivating $plugin_name ... \n";
				passthru('wp plugin deactivate '.$plugin_name);
			}
			next($plugins_to_toggle[ $env_name ]);
		}

	} else {
		echo "Toggle developer plugins: No plugin definitions found for the " . $_ENV['PANTHEON_ENVIRONMENT'] . " environment";
	}
}

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.

4 participants