Making WordPress.org


Ignore:
Timestamp:
10/29/2016 07:35:06 PM (8 years ago)
Author:
jmdodd
Message:

Support Forum: Redirect bbPress topic id shortlinks to new bbPress 2 permalinks.

bbPress 1 used topic ids to create shortlinks which were indexed by search engines and used in our own profile history. These links currently produce a 404 error.

The redirect_old_topic_id function uses a custom table to dereference the old topic's new post id rather than the postmeta table for performance reasons.

Fixes #1954.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-support-compat.php

    r4308 r4309  
    3636            // Adjust breadcrumbs for plugin and theme related topics.
    3737            add_filter( 'bbp_breadcrumbs', array( $this, 'breadcrumbs' ) );
     38
     39            // Redirect old topic ids to topic permalinks.
     40            add_action( 'template_redirect', array( $this, 'redirect_old_topic_id' ), 9 );
    3841
    3942            $this->loaded = true;
     
    427430    }
    428431
     432    /**
     433     * In bbPress 1, topics could be referenced using their topic id, and many
     434     * are indexed/linked via this rather than their pretty permalink. The
     435     * custom table topic2post makes it possible to quickly dereference these
     436     * and redirect them appropriately.
     437     */
     438    public function redirect_old_topic_id() {
     439        global $wpdb;
     440        if ( is_404() && 'topic' == get_query_var( 'post_type' ) && is_numeric( get_query_var( 'topic' ) ) ) {
     441            $topic_id = absint( get_query_var( 'topic' ) );
     442            if ( ! $topic_id ) {
     443                return;
     444            }
     445
     446            $cache_key = $topic_id;
     447            $cache_group = 'topic2post';
     448            $post_id = wp_cache_get( $cache_key, $cache_group );
     449            if ( false === $post_id ) {
     450                $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}topic2post WHERE topic_id = %d LIMIT 1", $topic_id ) );
     451                if ( $post_id ) {
     452                    $post_id = absint( $post_id );
     453                    wp_cache_set( $cache_key, $post_id, $cache_group );
     454                }
     455            }
     456
     457            if ( $post_id ) {
     458                $permalink = get_permalink( $post_id );
     459                if ( $permalink ) {
     460                    wp_safe_redirect( $permalink, 301 );
     461                    exit;
     462                }
     463            }
     464        }
     465    }
    429466}
Note: See TracChangeset for help on using the changeset viewer.