/
Wordpress.php
137 lines (115 loc) · 3.02 KB
/
Wordpress.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
<?php
/*
* This file is part of the Ekino Wordpress package.
*
* (c) 2013 Ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ekino\WordpressBundle\Wordpress;
use Symfony\Component\HttpKernel\KernelInterface;
use Ekino\WordpressBundle\Wordpress\WordpressResponse;
/**
* Class Wordpress
*
* This is the main application class that initializes Wordpress application
* to retrieve the content and returns a Wordpress response
*
* @author Vincent Composieux <composieux@ekino.com>
*/
class Wordpress
{
/**
* @var KernelInterface
*/
protected $kernel;
/**
* @var WordpressResponse
*/
protected $response;
/**
* @var string
*/
protected $directory;
/**
* Constructor
*
* @param KernelInterface $kernel Symfony kernel instance
* @param string $directory A wordpress directory (if specified in configuration)
*/
public function __construct(KernelInterface $kernel, $directory = null)
{
$this->kernel = $kernel;
$this->directory = $directory;
}
/**
* Initializes Wordpress
*
* @return $this
*/
public function initialize()
{
$content = $this->getContent();
$this->response = new WordpressResponse($content);
return $this;
}
/**
* Returns Wordpress content
*
* @return string
*
* @throws \InvalidArgumentException if Wordpress loader cannot be found
*/
public function getContent()
{
ob_start();
define('WP_USE_THEMES', true);
global $wp, $wp_the_query, $wpdb, $wp_query, $allowedentitynames;
$loader = $this->getWordpressDirectory() . 'wp-blog-header.php';
if (!file_exists($loader)) {
throw new \InvalidArgumentException(
sprintf('Unable to find Wordpress loader in: "%s".', $loader)
);
}
require_once $loader;
return ob_get_clean();
}
/**
* Returns Wordpress content response
*
* @return WordpressResponse
*/
public function getResponse()
{
return $this->response;
}
/**
* @return null|\WP_Query
*/
public function getWpQuery()
{
global $wp_query;
if (null === $wp_query || !$wp_query instanceof \WP_Query) {
return null;
}
return $wp_query;
}
/**
* Returns Wordpress directory if specified in configuration
* otherwise returns default structure:
*
* wordpress
* |- symfony <-- Symfony application must be installed on Wordpress root directory by default
* |- index.php
* |- wp-content
* |- ...
*
* @return string
*/
protected function getWordpressDirectory()
{
$directory = $this->directory ?: sprintf('%s/../../', $this->kernel->getRootDir());
return '/' == substr($directory, -1) ? $directory : $directory . '/';
}
}