As per twitter API 1.1 developer display requirements policy new version is updated. PLEASE DO NOT USE OLDER VERSIONS.
Version: 2.4
Text Domain: aitwitterfeeds
Author: August Infotech
Author URI: http://www.augustinfotech.com
*/
add_action('plugins_loaded', 'ai_tweets_init');
/*
*Make Admin Menu Item
*/
add_action('admin_menu','ai_twitter_setting');
/*
*Register Twitter Specific Options
*/
add_action('admin_init','ai_init');
add_action('wp_dashboard_setup', 'ai_add_dashboard_tweets_feed' );
/** Start Upgrade Notice **/
global $pagenow;
if ( 'plugins.php' === $pagenow ) {
// Better update message
$file = basename( __FILE__ );
$folder = basename( dirname( __FILE__ ) );
$hook = "in_plugin_update_message-{$folder}/{$file}";
add_action( $hook, 'ai_twitter_update_notification_message', 20, 2 );
}
function ai_twitter_update_notification_message( $plugin_data, $r ) {
$data = file_get_contents( 'http://plugins.trac.wordpress.org/browser/ai-twitter-feeds/trunk/readme.txt?format=txt' );
$upgradetext = stristr( $data, '== Upgrade Notice ==' );
$upgradenotice = stristr( $upgradetext, '*' );
$output = "
Update Notice : ".$upgradenotice."
";
return print $output;
}
/** End Upgrade Notice **/
# Load the language files
function ai_tweets_init() {
load_plugin_textdomain( 'aitwitterfeeds', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/*
*Setup Admin menu item
*/
function ai_twitter_setting() {
add_options_page('AI Twitter Settings','AI Twitter Settings','manage_options','ai-plugin','ai_option_page');
}
function ai_add_dashboard_tweets_feed() {
wp_add_dashboard_widget('ai_dashboard_widget', 'AI Twitter Feeds', 'ai_get_twitter_feeds');
}
/*
*Register Twitter Specific Options
*/
function ai_init(){
register_setting('ai_options','ai_consumer_screen_name');//todo - add sanitization function ", 'functionName'"
register_setting('ai_options','ai_consumer_key');
register_setting('ai_options','ai_consumer_secret');
register_setting('ai_options','ai_access_token');
register_setting('ai_options','ai_access_token_secret');
register_setting('ai_options','ai_display_number_of_tweets');
register_setting('ai_options','ai_twitter_css');
register_setting('ai_options','ai_load_more_text');
register_setting('ai_options','ai_list_grid');
}
if( function_exists('register_uninstall_hook') )
register_uninstall_hook(__FILE__,'ai_twitterfeed_uninstall');
function ai_twitterfeed_uninstall(){
delete_option('ai_consumer_screen_name');
delete_option('ai_consumer_key');
delete_option('ai_consumer_secret');
delete_option('ai_access_token');
delete_option('ai_access_token_secret');
delete_option('ai_display_number_of_tweets');
delete_option('ai_twitter_css');
delete_option('ai_load_more_text');
delete_option('ai_list_grid');
}
/*
*Display the Options form for AI Twitter Feed
*/
function ai_option_page(){ ?>
All the options along with the shortcode are optional.
','aitwitterfeeds');?>
Twitter API','aitwitterfeeds');?>
'. ((strlen( $m[1] ) >= $linkMaxLen ? substr($m[1],0,$linkMaxLen) . '...' : $m[1] )) . '';
},
$status
);
$status = preg_replace("/(@([_a-z0-9\-]+))/i","$1 ",$status);
$status = preg_replace("/\b([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})\b/i","", $status);
$status = preg_replace("/(#([_a-z0-9\-]+))/i","$1 ",$status);
return $status;
}
// parse time in a twitter style
function ai_getTime($ai_date){
$ai_timediff = time() - strtotime($ai_date);
if($ai_timediff < 60) return $ai_timediff . 's';
else if($ai_timediff < 3600) return intval(date('i', $ai_timediff)) . 'm';
else if($ai_timediff < 86400) return round($ai_timediff/60/60) . 'h';
else return date_i18n('M d', strtotime($ai_date));
}
function ai_twitter_formatter($ai_date){
$ai_epoch_timestamp = strtotime( $ai_date );
$ai_twitter_time = human_time_diff($ai_epoch_timestamp, current_time('timestamp') ) . ' ago';
return $ai_twitter_time;
}
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret){
$ai_connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $ai_connection;
}
function get_connect($ai_consumerkey_gt, $ai_consumersecret_gt, $ai_accesstoken_gt, $ai_accesstokensecret_gt,$ai_twitteruser_gt,$ai_notweets_gt){
$ai_connection = getConnectionWithAccessToken($ai_consumerkey_gt, $ai_consumersecret_gt, $ai_accesstoken_gt, $ai_accesstokensecret_gt);
$ai_tweets_all = $ai_connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$ai_twitteruser_gt."&count=".$ai_notweets_gt);
return $ai_tweets_all;
}
function get_NextTweets($ai_consumerkey_gt, $ai_consumersecret_gt, $ai_accesstoken_gt, $ai_accesstokensecret_gt,$ai_twitteruser_gt,$ai_notweets_gt,$maxid){
$ai_connection = getConnectionWithAccessToken($ai_consumerkey_gt, $ai_consumersecret_gt, $ai_accesstoken_gt, $ai_accesstokensecret_gt);
$ai_tweets_all = $ai_connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$ai_twitteruser_gt."&max_id=".$maxid."&count=".$ai_notweets_gt."&include_rts=false&exclude_replies=true");
return $ai_tweets_all;
}
/* Short code */
add_shortcode( 'AIGetTwitterFeeds' , 'ai_get_twitter_feeds' );
function ai_get_twitter_feeds($atts){
extract( shortcode_atts( array(
'ai_username' => '',
'ai_numberoftweets' => '',
'ai_tweet_title' =>'',
'ai_load_more_text' =>'',
'ai_list_grid' =>'',
'ai_short_widget' =>'shortcode',
), $atts));
$ai_get_twitteruser=$ai_username ? $ai_username : get_option('ai_consumer_screen_name');
$ai_get_notweets=$ai_numberoftweets ? $ai_numberoftweets : get_option('ai_display_number_of_tweets');
$ai_get_tweetstitle=$ai_tweet_title ? $ai_tweet_title:'Latest Twetter Feeds';
$ai_twitteruser = $ai_get_twitteruser;
$ai_notweets = $ai_get_notweets;
$ai_consumerkey = get_option('ai_consumer_key');
$ai_consumersecret = get_option('ai_consumer_secret');
$ai_accesstoken = get_option('ai_access_token');
$ai_accesstokensecret = get_option('ai_access_token_secret');
$ai_twitter_css = get_option('ai_twitter_css');
$ai_load_more_text = $ai_load_more_text ? $ai_load_more_text : get_option('ai_load_more_text');
$ai_list_grid = $ai_list_grid ? $ai_list_grid : get_option('ai_list_grid');
if($ai_twitteruser!='' && $ai_notweets !='' && $ai_consumerkey!='' && $ai_consumersecret!='' && $ai_accesstoken!='' && $ai_accesstokensecret!='') {
$ai_tweets = get_connect($ai_consumerkey, $ai_consumersecret, $ai_accesstoken, $ai_accesstokensecret,$ai_twitteruser,$ai_notweets);
wp_register_style('aitwitter', plugins_url('css/aitwitter.css', __FILE__));
wp_enqueue_style('aitwitter');
if(!empty($ai_twitter_css)) {
wp_add_inline_style( 'aitwitter', $ai_twitter_css );
}
if(is_admin()) {
$screen = get_current_screen();
if($screen->id == 'dashboard'){
$ai_wid_title="";
$ai_class="";
} else {
$ai_wid_title="";
$ai_class="aiwidgetscss widget";
}
} else {
$ai_wid_title="";
$ai_class="aiwidgetscss widget";
}
$grid_class = '';
if($ai_list_grid && $ai_short_widget == 'shortcode'){
$grid_class="ai-tweet-grid ai-grid-3";
}
$ai_output="";
} else {
$ai_output="";
}
return $ai_output;
}
/*
*AI Twitter Widget Widget
* enables the ability to use a widget to place the tweet feed in the widget areas
* of a theme.
*/
class AI_Twitter_Widget extends WP_Widget {
/* Register the widget for use in WordPress */
public function AI_Twitter_Widget(){
$this->options = array(
array(
'label' => 'AI Widget settings
',
'type' => 'separator'),
array(
'name' => 'ai_widget_title', 'label' => 'Widget title',
'type' => 'text', 'default' => 'Latest Tweets', 'tooltip' => 'Title of the widget'),
array(
'name' => 'ai_widget_username', 'label' => 'Username (Without the "@" symbol)',
'type' => 'text', 'default' => 'twitter', 'tooltip' => 'Twitter username for which you want to display tweets if widget type is set to Timeline'),
array(
'name' => 'ai_widget_count', 'label' => 'Number Of Tweets Per Page',
'type' => 'text', 'default' => '5', 'tooltip' => 'Number of Tweets to display per page'),
array(
'name' => 'ai_load_more_text', 'label' => 'Load more button text',
'type' => 'text', 'default' => 'Load More...', 'tooltip' => ''),
);
/* Widget settings. */
$widget_options = array(
'classname' => 'ai_widget',
'description' => 'AI Simple Twitter Feed Widget, Displays your latest Tweet',
);
/* Widget control settings. */
$control_ops = array('width' => 250);
//for old varsion
//parent::WP_Widget('ai_widget','AI Twiiter Feeds',$widget_options,$control_ops);
parent::__construct('ai_widget','AI Twiiter Feeds',$widget_options,$control_ops);
}
public function widget($args, $instance) {
$ai_get_widget_twitteruser=$instance['ai_widget_username'] ? $instance['ai_widget_username'] : get_option('ai_consumer_screen_name');
$ai_get_widget_notweets=$instance['ai_widget_count'] ? $instance['ai_widget_count'] : get_option('ai_display_number_of_tweets');
$title = ($instance['ai_widget_title']) ? $instance['ai_widget_title'] : 'Latest Twitter Feeds';
$ai_load_more_text = ($instance['ai_load_more_text']) ? $instance['ai_load_more_text'] : 'Load More...';
$ai_wid_twitteruser = $ai_get_widget_twitteruser;
$ai_wid_notweets = $ai_get_widget_notweets;
extract($args, EXTR_SKIP);
$atts_arr=array('ai_username' => $ai_get_widget_twitteruser,
'ai_numberoftweets' => $ai_get_widget_notweets,
'ai_tweet_title' =>$title,
'ai_load_more_text' =>$ai_load_more_text,
'ai_short_widget' =>'widget',
);
echo ai_get_twitter_feeds($atts_arr);
}
function update($new_instance, $old_instance) {
return $new_instance;
}
public function form($instance) {
if(empty($instance)) {
foreach($this->options as $val) {
if($val['type'] == 'separator') {
continue;
}
$instance[$val['name']] = $val['default'];
}
}
if(!is_callable('curl_init')) {
echo __('Your PHP doesn\'t have cURL extension enabled. Please contact your host and ask them to enable it.');
return;
}
foreach($this->options as $val) {
$title = '';
if(!empty($val['tooltip'])) {
$title = ' title="' . $val['tooltip'] . '"';
}
if($val['type'] == 'separator') {
echo $val['label'] . ' ';
} else if($val['type'] == 'text') {
$label = '' . $val['label'] . ' ';
$value = $val['default'];
if(isset($instance[$val['name']]))
$value = esc_attr($instance[$val['name']]);
echo '' . $label . ' ';
echo '
';
}
}
echo "More Settings ";
}
}
add_action('admin_enqueue_scripts', 'ai_loadjs');
function ai_loadjs() {
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('aisettings', plugins_url('/js/aisettings.js', __FILE__ ), array('jquery', 'wp-color-picker'));
}
# Add hook for front-end
function ai_custom_script() {
$ai_is_scroll = esc_attr(get_option('ai_is_scroll'));
$is_scroll = "no";
if($ai_is_scroll)
{
$is_scroll = "yes";
}
echo '';
}
//add_action( 'wp_head', 'ai_custom_script' );
add_action('wp_enqueue_scripts', 'ai_frontendloadjs');
function ai_frontendloadjs() {
wp_enqueue_script( 'ai_scripts', plugins_url( '/js/ai-twitter.js', __FILE__ ), array( 'jquery' ), CTF_VERSION, true );
wp_localize_script( 'ai_scripts', 'ai', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
)
);
}
/*
* Register the AI_Twitter_Widget widget
*/
function ai_widget_init() {
register_widget('AI_Twitter_Widget');
}
add_action('widgets_init', 'ai_widget_init');
function ai_get_more_posts() {
$ai_numberoftweets = isset( $_POST['ai_numberoftweets'] ) ? sanitize_text_field( $_POST['ai_numberoftweets'] ) : '';
$ai_get_twitteruser = isset( $_POST['ai_twitteruser'] ) ? $_POST['ai_twitteruser'] : get_option('ai_consumer_screen_name');
//$ai_get_twitteruser=$ai_username ? $ai_username : get_option('ai_consumer_screen_name');
$ai_get_notweets=$ai_numberoftweets ? $ai_numberoftweets : get_option('ai_display_number_of_tweets');
$ai_get_tweetstitle=$ai_tweet_title ? $ai_tweet_title:'Latest Twetter Feeds';
$ai_twitteruser = $ai_get_twitteruser;
$ai_notweets = $ai_get_notweets + 1;
$ai_consumerkey = get_option('ai_consumer_key');
$ai_consumersecret = get_option('ai_consumer_secret');
$ai_accesstoken = get_option('ai_access_token');
$ai_accesstokensecret = get_option('ai_access_token_secret');
$ai_twitter_css = get_option('ai_twitter_css');
$last_id_data = isset( $_POST['last_id_data'] ) ? sanitize_text_field( $_POST['last_id_data'] ) : '';
if($ai_twitteruser!='' && $ai_notweets !='' && $ai_consumerkey!='' && $ai_consumersecret!='' && $ai_accesstoken!='' && $ai_accesstokensecret!='') {
$ai_tweets = get_NextTweets($ai_consumerkey, $ai_consumersecret, $ai_accesstoken, $ai_accesstokensecret,$ai_twitteruser,$ai_notweets,$last_id_data);
$ai_output="";
#remove first
unset( $ai_tweets[0] );
for($i=1; $i<=count($ai_tweets); $i++) {
if(!empty($ai_tweets->errors)) {
$ai_output .= ''.$ai_tweets->errors[$i]->message.'
';
} else {
$ai_img_html=' ';
$ai_username_html=' ';
$ai_timestamp_html=''.ai_getTime($ai_tweets[$i]->created_at).' ';
$ai_replay_html='';
$ai_retweet_html='';
$ai_favorite_html='';
if( preg_match('/RT +@[^ :]+:?/ui', $ai_tweets[$i]->text, $retweets) ) {
$ai_tweets_text = $retweets[0].$ai_tweets[$i]->retweeted_status->text;
} else {
$ai_tweets_text = $ai_tweets[$i]->text;
}
$ai_output.='';
}
}
$ai_output.= "";
}
echo $ai_output;
die();
}
add_action( 'wp_ajax_nopriv_ai_get_more_posts', 'ai_get_more_posts' );
add_action( 'wp_ajax_ai_get_more_posts', 'ai_get_more_posts' );