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

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