PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.7.8
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.7.8
8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 All 534 releases
chatbot / addons / automator / includes / actions / workflow-actions.php

workflow-actions.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.7.8, at addons/automator/includes/actions/workflow-actions.php

109 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Workflow Actions
4 *
5 * @package WPbot_Automator
6 */
7
8 namespace WPbot_Automator\Actions;
9
10 use WPbot_Automator\Core\Database;
11 use WPbot_Automator\Engine\Workflow_Runner;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class Workflow_Actions
19 */
20 class Workflow_Actions extends Action {
21
22 /**
23 * Constructor
24 */
25 public function __construct() {
26 $this->id = 'workflow_actions';
27 $this->group = __( 'Workflows', 'wpbot-automator' );
28 }
29
30 /**
31 * Execute the action
32 */
33 public function execute( $action_data, $trigger_data ) {
34 $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : '';
35
36 switch ( $action_id ) {
37 case 'call_sub_workflow':
38 return $this->call_sub_workflow( $action_data, $trigger_data );
39 case 'return_values':
40 return $this->return_values( $action_data, $trigger_data );
41 default:
42 return array( 'success' => false, 'message' => 'Unknown workflow action.' );
43 }
44 }
45
46 /**
47 * Call a sub-workflow
48 */
49 private function call_sub_workflow( $action_data, $trigger_data ) {
50 $config = isset( $action_data['config'] ) ? $action_data['config'] : array();
51 $workflow_id = isset( $config['workflow_id'] ) ? absint( $config['workflow_id'] ) : 0;
52 $inputs = isset( $config['inputs'] ) ? $config['inputs'] : array();
53 $node_id = isset( $action_data['node_id'] ) ? $action_data['node_id'] : 'sub_workflow';
54
55 if ( ! $workflow_id ) {
56 return array( 'success' => false, 'message' => 'No sub-workflow selected.' );
57 }
58
59 global $wpdb;
60 $table = Database::get_workflows_table();
61 $workflow = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $workflow_id ), ARRAY_A );
62
63 if ( ! $workflow ) {
64 return array( 'success' => false, 'message' => 'Sub-workflow not found.' );
65 }
66
67 // Prepare input data for the sub-workflow.
68 $sub_trigger_data = array();
69 if ( is_array( $inputs ) ) {
70 foreach ( $inputs as $pair ) {
71 if ( ! empty( $pair['key'] ) ) {
72 $sub_trigger_data[ $pair['key'] ] = $this->parse_tokens( $pair['value'], $trigger_data );
73 }
74 }
75 }
76
77 // Execute the sub-workflow.
78 $result_data = Workflow_Runner::execute_workflow( $workflow, $sub_trigger_data, 'sub_workflow_trigger' );
79
80 // Return the result data namespaced by the node_id.
81 return array(
82 'success' => true,
83 $node_id => $result_data,
84 );
85 }
86
87 /**
88 * Return values from a sub-workflow
89 */
90 private function return_values( $action_data, $trigger_data ) {
91 $config = isset( $action_data['config'] ) ? $action_data['config'] : array();
92 $values = isset( $config['values'] ) ? $config['values'] : array();
93
94 $return_data = array();
95 if ( is_array( $values ) ) {
96 foreach ( $values as $pair ) {
97 if ( ! empty( $pair['key'] ) ) {
98 $return_data[ $pair['key'] ] = $this->parse_tokens( $pair['value'], $trigger_data );
99 }
100 }
101 }
102
103 return array(
104 'success' => true,
105 '__return_data' => $return_data,
106 );
107 }
108 }
109