-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvoce-post-pdfs.php
202 lines (159 loc) · 5.01 KB
/
voce-post-pdfs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
195
196
197
198
199
200
201
202
<?php
use Dompdf\Dompdf;
use Dompdf\Options;
if( file_exists( __DIR__ . '/vendor/autoload.php' ) ){
require_once __DIR__ . '/vendor/autoload.php';
}
class Voce_Post_PDFS {
const TEMPLATE = 'print.php';
/**
*
*/
public static function initialize() {
self::add_endpoints();
add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 2 );
add_filter( 'request', array( __CLASS__, 'request' ) );
add_filter( 'template_include', array( __CLASS__, 'template_include' ) );
}
/**
* Add 'print' and 'pdf' endpoints
*/
public static function add_endpoints() {
add_rewrite_endpoint( 'pdf', EP_PERMALINK );
}
/**
* Generate a pdf for the post when it is updated.
* @param $post_id
* @param $post
*/
public static function save_post( $post_id, $post ) {
if(
wp_is_post_autosave($post_id) ||
wp_is_post_revision($post_id) ||
! post_type_supports( $post->post_type, 'print_pdf' ) ||
isset($_REQUEST['bulk_edit'])
) {
return $post_id;
}
self::save_pdf( $post );
}
/**
* If 'pdf' request var is set. Set the value to true.
*
* @param $vars array of request variables
* @return array of request variables
*/
public static function request( $vars ) {
if ( isset( $vars['pdf'] ) )
$vars['pdf'] = true;
return $vars;
}
/**
* Override the default template
* @param $template path of the template file to load
* @return string the path to template to load
*/
public static function template_include( $template ) {
// redirect to the pdf or 404
if ( get_query_var( 'pdf' ) ) {
do_action( 'voce_post_pdfs_before_view_pdf' );
$template = self::view_pdf();
do_action( 'voce_post_pdfs_after_view_pdf' );
}
return $template;
}
private static function get_404_template() {
if ( ! $template = get_404_template() )
$template = get_index_template();
return $template;
}
/**
* Create the pdf if it does not exist. If the pdf creation
* fails do a 404 redirect. If the pdf creation is a success
* redirect to the pdf.
*/
private static function view_pdf() {
global $post;
if ( !post_type_supports( $post->post_type, 'print_pdf' ) )
return self::get_404_template();
$basepath = self::get_upload_basepath($post);
$baseurl = self::get_upload_baseurl($post);
$filename = apply_filters('voce_post_pdfs_save_filename', $post->post_name . '.pdf');
$file = $basepath . $filename;
// create pdf if it does not exist
if( ! file_exists( $file ) )
self::save_pdf( $post );
// redirect if the pdf exists
if( file_exists( $file ) ) {
wp_redirect( add_query_arg( 't', time(), $baseurl . $filename ));
die;
}
// 404 error if pdf does not exist
return locate_template( array( '404.php' ), false, false );;
}
public static function get_upload_basepath($post) {
$dir = self::wp_upload_dir();
$basepath = $dir['basedir'] . '/pdf/';
return apply_filters('voce_post_pdfs_upload_basepath', $basepath, $post);
}
public static function get_upload_baseurl($post) {
$dir = self::wp_upload_dir();
$baseurl = $dir['baseurl'] . '/pdf/';
return apply_filters('voce_post_pdfs_upload_baseurl', $baseurl, $post);
}
protected static function wp_upload_dir() {
static $wp_upload_dir = null;
if( is_null( $wp_upload_dir ) ) {
$wp_upload_dir = wp_upload_dir();
}
return $wp_upload_dir;
}
/**
* @param $post
* @param $overwrite boolean specify if the pdf should be overwritten, if it exists
* @return int a numer indicating the number of bytes written or FALSE on failure.
*/
public static function save_pdf( $post, $overwrite = true ) {
$args = apply_filters( 'voce_post_pdfs_save_query_args',
array(
'p' => $post->ID,
'post_type' => $post->post_type,
'post_status' => 'publish'
), $post );
// generate the html
query_posts( $args );
if( !have_posts() )
return;
$basepath = self::get_upload_basepath($post);
if( ! is_dir( $basepath ) )
mkdir( $basepath, 0777, true );
$filename = apply_filters('voce_post_pdfs_save_filename', $post->post_name . '.pdf');
$file = $basepath . $filename;
// check if pdf already exists
if ( !$overwrite && file_exists( $file ) )
return false;
ob_start();
$template_file = str_replace( TEMPLATEPATH, '', __DIR__ ) . DIRECTORY_SEPARATOR . self::TEMPLATE;
load_template( locate_template( apply_filters( 'voce_post_pdf_print_template', $template_file ) ), false );
$content = ob_get_clean();
wp_reset_query();
if( empty( $content ) )
return;
require_once __DIR__ . '/dompdf_config.custom.inc.php';
// Create new Dompdf Options object.
$options = new Options();
// Enable is remote to retreive assets that are referenced with an absolute URL.
$options->setIsRemoteEnabled( true );
// Generate the PDF
$dompdf = new Dompdf();
$dompdf->setOptions( $options );
$dompdf->load_html( $content );
$dompdf->set_paper( 'letter', 'portrait' );
$dompdf->render();
// save the pdf
return file_put_contents( $file, $dompdf->output() );
}
}
if ( function_exists( 'add_action' ) ) {
add_action( 'init', array( 'Voce_Post_PDFS', 'initialize' ) );
}