-
Notifications
You must be signed in to change notification settings - Fork 1
/
docViewer.php
166 lines (126 loc) · 5.44 KB
/
docViewer.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
<?php
/**
* Plugin Name: docViewer
* Plugin URI: https://sorensenjg.com/docViewer
* Description: A document viewer for wordpress that handles jpg, png, gif, pdf, doc, xls and ppt files
* Version: 1.0.0
* Author: Justin Sorensen
* Author URI: https://sorensenjg.com
* License: GPL2
*/
add_action('wp_enqueue_scripts', 'docViewer_enqueue_scripts');
function docViewer_enqueue_scripts() {
if (!wp_script_is( 'jquery.magnific-popup.min.js', 'enqueued' )) {
wp_enqueue_style( 'magnific-popup', plugins_url( '/css/vendor/magnific-popup.css', __FILE__ ) );
wp_enqueue_script( 'magnific-popup', plugins_url( '/js/vendor/jquery.magnific-popup.min.js', __FILE__ ), array('jquery'), '20170329', true );
wp_enqueue_style( 'docViewer', plugins_url( '/css/docViewer.css', __FILE__ ) );
wp_enqueue_script( 'docViewer', plugins_url( '/js/docViewer.js', __FILE__ ), array('jquery'), '20170329', true );
} else {
wp_enqueue_style( 'docViewer', plugins_url( '/css/docViewer.css', __FILE__ ) );
wp_enqueue_script( 'docViewer', plugins_url( '/js/docViewer.js', __FILE__ ), array('jquery'), '20170329', true );
}
}
// Display docViewer component
function docViewer( $url, $name ) {
ob_start();
$url = $url;
$name = $name;
$file = basename($url);
$filename = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
$file_info = new SplFileInfo($file);
$file_type = $file_info->getExtension();
if( $file_type == 'jpg' || $file_type == 'png' || $file_type == 'gif' ) {
$content = '<img src="'. $url .'" />';
} else {
$content = '<iframe src="//drive.google.com/viewer?url='. $url .'&embedded=true"></iframe>';
}
$random_int = rand();
if( $name ) {
$name = $name;
} else {
$name = $filename;
}
?>
<a href="#<?php echo $filename; ?>-modal-<?php echo $random_int; ?>" class="docViewer-open"><?php echo $name; ?></a>
<div id="<?php echo $filename; ?>-modal-<?php echo $random_int; ?>" class="docViewer mfp-hide">
<div class="docViewer-inner">
<div id="docViewer-header">
<div class="docViewer-header-inner left">
<label><?php echo $name; ?></label>
</div>
<div class="docViewer-header-inner right">
<a href="<?php echo $url; ?>" download><i class="fa fa-download" aria-hidden="true"></i></a>
<a href="javascript:void(0)" id="docViewer-close"><i class="fa fa-times" aria-hidden="true"></i></a>
</div>
</div>
<div id="docViewer-body">
<?php echo $content; ?>
<div id="docViewer-controls" class="<?php echo $file_type; ?>">
<div class="docViewer-controls-inner center">
<input type="text" id="zoomVal" name="zoomVal" value="100%" />
<div id="zoomOut"><i class="fa fa-search-minus" aria-hidden="true"></i></div>
<div id="zoomIn"><i class="fa fa-search-plus" aria-hidden="true"></i></div>
<div id="actualSize">Original Size</div>
<div id="fitContainer">Fit on Screen</div>
</div>
</div>
</div>
</div>
</div>
<?php
return ob_get_clean();
exit;
}
// Display docViewer component via shortcode
function shortcode_docViewer( $atts ) {
ob_start();
$url = $atts['url'];
$name = $atts['name'];
$file = basename($url);
$filename = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
$file_info = new SplFileInfo($file);
$file_type = $file_info->getExtension();
if( $file_type == 'jpg' || $file_type == 'png' || $file_type == 'gif' ) {
$content = '<img src="'. $url .'" />';
} else {
$content = '<iframe src="//drive.google.com/viewer?url='. $url .'&embedded=true"></iframe>';
}
$random_int = rand();
if( $name ) {
$name = $name;
} else {
$name = $filename;
}
?>
<a href="#<?php echo $filename; ?>-modal-<?php echo $random_int; ?>" class="docViewer-open"><?php echo $name; ?></a>
<div id="<?php echo $filename; ?>-modal-<?php echo $random_int; ?>" class="docViewer mfp-hide">
<div class="docViewer-inner">
<div id="docViewer-header">
<div class="docViewer-header-inner left">
<label><?php echo $name; ?></label>
</div>
<div class="docViewer-header-inner right">
<a href="<?php echo $url; ?>" download><i class="fa fa-download" aria-hidden="true"></i></a>
<a href="javascript:void(0)" id="docViewer-close"><i class="fa fa-times" aria-hidden="true"></i></a>
</div>
</div>
<div id="docViewer-body">
<?php echo $content; ?>
<div id="docViewer-controls" class="<?php echo $file_type; ?>">
<div class="docViewer-controls-inner center">
<input type="text" id="zoomVal" name="zoomVal" value="100%" />
<div id="zoomOut"><i class="fa fa-search-minus" aria-hidden="true"></i></div>
<div id="zoomIn"><i class="fa fa-search-plus" aria-hidden="true"></i></div>
<div id="actualSize">Original Size</div>
<div id="fitContainer">Fit on Screen</div>
</div>
</div>
</div>
</div>
</div>
<?php
return ob_get_clean();
exit;
}
// Expose shortcode
add_shortcode( 'docViewer', 'shortcode_docViewer');