Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions forms-bridge/addons/gsheets/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ function ( $defaults, $addon, $schema ) {
'name' => 'scope',
'label' => __( 'Scope', 'forms-bridge' ),
'type' => 'text',
'value' =>
'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
'value' => 'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
'required' => true,
),
array(
Expand Down Expand Up @@ -147,8 +146,7 @@ function ( $defaults, $addon, $schema ) {
'name' => '',
'schema' => 'Bearer',
'oauth_url' => 'https://accounts.google.com/o/oauth2/v2',
'scope' =>
'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
'scope' => 'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
'client_id' => '',
'client_secret' => '',
'access_token' => '',
Expand All @@ -173,8 +171,7 @@ function ( $data, $template_id ) {
return $data;
}

$data['bridge']['endpoint'] =
'/v4/spreadsheets/' . $data['bridge']['endpoint'];
$data['bridge']['endpoint'] = '/v4/spreadsheets/' . $data['bridge']['endpoint'];
return $data;
},
10,
Expand All @@ -184,7 +181,7 @@ function ( $data, $template_id ) {
add_filter(
'http_bridge_oauth_url',
function ( $url, $verb ) {
if ( strpos( $url, 'accounts.google.com' ) === false ) {
if ( false === strpos( $url, 'accounts.google.com' ) ) {
return $url;
}

Expand Down
Binary file added forms-bridge/addons/slack/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions forms-bridge/addons/slack/class-slack-addon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Class Slack_Addon
*
* @package formsbridge
*/

namespace FORMS_BRIDGE;

if ( ! defined( 'ABSPATH' ) ) {
exit();
}

require_once 'class-slack-form-bridge.php';
require_once 'hooks.php';

/**
* Slack addon class.
*/
class Slack_Addon extends Addon {
/**
* Holds the addon's title.
*
* @var string
*/
public const TITLE = 'Slack';

/**
* Holds the addon's name.
*
* @var string
*/
public const NAME = 'slack';

/**
* Holds the addon's custom bridge class.
*
* @var string
*/
public const BRIDGE = '\FORMS_BRIDGE\Slack_Form_Bridge';

/**
* Performs a request against the backend to check the connexion status.
*
* @param string $backend Backend name.
*
* @return boolean
*/
public function ping( $backend ) {
$bridge = new Slack_Form_Bridge(
array(
'name' => '__slack-' . time(),
'endpoint' => '/api/conversations.list',
'method' => 'GET',
'backend' => $backend,
)
);

$response = $bridge->submit();

if ( is_wp_error( $response ) ) {
Logger::log( 'Slack backend ping error response', Logger::ERROR );
Logger::log( $response, Logger::ERROR );
return false;
}

return true;
}

/**
* Performs a GET request against the backend endpoint and retrive the response data.
*
* @param string $endpoint API endpoint.
* @param string $backend Backend name.
*
* @return array|WP_Error
*/
public function fetch( $endpoint, $backend ) {
$bridge = new Slack_Form_Bridge(
array(
'name' => '__slack-' . time(),
'endpoint' => $endpoint,
'method' => 'GET',
'backend' => $backend,
),
'zulip'
);

return $bridge->submit();
}
}

Slack_Addon::setup();
153 changes: 153 additions & 0 deletions forms-bridge/addons/slack/class-slack-form-bridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Class Slack_Form_Bridge
*
* @package formsbridge
*/

namespace FORMS_BRIDGE;

use FBAPI;
use WP_Error;

if ( ! defined( 'ABSPATH' ) ) {
exit();
}

/**
* Form bridge implementation for the Slack API.
*/
class Slack_Form_Bridge extends Form_Bridge {
/**
* Holdes the current HTTP request.
*
* @var array|null
*/
private static $request;

/**
* Submits payload and attachments to the bridge's backend.
*
* @param array $payload Payload data.
* @param array $attachments Submission's attached files.
*
* @return array|WP_Error Http request response.
*/
public function submit( $payload = array(), $attachments = array() ) {
$uploads = FBAPI::get_uploads();

if ( ! empty( $uploads ) ) {
$attachments = Forms_Bridge::attachments( $uploads );
$backend = $this->backend();

foreach ( $attachments as $name => $path ) {
$info = pathinfo( $path );
$filename = $info['basename'];

$response = $backend->post(
'/api/files.getUploadURLExternal',
array(
'length' => filesize( $path ),
'filename' => $filename,
),
array(
'Content-Type' => 'application/x-www-form-urlencoded',
)
);

if ( is_wp_error( $response ) ) {
return $response;
}

if ( isset( $response['data']['error'] ) ) {
return new WP_Error( 'slack_upload', __( 'Can not upload a file to Slack', 'forms-bridge' ), $response['data'] );
}

$file_id = $response['data']['file_id'];
$upload_url = $response['data']['upload_url'];

$response = http_bridge_post(
$upload_url,
file_get_contents( $path ),
array(
'Content-Type' => 'application/octet-stream',
),
);

if ( is_wp_error( $response ) ) {
return $response;
}

$attachments[ $name ] = $file_id;
}

$files = array();
foreach ( $attachments as $name => $file_id ) {
$files[] = array(
'id' => $file_id,
'title' => $name,
);
}

$response = $backend->post(
'/api/files.completeUploadExternal',
array(
'files' => wp_json_encode( $files ),
'channel_id' => $payload['channel'] ?? $payload['channel_id'] ?? null,
),
array(
'Content-Type' => 'application/x-www-form-urlencoded',
),
);

if ( is_wp_error( $response ) ) {
return $response;
}

if ( isset( $response['data']['error'] ) ) {
return new WP_Error( 'slack_upload', __( 'Can not upload a file to Slack', 'forms-bridge' ), $response['data'] );
}

$annex = "\n\n----\n" . esc_html( __( 'Attachments', 'forms-bridge' ) ) . ":\n";

foreach ( $response['data']['files'] as $upload ) {
$annex .= "* [{$upload['name']}]({$upload['permalink']})\n";
}

if ( isset( $payload['markdown_text'] ) ) {
$payload['markdown_text'] .= $annex;
} else {
$payload['text'] .= $annex;
}
}

add_filter(
'http_bridge_request',
static function ( $request ) {
self::$request = $request;
return $request;
},
10,
1
);

$response = parent::submit( $payload, array() );

if ( is_wp_error( $response ) ) {
return $response;
}

if ( isset( $response['data']['error'] ) ) {
return new WP_Error(
'slack_rpc_error',
'Slack bridge response error',
array(
'request' => self::$request,
'response' => $response,
)
);
}

return $response;
}
}
Loading