Skip to content

Commit

Permalink
the plugin now sends webmentions asynchron
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle committed Feb 28, 2014
1 parent bc5a688 commit 85aaab1
Showing 1 changed file with 79 additions and 29 deletions.
108 changes: 79 additions & 29 deletions webmention.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public static function init() {
add_action('wp_head', array('WebMentionPlugin', 'html_header'), 99);
add_action('send_headers', array('WebMentionPlugin', 'http_header'));

// run webmentions before the other pinging stuff
add_action('do_pings', array('WebMentionPlugin', 'do_webmentions'), 5, 1);

add_action('publish_post', array('WebMentionPlugin', 'publish_post_hook'));

add_filter('webmention_title', array('WebMentionPlugin', 'default_title_filter'), 10, 4);
Expand Down Expand Up @@ -194,7 +197,7 @@ public static function parse_query($wp) {
$comment_ID = wp_new_comment($commentdata);
}

echo "WebMention received... Thanks :)";
echo apply_filters('webmention_success_message', 'WebMention received... Thanks :)');

do_action( 'webmention_post', $comment_ID );
exit;
Expand Down Expand Up @@ -265,14 +268,26 @@ public static function default_title_filter( $title, $contents, $target, $source
return $title;
}

/**
* Marks the post as "no webmentions sent yet"
*
* @param int $post_ID
*/
public static function publish_post_hook( $post_ID ) {
// check if pingbacks are enabled
if ( get_option('default_pingback_flag') )
add_post_meta( $post_ID, '_mentionme', '1', true );
}


/**
* Send WebMentions
*
* @param string $source source url
* @param string $target target url
* @return array of results including HTTP headers
*/
public static function send_webmention($source, $target) {
public static function send_webmention( $source, $target ) {
// stop selfpings
if ($source == $target) {
return false;
Expand All @@ -282,7 +297,8 @@ public static function send_webmention($source, $target) {
$webmention_server_url = self::discover_endpoint( $target );

$args = array(
'body' => 'source='.urlencode($source).'&target='.urlencode($target)
'body' => 'source='.urlencode($source).'&target='.urlencode($target),
'timeout' => 100
);

if ($webmention_server_url) {
Expand All @@ -292,32 +308,20 @@ public static function send_webmention($source, $target) {
return false;
}

/**
* The WebMention autodicovery meta-tags
*/
public static function html_header() {
// backwards compatibility with v0.1
echo '<link rel="http://webmention.org/" href="'.site_url("?webmention=endpoint").'" />'."\n";
echo '<link rel="webmention" href="'.site_url("?webmention=endpoint").'" />'."\n";
}

/**
* The WebMention autodicovery http-header
*/
public static function http_header() {
// backwards compatibility with v0.1
header('Link: <'.site_url("?webmention=endpoint").'>; rel="http://webmention.org/"', false);
header('Link: <'.site_url("?webmention=endpoint").'>; rel="webmention"', false);
}

/**
* Send WebMentions if new Post was saved
*
* You can still hook this function directly into the `publish_post` action:
*
* <code>
* add_action('publish_post', array('WebMentionPlugin', 'send_webmentions'));
* </code>
*
* @param array $links Links to ping
* @param array $punk Pinged links
* @param int $id The post_ID
*/
public static function publish_post_hook( $post_ID ) {
public static function send_webmentions( $post_ID ) {
// get source url
$source = get_permalink($post_ID);

Expand All @@ -327,21 +331,49 @@ public static function publish_post_hook( $post_ID ) {
// initialize links array
$links = array();

// Find all external links in the source
if (preg_match_all("/<a[^>]+href=.(https?:\/\/[^'\"]+)/i", $post->post_content, $matches)) {
$links = $matches[1];
}
// Parsing the post, external links (if any)
$links = wp_extract_urls( $post->post_content );

// filter links
$targets = apply_filters('webmention_links', $links, $post_ID);
$targets = array_unique($targets);

foreach ($targets as $target) {
// @todo check response
$data = self::send_webmention($source, $target);
// send webmention
$response = self::send_webmention($source, $target);

// check response
if ( !is_wp_error( $response ) ) {
$pung = get_pung($post_ID);

// if not already added to punged urls
if (!in_array($target, $pung)) {
// tell the pingback function not to ping these links again
add_ping( $post_ID, $target );
}
}

// if flood control is is active, rescedule the the cron
if (403 == wp_remote_retrieve_response_code( $response )) {
add_post_meta( $post_ID, '_mentionme', '1', true );
wp_schedule_single_event( time()+60, 'do_pings' );
}
}
}

/**
* Do webmentions
*/
public static function do_webmentions() {
global $wpdb;
// get all posts that should be "mentioned"
while ($mention = $wpdb->get_row("SELECT ID, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_mentionme' LIMIT 1")) {
delete_metadata_by_mid( 'post', $mention->meta_id );
// send them webmentions
self::send_webmentions( $mention->ID );
}
}

/**
* Finds a WebMention server URI based on the given URL.
*
Expand Down Expand Up @@ -410,6 +442,24 @@ public static function discover_endpoint( $url ) {
return false;
}

/**
* The WebMention autodicovery meta-tags
*/
public static function html_header() {
// backwards compatibility with v0.1
echo '<link rel="http://webmention.org/" href="'.site_url("?webmention=endpoint").'" />'."\n";
echo '<link rel="webmention" href="'.site_url("?webmention=endpoint").'" />'."\n";
}

/**
* The WebMention autodicovery http-header
*/
public static function http_header() {
// backwards compatibility with v0.1
header('Link: <'.site_url("?webmention=endpoint").'>; rel="http://webmention.org/"', false);
header('Link: <'.site_url("?webmention=endpoint").'>; rel="webmention"', false);
}

/**
* converts relative to absolute urls
*
Expand Down Expand Up @@ -446,4 +496,4 @@ public static function make_url_absolute( $base, $rel ) {
}

// end check if class already exists
endif;
endif;

0 comments on commit 85aaab1

Please sign in to comment.