Skip to content

Commit

Permalink
Merge pull request #465 from creative-commoners/pulls/1/shortcode-grant
Browse files Browse the repository at this point in the history
API Create extension hook for updating session grant on files
  • Loading branch information
michalkleiner committed Mar 2, 2022
2 parents 8ed643d + 640f8da commit f3bdb59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
49 changes: 37 additions & 12 deletions src/Shortcodes/FileShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,12 @@ public static function get_shortcodes()
*/
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = [])
{
$allowSessionGrant = static::config()->allow_session_grant;

/** @var CacheInterface $cache */
$cache = static::getCache();
$cacheKey = static::getCacheKey($arguments, $content);

$item = $cache->get($cacheKey);
if ($item) {
// Initiate a protected asset grant if necessary
if (!empty($item['filename']) && $allowSessionGrant) {
Injector::inst()->get(AssetStore::class)->grant($item['filename'], $item['hash']);
}

return $item['markup'];
$cachedMarkup = static::getCachedMarkup($cache, $cacheKey, $arguments);
if ($cachedMarkup) {
return $cachedMarkup;
}

// Find appropriate record, with fallback for error handlers
Expand All @@ -104,7 +96,8 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod

// Retrieve the file URL (ensuring session grant config is respected)
if ($record instanceof File) {
$url = $record->getURL($allowSessionGrant);
$grant = static::getGrant($record);
$url = $record->getURL($grant);
} else {
$url = $record->Link();
}
Expand Down Expand Up @@ -137,6 +130,38 @@ public static function handle_shortcode($arguments, $content, $parser, $shortcod
return $markup;
}

/**
* @param CacheInterface $cache
* @param string $cacheKey
* @param array $arguments
* @return string
*/
protected static function getCachedMarkup($cache, $cacheKey, $arguments): string
{
$item = $cache->get($cacheKey);
if ($item && !empty($item['filename'])) {
// Initiate a protected asset grant if necessary
$allowSessionGrant = static::getGrant(null, $arguments);
if ($allowSessionGrant) {
Injector::inst()->get(AssetStore::class)->grant($item['filename'], $item['hash']);
return $item['markup'];
}
}
return '';
}

/**
* @param File|null $record
* @param array|null $args
* @return bool
*/
protected static function getGrant(?File $record, ?array $args = null): bool
{
$grant = static::config()->allow_session_grant;
static::singleton()->extend('updateGrant', $grant, $record, $args);
return $grant;
}

/**
* Find the record to use for a given shortcode.
*
Expand Down
22 changes: 8 additions & 14 deletions src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,12 @@ public static function get_shortcodes()
*/
public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = [])
{
$allowSessionGrant = static::config()->allow_session_grant;

/** @var CacheInterface $cache */
$cache = static::getCache();
$cacheKey = static::getCacheKey($args);

$item = $cache->get($cacheKey);
if ($item) {
// Initiate a protected asset grant if necessary
if (!empty($item['filename']) && $allowSessionGrant) {
Injector::inst()->get(AssetStore::class)->grant($item['filename'], $item['hash']);
}

return $item['markup'];
$cacheKey = static::getCacheKey($args, $content);
$cachedMarkup = static::getCachedMarkup($cache, $cacheKey, $args);
if ($cachedMarkup) {
return $cachedMarkup;
}

// Find appropriate record, with fallback for error handlers
Expand All @@ -73,7 +66,8 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e
// Check if a resize is required
$width = null;
$height = null;
$src = $record->getURL($allowSessionGrant);
$grant = static::getGrant($record);
$src = $record->getURL($grant);
if ($record instanceof Image) {
$width = isset($args['width']) ? (int) $args['width'] : null;
$height = isset($args['height']) ? (int) $args['height'] : null;
Expand All @@ -82,7 +76,7 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e
$resized = $record->ResizedImage($width, $height);
// Make sure that the resized image actually returns an image
if ($resized) {
$src = $resized->getURL($allowSessionGrant);
$src = $resized->getURL($grant);
}
}
}
Expand Down

0 comments on commit f3bdb59

Please sign in to comment.