PluginProbe
WP-Print / 2.50
WP-Print v2.50
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.50, at wp-print.php

454 lines 15.5 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.50
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 */
10
11
12 /*
13 Copyright 2009 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', false, '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, 5, 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, 5, 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 = plugins_url('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.'&amp;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>&nbsp;<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 function print_link_shortcode2($atts) {
187 return;
188 }
189
190
191 ### Function: Short Code For DO NOT PRINT Content
192 add_shortcode('donotprint', 'print_donotprint_shortcode');
193 function print_donotprint_shortcode($atts, $content = null) {
194 return do_shortcode($content);
195 }
196 function print_donotprint_shortcode2($atts, $content = null) {
197 return;
198 }
199
200
201 ### Function: Print Content
202 function print_content($display = true) {
203 global $links_text, $link_number, $max_link_number, $matched_links, $pages, $multipage, $numpages, $post;
204 if (!isset($matched_links)) {
205 $matched_links = array();
206 }
207 if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) {
208 $content = get_the_password_form();
209 } else {
210 if($multipage) {
211 for($page = 0; $page < $numpages; $page++) {
212 $content .= $pages[$page];
213 }
214 } else {
215 $content = $pages[0];
216 }
217 if(function_exists('email_rewrite')) {
218 remove_shortcode('donotemail');
219 add_shortcode('donotemail', 'email_donotemail_shortcode2');
220 }
221 remove_shortcode('donotprint');
222 add_shortcode('donotprint', 'print_donotprint_shortcode2');
223 remove_shortcode('print_link');
224 add_shortcode('print_link', 'print_link_shortcode2');
225 $content = apply_filters('the_content', $content);
226 $content = str_replace(']]>', ']]&gt;', $content);
227 if(!print_can('images')) {
228 $content = remove_image($content);
229 }
230 if(!print_can('videos')) {
231 $content = remove_video($content);
232 }
233 if(print_can('links')) {
234 preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches);
235 for ($i=0; $i < count($matches[0]); $i++) {
236 $link_match = $matches[0][$i];
237 $link_url = $matches[2][$i];
238 if(stristr($link_url, 'https://')) {
239 $link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url;
240 } else if( stristr($link_url, 'mailto:')) {
241 $link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url;
242 } else if( $link_url[0] == '#' ) {
243 $link_url = $link_url;
244 } else {
245 $link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url;
246 }
247 $link_text = $matches[4][$i];+
248 $new_link = true;
249 $link_url_hash = md5($link_url);
250 if (!isset($matched_links[$link_url_hash])) {
251 $link_number = ++$max_link_number;
252 $matched_links[$link_url_hash] = $link_number;
253 } else {
254 $new_link = false;
255 $link_number = $matched_links[$link_url_hash];
256 }
257 $content = str_replace_one($link_match, "<a href=\"$link_url\" rel=\"external\">".$link_text.'</a> <sup>['.number_format_i18n($link_number).']</sup>', $content);
258 if ($new_link) {
259 if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) {
260 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.__('Image', 'wp-print').': <b><span dir="ltr">'.$link_url.'</span></b></p>';
261 } else {
262 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.$link_text.': <b><span dir="ltr">'.$link_url.'</span></b></p>';
263 }
264 }
265 }
266 }
267 }
268 if($display) {
269 echo $content;
270 } else {
271 return $content;
272 }
273 }
274
275
276 ### Function: Print Categories
277 function print_categories($before = '', $after = '') {
278 $temp_cat = strip_tags(get_the_category_list(',', $parents));
279 $temp_cat = explode(', ', $temp_cat);
280 $temp_cat = implode($after.__(',', 'wp-print').' '.$before, $temp_cat);
281 echo $before.$temp_cat.$after;
282 }
283
284
285 ### Function: Print Comments Content
286 function print_comments_content($display = true) {
287 global $links_text, $link_number, $max_link_number, $matched_links;
288 if (!isset($matched_links)) {
289 $matched_links = array();
290 }
291 $content = get_comment_text();
292 $content = apply_filters('comment_text', $content);
293 if(!print_can('images')) {
294 $content = remove_image($content);
295 }
296 if(!print_can('videos')) {
297 $content = remove_video($content);
298 }
299 if(print_can('links')) {
300 preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches);
301 for ($i=0; $i < count($matches[0]); $i++) {
302 $link_match = $matches[0][$i];
303 $link_url = $matches[2][$i];
304 if(stristr($link_url, 'https://')) {
305 $link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url;
306 } else if(stristr($link_url, 'mailto:')) {
307 $link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url;
308 } else if($link_url[0] == '#') {
309 $link_url = $link_url;
310 } else {
311 $link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url;
312 }
313 $new_link = true;
314 $link_url_hash = md5($link_url);
315 if (!isset($matched_links[$link_url_hash])) {
316 $link_number = ++$max_link_number;
317 $matched_links[$link_url_hash] = $link_number;
318 } else {
319 $new_link = false;
320 $link_number = $matched_links[$link_url_hash];
321 }
322 $content = str_replace_one($link_match, "<a href=\"$link_url\" rel=\"external\">".$link_text.'</a> <sup>['.number_format_i18n($link_number).']</sup>', $content);
323 if ($new_link) {
324 if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) {
325 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.__('Image', 'wp-print').': <b><span dir="ltr">'.$link_url.'</span></b></p>';
326 } else {
327 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.$link_text.': <b><span dir="ltr">'.$link_url.'</span></b></p>';
328 }
329 }
330 }
331 }
332 if($display) {
333 echo $content;
334 } else {
335 return $content;
336 }
337 }
338
339
340 ### Function: Print Comments
341 function print_comments_number() {
342 global $post;
343 $comment_text = '';
344 $comment_status = $post->comment_status;
345 if($comment_status == 'open') {
346 $num_comments = get_comments_number();
347 if($num_comments == 0) {
348 $comment_text = __('No Comments', 'wp-print');
349 } else {
350 $comment_text = sprintf(_n('%s Comment', '%s Comments', $num_comments, 'wp-print'), number_format_i18n($num_comments));
351 }
352 } else {
353 $comment_text = __('Comments Disabled', 'wp-print');
354 }
355 if(!empty($post->post_password) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password) {
356 _e('Comments Hidden', 'wp-print');
357 } else {
358 echo $comment_text;
359 }
360 }
361
362
363 ### Function: Print Links
364 function print_links($text_links = '') {
365 global $links_text;
366 if(empty($text_links)) {
367 $text_links = __('URLs in this post:', 'wp-print');
368 }
369 if(!empty($links_text)) {
370 echo $text_links.$links_text;
371 }
372 }
373
374
375 ### Function: Load WP-Print
376 add_action('template_redirect', 'wp_print', 5);
377 function wp_print() {
378 if(intval(get_query_var('print')) == 1 || intval(get_query_var('printpage')) == 1) {
379 include(WP_PLUGIN_DIR.'/wp-print/print.php');
380 exit();
381 }
382 }
383
384
385 ### Function: Add Print Comments Template
386 function print_template_comments($file = '') {
387 if(file_exists(TEMPLATEPATH.'/print-comments.php')) {
388 $file = TEMPLATEPATH.'/print-comments.php';
389 } else {
390 $file = WP_PLUGIN_DIR.'/wp-print/print-comments.php';
391 }
392 return $file;
393 }
394
395
396 ### Function: Print Page Title
397 function print_pagetitle($page_title) {
398 $page_title .= ' &raquo; '.__('Print', 'wp-print');
399 return $page_title;
400 }
401
402
403 ### Function: Can Print?
404 function print_can($type) {
405 $print_options = get_option('print_options');
406 return intval($print_options[$type]);
407 }
408
409
410 ### Function: Remove Image From Text
411 function remove_image($content) {
412 $content= preg_replace('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/', '',$content);
413 return $content;
414 }
415
416
417 ### Function: Remove Video From Text
418 function remove_video($content) {
419 $content= preg_replace('/<object[^>]*?>.*?<\/object>/', '',$content);
420 $content= preg_replace('/<embed[^>]*?>.*?<\/embed>/', '',$content);
421 return $content;
422 }
423
424
425 ### Function: Replace One Time Only
426 function str_replace_one($search, $replace, $content){
427 if ($pos = strpos($content, $search)) {
428 return substr($content, 0, $pos).$replace.substr($content, $pos+strlen($search));
429 } else {
430 return $content;
431 }
432 }
433
434
435 ### Function: Print Options
436 add_action('activate_wp-print/wp-print.php', 'print_init');
437 function print_init() {
438 print_textdomain();
439 // Add Options
440 $print_options = array();
441 $print_options['post_text'] = __('Print This Post', 'wp-print');
442 $print_options['page_text'] = __('Print This Page', 'wp-print');
443 $print_options['print_icon'] = 'print.gif';
444 $print_options['print_style'] = 1;
445 $print_options['print_html'] = '<a href="%PRINT_URL%" rel="nofollow" title="%PRINT_TEXT%">%PRINT_TEXT%</a>';
446 $print_options['comments'] = 0;
447 $print_options['links'] = 1;
448 $print_options['images'] = 1;
449 $print_options['videos'] = 0;
450 $print_options['disclaimer'] = sprintf(__('Copyright &copy; %s %s. All rights reserved.', 'wp-print'), date('Y'), get_option('blogname'));
451 add_option('print_options', $print_options, 'Print Options');
452 }
453 ?>
454