Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\Extensions\Top_Posts\load_assets_implementation | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
240 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Top Posts block render implementation. |
| 4 | * |
| 5 | * Loaded lazily from top-posts.php only when the block is rendered, to keep |
| 6 | * the render body out of the eager front-end PHP/opcache footprint. |
| 7 | * |
| 8 | * @package automattic/jetpack |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\Extensions\Top_Posts; |
| 12 | |
| 13 | use Automattic\Jetpack\Modules; |
| 14 | use Automattic\Jetpack\Status\Request; |
| 15 | use Jetpack_Gutenberg; |
| 16 | use Jetpack_Top_Posts_Helper; |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit( 0 ); |
| 20 | } |
| 21 | |
| 22 | if ( ! class_exists( 'Jetpack_Top_Posts_Helper' ) ) { |
| 23 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-top-posts-helper.php'; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Top Posts block registration/dependency declaration. |
| 28 | * |
| 29 | * @param array $attributes Array containing the Top Posts block attributes. |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | function load_assets_implementation( $attributes ) { |
| 34 | // Do not render anything when the Stats module is not active. |
| 35 | if ( ! ( new Modules() )->is_active( 'stats' ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // Do not render in contexts outside the front-end (eg. emails, API). |
| 40 | if ( ! Request::is_frontend() ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 45 | |
| 46 | /* |
| 47 | * We cannot rely on obtaining posts from the block because |
| 48 | * top posts might have changed since then. As such, we must |
| 49 | * check for updated stats. |
| 50 | */ |
| 51 | $period = $attributes['period']; |
| 52 | $number = $attributes['postsToShow']; |
| 53 | $types = implode( ',', array_keys( array_filter( $attributes['postTypes'] ) ) ); |
| 54 | |
| 55 | $data = Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types ); |
| 56 | |
| 57 | if ( ! is_array( $data ) ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); |
| 62 | |
| 63 | $output = sprintf( |
| 64 | '<div class="jetpack-top-posts%s%s%s"%sdata-item-count="%s"><div class="jetpack-top-posts-wrapper">', |
| 65 | ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '', |
| 66 | ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '', |
| 67 | ' is-' . esc_attr( $attributes['layout'] ) . '-layout', |
| 68 | ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '', |
| 69 | count( $data ) |
| 70 | ); |
| 71 | |
| 72 | foreach ( $data as $item ) { |
| 73 | $output .= '<div class="jetpack-top-posts-item">'; |
| 74 | |
| 75 | if ( $attributes['displayThumbnail'] ) { |
| 76 | $output .= '<a class="jetpack-top-posts-thumbnail-link" href="' . esc_url( $item['href'] ) . '">'; |
| 77 | |
| 78 | if ( ! empty( $item['thumbnail'] ) ) { |
| 79 | $output .= '<img class="jetpack-top-posts-thumbnail" src="' . esc_url( $item['thumbnail'] ) . '" alt="' . esc_attr( $item['title'] ) . '">'; |
| 80 | } else { |
| 81 | $output .= '<div class="jetpack-top-posts-mock-thumbnail"></div>'; |
| 82 | } |
| 83 | |
| 84 | $output .= '</a>'; |
| 85 | } |
| 86 | |
| 87 | $output .= '<span class="jetpack-top-posts-title"><a href="' . esc_url( $item['href'] ) . '">' . esc_html( $item['title'] ) . '</a></span>'; |
| 88 | |
| 89 | if ( $attributes['displayDate'] ) { |
| 90 | $output .= '<span class="jetpack-top-posts-date has-small-font-size">' . esc_html( $item['date'] ) . '</span>'; |
| 91 | } |
| 92 | |
| 93 | if ( $attributes['displayAuthor'] ) { |
| 94 | $output .= '<span class="jetpack-top-posts-author has-small-font-size">' . esc_html( $item['author'] ) . '</span>'; |
| 95 | } |
| 96 | |
| 97 | if ( $attributes['displayContext'] && ! empty( $item['context'] ) && is_array( $item['context'] ) ) { |
| 98 | $context = reset( $item['context'] ); |
| 99 | $output .= '<span class="jetpack-top-posts-context has-small-font-size"><a href="' . esc_url( get_category_link( $context->term_id ) ) . '">' . esc_html( $context->name ) . '</a></span>'; |
| 100 | } |
| 101 | |
| 102 | $output .= '</div>'; |
| 103 | } |
| 104 | |
| 105 | $output .= '</div></div>'; |
| 106 | |
| 107 | return $output; |
| 108 | } |