__( 'Display quote of the day on your website/blog automatically updated everyday!', 'themeidol-all-widget' ), ) // Args ); // Refreshing the widget's cached output with each new post add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); add_action( 'delete_attachment', array( $this, 'flush_group_cache' ) ); add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { $cache = (array) wp_cache_get( 'themeidol-site2quotes', 'widget' ); if(!is_array($cache)) $cache = array(); if(isset($cache[$args['widget_id']])){ echo $cache[$args['widget_id']]; return; } ob_start(); $qtype = apply_filters('widget_title', esc_attr($instance['qtype'])); $qdisplay = apply_filters('widget_title', esc_attr($instance['qdisplay'])); $title_hash = array( "none" => "Select Category", "any" => "All", "love" => "Love Quotes (Popular)", "inspirational" => "Inspirational Quotes (Popular)", "funny" => "Funny Quotes", "friendship" => "Friendship Quotes", "life" => "Life Quotes", "motivational" => "Motivational Quotes", "birthday" => "Birthday Quotes", "famous" => "Famous Quotes", "relationship" => "Relationship Quotes", "positive" => "Positive Quotes", "sad" => "Sad Quotes", "happiness" => "Happiness Quotes", "family" => "Family Quotes", "smile" => "Smile Quotes", "best" => "Best Quotes", "success" => "Success Quotes", "romantic" => "Romantic Quotes", "good" => "Good Quotes", "anniversary" => "Anniversary Quotes", "attitude" => "Attitude Quotes", "trust" => "Trust Quotes", ); if (strpos($args['before_widget'], 'widget ') !== false) { $before_widget = preg_replace('/widget /', "idol-widget ", $args['before_widget'], 1); } echo $before_widget; if ( ! empty( $qtype) ) $webpageurl=$_SERVER['REQUEST_URI']; $website=$_SERVER['SERVER_NAME']; $visitorip=$_SERVER['REMOTE_ADDR']; $resp = wp_remote_get('http://api.site2quotes.com/wp/quotations.aspx?category=' .$qtype. '&qdisplay=' .$qdisplay. '&visitorip=' .$visitorip. '&domain=' .$website. '&webpage=' .$webpageurl. '' ); if ( 200 == $resp['response']['code'] ) { $body = $resp['body']; echo __($body,'text_domain'); // perform action with the content. } echo $args['after_widget']; $widget_string = ob_get_flush(); $cache[$args['widget_id']] = $widget_string; wp_cache_add('themeidol-site2quotes', $cache, 'widget'); } public function flush_widget_cache() { wp_cache_delete( 'themeidol-site2quotes', 'widget' ); } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { if ( isset( $instance[ 'qtype' ] ) ) { $qtype = esc_attr($instance[ 'qtype' ]); } else { $qtype = "day"; } if ( isset( $instance[ 'qdisplay' ] ) ) { $qdisplay = esc_attr($instance[ 'qdisplay' ]); } else { $qdisplay = "image"; } ?>