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

Commit

Permalink
First completion of the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
aldolat committed Jun 1, 2020
1 parent c705a6c commit 4d0d7c5
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 36 deletions.
10 changes: 10 additions & 0 deletions TwitterOAuth/Exception/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Aldolat Twitter plugin index.php file.
*
* This file is intended to be empty.
*
* @package AldolatTwitter
*/

/* Silence is golden */
10 changes: 10 additions & 0 deletions TwitterOAuth/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Aldolat Twitter plugin index.php file.
*
* This file is intended to be empty.
*
* @package AldolatTwitter
*/

/* Silence is golden */
4 changes: 4 additions & 0 deletions aldolat-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* TODO: Change the caching so that the user can use more than one widget.
*/

/**
* Prevent direct access to this file.
*
Expand Down
8 changes: 5 additions & 3 deletions includes/aldolat-twitter-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
*/
function aldolat_twitter_get_defaults() {
$defaults = array(
'title' => esc_html__( 'My tweets', 'pinboard-bookmarks' ),
'title' => esc_html__( 'My latest tweets', 'pinboard-bookmarks' ),
'intro_text' => '',
'screen_name' => '',
'count' => 3,
'exclude_replies' => true,
'exclude_replies' => false,
'include_rts' => true,
'cache_duration' => 5, // In minutes.
'consumer_key' => '',
'consumer_secret' => '',
'oauth_token' => '',
Expand Down Expand Up @@ -62,7 +64,7 @@ function aldolat_twitter_get_tweets( $args ) {
if ( false === $feed ) {
$twitter_getter = new Aldolat_Twitter( $args );
$html = $twitter_getter->fetch();
set_transient( 'aldolat-twitter-tweets', $html, 5 * MINUTE_IN_SECONDS );
set_transient( 'aldolat-twitter-tweets', $html, $args['cache_duration'] * MINUTE_IN_SECONDS );
} else {
$html = $feed;
}
Expand Down
67 changes: 47 additions & 20 deletions includes/class-aldolat-twitter-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,21 @@ public function widget( $args, $instance ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}

// The introductory text.
if ( $instance['intro_text'] ) {
echo '<p class="aldolat-twitter-intro-text">' . wp_kses_post( $instance['intro_text'] ) . '</p>';
}

$params = array(
'screen_name' => $instance['screen_name'],
'count' => $instance['count'],
'exclude_replies' => $instance['exclude_replies'],
'include_rts' => $instance['include_rts'],
'cache_duration' => $instance['cache_duration'],
'consumer_key' => $instance['consumer_key'],
'consumer_secret' => $instance['consumer_secret'],
'oauth_token' => $instance['oauth_token'],
'oauth_token_secret' => $instance['oauth_token_secret'],
'screen_name' => $instance['screen_name'],
'count' => $instance['count'],
'exclude_replies' => $instance['exclude_replies'],
);
aldolat_twitter_tweets( $params );

Expand Down Expand Up @@ -109,8 +116,21 @@ public function update( $new_instance, $old_instance ) {
if ( 0 === $instance['count'] || '' === $instance['count'] || ! is_numeric( $instance['count'] ) ) {
$instance['count'] = 3;
}
if ( 200 < $instance['count'] ) {
$instance['count'] = 200;
}

$instance['exclude_replies'] = isset( $new_instance['exclude_replies'] ) ? true : false;
$instance['include_rts'] = isset( $new_instance['include_rts'] ) ? true : false;

$instance['cache_duration'] = absint( sanitize_text_field( $new_instance['cache_duration'] ) );
if ( 0 === $instance['cache_duration'] || '' === $instance['cache_duration'] || ! is_numeric( $instance['cache_duration'] ) ) {
$instance['cache_duration'] = 5;
}
if ( 5 > $instance['cache_duration'] ) {
$instance['cache_duration'] = 5;
}

$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 All @@ -135,27 +155,16 @@ public function form( $instance ) {

<div class="aldolat-twitter-widget-content">

<h4><?php esc_html_e( 'Introduction', 'aldolat-twitter' ); ?></h4>

<p>
<?php
esc_html_e(
'This widget allows you to publish your tweets in your sidebar.',
'aldolat-twitter'
);
?>
</p>

<h4><?php esc_html_e( 'Title of the widget', 'aldolat-twitter' ); ?></h4>

<?php
// Title.
pinboard_bookmarks_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'] ),
esc_html__( 'My bookmarks on Pinboard', 'aldolat-twitter' )
esc_html__( 'My latest tweets', 'aldolat-twitter' )
);
?>

Expand All @@ -168,13 +177,13 @@ public function form( $instance ) {
$this->get_field_id( 'intro_text' ),
$this->get_field_name( 'intro_text' ),
$instance['intro_text'],
esc_html__( 'These are my bookmarks on Pinboard about Italian recipes.', 'aldolat-twitter' ),
esc_html__( 'These are my latest tweets. Follow me on Twitter!', 'aldolat-twitter' ),
esc_html__( 'You can use some HTML, as you would do when writing a post.', 'aldolat-twitter' ),
$style = 'resize: vertical; height: 80px;'
);
?>

<h4><?php esc_html_e( 'Basic Setup', 'aldolat-twitter' ); ?></h4>
<h4><?php esc_html_e( 'Setup', 'aldolat-twitter' ); ?></h4>

<?php
// Username.
Expand All @@ -196,13 +205,31 @@ public function form( $instance ) {
'3'
);

// Random order.
// Exclude replies.
pinboard_bookmarks_form_checkbox(
esc_html__( 'Esclude replies', 'aldolat-twitter' ),
esc_html__( 'Exclude replies', 'aldolat-twitter' ),
$this->get_field_id( 'exclude_replies' ),
$this->get_field_name( 'exclude_replies' ),
$instance['exclude_replies']
);

// Include retweets.
pinboard_bookmarks_form_checkbox(
esc_html__( 'Include retweets', 'aldolat-twitter' ),
$this->get_field_id( 'include_rts' ),
$this->get_field_name( 'include_rts' ),
$instance['include_rts']
);

// Cache.
pinboard_bookmarks_form_input_text(
esc_html__( 'Cache duration:', 'aldolat-twitter' ),
$this->get_field_id( 'cache_duration' ),
$this->get_field_name( 'cache_duration' ),
esc_attr( $instance['cache_duration'] ),
'5',
esc_html__( 'In minutes. The minimum accepted value is 5.', 'aldolat-twitter' )
);
?>

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

/**
* Constructon method.
Expand All @@ -40,8 +41,9 @@ public function __construct( $args ) {
'oauth_token' => '',
'oauth_token_secret' => '',
'screen_name' => '',
'count' => '',
'exclude_replies' => '',
'count' => 5,
'exclude_replies' => false,
'include_rts' => true,
);
wp_parse_args( $args, $defaults );

Expand All @@ -53,10 +55,12 @@ public function __construct( $args ) {
'output_format' => 'text',
);

$this->connection = new TwitterOAuth( $settings );
$this->connection = new TwitterOAuth( $settings );

$this->screen_name = $args['screen_name'];
$this->count = $args['count'];
$this->exclude_replies = $args['exclude_replies'];
$this->include_rts = $args['include_rts'];
}

private function relative_time( $t ) {
Expand All @@ -65,32 +69,32 @@ private function relative_time( $t ) {
}

private function format( $tweet ) {
$tweet_text = $tweet->text;
$tweet_text = $tweet->full_text;
$tweet_entities = array();

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

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

foreach ( $tweet->entities->hashtags as $tag ) {
$string = substr( $tweet_text, $tag->indices[0], ( $tag->indices[1] - $tag->indices[0] ) );
$string = mb_substr( $tweet_text, $tag->indices[0], ( $tag->indices[1] - $tag->indices[0] ) );
$tweet_entities[] = array(
'type' => 'hashtag',
'curText' => 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>'
'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>',
);
}

Expand All @@ -106,6 +110,8 @@ public function fetch() {
'screen_name' => $this->screen_name,
'count' => $this->count,
'exclude_replies' => $this->exclude_replies,
'include_rts' => $this->include_rts,
'tweet_mode' => 'extended',
);

$html = '<div id="twitter-feed">';
Expand All @@ -115,7 +121,9 @@ public function fetch() {

foreach ( $tweets as $tweet ) {
$html .= '<div class="tweet">';
$html .= '<time class="tweet-date">' . $this->relative_time( $tweet->created_at ) . ' ' . esc_html__( 'ago', 'aldolat-twitter' ) . '</time>';
$html .= '<a rel="external noreferrer nofollow noopener" target="_blank" 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>';
$html .= '</div>';
}
Expand All @@ -124,4 +132,22 @@ public function fetch() {

return $html;
}

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 a week old.
if ( WEEK_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;
}
}
10 changes: 10 additions & 0 deletions includes/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Aldolat Twitter plugin index.php file.
*
* This file is intended to be empty.
*
* @package AldolatTwitter
*/

/* Silence is golden */
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Aldolat Twotter plugin index.php file.
* Aldolat Twitter plugin index.php file.
*
* This file is intended to be empty.
*
Expand Down
Binary file added languages/aldolat-twitter-it_IT.mo
Binary file not shown.
Loading

0 comments on commit 4d0d7c5

Please sign in to comment.