Skip to content

Commit

Permalink
Merge pull request #1447 from timber/1445/wpml_urls
Browse files Browse the repository at this point in the history
1445/wpml urls
  • Loading branch information
jarednova committed Jun 12, 2017
2 parents ddf0031 + 3f9825f commit a79eb54
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 17 deletions.
13 changes: 1 addition & 12 deletions lib/ImageHelper.php
Expand Up @@ -29,9 +29,9 @@ class ImageHelper {
const BASE_CONTENT = 2;

public static function init() {
self::add_constants();
self::add_actions();
self::add_filters();
return true;
}

/**
Expand Down Expand Up @@ -179,17 +179,6 @@ protected static function add_actions() {
}, 10, 2);
}

/**
* Adds a constant defining the path to the content directory relative to the site
* for example /wp-content or /content
*/
protected static function add_constants() {
if ( !defined('WP_CONTENT_SUBDIR') ) {
$wp_content_path = str_replace(get_home_url(), '', WP_CONTENT_URL);
define('WP_CONTENT_SUBDIR', $wp_content_path);
}
}

/**
* adds a 'relative' key to wp_upload_dir() result.
* It will contain the relative url to upload dir.
Expand Down
1 change: 1 addition & 0 deletions lib/Integrations.php
Expand Up @@ -30,5 +30,6 @@ public function maybe_init_integrations() {
if ( class_exists('CoAuthors_Plus') ) {
$this->coauthors_plus = new Integrations\CoAuthorsPlus();
}
$this->wpml = new Integrations\WPML();
}
}
21 changes: 21 additions & 0 deletions lib/Integrations/WPML.php
@@ -0,0 +1,21 @@
<?php

namespace Timber\Integrations;

class WPML {

public function __construct() {
add_filter('timber/URLHelper/file_system_to_url', array($this, 'file_system_to_url'), 10, 1);
add_filter('timber/URLHelper/get_content_subdir/home_url', array($this, 'file_system_to_url'), 10, 1);
add_filter('timber/URLHelper/url_to_file_system/path', array($this, 'file_system_to_url'), 10, 1);

}

public function file_system_to_url($url) {
if ( defined('ICL_LANGUAGE_CODE') ) {
$url = preg_replace('/\/' . ICL_LANGUAGE_CODE . '/', '', $url);
}
return $url;
}

}
16 changes: 15 additions & 1 deletion lib/URLHelper.php
Expand Up @@ -156,6 +156,7 @@ public static function get_full_path( $src ) {
*/
public static function url_to_file_system( $url ) {
$url_parts = parse_url($url);
$url_parts['path'] = apply_filters('timber/URLHelper/url_to_file_system/path', $url_parts['path']);
$path = ABSPATH.$url_parts['path'];
$path = str_replace('//', '/', $path);
return $path;
Expand All @@ -167,9 +168,22 @@ public static function url_to_file_system( $url ) {
public static function file_system_to_url( $fs ) {
$relative_path = self::get_rel_path($fs);
$home = home_url('/'.$relative_path);
$home = apply_filters('timber/URLHelper/file_system_to_url', $home);
return $home;
}

/**
* Get the path to the content directory relative to the site.
* This replaces the WP_CONTENT_SUBDIR constant
* @return string (ex: /wp-content or /content)
*/
public static function get_content_subdir() {
$home_url = get_home_url();
$home_url = apply_filters('timber/URLHelper/get_content_subdir/home_url', $home_url);
$wp_content_path = str_replace($home_url, '', WP_CONTENT_URL);
return $wp_content_path;
}

/**
*
*
Expand All @@ -182,7 +196,7 @@ public static function get_rel_path( $src ) {
}
//its outside the wordpress directory, alternate setups:
$src = str_replace(WP_CONTENT_DIR, '', $src);
return WP_CONTENT_SUBDIR.$src;
return self::get_content_subdir().$src;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/test-timber-image.php
Expand Up @@ -905,7 +905,7 @@ function testGifToJpg() {

function testImageHelperInit() {
$helper = TimberImageHelper::init();
$this->assertTrue(defined('WP_CONTENT_SUBDIR'));
$this->assertTrue($helper);
}

function testResizeGif() {
Expand Down
8 changes: 5 additions & 3 deletions tests/test-timber-site.php
Expand Up @@ -5,16 +5,18 @@ class TestTimberSite extends Timber_UnitTestCase {
function testStandardThemeLocation() {
switch_theme( 'twentyfifteen' );
$site = new TimberSite();
$this->assertEquals( WP_CONTENT_SUBDIR.'/themes/twentyfifteen', $site->theme->path );
$content_subdir = Timber\URLHelper::get_content_subdir();
$this->assertEquals( $content_subdir.'/themes/twentyfifteen', $site->theme->path );
}

function testChildParentThemeLocation() {
TestTimberLoader::_setupChildTheme();
$content_subdir = Timber\URLHelper::get_content_subdir();
$this->assertFileExists( WP_CONTENT_DIR.'/themes/fake-child-theme/style.css' );
switch_theme( 'fake-child-theme' );
$site = new TimberSite();
$this->assertEquals( WP_CONTENT_SUBDIR.'/themes/fake-child-theme', $site->theme->path );
$this->assertEquals( WP_CONTENT_SUBDIR.'/themes/twentyfifteen', $site->theme->parent->path );
$this->assertEquals( $content_subdir.'/themes/fake-child-theme', $site->theme->path );
$this->assertEquals( $content_subdir.'/themes/twentyfifteen', $site->theme->parent->path );
}

function testThemeFromContext() {
Expand Down
29 changes: 29 additions & 0 deletions tests/test-timber-url-helper.php
Expand Up @@ -5,6 +5,7 @@ class TestTimberURLHelper extends Timber_UnitTestCase {
private $mockUploadDir = false;



function testStartsWith() {
$haystack = 'http://nytimes.com/news/reports/2017';
$starts_with = 'http://nytimes.com/news';
Expand All @@ -29,6 +30,34 @@ function testStartsWithHTTPsFlip() {
$this->assertFalse(Timber\URLHelper::starts_with($haystack, $nope));
}

function testFileSystemToURL() {
$image = TestTimberImage::copyTestImage();
$url = Timber\URLHelper::file_system_to_url($image);
$this->assertEquals('http://example.org/wp-content/uploads/'.date('Y/m').'/arch.jpg', $url);
}

function addWPMLHomeFilter($url, $path) {
return 'http://example.org/en'.$path;
}

function _setupWPMLDirectory() {
define('ICL_LANGUAGE_CODE', 'en');
add_filter('home_url', array($this, 'addWPMLHomeFilter'), 10, 2);
}

function testFileSystemToURLWithWPMLPrefix() {
self::_setupWPMLDirectory();
$image = TestTimberImage::copyTestImage();
$url = Timber\URLHelper::file_system_to_url($image);
$this->assertEquals('http://example.org/wp-content/uploads/'.date('Y/m').'/arch.jpg', $url);
remove_filter('home_url', array($this, 'addWPMLHomeFilter'));
}

function testContentSubDirectory() {
$subdir = Timber\URLHelper::get_content_subdir();
$this->assertEquals('/wp-content', $subdir);
}

function testURLToFileSystem() {
$url = 'http://example.org/wp-content/uploads/2012/06/mypic.jpg';
$file = TimberURLHelper::url_to_file_system($url);
Expand Down

0 comments on commit a79eb54

Please sign in to comment.