Skip to content

Commit

Permalink
Updatable expiry time and set version
Browse files Browse the repository at this point in the history
Updatable expiry time and set version
  • Loading branch information
wilson1000-MoJ authored Mar 30, 2020
2 parents ca9a71a + e69b2d6 commit 891798e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
29 changes: 26 additions & 3 deletions rewrite-media-to-s3.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
<?php

/**
* Plugin name: Rewrite Media to CDN
* Description: This plugin will rewrite media asset URLs to their equivalent CDN URL.
* The file responsible for starting the Rewrite Media to CDN plugin
*
* The Rewrite Media to CDN is a plugin that rewrites the domain path
* of images, documents and other assets to a defined domain.
* It also signs URLs with a secure hash so it can be verified
* and accessible via the AWS s3 bucket.
* This particular file is responsible for
* including the necessary dependencies and starting the plugin.
*
* @package Rewrite Media to CDN
*
* @wordpress-plugin
* Plugin Name: Rewrite Media to CDN
* Plugin URI: https://github.com/ministryofjustice/wp-rewrite-media-to-s3
* Description: This plugin will rewrite media asset and sign URLs to their equivalent CDN URL.
* Version: 0.2.4
* Text Domain: rewrite-media-cdn
* Author: Ministry of Justice
* Author URI: https://github.com/ministryofjustice
* License: The MIT License (MIT)
* License URI: https://opensource.org/licenses/MIT
* Copyright: Crown Copyright (c) 2020 Ministry of Justice
*/

namespace MOJDigital\RewriteMediaToS3;


//Do not allow access outside of WP to plugin
defined('ABSPATH') or die();

require 'autoload.php';
require_once('src/settings.php');

Expand Down
10 changes: 8 additions & 2 deletions src/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct()
]
]);
} catch (AwsException $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
trigger_error('AWS S3Client caught exception: ' . $e->getMessage(), E_USER_WARNING);
}

$this->bucket = env('AWS_S3_BUCKET') ?: false;
Expand Down Expand Up @@ -65,7 +65,13 @@ public function uri($uri)
'Key' => $uri
]);

$request = $this->client->createPresignedRequest($cmd, '+5 minutes');
$expires_time = '5 minutes';
$expires = get_option('rewrite_media_to_s3_settings', []);
if (!empty($expires) && isset($expires['secure_expiry_time'])) {
$expires_time = $expires['secure_expiry_time'];
}

$request = $this->client->createPresignedRequest($cmd, '+' . $expires_time);

return (string)$request->getUri();
}
Expand Down
33 changes: 33 additions & 0 deletions src/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ function rewrite_media_to_s3_settings_init()
'rewrite_media_to_s3_plugin'
);

add_settings_field(
'rewrite_media_to_s3_select_expire',
__('Expiry Time:', 'wordpress'),
'rewrite_media_to_s3_select_field_2_render',
'rewrite_media_to_s3_plugin',
'rewrite_media_to_s3_settings_section'
);

add_settings_field(
'rewrite_media_to_s3_select',
__('Activation', 'wordpress'),
Expand All @@ -46,6 +54,31 @@ function rewrite_media_to_s3_select_field_1_render()
<?php
}

function rewrite_media_to_s3_select_field_2_render()
{
$options = get_option('rewrite_media_to_s3_settings');

$option_values = [
'option_1' => '30 seconds',
'option_2' => '1 minute',
'option_3' => '5 minutes',
'option_4' => '10 minutes',
'option_5' => '20 minutes',
'option_6' => '30 minutes'
];
?>
<select name='rewrite_media_to_s3_settings[secure_expiry_time]'>
<option value='' disabled="disabled">URLs expire after:</option>
<?php
foreach ($option_values as $value) {
echo "<option value='" . $value . "' " . selected($options['secure_expiry_time'], $value) . ">" . $value . "</option>";
}
?>
</select>
<p>Define the 'end of life' time for a media URL. The time starts once a request for the media has been made by a browser.<br>Once time has expired the media on S3 will no longer be accessible.</p>
<?php
}

function rewrite_media_to_s3_signature_section_intro()
{

Expand Down

0 comments on commit 891798e

Please sign in to comment.