From 16ea63170eedae7215cff0d72e4dda56128d24f8 Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Wed, 12 Feb 2014 16:23:27 -0500 Subject: [PATCH] Put the Twitter handle in the transient name so we can run multiple usernames; track handles that have been used by the plugin so we can clean up the handle-based transients on settings update --- displaytweets.php | 96 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 24 deletions(-) diff --git a/displaytweets.php b/displaytweets.php index 0ec1acc..60f5ae8 100644 --- a/displaytweets.php +++ b/displaytweets.php @@ -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 * @@ -112,7 +112,7 @@ private function __construct() { do_action_ref_array( 'displaytweets', array( $this ) ); } - + /** * Executes a network activation * @@ -121,7 +121,7 @@ private function __construct() { public static function do_network_activation() { self::get_instance()->network_activate(); } - + /** * Executes a network uninstall * @@ -130,7 +130,7 @@ public static function do_network_activation() { public static function do_network_uninstall() { self::get_instance()->network_uninstall(); } - + /** * Executes an activation * @@ -139,7 +139,7 @@ public static function do_network_uninstall() { public static function do_activation() { self::get_instance()->activate(); } - + /** * Executes an uninstall * @@ -148,7 +148,7 @@ public static function do_activation() { public static function do_uninstall() { self::get_instance()->uninstall(); } - + /** * Network activation hook * @@ -174,7 +174,7 @@ public function network_activate() { do_action_ref_array( 'displaytweets_network_activate', array( $this ) ); } - + /** * Network uninstall hook * @@ -196,7 +196,7 @@ public function network_uninstall() { do_action_ref_array( 'displaytweets_network_uninstall', array( $this ) ); } - + /** * Activation hook * @@ -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 * @@ -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. * @@ -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. * @@ -307,7 +309,7 @@ public function add_settings_page() { */ public function add_settings_link($links) { array_unshift($links, 'Settings'); - return $links; + return $links; } /** @@ -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 "

'. __( 'Settings have been saved successfully.', 'displaytweets' ) .'

";' ) ); } - + + /** + * 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 * @@ -457,7 +493,7 @@ public function settings_view() {
- + @@ -476,12 +512,12 @@ public function settings_view() {
- +
- + @@ -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 */ @@ -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'] ); } @@ -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 * @@ -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();