-
Notifications
You must be signed in to change notification settings - Fork 16
/
WordpressBundle.php
138 lines (95 loc) · 3.68 KB
/
WordpressBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Metabolism\WordpressBundle;
use Env\Env;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use function Env\env;
class WordpressBundle extends Bundle
{
private $root_dir;
private $public_dir;
private $log_dir;
private $router;
private $wp_path = "public/edition/";
public function getPath(): string
{
return \dirname(__DIR__);
}
public function boot()
{
Env::$options = Env::USE_ENV_ARRAY;
$kernel = $this->container->get('kernel');
$this->log_dir = $kernel->getLogDir();
$this->root_dir = $kernel->getProjectDir();
$this->router = $this->container->get('router');
$this->public_dir = $this->root_dir.(is_dir($this->root_dir.'/public') ? '/public' : '/web');
$this->resolveServer();
$this->loadWordpress();
}
private function resolveServer(){
$context = $this->router->getContext();
if( !isset($_SERVER['HTTP_HOST']) ) {
$multisite = env('WP_MULTISITE');
if( $multisite && php_sapi_name() == 'cli' ){
$url = parse_url($multisite);
$_SERVER['SERVER_PORT'] = $url['port']??80;
$_SERVER['REQUEST_SCHEME'] = $url['scheme']??'https';
if( $_SERVER['REQUEST_SCHEME'] == 'https' )
$_SERVER['HTTP_HOST'] = $url['host']??'127.0.0.1'.($_SERVER['SERVER_PORT']!=443?':'.$_SERVER['SERVER_PORT']:'');
else
$_SERVER['HTTP_HOST'] = $url['host']??'127.0.0.1'.($_SERVER['SERVER_PORT']!=80?':'.$_SERVER['SERVER_PORT']:'');
}
else{
$_SERVER['SERVER_PORT'] = $context->isSecure() ? $context->getHttpsPort() : $context->getHttpPort();
$_SERVER['REQUEST_SCHEME'] = $context->isSecure() ? 'https' : 'http';
if( $context->isSecure() )
$_SERVER['HTTP_HOST'] = $context->getHost().($context->getHttpsPort()!=443?':'.$context->getHttpsPort():'');
else
$_SERVER['HTTP_HOST'] = $context->getHost().($context->getHttpPort()!=80?':'.$context->getHttpPort():'');
}
$_SERVER['HTTPS'] = $_SERVER['REQUEST_SCHEME'] == 'https' ? 'on' : 'off';
}
if( !isset($_SERVER['REMOTE_ADDR']) && php_sapi_name() == "cli" )
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
}
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;
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;
remove_action( 'template_redirect', 'redirect_canonical' );
}
}