wp-print-friendly.php 28.5 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.)
7
Version: 0.4.4.2
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

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

41 42 43 44 45 46 47 48
	/*
	 * Clean up after plugin deactivation.
	 * @uses flush_rewrite_rules, delete_option
	 * @action register_deactivation_hook
	 * @return null
	 */
	function deactivation_hook() {
		flush_rewrite_rules();
49

50 51 52
		delete_option( $this->settings_key );
		delete_option( $this->notice_key );
	}
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	/*
	 * Register actions and filters.
	 * @uses add_action, add_filter, get_option
	 * @action plugins_loaded
	 * @return null
	 */
	function action_plugins_loaded() {
		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' ) );
		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 );
71

72 73 74
		if( !get_option( $this->notice_key ) )
			add_action( 'admin_notices', array( $this, 'action_admin_notices_activation' ) );
	}
75

76 77 78 79 80 81 82 83
	/*
	 * Add print endpoint and rewrite rules for term taxonomy archives
	 * @uses add_rewrite_endpoint, $wp_rewrite, get_taxonomies, add_rewrite_rule
	 * @action init
	 * @return null
	 */
	function action_init() {
		add_rewrite_endpoint( $this->query_var, EP_ALL );
84

85
		global $wp_rewrite;
86 87

		//Taxonomies, since they aren't covered by add_rewrite_endpoint
88 89 90 91 92
		if( $wp_rewrite->permalink_structure ) {
			$taxonomies = get_taxonomies( array(), 'objects' );
			foreach( $taxonomies as $taxonomy => $args ) {
				if( $args->rewrite == false )
					continue;
93

94 95 96
				$taxonomy_slug = '';
				if( $args->rewrite[ 'with_front' ] && $wp_rewrite->front != '/' ) $taxonomy_slug .= $wp_rewrite->front;
				$taxonomy_slug .= $args->rewrite[ 'slug' ];
97

98
				$query_var = $args->query_var ? $args->query_var : 'taxonomy=' . $taxonomy . '&term';
99 100

				add_rewrite_rule( $taxonomy_slug . '/(.+)/' . $this->query_var . '(/([0-9]*))?/?$', $wp_rewrite->index . '?' . $query_var . '=$matches[1]&' . $this->query_var . '=$matches[3]', 'top' );
101 102
			}
		}
103 104 105 106 107 108 109 110 111

		//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' );
		}
112
	}
113

114 115 116 117 118 119 120 121
	/*
	 * Register plugin option and disable rewrite rule flush warning.
	 * @uses register_setting, update_option
	 * @action admin_init
	 * @return null
	 */
	function action_admin_init() {
		register_setting( $this->settings_key, $this->settings_key, array( $this, 'admin_options_validate' ) );
122

123 124 125
		if( isset( $_GET[ $this->notice_key ] ) )
			update_option( $this->notice_key, 1 );
	}
126

127 128 129 130 131 132 133 134 135
	/*
	 * Determine if print template is being requested.
	 * @uses $wp_query
	 * @return bool
	 */
	function is_print() {
		global $wp_query;
		return is_array( $wp_query->query ) && array_key_exists( $this->query_var, $wp_query->query );
	}
136

137 138 139 140 141 142 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.
	 * @uses get_queried_object, is_home, is_front_page, locate_template
	 * @return array or false
	 */
	function template_chooser() {
		//Get queried object to check post type
		$queried_object = get_queried_object();
146

147 148
		//Get plugin path
		$pluginpath = dirname( __FILE__ );
149

150 151 152 153 154 155 156 157 158 159
		if( ( is_home() || is_front_page() ) && ( '' !== ( $path = locate_template( 'wpf-home.php', false ) ) ) ) {
			$template = array(
				'name' => 'wpf-home',
				'path' => $path
			);
		}
		elseif(
			is_object( $queried_object ) &&
			property_exists( $queried_object, 'taxonomy' ) &&
			property_exists( $queried_object, 'slug' ) &&
160
			( '' !== ( $path = locate_template( array( 'wpf-' . $queried_object->taxonomy . '-' . $queried_object->slug . '.php', 'wpf-' . $queried_object->taxonomy . '.php' ), false ) ) )
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		)
			$template = array(
				'name' => 'wpf-' . $queried_object->taxonomy,
				'path' => $path
			);
		elseif(
			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
			);
		elseif(
			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
			);
		elseif( '' !== ( $path = locate_template( 'wpf.php', false ) ) )
			$template = array(
				'name' => 'wpf-default',
				'path' => $path
			);
		elseif( file_exists( $pluginpath . '/default-template.php' ) )
			$template = array(
				'name' => 'wpf-plugin-default',
				'path' => $pluginpath . '/default-template.php'
			);
195

196 197
		return isset( $template ) ? $template : false;
	}
198

199 200 201 202 203 204 205 206 207 208 209 210
	/*
	 * Detect request for print stylesheet on the homepage and reset query variables
	 * @param array $qv
	 * @filter request
	 * @return array
	 */
	function filter_request( $qv ) {
		if( array_key_exists( 'pagename', $qv ) && $qv[ 'pagename' ] == $this->query_var ) {
			$qv[ $this->query_var ] = '';
			unset( $qv[ 'page' ] );
			unset( $qv[ 'pagename' ] );
		}
211

212 213
		if( array_key_exists( $this->query_var, $qv ) && is_numeric( $qv[ $this->query_var ] ) )
			$qv[ 'page' ] = intval( $qv[ $this->query_var ] );
214

215 216
		return $qv;
	}
217

218 219 220 221 222 223 224 225 226
	/*
	 * Filter query when request to print specific page is made.
	 * @param object $query
	 * @action pre_get_posts
	 * @return object
	 */
	function action_pre_get_posts( $query ) {
		if( array_key_exists( $this->query_var, $query->query_vars ) && !empty( $query->query_vars[ $this->query_var ] ) ) {
			$qv = explode( '/', $query->query_vars[ $this->query_var ] );
227

228 229 230
			if( array_key_exists( 1, $qv ) && is_numeric( $qv[ 1 ] ) )
				$query->query_vars[ 'page' ] = (int)$qv[ 1 ];
		}
231

232 233
		return $query;
	}
234

235 236 237 238 239 240 241 242 243
	/*
	 * Filter template include to return print template if requested.
	 * @param string $template
	 * @filter template_include
	 * @return string
	 */
	function filter_template_include( $template ) {
		if( $this->is_print() && ( $print_template = $this->template_chooser() ) )
			$template = $print_template[ 'path' ];
244

245 246
		return $template;
	}
247

248 249 250 251 252 253 254 255 256 257 258 259 260
	/*
	 * Filter body classes to include references to print template.
	 * @param array $classes
	 * @filter body_class
	 * @return array
	 */
	function filter_body_class( $classes ) {
		if( $this->is_print() && ( $print_template = $this->template_chooser() ) ) {
			if( $print_template[ 'name' ] == 'default' )
				$classes[] = 'wpf';
			else
				$classes[] = $print_template[ 'name' ];
		}
261

262 263
		return $classes;
	}
264

265 266 267 268 269 270 271 272 273 274
	/*
	 * Filter post content to support printing entire post on one page.
	 * @param string $content
	 * @uses get_query_var
	 * @filter the_content
	 * @return string
	 */
	function filter_the_content( $content ) {
		if( $this->is_print() ) {
			$print = get_query_var( $this->query_var );
275

276 277
			if( $print == 'all' || $print == '/all' || empty( $print ) ) {
				global $post;
278

279 280 281 282 283 284 285
				$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);
			}
		}
286

287 288
		return $content;
	}
289

290 291 292 293 294 295 296 297 298
	/*
	 * Filter the content if automatic inclusion is selected
	 * @param string $content
	 * @uses $this::get_options, $post, $this::print_url, get_query_var, apply_filters
	 * @filter the_content
	 * @return string
	 */
	function filter_the_content_auto( $content ) {
		$options = $this->get_options();
299

300
		global $post;
301

302 303
		if( is_array( $options ) && array_key_exists( 'auto', $options ) && $options[ 'auto' ] == true && in_array( $post->post_type, $options[ 'post_types' ] ) && !$this->is_print() ) {
			extract( $options );
304

305 306
			//Basic URL
			$print_url = $this->print_url();
307

308 309 310 311
			//Page URL, if necessary
			if( !empty( $print_text_page ) && strpos( $post->post_content, '<!--nextpage-->' ) !== false ) {
				$page = get_query_var( 'page' );
				$page = $page ? $page : 1;
312

313 314
				$print_url_page = $this->print_url( false, $page );
			}
315

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

319 320 321 322
			if( isset( $print_url_page ) ) {
				$link .= ' | ';
				$link .= '<a class="' . $css_class . ' ' . $css_class . '_cur" href="' . $print_url_page . '"' . ( $link_target == 'new' ? ' target="_blank"' : '' ) . '>' . $print_text_page . '</a>';
			}
323

324
			$link .= '</p><!-- .wpf_wrapper -->';
325

326 327 328 329 330 331 332 333
			//Place link(s)
			if( $placement == 'above' )
				$content = $link . $content;
			elseif( $placement == 'below' )
				$content = $content . $link;
			elseif( $placement == 'both' )
				$content = $link . $content . $link;
		}
334

335 336
		return $content;
	}
337

338 339 340 341 342 343 344 345 346 347
	/*
	 * Convert links to endnotes if desired
	 * @param string $content
	 * @uses $this::is_print, $this::get_options
	 * @filter the_content
	 * @return string
	 */
	function filter_the_content_late( $content ) {
		if( $this->is_print() ) {
			global $post;
348

349
			$options = $this->get_options();
350

351 352 353 354
			//Endnotes
			if( $options[ 'endnotes' ] ) {
				$links = array();
				$i = 1;
355

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

359 360 361 362 363 364 365 366 367 368 369 370 371 372
				if(
					isset( $matches ) && is_array( $matches ) &&
					array_key_exists( 0, $matches ) && !empty( $matches[ 0 ] ) &&
					array_key_exists( 2, $matches ) && !empty( $matches[ 2 ] ) &&
					array_key_exists( 5, $matches ) && !empty( $matches[ 5 ] )
				) {
					//Format matches for replacement in content
					$replacements = array();
					foreach( $matches[ 0 ] as $key => $match ) {
						$replacements[ $match ] = array(
							'url' => $matches[ 2 ][ $key ],
							'title' => $matches[ 5 ][ $key ]
						);
					}
373

374 375 376 377 378 379
					//Replace links with endnote markers
					foreach( $replacements as $match => $args ) {
						$content = str_replace( $match, $args[ 'title' ] . '[' . $i . ']', $content );
						$links[ $i ] = $args;
						$i++;
					}
380

381 382 383 384 385 386 387 388 389 390
					//Output endnotes
					$endnotes = '<div class="wpf-endnotes">';
					$endnotes .= '<strong>' . $options[ 'endnotes_label' ] . '</strong>';
					$endnotes .= '<ol>';
					foreach( $links as $link ) {
						$endnotes .= '<li>';
						$endnotes .=  preg_replace( '#<img(.*)>#', '[Image]', $link[ 'title' ] ) . ': ' . esc_url( $link[ 'url' ] );
						$endnotes .= '</li>';
					}
					$endnotes .= '</ol></div><!-- .wpf-endnotes -->';
391

392 393 394 395
					$content .= $endnotes;
				}
			}
		}
396

397 398
		return $content;
	}
399

400 401 402 403 404 405 406 407 408 409
	/*
	 * Generate URL for post's printer-friendly format
	 * @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
	 */
	function print_url( $post_id = false, $page = false ) {
		if( $page === true )
			return false;
410

411 412
		if( function_exists( 'is_view_all' ) && is_view_all() )
			$page = false;
413

414
		$link = false;
415

416 417 418 419 420 421
		//Get link base specific to page type being viewed
		if( is_singular() || in_the_loop() ) {
			if( $post_id == false ) {
				global $post;
				$post_id = $post->ID;
			}
422

423 424
			if( !$post_id )
				return false;
425

426 427 428 429 430 431 432 433 434 435 436 437 438
			$link = get_permalink( $post_id );
		}
		elseif( is_home() || is_front_page() )
			$link = home_url( '/' );
		elseif( is_category() )
			$link = get_category_link( get_query_var( 'cat' ) );
		elseif( is_tag() )
			$link = get_tag_link( get_query_var( 'tag_id' ) );
		/* DISABLED FOR NOW AS PRINTING OF DATE-BASED ARCHIVES DOESN'T WORK YET
		elseif( is_date() ) {
			$year = get_query_var( 'year' );
			$monthnum = get_query_var( 'monthnum' );
			$day = get_query_var( 'day' );
439

440 441 442 443 444 445 446 447 448
			if( $day )
				$link = get_day_link( $year, $monthnum, $day );
			elseif( $monthnum )
				$link = get_month_link( $year, $monthnum );
			else
				$link = get_year_link( $year );
		}*/
		elseif( is_tax() ) {
			$queried_object = get_queried_object();
449

450 451 452
			if( is_object( $queried_object ) && property_exists( $queried_object, 'taxonomy' ) && property_exists( $queried_object, 'term_id' ) )
				$link = get_term_link( (int)$queried_object->term_id, $queried_object->taxonomy );
		}
453

454 455 456
		//If link base is set, build link
		if( $link !== false ) {
			global $wp_rewrite;
457

458
			$page = intval( $page );
459

460 461
			if( $wp_rewrite->using_permalinks() ) {
				$link = path_join( $link, $this->query_var );
462

463 464
				if( $page )
					$link = path_join( $link, intval( $page ) );
465

466 467 468 469 470
				if( $wp_rewrite->use_trailing_slashes )
					$link = trailingslashit( $link );
			}
			else {
				$link = add_query_arg( $this->query_var, is_numeric( $page ) ? intval( $page ) : 'all', $link );
471

472 473 474 475
				if( $page )
					$link = add_query_arg( 'page', is_numeric( $page ) ? intval( $page ) : 'all', $link );
			}
		}
476

477 478
		return $link;
	}
479

480 481 482 483 484 485 486 487 488
	/*
	 * Add menu item for options page
	 * @uses add_options_page
	 * @action admin_menu
	 * @return null
	 */
	function action_admin_menu() {
		add_options_page( 'WP Print Friendly Options', 'WP Print Friendly', 'manage_options', $this->ns, array( $this, 'admin_options' ) );
	}
489

490 491 492 493 494 495 496 497 498
	/*
	 * Render options page
	 * @uses settings_fields, $this::get_options, _e, checked, esc_attr
	 * @return html
	 */
	function admin_options() {
	?>
		<div class="wrap">
			<h2>WP Print Friendly</h2>
499

500 501 502 503
			<form action="options.php" method="post">
				<?php
					settings_fields( $this->settings_key );
					$options = $this->get_options();
504

505 506
					$post_types = $this->post_types_array();
				?>
507

508
				<h3>Display Options</h3>
509

510 511 512 513
				<table class="form-table">
					<tr>
						<th scope="row"><?php _e( 'Automatically add print links based on settings below?', $this->ns ); ?></th>
						<td>
514 515
							<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', $this->ns ); ?></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', $this->ns ); ?></label>
516 517 518 519 520
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e( 'Automatically place link:', $this->ns ); ?></th>
						<td>
521 522 523
							<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', $this->ns ); ?></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', $this->ns ); ?></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', $this->ns ); ?></label>
524 525 526 527 528 529
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e( 'Display automatically on:', $this->ns ); ?></th>
						<td>
							<?php foreach( $post_types as $post_type ): ?>
530
								<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 />
531 532 533 534
							<?php endforeach; ?>
						</td>
					</tr>
				</table>
535

536
				<h3>Link Options</h3>
537

538 539 540 541
				<table class="form-table">
					<tr>
						<th scope="row"><?php _e( 'Text for link to print entire item:', $this->ns ); ?></th>
						<td>
542
							<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%;" />
543 544 545 546 547
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e( 'Text for link to print current page:', $this->ns ); ?></th>
						<td>
548 549
							<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%;" />

550 551 552 553 554 555 556
							<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.', $this->ns ); ?></p>
							<p class="description"><?php _e( '<strong>To hide this link,</strong> clear the field\'s contents.', $this->ns ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e( 'CSS for print links:', $this->ns ); ?></th>
						<td>
557 558
							<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%;" />

559 560 561 562 563 564 565
							<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.', $this->ns ); ?></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.', $this->ns ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php _e( 'Open print-friendly views:', $this->ns ); ?></th>
						<td>
566 567
							<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', $this->ns ); ?></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', $this->ns ); ?></label>
568 569 570
						</td>
					</tr>
				</table>
571

572
				<h3>Endnote Options</h3>
573

574 575 576 577
				<table class="form-table">
					<tr>
						<th scope="row"><?php _e( 'Include endnotes for links found in content?', $this->ns ); ?></th>
						<td>
578 579
							<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', $this->ns ); ?></label>

580 581 582 583 584 585
							<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.', $this->ns ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="endnotes-label"><?php _e( 'Endnotes heading:', $this->ns ); ?></label></th>
						<td>
586 587
							<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' ] ); ?>" />

588 589 590 591
							<p class="description"><?php _e( 'If endnotes are enabled, the text entered above will be output above the list of links.', $this->ns ); ?></p>
						</td>
					</tr>
				</table>
592

593 594 595 596
				<p class="submit">
					<input type="submit" class="button-primary" value="Save Changes" />
				</p>
			</form>
597

598 599 600
		</div><!-- .wrap -->
	<?php
	}
601

602 603 604 605 606 607 608 609 610 611
	/*
	 * Validate options
	 * @param array $options
	 * @uses $this::get_options, $this::post_types_array, delete_option, sanitize_text_field
	 * @return array
	 */
	function admin_options_validate( $options ) {
		$new_options = array(
			'endnotes' => false
		);
612

613 614 615 616 617 618 619
		if( is_array( $options ) ) {
			foreach( $options as $key => $value ) {
				switch( $key ) {
					case 'auto':
					case 'endnotes':
						$new_options[ $key ] = (bool)$value;
					break;
620

621 622 623 624 625 626
					case 'placement':
						$placements = array(
							'above',
							'below',
							'both'
						);
627

628 629
						$new_options[ $key ] = in_array( $value, $placements ) ? $value : 'below';
					break;
630

631 632
					case 'post_types':
						$post_types = $this->post_types_array();
633

634
						$new_options[ $key ] = array();
635

636 637 638 639 640 641 642
						if( is_array( $value ) && is_array( $post_types ) ) {
							foreach( $post_types as $post_type ) {
								if( in_array( $post_type->name, $value ) )
									$new_options[ $key ][] = $post_type->name;
							}
						}
					break;
643

644 645 646 647 648
					case 'print_text':
					case 'print_text_page':
					case 'css_class':
					case 'endnotes_label':
						$value = sanitize_text_field( $value );
649

650 651
						if( $key == 'print_text' && empty( $value ) )
							$value = 'Print this entry';
652

653 654
						$new_options[ $key ] = $value;
					break;
655

656 657 658
					case 'link_target':
						$new_options[ $key ] = $value == 'new' ? 'new' : 'same';
					break;
659

660 661 662 663 664 665
					default:
						continue;
					break;
				}
			}
		}
666

667 668
		return $new_options;
	}
669

670 671 672 673 674 675 676
	/*
	 * Return plugin options array parsed with default options
	 * @uses wp_parse_args, get_option
	 * @return array
	 */
	function get_options() {
		$options = get_option( $this->settings_key, $this->settings_defaults );
677

678 679
		if( !array_key_exists( 'post_types', $options ) )
			$options[ 'post_types' ] = array();
680

681 682
		return wp_parse_args( $options, $this->settings_defaults );
	}
683

684 685 686 687 688 689 690 691 692 693 694
	/*
	 * Build array of available post types, excluding all builtin ones except 'post' and 'page'
	 * @uses get_post_types
	 * @return array
	 */
	function post_types_array() {
		$post_types = array();
		foreach( get_post_types( array(), 'objects' ) as $post_type ) {
			if( $post_type->_builtin == false || $post_type->name == 'post' || $post_type->name == 'page' )
				$post_types[] = $post_type;
		}
695

696 697
		return $post_types;
	}
698

699 700 701 702 703 704 705 706 707
	/*
	 * Display admin notice regarding rewrite rules flush.
	 * @uses get_option, _e, __, admin_url, add_query_arg
	 * @action admin_notices
	 * @return html or null
	 */
	function action_admin_notices_activation() {
		if( !get_option( $this->notice_key ) ):
		?>
708

709 710
		<div id="wpf-rewrite-flush-warning" class="error fade">
			<p><strong><?php _e( 'WP Print Friendly', $this->ns ); ?></strong></p>
711

712
			<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.', $this->ns ), admin_url( 'options-permalink.php' ) ); ?></p>
713

714 715
			<p><?php printf( __( 'When finished, click <a href="%s">here</a> to hide this message.', $this->ns ), admin_url( add_query_arg( $this->notice_key, 1, 'index.php' ) ) ); ?></p>
		</div>
716

717 718 719
		<?php
		endif;
	}
720

721 722 723 724 725 726 727 728 729 730 731 732
	/*
	 * Render page numbers, such as "Page 1 of 5."
	 * @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
	 */
	function page_numbers( $post_id = false, $before = 'Page ', $separator = ' of ', $after = '' ) {
		if( !$this->is_print() )
			return false;
733

734 735 736 737
		//Don't display on views that include all pages of a post
		$print = get_query_var( $this->query_var );
		if( $print == 'all' || $print == '/all' || empty( $print ) )
			return false;
738

739 740
		//Get post ID and post content, or return false it either fails validation
		$post_id = intval( $post_id );
741

742 743 744 745 746
		if( !$post_id ) {
			global $post;
			$post_id = $post->ID;
			$post_content = $post->post_content;
		}
747

748
		$post_id = intval( $post_id );
749

750 751
		if( !$post_id )
			return false;
752

753 754
		if( !isset( $post_content ) || empty( $post_content ) )
			$post_content = get_post_field( 'post_content', $post_id );
755

756 757
		if( !is_string( $post_content ) || empty( $post_content ) || strpos( $post_content, '<!--nextpage-->' ) === false )
			return false;
758

759 760 761
		//Get current page
		$page = get_query_var( $this->query_var );
		$page = $page ? $page : 1;
762

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

766 767 768 769
		if( is_int( $num_pages ) && $num_pages > 0 )
			$num_pages = $num_pages + 1;
		else
			return false;
770

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
		//Having made it this far, return the specified string
		return $before . $page . $separator . $num_pages . $after;
	}
}
global $wpf;
$wpf = new wp_print_friendly;

/*
 * Shortcut to function for generating post's printer-friendly format URL
 * @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;
	if( !is_a( $wpf, 'wp_print_friendly' ) )
		$wpf = new wp_print_friendly;
789

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	return $wpf->print_url( intval( $post_id ), intval( $page ) );
}

/*
 * Output link to printer-friendly post format.
 * @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 );
808

809
	$page_link = (bool)$page_link;
810

811 812
	if( function_exists( 'is_view_all' ) && is_view_all() )
		$page_link = false;
813

814 815
	if( $url ) {
		$link = '<a ' . ( $class ? 'class="' . esc_attr( $class ) . '"' : '' ) . ' href="' . esc_url( $url ) . '"' . ( $link_target == 'new' ? ' target="_blank"' : '' ) . '>' . $link_text . '</a>';
816

817 818 819 820 821
		if( $page_link && strpos( $post->post_content, '<!--nextpage-->' ) !== false ) {
			$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>';
		}
822

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
		echo $link;
	}
}

/*
 * Display page numbers, such as "Page 1 of 5."
 * @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;
	if( !is_a( $wpf, 'wp_print_friendly' ) )
		$wpf = new wp_print_friendly;
840

841 842 843 844 845 846 847 848 849 850 851 852 853
	echo $wpf->page_numbers( intval( $post_id ), $before, $separator, $after );
}

if( !function_exists( 'is_print' ) ) {
	/*
	 * Conditional tag indicating if printer-friendly format was requested.
	 * @uses $wpf
	 * @return bool
	 */
	function is_print() {
		global $wpf;
		if( !is_a( $wpf, 'wp_print_friendly' ) )
			$wpf = new wp_print_friendly;
854

855 856 857 858
		return $wpf->is_print();
	}
}
?>