FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
embed-pdf-viewer/embed-pdf-viewer.php at develop · afragen/embed-pdf-viewer · GitHub
afragen
/
embed-pdf-viewer
Public
Notifications
You must be signed in to change notification settings
Fork
13
Star
41
Code
Issues
4
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
embed-pdf-viewer
/
embed-pdf-viewer.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
260 lines (231 loc) · 6.87 KB
Breadcrumbs
embed-pdf-viewer
/
embed-pdf-viewer.php
Copy path
File metadata and controls
260 lines (231 loc) · 6.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
/**
* Embed PDF Viewer
*
* @author Andy Fragen
* @license GPL-2.0+
* @link https://github.com/afragen/embed-pdf-viewer
* @package embed-pdf-viewer
*/
/**
* Plugin Name: Embed PDF Viewer
* Plugin URI: https://github.com/afragen/embed-pdf-viewer
* Description: Embed a PDF from the Media Library or elsewhere via oEmbed or as a block into an `iframe` tag.
* Author: Andy Fragen
* Author URI: https://github.com/afragen
* Version: 2.5.1
* License: GPLv2+
* Domain Path: /languages
* Text Domain: embed-pdf-viewer
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* GitHub Plugin URI: https://github.com/afragen/embed-pdf-viewer
* Requires PHP: 7.4
* Requires at least: 6.0
*/
/**
* Exit if called directly.
*/
if
( !
defined
(
'
ABSPATH
'
) ) {
die;
}
$
epd_version
=
get_file_data
(
__FILE__
, [
'
Version
'
=>
'
version
'
] )[
'
Version
'
];
add_filter
(
'
media_send_to_editor
'
, [ Embed_PDF_Viewer::
instance
(
$
epd_version
),
'
embed_pdf_media_editor
'
],
20
,
2
);
wp_embed_register_handler
(
'
oembed_pdf_viewer
'
,
'
#(^(https?)\:\/\/.+\.pdf$)#i
'
,
[
Embed_PDF_Viewer::
instance
(
$
epd_version
),
'
oembed_pdf_viewer
'
,
]
);
add_action
(
'
init
'
, [ Embed_PDF_Viewer::
instance
(
$
epd_version
),
'
register_block
'
] );
/**
* Class Embed_PDF_Viewer
*/
class
Embed_PDF_Viewer {
/**
* For singleton.
*
* @var bool
*/
private
static
$
instance
=
false
;
/**
* Plugin version number.
*
* @var string
*/
private
static
$
version
=
''
;
/**
* Create singleton.
*
* @param string $version Plugin version number.
* @return bool
*/
public
static
function
instance
(
$
version
) {
static
::
$
version
=
$
version
;
if
(
false
===
static
::
$
instance
) {
static
::
$
instance
=
new
self
(
$
version
);
}
return
static
::
$
instance
;
}
/**
* Register block.
*
* @return void
*/
public
function
register_block
() {
register_block_type
(
__DIR__
.
'
/blocks/block.json
'
,
[
'
render_callback
'
=> [
static
::
$
instance
,
'
dynamic_render_callback
'
] ]
);
}
/**
* Dynamically render callback based on browser.
*
* @global bool $is_chrome Tests for Chrome browser.
*
* @param array $attributes Array of attributes.
*
* @return string
*/
public
function
dynamic_render_callback
(
$
attributes
) {
global
$
is_chrome
;
$
url
=
$
attributes
[
'
url
'
] ??
null
;
if
( !
$
url
) {
return
''
;
}
$
classes
=
'
embed-pdf-viewer
'
;
$
src
=
$
is_chrome
||
wp_is_mobile
() ?
'
https://docs.google.com/viewer?url=
'
.
rawurlencode
(
$
url
) .
'
&embedded=true
'
:
$
url
;
$
description
=
$
attributes
[
'
title
'
] ??
''
;
$
description
=
$
attributes
[
'
description
'
] ??
$
description
;
$
width
=
$
attributes
[
'
width
'
] ??
'
600
'
;
$
height
=
$
attributes
[
'
height
'
] ??
'
600
'
;
$
iframe
=
sprintf
(
'
<iframe class="%1$s" src="%2$s" height="%3$s" width="%4$s" title="%5$s"%6$s></iframe>
'
,
$
classes
,
sanitize_url
(
$
src
),
esc_attr
(
$
height
),
esc_attr
(
$
width
),
esc_attr
(
$
description
),
$
is_chrome
||
wp_is_mobile
() ?
'
frameborder="0"
'
:
''
);
$
wrapper
=
get_block_wrapper_attributes
( [
'
class
'
=>
'
embed-pdf-viewer-wrapper
'
] );
return
sprintf
(
'
<div %1$s>%2$s</div>
'
,
$
wrapper
,
$
iframe
);
}
/**
* Insert URL to PDF from Media Library, then render as oEmbed.
*
* @param string $html an href link to the media.
* @param integer $id post_id.
*
* @return string
*/
public
function
embed_pdf_media_editor
(
$
html
,
$
id
) {
$
post
=
get_post
(
$
id
);
if
(
'
application/pdf
'
!==
$
post
->
post_mime_type
) {
return
$
html
;
}
return
$
post
->
guid
.
"\n\n"
;
}
/**
* Create oEmbed code.
*
* @param array $matches Regex matches.
* @param array $atts array of media height/width.
* @param string $url URI for media file.
*
* @return string|WP_Error
*/
public
function
oembed_pdf_viewer
(
$
matches
,
$
atts
,
$
url
) {
$
attachment_id
=
$
this
->
get_attachment_id_by_url
(
$
url
);
if
( !
empty
(
$
attachment_id
) ) {
$
post
=
get_post
(
$
attachment_id
);
}
else
{
/*
* URL is from outside of the Media Library.
*/
$
response
=
wp_safe_remote_get
(
$
url
);
if
(
is_wp_error
(
$
response
) ) {
return
$
response
;
}
$
post
=
new
WP_Post
(
new
stdClass
() );
$
post
->
guid
=
$
matches
[
0
];
$
post
->
post_mime_type
=
wp_remote_retrieve_header
(
$
response
,
'
content-type
'
);
$
post
->
post_name
=
preg_replace
(
'
/\.pdf$/
'
,
''
,
basename
(
$
matches
[
0
] ) );
}
wp_enqueue_style
(
'
embed-pdf-viewer
'
,
plugins_url
(
'
blocks/build/style-index.css
'
,
__FILE__
),
[],
static
::
$
version
);
return
$
this
->
create_output
(
$
post
,
$
atts
);
}
/**
* Create output for iframe and/or Google Doc Viewer and href link to file.
*
* @param \WP_Post $post Current post.
* @param array|string $atts array of media height/width or
* href to media library asset.
*
* @return bool|string
*/
private
function
create_output
(
WP_Post
$
post
,
$
atts
= [] ) {
global
$
is_chrome
;
if
(
'
application/pdf
'
!==
$
post
->
post_mime_type
) {
return
$
atts
;
}
$
default
= [
'
height
'
=>
500
,
'
width
'
=>
800
,
'
title
'
=>
$
post
->
post_title
,
'
description
'
=>
$
post
->
post_content
,
];
/*
* Ensure $atts isn't the href.
*/
$
atts
=
is_array
(
$
atts
) ?
$
atts
: [];
if
(
isset
(
$
atts
[
'
width
'
] ) ) {
$
atts
[
'
width
'
] =
'
100%
'
;
}
$
atts
=
array_merge
(
$
default
,
$
atts
);
/**
* Filter PDF attributes.
*
* @since 1.6.0
* @param array $atts Array of PDF attributes.
* @return array $atts
*/
$
atts
=
apply_filters
(
'
embed_pdf_viewer_pdf_attributes
'
,
$
atts
);
// Fix title or create from filename.
$
atts
[
'
title
'
] =
empty
(
$
atts
[
'
title
'
] )
?
ucwords
(
preg_replace
(
'
/(-|_)/
'
,
'
'
,
$
post
->
post_name
) )
:
ucwords
(
preg_replace
(
'
/(-|_)/
'
,
'
'
,
$
atts
[
'
title
'
] ) );
$
description
= !
empty
(
$
atts
[
'
description
'
] ) ?
$
atts
[
'
description
'
] :
$
atts
[
'
title
'
];
$
sanitized_url
=
sanitize_url
(
$
post
->
guid
);
if
(
$
is_chrome
||
wp_is_mobile
() ) {
$
iframe
=
'
<iframe class="embed-pdf-viewer" src="https://docs.google.com/viewer?url=
'
.
rawurlencode
(
$
sanitized_url
);
$
iframe
.=
'
&embedded=true" frameborder="0"
'
;
}
else
{
$
iframe
=
'
<iframe class="embed-pdf-viewer" src="
'
.
$
sanitized_url
.
'
"
'
;
}
$
iframe
.=
'
height="
'
.
esc_attr
(
$
atts
[
'
height
'
] ) .
'
" width="
'
.
esc_attr
(
$
atts
[
'
width
'
] ) .
'
"
'
;
$
iframe
.=
'
title="
'
.
esc_attr
(
$
description
) .
'
"></iframe>
'
.
"\n"
;
$
embed
=
'
<div>
'
;
$
embed
.=
$
iframe
;
$
embed
.=
'
<p><a href="
'
.
$
sanitized_url
.
'
" title="
'
.
esc_attr
(
$
description
) .
'
">
'
.
esc_html
(
$
description
) .
'
</a></p>
'
;
$
embed
.=
'
</div>
'
;
return
$
embed
;
}
/**
* Get attachment id by url.
*
* @param string $url URI of attachment.
*
* @return int|null
*/
private
function
get_attachment_id_by_url
(
$
url
) {
$
attachment
=
attachment_url_to_postid
(
$
url
);
return
empty
(
$
attachment
) ?
null
:
$
attachment
;
}
}
Back
|
FazBrowse Home
|
New Git URL