|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FORMS_BRIDGE; |
| 4 | + |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { |
| 6 | + exit(); |
| 7 | +} |
| 8 | + |
| 9 | +require_once 'class-grist-form-bridge.php'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Grist addon class. |
| 13 | + */ |
| 14 | +class Grist_Addon extends Addon { |
| 15 | + |
| 16 | + /** |
| 17 | + * Handles the addon's title. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + const TITLE = 'Grist'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Handles the addon's name. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + const NAME = 'grist'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Handles the addon's custom bridge class. |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + const BRIDGE = '\FORMS_BRIDGE\Grist_Form_Bridge'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Performs a request against the backend to check the connection status. |
| 39 | + * |
| 40 | + * @param string $backend Backend name. |
| 41 | + * |
| 42 | + * @return boolean |
| 43 | + */ |
| 44 | + public function ping( $backend ) { |
| 45 | + $bridge = new Grist_Form_Bridge( |
| 46 | + array( |
| 47 | + 'name' => '__grist-' . time(), |
| 48 | + 'backend' => $backend, |
| 49 | + 'endpoint' => '/api/docs', |
| 50 | + 'method' => 'GET', |
| 51 | + ) |
| 52 | + ); |
| 53 | + |
| 54 | + $backend = $bridge->backend; |
| 55 | + if ( ! $backend ) { |
| 56 | + Logger::log( 'Grist backend ping error: Bridge has no valid backend', Logger::ERROR ); |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + $credential = $backend->credential; |
| 61 | + if ( ! $credential ) { |
| 62 | + Logger::log( 'Grist backend ping error: Backend has no valid credential', Logger::ERROR ); |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + $access_token = $credential->get_access_token(); |
| 67 | + |
| 68 | + if ( ! $access_token ) { |
| 69 | + Logger::log( 'Grist backend ping error: Unable to recover the credential access token', Logger::ERROR ); |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Performs a GET request against the backend endpoint and retrive the response data. |
| 78 | + * |
| 79 | + * @param string $endpoint Grist endpoint. |
| 80 | + * @param string $backend Backend name. |
| 81 | + * |
| 82 | + * @return array|WP_Error |
| 83 | + */ |
| 84 | + public function fetch( $endpoint, $backend ) { |
| 85 | + $backend = FBAPI::get_backend( $backend ); |
| 86 | + if ( ! $backend ) { |
| 87 | + return new WP_Error( 'invalid_backend' ); |
| 88 | + } |
| 89 | + |
| 90 | + $credential = $backend->credential; |
| 91 | + if ( ! $credential ) { |
| 92 | + return new WP_Error( 'invalid_credential' ); |
| 93 | + } |
| 94 | + |
| 95 | + $access_token = $credential->get_access_token(); |
| 96 | + if ( ! $access_token ) { |
| 97 | + return new WP_Error( 'invalid_credential' ); |
| 98 | + } |
| 99 | + |
| 100 | + $response = http_bridge_get( |
| 101 | + $backend->base_url . $endpoint, |
| 102 | + array(), |
| 103 | + array( |
| 104 | + 'Authorization' => "Bearer {$access_token}", |
| 105 | + 'Accept' => 'application/json', |
| 106 | + ) |
| 107 | + ); |
| 108 | + |
| 109 | + if ( is_wp_error( $response ) ) { |
| 110 | + return $response; |
| 111 | + } |
| 112 | + |
| 113 | + return $response; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Performs an introspection of the backend API and returns a list of available endpoints. |
| 118 | + * |
| 119 | + * @param string $backend Target backend name. |
| 120 | + * @param string|null $method HTTP method. |
| 121 | + * |
| 122 | + * @return array|WP_Error |
| 123 | + */ |
| 124 | + public function get_endpoints( $backend, $method = null ) { |
| 125 | + // Grist doesn't have a standard endpoint discovery API |
| 126 | + // Return common Grist API endpoints |
| 127 | + return array( |
| 128 | + '/api/docs', |
| 129 | + '/api/tables', |
| 130 | + '/api/records', |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Performs an introspection of the backend endpoint and returns API fields |
| 136 | + * and accepted content type. |
| 137 | + * |
| 138 | + * @param string $endpoint Grist endpoint. |
| 139 | + * @param string $backend Backend name. |
| 140 | + * @param string|null $method HTTP method. |
| 141 | + * |
| 142 | + * @return array List of fields and content type of the endpoint. |
| 143 | + */ |
| 144 | + public function get_endpoint_schema( $endpoint, $backend, $method = null ) { |
| 145 | + if ( 'POST' !== $method ) { |
| 146 | + return array(); |
| 147 | + } |
| 148 | + |
| 149 | + $bridge = null; |
| 150 | + $bridges = FBAPI::get_addon_bridges( self::NAME ); |
| 151 | + foreach ( $bridges as $candidate ) { |
| 152 | + $data = $candidate->data(); |
| 153 | + if ( ! $data ) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + if ( |
| 158 | + $data['endpoint'] === $endpoint && |
| 159 | + $data['backend'] === $backend |
| 160 | + ) { |
| 161 | + /** |
| 162 | + * Current bridge. |
| 163 | + * |
| 164 | + * @var Grist_Form_Bridge |
| 165 | + */ |
| 166 | + $bridge = $candidate; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if ( ! isset( $bridge ) ) { |
| 171 | + return array(); |
| 172 | + } |
| 173 | + |
| 174 | + $fields = $bridge->get_fields(); |
| 175 | + |
| 176 | + if ( is_wp_error( $fields ) ) { |
| 177 | + return array(); |
| 178 | + } |
| 179 | + |
| 180 | + $schema = array(); |
| 181 | + foreach ( $fields as $field ) { |
| 182 | + $schema[] = array( |
| 183 | + 'name' => $field['name'], |
| 184 | + 'schema' => array( 'type' => $field['type'] ), |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + return $schema; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +Grist_Addon::setup(); |
0 commit comments