| 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.55 |
| 7 |
Author: Lester 'GaMerZ' Chan |
| 8 |
Author URI: http://lesterchan.net |
| 9 |
Text Domain: wp-print |
| 10 |
*/ |
| 11 |
|
| 12 |
|
| 13 |
/* |
| 14 |
Copyright 2014 Lester Chan (email : lesterchan@gmail.com) |
| 15 |
|
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License as published by |
| 18 |
the Free Software Foundation; either version 2 of the License, or |
| 19 |
(at your option) any later version. |
| 20 |
|
| 21 |
This program is distributed in the hope that it will be useful, |
| 22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
GNU General Public License for more details. |
| 25 |
|
| 26 |
You should have received a copy of the GNU General Public License |
| 27 |
along with this program; if not, write to the Free Software |
| 28 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 |
*/ |
| 30 |
|
| 31 |
|
| 32 |
### Create Text Domain For Translations |
| 33 |
add_action( 'plugins_loaded', 'print_textdomain' ); |
| 34 |
function print_textdomain() { |
| 35 |
load_plugin_textdomain( 'wp-print', false, dirname( plugin_basename( __FILE__ ) ) ); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
### Function: Print Option Menu |
| 40 |
add_action('admin_menu', 'print_menu'); |
| 41 |
function print_menu() { |
| 42 |
add_options_page(__('Print', 'wp-print'), __('Print', 'wp-print'), 'manage_options', 'wp-print/print-options.php') ; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
### Function: Add htaccess Rewrite Endpoint - this handles all the rules |
| 47 |
add_action( 'init', 'wp_print_endpoint' ); |
| 48 |
function wp_print_endpoint() { |
| 49 |
add_rewrite_endpoint( 'print', EP_PERMALINK | EP_PAGES ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
### Function: Print Public Variables |
| 54 |
add_filter('query_vars', 'print_variables'); |
| 55 |
function print_variables($public_query_vars) { |
| 56 |
$public_query_vars[] = 'print'; |
| 57 |
return $public_query_vars; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
### Function: Display Print Link |
| 62 |
function print_link($print_post_text = '', $print_page_text = '', $echo = true) { |
| 63 |
$polyglot_append = ''; |
| 64 |
if (function_exists('polyglot_get_lang')){ |
| 65 |
global $polyglot_settings; |
| 66 |
$polyglot_append = $polyglot_settings['uri_helpers']['lang_view'].'/'.polyglot_get_lang().'/'; |
| 67 |
} |
| 68 |
$output = ''; |
| 69 |
$using_permalink = get_option('permalink_structure'); |
| 70 |
$print_options = get_option('print_options'); |
| 71 |
$print_style = intval($print_options['print_style']); |
| 72 |
if(empty($print_post_text)) { |
| 73 |
$print_text = stripslashes($print_options['post_text']); |
| 74 |
} else { |
| 75 |
$print_text = $print_post_text; |
| 76 |
} |
| 77 |
$print_icon = plugins_url('wp-print/images/'.$print_options['print_icon']); |
| 78 |
$print_link = get_permalink(); |
| 79 |
$print_html = stripslashes($print_options['print_html']); |
| 80 |
// Fix For Static Page |
| 81 |
if(get_option('show_on_front') == 'page' && is_page()) { |
| 82 |
if(intval(get_option('page_on_front')) > 0) { |
| 83 |
$print_link = _get_page_link(); |
| 84 |
} |
| 85 |
} |
| 86 |
if(!empty($using_permalink)) { |
| 87 |
if(substr($print_link, -1, 1) != '/') { |
| 88 |
$print_link = $print_link.'/'; |
| 89 |
} |
| 90 |
if(is_page()) { |
| 91 |
if(empty($print_page_text)) { |
| 92 |
$print_text = stripslashes($print_options['page_text']); |
| 93 |
} else { |
| 94 |
$print_text = $print_page_text; |
| 95 |
} |
| 96 |
} |
| 97 |
$print_link = $print_link.'print/'.$polyglot_append; |
| 98 |
} else { |
| 99 |
if(is_page()) { |
| 100 |
if(empty($print_page_text)) { |
| 101 |
$print_text = stripslashes($print_options['page_text']); |
| 102 |
} else { |
| 103 |
$print_text = $print_page_text; |
| 104 |
} |
| 105 |
} |
| 106 |
$print_link = $print_link.'&print=1'; |
| 107 |
} |
| 108 |
unset($print_options); |
| 109 |
switch($print_style) { |
| 110 |
// Icon + Text Link |
| 111 |
case 1: |
| 112 |
$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>'; |
| 113 |
break; |
| 114 |
// Icon Only |
| 115 |
case 2: |
| 116 |
$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>'; |
| 117 |
break; |
| 118 |
// Text Link Only |
| 119 |
case 3: |
| 120 |
$output = '<a href="'.$print_link.'" title="'.$print_text.'" rel="nofollow">'.$print_text.'</a>'; |
| 121 |
break; |
| 122 |
case 4: |
| 123 |
$print_html = str_replace("%PRINT_URL%", $print_link, $print_html); |
| 124 |
$print_html = str_replace("%PRINT_TEXT%", $print_text, $print_html); |
| 125 |
$print_html = str_replace("%PRINT_ICON_URL%", $print_icon, $print_html); |
| 126 |
$output = $print_html; |
| 127 |
break; |
| 128 |
} |
| 129 |
if($echo) { |
| 130 |
echo $output."\n"; |
| 131 |
} else { |
| 132 |
return $output; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
### Function: Short Code For Inserting Prink Links Into Posts/Pages |
| 138 |
add_shortcode('print_link', 'print_link_shortcode'); |
| 139 |
function print_link_shortcode($atts) { |
| 140 |
if(!is_feed()) { |
| 141 |
return print_link('', '', false); |
| 142 |
} else { |
| 143 |
return __('Note: There is a print link embedded within this post, please visit this post to print it.', 'wp-print'); |
| 144 |
} |
| 145 |
} |
| 146 |
function print_link_shortcode2($atts) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
### Function: Short Code For DO NOT PRINT Content |
| 152 |
add_shortcode('donotprint', 'print_donotprint_shortcode'); |
| 153 |
function print_donotprint_shortcode($atts, $content = null) { |
| 154 |
return do_shortcode($content); |
| 155 |
} |
| 156 |
function print_donotprint_shortcode2($atts, $content = null) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
### Function: Print Content |
| 162 |
function print_content($display = true) { |
| 163 |
global $links_text, $link_number, $max_link_number, $matched_links, $pages, $multipage, $numpages, $post; |
| 164 |
if (!isset($matched_links)) { |
| 165 |
$matched_links = array(); |
| 166 |
} |
| 167 |
if(post_password_required()) { |
| 168 |
$content = get_the_password_form(); |
| 169 |
} else { |
| 170 |
if($multipage) { |
| 171 |
for($page = 0; $page < $numpages; $page++) { |
| 172 |
$content .= $pages[$page]; |
| 173 |
} |
| 174 |
} else { |
| 175 |
$content = $pages[0]; |
| 176 |
} |
| 177 |
if(function_exists('email_rewrite')) { |
| 178 |
remove_shortcode('donotemail'); |
| 179 |
add_shortcode('donotemail', 'email_donotemail_shortcode2'); |
| 180 |
} |
| 181 |
remove_shortcode('donotprint'); |
| 182 |
add_shortcode('donotprint', 'print_donotprint_shortcode2'); |
| 183 |
remove_shortcode('print_link'); |
| 184 |
add_shortcode('print_link', 'print_link_shortcode2'); |
| 185 |
$content = apply_filters('the_content', $content); |
| 186 |
$content = str_replace(']]>', ']]>', $content); |
| 187 |
if(!print_can('images')) { |
| 188 |
$content = remove_image($content); |
| 189 |
} |
| 190 |
if(!print_can('videos')) { |
| 191 |
$content = remove_video($content); |
| 192 |
} |
| 193 |
if(print_can('links')) { |
| 194 |
preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches); |
| 195 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 196 |
$link_match = $matches[0][$i]; |
| 197 |
$link_url = $matches[2][$i]; |
| 198 |
if(substr($link_url, 0, 2) == '//') { |
| 199 |
$link_url = (is_ssl() ? 'https:' : 'http:') . $link_url; |
| 200 |
} elseif(stristr($link_url, 'https://')) { |
| 201 |
$link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url; |
| 202 |
} else if(stristr($link_url, 'mailto:')) { |
| 203 |
$link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url; |
| 204 |
} else if($link_url[0] == '#') { |
| 205 |
$link_url = $link_url; |
| 206 |
} else { |
| 207 |
$link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url; |
| 208 |
} |
| 209 |
$link_text = $matches[4][$i]; |
| 210 |
$new_link = true; |
| 211 |
$link_url_hash = md5($link_url); |
| 212 |
if (!isset($matched_links[$link_url_hash])) { |
| 213 |
$link_number = ++$max_link_number; |
| 214 |
$matched_links[$link_url_hash] = $link_number; |
| 215 |
} else { |
| 216 |
$new_link = false; |
| 217 |
$link_number = $matched_links[$link_url_hash]; |
| 218 |
} |
| 219 |
$content = str_replace_one($link_match, "<a href=\"$link_url\" rel=\"external\">".$link_text.'</a> <sup>['.number_format_i18n($link_number).']</sup>', $content); |
| 220 |
if ($new_link) { |
| 221 |
if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) { |
| 222 |
$links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.__('Image', 'wp-print').': <b><span dir="ltr">'.$link_url.'</span></b></p>'; |
| 223 |
} else { |
| 224 |
$links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.$link_text.': <b><span dir="ltr">'.$link_url.'</span></b></p>'; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
if($display) { |
| 231 |
echo $content; |
| 232 |
} else { |
| 233 |
return $content; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
### Function: Print Categories |
| 239 |
function print_categories($before = '', $after = '') { |
| 240 |
$temp_cat = strip_tags(get_the_category_list(',')); |
| 241 |
$temp_cat = explode(', ', $temp_cat); |
| 242 |
$temp_cat = implode($after.__(',', 'wp-print').' '.$before, $temp_cat); |
| 243 |
echo $before.$temp_cat.$after; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
### Function: Print Comments Content |
| 248 |
function print_comments_content($display = true) { |
| 249 |
global $links_text, $link_number, $max_link_number, $matched_links; |
| 250 |
if (!isset($matched_links)) { |
| 251 |
$matched_links = array(); |
| 252 |
} |
| 253 |
$content = get_comment_text(); |
| 254 |
$content = apply_filters('comment_text', $content); |
| 255 |
if(!print_can('images')) { |
| 256 |
$content = remove_image($content); |
| 257 |
} |
| 258 |
if(!print_can('videos')) { |
| 259 |
$content = remove_video($content); |
| 260 |
} |
| 261 |
if(print_can('links')) { |
| 262 |
preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches); |
| 263 |
for ($i=0; $i < count($matches[0]); $i++) { |
| 264 |
$link_match = $matches[0][$i]; |
| 265 |
$link_url = $matches[2][$i]; |
| 266 |
if(stristr($link_url, 'https://')) { |
| 267 |
$link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url; |
| 268 |
} else if(stristr($link_url, 'mailto:')) { |
| 269 |
$link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url; |
| 270 |
} else if($link_url[0] == '#') { |
| 271 |
$link_url = $link_url; |
| 272 |
} else { |
| 273 |
$link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url; |
| 274 |
} |
| 275 |
$new_link = true; |
| 276 |
$link_url_hash = md5($link_url); |
| 277 |
if (!isset($matched_links[$link_url_hash])) { |
| 278 |
$link_number = ++$max_link_number; |
| 279 |
$matched_links[$link_url_hash] = $link_number; |
| 280 |
} else { |
| 281 |
$new_link = false; |
| 282 |
$link_number = $matched_links[$link_url_hash]; |
| 283 |
} |
| 284 |
$content = str_replace_one($link_match, "<a href=\"$link_url\" rel=\"external\">".$link_text.'</a> <sup>['.number_format_i18n($link_number).']</sup>', $content); |
| 285 |
if ($new_link) { |
| 286 |
if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) { |
| 287 |
$links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.__('Image', 'wp-print').': <b><span dir="ltr">'.$link_url.'</span></b></p>'; |
| 288 |
} else { |
| 289 |
$links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.$link_text.': <b><span dir="ltr">'.$link_url.'</span></b></p>'; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
if($display) { |
| 295 |
echo $content; |
| 296 |
} else { |
| 297 |
return $content; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
### Function: Print Comments |
| 303 |
function print_comments_number() { |
| 304 |
global $post; |
| 305 |
$comment_text = ''; |
| 306 |
$comment_status = $post->comment_status; |
| 307 |
if($comment_status == 'open') { |
| 308 |
$num_comments = get_comments_number(); |
| 309 |
if($num_comments == 0) { |
| 310 |
$comment_text = __('No Comments', 'wp-print'); |
| 311 |
} else { |
| 312 |
$comment_text = sprintf(_n('%s Comment', '%s Comments', $num_comments, 'wp-print'), number_format_i18n($num_comments)); |
| 313 |
} |
| 314 |
} else { |
| 315 |
$comment_text = __('Comments Disabled', 'wp-print'); |
| 316 |
} |
| 317 |
if(post_password_required()) { |
| 318 |
_e('Comments Hidden', 'wp-print'); |
| 319 |
} else { |
| 320 |
echo $comment_text; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
### Function: Print Links |
| 326 |
function print_links($text_links = '') { |
| 327 |
global $links_text; |
| 328 |
if(empty($text_links)) { |
| 329 |
$text_links = __('URLs in this post:', 'wp-print'); |
| 330 |
} |
| 331 |
if(!empty($links_text)) { |
| 332 |
echo $text_links.$links_text; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
### Function: Load WP-Print |
| 338 |
add_action('template_redirect', 'wp_print', 5); |
| 339 |
function wp_print() { |
| 340 |
global $wp_query; |
| 341 |
if( array_key_exists( 'print' , $wp_query->query_vars ) ) { |
| 342 |
include(WP_PLUGIN_DIR.'/wp-print/print.php'); |
| 343 |
exit(); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
### Function: Add Print Comments Template |
| 349 |
function print_template_comments() { |
| 350 |
if(file_exists(get_stylesheet_directory().'/print-comments.php')) { |
| 351 |
$file = get_stylesheet_directory().'/print-comments.php'; |
| 352 |
} else { |
| 353 |
$file = WP_PLUGIN_DIR.'/wp-print/print-comments.php'; |
| 354 |
} |
| 355 |
return $file; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
### Function: Print Page Title |
| 360 |
function print_pagetitle($page_title) { |
| 361 |
$page_title .= ' » '.__('Print', 'wp-print'); |
| 362 |
return $page_title; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
### Function: Can Print? |
| 367 |
function print_can($type) { |
| 368 |
$print_options = get_option('print_options'); |
| 369 |
return intval($print_options[$type]); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
### Function: Remove Image From Text |
| 374 |
function remove_image($content) { |
| 375 |
$content= preg_replace('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/', '',$content); |
| 376 |
return $content; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
### Function: Remove Video From Text |
| 381 |
function remove_video($content) { |
| 382 |
$content= preg_replace('/<object[^>]*?>.*?<\/object>/', '',$content); |
| 383 |
$content= preg_replace('/<embed[^>]*?>.*?<\/embed>/', '',$content); |
| 384 |
return $content; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
### Function: Replace One Time Only |
| 389 |
function str_replace_one($search, $replace, $content){ |
| 390 |
if ($pos = strpos($content, $search)) { |
| 391 |
return substr($content, 0, $pos).$replace.substr($content, $pos+strlen($search)); |
| 392 |
} else { |
| 393 |
return $content; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
### Function: Activate Plugin |
| 399 |
register_activation_hook( __FILE__, 'print_activation' ); |
| 400 |
function print_activation( $network_wide ) |
| 401 |
{ |
| 402 |
// Add Options |
| 403 |
$option_name = 'print_options'; |
| 404 |
$option = array( |
| 405 |
'post_text' => __('Print This Post', 'wp-print') |
| 406 |
, 'page_text' => __('Print This Page', 'wp-print') |
| 407 |
, 'print_icon' => 'print.gif' |
| 408 |
, 'print_style' => 1 |
| 409 |
, 'print_html' => '<a href="%PRINT_URL%" rel="nofollow" title="%PRINT_TEXT%">%PRINT_TEXT%</a>' |
| 410 |
, 'comments' => 0 |
| 411 |
, 'links' => 1 |
| 412 |
, 'images' => 1 |
| 413 |
, 'videos' => 0 |
| 414 |
, 'disclaimer' => sprintf(__('Copyright © %s %s. All rights reserved.', 'wp-print'), date('Y'), get_option('blogname')) |
| 415 |
); |
| 416 |
|
| 417 |
if ( is_multisite() && $network_wide ) |
| 418 |
{ |
| 419 |
$ms_sites = wp_get_sites(); |
| 420 |
|
| 421 |
if( 0 < sizeof( $ms_sites ) ) |
| 422 |
{ |
| 423 |
foreach ( $ms_sites as $ms_site ) |
| 424 |
{ |
| 425 |
switch_to_blog( $ms_site['blog_id'] ); |
| 426 |
add_option( $option_name, $option ); |
| 427 |
print_activate(); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
restore_current_blog(); |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
add_option( $option_name, $option ); |
| 436 |
print_activate(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
function print_activate() { |
| 441 |
flush_rewrite_rules(); |
| 442 |
} |