1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2017 Peter Putzer. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* |
21
|
|
|
* *** |
22
|
|
|
* |
23
|
|
|
* @package mundschenk-at/php-typography |
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace PHP_Typography\Fixes\Token_Fixes; |
28
|
|
|
|
29
|
|
|
use PHP_Typography\Fixes\Token_Fix; |
30
|
|
|
use PHP_Typography\RE; |
31
|
|
|
use PHP_Typography\Settings; |
32
|
|
|
use PHP_Typography\Text_Parser\Token; |
33
|
|
|
use PHP_Typography\U; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Wraps email parts zero-width spaces (if enabled). |
37
|
|
|
* |
38
|
|
|
* @author Peter Putzer <[email protected]> |
39
|
|
|
* |
40
|
|
|
* @since 5.0.0 |
41
|
|
|
*/ |
42
|
|
|
class Wrap_Emails_Fix extends Abstract_Token_Fix { |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* A regular expression matching email addresses. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $email_pattern; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates a new fix instance. |
53
|
|
|
* |
54
|
|
|
* @param bool $feed_compatible Optional. Default false. |
55
|
|
|
*/ |
56
|
1 |
|
public function __construct( $feed_compatible = false ) { |
57
|
1 |
|
parent::__construct( Token_Fix::OTHER, $feed_compatible ); |
58
|
|
|
|
59
|
1 |
|
$this->email_pattern = "/(?: |
60
|
|
|
\A |
61
|
|
|
[a-z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+ |
62
|
|
|
(?: |
63
|
|
|
\. |
64
|
|
|
[a-z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+ |
65
|
|
|
)* |
66
|
|
|
@ |
67
|
|
|
(?: |
68
|
|
|
[a-z0-9] |
69
|
|
|
[a-z0-9\-]{0,61} |
70
|
|
|
[a-z0-9] |
71
|
|
|
\. |
72
|
|
|
)+ |
73
|
|
|
(?: |
74
|
1 |
|
" . RE::top_level_domains() . ' |
75
|
|
|
) |
76
|
|
|
\Z |
77
|
|
|
)/Sxi'; // required modifiers: x (multiline pattern) i (case insensitive). |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Apply the tweak to a given textnode. |
82
|
|
|
* |
83
|
|
|
* @param Token[] $tokens Required. |
84
|
|
|
* @param Settings $settings Required. |
85
|
|
|
* @param bool $is_title Optional. Default false. |
86
|
|
|
* @param \DOMText|null $textnode Optional. Default null. |
87
|
|
|
* |
88
|
|
|
* @return Token[] An array of tokens. |
89
|
|
|
*/ |
90
|
6 |
|
public function apply( array $tokens, Settings $settings, $is_title = false, \DOMText $textnode = null ) { |
91
|
6 |
|
if ( empty( $settings[ Settings::EMAIL_WRAP ] ) ) { |
92
|
3 |
|
return $tokens; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Test for and parse urls. |
96
|
3 |
|
foreach ( $tokens as $index => $token ) { |
97
|
3 |
|
$value = $token->value; |
98
|
3 |
|
if ( \preg_match( $this->email_pattern, $value, $email_match ) ) { |
99
|
3 |
|
$tokens[ $index ] = $token->with_value( \preg_replace( '/([^a-zA-Z0-9])/S', '$1' . U::ZERO_WIDTH_SPACE, $value ) ); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return $tokens; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|