|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Plugin Name: WebODF Viewer |
| 5 | +Version: @WEBODF_VERSION@ |
| 6 | +Plugin URI: http://webodf.org/ |
| 7 | +Description: Embed ODF 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 | +$site_url = site_url(); |
| 27 | +$this_plugin_url = plugins_url() .'/'. plugin_basename(dirname(__FILE__)); |
| 28 | + |
| 29 | +function WebODF_ViewerAddPage() { |
| 30 | + add_options_page('WebODF-Viewer Options', 'WebODF-Viewer', '8', 'webodf-viewer.php', 'WebODF_ViewerOptions'); |
| 31 | +} |
| 32 | + |
| 33 | +function WebODF_ViewerOptions() { |
| 34 | + global $site_url; |
| 35 | + $message = ''; |
| 36 | + $options = get_option('WebODF_ViewerSettings'); |
| 37 | + if ($_POST) { |
| 38 | + if (isset($_POST['width'])) { |
| 39 | + $options['width'] = $_POST['width']; |
| 40 | + } |
| 41 | + if (isset($_POST['height'])) { |
| 42 | + $options['height'] = $_POST['height']; |
| 43 | + } |
| 44 | + |
| 45 | + update_option('WebODF_ViewerSettings', $options); |
| 46 | + $message = '<div class="updated"><p>width=' . $options['width'] . |
| 47 | + ' height=' . $options['height'] .'<strong> Options saved.</strong></p></div>'; |
| 48 | + } |
| 49 | + echo '<div class="wrap">'; |
| 50 | + echo '<h2>WebODF-Viewer Options</h2>'; |
| 51 | + echo $message; |
| 52 | + echo '<form method="post" action="options-general.php?page=webodf-viewer.php">'; |
| 53 | + echo "<p>You can adjut the width and height of the WebODF Viewer iframe here. This is a global setting.</p>"; |
| 54 | + echo '<h4>Width-Height</h4>' . "\n"; |
| 55 | + echo '<table class="form-table">' . "\n"; |
| 56 | + echo '<tr><th scope="row">width</th><td>' . "\n"; |
| 57 | + echo '<input type="text" name="width" value="' . $options['width'] . '" />'; |
| 58 | + echo '</td></tr>' . "\n"; |
| 59 | + echo '<tr><th scope="row">height</th><td>' . "\n"; |
| 60 | + echo '<input type="text" name="height" value="' . $options['height'] . '" />'; |
| 61 | + echo '</td></tr>' . "\n"; |
| 62 | + echo '</table>' . "\n"; |
| 63 | + |
| 64 | + echo '<p class="submit"><input class="button-primary" type="submit" method="post" value="Update Options"></p>'; |
| 65 | + echo '</form>'; |
| 66 | + |
| 67 | + echo '</div>'; |
| 68 | +} |
| 69 | + |
| 70 | +function WebODF_ViewerLoadDefaults() { |
| 71 | + $ret = array(); |
| 72 | + $ret['width'] = '450px'; |
| 73 | + $ret['height'] = '380px'; |
| 74 | + return $ret; |
| 75 | +} |
| 76 | + |
| 77 | +function WebODF_Viewer_activate() { |
| 78 | + update_option('WebODF_ViewerSettings', WebODF_ViewerLoadDefaults()); |
| 79 | +} |
| 80 | + |
| 81 | +register_activation_hook(__FILE__,'WebODF_Viewer_activate'); |
| 82 | + |
| 83 | +function WebODF_Viewer_deactivate() { |
| 84 | + delete_option('WebODF_ViewerSettings'); |
| 85 | +} |
| 86 | + |
| 87 | +register_deactivation_hook(__FILE__,'WebODF_Viewer_deactivate'); |
| 88 | + |
| 89 | +add_action('admin_menu', 'WebODF_ViewerAddPage'); |
| 90 | + |
| 91 | + |
| 92 | +function mime_type_filter($mime_types) { |
| 93 | + $mime_types['odt'] = 'application/vnd.oasis.opendocument.text'; |
| 94 | + $mime_types['odp'] = 'application/vnd.oasis.opendocument.presentation'; |
| 95 | + $mime_types['ods'] = 'application/vnd.oasis.opendocument.spreadsheet'; |
| 96 | + return $mime_types; |
| 97 | +} |
| 98 | +add_filter( 'upload_mimes', 'mime_type_filter'); |
| 99 | + |
| 100 | +function mime_type_icon($icon_uri, $mime_type, $post_id) { |
| 101 | + // this is bogus and not implemented |
| 102 | + if ($mime_type === 'application/vnd.oasis.opendocument.text') { |
| 103 | + return $icon_uri; |
| 104 | + } else if ($mime_type === 'application/vnd.oasis.opendocument.presentation') { |
| 105 | + return $icon_uri; |
| 106 | + } else if ($mime_type === 'application/vnd.oasis.opendocument.spreadsheet') { |
| 107 | + return $icon_uri; |
| 108 | + } else { |
| 109 | + return $icon_uri; |
| 110 | + } |
| 111 | + // return array($this_plugin_url . '/odf.png', 64, 64); |
| 112 | +} |
| 113 | +add_filter( 'wp_mime_type_icon', 'mime_type_icon', 10, 3); |
| 114 | + |
| 115 | +function webodf_media_send_to_editor($html, $send_id, $attachment) { |
| 116 | + $pid = $attachment['id']; |
| 117 | + |
| 118 | + $post = get_post($pid, ARRAY_A); |
| 119 | + if (preg_match('/^application\/vnd\.oasis\.opendocument/', $post['post_mime_type'])) { |
| 120 | + // place shortcode |
| 121 | + preg_match('/^http:\/\/[^\/]*(\/.*)$/', $attachment['url'], $matches); |
| 122 | + $document_url = $matches[1]; |
| 123 | + return "[webodf_viewer ".$document_url."]"; |
| 124 | + } |
| 125 | + return $html; |
| 126 | +} |
| 127 | +add_filter( 'media_send_to_editor', 'webodf_media_send_to_editor', 10, 3); |
| 128 | + |
| 129 | + |
| 130 | +/* |
| 131 | + TODO: introduce a smart switch on [post | attachment] scope |
| 132 | + to skip webodf-embedding... (user might to simply link |
| 133 | + some documents) |
| 134 | + For now: always use webodf for embedding, if this plugin |
| 135 | + is enabled. |
| 136 | + |
| 137 | +function field_filter($fields, $post_object) { |
| 138 | + if ($post_object->post_parent > 0) { |
| 139 | + // inside a post |
| 140 | + $use_webodf = get_post_meta($post_object['post_parent'], 'use_webodf'); |
| 141 | + } |
| 142 | + |
| 143 | + if (preg_match('/^application\/vnd\.oasis\.opendocument\.(text|presentation|spreadsheet)/', |
| 144 | + $post_object->post_mime_type)) { |
| 145 | + |
| 146 | + // insert document path |
| 147 | + preg_match('/^http:\/\/[^\/]*(\/.*)$/', $post_object->guid, $matches); |
| 148 | + $fields["webodf_document_url"] = array( |
| 149 | + "label" => __("Document URL"), |
| 150 | + "input" => "text", |
| 151 | + "value" => $matches[1] |
| 152 | + ); |
| 153 | + $fields["webodf_embed"] = array( |
| 154 | + "input" => "html", |
| 155 | + "label" => "Embed with WebODF", |
| 156 | + "html" => '<label for="attachments-'.$post_object->ID.'-webodf_embed"> '. |
| 157 | + '<input type="checkbox" id="attachments-"'.$post_object->ID.'-webodf_embed" '. |
| 158 | + ' name="attachments['.$post_object->ID.'][webodf_embed]" value="1" checked="checked"/>Use WebODF?</label>' |
| 159 | + ); |
| 160 | + } |
| 161 | + return $fields; |
| 162 | +} |
| 163 | +add_filter('attachment_fields_to_edit', 'field_filter', 10, 2); |
| 164 | + |
| 165 | +function field_save_legacy( $post, $attachment ) { |
| 166 | + $postid = $post['post_ID']; |
| 167 | + if (! $postid) $postid = $post['ID']; |
| 168 | + return field_save($postid); |
| 169 | +} |
| 170 | + |
| 171 | +function field_save( $post ) { |
| 172 | + $postid = $post; |
| 173 | + |
| 174 | + if (! empty($_POST['attachments'])) { |
| 175 | + if ($_POST['attachments'][$postid]['webodf_embed']) { |
| 176 | + update_post_meta($postid, 'webodf_embed', "yes"); |
| 177 | + } else { |
| 178 | + update_post_meta($postid, 'webodf_embed', "no"); |
| 179 | + } |
| 180 | + } |
| 181 | + return $post; |
| 182 | +} |
| 183 | + |
| 184 | +// legacy (wp < 3.5.1): |
| 185 | +add_filter('attachment_fields_to_save', 'field_save_legacy', 10, 2); |
| 186 | + |
| 187 | +// recent: |
| 188 | +add_filter('add_attachment', 'field_save', 10, 1); |
| 189 | +add_filter('edit_attachment', 'field_save', 10, 1); |
| 190 | + |
| 191 | +*/ |
| 192 | + |
| 193 | +function webodf_shortcode_handler($args) { |
| 194 | + global $this_plugin_url; |
| 195 | + $document_url = $args[0]; |
| 196 | + $options = get_option('WebODF_ViewerSettings'); |
| 197 | + $iframe_width = $options['width']; |
| 198 | + $iframe_height = $options['height']; |
| 199 | + return "<iframe src=\"$this_plugin_url/wv/" . |
| 200 | + '#' . $document_url .'" '. |
| 201 | + "width=\"$iframe_width\" ". |
| 202 | + "height=\"$iframe_height\" ". |
| 203 | + 'style="border: 1px solid black; border-radius: 5px;" '. |
| 204 | + 'webkitallowfullscreen="true" '. |
| 205 | + 'mozallowfullscreen="true"></iframe>'; |
| 206 | +} |
| 207 | +add_shortcode('webodf_viewer', 'webodf_shortcode_handler'); |
| 208 | + |
| 209 | +?> |
0 commit comments