/**
 * Flush rewrite rules for a given VIP
 * Most likely called from a post-commit job
 *
 * Usage: http://site.com/?action=wpcom-vip-flush-rewrite-rules&secret={WPCOM_VIP_FLUSH_REWRITE_RULES_SECRET}
 */
function wpcom_vip_handle_flush_rewrite_rules()
{
    if (!isset($_GET['action']) || !$_GET['action'] == 'wpcom-vip-flush-rewrite-rules') {
        return;
    }
    // Pass the secret key check
    if (!isset($_GET['secret']) || $_GET['secret'] != WPCOM_VIP_FLUSH_REWRITE_RULES_SECRET) {
        return;
    }
    global $wp_rewrite;
    /**
     * VIPs and other themes can declare the permastruct, tag and category bases in their themes.
     * This is done by filtering the option. To ensure we're getting the proper data, refresh.
     *
     * However, wpcom_vip_refresh_wp_rewrite() noops the values in the database so we only want to run it
     * if the permastructs are defined in the theme (e.g. not Enterprise)
     */
    if (defined('WPCOM_VIP_CUSTOM_PERMALINKS') && WPCOM_VIP_CUSTOM_PERMALINKS || defined('WPCOM_VIP_CUSTOM_CATEGORY_BASE') && WPCOM_VIP_CUSTOM_CATEGORY_BASE || defined('WPCOM_VIP_CUSTOM_TAG_BASE') && WPCOM_VIP_CUSTOM_TAG_BASE) {
        wpcom_vip_refresh_wp_rewrite();
    }
    /**
     * We can't use flush_rewrite_rules( false ) in this context because
     * on WPCOM it deletes the transient representation of rewrite_rules, not the option.
     * For now, we need to do some code replication.
     */
    $wp_rewrite->matches = 'matches';
    $wp_rewrite->rewrite_rules();
    update_option('rewrite_rules', $wp_rewrite->rules);
    wp_die(__('Rewrite rules have been flushed for ' . get_site_url()));
    exit;
}
function rri_wpcom_flush_rules()
{
    global $wp_rewrite;
    /**
     * VIPs and other themes can declare the permastruct, tag and category bases in their themes.
     * This is done by filtering the option. To ensure we're getting the proper data, refresh.
     *
     * However, wpcom_vip_refresh_wp_rewrite() noops the values in the database so we only want to run it
     * if the permastructs are defined in the theme (not for clients using the admin screen)
     */
    if (defined('WPCOM_VIP_CUSTOM_PERMALINKS') && WPCOM_VIP_CUSTOM_PERMALINKS || defined('WPCOM_VIP_CUSTOM_CATEGORY_BASE') && WPCOM_VIP_CUSTOM_CATEGORY_BASE || defined('WPCOM_VIP_CUSTOM_TAG_BASE') && WPCOM_VIP_CUSTOM_TAG_BASE) {
        wpcom_vip_refresh_wp_rewrite();
    }
    /**
     * We can't use flush_rewrite_rules( false ) in this context because
     * on WPCOM it deletes the transient representation of rewrite_rules, not the option.
     * For now, we need to do some code replication.
     *
     * See:
     * - https://wpcom.trac.automattic.com/ticket/2589
     * - https://wpcom.trac.automattic.com/browser/trunk/wp-includes/rewrite.php?rev=47524#L1928
     * - https://wpcom.trac.automattic.com/browser/trunk/wp-includes/rewrite.php?rev=47524#L1596
     */
    $wp_rewrite->matches = 'matches';
    $wp_rewrite->rewrite_rules();
    update_option('rewrite_rules', $wp_rewrite->rules);
}
function rri_wpcom_flush_rules()
{
    global $wp_rewrite;
    /**
     * VIPs and other themes can declare the permastruct, tag and category bases in their themes.
     * This is done by filtering the option. To ensure we're getting the proper data, refresh.
     */
    wpcom_vip_refresh_wp_rewrite();
    /**
     * We can't use flush_rewrite_rules( false ) in this context because
     * on WPCOM it deletes the transient representation of rewrite_rules, not the option.
     * For now, we need to do some code replication.
     *
     * See:
     * - https://wpcom.trac.automattic.com/ticket/2589
     * - https://wpcom.trac.automattic.com/browser/trunk/wp-includes/rewrite.php?rev=47524#L1928
     * - https://wpcom.trac.automattic.com/browser/trunk/wp-includes/rewrite.php?rev=47524#L1596
     */
    $wp_rewrite->matches = 'matches';
    $wp_rewrite->rewrite_rules();
    update_option('rewrite_rules', $wp_rewrite->rules);
}