1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Undefined\Stash; |
3
|
|
|
|
4
|
|
|
use TimberMenu; |
5
|
|
|
use TimberSite; |
6
|
|
|
use Twig_Extension_StringLoader; |
7
|
|
|
|
8
|
|
|
if (!class_exists('Timber')) { |
9
|
|
|
add_action('admin_notices', function () { |
10
|
|
|
echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url(admin_url('plugins.php#timber')) . '">' . esc_url(admin_url('plugins.php')) . '</a></p></div>'; |
11
|
|
|
}); |
12
|
|
|
|
13
|
|
|
return; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/* Include global functions */ |
17
|
|
|
include_once 'inc/functions/dd.php'; |
18
|
|
|
include_once 'inc/functions/advancedCustomSearch.php'; |
19
|
|
|
|
20
|
|
|
/* Include classes */ |
21
|
|
|
include_once 'inc/classes/ImageHelper.php'; |
22
|
|
|
include_once 'inc/classes/TransientHelper.php'; |
23
|
|
|
|
24
|
|
|
class Stash extends TimberSite |
25
|
|
|
{ |
26
|
|
|
protected $transientHelper; |
27
|
|
|
|
28
|
|
|
function __construct() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->transientHelper = new TransientHelper(); |
31
|
|
|
|
32
|
|
|
add_theme_support('post-formats'); |
33
|
|
|
add_theme_support('post-thumbnails'); |
34
|
|
|
add_theme_support('menus'); |
35
|
|
|
|
36
|
|
|
add_filter('timber_context', [$this, 'addToContext']); |
37
|
|
|
add_filter('get_twig', [$this, 'addToTwig']); |
38
|
|
|
add_filter('body_class', [$this, 'addPageClass']); |
39
|
|
|
|
40
|
|
|
add_action('init', [$this, 'registerPostTypes']); |
41
|
|
|
add_action('init', [$this, 'registerTaxonomies']); |
42
|
|
|
add_action('init', [$this, 'removeEmojiSupport']); |
43
|
|
|
add_action('init', [$this, 'acfSearch']); |
44
|
|
|
|
45
|
|
|
add_action('wp_enqueue_scripts', [$this, 'themeAssets']); |
46
|
|
|
|
47
|
|
|
add_action('post_updated', [$this, 'clearTransients']); |
48
|
|
|
add_action('save_post', [$this, 'clearTransients']); |
49
|
|
|
add_action('add_attachment', [$this, 'clearTransients']); |
50
|
|
|
|
51
|
|
|
parent::__construct(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Clear all transients after post create/update or media upload |
56
|
|
|
*/ |
57
|
|
|
function clearTransients() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->transientHelper->deleteAll(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Load all files from the inc/post-types/ folder |
64
|
|
|
*/ |
65
|
|
|
function registerPostTypes() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
foreach (glob(__DIR__ . "/inc/post-types/*.php") as $filename) { |
68
|
|
|
include_once $filename; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Check if ACF is installed, if so enable search |
74
|
|
|
*/ |
75
|
|
|
function acfSearch() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
if (!class_exists('acf')) { |
78
|
|
|
add_filter('posts_search', 'advanced_custom_search', 500, 2); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function registerTaxonomies() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
//this is where you can register custom taxonomies |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Remove Emoji support from wordpress |
90
|
|
|
*/ |
91
|
|
|
function removeEmojiSupport() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
remove_action('wp_head', 'print_emoji_detection_script', 7); |
94
|
|
|
remove_action('wp_print_styles', 'print_emoji_styles'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add things to the context for every twig template |
99
|
|
|
* @param $context |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
function addToContext($context) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$context['menu'] = new TimberMenu(); |
105
|
|
|
$context['site'] = $this; |
106
|
|
|
|
107
|
|
|
$context['is_home'] = is_front_page() ? "true" : "false"; |
108
|
|
|
$context['home_url'] = get_home_url(); |
109
|
|
|
|
110
|
|
|
if (function_exists("pll_current_language")) { |
111
|
|
|
$context['current_lang'] = pll_current_language(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $context; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* This is where you can add your own functions to twig |
119
|
|
|
* @param $twig |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
function addToTwig($twig) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$twig->addExtension(new Twig_Extension_StringLoader()); |
125
|
|
|
$twig->addGlobal('image', new ImageHelper()); |
126
|
|
|
|
127
|
|
|
return $twig; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Enqueue assets if files exist |
132
|
|
|
*/ |
133
|
|
|
function themeAssets() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$vendorJsExists = file_exists(get_template_directory() . '/dist/js/vendor.js'); |
136
|
|
|
$mainJsExists = file_exists(get_template_directory() . '/dist/js/main.js'); |
137
|
|
|
$mainJsDependencies = []; |
138
|
|
|
|
139
|
|
|
$mainCssExists = file_exists(get_template_directory() . '/dist/css/main.css'); |
140
|
|
|
$vendorCssExists = file_exists(get_template_directory() . '/dist/css/vendor.css'); |
141
|
|
|
$mainCssDependencies = []; |
142
|
|
|
|
143
|
|
|
if (!is_admin()) { |
144
|
|
|
wp_deregister_script('jquery'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($vendorJsExists) { |
148
|
|
|
wp_enqueue_script('stashVendorJs', get_template_directory_uri() . '/dist/js/vendor.js', [], filemtime(get_stylesheet_directory() . '/dist/js/vendor.js'), true); |
149
|
|
|
array_push($mainJsDependencies, 'stashVendorJs'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($mainJsExists) { |
153
|
|
|
wp_enqueue_script('stashMainJs', get_template_directory_uri() . '/dist/js/main.js', $mainJsDependencies, filemtime(get_stylesheet_directory() . '/dist/js/main.js'), true); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
if ($vendorCssExists) { |
|
|
|
|
157
|
|
|
wp_enqueue_style('stashVendorCss', get_template_directory_uri() . '/dist/css/vendor.css', [], filemtime(get_stylesheet_directory() . '/dist/css/vendor.css')); |
158
|
|
|
array_push($mainCssDependencies, 'stashVendorCss'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
if ($mainCssExists) { |
|
|
|
|
162
|
|
|
wp_enqueue_style('stashMainCss', get_template_directory_uri() . '/dist/css/main.css', $mainCssDependencies, filemtime(get_stylesheet_directory() . '/dist/css/main.css')); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Add the object slug name as class to the body classes |
168
|
|
|
* |
169
|
|
|
* @param $classes |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
function addPageClass($classes) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
if (!is_archive() && !is_home()) { |
175
|
|
|
global $post; |
|
|
|
|
176
|
|
|
$classes[] = get_post($post)->post_name; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $classes; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
new Stash(); |
184
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.