Ejemplo n.º 1
1
 /**
  * @api
  * @example
  * ```twig
  * <img src="{{ image.path }}" />
  * ```
  * ```html
  * <img src="/wp-content/uploads/2015/08/pic.jpg" />
  * ```
  * @return  string the /relative/path/to/the/file
  */
 public function path()
 {
     return URLHelper::get_rel_path($this->src());
 }
Ejemplo n.º 2
0
 /**
  * @internal
  * @param string $url
  * @param string $text
  * @return mixed
  */
 protected function get_archives_link($url, $text)
 {
     $ret = array();
     $ret['text'] = $ret['title'] = $ret['name'] = wptexturize($text);
     $ret['url'] = $ret['link'] = esc_url(URLHelper::prepend_to_url($url, $this->base));
     return $ret;
 }
Ejemplo n.º 3
0
 /**
  * @api
  * @return  string the relative path to the theme (ex: `/wp-content/themes/my-timber-theme`)
  */
 public function path()
 {
     return URLHelper::get_rel_url($this->link());
 }
Ejemplo n.º 4
0
 /**
  * Get context.
  * @api
  * @return array
  */
 public static function get_context()
 {
     if (empty(self::$context_cache)) {
         self::$context_cache['http_host'] = 'http://' . URLHelper::get_host();
         self::$context_cache['wp_title'] = Helper::get_wp_title();
         self::$context_cache['wp_head'] = Helper::function_wrapper('wp_head');
         self::$context_cache['wp_footer'] = Helper::function_wrapper('wp_footer');
         self::$context_cache['body_class'] = implode(' ', get_body_class());
         self::$context_cache['site'] = new Site();
         self::$context_cache['request'] = new Request();
         $user = new User();
         self::$context_cache['user'] = $user->ID ? $user : false;
         self::$context_cache['theme'] = self::$context_cache['site']->theme;
         self::$context_cache['posts'] = Timber::query_posts();
         self::$context_cache = apply_filters('timber_context', self::$context_cache);
         self::$context_cache = apply_filters('timber/context', self::$context_cache);
     }
     return self::$context_cache;
 }
Ejemplo n.º 5
0
 /**
  *
  *
  * @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;
 }
Ejemplo n.º 6
0
 /**
  * Main method that applies operation to src image:
  * 1. break down supplied URL into components
  * 2. use components to determine result file and URL
  * 3. check if a result file already exists
  * 4. otherwise, delegate to supplied TimberImageOperation
  *
  * @param  string  $src   an URL (absolute or relative) to an image
  * @param  object  $op    object of class TimberImageOperation
  * @param  boolean $force if true, remove any already existing result file and forces file generation
  * @return string         URL to the new image - or the source one if error
  *
  */
 private static function _operate($src, $op, $force = false)
 {
     if (empty($src)) {
         return '';
     }
     $external = false;
     // if external image, load it first
     if (URLHelper::is_external_content($src)) {
         $src = self::sideload_image($src);
         $external = true;
     }
     // break down URL into components
     $au = self::analyze_url($src);
     // build URL and filenames
     $new_url = self::_get_file_url($au['base'], $au['subdir'], $op->filename($au['filename'], $au['extension']), $au['absolute']);
     $new_server_path = self::_get_file_path($au['base'], $au['subdir'], $op->filename($au['filename'], $au['extension']));
     $old_server_path = self::_get_file_path($au['base'], $au['subdir'], $au['basename']);
     $new_url = apply_filters('timber/image/new_url', $new_url);
     $new_server_path = apply_filters('timber/image/new_path', $new_server_path);
     // if already exists...
     if (file_exists($new_server_path)) {
         if ($force) {
             // Force operation - warning: will regenerate the image on every pageload, use for testing purposes only!
             unlink($new_server_path);
         } else {
             // return existing file (caching)
             return $new_url;
         }
     }
     // otherwise generate result file
     if ($op->run($old_server_path, $new_server_path)) {
         if (get_class($op) === 'Timber\\Image\\Operation\\Resize' && $external) {
             $new_url = strtolower($new_url);
         }
         return $new_url;
     } else {
         // in case of error, we return source file itself
         return $src;
     }
 }
Ejemplo n.º 7
0
 /**
  * @api
  * @return string
  */
 public function path()
 {
     $link = $this->get_link();
     $rel = URLHelper::get_rel_url($link, true);
     $rel = apply_filters('timber_term_path', $rel, $this);
     return apply_filters('timber/term/path', $rel, $this);
 }
Ejemplo n.º 8
0
 /**
  *
  */
 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();
 }