-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththingview.php
137 lines (116 loc) · 4.47 KB
/
thingview.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @package ThingView
* @version 0.1
*/
/*
Plugin Name: ThingView
Plugin URI: http://guillaume.segu.in/blog/
Description: This plugins handles the display of uploaded STLs using thingiview.js in a simple way
Author: Guillaume Seguin
Version: 0.1
Author URI: http://guillaume.segu.in/
*/
require('thingiview.js/php/convert.php');
function thingview_upload_mimes($mimes) {
$mimes['stl'] = 'application/sla';
return $mimes;
}
add_filter('mime_types', 'thingview_upload_mimes');
function thingview_handle_upload($data, $task) {
if ($task != 'upload')
return $data;
$filename = $data['file'];
$json_filename = "$filename.json";
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if ($data['type'] != 'application/sla' || $ext != 'stl')
return $data;
set_time_limit(3000); // Increase time limit
$contents = file_get_contents($filename);
$contents = preg_replace('/$\s+.*/', '', $contents);
if (stripos($contents, 'solid') === FALSE) {
$handle = fopen($filename, 'rb');
if (!$handle) {
trigger_error("Failed to open file $filename");
}
$result = parse_stl_binary($handle);
} else {
$result = parse_stl_string($contents);
}
file_put_contents($json_filename, json_encode($result));
return $data;
}
add_filter('wp_handle_upload', 'thingview_handle_upload', 10, 2);
function thingview_delete_file($filename) {
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if ($ext != 'stl')
return $file;
$json_filename = "$filename.json";
if (file_exists($json_filename))
unlink($json_filename);
return $filename;
}
add_filter('wp_delete_file', 'thingview_delete_file');
function thingview_script() {
?>
<script type="text/javascript">
jQuery(document).ready( function($) {
$( 'li.attachment > div.type-application.subtype-sla:parent' ).live( 'click', function( event ) {
$( ".link-to > [value='none']").attr( "selected", true ); // selected none in select field
$( ".link-to-custom" ).val( '' ); // clear input field for target of link
$( '.media-sidebar div.setting' ).remove(); // remove link field
$( '.attachment-display-settings' ).attr( "hidden", true );
});
} );
</script>
<?php
}
add_action('print_media_templates', 'thingview_script');
function thingview_send_to_editor($html, $id, $attachment) {
$filename = get_attached_file($attachment['id'], true);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext == 'stl') {
$html = sprintf('[thing id=%d width="500px" height="360px" class="aligncenter"]', $attachment['id']);
}
return $html;
}
add_filter('media_send_to_editor', 'thingview_send_to_editor', 10, 3);
function thingview_thing_shortcode($attributes) {
$default_attributes = array('id' => 0,
'width' => '500px',
'height' => '360px',
'class' => '',
'color' => '#86E4FF',
'background' => 'inherit',
'border' => 'none',
'gridsize' => 200,
'gridunit' => 10);
$attributes = shortcode_atts($default_attributes, $attributes, 'thing');
$file_url = wp_get_attachment_url($attributes['id']);
if ($file_url === false)
return "Missing attachment STL file";
wp_enqueue_script('jquery');
wp_print_scripts('jquery');
$js_dir = plugins_url("thingiview.js/javascripts/", __FILE__);
wp_enqueue_script("Three.js", $js_dir . "three.min.js");
wp_enqueue_script("thingiview.js", $js_dir . "thingiview.js",
array("Three.js"));
return '
<script>
jQuery(document).ready(function() {
thingiurlbase = "' . $js_dir . '";
thingiview' . $attributes['id'] . ' = new Thingiview("thing-' . $attributes['id'] . '", ' . $attributes['gridsize'] . ', ' . $attributes['gridunit'] . ');
thingiview' . $attributes['id'] . '.setObjectColor("' . $attributes['color']. '");
thingiview' . $attributes['id'] . '.setBackgroundColor("' . $attributes['background'] . '");
thingiview' . $attributes['id'] . '.initScene();
thingiview' . $attributes['id'] . '.loadJSON("' . $file_url . '.json");
})
</script>
<div id="thing-' . $attributes['id'] .
'" class="' . $attributes['class'] .
'" style="width:' . $attributes['width'] .
'; height:' . $attributes['height'] .
'; border:' . $attributes['border'] . '"></div>';
}
add_shortcode( 'thing', 'thingview_thing_shortcode' );
?>