'http://trac.bbpress.org/',
'BP' => 'http://backpress.automattic.com/',
'WP' => 'http://trac.wordpress.org/'
);
$tl_code_blocks = array();
function tl_store_code_blocks( $text )
{
global $tl_code_blocks;
$tl_code_blocks = array();
$tl_text_blocks = array();
$all_blocks = preg_split( '@(.*)@mU', $text, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE );
foreach ( $all_blocks as $block ) {
if ( strpos( $block, '' ) === 0 ) {
$tl_code_blocks[] = $block;
} else {
$tl_text_blocks[] = $block;
}
}
return join( '%%tl_code_block%%', $tl_text_blocks );
}
function tl_restore_code_blocks( $text )
{
if ( strpos( $text, '%%tl_code_block%%' ) === false ) {
return $text;
}
global $tl_code_blocks;
foreach ( $tl_code_blocks as $block ) {
$text = preg_replace( '@%%tl_code_block%%@', $block, $text, 1 );
}
$tl_code_blocks = array();
return $text;
}
function tl_make_tracable( $text )
{
global $tl_trac;
global $tl_intertrac;
if ( !$tl_trac ) {
return $text;
}
// Check if anything in the text matches the basic form of the patterns at all
if ( !preg_match('@(?:#[a-z0-9]+)|(?:\[[a-z0-9]+\])@i', $text ) ) {
return $text;
}
// Strip out code blocks and save them
$text = tl_store_code_blocks( $text );
$_text = $text;
$tracs = array( $tl_trac );
if ( isset( $tl_intertrac ) && is_array( $tl_intertrac ) ) {
$tracs = array_merge( $tracs, $tl_intertrac );
}
$_tracs = array();
$i = 0;
foreach ( $tracs as $trac_id => $trac_url ) {
if ( $trac_url ) {
if ( !$parsed_trac_url = parse_url( $trac_url ) ) {
continue;
}
if ( $trac_id === 0 ) {
$trac_id = '';
}
$trac_url = rtrim( $trac_url, '/' ) . '/';
$_tracs[$i][0][0] = '([^&]?)#' . preg_quote( $trac_id ) . '([0-9]+)';
$_tracs[$i][0][1] = '$1';
$_tracs[$i][0][2] = $trac_url . 'ticket/$2';
$_tracs[$i][0][3] = '#' . $trac_id . '$2';
$_tracs[$i][1][0] = '\[' . preg_quote( $trac_id ) . '([0-9]+)\]';
$_tracs[$i][1][1] = '';
$_tracs[$i][1][2] = $trac_url . 'changeset/$1';
$_tracs[$i][1][3] = '[' . $trac_id . '$1]';
$i++;
}
}
if ( !count( $_tracs ) ) {
return $text;
}
foreach ( $_tracs as $pairs ) {
foreach ( $pairs as $pair ) {
$text = preg_replace( '@' . $pair[0] . '@', $pair[1] . '' . $pair[3] . '', $text );
}
}
// Put the code blocks back in
$text = tl_restore_code_blocks( $text );
return $text;
}
add_filter( 'post_text', 'tl_make_tracable', 1 );
?>