-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.php
132 lines (118 loc) · 3.19 KB
/
plugin.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
<?php
class PulponairSemanticImages extends KokenPlugin {
const PROPERTY_SETTING_PREFIX = 'property_';
/**
* Item property map
*
* @var array
*/
protected $itemPropertyMap = array(
'dateCreated' => 'image/captured_on/timestamp',
'datePublished' => 'image/published_on/timestamp',
'author' => 'profile/name',
);
/**
* Constructor registers filter
*
*/
public function __construct() {
$this->require_setup = true;
$this->register_filter('site.output', 'render');
}
/**
* The actual render method. Searches for image tags and appends semantinc tags.
*
* @param string $content
* @return string mixed
*/
public function render($content) {
$pattern = '/<img.*data-base=".*\/(.*)\/.*,.*?".+?>/';
$imageCount = preg_match_all($pattern, $content, $matches);
if ($imageCount) {
$context = array(
'profile' => Koken::$profile
);
$search = array();
$replace = array();
for ($i = 0; $i < $imageCount; $i++) {
$search[] = $matches[0][$i];
$context['image'] = Koken::api('/content/index/' . (int)$matches[1][$i]);
$replace[] = $this->wrapByItemScopeTag(
$matches[0][$i] .
$this->mapContextToItemPropertiesTags($context) ,
'ImageObject');
}
$content = str_replace($search, $replace, $content);
}
return $content;
}
/**
* Maps the context to item property tags. Taking the configuration into account
*
* @param array $context
* @return string
*/
protected function mapContextToItemPropertiesTags($context) {
$result = '';
foreach($this->data as $key => $value) {
if (strpos($key, self::PROPERTY_SETTING_PREFIX) !== 0) {
continue;
}
if ($value) {
$itemProperty = substr($key, strlen(self::PROPERTY_SETTING_PREFIX));
if ($path = $this->itemPropertyMap[$itemProperty]) {
$content = $this->getArrayElementByPath($context, $path);
} else {
$content = $this->getArrayElementByPath($context, $value);
}
if (strpos($itemProperty,'date') === 0) {
$content = date('Y-m-d', $content);
}
if (!empty($content)) {
$result .= $this->createItemPropertyTag($itemProperty, $content);
}
}
}
return $result;
}
/**
* Gets an array element by path
*
* @param array $source
* @param string $path
* @param string $pathDelimiter
* @return mixed
*/
protected function getArrayElementByPath($source, $path, $pathDelimiter = '/') {
$explodedPath = explode($pathDelimiter, $path);
$pointer = &$source;
foreach ($explodedPath as $segment) {
$pointer = &$pointer[$segment];
}
return $pointer;
}
/**
* Creates an item property tag.
*
* @param string $property
* @param string $content
* @param string $tag
* @return string
*/
protected function createItemPropertyTag($property, $content, $tag = 'meta') {
return '<' . $tag . ' itemprop="' . $property . '" content="' . $content . '" />';
}
/**
* Wraps given content in an schema.org item scope
*
* @param string $content
* @param sting $itemType
* @param string $tag
* @return string
*/
protected function wrapByItemScopeTag($content, $itemType, $tag = 'span') {
return '<' . $tag . ' itemscope itemtype="http://schema.org/' . $itemType . '">' .
$content .
'</' . $tag . '>';
}
}