From 85d8e04f49bf616c4a4136e379836b934f81d78c Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Tue, 4 Feb 2020 17:59:14 -0600 Subject: [PATCH] Added WP location guessing --- CHANGELOG.md | 3 ++- src/Installer.php | 48 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 227597b..2ace675 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog -## [Unreleased] +## [1.2] * Better handling of partial WP install +* Added guessing for no WP install ## [1.1] diff --git a/src/Installer.php b/src/Installer.php index f93f3b0..476295d 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -29,8 +29,12 @@ class Installer { */ public static function install( Event $event ) { if ( ! self::include_wp( dirname( __FILE__ ) ) ) { - echo "Couldn't include WP, mu-plugin autoloader installation aborted."; - exit( 1 ); + echo "Couldn't include WP... guessing location\n"; + + if ( ! self::wp_location_best_guess( $event->getComposer()->getPackage()->getExtra() ) ) { + echo "mu-plugin autoloader installation aborted\n"; + exit( 1 ); + } } $vendor_dir = $event->getComposer()->getConfig()->get( 'vendor-dir' ); @@ -80,6 +84,46 @@ private static function include_wp( $dir ) { return self::include_wp( $dir . '/..' ); } + /** + * Try to guess the WP install directory based on installer-paths in composer.json. + * + * @param array $extra Composer file extra section. + * @return boolean True if location guessed and path define()s set, false otherwise. + * @author Justin Foell + * @since 2020-02-04 + */ + private static function wp_location_best_guess( array $extra ) { + if ( empty( $extra['installer-paths'] ) ) { + return false; + } + + $composer_json_dir = getcwd(); + + foreach ( $extra['installer-paths'] as $path => $constraints ) { + $value = ( is_array( $constraints ) && count( $constraints ) === 1 ) ? current( $constraints ) : $constraints; + + if ( in_array( $value, array( 'type:wordpress-muplugin', 'type:wordpress-plugin' ), true ) ) { + $parts = explode( '/', $path ); + + foreach ( $parts as $index => $dir ) { + if ( in_array( $dir, array( 'wp-content', 'mu-plugins', 'plugins' ), true ) ) { + + $extra_path = 'wp-content' !== $dir ? '/..' : ''; + $partial = array_slice( $parts, 0, $index + 1 ); + $wp_content = realpath( $composer_json_dir . '/' . join( '/', $partial ) . $extra_path ); + + define( 'WP_CONTENT_DIR', $wp_content ); + define( 'ABSPATH', realpath( $wp_content . '/..' ) ); + + return true; + } + } + } + } + + return false; + } + /** * Try to do some WP constant substitutions in the autoload directory path. *