|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Plugin Name: ViewerJS Plugin |
| 5 | +Version: @VIEWERJS_VERSION@ |
| 6 | +Plugin URI: http://viewerjs.org/ |
| 7 | +Description: Embed ODF and PDF in WordPress |
| 8 | +Author: Tobias Hintze |
| 9 | +Author URI: http://kogmbh.com |
| 10 | + |
| 11 | +This WordPress plugin is free software: you can redistribute it |
| 12 | +and/or modify it under the terms of the GNU Affero General Public License |
| 13 | +(GNU AGPL) as published by the Free Software Foundation, either version 3 of |
| 14 | +the License, or (at your option) any later version. The code is distributed |
| 15 | +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. |
| 17 | + |
| 18 | +As additional permission under GNU AGPL version 3 section 7, you |
| 19 | +may distribute non-source (e.g., minimized or compacted) forms of |
| 20 | +that code without the copy of the GNU GPL normally required by |
| 21 | +section 4, provided you include this license notice and a URL |
| 22 | +through which recipients can access the Corresponding Source. |
| 23 | + |
| 24 | +*/ |
| 25 | + |
| 26 | +$viewerjs_plugin_url = plugins_url() .'/'. plugin_basename(dirname(__FILE__)); |
| 27 | + |
| 28 | +function ViewerJS_PluginAddPage() { |
| 29 | + add_options_page('ViewerJS-Plugin Options', 'ViewerJS-Plugin', '8', 'viewerjs-plugin.php', 'ViewerJS_PluginOptions'); |
| 30 | +} |
| 31 | + |
| 32 | +function ViewerJS_PluginOptions() { |
| 33 | + $message = ''; |
| 34 | + $options = get_option('ViewerJS_PluginSettings'); |
| 35 | + if ($_POST) { |
| 36 | + if (isset($_POST['width'])) { |
| 37 | + $options['width'] = $_POST['width']; |
| 38 | + } |
| 39 | + if (isset($_POST['height'])) { |
| 40 | + $options['height'] = $_POST['height']; |
| 41 | + } |
| 42 | + |
| 43 | + update_option('ViewerJS_PluginSettings', $options); |
| 44 | + $message = '<div class="updated"><p>width=' . $options['width'] . |
| 45 | + ' height=' . $options['height'] .'<strong> Options saved.</strong></p></div>'; |
| 46 | + } |
| 47 | + echo '<div class="wrap">'; |
| 48 | + echo '<h2>ViewerJS-Plugin Options</h2>'; |
| 49 | + echo $message; |
| 50 | + echo '<form method="post" action="options-general.php?page=viewerjs-plugin.php">'; |
| 51 | + echo "<p>You can adjut the width and height of the Viewer iframe here. This is a global setting.</p>"; |
| 52 | + echo '<h4>Width-Height</h4>' . "\n"; |
| 53 | + echo '<table class="form-table">' . "\n"; |
| 54 | + echo '<tr><th scope="row">width</th><td>' . "\n"; |
| 55 | + echo '<input type="text" name="width" value="' . $options['width'] . '" />'; |
| 56 | + echo '</td></tr>' . "\n"; |
| 57 | + echo '<tr><th scope="row">height</th><td>' . "\n"; |
| 58 | + echo '<input type="text" name="height" value="' . $options['height'] . '" />'; |
| 59 | + echo '</td></tr>' . "\n"; |
| 60 | + echo '</table>' . "\n"; |
| 61 | + |
| 62 | + echo '<p class="submit"><input class="button-primary" type="submit" method="post" value="Update Options"></p>'; |
| 63 | + echo '</form>'; |
| 64 | + |
| 65 | + echo '</div>'; |
| 66 | +} |
| 67 | + |
| 68 | +function ViewerJS_PluginLoadDefaults() { |
| 69 | + $ret = array(); |
| 70 | + $ret['width'] = '450px'; |
| 71 | + $ret['height'] = '380px'; |
| 72 | + return $ret; |
| 73 | +} |
| 74 | + |
| 75 | +function ViewerJS_Plugin_activate() { |
| 76 | + update_option('ViewerJS_PluginSettings', ViewerJS_PluginLoadDefaults()); |
| 77 | +} |
| 78 | + |
| 79 | +register_activation_hook(__FILE__,'ViewerJS_Plugin_activate'); |
| 80 | + |
| 81 | +function ViewerJS_Plugin_deactivate() { |
| 82 | + delete_option('ViewerJS_PluginSettings'); |
| 83 | +} |
| 84 | + |
| 85 | +register_deactivation_hook(__FILE__,'ViewerJS_Plugin_deactivate'); |
| 86 | + |
| 87 | +add_action('admin_menu', 'ViewerJS_PluginAddPage'); |
| 88 | + |
| 89 | + |
| 90 | +function viewerjs_mime_type_filter($mime_types) { |
| 91 | + $mime_types['odt'] = 'application/vnd.oasis.opendocument.text'; |
| 92 | + $mime_types['odp'] = 'application/vnd.oasis.opendocument.presentation'; |
| 93 | + $mime_types['ods'] = 'application/vnd.oasis.opendocument.spreadsheet'; |
| 94 | + $mime_types['pdf'] = 'application/pdf'; |
| 95 | + return $mime_types; |
| 96 | +} |
| 97 | +add_filter( 'upload_mimes', 'viewerjs_mime_type_filter'); |
| 98 | + |
| 99 | +function viewerjs_mime_type_icon($icon_uri, $mime_type, $post_id) { |
| 100 | + // this is bogus and not implemented |
| 101 | + if ($mime_type === 'application/vnd.oasis.opendocument.text') { |
| 102 | + return $icon_uri; |
| 103 | + } else if ($mime_type === 'application/vnd.oasis.opendocument.presentation') { |
| 104 | + return $icon_uri; |
| 105 | + } else if ($mime_type === 'application/vnd.oasis.opendocument.spreadsheet') { |
| 106 | + return $icon_uri; |
| 107 | + } else { |
| 108 | + return $icon_uri; |
| 109 | + } |
| 110 | + // return array($viewerjs_plugin_url . '/odf.png', 64, 64); |
| 111 | +} |
| 112 | +add_filter( 'wp_mime_type_icon', 'viewerjs_mime_type_icon', 10, 3); |
| 113 | + |
| 114 | +function viewerjs_media_send_to_editor($html, $send_id, $attachment) { |
| 115 | + $pid = $attachment['id']; |
| 116 | + |
| 117 | + $post = get_post($pid, ARRAY_A); |
| 118 | + if (preg_match('/^application\/(vnd\.oasis\.opendocument|pdf)/', $post['post_mime_type'])) { |
| 119 | + // place shortcode |
| 120 | + preg_match('/^http:\/\/[^\/]*(\/.*)$/', $attachment['url'], $matches); |
| 121 | + $document_url = $matches[1]; |
| 122 | + return "[viewerjs ".$document_url."]"; |
| 123 | + } |
| 124 | + return $html; |
| 125 | +} |
| 126 | +add_filter( 'media_send_to_editor', 'viewerjs_media_send_to_editor', 10, 3); |
| 127 | + |
| 128 | + |
| 129 | +function viewerjs_shortcode_handler($args) { |
| 130 | + global $viewerjs_plugin_url; |
| 131 | + $document_url = $args[0]; |
| 132 | + $options = get_option('ViewerJS_PluginSettings'); |
| 133 | + $iframe_width = $options['width']; |
| 134 | + $iframe_height = $options['height']; |
| 135 | + return "<iframe src=\"$viewerjs_plugin_url" . |
| 136 | + '#' . $document_url .'" '. |
| 137 | + "width=\"$iframe_width\" ". |
| 138 | + "height=\"$iframe_height\" ". |
| 139 | + 'style="border: 1px solid black; border-radius: 5px;" '. |
| 140 | + 'webkitallowfullscreen="true" '. |
| 141 | + 'mozallowfullscreen="true"></iframe>'; |
| 142 | +} |
| 143 | +add_shortcode('viewerjs', 'viewerjs_shortcode_handler'); |
| 144 | + |
| 145 | +?> |
0 commit comments