forked from HoF-Halle-Wittenberg/amb-dido
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amb-dido.php
769 lines (650 loc) · 28.2 KB
/
amb-dido.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
<?php
/**
* Plugin Name: AMB-DidO Plugin
* Description: Erstellt Metadaten gemäß AMB-Standard im JSON-Format für didaktische und Organisationsressourcen
* Version: 0.8.2
* Author Justus Henke, Manuel Oellers
*/
/**
* Features und Funktionen laden
* */
include_once(plugin_dir_path( __FILE__ ) . 'metabox.php'); // Weitere Metaboxen
include_once(plugin_dir_path( __FILE__ ) . 'frontend.php'); // Frontend-Darstellung
include_once(plugin_dir_path( __FILE__ ) . 'search.php'); // Suchfunktionen
require_once(plugin_dir_path( __FILE__ ) . 'options.php'); // Plugin-Einstellungen
/**
* Abhängige Dateien laden
*/
function amb_dido_enqueue_styles() {
wp_enqueue_style('amb-dido-styles', plugin_dir_url(__FILE__) . 'styles.css');
}
function amb_dido_enqueue_frontend_styles() {
wp_register_style('amb_dido_styles_frontend', plugins_url('styles-frontend.css', __FILE__));
wp_enqueue_style('amb_dido_styles_frontend');
}
function amb_enqueue_scripts() {
wp_enqueue_script('amb-keywords-js', plugin_dir_url(__FILE__) . 'scripts.js', [], null, true);
}
function amb_enqueue_admin_scripts() {
wp_enqueue_script('post'); // This script is needed for tag input and other post editing features.
}
add_action('admin_enqueue_scripts', 'amb_enqueue_admin_scripts');
/**
* Funktionen aufrufen
*/
add_action('admin_enqueue_scripts', 'amb_enqueue_scripts'); // JS laden
add_action('admin_enqueue_scripts', 'amb_dido_enqueue_styles'); // CSS Styles
add_action('wp_enqueue_scripts', 'amb_dido_enqueue_frontend_styles'); // CSS Styles für das Frontend
add_action('admin_menu', 'amb_dido_create_settings_page'); // Optionsseite erstellen
add_action('admin_init', 'amb_dido_register_settings'); // Optionen registrieren
add_action('add_meta_boxes', 'amb_dido_add_custom_box'); // Felder erzeugen
add_action('save_post', 'amb_dido_save_post_meta'); // Werte speichern
add_action('wp_head', 'amb_dido_add_json_ld_to_header'); // JSON schreiben
// Logfile erstellen
function amb_dido_log($message) {
if (is_array($message) || is_object($message)) {
$message = print_r($message, true);
}
$log_file = plugin_dir_path(__FILE__) . 'amb_dido.log';
file_put_contents($log_file, current_time('mysql') . " " . $message . "\n", FILE_APPEND);
}
/**
* AMB Metabox
* --------------
*/
/**
* Registriert die Metabox für die AMB-Attribute.
*/
function amb_dido_add_custom_box() {
$selected_post_types = get_option('amb_dido_post_types', []);
foreach ($selected_post_types as $post_type) {
add_meta_box(
'amb_dido_meta_box',
__('AMB Metadaten', 'amb_dido'),
'amb_dido_meta_box_callback',
$post_type,
'normal',
'high'
);
}
}
// Hartkodierte Wertelisten
function amb_get_other_fields() {
return [
'amb_inLanguage' => [
'field_label' => 'Sprache des Inhalts',
'options' => [
['de' => 'Deutsch'],
['en' => 'English'],
['fr' => 'Français']
],
'amb_key' => 'inLanguage'
],
'amb_isAccessibleForFree' => [
'field_label' => 'Kostenfreier Zugang',
'options' => [
['true' => 'Ja'],
['false' => 'Nein']
],
'amb_key' => 'isAccessibleForFree'
],
'amb_license' => [
'field_label' => 'Lizenz des Inhalts',
'options' => [
['http://creativecommons.org/licenses/by/4.0/legalcode.de' => 'CC BY 4.0 Namensnennung'],
['http://creativecommons.org/licenses/by-sa/4.0/legalcode.de' => 'CC BY-SA 4.0 Gleiche Bedingungen '],
['https://creativecommons.org/licenses/by-nc/4.0/legalcode.de' => 'CC BY-NC 4.0 Nichtkommmerziell'],
['https://www.gnu.org/licenses/gpl-3.0.xml' => 'GNU GPL 3 Software']
],
'amb_key' => 'license'
],
'amb_conditionsOfAccess' => [
'field_label' => 'Zugangsbedingungen',
'options' => [
['http://w3id.org/kim/conditionsOfAccess/no_login' => 'Keine Anmeldung erforderlich'],
['http://w3id.org/kim/conditionsOfAccess/login' => 'Anmeldung erforderlich']
],
'amb_key' => 'conditionsOfAccess'
],
'amb_interactivityType' => [
'field_label' => 'Lehr-/Lernform',
'options' => [
['http://purl.org/dcx/lrmi-vocabs/interactivityType/active' => 'aktiv (eher selbstgesteuert)'],
['http://purl.org/dcx/lrmi-vocabs/interactivityType/expositive' => 'erklärend (eher fremdgesteuert)'],
['http://purl.org/dcx/lrmi-vocabs/interactivityType/mixed' => 'gemischt']
],
'amb_key' => 'interactivityType'
]
];
}
/**
* Funktion zur Rückgabe der JSON-URLs für Wertelisten.
*
* @return array Array mit JSON-URLs und AMB-Schlüsseln
*/
function amb_get_json_urls() {
$predefined_json_urls = [
'amb_area' => [
'url' => 'https://hof-halle-wittenberg.github.io/vocabs/area/index.json',
'amb_key' => 'area'
],
'amb_type' => [
'url' => 'https://hof-halle-wittenberg.github.io/vocabs/type/index.json',
'amb_key' => 'type'
],
'amb_organisationalContext' => [
'url' => 'https://hof-halle-wittenberg.github.io/vocabs/organisationalContext/index.json',
'amb_key' => 'about'
],
'amb_didacticUseCase' => [
'url' => 'https://hof-halle-wittenberg.github.io/vocabs/didacticUseCase/index.json',
'amb_key' => 'about'
],
'amb_learningResourceType' => [
'url' => 'https://skohub.io/dini-ag-kim/hcrt/heads/master/w3id.org/kim/hcrt/scheme.json',
'amb_key' => 'learningResourceType'
],
'amb_audience' => [
'url' => 'https://hof-halle-wittenberg.github.io/vocabs/audience/index.json',
'amb_key' => 'audience'
],
'amb_hochschulfaechersystematik' => [
'url' => 'https://skohub.io/dini-ag-kim/hochschulfaechersystematik/heads/master/w3id.org/kim/hochschulfaechersystematik/scheme.json',
'amb_key' => 'about'
]
];
$custom_json_urls = get_option('amb_dido_custom_fields', []);
$json_urls = $predefined_json_urls;
foreach ($custom_json_urls as $key => $custom_json_url) {
$json_urls[$key] = [
'url' => $custom_json_url['url'],
'amb_key' => $custom_json_url['key']
];
}
return $json_urls;
}
function amb_get_all_external_values() {
$urls = amb_get_json_urls();
$all_values = [];
foreach ($urls as $key => $url_data) {
$values = amb_fetch_external_values($url_data['url'], $key, $url_data['amb_key']);
if (!empty($values)) {
$all_values[$key] = $values;
}
}
return $all_values;
}
function amb_fetch_external_values($url, $key, $amb_key) {
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return [];
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
$field_label = $data['title']['de'] ?? 'Standard-Titel';
$concepts = $data['hasTopConcept'] ?? [];
$options = amb_parse_concepts($concepts);
return [
'field_label' => $field_label,
'options' => $options,
'amb_key' => $amb_key
];
}
function amb_parse_concepts($concepts) {
$options = [];
foreach ($concepts as $concept) {
if (isset($concept['id']) && isset($concept['prefLabel']['de'])) {
$entry = [
$concept['id'] => $concept['prefLabel']['de']
];
if (isset($concept['narrower'])) {
$entry['narrower'] = amb_parse_concepts($concept['narrower']);
}
$options[] = $entry;
}
}
return $options;
}
// veraltet: Alle Wertelisten abrufen, nur für erste Ebene
function amb_get_all_external_values_broader() {
$urls = amb_get_json_urls();
$all_values = [];
foreach ($urls as $key => $url) {
$values = amb_get_external_values($key);
if (!empty($values)) {
$all_values[$key] = $values;
}
}
return $all_values;
}
// Externe Wertelisten, verallgemeinert für erste Ebene
function amb_get_external_values($key, $field_label = null) {
$urls = amb_get_json_urls();
if (!isset($urls[$key])) {
return []; // Keine Daten, wenn keine URL gefunden wird
}
$response = wp_remote_get($urls[$key]);
if (is_wp_error($response)) {
return [];
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Setzen des Feldetiketts, falls nicht explizit übergeben
if (is_null($field_label)) {
$field_label = $data['title']['de'] ?? 'Standard-Titel';
}
$concepts = $data['hasTopConcept'] ?? [];
$options = [];
foreach ($concepts as $concept) {
if (isset($concept['id']) && isset($concept['prefLabel']['de'])) {
$options[] = [$concept['id'] => $concept['prefLabel']['de']];
}
}
return [
'field_label' => $field_label,
'options' => $options
];
}
// Zugriff auf die Defaults und die entsprechenden Labels aus der Optionen-Konfiguration
function amb_dido_display_defaults($field, $options) {
$defaults = get_option('amb_dido_defaults');
// Mapping von field auf field_label
//$all_fields = amb_get_other_fields();
$all_fields = array_merge(amb_get_other_fields(), amb_get_all_external_values());
$field_label = isset($all_fields[$field]['field_label']) ? $all_fields[$field]['field_label'] : $field;
// Ausgabe der Default-Werte oder "Keine Auswahl"
if (isset($defaults[$field])) {
if ($defaults[$field] === 'deactivate') {
echo "<p>{$field_label}: <strong>Feld ausgeblendet</strong></p>";
} else {
$default_value = $defaults[$field];
$label = 'Unbekannte Auswahl';
// Überprüfe die Optionen und finde das passende Label
if (isset($all_fields[$field]['options'])) {
foreach ($all_fields[$field]['options'] as $option) {
if (isset($option[$default_value])) {
$label = $option[$default_value];
break;
}
}
}
echo "<p>{$field_label}: <strong>" . esc_html($label) . "</strong></p>";
}
} else {
echo "<p>{$field_label}: <strong>Keine Auswahl getroffen</strong></p>";
}
}
/**
* Generiert eine Checkbox-Gruppe mit SVG-Icons basierend auf den gegebenen Parametern.
*
* @param string $title Der Titel für die Gruppe.
* @param string $name Der Name des Feldes im Formular.
* @param array $options Die Optionen als Array von IDs zu Labels.
* @param array $stored_values Die gespeicherten Werte für die Checkboxen.
*/
// Zeigt erste und zweite Ebene eines Vokabulars an
function amb_generate_checkbox_group_any($name, $options, $stored_values, $title = null) {
// Falls kein Titel übergeben wurde, versuchen, den Titel aus den Optionen zu extrahieren
if ($title === null && isset($options['field_label'])) {
$title = $options['field_label'];
}
$svg_check = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" role="presentation" class="components-checkbox-control__checked" aria-hidden="true" focusable="false"><path d="M16.7 7.1l-6.3 8.5-3.3-2.5-.9 1.2 4.5 3.4L17.9 8z"></path></svg>';
echo '<label class="amb-field">' . esc_html($title) . '</label><br />';
echo '<div class="grid-container">';
foreach ($options['options'] as $option) {
foreach ($option as $id => $label) {
if (!isset($option['narrower'])) {
// Hauptoption ohne "narrower"-Unteroptionen oder falsche Struktur
$checked = in_array($id, $stored_values) ? 'checked' : '';
echo '<div class="grid-item components-base-control__field tbroad">';
echo '<span class="components-checkbox-control__input-container amb-control">';
echo '<input type="checkbox" name="' . esc_attr($name) . '[]" value="' . esc_attr($id) . '" ' . $checked . ' id="type_' . esc_attr($id) . '" class="components-checkbox-control__input" onchange="toggleSVG(this)">';
if ($checked) {
echo $svg_check;
}
echo '</span>';
echo '<label for="type_' . esc_attr($id) . '" class="label amb-control-label">' . esc_html($label) . '</label>';
echo '</div>';
} elseif (isset($option['narrower']) && !is_array($label)) {
// Option mit "narrower"-Unteroptionen
$checked = in_array($id, $stored_values) ? 'checked' : '';
$collapse_class = $checked ? '' : 'collapsed';
$expanded = $checked ? 'true' : 'false';
echo '<div class="grid-item components-base-control__field tnarrow">';
echo '<span class="components-checkbox-control__input-container amb-control">';
echo '<input type="checkbox" name="' . esc_attr($name) . '[]" value="' . esc_attr($id) . '" ' . $checked . ' id="type_' . esc_attr($id) . '" class="components-checkbox-control__input" onchange="toggleSVG(this)">';
if ($checked) {
echo $svg_check;
}
echo '</span>';
echo '<label for="type_' . esc_attr($id) . '" class="label amb-control-label">' . esc_html($label) . '</label>';
// Erste Ebene von "narrower"-Unteroptionen anzeigen
echo '<button type="button" onclick="toggleNarrower(this);" class="amb-narrower ' . $collapse_class . '" aria-expanded="' . $expanded . '"></button>';
echo '<div class="narrower-container grid-container" style="display: ' . ($checked ? 'block' : 'none') . ';">';
foreach ($option['narrower'] as $narrower_option) {
if (is_array($narrower_option)) {
foreach ($narrower_option as $narrower_id => $narrower_label) {
if (!is_array($narrower_label)) {
$checked = in_array($narrower_id, $stored_values) ? 'checked' : '';
echo '<div class="grid-item components-base-control__field tsub">';
echo '<span class="components-checkbox-control__input-container amb-control">';
echo '<input type="checkbox" name="' . esc_attr($name) . '[]" value="' . esc_attr($narrower_id) . '" ' . $checked . ' id="type_' . esc_attr($narrower_id) . '" class="components-checkbox-control__input" onchange="toggleSVG(this)">';
if ($checked) {
echo $svg_check;
}
echo '</span>';
echo '<label for="type_' . esc_attr($narrower_id) . '" class="label amb-control-label">' . esc_html($narrower_label) . '</label>';
echo '</div>';
}
}
}
}
echo '</div>'; // Ende der narrower-container
echo '</div>'; // Ende der grid-item tnarrow
}
}
}
echo '</div>';
}
/* Hilfsfunktion um ids aus Arrays zu extrahieren */
function amb_get_selected_ids($meta_field) {
$stored_values = get_post_meta(get_the_ID(), $meta_field, true);
$stored_values = is_array($stored_values) ? $stored_values : [];
$ids = [];
foreach ($stored_values as $value) {
if (isset($value['id'])) {
$ids[] = $value['id'];
}
}
return $ids;
}
/**
* Generiert das HTML für die AMB Metabox.
*/
function amb_dido_meta_box_callback($post) {
// Sicherheit: Einfügen eines Nonce-Feldes für Verifizierung
wp_nonce_field('amb_dido_save_meta_box_data', 'amb_dido_meta_box_nonce');
// Generierung Description
$description = '';
$use_excerpt_for_description = get_option('use_excerpt_for_description', 'no');
if ($use_excerpt_for_description !== 'yes') {
$description = get_post_meta($post->ID, 'amb_description', true);
echo '<label for="amb_description" class="amb-field">Beschreibung des Inhalts</label><br />';
echo '<p class="components-form-token-field__help">In zwei bis drei Sätzen den Inhalt beschreiben.</p>';
echo '<textarea name="amb_description" class="components-textarea-control__input amb-textarea" rows="4" cols="50">' . esc_textarea($description) . '</textarea><br />';
}
// Generierung Autoren
$creator = get_post_meta($post->ID, 'amb_creator', true);
echo '<label for="amb_creator" class="amb-field">Autor/innen</label><br />';
echo '<p class="components-form-token-field__help">Namen mit Kommas trennen.</p>';
echo '<input type="text" name="amb_creator" size="80" value="' . esc_attr($creator) . '" class="amb-textinput" /><br />';
// Generierung der Checkbox-Felder
$defaults = get_option('amb_dido_defaults');
$mapping = get_option('amb_dido_taxonomy_mapping', array());
$checkbox_options = array_merge(amb_get_other_fields(), amb_get_all_external_values());
// Zieht die Feldstruktur korrekt mit allen verfügbaren Ebenen
foreach ($checkbox_options as $field => $data) {
if (isset($defaults[$field]) && !empty($defaults[$field])) {
amb_dido_display_defaults($field, $data);
} elseif (isset($mapping[$field])) {
// Field is mapped to a taxonomy, don't display it
continue;
}
/* elseif (isset($defaults[$field]) && $defaults[$field] == 'deactivate') {
// do nothing
}*/
else {
$stored_ids = amb_get_selected_ids($field);
amb_generate_checkbox_group_any($field, $data, $stored_ids);
}
}
}
/**
* Speichert die Post-Metadaten.
*/
function amb_dido_save_post_meta($post_id) {
// Überprüfen der Berechtigungen
if (!isset($_POST['amb_dido_meta_box_nonce']) || !wp_verify_nonce($_POST['amb_dido_meta_box_nonce'], 'amb_dido_save_meta_box_data')) {
return;
}
// Überprüfen, ob dies ein Autosave ist. Falls ja, abbrechen.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Überprüfen, ob der Benutzer die Berechtigung zum Bearbeiten des Beitrags hat.
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Speicherung der offenen Felder
$open_fields = [
'amb_description',
'amb_creator'
];
foreach ($open_fields as $field) {
if (isset($_POST[$field])) {
update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
} else {
delete_post_meta($post_id, $field);
}
}
// Alle Wertelisten speichern
amb_save_all_checkbox_data($post_id);
}
function amb_save_all_checkbox_data($post_id) {
// Alle verfügbaren externen Werte abrufen
// $all_options = amb_get_all_external_values();
$all_options = array_merge(amb_get_other_fields(), amb_get_all_external_values());
if (empty($all_options)) {
return;
}
// Rekursive Funktion zur Suche und Hinzufügung von Labels
function find_label_and_add($type_id, $options, &$to_save) {
foreach ($options as $option) {
foreach ($option as $id => $label) {
if ($id == $type_id && !is_array($label)) {
$to_save[] = [
'id' => $type_id,
'prefLabel' => ['de' => $label],
'type' => 'Concept'
];
return true;
} elseif (isset($option['narrower']) && is_array($option['narrower'])) {
if (find_label_and_add($type_id, $option['narrower'], $to_save)) {
return true;
}
}
}
}
return false;
}
// Durch alle Felder iterieren und die Werte speichern
foreach ($all_options as $field_key => $field_data) {
if (isset($_POST[$field_key])) {
$selected_options = $_POST[$field_key];
$to_save = [];
foreach ($selected_options as $type_id) {
find_label_and_add($type_id, $field_data['options'], $to_save);
}
// Prüfen, ob `to_save` nicht leer ist, bevor es gespeichert wird
if (!empty($to_save)) {
update_post_meta($post_id, $field_key, $to_save);
} else {
delete_post_meta($post_id, $field_key);
}
} else {
delete_post_meta($post_id, $field_key);
}
}
}
// creator-Objekte vorbereiten
function amb_generate_creator_objects($post_id) {
$creators = array_filter(explode(',', get_post_meta($post_id, 'amb_creator', true)), function($value) {
return trim($value) !== '';
});
$creator_objects = [];
foreach ($creators as $creator) {
$creator_objects[] = [
'type' => 'Person',
'name' => $creator,
];
}
return $creator_objects;
}
function amb_get_keywords($post_id) {
$keywords = '';
$override_taxonomy = get_option('override_ambkeyword_taxonomy', '');
if (!empty($override_taxonomy)) {
$terms = wp_get_post_terms($post_id, $override_taxonomy, ['fields' => 'names']);
if (!is_wp_error($terms) && !empty($terms)) {
$keywords = implode(',', $terms);
}
} else {
$terms = get_the_terms($post_id, 'ambkeywords');
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
$tmp_keywords[] = $term->name;
}
$keywords = implode(',', $tmp_keywords);
}
}
return explode(',', $keywords);
}
/**
* Gets the description from excerpt or post meta
*
* @param WP_Post $post
* @return string
*/
function amb_get_description($post): string {
$description = '';
$use_excerpt_for_description = get_option('use_excerpt_for_description', 'no');
if ($use_excerpt_for_description === 'yes') {
$description = get_the_excerpt($post);
} else {
$description = get_post_meta($post->ID, 'description', true);
}
return $description;
}
/**
* Gets the language from post meta
*
* @param WP_Post $post
* @return array
*/
function amb_get_language($post): array {
$field = 'amb_inLanguage';
$defaults = get_option('amb_dido_defaults');
$post_languages = get_post_meta($post->ID, $field, true) ?: $defaults[$field];
$amb_languages = [];
if(is_array($post_languages)) {
foreach($post_languages as $lang) {
if(isset($lang['id'])) {
$amb_languages[] = $lang['id'];
}
}
} elseif (is_string($post_languages) && !empty($post_languages)) {
$amb_languages[] = $languages;
}
return $amb_languages;
}
/**
* Gets isAccessibleForFree
*
* @param WP_Post $post
* @return bool
*/
function amb_get_isaccessibleforfree($post): bool {
$field = 'amb_isAccessibleForFree';
$defaults = get_option('amb_dido_defaults');
$post_values = get_post_meta($post->ID, $field, true) ?: $defaults[$field];
$amb_value = true;
if(is_array($post_values)) {
if(isset($post_values[0]['id'])) {
$amb_value = $post_values[0]['id'];
}
} elseif (is_string($post_values) && !empty($post_values)) {
$amb_value = $post_values;
}
return filter_var($amb_value, FILTER_VALIDATE_BOOLEAN);
}
/**
* Bindet Custom-Fields in das JSON-LD-Format ein.
*/
function amb_dido_add_json_ld_to_header() {
if (is_singular(get_option('amb_dido_post_types', []))) {
global $post;
// Voreinstellungen aus Optionseite rufen
$defaults = get_option('amb_dido_defaults');
// Mit Taxonomien überbrückte Felder rufen
$mapping = get_option('amb_dido_taxonomy_mapping', array());
// Alle Felder (hartkodiert und extern) abrufen
$all_options = array_merge(amb_get_other_fields(), amb_get_all_external_values());
// JSON Elemente zusammenstellen
$json_ld_data = [
"@context" => ["https://w3id.org/kim/amb/context.jsonld", "https://schema.org", ["@language" => "de"]],
"id" => get_permalink($post->ID),
"dateCreated" => get_the_date('c', $post),
"datePublished" => get_the_date('c', $post),
"dateModified" => get_the_modified_date('c', $post),
"publisher" => [["type" => "Organization", "name" => get_bloginfo('name')]],
"creator" => amb_generate_creator_objects($post->ID),
"name" => get_the_title($post),
"description" => amb_get_description($post),
"inLanguage" => amb_get_language($post),
"isAccessibleForFree" => amb_get_isaccessibleforfree($post),
];
// Keywords auslesen
$keywords = amb_get_keywords($post->ID) ?: '';
if(!empty($keywords)) $json_ld_data['keywords'] = $keywords;
// Thumbnail
$image = get_the_post_thumbnail_url($post, 'full');
if($image !== false) $json_ld_data['image'] = $image;
foreach ($all_options as $field => $data) {
// Skip already handled fields
if(in_array($field, ['amb_inLanguage','amb_isAccessibleForFree'])) {
continue;
}
if (isset($mapping[$field])) {
// Field is mapped to a taxonomy
$terms = wp_get_post_terms($post->ID, $mapping[$field], array('fields' => 'all'));
$value = array();
foreach ($terms as $term) {
$value[] = array(
'id' => $term->slug,
'prefLabel' => array('de' => $term->name),
'type' => 'Concept'
);
}
} else {
$value = get_post_meta($post->ID, $field, true);
if (empty($value)) {
$value = $defaults[$field] ?? null;
}
}
$amb_key = $data['amb_key'] ?? 'about';
if (!is_null($value) && !empty($value)) {
$formatted_value = is_array($value) ? array_map(function ($item) use ($data) {
return [
'id' => $item['id'],
'prefLabel' => ['de' => $item['prefLabel']['de']],
'type' => 'Concept'
];
}, $value) : [
'id' => $value,
'prefLabel' => ['de' => $data['options'][0][$value]],
'type' => 'Concept'
];
if(in_array($amb_key, ['license', 'conditionsOfAccess', 'interactivityType'])) {
$formatted_value = $formatted_value[0] ?? $formatted_value;
}
if (isset($json_ld_data[$amb_key])) {
$json_ld_data[$amb_key] = array_merge($json_ld_data[$amb_key], $formatted_value);
} else {
$json_ld_data[$amb_key] = $formatted_value;
}
}
}
// JSON-LD-Ausgabe im Header
echo '<script type="application/ld+json">' . json_encode($json_ld_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>';
}
}