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