Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
aldolat committed Jun 13, 2020
2 parents 721ccab + d0ba9ff commit b519bb2
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 302 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.0.4] - 2020-06-13
### Added
* Added tweet author.
* Refactored the plugin.

## [0.0.3] - 2020-06-02
### Added
* Uninstall removes all the transients now.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
**Tags** twitter, sidebar, widget
**Requires at least** 5.4
**Tested up to** 5.4
**Stable tag** 0.0.3
**Stable tag** 0.0.4
**License** GPLv3 or later
**License URI** https://www.gnu.org/licenses/gpl-3.0.html

Expand Down
57 changes: 8 additions & 49 deletions aldolat-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Plugin URI: https://dev.aldolat.it/projects/aldolat-twitter/
* Author: Aldo Latino
* Author URI: https://www.aldolat.it/
* Version: 0.0.3
* Version: 0.0.4
* License: GPLv3 or later
* Text Domain: aldolat-twitter
* Domain Path: /languages/
Expand All @@ -42,6 +42,7 @@
*/

/**
* TODO: Add option to get user timeline or favorites (see lines 188 and 190 in `class-aldolat-twitter.php` ).
* TODO: Add option for getting tweets older or newer than a certain tweet.
* See: https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline
* TODO: Add option for displaying date and time.
Expand All @@ -56,56 +57,14 @@
exit( 'No script kiddies please!' );
}

/**
* Launch Aldolat Twitter.
*
* @since 0.0.1
*/
add_action( 'plugins_loaded', 'aldolat_twitter_setup' );

/**
* Setup the plugin and fire the necessary files.
*
* @since 0.0.1
*/
function aldolat_twitter_setup() {
/*
* Define the version of the plugin.
*/
define( 'ALDOLAT_TWITTER_PLUGIN_VERSION', '0.0.3' );

/*
* Load the translation.
*
* @since 0.0.1
*/
load_plugin_textdomain( 'aldolat-twitter', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

/*
* Include all necessary PHP files.
*
* @since 0.0.1
*/
// Load the TwitterOAuth library.
require_once plugin_dir_path( __FILE__ ) . 'TwitterOAuth/TwitterOAuth.php';
require_once plugin_dir_path( __FILE__ ) . 'TwitterOAuth/Exception/TwitterException.php';
// Load the class for Twitter.
require_once plugin_dir_path( __FILE__ ) . 'includes/class-aldolat-twitter.php';
// Load the init functions.
require_once plugin_dir_path( __FILE__ ) . 'includes/aldolat-twitter-functions.php';
// Load the widget's form functions.
require_once plugin_dir_path( __FILE__ ) . 'includes/aldolat-twitter-widget-form-functions.php';
// Load the widget's PHP file.
require_once plugin_dir_path( __FILE__ ) . 'includes/class-aldolat-twitter-widget.php';

/*
* Load Aldolat Twitter's widgets.
*
* @since 0.0.1
*/
add_action( 'widgets_init', 'aldolat_twitter_load_widget' );
function aldolat_twitter_run() {
require_once 'includes/class-aldolat-twitter.php';
$aldolat_twitter = new Aldolat_Twitter();
$aldolat_twitter->init();
}

aldolat_twitter_run();

/*
* CODE IS POETRY
*/
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
== Changelog ==

= 0.0.4 =

* Added tweet author.
* Refactored the plugin.

= 0.0.3 =

* Uninstall removes all the transients now.
Expand Down
52 changes: 1 addition & 51 deletions includes/aldolat-twitter-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

/**
* Returns the default options.
* Return the default options.
*
* $defaults contains the default parameters:
* string $title The title of the widget.
Expand Down Expand Up @@ -56,53 +56,3 @@ function aldolat_twitter_get_defaults() {

return $defaults;
}

/**
* Register the widget.
*
* @since 0.0.1
*/
function aldolat_twitter_load_widget() {
register_widget( 'Aldolat_Twitter_Widget' );
}

/**
* The main function that gets the tweets.
*
* @param array $args Various options to get tweets.
* This function is fired by Aldolat_Twitter_Widget class.
*
* @return string $html The HTML containing the tweets.
* @since 0.0.1
*/
function aldolat_twitter_get_tweets( $args ) {
$html = '';

/*
* Remove any non-digit from the widget ID.
* For example: 'aldolat_twitter_widget-2' becomes '2'.
*/
$widget_id = preg_replace( '/\D/', '', $args['widget_id'] );

$feed = get_transient( 'aldolat-twitter-tweets-' . $widget_id );

if ( false === $feed ) {
$twitter_getter = new Aldolat_Twitter( $args );
$html = $twitter_getter->fetch();
set_transient( 'aldolat-twitter-tweets-' . $widget_id, $html, $args['cache_duration'] * MINUTE_IN_SECONDS );
} else {
$html = $feed;
}

return $html;
}

/**
* An helper function to echo the HTML containing the tweets.
*
* @uses aldolat_twitter_get_tweets().
* @since 0.0.1
*/
function aldolat_twitter_tweets( $args ) {
echo aldolat_twitter_get_tweets( $args );
}
202 changes: 202 additions & 0 deletions includes/class-aldolat-twitter-core.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
/**
* The plugin core class.
*
* @since 0.0.1
* @package AldolatTwitter
*/

use TwitterOAuth\TwitterOAuth;

/**
* Main Aldolat Twitter class.
*
* @link https://gabrieleromanato.com/2018/06/wordpress-creare-un-plugin-per-reperire-i-dati-da-twitter
* @since 0.0.1
*/
class Aldolat_Twitter_Core {
/**
* The property that contains an instance of TwitterOauth.
*
* @var object $connection
* @access private
* @since 0.0.1
*/
private $connection;

/**
* The array with all plugin settings.
*
* @var array $plugin_settings
* @access private
* @since 0.0.4
*/
private $plugin_settings = array();

/**
* Constructon method.
*
* @since 0.0.1
*/
public function __construct( $args ) {
$defaults = aldolat_twitter_get_defaults();
wp_parse_args( $args, $defaults );

$this->plugin_settings = $args;

$this->connection = new TwitterOAuth(
array(
'consumer_key' => $this->plugin_settings['consumer_key'],
'consumer_secret' => $this->plugin_settings['consumer_secret'],
'oauth_token' => $this->plugin_settings['oauth_token'],
'oauth_token_secret' => $this->plugin_settings['oauth_token_secret'],
'output_format' => 'text',
)
);
}

/**
* Fetch the tweets from Twitter.
*
* @return string $html The final HTML with tweets.
* @since 0.0.1
*/
public function fetch() {
$params = array(
'screen_name' => $this->plugin_settings['screen_name'],
'count' => $this->plugin_settings['count'],
'exclude_replies' => $this->plugin_settings['exclude_replies'],
'include_rts' => $this->plugin_settings['include_rts'],
'tweet_mode' => 'extended',
);

$html = '<div id="twitter-feed">';

$widget_id = preg_replace( '/\D/', '', $this->plugin_settings['widget_id'] );

$feed = get_transient( 'aldolat-twitter-tweets-' . $widget_id );

if ( false === $feed ) {
// Grab user timeline.
$resp = $this->connection->get( 'statuses/user_timeline', $params );
// Grab the favorite tweets.
//$resp = $this->connection->get( 'favorites/list', $params );
$tweets = json_decode( $resp );
set_transient( 'aldolat-twitter-tweets-' . $widget_id, $tweets, $this->plugin_settings['cache_duration'] * MINUTE_IN_SECONDS );
} else {
$tweets = $feed;
}

if ( $this->plugin_settings['new_tab'] ) {
$new_tab_text = 'rel="external noreferrer nofollow noopener" target="_blank" ';
} else {
$new_tab_text = '';
}

foreach ( $tweets as $tweet ) {
$html .= '<div class="tweet">';
$html .= '<a ' . $new_tab_text . 'href="https://twitter.com/' . $this->plugin_settings['screen_name'] . '/status/' . $tweet->id_str . '">';
$html .= '<time class="tweet-date">' . $this->get_tweet_time( $tweet->created_at ) . '</time>';
$html .= '</a>';
$html .= '<span class="tweet-author">';
$html .= ' ' . esc_html__( 'by', 'aldolat-twitter' ) . ' <a ' . $new_tab_text . 'href="https://twitter.com/' . $this->plugin_settings['screen_name'] . '">' . $this->plugin_settings['screen_name'] . '</a>';
$html .= '</span>';
$html .= '<div class="tweet-body">' . $this->format( $tweet ) . '</div>';
$html .= '</div>';
}

$html .= '</div>';

return $html;
}

/**
* Returns the difference in seconds between the tweet time and now.
*
* @param integer $t The formatted datetime of the tweet.
* @return integer The difference in seconds
* @since 0.0.1
*/
private function relative_time( $t ) {
$new_tweet_time = strtotime( $t );
return human_time_diff( $new_tweet_time, time() );
}

/**
* Format the tweet adding HTML links to URL, mentions, and hashtags.
*
* @param object $tweet The object containing the tweet.
* @return string $tweet_text The resulting tweet with HTML.
* @since 0.0.1
*/
private function format( $tweet ) {
$tweet_text = $tweet->full_text;
$tweet_entities = array();

if ( $this->plugin_settings['new_tab'] ) {
$new_tab_text = 'rel="external noreferrer nofollow noopener" target="_blank" ';
} else {
$new_tab_text = '';
}

foreach ( $tweet->entities->urls as $url ) {
$tweet_entities[] = array(
'type' => 'url',
'curText' => mb_substr( $tweet_text, $url->indices[0], ( $url->indices[1] - $url->indices[0] ) ),
'newText' => '<a ' . $new_tab_text . 'href="' . $url->expanded_url . '">' . $url->display_url . '</a>',
);
}

foreach ( $tweet->entities->user_mentions as $mention ) {
$string = mb_substr( $tweet_text, $mention->indices[0], ( $mention->indices[1] - $mention->indices[0] ) );
$tweet_entities[] = array(
'type' => 'mention',
'curText' => mb_substr( $tweet_text, $mention->indices[0], ( $mention->indices[1] - $mention->indices[0] ) ),
'newText' => '<a ' . $new_tab_text . 'href="https://twitter.com/' . $mention->screen_name . '">' . $string . '</a>',
);
}

foreach ( $tweet->entities->hashtags as $tag ) {
$string = mb_substr( $tweet_text, $tag->indices[0], ( $tag->indices[1] - $tag->indices[0] ) );
$tweet_entities[] = array(
'type' => 'hashtag',
'curText' => mb_substr( $tweet_text, $tag->indices[0], ( $tag->indices[1] - $tag->indices[0] ) ),
'newText' => '<a ' . $new_tab_text . 'href="https://twitter.com/hashtag/' . $tag->text . '">' . $string . '</a>',
);
}

foreach ( $tweet_entities as $entity ) {
$tweet_text = str_replace( $entity['curText'], $entity['newText'], $tweet_text );
}

return $tweet_text;
}

/**
* Returns the datetime of the tweet using '... ago' form if the tweet is
* not older than a day.
*
* The function respects the local offset time and the option defined by the
* user about the formatting of date and time in the WordPress dashboard.
*
* @param string $tweet_time The formatted time of the tweet.
* @return string $time The datetime of the tweet or the '... ago' form.
*/
private function get_tweet_time( $tweet_time ) {
// Get the local GMT offset and date/time formats.
$local_offset = (int) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
$datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );

// Convert tweet time into UNIX timestamp and add local offset.
$unix_tweet_time = strtotime( $tweet_time ) + $local_offset;

// The tweet date/time is returned in the "... ago" form if the tweet is up to a day old.
if ( DAY_IN_SECONDS < ( time() - $unix_tweet_time ) ) {
$time = gmdate( $datetime_format, $unix_tweet_time );
} else {
$time = $this->relative_time( $tweet_time ) . ' ' . esc_html__( 'ago', 'aldolat-twitter' );
}

return $time;
}
}
5 changes: 3 additions & 2 deletions includes/class-aldolat-twitter-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

/**
* Creates the widget and display it.
* Create the widget and display it.
*
* @since 0.0.1
*/
Expand Down Expand Up @@ -90,7 +90,8 @@ public function widget( $args, $instance ) {
'oauth_token_secret' => $instance['oauth_token_secret'],
'widget_id' => $args['widget_id'],
);
aldolat_twitter_tweets( $params );
$aldolat_tweets = new Aldolat_Twitter_Core( $params );
echo $aldolat_tweets->fetch();

echo $args['after_widget'];

Expand Down
Loading

0 comments on commit b519bb2

Please sign in to comment.