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 2, 2020
2 parents af7c7f6 + 84ddf55 commit 721ccab
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 93 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## 0.0.2 - 2020-06-01
## [0.0.3] - 2020-06-02
### Added
* Uninstall removes all the transients now.
* Improved the cache management, so that we can create multiple cached widgets.
* Added option for opening links in a new tab

## [0.0.2] - 2020-06-01
### Added
* First completion of the plugin.

Expand All @@ -16,4 +22,5 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* First release of the plugin.

[Unreleased]: https://github.com/aldolat/aldolat-twitter/commits/develop
[0.0.3]: https://github.com/aldolat/aldolat-twitter/compare/0.0.2...0.0.3
[0.0.2]: https://github.com/aldolat/aldolat-twitter/compare/0.0.1...0.0.2
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.2
**Stable tag** 0.0.3
**License** GPLv3 or later
**License URI** https://www.gnu.org/licenses/gpl-3.0.html

Expand Down
8 changes: 5 additions & 3 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.2
* Version: 0.0.3
* License: GPLv3 or later
* Text Domain: aldolat-twitter
* Domain Path: /languages/
Expand All @@ -42,7 +42,9 @@
*/

/**
* TODO: Change the caching so that the user can use more than one widget.
* 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 Down Expand Up @@ -70,7 +72,7 @@ function aldolat_twitter_setup() {
/*
* Define the version of the plugin.
*/
define( 'ALDOLAT_TWITTER_PLUGIN_VERSION', '0.0.2' );
define( 'ALDOLAT_TWITTER_PLUGIN_VERSION', '0.0.3' );

/*
* Load the translation.
Expand Down
6 changes: 6 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
== Changelog ==

= 0.0.3 =

* Uninstall removes all the transients now.
* Improved the cache management, so that we can create multiple cached widgets.
* Added option for opening links in a new tab

= 0.0.2 =

* First completion of the plugin.
Expand Down
45 changes: 38 additions & 7 deletions includes/aldolat-twitter-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@
* Returns the default options.
*
* $defaults contains the default parameters:
* string $title The title of the widget.
* string $intro_text The introductory text for the widget.
* string $username The username on Twitter.
* string $widget_id The ID of the widget.
* string $title The title of the widget.
* string $intro_text The introductory text for the widget.
* string $screen_name The username on Twitter.
* string $count The number of tweets to retrieve.
* boolean $exclude_replies Whether to esclude replies.
* boolean $include_rts Whether to include retweets.
* integer $cache_duration The duration of the cache.
* boolean $new_tab Whether the links should be opened in a new tab.
* string $consumer_key The Consumer Key of the Twitter app.
* string $consumer_secret The Consumer Secret of the Twitter app.
* string $oauth_token The Oauth Token of the Twitter app.
* string $oauth_token_secret The Oauth Token Secret of the Twitter app.
* string $widget_id The ID of the widget.
* }
*
* @since 0.0.1
Expand All @@ -33,10 +42,11 @@ function aldolat_twitter_get_defaults() {
'title' => esc_html__( 'My latest tweets', 'aldolat-twitter' ),
'intro_text' => '',
'screen_name' => '',
'count' => 3,
'count' => 5,
'exclude_replies' => false,
'include_rts' => true,
'cache_duration' => 5, // In minutes.
'new_tab' => false,
'consumer_key' => '',
'consumer_secret' => '',
'oauth_token' => '',
Expand All @@ -56,22 +66,43 @@ 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 = '';

$feed = get_transient( 'aldolat-twitter-tweets' );
/*
* 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', $html, $args['cache_duration'] * MINUTE_IN_SECONDS );
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 );
}
14 changes: 7 additions & 7 deletions includes/aldolat-twitter-widget-form-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/**
* This file contains the functions used in the widget's forms
*
* @since 1.0
* @since 0.0.1
* @package AldolatTwitter
*/

/**
* Prevent direct access to this file.
*
* @since 2.0
* @since 0.0.1
*/
if ( ! defined( 'WPINC' ) ) {
exit( 'No script kiddies please!' );
Expand All @@ -18,7 +18,7 @@
/**
* Create a form label to be used in the widget panel.
*
* @since 1.12
* @since 0.0.1
* @param string $label The label to display.
* @param string $id The id of the label.
*/
Expand All @@ -29,7 +29,7 @@ function aldolat_twitter_form_label( $label, $id ) {
/**
* Create a form text input to be used in the widget panel.
*
* @since 1.12
* @since 0.0.1
* @param string $label The label to display.
* @param string $id The id of the label.
* @param string $name The name of the input form.
Expand Down Expand Up @@ -77,7 +77,7 @@ class="<?php echo esc_attr( $class ); ?>" />
* @param string $placeholder The HTML placeholder for the input form.
* @param string $comment An optional comment to display. It is displayed below the textarea form.
* @param string $style An optional inline style.
* @since 1.12
* @since 0.0.1
*/
function aldolat_twitter_form_textarea( $label, $id, $name, $text, $placeholder = '', $comment = '', $style = '' ) {
echo '<p>';
Expand Down Expand Up @@ -108,7 +108,7 @@ class="widefat"
* @param string $checked If the option is checked.
* @param string $comment An optional comment to display. It is displayed below the checkbox form.
* @param string $class An optional CSS class.
* @since 1.12
* @since 0.0.1
*/
function aldolat_twitter_form_checkbox( $label, $id, $name, $checked, $comment = '', $class = '' ) {
$class = rtrim( 'checkbox aldolat-twitter-checkbox ' . $class );
Expand Down Expand Up @@ -136,7 +136,7 @@ function aldolat_twitter_form_checkbox( $label, $id, $name, $checked, $comment =
* @param string $value The values of the select form.
* @param string $comment An optional comment to display. It is displayed below the select form.
* @param string $class The custom class for the select element.
* @since 1.12
* @since 0.0.1
*/
function aldolat_twitter_form_select( $label, $id, $name, $options, $value, $comment = '', $class = '' ) {
$class = rtrim( 'aldolat-twitter-select ' . $class );
Expand Down
15 changes: 13 additions & 2 deletions includes/class-aldolat-twitter-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public function widget( $args, $instance ) {
'exclude_replies' => $instance['exclude_replies'],
'include_rts' => $instance['include_rts'],
'cache_duration' => $instance['cache_duration'],
'new_tab' => $instance['new_tab'],
'consumer_key' => $instance['consumer_key'],
'consumer_secret' => $instance['consumer_secret'],
'oauth_token' => $instance['oauth_token'],
'oauth_token_secret' => $instance['oauth_token_secret'],
'widget_id' => $args['widget_id'],
);
aldolat_twitter_tweets( $params );

Expand Down Expand Up @@ -131,6 +133,7 @@ public function update( $new_instance, $old_instance ) {
$instance['cache_duration'] = 5;
}

$instance['new_tab'] = isset( $new_instance['new_tab'] ) ? true : false;
$instance['consumer_key'] = sanitize_text_field( $new_instance['consumer_key'] );
$instance['consumer_secret'] = sanitize_text_field( $new_instance['consumer_secret'] );
$instance['oauth_token'] = sanitize_text_field( $new_instance['oauth_token'] );
Expand Down Expand Up @@ -160,7 +163,7 @@ public function form( $instance ) {
<?php
// Title.
aldolat_twitter_form_input_text(
esc_html__( 'Title', 'aldolat-twitter' ),
esc_html__( 'Title:', 'aldolat-twitter' ),
$this->get_field_id( 'title' ),
$this->get_field_name( 'title' ),
esc_attr( $instance['title'] ),
Expand All @@ -173,7 +176,7 @@ public function form( $instance ) {
<?php
// Introductory text.
aldolat_twitter_form_textarea(
esc_html__( 'Place this text after the title', 'aldolat-twitter' ),
esc_html__( 'Place this text after the title:', 'aldolat-twitter' ),
$this->get_field_id( 'intro_text' ),
$this->get_field_name( 'intro_text' ),
$instance['intro_text'],
Expand Down Expand Up @@ -230,6 +233,14 @@ public function form( $instance ) {
'5',
esc_html__( 'In minutes. The minimum accepted value is 5.', 'aldolat-twitter' )
);

// New tab for links.
aldolat_twitter_form_checkbox(
esc_html__( 'Open links in a new browser tab', 'aldolat-twitter' ),
$this->get_field_id( 'new_tab' ),
$this->get_field_name( 'new_tab' ),
$instance['new_tab']
);
?>

<h4><?php esc_html_e( 'Twitter authentication', 'aldolat-twitter' ); ?></h4>
Expand Down
31 changes: 23 additions & 8 deletions includes/class-aldolat-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Aldolat_Twitter {
private $count;
private $exclude_replies;
private $include_rts;
private $new_tab;

/**
* Constructon method.
Expand All @@ -36,14 +37,15 @@ class Aldolat_Twitter {
*/
public function __construct( $args ) {
$defaults = array(
'consumer_key' => '',
'consumer_secret' => '',
'oauth_token' => '',
'oauth_token_secret' => '',
'screen_name' => '',
'count' => 5,
'exclude_replies' => false,
'include_rts' => true,
'new_tab' => false,
'consumer_key' => '',
'consumer_secret' => '',
'oauth_token' => '',
'oauth_token_secret' => '',
);
wp_parse_args( $args, $defaults );

Expand All @@ -61,6 +63,7 @@ public function __construct( $args ) {
$this->count = $args['count'];
$this->exclude_replies = $args['exclude_replies'];
$this->include_rts = $args['include_rts'];
$this->new_tab = $args['new_tab'];
}

private function relative_time( $t ) {
Expand All @@ -72,11 +75,17 @@ private function format( $tweet ) {
$tweet_text = $tweet->full_text;
$tweet_entities = array();

if ( $this->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 href="' . $url->expanded_url . '">' . $url->display_url . '</a>',
'newText' => '<a ' . $new_tab_text . 'href="' . $url->expanded_url . '">' . $url->display_url . '</a>',
);
}

Expand All @@ -85,7 +94,7 @@ private function format( $tweet ) {
$tweet_entities[] = array(
'type' => 'mention',
'curText' => mb_substr( $tweet_text, $mention->indices[0], ( $mention->indices[1] - $mention->indices[0] ) ),
'newText' => '<a href="https://twitter.com/' . $mention->screen_name . '">' . $string . '</a>',
'newText' => '<a ' . $new_tab_text . 'href="https://twitter.com/' . $mention->screen_name . '">' . $string . '</a>',
);
}

Expand All @@ -94,7 +103,7 @@ private function format( $tweet ) {
$tweet_entities[] = array(
'type' => 'hashtag',
'curText' => mb_substr( $tweet_text, $tag->indices[0], ( $tag->indices[1] - $tag->indices[0] ) ),
'newText' => '<a href="https://twitter.com/search?q=%23' . $tag->text . '&amp;src=hash">' . $string . '</a>',
'newText' => '<a ' . $new_tab_text . 'href="https://twitter.com/hashtag/' . $tag->text . '">' . $string . '</a>',
);
}

Expand All @@ -119,9 +128,15 @@ public function fetch() {
$resp = $this->connection->get( 'statuses/user_timeline', $params );
$tweets = json_decode( $resp );

if ( $this->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 rel="external noreferrer nofollow noopener" target="_blank" href="https://twitter.com/' . $this->screen_name . '/status/' . $tweet->id_str . '">';
$html .= '<a ' . $new_tab_text . 'href="https://twitter.com/' . $this->screen_name . '/status/' . $tweet->id_str . '">';
$html .= '<time class="tweet-date">' . $this->get_tweet_time( $tweet->created_at ) . '</time>';
$html .= '</a>';
$html .= '<div class="tweet-body">' . $this->format( $tweet ) . '</div>';
Expand Down
Binary file modified languages/aldolat-twitter-it_IT.mo
Binary file not shown.
Loading

0 comments on commit 721ccab

Please sign in to comment.