init
This commit is contained in:
+163
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
/**
|
||||
* Blogroll Item Block.
|
||||
*
|
||||
* @since 12.6
|
||||
*
|
||||
* @package automattic/jetpack
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack\Extensions\Blogroll_Item;
|
||||
|
||||
use Automattic\Jetpack\Blocks;
|
||||
use Jetpack_Gutenberg;
|
||||
|
||||
const FEATURE_NAME = 'blogroll-item';
|
||||
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
|
||||
|
||||
/**
|
||||
* Registers the block for use in Gutenberg
|
||||
* This is done via an action so that we can disable
|
||||
* registration if we need to.
|
||||
*/
|
||||
function register_block() {
|
||||
Blocks::jetpack_register_block(
|
||||
BLOCK_NAME,
|
||||
array(
|
||||
'render_callback' => __NAMESPACE__ . '\load_assets',
|
||||
'uses_context' => array( 'openLinksNewWindow' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
add_action( 'init', __NAMESPACE__ . '\register_block' );
|
||||
|
||||
/**
|
||||
* Blogroll Item block registration/dependency declaration.
|
||||
*
|
||||
* @param array $attr Array containing the Blogroll Item block attributes.
|
||||
* @param string $content String containing the block content.
|
||||
* @param object $block The block.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function load_assets( $attr, $content, $block ) {
|
||||
/*
|
||||
* Enqueue necessary scripts and styles.
|
||||
*/
|
||||
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
|
||||
$kses_defaults = wp_kses_allowed_html( 'post' );
|
||||
$name = wp_kses( $attr['name'], $kses_defaults );
|
||||
$name_attr = esc_attr( $attr['name'] );
|
||||
$id = esc_attr( $attr['id'] );
|
||||
$url = esc_url( $attr['url'] );
|
||||
$description = wp_kses( $attr['description'], $kses_defaults );
|
||||
$icon = esc_attr( isset( $attr['icon'] ) ? $attr['icon'] : null );
|
||||
$target = esc_attr( $block->context['openLinksNewWindow'] ? '_blank' : '_self' );
|
||||
$email = esc_attr( get_current_user_id() ? get_userdata( get_current_user_id() )->user_email : '' );
|
||||
$wp_nonce = esc_attr( wp_create_nonce( 'blogsub_subscribe_' . $id ) );
|
||||
$subscribe_text = esc_html__( 'Subscribe', 'jetpack' );
|
||||
$submit_text = esc_html__( 'Submit', 'jetpack' );
|
||||
$cancel_text = esc_html__( 'Cancel', 'jetpack' );
|
||||
$disabled_subscribe_button = '';
|
||||
$subscribe_button_class = 'is-style-fill';
|
||||
$is_following = ( function_exists( 'wpcom_subs_is_subscribed' ) && wpcom_subs_is_subscribed(
|
||||
array(
|
||||
'user_id' => get_current_user_id(),
|
||||
'blog_id' => $id,
|
||||
)
|
||||
) ) || isset( $_GET['blogid'] ) && $id === $_GET['blogid']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- View logic.
|
||||
|
||||
if ( $is_following ) {
|
||||
$subscribe_text = esc_html__( 'Subscribed', 'jetpack' );
|
||||
$disabled_subscribe_button = 'disabled';
|
||||
$subscribe_button_class = 'is-style-outline';
|
||||
}
|
||||
|
||||
$placeholder_site_icon = '';
|
||||
$site_icon_html = <<<HTML
|
||||
<img class="blogroll-item-image" src="$icon" alt="$name_attr" onerror="this.parentNode.classList.add('empty-site-icon')">
|
||||
HTML;
|
||||
|
||||
if ( empty( $icon ) ) {
|
||||
$site_icon_html = '';
|
||||
$placeholder_site_icon = 'empty-site-icon';
|
||||
}
|
||||
|
||||
if ( ! jetpack_is_frontend() ) {
|
||||
return <<<HTML
|
||||
<div style="margin-bottom: 10px;">
|
||||
<a href="$url">$name</a><div>$description</div>
|
||||
</div>
|
||||
HTML;
|
||||
}
|
||||
|
||||
$form_buttons = <<<HTML
|
||||
<!-- wp:buttons {"style":{"spacing":{"blockGap":"10px"}}} -->
|
||||
<div class="wp-block-buttons">
|
||||
<!-- wp:button {"className":"is-style-fill"} -->
|
||||
<div class="wp-block-button jetpack-blogroll-item-submit-button is-style-fill">
|
||||
<button type="submit" name="blog_id" value="$id" class="wp-block-button__link wp-element-button">$submit_text</button>
|
||||
</div>
|
||||
<!-- /wp:button -->
|
||||
|
||||
<!-- wp:button {"className":"is-style-outline"} -->
|
||||
<div class="wp-block-button jetpack-blogroll-item-cancel-button is-style-outline">
|
||||
<button type="reset" class="wp-block-button__link wp-element-button">$cancel_text</button>
|
||||
</div>
|
||||
</div>
|
||||
<!-- /wp:buttons -->
|
||||
HTML;
|
||||
|
||||
$subscribe_button = <<<HTML
|
||||
<!-- wp:button {"className":"$subscribe_button_class"} -->
|
||||
<div class="wp-block-button jetpack-blogroll-item-subscribe-button $subscribe_button_class">
|
||||
<button type="button" class="wp-block-button__link wp-element-button" {$disabled_subscribe_button}>$subscribe_text</button>
|
||||
</div>
|
||||
<!-- /wp:button -->
|
||||
HTML;
|
||||
|
||||
$subscribe_button_html = '';
|
||||
$fieldset = '';
|
||||
$has_subscription_form = defined( 'IS_WPCOM' ) && IS_WPCOM && isset( $attr['is_non_wpcom_site'] ) && ! $attr['is_non_wpcom_site'];
|
||||
$classes = Blocks::classes( FEATURE_NAME, $attr );
|
||||
|
||||
if ( $has_subscription_form ) {
|
||||
$classes .= ' has-subscription-form';
|
||||
$form_buttons_html = do_blocks( $form_buttons );
|
||||
$subscribe_button_html = do_blocks( $subscribe_button );
|
||||
|
||||
$fieldset = <<<HTML
|
||||
<fieldset disabled class="jetpack-blogroll-item-submit">
|
||||
<input type="hidden" name="_wpnonce" value="$wp_nonce">
|
||||
<input type="email" name="email" placeholder="Email address" value="$email" class="jetpack-blogroll-item-email-input">
|
||||
$form_buttons_html
|
||||
</fieldset>
|
||||
HTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the block content.
|
||||
*/
|
||||
$content = <<<HTML
|
||||
<div class="jetpack-blogroll-item-information">
|
||||
<figure class="$placeholder_site_icon">
|
||||
$site_icon_html
|
||||
</figure>
|
||||
<div class="jetpack-blogroll-item-content">
|
||||
<a class="jetpack-blogroll-item-title" href="$url" target="$target" rel="noopener noreferrer">$name</a>
|
||||
<div class="jetpack-blogroll-item-description">$description</div>
|
||||
</div>
|
||||
$subscribe_button_html
|
||||
</div>
|
||||
$fieldset
|
||||
HTML;
|
||||
|
||||
return sprintf(
|
||||
'<div class="%1$s">
|
||||
<hr class="wp-block-separator jetpack-blogroll-item-divider" />
|
||||
<div class="jetpack-blogroll-item-slider">%2$s</div>
|
||||
</div>',
|
||||
esc_attr( $classes ),
|
||||
$content
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Blogroll Block.
|
||||
*
|
||||
* @since 12.1
|
||||
*
|
||||
* @package automattic/jetpack
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack\Extensions\Blogroll;
|
||||
|
||||
require_once __DIR__ . '/blogroll-item/blogroll-item.php';
|
||||
|
||||
use Automattic\Jetpack\Blocks;
|
||||
use Jetpack_Gutenberg;
|
||||
|
||||
/**
|
||||
* Registers the block for use in Gutenberg
|
||||
* This is done via an action so that we can disable
|
||||
* registration if we need to.
|
||||
*/
|
||||
function register_block() {
|
||||
Blocks::jetpack_register_block(
|
||||
__DIR__,
|
||||
array(
|
||||
'render_callback' => __NAMESPACE__ . '\load_assets',
|
||||
'provides_context' => array(
|
||||
'openLinksNewWindow' => 'open_links_new_window',
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
add_action( 'init', __NAMESPACE__ . '\register_block' );
|
||||
|
||||
/**
|
||||
* Blogroll block registration/dependency declaration.
|
||||
*
|
||||
* @param array $attr Array containing the Blogroll block attributes.
|
||||
* @param string $content String containing the Blogroll block content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function load_assets( $attr, $content ) {
|
||||
global $wp;
|
||||
|
||||
/*
|
||||
* Enqueue necessary scripts and styles.
|
||||
*/
|
||||
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
|
||||
$current_location = home_url( $wp->request );
|
||||
$is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM );
|
||||
|
||||
$form_content = <<<HTML
|
||||
<form method="post" action="https://subscribe.wordpress.com" accept-charset="utf-8">
|
||||
<input name="action" type="hidden" value="subscribe">
|
||||
<input name="source" type="hidden" value="$current_location">
|
||||
<input name="sub-type" type="hidden" value="jetpack_blogroll">
|
||||
$content
|
||||
</form>
|
||||
HTML;
|
||||
|
||||
$blogroll_content = $is_wpcom && jetpack_is_frontend() ? $form_content : $content;
|
||||
|
||||
return sprintf(
|
||||
'<div class="%1$s">%2$s</div>',
|
||||
esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ),
|
||||
$blogroll_content
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register site_recommendations settings
|
||||
*
|
||||
* @since 12.7
|
||||
*/
|
||||
function site_recommendations_settings() {
|
||||
register_setting(
|
||||
'general',
|
||||
'Blogroll Recommendations', // Visible to the user see: https://github.com/WordPress/gutenberg/issues/41637
|
||||
array(
|
||||
'description' => __( 'Site Recommendations', 'jetpack' ),
|
||||
'type' => 'array',
|
||||
'show_in_rest' => array(
|
||||
'schema' => array(
|
||||
'items' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'text-field',
|
||||
),
|
||||
'name' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'text-field',
|
||||
),
|
||||
'icon' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
),
|
||||
'url' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
),
|
||||
'description' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'text-field',
|
||||
),
|
||||
'is_non_wpcom_site' => array(
|
||||
'type' => 'boolean',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'auth_callback' => function () {
|
||||
return current_user_can( 'edit_posts' );
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
add_action( 'rest_api_init', __NAMESPACE__ . '\site_recommendations_settings' );
|
||||
Reference in New Issue
Block a user