Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog Subscription Widget: deprecate widget and transform to block #21184

Merged
merged 6 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: compat

Subscription Widget: removed from Legacy Widget block and added transform
23 changes: 23 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/subscriptions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __, _x } from '@wordpress/i18n';
import { Rect, Path, SVG } from '@wordpress/components';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -58,6 +59,28 @@ export const settings = {
attributes,
edit,
save,
transforms: {
from: [
{
type: 'block',
isMultiBlock: false,
blocks: [ 'core/legacy-widget' ],
isMatch: ( { idBase, instance } ) => {
if ( ! instance?.raw ) {
return false;
}
return idBase === 'blog_subscription';
},
transform: ( { instance } ) => {
return createBlock( 'jetpack/subscriptions', {
showSubscribersTotal: instance.raw.show_subscribers_total,
submitButtonText: instance.raw.subscribe_button,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arcangelini 👋 have you tried changing it to some different custom string!
Example: submitButtonText: 'Subscribe to my newsletter'.
I tried something similar but it is not working for me somehow! 😞 submitButtonText value is set to default value that you mentioned in the attributes.js file
Can you please try at your end and confirm?
Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if you look at my last screenshot it shows the placeholder text as "Placeholder" and the submit button as "Button".

I can test again though!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand what this block of code does?

return createBlock( 'jetpack/subscriptions', {
    showSubscribersTotal: instance.raw.show_subscribers_total,
    submitButtonText: instance.raw.subscribe_button,
    subscribePlaceholder: instance.raw.subscribe_placeholder,
} );

If you see in the screenshot, I have commented default value of submitButtonText and if it has to read the value from index.js file instance.raw.subscribe_button then I have given some hard coded value custom button value which is not getting reflected. Button text is empty actually.

POC

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arcangelini, no need to test it again. It is working as expected. I understand now when that block of code is going to run.
Thank You 😄

subscribePlaceholder: instance.raw.subscribe_placeholder,
} );
},
},
],
},
example: {
attributes: {},
},
Expand Down
18 changes: 18 additions & 0 deletions projects/plugins/jetpack/modules/subscriptions/views.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

class Jetpack_Subscriptions_Widget extends WP_Widget {

const ID_BASE = 'blog_subscription';

static $instance_count = 0;

/**
* @var array When printing the submit button, what tags are allowed
*/
Expand Down Expand Up @@ -32,6 +36,7 @@ function __construct() {
'classname' => 'widget_blog_subscription jetpack_subscription_widget',
'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);

$name = self::is_jetpack() ?
Expand All @@ -54,6 +59,19 @@ function __construct() {
) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}

add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
}

/**
* Remove Social Icons widget from Legacy Widget block.
*
* @param array $widget_types Widget type data.
* This only applies to new blocks being added.
*/
public function hide_widget_in_block_editor( $widget_types ) {
$widget_types[] = self::ID_BASE;
return $widget_types;
}

/**
Expand Down