WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 2 years ago

#32787 assigned enhancement

make_clickable filter

Reported by: tlexcellent Owned by: johnjamesjacoby
Milestone: Future Release Priority: normal
Severity: normal Version: 0.71
Component: Formatting Keywords: has-patch needs-unit-tests needs-refresh
Focuses: Cc:

Description

Hi,

it could be very usefull to add filters to regex callback functions in make_clickable : _make_url_clickable _make_web_ftp_clickable_cb _make_email_clickable_cb

Example :

function _make_email_clickable_cb($matches) {
  $email = $matches[2] . '@' . $matches[3];
  $href = apply_filters( 'make_email_clickable_href', 'mailto:'.$email );
  $text = apply_filters( 'make_email_clickable_text', $email );
  $html = apply_filters( 'make_email_clickable_html', '<a href="'.$href.'">'.$text.'</a>' );
  return $matches[1] . $html;
}

Thank you.

Attachments (1)

32787.patch (4.8 KB) - added by johnjamesjacoby 2 years ago.

Download all attachments as: .zip

Change History (17)

#1 @dd32
3 years ago

Hi @tlexcellent Can you offer some suggestions for the uses of these filters?

#2 @chriscct7
3 years ago

  • Keywords needs-patch reporter-feedback added

#3 @tlexcellent
3 years ago

— The email filter could allow to encrypt addresses (via the antispambot() or an other function). — The url/ftp filter could be used to shorten displayed urls/ftps.

Maybe it would be easier to have only one filter by callback function, with email/url/ftp as a second argument :

function _make_email_clickable_cb($matches) {
  $email = $matches[2] . '@' . $matches[3];
  $html = apply_filters( 'make_email_clickable_html', '<a href="'.$href.'">'.$text.'</a>', $email );
  return $matches[1] . $html;
}

#4 follow-up: @Mte90
2 years ago

I was looking to create that patch but as i can see the _make_email_clickable_cb function is in the core but is not used so is useful to do a patch for that?

#5 in reply to: ↑ 4 @tlexcellent
2 years ago

Replying to Mte90:

I was looking to create that patch but as i can see the _make_email_clickable_cb function is in the core but is not used so is useful to do a patch for that?

It’s here, in the make_clickable function, in wp-includes/formatting.php L2240 > 2243 https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-includes/formatting.php#L2240

#6 @Mte90
2 years ago

Yes but exist in the core is not used. So maybe is not required anymore to be a private function.

#7 @johnjamesjacoby
2 years ago

  • Keywords 2nd-opinion added; reporter-feedback removed
  • Milestone changed from Awaiting Review to 4.6
  • Owner set to johnjamesjacoby
  • Status changed from new to assigned
  • Version changed from 4.2.2 to 0.71

I think I have a great example for adding a filter to make_clickable and I'll offer up a patch soon also.

BuddyPress, bbPress, and the P2 theme all use their own (but similar) approach to making usernames clickable using @ as a control character. If there were a filter in make_clickable vs. the currently hard-coded callbacks, these three projects could eliminate some duplication, and use a more reliable core filter vs. their own regex.

bbPress, for example, would deprecate 5 functions if this were possible.

Once patched, I'd like to champion for this to happen in 4.6. Performance with a filter will be only slightly negligible, but any plugin wanting to make anything in content blocks clickable with custom parameters will see relatively good performance gains from the consolidation of their logic.

#8 @DrewAPicture
2 years ago

@johnjamesjacoby I'd be interested in a patch if you still care to follow-up with one.

#9 follow-up: @johnjamesjacoby
2 years ago

32787.patch does the following:

  • Extracts regular expressions out into their own filters
  • Adds these new filters via default-filters.php.

Things that may need changing:

  • Function names. I used plurals because I think they read better. Also suffixed with _filter similar to _cb.
  • PHP doc. Is @access private correct? What about @internal?
  • Filters with callbacks kinda stinks, but I think the extra abstraction is necessary here.
  • Filter documentation for make_clickable filter

The end result of this patch, functionality speaking, is identical to before it. All we do is add a filter on each piece of $text being made clickable. This new make_clickable filter will allow for plugins to introduce their own clickables; specific to BuddyPress, bbPress, and P2, this means linking username mentions to their respective profiles without the need to duplicate the entire make_clickable() stack.

Last edited 2 years ago by johnjamesjacoby (previous) (diff)

#10 in reply to: ↑ 9 @DrewAPicture
2 years ago

  • Keywords has-patch added; needs-patch 2nd-opinion removed

Replying to johnjamesjacoby:

32787.patch does the following:

  • Extracts regular expressions out into their own filters
  • Adds these new filters via default-filters.php.

Things that may need changing:

  • Function names. I used plurals because I think they read better. Also suffixed with _filter similar to _cb.

I would remove the suffix. I think it's fine/expected to name the functions the same as the hooks.

  • PHP doc. Is @access private correct? What about @internal?

@access private is fine, @internal is only used for internal notes. The notes themselves don't get parsed, but the rest of the DocBlock does. I think you're thinking of @ignore which skips parsing of the element altogether – I don't think these warrant @ignore because we want them to be discoverable.

  • Filters with callbacks kinda stinks, but I think the extra abstraction is necessary here.

I also think it's important if people want to unhook it for whatever reason, maybe to completely replace it.

  • Filter documentation for make_clickable filter

Yes, we need this :-) Also, two parameters are passed to the hook but only one is passed to each of the callbacks – is that intentional?

The parameter and return documentation is also incomplete for all of the callbacks.


The end result of this patch, functionality speaking, is identical to before it. All we do is add a filter on each piece of $text being made clickable. This new make_clickable filter will allow for plugins to introduce their own clickables; specific to BuddyPress, bbPress, and P2, this means linking username mentions to their respective profiles without the need to duplicate the entire make_clickable() stack.

:+1:

This ticket was mentioned in Slack in #core by ocean90. View the logs.


2 years ago

#12 follow-up: @ocean90
2 years ago

  • Keywords needs-unit-tests added

@johnjamesjacoby Can you refresh your patch based on @DrewAPicture's feedback? Is there already enough test coverage for the new functions? I think there should be at least one which tests the output when one of the filters is removed.

#13 in reply to: ↑ 12 @johnjamesjacoby
2 years ago

Replying to ocean90:

@johnjamesjacoby Can you refresh your patch based on @DrewAPicture's feedback? Is there already enough test coverage for the new functions? I think there should be at least one which tests the output when one of the filters is removed.

Yes, I can.

I think the test coverage for the existing code is okay. There are 24 tests for make_clickable() but I like your idea of including a test that removes one of these filters and confirms the others still work okay, so I'll include that in the next patch.

#14 @ocean90
2 years ago

  • Keywords needs-refresh added

This ticket was mentioned in Slack in #core by ocean90. View the logs.


2 years ago

#16 @ocean90
2 years ago

  • Milestone changed from 4.6 to Future Release

Patch still needs a refresh. Deadline for beta 1 is tomorrow.

Note: See TracTickets for help on using tickets.