'; } public function description() { return __( 'Turn entries into Trello cards on any board and list.', 'fieldwren' ); } public function help() { return '

' . esc_html__( 'trello.com/power-ups/admin โ†’ New Power-Up โ†’ API key โ†’ generate the API key, then click "Token" next to it to authorise and copy the token.', 'fieldwren' ) . '

'; } public function credential_fields() { return array( 'key' => array( 'label' => __( 'API key', 'fieldwren' ), 'type' => 'text', 'required' => true ), 'token' => array( 'label' => __( 'Token', 'fieldwren' ), 'type' => 'password', 'required' => true ), ); } private function api( $method, $path, $body = null, array $creds = null ) { $c = null === $creds ? $this->creds() : $creds; $args = array( 'headers' => array( 'Authorization' => 'OAuth oauth_consumer_key="' . ( $c['key'] ?? '' ) . '", oauth_token="' . ( $c['token'] ?? '' ) . '"' ) ); if ( null !== $body ) { $args['body'] = $body; } $this->secrets[] = (string) ( $c['token'] ?? '' ); return self::body( $this->http( $method, 'https://api.trello.com/1' . $path, $args ) ); } public function verify( array $creds ) { $b = $this->api( 'GET', '/members/me?fields=fullName,username', null, $creds ); return is_wp_error( $b ) ? $b : (string) ( $b['fullName'] ?? '' ) . ' (@' . ( $b['username'] ?? '' ) . ')'; } public function options( $source, array $settings ) { $board = rawurlencode( (string) ( $settings['board'] ?? '' ) ); switch ( $source ) { case 'boards': $b = $this->api( 'GET', '/members/me/boards?filter=open&fields=name' ); return is_wp_error( $b ) ? $b : self::pluck( $b, 'id', 'name' ); case 'lists': $b = $this->api( 'GET', '/boards/' . $board . '/lists?filter=open&fields=name' ); return is_wp_error( $b ) ? $b : self::pluck( $b, 'id', 'name' ); case 'labels': $b = $this->api( 'GET', '/boards/' . $board . '/labels?fields=name,color' ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) $b as $l ) { $out[ $l['id'] ] = ( '' !== (string) $l['name'] ? $l['name'] : __( '(no name)', 'fieldwren' ) ) . ' ยท ' . $l['color']; } return $out; } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { $auto = self::auto_values( $form_id ); return array( array( 'key' => 'board', 'type' => 'select', 'source' => 'boards', 'label' => __( 'Board', 'fieldwren' ), 'required' => true ), array( 'key' => 'list', 'type' => 'select', 'source' => 'lists', 'depends' => array( 'board' ), 'label' => __( 'List', 'fieldwren' ), 'required' => true ), array( 'key' => 'name', 'type' => 'text', 'label' => __( 'Card title', 'fieldwren' ), 'required' => true, 'default' => '{form_title}: ' . ( $auto['name'] ? $auto['name'] : '#{entry.serial}' ) ), array( 'key' => 'desc', 'type' => 'textarea', 'label' => __( 'Description', 'fieldwren' ), 'rows' => 5, 'default' => "{all_data_text}\n\n{entry.admin_url}", 'desc' => __( 'Markdown is supported.', 'fieldwren' ) ), array( 'key' => 'labels', 'type' => 'select', 'multiple' => true, 'source' => 'labels', 'depends' => array( 'board' ), 'label' => __( 'Labels', 'fieldwren' ) ), array( 'key' => 'pos', 'type' => 'select', 'label' => __( 'Position', 'fieldwren' ), 'default' => 'top', 'no_empty' => true, 'options' => array( 'top' => __( 'Top', 'fieldwren' ), 'bottom' => __( 'Bottom', 'fieldwren' ) ) ), array( 'key' => 'due_days', 'type' => 'number', 'label' => __( 'Due in (days)', 'fieldwren' ), 'desc' => __( 'Optional.', 'fieldwren' ) ), ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; if ( empty( $s['list'] ) ) { return new \WP_Error( 'fwren_trello', __( 'Choose a list.', 'fieldwren' ) ); } $name = $this->val( $s['name'] ?? '' ); $card = array( 'idList' => (string) $s['list'], 'name' => self::cut( '' !== $name ? $name : $this->default_title(), 16000 ), 'desc' => self::cut( $this->val( $s['desc'] ?? '' ), 16000 ), 'pos' => 'bottom' === ( $s['pos'] ?? 'top' ) ? 'bottom' : 'top', ); if ( ! empty( $s['labels'] ) ) { $card['idLabels'] = implode( ',', array_map( 'strval', (array) $s['labels'] ) ); } if ( is_numeric( $s['due_days'] ?? '' ) && (int) $s['due_days'] > 0 ) { $card['due'] = gmdate( 'c', time() + (int) $s['due_days'] * DAY_IN_SECONDS ); } $b = $this->api( 'POST', '/cards', $card ); /* translators: %s card URL */ return is_wp_error( $b ) ? $b : sprintf( __( 'Card created: %s', 'fieldwren' ), (string) ( $b['shortUrl'] ?? '' ) ); } }