| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Plugin Name: Trac Links
|
|---|
| 4 | Plugin URI: https://wordpress.org/
|
|---|
| 5 | Description: Link ticket and changeset numbers to core.trac.
|
|---|
| 6 | Version: 1.0
|
|---|
| 7 | Author: WordPress.org
|
|---|
| 8 | Author URI: https://wordpress.org/
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | add_filter( 'the_content', 'markup_wporg_links', 10, 1 );
|
|---|
| 12 | add_filter( 'comment_text', 'markup_wporg_links', 10, 1 );
|
|---|
| 13 |
|
|---|
| 14 | function markup_wporg_links( $content ) {
|
|---|
| 15 | $url = parse_url( home_url( '/' ) );
|
|---|
| 16 | $url = untrailingslashit( $url['host'] . $url['path'] );
|
|---|
| 17 |
|
|---|
| 18 | switch( $url ) {
|
|---|
| 19 | case 'make.wordpress.org/meta':
|
|---|
| 20 | case 'make.wordpress.org/community':
|
|---|
| 21 | $trac = 'meta';
|
|---|
| 22 | break;
|
|---|
| 23 |
|
|---|
| 24 | default:
|
|---|
| 25 | $trac = 'core';
|
|---|
| 26 | break;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | $find = array(
|
|---|
| 30 | '/(\s|^|\()(#(\d{4,5}))(\b|$)/im', // core trac ticket #1234-core in http://core.trac.wordpress.org/ticket/
|
|---|
| 31 | '/(\s|^|\()(r(\d{4,5}))(\b|$)/im', // core changeset r1234-core in http://core.trac.wordpress.org/changeset/1234
|
|---|
| 32 | '/(?<!\w)\[(\d{4,5})\](?!\w)/im', // core changeset [12345]
|
|---|
| 33 | '/(?<!\w)\[(\d{4,5})-(\d{4,5})\](?!\w)/im', // core log [12345-54321]
|
|---|
| 34 | '/(\s|^|\()(diff:@(\d{4,5}):(\d{4,5}))(\b|$)/im', // core diff diff-core:@20:30 https://core.trac.wordpress.org/changeset?new=30&old=20
|
|---|
| 35 | );
|
|---|
| 36 |
|
|---|
| 37 | $replace = array(
|
|---|
| 38 | '$1<a href="https://'. $trac .'.trac.wordpress.org/ticket/$3">$2</a>', // trac ticket
|
|---|
| 39 | '$1<a href="https://'. $trac .'.trac.wordpress.org/changeset/$3">$2</a>', // trac changeset
|
|---|
| 40 | '<a href="https://'. $trac .'.trac.wordpress.org/changeset/$1">$0</a>', // trac changeset
|
|---|
| 41 | '<a href="https://'. $trac .'.trac.wordpress.org/log/?revs=$1-$2">$0</a>', // trac log
|
|---|
| 42 | '$1<a href="https://'. $trac .'.trac.wordpress.org/changeset?new=$4&old=$3">$2</a>', // diff
|
|---|
| 43 | );
|
|---|
| 44 |
|
|---|
| 45 | preg_match_all('#[^>]+(?=<[^/]*[^a])|[^>]+$#', $content, $matches, PREG_SET_ORDER);
|
|---|
| 46 |
|
|---|
| 47 | foreach ( $matches as $val ) {
|
|---|
| 48 | $content = str_replace($val[0],preg_replace($find,$replace,$val[0]),$content);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | return $content;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|