From 07642c69ba3050781400dbcc9c33530d34fa5691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arttu=20Erkkil=C3=A4?= Date: Wed, 27 Sep 2023 14:14:49 +0300 Subject: [PATCH 1/6] TMS-950: Empty check for phone repeater. --- CHANGELOG.MD | 2 ++ lib/Formatters/ContactFormatter.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 17621bab..6f030af8 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +- TMS-950: Empty check for phone repeater. + ## [1.49.0] - 2023-09-15 - TMS-962: Add Lunch Menus plugin caps to roles. diff --git a/lib/Formatters/ContactFormatter.php b/lib/Formatters/ContactFormatter.php index cce03568..715d2209 100644 --- a/lib/Formatters/ContactFormatter.php +++ b/lib/Formatters/ContactFormatter.php @@ -150,7 +150,7 @@ public function map_keys( array $posts, array $field_keys, $default_image = null $fields = $this->append_image( $fields, $field_key, $default_image ); } - if ( isset( $fields['phone_repeater'] ) ) { + if ( ! empty( $fields['phone_repeater'] ) ) { $fields['phone_repeater'] = array_filter( $fields['phone_repeater'], function ( $item ) { return ! empty( $item['phone_text'] ) || ! empty( $item['phone_number'] ); } ); From 0623387a8234a5d9e89c9bfdaa08fc6036b70de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= Date: Tue, 24 Oct 2023 14:49:47 +0300 Subject: [PATCH 2/6] TMS-950: Contacts & place-of-business blocks --- CHANGELOG.MD | 5 +- lib/ACF/Fields/ContactsFields.php | 11 ++-- lib/ACF/Fields/PlaceOfBusinessFields.php | 32 ++++++++++-- lib/ACF/PageContactsGroup.php | 1 - lib/Formatters/PlaceOfBusinessFormatter.php | 55 ++++++++++++++++++-- models/page-contacts.php | 11 +++- partials/shared/contact-item.dust | 56 +++++++++++---------- 7 files changed, 129 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 6f030af8..2ac033ba 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -- TMS-950: Empty check for phone repeater. +- TMS-950: + - Empty check for phone repeater. + - Place of business posts to block + - Contacts styles & accessibility ## [1.49.0] - 2023-09-15 diff --git a/lib/ACF/Fields/ContactsFields.php b/lib/ACF/Fields/ContactsFields.php index 14627c3b..9472922e 100644 --- a/lib/ACF/Fields/ContactsFields.php +++ b/lib/ACF/Fields/ContactsFields.php @@ -102,13 +102,13 @@ protected function sub_fields() : array { $key = $this->get_key(); $title_field = ( new Field\Text( $strings['title']['label'] ) ) - ->set_key( "${key}_title" ) + ->set_key( "{$key}_title" ) ->set_name( 'title' ) ->set_wrapper_width( 50 ) ->set_instructions( $strings['title']['instructions'] ); $description_field = ( new Field\Textarea( $strings['description']['label'] ) ) - ->set_key( "${key}_description" ) + ->set_key( "{$key}_description" ) ->set_name( 'description' ) ->set_rows( 4 ) ->set_new_lines( 'wpautop' ) @@ -116,7 +116,7 @@ protected function sub_fields() : array { ->set_instructions( $strings['description']['instructions'] ); $api_contacts_field = ( new Field\Select( $strings['api_contacts']['label'] ) ) - ->set_key( "${key}_api_contacts" ) + ->set_key( "{$key}_api_contacts" ) ->set_name( 'api_contacts' ) ->allow_multiple() ->allow_null() @@ -125,14 +125,15 @@ protected function sub_fields() : array { ->set_instructions( $strings['api_contacts']['instructions'] ); $contacts_field = ( new Field\Relationship( $strings['contacts']['label'] ) ) - ->set_key( "${key}_contacts" ) + ->set_key( "{$key}_contacts" ) ->set_name( 'contacts' ) + ->set_filters( [ 'search' ] ) ->set_post_types( [ Contact::SLUG ] ) ->set_return_format( 'id' ) ->set_instructions( $strings['contacts']['instructions'] ); $fields_field = ( new Field\Checkbox( $strings['fields']['label'] ) ) - ->set_key( "${key}_fields" ) + ->set_key( "{$key}_fields" ) ->set_name( 'fields' ) ->set_choices( $strings['fields']['choices'] ) ->set_default_value( $strings['fields']['default_value'] ) diff --git a/lib/ACF/Fields/PlaceOfBusinessFields.php b/lib/ACF/Fields/PlaceOfBusinessFields.php index dbc39c4a..f792efe0 100644 --- a/lib/ACF/Fields/PlaceOfBusinessFields.php +++ b/lib/ACF/Fields/PlaceOfBusinessFields.php @@ -62,18 +62,22 @@ protected function sub_fields() : array { 'label' => 'Tampere-sivuston toimipaikat', 'instructions' => '', ], + 'place_of_business_post' => [ + 'label' => 'Toimipaikat', + 'instructions' => '', + ], ]; $key = $this->get_key(); $title_field = ( new Field\Text( $strings['title']['label'] ) ) - ->set_key( "${key}_title" ) + ->set_key( "{$key}_title" ) ->set_name( 'title' ) ->set_wrapper_width( 50 ) ->set_instructions( $strings['title']['instructions'] ); $description_field = ( new Field\Textarea( $strings['description']['label'] ) ) - ->set_key( "${key}_description" ) + ->set_key( "{$key}_description" ) ->set_name( 'description' ) ->set_rows( 4 ) ->set_new_lines( 'wpautop' ) @@ -81,7 +85,7 @@ protected function sub_fields() : array { ->set_instructions( $strings['description']['instructions'] ); $place_of_business_field = ( new Field\Select( $strings['place_of_business']['label'] ) ) - ->set_key( "${key}_place_of_business" ) + ->set_key( "{$key}_place_of_business" ) ->set_name( 'place_of_business' ) ->allow_multiple() ->allow_null() @@ -89,10 +93,32 @@ protected function sub_fields() : array { ->use_ui() ->set_instructions( $strings['place_of_business']['instructions'] ); + $place_of_business_post_field = ( new Field\Relationship( $strings['place_of_business_post']['label'] ) ) + ->set_key( "{$key}_place_of_business_post" ) + ->set_name( 'place_of_business_post' ) + ->set_filters( [ 'search' ] ) + ->redipress_include_search( function ( $places ) { + if ( empty( $places ) ) { + return ''; + } + + $results = []; + + foreach ( $places as $place_id ) { + $results[] = get_field( 'title', $place_id ); + } + + return implode( ' ', $results ); + } ) + ->set_post_types( [ 'placeofbusiness-cpt' ] ) + ->set_return_format( 'id' ) + ->set_instructions( $strings['place_of_business_post']['instructions'] ); + return [ $title_field, $description_field, $place_of_business_field, + $place_of_business_post_field, ]; } diff --git a/lib/ACF/PageContactsGroup.php b/lib/ACF/PageContactsGroup.php index 0b46633e..45c99902 100644 --- a/lib/ACF/PageContactsGroup.php +++ b/lib/ACF/PageContactsGroup.php @@ -96,7 +96,6 @@ protected function get_contacts_fields( string $key ) : Field\Tab { $fields = new ContactsFields( 'Yhteystiedot', $key ); $fields->remove_field( 'title' ); $fields->remove_field( 'description' ); - $fields->remove_field( 'contacts' ); $tab->add_fields( $fields->get_fields() ); diff --git a/lib/Formatters/PlaceOfBusinessFormatter.php b/lib/Formatters/PlaceOfBusinessFormatter.php index 136d4cc3..b20ebd9e 100644 --- a/lib/Formatters/PlaceOfBusinessFormatter.php +++ b/lib/Formatters/PlaceOfBusinessFormatter.php @@ -40,15 +40,40 @@ public function hooks() : void { * @return array */ public function format( array $data ) { - if ( empty( $data['place_of_business'] ) ) { + if ( empty( $data['place_of_business'] ) && empty( $data['place_of_business_post'] ) ) { return $data; } - $data['items'] = $this->map_api_results( - $data['place_of_business'], + if ( ! empty( $data['place_of_business_post'] ) ) { + $the_query = new \WP_Query( [ + 'post_type' => 'placeofbusiness-cpt', + 'posts_per_page' => 100, + 'post__in' => array_map( 'absint', $data['place_of_business_post'] ), + 'no_found_rows' => true, + 'meta_key' => 'title', + 'orderby' => [ + 'menu_order' => 'ASC', + 'meta_value' => 'ASC', // phpcs:ignore + ], + ] ); + + $filled_places = $this->map_keys( + $the_query->posts, + ); + } + + if ( ! empty( $data['place_of_business'] ) ) { + $filled_api_places = $this->map_api_results( + $data['place_of_business'], + ); + } + + $data['items'] = array_merge( + $filled_places ?? [], + $filled_api_places ?? [], ); - $data['column_class'] = 'is-12-mobile is-6-tablet is-4-desktop'; + $data['column_class'] = 'is-12-mobile is-6-tablet'; return $data; } @@ -80,4 +105,26 @@ public function map_api_results( array $ids = [] ) : array { return in_array( $result['id'], $ids, true ); } ); } + + /** + * Map fields to posts + * + * @param array $posts Array of WP_Post instances. + * + * @return array + */ + public function map_keys( array $posts ) : array { + if( ! \is_plugin_active( 'tms-plugin-place-of-business-sync/plugin.php' ) ) { + return []; + } + + return array_map( function ( $id ) { + + foreach( \get_field_objects($id) as $field ) { + $item[ $field['name'] ] = \get_field( $field['name'], $id ); + } + + return $item; + }, $posts ); + } } diff --git a/models/page-contacts.php b/models/page-contacts.php index 179d246a..adcfe6e6 100644 --- a/models/page-contacts.php +++ b/models/page-contacts.php @@ -45,11 +45,18 @@ public function search() : array { * @return array */ protected function get_contacts() : array { + $selected_contacts = \get_field( 'contacts' ); + if ( empty( $selected_contacts ) ) { + return []; + } + $args = [ 'post_type' => Contact::SLUG, 'posts_per_page' => 200, // phpcs:ignore 'post_status' => 'publish', 'fields' => 'ids', + 'post__in' => array_map( 'absint', $selected_contacts ), + 'no_found_rows' => true, 'meta_key' => 'last_name', 'orderby' => [ 'menu_order' => 'ASC', @@ -57,7 +64,7 @@ protected function get_contacts() : array { ], ]; - $s = get_query_var( self::SEARCH_QUERY_VAR, false ); + $s = \get_query_var( self::SEARCH_QUERY_VAR, false ); if ( ! empty( $s ) ) { $args['s'] = $s; @@ -79,7 +86,7 @@ public function contacts() : array { $contacts = $formatter->map_keys( $contacts, - get_field( 'fields' ) ?? [], + \get_field( 'fields' ) ?? [], $default_image ); diff --git a/partials/shared/contact-item.dust b/partials/shared/contact-item.dust index d945baf8..12be55ba 100644 --- a/partials/shared/contact-item.dust +++ b/partials/shared/contact-item.dust @@ -9,21 +9,21 @@
{?additional_info_top} -
- {additional_info_top|s} -
+

+ {additional_info_top|kses} +

{/additional_info_top} {?title} -
- {title|s} -
+

+ {title|html} +

{/title} {@isset key1=first_name key2=last_name method="OR" } -
- {first_name|s} {last_name|s} -
+

+ {first_name|html} {last_name|html} +

{/isset} {?phone_repeater} @@ -35,7 +35,7 @@
-
{phone_text|html}
+ {phone_text|html}
@@ -66,9 +66,9 @@ {>"ui/icon" icon="building" class="icon--large is-primary" /}
-
- {office|s} -
+

+ {office|html} +

{/office} @@ -78,9 +78,9 @@ {>"ui/icon" icon="location" class="icon--large is-primary" /} -
- {domain|s} -
+

+ {domain|html} +

{/domain} @@ -90,9 +90,9 @@ {>"ui/icon" icon="location" class="icon--large is-primary" /} -
- {unit|s} -
+

+ {unit|html} +

{/unit} @@ -102,9 +102,11 @@ {>"ui/icon" icon="location" class="icon--large is-primary" /} - {visiting_address_street|s} - {visiting_address_zip_code|s} - {visiting_address_city|s} +

+ {visiting_address_street|html} + {visiting_address_zip_code|html} + {visiting_address_city|html} +

{/isset} @@ -114,15 +116,17 @@ {>"ui/icon" icon="location" class="icon--large is-primary" /} - {mail_address_street|s} - {mail_address_zip_code|s} - {mail_address_city|s} +

+ {mail_address_street|html} + {mail_address_zip_code|html} + {mail_address_city|html} +

{/isset} {?additional_info_bottom}
- {additional_info_bottom|s} + {additional_info_bottom|kses}
{/additional_info_bottom} From 97059b3f9dfb7927973c17fcb238481bb17be80f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= Date: Thu, 2 Nov 2023 15:00:23 +0200 Subject: [PATCH 3/6] TMS-950: Code review fixes --- lib/ACF/Fields/PlaceOfBusinessFields.php | 2 +- lib/Formatters/ContactFormatter.php | 15 +-------------- lib/Formatters/PlaceOfBusinessFormatter.php | 21 ++------------------- models/page-contacts.php | 16 +++++++++------- models/shared/header.php | 2 +- partials/blocks/block-contacts.dust | 4 ++-- 6 files changed, 16 insertions(+), 44 deletions(-) diff --git a/lib/ACF/Fields/PlaceOfBusinessFields.php b/lib/ACF/Fields/PlaceOfBusinessFields.php index f792efe0..fe0d7607 100644 --- a/lib/ACF/Fields/PlaceOfBusinessFields.php +++ b/lib/ACF/Fields/PlaceOfBusinessFields.php @@ -111,7 +111,7 @@ protected function sub_fields() : array { return implode( ' ', $results ); } ) ->set_post_types( [ 'placeofbusiness-cpt' ] ) - ->set_return_format( 'id' ) + ->set_return_format( 'object' ) ->set_instructions( $strings['place_of_business_post']['instructions'] ); return [ diff --git a/lib/Formatters/ContactFormatter.php b/lib/Formatters/ContactFormatter.php index 715d2209..b3ea312e 100644 --- a/lib/Formatters/ContactFormatter.php +++ b/lib/Formatters/ContactFormatter.php @@ -52,21 +52,8 @@ public function format( array $data ) { $default_image = Settings::get_setting( 'contacts_default_image' ); if ( ! empty( $data['contacts'] ) ) { - $the_query = new \WP_Query( [ - 'post_type' => Contact::SLUG, - 'posts_per_page' => 100, - 'fields' => 'ids', - 'post__in' => array_map( 'absint', $data['contacts'] ), - 'no_found_rows' => true, - 'meta_key' => 'last_name', - 'orderby' => [ - 'menu_order' => 'ASC', - 'meta_value' => 'ASC', // phpcs:ignore - ], - ] ); - $filled_contacts = $this->map_keys( - $the_query->posts, + $data['contacts'], $field_keys, $default_image ); diff --git a/lib/Formatters/PlaceOfBusinessFormatter.php b/lib/Formatters/PlaceOfBusinessFormatter.php index b20ebd9e..f03df34e 100644 --- a/lib/Formatters/PlaceOfBusinessFormatter.php +++ b/lib/Formatters/PlaceOfBusinessFormatter.php @@ -45,20 +45,8 @@ public function format( array $data ) { } if ( ! empty( $data['place_of_business_post'] ) ) { - $the_query = new \WP_Query( [ - 'post_type' => 'placeofbusiness-cpt', - 'posts_per_page' => 100, - 'post__in' => array_map( 'absint', $data['place_of_business_post'] ), - 'no_found_rows' => true, - 'meta_key' => 'title', - 'orderby' => [ - 'menu_order' => 'ASC', - 'meta_value' => 'ASC', // phpcs:ignore - ], - ] ); - $filled_places = $this->map_keys( - $the_query->posts, + $data['place_of_business_post'], ); } @@ -119,12 +107,7 @@ public function map_keys( array $posts ) : array { } return array_map( function ( $id ) { - - foreach( \get_field_objects($id) as $field ) { - $item[ $field['name'] ] = \get_field( $field['name'], $id ); - } - - return $item; + return \get_fields( $id ); }, $posts ); } } diff --git a/models/page-contacts.php b/models/page-contacts.php index adcfe6e6..4cf5989f 100644 --- a/models/page-contacts.php +++ b/models/page-contacts.php @@ -50,6 +50,13 @@ protected function get_contacts() : array { return []; } + $s = \get_query_var( self::SEARCH_QUERY_VAR, false ); + + // Return all selected contacts if no search was performed. + if ( empty( $s ) ) { + return $selected_contacts; + } + $args = [ 'post_type' => Contact::SLUG, 'posts_per_page' => 200, // phpcs:ignore @@ -62,15 +69,10 @@ protected function get_contacts() : array { 'menu_order' => 'ASC', 'meta_value' => 'ASC', // phpcs:ignore ], + 's' => $s, ]; - $s = \get_query_var( self::SEARCH_QUERY_VAR, false ); - - if ( ! empty( $s ) ) { - $args['s'] = $s; - } - - $the_query = new WP_Query( $args ); + $the_query = new \WP_Query( $args ); return $the_query->posts; } diff --git a/models/shared/header.php b/models/shared/header.php index 7caaa592..2eadf1d5 100644 --- a/models/shared/header.php +++ b/models/shared/header.php @@ -390,7 +390,7 @@ public function menu_item_classes( $classes, $item ) : array { } $current_page = \get_queried_object(); - if ( (int) $item->object_id === $current_page->ID ) { + if ( ! empty( $current_page->ID ) && (int) $item->object_id === $current_page->ID ) { $classes['is_current'] = 'is-current'; } diff --git a/partials/blocks/block-contacts.dust b/partials/blocks/block-contacts.dust index 67d43b33..45c39ce9 100644 --- a/partials/blocks/block-contacts.dust +++ b/partials/blocks/block-contacts.dust @@ -3,12 +3,12 @@
{?title}

- {title|s} + {title|html}

{/title} {?description} - {description|s} + {description|kses} {/description}
From 3008bda093c468cd2c8742b8512ad8ca984ff404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= Date: Thu, 2 Nov 2023 15:45:03 +0200 Subject: [PATCH 4/6] Update changelog --- CHANGELOG.MD | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 02c1dfce..6b4106da 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [1.53.0] - 2023-11-02 + +### Added + - TMS-950: - Empty check for phone repeater. - Place of business posts to block From 52b1a03a06c8ce5ce2fc4d76712bcb48f6dfeb4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= Date: Thu, 2 Nov 2023 16:00:22 +0200 Subject: [PATCH 5/6] Phpcs fix --- lib/Formatters/ContactFormatter.php | 2 +- lib/Formatters/PlaceOfBusinessFormatter.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Formatters/ContactFormatter.php b/lib/Formatters/ContactFormatter.php index b3ea312e..919682bd 100644 --- a/lib/Formatters/ContactFormatter.php +++ b/lib/Formatters/ContactFormatter.php @@ -86,7 +86,7 @@ public function format( array $data ) { * * @return array|array[] */ - public function map_api_contacts( array $ids = [], array $field_keys = [], $default_image = null ) { + public function map_api_contacts( array $ids = [], array $field_keys = [], $default_image = null ) { // phpcs:ignore if ( empty( $ids ) ) { return []; } diff --git a/lib/Formatters/PlaceOfBusinessFormatter.php b/lib/Formatters/PlaceOfBusinessFormatter.php index f03df34e..d66b253e 100644 --- a/lib/Formatters/PlaceOfBusinessFormatter.php +++ b/lib/Formatters/PlaceOfBusinessFormatter.php @@ -102,7 +102,7 @@ public function map_api_results( array $ids = [] ) : array { * @return array */ public function map_keys( array $posts ) : array { - if( ! \is_plugin_active( 'tms-plugin-place-of-business-sync/plugin.php' ) ) { + if ( ! \is_plugin_active( 'tms-plugin-place-of-business-sync/plugin.php' ) ) { return []; } From 1087a567b066e59f7d3724405c8ad08576816d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= Date: Tue, 7 Nov 2023 19:38:02 +0200 Subject: [PATCH 6/6] 1.53.0 --- .github/PULL_REQUEST_TEMPLATE.md | 3 +++ CHANGELOG.MD | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index f5bac30d..20af5dcc 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,3 +1,6 @@ +Severa-ID: 2108 +Task: https://hiondigital.atlassian.net/browse/TMS- + ## Description diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 6b4106da..7d708d7d 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -## [1.53.0] - 2023-11-02 +## [1.53.0] - 2023-11-07 ### Added