Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.00% covered (warning)
85.00%
34 / 40
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2/**
3 * Load More block render.
4 *
5 * @package automattic/jetpack-search
6 */
7
8namespace Automattic\Jetpack\Search;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
15
16$load_on_scroll = ! empty( $attributes['loadOnScroll'] );
17// Clamp to a sane CSS-px range. 0 = fire exactly when the sentinel hits the
18// viewport bottom; very large values would pre-fetch the entire feed.
19$load_on_scroll_offset = max( 0, min( 2000, (int) ( $attributes['loadOnScrollOffset'] ?? 200 ) ) );
20
21// Trim before the empty check so a whitespace-only label (e.g. "   ")
22// renders the translated default rather than a blank button — matches the
23// "Leave empty to use the default" copy in the editor inspector.
24$button_label = trim( (string) ( $attributes['buttonLabel'] ?? '' ) );
25if ( '' === $button_label ) {
26    $button_label = __( 'Load more results', 'jetpack-search-pkg' );
27}
28
29// The button below carries `wp-block-button__link` so it inherits the
30// theme's full core/button look (border-radius, hover, etc.). That class is
31// only styled when the core Button block's stylesheet is on the page —
32// without a core/button on the page, the handle isn't auto-enqueued and the
33// class is inert. Pull it in explicitly so this block stands alone. Skipped
34// in the loadOnScroll path since the button doesn't render there.
35if ( ! $load_on_scroll ) {
36    wp_enqueue_style( 'wp-block-button' );
37}
38
39$wrapper_attrs = get_block_wrapper_attributes();
40?>
41<div
42    <?php echo wp_kses_data( $wrapper_attrs ); ?>
43    data-wp-interactive="jetpack-search"
44    data-wp-bind--hidden="!state.showLoadMore"
45    <?php if ( $load_on_scroll ) : ?>
46        data-wp-init--load-more-observer="callbacks.initLoadMoreObserver"
47        data-load-on-scroll="1"
48        data-load-on-scroll-offset="<?php echo esc_attr( (string) $load_on_scroll_offset ); ?>"
49    <?php endif; ?>
50    hidden
51>
52    <?php if ( ! $load_on_scroll ) : ?>
53        <button
54            type="button"
55            class="jetpack-search-load-more__button wp-block-button__link wp-element-button"
56            data-wp-on--click="actions.loadMore"
57            data-wp-bind--hidden="state.isLoadingMore"
58        >
59            <?php echo esc_html( $button_label ); ?>
60        </button>
61    <?php endif; ?>
62    <span
63        class="jetpack-search-load-more__spinner"
64        data-wp-bind--hidden="!state.isLoadingMore"
65    >
66        <?php esc_html_e( 'Loading…', 'jetpack-search-pkg' ); ?>
67    </span>
68    <?php if ( $load_on_scroll ) : ?>
69        <span class="jetpack-search-load-more__sentinel" aria-hidden="true"></span>
70    <?php endif; ?>
71</div>