"; foreach($jsons as $json){ $json['status'] = preg_replace('~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?$0', $json['status']); $types = array("[screen_name]" =>$json['screen_name'],"[status]" =>$json['status'],"[created_at]" => date('h:i A - d M Y',$json['created_at']),"[url]" => "http://www.twitter.com/".$json['url'],"[image]" => $json['image'],"[id]" =>$json['id']); $all .= strtr($dt_atp_style['dt_atp_textarea_style'], $types); } $all .= ""; $all = $text = preg_replace('/(\#)([^\s]+)/', '#$2', $all); $all = $text = preg_replace('/(\@)([^\s]+)/', '@$2', $all); return $all; } } /* !3. FILTERS */ function dt_atp_admin_menus() { /* main menu */ $top_menu_item = 'dt_atp_dashboard_admin_page'; add_menu_page( '', 'Another Twitter', 'manage_options', 'dt_atp_dashboard_admin_page', 'dt_atp_dashboard_admin_page', 'dashicons-twitter' ); /* submenu items */ // dashboard add_submenu_page( $top_menu_item, '', 'Dashboard', 'manage_options', $top_menu_item, $top_menu_item ); // plugin settings add_submenu_page($top_menu_item, '', 'Display Style', 'manage_options', 'dt_atp_display_style_admin_page', 'dt_atp_display_style_admin_page' ); // plugin settings add_submenu_page($top_menu_item, '', 'Plugin Settings', 'manage_options', 'dt_atp_plugin_settings_admin_page', 'dt_atp_plugin_settings_admin_page' ); // twitter settings add_submenu_page($top_menu_item, '', 'Twitter Settings', 'manage_options', 'dt_atp_twitter_settings_admin_page', 'dt_atp_twitter_settings_admin_page' ); } /* !4. EXTERNAL SCRIPTS */ //4.1 add script to display settings for adding buttons to textarea function dt_atp_extra_javascript_files() { wp_enqueue_script( 'textarea', plugin_dir_url( __FILE__ ) . 'js/textarea.js'); wp_enqueue_style('loader', plugin_dir_url( __FILE__ ) . 'css/loader.css'); } /* !5. ACTIONS */ //5.1 get all new tweets function dt_atp_get_new_tweets(){ if(get_option('dt_atp_status_enabled') == 1 && count(get_option('dt_atp_textbox')) > 0){ include_once(plugin_dir_path( __FILE__ ).'twitteroauth/twitteroauth.php'); $options_twitter = dt_atp_get_current_options('twitter'); $options_settings = dt_atp_get_current_options('settings'); $currently_active = get_option('dt_atp_currently_active'); $new_currently_active = array(); $twitter_customer_key = $options_twitter['dt_atp_customer_key']; $twitter_customer_secret = $options_twitter['dt_atp_customer_secret']; $twitter_access_token = $options_twitter['dt_atp_access_token']; $twitter_access_token_secret = $options_twitter['dt_atp_access_token_secret']; $connection = new TwitterOAuth($twitter_customer_key, $twitter_customer_secret, $twitter_access_token, $twitter_access_token_secret); $json = json_decode(file_get_contents(plugin_dir_path( __FILE__ ).'twitter.json'),true); if($options_settings['dt_atp_radio'] == 'hashtags') { $tags = $options_settings['dt_atp_textbox']; $id = array(); foreach($tags as $tag){ if(!empty($tag)){ $title = $tag; $options = array(); $options['q'] = '#'.$title.' -filter:retweets'; $options['result_type'] = 'recent'; $options['count'] = $options_settings['dt_atp_number_of_tweets']; if(!empty($currently_active)){ if(array_key_exists($title,$currently_active)){ $options['since_id'] = $currently_active[$title]; } } $my_tweets = $connection->get('search/tweets', $options); $my_tweets = json_decode(json_encode($my_tweets),true); if(!empty($my_tweets['statuses'][0]['id'])){ $new_currently_active[$title] = $my_tweets['statuses'][0]['id']; foreach($my_tweets['statuses'] as $t){ if(in_array($t['id'], $id)){ continue; } else { $id[] = $t['id']; $json[] = array( 'id' => $t['id'], 'created_at' => strtotime($t['created_at']), 'url' => $t['user']['screen_name']."/status/".$t['id'], 'screen_name' => $t['user']['screen_name'], 'status' => $t['text'], 'image' => $t['user']['profile_image_url']); } } } else{ $new_currently_active[$title] = $currently_active[$title]; } } } } elseif ($options_settings['dt_atp_radio'] == 'username'){ $usernames = $options_settings['dt_atp_textbox']; foreach($usernames as $username){ if(!empty($username)){ $options = array(); $options['screen_name'] = $username; $options['count'] = $options_settings['dt_atp_number_of_tweets']; if(!empty($currently_active)){ if(array_key_exists($username,$currently_active)){ $options['since_id'] = $currently_active[$username]; } } $my_tweets = $connection->get('statuses/user_timeline', $options); $my_tweets = json_decode(json_encode($my_tweets),true); if(!empty($my_tweets[0]['id'])){ $new_currently_active[$username] = $my_tweets[0]['id']; foreach($my_tweets as $t){ $json[] = array( 'id' => $t['id'], 'created_at' => strtotime($t['created_at']), 'url' => $t['user']['screen_name']."/status/".$t['id'], 'screen_name' => $t['user']['screen_name'], 'status' => $t['text'], 'image' => $t['user']['profile_image_url']); } } else { $new_currently_active[$username] = $currently_active[$username]; } } } } else { return ; } update_option('dt_atp_currently_active',$new_currently_active); foreach ($json as $key => $row) { $mid[$key] = $row['created_at']; } array_multisort($mid, SORT_DESC, $json); $json = array_slice($json, 0, $options_settings['dt_atp_number_of_saved_tweets']); file_put_contents(plugin_dir_path( __FILE__ ).'twitter.json', json_encode($json)); update_option('dt_atp_last_update_time',time()); } $red = ($_SERVER["HTTP_REFERER"] != '' ? $_SERVER["HTTP_REFERER"] : site_url()); header("Location: " . $red); } /* !6. HELPERS */ // 6.1 // hint: get's the current options and returns values in associative array function dt_atp_get_current_options($for) { // setup our return variable $current_options = array(); if($for == 'twitter'){ // build our current options associative array $current_options = array( 'dt_atp_customer_key' => get_option('dt_atp_customer_key'), 'dt_atp_customer_secret' => get_option('dt_atp_customer_secret'), 'dt_atp_access_token' => get_option('dt_atp_access_token'), 'dt_atp_access_token_secret' => get_option('dt_atp_access_token_secret') ); } if($for == 'settings'){ if(!get_option('dt_atp_number_of_tweets')){ add_option('dt_atp_number_of_tweets',165); add_option('dt_atp_number_of_saved_tweets',200); add_option('dt_atp_cron_time',5); } $current_options = array( 'dt_atp_textbox' => get_option('dt_atp_textbox'), 'dt_atp_radio' => get_option('dt_atp_radio'), 'dt_atp_number_of_tweets' => (int)get_option('dt_atp_number_of_tweets'), 'dt_atp_number_of_saved_tweets' => (int)get_option('dt_atp_number_of_saved_tweets'), 'dt_atp_cron_time' => (int)get_option('dt_atp_cron_time'), ); } if($for == 'dashboard'){ $current_options = array( 'dt_atp_status_enabled' => (int)get_option('dt_atp_status_enabled'), 'dt_atp_wp_cron_enabled' => (int)get_option('dt_atp_wp_cron_enabled'), ); } if($for == 'display'){ if(!get_option('dt_atp_textarea_style')){ add_option('dt_atp_textarea_style','

[status]

'); } $current_options = array( 'dt_atp_textarea_style' => get_option('dt_atp_textarea_style'), 'dt_atp_wrapper_class' => get_option('dt_atp_wrapper_class'), ); } // return current options return $current_options; } /* !8. ADMIN PAGES */ // 8.1 // hint: dashboard admin page function dt_atp_dashboard_admin_page() { $options = dt_atp_get_current_options('dashboard'); $last_updated = dt_atp_last_update_time(); $tags = get_option('dt_atp_textbox'); echo('

Another Twitter Plugin

'); if($options['dt_atp_status_enabled'] == 0 ){ echo('

Plugin is disabled and not visible on your website, some functions may not work if plugin is disabled.

'); } if(count(get_option('dt_atp_textbox')) == 0){ echo('

You have to add hashtag/username here

'); } settings_errors('TwitterError'); echo('
'); // outputs a unique nounce for our plugin options settings_fields('dt_atp_dashboard_form1'); // generates a unique hidden field with our form handling url @do_settings_fields('dt_atp_dashboard_form1'); echo(''); } else { echo (''); } echo(''); } else { echo (''); } echo(''); echo('
Status '); if($options['dt_atp_status_enabled'] == 1){ echo (' '); submit_button( "Disable", "secondary","submit",false,array( 'style' => 'color:red;' ) ); echo(' '); submit_button( "Enable", "secondary","submit",false,array( 'style' => 'color:green;' ) ); echo('
WP Cron '); // outputs a unique nounce for our plugin options settings_fields('dt_atp_dashboard_form2'); // generates a unique hidden field with our form handling url @do_settings_fields('dt_atp_dashboard_form2'); if($options['dt_atp_wp_cron_enabled'] == 1){ echo (' '); submit_button( "Disable", "secondary","submit",false,array( 'style' => 'color:red;' ) ); echo(' '); submit_button( "Enable", "secondary","submit",false,array( 'style' => 'color:green;' ) ); echo('
Last Update ') ; submit_button("Update Now", "secondary","btnUpdate",false ); echo('
Shortcode

[dt_atp_twitter]

Currently Active '); if(!empty($tags)){ echo ('
    '); foreach($tags as $tag){ echo "
  1. ".$tag."
  2. "; } echo ('
'); } else { echo "No Active Tags"; } echo ('
Reset ') ; submit_button( "Reset", "secondary","btnReset",false ); echo('

Reset will delete all saved tweets.
SUGGESTION: Reset when you change hashtag/username so you don\'t see old Tweets .

'); // outputs the WP submit button html echo('
'); } // 8.2 // hint: plugin settings function dt_atp_plugin_settings_admin_page() { $options = dt_atp_get_current_options('settings'); echo('

Plugin Settings

'); $options1 = dt_atp_get_current_options('dashboard'); if($options1['dt_atp_status_enabled'] == 0 ){ echo('

Plugin is disabled and not visible on your website, some functions may not work if plugin is disabled.

'); } settings_errors('updateSettings'); echo('
'); // outputs a unique nounce for our plugin options settings_fields('dt_atp_plugin_settings'); // generates a unique hidden field with our form handling url @do_settings_fields('dt_atp_plugin_settings'); if($options['dt_atp_textbox']){ $options['dt_atp_textbox'] = array_slice($options['dt_atp_textbox'], 0, 5); } $dt_atp_fields = count($options['dt_atp_textbox']) == 0 ? '1' : count($options['dt_atp_textbox']); echo(''); for ($i=1; $i < $dt_atp_fields; $i++){ echo(' ' ); } echo('

'); // outputs the WP submit button html @submit_button('Save Changes','primary','btnChange'); echo('
'); echo('
'); // outputs a unique nounce for our plugin options settings_fields('dt_atp_plugin_additional_settings'); // generates a unique hidden field with our form handling url @do_settings_fields('dt_atp_plugin_additional_settings'); echo(''); echo('

Number of tweets you want to get per username/hashtag per API Call.
IMPORTANT: Limit is 180 API Calls, one API Call equals 15 Tweets.
SUGGESTION: for 5 hashtags/usernames, maximum is 165 tweets every 5 minutes.

Number of tweets you want to save and show on your website, some functions may not work if plugin is disabled.
IMPORTANT: Higher the number, higher is the loading time of your website, some functions may not work if plugin is disabled.
SUGGESTION: Limit to 200-300 Tweets, no one wants to scroll forever.

minutes

How offten to collect new Tweets, you have to active this option in Dashboard.
IMPORTANT: There is a limit, combine it with "Number of Tweets" from above.
SUGGESTION: If you have 5 hashtags/usernames and you left Number of Tweets on 165, leave this at default value ( 5 minutes ).
WP Cron activates every X minutes if you have a visitor, if you don\'t have high traffic you should add your website to cronjob.

'); // outputs the WP submit button html @submit_button(); echo('
'); } // 8.3 // hint: twitter settings function dt_atp_twitter_settings_admin_page() { // get the default values for our options $options = dt_atp_get_current_options('twitter'); echo('

Twitter Settings

'); $options1 = dt_atp_get_current_options('dashboard'); if($options1['dt_atp_status_enabled'] == 0 ){ echo('

Plugin is disabled and not visible on your website, some functions may not work if plugin is disabled.

'); } echo('

This is the page where you enter details about your Twitter Application
IMPORTANT: If you don\'t know how to make your twitter application, check our instructions under the form.

'); // outputs a unique nounce for our plugin options settings_fields('dt_atp_plugin_twitter'); // generates a unique hidden field with our form handling url @do_settings_fields('dt_atp_plugin_twitter'); echo('
'); // outputs the WP submit button html @submit_button(); echo('
'); $output = '

How to setup your Twitter Application

There is few simple steps you need to follow

  1. Go to http://apps.twitter.com/
  2. Login and click Create New App

    1. Name: Give your app a unique name
    2. Description: You don’t have to worry much about the description- you can change this later.
    3. Website: Put your website in the website field. It’s supposed to be your application’s publicly accessible home page.
    4. Callback URL: You can ignore the Callback URL field.
  3. You’ll then be presented with lots of information, but we’re not quite done yet. We now need to authorise the Twitter app for your Twitter account.
    To do this, click the “Create my access token” button. This takes a few seconds, so if you don’t see the access tokens on the next screen,
    you may have to refresh the page a few times.
  4. Once you’ve done this, you can copy your Consumer Key, Consumer Secret, OAuth Access Token, OAuth Access Token Secret to form above.
'; echo $output; } //8.4 // Display Style function dt_atp_display_style_admin_page() { $options = dt_atp_get_current_options('display'); echo('

Display Settings

'); $options1 = dt_atp_get_current_options('dashboard'); if($options1['dt_atp_status_enabled'] == 0 ){ echo('

Plugin is disabled and not visible on your website, some functions may not work if plugin is disabled.

'); } echo('

You can customize your format as you wish, you can see simple example in textarea, hover over buttons for description
IMPORTANT: Use only twitter option from buttons above textarea, custom options won\'t work.

Format your Tweets

'); // outputs a unique nounce for our plugin options settings_fields('dt_atp_display_style'); // generates a unique hidden field with our form handling url @do_settings_fields('dt_atp_display_style'); echo('Atributes from Twitter:

Add class to wrapper div:

'); @submit_button(); echo('Want to make it look better? add "scroll" class to wrapper and define it in your css as:
.scroll {
overflow: scroll;
max-height: 410px;
}
'); } /* !9. SETTINGS */ // 9.1 // hint: registers all our plugin options function dt_atp_register_twitter() { // plugin options register_setting('dt_atp_plugin_twitter', 'dt_atp_customer_key','dt_atp_validate_plugin'); register_setting('dt_atp_plugin_twitter', 'dt_atp_customer_secret','dt_atp_validate_plugin'); register_setting('dt_atp_plugin_twitter', 'dt_atp_access_token','dt_atp_validate_plugin'); register_setting('dt_atp_plugin_twitter', 'dt_atp_access_token_secret','dt_atp_validate_plugin'); } function dt_atp_register_settings() { // plugin options register_setting('dt_atp_plugin_settings', 'dt_atp_textbox','dt_atp_validation_function'); register_setting('dt_atp_plugin_settings', 'dt_atp_radio','dt_atp_validate_wp_cron_again'); if(isset($_POST['btnChange'])){ $type = 'notice-warning'; $message = __( 'After changing hashtags or usernames we suggest you go to Dashboard and reset old tweets.'); add_settings_error( 'updateSettings', esc_attr( 'settings_updated' ), $message, $type ); } } function dt_atp_register_additional_settings(){ register_setting('dt_atp_plugin_additional_settings', 'dt_atp_number_of_saved_tweets'); register_setting('dt_atp_plugin_additional_settings', 'dt_atp_cron_time'); register_setting('dt_atp_plugin_additional_settings', 'dt_atp_number_of_tweets','dt_atp_validate_wp_cron'); } function dt_atp_register_dashboard_form1(){ register_setting('dt_atp_dashboard_form1', 'dt_atp_status_enabled','dt_atp_validate_status'); } function dt_atp_register_dashboard_form2(){ register_setting('dt_atp_dashboard_form2', 'dt_atp_wp_cron_enabled','dt_atp_validate_status_settings'); } function dt_atp_register_display_style(){ register_setting('dt_atp_display_style', 'dt_atp_textarea_style'); register_setting('dt_atp_display_style', 'dt_atp_wrapper_class'); } /* !10. MISCELLANEOUS */ //if hashtag/username field empty don't save it function dt_atp_validation_function( $input ) { $input = array_filter($input); return $input; } //if twitter information empty don't allow plugin to be enabled function dt_atp_validate_status( $input ) { if($input == 1){ $options = dt_atp_get_current_options('twitter'); if(empty($options) || empty($options['dt_atp_customer_secret']) || empty($options['dt_atp_access_token']) || empty($options['dt_atp_access_token_secret']) || empty($options['dt_atp_customer_key'])){ return add_settings_error( 'TwitterError', esc_attr( 'check_status' ), 'You can\'t enable plugin if you did not add Twitter App information, you can do that here.', 'notice-warning' ); } } return $input; } // if twitter information empty disable plugin function dt_atp_validate_plugin( $input ) { if(empty($input)){ update_option('dt_atp_status_enabled',0); }else { return $input; } } // if wp cron settings pass limit, don't allow it function dt_atp_validate_wp_cron( $input ) { $time = floor(15 / get_option('dt_atp_cron_time')); $calls = ceil ($input/15); $time = $calls * count(get_option('dt_atp_textbox')) * $time; if($time > 179){ add_settings_error( 'updateSettings', esc_attr( 'settings_updated' ), 'Your current settings pass Limit of 180 API Calls, decrease number of Tweets or increase time of WP Cron.', 'error' ); return 29; } else{ return $input; } } function dt_atp_validate_wp_cron_again( $input ) { $time = floor(15 / get_option('dt_atp_cron_time')); $calls = ceil (get_option('dt_atp_number_of_tweets')/15); $time = $calls * count(get_option('dt_atp_textbox')) * $time; if($time > 179){ add_settings_error( 'updateSettings', esc_attr( 'settings_updated' ), 'Your current settings pass Limit of 180 API Calls, decrease number of Tweets or increase time of WP Cron.', 'error' ); update_option('dt_atp_number_of_tweets',29); } return $input; } //if twitter information empty and/or plugin disabled, don't allow wp cron function dt_atp_validate_status_settings( $input ) { if(get_option('dt_atp_status_enabled') == 1){ if(count(get_option('dt_atp_textbox')) == 0){ return dt_atp_validate_status( $input ); }else{ return add_settings_error( 'TwitterError', esc_attr( 'check_status' ), 'You have to add hashtag/username here', 'notice-error' ); } } else{ return add_settings_error( 'TwitterError', esc_attr( 'check_status' ), 'You have to enable plugin.', 'notice-warning' ); } } //get the last time json was modified function dt_atp_last_update_time(){ $time = get_option('dt_atp_last_update_time'); if ($time) { $time = time() - $time; $time = ($time<1)? 1 : $time; $tokens = array ( 31536000 => 'year', 2592000 => 'month', 604800 => 'week', 86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second' ); foreach ($tokens as $unit => $text) { if ($time < $unit) continue; $numberOfUnits = floor($time / $unit); return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s ':'').' ago'; } } else { return 'Never'; } } // reset json and last ids function dt_atp_reset_tweets(){ if(isset($_POST['btnReset'])){ file_put_contents(dt_atp_plugin_dir.'twitter.json', json_encode(new stdClass)); update_option('dt_atp_last_update_time',time()); update_option('dt_atp_currently_active',''); } $red = ($_SERVER["HTTP_REFERER"] != '' ? $_SERVER["HTTP_REFERER"] : site_url()); header("Location: " . $red); exit; } // SCHEDULE 'WP CRON' if you can call it that way if ( ! get_transient( 'schedule' ) && get_option('dt_atp_wp_cron_enabled')) { set_transient( 'schedule', true, get_option('dt_atp_cron_time') * MINUTE_IN_SECONDS ); dt_atp_get_new_tweets(); } function dt_atp_remove_options() { $options = array(); $options['group']['dt_atp_plugin_twitter'] = array('dt_atp_customer_key', 'dt_atp_customer_secret', 'dt_atp_access_token', 'dt_atp_access_token_secret'); $options['group']['dt_atp_plugin_settings'] = array('dt_atp_textbox', 'dt_atp_radio'); $options['group']['dt_atp_plugin_additional_settings'] = array('dt_atp_number_of_saved_tweets', 'dt_atp_cron_time', 'dt_atp_number_of_tweets'); $options['group']['dt_atp_dashboard_form1'] = array('dt_atp_status_enabled'); $options['group']['dt_atp_dashboard_form2'] = array('dt_atp_wp_cron_enabled'); $options['group']['dt_atp_display_style'] = array('dt_atp_textarea_style', 'dt_atp_wrapper_class'); // loop over all the settings foreach( $options['group'] as $g ): foreach($g as $s){ unregister_setting( $g, $s ); } endforeach; delete_option( 'currently_active' ); delete_option( 'last_update_time' ); }