HashandSalt / kirby3-twitter

A Twitter plugin forKirby
MIT License
8 stars 4 forks source link

Undefined variable: cachename with version 1.0.3 #9

Closed MaluNoPeleke closed 2 years ago

MaluNoPeleke commented 2 years ago

After updating from 1.0.2 to 1.0.3 and adapting the API changes I still get the following error message:

Whoops\Exception\ErrorException thrown with message "Undefined variable: cachename"

Stacktrace:

23 Whoops\Exception\ErrorException in /var/www/virtual/user/dev.domain.tld/site/snippets/twitter-timeline.php:3

22 Whoops\Run:handleError in /var/www/virtual/user/dev.domain.tld/site/snippets/twitter-timeline.php:3

My snippet at the moment looks like this:

<?php
if (option('twit.apiVersion') === '1.1') {   
    $tweets = $page->tweets($cachename, 'statuses/user_timeline', $params);
} elseif (option('twit.apiVersion') === '2') {
    $user = $page->twitterUserId($screenname);
    $tweets = $page->tweets($cachename, 'users/'.$user.'/tweets', $params);
    $tweets = $tweets['data'];
}
?>

<?php if (option('twit.apiVersion') === '1.1'): ?>
    <?php foreach ($tweets as $tweet): ?>

       <!-- component -->
        <div class="flex items-center justify-center mb-2">
        <div class="bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-800 md:px-4 md:pt-4 p-2 rounded-xl border max-w">
            <div class="flex justify-between">
            <div class="flex items-center">
                <img class="h-11 w-11 rounded-full" src="<?= $tweet['user']['profile_image_url_https'] ?>" width="48px" height="48px" alt="Profilfoto"/>
                <div class="ml-1.5 text-sm leading-tight">
                <a class="text-black dark:text-white font-bold block" href="https://twitter.com/<?= $tweet['user']['screen_name'] ?>"><?= $tweet['user']['name'] ?></a>
                <a class="text-gray-500 dark:text-gray-400 font-normal block" href="https://twitter.com/<?= $tweet['user']['screen_name'] ?>">@<?= $tweet['user']['screen_name'] ?></a>
                </div>
            </div>
            <svg class="text-blue-400 dark:text-white h-6 w-auto inline-block fill-current" viewBox="0 0 24 24"><g><path d="M23.643 4.937c-.835.37-1.732.62-2.675.733.962-.576 1.7-1.49 2.048-2.578-.9.534-1.897.922-2.958 1.13-.85-.904-2.06-1.47-3.4-1.47-2.572 0-4.658 2.086-4.658 4.66 0 .364.042.718.12 1.06-3.873-.195-7.304-2.05-9.602-4.868-.4.69-.63 1.49-.63 2.342 0 1.616.823 3.043 2.072 3.878-.764-.025-1.482-.234-2.11-.583v.06c0 2.257 1.605 4.14 3.737 4.568-.392.106-.803.162-1.227.162-.3 0-.593-.028-.877-.082.593 1.85 2.313 3.198 4.352 3.234-1.595 1.25-3.604 1.995-5.786 1.995-.376 0-.747-.022-1.112-.065 2.062 1.323 4.51 2.093 7.14 2.093 8.57 0 13.255-7.098 13.255-13.254 0-.2-.005-.402-.014-.602.91-.658 1.7-1.477 2.323-2.41z"></path></g></svg>
            </div>
            <p class="text-black dark:text-white block break-words leading-snug mt-3 twitter-text"><?= $tweet['full_text'] ?></p>
            <a class="mt-2 rounded-2xl border-0 border-gray-100 dark:border-gray-700" href="https://twitter.com/<?= $tweet['user']['screen_name'] ?>/status/<?= $tweet['id'] ?>">
                <?php
                    if($media === true) {
                        if(A::get($tweet, 'entities.media')) {
                        $url = $tweet['entities']['media'][0]['media_url_https'];
                        $height = $tweet['entities']['media'][0]['sizes']['small']['h'];
                        $width = $tweet['entities']['media'][0]['sizes']['small']['w'];
                        //echo Html::img($url . ":small");
                        echo "<img src='" . $url . ":small" . "' height='$height' width='$width' loading='lazy' decoding='async' alt='Bild aus Tweet'";
                        }
                    }
                    ?> 
                    </a>
            <a class="text-gray-500 dark:text-gray-400 text-sm py-1 my-0.5 float-right" href="https://twitter.com/<?= $tweet['user']['screen_name'] ?>/status/<?= $tweet['id'] ?>"><?= date('j.n.Y H:i', strtotime($tweet['created_at'])) ?></a>
        </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

<?php if (option('twit.apiVersion') === '2'): ?>  
    <?php foreach ($tweets as $tweet) : ?>
    <li>
     <?php echo '<p>'.$tweet['text'].'</p>';?>
    </li>
    <?php endforeach ?>
<?php endif; ?>

I didn't do any other changes than the modified snippet and the addition of bearer and API version in the config. How to fix this?

HashandSalt commented 2 years ago

The snippet call has changed slightly in the new version:

<?= snippet('twitter/tweets', ['cachename' => 'yourcachename', 'media' => true, 'params' => ['screen_name' => $screenName, 'tweet_mode' => 'extended', 'count' => 20, 'exclude_replies' => true]])?>

It sounds like you have updated code inside the snippet, but not the call to it in the template. The first option is the name of the cache file you want to store the data in - usually this will be the user name or tweet id, but you can set it to anything.

MaluNoPeleke commented 2 years ago

Sorry, I forgot about that step, that's correct. Now I changed it to this:

<?= snippet('twitter/tweets', ['cachename' => 'twittercache', 'media' => true, 'params' => ['screen_name' => 'mynameontwitter', 'tweet_mode' => 'extended', 'count' => 20, 'exclude_replies' => true]])?>

but then it throws me this error message:

TypeError thrown with message "Argument 2 passed to HashAndSalt\Twitter\Twitter::twitterCache() must be an instance of HashAndSalt\Twitter\mixed, array given, called in /var/www/virtual/user/dev.domain.tld/site/plugins/kirby3-twitter/src/classes/Twitter.php on line 90"

Stacktrace:

28 TypeError in /var/www/virtual/user/dev.domain.tld/site/plugins/kirby3-twitter/src/classes/Twitter.php:163

27 HashAndSalt\Twitter\Twitter:twitterCache in /var/www/virtual/user/dev.domain.tld/site/plugins/kirby3-twitter/src/classes/Twitter.php:90

26 HashAndSalt\Twitter\Twitter:tweet in /var/www/virtual/user/dev.domain.tld/site/plugins/kirby3-twitter/index.php:23

25 Kirby\Cms\Page:{closure} in [internal]:0

24 Closure:call in /var/www/virtual/user/dev.domain.tld/kirby/src/Cms/HasMethods.php:43

23 Kirby\Cms\Page:callMethod in /var/www/virtual/user/dev.domain.tld/kirby/src/Cms/Page.php:171

22 Kirby\Cms\Page:__call in /var/www/virtual/user/dev.domain.tld/site/plugins/kirby3-twitter/snippets/tweets.php:3

...

image

HashandSalt commented 2 years ago

@MaluNoPeleke Oh hrm odd, works fine for me locally. Let me take a look.

MaluNoPeleke commented 2 years ago

Okay, let me know if you need anything else for debugging it.

MaluNoPeleke commented 2 years ago

Did you already have the chance to take a look at it? If there is anything to support you with solving it you can contact me.

HashandSalt commented 2 years ago

@MaluNoPeleke Running the plugin on php 8 works fine. are you able to switch up at all?

MaluNoPeleke commented 2 years ago

This indeed solved the problem, thanks. Would be great if you could mention the requirement of PHP 8.0. Nothing else so far had this as a requirement.

HashandSalt commented 2 years ago

@MaluNoPeleke there is a note at the top of the readme to says it was tested on PHP 8 and kirby 3.6. That has been there since the new version was released.