'; } public function description() { return __( 'Create ClickUp tasks from entries.', 'fieldwren' ); } public function help() { return '

' . esc_html__( 'ClickUp → avatar → Settings → Apps → API Token → Generate (starts with pk_).', 'fieldwren' ) . '

'; } public function credential_fields() { return array( 'token' => array( 'label' => __( 'Personal API token', 'fieldwren' ), 'type' => 'password', 'required' => true, 'placeholder' => 'pk_…' ), ); } private function api( $method, $path, $body = null, $token = null ) { $args = array( 'headers' => array( 'Authorization' => null === $token ? $this->cred( 'token' ) : $token ) ); if ( null !== $body ) { $args['body'] = $body; } return self::body( $this->http( $method, 'https://api.clickup.com/api/v2' . $path, $args ) ); } protected function error_message( $body, $code ) { return is_array( $body ) && isset( $body['err'] ) ? (string) $body['err'] : parent::error_message( $body, $code ); } public function verify( array $creds ) { $b = $this->api( 'GET', '/user', null, (string) $creds['token'] ); return is_wp_error( $b ) ? $b : (string) ( $b['user']['username'] ?? '' ); } public function options( $source, array $settings ) { switch ( $source ) { case 'teams': $b = $this->api( 'GET', '/team' ); return is_wp_error( $b ) ? $b : self::pluck( $b['teams'] ?? array(), 'id', 'name' ); case 'spaces': $b = $this->api( 'GET', '/team/' . rawurlencode( (string) ( $settings['team'] ?? '' ) ) . '/space?archived=false' ); return is_wp_error( $b ) ? $b : self::pluck( $b['spaces'] ?? array(), 'id', 'name' ); case 'lists': $space = rawurlencode( (string) ( $settings['space'] ?? '' ) ); $b = $this->api( 'GET', '/space/' . $space . '/list?archived=false' ); if ( is_wp_error( $b ) ) { return $b; } $out = self::pluck( $b['lists'] ?? array(), 'id', 'name' ); $f = $this->api( 'GET', '/space/' . $space . '/folder?archived=false' ); foreach ( is_wp_error( $f ) ? array() : (array) ( $f['folders'] ?? array() ) as $folder ) { foreach ( (array) ( $folder['lists'] ?? array() ) as $l ) { $out[ (string) $l['id'] ] = $folder['name'] . ' › ' . $l['name']; } } return $out; } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { $auto = self::auto_values( $form_id ); return array( array( 'key' => 'team', 'type' => 'select', 'source' => 'teams', 'label' => __( 'Workspace', 'fieldwren' ), 'required' => true ), array( 'key' => 'space', 'type' => 'select', 'source' => 'spaces', 'depends' => array( 'team' ), 'label' => __( 'Space', 'fieldwren' ), 'required' => true ), array( 'key' => 'list', 'type' => 'select', 'source' => 'lists', 'depends' => array( 'space' ), 'label' => __( 'List', 'fieldwren' ), 'required' => true ), array( 'key' => 'name', 'type' => 'text', 'label' => __( 'Task name', 'fieldwren' ), 'required' => true, 'default' => '{form_title}: ' . ( $auto['name'] ? $auto['name'] : '#{entry.serial}' ) ), array( 'key' => 'description', 'type' => 'textarea', 'label' => __( 'Description', 'fieldwren' ), 'rows' => 5, 'default' => "{all_data_text}\n\n{entry.admin_url}", 'desc' => __( 'Markdown is supported.', 'fieldwren' ) ), array( 'key' => 'priority', 'type' => 'select', 'label' => __( 'Priority', 'fieldwren' ), 'default' => '', 'options' => array( '1' => __( 'Urgent', 'fieldwren' ), '2' => __( 'High', 'fieldwren' ), '3' => __( 'Normal', 'fieldwren' ), '4' => __( 'Low', 'fieldwren' ) ) ), array( 'key' => 'tags', 'type' => 'text', 'label' => __( 'Tags', 'fieldwren' ), 'desc' => __( 'Comma separated; smartcodes allowed.', 'fieldwren' ) ), array( 'key' => 'due_days', 'type' => 'number', 'label' => __( 'Due in (days)', 'fieldwren' ) ), ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; if ( empty( $s['list'] ) ) { return new \WP_Error( 'fwren_clickup', __( 'Choose a list.', 'fieldwren' ) ); } $name = $this->val( $s['name'] ?? '' ); $task = array( 'name' => '' !== $name ? $name : $this->default_title(), 'markdown_description' => $this->val( $s['description'] ?? '' ), ); if ( ! empty( $s['priority'] ) ) { $task['priority'] = (int) $s['priority']; } $tags = $this->list_of( $s['tags'] ?? '' ); if ( $tags ) { $task['tags'] = array_map( 'strtolower', $tags ); } if ( is_numeric( $s['due_days'] ?? '' ) && (int) $s['due_days'] > 0 ) { $task['due_date'] = ( time() + (int) $s['due_days'] * DAY_IN_SECONDS ) * 1000; } $b = $this->api( 'POST', '/list/' . rawurlencode( (string) $s['list'] ) . '/task', $task ); /* translators: %s task URL */ return is_wp_error( $b ) ? $b : sprintf( __( 'Task created: %s', 'fieldwren' ), (string) ( $b['url'] ?? '' ) ); } }