Skip to content

Commit 8e6d5ab

Browse files
committed
Initial commit of a wordpress plugin for viewerjs along with some
cmake mechanism to create a zip bundle for it
1 parent 24e785c commit 8e6d5ab

3 files changed

Lines changed: 275 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ FILE ( MAKE_DIRECTORY ${VIEWER_BUILD_DIR})
6161
SET ( VIEWERZIP ${CMAKE_BINARY_DIR}/Viewer.js-${VIEWERJS_VERSION}.zip)
6262
SET ( WEBODF_SOURCE_DIR ${CMAKE_BINARY_DIR}/WebODF-source )
6363

64+
SET ( WORDPRESS_ZIP_DIR ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-wordpress-${VIEWERJS_VERSION})
65+
SET ( WORDPRESSZIP ${WORDPRESS_ZIP_DIR}.zip)
66+
6467
ExternalProject_Add(
6568
WebODF
6669
GIT_REPOSITORY https://git.gitorious.org/webodf/webodf.git
@@ -100,3 +103,23 @@ add_custom_target(Viewer ALL
100103
WebODF
101104
PDFjs
102105
)
106+
107+
configure_file(viewerjs-plugin.php.in ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-plugin.php)
108+
109+
add_custom_command(
110+
OUTPUT ${WORDPRESSZIP}
111+
COMMAND ${CMAKE_COMMAND} -E remove_directory ${WORDPRESS_ZIP_DIR}
112+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${VIEWER_BUILD_DIR} ${WORDPRESS_ZIP_DIR}
113+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/viewerjs-plugin-README.txt ${WORDPRESS_ZIP_DIR}
114+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-plugin.php ${WORDPRESS_ZIP_DIR}
115+
COMMAND node ARGS ${WEBODF_SOURCE_DIR}/webodf/lib/runtime.js ${WEBODF_SOURCE_DIR}/webodf/tools/zipdir.js
116+
${WORDPRESS_ZIP_DIR}
117+
${WORDPRESSZIP}
118+
)
119+
120+
add_custom_target(wordpress-plugin ALL
121+
DEPENDS
122+
${WORDPRESSZIP}
123+
Viewer
124+
)
125+

viewerjs-plugin-README.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=== WebODF Viewer ===
2+
Contributors: Tobias Hintze
3+
Tags: ODF, open document, WebODF
4+
Requires at least: 3.2
5+
Tested up to: 3.5.1
6+
Stable tag: 1.0
7+
8+
9+
This plugin embeds a WebODF Viewer into WordPress.
10+
11+
== Description ==
12+
13+
This plugin embeds a WebODF Viewer into WordPress. See www.webodf.org for more details on WebODF.
14+
15+
== Usage ==
16+
17+
Navigate to a Open Document file in your WP blog's media library and press the button "Insert WebODF Viewer".
18+
Alternatively you can manually insert the WordPress shortcode `[webodf_viewer /path-to-some-file.odt]` to embed
19+
any `*.odt`, `*.odp`, or `*.ods` documents at that location.
20+
21+
The plugin also provides a settings panel for adjusting the width and height of the embeded element (iframe).
22+
23+
== Installation ==
24+
25+
1. Upload the whole `webodf-viewer` directory and content to the `/wp-content/plugins/` directory
26+
2. Activate the plugin through the 'Plugins' menu in WordPress
27+
28+
(or do the automatic stuff from the /wp-admin/...)
29+
30+
== Screenshots ==
31+
32+
no screenshots available (yet)
33+
34+
== Changelog ==
35+
36+
= 0.1 =
37+
38+
* first version hacked together.
39+
40+
= 0.2 =
41+
42+
* made it work with recent 3.5.1 (which changed API around attachment-fields)
43+

viewerjs-plugin.php.in

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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>&nbsp; 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

Comments
 (0)