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

Compat: Social Links: Remove legacy renderers from packages #20098

Merged
merged 2 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
105 changes: 82 additions & 23 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@
* @package gutenberg
*/

/**
* Retrieves registered social link blocks
*
* @return array Array of strings containing the registered social link block names.
*/
function gutenberg_get_registered_social_link_blocks() {
$social_link_prefix = 'core/social-link';
$social_link_prefix_length = strlen( $social_link_prefix );

$registry = WP_Block_Type_Registry::get_instance();
$block_types = $registry->get_all_registered();

$registered_social_link_blocks = array();
foreach ( $block_types as $block_type ) {
// Block type name starts with $social_link_prefix.
if ( strncmp( $block_type->name, $social_link_prefix, $social_link_prefix_length ) === 0 ) {
$registered_social_link_blocks[] = $block_type->name;
}
}
return $registered_social_link_blocks;
}

/**
* Substitutes the implementation of a core-registered block type, if exists,
* with the built result from the plugin.
Expand All @@ -50,7 +28,7 @@ function gutenberg_reregister_core_block_types() {
'rss.php' => 'core/rss',
'shortcode.php' => 'core/shortcode',
'search.php' => 'core/search',
'social-link.php' => gutenberg_get_registered_social_link_blocks(),
'social-link.php' => 'core/social-link',
'tag-cloud.php' => 'core/tag-cloud',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
Expand Down Expand Up @@ -85,6 +63,87 @@ function gutenberg_reregister_core_block_types() {
}
add_action( 'init', 'gutenberg_reregister_core_block_types' );

/**
* Complements the implementation of block type `core/social-icon`, whether it
Copy link
Member

Choose a reason for hiding this comment

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

I like where you are going with this, but I think we decided to leave the old name:

Suggested change
* Complements the implementation of block type `core/social-icon`, whether it
* Complements the implementation of block type `core/social-link`, whether it

😃

* be provided by core or the plugin, with derived block types for each
* "service" (WordPress, Twitter, etc.) supported by Social Links.
*
* This ensures backwards compatibility for any users running the Gutenberg
* plugin who have used Social Links prior to their conversion to block
* variations.
*
* This shim is INTENTIONALLY left out of core, as Social Links haven't yet
* landed there.
*
* @see https://github.com/WordPress/gutenberg/pull/19887
*/
function gutenberg_register_legacy_social_link_blocks() {
$sites = array(
Copy link
Member

Choose a reason for hiding this comment

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

It probably could be:

Suggested change
$sites = array(
$services = array(

but I see why it is called $sites. It's your call what to land.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That works — done.

'amazon',
'bandcamp',
'behance',
'chain',
'codepen',
'deviantart',
'dribbble',
'dropbox',
'etsy',
'facebook',
'feed',
'fivehundredpx',
'flickr',
'foursquare',
'goodreads',
'google',
'github',
'instagram',
'lastfm',
'linkedin',
'mail',
'mastodon',
'meetup',
'medium',
'pinterest',
'pocket',
'reddit',
'skype',
'snapchat',
'soundcloud',
'spotify',
'tumblr',
'twitch',
'twitter',
'vimeo',
'vk',
'wordpress',
'yelp',
'youtube',
);

foreach ( $sites as $site ) {
register_block_type(
'core/social-link-' . $site,
array(
'category' => 'widgets',
'attributes' => array(
'url' => array(
'type' => 'string',
),
'service' => array(
'type' => 'string',
'default' => $site,
),
'label' => array(
'type' => 'string',
),
),
'render_callback' => 'gutenberg_render_core_social_link',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will need rebasing after #20085.

)
);
}
}
add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );

if ( ! function_exists( 'register_block_style' ) ) {
/**
* Registers a new block style.
Expand Down
66 changes: 0 additions & 66 deletions packages/block-library/src/social-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,75 +30,9 @@ function render_core_social_link( $attributes ) {
* Registers the `core/social-link` blocks.
*/
function register_block_core_social_link() {
$sites = array(
'amazon',
'bandcamp',
'behance',
'chain',
'codepen',
'deviantart',
'dribbble',
'dropbox',
'etsy',
'facebook',
'feed',
'fivehundredpx',
'flickr',
'foursquare',
'goodreads',
'google',
'github',
'instagram',
'lastfm',
'linkedin',
'mail',
'mastodon',
'meetup',
'medium',
'pinterest',
'pocket',
'reddit',
'skype',
'snapchat',
'soundcloud',
'spotify',
'tumblr',
'twitch',
'twitter',
'vimeo',
'vk',
'wordpress',
'yelp',
'youtube',
);

$path = __DIR__ . '/social-link/block.json';
$metadata = json_decode( file_get_contents( $path ), true );

foreach ( $sites as $site ) {
register_block_type(
'core/social-link-' . $site,
array_merge(
$metadata,
array(
'attributes' => array(
'url' => array(
'type' => 'string',
),
'service' => array(
'type' => 'string',
'default' => $site,
),
'label' => array(
'type' => 'string',
),
),
'render_callback' => 'render_core_social_link',
)
)
);
}

register_block_type(
$metadata['name'],
array_merge(
Expand Down