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

Gutenberg Block for Tweet Shortcode? #7597

Closed
wants to merge 21 commits into from
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
728f07a
A good start maybe?
georgestephanis Aug 4, 2017
f3c43aa
Correct how we refer to the tweet attribute.
georgestephanis Aug 5, 2017
b9da75c
Update the prop when the field changes. Still not thrilled with this…
georgestephanis Aug 5, 2017
1fbb0d4
Add a dummy function to explore how we can parse this on initialization.
georgestephanis Aug 5, 2017
6817940
Somewhat tidier way of doing the script.
georgestephanis Aug 5, 2017
2e98277
Clear out some unused chaff.
georgestephanis Aug 7, 2017
7b3729b
Flesh out how to find the tweet url from the node.
georgestephanis Aug 7, 2017
eb7499e
Don't return the result of the array directly.
georgestephanis Aug 7, 2017
3d3cb22
Use onChange not onInput to satisfy browser console nag.
georgestephanis Aug 7, 2017
9b2f478
Add in handling for other optional attributes.
georgestephanis Aug 7, 2017
9b22ae0
Wrap it in the generated classname? Probably a good idea?
georgestephanis Aug 7, 2017
6fbc6c2
Oops, forgot the closing parens.
georgestephanis Aug 7, 2017
fb52f65
Set the generated shortcode to single.
georgestephanis Aug 7, 2017
2b8a30a
Change to an array so we can return multiples.
georgestephanis Aug 7, 2017
8d9f1f4
Add BlockControls AlignmentToolbar.
georgestephanis Aug 7, 2017
0b5476f
Add sidebar width control.
georgestephanis Aug 7, 2017
baa38bc
If there's a className to be set, wrap it in a div so it can be set o…
georgestephanis Aug 7, 2017
ba90d71
This is necessary so React doesn't turn `"` into `"`
georgestephanis Aug 7, 2017
32a0ea5
Rewrite attributes array to a function.
Aug 17, 2017
1f52720
Are you the keymaster?
Aug 17, 2017
ebe3953
Alias `el` to `wp.element.createElement`
Aug 17, 2017
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
188 changes: 188 additions & 0 deletions modules/shortcodes/tweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

add_shortcode( 'tweet', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode' ) );
add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Tweet', 'enqueue_block_editor_assets' ) );

class Jetpack_Tweet {

Expand Down Expand Up @@ -142,4 +143,191 @@ static public function jetpack_tweet_shortcode_script() {
}
}

static public function enqueue_block_editor_assets() {
wp_register_script(
'jetpack-shortcode-tweet-gutenberg',
null,
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'shortcode' )
);
wp_enqueue_script( 'jetpack-shortcode-tweet-gutenberg' );

ob_start();
self::jetpack_shortcode_tweet_gutenberg_script();
$content = ob_get_clean();

wp_script_add_data( 'jetpack-shortcode-tweet-gutenberg', 'data', $content );
}

static public function jetpack_shortcode_tweet_gutenberg_script() {
?>
// <script>
( function( wp ) {
var el = wp.element.createElement;

wp.blocks.registerBlockType( 'jetpack/tweet', {
title: wp.i18n.__( 'Tweet', 'jetpack' ),
icon: 'twitter',
category: 'layout',

attributes : function( content ) {
console.log( content );
var shortcode = wp.shortcode.next( 'tweet', content ),
data = {};

if ( shortcode.attrs.named.tweet ) {
data.tweet = shortcode.attrs.named.tweet;
} else if ( shortcode.attrs.numeric[0] ) {
data.tweet = shortcode.attrs.numeric[0];
}

if ( shortcode.attrs.named.align ) {
data.align = shortcode.attrs.named.align;
}

if ( shortcode.attrs.named.width ) {
data.width = shortcode.attrs.named.width;
}

if ( shortcode.attrs.named.lang ) {
data.lang = shortcode.attrs.named.lang;
}

if ( shortcode.attrs.named.hide_thread ) {
data.hide_thread = shortcode.attrs.named.hide_thread;
}

if ( shortcode.attrs.named.hide_media ) {
data.hide_media = shortcode.attrs.named.hide_media;
}

return data;
},

edit : function( props ) {
return [
!! props.focus && el(
wp.blocks.BlockControls,
{ key : 'controls' },
el(
wp.blocks.AlignmentToolbar,
{
key : '<?php echo md5( __FILE__ . __LINE__ ); ?>',
value : props.attributes.align,
onChange : function( newAlignment ) {
props.setAttributes( {
align : newAlignment
} );
}
}
)
),
!! props.focus && el(
wp.blocks.InspectorControls,
{ key : 'inspector' },
[
el(
wp.blocks.BlockDescription,
{ key : '<?php echo md5( __FILE__ . __LINE__ ); ?>' },
el(
'p',
null,
wp.i18n.__( 'Optional embed settings:' )
)
),
el(
'label',
{ key : '<?php echo md5( __FILE__ . __LINE__ ); ?>' },
el(
'label',
{ key : '<?php echo md5( __FILE__ . __LINE__ ); ?>' },
[
wp.i18n.__( 'Width:' ),
el(
'input',
{
key : '<?php echo md5( __FILE__ . __LINE__ ); ?>',
type : 'number',
min : 100,
value : props.attributes.width,
onChange : function( newWidth ) {
props.setAttributes( {
width : newWidth.target.value
} );
}
}
)
]
)
),
]
),
el(
'input',
{
key : '<?php echo md5( __FILE__ . __LINE__ ); ?>',
name : 'tweet',
type : 'url',
value : props.attributes.tweet,
onChange: function( event ) {
props.setAttributes({
tweet : event.target.value
});
}
},
null
)
];
},

save : function( props ) {
var args = {
tag : 'tweet',
type : 'single',
attrs : {
named : {},
numeric : [
props.attributes.tweet
]
}
};

// Populate optional attributes.
if ( props.attributes.align && props.attributes.align !== 'none' ) {
args.attrs.named.align = props.attributes.align;
}
if ( props.attributes.width && props.attributes.width !== '' ) {
args.attrs.named.width = props.attributes.width;
}
if ( props.attributes.lang && props.attributes.lang !== 'en' ) {
args.attrs.named.lang = props.attributes.lang;
}
if ( props.attributes.hide_thread && props.attributes.hide_thread !== 'none' ) {
args.attrs.named.hide_thread = props.attributes.hide_thread;
}
if ( props.attributes.hide_media && props.attributes.hide_media !== 'none' ) {
args.attrs.named.hide_media = props.attributes.hide_media;
}

// If there's a className to be set, wrap it in a div so it can be set on that.
if ( props.className || props.attributes.className ) {
return el(
'div',
{
// This is necessary so React doesn't turn `"` into `&quot;`
dangerouslySetInnerHTML: {
__html: wp.shortcode.string( args )
}
}
);
}

return wp.shortcode.string( args );
}

} );
} )( window.wp );
// </script>
<?php
}

} // class end