This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
semantic-linkbacks-microformats-handler.php
414 lines (355 loc) · 11.6 KB
/
semantic-linkbacks-microformats-handler.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
<?php
if(!class_exists("Mf2\Parser")) {
require_once 'Mf2/Parser.php';
}
use Mf2\Parser;
add_action('init', array('SemanticLinkbacksPlugin_MicroformatsHandler', 'init'));
/**
* provides a microformats handler for the semantic linkbacks
* WordPress plugin
*
* @author Matthias Pfefferle
*/
class SemanticLinkbacksPlugin_MicroformatsHandler {
/**
* initialize the plugin, registering WordPess hooks.
*/
public static function init() {
//
add_filter('semantic_linkbacks_commentdata', array('SemanticLinkbacksPlugin_MicroformatsHandler', 'generate_commentdata'), 1, 4);
}
/**
* all supported url types
*
* @return array
*/
public static function get_class_mapper() {
$class_mapper = array();
/*
* replies
* @link http://indiewebcamp.com/replies
*/
$class_mapper["in-reply-to"] = "reply";
$class_mapper["reply"] = "reply";
$class_mapper["reply-of"] = "reply";
/*
* repost
* @link http://indiewebcamp.com/repost
*/
$class_mapper["repost"] = "repost";
$class_mapper["repost-of"] = "repost";
/*
* likes
* @link http://indiewebcamp.com/likes
*/
$class_mapper["like"] = "like";
$class_mapper["like-of"] = "like";
/*
* favorite
* @link http://indiewebcamp.com/favorite
*/
$class_mapper["favorite"] = "favorite";
$class_mapper["favorite-of"] = "favorite";
/*
* mentions
* @link http://indiewebcamp.com/mentions
*/
//$class_mapper["mention"] = "mention";
/*
* rsvp
* @link http://indiewebcamp.com/rsvp
*/
$class_mapper["rsvp"] = "rsvp";
/*
* tag
* @link http://indiewebcamp.com/tag
*/
$class_mapper["tag-of"] = "tag";
return apply_filters("semantic_linkbacks_microformats_class_mapper", $class_mapper);
}
/**
* all supported url types
*
* @return array
*/
public static function get_rel_mapper() {
$rel_mapper = array();
/*
* replies
* @link http://indiewebcamp.com/in-reply-to
*/
$rel_mapper["in-reply-to"] = "reply";
$rel_mapper["reply-of"] = "reply";
return apply_filters("semantic_linkbacks_microformats_rel_mapper", $rel_mapper);
}
/**
* generate the comment data from the microformatted content
*
* @param WP_Comment $commentdata the comment object
* @param string $target the target url
* @param string $html the parsed html
*/
public static function generate_commentdata($commentdata, $target, $html) {
global $wpdb;
// add source
$source = $commentdata['comment_author_url'];
// parse source html
$parser = new Parser($html, $source);
$mf_array = $parser->parse(true);
// get all "relevant" entries
$entries = self::get_entries($mf_array);
if (empty($entries)) {
return array();
}
// get the entry of interest
$entry = self::get_representative_entry($entries, $target);
if (empty($entry)) {
return array();
}
// the entry properties
$properties = $entry['properties'];
// try to find some content
// @link http://indiewebcamp.com/comments-presentation
if (self::check_mf_attr('summary', $properties)) {
$commentdata['comment_content'] = wp_slash($properties['summary'][0]);
} elseif (self::check_mf_attr('content', $properties)) {
$commentdata['comment_content'] = wp_filter_kses($properties['content'][0]['html']);
} elseif (self::check_mf_attr('name', $properties)) {
$commentdata['comment_content'] = wp_slash($properties['name'][0]);
}
$commentdata['comment_content'] = trim($commentdata['comment_content']);
// set the right date
if (self::check_mf_attr('published', $properties)) {
$time = strtotime($properties['published'][0]);
$commentdata['comment_date'] = get_date_from_gmt(date("Y-m-d H:i:s", $time), 'Y-m-d H:i:s');
} elseif (self::check_mf_attr('updated', $properties)) {
$time = strtotime($properties['updated'][0]);
$commentdata['comment_date'] = get_date_from_gmt(date("Y-m-d H:i:s", $time), 'Y-m-d H:i:s');
}
$author = null;
// check if h-card has an author
if (isset($properties['author']) && isset($properties['author'][0]['properties'])) {
$author = $properties['author'][0]['properties'];
} else {
$author = self::get_representative_author($mf_array, $source);
}
// if author is present use the informations for the comment
if ($author) {
if (self::check_mf_attr('name', $author)) {
$commentdata['comment_author'] = wp_slash($author['name'][0]);
}
if (self::check_mf_attr('email', $author)) {
$commentdata['comment_author_email'] = wp_slash($author['email'][0]);
}
if (self::check_mf_attr('url', $author)) {
$commentdata['_author_url'] = esc_url_raw($author['url'][0]);
}
if (self::check_mf_attr('photo', $author)) {
$commentdata['_avatar'] = esc_url_raw($author['photo'][0]);
}
}
// set canonical url (u-url)
if (self::check_mf_attr('url', $properties)) {
$commentdata['_canonical'] = esc_url_raw($properties['url'][0]);
} else {
$commentdata['_canonical'] = esc_url_raw($source);
}
// check rsvp property
if (self::check_mf_attr('rsvp', $properties)) {
$commentdata['_type'] = wp_slash("rsvp:".$properties['rsvp'][0]);
} else {
// get post type
$commentdata['_type'] = wp_slash(self::get_entry_type($target, $entry, $mf_array));
}
return $commentdata;
}
/**
* get all h-entry items
*
* @param array $mf_array the microformats array
* @param array the h-entry array
*/
public static function get_entries($mf_array) {
$entries = array();
// some basic checks
if (!is_array($mf_array))
return $entries;
if (!isset($mf_array["items"]))
return $entries;
if (count($mf_array["items"]) == 0)
return $entries;
// get first item
$first_item = $mf_array["items"][0];
// check if it is an h-feed
if (isset($first_item['type']) && in_array("h-feed", $first_item["type"]) && isset($first_item['children'])) {
$mf_array["items"] = $first_item['children'];
}
// iterate array
foreach ($mf_array["items"] as $mf) {
if (isset($mf["type"]) && in_array("h-entry", $mf["type"])) {
$entries[] = $mf;
}
}
// return entries
return $entries;
}
/**
* helper to find the correct author node
*
* @param array $mf_array the parsed microformats array
* @param string $source the source url
* @return array|null the h-card node or null
*/
public static function get_representative_author($mf_array, $source) {
foreach ($mf_array["items"] as $mf) {
if (isset($mf["type"])) {
if (in_array("h-card", $mf["type"])) {
// check domain
if (isset($mf['properties']) && isset($mf['properties']['url'])) {
foreach ($mf['properties']['url'] as $url) {
if (parse_url($url, PHP_URL_HOST) == parse_url($source, PHP_URL_HOST)) {
return $mf['properties'];
break;
}
}
}
}
}
}
return null;
}
/**
* helper to find the correct h-entry node
*
* @param array $mf_array the parsed microformats array
* @param string $target the target url
* @return array the h-entry node or false
*/
public static function get_representative_entry($entries, $target) {
// iterate array
foreach ($entries as $entry) {
// check properties
if (isset($entry['properties'])) {
// check properties if target urls was mentioned
foreach ($entry['properties'] as $key => $values) {
// check "normal" links
if (self::compare_urls($target, $values)) {
return $entry;
}
// check included h-* formats and their links
foreach ($values as $obj) {
// check if reply is a "cite"
if (isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type'])) {
// check url
if (isset($obj['properties']) && isset($obj['properties']['url'])) {
// check target
if (self::compare_urls($target, $obj['properties']['url'])) {
return $entry;
}
}
}
}
}
// check properties if target urls was mentioned
foreach ($entry['properties'] as $key => $values) {
// check content for the link
if ($key == "content" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", $values[0]['html'], $context)) {
return $entry;
// check summary for the link
} elseif ($key == "summary" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", $values[0], $context)) {
return $entry;
}
}
}
}
// return first h-entry
return $entries[0];
}
/**
* check entry classes or document rels for post-type
*
* @param string $target the target url
* @param array $entry the represantative entry
* @param array $mf_array the document
* @return string the post-type
*/
public static function get_entry_type($target, $entry, $mf_array = array()) {
$classes = self::get_class_mapper();
// check properties for target-url
foreach ($entry['properties'] as $key => $values) {
// check u-* params
if (in_array($key, array_keys($classes))) {
// check "normal" links
if (self::compare_urls($target, $values)) {
return $classes[$key];
}
// iterate in-reply-tos
foreach ($values as $obj) {
// check if reply is a "cite" or "entry"
if (isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type'])) {
// check url
if (isset($obj['properties']) && isset($obj['properties']['url'])) {
// check target
if (self::compare_urls($target, $obj['properties']['url'])) {
return $classes[$key];
}
}
}
}
}
}
// check if site has any rels
if (!isset($mf_array['rels'])) {
return "mention";
}
$rels = self::get_rel_mapper();
// check rels for target-url
foreach ($mf_array['rels'] as $key => $values) {
// check rel params
if (in_array($key, array_keys($rels))) {
foreach ($values as $value) {
if ($value == $target) {
return $rels[$key];
}
}
}
}
return "mention";
}
/**
* checks if $node has $key
*
* @param string $key the array key to check
* @param array $node the array to be checked
*
* @return boolean
*/
public static function check_mf_attr($key, $node) {
if (isset($node[$key]) && isset($node[$key][0])) {
return true;
}
return false;
}
/**
* compare an url with a list of urls
*
* @param string $needle the target url
* @param array $haystack a list of urls
* @param boolean $schemelesse define if the target url should be checked
* with http:// and https://
* @return boolean
*/
public static function compare_urls($needle, $haystack, $schemeless = true) {
if ($schemeless === true) {
// remove url-scheme
$schemeless_target = preg_replace("/^https?:\/\//i", "", $needle);
// add both urls to the needle
$needle = array("http://".$schemeless_target, "https://".$schemeless_target);
} else {
// make $needle an array
$needle = array($needle);
}
// compare both arrays
return array_intersect($needle, $haystack);
}
}