public
Created

Save Post. Check Links. WordPress Plugin.

  • Download Gist
spcl.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
<?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
);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.