dukt /
twitter
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @link https://dukt.net/twitter/ |
||
| 4 | * @copyright Copyright (c) Dukt |
||
| 5 | * @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace dukt\twitter\fields; |
||
| 9 | |||
| 10 | use Craft; |
||
| 11 | use craft\base\ElementInterface; |
||
| 12 | use craft\base\Field; |
||
| 13 | use dukt\twitter\helpers\TwitterHelper; |
||
| 14 | use dukt\twitter\models\Tweet as TweetModel; |
||
| 15 | use dukt\twitter\Plugin; |
||
| 16 | use dukt\twitter\web\assets\tweetfield\TweetFieldAsset; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Tweet field |
||
| 20 | * |
||
| 21 | * @author Dukt <[email protected]> |
||
| 22 | * @since 3.0 |
||
| 23 | */ |
||
| 24 | class Tweet extends Field |
||
| 25 | { |
||
| 26 | // Public Methods |
||
| 27 | // ========================================================================= |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @inheritdoc |
||
| 31 | */ |
||
| 32 | public static function displayName(): string |
||
| 33 | { |
||
| 34 | return Craft::t('twitter', 'Tweet'); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @inheritdoc |
||
| 39 | */ |
||
| 40 | public function getInputHtml($value, ElementInterface $element = null): string |
||
| 41 | { |
||
| 42 | $name = $this->handle; |
||
| 43 | |||
| 44 | $id = Craft::$app->getView()->formatInputId($name); |
||
| 45 | |||
| 46 | $previewHtml = ''; |
||
| 47 | |||
| 48 | if ($value) { |
||
| 49 | $tweetId = TwitterHelper::extractTweetId($value); |
||
| 50 | |||
| 51 | if ($tweetId) { |
||
|
0 ignored issues
–
show
|
|||
| 52 | $cachedTweet = $this->getCachedTweet($tweetId); |
||
| 53 | |||
| 54 | if ($cachedTweet) { |
||
| 55 | $tweet = new TweetModel(); |
||
| 56 | Plugin::getInstance()->getApi()->populateTweetFromData($tweet, $cachedTweet); |
||
| 57 | |||
| 58 | $previewHtml = Craft::$app->getView()->renderTemplate('twitter/_components/tweet', [ |
||
| 59 | 'tweet' => $tweet |
||
| 60 | ]); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | Craft::$app->getView()->registerAssetBundle(TweetFieldAsset::class); |
||
| 66 | Craft::$app->getView()->registerJs('new TweetInput("'.Craft::$app->getView()->namespaceInputId($id).'");'); |
||
| 67 | |||
| 68 | return '<div class="tweet-field">'. |
||
| 69 | Craft::$app->getView()->renderTemplate('_includes/forms/text', [ |
||
| 70 | 'id' => $id, |
||
| 71 | 'name' => $name, |
||
| 72 | 'value' => $value, |
||
| 73 | 'placeholder' => Craft::t('twitter', 'Enter a tweet URL or ID'), |
||
| 74 | ]). |
||
| 75 | '<div class="spinner hidden"></div>'. |
||
| 76 | '<div class="preview'.($previewHtml ? '' : ' hidden').'">'.$previewHtml.'</div>'. |
||
| 77 | '</div>'; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Private Methods |
||
| 81 | // ========================================================================= |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string|int $tweetId |
||
| 85 | * |
||
| 86 | * @return mixed |
||
| 87 | */ |
||
| 88 | private function getCachedTweet($tweetId) |
||
| 89 | { |
||
| 90 | $uri = 'statuses/show'; |
||
| 91 | $headers = null; |
||
| 92 | $options = [ |
||
| 93 | 'query' => [ |
||
| 94 | 'id' => $tweetId, |
||
| 95 | 'tweet_mode' => 'extended' |
||
| 96 | ] |
||
| 97 | ]; |
||
| 98 | |||
| 99 | return Plugin::getInstance()->getCache()->get([$uri, $headers, $options]); |
||
| 100 | } |
||
| 101 | } |
||
| 102 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: