PluginProbe
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP / trunk
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP vtrunk
3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 3.0.0 2.4.13 2.4.12 2.4.11 2.4.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 110 releases
betterlinks / includes / Frontend / LinkChecker.php

LinkChecker.php in BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP trunk, at includes/Frontend/LinkChecker.php

87 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BetterLinks\Frontend;
4 if ( ! defined( 'ABSPATH' ) ) { exit; }
5
6 use BetterLinks\Admin\Cache;
7 use BetterLinks\Traits\Query;
8
9 class LinkChecker {
10 use Query;
11
12 private $link;
13 private $settings;
14
15
16 public function __construct() {
17 $this->settings = (object) Cache::get_json_settings();
18 add_filter( 'the_content', array( $this, 'check_links' ), 100 );
19 }
20
21 public function check_links( $content) {
22 preg_match_all('/<a[^>]*\bclass\s*=\s*["\'][^"\']*\bbetterlinks-linked-text\b[^"\']*["\'][^>]*>(.*?)<\/a>/i', $content, $matches, PREG_OFFSET_CAPTURE);
23
24 $next = 0;
25 foreach ($matches[0] as $match) {
26 $linkId = $this->get_string_text( $match[0], 'data-link-id' );
27 if( empty( $linkId ) ) {
28 continue;
29 }
30
31 $link = self::get_link_by_ID(intval($linkId));
32 $this->link = is_array( $link ) ? current( $link ) : false;
33 if( empty( $this->link ) ) continue;
34
35 $href = $this->get_string_text( $match[0], 'href' );
36 if( empty( $href ) ) {
37 continue;
38 }
39 $href = $this->check_hrefs($href);
40
41 $pattern = '/(<a\s+[^>]*href\s*=\s*["\'])([^"\']*)(["\'][^>]*>)/i';
42 $replacement = '${1}' . $href . '${3}';
43 $replace = preg_replace($pattern, $replacement, $match[0]);
44
45 // if( empty( $this->link['uncloaked'] ) ){
46 // $pattern = '/\s*data-link-id=["\'][^"\']*["\']/i';
47 // $replace = preg_replace($pattern, '', $replace);
48 // }
49 // error_log( print_r( $this->link, true ) );
50
51 $match[1] = $match[1] + $next;
52 $content = substr_replace( $content , $replace , $match[1] , strlen( $match[0] ) );
53
54 $next = $next + strlen( $replace ) - strlen( $match[0] );
55 }
56 return $content;
57 }
58
59 private function get_string_text($string, $attr){
60 preg_match('/'. $attr .'="([^"]+)"/', $string, $matches);
61 if( empty( $matches ) ) return '';
62 $value = explode('=', $matches[0]);
63 if( empty( $value[1] ) ) return '';
64 $value = trim($value[1], '"');
65 return $value;
66 }
67
68 private function check_hrefs($href) {
69 // error_log( print_r( $this->link, true ) );
70 if( !empty( $this->link['uncloaked'] ) ){
71 if( $href !== $this->link['target_url'] ){
72 $href = $this->link['target_url'];
73 }
74 }else {
75 $short_url = site_url('/');
76 // error_log( print_r( $this->link['short_url'], true ) );
77 // $short_url .= !empty( $this->settings->prefix ) ? $this->settings->prefix . '/' . $this->link['short_url'] : $this->link['short_url'];
78 $short_url .= $this->link['short_url'];
79 if( $href !== $short_url) {
80 $href = $short_url;
81 }
82 }
83 return $href;
84 }
85
86 }
87