Rewrite rules get flushed by other plugins.

Hello,

As I've discussed this yesterday within the live chat, I promised to make a post about it.

This issue is regarding my own plugin, where I discovered that Domain Mapping forces a flush on the 'init' action.

A flush on the 'init' action is all but advised. Since it will be overwritten by admin flushes. This should be marked for a fix. As the rewrite flush should be run on the 'shutdown' action hook.

To temporarily counter this issue, I've created the following function within my plugin:

Parameter $options_saved will be TRUE on Options Save (within the plugin settings page). $options_saved is set FALSE on init.

This function is hooked at the options saved (run in $_POST) action and on the 'init' action.
In both cases they add the Domain Mapping rewrite rule back.

/**
     * Add the WPMUdev Domain Mapping rules again. And flush them on init.
     * Domain Mapping bugfix.
     *
     * @param bool $options_saved : If we're in admin and the sanitiation function runs.
     *
     * Runs a flush and updates the site option to "true".
     * When the site option is set to true, it not flush again on init.
     *
     * If $options_saved is true, it will not check for the init action hook and continue,
     * So it will flush the next time on init.
     *
     * @since 2.3.0
     */
    public function wpmudev_domainmap_flush_fix( $options_saved = false ) {

        if ( $this->pretty_permalinks && $this->is_domainmapping_active() ) {
            if ( current_action() == 'init' || $options_saved ) {
                if ( class_exists( 'Domainmap_Module_Cdsso' ) && defined( 'Domainmap_Module_Cdsso::SSO_ENDPOINT' ) ) {
                    add_rewrite_endpoint( Domainmap_Module_Cdsso::SSO_ENDPOINT, EP_ALL );

                    //* Force extra flush on init.
                    if ( class_exists( 'domain_map' ) ) {
                        $key = 'the_seo_framework_wpmdev_dm' . get_current_blog_id() . '_extra_flush';

                        if ( $options_saved ) {
                            if ( get_site_option( $key ) ) {
                                error_log( $key . ' is now marked for flush. On: ' . current_action() );

                                //* Prevent flushing multiple times.
                                update_site_option( $key, false );
                            }
                        } else {
                            if ( ! get_site_option( $key ) ) {
                                error_log( $key . ' is now flushed on init. On: ' . current_action() );

                                //* Prevent flushing multiple times.
                                update_site_option( $key, true );

                                //* Now flush
                                flush_rewrite_rules();
                            }
                        }
                    }
                }
            }
        }

    }

The error log will show the following, after a save and a init run:

[10-Oct-2015 19:13:39 UTC] the_seo_framework_wpmdev_dm162_extra_flush is now marked for flush. On: sanitize_option_autodescription-site-settings
[10-Oct-2015 19:13:39 UTC] the_seo_framework_wpmdev_dm162_extra_flush is now flushed on init. On: init

This is just to let you know and maybe other people will find this fix useful :slight_smile:, maybe even for other plugins with the same issue.