Last active
May 8, 2022 09:23
-
-
Save janboddez/25b1c59a8cabf0a7f36d67e122edcb71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Webmention Widget | |
* Description: Add recent webmentions’ status to WordPress’ dashboard. | |
* Version: 0.1 | |
* Author: Jan Boddez | |
* Author URI: https://jan.boddez.net/ | |
* License: GNU General Public License v2 or later | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* | |
* @package Webmention_Widget | |
*/ | |
class Webmention_Widget { | |
/** | |
* Registers main hook. | |
*/ | |
function __construct() { | |
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) ); | |
add_action( 'webmention_post_send', array( $this, 'store_webmention_status' ), 10, 4 ); | |
} | |
/** | |
* Defines the Pages Dashboard Widget. | |
*/ | |
public function add_dashboard_widget() { | |
wp_add_dashboard_widget( | |
'webmention_dashboard_widget', // Widget slug. | |
__( 'Recently Sent Webmentions', 'webmention-widget' ), // Title. | |
array( $this, 'webmention_dashboard_widget' ) // Display function. | |
); | |
} | |
/** | |
* Stores a sent webmention's status. | |
*/ | |
public function store_webmention_status( $response, $source, $target, $post_id ) { | |
$recent_webmentions = get_transient( 'recent_webmentions' ); | |
if ( empty( $recent_webmentions ) ) { | |
$recent_webmentions = array(); | |
} | |
$result = array( | |
'target' => esc_url_raw( $target ), | |
'code' => wp_remote_retrieve_response_code( $response ) ?: 0, | |
); | |
if ( is_wp_error( $response ) ) { | |
$result['error'] = $response->get_error_message() ?: ''; | |
} | |
$recent_webmentions[] = $result; | |
set_transient( 'recent_webmentions', array_slice( $recent_webmentions, 0, 10 ) ); // Keep only the 10 most recent statuses. | |
} | |
/** | |
* Outputs the actual Webmention dashboard widget. | |
*/ | |
function webmention_dashboard_widget() { | |
$recent_webmentions = get_transient( 'recent_webmentions' ); | |
if ( empty( $recent_webmentions ) ) { | |
echo '<p>' . esc_html__( 'No recent webmentions found.', 'webmention-widget' ) . '</p>'; | |
return; | |
} | |
?> | |
<table style="width: 100%; box-sizing: border-box;"> | |
<?php | |
foreach ( $recent_webmentions as $result ) { | |
?> | |
<tr> | |
<td><a href="<?php echo esc_url( $result['target'] ); ?>"><?php echo esc_html( wp_parse_url( $result['target'], PHP_URL_HOST ) ); ?></a></td> | |
<td style="text-align: right;"><?php echo esc_html( $result['code'] ); ?></td> | |
</tr> | |
<?php if ( ! empty( $result['error'] ) ) : ?> | |
<tr> | |
<td colspan="2" style="color: rgb(170, 0, 0);"><?php echo esc_html( $result['error'] ); ?></td> | |
</tr> | |
<?php endif ?> | |
<?php | |
} | |
?> | |
</table> | |
<?php | |
} | |
} | |
new Webmention_Widget(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment