<?php
class WP_Term {
public $foo = 'foo';
public $data; // Change the visibility to protected or private and the script will throw an error.
}
$_POST['action'] = 'action';
function wp_parse_args( $args, $defaults = array() ) {
if ( is_object( $args ) ) {
$parsed_args = get_object_vars( $args );
} elseif ( is_array( $args ) ) {
$parsed_args =& $args;
} else {
wp_parse_str( $args, $parsed_args );
}
if ( is_array( $defaults ) && $defaults ) {
return array_merge( $defaults, $parsed_args );
}
return $parsed_args;
}
class WP_Ajax_Response {
public $responses = array();
public function add( $args = '' ) {
$defaults = array(
'what' => 'object',
'action' => false,
'id' => '0',
'old_id' => false,
'position' => 1,
'data' => '',
'supplemental' => array(),
);
$parsed_args = wp_parse_args( $args, $defaults );
$position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] );
$id = $parsed_args['id'];
$what = $parsed_args['what'];
$action = $parsed_args['action'];
$old_id = $parsed_args['old_id'];
$data = $parsed_args['data'];
$response = "<response_data><![CDATA[$data]]></response_data>";
$s = '';
if ( is_array( $parsed_args['supplemental'] ) ) {
foreach ( $parsed_args['supplemental'] as $k => $v ) {
$s .= "<$k><![CDATA[$v]]></$k>";
}
$s = "<supplemental>$s</supplemental>";
}
if ( false === $action ) {
$action = $_POST['action'];
}
$x = '';
$x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action.
$x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
$x .= $response;
$x .= $s;
$x .= "</$what>";
$x .= '</response>';
$this->responses[] = $x;
return $x;
}
}
$response = new WP_Ajax_Response();
$tag = new WP_Term();
$response->add(
array(
'what' => 'term',
'supplemental' => (array) $tag,
)
);
libxml_use_internal_errors( true );
$xml = simplexml_load_string( $response->responses[0], 'SimpleXMLElement' );
if ( $xml === false ) {
echo 'Failed loading XML: ';
foreach ( libxml_get_errors() as $error ) {
// Display each error
echo '<br>', $error->message . "\n";
}
// Clear the error buffer to free memory
libxml_clear_errors();
} else {
// Your XML is correctly parsed, proceed with your logic
echo 'XML loaded successfully.' . "\n";
}
preferences:
20.94 ms | 410 KiB | 5 Q