<?php
namespace Metabolism\WordpressBundle;
use App\Controller\WordpressPassthroughController;
use App\Helpers\WordpressPassthroughHelper;
use Env\Env;
use Metabolism\WordpressBundle\Entity\Blog;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use function Env\env;
class WordpressBundle extends Bundle
{
private $root_dir;
private $public_dir;
private $log_dir;
private $wp_path = "public/edition/";
public function getPath(): string
{
return 'wpsy';
}
public function boot()
{
Env::$options = Env::USE_ENV_ARRAY;
$this->log_dir = $this->container->get('kernel')->getLogDir();
$this->root_dir = $this->container->get('kernel')->getProjectDir();
$this->public_dir = $this->root_dir . (is_dir($this->root_dir . '/public') ? '/public' : '/web');
$this->resolveServer();
$this->loadWordpress();
if ($this->container->has('twig')) {
$twig = $this->container->get('twig');
$blog = Blog::getInstance();
$twig->addGlobal('blog', $blog);
// retro-compatibility
$blog->setGlobals($twig);
}
$this->container->get('event_dispatcher')->addListener(
'kernel.controller',
function (ControllerEvent $event) {
/** @var \WP $wp */
global $wp;
$controller = $event->getController();
if (is_array($controller) && $controller[0] instanceof WordpressPassthroughController) {
ob_start();
add_filter('wp_using_themes', function () {
return true;
});
$wp->handle_404();
require_once ABSPATH . WPINC . '/template-loader.php';
$buffer = ob_get_clean();
$this->container->get(WordpressPassthroughHelper::class)->setWordpressInitializationOutput($buffer);
} else {
do_action('template_redirect');
}
}
);
}
private function resolveServer()
{
if (!isset($_SERVER['REQUEST_METHOD']))
$_SERVER['REQUEST_METHOD'] = 'GET';
if (!isset($_SERVER['HTTP_HOST'])) {
if ($host = env('WP_MULTISITE')) {
$_SERVER['HTTP_HOST'] = $host;
} elseif ($host = ($_SERVER['SERVER_NAME'] ?? false)) {
$_SERVER['HTTP_HOST'] = $host;
} else {
$_SERVER['HTTP_HOST'] = '127.0.0.1:8000';
$_SERVER['SERVER_PORT'] = '8000';
}
}
}
public static function loadPlugins()
{
$plugins = scandir(__DIR__ . '/Plugin');
foreach ($plugins as $plugin) {
if (!in_array($plugin, ['.', '..'])) {
$classname = '\Metabolism\WordpressBundle\Plugin\\' . str_replace('.php', '', $plugin);
if (class_exists($classname))
new $classname();
}
}
}
public static function isLoginUrl()
{
$uri = explode('/', $_SERVER['SCRIPT_NAME']);
$page = end($uri);
return in_array($page, ['wp-login.php', 'wp-signup.php']);
}
/**
* @see wp-includes/class-wp.php, main function
*/
private function loadWordpress()
{
if (!file_exists($this->public_dir . '/wp-config.php'))
return;
global $request;
if (is_object($request) && get_class($request) == 'Symfony\Component\HttpFoundation\Request')
$httpRequest = $request;
if (!defined('WP_DEBUG_LOG'))
define('WP_DEBUG_LOG', realpath($this->log_dir . '/wp-errors.log'));
$composer = $this->root_dir . '/composer.json';
// get Wordpress path
if (!is_dir($this->root_dir . '/' . $this->wp_path) && file_exists($composer)) {
$composer = json_decode(file_get_contents($composer), true);
$installer_paths = $composer['extra']['installer-paths'] ?? [];
foreach ($installer_paths as $installer_path => $types) {
if (in_array("type:wordpress-core", $types))
$this->wp_path = $installer_path;
}
}
// start loading Wordpress core without theme support
$wp_load_script = $this->root_dir . '/' . $this->wp_path . 'wp-load.php';
if (!file_exists($wp_load_script))
return;
include $wp_load_script;
global /** @var \WP $wp */
$wp;
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
// Detect internal routes to force \WP_Query to *not* treat them as pages. This fixes issue with Multisite and Woocommerce Cache handling.
$wcCompatBlacklist = ['_wdt', '_profiler', '__wpsy'];
if (array_key_exists('REQUEST_URI', $_SERVER) && preg_match('/^\/(' . implode('|', $wcCompatBlacklist) . ')/', $_SERVER['REQUEST_URI'])) {
/** @var \WP_Query $wp_query */
global $wp_query;
$wp_query->is_page = false;
}
do_action_ref_array('wp', array(&$wp));
remove_action('template_redirect', 'redirect_canonical');
if (isset($httpRequest))
$request = $httpRequest;
}
}