From 492d677490337d6bbe336f469f19bba6a3413a76 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 22 Nov 2024 18:32:03 +0100 Subject: [PATCH 01/21] Add load more button block --- .../blocks/event-list/index.php | 1 + .../blocks/event-load-more-button/index.php | 22 +++++++++++++++++++ .../wporg-translate-events-2024/functions.php | 1 + 3 files changed, 24 insertions(+) create mode 100644 themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php diff --git a/themes/wporg-translate-events-2024/blocks/event-list/index.php b/themes/wporg-translate-events-2024/blocks/event-list/index.php index 8c91dca6..b28aa764 100644 --- a/themes/wporg-translate-events-2024/blocks/event-list/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-list/index.php @@ -45,6 +45,7 @@ ?> + function ( array $attributes, $content, $block ) { + ob_start(); + ?> + +
+ +
+ + Date: Fri, 22 Nov 2024 18:32:28 +0100 Subject: [PATCH 02/21] Remove unused parameters --- .../blocks/event-load-more-button/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php b/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php index b9bc38c7..0d1d3f45 100644 --- a/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php @@ -7,7 +7,7 @@ 'wporg-translate-events-2024/event-load-more-button', array( // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found - 'render_callback' => function ( array $attributes, $content, $block ) { + 'render_callback' => function () { ob_start(); ?> From 24ec2295f62472ec5658860fb7aad91f0752a917 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 25 Nov 2024 15:57:23 +0100 Subject: [PATCH 03/21] Add filter to attributes for button --- .../wporg-translate-events-2024/blocks/event-list/index.php | 3 ++- .../blocks/event-load-more-button/index.php | 5 +++-- .../blocks/pages/events/home/render.php | 3 +++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/themes/wporg-translate-events-2024/blocks/event-list/index.php b/themes/wporg-translate-events-2024/blocks/event-list/index.php index b28aa764..3db9c898 100644 --- a/themes/wporg-translate-events-2024/blocks/event-list/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-list/index.php @@ -9,6 +9,7 @@ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found 'render_callback' => function ( array $attributes ) { $event_ids = $attributes['event_ids'] ?? array(); + $event_filter = $attributes['filter_by'] ?? ''; if ( empty( $event_ids ) ) { return get_no_result_view(); @@ -45,7 +46,7 @@ ?> - + function () { + 'render_callback' => function ( $attributes ) { + $event_filter = $attributes['filter']; ob_start(); ?>
- +
$attributes['current_events_query']['event_ids'] ?? array(), + 'filter_by' => 'current', ); $upcoming_events_data = array( 'event_ids' => $attributes['upcoming_events_query']['event_ids'] ?? array(), + 'filter_by' => 'upcoming', ); $past_events_data = array( 'event_ids' => $attributes['past_events_query']['event_ids'] ?? array(), + 'filter_by' => 'past', ); ?> From 17e600524d0e95949cd356000df65c8fb38131e4 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 25 Nov 2024 15:57:55 +0100 Subject: [PATCH 04/21] Add click event to button --- assets/js/translation-events.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index 5052977d..34fb6cbd 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -99,6 +99,15 @@ } ); + $( document ).on( + 'click', + '.load-more-events-btn', + function ( e ) { + //load more events + + } + ); + } ); From 666b9cdb0bdbbf264d43c54670056d0197c9505a Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 25 Nov 2024 16:12:43 +0100 Subject: [PATCH 05/21] Fix lint error --- .../blocks/event-load-more-button/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php b/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php index 2b65a61d..7c14acec 100644 --- a/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-load-more-button/index.php @@ -13,7 +13,7 @@ ?>
- +
Date: Wed, 27 Nov 2024 01:41:57 +0100 Subject: [PATCH 06/21] Return list block rendered in html when called via ajax --- includes/routes/event/list.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index 42f14ac4..a03dabec 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -54,8 +54,24 @@ public function handle(): void { $upcoming_events_query = $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 10 ); $past_events_query = $this->event_repository->get_past_events( $_past_events_paged, 10 ); $user_attending_events_query = $this->event_repository->get_current_and_upcoming_events_for_user( get_current_user_id(), $_user_attending_events_paged, 10 ); - $this->use_theme(); + + if ( isset( $_GET['format'] ) ) { + $value = sanitize_text_field( wp_unslash( $_GET['format'] ) ); + if ( 'html' == $value ) { + $event_ids = $past_events_query->event_ids; + $list_block_markup = ''; + + $rendered_html = ''; + $parsed_blocks = parse_blocks( do_blocks( $list_block_markup ) ); + foreach ( $parsed_blocks as $block ) { + $rendered_html .= render_block( $block ); + } + echo $rendered_html; + return; + } + } + $this->tmpl( 'home', array( From b6039d9c10162a732e214f2758a7eadec41e1ee2 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 27 Nov 2024 17:14:43 +0100 Subject: [PATCH 07/21] Filter out the event_ids that needs to be returned --- includes/routes/event/list.php | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index a03dabec..07ef7f8e 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -22,45 +22,59 @@ public function handle(): void { $_upcoming_events_paged = 1; $_past_events_paged = 1; $_user_attending_events_paged = 1; + $filter_key = ''; // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['current_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['current_events_paged'] ) ); if ( is_numeric( $value ) ) { $_current_events_paged = (int) $value; + $filter_key = 'current_events_query'; } } if ( isset( $_GET['upcoming_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['upcoming_events_paged'] ) ); if ( is_numeric( $value ) ) { $_upcoming_events_paged = (int) $value; + $filter_key = 'upcoming_events_query'; } } if ( isset( $_GET['past_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['past_events_paged'] ) ); if ( is_numeric( $value ) ) { $_past_events_paged = (int) $value; + $filter_key = 'past_events_query'; } } if ( isset( $_GET['user_attending_events_paged'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['user_attending_events_paged'] ) ); if ( is_numeric( $value ) ) { $_user_attending_events_paged = (int) $value; + $filter_key = 'user_attending_events_query'; } } // phpcs:enable + $tmpl_args = array( + 'current_events_query' => $this->event_repository->get_current_events( $_current_events_paged, 10 ), + 'upcoming_events_query' => $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 10 ), + 'past_events_query' => $this->event_repository->get_past_events( $_past_events_paged, 10 ), + 'user_attending_events_query' => $this->event_repository->get_current_and_upcoming_events_for_user( get_current_user_id(), $_user_attending_events_paged, 10 ), + ); - $current_events_query = $this->event_repository->get_current_events( $_current_events_paged, 10 ); - $upcoming_events_query = $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 10 ); - $past_events_query = $this->event_repository->get_past_events( $_past_events_paged, 10 ); - $user_attending_events_query = $this->event_repository->get_current_and_upcoming_events_for_user( get_current_user_id(), $_user_attending_events_paged, 10 ); $this->use_theme(); if ( isset( $_GET['format'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['format'] ) ); - if ( 'html' == $value ) { - $event_ids = $past_events_query->event_ids; - $list_block_markup = ''; + if ( 'html' == $value && ! empty( $filter_key ) ) { + $event_ids = $tmpl_args[ $filter_key ]->event_ids; + $page_left = $tmpl_args[ $filter_key ]->page_count - $tmpl_args[ $filter_key ]->current_page; + + $list_block_markup = ''; $rendered_html = ''; $parsed_blocks = parse_blocks( do_blocks( $list_block_markup ) ); @@ -74,12 +88,7 @@ public function handle(): void { $this->tmpl( 'home', - array( - 'current_events_query' => $current_events_query, - 'upcoming_events_query' => $upcoming_events_query, - 'past_events_query' => $past_events_query, - 'user_attending_events_query' => $user_attending_events_query, - ), + $tmpl_args, ); } } From f838a31febc9eee15e5571d7ecb9b0c14a3e80d1 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 27 Nov 2024 17:15:17 +0100 Subject: [PATCH 08/21] Add page_left attribute to detect if there are more pages to load --- .../wporg-translate-events-2024/blocks/event-list/index.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/themes/wporg-translate-events-2024/blocks/event-list/index.php b/themes/wporg-translate-events-2024/blocks/event-list/index.php index 3db9c898..95da7466 100644 --- a/themes/wporg-translate-events-2024/blocks/event-list/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-list/index.php @@ -10,11 +10,11 @@ 'render_callback' => function ( array $attributes ) { $event_ids = $attributes['event_ids'] ?? array(); $event_filter = $attributes['filter_by'] ?? ''; - if ( empty( $event_ids ) ) { return get_no_result_view(); } $show_flag = ! empty( $attributes['show_flag'] ) && true === $attributes['show_flag']; + $show_load_more_btn = ! empty( $attributes['page_left'] ) && $attributes['page_left'] > 0; ob_start(); ?> @@ -46,8 +46,10 @@ ?> + Date: Thu, 28 Nov 2024 17:19:18 +0100 Subject: [PATCH 09/21] Pass next page number to load more button block --- .../blocks/event-list/index.php | 7 ++----- .../blocks/event-load-more-button/index.php | 9 +++++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/themes/wporg-translate-events-2024/blocks/event-list/index.php b/themes/wporg-translate-events-2024/blocks/event-list/index.php index 95da7466..225eeb21 100644 --- a/themes/wporg-translate-events-2024/blocks/event-list/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-list/index.php @@ -14,8 +14,7 @@ return get_no_result_view(); } $show_flag = ! empty( $attributes['show_flag'] ) && true === $attributes['show_flag']; - $show_load_more_btn = ! empty( $attributes['page_left'] ) && $attributes['page_left'] > 0; - + $next_page = ! empty( $attributes['next_page'] ) ? $attributes['next_page'] : 0; ob_start(); ?>
@@ -46,10 +45,8 @@ ?>
- - + function ( $attributes ) { - $event_filter = $attributes['filter']; + $event_filter = $attributes['filter'] ?? ''; + $next_page = $attributes['next_page'] ?? 1; + + if ( ! $event_filter || ! $next_page ) { + return; + } ob_start(); ?>
- +
Date: Thu, 28 Nov 2024 17:33:49 +0100 Subject: [PATCH 10/21] Pass next_page attribute to list block --- includes/routes/event/list.php | 8 +++++--- .../blocks/pages/events/home/render.php | 14 +++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index 07ef7f8e..00cc035a 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -66,13 +66,15 @@ public function handle(): void { if ( isset( $_GET['format'] ) ) { $value = sanitize_text_field( wp_unslash( $_GET['format'] ) ); if ( 'html' == $value && ! empty( $filter_key ) ) { - $event_ids = $tmpl_args[ $filter_key ]->event_ids; - $page_left = $tmpl_args[ $filter_key ]->page_count - $tmpl_args[ $filter_key ]->current_page; + $event_ids = $tmpl_args[ $filter_key ]->event_ids; + $current_page = $tmpl_args[ $filter_key ]->current_page; + $page_count = $tmpl_args[ $filter_key ]->page_count; + $next_page = ( ( $current_page + 1 ) <= $page_count ) ? $current_page + 1 : 0; $list_block_markup = ''; diff --git a/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php b/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php index 8b38eb52..2c493a00 100644 --- a/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php +++ b/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php @@ -1,21 +1,29 @@ $attributes['current_events_query']['event_ids'] ?? array(), + 'event_ids' => $current_events_query['event_ids'] ?? array(), 'filter_by' => 'current', + 'next_page' => ( $current_events_query['page_count'] >= $current_events_query['current_page'] + 1 ) ? $current_events_query['current_page'] + 1 : 0, ); $upcoming_events_data = array( - 'event_ids' => $attributes['upcoming_events_query']['event_ids'] ?? array(), + 'event_ids' => $upcoming_events_query['event_ids'] ?? array(), 'filter_by' => 'upcoming', + 'next_page' => ( $upcoming_events_query['page_count'] >= $upcoming_events_query['current_page'] + 1 ) ? $upcoming_events_query['current_page'] + 1 : 0, + ); $past_events_data = array( 'event_ids' => $attributes['past_events_query']['event_ids'] ?? array(), 'filter_by' => 'past', + 'next_page' => ( $past_events_query['page_count'] >= $past_events_query['current_page'] + 1 ) ? $past_events_query['current_page'] + 1 : 0, + ); ?> From 9b2e8b85b4163b3d3bcb403e8a05acf91c2d80aa Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Thu, 28 Nov 2024 17:46:58 +0100 Subject: [PATCH 11/21] Use same _GET variable as filter --- .../blocks/pages/events/home/render.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php b/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php index 2c493a00..729536da 100644 --- a/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php +++ b/themes/wporg-translate-events-2024/blocks/pages/events/home/render.php @@ -8,20 +8,20 @@ $current_events_data = array( 'event_ids' => $current_events_query['event_ids'] ?? array(), - 'filter_by' => 'current', + 'filter_by' => 'current_events_paged', 'next_page' => ( $current_events_query['page_count'] >= $current_events_query['current_page'] + 1 ) ? $current_events_query['current_page'] + 1 : 0, ); $upcoming_events_data = array( 'event_ids' => $upcoming_events_query['event_ids'] ?? array(), - 'filter_by' => 'upcoming', + 'filter_by' => 'upcoming_events_paged', 'next_page' => ( $upcoming_events_query['page_count'] >= $upcoming_events_query['current_page'] + 1 ) ? $upcoming_events_query['current_page'] + 1 : 0, ); $past_events_data = array( 'event_ids' => $attributes['past_events_query']['event_ids'] ?? array(), - 'filter_by' => 'past', + 'filter_by' => 'past_events_paged', 'next_page' => ( $past_events_query['page_count'] >= $past_events_query['current_page'] + 1 ) ? $past_events_query['current_page'] + 1 : 0, ); From c3dbb18f0b9b6308ef8fcca31a126ffabb225a07 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Thu, 28 Nov 2024 19:22:30 +0100 Subject: [PATCH 12/21] Update js to append next page to existing list --- assets/js/translation-events.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index 34fb6cbd..0708b836 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -103,7 +103,26 @@ 'click', '.load-more-events-btn', function ( e ) { - //load more events + let eventType = $( this ).data('event-type'); + let nextPage = $( this ).data('event-next-page'); + const url = `http://translate.test/events/?${encodeURIComponent(eventType)}=${encodeURIComponent(nextPage)}&format=html`; + + fetch(url) + .then(response => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.text(); + }) + .then(response => { + $(this).hide(); + const $html = $(response); + const $listItems = $html.find('li'); + $(this).parent().prev('.wp-block-wporg-event-list').find('ul').append($listItems); + }) + .catch(error => { + console.error('Error fetching next page:', error); + }); } ); From b72103de6a15529b98d4e7579193d0fce2acb513 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 29 Nov 2024 13:40:52 +0100 Subject: [PATCH 13/21] Pass event filter to list block --- includes/routes/event/list.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index 00cc035a..8c8ddedd 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -55,9 +55,9 @@ public function handle(): void { } // phpcs:enable $tmpl_args = array( - 'current_events_query' => $this->event_repository->get_current_events( $_current_events_paged, 10 ), - 'upcoming_events_query' => $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 10 ), - 'past_events_query' => $this->event_repository->get_past_events( $_past_events_paged, 10 ), + 'current_events_query' => $this->event_repository->get_current_events( $_current_events_paged, 2 ), + 'upcoming_events_query' => $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 2 ), + 'past_events_query' => $this->event_repository->get_past_events( $_past_events_paged, 2 ), 'user_attending_events_query' => $this->event_repository->get_current_and_upcoming_events_for_user( get_current_user_id(), $_user_attending_events_paged, 10 ), ); @@ -75,6 +75,7 @@ public function handle(): void { array( 'event_ids' => $event_ids, 'next_page' => $next_page, + 'filter_by' => $filter_key, ) ) . ' /-->'; From 4add1cc07e5e23b91329167ae88bcb74b71fdf63 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 29 Nov 2024 14:05:45 +0100 Subject: [PATCH 14/21] Check if event_ids are non empty and add next page value to response --- includes/routes/event/list.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index 8c8ddedd..81b6b5dc 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -64,6 +64,9 @@ public function handle(): void { $this->use_theme(); if ( isset( $_GET['format'] ) ) { + if ( empty( $tmpl_args[ $filter_key ]->event_ids ) ) { + return; + } $value = sanitize_text_field( wp_unslash( $_GET['format'] ) ); if ( 'html' == $value && ! empty( $filter_key ) ) { $event_ids = $tmpl_args[ $filter_key ]->event_ids; @@ -84,8 +87,13 @@ public function handle(): void { foreach ( $parsed_blocks as $block ) { $rendered_html .= render_block( $block ); } - echo $rendered_html; - return; + + wp_send_json_success( + array( + 'nextPage' => $next_page, + 'html' => $rendered_html, + ) + ); } } From 1e259a605d32eeae8202fd7482e4e2f3d03ec779 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 29 Nov 2024 14:47:16 +0100 Subject: [PATCH 15/21] Update JS to handle json response --- assets/js/translation-events.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index 0708b836..91e8cc12 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -112,18 +112,21 @@ if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } - return response.text(); + return response.json(); }) .then(response => { - $(this).hide(); - const $html = $(response); + const $html = $(response.data.html); const $listItems = $html.find('li'); + if ( 0 === response.data.nextPage ){ + $(this).hide(); + } else { + $(this).data('event-next-page', response.data.nextPage); + } $(this).parent().prev('.wp-block-wporg-event-list').find('ul').append($listItems); - }) + }) .catch(error => { console.error('Error fetching next page:', error); }); - } ); From 709ef58ef7c198ece28672b2f8d59a703545cc34 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 29 Nov 2024 15:01:28 +0100 Subject: [PATCH 16/21] Revert changes to number of events per page --- includes/routes/event/list.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index 81b6b5dc..c2ab3f49 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -55,9 +55,9 @@ public function handle(): void { } // phpcs:enable $tmpl_args = array( - 'current_events_query' => $this->event_repository->get_current_events( $_current_events_paged, 2 ), - 'upcoming_events_query' => $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 2 ), - 'past_events_query' => $this->event_repository->get_past_events( $_past_events_paged, 2 ), + 'current_events_query' => $this->event_repository->get_current_events( $_current_events_paged, 10 ), + 'upcoming_events_query' => $this->event_repository->get_upcoming_events( $_upcoming_events_paged, 10 ), + 'past_events_query' => $this->event_repository->get_past_events( $_past_events_paged, 10 ), 'user_attending_events_query' => $this->event_repository->get_current_and_upcoming_events_for_user( get_current_user_id(), $_user_attending_events_paged, 10 ), ); From 65124fd5976b03265ff26bf8e48553c04b9eab7d Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 29 Nov 2024 15:03:03 +0100 Subject: [PATCH 17/21] Fix lint errors --- includes/routes/event/list.php | 3 ++- .../blocks/event-list/index.php | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index c2ab3f49..0eb454d0 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -63,12 +63,13 @@ public function handle(): void { $this->use_theme(); + // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['format'] ) ) { if ( empty( $tmpl_args[ $filter_key ]->event_ids ) ) { return; } $value = sanitize_text_field( wp_unslash( $_GET['format'] ) ); - if ( 'html' == $value && ! empty( $filter_key ) ) { + if ( 'html' === $value && ! empty( $filter_key ) ) { $event_ids = $tmpl_args[ $filter_key ]->event_ids; $current_page = $tmpl_args[ $filter_key ]->current_page; $page_count = $tmpl_args[ $filter_key ]->page_count; diff --git a/themes/wporg-translate-events-2024/blocks/event-list/index.php b/themes/wporg-translate-events-2024/blocks/event-list/index.php index 225eeb21..aa321585 100644 --- a/themes/wporg-translate-events-2024/blocks/event-list/index.php +++ b/themes/wporg-translate-events-2024/blocks/event-list/index.php @@ -45,7 +45,16 @@ ?> - + Date: Mon, 2 Dec 2024 11:35:35 +0100 Subject: [PATCH 18/21] Abstract section to handle the load more request --- includes/routes/event/list.php | 62 +++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index 0eb454d0..b31eb203 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -62,39 +62,17 @@ public function handle(): void { ); $this->use_theme(); - // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['format'] ) ) { - if ( empty( $tmpl_args[ $filter_key ]->event_ids ) ) { - return; - } - $value = sanitize_text_field( wp_unslash( $_GET['format'] ) ); - if ( 'html' === $value && ! empty( $filter_key ) ) { - $event_ids = $tmpl_args[ $filter_key ]->event_ids; - $current_page = $tmpl_args[ $filter_key ]->current_page; - $page_count = $tmpl_args[ $filter_key ]->page_count; - $next_page = ( ( $current_page + 1 ) <= $page_count ) ? $current_page + 1 : 0; - $list_block_markup = ''; + $format = sanitize_text_field( wp_unslash( $_GET['format'] ) ); - $rendered_html = ''; - $parsed_blocks = parse_blocks( do_blocks( $list_block_markup ) ); - foreach ( $parsed_blocks as $block ) { - $rendered_html .= render_block( $block ); - } + if ( 'html' !== $format || empty( $filter_key ) ) { + return; + } - wp_send_json_success( - array( - 'nextPage' => $next_page, - 'html' => $rendered_html, - ) - ); + if ( ! empty( $tmpl_args[ $filter_key ]->event_ids ) ) { + $this->handle_ajax( $format, $filter_key, $tmpl_args ); } } @@ -103,4 +81,32 @@ public function handle(): void { $tmpl_args, ); } + + public function handle_ajax( $filter_key, $tmpl_args ) { + $event_ids = $tmpl_args[ $filter_key ]->event_ids; + $current_page = $tmpl_args[ $filter_key ]->current_page; + $page_count = $tmpl_args[ $filter_key ]->page_count; + $next_page = ( ( $current_page + 1 ) <= $page_count ) ? $current_page + 1 : 0; + + $list_block_markup = ''; + + $rendered_html = ''; + $parsed_blocks = parse_blocks( do_blocks( $list_block_markup ) ); + foreach ( $parsed_blocks as $block ) { + $rendered_html .= render_block( $block ); + } + + wp_send_json_success( + array( + 'nextPage' => $next_page, + 'html' => $rendered_html, + ) + ); + } } From 713e5f7a3d4f77dc3e740b49f933d659ffa76eac Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Thu, 12 Dec 2024 14:30:53 +0100 Subject: [PATCH 19/21] Remove unused parameter --- includes/routes/event/list.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/routes/event/list.php b/includes/routes/event/list.php index b31eb203..21c44978 100644 --- a/includes/routes/event/list.php +++ b/includes/routes/event/list.php @@ -72,7 +72,7 @@ public function handle(): void { } if ( ! empty( $tmpl_args[ $filter_key ]->event_ids ) ) { - $this->handle_ajax( $format, $filter_key, $tmpl_args ); + $this->handle_ajax( $filter_key, $tmpl_args ); } } From 3540c13cce35785278a564509c52a650ea7af3b6 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Thu, 12 Dec 2024 15:47:44 +0100 Subject: [PATCH 20/21] Use relative url --- assets/js/translation-events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index 91e8cc12..f485b78f 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -105,7 +105,7 @@ function ( e ) { let eventType = $( this ).data('event-type'); let nextPage = $( this ).data('event-next-page'); - const url = `http://translate.test/events/?${encodeURIComponent(eventType)}=${encodeURIComponent(nextPage)}&format=html`; + const url = `/events/?${encodeURIComponent(eventType)}=${encodeURIComponent(nextPage)}&format=html`; fetch(url) .then(response => { From 63c2fe7758f2ee7d7df2ea1bfe089c2e4c0b50b6 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Thu, 12 Dec 2024 15:59:17 +0100 Subject: [PATCH 21/21] Add loading text on button when clicked --- assets/js/translation-events.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/assets/js/translation-events.js b/assets/js/translation-events.js index f485b78f..d2fbc5ab 100644 --- a/assets/js/translation-events.js +++ b/assets/js/translation-events.js @@ -103,6 +103,7 @@ 'click', '.load-more-events-btn', function ( e ) { + $( this ).text('Loading...').prop('disabled', true); let eventType = $( this ).data('event-type'); let nextPage = $( this ).data('event-next-page'); const url = `/events/?${encodeURIComponent(eventType)}=${encodeURIComponent(nextPage)}&format=html`; @@ -123,6 +124,7 @@ $(this).data('event-next-page', response.data.nextPage); } $(this).parent().prev('.wp-block-wporg-event-list').find('ul').append($listItems); + $(this).text('Load more').prop('disabled', false); }) .catch(error => { console.error('Error fetching next page:', error);