Skip to content

Commit a5127bb

Browse files
authored
Update WordPressCoreInstaller.php
1 parent 9573b5d commit a5127bb

File tree

1 file changed

+101
-98
lines changed

1 file changed

+101
-98
lines changed

src/WordPressCoreInstaller.php

Lines changed: 101 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
<?php
22

33
/**
4-
* WordPress Core Installer - A Composer to install WordPress in a webroot subdirectory
5-
* Copyright (C) 2013 John P. Bloch
4+
* WordPress Core Installer - A Composer installer to install WordPress in a webroot subdirectory
5+
* Copyright (C) 2013 John P. Bloch
6+
* Modified by Moox Developers, 2025
67
*
7-
* This program is free software; you can redistribute it and/or modify
8-
* it under the terms of the GNU General Public License as published by
9-
* the Free Software Foundation; either version 2 of the License, or
10-
* (at your option) any later version.
11-
*
12-
* This program is distributed in the hope that it will be useful,
13-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
* GNU General Public License for more details.
16-
*
17-
* You should have received a copy of the GNU General Public License along
18-
* with this program; if not, write to the Free Software Foundation, Inc.,
19-
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
8+
* Licensed under the GPLv2 or later.
209
*/
2110

2211
namespace Moox\Composer;
@@ -25,88 +14,102 @@
2514
use Composer\Installer\LibraryInstaller;
2615
use Composer\Package\PackageInterface;
2716

28-
class WordPressCoreInstaller extends LibraryInstaller {
29-
30-
const TYPE = 'wordpress-core';
31-
32-
const MESSAGE_CONFLICT = 'Two packages (%s and %s) cannot share the same directory!';
33-
const MESSAGE_SENSITIVE = 'Warning! %s is an invalid WordPress install directory (from %s)!';
34-
35-
private static $_installedPaths = array();
36-
37-
private $sensitiveDirectories = array( '.' );
38-
39-
/**
40-
* {@inheritDoc}
41-
*/
42-
public function getInstallPath( PackageInterface $package ) {
43-
$installationDir = false;
44-
$prettyName = $package->getPrettyName();
45-
if ( $this->composer->getPackage() ) {
46-
$topExtra = $this->composer->getPackage()->getExtra();
47-
if ( ! empty( $topExtra['wordpress-install-dir'] ) ) {
48-
$installationDir = $topExtra['wordpress-install-dir'];
49-
if ( is_array( $installationDir ) ) {
50-
$installationDir = empty( $installationDir[ $prettyName ] ) ? false : $installationDir[ $prettyName ];
51-
}
52-
}
53-
}
54-
$extra = $package->getExtra();
55-
if ( ! $installationDir && ! empty( $extra['wordpress-install-dir'] ) ) {
56-
$installationDir = $extra['wordpress-install-dir'];
57-
}
58-
if ( ! $installationDir ) {
59-
$installationDir = 'wordpress';
60-
}
61-
$vendorDir = $this->composer->getConfig()->get( 'vendor-dir', Config::RELATIVE_PATHS ) ?: 'vendor';
62-
if (
63-
in_array( $installationDir, $this->sensitiveDirectories ) ||
64-
( $installationDir === $vendorDir )
65-
) {
66-
throw new \InvalidArgumentException( $this->getSensitiveDirectoryMessage( $installationDir, $prettyName ) );
67-
}
68-
if (
69-
! empty( self::$_installedPaths[ $installationDir ] ) &&
70-
$prettyName !== self::$_installedPaths[ $installationDir ] &&
71-
$package->getType() !== self::TYPE
72-
) {
73-
$conflict_message = $this->getConflictMessage( $prettyName, self::$_installedPaths[ $installationDir ] );
74-
throw new \InvalidArgumentException( $conflict_message );
75-
}
76-
self::$_installedPaths[ $installationDir ] = $prettyName;
77-
78-
return $installationDir;
79-
}
80-
81-
/**
82-
* {@inheritDoc}
83-
*/
84-
public function supports( $packageType ) {
85-
return self::TYPE === $packageType;
86-
}
87-
88-
/**
89-
* Get the exception message with conflicting packages
90-
*
91-
* @param string $attempted
92-
* @param string $alreadyExists
93-
*
94-
* @return string
95-
*/
96-
private function getConflictMessage( $attempted, $alreadyExists ) {
97-
return sprintf( self::MESSAGE_CONFLICT, $attempted, $alreadyExists );
98-
}
99-
100-
/**
101-
* Get the exception message for attempted sensitive directories
102-
*
103-
* @param string $attempted
104-
* @param string $packageName
105-
*
106-
* @return string
107-
*/
108-
private function getSensitiveDirectoryMessage( $attempted, $packageName ) {
109-
return sprintf( self::MESSAGE_SENSITIVE, $attempted, $packageName );
110-
}
17+
class WordPressCoreInstaller extends LibraryInstaller
18+
{
19+
const TYPE = 'wordpress-core';
20+
21+
const MESSAGE_CONFLICT = 'Two packages (%s and %s) cannot share the same directory!';
22+
const MESSAGE_SENSITIVE = 'Warning! %s is an invalid WordPress install directory (from %s)!';
23+
24+
private static $_installedPaths = [];
25+
26+
private $sensitiveDirectories = ['.'];
27+
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
public function getInstallPath(PackageInterface $package)
32+
{
33+
$installationDir = false;
34+
$prettyName = $package->getPrettyName();
35+
36+
// 1️⃣ Read from root package (top-level composer.json)
37+
if ($this->composer->getPackage()) {
38+
$topExtra = $this->composer->getPackage()->getExtra();
39+
if (!empty($topExtra['wordpress-install-dir'])) {
40+
$installationDir = $topExtra['wordpress-install-dir'];
41+
if (is_array($installationDir)) {
42+
$installationDir = empty($installationDir[$prettyName]) ? false : $installationDir[$prettyName];
43+
}
44+
}
45+
}
46+
47+
// 2️⃣ Read from dependent packages (like moox/press)
48+
if (!$installationDir) {
49+
foreach ($this->composer->getRepositoryManager()->getLocalRepository()->getPackages() as $pkg) {
50+
$pkgExtra = $pkg->getExtra();
51+
if (!empty($pkgExtra['wordpress-install-dir'])) {
52+
$installationDir = $pkgExtra['wordpress-install-dir'];
53+
break;
54+
}
55+
}
56+
}
57+
58+
// 3️⃣ Read from the WordPress package itself (rare case)
59+
$extra = $package->getExtra();
60+
if (!$installationDir && !empty($extra['wordpress-install-dir'])) {
61+
$installationDir = $extra['wordpress-install-dir'];
62+
}
63+
64+
// 4️⃣ Fallback default
65+
if (!$installationDir) {
66+
$installationDir = 'public/wp';
67+
}
68+
69+
// 5️⃣ Safety checks
70+
$vendorDir = $this->composer->getConfig()->get('vendor-dir', Config::RELATIVE_PATHS) ?: 'vendor';
71+
if (
72+
in_array($installationDir, $this->sensitiveDirectories) ||
73+
($installationDir === $vendorDir)
74+
) {
75+
throw new \InvalidArgumentException($this->getSensitiveDirectoryMessage($installationDir, $prettyName));
76+
}
77+
78+
if (
79+
!empty(self::$_installedPaths[$installationDir]) &&
80+
$prettyName !== self::$_installedPaths[$installationDir] &&
81+
$package->getType() !== self::TYPE
82+
) {
83+
$conflict_message = $this->getConflictMessage($prettyName, self::$_installedPaths[$installationDir]);
84+
throw new \InvalidArgumentException($conflict_message);
85+
}
86+
87+
self::$_installedPaths[$installationDir] = $prettyName;
88+
89+
return $installationDir;
90+
}
91+
92+
/**
93+
* {@inheritDoc}
94+
*/
95+
public function supports($packageType)
96+
{
97+
return self::TYPE === $packageType;
98+
}
99+
100+
/**
101+
* Get the exception message with conflicting packages.
102+
*/
103+
private function getConflictMessage($attempted, $alreadyExists)
104+
{
105+
return sprintf(self::MESSAGE_CONFLICT, $attempted, $alreadyExists);
106+
}
111107

108+
/**
109+
* Get the exception message for attempted sensitive directories.
110+
*/
111+
private function getSensitiveDirectoryMessage($attempted, $packageName)
112+
{
113+
return sprintf(self::MESSAGE_SENSITIVE, $attempted, $packageName);
114+
}
112115
}

0 commit comments

Comments
 (0)