From 847cc0965bee71905f0afd45c71067425b57e52c Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 26 Nov 2021 18:17:24 +0100 Subject: [PATCH 1/2] feat get_acf_link_classes and fix title attribute --- inc/Helpers/Formatting/Link.php | 52 +++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/inc/Helpers/Formatting/Link.php b/inc/Helpers/Formatting/Link.php index e23fdbfc..61c1570b 100644 --- a/inc/Helpers/Formatting/Link.php +++ b/inc/Helpers/Formatting/Link.php @@ -34,11 +34,12 @@ function get_acf_link( array $attributes, array $settings = [] ): string { return ''; } + $content = $attributes['field']['title']; + $attributes = wp_parse_args( $attributes, [ 'href' => $attributes['field']['url'], - 'title' => $attributes['field']['title'], 'target' => $attributes['field']['target'], ] ); @@ -51,7 +52,7 @@ function get_acf_link( array $attributes, array $settings = [] ): string { $settings = wp_parse_args( $settings, [ - 'content' => $attributes['title'], + 'content' => $content, ] ); @@ -213,3 +214,50 @@ function get_the_link( array $attributes, array $settings = [] ): string { 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 ); + } + + if ( trailingslashit( $field['url'] ) === trailingslashit( home_url( add_query_arg( null, null ) ) ) ) { + $classes = wp_parse_args( + $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 = wp_parse_args( + $classes, + [ + 'external' => 'external-menu-item', + ] + ); + } + + return implode( ' ', $classes ); + +} From 3c7d52a774d730e2d27225b4bbeda4863ef6ee52 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 4 Mar 2022 13:33:20 +0100 Subject: [PATCH 2/2] fix pr --- inc/Helpers/Formatting/Link.php | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/inc/Helpers/Formatting/Link.php b/inc/Helpers/Formatting/Link.php index 61c1570b..07e6aefc 100644 --- a/inc/Helpers/Formatting/Link.php +++ b/inc/Helpers/Formatting/Link.php @@ -236,26 +236,16 @@ 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 = wp_parse_args( - $classes, - [ - 'current' => 'current-menu-item', - ] - ); + $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 = wp_parse_args( - $classes, - [ - 'external' => 'external-menu-item', - ] - ); + $classes ['external'] = 'external-menu-item'; } return implode( ' ', $classes );