-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheme.php
More file actions
357 lines (306 loc) · 9.76 KB
/
Copy pathTheme.php
File metadata and controls
357 lines (306 loc) · 9.76 KB
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
347
348
349
350
351
352
353
354
355
356
357
<?php
namespace Vanilla;
use Vanilla\Fields;
use Vanilla\Query\Builder;
use Vanilla\View;
abstract class Theme {
use Concerns\Configurable,
Concerns\ManagesEndpoints,
Concerns\ManagesPostTypes,
Concerns\ManagesTaxonomies,
Concerns\ManagesCommands,
Concerns\ManagesPaths,
Concerns\GeneratesFaviconHtml,
Concerns\IntegratesBlade,
Concerns\MovesAdminBar,
Concerns\ManagesErrorReporting,
Concerns\ManagesLogging,
Concerns\HandlesEvents;
/** @var View\Factory */
protected $view;
/** @var Fields\Factory */
protected $fields;
/** @var static */
public static $instance;
/** @var array */
protected $templates = [];
/**
* Method that contains all the custom initialization logic
*/
abstract protected function startup();
/**
* Method that returns the array of data to be
* available via window.Vanilla object
*
* @return mixed
*/
abstract protected function vanillaObject();
/**
* @return static
*/
public static function getInstance()
{
if (is_null(static::$instance)) {
static::$instance = new static;
}
return static::$instance;
}
/**
* Bootstrap function for the class.
* Loads everything up based off of various parameters you can set.
*
* @param string $path root theme path
*/
public function bootstrap($path = '')
{
$this->setPath($path);
$this->loadConfiguration();
$this->registerErrorHandling();
/** Register actions on wp hooks */
add_action('init', [$this, 'init']);
add_action('phpmailer_init', [$this, 'configureSmtp']);
add_action('wp_enqueue_scripts', [$this, 'registerAssets']);
add_action('wp_head', [$this, 'echoCustomHeader']);
add_action('wp_footer', [$this, 'echoCustomFooter']);
/** Register filters */
add_filter('excerpt_more', [$this, 'substituteExcerpt']);
/** Disable theme editor if needed */
define('DISALLOW_FILE_EDIT', $this->config('disabled_theme_editor', false));
}
/**
* Code to be executed on wp_init
*/
public function init()
{
$this->rurnOffPageForPosts();
$this->updateSearchSlug();
$this->registerPostTypes();
$this->registerEndpoints();
$this->registerTaxonomies();
$this->registerCommands();
$this->initializeBlade();
$this->removeJunk();
$this->configureImages();
$this->configureMenus();
$this->configureSidebars();
$this->requireExtensions();
$this->moveAdminBar();
$this->fixPaginationWithCustomOffset();
$this->registerEvents();
$this->startup();
}
protected function rurnOffPageForPosts()
{
add_filter("pre_option_page_for_posts", function () {
return null;
});
add_action('admin_head', function () {
echo '<style type="text/css">#front-static-pages label[for="page_for_posts"] {display:none;}</style>';
});
}
protected function updateSearchSlug()
{
global $wp_rewrite;
$slug = $this->config('search.slug', 'search');
$wp_rewrite->search_base = $slug;
add_rewrite_rule("$slug$", "index.php?&s=", 'top');
}
public function configureSmtp($mailer)
{
$config = $this->config('email');
if(!$config) return;
$mailer->isSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = $config['smtp_host'];
$mailer->Port = $config['smtp_port'];
$mailer->Username = $config['smtp_username'];
$mailer->Password = $config['smtp_password'];
$mailer->From = $config['from'];
$mailer->FromName = $config['from_name'];
}
/**
* Register .js and .css assets
*/
public function registerAssets()
{
$noJQuery = $this->config('include_jquery') === false;
if ($noJQuery) {
wp_deregister_script('jquery');
}
wp_enqueue_script('app-manifest', mix('/js/manifest.js'), [], null, true);
wp_enqueue_script('vendor', mix('/js/vendor.js'), [], null, true);
wp_enqueue_script('script', mix('/js/app.js'), $noJQuery ? [] : ['jquery'], null, true);
wp_enqueue_style('style', mix('/css/theme.css'), [], null);
}
/**
* Echo custom header JS and CSS configured by admin and favicon <link> tags
*/
public function echoCustomHeader()
{
echo "<script>window.Vanilla = " . json_encode($this->vanillaObject()) . "</script>";
echo app()->fields()->get('header_css_js_custom', 'option');
echo $this->faviconHtml();
}
/**
* Echo custom footer JS and CSS configured by admin
*/
public function echoCustomFooter()
{
echo app()->fields()->get('custom_js_footer', 'option');
}
/**
* Clean up the_excerpt()
*/
public function substituteExcerpt()
{
$excerpt = $this->config('excerpt_more_text', '');
if ($this->config('excerpt_more_link')) {
$excerpt = '<a href="' . get_permalink() . '">' . $excerpt . '</a>';
}
return $excerpt;
}
/**
* Clean code = better code.
*/
protected function removeJunk()
{
remove_action('template_redirect', 'wp_shortlink_header', 11);
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10);
remove_action('wp_head', 'parent_post_rel_link', 10);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
add_filter('wp_headers', function ($headers) {
unset($headers['X-Pingback']);
return $headers;
});
}
/**
* Load Thumbnail Support and image sizes
*
* @see /config/images.php
*/
protected function configureImages()
{
if ($this->config('load_thumbnail_support')) {
add_theme_support('post-thumbnails');
}
foreach ($this->config('images', []) as $size) {
add_image_size($size['name'], $size['width'], $size['height'], $size['crop']);
}
}
/**
* Loads the menus.
* Menus are configured in the config/menus.php file
*
* @see /config/menus.php
*/
protected function configureMenus()
{
if (is_array($this->config('menus'))) {
add_theme_support('menus');
register_nav_menus($this->config('menus'));
}
}
/**
* Loads the sidebars.
* Sidebars are configured in the config/sidebars.php file
*
* @see /config/sidebars.php
*/
protected function configureSidebars()
{
if (is_array($this->config('sidebars'))) {
foreach ($this->config('sidebars') as $sidebar) {
register_sidebar($sidebar);
}
}
}
/**
* @return View\Factory
*/
public function view()
{
if (is_null($this->view)) {
$this->view = new View\Factory();
}
return $this->view;
}
/**
* @return Fields\Factory
*/
public function fields()
{
if (is_null($this->fields)) {
$this->fields = new Fields\Factory();
}
return $this->fields;
}
/**
* Require extensions files from app/extensions folder
*/
public function requireExtensions()
{
$extensions = glob($this->extensionsPath('*'));
foreach ($extensions as $path) {
require_once $path;
}
}
final private function __construct() {}
final private function __wakeup() {}
final private function __clone() {}
/**
* @see https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
*/
protected function fixPaginationWithCustomOffset()
{
add_action('pre_get_posts', function (\WP_Query &$query) {
global $paged;
$pageNum = isset($query->query_vars['_paged']) ? $query->query_vars['_paged'] : $paged;
if ($pageNum) {
$query->set('paged', $pageNum);
$query->is_paged = ($pageNum > 1);
if (isset($query->query_vars['_offset'])) {
$customOffset = $query->query_vars['_offset'];
$perPage = $query->query['posts_per_page'] ?: get_option('posts_per_page');
$trueOffset = $customOffset + (($pageNum - 1) * $perPage);
$query->set('offset', $trueOffset);
$query->query_vars['paged'] = $paged;
}
}
}, 1);
add_filter('found_posts', function ($found_posts, \WP_Query $query) {
if ($query->is_home()) {
return $found_posts - (isset($query->query_vars['_offset']) ? $query->query_vars['_offset'] : 0);
}
return $found_posts;
}, 1, 2);
}
protected function debugMode()
{
return defined('WP_DEBUG') && WP_DEBUG === true;
}
protected function registerShortCode($tag, $handler)
{
add_shortcode($tag, $handler);
return $this;
}
public function getRegisteredTemplates()
{
return wp_get_theme()->get_post_templates();
}
public function cacheHash()
{
$theme = wp_get_theme();
return md5($theme->get_theme_root() . '/' . $theme->get_stylesheet());
}
public function query()
{
return new Builder();
}
}