forked from TwisterMc/pdfjs-viewer-shortcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfjs-viewer.php
279 lines (251 loc) · 6.87 KB
/
pdfjs-viewer.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Plugin Name: PDFjs Viewer
* Plugin URI: https://byterevel.com
* Description: Embed PDFs with the gorgeous PDF.js viewer
* Version: 1.4
* Requires at least: 4.9
* Requires PHP: 5.6
* Author: Ben Lawson
* Author URI: https://byterevel.com
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: pdfjs-viewer-shortcode
* Domain Path: /languages
*
* @package PdfJsViewer
*/
/**
* Exit early if directly accessed via URL.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The full path and filename of this plugin bootstrap file with symlinks resolved.
*
* @var string PDFJS_FILE
*/
define( 'PDFJS_FILE', __FILE__ );
/**
* Load plugin textdomain.
*/
function pdfjs_load_plugin_textdomain() {
load_plugin_textdomain(
'pdfjs-viewer-shortcode',
false,
basename( dirname( PDFJS_FILE ) ) . '/languages'
);
}
add_action( 'plugins_loaded', 'pdfjs_load_plugin_textdomain' );
/**
* PdfJs Shortcode callback.
*
* @param array $attr The shortcode attributes.
* @param string $content The shortcode content (if any).
* @param string $tag The name of the shortcode.
*
* @return string
*/
function pdfjs_shortcode_handler( $attr = array(), $content = '', $tag = '' ) {
/**
* Combine user attributes with known attributes and fill in defaults when needed.
*/
$attr = shortcode_atts(
array(
'url' => '',
'title' => __( 'Embedded PDF Document', 'pdfjs-viewer-shortcode' ),
'viewer_height' => '1360px',
'viewer_width' => '100%',
'fullscreen' => true,
'download' => true,
'print' => true,
'openfile' => false,
'page' => 1,
'zoom' => null,
'nameddest' => null,
'pagemode' => 'none',
),
$attr
);
return pdfjs_generator( $attr );
}
add_shortcode( 'pdfjs-viewer', 'pdfjs_shortcode_handler' );
/**
* Construct the markup for the shortcode to return.
*
* @param array $attr The shortcode attributes.
*
* @return string
*/
function pdfjs_generator( $attr ) {
/**
* The URL to the PDF.js viewer.
*
* @var string $viewer_url
*/
$viewer_url = add_query_arg(
array(
'file' => pdfjs_encode_uri_component( $attr['url'] ),
'download' => pdfjs_bool_to_string( $attr['download'] ),
'print' => pdfjs_bool_to_string( $attr['print'] ),
'openfile' => pdfjs_bool_to_string( $attr['openfile'] ),
),
plugins_url( 'resources/js/pdfjs/web/viewer.php', PDFJS_FILE )
);
/**
* Fragment Identifiers to append to the Viewer URL.
*
* @var array $fragments
*/
$fragments = array();
/**
* Array of whitelisted fragment identifier keys.
*
* @var array $fragment_identifier_keys
*/
$fragment_identifier_keys = array(
'page',
'zoom',
'nameddest',
'pagemode',
);
/**
* Loop through the frag id keys to see if we have an attribute value.
*/
foreach ( $fragment_identifier_keys as $fragment_identifier_key ) {
if ( isset( $attr[ $fragment_identifier_key ] ) && ! empty( $attr[ $fragment_identifier_key ] ) ) {
$fragments[ $fragment_identifier_key ] = $attr[ $fragment_identifier_key ];
}
}
/**
* Edge case for Gutenberg block,
* allowing a "custom zoom" to be set
* if a standard zoom level isn't sufficient.
*/
if ( isset( $attr['zoom'] ) && 'custom' === $attr['zoom'] && isset( $attr['customZoom'] ) ) {
$fragments['zoom'] = $attr['customZoom'];
}
/**
* Build the fragment identifier string in the way that PDF.js viewer expects.
*
* Multiple values of either type can be combined by
* separating with an ampersand (&) after the hash (for example: #page=2&zoom=200).
*
* @link https://github.com/mozilla/pdf.js/wiki/Viewer-options
*/
if ( ! empty( $fragments ) ) {
$viewer_url .= '#' . http_build_query( $fragments );
}
/**
* Anchor markup to the fullscreen viewer.
*
* @var $fullscreen_link
*/
$fullscreen_link = '';
if ( true === pdfjs_string_to_bool( $attr['fullscreen'] ) ) {
$fullscreen_link = sprintf(
'<a href="%1$s">%2$s</a><br />',
esc_url( $viewer_url ),
esc_html__( 'View Fullscreen', 'pdfjs-viewer-shortcode' )
);
}
return sprintf(
'%1$s<iframe width="%2$s" height="%3$s" src="%4$s" title="%5$s"></iframe>',
$fullscreen_link,
esc_attr( $attr['viewer_width'] ),
esc_attr( $attr['viewer_height'] ),
esc_url( $viewer_url ),
esc_attr( $attr['title'] )
);
}
/**
* Add the media button.
*
* Priority is 12 since default button is 10.
*/
function pdfjs_media_button() {
echo '<a href="#" id="insert-pdfjs" class="button">' . esc_html__( 'Add PDF', 'pdfjs-viewer-shortcode' ) . '</a>';
}
add_action( 'media_buttons', 'pdfjs_media_button', 12 );
/**
* Include the media button handler script.
*/
function pdfjs_include_media_button_js_file() {
wp_enqueue_script(
'media_button',
plugins_url( 'resources/js/pdfjs-media-button.js', PDFJS_FILE ),
array( 'jquery' ),
'1.0',
true
);
}
add_action( 'wp_enqueue_media', 'pdfjs_include_media_button_js_file' );
/**
* Encode URL compatible with JavaScript's encodeURIComponent.
*
* PHP's rawurlencode escapes all non-alphanumeric characters except,
* -_.~
*
* JavaScript's encodeURIComponent escapes all non-alphanumeric characters except the following,
* -_.!~*'()
*
* According to PDF.js documentation,
* "the path of the PDF file to use (must be on the same server due to JavaScript limitations).
* Please notice that the path/URL must be encoded using encodeURIComponent."
*
* @link https://github.com/mozilla/pdf.js/wiki/Viewer-options
*
* @param string $url The URL to encode. This should be a raw, unencoded URL.
*
* @return string
*/
function pdfjs_encode_uri_component( $url ) {
if ( empty( $url ) ) {
return '';
}
/**
* Characters that JS' encodeURIComponent does not encode,
* but PHP's rawurlencode does.
*
* @var array $revert
*/
$unescape = array(
'%21' => '!',
'%2A' => '*',
'%27' => "'",
'%28' => '(',
'%29' => ')',
);
return strtr( rawurlencode( $url ), $unescape );
}
/**
* Get the string 'true' or 'false from a true boolean value.
*
* @param bool $variable Value to evaluate as 'true' or 'false'.
*
* @return string Returns 'true' for true. Returns 'false' otherwise.
*/
function pdfjs_bool_to_string( $variable ) {
return ( true === pdfjs_string_to_bool( $variable ) ) ? 'true' : 'false';
}
/**
* Get the true boolean value of 'true' or 'false' strings.
*
* @param mixed $variable Value to evaluate as true or false.
*
* @return bool Returns true for "1", "true", "on" and "yes". Returns false otherwise.
*/
function pdfjs_string_to_bool( $variable ) {
return filter_var( $variable, FILTER_VALIDATE_BOOLEAN );
}
/**
* Full path to the PDF.js block init file.
*/
$pdfjs_block_include = __DIR__ . '/resources/js/blocks/pdfjs.php';
/**
* Require the PDF.js block init file if it is readable.
*/
if ( is_readable( $pdfjs_block_include ) ) {
require $pdfjs_block_include;
}