Skip to content

Commit

Permalink
Add WordPress plugin for Viewer.JS
Browse files Browse the repository at this point in the history
(Merge branch 'wordpress')
  • Loading branch information
Tobias Hintze committed Sep 18, 2013
2 parents 24e785c + 786cc4c commit 0a5eef2
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 4 deletions.
25 changes: 24 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ project (Viewer.js)
cmake_minimum_required(VERSION 2.8.6)

# At this point, the version number that is used throughout is defined
set(VIEWERJS_VERSION 0.2.0)
set(VIEWERJS_VERSION 0.2.1)

if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "Compiling in the source directory is not supported. Use for example 'mkdir build; cd build; cmake ..'.")
Expand All @@ -61,6 +61,9 @@ FILE ( MAKE_DIRECTORY ${VIEWER_BUILD_DIR})
SET ( VIEWERZIP ${CMAKE_BINARY_DIR}/Viewer.js-${VIEWERJS_VERSION}.zip)
SET ( WEBODF_SOURCE_DIR ${CMAKE_BINARY_DIR}/WebODF-source )

SET ( WORDPRESS_ZIP_DIR ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-wordpress-${VIEWERJS_VERSION})
SET ( WORDPRESSZIP ${WORDPRESS_ZIP_DIR}.zip)

ExternalProject_Add(
WebODF
GIT_REPOSITORY https://git.gitorious.org/webodf/webodf.git
Expand Down Expand Up @@ -100,3 +103,23 @@ add_custom_target(Viewer ALL
WebODF
PDFjs
)

configure_file(viewerjs-plugin.php.in ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-plugin.php)

add_custom_command(
OUTPUT ${WORDPRESSZIP}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${WORDPRESS_ZIP_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${VIEWER_BUILD_DIR} ${WORDPRESS_ZIP_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/viewerjs-plugin-README.txt ${WORDPRESS_ZIP_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/viewerjs-plugin.php ${WORDPRESS_ZIP_DIR}
COMMAND node ARGS ${WEBODF_SOURCE_DIR}/webodf/lib/runtime.js ${WEBODF_SOURCE_DIR}/webodf/tools/zipdir.js
${WORDPRESS_ZIP_DIR}
${WORDPRESSZIP}
)

add_custom_target(wordpress-plugin ALL
DEPENDS
${WORDPRESSZIP}
Viewer
)

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Viewer.js
# Viewer.JS

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).
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).

The heavy lifting in Viewer.js is done by these awesome projects:
The heavy lifting in Viewer.JS is done by these awesome projects:

### WebODF

Expand Down
39 changes: 39 additions & 0 deletions viewerjs-plugin-README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== ViewerJS WordPress Plugin ===
Contributors: Tobias Hintze
Tags: ODF, PDF, Documents, open document, WebODF
Requires at least: 3.2
Tested up to: 3.6.1
Stable tag: 1.0


This plugin embeds Viewer.JS into WordPress.

== Description ==
This is a combination of WebODF and PDF.js.
This plugin embeds Viewer.JS into WordPress. This is a combination of WebODF and PDF.js. See www.viewerjs.org for more details.

== Usage ==

Navigate to a Document file (ODF or PDF) in your WP blog's media library and press the button "Insert with Viewer.JS".
Alternatively you can manually insert the WordPress shortcode `[viewerjs /path-to-some-file.pdf]` to embed
any `*.pdf`, `*.odt`, `*.odp`, or `*.ods` documents at that location.

The plugin also provides a settings panel for adjusting the width and height of the embeded element (iframe).

== Installation ==

1. Upload the whole `viewer.js-plugin` directory and content to the `/wp-content/plugins/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress

(or do the automatic stuff from the /wp-admin/...)

== Screenshots ==

no screenshots available (yet)

== Changelog ==

= 0.1 =

* first version forked from the WebODF version.

145 changes: 145 additions & 0 deletions viewerjs-plugin.php.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?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__));

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>&nbsp; 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['odp'] = 'application/vnd.oasis.opendocument.presentation';
$mime_types['ods'] = 'application/vnd.oasis.opendocument.spreadsheet';
$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');

?>

0 comments on commit 0a5eef2

Please sign in to comment.