Last active
December 11, 2015 19:08
-
-
Save LinzardMac/4646092 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Twitter Pull | |
* Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates | |
* Description: A brief description of the Plugin. | |
* Version: The Plugin's Version Number, e.g.: 1.0 | |
* Author: Name Of The Plugin Author | |
* Author URI: http://URI_Of_The_Plugin_Author | |
* License: A "Slug" license name e.g. GPL2 | |
*/ | |
// Schedules an event | |
function ttp_create_schedule() { | |
$options = get_option( 'ttp_options' ); | |
$recurrence = isset( $options['recurrence'] ) ? $options['recurrence'] : 'hourly'; | |
// Clear the schedule if recurrence was changed | |
$schedule = wp_get_schedule( 'ttp_tweets_to_posts' ); | |
if ( $schedule != $recurrence ) | |
wp_clear_scheduled_hook( 'ttp_tweets_to_posts' ); | |
if ( ! wp_next_scheduled( 'ttp_tweets_to_posts' ) ) { | |
wp_schedule_event( time(), $recurrence, 'ttp_tweets_to_posts' ); | |
} | |
// clear after testing wp_clear_scheduled_hook( 'my_tweets_to_posts' ); | |
// clear after testing do_action( 'my_tweets_to_posts' ); | |
} | |
add_action( 'init', 'ttp_create_schedule' ); | |
// Fired during our scheduled event | |
function ttp_fetch_tweets() { | |
$options = get_option( 'ttp_options' ); | |
$query = isset( $options['search'] ) ? $options['search'] : ''; | |
$author_id = isset( $options['author'] ) ? $options['author'] : 0; | |
$author = get_user_by( 'id', $author_id ); | |
// Don't do anything if not configured. | |
if ( empty( $author ) || empty( $query ) ) | |
return; | |
$max_id = get_option( 'ttp_max_tweet_id', 0 ); | |
// Fire the HTTP request to Twitter | |
$response = wp_remote_get( 'http://search.twitter.com/search.json?q=' . urlencode( $query ) . '&result_type=recent&rpp=10&since_id=' . $max_id ); | |
// Make sure no errors occured | |
if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) == 200 ) { | |
// Parse the response and grab the max_id | |
$response = json_decode( wp_remote_retrieve_body( $response ) ); | |
if ( ! empty( $response->results ) ) { | |
$max_id = $response->max_id_str; | |
update_option( 'ttp_max_tweet_id', $max_id ); | |
} | |
// Loop through the tweets (search results) and publish posts | |
foreach ( $response->results as $tweet ) { | |
$gmt_date = date( 'Y-m-d H:i:s', strtotime( $tweet->created_at ) ); | |
$post = array( | |
'post_content' => $tweet->text, | |
'post_type' => 'imported_content', | |
'post_title' => 'Tweet-' . $tweet->id, | |
'post_status' => 'publish', | |
'post_date_gmt' => $gmt_date, | |
'post_date' => get_date_from_gmt( $gmt_date ), | |
'post_author' => $author->ID, | |
); | |
$post_id = wp_insert_post( $post ); | |
wp_set_post_terms( $post_id, '4', 'category', false ) ; | |
} | |
} | |
} | |
add_action( 'ttp_tweets_to_posts', 'ttp_fetch_tweets' ); | |
//** add settings page **// | |
function ttp_options_page() { | |
add_options_page( 'Tweets to Posts Options', 'Tweets to Posts', 'manage_options', 'ttp_options', 'ttp_options_page_contents' ); | |
} | |
add_action( 'admin_menu', 'ttp_options_page' ); | |
function ttp_options_page_contents() { | |
if (isset($_GET['force_pull'])){ | |
do_action('ttp_tweets_to_posts'); | |
} | |
else { | |
echo "Josh is a douchebag who doesn't know shit"; | |
} | |
?> | |
<div class="wrap"> | |
<?php screen_icon(); ?> | |
<h2>Tweets to Posts Options</h2> | |
<form method="get" id="force" action=""> | |
<input type="hidden" name="force_pull" value="1"> | |
<input type="submit" value="force"> | |
</form> | |
<form method="post" action="options.php" id="options"> | |
<?php wp_nonce_field( 'update-options' ); ?> | |
<?php settings_fields( 'ttp_options' ); ?> | |
<?php do_settings_sections( 'ttp_options' ); ?> | |
<?php submit_button(); ?> | |
</form> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment