bloqhead
9/9/2015 - 11:06 AM

WordPress escape function extension for Twig. Escape functions as twig filters and functions.

WordPress escape function extension for Twig. Escape functions as twig filters and functions.

<?php


class Twig_Extension_WP_basic_functions extends Twig_Extension
{
    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return array(
            new Twig_SimpleFunction('esc_html', 'esc_html'),
            new Twig_SimpleFunction('esc_html__', 'esc_html__'),
            new Twig_SimpleFunction('esc_html_e', 'esc_html_e'),
            new Twig_SimpleFunction('esc_html_x', 'esc_html_x'),
            new Twig_SimpleFunction('esc_attr', 'esc_attr'),
            new Twig_SimpleFunction('esc_url', 'esc_url'),
            new Twig_SimpleFunction('esc_url_raw', 'esc_url_raw'),
            new Twig_SimpleFunction('esc_js', 'esc_js'),
            new Twig_SimpleFunction('esc_textarea', 'esc_textarea'),
            new Twig_SimpleFunction('sanitize_email', 'sanitize_email'),
            new Twig_SimpleFunction('antispambot', 'antispambot'),
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getFilters()
    {
        return array(
            new Twig_SimpleFilter( 'esc_html', 'esc_html' ),
            new Twig_SimpleFilter( 'esc_attr', 'esc_attr' ),
            new Twig_SimpleFilter( 'esc_url', 'esc_url' ),
            new Twig_SimpleFilter('esc_url_raw', 'esc_url_raw'),
            new Twig_SimpleFilter( 'esc_js', 'esc_js' ),
            new Twig_SimpleFilter('esc_textarea', 'esc_textarea'),
            new Twig_SimpleFilter('nl2br', 'nl2br'),
            new Twig_SimpleFilter('sanitize_email', 'sanitize_email'),
            new Twig_SimpleFilter('antispambot', 'antispambot'),
        );
    }


    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'wp_escape';
    }
}