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

Social links, default protocol to https if not set #30100

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 9 additions & 0 deletions packages/block-library/src/social-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ function render_block_core_social_link( $attributes, $content, $block ) {
)
);

// Add default https if no protocol specified. The following chunk of code
// is taken from esc_url which is hard-coded to use http. This can be
// changed to pass the parameter in esc_url after
// https://core.trac.wordpress.org/ticket/52886 is in core.
if ( strpos( $url, ':' ) === false && ! in_array( $url[0], array( '/', '#', '?' ), true ) &&
! preg_match( '/^[a-z0-9-]+?\.php/i', $url ) ) {
Copy link
Contributor

@gwwar gwwar May 28, 2021

Choose a reason for hiding this comment

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

Could we add a few unit tests for this? For example, why would we be looking for a .php filename? Are there any other valid protocol-less links that folks would normally enter?

To make this easier to test, we can pull this logic out to a helper function. See example and docs https://developer.wordpress.org/block-editor/contributors/code/testing-overview/#php-testing

$url = 'https://' . $url;
}

return '<li ' . $wrapper_attributes . '><a href="' . esc_url( $url ) . '" aria-label="' . esc_attr( $label ) . '" ' . $attribute . ' class="wp-block-social-link-anchor"> ' . $icon . '</a></li>';
}

Expand Down