| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Smart Excerpt |
|---|
| 4 | Version: 0.1 alpha |
|---|
| 5 | Plugin URI: http://www.semiologic.com/projects/smart-excerpt/ |
|---|
| 6 | Description: Enhances WordPress' default excerpt generator |
|---|
| 7 | Author: Denis de Bernardy |
|---|
| 8 | Author URI: http://www.semiologic.com |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | /* |
|---|
| 12 | * Copyright, license and disclaimer |
|---|
| 13 | * --------------------------------- |
|---|
| 14 | * Except where otherwise noted, this software is: |
|---|
| 15 | * - Copyright 2005, Denis de Bernardy |
|---|
| 16 | * - Licensed under the terms of the CC/GNU GPL v.2.0 |
|---|
| 17 | * http://creativecommons.org/licenses/GPL/2.0/ |
|---|
| 18 | * - Provided as is, with NO WARRANTY whatsoever |
|---|
| 19 | **/ |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /* |
|---|
| 24 | * sem_fancy_excerpt() |
|---|
| 25 | * ------------------- |
|---|
| 26 | * Creates a fancy default excerpt |
|---|
| 27 | **/ |
|---|
| 28 | |
|---|
| 29 | function sem_fancy_excerpt( $excerpt ) |
|---|
| 30 | { |
|---|
| 31 | global $post; |
|---|
| 32 | |
|---|
| 33 | $excerpt_length = 30; |
|---|
| 34 | |
|---|
| 35 | if ( $excerpt == '' ) |
|---|
| 36 | { |
|---|
| 37 | $excerpt = $post->post_content; |
|---|
| 38 | $excerpt = apply_filters( 'the_content', $excerpt ); |
|---|
| 39 | $excerpt = strip_tags( $excerpt ); |
|---|
| 40 | $excerpt = preg_replace( "/^\W*(\w+(\W+\w+){".($excerpt_length-1)."}[^\.]*\.)[^$]+/", "$1 (...)", $excerpt ); |
|---|
| 41 | } |
|---|
| 42 | return $excerpt; |
|---|
| 43 | } // end sem_fancy_excerpt() |
|---|
| 44 | |
|---|
| 45 | remove_filter('get_the_excerpt', 'wp_trim_excerpt'); |
|---|
| 46 | add_filter('get_the_excerpt', 'sem_fancy_excerpt'); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | /* |
|---|
| 51 | * sem_backlink_excerpt() |
|---|
| 52 | * ------------------- |
|---|
| 53 | * Creates a fancy excerpt for a backlink |
|---|
| 54 | **/ |
|---|
| 55 | |
|---|
| 56 | function sem_backlink_excerpt( $text, $link ) |
|---|
| 57 | { |
|---|
| 58 | |
|---|
| 59 | // clean up the text |
|---|
| 60 | |
|---|
| 61 | $text = preg_replace( "/<!DOC/", "<DOC", $text ); // strip_tags bug |
|---|
| 62 | $text = preg_replace( "/[\s\r\n\t]+/", " ", $text ); // normalize spaces |
|---|
| 63 | $text = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $text ); |
|---|
| 64 | $text = strip_tags( $text, "<title><a>" ); // just keep the tags we need |
|---|
| 65 | |
|---|
| 66 | //echo Markdown( $text ); |
|---|
| 67 | |
|---|
| 68 | // split into paragraphs |
|---|
| 69 | |
|---|
| 70 | $p = explode( "\n\n", $text ); |
|---|
| 71 | |
|---|
| 72 | // fetch the title |
|---|
| 73 | |
|---|
| 74 | $title = preg_replace( "/^[^<]*<[^>]*title[^>]*>([^<]*)<\/[^>]*title[^>]*>[^$]*/", "$1", $p[0] ); |
|---|
| 75 | |
|---|
| 76 | // fetch the first paragraph with the link |
|---|
| 77 | |
|---|
| 78 | $sem_regexp_pb = "/(\\/|\\\|\*|\?|\+|\.|\^|\\$|\(|\)|\[|\]|\||\{|\})/"; |
|---|
| 79 | $sem_regexp_fix = "\\\\$1"; |
|---|
| 80 | $link = preg_replace( $sem_regexp_pb, $sem_regexp_fix, $link ); |
|---|
| 81 | |
|---|
| 82 | for ( $i = 0; $p[$i] && !$excerpt; $i++ ) |
|---|
| 83 | { |
|---|
| 84 | if ( preg_match( "/<a[^>]+".$link."[^>]*>/", $p[$i] ) ) |
|---|
| 85 | { |
|---|
| 86 | $context = preg_replace( "/.*<a[^>]+".$link."[^>]*>([^>]+)<\/a>.*/", "$1", $p[$i] ); |
|---|
| 87 | $excerpt = trim( strip_tags( $p[$i] ) ); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // return the result |
|---|
| 92 | return array( $title, $context, $excerpt ); |
|---|
| 93 | } // end sem_backlink_excerpt () |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | function sem_backlink_excerpt_test() |
|---|
| 99 | { |
|---|
| 100 | list ( $title, $context, $excerpt ) = sem_backlink_excerpt( '<title>excerpt title</title> |
|---|
| 101 | <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do |
|---|
| 102 | eiusmod tempor incididunt ut labore et dolore <em>magna</em> aliqua. Ut enim |
|---|
| 103 | ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut |
|---|
| 104 | aliquip ex ea <strong>commodo</strong> consequat. Duis aute irure dolor in |
|---|
| 105 | reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla |
|---|
| 106 | pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa |
|---|
| 107 | qui officia deserunt mollit anim id est laborum.</p> |
|---|
| 108 | |
|---|
| 109 | <p>sss</p> |
|---|
| 110 | |
|---|
| 111 | <h1>test</h1> |
|---|
| 112 | |
|---|
| 113 | <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do |
|---|
| 114 | eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad |
|---|
| 115 | minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip |
|---|
| 116 | ex ea <a href="http://www.semiologic.com">commodo</a> consequat. Duis aute irure dolor in reprehenderit in |
|---|
| 117 | voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur |
|---|
| 118 | sint occaecat cupidatat non proident, sunt in culpa qui officia |
|---|
| 119 | deserunt mollit anim id est laborum. |
|---|
| 120 | <ul> |
|---|
| 121 | <li>unordered list test |
|---|
| 122 | <li>unordered list test |
|---|
| 123 | <li>ordered list test |
|---|
| 124 | <li>ordered list test |
|---|
| 125 | </ul> |
|---|
| 126 | blockquote test</p>', 'http://www.semiologic.com' ); |
|---|
| 127 | |
|---|
| 128 | echo "<h1>$title</h1>\n<h2>$context</h2>\n".Markdown($excerpt); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | add_action( 'wp_footer', 'sem_backlink_excerpt_test' ); |
|---|
| 132 | |
|---|
| 133 | ?> |
|---|