Skip to content
Open
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
96 changes: 72 additions & 24 deletions displaytweets.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ class DisplayTweets {
* @since 1.0
*/
public static function get_instance() {

if ( !self::$instance instanceof self )
self::$instance = new self;
return self::$instance;

}

/**
* Constructor
*
Expand Down Expand Up @@ -112,7 +112,7 @@ private function __construct() {
do_action_ref_array( 'displaytweets', array( $this ) );

}

/**
* Executes a network activation
*
Expand All @@ -121,7 +121,7 @@ private function __construct() {
public static function do_network_activation() {
self::get_instance()->network_activate();
}

/**
* Executes a network uninstall
*
Expand All @@ -130,7 +130,7 @@ public static function do_network_activation() {
public static function do_network_uninstall() {
self::get_instance()->network_uninstall();
}

/**
* Executes an activation
*
Expand All @@ -139,7 +139,7 @@ public static function do_network_uninstall() {
public static function do_activation() {
self::get_instance()->activate();
}

/**
* Executes an uninstall
*
Expand All @@ -148,7 +148,7 @@ public static function do_activation() {
public static function do_uninstall() {
self::get_instance()->uninstall();
}

/**
* Network activation hook
*
Expand All @@ -174,7 +174,7 @@ public function network_activate() {
do_action_ref_array( 'displaytweets_network_activate', array( $this ) );

}

/**
* Network uninstall hook
*
Expand All @@ -196,7 +196,7 @@ public function network_uninstall() {
do_action_ref_array( 'displaytweets_network_uninstall', array( $this ) );

}

/**
* Activation hook
*
Expand All @@ -220,12 +220,13 @@ public function activate() {
'include_rts' => true,
'exclude_replies' => false
) );
add_option( 'displaytweets_twitter_handles', array( 'matthewruddycom' ) );

/** Trigger hooks */
do_action_ref_array( 'displaytweets_activate', array( $this ) );

}

/**
* Uninstall Hook
*
Expand All @@ -236,13 +237,14 @@ public function uninstall() {
/** Delete options and transients */
delete_option( 'displaytweets_version' );
delete_option( 'displaytweets_settings' );
delete_transient( 'displaytweets_tweets' );
delete_option( 'displaytweets_twitter_handles' );
$this->delete_all_transients();

/** Trigger hooks */
do_action_ref_array( 'displaytweets_uninstall', array( $this ) );

}

/**
* Does a plugin version check, making sure the current Wordpress version is supported. If not, the plugin is deactivated and an error message is displayed.
*
Expand All @@ -257,7 +259,7 @@ public function version_check() {
}
return true;
}

/**
* Returns the ids of the various multisite blogs. Returns false if not a multisite installation.
*
Expand Down Expand Up @@ -307,7 +309,7 @@ public function add_settings_page() {
*/
public function add_settings_link($links) {
array_unshift($links, '<a href="options-general.php?page=displaytweets">Settings</a>');
return $links;
return $links;
}

/**
Expand Down Expand Up @@ -347,14 +349,48 @@ public function save_settings() {
/** Save the settings */
update_option( 'displaytweets_settings', stripslashes_deep( $this->validate_settings( $_POST['settings'] ) ) );

/** Delete the old transient to force a refresh */
delete_transient( 'displaytweets_tweets' );
/** Store the Twitter handle so we can clean it up later if necessary */
$this->store_twitter_handle( $_POST['settings']['screen_name'] );

/** Delete the old transient(s) to force a refresh */
$this->delete_all_transients();

/** Display success message */
add_action( 'admin_notices', create_function( '', 'echo "<div class=\"message updated\"><p>'. __( 'Settings have been saved successfully.', 'displaytweets' ) .'</p></div>";' ) );

}


/**
* Add a Twitter handle to the displaytweets_twitter_handles option array
*
* @param str $handle
*/
public function store_twitter_handle( $handle ) {
$handles = get_option( 'displaytweets_twitter_handles' );
if ( is_array( $handles ) ) {
if ( ! in_array( $handle, $handles ) ) {
$handles[] = $handle;
update_option( 'displaytweets_twitter_handles', $handles );
}

} else {
update_option( 'displaytweets_twitter_handles', array( $handle ) );
}
}

/**
* Delete all transients saved by the plugin
*/
public function delete_all_transients() {
$usernames = get_option( 'displaytweets_twitter_handles' );
if ( is_array( $usernames ) ) {
foreach ( $usernames as $username ) {
delete_transient( $this->get_transient_name( $username ) );
}
}
delete_transient( 'displaytweets_tweets' );
}

/**
* Executes a shortcode handler
*
Expand Down Expand Up @@ -457,7 +493,7 @@ public function settings_view() {
<td>
<fieldset>
<legend class="screen-reader-text"><span><?php _e( 'Include Retweets', 'displaytweets' ); ?></span></legend>

<label for="include_rts_true"><input type="radio" name="settings[include_rts]" id="include_rts_true" value="true" <?php checked( $s['include_rts'], true ); ?>>
<span><?php _e( 'Yes', 'displaytweets' ); ?></span>
</label>
Expand All @@ -476,12 +512,12 @@ public function settings_view() {
<td>
<fieldset>
<legend class="screen-reader-text"><span><?php _e( 'Exclude Replies', 'displaytweets' ); ?></span></legend>

<label for="exclude_replies_true"><input type="radio" name="settings[exclude_replies]" id="exclude_replies_true" value="true" <?php checked( $s['exclude_replies'], true ); ?>>
<span><?php _e( 'Yes', 'displaytweets' ); ?><span>
</label>
<br />

<label for="exclude_replies_false"><input type="radio" name="settings[exclude_replies]" id="exclude_replies_false" value="false" <?php checked( $s['exclude_replies'], false ); ?>>
<span><?php _e( 'No', 'displaytweets' ); ?><span>
</label>
Expand Down Expand Up @@ -535,7 +571,8 @@ public function get() {
) );

/** Get tweets from transient. False if it has expired */
$tweets = get_transient( "displaytweets_tweets" );
$transient_name = $this->get_transient_name( $args['screen_name'] );
$tweets = get_transient( $transient_name );
if ( $tweets === false ) {

/** Require the twitter auth class */
Expand All @@ -561,7 +598,8 @@ public function get() {
return false;

/** Set tweets */
set_transient( "displaytweets_tweets", $tweets, apply_filters( 'displaytweets_refresh_timeout', self::$refresh ) );
set_transient( $transient_name, $tweets, apply_filters( 'displaytweets_refresh_timeout', self::$refresh ) );
$this->store_twitter_handle( $args['screen_name'] );

}

Expand All @@ -570,6 +608,16 @@ public function get() {

}

/**
* Generate the transient name, based on the user's Twitter handle
*
* @param str $handle The user's Twitter handle
* @return str
*/
public function get_transient_name( $handle ) {
return ( $handle ? sprintf( 'displaytweets_tweets_%s', $handle ) : 'displaytweets_tweets' );
}

/**
* Prints the tweets
*
Expand Down Expand Up @@ -657,7 +705,7 @@ public function widget( $args, $instance ) {
echo $before_widget;
if ( !empty( $title ) )
echo $before_title . $title . $after_title;

/** Display tweets */
if ( function_exists( 'display_tweets' ) )
display_tweets();
Expand Down