-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathwpallimport_xml_row.php
More file actions
94 lines (79 loc) · 2.67 KB
/
wpallimport_xml_row.php
File metadata and controls
94 lines (79 loc) · 2.67 KB
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
<?php
/**
* ==================================
* Filter: wpallimport_xml_row
* ==================================
*
* PLEASE NOTE: The XML generated by this function cannot be used in the Advanced Custom Fields Add-On's repeater "For each" fields:
* https://d.pr/i/pT8hIE
*
*
* Allows reading or modification of the data record before importing
*
* Note that this isn't just for XML imports. Data records from other formats (CSV, JSON,
* Excel) are converted to SimpleXML objects internally for processing.
*
* @param $xml_node SimpleXMLElement - An object holding values for the current record
*
* @return SimpleXMLElement
*/
function wpai_xml_row($xml_node)
{
// Modify simpleXML object as needed
return $xml_node;
}
add_filter('wpallimport_xml_row', 'wpai_xml_row', 10, 1);
// ----------------------------
// Example uses below
// ----------------------------
/**
* Example of adding a child element called "title"
*
*/
function add_title_node($node)
{
$result = $node->xpath('//mydata[1]/*[1]');
if (!empty($result[0])) {
$name = $result[0]->getName();
$node->addChild('title', $name);
}
return $node;
}
add_filter('wpallimport_xml_row', 'add_title_node', 10, 1);
/**
* Example of converting HTML or XML embeded within a specific tag into HTML usable for import
*
*/
function parse_content($node){
$result = $node->xpath('//content'); // replace this with the XPath of the node
if (!empty($result[0])) {
// Optional replacements to convert custom XML tags to HTML equivalent
$find_xml = array('section_title','section_content','section', 'texteparagraphe','titreparagraphe');
$replace_html = array('h1','p','div','p','h2');
$html = str_replace($find_xml, $replace_html, $result[0]->asXML());
$node->addChild('content_html', $html);
}
return $node;
}
add_filter('wpallimport_xml_row', 'parse_content', 10, 1);
/**
* Example getting the tag names "Commerciale" or "Residenziale" when they
* don't contain any data. These can be called from the import using
* {TYPE[1]}
*/
function add_property_type($node) {
// Selects Commerciale in file
$commerciale = $node->xpath('//Commerciale[1]');
// If Commerciale exists add <TYPE>Commerciale</TYPE> to entry
if (!empty($commerciale[0])) {
$node->addChild('TYPE', 'Commerciale');
};
// Selects Residenziale in file
$residenziale = $node->xpath('//Residenziale[1]');
// If Residenziale exists add <TYPE>Residenziale</TYPE> to entry
if (!empty($residenziale[0])) {
$node->addChild('TYPE', 'Residenziale');
};
return $node;
};
add_filter('wpallimport_xml_row', 'add_property_type', 10, 1);