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

443 lines 13.9 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.58.2
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 Text Domain: wp-print
10 */
11
12
13 /*
14 Copyright 2020 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' );
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 ( (int) 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.'&amp;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>&nbsp;<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 $content = '';
168 if(post_password_required()) {
169 $content = get_the_password_form();
170 } else {
171 if($multipage) {
172 for($page = 0; $page < $numpages; $page++) {
173 $content .= $pages[$page];
174 }
175 } else {
176 $content = $pages[0];
177 }
178 if(function_exists('email_rewrite')) {
179 remove_shortcode('donotemail');
180 add_shortcode('donotemail', 'email_donotemail_shortcode2');
181 }
182 remove_shortcode('donotprint');
183 add_shortcode('donotprint', 'print_donotprint_shortcode2');
184 remove_shortcode('print_link');
185 add_shortcode('print_link', 'print_link_shortcode2');
186 $content = apply_filters('the_content', $content);
187 $content = str_replace(']]>', ']]&gt;', $content);
188 if(!print_can('images')) {
189 $content = remove_image($content);
190 }
191 if(!print_can('videos')) {
192 $content = remove_video($content);
193 }
194 if(print_can('links')) {
195 preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches);
196 for ($i=0; $i < count($matches[0]); $i++) {
197 $link_match = $matches[0][$i];
198 $link_url = $matches[2][$i];
199 if(substr($link_url, 0, 2) == '//') {
200 $link_url = (is_ssl() ? 'https:' : 'http:') . $link_url;
201 } elseif(stristr($link_url, 'https://')) {
202 $link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url;
203 } else if(stristr($link_url, 'mailto:')) {
204 $link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url;
205 } else if($link_url[0] == '#') {
206 $link_url = $link_url;
207 } else {
208 $link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url;
209 }
210 $link_text = $matches[4][$i];
211 $new_link = true;
212 $link_url_hash = md5($link_url);
213 if (!isset($matched_links[$link_url_hash])) {
214 $link_number = ++$max_link_number;
215 $matched_links[$link_url_hash] = $link_number;
216 } else {
217 $new_link = false;
218 $link_number = $matched_links[$link_url_hash];
219 }
220 $content = str_replace_one($link_match, "<a href=\"$link_url\" rel=\"external\">".$link_text.'</a> <sup>['.number_format_i18n($link_number).']</sup>', $content);
221 if ($new_link) {
222 if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) {
223 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.__('Image', 'wp-print').': <strong><span dir="ltr">'.$link_url.'</span></strong></p>';
224 } else {
225 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.$link_text.': <strong><span dir="ltr">'.$link_url.'</span></strong></p>';
226 }
227 }
228 }
229 }
230 }
231 if($display) {
232 echo $content;
233 } else {
234 return $content;
235 }
236 }
237
238
239 ### Function: Print Categories
240 function print_categories($before = '', $after = '') {
241 $temp_cat = strip_tags(get_the_category_list(','));
242 $temp_cat = explode(', ', $temp_cat);
243 $temp_cat = implode($after.__(',', 'wp-print').' '.$before, $temp_cat);
244 echo $before.$temp_cat.$after;
245 }
246
247
248 ### Function: Print Comments Content
249 function print_comments_content($display = true) {
250 global $links_text, $link_number, $max_link_number, $matched_links;
251 if (!isset($matched_links)) {
252 $matched_links = array();
253 }
254 $content = get_comment_text();
255 $content = apply_filters( 'comment_text', $content, null, array() );
256 if(!print_can('images')) {
257 $content = remove_image($content);
258 }
259 if(!print_can('videos')) {
260 $content = remove_video($content);
261 }
262 if(print_can('links')) {
263 preg_match_all('/<a(.+?)href=[\"\'](.+?)[\"\'](.*?)>(.+?)<\/a>/', $content, $matches);
264 for ($i=0; $i < count($matches[0]); $i++) {
265 $link_match = $matches[0][$i];
266 $link_url = $matches[2][$i];
267 if(stristr($link_url, 'https://')) {
268 $link_url =(strtolower(substr($link_url,0,8)) != 'https://') ?get_option('home') . $link_url : $link_url;
269 } else if(stristr($link_url, 'mailto:')) {
270 $link_url =(strtolower(substr($link_url,0,7)) != 'mailto:') ?get_option('home') . $link_url : $link_url;
271 } else if($link_url[0] == '#') {
272 $link_url = $link_url;
273 } else {
274 $link_url =(strtolower(substr($link_url,0,7)) != 'http://') ?get_option('home') . $link_url : $link_url;
275 }
276 $new_link = true;
277 $link_url_hash = md5($link_url);
278 if (!isset($matched_links[$link_url_hash])) {
279 $link_number = ++$max_link_number;
280 $matched_links[$link_url_hash] = $link_number;
281 } else {
282 $new_link = false;
283 $link_number = $matched_links[$link_url_hash];
284 }
285 $content = str_replace_one($link_match, "<a href=\"$link_url\" rel=\"external\">".$link_text.'</a> <sup>['.number_format_i18n($link_number).']</sup>', $content);
286 if ($new_link) {
287 if(preg_match('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/',$link_text)) {
288 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.__('Image', 'wp-print').': <strong><span dir="ltr">'.$link_url.'</span></strong></p>';
289 } else {
290 $links_text .= '<p style="margin: 2px 0;">['.number_format_i18n($link_number).'] '.$link_text.': <strong><span dir="ltr">'.$link_url.'</span></strong></p>';
291 }
292 }
293 }
294 }
295 if($display) {
296 echo $content;
297 } else {
298 return $content;
299 }
300 }
301
302
303 ### Function: Print Comments
304 function print_comments_number() {
305 global $post;
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 .= ' &raquo; '.__('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 if ( isset( $print_options[$type] ) ) {
370 return (int) $print_options[$type];
371 }
372
373 return 0;
374 }
375
376
377 ### Function: Remove Image From Text
378 function remove_image($content) {
379 $content= preg_replace('/<img(.+?)src=[\"\'](.+?)[\"\'](.*?)>/', '',$content);
380 return $content;
381 }
382
383
384 ### Function: Remove Video From Text
385 function remove_video( $content ) {
386 $content= preg_replace( '/<object[^>]*?>.*?<\/object>/', '',$content );
387 $content= preg_replace( '/<embed[^>]*?>.*?<\/embed>/', '',$content );
388 $content= preg_replace( '/<iframe[^>]*?>.*?<\/iframe>/', '',$content );
389 return $content;
390 }
391
392
393 ### Function: Replace One Time Only
394 function str_replace_one($search, $replace, $content){
395 if ($pos = strpos($content, $search)) {
396 return substr($content, 0, $pos).$replace.substr($content, $pos+strlen($search));
397 } else {
398 return $content;
399 }
400 }
401
402
403 ### Function: Activate Plugin
404 register_activation_hook( __FILE__, 'print_activation' );
405 function print_activation( $network_wide ) {
406 // Add Options
407 $option_name = 'print_options';
408 $option = array(
409 'post_text' => __('Print This Post', 'wp-print'),
410 'page_text' => __('Print This Page', 'wp-print'),
411 'print_icon' => 'print.gif',
412 'print_style' => 1,
413 'print_html' => '<a href="%PRINT_URL%" rel="nofollow" title="%PRINT_TEXT%">%PRINT_TEXT%</a>',
414 'comments' => 0,
415 'links' => 1,
416 'images' => 1,
417 'thumbnail' => 0,
418 'videos' => 0,
419 'disclaimer' => sprintf(__('Copyright &copy; %s %s. All rights reserved.', 'wp-print'), date('Y'), get_option('blogname'))
420 );
421
422 if ( is_multisite() && $network_wide ) {
423 $ms_sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites();
424
425 if( 0 < count( $ms_sites ) ) {
426 foreach ( $ms_sites as $ms_site ) {
427 $blog_id = isset( $ms_site['blog_id'] ) ? $ms_site['blog_id'] : $ms_site->blog_id;
428 switch_to_blog( $blog_id );
429 add_option( $option_name, $option );
430 print_activate();
431 }
432 }
433
434 restore_current_blog();
435 } else {
436 add_option( $option_name, $option );
437 print_activate();
438 }
439 }
440
441 function print_activate() {
442 flush_rewrite_rules();
443 }