Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from lewiscommunications/0.4.2
Browse files Browse the repository at this point in the history
0.4.2
  • Loading branch information
Ryan Payne authored Sep 14, 2017
2 parents 1889311 + 2329db7 commit afdaefe
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 30 deletions.
10 changes: 10 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,15 @@
"notes": [
"[Fixed] Fixed empty .csv import column issue."
]
},
{
"version": "0.4.2",
"downloadUrl": "https://github.com/lewiscommunications/craft-wistia/archive/0.4.2.zip",
"date": "2017-09-14T21:06:13+00:00",
"notes": [
"[Fixed] Fixed issue where required Wistia fields were not validating.",
"[Fixed] Fixed issue where nonexistent hashed ids stopped page load.",
"[Added] Added http session cache clearing on plugin settings save."
]
}
]
28 changes: 27 additions & 1 deletion wistia/WistiaPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class WistiaPlugin extends BasePlugin
{
private $name = 'Wistia';
private $version = '0.4.1';
private $version = '0.4.2';
private $description = 'Powerful fieldtype and template tags for Wistia videos.';

public function getName()
Expand Down Expand Up @@ -69,6 +69,32 @@ public function getSettingsHtml()
));
}

public function prepSettings($settings)
{
$projects = craft()->httpSession->get('wistiaProjects');

if ($projects) {
// Remove video session cache in each project
foreach($projects as $id => $name) {
$projectKey = 'wistia' . $id . 'Videos';

if (craft()->httpSession->get($projectKey)) {
craft()->httpSession->remove($projectKey);
}
}

// Remove project session cache
craft()->httpSession->remove('wistiaProjects');
}

// Remove all videos session cache
if (craft()->httpSession->get('wistiaAllVideos')) {
craft()->httpSession->remove('wistiaAllVideos');
}

return $settings;
}

public function registerCachePaths()
{
$cachePath = $_SERVER['DOCUMENT_ROOT'] .
Expand Down
12 changes: 6 additions & 6 deletions wistia/fieldtypes/Wistia_VideosFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ public function prepValueFromPost($hashedIds)
WistiaPlugin::log('Wistia video thumbnail with a hashed id of ' . $video['hashedId']. ' failed to load.', LogLevel::Warning);
}
}

$hashedIds = json_encode($hashedIds);
}

return json_encode($hashedIds);
return $hashedIds;
}

/**
* Modify stored data for output
*
* @param string $value
* @return object
* @param mixed $value
* @return mixed
*/
public function prepValue($value)
{
$value = json_decode($value);

return $value ? craft()->wistia_video->getVideos($value) : $value;
return ! empty($value) ? craft()->wistia_video->getVideos(json_decode($value)) : $value;
}

/**
Expand Down
66 changes: 44 additions & 22 deletions wistia/services/Wistia_VideoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ public function getVideosByHashedId($hashedIds, $params = array())
$embed = $this->getEmbed($hashedId, $params);

$cachedVideo = craft()->cache->get($cacheKey);
$video = $cachedVideo ?
$cachedVideo :
current(
$this->getApiData('medias.json', array(
'hashed_id' => $hashedId
))
);

// Cache Wistia API data
if ($cachedVideo) {
$video = $cachedVideo;
} else {
$video = current($this->getApiData('medias.json', array(
'hashed_id' => $hashedId
)));
// Does a video exist?
if (! $video) {
continue;
}

// Is there a cached video?
if (! $cachedVideo) {
$video['hashedId'] = $hashedId;

// Remove old embed code
Expand Down Expand Up @@ -131,6 +137,7 @@ public function getVideosByHashedId($hashedIds, $params = array())

$video['name'] = htmlspecialchars_decode($video['name']);

// Cache video for defined time period
craft()->cache
->set($cacheKey, $video, (int) craft()->plugins
->getPlugin('wistia')
Expand Down Expand Up @@ -170,12 +177,6 @@ public function getVideosByHashedId($hashedIds, $params = array())
*/
public function getVideosByProjectId($projects)
{
$cacheString = is_array($projects) ? implode('-', $projects) : '-' . $projects;

if (($videos = craft()->httpSession->get('wistiaProjectVideos' . $cacheString, false)) !== false) {
return $videos;
}

$videos = array();

// Add videos from each project
Expand All @@ -184,6 +185,7 @@ public function getVideosByProjectId($projects)
try {
$projectNames = $this->getProjects();
} catch (Exception $e) {
// TODO: lang method is an old EE method
throw new Exception(lang('error_no_projects'), 1, $e);
}

Expand All @@ -193,6 +195,14 @@ public function getVideosByProjectId($projects)
}

foreach ($projects as $project) {
$cachedVideos = craft()->httpSession->get('wistia' . $project . 'Videos');

if ($cachedVideos) {
$videos = array_merge($videos, $cachedVideos);

continue;
}

$params = array(
'sort_by' => 'name',
'project_id' => $project
Expand All @@ -202,10 +212,11 @@ public function getVideosByProjectId($projects)
try {
$data = $this->getApiData('medias.json', $params);
} catch (Exception $e) {
// TODO: lang method is an old EE method
throw new Exception(lang('error_no_video_list') . $project, 5, $e);
}

// Skip empty datasets
// Skip empty data sets
if (! is_array($data)) {
continue;
}
Expand All @@ -216,20 +227,28 @@ public function getVideosByProjectId($projects)

$videos[$hashedId] = $name;
}

craft()->httpSession->add('wistia' . $project . 'Videos', $videos);
}
} else {
foreach ($this->getApiData('medias.json') as $video) {
$hashedId = WistiaHelper::getValue('hashed_id', $video);
$name = htmlspecialchars_decode(WistiaHelper::getValue('name', $video));
$cachedVideos = craft()->httpSession->get('wistiaAllVideos', $videos);

if ($cachedVideos) {
$videos = array_merge($videos, $cachedVideos);
} else {
foreach ($this->getApiData('medias.json') as $video) {
$hashedId = WistiaHelper::getValue('hashed_id', $video);
$name = htmlspecialchars_decode(WistiaHelper::getValue('name', $video));

$videos[$hashedId] = $name;
$videos[$hashedId] = $name;

craft()->httpSession->add('wistiaAllVideos', $videos);
}
}
}

asort($videos);

craft()->httpSession->add('wistiaProjectVideos' . $cacheString, $videos);

return $videos;
}

Expand All @@ -245,7 +264,9 @@ public function getProjects()
throw new Exception(lang('error_no_api_key'), 0);
}

if ($projects = craft()->httpSession->get('wistiaProjects', false)) {
$projects = craft()->httpSession->get('wistiaProjects', false);

if ($projects) {
return $projects;
}

Expand All @@ -265,7 +286,7 @@ public function getProjects()
$projects[WistiaHelper::getValue('id', $project)] = WistiaHelper::getValue('name', $project);
}

craft()->httpSession->add('projects', $projects);
craft()->httpSession->add('wistiaProjects', $projects);

return $projects;
}
Expand Down Expand Up @@ -339,6 +360,7 @@ private function getApiData($endpoint, $params = array(), $page = 1)
$data = $this->send($apiUrl);

if ($data === false) {
// TODO: lang is an old EE method
throw new Exception(lang('error_remote_file') . $apiUrl, 3);
}

Expand Down
4 changes: 3 additions & 1 deletion wistia/templates/fieldtype/input.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<input type="hidden" name="{{ name }}">
{% if name is defined and name %}
<input type="hidden" name="{{ name }}" value="">
{% endif -%}

<div class="elementselect" id="{{ name }}">
<div class="elements">
Expand Down

0 comments on commit afdaefe

Please sign in to comment.