| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add Actions Ability |
| 4 |
* |
| 5 |
* @package AutomatorWP\AI_Assistant\Ability\Add_Actions |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_AI_Assistant_Ability_Add_Actions extends AutomatorWP_AI_Assistant_Ability { |
| 13 |
|
| 14 |
public $ability = 'add-actions'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Register the ability |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
public function register() { |
| 22 |
|
| 23 |
$this->args = array( |
| 24 |
'label' => esc_html__( 'Add new actions to an AutomatorWP automation', 'automatorwp' ), |
| 25 |
'description' => esc_html__( 'Design and insert a new actions into the database.', 'automatorwp' ), |
| 26 |
'input_schema' => array( |
| 27 |
'type' => 'object', |
| 28 |
'properties' => array( |
| 29 |
'automation' => array( |
| 30 |
'description' => __( 'The automation ID or title.', 'automatorwp' ), |
| 31 |
'type' => 'string', |
| 32 |
), |
| 33 |
'actions' => array( |
| 34 |
'description' => esc_html__( 'List of action that should be executed when the triggers are meet.', 'automatorwp' ), |
| 35 |
'type' => 'array', |
| 36 |
'items' => array( |
| 37 |
'type' => 'object', |
| 38 |
'properties' => automatorwp_ai_assistant_get_items_properties( 'action' ), |
| 39 |
), |
| 40 |
), |
| 41 |
), |
| 42 |
'required' => array( 'automation', 'actions' ), |
| 43 |
'additionalProperties' => false, |
| 44 |
), |
| 45 |
); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Ability execute callback |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @param array $args |
| 55 |
* |
| 56 |
* @return array array( 'success' => true|false, 'message' => '' ) |
| 57 |
*/ |
| 58 |
public function execute( $args ) { |
| 59 |
|
| 60 |
$args = wp_parse_args( (array) $args, array( |
| 61 |
'automation' => '', |
| 62 |
'actions' => array(), |
| 63 |
) ); |
| 64 |
|
| 65 |
$automation_search = sanitize_text_field( $args['automation'] ); |
| 66 |
$actions = $args['actions']; |
| 67 |
|
| 68 |
// Check the automation |
| 69 |
$automation = automatorwp_ai_assistant_get_automation( $automation_search ); |
| 70 |
|
| 71 |
if( $this->is_response_error( $automation ) ) |
| 72 |
return $automation; |
| 73 |
|
| 74 |
// Actions |
| 75 |
$updated_actions = automatorwp_ai_assistant_process_items( |
| 76 |
$actions, |
| 77 |
$automation->id, |
| 78 |
'action' |
| 79 |
); |
| 80 |
|
| 81 |
$count = count( $updated_actions ); |
| 82 |
$label = _n( 'action', 'actions', $count, 'automatorwp' ); |
| 83 |
$answer = automatorwp_ai_assistant_get_items_answer( $updated_actions, 'action', false ); |
| 84 |
|
| 85 |
// Final response |
| 86 |
$automation_display = $automation->title; |
| 87 |
|
| 88 |
if ( current_user_can( automatorwp_get_manager_capability() ) ) |
| 89 |
$automation_display = '[' . $automation_display . '](' . ct_get_edit_link( 'automatorwp_automations', $automation->id ) . ')'; |
| 90 |
|
| 91 |
// translators: %1$s: Award label. %2$s: Point type name |
| 92 |
$message = sprintf( __( 'I added the following %1$s to %2$s:', 'automatorwp' ), $label, $automation_display ); |
| 93 |
$message .= "\n"; |
| 94 |
$message .= $answer; |
| 95 |
|
| 96 |
return $this->response_success( $message ); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
new AutomatorWP_AI_Assistant_Ability_Add_Actions(); |