Skip to content

Commit

Permalink
Abstract section to handle the load more request
Browse files Browse the repository at this point in the history
  • Loading branch information
trymebytes committed Dec 2, 2024
1 parent 65124fd commit fe113bf
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions includes/routes/event/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<!-- wp:wporg-translate-events-2024/event-list ' . wp_json_encode(
array(
'event_ids' => $event_ids,
'next_page' => $next_page,
'filter_by' => $filter_key,
)
) . ' /-->';
$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 );
}
}

Expand All @@ -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 = '<!-- wp:wporg-translate-events-2024/event-list ' . wp_json_encode(
array(
'event_ids' => $event_ids,
'next_page' => $next_page,
'filter_by' => $filter_key,
)
) . ' /-->';

$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,
)
);
}
}

0 comments on commit fe113bf

Please sign in to comment.