Last active
October 23, 2017 12:40
-
-
Save rxnlabs/c6883f2808e71836fb78c76a94031e1e to your computer and use it in GitHub Desktop.
PHP + Bash - Move plugins from wordpress single install to composer.json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Change the upload path of the sites to make sure media files still work | |
SELECT * FROM wpdb_6_options WHERE option_name IN ('upload_path', 'upload_url_path'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir -p exported-sites && find . -maxdepth 1 -type d -exec bash -c 'cd "$0" && ~/wp-cli.phar mu-migration export all "$0".zip --uploads --themes && wp plugin list --format=json | xargs -0 php ../plugin-convert.php --save="../exported-sites" --domain="$0" && mv "$0".zip ../exported-sites/' {} \; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$short_args = 's::p::d::'; | |
$long_args = array( | |
'save::', | |
'plugins::', | |
'domain::' | |
); | |
// get the arguments passed to the script from the command line | |
$options = getopt($short_args, $long_args); | |
if (array_key_exists('s', $options) && !array_key_exists('save', $options)) { | |
$options['save'] = $options['s']; | |
} | |
if (array_key_exists('p', $options) && !empty($options['p']) && !array_key_exists('plugins', $options)) { | |
$plugins_installed = $options['p']; | |
} elseif (array_key_exists('plugins', $options) && !empty($options['plugins'])) { | |
$plugins_installed = $options['plugins']; | |
} else { | |
// get the plugins currently installed since stream data is passed to PHP as the last argument | |
$plugins_installed = end($argv); | |
} | |
$composer_file = 'composer.json'; | |
if (array_key_exists('save', $options) && !empty($options['save'])) { | |
$composer_file = $options['save'].'/'.$composer_file; | |
} else { | |
$composer_file = getcwd().'/'.$composer_file; | |
} | |
// create the save folder if it doesn't already exists | |
if (!file_exists($options['save'])) { | |
mkdir($options['save'], 0700); | |
} | |
if (!empty($plugins_installed) && !empty($composer_file)) { | |
$installed_plugins = json_decode($plugins_installed,true); | |
} else { | |
echo "No plugins found or installed for the WordPress site and/or unable to get the folder where the composer.json file should be installed".PHP_EOL; | |
die; | |
} | |
if (empty($installed_plugins)) { | |
die; | |
} | |
// check to see if the composer.json file already exists at the specified directory | |
if (file_exists($composer_file)) { | |
$handle = fopen($composer_file, 'r'); | |
$other_plugins = fread($handle, filesize($composer_file)); | |
fclose($handle); | |
} | |
$wp_packagist_plugin_namespace = 'wpackagist-plugin/'; | |
$plugins = array(); | |
// remove the wpackagist namespace from the plugin | |
if (!empty($other_plugins)) { | |
$other_plugins = json_decode(json_decode(json_encode($other_plugins)), TRUE); | |
$composer = $other_plugins; | |
$other_plugins = $other_plugins['require']; | |
foreach ($other_plugins as $plugin_name=>$version) { | |
$saved_plugin = str_replace($wp_packagist_plugin_namespace, '', $plugin_name); | |
$plugins[] = array('name'=>$saved_plugin, 'version'=>$version); | |
} | |
} | |
// add plugins to composer.json | |
foreach ($installed_plugins as $plugin) { | |
$plugin_found = false; | |
if (!empty($other_plugins)) { | |
// search for the plugins already written in the composer.json | |
foreach ($other_plugins as $name=>$version) { | |
$currently_installed_plugin = str_replace($wp_packagist_plugin_namespace, '', $name); | |
if ($currently_installed_plugin == $plugin['name']) { | |
$plugin_found = true; | |
$version = $version; | |
break; | |
} | |
} | |
} | |
if ($plugin_found === false) { | |
$plugins[] = array('name'=>$plugin['name'], 'version'=>$plugin['version']); | |
} | |
} | |
$exclude_plugins = array('backupbuddy', 'gravityforms', 'InstaBuilder', 'fb-traffic-pop', 'advanced-custom-fields', 'acf-repeater', 'acf-options', 'acf-flexible-con'); | |
$plugin_array = array(); | |
$project_name = 'rxnlabs/exported-plugins'; | |
$description = 'Exported WordPress plugins'; | |
$plugin_array['name'] = $project_name; | |
$plugin_array['description'] = $description; | |
$plugin_array['repositories'][] = array('type'=>'composer', 'url' => 'https://wpackagist.org'); | |
$composer_plugins = array(); | |
// remove any plugins in the excluded list (e.g. bad plugins, premium plugins, etc...) | |
foreach ($plugins as $plugin) { | |
$plugin_name = strtolower($plugin['name']); | |
// fix issue where the stored wordpress plugin in the DB and spit out by wp-cli is malformed (e.g. {"name":"rajce-embed\/rajce-embed","status":"inactive","update":"none","version":"1.5.1"}) | |
if (strpos($plugin_name, '/') !== false ) { | |
$plugin_name = explode('/', $plugin_name); | |
$plugin_name = end($plugin_name); | |
} | |
$plugin_version = $plugin['version']; | |
$bad_plugin_found = false; | |
foreach ($exclude_plugins as $bad_plugin) { | |
$bad_plugin = strtolower($bad_plugin); | |
if (strstr($plugin_name, $bad_plugin) == true) { | |
$bad_plugin_found = true; | |
break; | |
} | |
} | |
if ($bad_plugin_found == false){ | |
$composer_plugins[$plugin_name] = $plugin_version; | |
} | |
} | |
// added any extra plugins if they're not already installed | |
$composer_plugins['wordfence'] = '*'; | |
$composer_plugins['better-wp-security'] = '*'; | |
ksort($composer_plugins); | |
// place the wordpress plugins into the correct format for composer.json | |
foreach($composer_plugins as $plugin_name=>$version){ | |
$array_key = $key; | |
$plugin_array['require'][$wp_packagist_plugin_namespace.$plugin_name] = $version; | |
} | |
$composer = fopen($composer_file, 'w'); | |
$save_file = fwrite($composer, prettyPrint(json_encode($plugin_array))); | |
fclose($composer); | |
if ($composer !== false && $save_file !== false) { | |
echo "Wrote plugins to composer.json to ".$composer_file.PHP_EOL; | |
} else { | |
echo "Unable to write the composer.json file to ".$composer_file.". Make sure this script has permission to create files in the folder and make sure the directory currently exists".$options['save'].PHP_EOL; | |
} | |
// check to see if the domains-map.json file already exists at the specified directory | |
if (array_key_exists('domain', $options) || array_key_exists('d', $options)) { | |
$export_domains = true; | |
$domain = $options['domain']; | |
if (empty($domain)) { | |
$domain = $options['d']; | |
} | |
if (empty($domain)) { | |
$domain = getcwd(); | |
} | |
$domain_map = array(); | |
$domain_map_save_path = $options['save'].'/domains-map.json'; | |
// attempt to load the domain-map.json file | |
if (file_exists($domain_map_save_path)) { | |
$handle = fopen($domain_map_save_path, 'r'); | |
$domain_map = fread($handle, filesize($domain_map_save_path)); | |
fclose($handle); | |
$domain_map = json_decode($domain_map); | |
} | |
if (parse_url($domain) !== false) { | |
$domain = parse_url($domain, PHP_URL_HOST); | |
} else { | |
$domain = getcwd(); | |
$parts = explode('.', $domain); | |
$domain = end($parts); | |
} | |
$domain_map[$domain] = $domain.'.zip'; | |
var_dump($domain_map); | |
die; | |
// write domain to domain-map.json | |
$domain_json = fopen($domain_map_save_path, 'w'); | |
$save_file = fwrite($domain_json, prettyPrint(json_encode($domain_map))); | |
fclose($domain_json); | |
if ($composer !== false && $save_file !== false) { | |
echo "Wrote domains to domain-map.json to ".$domain_map_save_path.PHP_EOL; | |
} else { | |
echo "Unable to write the domain-map.json file to ".$domain_map_save_path.". Make sure this script has permission to create files in the folder and make sure the directory currently exists".$options['save'].PHP_EOL; | |
} | |
} | |
function prettyPrint( $json ) | |
{ | |
$result = ''; | |
$level = 0; | |
$in_quotes = false; | |
$in_escape = false; | |
$ends_line_level = NULL; | |
$json_length = strlen( $json ); | |
for( $i = 0; $i < $json_length; $i++ ) { | |
$char = $json[$i]; | |
$new_line_level = NULL; | |
$post = ""; | |
if( $ends_line_level !== NULL ) { | |
$new_line_level = $ends_line_level; | |
$ends_line_level = NULL; | |
} | |
if ( $in_escape ) { | |
$in_escape = false; | |
} else if( $char === '"' ) { | |
$in_quotes = !$in_quotes; | |
} else if( ! $in_quotes ) { | |
switch( $char ) { | |
case '}': case ']': | |
$level--; | |
$ends_line_level = NULL; | |
$new_line_level = $level; | |
break; | |
case '{': case '[': | |
$level++; | |
case ',': | |
$ends_line_level = $level; | |
break; | |
case ':': | |
$post = " "; | |
break; | |
case " ": case "\t": case "\n": case "\r": | |
$char = ""; | |
$ends_line_level = $new_line_level; | |
$new_line_level = NULL; | |
break; | |
} | |
} else if ( $char === '\\' ) { | |
$in_escape = true; | |
} | |
if( $new_line_level !== NULL ) { | |
$result .= "\n".str_repeat( "\t", $new_line_level ); | |
} | |
$result .= $char.$post; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment