Skip to content

Commit

Permalink
Compat: Social Links: Remove legacy renderers from packages (#20098)
Browse files Browse the repository at this point in the history
* Compat: Social Links: Remove legacy renderers from packages

* Address review feedback
  • Loading branch information
mcsf authored Feb 7, 2020
1 parent 481840c commit 48133c7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 89 deletions.
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
* 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() {
$services = 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',
);

foreach ( $services as $service ) {
register_block_type(
'core/social-link-' . $service,
array(
'category' => 'widgets',
'attributes' => array(
'url' => array(
'type' => 'string',
),
'service' => array(
'type' => 'string',
'default' => $service,
),
'label' => array(
'type' => 'string',
),
),
'render_callback' => 'gutenberg_render_core_social_link',
)
);
}
}
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

0 comments on commit 48133c7

Please sign in to comment.