| @@ -1,128 +1,128 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Abstract Action Class | |
| 4 | - * | |
| 5 | - * @package WPbot_Automator | |
| 6 | - */ | |
| 7 | - | |
| 8 | -namespace WPbot_Automator\Actions; | |
| 9 | - | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit; | |
| 12 | -} | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * Abstract Class Action | |
| 16 | - */ | |
| 17 | -abstract class Action { | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Action ID | |
| 21 | - * | |
| 22 | - * @var string | |
| 23 | - */ | |
| 24 | - protected $id; | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Action Title | |
| 28 | - * | |
| 29 | - * @var string | |
| 30 | - */ | |
| 31 | - protected $title; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * Action Group | |
| 35 | - * | |
| 36 | - * @var string | |
| 37 | - */ | |
| 38 | - protected $group; | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Get Action ID | |
| 42 | - */ | |
| 43 | - public function get_id() { | |
| 44 | - return $this->id; | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Get Action Title | |
| 49 | - */ | |
| 50 | - public function get_title() { | |
| 51 | - return $this->title; | |
| 52 | - } | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Get Action Group | |
| 56 | - */ | |
| 57 | - public function get_group() { | |
| 58 | - return $this->group; | |
| 59 | - } | |
| 60 | - | |
| 61 | - /** | |
| 62 | - * Execute the action | |
| 63 | - * | |
| 64 | - * @param array $action_data Configuration for this action from the workflow. | |
| 65 | - * @param array $trigger_data Data passed from the trigger. | |
| 66 | - * @return bool|array Success or data to pass to next node. | |
| 67 | - */ | |
| 68 | - abstract public function execute( $action_data, $trigger_data ); | |
| 69 | - | |
| 70 | - /** | |
| 71 | - * Parse tokens in string | |
| 72 | - * | |
| 73 | - * @param string $string The string containing tokens like {key} or {key.subkey}. | |
| 74 | - * @param array $data The trigger data to pull values from. | |
| 75 | - * @return string | |
| 76 | - */ | |
| 77 | - protected function parse_tokens( $string, $data ) { | |
| 78 | - $result = (string) $string; | |
| 79 | - if ( ! is_array( $data ) || empty( $result ) ) { | |
| 80 | - return $result; | |
| 81 | - } | |
| 82 | - | |
| 83 | - foreach ( $data as $key => $value ) { | |
| 84 | - if ( is_string( $value ) || is_numeric( $value ) ) { | |
| 85 | - $result = str_replace( '{' . $key . '}', $value, $result ); | |
| 86 | - $result = str_replace( '{{' . $key . '}}', $value, $result ); | |
| 87 | - } elseif ( is_array( $value ) ) { | |
| 88 | - // Flatten arrays for tokens (e.g., {fields.1.value}) | |
| 89 | - $flattened = $this->flatten_array( $value, $key ); | |
| 90 | - foreach ( $flattened as $flat_key => $flat_value ) { | |
| 91 | - if ( is_string( $flat_value ) || is_numeric( $flat_value ) ) { | |
| 92 | - $result = str_replace( '{' . $flat_key . '}', $flat_value, $result ); | |
| 93 | - $result = str_replace( '{{' . $flat_key . '}}', $flat_value, $result ); | |
| 94 | - } | |
| 95 | - } | |
| 96 | - // Also allow replacing the base key with JSON if it's still present in the result. | |
| 97 | - if ( strpos( $result, '{' . $key . '}' ) !== false || strpos( $result, '{{' . $key . '}}' ) !== false ) { | |
| 98 | - $json_val = wp_json_encode( $value ); | |
| 99 | - $result = str_replace( '{' . $key . '}', $json_val, $result ); | |
| 100 | - $result = str_replace( '{{' . $key . '}}', $json_val, $result ); | |
| 101 | - } | |
| 102 | - } | |
| 103 | - } | |
| 104 | - | |
| 105 | - //error_log( sprintf( 'WPbot Automator - Token Parse - Input: "%s", Result: "%s"', $string, $result ) ); | |
| 106 | - return $result; | |
| 107 | - } | |
| 108 | - | |
| 109 | - /** | |
| 110 | - * Flatten a multi-dimensional array into a single level with dot notation keys | |
| 111 | - * | |
| 112 | - * @param array $array The array to flatten. | |
| 113 | - * @param string $prefix Prefix for the keys. | |
| 114 | - * @return array | |
| 115 | - */ | |
| 116 | - private function flatten_array( $array, $prefix = '' ) { | |
| 117 | - $result = array(); | |
| 118 | - foreach ( $array as $key => $value ) { | |
| 119 | - $new_key = $prefix ? $prefix . '.' . $key : $key; | |
| 120 | - if ( is_array( $value ) ) { | |
| 121 | - $result = array_merge( $result, $this->flatten_array( $value, $new_key ) ); | |
| 122 | - } else { | |
| 123 | - $result[ $new_key ] = $value; | |
| 124 | - } | |
| 125 | - } | |
| 126 | - return $result; | |
| 127 | - } | |
| 128 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Abstract Action Class | |
| 4 | + * | |
| 5 | + * @package WPbot_Automator | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace WPbot_Automator\Actions; | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Abstract Class Action | |
| 16 | + */ | |
| 17 | +abstract class Action { | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Action ID | |
| 21 | + * | |
| 22 | + * @var string | |
| 23 | + */ | |
| 24 | + protected $id; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Action Title | |
| 28 | + * | |
| 29 | + * @var string | |
| 30 | + */ | |
| 31 | + protected $title; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Action Group | |
| 35 | + * | |
| 36 | + * @var string | |
| 37 | + */ | |
| 38 | + protected $group; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Get Action ID | |
| 42 | + */ | |
| 43 | + public function get_id() { | |
| 44 | + return $this->id; | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Get Action Title | |
| 49 | + */ | |
| 50 | + public function get_title() { | |
| 51 | + return $this->title; | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Get Action Group | |
| 56 | + */ | |
| 57 | + public function get_group() { | |
| 58 | + return $this->group; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Execute the action | |
| 63 | + * | |
| 64 | + * @param array $action_data Configuration for this action from the workflow. | |
| 65 | + * @param array $trigger_data Data passed from the trigger. | |
| 66 | + * @return bool|array Success or data to pass to next node. | |
| 67 | + */ | |
| 68 | + abstract public function execute( $action_data, $trigger_data ); | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Parse tokens in string | |
| 72 | + * | |
| 73 | + * @param string $string The string containing tokens like {key} or {key.subkey}. | |
| 74 | + * @param array $data The trigger data to pull values from. | |
| 75 | + * @return string | |
| 76 | + */ | |
| 77 | + protected function parse_tokens( $string, $data ) { | |
| 78 | + $result = (string) $string; | |
| 79 | + if ( ! is_array( $data ) || empty( $result ) ) { | |
| 80 | + return $result; | |
| 81 | + } | |
| 82 | + | |
| 83 | + foreach ( $data as $key => $value ) { | |
| 84 | + if ( is_string( $value ) || is_numeric( $value ) ) { | |
| 85 | + $result = str_replace( '{' . $key . '}', $value, $result ); | |
| 86 | + $result = str_replace( '{{' . $key . '}}', $value, $result ); | |
| 87 | + } elseif ( is_array( $value ) ) { | |
| 88 | + // Flatten arrays for tokens (e.g., {fields.1.value}) | |
| 89 | + $flattened = $this->flatten_array( $value, $key ); | |
| 90 | + foreach ( $flattened as $flat_key => $flat_value ) { | |
| 91 | + if ( is_string( $flat_value ) || is_numeric( $flat_value ) ) { | |
| 92 | + $result = str_replace( '{' . $flat_key . '}', $flat_value, $result ); | |
| 93 | + $result = str_replace( '{{' . $flat_key . '}}', $flat_value, $result ); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + // Also allow replacing the base key with JSON if it's still present in the result. | |
| 97 | + if ( strpos( $result, '{' . $key . '}' ) !== false || strpos( $result, '{{' . $key . '}}' ) !== false ) { | |
| 98 | + $json_val = wp_json_encode( $value ); | |
| 99 | + $result = str_replace( '{' . $key . '}', $json_val, $result ); | |
| 100 | + $result = str_replace( '{{' . $key . '}}', $json_val, $result ); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + //error_log( sprintf( 'WPbot Automator - Token Parse - Input: "%s", Result: "%s"', $string, $result ) ); | |
| 106 | + return $result; | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Flatten a multi-dimensional array into a single level with dot notation keys | |
| 111 | + * | |
| 112 | + * @param array $array The array to flatten. | |
| 113 | + * @param string $prefix Prefix for the keys. | |
| 114 | + * @return array | |
| 115 | + */ | |
| 116 | + private function flatten_array( $array, $prefix = '' ) { | |
| 117 | + $result = array(); | |
| 118 | + foreach ( $array as $key => $value ) { | |
| 119 | + $new_key = $prefix ? $prefix . '.' . $key : $key; | |
| 120 | + if ( is_array( $value ) ) { | |
| 121 | + $result = array_merge( $result, $this->flatten_array( $value, $new_key ) ); | |
| 122 | + } else { | |
| 123 | + $result[ $new_key ] = $value; | |
| 124 | + } | |
| 125 | + } | |
| 126 | + return $result; | |
| 127 | + } | |
| 128 | +} | |