wpsy/WordpressBundle.php line 13

Open in your IDE?
  1. <?php
  2. namespace Metabolism\WordpressBundle;
  3. use App\Controller\WordpressPassthroughController;
  4. use App\Helpers\WordpressPassthroughHelper;
  5. use Env\Env;
  6. use Metabolism\WordpressBundle\Entity\Blog;
  7. use Symfony\Component\HttpKernel\Bundle\Bundle;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use function Env\env;
  10. class WordpressBundle extends Bundle
  11. {
  12.     private $root_dir;
  13.     private $public_dir;
  14.     private $log_dir;
  15.     private $wp_path "public/edition/";
  16.     public function getPath(): string
  17.     {
  18.         return 'wpsy';
  19.     }
  20.     public function boot()
  21.     {
  22.         Env::$options Env::USE_ENV_ARRAY;
  23.         $this->log_dir $this->container->get('kernel')->getLogDir();
  24.         $this->root_dir $this->container->get('kernel')->getProjectDir();
  25.         $this->public_dir $this->root_dir . (is_dir($this->root_dir '/public') ? '/public' '/web');
  26.         $this->resolveServer();
  27.         $this->loadWordpress();
  28.         if ($this->container->has('twig')) {
  29.             $twig $this->container->get('twig');
  30.             $blog Blog::getInstance();
  31.             $twig->addGlobal('blog'$blog);
  32.             // retro-compatibility
  33.             $blog->setGlobals($twig);
  34.         }
  35.         $this->container->get('event_dispatcher')->addListener(
  36.             'kernel.controller',
  37.             function (ControllerEvent $event) {
  38.                 /** @var \WP $wp */
  39.                 global $wp;
  40.                 $controller $event->getController();
  41.                 if (is_array($controller) && $controller[0] instanceof WordpressPassthroughController) {
  42.                     ob_start();
  43.                     add_filter('wp_using_themes', function () {
  44.                         return true;
  45.                     });
  46.                     $wp->handle_404();
  47.                     require_once ABSPATH WPINC '/template-loader.php';
  48.                     $buffer ob_get_clean();
  49.                     $this->container->get(WordpressPassthroughHelper::class)->setWordpressInitializationOutput($buffer);
  50.                 } else {
  51.                     do_action('template_redirect');
  52.                 }
  53.             }
  54.         );
  55.     }
  56.     private function resolveServer()
  57.     {
  58.         if (!isset($_SERVER['REQUEST_METHOD']))
  59.             $_SERVER['REQUEST_METHOD'] = 'GET';
  60.         if (!isset($_SERVER['HTTP_HOST'])) {
  61.             if ($host env('WP_MULTISITE')) {
  62.                 $_SERVER['HTTP_HOST'] = $host;
  63.             } elseif ($host = ($_SERVER['SERVER_NAME'] ?? false)) {
  64.                 $_SERVER['HTTP_HOST'] = $host;
  65.             } else {
  66.                 $_SERVER['HTTP_HOST'] = '127.0.0.1:8000';
  67.                 $_SERVER['SERVER_PORT'] = '8000';
  68.             }
  69.         }
  70.     }
  71.     public static function loadPlugins()
  72.     {
  73.         $plugins scandir(__DIR__ '/Plugin');
  74.         foreach ($plugins as $plugin) {
  75.             if (!in_array($plugin, ['.''..'])) {
  76.                 $classname '\Metabolism\WordpressBundle\Plugin\\' str_replace('.php'''$plugin);
  77.                 if (class_exists($classname))
  78.                     new $classname();
  79.             }
  80.         }
  81.     }
  82.     public static function isLoginUrl()
  83.     {
  84.         $uri explode('/'$_SERVER['SCRIPT_NAME']);
  85.         $page end($uri);
  86.         return in_array($page, ['wp-login.php''wp-signup.php']);
  87.     }
  88.     /**
  89.      * @see wp-includes/class-wp.php, main function
  90.      */
  91.     private function loadWordpress()
  92.     {
  93.         if (!file_exists($this->public_dir '/wp-config.php'))
  94.             return;
  95.         global $request;
  96.         if (is_object($request) && get_class($request) == 'Symfony\Component\HttpFoundation\Request')
  97.             $httpRequest $request;
  98.         if (!defined('WP_DEBUG_LOG'))
  99.             define('WP_DEBUG_LOG'realpath($this->log_dir '/wp-errors.log'));
  100.         $composer $this->root_dir '/composer.json';
  101.         // get Wordpress path
  102.         if (!is_dir($this->root_dir '/' $this->wp_path) && file_exists($composer)) {
  103.             $composer json_decode(file_get_contents($composer), true);
  104.             $installer_paths $composer['extra']['installer-paths'] ?? [];
  105.             foreach ($installer_paths as $installer_path => $types) {
  106.                 if (in_array("type:wordpress-core"$types))
  107.                     $this->wp_path $installer_path;
  108.             }
  109.         }
  110.         // start loading Wordpress core without theme support
  111.         $wp_load_script $this->root_dir '/' $this->wp_path 'wp-load.php';
  112.         if (!file_exists($wp_load_script))
  113.             return;
  114.         include $wp_load_script;
  115.         global /** @var \WP $wp */
  116.         $wp;
  117.         $wp->init();
  118.         $wp->parse_request();
  119.         $wp->query_posts();
  120.         $wp->register_globals();
  121.         // Detect internal routes to force \WP_Query to *not* treat them as pages. This fixes issue with Multisite and Woocommerce Cache handling.
  122.         $wcCompatBlacklist = ['_wdt''_profiler''__wpsy'];
  123.         if (array_key_exists('REQUEST_URI'$_SERVER) && preg_match('/^\/(' implode('|'$wcCompatBlacklist) . ')/'$_SERVER['REQUEST_URI'])) {
  124.             /** @var \WP_Query $wp_query */
  125.             global $wp_query;
  126.             $wp_query->is_page false;
  127.         }
  128.         do_action_ref_array('wp', array(&$wp));
  129.         remove_action('template_redirect''redirect_canonical');
  130.         if (isset($httpRequest))
  131.             $request $httpRequest;
  132.     }
  133. }