| 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.05 |
| 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 Option Menu |
| 31 |
add_action('admin_menu', 'print_menu'); |
| 32 |
function print_menu() { |
| 33 |
if (function_exists('add_options_page')) { |
| 34 |
add_options_page(__('Print'), __('Print'), 'manage_options', 'print/print-options.php') ; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
### Function: Print htaccess ReWrite Rules |
| 40 |
add_filter('generate_rewrite_rules', 'print_rewrite'); |
| 41 |
function print_rewrite($wp_rewrite) { |
| 42 |
$r_rule = ''; |
| 43 |
$r_link = ''; |
| 44 |
$rewrite_rules2 = $wp_rewrite->generate_rewrite_rule($wp_rewrite->permalink_structure.'print'); |
| 45 |
array_splice($rewrite_rules2, 1); |
| 46 |
$r_rule = array_keys($rewrite_rules2); |
| 47 |
$r_rule = array_shift($r_rule); |
| 48 |
$r_rule = str_replace('/trackback', '',$r_rule); |
| 49 |
$r_link = array_values($rewrite_rules2); |
| 50 |
$r_link = array_shift($r_link); |
| 51 |
$r_link = str_replace('tb=1', 'print=1', $r_link); |
| 52 |
$print_rules = array($r_rule => $r_link, '(.+)/printpage/?$' => 'index.php?pagename='.$wp_rewrite->preg_index(1).'&print=1'); |
| 53 |
$wp_rewrite->rules = $print_rules + $wp_rewrite->rules; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
### Function: Print Public Variables |
| 58 |
add_filter('query_vars', 'print_variables'); |
| 59 |
function print_variables($public_query_vars) { |
| 60 |
$public_query_vars[] = 'print'; |
| 61 |
return $public_query_vars; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
### Function: Display Print Link |
| 66 |
function print_link($text_post = 'Print This Post', $text_page = 'Print This Page') { |
| 67 |
global $id; |
| 68 |
$using_permalink = get_settings('permalink_structure'); |
| 69 |
$permalink = get_permalink(); |
| 70 |
if(!empty($using_permalink)) { |
| 71 |
if(is_page()) { |
| 72 |
echo '<a href="'.$permalink.'printpage/" title="'.$text_page.'" rel="nofollow">'.$text_page.'</a>'; |
| 73 |
} else { |
| 74 |
echo '<a href="'.$permalink.'print/" title="'.$text_post.'" rel="nofollow">'.$text_post.'</a>'; |
| 75 |
} |
| 76 |
} else { |
| 77 |
echo '<a href="'.$permalink.'&print=1" title="'.$text_post.'" rel="nofollow">'.$text_post.'</a>'; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
### Function: Display Print Image Link |
| 83 |
function print_link_image() { |
| 84 |
global $id; |
| 85 |
$using_permalink = get_settings('permalink_structure'); |
| 86 |
$permalink = get_permalink(); |
| 87 |
if(file_exists(ABSPATH.'/wp-content/plugins/print/images/print.gif')) { |
| 88 |
$print_image = '<img src="'.get_settings('siteurl').'/wp-content/plugins/print/images/print.gif" alt="Print This Post/Page" />'; |
| 89 |
} else { |
| 90 |
$print_image = 'Print'; |
| 91 |
} |
| 92 |
if(!empty($using_permalink)) { |
| 93 |
if(is_page()) { |
| 94 |
echo '<a href="'.$permalink.'printpage/" title="Print This Page" rel="nofollow">'.$print_image.'</a>'; |
| 95 |
} else { |
| 96 |
echo '<a href="'.$permalink.'print/" title="Print This Post" rel="nofollow">'.$print_image.'</a>'; |
| 97 |
} |
| 98 |
} else { |
| 99 |
echo '<a href="'.$permalink.'&print=1" title="Print This Post/Page" rel="nofollow">'.$print_image.'</a>'; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
### Function: Print Content |
| 105 |
function print_content($display = true) { |
| 106 |
global $links_text, $link_number, $pages, $multipage, $numpages, $post; |
| 107 |
$max_url_char = 80; |
| 108 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 109 |
$content = get_the_password_form(); |
| 110 |
} else { |
| 111 |
if($multipage) { |
| 112 |
for($page = 0; $page < $numpages; $page++) { |
| 113 |
$content .= $pages[$page]; |
| 114 |
} |
| 115 |
} else { |
| 116 |
$content = $pages[0]; |
| 117 |
} |
| 118 |
$content = apply_filters('the_content', $content); |
| 119 |
$content = str_replace(']]>', ']]>', $content); |
| 120 |
if(!print_can('images')) { |
| 121 |
$content = remove_image($content); |
| 122 |
} |
| 123 |
if(print_can('links')) { |
| 124 |
preg_match_all('/<a(.+\s?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); |
| 125 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 126 |
$link_match = $matches[0][$i]; |
| 127 |
$link_number++; |
| 128 |
$link_url = $matches[2][$i]; |
| 129 |
$link_url = (strtolower(substr($link_url,0,7)) != 'http://') ? get_settings('home') . $link_url : $link_url; |
| 130 |
$link_text = $matches[4][$i]; |
| 131 |
$content = str_replace_one($link_match, '['.$link_number."] <a href=\"$link_url\" rel=\"external\">".$link_text.'</a>', $content); |
| 132 |
if(strlen($link_url) > 100) { |
| 133 |
$link_url = chunk_split($link_url, 100, "<br />\n"); |
| 134 |
} |
| 135 |
if(preg_match('/<img(.+?)src=\"(.+?)\"(.*?)>/',$link_text)) { |
| 136 |
$links_text .= '<br />['.$link_number.'] '.__('Image').': <b>'.$link_url.'</b>'; |
| 137 |
} else { |
| 138 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
if($display) { |
| 144 |
echo $content; |
| 145 |
} else { |
| 146 |
return $content; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
### Function: Print Categories |
| 152 |
function print_categories($before = '', $after = '') { |
| 153 |
$temp_cat = strip_tags(get_the_category_list(',' , $parents)); |
| 154 |
$temp_cat = explode(', ', $temp_cat); |
| 155 |
$temp_cat = implode($after.', '.$before, $temp_cat); |
| 156 |
echo $before.$temp_cat.$after; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
### Function: Print Comments Content |
| 161 |
function print_comments_content($display = true) { |
| 162 |
global $links_text, $link_number; |
| 163 |
$content = get_comment_text(); |
| 164 |
$content = apply_filters('comment_text', $content); |
| 165 |
if(!print_can('images')) { |
| 166 |
$content = remove_image($content); |
| 167 |
} |
| 168 |
if(print_can('links')) { |
| 169 |
preg_match_all('/<a(.+\s?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); |
| 170 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 171 |
$link_match = $matches[0][$i]; |
| 172 |
$link_number++; |
| 173 |
$link_url = $matches[2][$i]; |
| 174 |
$link_url = (strtolower(substr($link_url,0,7)) != 'http://') ? get_settings('home') . $link_url : $link_url; |
| 175 |
$link_text = $matches[4][$i]; |
| 176 |
$content = str_replace_one($link_match, '['.$link_number."] <a href=\"$link_url\" rel=\"external\">".$link_text.'</a>', $content); |
| 177 |
if(strlen($link_url) > 100) { |
| 178 |
$link_url = chunk_split($link_url, 100, "<br />\n"); |
| 179 |
} |
| 180 |
if(preg_match('/<img(.+?)src=\"(.+?)\"(.*?)>/',$link_text)) { |
| 181 |
$links_text .= '<br />['.$link_number.'] '.__('Image').': <b>'.$link_url.'</b>'; |
| 182 |
} else { |
| 183 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
if($display) { |
| 188 |
echo $content; |
| 189 |
} else { |
| 190 |
return $content; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
### Function: Print Comments |
| 196 |
function print_comments_number() { |
| 197 |
global $post; |
| 198 |
$comment_text = ''; |
| 199 |
$comment_status = $post->comment_status; |
| 200 |
if($comment_status == 'open') { |
| 201 |
$num_comments = get_comments_number(); |
| 202 |
if($num_comments == 0) { |
| 203 |
$comment_text = __('No Comments'); |
| 204 |
} elseif($num_comments == 1) { |
| 205 |
$comment_text = __('1 Comment'); |
| 206 |
} else { |
| 207 |
$comment_text = __($num_comments.' Comments'); |
| 208 |
} |
| 209 |
} else { |
| 210 |
$comment_text = __('Comments Disabled'); |
| 211 |
} |
| 212 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 213 |
_e('Comments Hidden'); |
| 214 |
} else { |
| 215 |
echo $comment_text; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
### Function: Print Links |
| 221 |
function print_links($text_links = 'URLs in this post:') { |
| 222 |
global $links_text; |
| 223 |
if(!empty($links_text)) { |
| 224 |
echo $text_links.$links_text; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
### Function: Load WP-Print |
| 230 |
add_action('template_redirect', 'wp_print'); |
| 231 |
function wp_print() { |
| 232 |
if(intval(get_query_var('print')) == 1) { |
| 233 |
include(ABSPATH.'wp-content/plugins/print/wp-print.php'); |
| 234 |
exit; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
### Function: Add Print Comments Template |
| 240 |
function print_template_comments($file = '') { |
| 241 |
$file = ABSPATH.'wp-content/plugins/print/wp-print-comments.php'; |
| 242 |
return $file; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
### Function: Print Page Title |
| 247 |
function print_pagetitle($print_pagetitle) { |
| 248 |
return '» Print'.$print_pagetitle; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
### Function: Can Print? |
| 253 |
function print_can($type) { |
| 254 |
$print_options = get_settings('print_options'); |
| 255 |
return intval($print_options[$type]); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
### Function: Remove Image From Text |
| 260 |
function remove_image($content) { |
| 261 |
$content= preg_replace('/<img(.+?)src=\"(.+?)\"(.*?)>/', '',$content); |
| 262 |
return $content; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
### Function: Replace One Time Only |
| 267 |
function str_replace_one($search, $replace, $content){ |
| 268 |
if ($pos = strpos($content, $search)) { |
| 269 |
return substr($content, 0, $pos).$replace.substr($content, $pos+strlen($search)); |
| 270 |
} else { |
| 271 |
return $content; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
### Function: Print Options |
| 277 |
add_action('activate_print/print.php', 'print_init'); |
| 278 |
function print_init() { |
| 279 |
// Add Options |
| 280 |
$print_options = array(); |
| 281 |
$print_options['comments'] = 0; |
| 282 |
$print_options['links'] = 1; |
| 283 |
$print_options['images'] = 1; |
| 284 |
add_option('print_options', $print_options, 'Print Options'); |
| 285 |
} |
| 286 |
?> |