PluginProbe
WP-Print / 2.31
WP-Print v2.31
3.0.0 trunk 1.00 2.00 2.01 2.02 2.03 2.04 2.05 2.06 2.10 2.11 2.20 2.30 2.31 2.40 2.50 2.52 2.53 2.54 2.55 2.56 2.57 2.57.1 2.57.2 All 29 releases
wp-print / wp-print.php

wp-print.php in WP-Print 2.31, at wp-print.php

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