| 1 |
<?php |
| 2 |
/** |
| 3 |
* Data Transformer Actions |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Actions; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
// File: includes/class-dts-transformer.php |
| 14 |
|
| 15 |
class Data_Transformer extends Action { |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
$this->id = 'data_transformer'; |
| 19 |
$this->group = __( 'Data Transformer', 'wpbot-automator' ); |
| 20 |
} |
| 21 |
|
| 22 |
public function execute( $action_data, $trigger_data ) { |
| 23 |
$action_id = $action_data['actionId'] ?? ''; |
| 24 |
|
| 25 |
if ( empty( $action_id ) ) { |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
$config_values = $action_data['config'] ?? []; |
| 30 |
$value = $this->parse_tokens( $config_values['value'] ?? '', $trigger_data ); |
| 31 |
$secret = $this->parse_tokens( $config_values['secret'] ?? '', $trigger_data ); |
| 32 |
|
| 33 |
return Data_Transformer::run( $action_id, $value, [ 'secret' => $secret ] ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Run a transformation action on a value. |
| 38 |
* |
| 39 |
* @param string $action One of the supported action slugs. |
| 40 |
* @param string $value The input string. |
| 41 |
* @param array $options Extra options (e.g. secret key for HMAC). |
| 42 |
* @return array { success: bool, output: string, error: string } |
| 43 |
*/ |
| 44 |
public static function run( string $action, string $value, array $options = [] ): array { |
| 45 |
try { |
| 46 |
$output = match( $action ) { |
| 47 |
'html_encode' => htmlspecialchars( $value, ENT_QUOTES | ENT_HTML5, 'UTF-8' ), |
| 48 |
'html_decode' => htmlspecialchars_decode( $value, ENT_QUOTES | ENT_HTML5 ), |
| 49 |
'base64_encode' => base64_encode( $value ), |
| 50 |
'base64_decode' => self::safe_base64_decode( $value ), |
| 51 |
'sha256' => hash( 'sha256', $value ), |
| 52 |
'sha1' => hash( 'sha1', $value ), |
| 53 |
'sha512' => hash( 'sha512', $value ), |
| 54 |
'hmac_sha256' => hash_hmac( 'sha256', $value, $options['secret'] ?? '' ), |
| 55 |
'md5' => md5( $value ), |
| 56 |
'strip_tags' => wp_strip_all_tags( $value ), |
| 57 |
default => throw new InvalidArgumentException( "Unknown action: {$action}" ), |
| 58 |
}; |
| 59 |
|
| 60 |
return [ 'success' => true, 'output' => $output, 'error' => '' ]; |
| 61 |
|
| 62 |
} catch ( Throwable $e ) { |
| 63 |
return [ 'success' => false, 'output' => '', 'error' => $e->getMessage() ]; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
private static function safe_base64_decode( string $value ): string { |
| 68 |
$decoded = base64_decode( $value, true ); |
| 69 |
if ( $decoded === false ) { |
| 70 |
throw new RuntimeException( 'Invalid Base64 string.' ); |
| 71 |
} |
| 72 |
return $decoded; |
| 73 |
} |
| 74 |
|
| 75 |
// File: qcld_wpbot_autometor |
| 76 |
|
| 77 |
public static function register(): void { |
| 78 |
add_action( 'rest_api_init', [ __CLASS__, 'register_routes' ] ); |
| 79 |
} |
| 80 |
|
| 81 |
public static function register_routes(): void { |
| 82 |
register_rest_route( 'qwautomator/v1', '/transform', [ |
| 83 |
'methods' => 'POST', |
| 84 |
'callback' => [ __CLASS__, 'handle' ], |
| 85 |
'permission_callback' => [ __CLASS__, 'auth' ], |
| 86 |
'args' => [ |
| 87 |
'action' => [ 'required' => true, 'type' => 'string' ], |
| 88 |
'value' => [ 'required' => true, 'type' => 'string' ], |
| 89 |
'secret' => [ 'required' => false, 'type' => 'string', 'default' => '' ], |
| 90 |
], |
| 91 |
] ); |
| 92 |
} |
| 93 |
|
| 94 |
public static function handle( WP_REST_Request $req ): WP_REST_Response { |
| 95 |
$result = Data_Transformer::run( |
| 96 |
$req->get_param( 'action' ), |
| 97 |
$req->get_param( 'value' ), |
| 98 |
[ 'secret' => $req->get_param( 'secret' ) ] |
| 99 |
); |
| 100 |
|
| 101 |
$status = $result['success'] ? 200 : 422; |
| 102 |
return new WP_REST_Response( $result, $status ); |
| 103 |
} |
| 104 |
|
| 105 |
public static function auth(): bool { |
| 106 |
return current_user_can( 'manage_options' ) |
| 107 |
|| self::valid_api_key(); |
| 108 |
} |
| 109 |
|
| 110 |
private static function valid_api_key(): bool { |
| 111 |
$key = get_option( 'dts_api_key' ); |
| 112 |
$header = sanitize_text_field( $_SERVER['HTTP_X_DTS_KEY'] ?? '' ); |
| 113 |
return $key && hash_equals( $key, $header ); |
| 114 |
} |
| 115 |
} |
| 116 |
// File: includes/class-dts-workflow-step.php |
| 117 |
|
| 118 |
// Use this when your workflow engine fires an action with step data. |
| 119 |
|
| 120 |
add_filter( 'wpbot_automator_execute_step', function( array $step_result, array $step_config ) { |
| 121 |
if ( ( $step_config['type'] ?? '' ) !== 'data_transformer' ) { |
| 122 |
return $step_result; |
| 123 |
} |
| 124 |
|
| 125 |
$action = $step_config['action'] ?? ''; |
| 126 |
// Resolve dynamic variable from previous step output |
| 127 |
$value = dts_resolve_variable( $step_config['value'], $step_result['context'] ); |
| 128 |
$secret = dts_resolve_variable( $step_config['secret'] ?? '', $step_result['context'] ); |
| 129 |
|
| 130 |
$result = Data_Transformer::run( $action, $value, compact( 'secret' ) ); |
| 131 |
|
| 132 |
$step_result['output'] = $result['output']; |
| 133 |
$step_result['success'] = $result['success']; |
| 134 |
$step_result['error'] = $result['error']; |
| 135 |
|
| 136 |
return $step_result; |
| 137 |
}, 10, 2 ); |