| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP-Print |
| 4 |
Plugin URI: http://www.lesterchan.net/portfolio/programming.php |
| 5 |
Description: Displays A Printable Version Of Your WordPress Weblog Post. |
| 6 |
Version: 2.04 |
| 7 |
Author: GaMerZ |
| 8 |
Author URI: http://www.lesterchan.net |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
/* Copyright 2006 Lester Chan (email : gamerz84@hotmail.com) |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation; either version 2 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
### Function: Print htaccess ReWrite Rules |
| 31 |
add_filter('generate_rewrite_rules', 'print_rewrite'); |
| 32 |
function print_rewrite($wp_rewrite) { |
| 33 |
$r_rule = ''; |
| 34 |
$r_link = ''; |
| 35 |
$rewrite_rules2 = $wp_rewrite->generate_rewrite_rule($wp_rewrite->permalink_structure.'print'); |
| 36 |
array_splice($rewrite_rules2, 1); |
| 37 |
$r_rule = array_shift(array_keys($rewrite_rules2)); |
| 38 |
$r_rule = str_replace('/trackback', '',$r_rule); |
| 39 |
$r_link = array_shift(array_values($rewrite_rules2)); |
| 40 |
$r_link = str_replace('tb=1', 'print=1', $r_link); |
| 41 |
$print_rules = array($r_rule => $r_link, '(.+)/printpage/?$' => 'index.php?pagename='.$wp_rewrite->preg_index(1).'&print=1'); |
| 42 |
$wp_rewrite->rules = $print_rules + $wp_rewrite->rules; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
### Function: Print Public Variables |
| 47 |
add_filter('query_vars', 'print_variables'); |
| 48 |
function print_variables($public_query_vars) { |
| 49 |
$public_query_vars[] = 'print'; |
| 50 |
return $public_query_vars; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
### Function: Display Print Link |
| 55 |
function print_link($text_post = 'Print This Post', $text_page = 'Print This Page') { |
| 56 |
global $id; |
| 57 |
$using_permalink = get_settings('permalink_structure'); |
| 58 |
$permalink = get_permalink(); |
| 59 |
if(!empty($using_permalink)) { |
| 60 |
if(is_page()) { |
| 61 |
echo '<a href="'.$permalink.'printpage/" title="'.$text_page.'">'.$text_page.'</a>'; |
| 62 |
} else { |
| 63 |
echo '<a href="'.$permalink.'print/" title="'.$test_post.'">'.$text_post.'</a>'; |
| 64 |
} |
| 65 |
} else { |
| 66 |
echo '<a href="'.$permalink.'&print=1" title="'.$text_post.'">'.$text_post.'</a>'; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
### Function: Display Print Image Link |
| 72 |
function print_link_image() { |
| 73 |
global $id; |
| 74 |
$using_permalink = get_settings('permalink_structure'); |
| 75 |
$permalink = get_permalink(); |
| 76 |
if(file_exists(ABSPATH.'/wp-content/plugins/print/images/print.gif')) { |
| 77 |
$print_image = '<img src="'.get_settings('siteurl').'/wp-content/plugins/print/images/print.gif" alt="Print This Post/Page" />'; |
| 78 |
} else { |
| 79 |
$print_image = 'Print'; |
| 80 |
} |
| 81 |
if(!empty($using_permalink)) { |
| 82 |
if(is_page()) { |
| 83 |
echo '<a href="'.$permalink.'printpage/" title="Print This Page">'.$print_image.'</a>'; |
| 84 |
} else { |
| 85 |
echo '<a href="'.$permalink.'print/" title="Print This Post">'.$print_image.'</a>'; |
| 86 |
} |
| 87 |
} else { |
| 88 |
echo '<a href="'.$permalink.'&print=1" title="Print This Post/Page">'.$print_image.'</a>'; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
### Function: Print Content |
| 94 |
function print_content($display = true) { |
| 95 |
global $links_text, $link_number, $pages, $multipage, $numpages, $post; |
| 96 |
$max_url_char = 100; |
| 97 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 98 |
$content = get_the_password_form(); |
| 99 |
} else { |
| 100 |
if($multipage) { |
| 101 |
for($page = 0; $page < $numpages; $page++) { |
| 102 |
$content .= $pages[$page]; |
| 103 |
} |
| 104 |
} else { |
| 105 |
$content = $pages[0]; |
| 106 |
} |
| 107 |
$content = wptexturize($content); |
| 108 |
$content = convert_smilies($content); |
| 109 |
$content = convert_chars($content); |
| 110 |
$content = wpautop($content); |
| 111 |
$content = apply_filters('the_content', $content); |
| 112 |
$content = str_replace(']]>', ']]>', $content); |
| 113 |
preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); |
| 114 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 115 |
$link_match = $matches[0][$i]; |
| 116 |
$link_number++; |
| 117 |
$link_url = $matches[2][$i]; |
| 118 |
$link_url = (strtolower(substr($link_url,0,7)) != 'http://') ? get_settings('home') . $link_url : $link_url; |
| 119 |
$link_text = $matches[4][$i]; |
| 120 |
$content = str_replace($link_match, '['.$link_number."] <a href=\"$link_url\" target=\"_blank\">".$link_text.'</a>', $content); |
| 121 |
if(strlen($link_url) > $max_url_char) { |
| 122 |
$link_url = substr($link_url, 0, $max_url_char).'<br />'.substr($link_url, $max_url_char, strlen($link_url)); |
| 123 |
} |
| 124 |
if(preg_match('/<img(.+?)src=\"(.+?)\"(.*?)>/',$link_text)) { |
| 125 |
$links_text .= '<br />['.$link_number.'] '.__('Image').': <b>'.$link_url.'</b>'; |
| 126 |
} else { |
| 127 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
if($display) { |
| 132 |
echo $content; |
| 133 |
} else { |
| 134 |
return $content; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
### Function: Print Categories |
| 140 |
function print_categories($before = '', $after = '') { |
| 141 |
$temp_cat = strip_tags(get_the_category_list(',' , $parents)); |
| 142 |
$temp_cat = explode(', ', $temp_cat); |
| 143 |
$temp_cat = implode($after.', '.$before, $temp_cat); |
| 144 |
echo $before.$temp_cat.$after; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
### Function: Print Comments Content |
| 149 |
function print_comments_content($display = true) { |
| 150 |
global $links_text, $link_number; |
| 151 |
$max_url_char = 100; |
| 152 |
$content = get_comment_text(); |
| 153 |
preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); |
| 154 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 155 |
$link_match = $matches[0][$i]; |
| 156 |
$link_number++; |
| 157 |
$link_url = $matches[2][$i]; |
| 158 |
$link_url = (strtolower(substr($link_url,0,7)) != 'http://') ? get_settings('home') . $link_url : $link_url; |
| 159 |
$link_text = $matches[4][$i]; |
| 160 |
$content = str_replace($link_match, '['.$link_number."] <a href=\"$link_url\" target=\"_blank\">".$link_text.'</a>', $content); |
| 161 |
if(strlen($link_url) > $max_url_char) { |
| 162 |
$link_url = substr($link_url, 0, $max_url_char).'<br />'.substr($link_url, $max_url_char, strlen($link_url)); |
| 163 |
} |
| 164 |
if(preg_match('/<img(.+?)src=\"(.+?)\"(.*?)>/',$link_text)) { |
| 165 |
$links_text .= '<br />['.$link_number.'] '.__('Image').': <b>'.$link_url.'</b>'; |
| 166 |
} else { |
| 167 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 168 |
} |
| 169 |
} |
| 170 |
if($display) { |
| 171 |
echo $content; |
| 172 |
} else { |
| 173 |
return $content; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
### Function: Print Comments |
| 179 |
function print_comments_number() { |
| 180 |
global $post; |
| 181 |
$comment_text = ''; |
| 182 |
$comment_status = $post->comment_status; |
| 183 |
if($comment_status == 'open') { |
| 184 |
$num_comments = get_comments_number(); |
| 185 |
if($num_comments == 0) { |
| 186 |
$comment_text = __('No Comments'); |
| 187 |
} elseif($num_comments == 1) { |
| 188 |
$comment_text = __('1 Comment'); |
| 189 |
} else { |
| 190 |
$comment_text = __($num_comments.' Comments'); |
| 191 |
} |
| 192 |
} else { |
| 193 |
$comment_text = __('Comments Disabled'); |
| 194 |
} |
| 195 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 196 |
_e('Comments Hidden'); |
| 197 |
} else { |
| 198 |
echo $comment_text; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
### Function: Print Links |
| 204 |
function print_links($text_links = 'URLs in this post:') { |
| 205 |
global $links_text; |
| 206 |
if(!empty($links_text)) { |
| 207 |
echo $text_links.$links_text; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
### Function: Load WP-Print |
| 213 |
add_action('template_redirect', 'wp_print'); |
| 214 |
function wp_print() { |
| 215 |
if(intval(get_query_var('print')) == 1) { |
| 216 |
include(ABSPATH.'wp-content/plugins/print/wp-print.php'); |
| 217 |
exit; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
### Function: Add Print Comments Template |
| 223 |
function print_template_comments($file = '') { |
| 224 |
$file = ABSPATH.'wp-content/plugins/print/wp-print-comments.php'; |
| 225 |
return $file; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
### Function: Print Page Title |
| 230 |
function print_pagetitle($print_pagetitle) { |
| 231 |
return '» Print'.$print_pagetitle; |
| 232 |
} |
| 233 |
?> |