Created
February 23, 2017 06:21
-
-
Save adaptifyDesigns/0abecfa45cbfd7afc490cb9e47d2c5c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WUWO css2pdf implementation | |
Plugin URI: | |
Description: Using the css2pdf library for generating client side WUWO Session Plan PDF export files. | |
Author: Colin Safranek / css2pdf | |
Version: 1.0 | |
Author URI: https://github.com/Xportability/css-to-pdf | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
// Register plugin scripts | |
// ============================================================================= | |
function wuwo_enqueue_css2pdf_scripts() | |
{ | |
$path = plugin_dir_url( __FILE__ ); | |
// xepOnline.jqPlugin.js | |
wp_register_script( 'wuwo-css2pdf', $path . 'js/xepOnline.jqPlugin.js', array( 'jquery' ), '1.0.0', true ); | |
// css2pdf-onlick.js | |
wp_register_script( 'wuwo-css2pdf-onClick', $path . 'js/css2pdf-onlick.js', array( 'wuwo-css2pdf' ), '1.0.0', true ); | |
// } | |
} | |
add_action( 'wp_enqueue_scripts', 'wuwo_enqueue_css2pdf_scripts' ); | |
/** | |
* Rewrite Endpoint | |
*/ | |
function wuwo_css2pdf_endpoint() | |
{ | |
add_rewrite_endpoint( 'pdf', EP_ALL ); | |
} | |
add_action( 'init', 'wuwo_css2pdf_endpoint' ); | |
// This is required, otherwise, a ?pdf query var with no value evaluates to FALSE, as if it did not exist. | |
function wuwo_css2pdf_rewrite_filter_request( $vars ) | |
{ | |
if( isset( $vars['pdf'] ) ) | |
$vars['pdf'] = true; | |
return $vars; | |
} | |
add_filter( 'request', 'wuwo_css2pdf_rewrite_filter_request' ); | |
/** | |
* PDF Templates | |
*/ | |
function wuwo_css2pdf_templates( $template ) | |
{ | |
write_log( 'wuwo-css2pdf - Hooking into template_include... Initial template used: ' ); | |
write_log( $template ); | |
// if there is no "/pdf/" endpoint, or "/?pdf" query var, | |
// then use the default template: | |
if( ! get_query_var( 'pdf' ) ) | |
{ | |
write_log( 'wuwo-css2pdf - No "/pdf" endpoint was detected, or it returned FALSE.' ); | |
return $template; | |
} | |
// locate proper template file | |
// NOTE: this only works if the standard template file exists as well | |
// i.e. to use single-product-pdf.php you must also have single-product.php | |
$pdf_template = str_replace( '.php', '-pdf.php', basename( $template ) ); | |
if( file_exists( get_stylesheet_directory() . '/' . $pdf_template ) ) | |
{ | |
$template_path = get_stylesheet_directory() . '/' . $pdf_template; | |
} | |
else if( file_exists( get_template_directory() . '/' . $pdf_template ) ) | |
{ | |
$template_path = get_template_directory() . '/' . $pdf_template; | |
} | |
else if( file_exists( plugin_dir_path(__FILE__) . $pdf_template ) ) | |
{ | |
$template_path = plugin_dir_path(__FILE__) . $pdf_template; | |
} | |
else | |
{ | |
return $template; | |
} | |
// disable scripts and stylesheets | |
// NOTE: We do this to get rid of all extraneous styles and scripts. | |
// This way you have to define your own PDF styles using <style> tags in the template. | |
add_action('wp_print_styles', 'wuwo_css2pdf_remove_dep_arrays', ~PHP_INT_MAX); | |
write_log( 'wuwo-css2pdf - After PDF template redirect magic, this is the new template: ' ); | |
write_log( $template_path ); | |
return $template_path; | |
} | |
add_action( 'template_include', 'wuwo_css2pdf_templates', 100 ); | |
// Function for removing all styles and scripts: | |
function wuwo_css2pdf_remove_dep_arrays() | |
{ | |
global $wp_styles; | |
// write_log( 'Hooking into template_redirect... $wp_styles array is thus: ' ); | |
// write_log( $wp_styles ); | |
$wp_styles = array(); | |
// Now enqueue our additional necessary scripts: | |
// xepOnline.jqPlugin.js | |
wp_enqueue_script( 'wuwo-css2pdf' ); | |
// css2pdf-onlick.js | |
wp_enqueue_script( 'wuwo-css2pdf-onClick' ); | |
} | |
// Define main wuwo_css2pdf_active function | |
// this can be used to check if the plugin is active from within a page template: | |
function wuwo_css2pdf_active() | |
{ | |
return true; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment