-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-signed-s3-links.php
277 lines (248 loc) · 6.97 KB
/
class-signed-s3-links.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
<?php
/**
* Signed_S3_Links install, init, admin
*
* @package Signed-S3-links
* @author Jonathan Bredin <[email protected]>
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
* @link https://github.com/jonathanlb/signed-s3-links
* @since 0.1.0
*/
use Aws\Credentials\CredentialProvider;
/**
* Signed_S3_Links
*/
class Signed_S3_Links {
/**
* Signal loaded.
*
* @var $initialized Flag that the plugin has completed loading.
*/
public static $initialized = false;
static $s3_client = null;
static $s3_config = null;
/**
* Respond to admin_init events.
*/
public static function admin_init() {
/** Callback to print the header of settings pane. */
function aws_settings_callback() {
esc_html_e( 'AWS Settings' );
}
/**
* Retrieve an option value and populate it to input field.
*
* @param array $text_input Text-input attributes.
*/
function text_input_callback( $text_input ) {
$option_group = $text_input['option_group'];
$option_id = $text_input['option_id'];
$option_name = "{$option_group}[{$option_id}]";
$options = get_option( $option_group );
$option_value = $options[ $option_id ] ?? '';
?>
<input type="text" size="32" id="<?php echo esc_attr( $option_id ); ?>"
name="<?php echo esc_attr( $option_name ); ?>"
value="<?php echo esc_attr( $option_value ); ?>" />
<?php
}
add_settings_section(
'aws_settings_section',
__( 'AWS Settings' ),
'aws_settings_callback',
'ss3_settings'
);
add_settings_field(
'aws_credentials_path',
__( 'Path to credentials file' ),
'text_input_callback',
'ss3_settings',
'aws_settings_section',
array(
'label_for' => 'aws_credentials_path',
'option_group' => 'ss3_settings',
'option_id' => 'aws_credentials_path',
)
);
add_settings_field(
'aws_credentials_profile',
__( 'Credentials profile' ),
'text_input_callback',
'ss3_settings',
'aws_settings_section',
array(
'label_for' => 'aws_credentials_profile',
'option_group' => 'ss3_settings',
'option_id' => 'aws_credentials_profile',
)
);
add_settings_field(
'link_timeout',
__( 'Link Timeout' ),
'text_input_callback',
'ss3_settings',
'aws_settings_section',
array(
'label_for' => 'link_timeout',
'option_group' => 'ss3_settings',
'option_id' => 'link_timeout',
)
);
add_settings_field(
'aws_region',
__( 'Region' ),
'text_input_callback',
'ss3_settings',
'aws_settings_section',
array(
'label_for' => 'aws_region',
'option_group' => 'ss3_settings',
'option_id' => 'aws_region',
)
);
add_settings_field(
'aws_version',
__( 'Version' ),
'text_input_callback',
'ss3_settings',
'aws_settings_section',
array(
'label_for' => 'aws_version',
'option_group' => 'ss3_settings',
'option_id' => 'aws_version',
)
);
register_setting( 'ss3_settings', 'ss3_settings' );
}
/**
* Respond to admin_menu events.
*/
public static function admin_menu() {
add_submenu_page(
'options-general.php',
__( 'Signed S3 Links Settings' ),
__( 'Signed S3 Links' ),
'manage_options',
'ss3_settings',
array( 'Signed_S3_Links', 'render_settings_page' )
);
}
/**
* Build an AWS SDK object from plugin options.
*
* @param array $aws_opts configuration to override global options.
*/
private static function create_aws_sdk( $aws_opts ) {
$options = get_option( 'ss3_settings' );
$aws_config = array(
'region' => isset( $aws_opts['region'] ) ? $aws_opts['region'] : $options['aws_region'],
'version' => $options['aws_version'],
);
$profile = $options['aws_credentials_profile'];
$credentials_path = $options['aws_credentials_path'];
if ( $credentials_path ) {
if ( ! str_starts_with( $credentials_path, '/' ) ) {
$credentials_path = SIGNED_S3_LINKS__PLUGIN_DIR . $credentials_path;
}
$provider = CredentialProvider::ini( $profile, $credentials_path );
$provider = CredentialProvider::memoize( $provider );
$aws_config['credentials'] = $provider;
} else {
$aws_config['profile'] = $profile;
}
$sdk = new Aws\Sdk( $aws_config );
return $sdk;
}
/**
* Setup hooks if necessary
*/
public static function init() {
if ( self::$initialized ) {
return;
}
self::$initialized = true;
add_action( 'admin_init', array( 'Signed_S3_Links', 'admin_init' ) );
add_action( 'admin_menu', array( 'Signed_S3_Links', 'admin_menu' ) );
add_action( 'wp_enqueue_scripts', array( 'Signed_S3_Links', 'wp_enqueue_scripts' ) );
add_shortcode( 'ss3_audio', array( 'Signed_S3_Link_Handler', 'audio_shortcode' ) );
add_shortcode( 'ss3_dir', array( 'Signed_S3_Link_Handler', 'list_dir_shortcode' ) );
add_shortcode( 'ss3_ref', array( 'Signed_S3_Link_Handler', 'href_shortcode' ) );
add_action( 'updated_option', array( 'Signed_S3_Links', 'respond_updated_option' ) );
}
/**
* Respond to plugin_activation events.
*/
public static function plugin_activation() {
// Write default settings.
$options = get_option( 'ss3_settings' );
if ( ! isset( $options['aws_credentials_path'] ) ) {
$options['aws_credentials_path'] = '';
}
if ( ! isset( $options['aws_credentials_profile'] ) ) {
$options['aws_credentials_profile'] = 'default';
}
if ( ! isset( $options['aws_region'] ) ) {
$options['aws_region'] = 'us-east-2';
}
if ( ! isset( $options['aws_version'] ) ) {
$options['aws_version'] = 'latest';
}
if ( ! isset( $options['link_timeout'] ) ) {
$options['link_timeout'] = '+8 hours';
}
add_option( 'ss3_settings', $options );
}
/**
* Respond to plugin_deactivation events.
*/
public static function plugin_deactivation() {
delete_option( 'ss3_settings' );
}
/**
* Build the plugin settings page.
*/
public static function render_settings_page() {
?>
<div class="wrap">
<h2><?php esc_html_e( 'Signed S3 Links Settings' ); ?></h2>
<form method="post" action="options.php">
<?php
settings_fields( 'ss3_settings' );
do_settings_sections( 'ss3_settings' );
submit_button();
?>
</form>
</div><!-- /.wrap -->
<?php
}
/**
* Reset the S3 client if the admin changes the values. Defer building
* the client until necessary.
*/
public static function respond_updated_option( $option_name ) {
Signed_S3_Links::$s3_client = null;
Signed_S3_Links::$s3_config = null;
}
/**
* Provide an S3 client.
*
* @param array $aws_opts configuration to override global options.
*/
public static function s3( $aws_opts ) {
if ( $aws_opts == self::$s3_config && self::$s3_client != null ) {
return self::$s3_client;
}
self::$s3_config = $aws_opts;
$aws_sdk = self::create_aws_sdk( $aws_opts );
self::$s3_client = $aws_sdk->createS3();
return self::$s3_client;
}
/**
* Respond to wp_enqueue_scripts events.
*/
public static function wp_enqueue_scripts() {
wp_register_style( 'signed-s3-links', plugins_url( 'style.css', __FILE__ ), array(), '0.1.0' );
wp_enqueue_style( 'signed-s3-links' );
}
}
?>