if (count($html_exclusion) > 0) {
foreach ($html_exclusion as $exclude_ancestor) {
if (taxopress_starts_with($ancestor, '<' . strtolower($exclude_ancestor) . '')) {
$pass_check = false;
break;
}
}
}
// Prepare exclude terms array
$excludes_class = explode(',', $exclude_class);
if (!empty($excludes_class)) {
$excludes_class = array_filter($excludes_class);
$excludes_class = array_unique($excludes_class);
if (count($excludes_class) > 0) {
foreach ($excludes_class as $idclass) {
if (substr(trim($idclass), 0, 1) === "#") {
$div_id = ltrim(trim($idclass), "#");
if (preg_match_all('/<[a-z \'"]*id="' . $div_id . '"/i', $ancestor, $matches) || preg_match_all('/<[a-z \'"]*id=\'' . $div_id . '\'/i', $ancestor, $matches)) {
$pass_check = false;
break;
}
} else {
$div_class = ltrim(trim($idclass), ".");
if (preg_match_all('/<[a-z ]*class="' . $div_class . '"/i', $ancestor, $matches) || preg_match_all('/<[a-z ]*class=\'' . $div_class . '\'/i', $ancestor, $matches)) {
$pass_check = false;
break;
}
}
}
}
}
}
if ($anchor_level === 0 && $pass_check) { // linkify if not inside anchor tags
if (preg_match($match, $token)) { // use preg_match for compatibility with PHP 4
$j++;
$remaining_usage = $max_by_post - self::$tagged_link_count;
if ($same_usage_max > $remaining_usage) {
$same_usage_max = $remaining_usage;
}
if ($same_usage_max > 0) { // Limit replacement at 1 by default, or options value !
$token = preg_replace($match, $substitute, $token, $same_usage_max, $rep_count); // only PHP 5 supports calling preg_replace with 5 arguments
self::$tagged_link_count = self::$tagged_link_count + $rep_count;
}
$must_tokenize = true; // re-tokenize next time around
}
}
} else { // this token is markup
if (preg_match("#<\s*a\s+[^>]*>#i", $token)) { // found
$ancestor = $token;
$anchor_level++;
} elseif (preg_match("#<\s*/\s*a\s*>#i", $token)) { // found
$anchor_level--;
} elseif (taxopress_starts_with($token, "")) {
$ancestor = '';
} else {
$ancestor = $token;
}
}
$filtered .= $token; // this token has now been filtered
}
$content = $filtered; // filtering completed for this link
}
}
/**
* Replace text by link to tag
*
* @param string $content
*
* @return string
*/
public static function taxopress_autolinks_the_content($content = '')
{
global $post;
if (!is_object($post) || is_admin()) {
return $content;
}
$post_tags = taxopress_get_autolink_data();
// user preference for this post ?
$meta_value = get_post_meta($post->ID, '_exclude_autolinks', true);
if (!empty($meta_value)) {
return $content;
}
if (count($post_tags) > 0) {
$auto_link_replace = [];
foreach ($post_tags as $post_tag) {
// Get option
$embedded = (isset($post_tag['embedded']) && is_array($post_tag['embedded']) && count($post_tag['embedded']) > 0) ? $post_tag['embedded'] : false;
if (!$embedded) {
continue;
}
if (!in_array($post->post_type, $embedded)) {
continue;
}
if ($post_tag['autolink_display'] === 'post_title') {
continue;
}
//reset tags just in case
self::$link_tags = [];
// Get currents tags if no exists
self::prepare_auto_link_tags($post_tag);
// Shuffle array
SimpleTags_Client::random_array(self::$link_tags);
// HTML Rel (tag/no-follow)
$rel = SimpleTags_Client::get_rel_attribut();
// only continue if the database actually returned any links
if (!isset(self::$link_tags) || !is_array(self::$link_tags) || empty(self::$link_tags)) {
$can_continue = false;
} else {
$can_continue = true;
}
if ($can_continue) {
// Case option ?
$case = (1 === (int) $post_tag['ignore_case']) ? 'i' : '';
$strpos_fnc = ('i' === $case) ? 'stripos' : 'strpos';
// Prepare exclude terms array
$excludes_terms = explode(',', $post_tag['auto_link_exclude']);
if (empty($excludes_terms)) {
$excludes_terms = array();
} else {
$excludes_terms = array_filter($excludes_terms, '_delete_empty_element');
$excludes_terms = array_unique($excludes_terms);
}
$z = 0;
foreach ((array) self::$link_tags as $term_name => $term_link) {
$z++;
// Force string for tags "number"
$term_name = (string) $term_name;
// Exclude terms ? next...
if (taxopress_in_array_i($term_name, (array) $excludes_terms, true)) {
continue;
}
// Make a first test with PHP function, economize CPU with regexp
if (false === $strpos_fnc($content, $term_name)) {
continue;
}
$auto_link_replace[] = [
'term_name' => $term_name,
'term_link' => $term_link,
'case' => $case,
'rel' => $rel,
'options' => $post_tag,
'option_id' => $post_tag['ID'],
'post_limit' => $post_tag['autolink_usage_max'],
'term_limit' => $post_tag['autolink_same_usage_max'],
'type' => 'content',
];
if (!class_exists('DOMDocument') || !class_exists('DOMXPath')) {
self::replace_by_links_regexp($content, $term_name, $term_link, $case, $rel, $post_tag);
}
}
}
}
if (class_exists('DOMDocument') && class_exists('DOMXPath')) {
self::replace_by_links_dom($content, $auto_link_replace);
}
}
return $content;
}
/**
* Replace text by link to tag
*
* @param string $title
*
* @return string
*/
public static function taxopress_autolinks_the_title($title = '')
{
global $post;
if (!is_object($post) || is_admin()) {
return $title;
}
$post_tags = taxopress_get_autolink_data();
// user preference for this post ?
$meta_value = get_post_meta($post->ID, '_exclude_autolinks', true);
if (!empty($meta_value)) {
return $title;
}
if (count($post_tags) > 0) {
foreach ($post_tags as $post_tag) {
// Get option
$embedded = (isset($post_tag['embedded']) && is_array($post_tag['embedded']) && count($post_tag['embedded']) > 0) ? $post_tag['embedded'] : false;
if (!$embedded) {
continue;
}
if (!in_array($post->post_type, $embedded)) {
continue;
}
if ($post_tag['autolink_display'] === 'post_content') {
continue;
}
//reset tags just in case
self::$link_tags = [];
// Get currents tags if no exists
self::prepare_auto_link_tags($post_tag);
// Shuffle array
SimpleTags_Client::random_array(self::$link_tags);
// HTML Rel (tag/no-follow)
$rel = SimpleTags_Client::get_rel_attribut();
// only continue if the database actually returned any links
if (!isset(self::$link_tags) || !is_array(self::$link_tags) || empty(self::$link_tags)) {
$can_continue = false;
} else {
$can_continue = true;
}
if ($can_continue) {
// Case option ?
$case = (1 === (int) $post_tag['ignore_case']) ? 'i' : '';
$strpos_fnc = ('i' === $case) ? 'stripos' : 'strpos';
// Prepare exclude terms array
$excludes_terms = explode(',', $post_tag['auto_link_exclude']);
if (empty($excludes_terms)) {
$excludes_terms = array();
} else {
$excludes_terms = array_filter($excludes_terms, '_delete_empty_element');
$excludes_terms = array_unique($excludes_terms);
}
$z = 0;
$auto_link_replace = [];
foreach ((array) self::$link_tags as $term_name => $term_link) {
$z++;
// Force string for tags "number"
$term_name = (string) $term_name;
// Exclude terms ? next...
if (taxopress_in_array_i($term_name, (array) $excludes_terms, true)) {
continue;
}
// Make a first test with PHP function, economize CPU with regexp
if (false === $strpos_fnc($title, $term_name)) {
continue;
}
$auto_link_replace[] = [
'term_name' => $term_name,
'term_link' => $term_link,
'case' => $case,
'rel' => $rel,
'options' => $post_tag,
'option_id' => $post_tag['ID'],
'post_limit' => $post_tag['autolink_usage_max'],
'term_limit' => $post_tag['autolink_same_usage_max'],
'type' => 'content',
];
if (!class_exists('DOMDocument') || !class_exists('DOMXPath')) {
self::replace_by_links_regexp($title, $term_name, $term_link, $case, $rel, $post_tag);
}
}
if (class_exists('DOMDocument') && class_exists('DOMXPath')) {
self::replace_by_links_dom($title, $auto_link_replace);
}
}
}
}
return $title;
}
}