Plugin Directory

Changeset 2931487


Ignore:
Timestamp:
06/27/2023 03:25:39 PM (3 years ago)
Author:
blapps
Message:

New feature and bug fixes

Location:
customize-external-links-and-add-icon/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • customize-external-links-and-add-icon/trunk/customize-external-links.php

    r2767240 r2931487  
    55 * Description: Add nofollow and remove noreferrer attributes in external links. Choose between different icons to show users which link is an external one.
    66 * Author: blapps
    7  * Version: 1.9.2
    8  * Tested up to: 6.0
     7 * Version: 2.0
     8 * Tested up to: 6.2
    99 * Text Domain: customize-external-links
    1010 * Domain Path: /languages
     
    3030
    3131    if (str_starts_with($url, '#')) {
     32        return true;
     33    }
     34
     35    if (str_starts_with($url, '/#')) {
    3236        return true;
    3337    }
     
    178182}
    179183
     184function cuexlinks_add_rel_noreferrer($url, $tag)
     185{
     186    $no_referrer = '';
     187    // $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
     188    // $pattern = '/rel\s*=\s*\"[a-zA-Z0-9_\s]*[n|d]ofollow[a-zA-Z0-9_\s]*\"/';
     189    $pattern = '/rel\s*=\s*\"[a-zA-Z0-9_\s]*\"/';
     190
     191    $result = preg_match($pattern, $url, $match);
     192
     193    if ($result === 0) {
     194        $no_referrer .= ' rel="noreferrer"';
     195    } else {
     196        if (
     197            strpos($match[0], 'noreferrer') === false &&
     198            strpos($match[0], 'referrer') === false
     199        ) {
     200            $temp = $match[0];
     201            $temp = substr_replace($temp, ' noreferrer"', -1);
     202            $tag = str_replace($match[0], $temp, $tag);
     203        }
     204    }
     205
     206    if ($no_referrer) {
     207        $tag = cuexlinks_update_close_tag($tag, $no_referrer);
     208    }
     209
     210    return $tag;
     211}
     212
     213
    180214function cuexlinks_update_close_tag($tag, $no_follow)
    181215{
     
    209243        if (cuexlinks_is_domain_not_excluded($url)) {
    210244            $tag = cuexlinks_add_rel_nofollow($url, $tag);
     245        }
     246
     247        $content = str_replace($url, $tag, $content);
     248    } // end for loop
     249
     250    $content = str_replace(']]>', ']]>', $content);
     251    return $content;
     252}
     253
     254function cuexlinks_url_parse_noreferrer($content)
     255{
     256
     257    $matches = cuexlinks_is_link_available($content);
     258
     259    if ($matches === null) {
     260        return $content;
     261    }
     262
     263    // loop through each links
     264    for ($i = 0; $i < count($matches); $i++) {
     265        $tag = $matches[$i][0];
     266        $url = $matches[$i][0];
     267
     268        $blogurl = get_site_url();
     269        //         if ( (cuexlinks_is_internal_link($url)) <> (strpos($url, $blogurl) != 0) ) {
     270
     271        if (cuexlinks_is_internal_link($url)) {
     272            continue;
     273        }
     274
     275        $tag = cuexlinks_add_target_blank($url, $tag);
     276
     277        //exclude domain or add nofollow
     278        if (cuexlinks_is_domain_not_excluded($url)) {
     279            $tag = cuexlinks_add_rel_noreferrer($url, $tag);
    211280        }
    212281
     
    357426} else {
    358427    // do nothing because Gutenberg adds noreferrer automatically
    359 }
     428    // new: to show it
     429    add_filter('the_content', 'cuexlinks_url_parse_noreferrer');
     430
     431}
     432
     433
     434
    360435
    361436function cuexlinks_define_icons()
  • customize-external-links-and-add-icon/trunk/readme.txt

    r2767240 r2931487  
    33Donate link:
    44Tags: nofollow, noreferer, icon external link, nofollow links, external links, nofollow for external links, nofollow external link, nofollow external links, custom icons, remove noreferrer
    5 Requires at least: 2.8.6
    6 Tested up to: 6.0
    7 Stable tag: 1.9.2
     5Requires at least: 4
     6Tested up to: 6.2
     7Stable tag: 2.0
    88License: GPL2
    99
     
    5656== Changelog ==
    5757
     58= 2.0 =
     59- added noreferrer
     60- fixed internal links starting with /#
     61- ready for WP 6.2
     62
     63
    5864= 1.9.2 =
    5965- fixed bugs in debug mode
Note: See TracChangeset for help on using the changeset viewer.