| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP-Print |
| 4 |
Plugin URI: http://lesterchan.net/portfolio/programming/php/ |
| 5 |
Description: Displays a printable version of your WordPress blog's post/page. |
| 6 |
Version: 2.30 |
| 7 |
Author: Lester 'GaMerZ' Chan |
| 8 |
Author URI: http://lesterchan.net |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
/* |
| 13 |
Copyright 2008 Lester Chan (email : lesterchan@gmail.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 |
add_action('init', 'print_textdomain'); |
| 33 |
function print_textdomain() { |
| 34 |
load_plugin_textdomain('wp-print', 'wp-content/plugins/wp-print'); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
### Function: Print Option Menu |
| 39 |
add_action('admin_menu', 'print_menu'); |
| 40 |
function print_menu() { |
| 41 |
if (function_exists('add_options_page')) { |
| 42 |
add_options_page(__('Print', 'wp-print'), __('Print', 'wp-print'), 'manage_options', 'wp-print/print-options.php') ; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
### Function: Print htaccess ReWrite Rules |
| 48 |
add_filter('generate_rewrite_rules', 'print_rewrite'); |
| 49 |
function print_rewrite($wp_rewrite) { |
| 50 |
// Print Rules For Posts |
| 51 |
$r_rule = ''; |
| 52 |
$r_link = ''; |
| 53 |
$print_link = get_permalink(); |
| 54 |
if(substr($print_link, -1, 1) != '/' && substr($wp_rewrite->permalink_structure, -1, 1) != '/') { |
| 55 |
$print_link_text = '/print'; |
| 56 |
} else { |
| 57 |
$print_link_text = 'print'; |
| 58 |
} |
| 59 |
$rewrite_rules = $wp_rewrite->generate_rewrite_rule($wp_rewrite->permalink_structure.$print_link_text, EP_PERMALINK); |
| 60 |
$rewrite_rules = array_slice($rewrite_rules, 4, 1); |
| 61 |
$r_rule = array_keys($rewrite_rules); |
| 62 |
$r_rule = array_shift($r_rule); |
| 63 |
$r_rule = str_replace('/trackback', '',$r_rule); |
| 64 |
$r_link = array_values($rewrite_rules); |
| 65 |
$r_link = array_shift($r_link); |
| 66 |
$r_link = str_replace('tb=1', 'print=1', $r_link); |
| 67 |
$wp_rewrite->rules = array_merge(array($r_rule => $r_link), $wp_rewrite->rules); |
| 68 |
// Print Rules For Pages |
| 69 |
$page_uris = $wp_rewrite->page_uri_index(); |
| 70 |
$uris = $page_uris[0]; |
| 71 |
if(is_array($uris)) { |
| 72 |
$print_page_rules = array(); |
| 73 |
foreach ($uris as $uri => $pagename) { |
| 74 |
$wp_rewrite->add_rewrite_tag('%pagename%', "($uri)", 'pagename='); |
| 75 |
$rewrite_rules = $wp_rewrite->generate_rewrite_rules($wp_rewrite->get_page_permastruct().'/printpage', EP_PAGES); |
| 76 |
$rewrite_rules = array_slice($rewrite_rules, 4, 1); |
| 77 |
$r_rule = array_keys($rewrite_rules); |
| 78 |
$r_rule = array_shift($r_rule); |
| 79 |
$r_rule = str_replace('/trackback', '',$r_rule); |
| 80 |
$r_link = array_values($rewrite_rules); |
| 81 |
$r_link = array_shift($r_link); |
| 82 |
$r_link = str_replace('tb=1', 'print=1', $r_link); |
| 83 |
$print_page_rules = array_merge($print_page_rules, array($r_rule => $r_link)); |
| 84 |
} |
| 85 |
$wp_rewrite->rules = array_merge($print_page_rules, $wp_rewrite->rules); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
### Function: Print Public Variables |
| 91 |
add_filter('query_vars', 'print_variables'); |
| 92 |
function print_variables($public_query_vars) { |
| 93 |
$public_query_vars[] = 'print'; |
| 94 |
$public_query_vars[] = 'printpage'; |
| 95 |
return $public_query_vars; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
### Function: Display Print Link |
| 100 |
function print_link($print_post_text = '', $print_page_text = '', $echo = true) { |
| 101 |
global $id; |
| 102 |
if (function_exists('polyglot_get_lang')){ |
| 103 |
global $polyglot_settings; |
| 104 |
$polyglot_append = $polyglot_settings['uri_helpers']['lang_view'].'/'.polyglot_get_lang().'/'; |
| 105 |
} |
| 106 |
$output = ''; |
| 107 |
$using_permalink = get_option('permalink_structure'); |
| 108 |
$print_options = get_option('print_options'); |
| 109 |
$print_style = intval($print_options['print_style']); |
| 110 |
if(empty($print_post_text)) { |
| 111 |
$print_text = stripslashes($print_options['post_text']); |
| 112 |
} else { |
| 113 |
$print_text = $print_post_text; |
| 114 |
} |
| 115 |
$print_icon = get_option('siteurl').'/wp-content/plugins/wp-print/images/'.$print_options['print_icon']; |
| 116 |
$print_link = get_permalink(); |
| 117 |
$print_html = stripslashes($print_options['print_html']); |
| 118 |
// Fix For Static Page |
| 119 |
if(get_option('show_on_front') == 'page' && is_page()) { |
| 120 |
if(intval(get_option('page_on_front')) > 0) { |
| 121 |
$print_link = _get_page_link(); |
| 122 |
} |
| 123 |
} |
| 124 |
if(!empty($using_permalink)) { |
| 125 |
if(substr($print_link, -1, 1) != '/') { |
| 126 |
$print_link = $print_link.'/'; |
| 127 |
} |
| 128 |
if(is_page()) { |
| 129 |
if(empty($print_page_text)) { |
| 130 |
$print_text = stripslashes($print_options['page_text']); |
| 131 |
} else { |
| 132 |
$print_text = $print_page_text; |
| 133 |
} |
| 134 |
$print_link = $print_link.'printpage/'.$polyglot_append; |
| 135 |
} else { |
| 136 |
$print_link = $print_link.'print/'.$polyglot_append; |
| 137 |
} |
| 138 |
} else { |
| 139 |
if(is_page()) { |
| 140 |
if(empty($print_page_text)) { |
| 141 |
$print_text = stripslashes($print_options['page_text']); |
| 142 |
} else { |
| 143 |
$print_text = $print_page_text; |
| 144 |
} |
| 145 |
} |
| 146 |
$print_link = $print_link.'&print=1'; |
| 147 |
} |
| 148 |
unset($print_options); |
| 149 |
switch($print_style) { |
| 150 |
// Icon + Text Link |
| 151 |
case 1: |
| 152 |
$output = '<a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow"><img class="WP-PrintIcon" src="'.$print_icon.'" alt="'.$print_text.'" title="'.$print_text.'" style="border: 0px;" /></a> <a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow">'.$print_text.'</a>'; |
| 153 |
break; |
| 154 |
// Icon Only |
| 155 |
case 2: |
| 156 |
$output = '<a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow"><img class="WP-PrintIcon" src="'.$print_icon.'" alt="'.$print_text.'" title="'.$print_text.'" style="border: 0px;" /></a>'; |
| 157 |
break; |
| 158 |
// Text Link Only |
| 159 |
case 3: |
| 160 |
$output = '<a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow">'.$print_text.'</a>'; |
| 161 |
break; |
| 162 |
case 4: |
| 163 |
$print_html = str_replace("%PRINT_URL%", $print_link, $print_html); |
| 164 |
$print_html = str_replace("%PRINT_TEXT%", $print_text, $print_html); |
| 165 |
$print_html = str_replace("%PRINT_ICON_URL%", $print_icon, $print_html); |
| 166 |
$output = $print_html; |
| 167 |
break; |
| 168 |
} |
| 169 |
if($echo) { |
| 170 |
echo $output."\n"; |
| 171 |
} else { |
| 172 |
return $output; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
### Function: Short Code For Inserting Prink Links Into Posts/Pages |
| 178 |
add_shortcode('print_link', 'print_link_shortcode'); |
| 179 |
function print_link_shortcode($atts) { |
| 180 |
if(!is_feed()) { |
| 181 |
return print_link('', '', false); |
| 182 |
} else { |
| 183 |
return __('Note: There is a print link embedded within this post, please visit this post to print it.', 'wp-print'); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
### Function: Print Content |
| 189 |
function print_content($display = true) { |
| 190 |
global $links_text, $link_number, $max_link_number, $matched_links, $pages, $multipage, $numpages, $post; |
| 191 |
$max_url_char = 80; |
| 192 |
if (!isset($matched_links)) { |
| 193 |
$matched_links = array(); |
| 194 |
} |
| 195 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 196 |
$content = get_the_password_form(); |
| 197 |
} else { |
| 198 |
if($multipage) { |
| 199 |
for($page = 0; $page < $numpages; $page++) { |
| 200 |
$content .= $pages[$page]; |
| 201 |
} |
| 202 |
} else { |
| 203 |
$content = $pages[0]; |
| 204 |
} |
| 205 |
$content = apply_filters('the_content', $content); |
| 206 |
$content = str_replace(']]>', ']]>', $content); |
| 207 |
if(!print_can('images')) { |
| 208 |
$content = remove_image($content); |
| 209 |
} |
| 210 |
if(!print_can('videos')) { |
| 211 |
$content = remove_video($content); |
| 212 |
} |
| 213 |
if(print_can('links')) { |
| 214 |
preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches); |
| 215 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 216 |
$link_match = $matches[0][$i]; |
| 217 |
$link_url = $matches[2][$i]; |
| 218 |
if(stristr($link_url, 'https://')) { |
| 219 |
$link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url; |
| 220 |
} else if( stristr($link_url, 'mailto:')) { |
| 221 |
$link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url; |
| 222 |
} else if( $link_url[0] == '#' ) { |
| 223 |
$link_url = $link_url; |
| 224 |
} else { |
| 225 |
$link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url; |
| 226 |
} |
| 227 |
$link_text = $matches[4][$i];+ |
| 228 |
$new_link = true; |
| 229 |
$link_url_hash = md5($link_url); |
| 230 |
if (!isset($matched_links[$link_url_hash])) { |
| 231 |
$link_number = ++$max_link_number; |
| 232 |
$matched_links[$link_url_hash] = $link_number; |
| 233 |
} else { |
| 234 |
$new_link = false; |
| 235 |
$link_number = $matched_links[$link_url_hash]; |
| 236 |
} |
| 237 |
$content = str_replace_one($link_match, '['.$link_number."] <a href=\"$link_url\" rel=\"external\">".$link_text.'</a>', $content); |
| 238 |
if ($new_link) { |
| 239 |
if(strlen($link_url) > 100) { |
| 240 |
$link_url = chunk_split($link_url, 100, "<br />\n"); |
| 241 |
} |
| 242 |
if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) { |
| 243 |
$links_text .= '<br />['.$link_number.'] '.__('Image', 'wp-print').': <b>'.$link_url.'</b>'; |
| 244 |
} else { |
| 245 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
if($display) { |
| 252 |
echo $content; |
| 253 |
} else { |
| 254 |
return $content; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
### Function: Print Categories |
| 260 |
function print_categories($before = '', $after = '') { |
| 261 |
$temp_cat = strip_tags(get_the_category_list(',' , $parents)); |
| 262 |
$temp_cat = explode(', ', $temp_cat); |
| 263 |
$temp_cat = implode($after.', '.$before, $temp_cat); |
| 264 |
echo $before.$temp_cat.$after; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
### Function: Print Comments Content |
| 269 |
function print_comments_content($display = true) { |
| 270 |
global $links_text, $link_number, $max_link_number, $matched_links; |
| 271 |
if (!isset($matched_links)) { |
| 272 |
$matched_links = array(); |
| 273 |
} |
| 274 |
$content = get_comment_text(); |
| 275 |
$content = apply_filters('comment_text', $content); |
| 276 |
if(!print_can('images')) { |
| 277 |
$content = remove_image($content); |
| 278 |
} |
| 279 |
if(!print_can('videos')) { |
| 280 |
$content = remove_video($content); |
| 281 |
} |
| 282 |
if(print_can('links')) { |
| 283 |
preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches); |
| 284 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 285 |
$link_match = $matches[0][$i]; |
| 286 |
$link_url = $matches[2][$i]; |
| 287 |
if(stristr($link_url, 'https://')) { |
| 288 |
$link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url; |
| 289 |
} else if(stristr($link_url, 'mailto:')) { |
| 290 |
$link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url; |
| 291 |
} else if($link_url[0] == '#') { |
| 292 |
$link_url = $link_url; |
| 293 |
} else { |
| 294 |
$link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url; |
| 295 |
} |
| 296 |
$new_link = true; |
| 297 |
$link_url_hash = md5($link_url); |
| 298 |
if (!isset($matched_links[$link_url_hash])) { |
| 299 |
$link_number = ++$max_link_number; |
| 300 |
$matched_links[$link_url_hash] = $link_number; |
| 301 |
} else { |
| 302 |
$new_link = false; |
| 303 |
$link_number = $matched_links[$link_url_hash]; |
| 304 |
} |
| 305 |
$content = str_replace_one($link_match, '['.$link_number."] <a href=\"$link_url\" rel=\"external\">".$link_text.'</a>', $content); |
| 306 |
if ($new_link) { |
| 307 |
if(strlen($link_url) > 100) { |
| 308 |
$link_url = chunk_split($link_url, 100, "<br />\n"); |
| 309 |
} |
| 310 |
if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) { |
| 311 |
$links_text .= '<br />['.$link_number.'] '.__('Image', 'wp-print').': <b>'.$link_url.'</b>'; |
| 312 |
} else { |
| 313 |
$links_text .= '<br />['.$link_number.'] '.$link_text.': <b>'.$link_url.'</b>'; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
if($display) { |
| 319 |
echo $content; |
| 320 |
} else { |
| 321 |
return $content; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
### Function: Print Comments |
| 327 |
function print_comments_number() { |
| 328 |
global $post; |
| 329 |
$comment_text = ''; |
| 330 |
$comment_status = $post->comment_status; |
| 331 |
if($comment_status == 'open') { |
| 332 |
$num_comments = get_comments_number(); |
| 333 |
if($num_comments == 0) { |
| 334 |
$comment_text = __('No Comments', 'wp-print'); |
| 335 |
} elseif($num_comments == 1) { |
| 336 |
$comment_text = __('1 Comment', 'wp-print'); |
| 337 |
} else { |
| 338 |
$comment_text = sprintf(__('%s Comments', 'wp-print'), $num_comments); |
| 339 |
} |
| 340 |
} else { |
| 341 |
$comment_text = __('Comments Disabled', 'wp-print'); |
| 342 |
} |
| 343 |
if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) { |
| 344 |
_e('Comments Hidden', 'wp-print'); |
| 345 |
} else { |
| 346 |
echo $comment_text; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
### Function: Print Links |
| 352 |
function print_links($text_links = '') { |
| 353 |
global $links_text; |
| 354 |
if(empty($text_links)) { |
| 355 |
$text_links = __('URLs in this post:', 'wp-print'); |
| 356 |
} |
| 357 |
if(!empty($links_text)) { |
| 358 |
echo $text_links.$links_text; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
### Function: Load WP-Print |
| 364 |
add_action('template_redirect', 'wp_print'); |
| 365 |
function wp_print() { |
| 366 |
if(intval(get_query_var('print')) == 1 || intval(get_query_var('printpage')) == 1) { |
| 367 |
include(ABSPATH.'wp-content/plugins/wp-print/print-posts.php'); |
| 368 |
exit; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
### Function: Add Print Comments Template |
| 374 |
function print_template_comments($file = '') { |
| 375 |
$file = ABSPATH.'wp-content/plugins/wp-print/print-comments.php'; |
| 376 |
return $file; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
### Function: Print Page Title |
| 381 |
function print_pagetitle($print_pagetitle) { |
| 382 |
return '» Print'.$print_pagetitle; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
### Function: Can Print? |
| 387 |
function print_can($type) { |
| 388 |
$print_options = get_option('print_options'); |
| 389 |
return intval($print_options[$type]); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
### Function: Remove Image From Text |
| 394 |
function remove_image($content) { |
| 395 |
$content= preg_replace('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/', '',$content); |
| 396 |
return $content; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
### Function: Remove Video From Text |
| 401 |
function remove_video($content) { |
| 402 |
$content= preg_replace('/<object[^>]*?>.*?<\/object>/', '',$content); |
| 403 |
return $content; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
### Function: Replace One Time Only |
| 408 |
function str_replace_one($search, $replace, $content){ |
| 409 |
if ($pos = strpos($content, $search)) { |
| 410 |
return substr($content, 0, $pos).$replace.substr($content, $pos+strlen($search)); |
| 411 |
} else { |
| 412 |
return $content; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
### Function: Print Options |
| 418 |
add_action('activate_wp-print/wp-print.php', 'print_init'); |
| 419 |
function print_init() { |
| 420 |
// Add Options |
| 421 |
$print_options = array(); |
| 422 |
$print_options['post_text'] = __('Print This Post', 'wp-print'); |
| 423 |
$print_options['page_text'] = __('Print This Page', 'wp-print'); |
| 424 |
$print_options['print_icon'] = 'print.gif'; |
| 425 |
$print_options['print_style'] = 1; |
| 426 |
$print_options['print_html'] = '<a href="%PRINT_URL%" rel="nofollow" title="%PRINT_TEXT%">%PRINT_TEXT%</a>'; |
| 427 |
$print_options['comments'] = 0; |
| 428 |
$print_options['links'] = 1; |
| 429 |
$print_options['images'] = 1; |
| 430 |
$print_options['videos'] = 0; |
| 431 |
$print_options['disclaimer'] = sprintf(__('Copyright © %s %s. All rights reserved.', 'wp-print'), date('Y'), get_option('blogname')); |
| 432 |
$print_options['text_direction'] = 'ltr'; |
| 433 |
add_option('print_options', $print_options, 'Print Options'); |
| 434 |
} |
| 435 |
?> |