wp-print-friendly.php 29.4 KB
Newer Older
1 2 3 4 5 6
<?php
/*
Plugin Name: WP Print Friendly
Plugin URI: http://www.thinkoomph.com/plugins-modules/wp-print-friendly/
Description: Extends WordPress' template system to support printer-friendly templates. Works with permalink structures to support nice URLs.
Author: Erick Hitter (Oomph, Inc.)
Erick Hitter's avatar
Erick Hitter committed
7
Version: 0.5
8 9 10 11 12
Author URI: http://www.thinkoomph.com/
*/

class wp_print_friendly {
	var $query_var = 'print';
13

14
	var $ns = 'wp_print_friendly';
15

16 17 18 19 20 21 22 23 24 25 26 27
	var $settings_key = 'wpf';
	var $settings_defaults = array(
		'auto' => false,
		'placement' => 'below',
		'post_types' => array( 'post', 'page' ),
		'print_text' => 'Print this entry',
		'print_text_page' => 'Print this page',
		'css_class' => 'print_link',
		'link_target' => 'same',
		'endnotes' => true,
		'endnotes_label' => 'Endnotes:'
	);
28

29
	var $notice_key = 'wpf_admin_notice_dismissed';
30

Erick Hitter's avatar
Erick Hitter committed
31
	/**
32
	 * Register deactivation hook and filter.
Erick Hitter's avatar
Erick Hitter committed
33
	 *
34 35 36
	 * @uses register_deactivation_hook, add_filter
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
37
	public function __construct() {
38 39 40
		register_deactivation_hook( __FILE__, array( $this, 'deactivation_hook' ) );
		add_action( 'plugins_loaded', array( $this, 'action_plugins_loaded' ) );
	}
41

Erick Hitter's avatar
Erick Hitter committed
42
	/**
43
	 * Clean up after plugin deactivation.
Erick Hitter's avatar
Erick Hitter committed
44
	 *
45 46 47 48
	 * @uses flush_rewrite_rules, delete_option
	 * @action register_deactivation_hook
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
49
	public function deactivation_hook() {
50
		flush_rewrite_rules();
51

52 53 54
		delete_option( $this->settings_key );
		delete_option( $this->notice_key );
	}
55

Erick Hitter's avatar
Erick Hitter committed
56
	/**
57
	 * Register actions and filters.
Erick Hitter's avatar
Erick Hitter committed
58
	 *
59 60 61 62
	 * @uses add_action, add_filter, get_option
	 * @action plugins_loaded
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
63
	public function action_plugins_loaded() {
64 65 66 67 68 69
		add_action( 'init', array( $this, 'action_init' ), 20 );
		add_action( 'admin_init', array( $this, 'action_admin_init' ) );
		add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
		add_filter( 'request', array( $this, 'filter_request' ) );
		add_action( 'pre_get_posts', array( $this, 'action_pre_get_posts' ) );
		add_filter( 'template_include', array( $this, 'filter_template_include' ) );
Erick Hitter's avatar
Erick Hitter committed
70
		add_filter( 'redirect_canonical', array( $this, 'filter_redirect_canonical' ) );
71 72 73 74
		add_filter( 'body_class', array( $this, 'filter_body_class' ) );
		add_filter( 'the_content', array( $this, 'filter_the_content' ), 0 );
		add_filter( 'the_content', array( $this, 'filter_the_content_auto' ) );
		add_filter( 'the_content', array( $this, 'filter_the_content_late' ), 99 );
75

Erick Hitter's avatar
Erick Hitter committed
76
		if ( ! get_option( $this->notice_key ) )
77 78
			add_action( 'admin_notices', array( $this, 'action_admin_notices_activation' ) );
	}
79

Erick Hitter's avatar
Erick Hitter committed
80
	/**
81
	 * Add print endpoint and rewrite rules for term taxonomy archives
Erick Hitter's avatar
Erick Hitter committed
82 83
	 *
	 * @uses add_rewrite_endpoint, $wp_rewrite, get_taxonomies, add_rewrite_rule, trailingslashit
84 85 86
	 * @action init
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
87
	public function action_init() {
88
		add_rewrite_endpoint( $this->query_var, EP_ALL );
89

90
		global $wp_rewrite;
91 92

		//Taxonomies, since they aren't covered by add_rewrite_endpoint
Erick Hitter's avatar
Erick Hitter committed
93
		if ( $wp_rewrite->permalink_structure ) {
94
			$taxonomies = get_taxonomies( array(), 'objects' );
Erick Hitter's avatar
Erick Hitter committed
95 96
			foreach ( $taxonomies as $taxonomy => $args ) {
				if ( $args->rewrite == false || 'post_format' == $taxonomy )
97
					continue;
98

99
				$taxonomy_slug = '';
Erick Hitter's avatar
Erick Hitter committed
100
				if ( $args->rewrite[ 'with_front' ] && $wp_rewrite->front != '/' ) $taxonomy_slug .= $wp_rewrite->front;
101
				$taxonomy_slug .= $args->rewrite[ 'slug' ];
102

103
				$query_var = $args->query_var ? $args->query_var : 'taxonomy=' . $taxonomy . '&term';
104 105

				add_rewrite_rule( $taxonomy_slug . '/(.+)/' . $this->query_var . '(/([0-9]*))?/?$', $wp_rewrite->index . '?' . $query_var . '=$matches[1]&' . $this->query_var . '=$matches[3]', 'top' );
106 107
			}
		}
108 109 110 111 112 113 114 115 116

		//Extra rules needed if verbose page rules are requested
		if ( $wp_rewrite->use_verbose_page_rules ) {
			$regex = substr( str_replace( $wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $wp_rewrite->permalink_structure ), 1 );
			$regex = trailingslashit( $regex );
			$regex .= $this->query_var . '(/([0-9]*))?/?$';

			add_rewrite_rule( $regex, $wp_rewrite->index . '?category_name=$matches[1]&name=$matches[2]&' . $this->query_var . '=$matches[4]', 'top' );
		}
117
	}
118

Erick Hitter's avatar
Erick Hitter committed
119
	/**
120
	 * Register plugin option and disable rewrite rule flush warning.
Erick Hitter's avatar
Erick Hitter committed
121
	 *
122 123 124 125
	 * @uses register_setting, update_option
	 * @action admin_init
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
126
	public function action_admin_init() {
127
		register_setting( $this->settings_key, $this->settings_key, array( $this, 'admin_options_validate' ) );
128

Erick Hitter's avatar
Erick Hitter committed
129
		if ( isset( $_GET[ $this->notice_key ] ) )
130 131
			update_option( $this->notice_key, 1 );
	}
132

Erick Hitter's avatar
Erick Hitter committed
133
	/**
134
	 * Determine if print template is being requested.
Erick Hitter's avatar
Erick Hitter committed
135 136
	 *
	 * @uses get_query_var
137 138
	 * @return bool
	 */
Erick Hitter's avatar
Erick Hitter committed
139 140
	public function is_print() {
		return (bool) get_query_var( $this->query_var );
141
	}
142

Erick Hitter's avatar
Erick Hitter committed
143
	/**
144 145
	 * Select appropriate template based on post type and available templates.
	 * Returns an array with name and path keys for available template or false if no template is found.
Erick Hitter's avatar
Erick Hitter committed
146
	 *
147 148 149
	 * @uses get_queried_object, is_home, is_front_page, locate_template
	 * @return array or false
	 */
Erick Hitter's avatar
Erick Hitter committed
150
	public function template_chooser() {
151 152
		//Get queried object to check post type
		$queried_object = get_queried_object();
153

154 155
		//Get plugin path
		$pluginpath = dirname( __FILE__ );
156

Erick Hitter's avatar
Erick Hitter committed
157
		if ( ( is_home() || is_front_page() ) && ( '' !== ( $path = locate_template( 'wpf-home.php', false ) ) ) ) {
158 159 160 161 162
			$template = array(
				'name' => 'wpf-home',
				'path' => $path
			);
		}
Erick Hitter's avatar
Erick Hitter committed
163
		elseif (
164 165 166
			is_object( $queried_object ) &&
			property_exists( $queried_object, 'taxonomy' ) &&
			property_exists( $queried_object, 'slug' ) &&
167
			( '' !== ( $path = locate_template( array( 'wpf-' . $queried_object->taxonomy . '-' . $queried_object->slug . '.php', 'wpf-' . $queried_object->taxonomy . '.php' ), false ) ) )
168 169 170 171 172
		)
			$template = array(
				'name' => 'wpf-' . $queried_object->taxonomy,
				'path' => $path
			);
Erick Hitter's avatar
Erick Hitter committed
173
		elseif (
174 175 176 177 178 179 180 181 182
			is_object( $queried_object ) &&
			property_exists( $queried_object, 'post_type' ) &&
			property_exists( $queried_object, 'post_name' ) &&
			( '' !== ( $path = locate_template( array( 'wpf-' . $queried_object->post_type . '-' . $queried_object->post_name . '.php', 'wpf-' . $queried_object->post_type . '.php' ), false ) ) )
		)
			$template = array(
				'name' => 'wpf-' . $queried_object->post_type,
				'path' => $path
			);
Erick Hitter's avatar
Erick Hitter committed
183
		elseif (
184 185 186 187 188 189 190 191
			is_object( $queried_object ) &&
			property_exists( $queried_object, 'post_name' ) &&
			( '' !== ( $path = locate_template( 'wpf-' . $queried_object->post_name . '.php', false ) ) )
		)
			$template = array(
				'name' => 'wpf-' . $queried_object->post_name,
				'path' => $path
			);
Erick Hitter's avatar
Erick Hitter committed
192
		elseif ( '' !== ( $path = locate_template( 'wpf.php', false ) ) )
193 194 195 196
			$template = array(
				'name' => 'wpf-default',
				'path' => $path
			);
Erick Hitter's avatar
Erick Hitter committed
197
		elseif ( file_exists( $pluginpath . '/default-template.php' ) )
198 199 200 201
			$template = array(
				'name' => 'wpf-plugin-default',
				'path' => $pluginpath . '/default-template.php'
			);
202

203 204
		return isset( $template ) ? $template : false;
	}
205

Erick Hitter's avatar
Erick Hitter committed
206 207 208
	/**
	 * Detect request for print stylesheet on the homepage and reset query variables.
	 *
209 210 211 212
	 * @param array $qv
	 * @filter request
	 * @return array
	 */
Erick Hitter's avatar
Erick Hitter committed
213 214
	public function filter_request( $qv ) {
		if ( array_key_exists( 'pagename', $qv ) && $qv[ 'pagename' ] == $this->query_var ) {
215 216 217 218
			$qv[ $this->query_var ] = '';
			unset( $qv[ 'page' ] );
			unset( $qv[ 'pagename' ] );
		}
219

Erick Hitter's avatar
Erick Hitter committed
220
		if ( array_key_exists( $this->query_var, $qv ) && is_numeric( $qv[ $this->query_var ] ) )
221
			$qv[ 'page' ] = intval( $qv[ $this->query_var ] );
222

223 224
		return $qv;
	}
225

Erick Hitter's avatar
Erick Hitter committed
226
	/**
227
	 * Filter query when request to print specific page is made.
Erick Hitter's avatar
Erick Hitter committed
228
	 *
229 230 231 232
	 * @param object $query
	 * @action pre_get_posts
	 * @return object
	 */
Erick Hitter's avatar
Erick Hitter committed
233 234
	public function action_pre_get_posts( $query ) {
		if ( array_key_exists( $this->query_var, $query->query_vars ) && ! empty( $query->query_vars[ $this->query_var ] ) ) {
235
			$qv = explode( '/', $query->query_vars[ $this->query_var ] );
236

Erick Hitter's avatar
Erick Hitter committed
237
			if ( array_key_exists( 1, $qv ) && is_numeric( $qv[ 1 ] ) )
238 239
				$query->query_vars[ 'page' ] = (int)$qv[ 1 ];
		}
240

241 242
		return $query;
	}
243

Erick Hitter's avatar
Erick Hitter committed
244
	/**
245
	 * Filter template include to return print template if requested.
Erick Hitter's avatar
Erick Hitter committed
246
	 *
247 248 249 250
	 * @param string $template
	 * @filter template_include
	 * @return string
	 */
Erick Hitter's avatar
Erick Hitter committed
251 252
	public function filter_template_include( $template ) {
		if ( $this->is_print() && ( $print_template = $this->template_chooser() ) )
253
			$template = $print_template[ 'path' ];
254

255 256
		return $template;
	}
257

Erick Hitter's avatar
Erick Hitter committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	/**
	 * Prevent canonical redirect if print URL is requested.
	 *
	 * @param string $url
	 * @uses this::is_print
	 * @filter redirect_canonical
	 * @return string or false
	 */
	public function filter_redirect_canonical( $url ) {
		if ( $this->is_print() )
			$url = false;

		return $url;
	}

	/**
274
	 * Filter body classes to include references to print template.
Erick Hitter's avatar
Erick Hitter committed
275
	 *
276 277 278 279
	 * @param array $classes
	 * @filter body_class
	 * @return array
	 */
Erick Hitter's avatar
Erick Hitter committed
280 281 282
	public function filter_body_class( $classes ) {
		if ( $this->is_print() && ( $print_template = $this->template_chooser() ) ) {
			if ( $print_template[ 'name' ] == 'default' )
283 284 285 286
				$classes[] = 'wpf';
			else
				$classes[] = $print_template[ 'name' ];
		}
287

288 289
		return $classes;
	}
290

Erick Hitter's avatar
Erick Hitter committed
291
	/**
292
	 * Filter post content to support printing entire post on one page.
Erick Hitter's avatar
Erick Hitter committed
293
	 *
294 295 296 297 298
	 * @param string $content
	 * @uses get_query_var
	 * @filter the_content
	 * @return string
	 */
Erick Hitter's avatar
Erick Hitter committed
299 300
	public function filter_the_content( $content ) {
		if ( $this->is_print() ) {
301
			$print = get_query_var( $this->query_var );
302

Erick Hitter's avatar
Erick Hitter committed
303
			if ( $print == 'all' || $print == '/all' || empty( $print ) ) {
304
				global $post;
305

306 307 308 309 310 311 312
				$content = $post->post_content;
				$content = str_replace("\n<!--nextpage-->\n", "\n\n", $content);
				$content = str_replace("\n<!--nextpage-->", "\n", $content);
				$content = str_replace("<!--nextpage-->\n", "\n", $content);
				$content = str_replace("<!--nextpage-->", ' ', $content);
			}
		}
313

314 315
		return $content;
	}
316

Erick Hitter's avatar
Erick Hitter committed
317 318 319
	/**
	 * Filter the content if automatic inclusion is selected.
	 *
320 321 322 323 324
	 * @param string $content
	 * @uses $this::get_options, $post, $this::print_url, get_query_var, apply_filters
	 * @filter the_content
	 * @return string
	 */
Erick Hitter's avatar
Erick Hitter committed
325
	public function filter_the_content_auto( $content ) {
326
		$options = $this->get_options();
327

328
		global $post;
329

Erick Hitter's avatar
Erick Hitter committed
330
		if ( is_array( $options ) && array_key_exists( 'auto', $options ) && $options[ 'auto' ] == true && in_array( $post->post_type, $options[ 'post_types' ] ) && ! $this->is_print() ) {
331
			extract( $options );
332

333 334
			//Basic URL
			$print_url = $this->print_url();
335

336
			//Page URL, if necessary
Erick Hitter's avatar
Erick Hitter committed
337
			if ( ! empty( $print_text_page ) && strpos( $post->post_content, '<!--nextpage-->' ) !== false ) {
338 339
				$page = get_query_var( 'page' );
				$page = $page ? $page : 1;
340

341 342
				$print_url_page = $this->print_url( false, $page );
			}
343

344 345
			//Build link(s)
			$link = '<p class="wpf_wrapper"><a class="' . $css_class . '" href="' . $print_url . '"' . ( $link_target == 'new' ? ' target="_blank"' : '' ) . '>' . $print_text . '</a>';
346

Erick Hitter's avatar
Erick Hitter committed
347
			if ( isset( $print_url_page ) ) {
348 349 350
				$link .= ' | ';
				$link .= '<a class="' . $css_class . ' ' . $css_class . '_cur" href="' . $print_url_page . '"' . ( $link_target == 'new' ? ' target="_blank"' : '' ) . '>' . $print_text_page . '</a>';
			}
351

352
			$link .= '</p><!-- .wpf_wrapper -->';
353

354
			//Place link(s)
Erick Hitter's avatar
Erick Hitter committed
355
			if ( $placement == 'above' )
356
				$content = $link . $content;
Erick Hitter's avatar
Erick Hitter committed
357
			elseif ( $placement == 'below' )
358
				$content = $content . $link;
Erick Hitter's avatar
Erick Hitter committed
359
			elseif ( $placement == 'both' )
360 361
				$content = $link . $content . $link;
		}
362

363 364
		return $content;
	}
365

Erick Hitter's avatar
Erick Hitter committed
366 367 368
	/**
	 * Convert links to endnotes if desired.
	 *
369 370 371 372 373
	 * @param string $content
	 * @uses $this::is_print, $this::get_options
	 * @filter the_content
	 * @return string
	 */
Erick Hitter's avatar
Erick Hitter committed
374 375
	public function filter_the_content_late( $content ) {
		if ( $this->is_print() ) {
376
			global $post;
377

378
			$options = $this->get_options();
379

380
			//Endnotes
Erick Hitter's avatar
Erick Hitter committed
381
			if ( $options[ 'endnotes' ] ) {
382 383
				$links = array();
				$i = 1;
384

385 386
				//Build array of links
				preg_match_all( '#<a href=(["\'{1}])([^"\']+)(["\'{1}])([^>]*)>(.*?)</a>#i', $content, $matches );
387

Erick Hitter's avatar
Erick Hitter committed
388
				if (
389
					isset( $matches ) && is_array( $matches ) &&
Erick Hitter's avatar
Erick Hitter committed
390 391 392
					array_key_exists( 0, $matches ) && ! empty( $matches[ 0 ] ) &&
					array_key_exists( 2, $matches ) && ! empty( $matches[ 2 ] ) &&
					array_key_exists( 5, $matches ) && ! empty( $matches[ 5 ] )
393 394 395
				) {
					//Format matches for replacement in content
					$replacements = array();
Erick Hitter's avatar
Erick Hitter committed
396
					foreach ( $matches[ 0 ] as $key => $match ) {
397 398 399 400 401
						$replacements[ $match ] = array(
							'url' => $matches[ 2 ][ $key ],
							'title' => $matches[ 5 ][ $key ]
						);
					}
402

403
					//Replace links with endnote markers
Erick Hitter's avatar
Erick Hitter committed
404
					foreach ( $replacements as $match => $args ) {
405 406 407 408
						$content = str_replace( $match, $args[ 'title' ] . '[' . $i . ']', $content );
						$links[ $i ] = $args;
						$i++;
					}
409

410 411 412 413
					//Output endnotes
					$endnotes = '<div class="wpf-endnotes">';
					$endnotes .= '<strong>' . $options[ 'endnotes_label' ] . '</strong>';
					$endnotes .= '<ol>';
Erick Hitter's avatar
Erick Hitter committed
414
					foreach ( $links as $link ) {
415 416 417 418 419
						$endnotes .= '<li>';
						$endnotes .=  preg_replace( '#<img(.*)>#', '[Image]', $link[ 'title' ] ) . ': ' . esc_url( $link[ 'url' ] );
						$endnotes .= '</li>';
					}
					$endnotes .= '</ol></div><!-- .wpf-endnotes -->';
420

421 422 423 424
					$content .= $endnotes;
				}
			}
		}
425

426 427
		return $content;
	}
428

Erick Hitter's avatar
Erick Hitter committed
429 430 431
	/**
	 * Generate URL for post's printer-friendly format.
	 *
432 433 434 435 436
	 * @param int $post_id
	 * @param int $page
	 * @uses is_view_all, is_home, is_front_page, home_url, $post, get_permalink, is_category, get_category_link, is_tag, get_tag_link, is_date, get_query_var, get_day_link, get_month_link, get_year_link, is_tax, get_queried_object, get_term_link, $wp_rewrite, path_join, trailingslashit, add_query_arg
	 * @return string or bool
	 */
Erick Hitter's avatar
Erick Hitter committed
437 438
	public function print_url( $post_id = false, $page = false ) {
		if ( $page === true )
439
			return false;
440

Erick Hitter's avatar
Erick Hitter committed
441
		if ( function_exists( 'is_view_all' ) && is_view_all() )
442
			$page = false;
443

444
		$link = false;
445

446
		//Get link base specific to page type being viewed
Erick Hitter's avatar
Erick Hitter committed
447 448
		if ( is_singular() || in_the_loop() ) {
			if ( $post_id == false ) {
449 450 451
				global $post;
				$post_id = $post->ID;
			}
452

Erick Hitter's avatar
Erick Hitter committed
453
			if ( ! $post_id )
454
				return false;
455

456 457
			$link = get_permalink( $post_id );
		}
Erick Hitter's avatar
Erick Hitter committed
458
		elseif ( is_home() || is_front_page() )
459
			$link = home_url( '/' );
Erick Hitter's avatar
Erick Hitter committed
460
		elseif ( is_category() )
461
			$link = get_category_link( get_query_var( 'cat' ) );
Erick Hitter's avatar
Erick Hitter committed
462
		elseif ( is_tag() )
463 464
			$link = get_tag_link( get_query_var( 'tag_id' ) );
		/* DISABLED FOR NOW AS PRINTING OF DATE-BASED ARCHIVES DOESN'T WORK YET
Erick Hitter's avatar
Erick Hitter committed
465
		elseif ( is_date() ) {
466 467 468
			$year = get_query_var( 'year' );
			$monthnum = get_query_var( 'monthnum' );
			$day = get_query_var( 'day' );
469

Erick Hitter's avatar
Erick Hitter committed
470
			if ( $day )
471
				$link = get_day_link( $year, $monthnum, $day );
Erick Hitter's avatar
Erick Hitter committed
472
			elseif ( $monthnum )
473 474 475 476
				$link = get_month_link( $year, $monthnum );
			else
				$link = get_year_link( $year );
		}*/
Erick Hitter's avatar
Erick Hitter committed
477
		elseif ( is_tax() ) {
478
			$queried_object = get_queried_object();
479

Erick Hitter's avatar
Erick Hitter committed
480
			if ( is_object( $queried_object ) && property_exists( $queried_object, 'taxonomy' ) && property_exists( $queried_object, 'term_id' ) )
481 482
				$link = get_term_link( (int)$queried_object->term_id, $queried_object->taxonomy );
		}
483

484
		//If link base is set, build link
Erick Hitter's avatar
Erick Hitter committed
485
		if ( $link !== false ) {
486
			global $wp_rewrite;
487

488
			$page = intval( $page );
489

Erick Hitter's avatar
Erick Hitter committed
490
			if ( $wp_rewrite->using_permalinks() ) {
491
				$link = path_join( $link, $this->query_var );
492

Erick Hitter's avatar
Erick Hitter committed
493
				if ( $page )
494
					$link = path_join( $link, intval( $page ) );
495

Erick Hitter's avatar
Erick Hitter committed
496
				if ( $wp_rewrite->use_trailing_slashes )
497 498 499 500
					$link = trailingslashit( $link );
			}
			else {
				$link = add_query_arg( $this->query_var, is_numeric( $page ) ? intval( $page ) : 'all', $link );
501

Erick Hitter's avatar
Erick Hitter committed
502
				if ( $page )
503 504 505
					$link = add_query_arg( 'page', is_numeric( $page ) ? intval( $page ) : 'all', $link );
			}
		}
506

507 508
		return $link;
	}
509

Erick Hitter's avatar
Erick Hitter committed
510 511 512
	/**
	 * Add menu item for options page.
	 *
513 514 515 516
	 * @uses add_options_page
	 * @action admin_menu
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
517
	public function action_admin_menu() {
518 519
		add_options_page( 'WP Print Friendly Options', 'WP Print Friendly', 'manage_options', $this->ns, array( $this, 'admin_options' ) );
	}
520

Erick Hitter's avatar
Erick Hitter committed
521 522 523
	/**
	 * Render options page.
	 *
524 525 526
	 * @uses settings_fields, $this::get_options, _e, checked, esc_attr
	 * @return html
	 */
Erick Hitter's avatar
Erick Hitter committed
527
	public function admin_options() {
528 529 530
	?>
		<div class="wrap">
			<h2>WP Print Friendly</h2>
531

532 533 534 535
			<form action="options.php" method="post">
				<?php
					settings_fields( $this->settings_key );
					$options = $this->get_options();
536

537 538
					$post_types = $this->post_types_array();
				?>
539

540
				<h3>Display Options</h3>
541

542 543
				<table class="form-table">
					<tr>
Erick Hitter's avatar
Erick Hitter committed
544
						<th scope="row"><?php _e( 'Automatically add print links based on settings below?', 'wp_print_friendly' ); ?></th>
545
						<td>
Erick Hitter's avatar
Erick Hitter committed
546 547
							<input type="radio" name="<?php echo esc_attr( $this->settings_key ); ?>[auto]" id="auto-true" value="1"<?php checked( $options[ 'auto' ], true, true ); ?> /> <label for="auto-true"><?php _e( 'Yes', 'wp_print_friendly' ); ?></label><br />
							<input type="radio" name="<?php echo esc_attr( $this->settings_key ); ?>[auto]" id="auto-false" value="0"<?php checked( $options[ 'auto' ], false, true ); ?> /> <label for="auto-false"><?php _e( 'No', 'wp_print_friendly' ); ?></label>
548 549 550
						</td>
					</tr>
					<tr>
Erick Hitter's avatar
Erick Hitter committed
551
						<th scope="row"><?php _e( 'Automatically place link:', 'wp_print_friendly' ); ?></th>
552
						<td>
Erick Hitter's avatar
Erick Hitter committed
553 554 555
							<input type="radio" name="<?php echo esc_attr( $this->settings_key ); ?>[placement]" id="placement-above" value="above"<?php checked( $options[ 'placement' ], 'above', true ); ?> /> <label for="placement-above"><?php _e( 'Above content', 'wp_print_friendly' ); ?></label><br />
							<input type="radio" name="<?php echo esc_attr( $this->settings_key ); ?>[placement]" id="placement-below" value="below"<?php checked( $options[ 'placement' ], 'below', true ); ?> /> <label for="placement-below"><?php _e( 'Below content', 'wp_print_friendly' ); ?></label><br />
							<input type="radio" name="<?php echo esc_attr( $this->settings_key ); ?>[placement]" id="placement-both" value="both"<?php checked( $options[ 'placement' ], 'both', true ); ?> /> <label for="placement-both"><?php _e( 'Above and below content', 'wp_print_friendly' ); ?></label>
556 557 558
						</td>
					</tr>
					<tr>
Erick Hitter's avatar
Erick Hitter committed
559
						<th scope="row"><?php _e( 'Display automatically on:', 'wp_print_friendly' ); ?></th>
560
						<td>
Erick Hitter's avatar
Erick Hitter committed
561 562
							<?php foreach ( $post_types as $post_type ): ?>
								<input type="checkbox" name="<?php echo esc_attr( $this->settings_key ); ?>[post_types][]" id="pt-<?php echo $post_type->name; ?>" value="<?php echo $post_type->name; ?>"<?php if ( in_array( $post_type->name, $options[ 'post_types' ] ) ) echo ' checked="checked"'; ?> /> <label for="pt-<?php echo $post_type->name; ?>"><?php echo $post_type->labels->name; ?></label><br />
563 564 565 566
							<?php endforeach; ?>
						</td>
					</tr>
				</table>
567

568
				<h3>Link Options</h3>
569

570 571
				<table class="form-table">
					<tr>
Erick Hitter's avatar
Erick Hitter committed
572
						<th scope="row"><?php _e( 'Text for link to print entire item:', 'wp_print_friendly' ); ?></th>
573
						<td>
574
							<input type="text" name="<?php echo esc_attr( $this->settings_key ); ?>[print_text]" id="print_text" value="<?php echo esc_attr( $options[ 'print_text' ] ); ?>" style="width: 40%;" />
575 576 577
						</td>
					</tr>
					<tr>
Erick Hitter's avatar
Erick Hitter committed
578
						<th scope="row"><?php _e( 'Text for link to print current page:', 'wp_print_friendly' ); ?></th>
579
						<td>
580 581
							<input type="text" name="<?php echo esc_attr( $this->settings_key ); ?>[print_text_page]" id="print_text_page" value="<?php echo esc_attr( $options[ 'print_text_page' ] ); ?>" style="width: 40%;" />

Erick Hitter's avatar
Erick Hitter committed
582 583
							<p class="description"><?php _e( 'If viewing a multipage post (set by using the &lt;!--nextpage--&gt; tag), the text above is used for a link to print just the current page.', 'wp_print_friendly' ); ?></p>
							<p class="description"><?php _e( '<strong>To hide this link,</strong> clear the field\'s contents.', 'wp_print_friendly' ); ?></p>
584 585 586
						</td>
					</tr>
					<tr>
Erick Hitter's avatar
Erick Hitter committed
587
						<th scope="row"><?php _e( 'CSS for print links:', 'wp_print_friendly' ); ?></th>
588
						<td>
589 590
							<input type="text" name="<?php echo esc_attr( $this->settings_key ); ?>[css_class]" id="css_class" value="<?php echo esc_attr( $options[ 'css_class' ] ); ?>" style="width: 40%;" />

Erick Hitter's avatar
Erick Hitter committed
591 592
							<p class="description"><?php _e( 'For page-specific print links, a second class, created by appending <strong>_cur</strong> to the above text, is added to each link.', 'wp_print_friendly' ); ?></p>
							<p class="description"><?php _e( 'Be aware that Internet Explorer will only interpret the first two CSS classes, so if multiple classes are entered above, the page-specific class may not be available in IE.', 'wp_print_friendly' ); ?></p>
593 594 595
						</td>
					</tr>
					<tr>
Erick Hitter's avatar
Erick Hitter committed
596
						<th scope="row"><?php _e( 'Open print-friendly views:', 'wp_print_friendly' ); ?></th>
597
						<td>
Erick Hitter's avatar
Erick Hitter committed
598 599
							<input type="radio" name="<?php echo esc_attr( $this->settings_key ); ?>[link_target]" id="target-same" value="same"<?php checked( $options[ 'link_target' ], 'same', true ); ?> /> <label for="target-same"><?php _e( 'In the same window', 'wp_print_friendly' ); ?></label><br />
							<input type="radio" name="<?php echo esc_attr( $this->settings_key ); ?>[link_target]" id="target-new" value="new"<?php checked( $options[ 'link_target' ], 'new', true ); ?> /> <label for="target-new"><?php _e( 'In a new window', 'wp_print_friendly' ); ?></label>
600 601 602
						</td>
					</tr>
				</table>
603

604
				<h3>Endnote Options</h3>
605

606 607
				<table class="form-table">
					<tr>
Erick Hitter's avatar
Erick Hitter committed
608
						<th scope="row"><?php _e( 'Include endnotes for links found in content?', 'wp_print_friendly' ); ?></th>
609
						<td>
Erick Hitter's avatar
Erick Hitter committed
610
							<input type="checkbox" name="<?php echo esc_attr( $this->settings_key ); ?>[endnotes]" id="endnotes" value="1"<?php checked( $options[ 'endnotes' ], true, true ); ?> /> <label for="endnotes"><?php _e( 'Yes', 'wp_print_friendly' ); ?></label>
611

Erick Hitter's avatar
Erick Hitter committed
612
							<p class="description"><?php _e( 'If enabled, content is automatically scanned for links and an endnote is added for each link found. This can be helpful for users if your content includes many links.', 'wp_print_friendly' ); ?></p>
613 614 615
						</td>
					</tr>
					<tr>
Erick Hitter's avatar
Erick Hitter committed
616
						<th scope="row"><label for="endnotes-label"><?php _e( 'Endnotes heading:', 'wp_print_friendly' ); ?></label></th>
617
						<td>
618 619
							<input type="text" name="<?php echo esc_attr( $this->settings_key ); ?>[endnotes_label]" class="regular-text code" id="endnotes-label" value="<?php echo esc_attr( $options[ 'endnotes_label' ] ); ?>" />

Erick Hitter's avatar
Erick Hitter committed
620
							<p class="description"><?php _e( 'If endnotes are enabled, the text entered above will be output above the list of links.', 'wp_print_friendly' ); ?></p>
621 622 623
						</td>
					</tr>
				</table>
624

625 626 627 628
				<p class="submit">
					<input type="submit" class="button-primary" value="Save Changes" />
				</p>
			</form>
629

630 631 632
		</div><!-- .wrap -->
	<?php
	}
633

Erick Hitter's avatar
Erick Hitter committed
634
	/**
635
	 * Validate options
Erick Hitter's avatar
Erick Hitter committed
636
	 *
637 638 639 640
	 * @param array $options
	 * @uses $this::get_options, $this::post_types_array, delete_option, sanitize_text_field
	 * @return array
	 */
Erick Hitter's avatar
Erick Hitter committed
641
	public function admin_options_validate( $options ) {
642 643 644
		$new_options = array(
			'endnotes' => false
		);
645

Erick Hitter's avatar
Erick Hitter committed
646 647
		if ( is_array( $options ) ) {
			foreach ( $options as $key => $value ) {
648 649 650 651 652
				switch( $key ) {
					case 'auto':
					case 'endnotes':
						$new_options[ $key ] = (bool)$value;
					break;
653

654 655 656 657 658 659
					case 'placement':
						$placements = array(
							'above',
							'below',
							'both'
						);
660

661 662
						$new_options[ $key ] = in_array( $value, $placements ) ? $value : 'below';
					break;
663

664 665
					case 'post_types':
						$post_types = $this->post_types_array();
666

667
						$new_options[ $key ] = array();
668

Erick Hitter's avatar
Erick Hitter committed
669 670 671
						if ( is_array( $value ) && is_array( $post_types ) ) {
							foreach ( $post_types as $post_type ) {
								if ( in_array( $post_type->name, $value ) )
672 673 674 675
									$new_options[ $key ][] = $post_type->name;
							}
						}
					break;
676

677 678 679 680 681
					case 'print_text':
					case 'print_text_page':
					case 'css_class':
					case 'endnotes_label':
						$value = sanitize_text_field( $value );
682

Erick Hitter's avatar
Erick Hitter committed
683
						if ( $key == 'print_text' && empty( $value ) )
684
							$value = 'Print this entry';
685

686 687
						$new_options[ $key ] = $value;
					break;
688

689 690 691
					case 'link_target':
						$new_options[ $key ] = $value == 'new' ? 'new' : 'same';
					break;
692

693 694 695 696 697 698
					default:
						continue;
					break;
				}
			}
		}
699

700 701
		return $new_options;
	}
702

Erick Hitter's avatar
Erick Hitter committed
703 704 705
	/**
	 * Return plugin options array parsed with default options.
	 *
706 707 708
	 * @uses wp_parse_args, get_option
	 * @return array
	 */
Erick Hitter's avatar
Erick Hitter committed
709
	public function get_options() {
710
		$options = get_option( $this->settings_key, $this->settings_defaults );
711

Erick Hitter's avatar
Erick Hitter committed
712
		if ( ! array_key_exists( 'post_types', $options ) )
713
			$options[ 'post_types' ] = array();
714

715 716
		return wp_parse_args( $options, $this->settings_defaults );
	}
717

Erick Hitter's avatar
Erick Hitter committed
718 719 720
	/**
	 * Build array of available post types, excluding all builtin ones except 'post' and 'page'.
	 *
721 722 723
	 * @uses get_post_types
	 * @return array
	 */
Erick Hitter's avatar
Erick Hitter committed
724
	public function post_types_array() {
725
		$post_types = array();
Erick Hitter's avatar
Erick Hitter committed
726 727
		foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
			if ( $post_type->_builtin == false || $post_type->name == 'post' || $post_type->name == 'page' )
728 729
				$post_types[] = $post_type;
		}
730

731 732
		return $post_types;
	}
733

Erick Hitter's avatar
Erick Hitter committed
734
	/**
735
	 * Display admin notice regarding rewrite rules flush.
Erick Hitter's avatar
Erick Hitter committed
736
	 *
737 738 739 740
	 * @uses get_option, _e, __, admin_url, add_query_arg
	 * @action admin_notices
	 * @return html or null
	 */
Erick Hitter's avatar
Erick Hitter committed
741 742
	public function action_admin_notices_activation() {
		if ( ! get_option( $this->notice_key ) ):
743
		?>
744

745
		<div id="wpf-rewrite-flush-warning" class="error fade">
Erick Hitter's avatar
Erick Hitter committed
746
			<p><strong><?php _e( 'WP Print Friendly', 'wp_print_friendly' ); ?></strong></p>
747

Erick Hitter's avatar
Erick Hitter committed
748
			<p><?php printf( __( 'You must refresh your site\'s permalinks before WP Print Friendly is fully activated. To do so, go to <a href="%s">Permalinks</a> and click the <strong><em>Save Changes</em></strong> button at the bottom of the screen.', 'wp_print_friendly' ), admin_url( 'options-permalink.php' ) ); ?></p>
749

Erick Hitter's avatar
Erick Hitter committed
750
			<p><?php printf( __( 'When finished, click <a href="%s">here</a> to hide this message.', 'wp_print_friendly' ), admin_url( add_query_arg( $this->notice_key, 1, 'index.php' ) ) ); ?></p>
751
		</div>
752

753 754 755
		<?php
		endif;
	}
756

Erick Hitter's avatar
Erick Hitter committed
757
	/**
758
	 * Render page numbers, such as "Page 1 of 5."
Erick Hitter's avatar
Erick Hitter committed
759
	 *
760 761 762 763 764 765 766
	 * @param int $post_id
	 * @param string $before
	 * @param string $separator
	 * @param string $after
	 * @uses $this::is_print, get_query_var, get_post_field
	 * @return string or false
	 */
Erick Hitter's avatar
Erick Hitter committed
767 768
	public function page_numbers( $post_id = false, $before = 'Page ', $separator = ' of ', $after = '' ) {
		if ( ! $this->is_print() )
769
			return false;
770

771 772
		//Don't display on views that include all pages of a post
		$print = get_query_var( $this->query_var );
Erick Hitter's avatar
Erick Hitter committed
773
		if ( $print == 'all' || $print == '/all' || empty( $print ) )
774
			return false;
775

776 777
		//Get post ID and post content, or return false it either fails validation
		$post_id = intval( $post_id );
778

Erick Hitter's avatar
Erick Hitter committed
779
		if ( ! $post_id ) {
780 781 782 783
			global $post;
			$post_id = $post->ID;
			$post_content = $post->post_content;
		}
784

785
		$post_id = intval( $post_id );
786

Erick Hitter's avatar
Erick Hitter committed
787
		if ( ! $post_id )
788
			return false;
789

Erick Hitter's avatar
Erick Hitter committed
790
		if ( ! isset( $post_content ) || empty( $post_content ) )
791
			$post_content = get_post_field( 'post_content', $post_id );
792

Erick Hitter's avatar
Erick Hitter committed
793
		if ( ! is_string( $post_content ) || empty( $post_content ) || strpos( $post_content, '<!--nextpage-->' ) === false )
794
			return false;
795

796 797 798
		//Get current page
		$page = get_query_var( $this->query_var );
		$page = $page ? $page : 1;
799

800 801
		//Get total number of pages, or return false if total cannot be determined
		$num_pages = substr_count( $post_content, '<!--nextpage-->' );
802

Erick Hitter's avatar
Erick Hitter committed
803
		if ( is_int( $num_pages ) && $num_pages > 0 )
804 805 806
			$num_pages = $num_pages + 1;
		else
			return false;
807

808 809 810 811 812 813 814
		//Having made it this far, return the specified string
		return $before . $page . $separator . $num_pages . $after;
	}
}
global $wpf;
$wpf = new wp_print_friendly;

Erick Hitter's avatar
Erick Hitter committed
815
/**
816
 * Shortcut to function for generating post's printer-friendly format URL
Erick Hitter's avatar
Erick Hitter committed
817
 *
818 819 820 821 822 823 824
 * @param int $post_id
 * @param int $page
 * @uses $wpf
 * @return string or bool
 */
function wpf_get_print_url( $post_id = false, $page = false ) {
	global $wpf;
Erick Hitter's avatar
Erick Hitter committed
825
	if ( ! is_a( $wpf, 'wp_print_friendly' ) )
826
		$wpf = new wp_print_friendly;
827

828 829 830
	return $wpf->print_url( intval( $post_id ), intval( $page ) );
}

Erick Hitter's avatar
Erick Hitter committed
831
/**
832
 * Output link to printer-friendly post format.
Erick Hitter's avatar
Erick Hitter committed
833
 *
834 835 836 837 838 839 840 841 842 843 844 845 846
 * @param string $link_text
 * @param string $class
 * @param int $post_id
 * @param bool $page_link
 * @param string $page_link_separator
 * @param string $page_link_text
 * @param string $link_target
 * @uses $post, wpf_get_print_url, esc_attr, esc_url, get_query_var
 * @return string or null
 */
function wpf_the_print_link( $page_link = false, $link_text = 'Print this post', $class = 'print_link', $page_link_separator = ' | ', $page_link_text = 'Print this page', $link_target = 'same' ) {
	global $post;
	$url = wpf_get_print_url( $post->ID );
847

848
	$page_link = (bool)$page_link;
849

Erick Hitter's avatar
Erick Hitter committed
850
	if ( function_exists( 'is_view_all' ) && is_view_all() )
851
		$page_link = false;
852

Erick Hitter's avatar
Erick Hitter committed
853
	if ( $url ) {
854
		$link = '<a ' . ( $class ? 'class="' . esc_attr( $class ) . '"' : '' ) . ' href="' . esc_url( $url ) . '"' . ( $link_target == 'new' ? ' target="_blank"' : '' ) . '>' . $link_text . '</a>';
855

Erick Hitter's avatar
Erick Hitter committed
856
		if ( $page_link && strpos( $post->post_content, '<!--nextpage-->' ) !== false ) {
857 858 859 860
			$page = get_query_var( 'page' );
			$page = $page ? $page : 1;
			$link .= $page_link_separator . '<a ' . ( $class ? 'class="' . esc_attr( $class ) . '_cur ' . esc_attr( $class ) . '"' : '' ) . ' href="' . esc_url( wpf_get_print_url( $post->ID, $page ) ) . '"' . ( $link_target == 'new' ? ' target="_blank"' : '' ) . '>' . $page_link_text . '</a>';
		}
861

862 863 864 865
		echo $link;
	}
}

Erick Hitter's avatar
Erick Hitter committed
866
/**
867
 * Display page numbers, such as "Page 1 of 5."
Erick Hitter's avatar
Erick Hitter committed
868
 *
869 870 871 872 873 874 875 876 877
 * @param int $post_id
 * @param string $before
 * @param string $separator
 * @param string $after
 * @uses $wpf
 * @return string or false
 */
function wpf_the_page_numbers( $post_id = false, $before = 'Page ', $separator = ' of ', $after = '' ) {
	global $wpf;
Erick Hitter's avatar
Erick Hitter committed
878
	if ( ! is_a( $wpf, 'wp_print_friendly' ) )
879
		$wpf = new wp_print_friendly;
880

881 882 883
	echo $wpf->page_numbers( intval( $post_id ), $before, $separator, $after );
}

Erick Hitter's avatar
Erick Hitter committed
884 885
if ( ! function_exists( 'is_print' ) ) {
	/**
886
	 * Conditional tag indicating if printer-friendly format was requested.
Erick Hitter's avatar
Erick Hitter committed
887
	 *
888 889 890 891 892
	 * @uses $wpf
	 * @return bool
	 */
	function is_print() {
		global $wpf;
Erick Hitter's avatar
Erick Hitter committed
893
		if ( ! is_a( $wpf, 'wp_print_friendly' ) )
894
			$wpf = new wp_print_friendly;
895

896 897 898 899
		return $wpf->is_print();
	}
}
?>