| 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 |
|