Skip to content

@sergejmueller /spcl.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Save Post. Check Links. WordPress Plugin.
<?php
/*
Plugin Name: Save Post. Check Links.
Description: Bei der Speicherung eines Artikels prüft das Plugin die im Text vorhandenen Verlinkungen auf ihre Erreichbarkeit bzw. Gültigkeit.
Author: Sergej M&uuml;ller
Author URI: http://wpseo.de
Plugin URI: https://plus.google.com/110569673423509816572/posts/hDtKSyEozeR
Version: 0.2
*/
/* Sicherheitsabfrage */
if ( !class_exists('WP') ) {
header('Status: 403 Forbidden');
header('HTTP/1.1 403 Forbidden');
exit();
}
/**
* Cachify
*/
final class SPCL {
/**
* Initiator der Klasse
*
* @since 0.1
* @change 0.2
*/
public static function init()
{
/* Filter */
if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) or (defined('DOING_CRON') && DOING_CRON) or (defined('DOING_AJAX') && DOING_AJAX) or (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ) {
return;
}
/* Actions */
add_action(
'save_post',
array(
__CLASS__,
'check_post_links'
)
);
add_action(
'publish_post',
array(
__CLASS__,
'check_post_links'
)
);
add_action(
'publish_page',
array(
__CLASS__,
'check_post_links'
)
);
add_action(
'admin_head-post.php',
array(
__CLASS__,
'handle_admin_notices'
)
);
}
/**
* Proxy für die Adminnotiz
*
* @since 0.2
* @change 0.2
*
*/
public static function handle_admin_notices() {
/* Leer? */
if ( empty($_GET['message']) ) {
return;
}
/* Ausführen */
add_action(
'admin_notices',
array(
__CLASS__,
'show_admin_notices'
)
);
}
/**
* Prüfung der Links
*
* @since 0.1
* @change 0.1
*
* @param intval $id ID des Beitrags
*/
public static function check_post_links($id)
{
/* Keine ID? */
if ( empty($id) ) {
return;
}
/* Post */
$post = get_post($id);
/* Leer? */
if ( empty($post) or empty($post->post_content) ) {
return;
}
/* Links suchen */
preg_match_all(
'/<a [^>]*href=["\'](http(?:.+?))["\']/i',
$post->post_content,
$out
);
/* Leer? */
if ( empty($out[1]) ) {
return;
}
/* Init */
$found = array();
/* Loopen */
foreach ( $out[1] as $url ) {
$url = esc_url($url);
$response = wp_remote_head($url);
if ( is_wp_error($response) or intval(wp_remote_retrieve_response_code($response)) >= 400 ) {
$found[] = $url;
}
}
/* Leer? */
if ( empty($found) ) {
return;
}
/* Speichern */
set_transient(
self::get_transient_hash(),
array_unique($found),
60*30
);
}
/**
* Anzeige des Resultats
*
* @since 0.1
* @change 0.1
*
*/
public static function show_admin_notices()
{
/* Init */
$hash = self::get_transient_hash();
/* Auslesen */
if ( !$found = get_transient($hash) ) {
return;
}
/* Löschen */
delete_transient($hash);
echo '<div id="message" class="updated"><p><strong>Nicht erreichbare Links</strong></p><ul>';
/* Loop */
foreach ( $found as $url ) {
echo '<li><a href="' .$url. '" target="_blank">' .$url. '</a></li>';
}
echo '</ul></div>';
}
/**
* Generierung eines Hash-Wertes
*
* @since 0.1
* @change 0.1
*
* @return string Hash-Wert
*/
private static function get_transient_hash() {
return md5(
sprintf(
'SPCL_%s_%s',
get_queried_object_id(),
wp_get_current_user()->ID
)
);
}
}
/* Fire */
add_action(
'plugins_loaded',
array(
'SPCL',
'init'
),
99
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.