top-posts.php
131 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Top Posts Block. |
| 4 | * |
| 5 | * @since 13.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Top_Posts; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 14 | use Automattic\Jetpack\Modules; |
| 15 | use Automattic\Jetpack\Status; |
| 16 | use Automattic\Jetpack\Status\Host; |
| 17 | use Jetpack_Gutenberg; |
| 18 | use Jetpack_Top_Posts_Helper; |
| 19 | |
| 20 | if ( ! class_exists( 'Jetpack_Top_Posts_Helper' ) ) { |
| 21 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-top-posts-helper.php'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Registers the block for use in Gutenberg |
| 26 | * This is done via an action so that we can disable |
| 27 | * registration if we need to. |
| 28 | */ |
| 29 | function register_block() { |
| 30 | /* |
| 31 | * The block is available even when the module is not active, |
| 32 | * so we can display a nudge to activate the module instead of the block. |
| 33 | * However, since non-admins cannot activate modules, we do not display the empty block for them. |
| 34 | */ |
| 35 | if ( ! ( new Modules() )->is_active( 'stats' ) && ! current_user_can( 'jetpack_activate_modules' ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if ( ( new Host() )->is_wpcom_simple() || ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) { |
| 40 | Blocks::jetpack_register_block( |
| 41 | __DIR__, |
| 42 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 43 | ); |
| 44 | } |
| 45 | } |
| 46 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 47 | |
| 48 | /** |
| 49 | * Top Posts block registration/dependency declaration. |
| 50 | * |
| 51 | * @param array $attributes Array containing the Top Posts block attributes. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | function load_assets( $attributes ) { |
| 56 | // Do not render anything when the Stats module is not active. |
| 57 | if ( ! ( new Modules() )->is_active( 'stats' ) ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | // Do not render in contexts outside the front-end (eg. emails, API). |
| 62 | if ( ! jetpack_is_frontend() ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 67 | |
| 68 | /* |
| 69 | * We cannot rely on obtaining posts from the block because |
| 70 | * top posts might have changed since then. As such, we must |
| 71 | * check for updated stats. |
| 72 | */ |
| 73 | $period = $attributes['period']; |
| 74 | $number = $attributes['postsToShow']; |
| 75 | $types = implode( ',', array_keys( array_filter( $attributes['postTypes'] ) ) ); |
| 76 | |
| 77 | $data = Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types ); |
| 78 | |
| 79 | if ( ! is_array( $data ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); |
| 84 | |
| 85 | $output = sprintf( |
| 86 | '<div class="jetpack-top-posts%s%s%s"%sdata-item-count="%s"><div class="jetpack-top-posts-wrapper">', |
| 87 | ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '', |
| 88 | ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '', |
| 89 | ' is-' . esc_attr( $attributes['layout'] ) . '-layout', |
| 90 | ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '', |
| 91 | count( $data ) |
| 92 | ); |
| 93 | |
| 94 | foreach ( $data as $item ) { |
| 95 | $output .= '<div class="jetpack-top-posts-item">'; |
| 96 | |
| 97 | if ( $attributes['displayThumbnail'] ) { |
| 98 | $output .= '<a class="jetpack-top-posts-thumbnail-link" href="' . esc_url( $item['href'] ) . '">'; |
| 99 | |
| 100 | if ( ! empty( $item['thumbnail'] ) ) { |
| 101 | $output .= '<img class="jetpack-top-posts-thumbnail" src="' . esc_url( $item['thumbnail'] ) . '" alt="' . esc_attr( $item['title'] ) . '">'; |
| 102 | } else { |
| 103 | $output .= '<div class="jetpack-top-posts-mock-thumbnail"></div>'; |
| 104 | } |
| 105 | |
| 106 | $output .= '</a>'; |
| 107 | } |
| 108 | |
| 109 | $output .= '<span class="jetpack-top-posts-title"><a href="' . esc_url( $item['href'] ) . '">' . esc_html( $item['title'] ) . '</a></span>'; |
| 110 | |
| 111 | if ( $attributes['displayDate'] ) { |
| 112 | $output .= '<span class="jetpack-top-posts-date has-small-font-size">' . esc_html( $item['date'] ) . '</span>'; |
| 113 | } |
| 114 | |
| 115 | if ( $attributes['displayAuthor'] ) { |
| 116 | $output .= '<span class="jetpack-top-posts-author has-small-font-size">' . esc_html( $item['author'] ) . '</span>'; |
| 117 | } |
| 118 | |
| 119 | if ( $attributes['displayContext'] && ! empty( $item['context'] ) && is_array( $item['context'] ) ) { |
| 120 | $context = reset( $item['context'] ); |
| 121 | $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>'; |
| 122 | } |
| 123 | |
| 124 | $output .= '</div>'; |
| 125 | } |
| 126 | |
| 127 | $output .= '</div></div>'; |
| 128 | |
| 129 | return $output; |
| 130 | } |
| 131 |