-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLink.php
More file actions
287 lines (251 loc) · 10.7 KB
/
Link.php
File metadata and controls
287 lines (251 loc) · 10.7 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace BEA\Theme\Framework\Helpers\Formatting\Link;
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value;
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_attribute_value;
/**
* @usage BEA\Theme\Framework\Helpers\Formatting\Link\get_acf_link( ['field' => ..., 'class' => ...], [ 'before' => '<p>%s', 'after' => '</p>' ] );
*
* @param array $attributes {
* Attributes for the acf link markup.
*
* @type array $field ACF link field. Example ['url' => 'https://....', 'title' => 'My title', 'target' => '_blank' ]
* @type string $target Target for the link.
* @type string $class CSS class name or space-separated list of classes.
* Default is empty.
* @type string $rel The attribute indicates the relationship between the target of the link and the object making the link.
* }
*
* @param array $settings {
* Optional. Settings for the acf link markup.
*
* @type string $content Optional. The content of the link
* @type string $before Optional. Markup to prepend to the image. Default empty.
* @type string $after Optional. Markup to append to the image. Default empty.
* @type array $escape Optional. An array where we specify as key the value we want to escape and as value the method to use. Example for the href ['escape' => ['href' => 'esc_url'] ]
* @type string $new_window Optional. Add <span class="sr-only> for a11y
* @type string $mode Optional. For social links use mode 'button' for better SEO (it hides the link to search engines using a button element), by default 'link' returns an <a> element.
* }
*
* @return string Return the markup of the link
*/
function get_acf_link( array $attributes, array $settings = [] ): string {
if ( empty( $attributes['field']['url'] ) || empty( $attributes['field']['title'] ) ) {
return '';
}
$content = $attributes['field']['title'];
$attributes = wp_parse_args(
$attributes,
[
'href' => $attributes['field']['url'],
'target' => $attributes['field']['target'],
]
);
$content = $attributes['field']['title'];
// Unset unused field params
unset( $attributes['field'] );
$attributes = apply_filters( 'bea_theme_framework_acf_link_attribute', $attributes, $settings );
$settings = wp_parse_args(
$settings,
[
'content' => $content,
]
);
$settings = apply_filters( 'bea_theme_framework_acf_link_settings', $settings, $attributes );
return get_the_link( $attributes, $settings );
}
/**
* @usage BEA\Theme\Framework\Helpers\Formatting\Link\the_custom_link( ['url' => ..., 'title' => ...], [ 'wrapper' => '<p></p>' ] );
*
* @param array $attributes {
* Attributes for the acf link markup.
*
* @type array $field ACF link field. Example ['url' => 'https://....', 'title' => 'My title', 'target' => '_blank' ]
* @type string $target Target for the link.
* @type string $class CSS class name or space-separated list of classes.
* Default is empty.
* @type string $rel The attribute indicates the relationship between the target of the link and the object making the link.
* }
*
* @param array $settings {
* Optional. Settings for the acf link markup.
*
* @type string $content Optional. The content of the link
* @type string $before Optional. Markup to prepend to the image. Default empty.
* @type string $after Optional. Markup to append to the image. Default empty.
* @type array $escape Optional. An array where we specify as key the value we want to escape and as value the method to use. Example for the href ['escape' => ['href' => 'esc_url'] ]
* @type string $new_window Optional. Add <span class="sr-only> for a11y
* @type string $mode Optional. For social links use mode 'button' for better SEO (it hides the link to search engines using a button element), by default 'link' returns an <a> element.
* }
*
* @return void Echo of the link markup
*/
function the_acf_link( array $attributes, array $settings = [] ): void {
echo get_acf_link( $attributes, $settings ); //phpcs:ignore
}
/**
* @usage BEA\Theme\Framework\Helpers\Formatting\Link\get_link( ['href' => ..., 'title' => ...], [ 'wrapper' => '<p>%s</p>' ] );
*
* @param array $attributes {
* Attributes for the acf link markup.
*
* @type string $href URL link.
* @type string $title title link.
* @type string $target Target for the link.
* @type string $class CSS class name or space-separated list of classes.
* Default is empty.
* @type string $rel The attribute indicates the relationship between the target of the link and the object making the link.
* }
*
* @param array $settings {
* Optional. Settings for the link markup.
*
* @type string $content Optional. The content of the link
* @type string $before Optional. Markup to prepend to the image. Default empty.
* @type string $after Optional. Markup to append to the image. Default empty.
* @type array $escape Optional. An array where we specify as key the value we want to escape and as value the method to use. Example for the href ['escape' => ['href' => 'esc_url'] ]
* @type string $new_window Optional. Add <span class="sr-only> for a11y
* @type string $mode Optional. For social links use mode 'button' for better SEO (it hides the link to search engines using a button element), by default 'link' returns an <a> element.
*
* }
*
* @return string Return the markup of the link
*/
function get_the_link( array $attributes, array $settings = [] ): string {
if ( empty( $attributes['href'] ) ) {
return '';
}
$link_markup = '<a %s>%s%s</a>';
$attributes = wp_parse_args(
$attributes,
[
'title' => '',
'target' => '',
]
);
// For security reason if target _blank add rel noopener
if ( '_blank' === $attributes['target'] ) {
$attributes['rel'] = 'noopener';
$settings['new_window'] = ! empty( $settings['new_window'] ) ? $settings['new_window'] : '<span class="sr-only">' . esc_html__(
'New window',
'beapi-frontend-framework'
) . '</span>';
}
$attributes = apply_filters( 'bea_theme_framework_link_attributes', $attributes, $settings );
// revove empty attributes
if ( empty( $attributes['title'] ) ) {
unset( $attributes['title'] );
}
if ( empty( $attributes['target'] ) ) {
unset( $attributes['target'] );
}
$settings = wp_parse_args(
$settings,
[
'before' => '',
'content' => '',
'new_window' => '',
'after' => '',
'mode' => 'link',
'escape' => [
'href' => 'esc_url',
'data-href' => 'esc_url',
],
]
);
$settings = apply_filters( 'bea_theme_framework_link_settings', $settings, $attributes );
/**************************************** MODE BUTTON ****************************************/
if ( 'button' === $settings['mode'] ) {
$link_markup = '<button %s>%s%s</button>';
$attributes['data-seo-click'] = 'true';
$attributes['type'] = 'button';
$attributes['role'] = 'link';
$attributes['data-href'] = $attributes['href'];
$attributes['data-rel'] = $attributes['rel'];
$attributes['data-target'] = $attributes['target'];
unset( $attributes['href'], $attributes['rel'], $attributes['target'] );
}
/**************************************** START MARKUP LINK ****************************************/
$attributes_escaped = [];
foreach ( $attributes as $name => $value ) {
// Handle single attributes like checked or data-seo-target, if null no attribute value
if ( null === $value ) {
$attributes_escaped[] = $name;
} else {
// Use user escape function, or default
$value = escape_attribute_value( $value, $settings['escape'][ $name ] ?? 'esc_attr' );
$attributes_escaped[] = sprintf( '%s="%s"', $name, $value );
}
}
// Implode all attributes for display purposes
$attributes_escaped = implode( ' ', $attributes_escaped );
// Escape content for display purposes
$label = $settings['content'] ? escape_content_value( $settings['content'], $settings['escape']['content'] ?? 'wp_kses_post' ) : '';
$link_markup = sprintf( $link_markup, $attributes_escaped, $settings['new_window'], $label );
/**************************************** END MARKUP LINK ****************************************/
$link_markup = apply_filters( 'bea_theme_framework_link_markup', $link_markup, $attributes, $settings );
return $settings['before'] . $link_markup . $settings['after'];
}
/**
* @usage BEA\Theme\Framework\Helpers\Formatting\Link\the_link( ['href' => ..., 'title' => ...], [ 'wrapper' => '<p></p>' ] );
*
* @param array $attributes {
* Attributes for the link markup.
*
* @type string $href URL link.
* @type string $title title link.
* @type string $target Target for the link.
* @type string $class CSS class name or space-separated list of classes.
* Default is empty.
* @type string $rel The attribute indicates the relationship between the target of the link and the object making the link.
* }
*
* @param array $settings {
* Optional. Settings for the acf link markup.
*
* @type string $content Optional. The content of the link
* @type string $before Optional. Markup to prepend to the image. Default empty.
* @type string $after Optional. Markup to append to the image. Default empty.
* @type array $escape Optional. An array where we specify as key the value we want to escape and as value the method to use. Example for the href ['escape' => ['href' => 'esc_url'] ]
* @type string $new_window Optional. Add <span class="sr-only> for a11y
* @type string $mode Optional. For social links use mode 'button' for better SEO (it hides the link to search engines using a button element), by default 'link' returns an <a> element.
* }
*
*
* @return void Echo of the link markup
*/
function the_link( array $attributes, array $settings = [] ): void {
echo get_the_link( $attributes, $settings );
}
/**
* @usage BEA\Theme\Framework\Helpers\Formatting\Link\get_acf_link_classes( ['url' => ...], [ 'menu-item'] );
*
* @param array|null $field {
*
* @type string $url
* @type string $title
* @type string $target
* }
*
* @param array $classes {
*
* @type string $current
* @type string $external
* }
*
* @return string Echo of the link classes
*/
function get_acf_link_classes( $field, array $classes ): string {
if ( empty( $field['url'] ) ) {
return implode( ' ', $classes );
}
// chek if current is the current url marches with the url of the field
if ( trailingslashit( $field['url'] ) === trailingslashit( home_url( add_query_arg( null, null ) ) ) ) {
$classes ['current'] = 'current-menu-item';
}
$components = wp_parse_url( $field['url'] );
$base = wp_parse_url( home_url( '/' ) );
if ( ! empty( $components['host'] ) && ! empty( $base['host'] ) && strcasecmp( $components['host'], $base['host'] ) ) {
$classes ['external'] = 'external-menu-item';
}
return implode( ' ', $classes );
}