to to fix target_new or target_blank issues. 2.1.5 - Fix conflict with link tracking plugins. Custom image support for hosted wordpress sites. 2.1.4 - wp head fix. 2.1.3 - Manual option for button placement. Security updates for multi-author sites. 2.1.2 - Improvements to Setting page layout and PrintFriendly button launching from post pages. 2.1.1 - Fixed admin settings bug. 2.1 - Update for mult-author websites. Improvements to Settings page. 2.0 - Customize the style, placement, and pages your printfriendly button appears. 1.5 - Added developer ability to disable hook and use the pf_show_link() function to better be used in a custom theme & Uninstall cleanup. 1.4 - Changed Name. 1.3 - Added new buttons, removed redundant code. 1.2 - User can choose to show or not show buttons on the listing page. */ /** * PrintFriendly WordPress plugin. Allows easy embedding of printfriendly.com buttons. * @package PrintFriendly_WordPress * @author PrintFriendly * @copyright Copyright (C) 2012, PrintFriendly */ if ( ! class_exists( 'PrintFriendly_WordPress' ) ) { /** * Class containing all the plugins functionality. * @package PrintFriendly_WordPress */ class PrintFriendly_WordPress { /** * The hook, used for text domain as well as hooks on pages and in get requests for admin. * @var string */ var $hook = 'printfriendly'; /** * The option name, used throughout to refer to the plugins option and option group. * @var string */ var $option_name = 'printfriendly_option'; /** * The plugins options, loaded on init containing all the plugins settings. * @var array */ var $options = array(); /** * Database version, used to allow for easy upgrades to / additions in plugin options between plugin versions. * @var int */ var $db_version = 2; /** * Settings page, used within the plugin to reliably load the plugins admin JS and CSS files only on the admin page. * @var string */ var $settings_page = ''; /** * Constructor * * @since 3.0 */ function __construct() { // delete_option( $this->option_name ); // Retrieve the plugin options $this->options = get_option( $this->option_name ); // If the options array is empty, set defaults if ( ! is_array( $this->options ) ) $this->set_defaults(); // If the version number doesn't match, upgrade if ( $this->db_version > $this->options['db_version'] ) $this->upgrade(); add_action( 'wp_head', array( &$this, 'front_head' ) ); // automaticaly add the link if( 'manual' != $this->options['show_list'] ) { add_filter( 'the_content', array( &$this, 'show_link' ) ); add_filter( 'the_excerpt', array( &$this, 'show_link' ) ); } if ( !is_admin() ) return; // Hook into init for registration of the option and the language files add_action( 'admin_init', array( &$this, 'init' ) ); // Register the settings page add_action( 'admin_menu', array( &$this, 'add_config_page' ) ); // Register the contextual help add_filter( 'contextual_help', array( &$this, 'contextual_help' ), 10, 2 ); // Enqueue the needed scripts and styles add_action( 'admin_enqueue_scripts',array( &$this, 'admin_enqueue_scripts' ) ); // Register a link to the settings page on the plugins overview page add_filter( 'plugin_action_links', array( &$this, 'filter_plugin_actions' ), 10, 2 ); } /** * PHP 4 Compatible Constructor * * @since 3.0 */ function PrintFriendly_WordPress() { $this->__construct(); } /** * Prints the PrintFriendly button CSS, in the header. * * @since 3.0 */ function front_head() { if ( !isset( $this->options['enable_css'] ) || $this->options['enable_css'] != 'on' ) return; ?> options['javascript']) && $this->options['javascript'] == 'no') return; else { $tagline = $this->options['tagline']; $image_url = $this->options['image_url']; if( $this->options['logo'] == 'favicon' ) { $tagline = ''; $image_url = ''; } // Currently we use v3 for both: normal and password protected sites $pf_src = '//cdn.printfriendly.com/printfriendly.js'; if($this->options['website_protocol'] == 'https') $pf_src = 'https://pf-cdn.printfriendly.com/ssl/main.js'; ?> options['show_list'] ) return ""; $onclick = 'onclick="window.print(); return false;"'; $href = 'http://www.printfriendly.com/print/v2?url='.get_permalink(); if ( isset( $this->options['javascript'] ) && $this->options['javascript'] == 'no' ) $onclick = 'target="_blank"'; if ( !is_singular() && '' != $onclick ) { $onclick = ''; $href = get_permalink().'?pfstyle=wp'; } $align = ''; if ( 'none' != $this->options['content_position'] ) $align = ' align'.$this->options['content_position']; $button = apply_filters( 'printfriendly_button', '' ); if (is_singular()) { // Hook the script call now, so it only get's loaded when needed, and need is determined by the user calling pf_button add_action( 'wp_footer', array( &$this, 'print_script_footer' ) ); } if ( 'manual' == $this->options['show_list'] ) { return $button; } else { if (is_single() || ( is_page() && 'posts' != $this->options['show_list'] ) || ((is_home() || is_category()) && 'all' == $this->options['show_list'] )) { if ( $this->options['content_placement'] == 'before' ) return $button.$content; else return $content.$button; } else { return $content; } } } /** * Register the textdomain and the options array along with the validation function * * @since 3.0 */ function init() { // Allow for localization load_plugin_textdomain( $this->hook, false, basename( dirname( __FILE__ ) ) . '/languages' ); // Register our option array register_setting( $this->option_name, $this->option_name, array( &$this, 'options_validate' ) ); } /** * Validate the saved options. * * @since 3.0 * @param array $input with unvalidated options. * @return array $valid_input with validated options. */ function options_validate( $input ) { $valid_input = $input; // echo '
'.print_r($input,1).'
'; // die; if ( !in_array( $input['button_type'], array( 'pf-button.gif', 'button-print-grnw20.png', 'button-print-blu20.png', 'button-print-gry20.png', 'button-print-whgn20.png', 'pf_button_sq_gry_m.png', 'pf_button_sq_gry_l.png', 'pf_button_sq_grn_m.png', 'pf_button_sq_grn_l.png', 'pf-button-big.gif', 'pf-icon-small.gif', 'pf-icon.gif', 'pf-button-both.gif', 'pf-icon-both.gif', 'text-only', 'custom-image') ) ) $valid_input['button_type'] = 'pf-button.gif'; if ( !isset( $input['custom_image'] ) ) $valid_input['custom_image'] = ''; if ( !in_array( $input['show_list'], array( 'all', 'single', 'posts', 'manual') ) ) $valid_input['show_list'] = 'all'; if ( !in_array( $input['content_position'], array( 'none', 'left', 'center', 'right' ) ) ) $valid_input['content_position'] = 'left'; if ( !in_array( $input['content_placement'], array( 'before', 'after' ) ) ) $valid_input['content_placement'] = 'after'; foreach ( array( 'margin_top', 'margin_right', 'margin_bottom', 'margin_left' ) as $opt ) $valid_input[$opt] = (int) $input[$opt]; $valid_input['text_size'] = (int) $input['text_size']; if ( !isset($valid_input['text_size']) || 0 == $valid_input['text_size'] ) { $valid_input['text_size'] = 14; } else if ( 25 < $valid_input['text_size'] || 9 > $valid_input['text_size'] ) { $valid_input['text_size'] = 14; add_settings_error( $this->option_name, 'invalid_color', __( 'The text size you entered is invalid, please stay between 9px and 25px', $this->hook ) ); } if ( !isset( $input['text_color'] )) { $valid_input['text_color'] = $this->options['text_color']; } else if ( ! preg_match('/^#[a-f0-9]{3,6}$/i', $input['text_color'] ) ) { // Revert to previous setting and throw error. $valid_input['text_color'] = $this->options['text_color']; add_settings_error( $this->option_name, 'invalid_color', __( 'The color you entered is not valid, it must be a valid hexadecimal RGB font color.', $this->hook ) ); } $valid_input['db_version'] = $this->db_version; return $valid_input; } /** * Register the config page for all users that have the manage_options capability * * @since 3.0 */ function add_config_page() { $this->settings_page = add_options_page( __( 'PrintFriendly Options', $this->hook ), __( 'Print Friendly & PDF', $this->hook ), 'manage_options', $this->hook, array( &$this, 'config_page' ) ); } /** * Shows help on the plugin page when clicking on the Help button, top right. * * @since 3.0 */ function contextual_help( $contextual_help, $screen_id ) { if ( $this->settings_page == $screen_id ) { $contextual_help = ''.__( "Need Help?", $this->hook ).'
' .sprintf( __( "Be sure to check out the %s!"), ''.__( "Frequently Asked Questions", $this->hook ).'' ); } return $contextual_help; } /** * Enqueue the scripts for the admin settings page * * @since 3.0 * @param string $hook_suffix hook to check against whether the current page is the PrintFriendly settings page. */ function admin_enqueue_scripts( $screen_id ) { if ( $this->settings_page == $screen_id ) { wp_register_script( 'pf-color-picker', plugins_url( 'colorpicker.js', __FILE__ ), array( 'jquery', 'media-upload' ) ); wp_register_script( 'pf-admin-js', plugins_url( 'admin.js', __FILE__ ), array( 'jquery', 'media-upload' ) ); wp_enqueue_script( 'pf-color-picker' ); wp_enqueue_script( 'pf-admin-js' ); wp_enqueue_style( 'printfriendly-admin-css', plugins_url( 'admin.css', __FILE__ ) ); } } /** * Register the settings link for the plugins page * * @since 3.0 * @param array $links the links for the plugins. * @param string $file filename to check against plugins filename. * @return array $links the links with the settings link added to it if appropriate. */ function filter_plugin_actions( $links, $file ){ // Static so we don't call plugin_basename on every plugin row. static $this_plugin; if ( ! $this_plugin ) $this_plugin = plugin_basename( __FILE__ ); if ( $file == $this_plugin ){ $settings_link = '' . __( 'Settings', $this->hook ) . ''; array_unshift( $links, $settings_link ); // before other links } return $links; } /** * Set default values for the plugin. If old, as in pre 1.0, settings are there, use them and then delete them. * * @since 3.0 */ function set_defaults() { // Set some defaults $this->options = array( 'button_type' => 'pf-button.gif', 'content_position' => 'left', 'content_placement' => 'after', 'custom_image' => 'http://cdn.printfriendly.com/pf-icon.gif', 'custom_text' => 'Print Friendly', 'enable_css' => 'on', 'margin_top' => '12', 'margin_right' => '12', 'margin_bottom' => '12', 'margin_left' => '12', 'show_list' => 'single', 'text_color' => '#6D9F00', 'text_size' => 14, 'logo' => 'favicon', 'image_url' => '', 'tagline' => '', 'click_to_delete' => '0', // 0 - allow, 1 - do not allow 'website_protocol' => 'http', 'password_protected' => 'no', 'javascript' => 'yes' ); // Check whether the old badly named singular options are there, if so, use the data and delete them. foreach ( array_keys( $this->options ) as $opt ) { $old_opt = get_option( 'pf_'.$opt ); if ( $old_opt !== false ) { $this->options[$opt] = $old_opt; delete_option( 'pf_'.$opt ); } } // This should always be set to the latest immediately when defaults are pushed in. $this->options['db_version'] = $this->db_version; update_option( $this->option_name, $this->options ); } /** * Upgrades the stored options, used to add new defaults if needed etc. * * @since 3.0 */ function upgrade() { // Do stuff // update options to version 2 if($this->options['db_version'] < 2) { $additional_options = array( 'enable_css' => 'on', 'logo' => 'favicon', 'image_url' => '', 'tagline' => '', 'click_to_delete' => '0', 'website_protocol' => 'http', 'password_protected' => 'no', 'javascript' => 'yes' ); // use old javascript_include value to initialize javascript if(!isset($this->options['javascript_include'])) $additional_options['javascript'] = 'no'; unset($this->options['javascript_include']); unset($this->options['javascript_fallback']); // correcting badly named option if(isset($this->options['disable_css'])) { $additional_options['enable_css'] = $this->options['disable_css']; unset($this->options['disable_css']); } // check whether image we do not list any more was used if(in_array($this->options['button_type'], array('button-print-whgn20.png', 'pf_button_sq_qry_m.png', 'pf_button_sq_qry_l.png', 'pf_button_sq_grn_m.png', 'pf_button_sq_grn_l.png'))) { // previous version had a bug with button name if(in_array($this->options['button_type'], array('pf_button_sq_qry_m.png', 'pf_button_sq_qry_l.png'))) $this->options['button_type'] = str_replace('qry', 'gry', $this->options['button_type']); $image_address = '//cdn.printfriendly.com/'.$this->options['button_type']; $this->options['button_type'] = 'custom-image'; $this->options['custom_text'] = ''; $this->options['custom_image'] = $image_address; } $this->options = array_merge($this->options, $additional_options); } $this->options['db_version'] = $this->db_version; update_option( $this->option_name, $this->options ); } /** * Displays radio button in the admin area * * @since 3.0 * @param string $name the name of the radio button to generate. * @param boolean $br whether or not to add an HTML
tag, defaults to true. */ function radio($name, $br = false){ $var = 'checked( 'button_type', $name, false ).'/>'; $button = $this->button( $name ); if ( '' != $button ) echo ''; else echo $var; if ( $br ) echo '
'; } /** * Displays button image in the admin area * * @since 3.0 * @param string $name the name of the button to generate. */ function button( $name = false ){ if( !$name ) $name = $this->options['button_type']; $text = $this->options['custom_text']; switch($name){ case "custom-image": if( '' == $this->options['custom_image'] ) $return = ''; else $return = 'Print Friendly'; $return .= $this->options['custom_text']; return $return; break; case "text-only": return ''.$text.''; break; case "pf-icon-both.gif": return 'Print Friendly Version of this pagePrint Get a PDF version of this webpagePDF'; break; case "pf-icon-small.gif": return 'PrintFriendly and PDF'.$text.''; break; case "pf-icon.gif": return 'PrintFriendly and PDF'.$text.''; break; default: return 'Print Friendly'; break; } } /** * Convenience function to output a value custom button preview elements * * @since 3.0.9 */ function custom_button_preview() { if( '' == trim($this->options['custom_image']) ) $button_preview = ''; else $button_preview = 'Print Friendly'; $button_preview .= ''.$this->options['custom_text'].''; echo $button_preview; } /** * Convenience function to output a value for an input * * @since 3.0 * @param string $val value to check. */ function val( $val ) { if ( isset( $this->options[$val] ) ) echo esc_attr( $this->options[$val] ); } /** * Like the WordPress checked() function but it doesn't throw notices when the array key isn't set and uses the plugins options array. * * @since 3.0 * @param mixed $val value to check. * @param mixed $check_against value to check against. * @param boolean $echo whether or not to echo the output. * @return string checked, when true, empty, when false. */ function checked( $val, $check_against = true, $echo = true ) { if ( !isset( $this->options[$val] ) ) return; if ( $this->options[$val] == $check_against ) { if ( $echo ) echo ' checked="checked" '; else return ' checked="checked" '; } } /** * Like the WordPress selected() function but it doesn't throw notices when the array key isn't set and uses the plugins options array. * * @since 3.0.9 * @param mixed $val value to check. * @param mixed $check_against value to check against. * @return string checked, when true, empty, when false. */ function selected( $val, $check_against = true) { if ( !isset( $this->options[$val] ) ) return; return selected ($this->options[$val], $check_against); } /** * Output the config page * * @since 3.0 */ function config_page() { // Since WP 3.2 outputs these errors by default, only display them when we're on versions older than 3.2 that do support the settings errors. global $wp_version; if ( version_compare( $wp_version, '3.2', '<' ) ) settings_errors(); // Show the content of the options array when debug is enabled if ( WP_DEBUG ) echo '
Options:

' . print_r( $this->options, 1 ) . '
'; ?>

hook ); ?>

option_name ); ?>

hook ); ?>

radio('pf-button.gif'); ?> radio('pf-button-both.gif'); ?> radio('pf-button-big.gif'); ?>
radio('button-print-grnw20.png'); ?> radio('button-print-blu20.png'); ?> radio('button-print-gry20.png'); ?>
radio('pf-icon-small.gif'); ?> radio('pf-icon-both.gif'); ?> radio('pf-icon.gif'); ?> radio('text-only'); ?>
hook ); ?>
Ex: /wp/wp-content/uploads/example.png)", $this->hook ); ?>
hook ); ?>
hook ); ?>
hook ); ?>
custom_button_preview(); ?>

hook ); ?>

hook ); ?>

hook ); ?>"/> hook ); ?>"/>

Need professional options for your corporate, education, or agency developed website? Check out PrintFriendly Pro.

hook ); ?> hook ); ?>. hook ); ?> support@PrintFriendly.com.

show_link(); }