Skip to content

Commit

Permalink
Craft CMS 4 ready
Browse files Browse the repository at this point in the history
  • Loading branch information
ammannbe committed Jul 22, 2022
1 parent e070b58 commit 8883457
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres
to [Semantic Versioning](http://semver.org/).

## 2.0.0 - 2022-07-22

- Craft CMS 4 ready

## 1.2.3 - 2021-09-16

- Consistent plugin naming in all files
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "homm/hommsocialfeed",
"description": "Craft CMS Social Feed Adapter for juicer.io",
"type": "craft-plugin",
"version": "1.2.3",
"version": "2.0.0",
"keywords": [
"craft",
"cms",
Expand All @@ -24,8 +24,10 @@
}
],
"require": {
"craftcms/cms": "^3.0.0",
"ext-json": "*"
"php": "^8.0|^8.1",
"craftcms/cms": "^4.0.0",
"ext-json": "*",
"ext-fileinfo": "*"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 5 additions & 5 deletions src/HOMMSocialFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ class HOMMSocialFeed extends Plugin
/**
* @var string
*/
public $schemaVersion = '1.2.0';
public string $schemaVersion = '1.2.0';

/**
* @var bool
*/
public $hasCpSettings = true;
public bool $hasCpSettings = true;

/**
* @var bool
*/
public $hasCpSection = true;
public bool $hasCpSection = true;

// Public Methods
// =========================================================================
Expand Down Expand Up @@ -124,15 +124,15 @@ function (Event $event) {
/**
* @inheritdoc
*/
protected function createSettingsModel()
protected function createSettingsModel(): ?\craft\base\Model
{
return new Settings();
}

/**
* @inheritdoc
*/
protected function settingsHtml(): string
protected function settingsHtml(): ?string
{
return Craft::$app->view->renderTemplate(
'hommsocialfeed/settings',
Expand Down
71 changes: 43 additions & 28 deletions src/elements/SocialFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

use Craft;
use craft\base\Element;
use craft\elements\actions\Edit;
use craft\elements\actions\SetStatus;
use craft\elements\db\ElementQueryInterface;
use craft\elements\User;
use craft\validators\StringValidator;
use craft\web\View;
use GuzzleHttp\Client as GuzzleClient;
Expand All @@ -36,7 +38,7 @@ class SocialFeed extends Element
/**
* @var int Social Feed ID
*/
public $feedId;
public int $feedId;

/**
* @var \DateTime Social Feed Date Created
Expand All @@ -46,17 +48,17 @@ class SocialFeed extends Element
/**
* @var string The URL to the feed
*/
public $feedUrl;
public string $feedUrl;

/**
* @var string|null External URL provided from the feed
*/
public $externalUrl = null;
public ?string $externalUrl = null;

/**
* @var string|null Name of the social media provider
*/
public $source = null;
public ?string $source = null;

/**
* @var string|null Source options
Expand All @@ -65,47 +67,42 @@ class SocialFeed extends Element
* of response you get back they will be included here.
* E.g. "retweets".
*/
public $sourceOptions = null;
public ?string $sourceOptions = null;

/**
* @var string Feed text or title (as HTML)
*/
public $message;
public string $message;

/**
* @var string Like Count
* @var int Like Count
*/
public $likeCount = 0;
public int $likeCount = 0;

/**
* @var string|null Image URL
*/
public $image = null;
public ?string $image = null;

/**
* @var array|null Additional photos URL
*/
public $additionalPhotos = null;
public ?array $additionalPhotos = null;

/**
* @var string|null Video URL
*/
public $video = null;
public ?string $video = null;

/**
* @var bool Hide the image or not
*/
public $isMediaHidden = false;
public bool $isMediaHidden = false;

/**
* @var string|null Color handle
*/
public $color = null;

/**
* @var int|null Pseudo property to count feeds by source
*/
public $socialfeedsCount = null;
public ?string $color = null;

/**
* @inheritdoc
Expand All @@ -126,7 +123,7 @@ public static function pluralDisplayName(): string
/**
* @inheritdoc
*/
public static function refHandle()
public static function refHandle(): ?string
{
return 'socialfeed';
}
Expand All @@ -139,6 +136,26 @@ public static function hasStatuses(): bool
return true;
}

/**
* @inheritdoc
*/
public function canView(User $user): bool
{
return false;
}

/**
* @inheritdoc
*/
public function canSave(User $user): bool
{
if (parent::canSave($user)) {
return true;
}

return $user->can('hommsocialfeed-saveSocialFeed:' . $this->uid);
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -183,7 +200,7 @@ protected static function defineSources(string $context = null): array
$sources[] = [
'key' => '*',
'label' => Craft::t('hommsocialfeed', 'All feeds'),
'badgeCount' => self::find()->anyStatus()->count(),
'badgeCount' => self::find()->status(null)->count(),
'defaultSort' => ['feedDateCreated', 'desc'],
'criteria' => [],
];
Expand Down Expand Up @@ -245,13 +262,11 @@ protected static function defineSearchableAttributes(): array
*/
protected static function defineActions(string $source = null): array
{
$actions = [];

$actions[] = SetStatus::class;
$actions[] = SetColor::class;
$actions[] = HideMedia::class;

return $actions;
return [
SetStatus::class,
SetColor::class,
HideMedia::class,
];
}

/**
Expand Down Expand Up @@ -310,7 +325,7 @@ public function beforeSave(bool $isNew): bool
/**
* @inheritdoc
*/
public function afterSave(bool $isNew)
public function afterSave(bool $isNew): void
{
$attributes = [
'feedId' => $this->feedId,
Expand Down
4 changes: 2 additions & 2 deletions src/elements/actions/HideMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HideMedia extends ElementAction
/**
* @inheritdoc
*/
public function getTriggerHtml()
public function getTriggerHtml(): ?string
{
\Craft::$app->view->setTemplateMode(\craft\web\View::TEMPLATE_MODE_CP);
return \Craft::$app->view->renderTemplate('hommsocialfeed/feeds/_hideMediaTrigger');
Expand All @@ -53,7 +53,7 @@ public function performAction(ElementQueryInterface $query): bool
}
}

$this->setMessage(Craft::t('hommsocialfeed', 'Successfully updated.'));
$this->setMessage(\Craft::t('hommsocialfeed', 'Successfully updated.'));
return true;
}
}
4 changes: 2 additions & 2 deletions src/elements/actions/SetColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SetColor extends ElementAction
/**
* @inheritdoc
*/
public function getTriggerHtml()
public function getTriggerHtml(): ?string
{
// Render the trigger menu template with all the available ingredients
$colors = HOMMSocialFeed::$plugin->getSettings()->colors;
Expand All @@ -56,7 +56,7 @@ public function performAction(ElementQueryInterface $query): bool
}
}

$this->setMessage(Craft::t('hommsocialfeed', 'Successfully updated.'));
$this->setMessage(\Craft::t('hommsocialfeed', 'Successfully updated.'));
return true;
}
}
2 changes: 1 addition & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Settings extends Model
/**
* @inheritdoc
*/
public function rules()
public function rules(): array
{
return [
[['apiPath', 'numberOfFeeds'], 'required'],
Expand Down
4 changes: 2 additions & 2 deletions src/services/SocialFeedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function fetch()
$posts = json_decode($response->getBody())->posts->items; // TODO: extend the Guzzle ResponseInterface

$errors = [];
$socialFeeds = SocialFeed::find()->anyStatus()->where(['feedId' => array_column($posts, 'id')])->all();
$socialFeeds = SocialFeed::find()->status(null)->where(['feedId' => array_column($posts, 'id')])->all();
$feedIds = array_column($socialFeeds, 'feedId');
foreach ($posts as $post) {
$socialFeed = new SocialFeed();
if (in_array($post->id, $feedIds)) {
$socialFeed = SocialFeed::find()->anyStatus()->where(['feedId' => $post->id])->one();
$socialFeed = SocialFeed::find()->status(null)->where(['feedId' => $post->id])->one();
}

$attributes = [
Expand Down

0 comments on commit 8883457

Please sign in to comment.