$email, 'dominios' => $domains, ) ); } /** * Canjea el código. Si sale bien, devuelve `apiKey`, `nombreDelBot` y `dominios`. * * @param string $email * @param string $code * @param string[] $domains * @return array{ok: bool, status: int, error: string, data: array} */ public static function activate( $email, $code, array $domains ) { return self::post( '/api/plugin/activar', array( 'correo' => $email, 'codigo' => $code, 'dominios' => $domains, ) ); } /** * @param string $path * @param array $body * @return array{ok: bool, status: int, error: string, data: array} */ private static function post( $path, array $body ) { $response = wp_remote_post( Doxi_Settings::api_url() . $path, array( 'timeout' => self::TIMEOUT, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => wp_json_encode( $body ), 'data_format' => 'body', ) ); if ( is_wp_error( $response ) ) { return array( 'ok' => false, 'status' => 0, 'error' => 'sin_conexion', 'data' => array(), ); } $status = (int) wp_remote_retrieve_response_code( $response ); $data = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $data ) ) { $data = array(); } $ok = $status >= 200 && $status < 300; return array( 'ok' => $ok, 'status' => $status, 'error' => $ok ? '' : self::reason( $status, $data ), 'data' => $data, ); } /** * El motivo que manda el backend en `motivo`. Si no viene, se deduce lo poco que se * puede deducir del código HTTP. * * @param int $status * @param array $data */ private static function reason( $status, array $data ) { if ( isset( $data['motivo'] ) && is_string( $data['motivo'] ) && '' !== $data['motivo'] ) { return sanitize_key( $data['motivo'] ); } if ( 429 === $status ) { return 'demasiados_intentos'; } return 'error_desconocido'; } }