true, // 'html' by default, just highlight that it's secure to not escape TWIG variable values in PHP 'autoescape' => 'html', ); private ?FilesystemLoader $filesystem_loader; private ?Environment $environment; public function __construct( string $templates_folder, Logger $logger, Settings_Storage $settings, WP_Filesystem_Base $wp_filesystem_base ) { parent::__construct( $templates_folder, $logger, $settings, $wp_filesystem_base ); $this->filesystem_loader = null; $this->environment = null; } /** * @param array $args * @throws Exception */ protected function render( string $template_name, array $args ): string { return $this->get_twig()->render( $template_name . '.' . $this->get_extension(), $args ); } protected function get_extension(): string { return 'twig'; } protected function get_cache_file( string $unique_id ): string { // no caching enabled. return ''; } /** * @return mixed[] */ protected function get_custom_functions(): array { return array( array( 'wp_interactivity_state', function ( string $store_namespace, array $state = array() ): void { if ( false === function_exists( 'wp_interactivity_state' ) ) { return; } wp_interactivity_state( $store_namespace, $state ); }, ), array( 'wp_interactivity_data_wp_context', function ( array $context ): void { if ( false === function_exists( 'wp_interactivity_data_wp_context' ) ) { return; } // @phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo wp_interactivity_data_wp_context( $context ); }, ), array( 'paginate_links', function ( array $args ): void { $paginate_links = paginate_links( $args ); // null if less than 2 pages. if ( false === is_string( $paginate_links ) ) { return; } // @phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $paginate_links; }, ), array( 'print_r', function ( $data ): void { // @phpcs:ignore print_r( $data ); }, ), array( '_query_argument', function ( string $arg_name = '' ) { $arg_value = Query_Arguments::get_string_for_non_action( $arg_name ); return sanitize_text_field( $arg_value ); }, ), array( '_site_url', fn( string $path = '', $scheme = null ) => site_url( $path, $scheme ), ), array( '_home_url', fn( string $path = '', $scheme = null ) => home_url( $path, $scheme ), ), array( '_is_user_logged_in', fn() => is_user_logged_in(), ), array( '_is_user_with_role', function ( string $role, string $user_id = '' ) { $user = '' !== $user_id ? get_user_by( 'id', $user_id ) : wp_get_current_user(); return false !== $user && in_array( $role, $user->roles, true ); }, ), array( '__', fn( string $label, string $text_domain = '' ) => Plugin::get_label_translation( $label, $text_domain ), ), ); } /** * @return mixed[] */ protected function get_custom_filters(): array { return array( array( 'translate', fn( string $label, string $text_domain = '' ) => Plugin::get_label_translation( $label, $text_domain ), ), ); } protected function init_twig(): Environment { $this->filesystem_loader = new FilesystemLoader( $this->get_templates_folder() ); $this->environment = new Environment( $this->filesystem_loader, self::ENVIRONMENT_ARGS ); // reminder: TwigFunctions automatically escape the output // (as long you not pass ['is_safe' => ['html']] to the constructor). $custom_functions = Plugin::apply_filters( array( 'advanced_views/twig/custom_functions', 'acf_views/twig/custom_functions', ), $this->get_custom_functions() ); $custom_functions = arr( $custom_functions ); $custom_filters = Plugin::apply_filters( array( 'advanced_views/twig/custom_filters', 'acf_views/twig/custom_filters', ), $this->get_custom_filters() ); $custom_filters = arr( $custom_filters ); foreach ( $custom_functions as $custom_function ) { $custom_function = arr( $custom_function ); $function_name = string( $custom_function, 0 ); $function_callback = key_exists( 1, $custom_function ) && is_callable( $custom_function[1] ) ? $custom_function[1] : null; $function_args = arr( $custom_function, 2 ); if ( '' === $function_name || null === $function_callback ) { continue; } $this->environment->addFunction( new TwigFunction( $function_name, $function_callback, $function_args ) ); } foreach ( $custom_filters as $custom_filter ) { $custom_filter = arr( $custom_filter ); $filter_name = string( $custom_filter, 0 ); $filter_callback = key_exists( 1, $custom_filter ) && is_callable( $custom_filter[1] ) ? $custom_filter[1] : null; $filter_args = arr( $custom_filter, 2 ); if ( '' === $filter_name || null === $filter_callback ) { continue; } $this->environment->addFilter( new TwigFilter( $filter_name, $filter_callback, $filter_args ) ); } return $this->environment; } protected function get_twig(): Environment { if ( null === $this->environment ) { return $this->init_twig(); } return $this->environment; } public function is_available(): bool { // always available. return true; } }