Skip to content

Instantly share code, notes, and snippets.

@solaceten
Last active August 15, 2023 04:00
Show Gist options
  • Save solaceten/2d28f1679558404409921b4d8e87c115 to your computer and use it in GitHub Desktop.
Save solaceten/2d28f1679558404409921b4d8e87c115 to your computer and use it in GitHub Desktop.
<?php
// function to access node <package> which is above <hotel>
// https://www.wpallimport.com/documentation/code-snippets/#access-data-above-element-chosen-on-step-2
function wpai_pmxi_before_xml_import( $importID ) {
// output all activity to a debug log, so we can see where script is failing
defined('PDD_DEBUG') or define('PDD_DEBUG', 1); // set to 1 for ON
defined('PDD_LOG_FILE') or define('PDD_LOG_FILE', WP_CONTENT_DIR . '/errors-pdd.log');
if (PDD_DEBUG) { // << set ABOVE
// tell PHP to log errors */
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
ini_set('html_errors', TRUE);
ini_set('log_errors', TRUE);
ini_set('display_errors', TRUE);
ini_set('error_log', PDD_LOG_FILE);
}
if (PDD_DEBUG) {
error_log("-------------------------------------------\r\n" . PHP_EOL, 3, PDD_LOG_FILE);
}
if (PDD_DEBUG) {
error_log("1 Open the debug file" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Retrieve import object.
$import = new PMXI_Import_Record();
$import->getById( $importID );
// Ensure import object is valid.
if ( ! $import->isEmpty() ) {
if (PDD_DEBUG) {
error_log("2 import is ok" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Retrieve history file object.
$history_file = new PMXI_File_Record();
$history_file->getBy( 'import_id', $importID );
// Ensure history file object is valid.
if ( ! $history_file->isEmpty() ) {
if (PDD_DEBUG) {
error_log("3 history is ok" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Retrieve import file path.
$file_to_import = wp_all_import_get_absolute_path( $history_file->path );
// Load import file as SimpleXml.
$file = simplexml_load_file( $file_to_import );
// Check if Status is a child of Procurement.
// $query = $file->xpath( "//Apartment/Procurements[1]/Procurement[1]/Status[1]" );
// Check if package is already a child of hotels
$query = $file->xpath( "//flyer/hotels[1]/hotel[1]/package[1]" );
if ( ! empty( $query ) ) {
if (PDD_DEBUG) {
error_log("4 query is ok" . PHP_EOL, 3, PDD_LOG_FILE);
}
// If it is, do nothing.
return;
}
// Get Status value.
//$iquery = $file->xpath( "//Apartment/Status[1]" );
// Get package value.
$iquery = $file->xpath( "//flyer/packages[1]/package[1]" );
// Ensure value isn't empty.
if ( ! empty( $iquery ) ) {
if (PDD_DEBUG) {
error_log("5 value is ok" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Value of status as string.
//$status = $iquery[0]->__toString();
$package = $iquery[0]->__toString();
if (PDD_DEBUG) {
error_log("6 got package" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Target path.
//$new_query = $file->xpath( "./Procurements/Procurement" );
//$new_query = $file->xpath( "./hotels/hotel" );
//flyer/packages/package/hotels/hotel
$new_query = $file->xpath( "//flyer/packages/package/hotels/hotel" );
if (PDD_DEBUG) {
error_log("7 got new query" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Ensure path is valid.
if ( ! empty( $new_query ) ) {
if (PDD_DEBUG) {
error_log("8 path is ok" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Process each Procurement element.
foreach ( $new_query as $record ) {
if (PDD_DEBUG) {
error_log("----------------------------------------- \r\n" . PHP_EOL, 3, PDD_LOG_FILE);
// dump the array from previous step to make sure we are seeing it all
error_log("--- iquery= " . print_r($iquery, true) . " --\r" . PHP_EOL, 3, PDD_LOG_FILE);
}
if (PDD_DEBUG) {
error_log("9 foreach" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Ensure this element doesn't have Status (package)
if ( ! isset( $record->package ) ) {
if (PDD_DEBUG) {
error_log("10 -- is set package" . PHP_EOL, 3, PDD_LOG_FILE);
}
// Add {Status[1]} as child node.
// Add {package[1]} as child node.
$record->addChild( 'package', $package );
if (PDD_DEBUG) {
error_log("11 package add" . PHP_EOL, 3, PDD_LOG_FILE);
}
}
}
// Save updated file.
$updated_file = $file->asXML( $file_to_import );
if (PDD_DEBUG) {
error_log("12 update file" . PHP_EOL, 3, PDD_LOG_FILE);
}
}
}
}
}
}
add_action( 'pmxi_before_xml_import', 'wpai_pmxi_before_xml_import', 10, 1 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment