Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMS-950: Contact & place-of-business sync fuctionalities #480

Merged
merged 7 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Severa-ID: 2108
Task: https://hiondigital.atlassian.net/browse/TMS-

<!--- Provide a general summary of your changes in the Title above -->

## Description
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.53.0] - 2023-11-07

### Added

- TMS-950:
- Empty check for phone repeater.
- Place of business posts to block
- Contacts styles & accessibility

## [1.52.0] - 2023-10-23

### Added
Expand Down
11 changes: 6 additions & 5 deletions lib/ACF/Fields/ContactsFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,21 @@ 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' )
->set_wrapper_width( 50 )
->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()
Expand All @@ -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'] )
Expand Down
32 changes: 29 additions & 3 deletions lib/ACF/Fields/PlaceOfBusinessFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,63 @@ 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' )
->set_wrapper_width( 50 )
->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()
->use_ajax()
->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( 'object' )
->set_instructions( $strings['place_of_business_post']['instructions'] );

return [
$title_field,
$description_field,
$place_of_business_field,
$place_of_business_post_field,
];
}

Expand Down
1 change: 0 additions & 1 deletion lib/ACF/PageContactsGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

Expand Down
19 changes: 3 additions & 16 deletions lib/Formatters/ContactFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -99,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 [];
}
Expand Down Expand Up @@ -150,7 +137,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'] );
} );
Expand Down
38 changes: 34 additions & 4 deletions lib/Formatters/PlaceOfBusinessFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,28 @@ 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'] ) ) {
$filled_places = $this->map_keys(
$data['place_of_business_post'],
);
}

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;
}
Expand Down Expand Up @@ -80,4 +93,21 @@ 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 ) {
return \get_fields( $id );
}, $posts );
}
}
25 changes: 17 additions & 8 deletions models/page-contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,34 @@ public function search() : array {
* @return array
*/
protected function get_contacts() : array {
$selected_contacts = \get_field( 'contacts' );
if ( empty( $selected_contacts ) ) {
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
'post_status' => 'publish',
'fields' => 'ids',
'post__in' => array_map( 'absint', $selected_contacts ),
'no_found_rows' => true,
'meta_key' => 'last_name',
'orderby' => [
'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;
}
Expand All @@ -79,7 +88,7 @@ public function contacts() : array {

$contacts = $formatter->map_keys(
$contacts,
get_field( 'fields' ) ?? [],
\get_field( 'fields' ) ?? [],
$default_image
);

Expand Down
2 changes: 1 addition & 1 deletion models/shared/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
4 changes: 2 additions & 2 deletions partials/blocks/block-contacts.dust
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<div class="column is-12">
{?title}
<h2>
{title|s}
{title|html}
</h2>
{/title}

{?description}
{description|s}
{description|kses}
{/description}
</div>
</div>
Expand Down
Loading
Loading