Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 191
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2/**
3 * Article template.
4 *
5 * @global array $attributes Block attributes.
6 * @package WordPress
7 */
8
9call_user_func(
10    function( $data ) {
11        $attributes = apply_filters( 'newspack_blocks_homepage_posts_block_attributes', $data['attributes'] );
12        $authors    = Newspack_Blocks::prepare_authors();
13        $classes    = array();
14        $styles     = '';
15        $post_id    = get_the_ID();
16
17        // Get sponsors for this post.
18        $sponsors = Newspack_Blocks::get_all_sponsors( $post_id );
19
20        // Get tag labels for this post, but only when the block is set to show them
21        // (avoids a per-post taxonomy lookup when tag labels are disabled).
22        $tag_labels = ( $attributes['showTagLabels'] ?? true ) ? Newspack_Blocks::get_tag_labels( $post_id ) : null;
23
24        // Add classes based on the post's assigned categories and tags.
25        $classes[] = Newspack_Blocks::get_term_classes( $post_id );
26
27        // Add class if post has a featured image.
28        if ( has_post_thumbnail() ) {
29            $classes[] = 'post-has-image';
30        }
31
32        // If the post is a sponsor or supporter, it won't have a working permalink, but it might have an external URL.
33        $post_link = Newspack_Blocks::get_post_link( $post_id );
34
35        if ( 'behind' === $attributes['mediaPosition'] && $attributes['showImage'] && has_post_thumbnail() ) {
36            $styles = 'min-height: ' . absint( $attributes['minHeight'] ) . 'vh; padding-top: ' . ( absint( $attributes['minHeight'] ) / 5 ) . 'vh;';
37        }
38        $image_size = 'newspack-article-block-uncropped';
39        if ( has_post_thumbnail() && 'uncropped' !== $attributes['imageShape'] ) {
40            $image_size = Newspack_Blocks::image_size_for_orientation( $attributes['imageShape'] );
41        }
42        $thumbnail_args = array(
43            'data-hero-candidate' => true,
44            'alt'                 => trim( wp_strip_all_tags( get_the_title( $post_id ) ) ),
45        );
46
47        // This global will be used by the newspack_blocks_filter_hpb_srcset filter.
48        global $newspack_blocks_hpb_rendering_context;
49        $newspack_blocks_hpb_rendering_context = [ 'attrs' => $attributes ];
50
51        // Disable lazy loading by using an arbitraty `loading` attribute other than `lazy`.
52        // Empty string or `false` would still result in `lazy`.
53        if ( $attributes['disableImageLazyLoad'] ) {
54            $thumbnail_args['loading'] = 'none';
55        }
56        if ( $attributes['fetchPriority'] && in_array( $attributes['fetchPriority'], [ 'high', 'low', 'auto' ], true ) ) {
57            $thumbnail_args['fetchpriority'] = $attributes['fetchPriority'];
58        }
59
60        // Support Newspack Listings hide author/publish date options.
61        $hide_author       = apply_filters( 'newspack_listings_hide_author', false ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
62        $hide_publish_date = apply_filters( 'newspack_listings_hide_publish_date', false ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
63        $show_author       = $attributes['showAuthor'] && ! $hide_author;
64        $show_date         = $attributes['showDate'] && ! $hide_publish_date;
65        ?>
66
67    <article data-post-id="<?php the_id(); ?>"
68        class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
69        <?php if ( $styles ) : ?>
70        style="<?php echo esc_attr( $styles ); ?>"
71        <?php endif; ?>
72        >
73        <?php echo Newspack_Blocks::get_post_status_label(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
74        <?php if ( has_post_thumbnail() && $attributes['showImage'] && $attributes['imageShape'] ) : ?>
75            <figure class="post-thumbnail">
76                <?php if ( $post_link ) : ?>
77                <a href="<?php echo esc_url( $post_link ); ?>" rel="bookmark" tabindex="-1" aria-hidden="true">
78                <?php endif; ?>
79                <?php add_filter( 'wp_calculate_image_sizes', 'newspack_blocks_filter_hpb_sizes' ); ?>
80                <?php the_post_thumbnail( $image_size, $thumbnail_args ); ?>
81                <?php remove_filter( 'wp_calculate_image_sizes', 'newspack_blocks_filter_hpb_sizes' ); ?>
82                <?php if ( $post_link ) : ?>
83                </a>
84                <?php endif; ?>
85
86                <?php if ( $attributes['showCaption'] || $attributes['showCredit'] ) : ?>
87                    <?php echo wp_kses_post( Newspack_Blocks::get_image_caption( get_post_thumbnail_id(), $attributes['showCaption'], $attributes['showCredit'] ) ); ?>
88                <?php endif; ?>
89            </figure><!-- .featured-image -->
90        <?php endif; ?>
91
92        <div class="entry-wrapper">
93            <?php
94            /**
95             * Fires at the beginning of the entry wrapper for each article in the homepage articles block.
96             *
97             * @param int   $post_id    The current post ID.
98             * @param array $attributes The block attributes array.
99             */
100            do_action( 'newspack_blocks_content_loop_entry_wrapper_start', $post_id, $attributes );
101            ?>
102            <?php if ( ! empty( $sponsors ) || ( $attributes['showCategory'] ) ) : ?>
103
104                <div class="cat-links <?php if ( ! empty( $sponsors ) ) : ?>sponsor-label<?php endif; // phpcs:ignore Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace ?>">
105                    <?php if ( ! empty( $sponsors ) ) : ?>
106                        <span class="flag">
107                            <?php echo esc_html( Newspack_Blocks::get_sponsor_label( $sponsors ) ); ?>
108                        </span>
109                        <?php
110                    endif;
111
112                    if ( $attributes['showCategory'] && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) :
113                        echo wp_kses_post( newspack_blocks_format_categories( $post_id ) );
114                    endif;
115                    ?>
116                </div>
117                <?php
118            endif;
119
120            if ( ! empty( $tag_labels ) ) {
121                Newspack_Blocks::display_tag_labels( $tag_labels );
122            }
123
124            if ( '' === $attributes['sectionHeader'] ) {
125                // Don't link the title if the post lacks a valid URL.
126                if ( ! $post_link ) {
127                    the_title( '<h2 class="entry-title">', '</h2>' );
128                } else {
129                    the_title( '<h2 class="entry-title"><a href="' . esc_url( $post_link ) . '" rel="bookmark">', '</a></h2>' );
130                }
131            } elseif ( ! $post_link ) {
132                // Don't link the title if the post lacks a valid URL.
133                the_title( '<h3 class="entry-title">', '</h3>' );
134            } else {
135                the_title( '<h3 class="entry-title"><a href="' . esc_url( $post_link ) . '" rel="bookmark">', '</a></h3>' );
136            }
137            ?>
138            <?php
139            if ( $attributes['showSubtitle'] ) :
140                $subtitle = get_post_meta( $post_id, 'newspack_post_subtitle', true );
141
142                ?>
143                <div class="newspack-post-subtitle newspack-post-subtitle--in-homepage-block">
144                    <?php
145                    $allowed_tags = array(
146                        'b'      => true,
147                        'strong' => true,
148                        'i'      => true,
149                        'em'     => true,
150                        'mark'   => true,
151                        'u'      => true,
152                        'small'  => true,
153                        'sub'    => true,
154                        'sup'    => true,
155                        'a'      => array(
156                            'href'   => true,
157                            'target' => true,
158                            'rel'    => true,
159                        ),
160                    );
161
162                    echo wptexturize( wp_kses( $subtitle, $allowed_tags ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
163                    ?>
164                </div>
165            <?php endif; ?>
166            <?php
167            /**
168             * Fires before the main content (excerpt or full content) for each article in the homepage articles block.
169             *
170             * @param int   $post_id    The current post ID.
171             * @param array $attributes The block attributes array.
172             */
173            do_action( 'newspack_blocks_content_loop_content_start', $post_id, $attributes );
174            if ( $attributes['showExcerpt'] && ! $attributes['showFullContent'] ) :
175                the_excerpt();
176            endif;
177            if ( $attributes['showFullContent'] && ! $attributes['showExcerpt'] ) :
178                the_content();
179            endif;
180            /**
181             * Fires after the main content (excerpt or full content) for each article in the homepage articles block.
182             *
183             * @param int   $post_id    The current post ID.
184             * @param array $attributes The block attributes array.
185             */
186            do_action( 'newspack_blocks_content_loop_content_end', $post_id, $attributes );
187
188            if ( $post_link && ! $attributes['showFullContent'] && $attributes['showReadMore'] ) :
189                ?>
190                <a class="more-link" href="<?php echo esc_url( $post_link ); ?>" rel="bookmark">
191                    <?php echo esc_html( $attributes['readMoreLabel'] ); ?>
192                </a>
193                <?php
194            endif;
195            if ( $show_author || $show_date || ! empty( $sponsors ) ) :
196                ?>
197                <div class="entry-meta">
198                    <?php
199                    if ( ! empty( $sponsors ) ) :
200                        $sponsor_classes = [ 'entry-sponsors' ];
201                        if ( Newspack_Blocks::newspack_display_sponsors_and_authors( $sponsors ) ) {
202                            $sponsor_classes[] = 'plus-author';
203                        }
204                        ?>
205                        <span class="<?php echo esc_attr( implode( ' ', $sponsor_classes ) ); ?>">
206                                <?php
207                                $logos = Newspack_Blocks::get_sponsor_logos( $sponsors );
208                                if ( ! empty( $logos ) ) :
209                                    ?>
210                                <span class="sponsor-logos">
211                                    <?php
212                                    foreach ( $logos as $logo ) {
213                                        if ( '' !== $logo['url'] ) {
214                                            echo '<a href="' . esc_url( $logo['url'] ) . '" target="_blank">';
215                                        }
216                                        echo '<img src="' . esc_url( $logo['src'] ) . '" alt="' . esc_attr( $logo['alt'] ) . '" width="' . esc_attr( $logo['width'] ) . '" height="' . esc_attr( $logo['height'] ) . '">';
217                                        if ( '' !== $logo['url'] ) {
218                                            echo '</a>';
219                                        }
220                                    }
221                                    ?>
222                                </span>
223                            <?php endif; ?>
224                            <span class="byline sponsor-byline">
225                                <?php
226                                $bylines = Newspack_Blocks::get_sponsor_byline( $sponsors );
227                                echo esc_html( $bylines[0]['byline'] ) . ' ';
228                                foreach ( $bylines as $byline ) {
229                                    echo '<span class="author">';
230                                    if ( '' !== $byline['url'] ) {
231                                        echo '<a target="_blank" href="' . esc_url( $byline['url'] ) . '">';
232                                    }
233                                    echo esc_html( $byline['name'] );
234                                    if ( '' !== $byline['url'] ) {
235                                        echo '</a>';
236                                    }
237                                    echo '</span>' . esc_html( $byline['sep'] );
238                                }
239                                ?>
240                            </span><!-- /.sponsor-byline -->
241                        </span><!-- .entry-sponsors -->
242                        <?php
243                    endif;
244
245                    if ( $show_author && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_authors( $sponsors ) ) ) :
246                        if ( $attributes['showAvatar'] ) :
247                            echo wp_kses(
248                                newspack_blocks_format_avatars( $authors ),
249                                Newspack_Blocks::get_sanitized_image_attributes()
250                            );
251                        endif;
252                        ?>
253                        <span class="byline">
254                            <?php echo wp_kses_post( newspack_blocks_format_byline( $authors ) ); ?>
255                        </span><!-- .author-name -->
256                        <?php
257                    endif;
258                    if ( $show_date ) :
259                        $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
260                        if ( get_the_time( 'U' ) < get_the_modified_time( 'U' ) ) :
261                            $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
262                        endif;
263                        printf(
264                            wp_kses(
265                                $time_string,
266                                array(
267                                    'time' => array(
268                                        'class'    => true,
269                                        'datetime' => true,
270                                    ),
271                                )
272                            ),
273                            esc_attr( Newspack_Blocks::get_datetime_post_date() ),
274                            esc_html( Newspack_Blocks::get_formatted_displayed_post_date() ),
275                            esc_attr( get_the_modified_date( DATE_W3C ) ),
276                            esc_html( get_the_modified_date() )
277                        );
278                    endif;
279                    echo Newspack_Blocks::get_article_meta_footer();  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
280                    ?>
281                </div><!-- .entry-meta -->
282            <?php endif; ?>
283            <?php
284            /**
285             * Fires at the end of the entry wrapper for each article in the homepage articles block.
286             *
287             * @param int   $post_id    The current post ID.
288             * @param array $attributes The block attributes array.
289             */
290            do_action( 'newspack_blocks_content_loop_entry_wrapper_end', $post_id, $attributes );
291            ?>
292        </div><!-- .entry-wrapper -->
293    </article>
294
295        <?php
296    },
297    $data // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
298);