-
-
Save bdeleasa/dc7c7e746062f74a732ad66dde2954a9 to your computer and use it in GitHub Desktop.
Convert WP to Composer
This file contains 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
#!/usr/bin/php | |
<?php | |
/** | |
* USAGE: | |
* | |
* (optional) Download script: | |
* "wget https://gist.githubusercontent.com/pelmered/2ee27f1cb18b8c73a0205ded580e3195/raw/convert-to-composer.php" | |
* | |
* Basic usage: | |
* php convert-to-composer.php | |
* And then follow the instructions | |
* | |
* Options | |
* php convert-to-composer.php --path=public/wp --url=https://www.example.com --version | |
* | |
* path - specify path to WordPress core files. Same as WP CLI path | |
* url - specify url to site(needed for multisite) | |
* version - Use latest version in genereated composer.json(for example "^2.3.1"). Omitting this sets "*" as version. | |
* | |
**/ | |
$options = getopt('p::u::v::',['path::','url::','version::']); | |
$extra_params = ''; | |
if( !empty( $options['p'] ) ) { | |
$extra_params .= ' --path=' . $options['p']; | |
} | |
else if( !empty( $options['path'] ) ) { | |
$extra_params .= ' --path=' . $options['path']; | |
} | |
if( !empty( $options['u'] ) ) { | |
$extra_params .= ' --url=' . $options['u']; | |
} | |
else if( !empty( $options['url'] ) ) { | |
$extra_params .= ' --url=' . $options['url']; | |
} | |
$wp_content_path = str_replace(getcwd(), '', trim( @shell_exec('wp config get WP_CONTENT_DIR ' . $extra_params) ) ); | |
$plugins_path = str_replace(getcwd(), '', trim( @shell_exec('wp plugin path ' . $extra_params) ) ); | |
$use_version = !isset( $options['v'], $options['version'] ); | |
echo 'Getting plugins list... '; | |
$plugins_csv = @shell_exec('wp plugin list --fields=name,version,status --format=csv ' . $extra_params); | |
$lines = explode("\n", $plugins_csv); | |
$head = str_getcsv(array_shift($lines)); | |
$composer_plugins = []; | |
// remove empty items in array | |
$lines = array_filter($lines); | |
echo 'Done!'."\n\n"; | |
$gitignores = []; | |
echo 'Checking plugins against wordpress.org repo...'."\n\n"; | |
if( isset($lines) && is_array($lines ) ) | |
{ | |
foreach( $lines as $line ) | |
{ | |
$plugin = array_combine($head, str_getcsv($line)); | |
// Skip dropins and must-use | |
if( in_array( $plugin['status'], [ 'must-use', 'dropin' ] ) ) | |
{ | |
echo 'Plugin: '. $plugin['name'].' is '.$plugin['status'].'. Skipping.'."\n"; | |
$gitignores[] = $wp_content_path . '/' . ( $plugin['status'] == 'must-use' ? 'mu-plugins/' : '' ) . $plugin['name']; | |
continue; | |
} | |
$data = file_get_contents( 'https://api.wordpress.org/plugins/info/1.0/'.$plugin['name'] ); | |
$wp_object = unserialize($data); | |
if( isset($wp_object->slug) && $wp_object->slug == $plugin['name'] ) { | |
$wporg_plugins[] = $wp_object; | |
echo 'Plugin: '. $wp_object->name.' found.'."\n"; | |
} | |
else | |
{ | |
$gitignores[] = $plugins_path . '/' . $plugin['name']; | |
echo 'Plugin: '. $plugin['name'].' not found.'."\n"; | |
} | |
} | |
} | |
else | |
{ | |
die('No plugins found. Check output of : wp plugin list --fields=name,version,status --format=csv ' . $extra_params ); | |
} | |
echo "\n\n\n"; | |
echo "#################### \n"; | |
echo "# SCRIPT RESULTS # \n"; | |
echo "#################### \n"; | |
echo "\n\n"; | |
if( isset($wporg_plugins) && is_array($wporg_plugins ) ) | |
{ | |
echo 'PLUGINS FOUND ON WORDPRESS.ORG(paste this into the require section of your composers.json):'; | |
echo "\n\n"; | |
foreach( $wporg_plugins AS $plugin ) | |
{ | |
printf('"wpackagist-plugin/%s":"%s",', $plugin->slug, ( $use_version ? '^'.$plugin->version : '*' ) ); | |
echo "\n"; | |
} | |
} | |
else | |
{ | |
echo 'NO PLUGINS FOUND ON WORDPRESS.ORG :( '; | |
echo "\n\n"; | |
} | |
echo "\n\n\n"; | |
if( isset($gitignores) && is_array($gitignores ) ) | |
{ | |
echo "PLUGINS NOT FOUND ON WORDPRESS.ORG. You need to do one of the following: \n"; | |
echo " - Find an alternative source / composer repository, or create you own using Satis or similar. \n"; | |
echo " - Add the following to your .gitignore to add the plugins to your repo for local version control: \n\n"; | |
foreach( $gitignores AS $gitignore ) { | |
echo '!' . $gitignore . "\n"; | |
} | |
} | |
else | |
{ | |
echo 'ALL PLUGINS FOUND ON WORDPRESS.ORG :D '; | |
echo "\n"; | |
} | |
die(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment