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

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