-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-editor.php
107 lines (85 loc) · 2.93 KB
/
custom-editor.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
<?php
function my_acf_admin_head() {
?>
<style type="text/css">
.acf-repeater.-row > table > tbody > tr > td,
.acf-repeater.-block > table > tbody > tr > td,
.acf-table > tbody > tr > td {
border-bottom-color: #444;
border-top-color: #444;
}
.acf-vimeo-data-display__preview a {
overflow: auto;
}
</style>
<?php
}
add_action('acf/input/admin_head', 'my_acf_admin_head');
//
function sync_connections ($ok, $post_id) {
$connections = get_field('connections', $post_id, false);
if ($connections) {
foreach ($connections as $str) {
array_push($ok, (int)$str);
}
}
if (count($ok) > 0) {
update_field('connections', array_unique($ok), $post_id);
// $fp = fopen('/Users/nics/Sites/ns-doku/api/custom-log.txt', 'a');
// fwrite($fp, date(DATE_ISO8601) . ' - ' . $post_id . ' updated ' . $done . "\n");
// fwrite($fp, date(DATE_ISO8601) . ' - ' . json_encode(array_unique($ok)) . "\n");
// fclose($fp);
}
return;
}
function dn_get_ids_from_links ($value) {
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $value, $matches);
$ok = [];
foreach ($matches[0] as $url) {
if (preg_match('/ns-doku\.test|localhost\:8080|doku\.n-kort\.net|dn-api\.nsdoku\.de|dn-api-en\.nsdoku\.de/', $url)) {
$id = url_to_postid($url);
array_push($ok, $id);
}
}
return $ok;
}
// on save event
function my_acf_save_post ($post_id) {
$type = get_post_type($post_id);
if ($type !== 'projekt' && $type !== 'page') return;
// get all acf fields
$values = get_fields($post_id, false);
// rich fields, outside of content[]
$toParse = [];
if (isset($values['description'])) {
array_push($toParse, $values['description']);
}
// all content rows, as string
if (isset($values['content']) && (gettype($values['content']) == 'array' || gettype($values['content']) == 'object')) {
foreach ($values['content'] as $block) {
array_push($toParse, json_encode($block, JSON_UNESCAPED_SLASHES)); // so regex works
}
}
if (count($toParse) < 1) return;
$linked_ids = [];
// get ids from text
foreach ($toParse as $val) {
$linked_ids = array_merge($linked_ids, dn_get_ids_from_links($val, $post_id));
}
// do the save
sync_connections($linked_ids, $post_id);
}
add_action('acf/save_post', 'my_acf_save_post'); // after save
// output modification (perhaps)
function acf_format_textarea ($value) {
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $value, $matches);
foreach ($matches[0] as $url) {
preg_match_all('/https?:\/\/(ns-doku\.test\/|localhost\:8080\/|doku\.n-kort\.net\/|dn-api(\.|-en\.)nsdoku\.de\/)/', $url, $hit);
if ($hit && isset($hit[0]) && isset($hit[0][0])) {
$new_url = str_replace($hit[0][0], '/', $url);
$value = str_replace($url, $new_url, $value);
}
}
return $value;
}
add_filter('acf/format_value/type=wysiwyg', 'acf_format_textarea', 10, 3);