Skip to content

Commit

Permalink
Only add/remove from htaccess file when necessary.
Browse files Browse the repository at this point in the history
 Ensures that we only add/remove from the htaccess file when there's an
 option enabled that has associated htaccess rules, or when the htaccess
 file contains rules that should be removed because we've disabled all
 options that require htaccess rules.

See wpsharks/comet-cache#641
  • Loading branch information
raamdev committed Dec 28, 2015
1 parent ea72374 commit fa7bbfc
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/includes/closures/Plugin/HtaccessUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
*/
$self->htaccess_marker = 'WmVuQ2FjaGU';

/*
* Plugin options that have associated htaccess rules.
*
* @since 15xxxx Improving `.htaccess` tweaks.
*
* @return array Plugin options that have associated htaccess rules
*/
$self->options_with_htaccess_rules = array('cdn_enable');

/*
* Add template blocks to `/.htaccess` file.
*
Expand All @@ -26,6 +35,12 @@
if (!$self->options['enable']) {
return true; // Nothing to do.
}
if (!$self->needHtaccessRules()) {
if($self->findHtaccessMarker()) { // Do we need to clean up previously added rules?
$self->removeWpHtaccess(); // Fail silently since we don't need rules in place.
}
return true; // Nothing to do; no options enabled that require htaccess rules.
}
if (!$self->removeWpHtaccess()) {
return false; // Unable to remove.
}
Expand Down Expand Up @@ -116,6 +131,25 @@
return $file;
};

/*
* Determines if there are any plugin options enabled that require htaccess rules to be added.
*
* @since 15xxxx Improving `.htaccess` tweaks.
*
* @return bool True when an option is enabled that requires htaccess rules, false otherwise.
*/
$self->needHtaccessRules = function () use ($self) {
if(!is_array($self->options_with_htaccess_rules)) {
return false; // Nothing to do.
}
foreach ($self->options_with_htaccess_rules as $option) {
if ($self->options[$option]) {
return true; // Yes, there are options enabled that require htaccess rules.
}
}
return false; // No, there are no options enabled that require htaccess rules.
};

/*
* Utility method used to unlock and close htaccess file resource.
*
Expand Down

0 comments on commit fa7bbfc

Please sign in to comment.