This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faf-getasset.php
151 lines (133 loc) · 4.22 KB
/
faf-getasset.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
<?php
/*
Plugin Name: FAF GetAsset
Plugin URI: https://github.com/fafiebig/faf-getasset
Description: Media proxy to privatize media urls.
Version: 1.0
Author: F.A. Fiebig
Author URI: http://fafworx.com
License: GNU GENERAL PUBLIC LICENSE
*/
defined('ABSPATH') or die('No direct script access allowed!');
/**
* @param $param
* @return string
*/
function shortDownloadImage($param)
{
$title = (isset($param['title'])) ? $param['title'] : 'Download Image';
$link = (isset($param['link'])) ? $param['link'] : 'Download '.get_the_title($param['id']);
$size = (isset($param['size'])) ? $param['size'] : 'thumbnail';
$class = (isset($param['class'])) ? $param['class'] : 'class';
$url = getSecureAttachmentImageSrc(null, $param['id'], $size, 'attachment');
echo '<a class="'.$class.'" href="'.$url[0].'" download="'.$url[0].'" title="'.$title.'">'.$link.'</a>';
}
add_shortcode('download_image', 'shortDownloadImage');
/**
* @param $param
* @return string
*/
function shortDownloadAttachment($param)
{
$title = (isset($param['title'])) ? $param['title'] : 'Download Attachment';
$link = (isset($param['link'])) ? $param['link'] : 'Download '.get_the_title($param['id']);
$class = (isset($param['class'])) ? $param['class'] : 'class';
$url = getSecureAttachmentUrl(null, $param['id'], 'attachment');
echo '<a class="'.$class.'" href="'.$url.'" download="'.$url.'" title="'.$title.'">'.$link.'</a>';
}
add_shortcode('download_attachment', 'shortDownloadAttachment');
/**
* @param $form_fields
* @param $post
* @return mixed
*/
function addAttachmentFields( $form_fields, $post )
{
$private = get_post_meta($post->ID, 'private', true);
$checked = ($private === 'yes') ? 'checked="checked"' : '';
$form_fields['private'] = array(
'label' => 'Privat',
'input' => 'html',
'html' => '
<input type="checkbox" id="attachments-'.$post->ID.'-private" name="attachments['.$post->ID.'][private]" value="yes" '.$checked.' />
<label for="attachments-'.$post->ID.'-private">Nur für eingeloggte Nutzer sichtbar.</label>
',
'value' => $private,
'helps' => ''
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'addAttachmentFields', 10, 2);
/**
* @param $attachment_id
*/
function saveAttachmentFields( $id )
{
$private = null;
if ( isset( $_REQUEST['attachments'][$id]['private'] ) ) {
$private = $_REQUEST['attachments'][$id]['private'];
}
update_post_meta( $id, 'private', $private );
}
add_action( 'edit_attachment', 'saveAttachmentFields', 10, 2 );
/**
* @param $url
* @return mixed
*/
function getSecureAttachmentUrl($url, $fileId, $disp = 'inline')
{
if (!is_admin()) {
$private = get_post_meta($fileId, 'private', true);
if ($private === 'yes' && !wp_attachment_is_image($fileId)) {
$assetHash = base64_encode($fileId.'||'.$disp);
return plugin_dir_url( __FILE__ ).'asset.php?asset=' . $assetHash;
}
}
return $url;
}
add_filter('wp_get_attachment_url', 'getSecureAttachmentUrl', 10, 2);
/**
* @param $url
* @return mixed
*/
function getSecureAttachmentImageSrc($url, $fileId, $size, $disp = 'inline')
{
if (!is_admin()) {
$private = get_post_meta($fileId, 'private', true);
if ($private === 'yes') {
$assetHash = base64_encode($fileId.'|'.$size.'|'.$disp);
$url[0] = plugin_dir_url( __FILE__ ).'asset.php?asset='.$assetHash;
$url[1] = null;
$url[2] = null;
}
}
return $url;
}
add_filter('wp_get_attachment_image_src', 'getSecureAttachmentImageSrc', 10, 3);
/**
* @param $columns
* @return array
*/
function addAttachmentColumns($columns)
{
$retval = [];
foreach ($columns AS $key => $col) {
$retval[$key] = $col;
if ($key == 'author') {
$retval['private'] = 'Private';
}
}
return $retval;
}
add_filter("manage_media_columns", "addAttachmentColumns");
/**
* @param $column
*/
function addAttachmentColumnsValues($column)
{
global $post;
if ($column == 'private') {
echo get_post_meta($post->ID, 'private', true);
}
}
add_action("manage_media_custom_column", "addAttachmentColumnsValues");