Skip to content
Browse files

Logic has been encapsulated in a new class.

  • Loading branch information...
1 parent 672dc0d commit 063ba923bd066d5cf5dd1d3c1eab59318bbbafd1 @wmark committed May 22, 2011
Showing with 153 additions and 126 deletions.
  1. +116 −71 cdn-linker-base.php
  2. +36 −46 test.php
  3. +1 −9 wp-cdn-linker.php
View
187 cdn-linker-base.php
@@ -5,96 +5,141 @@
Control flow is this (begin reading with ossdl_off_filter()):
ossdl_off_additional_directories <-- ossdl_off_filter --> ossdl_off_rewriter --> ossdl_off_exclude_match
-
-As this plugin hooks into the PHP output buffer "ossdl_off_filter" cannot have any parameters (beyond the obvious one, that is.)
-Therefore these global variables are used:
- - $ossdl_off_blog_url String: the blog's URL ( get_option('siteurl') )
- - $ossdl_off_cdn_url String: URL of the CDN domain
- - $ossdl_off_include_dirs String: directories to include in static file matching, comma-delimited list
- - $arr_of_excludes Array: strings which indicate that a given element should not be rewritten (i.e., ".php")
-
- - $ossdl_off_rootrelative Boolean: if true, modifies root-relative links (default is false)
*/
/**
- * Determines whether to exclude a match.
- *
- * @param String $match URI to examine
- * @param Array $excludes array of "badwords"
- * @return Boolean true if to exclude given match from rewriting
+ * Reperesents the CDN Linker's rewrite logic.
*/
-function ossdl_off_exclude_match(&$match, &$excludes) {
- foreach ($excludes as $badword) {
- if (stristr($match, $badword) != false) {
- return true;
+class CDNLinksRewriter
+{
+ /** String: the blog's URL ( get_option('siteurl') ) */
+ var $blog_url = null;
+ /** String: URL of the CDN domain */
+ var $cdn_url = null;
+ /** String: directories to include in static file matching, comma-delimited list */
+ var $include_dirs = null;
+ /** Array: strings which indicate that a given element should not be rewritten (i.e., ".php") */
+ var $excludes = null;
+ /** Boolean: if true, modifies root-relative links */
+ var $rootrelative = false;
+
+ /** Constructor. */
+ function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) {
+ $this->blog_url = $blog_url;
+ $this->cdn_url = $cdn_url;
+ $this->include_dirs = $include_dirs;
+ $this->excludes = $excludes;
+ $this->rootrelative = $root_relative;
+ }
+
+ /**
+ * Determines whether to exclude a match.
+ *
+ * @param String $match URI to examine
+ * @param Array $excludes array of "badwords"
+ * @return Boolean true if to exclude given match from rewriting
+ */
+ function ossdl_off_exclude_match(&$match, &$excludes) {
+ foreach ($excludes as $badword) {
+ if (stristr($match, $badword) != false) {
+ return true;
+ }
}
+ return false;
}
- return false;
-}
-/**
- * Rewriter of URLs, used as callback for rewriting in {@link ossdl_off_filter}.
- *
- * @param String $match An URI as candidate for rewriting
- * @return String the unmodified URI if it is not to be rewritten, otherwise a modified one pointing to CDN
- */
-function ossdl_off_rewriter(&$match) {
- global $ossdl_off_blog_url, $ossdl_off_cdn_url, $arr_of_excludes, $ossdl_off_rootrelative;
- if (ossdl_off_exclude_match($match[0], $arr_of_excludes)) {
- return $match[0];
- } else {
- if (!$ossdl_off_rootrelative || strstr($match[0], $ossdl_off_blog_url)) {
- return str_replace($ossdl_off_blog_url, $ossdl_off_cdn_url, $match[0]);
- } else { // obviously $ossdl_off_rootrelative is true aand we got a root-relative link - else that case won't happen
- return $ossdl_off_cdn_url . $match[0];
+ /**
+ * Rewriter of URLs, used as callback for rewriting in {@link ossdl_off_filter}.
+ *
+ * @param String $match An URI as candidate for rewriting
+ * @return String the unmodified URI if it is not to be rewritten, otherwise a modified one pointing to CDN
+ */
+ function ossdl_off_rewriter(&$match) {
+ if ($this->ossdl_off_exclude_match($match[0], $this->excludes)) {
+ return $match[0];
+ } else {
+ if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
+ return str_replace($this->blog_url, $this->cdn_url, $match[0]);
+ } else { // obviously $this->rootrelative is true and we got a root-relative link - else that case won't happen
+ return $this->cdn_url . $match[0];
+ }
}
}
-}
-/**
- * Creates a regexp compatible pattern from the directories to be included in matching.
- *
- * @return String regexp pattern for those directories, or empty if none are given
- */
-function ossdl_off_additional_directories() {
- global $ossdl_off_include_dirs;
- $input = explode(',', $ossdl_off_include_dirs);
- if ($ossdl_off_include_dirs == '' || count($input) < 1) {
- return 'wp\-content|wp\-includes';
- } else {
- return implode('|', array_map('quotemeta', array_map('trim', $input)));
+ /**
+ * Creates a regexp compatible pattern from the directories to be included in matching.
+ *
+ * @return String regexp pattern for those directories, or empty if none are given
+ */
+ function ossdl_off_additional_directories() {
+ $input = explode(',', $this->include_dirs);
+ if ($this->include_dirs == '' || count($input) < 1) {
+ return 'wp\-content|wp\-includes';
+ } else {
+ return implode('|', array_map('quotemeta', array_map('trim', $input)));
+ }
}
+
+ /**
+ * Output filter which runs the actual plugin logic.
+ *
+ * @param String $content the raw HTML of the page from Wordpress, meant to be returned to the requester but intercepted here
+ * @return String modified HTML with replaced links - will be served by the HTTP server to the requester
+ */
+ function ossdl_off_filter(&$content) {
+ if ($this->blog_url == $this->cdn_url) { // no rewrite needed
+ return $content;
+ } else {
+ $dirs = $this->ossdl_off_additional_directories();
+ $regex = '#(?<=[(\"\'])';
+ $regex .= $this->rootrelative
+ ? ('(?:'.quotemeta($this->blog_url).')?')
+ : quotemeta($this->blog_url);
+ $regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
+ return preg_replace_callback($regex, array(&$this, 'ossdl_off_rewriter'), $content);
+ }
+ }
+
}
+
/**
- * Output filter which runs the actual plugin logic.
- *
- * @param String $content the raw HTML of the page from Wordpress, meant to be returned to the requester but intercepted here
- * @return String modified HTML with replaced links - will be served by the HTTP server to the requester
+ * The rewrite logic with calls to Wordpress.
*/
-function ossdl_off_filter(&$content) {
- global $ossdl_off_blog_url, $ossdl_off_cdn_url, $ossdl_off_rootrelative;
- if ($ossdl_off_blog_url == $ossdl_off_cdn_url) { // no rewrite needed
- return $content;
- } else {
- $dirs = ossdl_off_additional_directories();
- $regex = '#(?<=[(\"\'])';
- $regex .= $ossdl_off_rootrelative
- ? ('(?:'.quotemeta($ossdl_off_blog_url).')?')
- : quotemeta($ossdl_off_blog_url);
- $regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
- return preg_replace_callback($regex, 'ossdl_off_rewriter', $content);
+class CDNLinksRewriterWordpress extends CDNLinksRewriter
+{
+ /** Initializes all options calling Wordpress functions. */
+ function __construct() {
+ $excl_tmp = trim(get_option('ossdl_off_exclude'));
+ $excludes = array_map('trim', explode(',', $excl_tmp));
+ parent::__construct(
+ get_option('siteurl'),
+ trim(get_option('ossdl_off_cdn_url')),
+ trim(get_option('ossdl_off_include_dirs')),
+ $excludes,
+ !!trim(get_option('ossdl_off_exclude'))
+ );
}
+
+ /**
+ * Registers ossdl_off_filter as output buffer, if needed.
+ *
+ * This function is called by Wordpress if the plugin was enabled.
+ */
+ function do_ossdl_off_ob_start() {
+ if ($this->blog_url != $this->cdn_url) {
+ ob_start(array(&$this, 'ossdl_off_filter'));
+ }
+ }
+
}
+
/**
- * Registers ossdl_off_filter as output buffer, if needed.
- *
- * This function is called by Wordpress if the plugin was enabled.
+ * This function actually registers the rewriter.
+ * It is called by Wordpress.
*/
function do_ossdl_off_ob_start() {
- global $ossdl_off_blog_url, $ossdl_off_cdn_url;
- if ($ossdl_off_blog_url != $ossdl_off_cdn_url) {
- ob_start('ossdl_off_filter');
- }
+ $rewriter = new CDNLinksRewriterWordpress();
+ $rewriter->do_ossdl_off_ob_start();
}
View
82 test.php
@@ -9,6 +9,9 @@
class CDNLinkerTest extends PHPUnit_Framework_TestCase
{
+ /** Instance of CDNLinksRewriter. */
+ var $ctx = null;
+
public function readCompressedSample($filename) {
$fp = gzopen('test/samples/'.$filename, 'r') or die("can't open: $php_errormsg");
$content = gzread($fp, 128000);
@@ -24,92 +27,76 @@ public function writeCompressedSample($filename, $content) {
gzclose($fp);
}
- protected function setGlobalRootRelative($root_relative) {
- global $ossdl_off_rootrelative;
- $ossdl_off_rootrelative = !!$root_relative;
- }
-
- /**
- * Sets global variables like the plugin for Wordpress does.
- */
- protected function setGlobals($blog_url = 'http://test.local',
- $cdn_url = 'http://cdn.test.local',
- $include_dirs = 'wp-content,wp-includes',
- array $excludes = array('.php'),
- $root_relative = false) {
- global $ossdl_off_blog_url, $ossdl_off_cdn_url, $ossdl_off_include_dirs, $arr_of_excludes,
- $ossdl_off_rootrelative;
-
- $ossdl_off_blog_url = $blog_url;
- $ossdl_off_cdn_url = $cdn_url;
- $ossdl_off_include_dirs = $include_dirs;
- $arr_of_excludes = $excludes;
- $ossdl_off_rootrelative = $root_relative;
+ protected function setUp() {
+ $this->ctx = new CDNLinksRewriter(
+ 'http://test.local',
+ 'http://cdn.test.local',
+ 'wp-content,wp-includes',
+ array('.php'),
+ false
+ );
}
public function testNoModificationIfUrlsMatch() {
- $this->setGlobals('http://test.local', 'http://test.local');
+ $this->ctx->cdn_url = 'http://test.local';
$input = '<a href="http://test.local/favicon.ico">some text</a>';
- $output = ossdl_off_filter($input);
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($input, $output);
}
public function testModifiesIfUrlsDontMatch() {
- $this->setGlobals();
$input = '<a href="http://test.local/favicon.ico">some text</a>';
$expected = '<a href="http://cdn.test.local/favicon.ico">some text</a>';
- $output = ossdl_off_filter($input);
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
}
public function testRootRelativeLinkDisabled() {
- $this->setGlobals();
- $this->setGlobalRootRelative(false);
+ $this->ctx->rootrelative = false;
$input = '<a href="/favicon.ico">some text</a>';
$expected = '<a href="/favicon.ico">some text</a>';
- $output = ossdl_off_filter($input);
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
}
public function testRootRelativeLinkEnabled() {
- $this->setGlobals();
- $this->setGlobalRootRelative(true);
+ $this->ctx->rootrelative = true;
$input = '<a href="/favicon.ico"><img src="http://test.local/favicon.ico" /></a>';
$expected = '<a href="http://cdn.test.local/favicon.ico"><img src="http://cdn.test.local/favicon.ico" /></a>';
- $output = ossdl_off_filter($input);
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
}
public function testLinksToPostsAndPagesNotAffected() {
- $this->setGlobals();
$expected = $input = $this->readCompressedSample('virtual-1.before.gz');
- $this->setGlobalRootRelative(false);
- $output = ossdl_off_filter($input);
+ $this->ctx->rootrelative = false;
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
- $this->setGlobalRootRelative(true);
- $output = ossdl_off_filter($input);
+ $this->ctx->rootrelative = true;
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
}
public function testExcludes() {
- $this->setGlobals();
- $this->setGlobalRootRelative(true);
+ $this->ctx->rootrelative = true;
$input = '<a href="/favicongetter.php"><img src="/favicon.ico" /></a>';
$expected = '<a href="/favicongetter.php"><img src="http://cdn.test.local/favicon.ico" /></a>';
- $output = ossdl_off_filter($input);
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
}
/** Now come actual sites. */
protected function comparePair($blog_url, $cdn_url, $root_relative, $fn_before, $fn_after) {
- $this->setGlobals($blog_url, $cdn_url);
- $this->setGlobalRootRelative($root_relative);
+ $this->ctx->blog_url = $blog_url;
+ $this->ctx->cdn_url = $cdn_url;
+ $this->ctx->rootrelative = $root_relative;
+
$input = $this->readCompressedSample($fn_before);
$expected = $this->readCompressedSample($fn_after);
- $output = ossdl_off_filter($input);
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
}
@@ -127,14 +114,17 @@ public function testImagesAtRootLevel2() {
public function testIncludeDirs() {
// this one has root-relative links to "/extscripts/..."
- $this->setGlobals('http://screenrant.com', 'http://cdn.screenrant.com',
- 'wp-content,wp-includes,extscripts');
- $this->setGlobalRootRelative(true);
+ $this->ctx->blog_url = 'http://screenrant.com';
+ $this->ctx->cdn_url = 'http://cdn.screenrant.com';
+ $this->ctx->include_dirs = 'wp-content,wp-includes,extscripts';
+ $this->ctx->rootrelative = true;
+
$input = $this->readCompressedSample('screenrant.com-before.gz');
$expected = str_replace('/extscripts/', 'http://cdn.screenrant.com/extscripts/',
$this->readCompressedSample('screenrant.com-after.gz'));
- $output = ossdl_off_filter($input);
+ $output = $this->ctx->ossdl_off_filter($input);
$this->assertEquals($expected, $output);
}
}
+
View
10 wp-cdn-linker.php
@@ -3,20 +3,12 @@
Plugin Name: CDN Linker
Plugin URI: https://github.com/wmark/CDN-Linker
Description: Replaces the blog URL by another for all files under <code>wp-content</code> and <code>wp-includes</code>. That way static content can be handled by a CDN by origin pull - the origin being your blog address - or loaded from an other site.
-Version: 1.1.2
+Version: 1.2.0
Author: W-Mark Kubacki
Author URI: http://mark.ossdl.de/
License: RPL for non-commercial
*/
-$ossdl_off_blog_url = get_option('siteurl');
-$ossdl_off_cdn_url = trim(get_option('ossdl_off_cdn_url'));
-$ossdl_off_include_dirs = trim(get_option('ossdl_off_include_dirs'));
-$ossdl_off_exclude = trim(get_option('ossdl_off_exclude'));
-$ossdl_off_rootrelative = !!trim(get_option('ossdl_off_exclude'));
-
-$arr_of_excludes = array_map('trim', explode(',', $ossdl_off_exclude));
-
if ( @include_once('cdn-linker-base.php') ) {
add_action('template_redirect', 'do_ossdl_off_ob_start');
}

0 comments on commit 063ba92

Please sign in to comment.
Something went wrong with that request. Please try again.