WordPress.org

Make WordPress Core

Ticket #17347: trac-links.1.0.2.php

File trac-links.1.0.2.php, 1.2 KB (added by sivel, 4 years ago)
Line 
1<?php
2/*
3Plugin Name: Trac Links
4Plugin URI: http://wordpress.org/
5Description: Link ticket and changeset numbers to core.trac.wordpress.org
6Version: 1.0.2
7Author: Automattic
8Author URI: http://wordpress.org/
9*/
10
11add_filter( 'the_content', 'markup_wporg_links', 1, 1 );
12add_filter( 'comment_text', 'markup_wporg_links', 1, 1 );
13
14function markup_wporg_links( $content ) {
15        $find = array(
16                '/[\b]?(?<!&)(?<!38;)(?<!amp;)#(\d{3,6})[\b]?/i', // core trac ticket #1234 in http://core.trac.wordpress.org/ticket/1234
17                '/[\b]?r(\d{3,6})[\b]?/i', // core changeset r1234 in http://core.trac.wordpress.org/changeset/1234
18                '/[\b]?diff:@(\d{3,6}):(\d{3,6})[\b]?/i', // core diff diff:@200:300 https://core.trac.wordpress.org/changeset?new=300&old=200
19        );
20
21        $replace = array(
22                '<a href="http://core.trac.wordpress.org/ticket/$1">$0</a>', // core trac ticket
23                '<a href="http://core.trac.wordpress.org/changeset/$1">$0</a>', // core trac changeset
24                '<a href="http://core.trac.wordpress.org/changeset?new=$2&old=$1">$0</a>', // core diff
25        );
26
27        preg_match_all( '#[^>]+(?=<[^/]*[^a])|[^>]+$#', $content, $matches, PREG_SET_ORDER );
28
29        foreach ( $matches as $match ) {
30                $content = str_replace( $match[0], preg_replace( $find, $replace, $match[0] ), $content );
31        }
32
33        return $content;
34}