Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter Intergration #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/extensions/PHPemTwitterAPI/assets/tweet.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<li>
{% if retweeted_status is empty %}
<p>{{ text|striptags('<a>')|raw }}</p>
<small class="author">by <a href="https://www.twitter.com/{{ user.screen_name }}" target="_blank">{{ user.name }}</a></small>
<small class="date">{{ __("at") }} <a href="http://www.twitter.com/{{ user.screen_name }}/status/{{ id_str }}" target="_blank">{{ created_at|date("g:sa") }}</a> {{ __("on") }} {{ created_at|date("jS F Y") }}</small>
{% else %}
<p><small class="retweet"><a href="https://www.twitter.com/{{ user.screen_name }}/status/{{ id_str }}" target="_blank">{{ __("Retweet") }}</a></small>{{ retweeted_status.text|striptags('<a>')|raw }}</p>
<small class="author">by <a href="https://www.twitter.com/{{ retweeted_status.user.screen_name }}" target="_blank">{{ retweeted_status.user.name }}</a></small>
<small class="date">{{ __("at") }} <a href="http://www.twitter.com/{{ retweeted_status.user.screen_name }}/status/{{ id_str }}" target="_blank">{{ retweeted_status.created_at|date("g:sa") }}</a> {{ __("on") }} {{ retweeted_status.created_at|date("jS F Y") }}</small>
{% endif %}
</li>
10 changes: 10 additions & 0 deletions app/extensions/PHPemTwitterAPI/config.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
template:
tweet: assets/tweet.twig

auth:
consumer:
key: SomeConsumerKey
secret: SomeConsumerSecret
access:
token: SomeAccessToken
secret: SomeAccessTokenSecret
127 changes: 127 additions & 0 deletions app/extensions/PHPemTwitterAPI/extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace PHPemTwitterAPI;

require_once dirname(__FILE__) . '/vendor/tmhOAuth.php';

use Bolt\BaseExtension;

class Extension extends BaseExtension
{
/**
* @return array
*/
public function info()
{
$data = array(
'name' => "PHPemTwitterAPI",
'description' => "Twitter API integration for the PHPem website",
'author' => "Jack Bentley (Viva IT)",
'link' => "http://www.vivait.co.uk",
'version' => "1.0",
'required_bolt_version' => "1.5",
'highest_bolt_version' => "2.0",
'type' => "Twig function",
'first_releasedate' => "0000-00-00",
'latest_releasedate' => "0000-00-00",
'allow_in_user_content' => true,
);

return $data;
}

public function initialize()
{
$this->app['twig.loader.filesystem']->addPath(__DIR__);

$this->addTwigFunction('latestTweets', 'latestTweets');
}

public function latestTweets($user, $amount = 1)
{

$api = new \tmhOAuth([
'consumer_key' => $this->config['auth']['consumer']['key'],
'consumer_secret' => $this->config['auth']['consumer']['secret'],
'token' => $this->config['auth']['access']['token'],
'secret' => $this->config['auth']['access']['secret'],
//'bearer' => 'YOUR_OAUTH2_TOKEN',
'user_agent' => 'PHPem Website',
]);

$api->apponly_request([
'url' => $api->url('1.1/statuses/user_timeline'),
'without_bearer' => true,
'params' => [
'screen_name' => $user,
'count' => $amount,
]
]);

$response = json_decode($api->response['response'], true);

$output = '';

foreach($response as $tweet) {
$this->embedTweetLinks($tweet);

if(isset($tweet['retweeted_status']))
$this->embedTweetLinks($tweet['retweeted_status']);

$output .= $this->app['render']->render($this->config['template']['tweet'], $tweet);
}

return new \Twig_Markup($output, 'UTF-8');
}

private function embedTweetLinks(&$tweet) {
$tweet['text'] = htmlentities($tweet['text']);
if(isset($tweet['entities']['user_mentions'])) {
foreach(array_reverse($tweet['entities']['user_mentions']) as $entity) {
$tweet['text'] = substr_replace(
$tweet['text'],
'<a href="https://www.twitter.com/' . urlencode($entity['screen_name']) . '" target="_blank">'
. substr($tweet['text'], $entity['indices'][0], $entity['indices'][1] - $entity['indices'][0])
. '</a>',
$entity['indices'][0], $entity['indices'][1] - $entity['indices'][0]
);
}
}

if(isset($tweet['entities']['hashtags'])) {
foreach(array_reverse($tweet['entities']['hashtags']) as $entity) {
$tweet['text'] = str_replace(
'#' . $entity['text'],
'<a href="https://www.twitter.com/hashtag/' . urlencode($entity['text']) . '" target="_blank">'
. '#' . $entity['text']
. '</a>',
$tweet['text']
);
}
}

if(isset($tweet['entities']['media'])) {
foreach(array_reverse($tweet['entities']['media']) as $entity) {
$tweet['text'] = str_replace(
$entity['url'],
'<a href="' . $entity['url'] . '" target="_blank">'
. $entity['display_url']
. '</a>',
$tweet['text']
);
}
}

if(isset($tweet['entities']['urls'])) {
foreach(array_reverse($tweet['entities']['urls']) as $entity) {
$tweet['text'] = str_replace(
$entity['url'],
'<a href="' . $entity['url'] . '" target="_blank">'
. $entity['display_url']
. '</a>',
$tweet['text']
);
}
}
}
}
28 changes: 0 additions & 28 deletions theme/phpem/README.md

This file was deleted.

15 changes: 2 additions & 13 deletions theme/phpem/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,9 @@
<section class="tweets">
<h1>{{ __("Tweets") }}</h1>
<ul class="item-list">
<li>
<p>Just a day to go for the November meetup by <a href="#">@lornajane</a> on New Wave PHP <a href="#">http://www.meetup.com/ugPHPem/events/211696112/&hellip;</a> <a href="#">#Leicester</a> <a href="#">#EastMidlands</a></p>
<small class="date"><a href="#">10:54am</a> on 5th November 2014</small>
</li>
<li>
<p>On November 6th .<a href="#">@lornajane</a> will be doing a talk on New Wave PHP <a href="#">http://www.meetup.com/ugPHPem/events/211696112/&hellip;</a></p>
<small class="date"><a href="#">11:06am</a> on 16th October 2014</small>
</li>
<li>
<p>We have a new logo! Curtesy of <a href="#">@vivaitltd</a> Thanks guys :)</p>
<small class="date"><a href="#">12:16pm</a> on 15th October 2014</small>
</li>
{{ latestTweets('PHPem', 3) }}
</ul>
<a class="button" href="https://twitter.com/phpem">Follow @PHPem</a>
<a class="button" href="https://twitter.com/PHPem">Follow @PHPem</a>
</section>

</div>
Expand Down