Last active
September 17, 2021 11:48
-
-
Save propertyhive/004f290ad80df9d86e462919011f7a6e to your computer and use it in GitHub Desktop.
Add new nodes to Kyero XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NEW: Inserts fields at specific position in XML | |
function simplexml_insert_after(SimpleXMLElement $insert, SimpleXMLElement $target) | |
{ | |
$target_dom = dom_import_simplexml($target); | |
$insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true); | |
if ($target_dom->nextSibling) { | |
return $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling); | |
} else { | |
return $target_dom->parentNode->appendChild($insert_dom); | |
} | |
} | |
add_filter( 'ph_kyero_property_values', 'custom_kyero', 10, 3 ); | |
function custom_kyero($property_xml, $post_id, $portal_id) | |
{ | |
$property = new PH_Property($post_id); | |
$insert = new SimpleXMLElement("<pool>" . $property->pool . "</pool>"); | |
// Get the last nodeA element | |
$target = current($property_xml->xpath('//baths[last()]')); | |
if ( $target === false ) | |
{ | |
$target = current($property_xml->xpath('//beds[last()]')); | |
} | |
simplexml_insert_after($insert, $target); | |
$insert = new SimpleXMLElement("<surface_area><built>" . $property->build . "</built><plot>" . $property->plot . "</plot></surface_area>"); | |
// Get the last nodeA element | |
$target = current($property_xml->xpath('//pool[last()]')); | |
// Insert the new element after the last nodeA | |
simplexml_insert_after($insert, $target); | |
return $property_xml; | |
} | |
// OLD: Just appends fields to end | |
add_filter( 'ph_kyero_property_values', 'custom_kyero', 10, 3 ); | |
function custom_kyero($property_xml, $post_id, $portal_id) | |
{ | |
$property = new PH_Property($post_id); | |
$property_xml->addChild('pool'); | |
$property_xml->pool = ( $property->pool == 'Yes' ? 1 : 0 ); | |
$surface_area_xml = $property_xml->addChild('surface_area'); | |
$surface_area_xml->addChild('built'); | |
$surface_area_xml->built = $property->build; | |
$surface_area_xml->addChild('plot'); | |
$surface_area_xml->plot = $property->plot; | |
return $property_xml; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment