This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
forked from afragen/embed-pdf-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-pdf-viewer.php
178 lines (155 loc) · 4.5 KB
/
wp-pdf-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
<?php
/**
* Plugin Name: WP PDF oEmbed
* Plugin URI: https://github.com/udx/wp-pdf-oembed
* Description: Embed a PDF from the Media Library or elsewhere via oEmbed into an `object` tag or Google Doc Viewer as fallback.
* Author: UsabilityDynamics, inc.
* Author URI: https://github.com/udx
* Version: 1.0.0
* License: GPLv2+
* Domain Path: /languages
* Text Domain: wp-pdf-oembed
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* GitHub Plugin URI: https://github.com/udx/wp-pdf-oembed
* GitHub Branch: latest
* Requires PHP: 5.3
* Requires WP: 4.0
*/
/**
* Exit if called directly.
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
add_filter( 'media_send_to_editor', array( Embed_PDF_Viewer::instance(), 'embed_pdf_media_editor' ), 20, 2 );
wp_embed_register_handler( 'oembed_pdf_viewer', '#(^(https?)\:\/\/.+\.pdf$)#i', array(
Embed_PDF_Viewer::instance(),
'oembed_pdf_viewer',
) );
/**
* Class Embed_PDF_Viewer
*/
class Embed_PDF_Viewer {
/**
* For singleton.
*
* @var bool
*/
private static $instance = false;
/**
* Create singleton.
*
* @return bool
*/
public static function instance() {
if ( false === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* 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
* @param array $atts array of media height/width.
* @param string $url URI for media file.
*
* @return string
*/
public function oembed_pdf_viewer( $matches, $atts, $url ) {
$attachment_id = $this->get_attachment_id_by_url( $url );
if ( ! empty( $attachment_id ) ) {
$post = get_post( $this->get_attachment_id_by_url( $url ) );
} else {
/*
* URL is from outside of the Media Library.
*/
$post = new WP_Post( new stdClass() );
$post->guid = $matches[0];
$post->post_mime_type = 'application/pdf';
$post->post_name = preg_replace( '/\.pdf$/', '', basename( $matches[0] ) );
}
return $this->create_output( $post, $atts );
}
/**
* Create output for Google Doc Viewer and href link to file.
*
* @param \WP_Post $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 = array() ) {
if ( 'application/pdf' !== $post->post_mime_type ) {
return $atts;
}
$atts = array(
'title' => $post->post_title,
);
/*
* Create title from filename.
*/
if ( empty( $atts['title'] ) ) {
$atts['title'] = ucwords( preg_replace( '/(-|_)/', ' ', $post->post_name ) );
}
$iframe_fallback = '<iframe class="wp-pdf-oembed" src="https://docs.google.com/viewer?url=' . urlencode( $post->guid );
$iframe_fallback .= '&embedded=true" frameborder="0" ';
$iframe_fallback .= 'style="height:600px;width:100%" ';
$iframe_fallback .= 'title="' . $atts['title'] . '"></iframe>' . "\n";
/*
* Because <object> doesn't scroll in iOS
* Hide <object> in iOS and hide <iframe> in not iOS
*/
$style = '<style>
@media only screen and (max-device-width: 1024px) {
object.wp-pdf-oembed { display:none; }
}
@media only screen and (min-device-width : 1024px) {
iframe.wp-pdf-oembed { display:none; }
object iframe.wp-pdf-oembed { display:block; }
}
</style>';
$object = '<object class="wp-pdf-oembed" data="' . $post->guid;
$object .= '#scrollbar=1&toolbar=1"';
$object .= 'type="application/pdf" ';
$object .= 'height=600 width=100% > ';
$object .= $iframe_fallback;
$object .= '</object>';
$embed = $object;
$embed .= $style . $iframe_fallback;
return $embed;
}
/**
* Get attachment id by url. Thanks Pippin.
*
* @link https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
*
* @param string $url URI of attachment.
*
* @return mixed
*/
private function get_attachment_id_by_url( $url ) {
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $url ) );
if ( empty( $attachment ) ) {
return null;
}
return $attachment[0];
}
}