| 1 |
<?php |
| 2 |
/** |
| 3 |
* XML Parser Actions |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Actions; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class XML_Parser_Actions |
| 16 |
*/ |
| 17 |
class XML_Parser_Actions extends Action { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'xml_parser'; |
| 24 |
$this->group = __( 'XML Parser', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Execute the action |
| 29 |
*/ |
| 30 |
public function execute( $action_data, $trigger_data ) { |
| 31 |
$action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; |
| 32 |
|
| 33 |
if ( empty( $action_id ) ) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
switch ( $action_id ) { |
| 38 |
case 'parse_xml_file': |
| 39 |
return $this->parse_xml_file( $action_data, $trigger_data ); |
| 40 |
default: |
| 41 |
return false; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Parse XML file URL into JSON data |
| 47 |
*/ |
| 48 |
private function parse_xml_file( $config, $trigger_data ) { |
| 49 |
$config_values = isset( $config['config'] ) ? $config['config'] : array(); |
| 50 |
|
| 51 |
$xml_url = isset( $config_values['xml_url'] ) ? $this->parse_tokens( $config_values['xml_url'], $trigger_data ) : ''; |
| 52 |
|
| 53 |
if ( empty( $xml_url ) ) { |
| 54 |
return array( |
| 55 |
'success' => false, |
| 56 |
'message' => 'XML URL is required.', |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
$response = wp_remote_get( $xml_url, array( 'timeout' => 45 ) ); |
| 61 |
|
| 62 |
if ( is_wp_error( $response ) ) { |
| 63 |
return array( |
| 64 |
'success' => false, |
| 65 |
'message' => $response->get_error_message(), |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 70 |
$xml_content = wp_remote_retrieve_body( $response ); |
| 71 |
|
| 72 |
if ( $response_code < 200 || $response_code >= 300 ) { |
| 73 |
return array( |
| 74 |
'success' => false, |
| 75 |
'message' => sprintf( 'Failed to fetch XML. Response Code: %d', $response_code ), |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
if ( empty( $xml_content ) ) { |
| 80 |
return array( |
| 81 |
'success' => false, |
| 82 |
'message' => 'The XML file is empty.', |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
// Parse XML |
| 87 |
// Use LIBXML_NOCDATA to merge CDATA as text nodes. |
| 88 |
libxml_use_internal_errors(true); |
| 89 |
$xml = simplexml_load_string( $xml_content, 'SimpleXMLElement', LIBXML_NOCDATA ); |
| 90 |
|
| 91 |
if ( false === $xml ) { |
| 92 |
$errors = libxml_get_errors(); |
| 93 |
$message = 'Failed to parse XML content.'; |
| 94 |
if ( ! empty( $errors ) ) { |
| 95 |
$message .= ' Error: ' . $errors[0]->message; |
| 96 |
} |
| 97 |
libxml_clear_errors(); |
| 98 |
return array( |
| 99 |
'success' => false, |
| 100 |
'message' => $message, |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
// Convert SimpleXMLElement to associative array |
| 105 |
$json = wp_json_encode( $xml ); |
| 106 |
$data = json_decode( $json, true ); |
| 107 |
|
| 108 |
return array( |
| 109 |
'success' => true, |
| 110 |
'data' => $data, |
| 111 |
'message' => 'XML parsed successfully.', |
| 112 |
); |
| 113 |
} |
| 114 |
} |
| 115 |
|