Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 89 |
|
0.00% |
0 / 2 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\Extensions\Blogroll_Item\register_block | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| Automattic\Jetpack\Extensions\Blogroll_Item\load_assets | |
0.00% |
0 / 77 |
|
0.00% |
0 / 1 |
182 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Blogroll Item Block. |
| 4 | * |
| 5 | * @since 12.6 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Blogroll_Item; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Automattic\Jetpack\Status\Request; |
| 14 | use Jetpack_Gutenberg; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 0 ); |
| 18 | } |
| 19 | |
| 20 | const FEATURE_NAME = 'blogroll-item'; |
| 21 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 22 | |
| 23 | /** |
| 24 | * Registers the block for use in Gutenberg |
| 25 | * This is done via an action so that we can disable |
| 26 | * registration if we need to. |
| 27 | */ |
| 28 | function register_block() { |
| 29 | Blocks::jetpack_register_block( |
| 30 | BLOCK_NAME, |
| 31 | array( |
| 32 | 'render_callback' => __NAMESPACE__ . '\load_assets', |
| 33 | 'uses_context' => array( 'openLinksNewWindow' ), |
| 34 | ) |
| 35 | ); |
| 36 | } |
| 37 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 38 | |
| 39 | /** |
| 40 | * Blogroll Item block registration/dependency declaration. |
| 41 | * |
| 42 | * @param array $attr Array containing the Blogroll Item block attributes. |
| 43 | * @param string $content String containing the block content. |
| 44 | * @param object $block The block. |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | function load_assets( $attr, $content, $block ) { |
| 49 | /* |
| 50 | * Enqueue necessary scripts and styles. |
| 51 | */ |
| 52 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
| 53 | $kses_defaults = wp_kses_allowed_html( 'post' ); |
| 54 | $name = wp_kses( $attr['name'] ?? '', $kses_defaults ); |
| 55 | $name_attr = esc_attr( $attr['name'] ?? '' ); |
| 56 | $id = esc_attr( $attr['id'] ?? '' ); |
| 57 | $url = esc_url( $attr['url'] ?? '' ); |
| 58 | $description = wp_kses( $attr['description'] ?? '', $kses_defaults ); |
| 59 | $icon = esc_attr( $attr['icon'] ?? null ); |
| 60 | $target = esc_attr( ! empty( $block->context['openLinksNewWindow'] ) ? '_blank' : '_self' ); |
| 61 | $email = esc_attr( get_current_user_id() ? get_userdata( get_current_user_id() )->user_email : '' ); |
| 62 | $wp_nonce = esc_attr( wp_create_nonce( 'blogsub_subscribe_' . $id ) ); |
| 63 | $subscribe_text = esc_html__( 'Subscribe', 'jetpack' ); |
| 64 | $submit_text = esc_html__( 'Submit', 'jetpack' ); |
| 65 | $cancel_text = esc_html__( 'Cancel', 'jetpack' ); |
| 66 | $disabled_subscribe_button = ''; |
| 67 | $subscribe_button_class = 'is-style-fill'; |
| 68 | $is_following = ( function_exists( 'wpcom_subs_is_subscribed' ) && wpcom_subs_is_subscribed( |
| 69 | array( |
| 70 | 'user_id' => get_current_user_id(), |
| 71 | 'blog_id' => $id, |
| 72 | ) |
| 73 | ) ) || isset( $_GET['blogid'] ) && $id === $_GET['blogid']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- View logic. |
| 74 | |
| 75 | if ( $is_following ) { |
| 76 | $subscribe_text = esc_html__( 'Subscribed', 'jetpack' ); |
| 77 | $disabled_subscribe_button = 'disabled'; |
| 78 | $subscribe_button_class = 'is-style-outline'; |
| 79 | } |
| 80 | |
| 81 | $placeholder_site_icon = ''; |
| 82 | $site_icon_html = <<<HTML |
| 83 | <img class="blogroll-item-image" src="$icon" alt="$name_attr" onerror="this.parentNode.classList.add('empty-site-icon')"> |
| 84 | HTML; |
| 85 | |
| 86 | if ( empty( $icon ) ) { |
| 87 | $site_icon_html = ''; |
| 88 | $placeholder_site_icon = 'empty-site-icon'; |
| 89 | } |
| 90 | |
| 91 | if ( ! Request::is_frontend() ) { |
| 92 | return <<<HTML |
| 93 | <div style="margin-bottom: 10px;"> |
| 94 | <a href="$url">$name</a><div>$description</div> |
| 95 | </div> |
| 96 | HTML; |
| 97 | } |
| 98 | |
| 99 | $form_buttons = <<<HTML |
| 100 | <!-- wp:buttons {"style":{"spacing":{"blockGap":"10px"}}} --> |
| 101 | <div class="wp-block-buttons"> |
| 102 | <!-- wp:button {"className":"is-style-fill"} --> |
| 103 | <div class="wp-block-button jetpack-blogroll-item-submit-button is-style-fill"> |
| 104 | <button type="submit" name="blog_id" value="$id" class="wp-block-button__link wp-element-button">$submit_text</button> |
| 105 | </div> |
| 106 | <!-- /wp:button --> |
| 107 | |
| 108 | <!-- wp:button {"className":"is-style-outline"} --> |
| 109 | <div class="wp-block-button jetpack-blogroll-item-cancel-button is-style-outline"> |
| 110 | <button type="reset" class="wp-block-button__link wp-element-button">$cancel_text</button> |
| 111 | </div> |
| 112 | </div> |
| 113 | <!-- /wp:buttons --> |
| 114 | HTML; |
| 115 | |
| 116 | $subscribe_button = <<<HTML |
| 117 | <!-- wp:button {"className":"$subscribe_button_class"} --> |
| 118 | <div class="wp-block-button jetpack-blogroll-item-subscribe-button $subscribe_button_class"> |
| 119 | <button type="button" class="wp-block-button__link wp-element-button" {$disabled_subscribe_button}>$subscribe_text</button> |
| 120 | </div> |
| 121 | <!-- /wp:button --> |
| 122 | HTML; |
| 123 | |
| 124 | $subscribe_button_html = ''; |
| 125 | $fieldset = ''; |
| 126 | $has_subscription_form = defined( 'IS_WPCOM' ) && IS_WPCOM && isset( $attr['is_non_wpcom_site'] ) && ! $attr['is_non_wpcom_site']; |
| 127 | $classes = Blocks::classes( FEATURE_NAME, $attr ); |
| 128 | |
| 129 | if ( $has_subscription_form ) { |
| 130 | $classes .= ' has-subscription-form'; |
| 131 | $form_buttons_html = do_blocks( $form_buttons ); |
| 132 | $subscribe_button_html = do_blocks( $subscribe_button ); |
| 133 | |
| 134 | $fieldset = <<<HTML |
| 135 | <fieldset disabled class="jetpack-blogroll-item-submit"> |
| 136 | <input type="hidden" name="_wpnonce" value="$wp_nonce"> |
| 137 | <input type="email" name="email" placeholder="Email address" value="$email" class="jetpack-blogroll-item-email-input"> |
| 138 | $form_buttons_html |
| 139 | </fieldset> |
| 140 | HTML; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Build the block content. |
| 145 | */ |
| 146 | $content = <<<HTML |
| 147 | <div class="jetpack-blogroll-item-information"> |
| 148 | <figure class="$placeholder_site_icon"> |
| 149 | $site_icon_html |
| 150 | </figure> |
| 151 | <div class="jetpack-blogroll-item-content"> |
| 152 | <a class="jetpack-blogroll-item-title" href="$url" target="$target" rel="noopener noreferrer">$name</a> |
| 153 | <div class="jetpack-blogroll-item-description">$description</div> |
| 154 | </div> |
| 155 | $subscribe_button_html |
| 156 | </div> |
| 157 | $fieldset |
| 158 | HTML; |
| 159 | |
| 160 | return sprintf( |
| 161 | '<div class="%1$s"> |
| 162 | <hr class="wp-block-separator jetpack-blogroll-item-divider" /> |
| 163 | <div class="jetpack-blogroll-item-slider">%2$s</div> |
| 164 | </div>', |
| 165 | esc_attr( $classes ), |
| 166 | $content |
| 167 | ); |
| 168 | } |