forked from xserver-inc/cocoon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
links.php
274 lines (241 loc) · 9.43 KB
/
links.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php //本文のリンク設定
/**
* Cocoon WordPress Theme
* @author: yhira
* @link: https://wp-cocoon.com/
* @license: http://www.gnu.org/licenses/gpl-2.0.html GPL v2 or later
*/
if ( !defined( 'ABSPATH' ) ) exit;
//アンカーリンクはブログカードか
if ( !function_exists( 'is_anchor_link_tag_blogcard' ) ):
function is_anchor_link_tag_blogcard($anchor_link_tag){
//_v($anchor_link_tag);
if (strpos($anchor_link_tag, ' class="blogcard-wrap ') !== false) {
return true;
}
}
endif;
//本文の内部・外部リンクの置換
add_filter('the_content', 'replace_anchor_links', 12);
add_filter('the_author_box_name', 'replace_anchor_links', 12);
add_filter('get_the_author_description', 'replace_anchor_links', 12);
add_filter('widget_text', 'replace_anchor_links', 12);//テキストウィジェットをフック
add_filter('widget_text_pc_text', 'replace_anchor_links', 12);
add_filter('widget_classic_text', 'replace_anchor_links', 12);
add_filter('widget_text_mobile_text', 'replace_anchor_links', 12);
add_filter('the_category_tag_content', 'replace_anchor_links', 12);
if ( !function_exists( 'replace_anchor_links' ) ):
function replace_anchor_links($the_content) {
$res = preg_match_all('{<a [^>]+?>.+?</a>}is', $the_content, $m);
//_v($m);
//Aリンクがある場合
if ($res && $m[0]) {
foreach ($m[0] as $value) {
//初期値の設定
$old_a = $value;
$new_a = $value;
//aタグの均一化
$value = strtolower(str_replace('”', '"', $value));
//rel属性値の取得
$rels = array();
$res = preg_match('/ *rel="([^"]*?)"/i', $new_a, $m);
//rel属性があれば値を取得する
if ($res && $m[1]) {
$rels = explode(' ', $m[1]);
}
//目次リンク(#リンク)の場合
if ((strpos($value, 'href="#') > 0)) {
continue;
}
//イメージリンクを除外
if (preg_match('{<a[^>]+?href="[^"]+?"[^>]*?>\s*?<img .+?>\s*?</a>}is', $value)) {
continue;
}
// _v($value);
// _v(includes_string($value, 'href="'.home_url()));
if (
//ホームURLを含んでいるか
includes_string($value, 'href="'.home_url()) ||
//http,httpsなしのURLだった場合
includes_string($value, 'href="'.preg_replace('/https?:/', '', home_url())) ||
!preg_match('{href="(https?:)?//}', $value)
) {//内部リンクの場合
//if ( preg_match('{href="https?://'.get_the_site_domain().'}i') ) {//内部リンクの場合
//リンクの開き方を変更する
$new_a = replace_target_attr_tag( get_internal_link_open_type(), $new_a );
//フォロータイプの設定
$rels = get_rel_follow_attr_values( get_internal_link_follow_type(), $rels );
//noopenerの追加と削除
$rels = get_noopener_rels( is_internal_link_noopener_enable(), $rels );
//target="_blank"のnoopener
if (!is_internal_link_noopener_enable() && includes_target_blalk($new_a)) {
$rels = get_noopener_rels( is_internal_target_blank_link_noopener_enable(), $rels );
}
//noreferrerの追加と削除
$rels = get_noreferrer_rels( is_internal_link_noreferrer_enable(), $rels );
//target="_blank"のnoreferrer
if (!is_internal_link_noreferrer_enable() && includes_target_blalk($new_a)) {
$rels = get_noreferrer_rels( is_internal_target_blank_link_noreferrer_enable(), $rels );
}
if (!is_anchor_link_tag_blogcard($value)) {
//アイコンフォントの表示
$new_a = replace_link_icon_font_tag( is_internal_link_icon_visible(), get_internal_link_icon(), 'internal-icon anchor-icon', $new_a );
}
} else { //外部リンクの場合
//リンクの開き方を変更する
$new_a = replace_target_attr_tag( get_external_link_open_type(), $new_a );
//フォロータイプの設定
$rels = get_rel_follow_attr_values( get_external_link_follow_type(), $rels );
//noopenerの追加と削除
$rels = get_noopener_rels( is_external_link_noopener_enable(), $rels );
//target="_blank"のnoopener
if (!is_external_link_noopener_enable() && includes_target_blalk($new_a)) {
$rels = get_noopener_rels( is_external_target_blank_link_noopener_enable(), $rels );
}
//noreferrerの追加と削除
$rels = get_noreferrer_rels( is_external_link_noreferrer_enable(), $rels );
//target="_blank"のnoreferrer
if (!is_external_link_noreferrer_enable() && includes_target_blalk($new_a)) {
$rels = get_noreferrer_rels( is_external_target_blank_link_noreferrer_enable(), $rels );
}
//externalの追加と削除
$rels = get_external_rels( is_external_link_external_enable(), $rels );
if (!is_anchor_link_tag_blogcard($value)) {
//アイコンフォントの表示
$new_a = replace_link_icon_font_tag( is_external_link_icon_visible(), get_external_link_icon(), 'external-icon anchor-icon', $new_a );
}
}//内部リンクか外部リンクか条件分岐の終わり
//変更する場合はrel属性のクリアを行う
$new_a = preg_replace('/ *rel="[^"]*?"/i', '', $new_a);
$new_a = preg_replace('/<a/i', '<a rel="'.implode(' ', $rels).'"', $new_a);
//rel属性が空の場合は削除
$new_a = str_replace(' rel=""', '', $new_a);
//何かしらの変更があった場合
if ($old_a != $new_a) {
$the_content = str_replace($old_a, $new_a, $the_content);
}
}//foreach
}//Aリンクがある場合
return $the_content;
}
endif;
//ビジュアルエディターでrel="noopener noreferrer"自動付加の解除
add_filter('tiny_mce_before_init','tinymce_allow_unsafe_link_target');
if ( !function_exists( 'tinymce_allow_unsafe_link_target' ) ):
function tinymce_allow_unsafe_link_target( $mce_init ) {
$mce_init['allow_unsafe_link_target'] = true;
return $mce_init;
}
endif;
//配列に文字列を追加
if ( !function_exists( 'add_string_to_array' ) ):
function add_string_to_array( $add_string, $rels ) {
if (!in_array($add_string, $rels)) {
$rels[] = $add_string;
}
return $rels;
}
endif;
//配列にnoopenerの追加と削除
if ( !function_exists( 'get_noopener_rels' ) ):
function get_noopener_rels( $noopener_enable, $rels ) {
//noopenerの追加と削除
if ($noopener_enable) {
//noopenerの追加
$rels = add_string_to_array( 'noopener', $rels );
} else {
//noopenerの削除
$rels = delete_string_from_array( 'noopener', $rels );
}
return $rels;
}
endif;
//配列にnoreferrerの追加と削除
if ( !function_exists( 'get_noreferrer_rels' ) ):
function get_noreferrer_rels( $noreferrer_enable, $rels ) {
//noreferrerの追加と削除
if ($noreferrer_enable) {
//noreferrerの追加
$rels = add_string_to_array( 'noreferrer', $rels );
} else {
//noreferrerの削除
$rels = delete_string_from_array( 'noreferrer', $rels );
}
return $rels;
}
endif;
//配列にexternalの追加と削除
if ( !function_exists( 'get_external_rels' ) ):
function get_external_rels( $external_enable, $rels ) {
//externalの追加と削除
if ($external_enable) {
//externalの追加
$rels = add_string_to_array( 'external', $rels );
} else {
//externalの削除
$rels = delete_string_from_array( 'external', $rels );
}
return $rels;
}
endif;
//配列から文字列を削除
if ( !function_exists( 'delete_string_from_array' ) ):
function delete_string_from_array( $delete_string, $rels ) {
if(($key = array_search($delete_string, $rels)) !== false) {
unset($rels[$key]);
}
return $rels;
}
endif;
//target属性の置換
if ( !function_exists( 'replace_target_attr_tag' ) ):
function replace_target_attr_tag( $link_open_type, $the_a_tag ) {
//リンクの開き方を変更する場合
if ($link_open_type != 'default') {
//外部リンクの開き方を変更する場合はtarget属性のクリアを行う
$the_a_tag = preg_replace('/ *target="[^"]*?"/i', '', $the_a_tag);
switch ($link_open_type) {
case 'blank':
$the_a_tag = str_replace('<a', '<a target="_blank"', $the_a_tag);
break;
case 'self':
$the_a_tag = str_replace('<a', '<a target="_self"', $the_a_tag);
break;
}
}
return $the_a_tag;
}
endif;
//リンクアイコンフォントの置換
if ( !function_exists( 'replace_link_icon_font_tag' ) ):
function replace_link_icon_font_tag( $icon_font_visible, $icon_font, $class, $the_a_tag ) {
//アイコンフォントの表示
if ($icon_font_visible) {
$the_a_tag = str_replace('</a>', '<span class="fa '.$icon_font.' '.$class.'"></span></a>', $the_a_tag);
}
return $the_a_tag;
}
endif;
//follow, nofollowのrel属性値取得
if ( !function_exists( 'get_rel_follow_attr_values' ) ):
function get_rel_follow_attr_values( $follow_type, $rels ) {
//フォロータイプの設定
if ($follow_type != 'default') {
switch ($follow_type) {
case 'nofollow':
//nofollowの追加
$rels = add_string_to_array( 'nofollow', $rels );
//followがある場合は削除
$rels = delete_string_from_array( 'follow', $rels );
break;
case 'follow':
//followの追加
$rels = add_string_to_array( 'follow', $rels );
//nofollowがある場合は削除
$rels = delete_string_from_array( 'nofollow', $rels );
break;
}
}
return $rels;
}
endif;