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:
parent
8bd9659d0d
commit
a563a5b2fa
|
@ -1827,17 +1827,9 @@ function pingback($content, $post_ID) {
|
||||||
|
|
||||||
$pung = get_pung($post_ID);
|
$pung = get_pung($post_ID);
|
||||||
|
|
||||||
// Variables
|
|
||||||
$ltrs = '\w';
|
|
||||||
$gunk = '/#~:.?+=&%@!\-';
|
|
||||||
$punc = '.:?\-';
|
|
||||||
$any = $ltrs . $gunk . $punc;
|
|
||||||
|
|
||||||
// Step 1
|
// Step 1
|
||||||
// Parsing the post, external links (if any) are stored in the $post_links array
|
// Parsing the post, external links (if any) are stored in the $post_links array
|
||||||
// This regexp comes straight from phpfreaks.com
|
$post_links_temp = wp_extract_urls( $content );
|
||||||
// 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);
|
|
||||||
|
|
||||||
// Step 2.
|
// Step 2.
|
||||||
// Walking thru the links array
|
// Walking thru the links array
|
||||||
|
@ -1848,7 +1840,7 @@ function pingback($content, $post_ID) {
|
||||||
// http://dummy-weblog.org/post.php
|
// http://dummy-weblog.org/post.php
|
||||||
// We don't wanna ping first and second types, even if they have a valid <link/>
|
// 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
|
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.
|
&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
|
||||||
if ( $test = @parse_url($link_test) ) {
|
if ( $test = @parse_url($link_test) ) {
|
||||||
|
|
|
@ -392,6 +392,26 @@ function xmlrpc_removepostdata( $content ) {
|
||||||
return $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.
|
* 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 );
|
$pung = get_enclosed( $post_ID );
|
||||||
|
|
||||||
$ltrs = '\w';
|
$post_links_temp = wp_extract_urls( $content );
|
||||||
$gunk = '/#~:.?+=&%@!\-';
|
|
||||||
$punc = '.:?\-';
|
|
||||||
$any = $ltrs . $gunk . $punc;
|
|
||||||
|
|
||||||
preg_match_all( "{\b https? : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
|
|
||||||
|
|
||||||
foreach ( $pung as $link_test ) {
|
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 ) . '%') );
|
$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 )
|
foreach ( $mids as $mid )
|
||||||
delete_metadata_by_mid( 'post', $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
|
if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
|
||||||
$test = @parse_url( $link_test );
|
$test = @parse_url( $link_test );
|
||||||
if ( false === $test )
|
if ( false === $test )
|
||||||
|
|
Loading…
Reference in New Issue