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] 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}