| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create Task |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\ClickUp\Actions\Create_Task |
| 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_ClickUp_Create_Task extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'clickup'; |
| 15 |
public $action = 'clickup_create_task'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_action( $this->action, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'Create a new task in list', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Create a new task in <strong>list</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Task. */ |
| 29 |
'edit_label' => sprintf( __( 'User creates a new task in %1$s', 'automatorwp' ), '{list}' ), |
| 30 |
/* translators: %1$s: Task. */ |
| 31 |
'log_label' => sprintf( __( 'User creates a new task in %1$s', 'automatorwp' ), '{list}' ), |
| 32 |
'options' => array( |
| 33 |
'list' => array( |
| 34 |
'from' => 'list', |
| 35 |
'default' => __( 'list', 'automatorwp' ), |
| 36 |
'fields' => array( |
| 37 |
'team' => automatorwp_utilities_ajax_selector_field( array( |
| 38 |
'name' => __( 'Team:', 'automatorwp' ), |
| 39 |
'option_none' => false, |
| 40 |
'action_cb' => 'automatorwp_clickup_get_teams', |
| 41 |
'options_cb' => 'automatorwp_clickup_options_cb_team', |
| 42 |
'placeholder' => 'Select a team', |
| 43 |
) ), |
| 44 |
'space' => automatorwp_utilities_ajax_selector_field( array( |
| 45 |
'name' => __( 'Space:', 'automatorwp' ), |
| 46 |
'option_none' => false, |
| 47 |
'action_cb' => 'automatorwp_clickup_get_spaces', |
| 48 |
'options_cb' => 'automatorwp_clickup_options_cb_space', |
| 49 |
'placeholder' => 'Select a space', |
| 50 |
'default' => '' |
| 51 |
) ), |
| 52 |
'folder' => automatorwp_utilities_ajax_selector_field( array( |
| 53 |
'name' => __( 'Folder:', 'automatorwp' ), |
| 54 |
'option_none' => false, |
| 55 |
'action_cb' => 'automatorwp_clickup_get_folders', |
| 56 |
'options_cb' => 'automatorwp_clickup_options_cb_folder', |
| 57 |
'placeholder' => 'Select a folder', |
| 58 |
'default' => '' |
| 59 |
) ), |
| 60 |
'list' => automatorwp_utilities_ajax_selector_field( array( |
| 61 |
'name' => __( 'List:', 'automatorwp' ), |
| 62 |
'option_none' => false, |
| 63 |
'action_cb' => 'automatorwp_clickup_get_lists', |
| 64 |
'options_cb' => 'automatorwp_clickup_options_cb_list', |
| 65 |
'placeholder' => 'Select a list', |
| 66 |
'default' => '' |
| 67 |
) ), |
| 68 |
'name_task' => array( |
| 69 |
'field' => 'name_task', |
| 70 |
'name' => __( 'Task name:', 'automatorwp' ), |
| 71 |
'desc' => __( 'The task name', 'automatorwp' ), |
| 72 |
'type' => 'text', |
| 73 |
'required' => true, |
| 74 |
'default' => '' |
| 75 |
), |
| 76 |
'description_task' => array( |
| 77 |
'field' => 'description_task', |
| 78 |
'name' => __( 'Task description:', 'automatorwp' ), |
| 79 |
'desc' => __( 'The task description', 'automatorwp' ), |
| 80 |
'type' => 'textarea', |
| 81 |
'default' => '' |
| 82 |
), |
| 83 |
), |
| 84 |
), |
| 85 |
), |
| 86 |
) ); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Action execution function |
| 93 |
* |
| 94 |
* @since 1.0.0 |
| 95 |
* |
| 96 |
* @param stdClass $action The action object |
| 97 |
* @param int $user_id The user ID |
| 98 |
* @param array $action_options The action's stored options (with tags already passed) |
| 99 |
* @param stdClass $automation The action's automation object |
| 100 |
*/ |
| 101 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 102 |
|
| 103 |
// Shorthand |
| 104 |
$list_id = $action_options['list']; |
| 105 |
$name_task = $action_options['name_task']; |
| 106 |
$description_task = $action_options['description_task']; |
| 107 |
|
| 108 |
// Bail if name is empty |
| 109 |
if ( empty ( $name_task ) ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
$user = get_user_by ( 'ID', $user_id ); |
| 114 |
|
| 115 |
$this->result = ''; |
| 116 |
|
| 117 |
// Bail if ClickUp not configured |
| 118 |
if( ! automatorwp_clickup_get_api() ) { |
| 119 |
$this->result = __( 'ClickUp integration not configured in AutomatorWP settings.', 'automatorwp' ); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$response = automatorwp_clickup_create_task( $list_id, $name_task, $description_task ); |
| 124 |
|
| 125 |
// Create list if not exist |
| 126 |
if ( $response === 200 ) { |
| 127 |
$this->result = __( 'Task created', 'automatorwp' ); |
| 128 |
} else { |
| 129 |
$this->result = __( 'The task could not be created', 'automatorwp' ); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Register required hooks |
| 136 |
* |
| 137 |
* @since 1.0.0 |
| 138 |
*/ |
| 139 |
public function hooks() { |
| 140 |
|
| 141 |
// Configuration notice |
| 142 |
add_filter( 'automatorwp_automation_ui_after_item_label', array( $this, 'configuration_notice' ), 10, 2 ); |
| 143 |
|
| 144 |
// Log meta data |
| 145 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 146 |
|
| 147 |
// Log fields |
| 148 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 149 |
|
| 150 |
parent::hooks(); |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Configuration notice |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* |
| 159 |
* @param stdClass $object The trigger/action object |
| 160 |
* @param string $item_type The object type (trigger|action) |
| 161 |
*/ |
| 162 |
public function configuration_notice( $object, $item_type ) { |
| 163 |
|
| 164 |
// Bail if action type don't match this action |
| 165 |
if( $item_type !== 'action' ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
if( $object->type !== $this->action ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
// Warn user if the authorization has not been setup from settings |
| 174 |
if( ! automatorwp_clickup_get_api() ) : ?> |
| 175 |
<div class="automatorwp-notice-warning" style="margin-top: 10px; margin-bottom: 0;"> |
| 176 |
<?php echo sprintf( |
| 177 |
__( 'You need to configure the <a href="%s" target="_blank">ClickUp settings</a> to get this action to work.', 'automatorwp' ), |
| 178 |
get_admin_url() . 'admin.php?page=automatorwp_settings&tab=opt-tab-clickup' |
| 179 |
); ?> |
| 180 |
<?php echo sprintf( |
| 181 |
__( '<a href="%s" target="_blank">Documentation</a>', 'automatorwp' ), |
| 182 |
'https://automatorwp.com/docs/clickup/' |
| 183 |
); ?> |
| 184 |
</div> |
| 185 |
<?php endif; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Action custom log meta |
| 191 |
* |
| 192 |
* @since 1.0.0 |
| 193 |
* |
| 194 |
* @param array $log_meta Log meta data |
| 195 |
* @param stdClass $action The action object |
| 196 |
* @param int $user_id The user ID |
| 197 |
* @param array $action_options The action's stored options (with tags already passed) |
| 198 |
* @param stdClass $automation The action's automation object |
| 199 |
* |
| 200 |
* @return array |
| 201 |
*/ |
| 202 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 203 |
|
| 204 |
// Bail if action type don't match this action |
| 205 |
if( $action->type !== $this->action ) { |
| 206 |
return $log_meta; |
| 207 |
} |
| 208 |
|
| 209 |
// Store the action's result |
| 210 |
$log_meta['result'] = $this->result; |
| 211 |
|
| 212 |
return $log_meta; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Action custom log fields |
| 217 |
* |
| 218 |
* @since 1.0.0 |
| 219 |
* |
| 220 |
* @param array $log_fields The log fields |
| 221 |
* @param stdClass $log The log object |
| 222 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 223 |
* |
| 224 |
* @return array |
| 225 |
*/ |
| 226 |
public function log_fields( $log_fields, $log, $object ) { |
| 227 |
|
| 228 |
// Bail if log is not assigned to an action |
| 229 |
if( $log->type !== 'action' ) { |
| 230 |
return $log_fields; |
| 231 |
} |
| 232 |
|
| 233 |
// Bail if action type don't match this action |
| 234 |
if( $object->type !== $this->action ) { |
| 235 |
return $log_fields; |
| 236 |
} |
| 237 |
|
| 238 |
$log_fields['result'] = array( |
| 239 |
'name' => __( 'Result:', 'automatorwp' ), |
| 240 |
'type' => 'text', |
| 241 |
); |
| 242 |
|
| 243 |
return $log_fields; |
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
new AutomatorWP_ClickUp_Create_Task(); |