Skip to content

Instantly share code, notes, and snippets.

@allysonsouza
Last active July 21, 2021 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allysonsouza/7868dc5980e704a56ab2c9f60a4adbcd to your computer and use it in GitHub Desktop.
Save allysonsouza/7868dc5980e704a56ab2c9f60a4adbcd to your computer and use it in GitHub Desktop.
WP Install Command - Copy command to install plugins trough WP-CLI or Composer easily.
<?php
/*
Plugin Name: Install Commands
Plugin URI:
Description: Copy command to install plugins trough WP-CLI or Composer.
Author: Allyson Souza
Version: 1.0
Author URI: https://hastedesign.com.br
Text Domain: install-commands
Domain Path: /languages
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Create the option page.
* Hook: admin_menu
*
* @return void
*/
function install_commands_options_page() {
add_options_page(
'Install Commnads',
'Install Commands',
'manage_options',
'install-commands',
'install_commands_html'
);
}
add_action( 'admin_menu', 'install_commands_options_page' );
/**
* Create the option page HTML.
*
* @return void
*/
function install_commands_html() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'Install Commands', 'install-commands' ); ?></h1>
<h2 class="title"><?php esc_html_e( 'Composer WPackagist command', 'install-commands' ); ?></h2>
<textarea class="large-text code" rows="10" cols="50"><?php wpackagist_command(); ?></textarea>
<h2 class="title"><?php esc_html_e( 'WP-CLI command', 'install-commands' ); ?></h2>
<textarea class="large-text code" rows="10" cols="50"><?php wpcli_command(); ?></textarea>
<h2 class="title"><?php esc_html_e( 'Parameters', 'install-commands' ); ?></h2>
<p><?php esc_html_e( 'Set some parameters to customize the install commands generation. You can pass any character as separator, and choose if you want installed or only active plugins, for example.', 'install-commands' ); ?></p>
<form action="" method="post">
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row">
<label for="separator"><?php esc_html_e( 'Separator', 'install-commands' ); ?></label>
</th>
<td>
<input type="text" id="separator" class="small-text" name="separator" />
<p class="description" id="tagline-description"><?php printf( esc_html__( 'Choose the separator between commands, i.e: %s', 'install-commands' ), '<pre> ; | && | , </pre>' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="separator"><?php esc_html_e( 'Plugins', 'install-commands' ); ?></label>
</th>
<td>
<div class="plugin-options">
<p>
<input type="radio" name="plugins-included" id="plugins-active" value="active" checked="checked">
<label for="plugins-active"><?php esc_html_e( 'Active', 'install-commands' ); ?></label>
</p>
<p>
<input type="radio" name="plugins-included" id="plugins-installed" value="installed">
<label for="plugins-installed"><?php esc_html_e( 'Installed', 'install-commands' ); ?></label>
</p>
</div>
</td>
</tr>
</tbody>
</table>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Generate commands', 'install-commands' ); ?>"></p>
</form>
</div>
<?php
}
/**
* Output WPackagist Composer require command.
*/
function wpackagist_command() {
$plugins_include = get_plugins_included();
$plugins = get_plugin_list( $plugins_include );
$slugs = array();
foreach ( $plugins as $plugin => $meta ) {
if ( ! is_array( $meta ) ) {
$plugin = $meta;
}
$slugs[] = 'composer require wpackagist-plugin/' . sanitize_title( get_plugin_slug( $plugin ) );
}
print_r( implode( ' ' . get_separator() . ' ', $slugs ) );
}
/**
* Output WP-CLI install command.
*/
function wpcli_command() {
$plugins_include = get_plugins_included();
$plugins = get_plugin_list( $plugins_include );
$slugs = array();
foreach ( $plugins as $plugin => $meta ) {
if ( ! is_array( $meta ) ) {
$plugin = $meta;
}
$slugs[] = 'wp plugin install ' . sanitize_title( get_plugin_slug( $plugin ) ) . ' --activate';
}
print_r( implode( ' ' . get_separator() . ' ', $slugs ) );}
/**
* Return the plugin slug.
*
* @param string $plugin
* @return string $slug
*/
function get_plugin_slug( $plugin ) {
$slash_pos = strpos( $plugin, '/' );
if ( ! $slash_pos === false ) {
$slug = substr( $plugin, 0, $slash_pos );
} else {
$slug = str_replace( '.php', '', $plugin );
}
return $slug;
}
/**
* Return the plugin list according to plugins to be included:
* active or installed.
*
* @param string $plugins_include 'active' or 'installed'.
* @return array Plugin list.
*/
function get_plugin_list( $plugins_include ) {
switch ( $plugins_include ) {
case 'active';
return get_active_plugins();
case 'installed';
return get_plugins();
default;
return get_plugins();
break;
}
}
/**
* Return active plugins list.
*
* @return array
*/
function get_active_plugins() {
return get_option( 'active_plugins' );
}
/**
* Get separator
*
* @return string Separator.
*/
function get_separator() {
$separator = $_REQUEST['separator'] ?? ';';
return sanitize_text_field( $separator );
}
/**
* Get plugins included.
*/
function get_plugins_included() {
$included = $_REQUEST['plugins-included'] ?? 'active';
return sanitize_text_field( $included );
}
@allysonsouza
Copy link
Author

To-Do: get the plugin versions to generate the commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment