/** * get a preview of your post, if you have an excerpt it will use that, * otherwise it will pull from the post_content. * If there's a <!-- more --> tag it will use that to mark where to pull through. * @api * @example * ```twig * <p>{{post.get_preview(50)}}</p> * ``` * @param int $len The number of words that WP should use to make the tease. (Isn't this better than [this mess](http://wordpress.org/support/topic/changing-the-default-length-of-the_excerpt-1?replies=14)?). If you've set a post_excerpt on a post, we'll use that for the preview text; otherwise the first X words of the post_content * @param bool $force What happens if your custom post excerpt is longer then the length requested? By default (`$force = false`) it will use the full `post_excerpt`. However, you can set this to true to *force* your excerpt to be of the desired length * @param string $readmore The text you want to use on the 'readmore' link * @param bool|string $strip true for default, false for none, string for list of custom attributes * @param string $end The text to end the preview with (defaults to ...) * @return string of the post preview */ public function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true, $end = '…') { $text = ''; $link = ''; $trimmed = false; $last_p_tag = null; if (isset($this->post_excerpt) && strlen(trim($this->post_excerpt))) { if ($force) { $text = TextHelper::trim_words($this->post_excerpt, $len, false); $trimmed = true; } else { $text = $this->post_excerpt; } } if (!strlen($text) && preg_match('/<!--\\s?more(.*?)?-->/', $this->post_content, $readmore_matches)) { $pieces = explode($readmore_matches[0], $this->post_content); $text = $pieces[0]; if ($force) { $text = TextHelper::trim_words($text, $len, false); $trimmed = true; } $text = do_shortcode($text); } if (!strlen($text)) { $text = TextHelper::trim_words($this->get_content(), $len, false); $trimmed = true; } if (!strlen(trim($text))) { return trim($text); } if ($strip) { $allowable_tags = is_string($strip) ? $strip : null; $text = trim(strip_tags($text, $allowable_tags)); } if (strlen($text)) { $text = trim($text); $last = $text[strlen($text) - 1]; if ($last != '.' && $trimmed) { $text .= $end; } if (!$strip) { $last_p_tag = strrpos($text, '</p>'); if ($last_p_tag !== false) { $text = substr($text, 0, $last_p_tag); } if ($last != '.' && $trimmed) { $text .= $end . ' '; } } $read_more_class = apply_filters('timber/post/get_preview/read_more_class', "read-more"); if ($readmore && isset($readmore_matches) && !empty($readmore_matches[1])) { $link = ' <a href="' . $this->link() . '" class="' . $read_more_class . '">' . trim($readmore_matches[1]) . '</a>'; } elseif ($readmore) { $link = ' <a href="' . $this->link() . '" class="' . $read_more_class . '">' . trim($readmore) . '</a>'; } $text .= apply_filters('timber/post/get_preview/read_more_link', $link); if (!$strip && $last_p_tag && (strpos($text, '<p>') || strpos($text, '<p '))) { $text .= '</p>'; } } return trim($text); }
function testTrimCharacters() { $text = "Sometimes you need to do such weird things like remove all comments from your project."; $trimmed = \Timber\TextHelper::trim_characters($text, 20); $this->assertEquals("Sometimes yo…", $trimmed); }
/** * * * @param Twig_Environment $twig * @return Twig_Environment */ public function add_timber_filters($twig) { /* image filters */ $twig->addFilter(new \Twig_SimpleFilter('resize', array('Timber\\ImageHelper', 'resize'))); $twig->addFilter(new \Twig_SimpleFilter('retina', array('Timber\\ImageHelper', 'retina_resize'))); $twig->addFilter(new \Twig_SimpleFilter('letterbox', array('Timber\\ImageHelper', 'letterbox'))); $twig->addFilter(new \Twig_SimpleFilter('tojpg', array('Timber\\ImageHelper', 'img_to_jpg'))); /* debugging filters */ $twig->addFilter(new \Twig_SimpleFilter('get_class', 'get_class')); $twig->addFilter(new \Twig_SimpleFilter('get_type', 'get_type')); $twig->addFilter(new \Twig_SimpleFilter('print_r', function ($arr) { return print_r($arr, true); })); /* other filters */ $twig->addFilter(new \Twig_SimpleFilter('stripshortcodes', 'strip_shortcodes')); $twig->addFilter(new \Twig_SimpleFilter('array', array($this, 'to_array'))); $twig->addFilter(new \Twig_SimpleFilter('excerpt', 'wp_trim_words')); $twig->addFilter(new \Twig_SimpleFilter('excerpt_chars', array('Timber\\TextHelper', 'trim_characters'))); $twig->addFilter(new \Twig_SimpleFilter('function', array($this, 'exec_function'))); $twig->addFilter(new \Twig_SimpleFilter('pretags', array($this, 'twig_pretags'))); $twig->addFilter(new \Twig_SimpleFilter('sanitize', 'sanitize_title')); $twig->addFilter(new \Twig_SimpleFilter('shortcodes', 'do_shortcode')); $twig->addFilter(new \Twig_SimpleFilter('time_ago', array($this, 'time_ago'))); $twig->addFilter(new \Twig_SimpleFilter('wpautop', 'wpautop')); $twig->addFilter(new \Twig_SimpleFilter('list', array($this, 'add_list_separators'))); $twig->addFilter(new \Twig_SimpleFilter('pluck', array('Timber\\Helper', 'pluck'))); $twig->addFilter(new \Twig_SimpleFilter('relative', function ($link) { return URLHelper::get_rel_url($link, true); })); $twig->addFilter(new \Twig_SimpleFilter('date', array($this, 'intl_date'))); $twig->addFilter(new \Twig_SimpleFilter('truncate', function ($text, $len) { return TextHelper::trim_words($text, $len); })); /* actions and filters */ $twig->addFunction(new \Twig_SimpleFunction('action', function ($context) { $args = func_get_args(); array_shift($args); $args[] = $context; call_user_func_array('do_action', $args); }, array('needs_context' => true))); $twig->addFilter(new \Twig_SimpleFilter('apply_filters', function () { $args = func_get_args(); $tag = current(array_splice($args, 1, 1)); return apply_filters_ref_array($tag, $args); })); $twig->addFunction(new \Twig_SimpleFunction('function', array(&$this, 'exec_function'))); $twig->addFunction(new \Twig_SimpleFunction('fn', array(&$this, 'exec_function'))); $twig->addFunction(new \Twig_SimpleFunction('shortcode', 'do_shortcode')); /* TimberObjects */ $twig->addFunction(new \Twig_SimpleFunction('TimberPost', function ($pid, $PostClass = 'Timber\\Post') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $PostClass($p); } return $pid; } return new $PostClass($pid); })); $twig->addFunction(new \Twig_SimpleFunction('TimberImage', function ($pid = false, $ImageClass = 'Timber\\Image') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $ImageClass($p); } return $pid; } return new $ImageClass($pid); })); $twig->addFunction(new \Twig_SimpleFunction('TimberTerm', function ($pid, $TermClass = 'Timber\\Term') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $TermClass($p); } return $pid; } return new $TermClass($pid); })); $twig->addFunction(new \Twig_SimpleFunction('TimberUser', function ($pid, $UserClass = 'Timber\\User') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $UserClass($p); } return $pid; } return new $UserClass($pid); })); /* TimberObjects Alias */ $twig->addFunction(new \Twig_SimpleFunction('Post', function ($pid, $PostClass = 'Timber\\Post') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $PostClass($p); } return $pid; } return new $PostClass($pid); })); $twig->addFunction(new \Twig_SimpleFunction('Image', function ($pid, $ImageClass = 'Timber\\Image') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $ImageClass($p); } return $pid; } return new $ImageClass($pid); })); $twig->addFunction(new \Twig_SimpleFunction('Term', function ($pid, $TermClass = 'Timber\\Term') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $TermClass($p); } return $pid; } return new $TermClass($pid); })); $twig->addFunction(new \Twig_SimpleFunction('User', function ($pid, $UserClass = 'Timber\\User') { if (is_array($pid) && !Helper::is_array_assoc($pid)) { foreach ($pid as &$p) { $p = new $UserClass($p); } return $pid; } return new $UserClass($pid); })); /* bloginfo and translate */ $twig->addFunction(new \Twig_SimpleFunction('bloginfo', function ($show = '', $filter = 'raw') { return get_bloginfo($show, $filter); })); $twig->addFunction(new \Twig_SimpleFunction('__', function ($text, $domain = 'default') { return __($text, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('translate', function ($text, $domain = 'default') { return translate($text, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('_e', function ($text, $domain = 'default') { return _e($text, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('_n', function ($single, $plural, $number, $domain = 'default') { return _n($single, $plural, $number, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('_x', function ($text, $context, $domain = 'default') { return _x($text, $context, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('_ex', function ($text, $context, $domain = 'default') { return _ex($text, $context, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('_nx', function ($single, $plural, $number, $context, $domain = 'default') { return _nx($single, $plural, $number, $context, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('_n_noop', function ($singular, $plural, $domain = 'default') { return _n_noop($singular, $plural, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('_nx_noop', function ($singular, $plural, $context, $domain = 'default') { return _nx_noop($singular, $plural, $context, $domain); })); $twig->addFunction(new \Twig_SimpleFunction('translate_nooped_plural', function ($nooped_plural, $count, $domain = 'default') { return translate_nooped_plural($nooped_plural, $count, $domain); })); $twig = apply_filters('timber/twig', $twig); /** * get_twig is deprecated, use timber/twig */ $twig = apply_filters('get_twig', $twig); return $twig; }
/** * @deprecated since 1.2.0 * @see TextHelper::close_tags * @param string $html * @return string */ public static function close_tags($html) { return TextHelper::close_tags($html); }