| 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 |
$complete = false; |
| 14 |
$hook = function () use (&$complete) { |
| 15 |
if (!$complete && defined('WP_CONTENT_DIR')) { |
| 16 |
throw new WordPressLoadedException(); |
| 17 |
} |
| 18 |
}; |
| 19 |
$GLOBALS['wp_filter']['all'][0][] = [ |
| 20 |
'function' => $hook, |
| 21 |
'accepted_args' => 0, |
| 22 |
]; |
| 23 |
try { |
| 24 |
require self::findWPLoad(); |
| 25 |
} catch (WordPressLoadedException $e) { |
| 26 |
return; |
| 27 |
} finally { |
| 28 |
$complete = true; |
| 29 |
} |
| 30 |
throw new RuntimeException('WordPress loaded without triggering any hooks'); |
| 31 |
} |
| 32 |
|
| 33 |
private static function findWPLoad() { |
| 34 |
$startDir = dirname($_SERVER['SCRIPT_FILENAME']); |
| 35 |
if (!$startDir) { |
| 36 |
throw new RuntimeException('Could not get directory from SCRIPT_FILENAME'); |
| 37 |
} |
| 38 |
$dir = $startDir; |
| 39 |
while (true) { |
| 40 |
$path = $dir . '/wp-load.php'; |
| 41 |
if (file_exists($path)) { |
| 42 |
return $path; |
| 43 |
} |
| 44 |
$parent = dirname($dir); |
| 45 |
if ($parent == $dir) { |
| 46 |
break; |
| 47 |
} |
| 48 |
$dir = $parent; |
| 49 |
} |
| 50 |
throw new RuntimeException( |
| 51 |
"Could not find wp-load.php in $startDir or any of its parent directories" |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
public static function loadRequests() { |
| 56 |
if (!class_exists(Requests::class)) { |
| 57 |
require ABSPATH . WPINC . '/class-requests.php'; |
| 58 |
Requests::register_autoloader(); |
| 59 |
Requests::set_certificate_path(ABSPATH . WPINC . '/certificates/ca-bundle.crt'); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
class WordPressLoadedException extends Exception { |
| 65 |
} |
| 66 |
|