-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkirbytextWrap.php
78 lines (70 loc) · 1.85 KB
/
kirbytextWrap.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
<?php
/**
* @package yoanmalie/kirbytextwrap
* @version 1.2.0
* @license MIT
* @author Yoan Malié <[email protected]>
*/
use Kirby\Toolkit\Html as Html;
/**
* Check for Kirby version
* @param int $v
* Kirby version targeted
* @return false/true
*/
function isVersion($v)
{
return Str::startsWith(Kirby::version(), strval($v));
}
// Kirby 2 plugin definition
if (isVersion(2)) {
field::$methods['kirbytextWrap'] = field::$methods['ktw'] = function (
$field,
$tag = false,
$attr = []
) {
return kirbytextWrap($field, $tag, $attr);
};
}
// Kirby 3 plugin definition
if (isVersion(3)) {
Kirby::plugin('yoanmalie/kirbytextwrap', [
'fieldMethods' => [
'kirbytextWrap' => function ($field, $tag = false, $attr = []) {
return kirbytextWrap($field, $tag, $attr);
},
'ktw' => function ($field, $tag = false, $attr = []) {
return kirbytextWrap($field, $tag, $attr);
},
],
]);
}
/**
* Plugin core
* @param object $field
* Kirby field object
* @param string $tag
* A new enclosing tag
* @param string|array $attr
* Pass HTML attributes and their values to the new tag
* @return mixed
*/
function kirbytextWrap($field, $tag, $attr)
{
$text = $field->kirbytext();
if (function_exists('kirbytextinline')) {
// Remove <p> tag natively
$text = $field->kirbytextinline();
} else {
// Remove <p> tag manually
if (Str::startsWith($text, '<p>') && Str::endsWith($text, '</p>')) {
$text = Str::between($text, '<p>', '</p>');
}
}
// Add new tag and his attributes
if ($tag) {
$text = isVersion(2) ? brick($tag, $text, $attr) : $text;
$text = isVersion(3) ? Html::tag($tag, $text, $attr) : $text;
}
return $text;
}