| 1 |
<?php |
| 2 |
/** |
| 3 |
* Logs |
| 4 |
* |
| 5 |
* @package AutomatorWP\AI_Assistant\Logs |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Log types |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @param array $log_types The log types |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
function automatorwp_ai_assistant_log_types( $log_types ) { |
| 22 |
|
| 23 |
$log_types['run_action'] = __( 'Action (Direct Run)', 'automatorwp' ); |
| 24 |
|
| 25 |
return $log_types; |
| 26 |
} |
| 27 |
add_filter( 'automatorwp_log_types', 'automatorwp_ai_assistant_log_types' ); |
| 28 |
|
| 29 |
/** |
| 30 |
* Override log icon |
| 31 |
* |
| 32 |
* @since 6.0.0 |
| 33 |
* |
| 34 |
* @param string $icon The icon URL |
| 35 |
* @param stdClass $log The log object |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
function automatorwp_ai_assistant_get_log_icon( $icon, $log ) { |
| 40 |
|
| 41 |
if( $log->type !== 'run_action' ) return $icon; |
| 42 |
|
| 43 |
ct_setup_table('automatorwp_logs'); |
| 44 |
$type = ct_get_object_meta( $log->id, 'run_action_type', true ); |
| 45 |
ct_reset_setup_table(); |
| 46 |
|
| 47 |
$type_args = automatorwp_get_action( $type ); |
| 48 |
|
| 49 |
if( $type_args ) { |
| 50 |
$integration = automatorwp_get_integration( $type_args['integration'] ); |
| 51 |
|
| 52 |
if( $integration ) { |
| 53 |
$object = (object) array( 'type' => $type ); |
| 54 |
$integration = apply_filters( 'automatorwp_get_log_integration_icon_integration', $integration, $object, $log->type, $log ); |
| 55 |
|
| 56 |
return $integration['icon']; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return $icon; |
| 61 |
|
| 62 |
} |
| 63 |
add_filter( 'automatorwp_get_log_default_icon', 'automatorwp_ai_assistant_get_log_icon', 10, 2 ); |
| 64 |
|
| 65 |
/** |
| 66 |
* Override log icon title |
| 67 |
* |
| 68 |
* @since 6.0.0 |
| 69 |
* |
| 70 |
* @param string $title The icon title |
| 71 |
* @param stdClass $log The log object |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
function automatorwp_ai_assistant_get_log_icon_title( $title, $log ) { |
| 76 |
|
| 77 |
if( $log->type !== 'run_action' ) return $title; |
| 78 |
|
| 79 |
ct_setup_table('automatorwp_logs'); |
| 80 |
$type = ct_get_object_meta( $log->id, 'run_action_type', true ); |
| 81 |
ct_reset_setup_table(); |
| 82 |
|
| 83 |
$type_args = automatorwp_get_action( $type ); |
| 84 |
|
| 85 |
if( $type_args ) { |
| 86 |
$integration = automatorwp_get_integration( $type_args['integration'] ); |
| 87 |
|
| 88 |
if( $integration ) { |
| 89 |
$object = (object) array( 'type' => $type ); |
| 90 |
$integration = apply_filters( 'automatorwp_get_log_integration_icon_integration', $integration, $object, $log->type, $log ); |
| 91 |
|
| 92 |
return $integration['label']; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return $title; |
| 97 |
|
| 98 |
} |
| 99 |
add_filter( 'automatorwp_get_log_default_icon_title', 'automatorwp_ai_assistant_get_log_icon_title', 10, 2 ); |
| 100 |
|
| 101 |
/** |
| 102 |
* Columns rendering for logs list view |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param string $column_name |
| 107 |
* @param integer $object_id |
| 108 |
*/ |
| 109 |
function automatorwp_ai_assistant_manage_logs_custom_column( $column_name, $object_id ) { |
| 110 |
|
| 111 |
// Setup vars |
| 112 |
$log = ct_get_object( $object_id ); |
| 113 |
|
| 114 |
if( $log->type !== 'run_action' ) return; |
| 115 |
|
| 116 |
switch( $column_name ) { |
| 117 |
case 'user_id': |
| 118 |
?> |
| 119 |
|
| 120 |
<br> |
| 121 |
<small><?php echo sprintf( __( 'Run with %s', 'automatorwp' ), '<strong>' . __( 'AI Assistant', 'automatorwp' ) . '</strong>' ); ?></small> |
| 122 |
|
| 123 |
<?php |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
add_action( 'manage_automatorwp_logs_custom_column', 'automatorwp_ai_assistant_manage_logs_custom_column', 15, 2 ); |
| 129 |
function automatorwp_ai_assistant_log_details_meta_box_after_user( $log ) { |
| 130 |
|
| 131 |
if( $log->type !== 'run_action' ) return; |
| 132 |
|
| 133 |
?> |
| 134 |
|
| 135 |
<br> |
| 136 |
<small style="margin-left: 26px;"><?php echo sprintf( __( 'Run with %s', 'automatorwp' ), '<strong>' . __( 'AI Assistant', 'automatorwp' ) . '</strong>' ); ?></small> |
| 137 |
|
| 138 |
<?php |
| 139 |
|
| 140 |
} |
| 141 |
add_action( 'automatorwp_log_details_meta_box_after_user', 'automatorwp_ai_assistant_log_details_meta_box_after_user' ); |
| 142 |
|
| 143 |
/** |
| 144 |
* Filter to override the log if needed |
| 145 |
* |
| 146 |
* @since 6.0.0 |
| 147 |
* |
| 148 |
* @param stdClass $log The log object |
| 149 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 150 |
* |
| 151 |
* @return stdClass |
| 152 |
*/ |
| 153 |
function automatorwp_ai_assistant_log_fields_log( $log, $object ) { |
| 154 |
|
| 155 |
global $run_action_override; |
| 156 |
|
| 157 |
$run_action_override = false; |
| 158 |
|
| 159 |
if( $log->type === 'run_action' ) { |
| 160 |
$run_action_override = true; |
| 161 |
$log->type = 'action'; |
| 162 |
} |
| 163 |
|
| 164 |
return $log; |
| 165 |
|
| 166 |
} |
| 167 |
add_filter( 'automatorwp_log_fields_log', 'automatorwp_ai_assistant_log_fields_log', 10, 2 ); |
| 168 |
|
| 169 |
/** |
| 170 |
* Filter to override the object if needed |
| 171 |
* |
| 172 |
* @since 6.0.0 |
| 173 |
* |
| 174 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 175 |
* @param stdClass $log The log object |
| 176 |
* |
| 177 |
* @return stdClass |
| 178 |
*/ |
| 179 |
function automatorwp_ai_assistant_log_fields_object( $object, $log ) { |
| 180 |
|
| 181 |
global $run_action_override; |
| 182 |
|
| 183 |
if( $run_action_override !== true ) return $object; |
| 184 |
|
| 185 |
ct_setup_table('automatorwp_logs'); |
| 186 |
$type = ct_get_object_meta( $log->id, 'run_action_type', true ); |
| 187 |
ct_reset_setup_table(); |
| 188 |
|
| 189 |
$object = (object) array( 'type' => $type ); |
| 190 |
|
| 191 |
return $object; |
| 192 |
|
| 193 |
} |
| 194 |
add_filter( 'automatorwp_log_fields_object', 'automatorwp_ai_assistant_log_fields_object', 10, 2 ); |
| 195 |
|
| 196 |
/** |
| 197 |
* Filter to set custom log fields |
| 198 |
* |
| 199 |
* @since 1.0.0 |
| 200 |
* |
| 201 |
* @param array $log_fields The log fields |
| 202 |
* @param stdClass $log The log object |
| 203 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 204 |
* |
| 205 |
* @return array |
| 206 |
*/ |
| 207 |
function automatorwp_ai_assistant_log_fields( $log_fields, $log, $object ) { |
| 208 |
|
| 209 |
global $run_action_override; |
| 210 |
|
| 211 |
if( $run_action_override !== true ) return $log_fields; |
| 212 |
|
| 213 |
$log_fields['ai_assistant_title'] = array( |
| 214 |
'name' => __( 'AI Assistant', 'automatorwp' ), |
| 215 |
'desc' => __( 'Information about the data processed internally by the AI Assistant.', 'automatorwp' ), |
| 216 |
'type' => 'title', |
| 217 |
); |
| 218 |
|
| 219 |
$log_fields['ai_assistant_prompt'] = array( |
| 220 |
'name' => __( 'Prompt', 'automatorwp' ), |
| 221 |
'desc' => __( 'The prompt used to execute this action.', 'automatorwp' ), |
| 222 |
'type' => 'text', |
| 223 |
); |
| 224 |
|
| 225 |
$log_fields['run_action_options'] = array( |
| 226 |
'name' => __( 'Data', 'automatorwp' ), |
| 227 |
'desc' => __( 'The data passed to the action by the AI Assistant.', 'automatorwp' ), |
| 228 |
'type' => 'text', |
| 229 |
); |
| 230 |
|
| 231 |
return $log_fields; |
| 232 |
|
| 233 |
} |
| 234 |
add_filter( 'automatorwp_log_fields', 'automatorwp_ai_assistant_log_fields', 99, 3 ); |