| 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 blog's post/page. |
| 6 |
Version: 2.10 |
| 7 |
Author: GaMerZ |
| 8 |
Author URI: http://www.lesterchan.net |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
/* |
| 13 |
Copyright 2007 Lester Chan (email : gamerz84@hotmail.com) |
| 14 |
|
| 15 |
This program is free software; you can redistribute it and/or modify |
| 16 |
it under the terms of the GNU General Public License as published by |
| 17 |
the Free Software Foundation; either version 2 of the License, or |
| 18 |
(at your option) any later version. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with this program; if not, write to the Free Software |
| 27 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
### Create Text Domain For Translations |
| 32 |
load_plugin_textdomain('wp-print', 'wp-content/plugins/print'); |
| 33 |
|
| 34 |
|
| 35 |
### Function: Print Option Menu |
| 36 |
add_action('admin_menu', 'print_menu'); |
| 37 |
function print_menu() { |
| 38 |
if (function_exists('add_options_page')) { |
| 39 |
add_options_page(__('Print', 'wp-print'), __('Print', 'wp-print'), 'manage_options', 'print/print-options.php') ; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
### Function: Print htaccess ReWrite Rules |
| 45 |
add_filter('generate_rewrite_rules', 'print_rewrite'); |
| 46 |
function print_rewrite($wp_rewrite) { |
| 47 |
$r_rule = ''; |
| 48 |
$r_link = ''; |
| 49 |
$rewrite_rules2 = $wp_rewrite->generate_rewrite_rule($wp_rewrite->permalink_structure.'print'); |
| 50 |
array_splice($rewrite_rules2, 1); |
| 51 |
$r_rule = array_keys($rewrite_rules2); |
| 52 |
$r_rule = array_shift($r_rule); |
| 53 |
$r_rule = str_replace('/trackback', '',$r_rule); |
| 54 |
$r_link = array_values($rewrite_rules2); |
| 55 |
$r_link = array_shift($r_link); |
| 56 |
$r_link = str_replace('tb=1', 'print=1', $r_link); |
| 57 |
$print_rules = array($r_rule => $r_link, '(.+)/printpage/?$' => 'index.php?pagename='.$wp_rewrite->preg_index(1).'&print=1'); |
| 58 |
$wp_rewrite->rules = $print_rules + $wp_rewrite->rules; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
### Function: Print Public Variables |
| 63 |
add_filter('query_vars', 'print_variables'); |
| 64 |
function print_variables($public_query_vars) { |
| 65 |
$public_query_vars[] = 'print'; |
| 66 |
return $public_query_vars; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
### Function: Display Print Link |
| 71 |
function print_link($deprecated = '', $deprecated2 ='') { |
| 72 |
global $id; |
| 73 |
$using_permalink = get_option('permalink_structure'); |
| 74 |
$print_options = get_option('print_options'); |
| 75 |
$print_style = intval($print_options['print_style']); |
| 76 |
$print_text = stripslashes($print_options['post_text']); |
| 77 |
$print_icon = get_option('siteurl').'/wp-content/plugins/print/images/'.$print_options['print_icon']; |
| 78 |
$print_link = get_permalink(); |
| 79 |
$print_html = stripslashes($print_options['print_html']); |
| 80 |
if(!empty($using_permalink)) { |
| 81 |
if(is_page()) { |
| 82 |
$print_text = stripslashes($print_options['page_text']); |
| 83 |
$print_link = $print_link.'printpage/'; |
| 84 |
} else { |
| 85 |
$print_link = $print_link.'print/'; |
| 86 |
} |
| 87 |
} else { |
| 88 |
if(is_page()) { |
| 89 |
$print_text = stripslashes($print_options['page_text']); |
| 90 |
} |
| 91 |
$print_link = $print_link.'&print=1'; |
| 92 |
} |
| 93 |
unset($print_options); |
| 94 |
switch($print_style) { |
| 95 |
// Icon + Text Link |
| 96 |
case 1: |
| 97 |
echo '<img src="'.$print_icon.'" alt="'.$print_text.'" title="'.$print_text.'" border="0" /> <a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow">'.$print_text.'</a>'."\n"; |
| 98 |
break; |
| 99 |
// Icon Only |
| 100 |
case 2: |
| 101 |
echo '<a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow"><img src="'.$print_icon.'" alt="'.$print_text.'" title="'.$print_text.'" border="0" /></a>'."\n"; |
| 102 |
break; |
| 103 |
// Text Link Only |
| 104 |
case 3: |
| 105 |
echo '<a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow">'.$print_text.'</a>'."\n"; |
| 106 |
break; |
| 107 |
case 4: |
| 108 |
$print_html = str_replace("%PRINT_URL%", $print_link, $print_html); |
| 109 |
$print_html = str_replace("%PRINT_TEXT%", $print_text, $print_html); |
| 110 |
$print_html = str_replace("%PRINT_ICON_URL%", $print_icon, $print_html); |
| 111 |
echo $print_html; |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
### Function: Display Print Image Link (Deprecated) |
| 118 |
function print_link_image() { |
| 119 |
print_link(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
### Function: Print Content |
| 124 |
function print_content($display = true) { |
| 125 |
global $links_text, $link_number, $pages, $multipage, $numpages, $post; |
| 126 |
$max_url_char = 80; |
| 127 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 128 |
$content = get_the_password_form(); |
| 129 |
} else { |
| 130 |
if($multipage) { |
| 131 |
for($page = 0; $page < $numpages; $page++) { |
| 132 |
$content .= $pages[$page]; |
| 133 |
} |
| 134 |
} else { |
| 135 |
$content = $pages[0]; |
| 136 |
} |
| 137 |
$content = apply_filters('the_content', $content); |
| 138 |
$content = str_replace(']]>', ']]>', $content); |
| 139 |
if(!print_can('images')) { |
| 140 |
$content = remove_image($content); |
| 141 |
} |
| 142 |
if(print_can('links')) { |
| 143 |
preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); |
| 144 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 145 |
$link_match = $matches[0][$i]; |
| 146 |
$link_number++; |
| 147 |
$link_url = $matches[2][$i]; |
| 148 |
if(stristr($link_url, 'https://')) { |
| 149 |
$link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url; |
| 150 |
} else if( stristr($link_url, 'mailto:')) { |
| 151 |
$link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url; |
| 152 |
} else if( $link_url[0] == '#' ) { |
| 153 |
$link_url = $link_url; |
| 154 |
} else { |
| 155 |
$link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url; |
| 156 |
} |
| 157 |
$link_text = $matches[4][$i]; |
| 158 |
$content = str_replace_one($link_match, '['.$link_number."] <a href=\"$link_url\" rel=\"external\">".$link_text.'</a>', $content); |
| 159 |
if(strlen($link_url) > 100) { |
| 160 |
$link_url = chunk_split($link_url, 100, "<br />\n"); |
| 161 |
} |
| 162 |
if(preg_match('/<img(.+?)src=\"(.+?)\"(.*?)>/',$link_text)) { |
| 163 |
$links_text .= '<br />['.$link_number.'] '.__('Image', 'wp-print').': <b>'.$link_url.'</b>'; |
| 164 |
} else { |
| 165 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
if($display) { |
| 171 |
echo $content; |
| 172 |
} else { |
| 173 |
return $content; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
### Function: Print Categories |
| 179 |
function print_categories($before = '', $after = '') { |
| 180 |
$temp_cat = strip_tags(get_the_category_list(',' , $parents)); |
| 181 |
$temp_cat = explode(', ', $temp_cat); |
| 182 |
$temp_cat = implode($after.', '.$before, $temp_cat); |
| 183 |
echo $before.$temp_cat.$after; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
### Function: Print Comments Content |
| 188 |
function print_comments_content($display = true) { |
| 189 |
global $links_text, $link_number; |
| 190 |
$content = get_comment_text(); |
| 191 |
$content = apply_filters('comment_text', $content); |
| 192 |
if(!print_can('images')) { |
| 193 |
$content = remove_image($content); |
| 194 |
} |
| 195 |
if(print_can('links')) { |
| 196 |
preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches); |
| 197 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 198 |
$link_match = $matches[0][$i]; |
| 199 |
$link_number++; |
| 200 |
$link_url = $matches[2][$i]; |
| 201 |
if(stristr($link_url, 'https://')) { |
| 202 |
$link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url; |
| 203 |
} else if(stristr($link_url, 'mailto:')) { |
| 204 |
$link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url; |
| 205 |
} else if($link_url[0] == '#') { |
| 206 |
$link_url = $link_url; |
| 207 |
} else { |
| 208 |
$link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url; |
| 209 |
} |
| 210 |
$link_text = $matches[4][$i]; |
| 211 |
$content = str_replace_one($link_match, '['.$link_number."] <a href=\"$link_url\" rel=\"external\">".$link_text.'</a>', $content); |
| 212 |
if(strlen($link_url) > 100) { |
| 213 |
$link_url = chunk_split($link_url, 100, "<br />\n"); |
| 214 |
} |
| 215 |
if(preg_match('/<img(.+?)src=\"(.+?)\"(.*?)>/',$link_text)) { |
| 216 |
$links_text .= '<br />['.$link_number.'] '.__('Image', 'wp-print').': <b>'.$link_url.'</b>'; |
| 217 |
} else { |
| 218 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
if($display) { |
| 223 |
echo $content; |
| 224 |
} else { |
| 225 |
return $content; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
### Function: Print Comments |
| 231 |
function print_comments_number() { |
| 232 |
global $post; |
| 233 |
$comment_text = ''; |
| 234 |
$comment_status = $post->comment_status; |
| 235 |
if($comment_status == 'open') { |
| 236 |
$num_comments = get_comments_number(); |
| 237 |
if($num_comments == 0) { |
| 238 |
$comment_text = __('No Comments', 'wp-print'); |
| 239 |
} elseif($num_comments == 1) { |
| 240 |
$comment_text = __('1 Comment', 'wp-print'); |
| 241 |
} else { |
| 242 |
$comment_text = sprintf(__('%s Comments', 'wp-print'), $num_comments); |
| 243 |
} |
| 244 |
} else { |
| 245 |
$comment_text = __('Comments Disabled', 'wp-print'); |
| 246 |
} |
| 247 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 248 |
_e('Comments Hidden', 'wp-print'); |
| 249 |
} else { |
| 250 |
echo $comment_text; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
### Function: Print Links |
| 256 |
function print_links($text_links = '') { |
| 257 |
global $links_text; |
| 258 |
if(empty($text_links)) { |
| 259 |
$text_links = __('URLs in this post:', 'wp-print'); |
| 260 |
} |
| 261 |
if(!empty($links_text)) { |
| 262 |
echo $text_links.$links_text; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
### Function: Load WP-Print |
| 268 |
add_action('template_redirect', 'wp_print'); |
| 269 |
function wp_print() { |
| 270 |
if(intval(get_query_var('print')) == 1) { |
| 271 |
include(ABSPATH.'wp-content/plugins/print/wp-print.php'); |
| 272 |
exit; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
### Function: Add Print Comments Template |
| 278 |
function print_template_comments($file = '') { |
| 279 |
$file = ABSPATH.'wp-content/plugins/print/wp-print-comments.php'; |
| 280 |
return $file; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
### Function: Print Page Title |
| 285 |
function print_pagetitle($print_pagetitle) { |
| 286 |
return '» Print'.$print_pagetitle; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
### Function: Can Print? |
| 291 |
function print_can($type) { |
| 292 |
$print_options = get_option('print_options'); |
| 293 |
return intval($print_options[$type]); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
### Function: Remove Image From Text |
| 298 |
function remove_image($content) { |
| 299 |
$content= preg_replace('/<img(.+?)src=\"(.+?)\"(.*?)>/', '',$content); |
| 300 |
return $content; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
### Function: Replace One Time Only |
| 305 |
function str_replace_one($search, $replace, $content){ |
| 306 |
if ($pos = strpos($content, $search)) { |
| 307 |
return substr($content, 0, $pos).$replace.substr($content, $pos+strlen($search)); |
| 308 |
} else { |
| 309 |
return $content; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
### Function: Print Options |
| 315 |
add_action('activate_print/print.php', 'print_init'); |
| 316 |
function print_init() { |
| 317 |
// Delete Options First |
| 318 |
delete_option('print_options'); |
| 319 |
// Add Options |
| 320 |
$print_options = array(); |
| 321 |
$print_options['post_text'] = __('Print This Post', 'wp-print'); |
| 322 |
$print_options['page_text'] = __('Print This Page', 'wp-print'); |
| 323 |
$print_options['print_icon'] = 'print.gif'; |
| 324 |
$print_options['print_style'] = 1; |
| 325 |
$print_options['print_html'] = '<a href="%PRINT_URL%" rel="nofollow" title="%PRINT_TEXT%">%PRINT_TEXT%</a>'; |
| 326 |
$print_options['comments'] = 0; |
| 327 |
$print_options['links'] = 1; |
| 328 |
$print_options['images'] = 1; |
| 329 |
add_option('print_options', $print_options, 'Print Options'); |
| 330 |
} |
| 331 |
?> |