-
Notifications
You must be signed in to change notification settings - Fork 0
/
webviewer.php
110 lines (90 loc) · 3.56 KB
/
webviewer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* Plugin Name: Webviewer
* Plugin URI:
* Description: Embed attached content in your Wordpress blog using the [webviewer] macro.
* Version: 0.1
* Author: Caleb James DeLisle
* Author URI: https://github.com/cjdelisle/
* License: LGPLv2.1 (http://www.gnu.org/licenses/lgpl-2.1.txt)
*/
define('WP_WEBVIEWER_DIR', plugin_dir_path(__FILE__));
// workaround for http://core.trac.wordpress.org/ticket/16953
function wp_webviewer_get_url()
{
static $url;
if (!$url) {
$url = preg_replace('/.*\/wp-content\/plugins\/([^\/]*)\/.*$/', '$1', __FILE__);
if ($url && $url !== __FILE__) {
$url = plugins_url() . '/' . $url . '/';
} else {
error_log("could not find directory location ".__FILE__.":".__LINE__);
$url = plugin_dir_url(__FILE__);
}
}
return $url;
}
define('WP_WEBVIEWER_URL', wp_webviewer_get_url());
function wp_webviewer_init() {
wp_register_script('wp_webviewer_jschannel', WP_WEBVIEWER_URL.'resources/jschannel.js');
wp_register_script('wp_webviewer_renderjs_jquery',
WP_WEBVIEWER_URL.'resources/renderjs_jquery.js');
wp_register_script('wp_webviewer_renderjs',
WP_WEBVIEWER_URL.'resources/renderjs.js',
array('wp_webviewer_jschannel','wp_webviewer_renderjs_jquery'));
wp_register_script('wp_webviewer_main',
WP_WEBVIEWER_URL.'resources/webviewer.js',
array('wp_webviewer_renderjs'));
}
add_action('init', 'wp_webviewer_init');
function wp_webviewer_get_viewer($fileType, $action) {
$path = WP_WEBVIEWER_DIR.'viewers/';
$dh = opendir($path);
while (false !== ($filename = readdir($dh))) {
if ($filename === '..' || $filename === '.') { continue; }
$fpath = $path.$filename;
if (!is_dir($fpath)) { continue; }
$filePath = $fpath.'/webviewer.json';
if (!file_exists($filePath)) { continue; }
$content = file_get_contents($filePath);
if (!($json = json_decode($content))) { continue; }
if (!isset($json->actions)) { continue; }
if (!isset($json->actions->{$action})) { continue; }
$fileTypes = $json->actions->{$action};
if (!in_array($fileType, $fileTypes)) { continue; }
// Found an appropriate viewer...
$main = isset($json->main) ? $json->main : 'index.html';
return str_replace(WP_WEBVIEWER_DIR, WP_WEBVIEWER_URL, $fpath.'/'.$main);
}
return NULL;
}
function wp_webdriver_get_fileType($url)
{
return preg_replace('/^.*\.([^.]*)$/', '$1', $url);
}
// shortcode [embed id=123 width=300px height=200px]
function wp_webviewer_embed($atts)
{
extract(shortcode_atts(array(
'id' => '-1',
'url' => '',
'width' => '800px',
'height' => '400px'
), $atts));
if ($id >= 0) {
$url = wp_get_attachment_url($id);
} else if ($url === '') {
return "[webviewer: MISSING ATTACHMENT ID OR URL!]";
}
$fileType = wp_webdriver_get_fileType($url);
$viewer = wp_webviewer_get_viewer($fileType, 'view');
if ($viewer === NULL) {
return "[webviewer: NO WEBVIEWER AVAILABLE FOR FILE TYPE: '".htmlentities($fileType)."']";
}
wp_enqueue_script('wp_webviewer_main');
return '<div style="width:'.htmlentities($width).';height:'.htmlentities($height).'" '
.'data-gadget="'.htmlentities($viewer).'" '
.'data-gadget-content="'.htmlentities($url).'"></div>';
}
add_shortcode('webviewer', 'wp_webviewer_embed');
?>