| 1 |
<?php |
| 2 |
namespace Kibo\PhastPlugins\PhastPress; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use Requests; |
| 6 |
use RuntimeException; |
| 7 |
|
| 8 |
class WordPress { |
| 9 |
public static function loadConfig() { |
| 10 |
if (defined('ABSPATH')) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
$complete = false; |
| 15 |
|
| 16 |
// If set_error_handler is used in wp-config.php this will be a fallback |
| 17 |
$GLOBALS['wp_filter']['all'][0][] = [ |
| 18 |
'function' => function () use (&$complete) { |
| 19 |
if (!$complete && defined('WP_CONTENT_DIR')) { |
| 20 |
throw new WordPressLoadedException(); |
| 21 |
} |
| 22 |
}, |
| 23 |
'accepted_args' => 0, |
| 24 |
]; |
| 25 |
|
| 26 |
// This should trigger as soon as wp-settings.php is loaded |
| 27 |
set_error_handler(function ($errno, $errstr, $errfile) use ($complete) { |
| 28 |
if (!$complete && basename($errfile) === 'wp-settings.php') { |
| 29 |
throw new WordPressLoadedException(); |
| 30 |
} |
| 31 |
}); |
| 32 |
|
| 33 |
// Cause an error to be raised when wp-settings.php is loaded |
| 34 |
define('WPINC', 'wp-includes'); |
| 35 |
|
| 36 |
try { |
| 37 |
$wpLoadScript = self::findWPLoad(); |
| 38 |
chdir(dirname($wpLoadScript)); |
| 39 |
require $wpLoadScript; |
| 40 |
throw new RuntimeException('WordPress loaded without triggering any hooks'); |
| 41 |
} catch (WordPressLoadedException $e) { |
| 42 |
} finally { |
| 43 |
$complete = true; |
| 44 |
set_error_handler(null); |
| 45 |
} |
| 46 |
|
| 47 |
if (!defined('WP_CONTENT_DIR')) { |
| 48 |
define('WP_CONTENT_DIR', ABSPATH . 'wp-content'); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
private static function findWPLoad() { |
| 53 |
$startDir = dirname($_SERVER['SCRIPT_FILENAME']); |
| 54 |
if (!$startDir) { |
| 55 |
throw new RuntimeException('Could not get directory from SCRIPT_FILENAME'); |
| 56 |
} |
| 57 |
$dir = $startDir; |
| 58 |
while (true) { |
| 59 |
$path = $dir . '/wp-load.php'; |
| 60 |
if (file_exists($path)) { |
| 61 |
return $path; |
| 62 |
} |
| 63 |
$wpPath = $dir . '/wp/wp-load.php'; |
| 64 |
if (file_exists($wpPath)) { |
| 65 |
return $wpPath; |
| 66 |
} |
| 67 |
$parent = dirname($dir); |
| 68 |
if ($parent == $dir) { |
| 69 |
break; |
| 70 |
} |
| 71 |
$dir = $parent; |
| 72 |
} |
| 73 |
throw new RuntimeException( |
| 74 |
"Could not find wp-load.php in $startDir or any of its parent directories" |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
public static function loadRequests() { |
| 79 |
if (class_exists(\WpOrg\Requests\Requests::class)) { |
| 80 |
return; |
| 81 |
} |
| 82 |
require ABSPATH . WPINC . '/Requests/src/Autoload.php'; |
| 83 |
\WpOrg\Requests\Autoload::register(); |
| 84 |
\WpOrg\Requests\Requests::set_certificate_path(ABSPATH . WPINC . '/certificates/ca-bundle.crt'); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
class WordPressLoadedException extends Exception { |
| 89 |
} |
| 90 |
|