Replace the ancient `phpfreaks.com` RegEx to extract urls to ping with a more robust matcher. URLs with commas and things like `&` were not being pinged. The new matcher even works for most IDN URLs. Adds unit tests.

Fixes #9064.


Built from https://develop.svn.wordpress.org/trunk@25313


git-svn-id: http://core.svn.wordpress.org/trunk@25275 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2013-09-10 03:18:08 +00:00
parent 8bd9659d0d
commit a563a5b2fa
2 changed files with 25 additions and 18 deletions

View File

@ -1827,17 +1827,9 @@ function pingback($content, $post_ID) {
$pung = get_pung($post_ID);
// Variables
$ltrs = '\w';
$gunk = '/#~:.?+=&%@!\-';
$punc = '.:?\-';
$any = $ltrs . $gunk . $punc;
// Step 1
// Parsing the post, external links (if any) are stored in the $post_links array
// This regexp comes straight from phpfreaks.com
// http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
$post_links_temp = wp_extract_urls( $content );
// Step 2.
// Walking thru the links array
@ -1848,7 +1840,7 @@ function pingback($content, $post_ID) {
// http://dummy-weblog.org/post.php
// We don't wanna ping first and second types, even if they have a valid <link/>
foreach ( (array) $post_links_temp[0] as $link_test ) :
foreach ( (array) $post_links_temp as $link_test ) :
if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself
&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
if ( $test = @parse_url($link_test) ) {

View File

@ -392,6 +392,26 @@ function xmlrpc_removepostdata( $content ) {
return $content;
}
/**
* Use RegEx to extract URLs from arbitrary content
*
* @since 3.7.0
*
* @param string $content
* @return array URLs found in passed string
*/
function wp_extract_urls( $content ) {
preg_match_all(
"#((?:[\w-]+://?|[\w\d]+[.])[^\s()<>]+[.](?:\([\w\d]+\)|(?:[^`!()\[\]{};:'\".,<>?«»“”‘’\s]|(?:[:]\d+)?/?)+))#",
$content,
$post_links
);
$post_links = array_unique( array_map( 'html_entity_decode', $post_links[0] ) );
return array_values( $post_links );
}
/**
* Check content for video and audio links to add as enclosures.
*
@ -417,22 +437,17 @@ function do_enclose( $content, $post_ID ) {
$pung = get_enclosed( $post_ID );
$ltrs = '\w';
$gunk = '/#~:.?+=&%@!\-';
$punc = '.:?\-';
$any = $ltrs . $gunk . $punc;
preg_match_all( "{\b https? : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
$post_links_temp = wp_extract_urls( $content );
foreach ( $pung as $link_test ) {
if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
$mids = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $link_test ) . '%') );
foreach ( $mids as $mid )
delete_metadata_by_mid( 'post', $mid );
}
}
foreach ( (array) $post_links_temp[0] as $link_test ) {
foreach ( (array) $post_links_temp as $link_test ) {
if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
$test = @parse_url( $link_test );
if ( false === $test )