send_closte_api_request( '/adddomainalias', [ 'domain' => $domain, 'wildcard' => str_starts_with($domain, '*.'), ] ); } /** * This method gets called when a mapped domain is removed. * * @since 2.0.0 * @param string $domain The domain name being removed. * @param int $site_id ID of the site that is receiving that mapping. * @return void */ public function on_remove_domain($domain, $site_id): void { $this->send_closte_api_request( '/deletedomainalias', [ 'domain' => $domain, 'wildcard' => str_starts_with($domain, '*.'), ] ); } /** * This method gets called when a new subdomain is being added. * * This happens every time a new site is added to a network running on subdomain mode. * * @since 2.0.0 * @param string $subdomain The subdomain being added to the network. * @param int $site_id ID of the site that is receiving that mapping. * @return void */ public function on_add_subdomain($subdomain, $site_id) {} /** * This method gets called when a new subdomain is being removed. * * This happens every time a new site is removed to a network running on subdomain mode. * * @since 2.0.0 * @param string $subdomain The subdomain being removed to the network. * @param int $site_id ID of the site that is receiving that mapping. * @return void */ public function on_remove_subdomain($subdomain, $site_id) {} /** * Tests the connection with the API. * * Needs to be implemented by integrations. * * @since 2.0.0 * @return void */ public function test_connection(): void { $response = $this->send_closte_api_request('/adddomainalias', []); if (wu_get_isset($response, 'error') === 'Invalid or empty domain: ') { wp_send_json_success( [ 'message' => __('Access Authorized'), ] ); } $error = new \WP_Error('not-auth', __('Something went wrong', 'wp-multisite-waas')); wp_send_json_error($error); } /** * Sends a request to Closte, with the right API key. * * @since 1.7.3 * @param string $endpoint Endpoint to send the call to. * @param array $data Array containing the params to the call. * @return object */ public function send_closte_api_request($endpoint, $data) { if (defined('CLOSTE_CLIENT_API_KEY') === false) { return (object) [ 'success' => false, 'error' => 'Closte API Key not found.', ]; } $post_fields = [ 'blocking' => true, 'timeout' => 45, 'method' => 'POST', 'body' => array_merge( [ 'apikey' => CLOSTE_CLIENT_API_KEY, ], $data ), ]; $response = wp_remote_post('https://app.closte.com/api/client' . $endpoint, $post_fields); wu_log_add('integration-closte', wp_remote_retrieve_body($response)); if ( ! is_wp_error($response)) { $body = json_decode(wp_remote_retrieve_body($response), true); if (json_last_error() === JSON_ERROR_NONE) { return $body; } return (object) [ 'success' => false, 'error' => 'unknown', ]; } return $response; } /** * Returns the description of this integration. * * @since 2.0.0 * @return string */ public function get_description() { return __('Closte is not just another web hosting who advertise their services as a cloud hosting while still provides fixed plans like in 1995.', 'wp-multisite-waas'); } /** * Returns the logo for the integration. * * @since 2.0.0 * @return string */ public function get_logo() { return wu_get_asset('closte.svg', 'img/hosts'); } }