|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Zulip_Addon |
| 4 | + * |
| 5 | + * @package formsbridge |
| 6 | + */ |
| 7 | + |
| 8 | +namespace FORMS_BRIDGE; |
| 9 | + |
| 10 | +use Exception; |
| 11 | + |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { |
| 13 | + exit(); |
| 14 | +} |
| 15 | + |
| 16 | +require_once 'class-zulip-form-bridge.php'; |
| 17 | +require_once 'hooks.php'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Zulip addon class |
| 21 | + */ |
| 22 | +class Zulip_Addon extends Addon { |
| 23 | + |
| 24 | + /** |
| 25 | + * Holds the addon's title. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + public const TITLE = 'Zulip'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Holds the addon's name. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + public const NAME = 'zulip'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Holds the addon's custom bridge class. |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + public const BRIDGE = '\FORMS_BRIDGE\Zulip_Form_Bridge'; |
| 44 | + |
| 45 | + /** |
| 46 | + * Holds the Zulip OAS public URL. |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + public const OAS_URL = 'https://raw.githubusercontent.com/zulip/zulip/refs/heads/main/zerver/openapi/zulip.yaml'; |
| 51 | + |
| 52 | + /** |
| 53 | + * Performs a request against the backend to check the connexion status. |
| 54 | + * |
| 55 | + * @param string $backend Backend name. |
| 56 | + * |
| 57 | + * @return boolean |
| 58 | + */ |
| 59 | + public function ping( $backend ) { |
| 60 | + $bridge = new Zulip_Form_Bridge( |
| 61 | + array( |
| 62 | + 'name' => '__zulip-' . time(), |
| 63 | + 'endpoint' => '/api/v1/streams', |
| 64 | + 'method' => 'GET', |
| 65 | + 'backend' => $backend, |
| 66 | + ), |
| 67 | + 'zulip' |
| 68 | + ); |
| 69 | + |
| 70 | + $response = $bridge->submit(); |
| 71 | + |
| 72 | + if ( is_wp_error( $response ) ) { |
| 73 | + Logger::log( 'Zulip backend ping error response', Logger::ERROR ); |
| 74 | + Logger::log( $response, Logger::ERROR ); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Performs a GET request against the backend endpoint and retrive the response data. |
| 83 | + * |
| 84 | + * @param string $endpoint API endpoint. |
| 85 | + * @param string $backend Backend name. |
| 86 | + * |
| 87 | + * @return array|WP_Error |
| 88 | + */ |
| 89 | + public function fetch( $endpoint, $backend ) { |
| 90 | + $bridge = new Zulip_Form_Bridge( |
| 91 | + array( |
| 92 | + 'name' => '__zulip-' . time(), |
| 93 | + 'endpoint' => $endpoint, |
| 94 | + 'method' => 'GET', |
| 95 | + 'backend' => $backend, |
| 96 | + ), |
| 97 | + 'zulip' |
| 98 | + ); |
| 99 | + |
| 100 | + return $bridge->submit(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Performs an introspection of the backend endpoint and returns API fields. |
| 105 | + * |
| 106 | + * @param string $endpoint API endpoint. |
| 107 | + * @param string $backend Backend name. |
| 108 | + * @param string|null $method HTTP method. |
| 109 | + * |
| 110 | + * @return array List of fields and content type of the endpoint. |
| 111 | + */ |
| 112 | + public function get_endpoint_schema( $endpoint, $backend, $method = null ) { |
| 113 | + if ( function_exists( 'yaml_parse' ) ) { |
| 114 | + $response = wp_remote_get( self::OAS_URL ); |
| 115 | + |
| 116 | + if ( ! is_wp_error( $response ) ) { |
| 117 | + $data = yaml_parse( $response['body'] ); |
| 118 | + |
| 119 | + if ( $data ) { |
| 120 | + try { |
| 121 | + $oa_explorer = new OpenAPI( $data ); |
| 122 | + |
| 123 | + $method = strtolower( $method ?? 'post' ); |
| 124 | + $path = preg_replace( '', '', $endpoint ); |
| 125 | + $source = in_array( $method, array( 'post', 'put', 'patch' ), true ) ? 'body' : 'query'; |
| 126 | + $params = $oa_explorer->params( $path, $method, $source ); |
| 127 | + |
| 128 | + return $params ?: array(); |
| 129 | + } catch ( Exception ) { |
| 130 | + // do nothing. |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if ( '/api/v1/messages' !== $endpoint ) { |
| 137 | + return array(); |
| 138 | + } |
| 139 | + |
| 140 | + return array( |
| 141 | + array( |
| 142 | + 'name' => 'type', |
| 143 | + 'schema' => array( |
| 144 | + 'type' => 'string', |
| 145 | + 'enum' => array( 'direct', 'stream' ), |
| 146 | + ), |
| 147 | + 'required' => true, |
| 148 | + ), |
| 149 | + array( |
| 150 | + 'name' => 'to', |
| 151 | + 'schema' => array( |
| 152 | + 'type' => 'array', |
| 153 | + 'items' => array( |
| 154 | + 'type' => array( 'string', 'integer' ), |
| 155 | + ), |
| 156 | + ), |
| 157 | + 'required' => true, |
| 158 | + ), |
| 159 | + array( |
| 160 | + 'name' => 'content', |
| 161 | + 'schema' => array( 'type' => 'string' ), |
| 162 | + 'required' => true, |
| 163 | + ), |
| 164 | + array( |
| 165 | + 'name' => 'topic', |
| 166 | + 'schema' => array( |
| 167 | + 'type' => 'string', |
| 168 | + 'default' => '(no topic)', |
| 169 | + ), |
| 170 | + ), |
| 171 | + array( |
| 172 | + 'name' => 'queue_id', |
| 173 | + 'schema' => array( 'type' => 'string' ), |
| 174 | + ), |
| 175 | + array( |
| 176 | + 'name' => 'local_id', |
| 177 | + 'schema' => array( 'type' => 'string' ), |
| 178 | + ), |
| 179 | + array( |
| 180 | + 'name' => 'read_by_sender', |
| 181 | + 'schema' => array( 'type' => 'boolean' ), |
| 182 | + ), |
| 183 | + ); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +Zulip_Addon::setup(); |
0 commit comments