-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-mandala-kadence.php
419 lines (387 loc) · 15.8 KB
/
class-mandala-kadence.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
* Mandala Kadence Theme Class : A child theme of Kadence for use with Mandala content
*
* This theme allows for custom styling of Kadence to work with Mandala content. The stylesheets are written in
* SCSS but must be processed into CSS which is also stored in this repo. In the init() function the theme only
* adds the CSS file. Only generic styles for adapting any Mandala content to the Kadence theme should be placed
* in this theme’s css. Custom CSS for subsites or specific sites should use the subsite class and custom CSS.
*
* This theme also allows for subsites. This requires the use of custom metadata for pages. This is best implemented
* through the plugin Advanced Custom Fields. The necessary fields are:
*
* subsite_nav : The ID number of the custom menu to use
* subsite_title : The Title for the subsite to display in the header in place of the blogname
* subsite_class : A special class to put in the <body> element to identify pages of a subsite.
*
* Any page *or children of a page* that has these fields set will display the specific menu and title in the header
* and attach the subsite class to the body along with the generic class "subsite".
*
* @author Than Grove
* @package Mandala Kadence
* @version 1.0
*/
class MandalaKadence {
/**
* The construct function adds actions and filters for
*/
public $main_title = '';
function __construct() {
// Custom Actions
add_action('after_setup_theme', array($this, 'init'));
add_action('wp_head', array($this, 'mandala_update_header'));
add_action('kadence_top_header', array($this, 'subsite_back_link'));
add_action('kadence_header', array($this, 'add_custom_data'));
add_action('get_template_part_template-parts/header/navigation', array($this, 'subsite_nav'));
add_action('before_kadence_logo_output', array($this, 'subsite_logo'));
add_action('kadence_footer_navigation', array($this, 'subsite_footer'));
//add_action('kadence_hero_header', array($this, 'subsite_title'));
// Custom Filters
add_filter('pre_get_document_title', array($this, 'subsite_title_clean'));
add_filter('body_class', array($this, 'update_body_class'));
// Get main site title before overwriting in the following filter
$this->main_title = get_bloginfo();
add_filter('pre_option_blogname', array($this, 'subsite_bname'));
add_filter('kadence_logo_url', array($this, 'subsite_url'));
// Disable update emails
add_filter( 'auto_core_update_send_email', array($this, 'stop_wpupdate_emails'), 10, 4);
add_filter( 'auto_plugin_update_send_email', '__return_false' );
add_filter( 'auto_theme_update_send_email', '__return_false' );
}
function init() {
// Reigster styles and scripts for Mandala Kadence theme
wp_enqueue_style( 'mandala-kadence-styles', get_stylesheet_directory_uri() . '/css/mandala-kadence.css',
false,'1.0','all');
wp_enqueue_script( 'mandala-kadence-scripts', get_stylesheet_directory_uri() . '/js/mandala-kadence.js',
array ( 'jquery' ), 1.0, true);
// Add custom styles from /css/custom folder (just files ending in *.css)
$custom_folder = get_stylesheet_directory() . '/css/custom';
$files = array_diff(scandir($custom_folder), array('.', '..'));
$cssfiles = array_filter($files, function($fnm) {
return str_ends_with($fnm, '.css');
});
foreach($cssfiles as $n => $cssfile) {
// error_log(get_stylesheet_directory_uri() . "/css/custom/$cssfile");
wp_enqueue_style( 'mandala-kadence-custom-styles-' . $n, get_stylesheet_directory_uri() . "/css/custom/$cssfile",
false,'1.0','all');
}
}
/**
* This function adds custom data as a JSON object to the DOM to be used by an embedded React standalone
* Adds script tage with JSON text in it. (Deprecated)
* Replaced by window.mandala_wp settings in mandala plugin's enqueue_scripts function
*/
public function add_custom_data() {
$options = get_option( 'mandala_plugin_options' );
if (is_numeric($options['default_sidebar'])) {
$sbval = $options['default_sidebar'] * 1;
echo '<script type="application/json" id="mandala_data">{ "sidebar": ' . $sbval . ' }</script>';
}
}
/**
* Adds a CSS script tag to the head of a subsite page that contains the subsite CSS
* Each rule in the CSS field and each selector within each rule is appended with the
* body class for the subsite, e.g .my-subsite div.foo, .my-subsite div.bar {...}, etc.
*/
public function mandala_update_header() {
$pgid = get_the_ID();
// Import subsite info for a page
$subinf = $this->get_subsite_info();
if (!empty($subinf['css'])) {
$subid = $subinf['class'] ?: 'subsite' . $pgid;
$css_lines = preg_split("/\n*}\n*/", $subinf['css']);
$css_lines = array_map(function($item) use ($subid) {
if (empty($item)) { return ""; }
$item = trim($item);
list($selector, $rule) = explode('{', $item, 2);
$selpts = explode(',', $selector);
$selpts = array_map(function($sel) use ($subid) {
$sel = trim($sel);
if (empty($sel)) { return ""; }
return ".$subid $sel";
}, $selpts);
return implode(",\n", $selpts) . " {\n" . $rule;
}, $css_lines);
$css_lines = implode("\n}\n", $css_lines);
// error_log("Subsite CSS ($subid):\n$css_lines");
echo '<style id="' . $subid . '-styles">' . $css_lines . '</style>';
}
// Get text info if article in journal and add citation meta tags
if ($pgid) {
$text_id = get_field('mandala_text_id', $pgid);
if ($text_id) {
$url = 'https://texts.mandala.library.virginia.edu/shanti_texts/node_json/' . $text_id;
$data = $this->get_data($url);
$data = json_decode($data, true);
echo "\n\n<!-- Mandala Meta Tags -->\n";
// Title
$title = $data['title'];
$this->add_meta('citation_title', $title);
// Authors
$authors = $data['field_book_author']['und'];
$author_map = array_map(function ($a) { return $a['value']; }, $authors);
$alist = implode(', ', $author_map);
$this->add_meta('citation_author', $alist);
// Journal Title
$options = get_option( 'mandala_plugin_options' );
$journal = !empty($options['journal_title']) ? $options['journal_title'] : False;
if (!empty($journal)) {
$this->add_meta('citation_journal', $journal);
}
// DOI
$doi = $data['field_doi']['und'][0]['value'];
$this->add_meta('citation_doi', trim($doi));
// Abstract
$fullbook = $data['field_book_content']['und']['0']['value'];
if(str_contains($fullbook, 'Abstract:')) {
$abs = explode('Abstract:', $fullbook)[1];
$abs = strip_tags($abs);
if (!empty($abs)) {
$this->add_meta('citations_abstract', trim($abs));
}
}
// Comment closure
echo "<!-- End of Mandala Meta Tags -->\n\n";
}
}
}
private function add_meta($name, $cnt) {
echo "<meta name=\"$name\" content=\"$cnt\" /> \n";
}
/**
* Displays area above header with link back to main site
*/
public function subsite_back_link() {
$subs = $this->get_subsite_info();
if ($this->is_subsite()) {
$site_logo = get_custom_logo();
$home_url = get_home_url();
echo '<div class="subsite-back">' . $site_logo . ' <a class="back-link" href="' . $home_url . '">' .
$this->main_title . '</a> </div>';
}
}
/**
* Display the subsite logo
* CSS is defined to hide site logo if it falls after subsite logo
*/
public function subsite_logo() {
$sublogo = $this->get_subsite_info('logo');
if (!empty($sublogo)) {
// error_log( json_encode($sublogo) );
$logo = wp_get_attachment_image( $sublogo, 'full', true, array('class' => 'subsite-logo'));
echo $logo;
}
}
/**
* Called as a filter for pre_get_document_title. Serves to strip tags from the title
* that is inserted into the HTML head meta tag
*
* @param $title
* @return string
*/
public function subsite_title_clean($title) {
$subsite_title = $this->get_subsite_info('title');
if ($subsite_title) {
return strip_tags($subsite_title);
}
}
/**
* Replaces the site title (i.e. blog name) with the subsite title
*
* @param $bname
*
* @return mixed
*/
public function subsite_bname($bname) {
$subsite_title = $this->get_subsite_info('title');
if (!empty($subsite_title)) {
return $subsite_title;
}
return $bname;
}
public function subsite_url($homeurl) {
if ($this->is_subsite()) {
$myid = get_the_ID();
$homeurl = $this->get_subsite_home($myid);
}
return $homeurl;
}
/**
* Called by the filter for "body_class".
*
* @return array|string[]
*/
public function update_body_class() {
$extra_classes = array('loading'); # Add loading class to hide menu initially
// Adds specific classes to the body for subsites.
if ($this->is_subsite()) {
$extra_classes[] = 'subsite'; # start list with generic "subsite" class for body
$subsite_class = $this->get_subsite_info('class') ?: 'subsite' . get_the_ID(); # add unique ss id
$subsite_class = explode(' ', $subsite_class); # if user has space delimited classes in field
$extra_classes = array_merge($extra_classes, $subsite_class);
}
// Logged in class
if (is_user_logged_in()) {
$extra_classes[] = 'logged-in';
}
return $extra_classes;
}
/**
* Show a particular navigation menu for a subsite
*/
public function subsite_nav() {
$subsite = $this->get_subsite_info('menu');
if (!empty($subsite)) {
$menu_args = array(
'menu' => $subsite,
'menu_class' => 'menu',
'container' => 'div',
'container_id' => 'subsite-menu',
'container_class' => 'subsite-menu primary-menu-container header-menu-container',
'theme_location' => 'main_center',
'echo' => false,
);
$menu = wp_nav_menu( $menu_args );
$prefels = '<div id="mandala-subsite-menu"
class="site-header-item site-header-focus-item site-header-item-main-navigation
header-navigation-layout-stretch-false header-navigation-layout-fill-stretch-false"
data-section="kadence_customizer_primary_navigation">
<nav id="site-navigation"
class="main-navigation header-navigation nav--toggle-sub
header-navigation-style-underline header-navigation-dropdown-animation-none"
role="navigation" aria-label="Primary Navigation">';
$suffels = '</nav></div>';
echo "$prefels $menu $suffels";
}
}
/**
* Display a custom footer menu for a subsite
*/
public function subsite_footer() {
// Custom fields for subsite existence and subsite alt text.
$sbsfmenu = $this->get_subsite_info('footer_menu');
if (!empty($sbsfmenu)) {
$menu_args = array(
'menu' => $sbsfmenu,
'menu_class' => 'menu',
'container' => 'div',
'container_id' => 'subsite-menu',
'container_class' => 'subsite-menu primary-menu-container header-menu-container',
'theme_location' => 'main_center',
'echo' => false,
);
$footer_menu = wp_nav_menu( $menu_args );
echo "<nav id=\"subsite-footer-menu\">$footer_menu</nav>";
}
}
/**
* Gets all subsite info as an associative array or gets the value for a particular field
* @param string $field
* @return array|mixed
*/
public function get_subsite_info($field='all') {
$myid = get_the_ID();
$ssinfo = array(
'title' => $this->get_ancestor_value($myid, 'subsite_title'),
'logo' => $this->get_ancestor_value($myid, 'subsite_logo'),
'class' => $this->get_ancestor_value($myid, 'subsite_class'),
'menu' => $this->get_ancestor_value($myid, 'subsite_menu'),
'footer_menu' => $this->get_ancestor_value($myid, 'subsite_footer_menu'),
'css' => $this->get_ancestor_value($myid, 'subsite_css'),
);
if (in_array($field, array_keys($ssinfo))) {
return $ssinfo[$field];
}
return $ssinfo;
}
/**
* Determine if a page is part of a subsite based on whether _any_ of the subsite variables are set
* @return bool
*/
public function is_subsite() {
$ssinfo = $this->get_subsite_info();
foreach($ssinfo as $k => $v) {
if (!empty($v)) {
return true; // If a single subsite value is not empty then it is considered a subsite
}
}
return false;
}
/**
* Returns custom setting value by name if it is set on this page or any of its ancestors
*
* @param $pid
* @param $varname
* @return false|mixed
*/
public function get_ancestor_value($pid, $varname) {
if (empty($pid) || !is_numeric($pid)) { return false; }
$val = get_post_meta($pid, $varname, true);
if (empty($val)) {
$thepost = get_post($pid);
if (!empty($thepost->post_parent)) {
$val = $this->get_ancestor_value($thepost->post_parent, $varname);
} elseif ($thepost->post_type == 'post') {
$blog_home_id = get_option( 'blog_homepage' );
if (!empty($blog_home_id)) {
$val = $this->get_ancestor_value($blog_home_id, $varname);
}
}
}
return $val;
}
/**
* Get a subsite's home page url
*
* @param $pid
* @return false|string
*/
public function get_subsite_home($pid) {
$thepost = get_post($pid);
$blog_home_id = get_option( 'blog_homepage' );
if ($thepost->post_type == 'page') {
while (!empty($thepost->post_parent)) {
$thepost = $thepost->post_parent;
}
return get_home_url() . '/' . get_page_uri($thepost);
} elseif ($thepost->post_type == 'post' && !empty($blog_home_id)) {
$thepost = get_post($blog_home_id);
return get_home_url() . '/' . get_page_uri($thepost);
} else {
return get_home_url();
}
}
private function get_data($url) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"x-rapidapi-host: unogsng.p.rapidapi.com",
"x-rapidapi-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
error_log("cURL ($url) Error #:" . $err);
} else {
return $response;
}
}
// Disable automatic emails for wp update when they succeed
public function stop_wpupdate_emails( $send, $type, $core_update, $result ) {
if ( ! empty( $type ) && $type == 'success' ) {
return false;
}
return true;
}
}
$mandalakadence = new MandalaKadence();