Skip to content

Commit 0a5eef2

Browse files
author
Tobias Hintze
committed
Add WordPress plugin for Viewer.JS
(Merge branch 'wordpress')
2 parents 24e785c + 786cc4c commit 0a5eef2

File tree

4 files changed

+211
-4
lines changed

4 files changed

+211
-4
lines changed

CMakeLists.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ project (Viewer.js)
3636
cmake_minimum_required(VERSION 2.8.6)
3737

3838
# At this point, the version number that is used throughout is defined
39-
set(VIEWERJS_VERSION 0.2.0)
39+
set(VIEWERJS_VERSION 0.2.1)
4040

4141
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
4242
message(FATAL_ERROR "Compiling in the source directory is not supported. Use for example 'mkdir build; cd build; cmake ..'.")
@@ -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+

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Viewer.js
1+
# Viewer.JS
22

3-
Viewer.js combines a number of excellent open source tools that are built on HTML and javascript. Viewer.js was funded by [NLnet foundation](http://nlnet.nl) and developed by [KO GmbH](http://kogmbh.com).
3+
Viewer.JS combines a number of excellent open source tools that are built on HTML and javascript. Viewer.JS was funded by [NLnet foundation](http://nlnet.nl) and developed by [KO GmbH](http://kogmbh.com).
44

5-
The heavy lifting in Viewer.js is done by these awesome projects:
5+
The heavy lifting in Viewer.JS is done by these awesome projects:
66

77
### WebODF
88

viewerjs-plugin-README.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== ViewerJS WordPress Plugin ===
2+
Contributors: Tobias Hintze
3+
Tags: ODF, PDF, Documents, open document, WebODF
4+
Requires at least: 3.2
5+
Tested up to: 3.6.1
6+
Stable tag: 1.0
7+
8+
9+
This plugin embeds Viewer.JS into WordPress.
10+
11+
== Description ==
12+
This is a combination of WebODF and PDF.js.
13+
This plugin embeds Viewer.JS into WordPress. This is a combination of WebODF and PDF.js. See www.viewerjs.org for more details.
14+
15+
== Usage ==
16+
17+
Navigate to a Document file (ODF or PDF) in your WP blog's media library and press the button "Insert with Viewer.JS".
18+
Alternatively you can manually insert the WordPress shortcode `[viewerjs /path-to-some-file.pdf]` to embed
19+
any `*.pdf`, `*.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 `viewer.js-plugin` 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 forked from the WebODF version.
39+

viewerjs-plugin.php.in

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

Comments
 (0)