-
Notifications
You must be signed in to change notification settings - Fork 418
/
viewerjs-plugin.php.in
151 lines (127 loc) · 5.6 KB
/
viewerjs-plugin.php.in
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
<?php
/*
Plugin Name: ViewerJS Plugin
Version: @VIEWERJS_VERSION@
Plugin URI: http://viewerjs.org/
Description: Embed ODF and PDF in WordPress
Author: Tobias Hintze
Author URI: http://kogmbh.com
This WordPress plugin is free software: you can redistribute it
and/or modify it under the terms of the GNU Affero General Public License
(GNU AGPL) as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version. The code is distributed
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
As additional permission under GNU AGPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
*/
$viewerjs_plugin_url = plugins_url() .'/'. plugin_basename(dirname(__FILE__) .'/index.html');
function ViewerJS_PluginAddPage() {
add_options_page('ViewerJS-Plugin Options', 'ViewerJS-Plugin', '8', 'viewerjs-plugin.php', 'ViewerJS_PluginOptions');
}
function ViewerJS_PluginOptions() {
$message = '';
$options = get_option('ViewerJS_PluginSettings');
if ($_POST) {
if (isset($_POST['width'])) {
$options['width'] = $_POST['width'];
}
if (isset($_POST['height'])) {
$options['height'] = $_POST['height'];
}
update_option('ViewerJS_PluginSettings', $options);
$message = '<div class="updated"><p>width=' . $options['width'] .
' height=' . $options['height'] .'<strong> Options saved.</strong></p></div>';
}
echo '<div class="wrap">';
echo '<h2>ViewerJS-Plugin Options</h2>';
echo $message;
echo '<form method="post" action="options-general.php?page=viewerjs-plugin.php">';
echo "<p>You can adjut the width and height of the Viewer iframe here. This is a global setting.</p>";
echo '<h4>Width-Height</h4>' . "\n";
echo '<table class="form-table">' . "\n";
echo '<tr><th scope="row">width</th><td>' . "\n";
echo '<input type="text" name="width" value="' . $options['width'] . '" />';
echo '</td></tr>' . "\n";
echo '<tr><th scope="row">height</th><td>' . "\n";
echo '<input type="text" name="height" value="' . $options['height'] . '" />';
echo '</td></tr>' . "\n";
echo '</table>' . "\n";
echo '<p class="submit"><input class="button-primary" type="submit" method="post" value="Update Options"></p>';
echo '</form>';
echo '</div>';
}
function ViewerJS_PluginLoadDefaults() {
$ret = array();
$ret['width'] = '450px';
$ret['height'] = '380px';
return $ret;
}
function ViewerJS_Plugin_activate() {
update_option('ViewerJS_PluginSettings', ViewerJS_PluginLoadDefaults());
}
register_activation_hook(__FILE__,'ViewerJS_Plugin_activate');
function ViewerJS_Plugin_deactivate() {
delete_option('ViewerJS_PluginSettings');
}
register_deactivation_hook(__FILE__,'ViewerJS_Plugin_deactivate');
add_action('admin_menu', 'ViewerJS_PluginAddPage');
function viewerjs_mime_type_filter($mime_types) {
$mime_types['odt'] = 'application/vnd.oasis.opendocument.text';
$mime_types['fodt'] = 'application/vnd.oasis.opendocument.text-flat-xml';
$mime_types['ott'] = 'application/vnd.oasis.opendocument.text-template';
$mime_types['odp'] = 'application/vnd.oasis.opendocument.presentation';
$mime_types['fodp'] = 'application/vnd.oasis.opendocument.presentation-flat-xml';
$mime_types['otp'] = 'application/vnd.oasis.opendocument.presentation-template';
$mime_types['ods'] = 'application/vnd.oasis.opendocument.spreadsheet';
$mime_types['fods'] = 'application/vnd.oasis.opendocument.spreadsheet-flat-xml';
$mime_types['ots'] = 'application/vnd.oasis.opendocument.spreadsheet-template';
$mime_types['pdf'] = 'application/pdf';
return $mime_types;
}
add_filter( 'upload_mimes', 'viewerjs_mime_type_filter');
function viewerjs_mime_type_icon($icon_uri, $mime_type, $post_id) {
// this is bogus and not implemented
if ($mime_type === 'application/vnd.oasis.opendocument.text') {
return $icon_uri;
} else if ($mime_type === 'application/vnd.oasis.opendocument.presentation') {
return $icon_uri;
} else if ($mime_type === 'application/vnd.oasis.opendocument.spreadsheet') {
return $icon_uri;
} else {
return $icon_uri;
}
// return array($viewerjs_plugin_url . '/odf.png', 64, 64);
}
add_filter( 'wp_mime_type_icon', 'viewerjs_mime_type_icon', 10, 3);
function viewerjs_media_send_to_editor($html, $send_id, $attachment) {
$pid = $attachment['id'];
$post = get_post($pid, ARRAY_A);
if (preg_match('/^application\/(vnd\.oasis\.opendocument|pdf)/', $post['post_mime_type'])) {
// place shortcode
preg_match('/^http:\/\/[^\/]*(\/.*)$/', $attachment['url'], $matches);
$document_url = $matches[1];
return "[viewerjs ".$document_url."]";
}
return $html;
}
add_filter( 'media_send_to_editor', 'viewerjs_media_send_to_editor', 10, 3);
function viewerjs_shortcode_handler($args) {
global $viewerjs_plugin_url;
$document_url = $args[0];
$options = get_option('ViewerJS_PluginSettings');
$iframe_width = $options['width'];
$iframe_height = $options['height'];
return "<iframe src=\"$viewerjs_plugin_url" .
'#' . $document_url .'" '.
"width=\"$iframe_width\" ".
"height=\"$iframe_height\" ".
'style="border: 1px solid black; border-radius: 5px;" '.
'webkitallowfullscreen="true" '.
'mozallowfullscreen="true"></iframe>';
}
add_shortcode('viewerjs', 'viewerjs_shortcode_handler');
?>