-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-naswp-helpers.php
214 lines (189 loc) · 5.23 KB
/
class-naswp-helpers.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
<?php
/**
* Main class for loading of helpers - mapping classes to functions
*
* @author Vladimír Smitka, Lynt.cz
* @author Karolína Vyskočilová, kybernaut.cz
* @package NasWP_Helpers
*/
if (!class_exists('NasWP_Helpers')) {
class NasWP_Helpers
{
/**
* Config
* @var array
*/
private $_config = [];
/**
* Construct
*
* @param string $config_path Location of the config file.
* @return void
*/
function __construct(string $config_path = '')
{
$this->_load_config($config_path);
}
public function auto_async_js()
{
require_once "inc/class-naswp-auto-async-js.php";
$async = new NasWP_AutoAsyncJS();
$async->init();
}
public function colors($colors = [], $allow_custom_colors = null, $gradients = [], $allow_custom_gradients = null)
{
if (empty($colors)) {
$colors = $this->_get_value_from_config('colors', 'array');
}
if ($allow_custom_colors === null) {
$allow_custom_colors = $this->_get_value_from_config('allow_custom_colors', 'boolean', true);
}
if (empty($gradients)) {
$gradients = $this->_get_value_from_config('gradients', 'array', null);
}
if ($allow_custom_gradients === null) {
$allow_custom_gradients = $this->_get_value_from_config('allow_custom_gradients', 'boolean', true);
}
require_once "inc/class-naswp-colors.php";
$colors_obj = new NasWP_Colors($colors, $allow_custom_colors, $gradients, $allow_custom_gradients);
$colors_obj->init();
}
public function file_names()
{
require_once "inc/class-naswp-file-names.php";
$filenames = new NasWP_FileNames();
$filenames->init();
}
/**
* Load GA
* @param string $id GA ID. Could by passed in config file.
* @return void
*/
public function ga($id = '')
{
if (empty($id)) {
$id = $this->_get_value_from_config('ga');
}
require_once "inc/class-naswp-ga.php";
$ga = new NasWP_GA($id);
$ga->init();
}
/**
* Load GTM
* @param string $id GTM ID. Could by passed in config file.
* @return void
*/
public function gtm($id = '')
{
if (empty($id)) {
$id = $this->_get_value_from_config('gtm');
}
require_once "inc/class-naswp-gtm.php";
$gtm = new NasWP_GTM($id);
$gtm->init();
}
/**
* Load lightbox
* @param mixed $path_to_helpers The path to helpers.
* @return void
*/
public function lightbox($path_to_helpers)
{
// TODO Jak vyřešit cestu ke zdrojovým souborům?
require_once "inc/class-naswp-lightbox.php";
$lightbox = new NasWP_Lightbox($path_to_helpers);
$lightbox->init();
}
/**
* Allow selected mime types.
* @param array $formats_array Mime types keyed by the file extension regex corresponding to those types. Could by passed in config file.
* @return void
*/
public function mimes($formats_array = [])
{
if (empty($formats_array)) {
$formats_array = $this->_get_value_from_config('mimes', 'array');
}
require_once "inc/class-naswp-mimes.php";
$mimes = new NasWP_Mimes($formats_array);
$mimes->init();
}
public function protected_member($protected_prefix = false)
{
require_once "inc/class-naswp-protected-member.php";
$protected_member = new NasWP_Protected_Member($protected_prefix);
$protected_member->init();
}
public function seo()
{
require_once "inc/class-naswp-seo.php";
$seo = new NasWP_SEO();
$seo->init();
}
/**
* Generate sitemap for selected post types.
* @param array $cpts List of postypes. Could by passed in config file.
* @return void
*/
public function sitemap($cpts = [])
{
if (empty($cpts)) {
$cpts = $this->_get_value_from_config('sitemap', 'array', ['post', 'page']);
}
require_once "inc/class-naswp-sitemap.php";
$sitemap = new NasWP_Sitemap($cpts);
$sitemap->init();
}
/**
* Get value from config and check its type. Die with error if no default value provided.
* @param string $key Value key.
* @param string $type Value type.
* @param mixed $default Default value.
* @return mixed
*/
private function _get_value_from_config(string $key, string $type = 'string', $default = '')
{
if (isset($this->_config[$key])) {
$value = $this->_config[$key];
if (gettype($value) === $type) {
return $value;
} else {
trigger_error("NasWP_Helpers: Incorect value type declared for config key $key", E_USER_WARNING);
if ($default) {
return $default;
} else {
die();
}
}
}
if ('' === $default) {
return $default;
} else {
trigger_error("NasWP_Helpers: Parameter for $key not declared in config file or passed in the function.", E_USER_WARNING);
die();
}
}
/**
* Load & parse config file
* @param string $config_path Location of the config file.
* @return bool Whether the config was loaded or not.
*/
private function _load_config(string $config_path = '')
{
if ($config_path) {
// Check the file extension.
if (substr($config_path, -4) !== 'json') {
trigger_error("The NasWP Helpers config is not a json file.", E_USER_WARNING);
return false;
}
// Load content.
$config_loaded = file_get_contents($config_path);
if ($config_loaded) {
$this->_config = json_decode($config_loaded, true);
return true;
}
}
return false;
}
}
}