File "class-wc-api-xml-handler.php"
Full path: /home/kosmetik/public_html/wp-content/plugins/woocommerce/includes/legacy/api/v1/class-wc-api-xml-handler.php
File
size: 6.17 B
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Editor Back
<?php
if (!defined('ABSPATH')) {
exit;
}
class WC_API_XML_Handler implements WC_API_Handler
{
private $xml;
public function __construct()
{
add_filter('woocommerce_api_report_response', array($this, 'format_sales_report_data'), 100);
add_filter('woocommerce_api_product_response', array($this, 'format_product_data'), 100);
}
public function get_content_type()
{
return 'application/xml; charset=' . get_option('blog_charset');
}
public function parse_body($data)
{
}
public function generate_response($data)
{
$this->xml = new XMLWriter();
$this->xml->openMemory();
$this->xml->setIndent(true);
$this->xml->startDocument('1.0', 'UTF-8');
$root_element = key($data);
$data = $data[$root_element];
switch ($root_element) {
case 'orders':
$data = array('order' => $data);
break;
case 'order_notes':
$data = array('order_note' => $data);
break;
case 'customers':
$data = array('customer' => $data);
break;
case 'coupons':
$data = array('coupon' => $data);
break;
case 'products':
$data = array('product' => $data);
break;
case 'product_reviews':
$data = array('product_review' => $data);
break;
default:
$data = apply_filters('woocommerce_api_xml_data', $data, $root_element);
break;
}
$this->array_to_xml($root_element, $data);
$this->xml->endDocument();
return $this->xml->outputMemory();
}
private function array_to_xml($element_key, $element_value = array())
{
if (is_array($element_value)) {
if ('@attributes' === $element_key) {
foreach ($element_value as $attribute_key => $attribute_value) {
$this->xml->startAttribute($attribute_key);
$this->xml->text($attribute_value);
$this->xml->endAttribute();
}
return;
}
if (is_numeric(key($element_value))) {
foreach ($element_value as $child_element_key => $child_element_value) {
$this->xml->startElement($element_key);
foreach ($child_element_value as $sibling_element_key => $sibling_element_value) {
$this->array_to_xml($sibling_element_key, $sibling_element_value);
}
$this->xml->endElement();
}
} else {
$this->xml->startElement($element_key);
foreach ($element_value as $child_element_key => $child_element_value) {
$this->array_to_xml($child_element_key, $child_element_value);
}
$this->xml->endElement();
}
} else {
if ('@value' == $element_key) {
$this->xml->text($element_value);
} else {
if (false !== strpos($element_value, '<') || false !== strpos($element_value, '>')) {
$this->xml->startElement($element_key);
$this->xml->writeCdata($element_value);
$this->xml->endElement();
} else {
$this->xml->writeElement($element_key, $element_value);
}
}
return;
}
}
public function format_sales_report_data($data)
{
if (!empty($data['totals'])) {
foreach ($data['totals'] as $date => $totals) {
unset($data['totals'][$date]);
$data['totals'][] = array_merge(array('@attributes' => array('date' => $date)), $totals);
}
}
return $data;
}
public function format_product_data($data)
{
if (!empty($data['attributes'])) {
foreach ($data['attributes'] as $attribute_key => $attribute) {
if (!empty($attribute['options']) && is_array($attribute['options'])) {
foreach ($attribute['options'] as $option_key => $option) {
unset($data['attributes'][$attribute_key]['options'][$option_key]);
$data['attributes'][$attribute_key]['options']['option'][] = array($option);
}
}
}
}
$fields_to_fix = array('related_ids' => 'related_id', 'upsell_ids' => 'upsell_id', 'cross_sell_ids' => 'cross_sell_id', 'categories' => 'category', 'tags' => 'tag');
foreach ($fields_to_fix as $parent_field_name => $child_field_name) {
if (!empty($data[$parent_field_name])) {
foreach ($data[$parent_field_name] as $field_key => $field) {
unset($data[$parent_field_name][$field_key]);
$data[$parent_field_name][$child_field_name][] = array($field);
}
}
}
if (!empty($data['parent'])) {
if (!empty($data['parent']['attributes'])) {
foreach ($data['parent']['attributes'] as $attribute_key => $attribute) {
if (!empty($attribute['options']) && is_array($attribute['options'])) {
foreach ($attribute['options'] as $option_key => $option) {
unset($data['parent']['attributes'][$attribute_key]['options'][$option_key]);
$data['parent']['attributes'][$attribute_key]['options']['option'][] = array($option);
}
}
}
}
foreach ($fields_to_fix as $parent_field_name => $child_field_name) {
if (!empty($data['parent'][$parent_field_name])) {
foreach ($data['parent'][$parent_field_name] as $field_key => $field) {
unset($data['parent'][$parent_field_name][$field_key]);
$data['parent'][$parent_field_name][$child_field_name][] = array($field);
}
}
}
}
return $data;
}
}