Flushing rewrite rules causes issues with 3rd party plugin.

Hi guys,

I just installed a Polylang plugin on my multi-site and noticed that all links to all of my pages got broken. For example, before activating the plugin I could access my sample-page at http://example.com/sample-page/. Once Polylang was activated, the same URL resulted in an infinite loop redirect.

While trying to investigate this issue, I eventually discovered that the problem is caused by Events+ plugin flushing rewrite rules as part of its check_rewrite_rules() function, defined in events-and-bookings.php. This seems to happen on every page load, and contrary to WP recommendation:

Because this function (flush_rules) can be extremely costly in terms of performance, it should be used as sparingly as possible - such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.

Now, my question is, how required is that flush_rewrite() in the given plugin?? Would anything break if I simply disable it??

Also, if it is required, may I suggest rewriting the function in the following way:

function check_rewrite_rules($value) {
        //prevent an infinite loop
        if ( ! post_type_exists( 'incsub_event' ) )
            return $value;

        if (!is_array($value))
            $value = array();

        $slug = $this->_data->get_option('slug');
        $slug = array(
            $slug . '/([0-9]{4})/?$',
            $slug . '/([0-9]{4})/([0-9]{1,2})/?$',
            $slug . '/([0-9]{4})/([0-9]{1,2})/(.+?)/?$',
        );  

        if ( in_array( $slug, array_keys( $value )))
            return $value;

        add_action( 'wp_loaded', array(&$this, 'flush_rewrite'), 999 );
        return $value;
    }

This would:

a) Prevent flush_rewrite() from being executed three times in a row (eg: if supplied $value is not an array).

b) Delay flush_rewrite() action until all themes and plugins are loaded. Thus, preventing conflict with Polylang.

Hope you find this useful,

Dmytro