WordPress.org

Making WordPress.org

Ticket #1299: resolve-topic-slug-collisions.php

File resolve-topic-slug-collisions.php, 1.3 KB (added by SergeyBiryukov, 4 years ago)
Line 
1<?php
2/*
3Plugin Name: Resolve Topic Slug Collisions
4Description: Resolves long topic slug collisions in Memcached due to the 250 chars key length.
5Author: wordpressdotorg
6Author URI: http://wordpress.org/
7Version: 1.0
8License: GPLv2
9License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10*/
11
12/**
13 * Resolves long topic slug collisions by pre-matching topic slugs to IDs
14 * in order to bypass the 'bb_topic_slug' cache group in get_topic(),
15 * which produces erroneous results for slugs close to 250 chars length
16 * due to Memcached key size limit.
17 *
18 * @param string|int $topic_slug Requested topic slug or ID.
19 * @return int|string Topic ID if resolved, topic slug otherwise.
20 */
21function wporg_resolve_topic_slug_collisions( $topic_slug ) {
22        if ( is_numeric( $topic_slug ) ) {
23                return $topic_slug;
24        }
25
26        $location = bb_get_location();
27
28        if ( ! in_array( $location, array( 'topic-page', 'topic-edit-page' ) ) ) {
29                return $topic_slug;
30        }
31
32        $topic_id = wp_cache_get( md5( $topic_slug ), 'bb_topic_slug_md5' );
33
34        if ( false === $topic_id ) {
35                $topic_id = bb_get_id_from_slug( 'topic', $topic_slug );
36
37                wp_cache_add( md5( $topic_slug ), $topic_id, 'bb_topic_slug_md5' );
38        }
39
40        return $topic_id;
41}
42add_filter( 'bb_repermalink', 'wporg_resolve_topic_slug_collisions' );