-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
220 lines (186 loc) · 5.96 KB
/
functions.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
<?php
add_theme_support('custom-logo');
add_theme_support('menus');
// Add custom settings to general settings
add_filter('admin_init', 'register_fields');
function register_fields()
{
register_setting('general', 'frontend-dir', 'esc_attr');
add_settings_field('frontend-dir', '<label for="frontend-dir">' . __('Frontend Directory', 'frontend-dir') . '</label>', 'print_custom_field', 'general');
}
function print_custom_field()
{
$host = explode(".", $_SERVER['HTTP_HOST']);
$defaultValue = $host[count($host) - 2]; // get the second-last item in the array to strip of also the TLD
$value = get_option('frontend-dir', $defaultValue);
echo '<input type="text" id="frontend-dir" name="frontend-dir" value="' . $value . '" />';
}
// Add site logo to rest api
add_action('rest_api_init', 'add_logo_to_JSON');
function add_logo_to_JSON()
{
register_rest_field('page', 'site_logo_src', array( // post for where to register - page_logo_src is the name for api
'get_callback' => 'get_logo_src',
'update_callback' => null,
'schema' => null,
));
}
function get_logo_src($object, $field_name, $request)
{
$size = 'full';
$custom_logo_id = get_theme_mod('custom_logo');
$feat_img_array = wp_get_attachment_image_src($custom_logo_id, $size, true);
return $feat_img_array[0];
}
// Add rebuild button to adminbar (top)
add_action('admin_bar_menu', 'add_item', 100);
function add_item($admin_bar)
{
global $pagenow;
$admin_bar->add_menu(array('id' => 'rebuild-frontend', 'title' => 'Rebuild Frontend', 'href' => '#', 'meta' => [
'html' => "<span style='opacity: 0'>" . get_option('frontend-dir') . '</span>'
]));
}
add_action('admin_footer', 'clickHandlerRebuildButton');
function clickHandlerRebuildButton()
{ ?>
<script type="text/javascript">
const rebuildBtn = document.querySelector(
"#wp-admin-bar-rebuild-frontend .ab-item"
);
let rebuilding = false;
rebuildBtn.addEventListener("click", (e) => {
e.preventDefault();
// Tiny bouncer
if (rebuilding) return;
rebuilding = true;
let originalText = rebuildBtn.textContent;
rebuildBtn.textContent = "Rebuilding...";
// reads frontend directory name from hidden span next to it.
let name = rebuildBtn.nextElementSibling.textContent;
fetch(`https://rebuilds.codebirds-apiserver.nl/${name}`, {
method: "POST",
})
.then((response) => response.json())
.then((data) => {
//Handle your data
if (data === "error")
rebuildBtn.textContent = `${originalText} (last rebuild failed)`;
else
rebuildBtn.textContent = `${originalText} (last rebuild was successful)`;
rebuilding = false;
})
.catch((err) => {
console.log(err);
rebuildBtn.textContent = `${originalText} (last rebuild failed)`;
});
let timeout = setTimeout(() => {
rebuildBtn.textContent = originalText;
clearTimeout(timeout);
}, 1000 * 60);
});
</script>
<?php
}
/* THIS APPLIES TO THE SAVE/PUBLISH BUTTONS TRIGGERED BY CTRL/CMD + S */
add_action('admin_footer', 'addKeyboardShortcut');
function addKeyboardShortcut()
{
?>
<script type="text/javascript">
const body = document.body;
let button = '';
let doingClick = false;
// let JSie know on which page to seek the buttons :)
if (body.classList.contains('post-php')) {
// now we're on a page where save buttons appear, let's seek for it!
button = body.querySelector("input[name='save']")
} else if (body.classList.contains('nav-menus-php')) {
button = body.querySelector("input[name='save_menu']")
} else if (body.classList.contains('theme-editor-php' || 'plugin-editor-php')) {
button = body.querySelector("input[name='submit']")
}
if (button) {
document.addEventListener('keydown', (e) => {
if (e.ctrlKey || e.metaKey && e.key === "s") {
if (doingClick) return
doingClick = true
// click that button!
button.click();
e.preventDefault();
doingClick = false
}
})
}
</script>
<?php
}
// Make ACF return null instead of false when value doesn't exist so Graphql won't throw an error
add_filter('acf/format_value', 'acf_nullify_empty', 100, 3);
if (!function_exists('acf_nullify_empty')) {
/**
* Return `null` if an empty value is returned from ACF.
*
* @param mixed $value
* @param mixed $post_id
* @param array $field
*
* @return mixed
*/
function acf_nullify_empty($value, $post_id, $field)
{
if (empty($value)) {
return null;
}
return $value;
}
}
// HEADLESS MODE CONFIGURATION
if (!defined('HEADLESS_MODE_CLIENT_URL')) {
$host = $_SERVER['HTTP_HOST'];
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == 'https' ? 'https' : 'http';
$domain = $protocol . '://' . $host;
define('HEADLESS_MODE_CLIENT_URL', $domain);
};
/**
*
* @see https://stackoverflow.com/a/768472/1469799
*
* @param $url
* @param bool $permanent
*/
function headless_mode_redirect($url, $permanent = false)
{
if (!HEADLESS_MODE_CLIENT_URL) {
return;
}
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
/**
* Based on https://gist.github.com/jasonbahl/5dd6c046cd5a5d39bda9eaaf7e32a09d
*/
add_action('parse_request', 'headless_mode_disable_front_end', 99);
function headless_mode_disable_front_end()
{
if (current_user_can('edit_posts')) {
return;
}
global $wp;
/**
* If the request is not part of a CRON, REST Request, GraphQL Request or Admin request,
* output some basic, blank markup
*/
if (
!defined('DOING_CRON') &&
!defined('REST_REQUEST') &&
!is_admin() &&
(empty($wp->query_vars['rest_oauth1']) &&
!defined('GRAPHQL_HTTP_REQUEST'))
) {
// adds the rest of the request to the new URL.
$new_url = trailingslashit(HEADLESS_MODE_CLIENT_URL) . $wp->request;
headless_mode_redirect($new_url, true);
exit;
}
}