Skip to content

Commit

Permalink
Merge pull request #45 from 1bladesforhire/master
Browse files Browse the repository at this point in the history
Adding support for home url
  • Loading branch information
mark-kubacki committed Oct 19, 2015
2 parents 4667e54 + 52e5e61 commit 38a29c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
19 changes: 12 additions & 7 deletions cdn-linker-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function target_url_strategy_for($pattern) {
*/
class URI_changer
{
/** String: the blog's URL ( get_option('siteurl') ) */
/** String: the blog's URL ( get_option('home') ) */
var $blog_url = null;
/** Target_URL_Strategy: results in URL of a CDN domain */
var $get_target_url = null;
Expand All @@ -105,15 +105,15 @@ class URI_changer
function __construct($blog_url, Target_URL_Strategy $get_target_url, $include_dirs,
array $excludes, $root_relative, $www_is_optional,
$https_deactivates_rewriting) {
$this->blog_url = $blog_url;
$root_location = get_option('ossdl_site_root') ? 'home' : 'siteurl';
$this->$root_location = $blog_url;
$this->get_target_url = $get_target_url;
$this->include_dirs = $include_dirs;
$this->excludes = $excludes;
$this->rootrelative = $root_relative;
$this->www_is_optional = $www_is_optional;
$this->https_deactivates_rewriting = $https_deactivates_rewriting;
}

/**
* Determines whether to exclude a match.
*
Expand All @@ -140,7 +140,8 @@ protected function rewrite_single($match) {
return $match[0];
}

$blog_url = $this->blog_url;
$blog_location2= get_option('ossdl_site_root') ? 'home':'siteurl';
$blog_url = $this->$blog_location2;
if ($this->www_is_optional && $match[0]{0} != '/' && !strstr($match[0], '//www.')) {
$blog_url = str_replace('//www.', '//', $blog_url);
}
Expand Down Expand Up @@ -170,7 +171,9 @@ protected function include_dirs_to_pattern() {
* @return String regexp pattern such as {@code '(?:http://(?:www\.)?example\.com)?'}
*/
protected function blog_url_to_pattern() {
$blog_url = quotemeta($this->blog_url);
$blog_location = get_option('ossdl_site_root')? 'home' : 'blog_url';
$rewrite_blog_url = $this->$blog_location;
$blog_url = quotemeta($rewrite_blog_url);
$max_occurences = 1; // due to PHP's stupidity this must be a variable
if ($this->www_is_optional && strstr($blog_url, '//www\.')) {
$blog_url = str_replace('//www\.', '//(?:www\.)?', $blog_url, $max_occurences);
Expand Down Expand Up @@ -220,13 +223,15 @@ public function rewrite(&$content) {
* This is called by Wordpress.
*/
function register_as_output_buffer_handler() {
if (get_option('siteurl') == trim(get_option('ossdl_off_cdn_url'))) {
$blog_location = get_option('ossdl_site_root')? 'home' : 'siteurl';
$blog_locale = get_option($blog_location);
if ($blog_locale == trim(get_option('ossdl_off_cdn_url'))) {
return;
}

$excludes = array_map('trim', explode(',', get_option('ossdl_off_exclude')));
$rewriter = new URI_changer(
get_option('siteurl'),
get_option($blog_location),
target_url_strategy_for(trim(get_option('ossdl_off_cdn_url'))),
trim(get_option('ossdl_off_include_dirs')),
$excludes,
Expand Down
23 changes: 18 additions & 5 deletions wp-cdn-linker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
/********** WordPress Administrative ********/

function on_plugin_activation() {
add_option('ossdl_off_cdn_url', get_option('siteurl'));
add_option('ossdl_site_root', '');
add_option('ossdl_off_cdn_url', get_option($ossdl_site_root? 'home' : 'siteurl'));
add_option('ossdl_off_include_dirs', 'wp-content,wp-includes');
add_option('ossdl_off_exclude', '.php');
add_option('ossdl_off_rootrelative', '');
Expand Down Expand Up @@ -53,6 +54,7 @@ function on_handle_admin_page() {
// Yes, this URL is not subject to esc_url() or esc_url_raw() (please don’t report it), because:
// 1) Only the owner of the installation can change this setting — if he/she wants something odd here (javascript:…; HTML fragments), we allow for it!
update_option('ossdl_off_cdn_url', $_POST['ossdl_off_cdn_url']);
update_option('ossdl_site_root', $_POST['ossdl_site_root'] ? 'home' : 'siteurl');
update_option('ossdl_off_include_dirs', $_POST['ossdl_off_include_dirs'] == '' ? 'wp-content,wp-includes' : $_POST['ossdl_off_include_dirs']);
if(strstr($_POST['ossdl_off_exclude'], '.php')) {
update_option('ossdl_off_exclude', $_POST['ossdl_off_exclude']);
Expand All @@ -64,19 +66,23 @@ function on_handle_admin_page() {
}
// checkboxes which are not checked are sometimes not sent, hence the additional calls to isset($_POST(…))
$ossdl_off_rootrelative = isset($_POST['ossdl_off_rootrelative']) ? !!$_POST['ossdl_off_rootrelative'] : false;
$ossdl_site_root = isset($_POST['ossdl_site_root']) ? !!$_POST['ossdl_site_root'] : false;
$ossdl_off_www_is_optional = isset($_POST['ossdl_off_www_is_optional']) ? !!$_POST['ossdl_off_www_is_optional'] : false;
$ossdl_off_disable_cdnuris_if_https = isset($_POST['ossdl_off_disable_cdnuris_if_https']) ? !!$_POST['ossdl_off_disable_cdnuris_if_https'] : false;
update_option('ossdl_off_rootrelative', $ossdl_off_rootrelative);
update_option('ossdl_off_www_is_optional', $ossdl_off_www_is_optional);
update_option('ossdl_off_disable_cdnuris_if_https', $ossdl_off_disable_cdnuris_if_https);
update_option('ossdl_site_root', $ossdl_site_root);
}

$example_file_rr = '/wp-includes/images/rss.png';
if (get_option('ossdl_off_cdn_url') == get_option('siteurl')) {
$example_cdn_uri = str_replace('http://', 'http://cdn.', str_replace('www.', '', get_option('siteurl')))

$cdn_site_root = get_option('ossdl_site_root') ? 'home' : 'siteurl';
if (get_option('ossdl_off_cdn_url') == get_option($cdn_site_root)) {
$example_cdn_uri = str_replace('http://', 'http://cdn.', str_replace('www.', '', get_option($cdn_site_root)))
. $example_file_rr;
} else {
$example_uri = get_option('siteurl') . $example_file_rr;
$example_uri = get_option($cdn_site_root) . $example_file_rr;
$get_target_url = cdn\target_url_strategy_for(trim(get_option('ossdl_off_cdn_url')));
$example_cdn_uri = $get_target_url->for_source($example_uri) . $example_file_rr;
}
Expand All @@ -102,7 +108,7 @@ function on_handle_admin_page() {
<th scope="row"><label for="ossdl_off_cdn_url">CDN URL</label></th>
<td>
<input type="text" name="ossdl_off_cdn_url" value="<?php echo(esc_attr(get_option('ossdl_off_cdn_url'))); ?>" size="64" class="regular-text code" />
<span class="description">The new URL to be used in place of <?php echo(get_option('siteurl')); ?> for rewriting. No trailing <code>/</code> please. E.&thinsp;g. <code><?php echo($example_cdn_uri); ?></code>.
<span class="description">The new URL to be used in place of <?php echo(get_option($cdn_site_root)); ?> for rewriting. No trailing <code>/</code> please. E.&thinsp;g. <code><?php echo($example_cdn_uri); ?></code>.
&mdash;
You can use <code>%4%</code> (a number between 1 and 9, surrounded by percent signs) to use that many hostname variations.
Should be between 2 and 4, with 4 being beyond an universal optimum.
Expand All @@ -111,6 +117,13 @@ function on_handle_admin_page() {
</span>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="ossdl_site_root">Use home or site url</label></th>
<td>
<input type="checkbox" name="ossdl_site_root" <?php echo(!!get_option('ossdl_site_root') ? 'checked="1" ' : '') ?>value="true" />
<span class="description">Check this if you want to use your site address instead of your WordPress url</span>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="ossdl_off_rootrelative">rewrite root-relative refs</label></th>
<td>
Expand Down

0 comments on commit 38a29c2

Please sign in to comment.