broken-link-checker
Last commit date
images
17 years ago
JSON.php
17 years ago
broken-link-checker.php
16 years ago
config-manager.php
16 years ago
core.php
16 years ago
highlighter-class.php
16 years ago
instance-classes.php
17 years ago
link-classes.php
16 years ago
readme.txt
16 years ago
uninstall.php
16 years ago
utility-class.php
16 years ago
highlighter-class.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author W-Shadow |
| 5 | * @copyright 2009 |
| 6 | * |
| 7 | * @requires blcUtility |
| 8 | */ |
| 9 | |
| 10 | class blcLinkHighlighter { |
| 11 | |
| 12 | var $links_to_remove; |
| 13 | var $broken_link_css; |
| 14 | var $current_permalink; |
| 15 | |
| 16 | function blcLinkHighlighter( $broken_link_css = '' ) { |
| 17 | if ( !empty( $broken_link_css ) ){ |
| 18 | $this->broken_link_css = $broken_link_css; |
| 19 | add_action( 'wp_head', array(&$this,'hook_wp_head') ); |
| 20 | } |
| 21 | |
| 22 | add_filter( 'the_content', array(&$this,'hook_the_content') ); |
| 23 | $this->current_permalink = ''; |
| 24 | } |
| 25 | |
| 26 | function hook_the_content($content){ |
| 27 | global $post, $wpdb; |
| 28 | if ( empty($post) ) return $content; |
| 29 | |
| 30 | //Get the post permalink - it's used to resolve relative URLs |
| 31 | $this->current_permalink = get_permalink( $post->ID ); |
| 32 | |
| 33 | $q = " |
| 34 | SELECT instances.link_text, links.* |
| 35 | |
| 36 | FROM {$wpdb->prefix}blc_instances AS instances, {$wpdb->prefix}blc_links AS links |
| 37 | |
| 38 | WHERE |
| 39 | instances.source_id = %d |
| 40 | AND instances.source_type = 'post' |
| 41 | AND instances.instance_type = 'link' |
| 42 | |
| 43 | AND instances.link_id = links.link_id |
| 44 | AND links.check_count > 0 |
| 45 | AND ( links.http_code < 200 OR links.http_code >= 400 OR links.timeout = 1 ) |
| 46 | AND links.http_code <> " . BLC_CHECKING; |
| 47 | |
| 48 | $rows = $wpdb->get_results( $wpdb->prepare( $q, $post->ID ), ARRAY_A ); |
| 49 | if( $rows ){ |
| 50 | $this->links_to_remove = array(); |
| 51 | foreach($rows as $row){ |
| 52 | $this->links_to_remove[$row['url']] = $row; |
| 53 | } |
| 54 | $content = preg_replace_callback( blcUtility::link_pattern(), array(&$this,'mark_broken_links'), $content ); |
| 55 | }; |
| 56 | |
| 57 | return $content; |
| 58 | } |
| 59 | |
| 60 | function mark_broken_links($matches){ |
| 61 | //TODO: Tooltip-style popups with more info |
| 62 | $url = blcUtility::normalize_url( html_entity_decode( $matches[3] ), $this->current_permalink ); |
| 63 | if( isset( $this->links_to_remove[$url] ) ){ |
| 64 | return $matches[1].$matches[2].$matches[3].$matches[2].' class="broken_link" '.$matches[4]. |
| 65 | $matches[5].$matches[6]; |
| 66 | } else { |
| 67 | return $matches[0]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function hook_wp_head(){ |
| 72 | echo '<style type="text/css">',$this->broken_link_css,'</style>'; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | ?> |