Skip to content

Commit

Permalink
Added WP location guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfoell committed Feb 4, 2020
1 parent d515db4 commit 85d8e04
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## [Unreleased]
## [1.2]

* Better handling of partial WP install
* Added guessing for no WP install

## [1.1]

Expand Down
48 changes: 46 additions & 2 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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 <justin.foell@webdevstudios.com>
* @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.
*
Expand Down

0 comments on commit 85d8e04

Please sign in to comment.