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