<commit_before><?php

namespace Salaros\Wordpress\Template;

class TwigExtensions extends \Twig_Extension
{
    private static $default_text_domain;

    public static function set_default_text_domain($domain) {
        self::$default_text_domain = $domain;
    }

    public function getName()
    {
        return 'wordpress';
    }

    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction( 'slugToPage', array($this, 'get_page_by_slug') ),
            new \Twig_SimpleFunction( 'slugToUrl', array($this, 'get_page_url_by_slug') ),
        );
    }

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter( 'translate',  array($this, 'get_translation_with_context') ),
        );
    }

    /**
    * Translate function used as Twig filter for gettext-driven translations
    * @param  string $label           The label to translate
    * @param  string [$domain= null]  Domain of the translation, fallbacks on default text domain if null
    * @return string                  Translated label
    */
    public static function get_translation($label, $domain = null) {

        if(empty($domain))
            $domain = self::$default_text_domain;

        $translation = __($label, $domain);
        return ($translation !== $label)
            ? $translation
            : __($label);
    }

    /**
    * Translate function used as Twig filter for gettext-driven translations
    * @param  string $label           The label to translate
    * @param  string [$context= null] Context for the translation, fallbacks on regular translation if null
    * @param  string [$domain= null]  Domain of the translation, fallbacks on default text domain if null
    * @return string                  Translated label
    */
    public static function get_translation_with_context($label, $context = null, $domain = null) {

        if (empty($context))
            return self::get_translation($label, $domain);

        if(empty($domain))
            $domain = self::$default_text_domain;

        $translation = _x($label, $context, $domain);
        return ($translation !== $label)
            ? $translation
            : _x($label, $context);
    }

    /**
    * Returns WP_Post object (or something else) having the given slug
    * @param  string $page_slug            The slug
    * @param  string [$post_type = 'page'] Post type of the item (post, page etc)
    * @param  string [$output = OBJECT]    Tells the funcion the preferred type for the returned object
    * @return WP_Post The object with the given slug, casted to the type specified by output parameter (default is WP_Post )
    */
    public static function get_page_by_slug($page_slug, $post_type = 'page', $output = OBJECT ) {
        global $wpdb;
        $sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'" ;
        $page = $wpdb->get_var( $wpdb->prepare($sql, $page_slug, $post_type) );
        return ( $page )
            ? get_post($page, $output)
            : null;
    }

    /**
    * Returns the URL of the page, post or whatever having the given slug
    * @param  string $page_slug            The slug
    * @param  string [$post_type = 'page'] Post type of the item (post, page etc)
    * @param  string [$output = OBJECT]    Tells the funcion the preferred type for the returned object
    * @return WP_Post The URL of the page
    */
    public function get_page_url_by_slug($page_slug, $post_type = 'page', $output = OBJECT ) {
        return get_permalink(get_page_by_slug($page_slug));
    }
}<commit_msg>added one function (slutToID) and one filter (dump, which pretty-prints the variable in Twig) in TwigExtensions class<commit_after><?php

namespace Salaros\Wordpress\Template;

class TwigExtensions extends \Twig_Extension
{
    private static $default_text_domain;

    public static function set_default_text_domain($domain) {
        self::$default_text_domain = $domain;
    }

    public function getName()
    {
        return 'wordpress';
    }

    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction( 'slugToPage', array($this, 'get_page_by_slug') ),
            new \Twig_SimpleFunction( 'slugToUrl', array($this, 'get_page_url_by_slug') ),
            new \Twig_SimpleFunction( 'slugToID', array($this, 'get_page_id_by_slug') ),
        );
    }

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter( 'translate',  array($this, 'get_translation_with_context') ),
            new \Twig_SimpleFilter( 'dump',  array($this, 'get_dump_info') ),
        );
    }

    /**
    * Translate function used as Twig filter for gettext-driven translations
    * @param  string $label           The label to translate
    * @param  string [$domain= null]  Domain of the translation, fallbacks on default text domain if null
    * @return string                  Translated label
    */
    public static function get_translation($label, $domain = null) {

        if(empty($domain))
            $domain = self::$default_text_domain;

        $translation = __($label, $domain);
        return ($translation !== $label)
            ? $translation
            : __($label);
    }
    
    public static function get_dump_info($variable) {
        ob_start();
        var_dump($variable);
        $result = ob_get_clean();
        return sprintf("<pre>%s</pre>", $result);
    }

    /**
    * Translate function used as Twig filter for gettext-driven translations
    * @param  string $label           The label to translate
    * @param  string [$context= null] Context for the translation, fallbacks on regular translation if null
    * @param  string [$domain= null]  Domain of the translation, fallbacks on default text domain if null
    * @return string                  Translated label
    */
    public static function get_translation_with_context($label, $context = null, $domain = null) {

        if (empty($context))
            return self::get_translation($label, $domain);

        if(empty($domain))
            $domain = self::$default_text_domain;

        $translation = _x($label, $context, $domain);
        return ($translation !== $label)
            ? $translation
            : _x($label, $context);
    }

    /**
    * Returns WP_Post object (or something else) having the given slug
    * @param  string $page_slug            The slug
    * @param  string [$post_type = 'page'] Post type of the item (post, page etc)
    * @param  string [$output = OBJECT]    Tells the funcion the preferred type for the returned object
    * @return WP_Post The object with the given slug, casted to the type specified by output parameter (default is WP_Post )
    */
    public static function get_page_by_slug($page_slug, $post_type = 'page', $output = OBJECT ) {
        global $wpdb;
        $sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'" ;
        $page = $wpdb->get_var( $wpdb->prepare($sql, $page_slug, $post_type) );
        return ( $page )
            ? get_post($page, $output)
            : null;
    }

    /**
    * Returns the URL of the page, post or whatever having the given slug
    * @param  string $page_slug            The slug
    * @param  string [$post_type = 'page'] Post type of the item (post, page etc)
    * @param  string [$output = OBJECT]    Tells the funcion the preferred type for the returned object
    * @return WP_Post The URL of the page
    */
    public function get_page_url_by_slug($page_slug, $post_type = 'page', $output = OBJECT ) {
        return get_permalink($this->get_page_by_slug($page_slug));
    }
    
    public function get_page_id_by_slug($page_slug) {
        $page = get_page_by_path($page_slug);
        return ($page)
            ? $page->ID
            : null;
    }
}
{'fraction_non_alphanumeric': 0.10447195553309753, 'fraction_numerical': 0.0, 'mean_word_length': 2.5662162162162163, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942710', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2243, 'n_words': 700}