-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeta.php
79 lines (71 loc) · 2.06 KB
/
Meta.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
/**
* Transform rules for Wordpress Meta Elements into Kirby content file fields.
*
* @version 0.1.0 2020-01-21
* @license WTFPL 2.0
* @author Rene Serradeil <[email protected]>
*/
namespace WebMechanic\Converter;
use DOMNode;
use WebMechanic\Converter\Wordpress\Item;
/**
* Transform mappings of wp:postmeta fields into specific content fields
* of various output files. Defaults to writing to the generated page.
*
* Allow for customisation of WP plugin meta fields (ie galleries) into
* something a Kirby instance might make use of.
*
* @see Transform
*/
class Meta extends Transform
{
/**
* @see apply()
* @var null a list of Closures to call during apply()
*/
private $handler = [];
/**
* Called on `<wp:postmeta>` and it's childNodes <wp:meta_key>, <wp:meta_value>
* Override because Meta does not use a Closure for the transform
* but some more complex rules.
*
* Known meta_key:
* - _wp_page_template may become a blueprint filename in Kirby
* - _wp_attached_file relative file path
* - _wp_attachment_metadata serialised array with image meta data
* - _edit_last edit history count?
* - seo_follow false|true ~ rel follow|nofollow
* - seo_noindex false|true ~ rel index|noindex
* - "Custom Fieldname"
*
* @param DOMNode $node
* @param Item|null $post
* @see addHandler()
* @see pageTemplate(), seoFollow(), seoNoindex()
*/
function apply(DOMNode $node, Item $post = null): void
{
if ($node->nodeName !== 'wp:postmeta') {
return;
}
$key = $node->firstChild->textContent;
if (isset($this->handler[$key])) {
if (is_callable($this->handler[$key])) {
$callback = $this->handler[$key];
$callback($node, $post);
return;
}
}
/* is there a public setter? */
$method = 'set' . ucwords($key, '_');
$method = str_replace(array('_wp_', '_'), '', $method);
if (method_exists($post, $method)) {
$post->$method($node);
}
}
public function addHandler($key, \Closure $handler)
{
$this->handler[$key] = $handler;
}
}