-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetx-media.php
executable file
·430 lines (370 loc) · 14.9 KB
/
netx-media.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* @package NetxWPPlugin
*/
class NetXMediaManager {
const NETX_ASSET_ID_KEY = '_netx_asset_id';
const NETX_ASSET_VIEWNAME_KEY = '_netx_asset_view_name';
const NETX_ENFORCE_DOWNLOAD_KEY = '_netx_enforce_download';
const NETX_FULL_PROXY_URL = '_netx_proxy_url';
const NETX_FILE_SIZE = '_netx_file_size';
const NETX_FILE_NAME = '_netx_file_name';
const NETX_FILE_TYPE = '_netx_file_type';
public static function init() {
new NetXMediaManager();
}
public function __construct() {
//include our client side code
add_action( 'admin_enqueue_scripts', array($this, 'setup_admin_client_scripts'));
//add ajax endpoints
add_action( 'wp_ajax_netx_load', array($this, 'ajax_load'));
add_action( 'wp_ajax_netx_get_image_select_form', array($this, 'get_image_select_form'));
add_action( 'wp_ajax_netx_import_from_netx', array($this, 'import_from_netx'));
//add our custom html filter.
add_filter('image_send_to_editor', array($this, 'wrap_image_html'), 10, 8);
}
public function setup_admin_client_scripts() {
wp_enqueue_style('netx_style', plugins_url('scripts/style.css', __FILE__ ));
wp_enqueue_script('netx_script', plugins_url('scripts/script.js', __FILE__ ));
wp_localize_script('netx_script', 'netxScript', array(
'ajaxLoaderUrl' => plugins_url('scripts/ajax-loader.gif', __FILE__ ),
'debug' => "0"
));
}
//Ajax Callback. Returns the netx form loader as html
public function ajax_load() {
$wrapper = new netxRestWrapper();
$netx = $wrapper->getNetx();
$cats = $netx->getCategoryTree();
$options = get_option('netx_options');
?>
<form id="netx-form" class="media-upload-form validate" action="<?php echo get_bloginfo("url"); ?>/wp-admin/media-upload.php?post_id=<?php echo $postID; ?>&tab=netx" method="post" enctype="multipart/form-data">
<div id="category-tree">
<ul>
<?php $this->tree_node_view($this->getRootCategoryFromTree($cats['1']['children'], $options['netx_base_category_id']), $options['netx_base_category_id']); ?>
</ul>
</div>
<div id="media-items">
<div class="netx-upload-loading-area">
<div class="netx-spinner">
<div class="netx-dot1"></div>
<div class="netx-dot2"></div>
</div>
</div>
</div>
<div class="clear-fix"></div>
</form>
<?php
wp_die();
}
public function import_from_netx() {
$wrapper = new netxRestWrapper();
$netx = $wrapper->getNetx();
$assetID = $_POST['asset_id'];
$assetView = $_POST['asset_view'];
$attachment_id = $this->AddMediaToLibraryFromNetx($wrapper, $netx, $assetID, $assetView, $_POST['enforce_download'] == 'true')['attachment_id'];
echo($attachment_id);
wp_die();
}
//Ajax Callback. Returns the netx form as html.
public function get_image_select_form() {
$options = get_option('netx_options');
$wrapper = new netxRestWrapper();
$netx = $wrapper->getNetx();
$supportedFileTypes = array(
//images
"jpg",
"jpeg",
"png",
"gif",
"ico",
//documents
"pdf",
"doc",
"ppt",
"odt",
"xls",
"psd",
//audio
"mp3",
"m4a",
"ogg",
"wav",
//video
"mp4",
"mov",
"wmv",
"avi",
"mpg",
"ogv",
"3gp",
"3g2"
);
$pagingSize = intval($options['netx_paging_size']);
if($pagingSize == 0) {
$pagingSize = 200;
}
//get current category id
$currentCatId = (trim($_POST['catId']) != '') ? $_POST['catId'] : $options['netx_base_category_id'];
//get assets in current category
$catAssets = $netx->getAssetsByCategoryID($currentCatId);
$catAssetsNum = 0;
foreach($catAssets as $key=>$asset){
$catAssetsNum++;
}
$postID = intval($_REQUEST['post_id']);
// $proxyURL = dirname(__FILE__) . '/proxy.php'
?>
<script type="text/javascript">post_id = <?php echo $postID ?>;</script>
<?php
$paged = isset($_GET['paged']) ? $_GET['paged'] : 1;
$catAssetsPages = ceil($catAssetsNum/$pagingSize);
$catAssetsPage = $netx->getAssetsByCategoryID($currentCatId,$paged);
if($catAssetsPages > 1){
echo '<div class="tablenav"><div class="tablenav-pages">';
if($paged != 1){
echo '<a class="next page-numbers" data-post-id="'.$postID.'" data-page-num="'.($paged-1).'">«</a>';
}
for($i=($catAssetsPages <= 5) ? 1 : $paged;$i<=$paged+4;$i++){
if($paged == $i){
echo '<span class="page-numbers current">'.$i.'</span>';
} else {
echo '<a class="page-numbers" data-post-id="'.$postID.'" data-page-num="'.$i.'">'.$i.'</a>';
}
}
if($paged != $catAssetsPages){
echo '<a class="next page-numbers" data-post-id="'.$postID.'" data-page-num="'.($paged+1).'">»</a>';
}
echo '</div></div>';
}
$catAssetsPageFiltered = array();
if(count($catAssetsPage) > 0) {
foreach($catAssetsPage as $key=>$asset){
$assetID = $asset->getAssetID();
$ast = $netx->getAsset($assetID);
$fileIsSupported = false;
foreach($supportedFileTypes as $supportedFileType) {
$filename = $ast->getFile();
$substrlength = strlen('.' . $supportedFileType);
if(substr($filename, - $substrlength) === ('.' . $supportedFileType)) {
$fileIsSupported = true;
break;
}
}
if($fileIsSupported) {
$catAssetsPageFiltered[$key] = $asset;
}
}
}
if(count($catAssetsPageFiltered) > 0) {
foreach($catAssetsPageFiltered as $key=>$asset){
$assetID = $asset->getAssetID();
$ast = $netx->getAsset($assetID);
?>
<div id="media-item-<?php echo $assetID; ?>" class="media-item">
<img class="pinkynail toggle" style="margin-top: 3px; display: block;" alt="<?php echo $asset->getLabel1(); ?>" src="<?php echo $wrapper->netxThumbUrl($assetID) ?>" />
<a class="toggle describe-toggle-on" style="display: block;">Show</a>
<a class="toggle describe-toggle-off" style="display: none;">Hide</a>
<div class="filename toggle"><span class="title"><?php echo $asset->getLabel1(); ?></span></div>
<table class="slidetoggle describe" style="display:none;">
<thead id="media-head-<?php echo $assetID; ?>" class="media-item-info">
<tr valign="top">
<td id="thumbnail-head-<?php echo $assetID; ?>" class="">
<p><a target="_blank" href="<?php echo $wrapper->netxPreviewUrl($assetID); ?>"><img style="margin-top: 3px" alt="" src="<?php echo $wrapper->netxThumbUrl($assetID); ?>" class="thumbnail"></a></p>
</td>
<td>
<p><strong>File name:</strong> <?php echo $asset->getLabel2(); ?></p>
<p><strong>File type:</strong> <?php echo $asset->getLabel3(); ?></p>
<p><strong>File size:</strong> <?php echo $asset->getLabel4(); ?></p>
<p><strong>Upload date:</strong> <?php echo $asset->getLabel5(); ?></p>
</td></tr>
</thead>
<tbody>
<tr class="image-size">
<th valign="top" class="label" scope="row"><label for="asset[<?php echo $assetID; ?>][image-size]"><span class="alignleft">Size</span><br class="clear"></label></th>
<td class="field">
<div class="image-size-item"><input type="radio" value="thumbnail" id="image-size-thumbnail-<?php echo $assetID; ?>" name="asset[<?php echo $assetID; ?>][image-size]"><label for="image-size-thumbnail-<?php echo $assetID; ?>">Thumbnail</label> <label class="help" for="image-size-thumbnail-<?php echo $assetID; ?>">(150 x 150)</label></div>
<div class="image-size-item"><input type="radio" value="medium" id="image-size-preview-<?php echo $assetID; ?>" name="asset[<?php echo $assetID; ?>][image-size]"><label for="image-size-preview-<?php echo $assetID; ?>">Preview</label> <label class="help" for="image-size-full-<?php echo $assetID; ?>">(<?php echo $ast->getPreviewfilewidth(); ?> x <?php echo $ast->getPreviewfileheight(); ?>)</label></div>
<div class="image-size-item"><input type="radio" checked="checked" value="full" id="image-size-full-<?php echo $assetID; ?>" name="asset[<?php echo $assetID; ?>][image-size]"><label for="image-size-full-<?php echo $assetID; ?>">Full Size</label> <label class="help" for="image-size-full-<?php echo $assetID; ?>">(<?php echo $ast->getFilewidth(); ?> x <?php echo $ast->getFileheight(); ?>)</label></div>
<?php
if(!empty($ast->getViewNames())){
foreach($ast->getViewNames() as $viewName) {
if($viewName === 'previewXMP') {
continue;
}
?>
<div class="image-size-item"><input type="radio" checked="checked" value="<?php echo $viewName ?>" id="image-size-full-<?php echo $assetID; ?>" name="asset[<?php echo $assetID; ?>][image-size]"><label for="image-size-full-<?php echo $assetID; ?>">View: </label> <label class="help" for="image-size-full-<?php echo $assetID; ?>"><?php echo $viewName ?></label></div>
<?php
}
}
?>
</td>
</tr>
<?php if(isset($options["netx_access_token"]) && !empty(trim($options["netx_access_token"]))): ?>
<tr class="enable-download">
<th valign="top" class="label" scope="row"><label for="asset[<?php echo $assetID; ?>][enable-download]"><span class="alignleft">Enable Download</span><br class="clear"></label></th>
<td class="field">
<div class="image-size-item">
<input type="checkbox" value="enable-download" id="asset[<?php echo $assetID; ?>][enable-download]" name="asset[<?php echo $assetID; ?>][enable-download]">
<label for="asset[<?php echo $assetID; ?>][enable-download]">Save as download link</label>
</div>
</td>
</tr>
<?php endif; ?>
<tr class="submit">
<td></td>
<td class="savesend">
<input type="button" data-asset-id="<?php echo($assetID); ?>" value="Insert into Post" class="button netx-submit-button netx-add-item-submit" id="" name="send[<?php echo $assetID; ?>]">
</td>
</tr>
</tbody>
</table>
</div>
<?php
}
}else{
?>
<p class="netx-no-files-found-label">No Files Found</p>
<?php
}
wp_die();
}
public function wrap_image_html($html, $id, $caption, $title, $align, $url, $size, $alt)
{
$enforceNetxDownload = get_post_meta($id, self::NETX_ENFORCE_DOWNLOAD_KEY, true);
if(!empty($enforceNetxDownload) && $enforceNetxDownload === 'true') {
$assetID = get_post_meta($id, self::NETX_ASSET_ID_KEY, true);
$sizeStr = get_post_meta($id, self::NETX_ASSET_VIEWNAME_KEY, true);
$filename = basename(get_attached_file($id));
//awesome the magic strings used through out this code don't match the magic strings
// in netx. Perfect.
if ($sizeStr == 'thumbnail'){
$sizeStr = 'thumb';
}
else if ($sizeStr == 'medium'){
$sizeStr = 'preview';
}
else if ($sizeStr == 'full'){
$sizeStr = 'original';
}
$downloadUrl = plugins_url('endpoints/asset.php', __FILE__);
$downloadUrl .= '?assetID=' . $assetID;
$downloadUrl .= '&sizeStr=' . $sizeStr;
$downloadUrl .= '&filename=' . $filename;
return '<a class="netx-download-link" href="' . $downloadUrl . '" target="_blank"><img src="' . wp_get_attachment_url($id) . '" /></a>';
}
return $html;
}
private function tree_node_view($node, $selectedId) {
$treeNodeClass = 'netx-tree-node collapsed';
$treeNodeLinkClass = 'netx-tree-node-link';
$treeToggleIconClass = 'dashicons dashicons-plus';
if($node['categoryId'] == $selectedId) {
$treeNodeClass = 'netx-tree-node';
$treeNodeLinkClass = 'netx-tree-node-link selected';
$treeToggleIconClass = 'dashicons dashicons-minus';
}
?>
<li class="<?php echo($treeNodeClass); ?>">
<?php if(count($node['children']) > 0) { ?>
<a class="netx-tree-node-toggle">
<span class="<?php echo($treeToggleIconClass); ?>"></span>
</a>
<?php }else{ ?>
<span class="netx-tree-node-nontoggle"><span class="dashicons dashicons-portfolio"></span></span>
<?php } ?>
<a class="<?php echo($treeNodeLinkClass); ?>" data-cat-id="<?php echo($node['categoryId']); ?>"><?php echo($node['label']); ?></a>
<ul class="netx-tree-node-child">
<?php foreach($node['children'] as $key => $child) {
$this->tree_node_view($child, $selectedId);
} ?>
</ul>
</li>
<?php
}
private function getRootCategoryFromTree($tree, $rootId) {
if(is_array($tree)) {
if(array_key_exists($rootId, $tree)) {
return $tree[$rootId];
}else{
foreach($tree as $key => $child) {
if(is_array($child['children']) && (count($child['children']) > 0)) {
$recursiveResult = $this->getRootCategoryFromTree($child['children'], $rootId);
if($recursiveResult != null) {
return $recursiveResult;
}
}
}
}
}
return null;
}
private function AddMediaToLibraryFromNetx($wrapper, $netx, $assetID, $sizeStr, $directLink = false)
{
$ast = $netx->getAsset($assetID);
$filename = $ast->getFile();
$filesize = $ast->getFilesize();
$filetype = $ast->getFiletypelabel();
if($directLink) {
$file = $wrapper->getAssetData($assetID, 't');
} else {
if ($sizeStr === 'full') {
$file = $wrapper->getAssetData($assetID, 'o');
} else if ($sizeStr === 'medium') {
$file = $wrapper->getAssetData($assetID, 'p');
} else if ($sizeStr === 'thumbnail') {
$file = $wrapper->getAssetData($assetID, 't');
} else {
$file = $wrapper->getAssetData($assetID, $sizeStr);
}
}
if (!$file['error']) {
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $file['file']);
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $file['file']);
wp_update_attachment_metadata($attachment_id, $attachment_data);
//set custom netx metadata
add_post_meta($attachment_id, NetXMediaManager::NETX_ASSET_ID_KEY, $assetID, true);
add_post_meta($attachment_id, NetXMediaManager::NETX_ASSET_VIEWNAME_KEY, $sizeStr, true);
$downloadUrl = plugins_url('endpoints/asset.php', __FILE__);
if ($sizeStr == 'thumbnail'){
$sizeStr2 = 'thumb';
}
else if ($sizeStr == 'medium'){
$sizeStr2 = 'preview';
}
else if ($sizeStr == 'full'){
$sizeStr2 = 'original';
} else {
$sizeStr2 = $sizeStr;
}
$downloadUrl .= '?assetID=' . $assetID;
$downloadUrl .= '&sizeStr=' . $sizeStr2;
$downloadUrl .= '&filename=' . $filename;
add_post_meta($attachment_id, NetXMediaManager::NETX_FULL_PROXY_URL, $downloadUrl, true);
add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_SIZE, $filesize, true);
add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_NAME, $filename, true);
add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_TYPE, $filetype, true);
if($directLink) {
add_post_meta($attachment_id, NetXMediaManager::NETX_ENFORCE_DOWNLOAD_KEY, "true", true);
} else {
add_post_meta($attachment_id, NetXMediaManager::NETX_ENFORCE_DOWNLOAD_KEY, "false", true);
}
}
}
return array(
"file" => $file,
"attachment_id" => $attachment_id
);
}
}
add_action('init', array('NetXMediaManager', 'init'));
?>