forked from themosis/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
themosis.php
346 lines (306 loc) · 11.4 KB
/
themosis.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/*
Plugin Name: Themosis framework
Plugin URI: https://framework.themosis.com/
Description: A WordPress framework.
Version: 1.3.2
Author: Julien Lambé
Author URI: http://www.themosis.com/
License: GPLv2
*/
/*----------------------------------------------------*/
// The directory separator.
/*----------------------------------------------------*/
defined('DS') ? DS : define('DS', DIRECTORY_SEPARATOR);
/*----------------------------------------------------*/
// Themosis framework textdomain.
//
// This constant is only used by the core plugin.
// Developers should not try to use it into their
// own projects.
/*----------------------------------------------------*/
defined('THEMOSIS_FRAMEWORK_TEXTDOMAIN') ? THEMOSIS_FRAMEWORK_TEXTDOMAIN : define('THEMOSIS_FRAMEWORK_TEXTDOMAIN', 'themosis-framework');
/*----------------------------------------------------*/
// Storage path.
/*----------------------------------------------------*/
defined('THEMOSIS_STORAGE') ? THEMOSIS_STORAGE : define('THEMOSIS_STORAGE', WP_CONTENT_DIR.DS.'storage');
if (!function_exists('themosis_set_paths')) {
/**
* Register paths globally.
*
* @param array $paths Paths to register using alias => path pairs.
*/
function themosis_set_paths(array $paths)
{
foreach ($paths as $name => $path) {
if (!isset($GLOBALS['themosis.paths'][$name])) {
$GLOBALS['themosis.paths'][$name] = realpath($path).DS;
}
}
}
}
if (!function_exists('themosis_path')) {
/**
* Helper function to retrieve a previously registered path.
*
* @param string $name The path name/alias. If none is provided, returns all registered paths.
*
* @return string|array
*/
function themosis_path($name = '')
{
if (!empty($name)) {
return $GLOBALS['themosis.paths'][$name];
}
return $GLOBALS['themosis.paths'];
}
}
/*
* Main class that bootstraps the framework.
*/
if (!class_exists('Themosis')) {
class Themosis
{
/**
* Themosis instance.
*
* @var \Themosis
*/
protected static $instance = null;
/**
* Framework version.
*
* @var float
*/
const VERSION = '1.3.2';
/**
* The service container.
*
* @var \Themosis\Foundation\Application
*/
public $container;
private function __construct()
{
$this->autoload();
$this->bootstrap();
}
/**
* Retrieve Themosis class instance.
*
* @return \Themosis
*/
public static function instance()
{
if (is_null(static::$instance)) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Check for the composer autoload file.
*/
protected function autoload()
{
// Check if there is a autoload.php file.
// Meaning we're in development mode or
// the plugin has been installed on a "classic" WordPress configuration.
if (file_exists($autoload = __DIR__.DS.'vendor'.DS.'autoload.php')) {
require $autoload;
// Developers using the framework in a "classic" WordPress
// installation can activate this by defining
// a THEMOSIS_ERROR constant and set its value to true or false
// depending of their environment.
if (defined('THEMOSIS_ERROR') && THEMOSIS_ERROR) {
$whoops = new \Whoops\Run();
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
$whoops->register();
}
}
}
/**
* Bootstrap the core plugin.
*/
protected function bootstrap()
{
/*
* Define core framework paths.
* These are real paths, not URLs to the framework files.
*/
$paths['core'] = __DIR__.DS;
$paths['sys'] = __DIR__.DS.'src'.DS.'Themosis'.DS;
$paths['storage'] = THEMOSIS_STORAGE;
themosis_set_paths($paths);
/*
* Instantiate the service container for the project.
*/
$this->container = new \Themosis\Foundation\Application();
/*
* Create a new Request instance and register it.
* By providing an instance, the instance is shared.
*/
$request = \Themosis\Foundation\Request::capture();
$this->container->instance('request', $request);
/*
* Setup the facade.
*/
\Themosis\Facades\Facade::setFacadeApplication($this->container);
/*
* Register into the container, the registered paths.
* Normally at this stage, plugins should have
* their paths registered into the $GLOBALS array.
*/
$this->container->registerAllPaths(themosis_path());
/*
* Register core service providers.
*/
$this->registerProviders();
/*
* Setup core.
*/
$this->setup();
/*
* Project hooks.
* Added in their called order.
*/
add_action('admin_enqueue_scripts', [$this, 'adminEnqueueScripts']);
add_action('admin_head', [$this, 'adminHead']);
add_action('template_redirect', 'redirect_canonical');
add_action('template_redirect', 'wp_redirect_admin_locations');
add_action('template_redirect', [$this, 'setRouter'], 20);
}
/**
* Register core framework service providers.
*/
protected function registerProviders()
{
/*
* Service providers.
*/
$providers = apply_filters('themosis_service_providers', [
Themosis\Ajax\AjaxServiceProvider::class,
Themosis\Asset\AssetServiceProvider::class,
Themosis\Config\ConfigServiceProvider::class,
Themosis\Database\DatabaseServiceProvider::class,
Themosis\Field\FieldServiceProvider::class,
Themosis\Finder\FinderServiceProvider::class,
Themosis\Hook\HookServiceProvider::class,
Themosis\Html\FormServiceProvider::class,
Themosis\Html\HtmlServiceProvider::class,
Themosis\Load\LoaderServiceProvider::class,
Themosis\Metabox\MetaboxServiceProvider::class,
Themosis\Page\PageServiceProvider::class,
Themosis\Page\Sections\SectionServiceProvider::class,
Themosis\PostType\PostTypeServiceProvider::class,
Themosis\Route\RouteServiceProvider::class,
Themosis\Taxonomy\TaxonomyServiceProvider::class,
Themosis\User\UserServiceProvider::class,
Themosis\Validation\ValidationServiceProvider::class,
Themosis\View\ViewServiceProvider::class,
]);
foreach ($providers as $provider) {
$this->container->register($provider);
}
}
/**
* Setup core framework parameters.
* At this moment, all activated plugins have been loaded.
* Each plugin has its service providers registered.
*/
protected function setup()
{
/*
* Add view paths.
*/
$viewFinder = $this->container['view.finder'];
$viewFinder->addLocation(themosis_path('sys').'Metabox'.DS.'Views');
$viewFinder->addLocation(themosis_path('sys').'Page'.DS.'Views');
$viewFinder->addLocation(themosis_path('sys').'PostType'.DS.'Views');
$viewFinder->addLocation(themosis_path('sys').'Field'.DS.'Fields'.DS.'Views');
$viewFinder->addLocation(themosis_path('sys').'Taxonomy'.DS.'Views');
$viewFinder->addLocation(themosis_path('sys').'User'.DS.'Views');
/*
* Add paths to asset finder.
*/
$url = plugins_url('src/Themosis/_assets', __FILE__);
$assetFinder = $this->container['asset.finder'];
$assetFinder->addPaths([$url => themosis_path('sys').'_assets']);
/*
* Add framework core assets URL to the global
* admin JS object.
*/
add_filter('themosisAdminGlobalObject', function ($data) use ($url) {
$data['_themosisAssets'] = $url;
return $data;
});
/*
* Register framework media image size.
*/
$images = new Themosis\Config\Images([
'_themosis_media' => [100, 100, true, __('Mini', THEMOSIS_FRAMEWORK_TEXTDOMAIN)],
], $this->container['filter']);
$images->make();
/*
* Register framework assets.
*/
$this->container['asset']->add('themosis-core-styles', 'css/_themosisCore.css', ['wp-color-picker'])->to('admin');
$this->container['asset']->add('themosis-core-scripts', 'js/_themosisCore.js', ['jquery', 'jquery-ui-sortable', 'underscore', 'backbone', 'mce-view', 'wp-color-picker'], '1.3.0', true)->to('admin');
}
/**
* Hook into front-end routing.
* Setup the router API to be executed before
* theme default templates.
*/
public function setRouter()
{
if (is_feed() || is_comment_feed()) {
return;
}
try {
$request = $this->container['request'];
$response = $this->container['router']->dispatch($request);
// We only send back the content because, headers are already defined
// by WordPress internals.
$response->sendContent();
} catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception) {
/*
* Fallback to WordPress templates.
*/
}
}
/**
* Enqueue Admin scripts.
*/
public function adminEnqueueScripts()
{
/*
* Make sure the media scripts are always enqueued.
*/
wp_enqueue_media();
}
/**
* Output a global JS object in the <head> tag for the admin.
* Allow developers to add JS data for their project in the admin area only.
*/
public function adminHead()
{
$datas = apply_filters('themosisAdminGlobalObject', []);
$output = "<script type=\"text/javascript\">\n\r";
$output .= "//<![CDATA[\n\r";
$output .= "var themosisAdmin = {\n\r";
if (!empty($datas)) {
foreach ($datas as $key => $value) {
$output .= $key.': '.json_encode($value).",\n\r";
}
}
$output .= "};\n\r";
$output .= "//]]>\n\r";
$output .= '</script>';
// Output the datas.
echo $output;
}
}
}
/*
* Globally register the instance.
*/
$GLOBALS['themosis'] = Themosis::instance();