Just an update on this. I actually came up with two solutions.
The first is a custom plugin that extends the make_clickable_cb fucntion to call antispambot()
function _make_email_clickable_antispambot_cb( $content ) {
$email = $content[2] . '@' . $content[3];
if ( ! is_email( $email ) ) {
return;
}
return '\<a href="mailto:' . antispambot( $email ) . '">' . antispambot( $email ) . '</a>';
add_shortcode( 'email', 'wpcodex_hide_email_shortcode' );
}
And then when the match is returned, there is a callback that processes the antispambot function.
$ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_antispambot_cb', $ret );
Here’s the full modified plugin file, if you want to add this work to your plugin, that would be great.
<?php
/*
Plugin Name: TablePress Extension: Automatic URL conversion
Plugin URI: https://tablepress.org/extensions/automatic-url-conversion/
Description: Custom Extension for TablePress to automatically make URLs (www, ftp, and email) in table cells clickable
Version: 1.3
Author: Tobias Bäthge
Author URI: https://tobias.baethge.com/
*/
/*
* Usage and possible parameters:
* [table id=1 automatic_url_conversion=true automatic_url_conversion_new_window=true automatic_url_conversion_rel_nofollow=true /]
*
* automatic_url_conversion: Whether URLs shall be made clickable.
* automatic_url_conversion_new_window: Whether http(s) links shall open in a new window.
* automatic_url_conversion_rel_nofollow: Whether http(s) links shall get the <code>nofollow</code> attribute.
*/
add_filter( 'tablepress_table_output', 'tablepress_auto_url_conversion', 10, 3 );
add_filter( 'tablepress_shortcode_table_default_shortcode_atts', 'tablepress_add_shortcode_parameter_auto_url_conversion' );
/**
* Add Extension's parameters as a valid parameters to the [table /] Shortcode.
*/
function tablepress_add_shortcode_parameter_auto_url_conversion( $default_atts ) {
$default_atts['automatic_url_conversion'] = false;
$default_atts['automatic_url_conversion_new_window'] = false;
$default_atts['automatic_url_conversion_rel_nofollow'] = false;
return $default_atts;
}
function _make_email_clickable_antispambot_cb( $content ) {
$email = $content[2] . '@' . $content[3];
if ( ! is_email( $email ) ) {
return;
}
return '\<a href="mailto:' . antispambot( $email ) . '">' . antispambot( $email ) . '</a>';
add_shortcode( 'email', 'wpcodex_hide_email_shortcode' );
//return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}
/**
* Convert plaintext URI to HTML links.
* Modified to call the local antispambot version of _make_email_clickable_cb()
*
* Converts URI, www and ftp, and email addresses. Finishes by fixing links
* within links.
*
* @since 0.71
*
* @param string $text Content to convert URIs.
* @return string Content with converted URIs.
*/
function make_clickable_cb( $text ) {
$r = '';
//strip mailto tags from the text first - otherwise they are skipped by the following code and while the displayed email
//address will be obfuscated, the one actually in the mailto tag will not.
$text = $content = preg_replace("~<a\s+?href=[\'|\"]mailto:(.*?)[\'|\"].*?>.*?</a>~", "$1", $text);
$textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
$nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
foreach ( $textarr as $piece ) {
if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) )
$nested_code_pre++;
elseif ( ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) ) && $nested_code_pre )
$nested_code_pre--;
if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
$r .= $piece;
continue;
}
// Long strings might contain expensive edge cases ...
if ( 10000 < strlen( $piece ) ) {
// ... break it up
foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
if ( 2101 < strlen( $chunk ) ) {
$r .= $chunk; // Too big, no whitespace: bail.
} else {
$r .= make_clickable_cb( $chunk );
}
}
} else {
$ret = " $piece "; // Pad with whitespace to simplify the regexes
$url_clickable = '~
([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
( # 2: URL
[\\w]{1,20}+:// # Scheme and hier-part prefix
(?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long
[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character
(?: # Unroll the Loop: Only allow punctuation URL character if followed by a non-punctuation URL character
[\'.,;:!?)] # Punctuation URL character
[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
)*
)
(\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing)
~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
// Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
$ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
$ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
$ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_antispambot_cb', $ret );
$ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
$r .= $ret;
}
}
// Cleanup of accidental links within links
return preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
}
/**
* Convert URLs to links, if Shortcode parameter is set,
* add the <code>target</code> attribute in http(s):// links,
* or add the <code>rel</code> attribute, if the Shortcode parameter is set.
*/
function tablepress_auto_url_conversion ( $output, $table, $render_options ) {
if ( $render_options['automatic_url_conversion'] ) {
$output = make_clickable_cb( $output );
}
if ( $render_options['automatic_url_conversion_new_window'] && $render_options['automatic_url_conversion_rel_nofollow'] ) {
$output = str_replace( '<a href="http', '<a target="_blank" rel="nofollow" href="http', $output );
} elseif ( $render_options['automatic_url_conversion_new_window'] ) {
$output = str_replace( '<a href="http', '<a target="_blank" href="http', $output );
} elseif ( $render_options['automatic_url_conversion_rel_nofollow'] ) {
$output = str_replace( '<a href="http', '<a rel="nofollow" href="http', $output );
}
return $output;
}
The second solution is that I found an existing email obfuscation plugin that does convert email addresses that are stored in TablePress. Apparently, these two solutions work independently and don’t conflict with each other.
https://wordpress.org/plugins/obfuscate-email/