-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
628 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Media Manager | ||
* | ||
* @package PaperTiger:MediaManager | ||
* @author Paper Tiger | ||
* @copyright Copyright (c) 2020 Paper Tiger | ||
* @link https://www.papertiger.com/ | ||
*/ | ||
|
||
namespace papertiger\mediamanager\jobs; | ||
|
||
use Craft; | ||
use craft\helpers\Db; | ||
use craft\queue\BaseJob; | ||
use craft\elements\Entry; | ||
use craft\elements\Asset; | ||
use craft\elements\Tag; | ||
use craft\helpers\ElementHelper; | ||
use craft\helpers\Assets as AssetHelper; | ||
|
||
use papertiger\mediamanager\MediaManager; | ||
use papertiger\mediamanager\helpers\SettingsHelper; | ||
|
||
class CancelStaleMedia extends BaseJob | ||
{ | ||
|
||
// Public Properties | ||
// ========================================================================= | ||
|
||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
public function execute( $queue ): void | ||
{ | ||
|
||
$relatedMediaObjects = Entry::find()->markedForDeletion(1); | ||
|
||
foreach(Db::each($relatedMediaObjects) as $media) { | ||
$media->setFieldValue('markedForDeletion', 0); | ||
Craft::$app->getElements()->saveElement($media); | ||
} | ||
} | ||
|
||
// Protected Methods | ||
// ========================================================================= | ||
|
||
protected function defaultDescription(): string | ||
{ | ||
return Craft::t( 'mediamanager', "Unmarking entries for deletion." ); | ||
} | ||
|
||
// Private Methods | ||
// ========================================================================= | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* Media Manager | ||
* | ||
* @package PaperTiger:MediaManager | ||
* @author Paper Tiger | ||
* @copyright Copyright (c) 2020 Paper Tiger | ||
* @link https://www.papertiger.com/ | ||
*/ | ||
|
||
namespace papertiger\mediamanager\jobs; | ||
|
||
use Craft; | ||
use craft\helpers\Db; | ||
use craft\queue\BaseJob; | ||
use craft\elements\Entry; | ||
use craft\elements\Asset; | ||
use craft\elements\Tag; | ||
use craft\helpers\ElementHelper; | ||
use craft\helpers\Assets as AssetHelper; | ||
|
||
use DateTime; | ||
use papertiger\mediamanager\MediaManager; | ||
use papertiger\mediamanager\helpers\SettingsHelper; | ||
|
||
class IdentifyStaleMedia extends BaseJob | ||
{ | ||
|
||
// Public Properties | ||
// ========================================================================= | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public string $date; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public string $tags; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public int $sectionId; | ||
|
||
/** | ||
* @var int|array | ||
*/ | ||
public int|array $siteId; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
public function execute( $queue ): void | ||
{ | ||
|
||
if (!$this->tags) { | ||
// too generic, exit | ||
return; | ||
} | ||
|
||
|
||
$relatedMediaObjects = Entry::find()->sectionId($this->sectionId)->lastSynced("< {$this->date}")->relatedTo(['and', $this->tags])->markedForDeletion(0)->siteId($this->siteId)->ids(); | ||
|
||
foreach($relatedMediaObjects as $media) { | ||
if (!$this->_queueJobExists($media)) { | ||
$queue = Craft::$app->getQueue(); | ||
$queue->push(new MarkStaleMedia(['entryId' => $media])); | ||
} | ||
} | ||
} | ||
|
||
// Protected Methods | ||
// ========================================================================= | ||
|
||
protected function defaultDescription(): string | ||
{ | ||
return Craft::t( 'mediamanager', "Marking entries for deletion." ); | ||
} | ||
|
||
// Private Methods | ||
// ========================================================================= | ||
|
||
private function _queueJobExists(int $entryId): bool | ||
{ | ||
// Preflight check to ensure regular queue in place | ||
if(!Craft::$app->queue->hasProperty('jobInfo')){ | ||
return false; | ||
} | ||
|
||
return in_array("Marking entry {$entryId} for deletion.", array_column(Craft::$app->queue->jobInfo, 'description'), true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* Media Manager | ||
* | ||
* @package PaperTiger:MediaManager | ||
* @author Paper Tiger | ||
* @copyright Copyright (c) 2020 Paper Tiger | ||
* @link https://www.papertiger.com/ | ||
*/ | ||
|
||
namespace papertiger\mediamanager\jobs; | ||
|
||
use Craft; | ||
use craft\helpers\Db; | ||
use craft\helpers\Json; | ||
use craft\queue\BaseJob; | ||
use craft\elements\Entry; | ||
use craft\elements\Asset; | ||
use craft\elements\Tag; | ||
use craft\helpers\ElementHelper; | ||
use craft\helpers\Assets as AssetHelper; | ||
|
||
use DateTime; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Exception\RequestException; | ||
use papertiger\mediamanager\MediaManager; | ||
use papertiger\mediamanager\helpers\SettingsHelper; | ||
use papertiger\mediamanager\services\Api; | ||
|
||
class MarkStaleMedia extends BaseJob | ||
{ | ||
|
||
// Public Properties | ||
// ========================================================================= | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public int $entryId; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
public function execute( $queue ): void | ||
{ | ||
$entry = Entry::find()->id($this->entryId)->one(); | ||
|
||
if(!$entry) { | ||
return; | ||
} | ||
|
||
$markForDeletion = 0; | ||
// hit PBS API to check if there is data for this entry's api key | ||
$client = Craft::createGuzzleClient(); | ||
$baseUrl = MediaManager::$plugin->api->getApiBaseUrl(); | ||
$apiAuth = MediaManager::$plugin->api->getApiAuth(); | ||
|
||
try { | ||
$data = $client->get($baseUrl . "assets/" . $entry->mediaManagerId . "?platform-slug=bento&platform-slug=partnerplayer", $apiAuth); | ||
|
||
$asset = Json::decode($data->getBody(), false); | ||
if(isset($asset->data)){ | ||
if(!isset($asset->data->attributes->availabilities->public, $asset->data->attributes->availabilities->all_members)){ | ||
return; | ||
} | ||
|
||
if($asset->data->attributes->availabilities->public->start === null && $asset->data->attributes->availabilities->all_members->start === null){ | ||
$markForDeletion = 1; | ||
} | ||
} | ||
} catch (ClientException $e) { | ||
if ($e->getCode() === 404) { | ||
$markForDeletion = 1; | ||
} | ||
} catch (RequestException $e) { | ||
Craft::error($e->getMessage(), __METHOD__); | ||
return; | ||
} | ||
|
||
// if data object is empty, return | ||
Craft::warning("Marking entry ID {$entry->id} for deletion.", __METHOD__); | ||
$entry->setFieldValue('markedForDeletion', $markForDeletion); | ||
$entry->setFieldValue('lastSynced', (new DateTime())); | ||
Craft::$app->getElements()->saveElement($entry); | ||
} | ||
|
||
// Protected Methods | ||
// ========================================================================= | ||
|
||
protected function defaultDescription(): string | ||
{ | ||
return Craft::t( 'mediamanager', "Marking entry {$this->entryId} for deletion." ); | ||
} | ||
|
||
// Private Methods | ||
// ========================================================================= | ||
} |
Oops, something went wrong.