public function __construct($posts = array(), $post_class = '\\Timber\\Post') { $returned_posts = array(); if (is_null($posts)) { $posts = array(); } foreach ($posts as $post_object) { $post_class_use = $post_class; if (is_array($post_class)) { $post_type = get_post_type($post_object); $post_class_use = '\\Timber\\Post'; if (isset($post_class[$post_type])) { $post_class_use = $post_class[$post_type]; } else { if (is_array($post_class)) { Helper::error_log($post_type . ' of ' . $post_object->ID . ' not found in ' . print_r($post_class, true)); } else { Helper::error_log($post_type . ' not found in ' . $post_class); } } } // Don't create yet another object if $post_object is already of the right type if (is_a($post_object, $post_class_use)) { $post = $post_object; } else { $post = new $post_class_use($post_object); } if (isset($post->ID)) { $returned_posts[] = $post; } } $returned_posts = self::maybe_set_preview($returned_posts); parent::__construct($returned_posts, $flags = 0, 'Timber\\PostsIterator'); }
public function __construct($query = false, $posts_class = 'TimberPost') { add_action('pre_get_posts', array($this, 'fix_number_posts_wp_quirk')); if ($posts_class) { $this->_posts_class = $posts_class; } if (is_a($query, 'WP_Query')) { // We got a full-fledged WP Query, look no further! $the_query = $query; } elseif (false === $query) { // If query is explicitly set to false, use the main loop global $wp_query; $the_query =& $wp_query; //if we're on a custom posts page? $the_query = self::handle_maybe_custom_posts_page($the_query); } elseif (Helper::is_array_assoc($query) || is_string($query) && strstr($query, '=')) { // We have a regularly formed WP query string or array to use $the_query = new \WP_Query($query); } elseif (is_numeric($query) || is_string($query)) { // We have what could be a post name or post ID to pull out $the_query = self::get_query_from_string($query); } elseif (is_array($query) && count($query) && (is_integer($query[0]) || is_string($query[0]))) { // We have a list of pids (post IDs) to extract from $the_query = self::get_query_from_array_of_ids($query); } elseif (is_array($query) && empty($query)) { // it's an empty array $the_query = array(); } else { Helper::error_log('I have failed you! in ' . basename(__FILE__) . '::' . __LINE__); Helper::error_log($query); // We have failed hard, at least let get something. $the_query = new \WP_Query(); } $this->_query = $the_query; }
/** * Performs the actual image manipulation, * including saving the target file. * * @param string $load_filename filepath (not URL) to source file * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) * @param string $save_filename filepath (not URL) where result file should be saved * (ex: /src/var/www/wp-content/uploads/my-pic@2x.jpg) * @return bool true if everything went fine, false otherwise */ function run($load_filename, $save_filename) { $image = wp_get_image_editor($load_filename); if (!is_wp_error($image)) { $current_size = $image->get_size(); $src_w = $current_size['width']; $src_h = $current_size['height']; // Get ratios $w = $src_w * $this->factor; $h = $src_h * $this->factor; $image->crop(0, 0, $src_w, $src_h, $w, $h); $result = $image->save($save_filename); if (is_wp_error($result)) { // @codeCoverageIgnoreStart Helper::error_log('Error resizing image'); Helper::error_log($result); return false; // @codeCoverageIgnoreEnd } else { return true; } } else { if (isset($image->error_data['error_loading_image'])) { Helper::error_log('Error loading ' . $image->error_data['error_loading_image']); } else { Helper::error_log($image); } } return false; }
/** * * * @return string */ public function call() { $args = $this->_parse_args(func_get_args(), $this->_args); $callable = isset($this->_class) ? array($this->_class, $this->_function) : $this->_function; if ($this->_use_ob) { return Helper::ob_function($callable, $args); } else { return call_user_func_array($callable, $args); } }
/** * @param string|array $args * @param array $maybe_args * @param string $TermClass * @return mixed */ public static function get_terms($args = null, $maybe_args = array(), $TermClass = '\\Timber\\Term') { if (is_string($maybe_args) && !strstr($maybe_args, '=')) { //the user is sending the $TermClass in the second argument $TermClass = $maybe_args; } if (is_string($maybe_args) && strstr($maybe_args, '=')) { parse_str($maybe_args, $maybe_args); } if (is_string($args) && strstr($args, '=')) { //a string and a query string! $parsed = self::get_term_query_from_query_string($args); if (is_array($maybe_args)) { $parsed->args = array_merge($parsed->args, $maybe_args); } return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); } else { if (is_string($args)) { //its just a string with a single taxonomy $parsed = self::get_term_query_from_string($args); if (is_array($maybe_args)) { $parsed->args = array_merge($parsed->args, $maybe_args); } return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); } else { if (is_array($args) && Helper::is_array_assoc($args)) { //its an associative array, like a good ole query $parsed = self::get_term_query_from_assoc_array($args); return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); } else { if (is_array($args)) { //its just an array of strings or IDs (hopefully) $parsed = self::get_term_query_from_array($args); if (is_array($maybe_args)) { $parsed->args = array_merge($parsed->args, $maybe_args); } return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); } else { if (is_null($args)) { return self::handle_term_query(get_taxonomies(), array(), $TermClass); } } } } } return null; }
/** * @deprecated 0.20.0 use link() instead * @codeCoverageIgnore * @return string */ public function permalink() { Helper::warn('post.permalink has been removed, please use post.link'); return $this->link(); }
/** * Performs the actual image manipulation, * including saving the target file. * * @param string $load_filename filepath (not URL) to source file * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) * @param string $save_filename filepath (not URL) where result file should be saved * (ex: /src/var/www/wp-content/uploads/my-pic-lbox-300x200-FF3366.jpg) * @return bool true if everything went fine, false otherwise */ public function run($load_filename, $save_filename) { $w = $this->w; $h = $this->h; $bg = imagecreatetruecolor($w, $h); $c = self::hexrgb($this->color); $bgColor = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']); imagefill($bg, 0, 0, $bgColor); $image = wp_get_image_editor($load_filename); if (!is_wp_error($image)) { $current_size = $image->get_size(); $quality = $image->get_quality(); $ow = $current_size['width']; $oh = $current_size['height']; $new_aspect = $w / $h; $old_aspect = $ow / $oh; if ($new_aspect > $old_aspect) { //taller than goal $h_scale = $h / $oh; $owt = $ow * $h_scale; $y = 0; $x = $w / 2 - $owt / 2; $oht = $h; $image->crop(0, 0, $ow, $oh, $owt, $oht); } else { $w_scale = $w / $ow; $oht = $oh * $w_scale; $x = 0; $y = $h / 2 - $oht / 2; $owt = $w; $image->crop(0, 0, $ow, $oh, $owt, $oht); } $result = $image->save($save_filename); $func = 'imagecreatefromjpeg'; $save_func = 'imagejpeg'; $ext = pathinfo($save_filename, PATHINFO_EXTENSION); if ($ext == 'gif') { $func = 'imagecreatefromgif'; $save_func = 'imagegif'; } else { if ($ext == 'png') { $func = 'imagecreatefrompng'; $save_func = 'imagepng'; if ($quality > 9) { $quality = $quality / 10; $quality = round(10 - $quality); } } } $image = $func($save_filename); imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht); if ($save_func === 'imagegif') { return $save_func($bg, $save_filename); } return $save_func($bg, $save_filename, $quality); } Helper::error_log($image); return false; }
/** * Add route. * * @param string $route * @param callable $callback * @param array $args * @deprecated since 0.20.0 and will be removed in 1.1 */ public static function add_route($route, $callback, $args = array()) { Helper::warn('Timber::add_route (and accompanying methods for load_view, etc. Have been deprecated and will soon be removed. Please update your theme with Route::map. You can read more in the 1.0 Upgrade Guide: https://github.com/timber/timber/wiki/1.0-Upgrade-Guide'); \Routes::map($route, $callback, $args); }
/** * Performs the actual image manipulation, * including saving the target file. * * @param string $load_filename filepath (not URL) to source file * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) * @param string $save_filename filepath (not URL) where result file should be saved * (ex: /src/var/www/wp-content/uploads/my-pic-300x200-c-default.jpg) * @return boolean|null true if everything went fine, false otherwise */ public function run($load_filename, $save_filename) { //should be resized by gif resizer if (\Timber\ImageHelper::is_animated_gif($load_filename)) { //attempt to resize //return if successful //proceed if not $gif = self::run_animated_gif($load_filename, $save_filename); if ($gif) { return true; } } $image = wp_get_image_editor($load_filename); if (!is_wp_error($image)) { $crop = self::get_target_sizes($load_filename); $image->crop($crop['x'], $crop['y'], $crop['src_w'], $crop['src_h'], $crop['target_w'], $crop['target_h']); $result = $image->save($save_filename); if (is_wp_error($result)) { // @codeCoverageIgnoreStart Helper::error_log('Error resizing image'); Helper::error_log($result); return false; // @codeCoverageIgnoreEnd } else { return true; } } else { if (isset($image->error_data['error_loading_image'])) { // @codeCoverageIgnoreStart Helper::error_log('Error loading ' . $image->error_data['error_loading_image']); } else { Helper::error_log($image); // @codeCoverageIgnoreEnd } } }
/** * * * @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; }
function testCloseTagsWithSelfClosingTags() { $p = '<p>My thing is this <hr>Whatever'; $html = \Timber\Helper::close_tags($p); $this->assertEquals('<p>My thing is this <hr />Whatever</p>', $html); }
/** * @deprecated since 0.21.9 use src() instead * @return string */ public function get_url($size = '') { Helper::warn('{{image.get_url}} is deprecated and will be removed in 1.1; use {{image.src}}'); return $this->src($size); }
/** * @param string $post_type * @param string|array $post_class * * @return string */ public static function get_post_class($post_type, $post_class = '\\Timber\\Post') { $post_class = apply_filters('Timber\\PostClassMap', $post_class); $post_class_use = '\\Timber\\Post'; if (is_array($post_class)) { if (isset($post_class[$post_type])) { $post_class_use = $post_class[$post_type]; } else { Helper::error_log($post_type . ' not found in ' . print_r($post_class, true)); } } elseif (is_string($post_class)) { $post_class_use = $post_class; } else { Helper::error_log('Unexpeted value for PostClass: ' . print_r($post_class, true)); } if (!class_exists($post_class_use) || !(is_subclass_of($post_class_use, '\\Timber\\Post') || is_a($post_class_use, '\\Timber\\Post', true))) { Helper::error_log('Class ' . $post_class_use . ' either does not exist or implement \\Timber\\Post'); } return $post_class_use; }
/** * */ function get_current_url() { Helper::warn('TimberHelper::get_current_url() is deprecated and will be removed in future versions, use Timber\\URLHelper::get_current_url()'); return URLHelper::get_current_url(); }
/** * Add functions to Timber context * * @param array $context Timber context * @return array $context Adapted Timber context */ public function filter_timber_context($context) { Timber\Helper::function_wrapper('get_timber_image_responsive_acf'); return $context; }
/** * Gets the link a menu item points at * @internal * @deprecated since 0.21.7 use link instead * @see link() * @return string a full URL like http://mysite.com/thing/ */ public function permalink() { Helper::warn('{{item.permalink}} is deprecated, use {{item.link}} instead'); return $this->link(); }
/** * @deprecated 0.21.9 * @internal * @return string */ public function get_url() { Helper::warn('{{site.get_url}} is deprecated, use {{site.link}} instead'); return $this->link(); }
/** * @api * @example * ```twig * {% if comment.approved %} * Your comment is good * {% else %} * Do you kiss your mother with that mouth? * {% endif %} * ``` * @return boolean */ public function approved() { return Helper::is_true($this->comment_approved); }