Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 48 Minify regex rewrite rule broken by external plugin / theme #49

Merged
merged 7 commits into from
Dec 12, 2013
18 changes: 9 additions & 9 deletions dependency-minification.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,26 @@ static function setup() {
static function hook_rewrites() {
add_filter( 'query_vars', array( __CLASS__, 'filter_query_vars' ) );
add_action( 'pre_get_posts', array( __CLASS__, 'handle_request' ) );
self::add_rewrite_rule();
add_filter( 'rewrite_rules_array', array( __CLASS__, 'add_rewrite_rule' ), 99999 );
}

static function get_rewrite_regex() {
return sprintf( '^%s/%s', self::$options['endpoint'], self::FILENAME_PATTERN );
}

static function add_rewrite_rule() {
static function add_rewrite_rule( $rules ) {
$regex = self::get_rewrite_regex();
$redirect = 'index.php?';
for ( $i = 0; $i < count( self::$query_vars ); $i += 1 ) {
$redirect .= sprintf( '%s=$matches[%d]&', self::$query_vars[$i], $i + 1 );
}
add_rewrite_rule( $regex, $redirect, 'top' );
$new_rules[$regex] = $redirect;

return array_merge( $rules, $new_rules );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c3mdigital it seems I need to reverse the order here, so that $new_rules will appear at the top. Otherwise, on one site I'm getting the page rewrite rule matching before the the minification rewrite rule can be reached.

}

static function remove_rewrite_rule() {
$regex = self::get_rewrite_regex();
global $wp_rewrite;
unset( $wp_rewrite->extra_rules_top[ $regex ] );
remove_filter( 'rewrite_rules_array', array( __CLASS__, 'add_rewrite_rule' ), 99999 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c3mdigital good idea!

}

protected static $is_footer = array(
Expand All @@ -116,7 +116,7 @@ static function remove_rewrite_rule() {
*/
static function activate() {
self::setup();
self::add_rewrite_rule();
add_filter( 'rewrite_rules_array', array( __CLASS__, 'add_rewrite_rule' ), 99999 );
flush_rewrite_rules();
}

Expand Down Expand Up @@ -398,7 +398,7 @@ static function admin_page() {
$_link_params = $link_params;
$_link_params['depmin_task'] = 'purge';
?>
<a class="submitdelete" title="<?php esc_attr_e( 'Delete the cached error to try again.', 'dependency-minification' ) ?>" href="<?php echo esc_url( add_query_arg( $_link_params, admin_url( 'admin-ajax.php' ) ) ) ?>">
<a class="submitdelete" title="<?php esc_attr_e( 'Delete the cached error to try again.', 'depmin' ) ?>" href="<?php echo esc_url( add_query_arg( $_link_params, admin_url( 'admin-ajax.php' ) ) ) ?>">
<?php esc_html_e( 'Try again', 'dependency-minification' ) ?>
</a>
</span>
Expand Down Expand Up @@ -505,7 +505,7 @@ static function get_cache_option_name( $src_hash ) {
static function get_dependency_minified_url( array $deps, $type ) {
$src_hash = self::hash_array( wp_list_pluck( $deps, 'src' ) );
$ver_hash = self::hash_array( wp_list_pluck( $deps, 'ver' ) );
$src = trailingslashit( home_url( self::$options['endpoint'] ) );
$src = trailingslashit( get_option( 'home' ) . DIRECTORY_SEPARATOR . self::$options['endpoint'] );
$src .= join( '.', array(
join( ',', wp_list_pluck( $deps, 'handle' ) ),
$src_hash,
Expand Down