coding standard cleanup

This commit is contained in:
Peter Molnar 2016-01-12 22:36:38 +00:00
parent 48628d28c6
commit 447083efc2

View file

@ -10,12 +10,12 @@ License: GPLv3
Required minimum PHP version: 5.3
*/
if (!class_exists('WP_WEBMENTION_AGAIN')):
if ( ! class_exists( 'WP_Webmention_Again' ) ):
// global send_webmention function
if ( ! function_exists( 'send_webmention' ) ) {
function send_webmention( $source, $target ) {
return WP_WEBMENTION_AGAIN::send( $source, $target );
return WP_Webmention_Again::send( $source, $target );
}
}
@ -30,7 +30,7 @@ if (!class_exists('EmojiRecognizer')) {
require ( __DIR__ . '/vendor/dissolve/single-emoji-recognizer/src/emoji.php' );
}
class WP_WEBMENTION_AGAIN {
class WP_Webmention_Again {
// post meta key for queued incoming mentions
const meta_received = '_webmention_received';
@ -44,7 +44,19 @@ class WP_WEBMENTION_AGAIN {
const expire = 10;
/**
* cron interval for processing incoming
* regular cron interval for processing incoming
*
* use 'wp-webmention-again_interval_received' to filter this integer
*
* @return int cron interval in seconds
*
*/
protected static function remote_timeout () {
return apply_filters( 'wp-webmention-again_remote_timeout', 100 );
}
/**
* regular cron interval for processing incoming
*
* use 'wp-webmention-again_interval_received' to filter this integer
*
@ -52,11 +64,23 @@ class WP_WEBMENTION_AGAIN {
*
*/
protected static function interval_received () {
return apply_filters('wp-webmention-again_interval_received', 90);
return apply_filters( 'wp-webmention-again_interval_received', 600 );
}
/**
* cron interval for processing outgoing
* minimum cron interval for processing incoming
*
* use 'wp-webmention-again_interval_received' to filter this integer
*
* @return int cron interval in seconds
*
*/
protected static function interval_received_min () {
return apply_filters( 'wp-webmention-again_interval_received_min', 60 );
}
/**
* regular cron interval for processing outgoing
*
* use 'wp-webmention-again_interval_send' to filter this integer
*
@ -64,9 +88,20 @@ class WP_WEBMENTION_AGAIN {
*
*/
protected static function interval_send () {
return apply_filters('wp-webmention-again_interval_send', 90);
return apply_filters( 'wp-webmention-again_interval_send', 600 );
}
/**
* minimum cron interval for processing outgoing
*
* use 'wp-webmention-again_interval_send' to filter this integer
*
* @return int cron interval in seconds
*
*/
protected static function interval_send_min () {
return apply_filters( 'wp-webmention-again_interval_send_min', 60 );
}
/**
* max number of retries ( both for outgoing and incoming )
*
@ -145,9 +180,10 @@ class WP_WEBMENTION_AGAIN {
add_action( 'send_headers', array( &$this, 'http_header' ) );
// this is mostly for debugging reasons
register_activation_hook( __FILE__ , array( &$this, 'plugin_activate' ) );
register_activation_hook( __FILE__ , array( 'WP_Webmention_Again', 'plugin_activate' ) );
// clear schedules if there's any on deactivation
register_deactivation_hook( __FILE__ , array( &$this, 'plugin_deactivate' ) );
register_deactivation_hook( __FILE__ , array( 'WP_Webmention_Again', 'plugin_deactivate' ) );
// extend current cron schedules with our entry
add_filter( 'cron_schedules', array(&$this, 'add_cron_schedule' ) );
@ -180,6 +216,9 @@ class WP_WEBMENTION_AGAIN {
// additional avatar filter
add_filter( 'get_avatar' , array( &$this, 'get_avatar' ), 1, 5 );
// get_pung is not restrictive enough
add_filter ( 'get_pung', array( &$this, 'get_pung' ) );
if ( ! wp_get_schedule( static::cron_received ) ) {
wp_schedule_event( time(), static::cron_received, static::cron_received );
}
@ -196,7 +235,7 @@ class WP_WEBMENTION_AGAIN {
* dies if PHP version is too low
*
*/
public function plugin_activate() {
public static function plugin_activate() {
if ( version_compare( phpversion(), 5.3, '<' ) ) {
die( 'The minimum PHP version required for this plugin is 5.3' );
}
@ -210,7 +249,7 @@ class WP_WEBMENTION_AGAIN {
* makes sure there are no scheduled cron hooks left
*
*/
public function plugin_deactivate () {
public static function plugin_deactivate () {
wp_unschedule_event( time(), static::cron_received );
wp_clear_scheduled_hook( static::cron_received );
@ -319,6 +358,7 @@ class WP_WEBMENTION_AGAIN {
$options['comment_types'][ $reacji ] = $reacji;
update_option( __CLASS__ , $options );
}
}
/**
@ -332,10 +372,10 @@ class WP_WEBMENTION_AGAIN {
public function comment_types_dropdown( $types ) {
$options = static::get_options();
foreach ($options['comment_types'] as $type => $fancy ) {
foreach ( $options['comment_types'] as $type => $fancy )
if ( ! isset( $types[ $type ] ) )
$types[ $type ] = ucfirst( $type );
}
return $types;
}
@ -350,10 +390,9 @@ class WP_WEBMENTION_AGAIN {
public function add_comment_types ( $types ) {
$options = static::get_options();
foreach ($options['comment_types'] as $type => $fancy ) {
foreach ( $options['comment_types'] as $type => $fancy )
if ( ! in_array( $type, $types ) )
array_push( $types, $type );
}
return $types;
}
@ -391,20 +430,21 @@ class WP_WEBMENTION_AGAIN {
$target = filter_var( $_POST['target'], FILTER_SANITIZE_URL );
$source = filter_var( $_POST['source'], FILTER_SANITIZE_URL );
if ( filter_var($target, FILTER_VALIDATE_URL) === false ) {
if ( false === filter_var( $target, FILTER_VALIDATE_URL ) ) {
status_header( 400 );
echo '"target" is an invalid URL';
exit;
}
if ( filter_var($source, FILTER_VALIDATE_URL) === false ) {
if ( false === filter_var( $source, FILTER_VALIDATE_URL ) ) {
status_header( 400 );
echo '"source" is an invalid URL';
exit;
}
$post_id = static::validate_local( $target );
if (!$post_id || $post_id == 0) {
if (! $post_id || 0 == $post_id ) {
status_header( 404 );
echo '"target" not found.';
exit;
@ -420,7 +460,7 @@ class WP_WEBMENTION_AGAIN {
// queue here, the remote check will be async
$r = static::queue_receive( $source, $target, $post_id );
if ($r) {
if ( true == $r ) {
status_header( 202 );
echo 'Webmention accepted in the queue.';
}
@ -452,10 +492,17 @@ class WP_WEBMENTION_AGAIN {
);
static::debug( "queueing {static::meta_received} meta for #{$post_id}; source: {$source}, target: {$target}" );
$r = add_post_meta( $post_id, static::meta_received, $val, false );
if ($r == false )
if ( false == $r ) {
static::debug( "adding {static::meta_received} meta for #{$post_id} failed" );
}
else {
// fire up a single cron event if the scheduled is too far in the future
if ( wp_next_scheduled( static::cron_received ) > static::interval_received_min() )
wp_schedule_single_event( time() , static::cron_received );
}
return $r;
}
@ -474,9 +521,9 @@ class WP_WEBMENTION_AGAIN {
return true;
foreach ( $posts as $post_id ) {
$received_mentions = get_post_meta ( $post_id, static::meta_received, false );
static::debug('todo: ' . json_encode($received_mentions));
foreach ( $received_mentions as $m ) {
// $m should not be modified as this is how the current entry can be identified!
$_m = $m;
@ -484,7 +531,11 @@ class WP_WEBMENTION_AGAIN {
static::debug( "working on webmention for post #{$post_id}" );
// this really should not happen, but if it does, get rid of this entry immediately
if (!isset($_m['target']) || empty($_m['target']) || !isset($_m['source']) || empty($_m['source'])) {
if (! isset( $_m['target'] ) ||
empty( $_m['target'] ) ||
! isset( $_m['source'] ) ||
empty( $_m['source'] )
) {
static::debug( " target or souce empty, aborting" );
continue;
}
@ -506,7 +557,7 @@ class WP_WEBMENTION_AGAIN {
// validate target
$remote = static::try_receive_remote( $post_id, $_m['source'], $_m['target'] );
if ($remote === false || empty($remote)) {
if ( false === $remote || empty( $remote ) ) {
static::debug( " parsing this mention failed, retrying next time" );
update_post_meta( $post_id, static::meta_received, $_m, $m );
continue;
@ -516,7 +567,7 @@ class WP_WEBMENTION_AGAIN {
$c = static::try_parse_remote ( $post_id, $_m['source'], $_m['target'], $remote );
$ins = static::insert_comment ( $post_id, $_m['source'], $_m['target'], $remote, $c );
if ($ins === true) {
if ( true === $ins ) {
static::debug( " duplicate (or something similar): this queue element has to be ignored; deleting queue entry" );
delete_post_meta( $post_id, static::meta_received, $m );
}
@ -552,28 +603,16 @@ class WP_WEBMENTION_AGAIN {
$q = $wpdb->get_results( $db_command );
}
catch ( Exception $e ) {
static::debug('Something went wrong: ' . $e->getMessage());
static::debug( "Something went wrong: " . $e->getMessage() );
}
if ( ! empty( $q ) && is_array( $q ) ) {
foreach ( $q as $post ) {
$r[] = $post->post_id;
array_push( $r, $post->post_id );
}
}
return $r;
/*
* this does not work reliably
$args = array(
'posts_per_page' => static::per_batch(),
'meta_key' => static::meta_received,
//'meta_value' => '*',
'post_type' => 'any',
'post_status' => 'publish',
);
return get_posts( $args );
*/
}
@ -591,22 +630,22 @@ class WP_WEBMENTION_AGAIN {
$q = wp_remote_get( $source );
if ( is_wp_error( $q ) ) {
static::debug(' something went wrong: ' . $q->get_error_message());
static::debug( " something went wrong: " . $q->get_error_message() );
return false;
}
if ( !is_array( $q ) ) {
static::debug(' $q is not an array. It should be one.');
static::debug( " $q is not an array. It should be one." );
return false;
}
if ( ! isset( $q['headers'] ) || ! is_array( $q['headers'] ) ) {
static::debug(' missing response headers.');
static::debug( " missing response headers." );
return false;
}
if ( ! isset( $q['body'] ) || empty( $q['body'] ) ) {
static::debug(' missing body');
static::debug( " missing body" );
return false;
}
@ -628,7 +667,7 @@ class WP_WEBMENTION_AGAIN {
$content = false;
$q = static::_wp_remote_get( $source );
if ($q === false)
if ( false === $q )
return false;
$t = $target;
@ -645,7 +684,7 @@ class WP_WEBMENTION_AGAIN {
$ctype = isset( $q['headers']['content-type'] ) ? $q['headers']['content-type'] : 'text/html';
if ($ctype == "text/plain") {
if ( "text/plain" == $ctype ) {
static::debug( " interesting, plain text webmention. I'm not prepared for this yet" );
return false;
}
@ -659,7 +698,7 @@ class WP_WEBMENTION_AGAIN {
$content = Mf2\parse( $q['body'], $source );
}
catch ( Exception $e ) {
static::debug(' parsing MF2 failed:' . $e->getMessage());
static::debug( " parsing MF2 failed: " . $e->getMessage() );
return false;
}
@ -689,10 +728,10 @@ class WP_WEBMENTION_AGAIN {
}
elseif ( is_array($content['items'] ) && ! empty( $content['items']['type'] ) ) {
foreach ( $content['items'] as $i ) {
if ($i['type'] == 'h-entry') {
if ( 'h-entry' == $i['type'] ) {
$item = $i;
}
elseif ($i['type'] == 'h-card') {
elseif ( 'h-card' == $i['type'] ) {
$p_authors[] = $i;
}
}
@ -763,7 +802,7 @@ class WP_WEBMENTION_AGAIN {
$emoji = EmojiRecognizer::isSingleEmoji( $c );
if ( $emoji ) {
static::debug('wheeeee, reacji!');
static::debug( "wheeeee, reacji!" );
$type = trim( $c );
static::register_reacji( $type );
}
@ -828,7 +867,7 @@ class WP_WEBMENTION_AGAIN {
// so if the type is comment and you add type = 'comment', WP will not return the comments
// such logical!
if ( $comment['comment_type'] != 'comment')
if ( 'comment' != $comment['comment_type'] )
$testargs['type'] = $comment['comment_type'];
// in case it's a fav or a like, the date field is not always present
@ -852,8 +891,6 @@ class WP_WEBMENTION_AGAIN {
'second' => $t[2],
);
//$testargs['date_query'] = $comment['comment_date'];
//test if we already have this imported
static::debug( "checking comment existence (with date) for post #{$post_id}" );
}
@ -869,7 +906,7 @@ class WP_WEBMENTION_AGAIN {
// no matching comment yet, insert it
if ( ! empty( $existing ) ) {
static::debug ('comment already exists');
static::debug ( "comment already exists" );
return true;
}
@ -880,9 +917,8 @@ class WP_WEBMENTION_AGAIN {
if ( $comment_id = wp_new_comment( $comment ) ) {
// add avatar for later use if present
if (!empty($avatar)) {
if ( ! empty( $avatar ) )
update_comment_meta( $comment_id, 'avatar', $avatar );
}
// full raw response for the vote, just in case
update_comment_meta( $comment_id, 'webmention_source_mf2', $raw );
@ -904,13 +940,11 @@ class WP_WEBMENTION_AGAIN {
add_filter( 'check_comment_flood', 'check_comment_flood_db', 10, 3 );
static::debug( $r );
return $comment_id;
}
/**
* triggered on post transition, applied when new status is publish, therefore
* applied on edit of published posts as well
@ -922,18 +956,25 @@ class WP_WEBMENTION_AGAIN {
*/
public function queue_send( $new_status, $old_status, $post ) {
if ( ! static::is_post( $post ) ) {
static::debug("Woops, this is not a post.");
static::debug( "Whoops, this is not a post." );
return false;
}
if ($new_status != 'publish') {
if ( 'publish' != $new_status ) {
static::debug( "Not adding {$post->ID} to mention queue yet; not published" );
return false;
}
$r = add_post_meta( $post->ID, static::meta_send, 1, true );
if (! $r = add_post_meta($post->ID, static::meta_send, 1, true) )
if ( ! $r ) {
static::debug( "Tried adding post #{$post->ID} to mention queue, but it didn't go well" );
}
else {
// fire up a single cron event if the scheduled is too far in the future
if ( wp_next_scheduled( static::cron_send ) > static::interval_send_min() )
wp_schedule_single_event( time() , static::cron_send );
}
return $r;
}
@ -964,15 +1005,16 @@ class WP_WEBMENTION_AGAIN {
// process the content as if it was the_content()
$content = static::get_the_content( $post );
// get all urls in content
$urls = static::extract_urls( $content );
// for special ocasions when someone wants to add to this list
$urls = apply_filters( 'webmention_links', $urls, $post->ID );
$urls = array_unique( $urls );
$todo = $urls;
$failed = array();
$pung = static::get_pung( $post->ID );
$pung = get_pung( $post->ID );
foreach ( $urls as $target ) {
$target = strtolower( $target );
@ -980,7 +1022,7 @@ class WP_WEBMENTION_AGAIN {
// already pinged, skip
if ( in_array( $target, $pung ) ) {
static::debug(' already pinged!' );
static::debug( " already pinged!" );
$todo = array_diff( $todo, array( $target ) );
continue;
}
@ -988,7 +1030,8 @@ class WP_WEBMENTION_AGAIN {
// tried too many times
$try_key = static::meta_send . '_' . $target;
$tries = intval( get_post_meta( $post->ID, $try_key, true ) );
if ($tries && $tries >= static::retry() ) {
if ( false != $tries && $tries >= static::retry() ) {
static::debug( " failed too many times; skipping" );
$todo = array_diff( $todo, array( $target ) );
array_push( $failed, $try_key );
@ -1005,7 +1048,7 @@ class WP_WEBMENTION_AGAIN {
continue;
}
else {
static::debug(' sending succeeded!');
static::debug( " sending succeeded!" );
add_ping( $post->ID, $target );
$todo = array_diff( $todo, array( $target ) );
}
@ -1013,7 +1056,7 @@ class WP_WEBMENTION_AGAIN {
}
if ( empty( $todo ) ) {
static::debug(' no more urls to ping or no more tries left, cleaning up');
static::debug( " no more urls to ping or no more tries left, cleaning up" );
foreach ( $failed as $try_key )
delete_post_meta( $post->ID, $try_key );
@ -1026,14 +1069,20 @@ class WP_WEBMENTION_AGAIN {
}
/**
* make pung stricter
*
* @param array $pung array of pinged urls
*
* @return array a better array of pinged urls
*
*/
protected static function get_pung ($post_id) {
$pung = get_pung( $post_id );
foreach ($pung as $k => $e ) {
public function get_pung ( $pung ) {
foreach ($pung as $k => $e )
$pung[ $k ] = strtolower( $e );
}
$pung = array_unique($pung);
return $pung;
}
@ -1044,6 +1093,7 @@ class WP_WEBMENTION_AGAIN {
*/
protected static function get_send () {
global $wpdb;
$r = array();
$dbname = "{$wpdb->prefix}postmeta";
@ -1055,32 +1105,16 @@ class WP_WEBMENTION_AGAIN {
$q = $wpdb->get_results( $db_command );
}
catch ( Exception $e ) {
static::debug('Something went wrong: ' . $e->getMessage());
static::debug( "Something went wrong: " . $e->getMessage() );
}
if ( ! empty( $q ) && is_array( $q ) ) {
foreach ( $q as $post ) {
$r[] = $post->post_id;
array_push( $r, $post->post_id );
}
}
return $r;
/*
* this is not reliable
$args = array(
//'posts_per_page' => static::per_batch(),
'posts_per_page' => -1,
'meta_key' => static::meta_send,
'meta_value' => 1,
'post_type' => 'any',
'post_status' => 'publish',
);
return get_posts( $args );
*/
}
/**
@ -1114,10 +1148,10 @@ class WP_WEBMENTION_AGAIN {
if ( $webmention_server_url ) {
$args = array(
'body' => 'source=' . urlencode( $source ) . '&target=' . urlencode( $target ),
'timeout' => 100,
'timeout' => static::remote_timeout(),
);
static::debug('Sending webmention to: ' .$webmention_server_url . ' as: ' . $args['body']);
static::debug( "Sending webmention to: " .$webmention_server_url . " as: " . $args['body'] );
$response = wp_remote_post( $webmention_server_url, $args );
// use the response to do something usefull
@ -1143,22 +1177,20 @@ class WP_WEBMENTION_AGAIN {
* @return bool|string False on failure, string containing URI on success
*/
protected static function discover_endpoint( $url ) {
/** @todo Should use Filter Extension or custom preg_match instead. */
$parsed_url = parse_url( $url );
if ( ! isset( $parsed_url['host'] ) ) { // Not an URL. This should never happen.
// Not an URL. This should never happen.
if ( false === filter_var( $url, FILTER_VALIDATE_URL ) )
return false;
}
// do not search for a WebMention server on our own uploads
$uploads_dir = wp_upload_dir();
if ( 0 === strpos( $url, $uploads_dir['baseurl'] ) ) {
if ( 0 === strpos( $url, $uploads_dir['baseurl'] ) )
return false;
}
$response = wp_remote_head( $url, array( 'timeout' => 100, 'httpversion' => '1.0' ) );
$response = wp_remote_head( $url, array( 'timeout' => static::remote_timeout(), 'httpversion' => '1.0' ) );
if ( is_wp_error( $response ) ) {
static::debug( "Something went wrong: " . $response->get_error_message() );
return false;
}
@ -1183,7 +1215,7 @@ class WP_WEBMENTION_AGAIN {
}
// now do a GET since we're going to look in the html headers (and we're sure its not a binary file)
$response = wp_remote_get( $url, array( 'timeout' => 100, 'httpversion' => '1.0' ) );
$response = wp_remote_get( $url, array( 'timeout' => static::remote_timeout(), 'httpversion' => '1.0' ) );
if ( is_wp_error( $response ) ) {
return false;
@ -1269,6 +1301,9 @@ class WP_WEBMENTION_AGAIN {
preg_match_all("/\b(?:http|https)\:\/\/?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.[a-zA-Z0-9\.\/\?\:@\-_=#]*/i", $text, $matches);
$matches = $matches[0];
$matches = array_unique($matches);
return $matches;
}
@ -1362,8 +1397,7 @@ class WP_WEBMENTION_AGAIN {
else
$safe_alt = esc_attr( $alt );
return sprintf( '<img alt="%s" src="%s" class="avatar photo u-photo" style="width: %spx; height: %spx;" />', $safe_alt, $c_avatar, $size, $size );
return sprintf( '<img alt="%s" src="%s" class="avatar photo u-photo" style="width: %dpx; height: %dpx;" />', $safe_alt, $c_avatar, $size, $size );
}
/**
@ -1405,7 +1439,7 @@ class WP_WEBMENTION_AGAIN {
wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' );
exit;
default:
if ( !defined( 'WP_DEBUG' ) || WP_DEBUG != true )
if ( ! defined( 'WP_DEBUG' ) || true != WP_DEBUG )
return;
break;
}
@ -1414,6 +1448,6 @@ class WP_WEBMENTION_AGAIN {
}
}
$WP_WEBMENTION_AGAIN = new WP_WEBMENTION_AGAIN();
$WP_Webmention_Again = new WP_Webmention_Again();
endif;