get_instance( new $ai_provider() ); } /** * Get Provider Instance. * * This method presents us a work-around with * correctly mocking and writing tests for the * `get_provider` method. * * @since 1.1.2 * * @param Provider $provider * @return Provider */ protected function get_instance( Provider $provider ): Provider { /** * Filter AI Provider. * * @since 1.0.0 * * @param Provider $provider AI Provider. * @return Provider */ return apply_filters( 'apbe_ai_provider', $provider ); } /** * Run AI Provider. * * @since 1.0.0 * * @param mixed[] $payload JSON Payload. * @return string|\WP_Error */ public function run( $payload ) { // Sanitize Prompt. $payload['content'] = html_entity_decode( wp_strip_all_tags( apply_filters( 'the_content', $payload['content'] ?? '' ), true ) ); try { return $this->get_provider()->run( $payload ); } catch ( Exception $e ) { return new WP_Error( 'apbe-run-error', sprintf( 'Server Error: %s', $e->getMessage() ), [ 'status' => 500 ] ); } } }