Skip to content

Commit 5d84be0

Browse files
committed
PR 12/11
1 parent e4f1800 commit 5d84be0

File tree

5 files changed

+46
-55
lines changed

5 files changed

+46
-55
lines changed

inc/Helpers/Formatting/Escape.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* Method for escaping attributes
66
*
77
* @param string $value Value to be escaped
8-
* @param callable|string $escape We can pass an anonymous function or method in native escape form as a string (example: 'esc_url'). By default the method escapes the attributes
8+
* @param string $escape We can pass an anonymous function or method in native escape form as a string (example: 'esc_url'). By default the method escapes the attributes
99
*
1010
* @return mixed Return value escape
1111
*/
12-
function escape_attribute_value( string $value, $escape = 'esc_attr' ) {
13-
if ( ! is_callable( $escape ) ) {
14-
return esc_attr( $value );
12+
function escape_attribute_value( string $value, string $escape ) {
13+
if ( ! function_exists( $escape ) ) {
14+
return $value;
1515
}
1616

1717
return $escape( $value );
@@ -21,13 +21,13 @@ function escape_attribute_value( string $value, $escape = 'esc_attr' ) {
2121
* Method for escaping html or content
2222
*
2323
* @param string $value Value to be escaped
24-
* @param callable|string $escape We can pass an anonymous function or method in native escape form as a string (example: 'wp_kses'). By default the method escapes the html
24+
* @param string $escape We can pass an anonymous function or method in native escape form as a string (example: 'wp_kses'). By default the method escapes the html
2525
*
2626
* @return mixed Return value escape
2727
*/
28-
function escape_content_value( string $value, $escape = 'esc_html' ) {
29-
if ( ! is_callable( $escape ) ) {
30-
return esc_html( $value );
28+
function escape_content_value( string $value, string $escape ) {
29+
if ( ! function_exists( $escape ) ) {
30+
return $value;
3131
}
3232

3333
return $escape( $value );

inc/Helpers/Formatting/Image.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Attributes for the image markup.
1111
*
1212
* @type string $src Image attachment URL.
13-
* @type string $data-location Image attachment URL.
13+
* @type string $data-location Location of image.
1414
* @type string $class CSS class name or space-separated list of classes.
1515
* Default `attachment-$size_class size-$size_class`,
1616
* where `$size_class` is the image size being requested.
@@ -83,7 +83,7 @@ function get_the_image( int $image_id, array $attributes, array $settings = [] )
8383
* Attributes for the image markup.
8484
*
8585
* @type string $src Image attachment URL.
86-
* @type string $data-location Image attachment URL.
86+
* @type string $data-location Location of image.
8787
* @type string $class CSS class name or space-separated list of classes.
8888
* Default `attachment-$size_class size-$size_class`,
8989
* where `$size_class` is the image size being requested.

inc/Helpers/Formatting/Link.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @type string $before Optional. Markup to prepend to the image. Default empty.
2525
* @type string $after Optional. Markup to append to the image. Default empty.
2626
* @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'] ]
27-
*
27+
* @type string $new_window Optional. Add <span class="sronly> for a11y
2828
* }
2929
*
3030
* @return string Return the markup of the link
@@ -80,7 +80,7 @@ function get_acf_link( array $attributes, array $settings = [] ): string {
8080
* @type string $before Optional. Markup to prepend to the image. Default empty.
8181
* @type string $after Optional. Markup to append to the image. Default empty.
8282
* @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'] ]
83-
*
83+
* @type string $new_window Optional. Add <span class="sronly> for a11y
8484
* }
8585
*
8686
* @return void Echo of the link markup
@@ -104,12 +104,13 @@ function the_acf_link( array $attributes, array $settings = [] ): void {
104104
* }
105105
*
106106
* @param array $settings {
107-
* Optional. Settings for the acf link markup.
107+
* Optional. Settings for the link markup.
108108
*
109109
* @type string $content Optional. The content of the link
110110
* @type string $before Optional. Markup to prepend to the image. Default empty.
111111
* @type string $after Optional. Markup to append to the image. Default empty.
112112
* @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'] ]
113+
* @type string $new_window Optional. Add <span class="sronly> for a11y
113114
*
114115
* }
115116
*
@@ -130,18 +131,23 @@ function get_the_link( array $attributes, array $settings = [] ): string {
130131

131132
// For security reason if target _blank add rel noopener
132133
if ( '_blank' === $attributes['target'] ) {
133-
$attributes['rel'] = 'noopener';
134+
$attributes['rel'] = 'noopener';
135+
$settings['new_window'] = ! empty( $settings['new_window'] ) ? $settings['new_window'] : '<span class="sronly">' . esc_html__(
136+
'New window',
137+
'beapi-frontend-framework'
138+
) . '</span>';
134139
}
135140

136141
$attributes = apply_filters( 'bea_theme_framework_link_attributes', $attributes, $settings );
137142

138143
$settings = wp_parse_args(
139144
$settings,
140145
[
141-
'before' => '',
142-
'content' => $attributes['title'],
143-
'after' => '',
144-
'escape' => [
146+
'before' => '',
147+
'content' => $attributes['title'],
148+
'new_window' => '',
149+
'after' => '',
150+
'escape' => [
145151
'href' => 'esc_url',
146152
],
147153
]
@@ -151,21 +157,21 @@ function get_the_link( array $attributes, array $settings = [] ): string {
151157

152158
/**************************************** START MARKUP LINK ****************************************/
153159

154-
$attributes_escape = [];
160+
$attributes_escaped = [];
155161
foreach ( $attributes as $name => $value ) {
156162
// Use user escape function, or default
157163
$value = escape_attribute_value( $value, $settings['escape'][ $name ] ?? '' );
158164

159165
// Handle single attributes like checked or data-seo-target, if null no attribute value
160-
$attributes_escape[] = null === $value ? $name : sprintf( '%s="%s"', $name, $value );
166+
$attributes_escaped[] = null === $value ? $name : sprintf( '%s="%s"', $name, $value );
161167
}
162168

163169
// Implode all attributes for display purposes
164-
$attributes_escape = implode( ' ', $attributes_escape );
170+
$attributes_escaped = implode( ' ', $attributes_escaped );
165171
// Escape content for display purposes
166172
$label = escape_content_value( $settings['content'], $settings['escape']['content'] ?? '' );
167173

168-
$link_markup = sprintf( '<a %s>%s</a>', $attributes_escape, $label );
174+
$link_markup = sprintf( '<a %s>%s%s</a>', $attributes_escaped, $settings['new_window'], $label );
169175

170176
/**************************************** END MARKUP LINK ****************************************/
171177

@@ -175,10 +181,10 @@ function get_the_link( array $attributes, array $settings = [] ): string {
175181
}
176182

177183
/**
178-
* @usage BEA\Theme\Framework\Helpers\Formatting\Link\the_link( ['url' => ..., 'title' => ...], [ 'wrapper' => '<p></p>' ] );
184+
* @usage BEA\Theme\Framework\Helpers\Formatting\Link\the_link( ['href' => ..., 'title' => ...], [ 'wrapper' => '<p></p>' ] );
179185
*
180186
* @param array $attributes {
181-
* Attributes for the acf link markup.
187+
* Attributes for the link markup.
182188
*
183189
* @type string $href URL link.
184190
* @type string $title title link.
@@ -195,7 +201,7 @@ function get_the_link( array $attributes, array $settings = [] ): string {
195201
* @type string $before Optional. Markup to prepend to the image. Default empty.
196202
* @type string $after Optional. Markup to append to the image. Default empty.
197203
* @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'] ]
198-
*
204+
* @type string $new_window Optional. Add <span class="sronly> for a11y
199205
* }
200206
*
201207
*

inc/Helpers/Formatting/Term.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
<?php
22
namespace BEA\Theme\Framework\Helpers\Formatting\Term;
33

4+
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value;
45
/**
56
* @usage BEA\Theme\Framework\Helpers\Formatting\Term\get_terms_name( 0, 'news-type' );
67
*
7-
* @param int $post_id Post ID
8+
* @param int|\WP_Post $post Post ID or object.
89
* @param string $taxonomy Taxonomy name
910
*
1011
* @return array Return an array with the terms name
1112
*/
12-
function get_terms_name( int $post_id, string $taxonomy ): array {
13-
if ( empty( $post_id ) || empty( $taxonomy ) ) {
14-
return [];
15-
}
16-
13+
function get_terms_name( $post_id, string $taxonomy ): array {
1714
$terms = get_the_terms( $post_id, $taxonomy );
1815

19-
if ( empty( $terms ) || is_wp_error( $terms ) ) {
16+
if ( false !== $terms || is_wp_error( $terms ) ) {
2017
return [];
2118
}
2219

@@ -26,13 +23,9 @@ function get_terms_name( int $post_id, string $taxonomy ): array {
2623
/**
2724
* @usage BEA\Theme\Framework\Helpers\Formatting\Term\get_terms_list( ['post_id' => 0,'taxonomy' => 'news-type'],['items' => '<span>%s</span>', 'separator' => ' ', 'wrapper' => '<p>%s</p>'] );
2825
*
29-
* @param array $attributes {
30-
* Attributes for generating the list of terms
26+
* @param int|\WP_Post $post Post ID or object.
27+
* @param string $taxonomy Taxonomy name.
3128
*
32-
* @type int $post_id ID of the post
33-
* @type string $taxonomy Name of the taxonomy
34-
*
35-
* }
3629
* @param array $settings {
3730
* Optional. Settings for the terms list markup.
3831
*
@@ -46,13 +39,9 @@ function get_terms_name( int $post_id, string $taxonomy ): array {
4639
*
4740
* @return string
4841
*/
49-
function get_terms_list( array $attributes, array $settings = [] ): string {
50-
if ( empty( $attributes['post_id'] ) || empty( $attributes['taxonomy'] ) ) {
51-
return '';
52-
}
53-
54-
$attributes = apply_filters( 'bea_theme_framework_term_list_attributes', $attributes, $settings );
55-
$terms = get_terms_name( $attributes['post_id'], $attributes['taxonomy'] );
42+
function get_terms_list( $post, string $taxonomy, array $settings = [] ): string {
43+
$attributes = apply_filters( 'bea_theme_framework_term_list_attributes', $post, $taxonomy, $settings );
44+
$terms = get_terms_name( $post, $taxonomy );
5645

5746
if ( empty( $terms ) ) {
5847
return '';
@@ -91,13 +80,9 @@ function get_terms_list( array $attributes, array $settings = [] ): string {
9180
/**
9281
* @usage BEA\Theme\Framework\Helpers\Formatting\Term\the_terms_list( ['post_id' => 0,'taxonomy' => 'news-type'],['items' => '<span>%s</span>', 'separator' => ' ', 'wrapper' => '<p>%s</p>'] );
9382
*
94-
* @param array $attributes {
95-
* Attributes for generating the list of terms
83+
* @param int|\WP_Post $post Post ID or object.
84+
* @param string $taxonomy Taxonomy name.
9685
*
97-
* @type int $post_id ID of the post
98-
* @type string $taxonomy Name of the taxonomy
99-
*
100-
* }
10186
* @param array $settings {
10287
* Optional. Settings for the terms list markup.
10388
*
@@ -109,6 +94,6 @@ function get_terms_list( array $attributes, array $settings = [] ): string {
10994
*
11095
* }
11196
*/
112-
function the_terms_list( array $attributes, array $settings = [] ): void {
113-
echo get_terms_list( $attributes, $settings );
97+
function the_terms_list( $post, string $taxonomy, array $settings = [] ): void {
98+
echo get_terms_list( $post, $taxonomy, $settings );
11499
}

inc/Helpers/Formatting/Text.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @type string $before Optional. Markup to prepend to the text. Default empty.
1414
* @type string $after Optional. Markup to prepend to the text. Default empty.
15-
* @type string $escpae Optional. Markup to prepend to the item. Default esc_html.
15+
* @type string $escape Optional. Markup to prepend to the item. Default esc_html.
1616
*
1717
* }
1818
*
@@ -32,7 +32,7 @@ function the_text( string $value, array $settings = [] ): void {
3232
*
3333
* @type string $before Optional. Markup to prepend to the text. Default empty.
3434
* @type string $after Optional. Markup to prepend to the text. Default empty.
35-
* @type string $escpae Optional. Markup to prepend to the item. Default esc_html.
35+
* @type string $escape Optional. Markup to prepend to the item. Default esc_html.
3636
*
3737
* }
3838
*

0 commit comments

Comments
 (0)