/**
  * @param string $src
  * @param int $w
  * @param int $h
  * @param string $crop
  * @return string
  */
 static function get_retina_file_path($src, $mult = 2)
 {
     $new_name = self::get_retina_file_name_relative_to_content($src, $mult);
     $new_server_path = WP_CONTENT_DIR . $new_name;
     $new_server_path = TimberURLHelper::remove_double_slashes($new_server_path);
     return $new_server_path;
 }
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(TimberURLHelper::prepend_to_url($url, $this->base));
     return $ret;
 }
 function testGetParams()
 {
     $_SERVER['REQUEST_URI'] = 'http://example.org/blog/post/news/2014/whatever';
     $params = TimberURLHelper::get_params();
     $this->assertEquals(7, count($params));
     $whatever = TimberURLHelper::get_params(-1);
     $blog = TimberURLHelper::get_params(2);
     $this->assertEquals('whatever', $whatever);
     $this->assertEquals('blog', $blog);
 }
 function testSubDirectoryImageLocaion()
 {
     if (!is_multisite()) {
         $this->markTestSkipped('Test is only for Multisite');
         return;
     }
     $blog_id = TestTimberMultisite::createSubDirectorySite();
     $this->assertGreaterThan(1, $blog_id);
     $blog_details = get_blog_details($blog_id);
     $pretend_image = 'http://example.org/wp-content/2015/08/fake-pic.jpg';
     $is_external = TimberURLHelper::is_external_content($pretend_image);
     $this->assertFalse($is_external);
 }
Ejemplo n.º 5
0
 /**
  * @param string $url
  * @param bool $force
  * @return string
  */
 public static function get_rel_url($url, $force = false)
 {
     $url_info = parse_url($url);
     if (isset($url_info['host']) && $url_info['host'] != $_SERVER['HTTP_HOST'] && !$force) {
         return $url;
     }
     $link = '';
     if (isset($url_info['path'])) {
         $link = $url_info['path'];
     }
     if (isset($url_info['query']) && strlen($url_info['query'])) {
         $link .= '?' . $url_info['query'];
     }
     if (isset($url_info['fragment']) && strlen($url_info['fragment'])) {
         $link .= '#' . $url_info['fragment'];
     }
     $link = TimberURLHelper::remove_double_slashes($link);
     return $link;
 }
Ejemplo n.º 6
0
 /**
  * Takes in an URL and breaks it into components,
  * that will then be used in the different steps of image processing.
  * The image is expected to be either part of a theme, plugin, or an upload.
  *
  * @param  string $url an URL (absolute or relative) pointing to an image
  * @return array       an array (see keys in code below)
  */
 private static function analyze_url($url)
 {
     $result = array('url' => $url, 'absolute' => TimberURLHelper::is_absolute($url), 'base' => 0, 'subdir' => '', 'filename' => '', 'extension' => '', 'basename' => '');
     $upload_dir = wp_upload_dir();
     $tmp = $url;
     if (0 === strpos($tmp, ABSPATH)) {
         // we've been given a dir, not an url
         $result['absolute'] = true;
         if (0 === strpos($tmp, $upload_dir['basedir'])) {
             $result['base'] = self::BASE_UPLOADS;
             // upload based
             $tmp = str_replace($upload_dir['basedir'], '', $tmp);
         }
         if (0 === strpos($tmp, WP_CONTENT_DIR)) {
             $result['base'] = self::BASE_CONTENT;
             // content based
             $tmp = str_replace(WP_CONTENT_DIR, '', $tmp);
         }
     } else {
         if (!$result['absolute']) {
             $tmp = home_url() . $tmp;
         }
         if (0 === strpos($tmp, $upload_dir['baseurl'])) {
             $result['base'] = self::BASE_UPLOADS;
             // upload based
             $tmp = str_replace($upload_dir['baseurl'], '', $tmp);
         }
         if (0 === strpos($tmp, content_url())) {
             $result['base'] = self::BASE_CONTENT;
             // content-based
             $tmp = str_replace(content_url(), '', $tmp);
         }
     }
     $parts = pathinfo($tmp);
     $result['subdir'] = $parts['dirname'];
     $result['filename'] = $parts['filename'];
     $result['extension'] = $parts['extension'];
     $result['basename'] = $parts['basename'];
     // todo filename
     return $result;
 }
Ejemplo n.º 7
0
 /**
  * Get context.
  *
  * @return array
  */
 public static function get_context()
 {
     $data = array();
     $data['http_host'] = 'http://' . TimberURLHelper::get_host();
     $data['wp_title'] = TimberHelper::get_wp_title();
     $data['wp_head'] = TimberHelper::function_wrapper('wp_head');
     $data['wp_footer'] = TimberHelper::function_wrapper('wp_footer');
     $data['body_class'] = implode(' ', get_body_class());
     $data['site'] = new TimberSite();
     $data['theme'] = $data['site']->theme;
     //deprecated, these should be fetched via TimberSite or TimberTheme
     $data['theme_dir'] = WP_CONTENT_SUBDIR . str_replace(WP_CONTENT_DIR, '', get_stylesheet_directory());
     $data['language_attributes'] = TimberHelper::function_wrapper('language_attributes');
     $data['stylesheet_uri'] = get_stylesheet_uri();
     $data['template_uri'] = get_template_directory_uri();
     $data['posts'] = Timber::query_posts();
     //deprecated, this should be fetched via TimberMenu
     if (function_exists('wp_nav_menu')) {
         $locations = get_nav_menu_locations();
         if (count($locations)) {
             $data['wp_nav_menu'] = wp_nav_menu(array('container_class' => 'menu-header', 'echo' => false, 'menu_class' => 'nav-menu'));
         }
     }
     $data = apply_filters('timber_context', $data);
     $data = apply_filters('timber/context', $data);
     return $data;
 }
Ejemplo n.º 8
0
 function testInitFromURL()
 {
     $destination_path = self::copyTestImage();
     $destination_path = TimberURLHelper::get_rel_path($destination_path);
     $destination_url = 'http://' . $_SERVER['HTTP_HOST'] . $destination_path;
     $image = new TimberImage($destination_url);
     $this->assertEquals($destination_url, $image->get_src());
     $this->assertEquals($destination_url, (string) $image);
 }
Ejemplo n.º 9
0
 /**
  * @api
  * @return  string the relative path to the theme (ex: `/wp-content/themes/my-timber-theme`)
  */
 public function path()
 {
     return TimberURLHelper::get_rel_url($this->link());
 }
 /**
  *
  *
  * @param Twig_Environment $twig
  * @return Twig_Environment
  */
 function add_timber_filters($twig)
 {
     /* image filters */
     $twig->addFilter(new Twig_SimpleFilter('resize', array('TimberImageHelper', 'resize')));
     $twig->addFilter(new Twig_SimpleFilter('retina', array('TimberImageHelper', 'retina_resize')));
     $twig->addFilter(new Twig_SimpleFilter('letterbox', array('TimberImageHelper', 'letterbox')));
     $twig->addFilter(new Twig_SimpleFilter('tojpg', array('TimberImageHelper', 'img_to_jpg')));
     /* debugging filters */
     $twig->addFilter(new Twig_SimpleFilter('docs', 'twig_object_docs'));
     $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);
     }));
     $twig->addFilter(new Twig_SimpleFilter('print_a', function ($arr) {
         return '<pre>' . self::object_docs($arr, true) . '</pre>';
     }));
     /* 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('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('relative', function ($link) {
         return TimberURLHelper::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 TimberHelper::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 = 'TimberPost') {
         if (is_array($pid) && !TimberHelper::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, $ImageClass = 'TimberImage') {
         if (is_array($pid) && !TimberHelper::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 = 'TimberTerm') {
         if (is_array($pid) && !TimberHelper::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 = 'TimberUser') {
         if (is_array($pid) && !TimberHelper::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 = 'TimberPost') {
         if (is_array($pid) && !TimberHelper::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 = 'TimberImage') {
         if (is_array($pid) && !TimberHelper::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 = 'TimberTerm') {
         if (is_array($pid) && !TimberHelper::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 = 'TimberUser') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $UserClass($p);
             }
             return $pid;
         }
         return new $UserClass($pid);
     }));
     /* bloginfo and translate */
     $twig->addFunction('bloginfo', 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);
     }));
     /* get_twig is deprecated, use timber/twig */
     $twig = apply_filters('get_twig', $twig);
     $twig = apply_filters('timber/twig', $twig);
     return $twig;
 }
Ejemplo n.º 11
0
 function testResizeGif()
 {
     $filename = self::copyTestImage('loading.gif');
     $gif_url = str_replace(ABSPATH, 'http://' . $_SERVER['HTTP_HOST'] . '/', $filename);
     $str = '<img src="{{' . "'{$gif_url}'" . '|resize(200)}}" />';
     $result = Timber::compile_string($str);
     $resized_url = str_replace('loading.gif', 'loading-200x0-c-default.gif', $gif_url);
     $resized_path = str_replace('http://example.org', ABSPATH, $resized_url);
     $resized_path = TimberURLHelper::remove_double_slashes($resized_path);
     $this->assertFileExists($resized_path);
 }
Ejemplo n.º 12
0
 /**
  * @return string
  */
 public function get_path()
 {
     $link = $this->get_link();
     $rel = TimberURLHelper::get_rel_url($link, true);
     return apply_filters('timber_term_path', $rel, $this);
 }
 /**
  *
  *
  * @param string  $src
  * @param int     $w
  * @param int     $h
  * @param string  $crop
  * @param bool    $force_resize
  * @return string
  */
 public static function resize($src, $w, $h = 0, $crop = 'default', $force_resize = false)
 {
     if (empty($src)) {
         return '';
     }
     if (strstr($src, 'http') && !strstr($src, home_url())) {
         $src = self::sideload_image($src);
     }
     $abs = false;
     if (strstr($src, 'http')) {
         $abs = true;
     }
     // Sanitize crop position
     $allowed_crop_positions = array('default', 'center', 'top', 'bottom', 'left', 'right');
     if ($crop !== false && !in_array($crop, $allowed_crop_positions)) {
         $crop = $allowed_crop_positions[0];
     }
     //oh good, it's a relative image in the uploads folder!
     $new_path = self::get_resize_file_rel($src, $w, $h, $crop);
     $new_server_path = self::get_resize_file_path($src, $w, $h, $crop);
     $old_server_path = self::get_server_location($src);
     $old_server_path = TimberURLHelper::remove_double_slashes($old_server_path);
     $new_server_path = TimberURLHelper::remove_double_slashes($new_server_path);
     if (file_exists($new_server_path)) {
         if ($force_resize) {
             // Force resize - warning: will regenerate the image on every pageload, use for testing purposes only!
             unlink($new_server_path);
         } else {
             if (!$abs) {
                 return TimberURLHelper::preslashit($new_path);
             }
             return untrailingslashit(home_url()) . $new_path;
         }
     }
     $image = wp_get_image_editor($old_server_path);
     if (!is_wp_error($image)) {
         $current_size = $image->get_size();
         $src_w = $current_size['width'];
         $src_h = $current_size['height'];
         $src_ratio = $src_w / $src_h;
         if (!$h) {
             $h = round($w / $src_ratio);
         }
         // Get ratios
         $dest_ratio = $w / $h;
         $src_wt = $src_h * $dest_ratio;
         $src_ht = $src_w / $dest_ratio;
         if (!$crop) {
             // Always crop, to allow resizing upwards
             $image->crop(0, 0, $src_w, $src_h, $w, $h);
         } else {
             //start with defaults:
             $src_x = $src_w / 2 - $src_wt / 2;
             $src_y = ($src_h - $src_ht) / 6;
             //now specific overrides based on options:
             if ($crop == 'center') {
                 // Get source x and y
                 $src_x = round(($src_w - $src_wt) / 2);
                 $src_y = round(($src_h - $src_ht) / 2);
             } else {
                 if ($crop == 'top') {
                     $src_y = 0;
                 } else {
                     if ($crop == 'bottom') {
                         $src_y = $src_h - $src_ht;
                     } else {
                         if ($crop == 'left') {
                             $src_x = 0;
                         } else {
                             if ($crop == 'right') {
                                 $src_x = $src_w - $src_wt;
                             }
                         }
                     }
                 }
             }
             // Crop the image
             if ($dest_ratio > $src_ratio) {
                 $image->crop(0, $src_y, $src_w, $src_ht, $w, $h);
             } else {
                 $image->crop($src_x, 0, $src_wt, $src_h, $w, $h);
             }
         }
         $result = $image->save($new_server_path);
         if (is_wp_error($result)) {
             error_log('Error resizing image');
             error_log(print_r($result, true));
         }
         if ($abs) {
             return untrailingslashit(home_url()) . $new_path;
         }
         return $new_path;
     } else {
         if (isset($image->error_data['error_loading_image'])) {
             TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']);
         } else {
             TimberHelper::error_log($image);
         }
     }
     return $src;
 }
Ejemplo n.º 14
0
 /**
  * @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 TimberURLHelper::get_rel_path($this->src());
 }
Ejemplo n.º 15
0
 /**
  * Get context.
  *
  * @return array
  */
 public static function get_context()
 {
     $data = array();
     $data['http_host'] = 'http://' . TimberURLHelper::get_host();
     $data['wp_title'] = TimberHelper::get_wp_title();
     $data['wp_head'] = TimberHelper::function_wrapper('wp_head');
     $data['wp_footer'] = TimberHelper::function_wrapper('wp_footer');
     $data['body_class'] = implode(' ', get_body_class());
     $data['site'] = new TimberSite();
     $data['theme'] = $data['site']->theme;
     $data['posts'] = Timber::query_posts();
     $data = apply_filters('timber_context', $data);
     $data = apply_filters('timber/context', $data);
     return $data;
 }
Ejemplo n.º 16
0
 /**
  * Get the permalink for a post, but as a relative path
  * For example, where {{post.link}} would return "http://example.org/2015/07/04/my-cool-post"
  * this will return the relative version: "/2015/07/04/my-cool-post"
  * @internal
  * @return string
  */
 function get_path()
 {
     return TimberURLHelper::get_rel_url($this->get_link());
 }
Ejemplo n.º 17
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 '';
     }
     // if external image, load it first
     if (TimberURLHelper::is_external_content($src)) {
         $src = self::sideload_image($src);
     }
     // 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']);
     // 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)) {
         return $new_url;
     } else {
         // in case of error, we return source file itself
         return $src;
     }
 }
Ejemplo n.º 18
0
 /**
  * @deprecated
  */
 static function get_params($i = -1)
 {
     return TimberURLHelper::get_params($i);
 }
Ejemplo n.º 19
0
 /**
  *
  *
  * @return bool
  */
 function is_external()
 {
     if ($this->type != 'custom') {
         return false;
     }
     return TimberURLHelper::is_external($this->url);
 }
Ejemplo n.º 20
0
 /**
  * @param string $url
  */
 private function init_with_url($url)
 {
     $this->abs_url = $url;
     if (TimberURLHelper::is_local($url)) {
         $this->file = ABSPATH . TimberURLHelper::get_rel_url($url);
         $this->file_loc = ABSPATH . TimberURLHelper::get_rel_url($url);
     }
 }
Ejemplo n.º 21
0
 /**
  * This function is slightly different from the one below in the case of:
  * an image hosted on the same domain BUT on a different site than the
  * Wordpress install will be reported as external content.
  *
  * @param string  $url a URL to evaluate against
  * @return boolean if $url points to an external location returns true
  */
 public static function is_external_content($url)
 {
     // using content_url() instead of site_url or home_url is IMPORTANT
     // otherwise you run into errors with sites that:
     // 1. use WPML plugin
     // 2. or redefine upload directory
     $is_external = TimberURLHelper::is_absolute($url) && !strstr($url, content_url());
     return $is_external;
 }
Ejemplo n.º 22
0
 /**
  * This function is slightly different from the one below in the case of:
  * an image hosted on the same domain BUT on a different site than the
  * Wordpress install will be reported as external content.
  *
  * @param string  $url a URL to evaluate against
  * @return boolean if $url points to an external location returns true
  */
 public static function is_external_content($url)
 {
     $is_external = TimberURLHelper::is_absolute($url) && !TimberURLHelper::is_internal_content($url);
     return $is_external;
 }