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
4 changes: 2 additions & 2 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ behat.yml
bitbucket-pipelines.yml
bin
.circleci/config.yml
# composer.json
composer.json
composer.lock
dependencies.yml
Gruntfile.js
# package.json
package.json
package-lock.json
phpunit.xml
phpunit.xml.dist
Expand Down
14 changes: 12 additions & 2 deletions forms-bridge/addons/gsheets/class-gsheets-addon.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php
/**
* Class Google_Sheets_Addon
*
* @package formsbridge
*/

namespace FORMS_BRIDGE;

use FBAPI;
use WP_Error;

if ( ! defined( 'ABSPATH' ) ) {
exit();
Expand Down Expand Up @@ -37,13 +43,17 @@ class Google_Sheets_Addon extends Addon {
*/
const BRIDGE = '\FORMS_BRIDGE\Google_Sheets_Form_Bridge';

/**
* Addon loader. Set up hooks to skip payload prunes if it comes from a
* google sheets bridge.
*/
public function load() {
parent::load();

add_filter(
'forms_bridge_prune_empties',
static function ( $prune, $bridge ) {
if ( $bridge->addon === 'gsheets' ) {
if ( 'gsheets' === $bridge->addon ) {
return false;
}

Expand Down Expand Up @@ -85,7 +95,7 @@ public function ping( $backend ) {
$parsed = wp_parse_url( $backend->base_url );
$host = $parsed['host'] ?? '';

if ( $host !== 'sheets.googleapis.com' ) {
if ( 'sheets.googleapis.com' !== $host ) {
return false;
}

Expand Down
64 changes: 59 additions & 5 deletions forms-bridge/addons/gsheets/class-gsheets-form-bridge.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Class Google_Sheets_Form_Bridge
*
* @package formsbridge
*/

namespace FORMS_BRIDGE;

Expand All @@ -13,10 +18,24 @@
*/
class Google_Sheets_Form_Bridge extends Form_Bridge {

/**
* Bridge constructor with addon name provisioning.
*
* @param array $data Bridge data.
*/
public function __construct( $data ) {
parent::__construct( $data, 'gsheets' );
}

/**
* Given an array of values returns a value range descriptor.
*
* @param array $values Array of values.
*
* @return string
*
* @deprecated
*/
private function value_range( $values ) {
$range = rawurlencode( $this->tab );

Expand All @@ -29,12 +48,15 @@ private function value_range( $values ) {
$len = strlen( $abc );

$columns = array();
for ( $row = 0; $row < count( $values ); $row++ ) {

$rows = count( $values );
for ( $row = 0; $row < $rows; $row++ ) {
$rowcols = array();
$i = -1;

for ( $col = 0; $col < count( $values[ $row ] ); $col++ ) {
if ( $col > 0 && $col % $len === 0 ) {
$i = -1;
$cols = count( $values[ $row ] );
for ( $col = 0; $col < $cols; $col++ ) {
if ( $col > 0 && 0 === $col % $len ) {
++$i;
}

Expand All @@ -57,6 +79,13 @@ private function value_range( $values ) {
return $range;
}

/**
* Fetches the first row of the sheet and return it as an array of headers / columns.
*
* @param Backend|null $backend Bridge backend instance.
*
* @return array<string>
*/
public function get_headers( $backend = null ) {
if ( ! $this->is_valid ) {
return new WP_Error( 'invalid_bridge' );
Expand All @@ -77,6 +106,15 @@ public function get_headers( $backend = null ) {
return $response['data']['values'][0] ?? array();
}

/**
* Creates a new sheet on the document.
*
* @param integer $index Position of the new sheet in the sheets list.
* @param string $title Sheet title.
* @param Backend $backend Bridge backend instance.
*
* @return array|WP_Error Sheet data or creation error.
*/
private function add_sheet( $index, $title, $backend ) {
$response = $backend->post(
$this->endpoint . ':batchUpdate',
Expand Down Expand Up @@ -108,6 +146,13 @@ private function add_sheet( $index, $title, $backend ) {
return $response['data'];
}

/**
* Request for the list of sheets of the document.
*
* @param Backend $backend Bridge backend instance.
*
* @return array<string>|WP_Error
*/
private function get_sheets( $backend ) {
$response = $backend->get( $this->endpoint );

Expand All @@ -124,6 +169,7 @@ private function get_sheets( $backend ) {
}

/**
* Sends the payload to the backend.
*
* @param array $payload Submission data.
* @param array $attachments Submission's attached files. Will be ignored.
Expand Down Expand Up @@ -152,7 +198,7 @@ public function submit( $payload = array(), $attachments = array() ) {
$endpoint = $this->endpoint . '/values/' . rawurlencode( $this->tab );
$method = $this->method;

if ( $method === 'POST' || $method === 'PUT' ) {
if ( 'POST' === $method || 'PUT' === $method ) {
$endpoint .= '!A1:Z:append/?valueInputOption=USER_ENTERED';

$headers = $this->get_headers( $backend );
Expand Down Expand Up @@ -216,6 +262,14 @@ private static function flatten_payload( $payload, $path = '' ) {
return $flat;
}

/**
* Returns array values as a flat vector of play key values.
*
* @param mixed $value Payload value.
* @param string $path Hierarchical path to the value.
*
* @return mixed
*/
private static function flatten_value( $value, $path = '' ) {
if ( ! is_array( $value ) ) {
return $value;
Expand Down
11 changes: 8 additions & 3 deletions forms-bridge/addons/gsheets/hooks.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Google Sheets addon hooks
*
* @package formsbridge
*/

if ( ! defined( 'ABSPATH' ) ) {
exit();
Expand All @@ -7,7 +12,7 @@
add_filter(
'forms_bridge_bridge_schema',
function ( $schema, $addon ) {
if ( $addon !== 'gsheets' ) {
if ( 'gsheets' !== $addon ) {
return $schema;
}

Expand Down Expand Up @@ -36,7 +41,7 @@ function ( $schema, $addon ) {
add_filter(
'forms_bridge_template_defaults',
function ( $defaults, $addon, $schema ) {
if ( $addon !== 'gsheets' ) {
if ( 'gsheets' !== $addon ) {
return $defaults;
}

Expand Down Expand Up @@ -183,7 +188,7 @@ function ( $url, $verb ) {
return $url;
}

if ( $verb === 'auth' ) {
if ( 'auth' === $verb ) {
return $url;
}

Expand Down
3 changes: 2 additions & 1 deletion forms-bridge/addons/nextcloud/class-nextcloud-addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Nextcloud_Addon extends Addon {
const BRIDGE = '\FORMS_BRIDGE\Nextcloud_Form_Bridge';

/**
* Addon loader. Set up hooks to skip payload prunes if it comes from a nextcloud bridge.
* Addon loader. Set up hooks to skip payload prunes if it comes from a
* nextcloud bridge.
*/
public function load() {
parent::load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private function add_row( $payload ) {
}

/**
* Submits submission to the backend.
* Sends the payload to the backend.
*
* @param array $payload Submission data.
* @param array $attachments Submission attachments.
Expand Down
14 changes: 5 additions & 9 deletions forms-bridge/addons/nextcloud/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,11 @@ function ( $defaults, $addon, $schema ) {
'value' => 'Basic',
),
array(
'ref' => '#credential',
'name' => 'client_id',
'label' => __( 'User login', 'forms-bridge' ),
'description' => __(
'Either, a user name or email',
'forms-bridge'
),
'type' => 'text',
'required' => true,
'ref' => '#credential',
'name' => 'client_id',
'label' => __( 'User login', 'forms-bridge' ),
'type' => 'text',
'required' => true,
),
array(
'ref' => '#credential',
Expand Down
2 changes: 1 addition & 1 deletion forms-bridge/deps/http
Submodule http updated from 714661 to 25fbd7
2 changes: 1 addition & 1 deletion forms-bridge/deps/plugin
Submodule plugin updated from cfbbf3 to bccdde
18 changes: 17 additions & 1 deletion src/components/Credential/AuthorizeButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useError } from "../../providers/Error";
import { useFetchSettings } from "../../providers/Settings";
import { restUrl } from "../../lib/utils";

const { useMemo } = wp.element;
const { Button } = wp.components;
const apiFetch = wp.apiFetch;
const { __ } = wp.i18n;
Expand All @@ -13,6 +14,21 @@ export default function AuthorizeButton({ addon, data }) {

const fetchSettings = useFetchSettings();

const authorized = useMemo(() => {
if (!(data.access_token && data.expires_at)) return false;

if (data.refresh_token) {
return true;
}

let expirationDate = new Date(data.expires_at);
if (expirationDate.getFullYear() === 1970) {
expirationDate = new Date(data.expires_at * 1000);
}

return Date.now() < expirationDate.getTime();
}, [data.access_token, data.expires_at]);

const revoke = () => {
setLoading(true);

Expand Down Expand Up @@ -65,7 +81,7 @@ export default function AuthorizeButton({ addon, data }) {
.finally(() => setLoading(false));
};

if (data.refresh_token) {
if (authorized) {
return (
<Button
onClick={revoke}
Expand Down
15 changes: 14 additions & 1 deletion src/components/Templates/Wizard/useAuthorizedCredential.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,20 @@ export default function useAuthorizedCredential({ data = {}, fields = [] }) {

const isOauth = data.schema === "Bearer";

const authorized = !isOauth || !!data.refresh_token;
const authorized = useMemo(() => {
if (!(isOauth && data.access_token && data.expires_at)) return false;

if (data.refresh_token) {
return true;
}

let expirationDate = new Date(data.expires_at);
if (expirationDate.getFullYear() === 1970) {
expirationDate = new Date(data.expires_at * 1000);
}

return Date.now() < expirationDate.getTime();
}, [isOauth, data.access_token, data.expires_at]);

const fetchSettings = useFetchSettings();
useEffect(() => {
Expand Down