From dbd439088e36293264c637a1842f8e902c85b466 Mon Sep 17 00:00:00 2001 From: Ryan Payne Date: Tue, 12 Sep 2017 17:32:25 -0500 Subject: [PATCH 1/5] Check if a video is returned before adding to array output Fixes #2 --- wistia/services/Wistia_VideoService.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/wistia/services/Wistia_VideoService.php b/wistia/services/Wistia_VideoService.php index 879b995..fe00ac2 100755 --- a/wistia/services/Wistia_VideoService.php +++ b/wistia/services/Wistia_VideoService.php @@ -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 @@ -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') From d31d983001ae6e839452355f287c065a5d4d4587 Mon Sep 17 00:00:00 2001 From: Ryan Payne Date: Tue, 12 Sep 2017 18:56:04 -0500 Subject: [PATCH 2/5] Fix required field validation issue Fixes #3 --- wistia/fieldtypes/Wistia_VideosFieldType.php | 12 ++++++------ wistia/templates/fieldtype/input.twig | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/wistia/fieldtypes/Wistia_VideosFieldType.php b/wistia/fieldtypes/Wistia_VideosFieldType.php index e69eff3..383fa5a 100644 --- a/wistia/fieldtypes/Wistia_VideosFieldType.php +++ b/wistia/fieldtypes/Wistia_VideosFieldType.php @@ -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; } /** diff --git a/wistia/templates/fieldtype/input.twig b/wistia/templates/fieldtype/input.twig index a0acf45..4eef35c 100644 --- a/wistia/templates/fieldtype/input.twig +++ b/wistia/templates/fieldtype/input.twig @@ -1,4 +1,6 @@ - +{% if name is defined and name %} + +{% endif -%}
From f7669a8090af13473508ed4e2deac644edac6d8f Mon Sep 17 00:00:00 2001 From: Ryan Payne Date: Wed, 13 Sep 2017 11:32:11 -0500 Subject: [PATCH 3/5] Clear session cache on save settings --- wistia/WistiaPlugin.php | 26 ++++++++++++++ wistia/services/Wistia_VideoService.php | 45 ++++++++++++++++--------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/wistia/WistiaPlugin.php b/wistia/WistiaPlugin.php index eacfdeb..d25275c 100644 --- a/wistia/WistiaPlugin.php +++ b/wistia/WistiaPlugin.php @@ -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'] . diff --git a/wistia/services/Wistia_VideoService.php b/wistia/services/Wistia_VideoService.php index fe00ac2..5355b37 100755 --- a/wistia/services/Wistia_VideoService.php +++ b/wistia/services/Wistia_VideoService.php @@ -177,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 @@ -191,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); } @@ -200,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 @@ -209,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; } @@ -223,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); - $videos[$hashedId] = $name; + 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; + + craft()->httpSession->add('wistiaAllVideos', $videos); + } } } asort($videos); - craft()->httpSession->add('wistiaProjectVideos' . $cacheString, $videos); - return $videos; } @@ -252,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; } @@ -272,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; } @@ -346,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); } From 2bfb4fd81a355219f748b9de3ed9c204019d7ef3 Mon Sep 17 00:00:00 2001 From: Ryan Payne Date: Wed, 13 Sep 2017 11:34:14 -0500 Subject: [PATCH 4/5] Bump version number Closes #1 --- wistia/WistiaPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wistia/WistiaPlugin.php b/wistia/WistiaPlugin.php index d25275c..73b070d 100644 --- a/wistia/WistiaPlugin.php +++ b/wistia/WistiaPlugin.php @@ -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() From 2329db70bd90657c44e7e1033715f477ba410dc3 Mon Sep 17 00:00:00 2001 From: Ryan Payne Date: Thu, 14 Sep 2017 16:08:54 -0500 Subject: [PATCH 5/5] Update releases file --- releases.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/releases.json b/releases.json index ce9a1dd..7cff34a 100644 --- a/releases.json +++ b/releases.json @@ -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." + ] } ]