3 Plugin Name: Ozh' PLC-WP-Twitter Bridge
4 Plugin URI: http://planetozh.com/
5 Description: Create short URL for Posts with P.L.C. (such as http://ozh.in/) and tweet it
7 Author URI: http://planetozh.com/
10 /*********************** EDIT THIS **********************/
13 define( 'WPyourls_DO_POSTS', true ); // generate short url & tweet when new post
14 define( 'WPyourls_DO_PAGES', false ); // generate short url & tweet when new page
17 define( 'WPyourls_TWITTER_LOGIN', 'login' ); // twitter id
18 define( 'WPyourls_TWITTER_PWD', 'password' ); // twitter password
19 define( 'WPyourls_TWITTER_MSG', 'New blog post: %T %U'); // new tweet. Keep template short! Will replace %T and %U with post title & short URL
22 define( 'WPyourls_yourls_INCLUDES_DIR', '/home/planetozh/ozh.in/includes/' ); // trailing slash plz
24 /********************* DO NOT EDIT *********************/
27 add_action('new_to_publish', 'wp_ozh_yourls_newpost');
28 add_action('draft_to_publish', 'wp_ozh_yourls_newpost');
29 add_action('pending_to_publish', 'wp_ozh_yourls_newpost');
30 add_action('future_to_publish', 'wp_ozh_yourls_newpost');
31 add_action('admin_init', 'wp_ozh_yourls_init' );
33 //require_once(dirname(__FILE__).'/inc/options.php');
34 //add_action('admin_menu', 'wp_ozh_yourls_add_page');
37 // Function called when new post. Passing post id.
38 // Everything will break if PLC files not found.
39 function wp_ozh_yourls_newpost( $post ) {
42 $post = get_post($post_id);
44 if ( ( $post->post_type == 'post' && !WPyourls_DO_POSTS) || ( $post->post_type == 'page' && !WPyourls_DO_PAGES) )
47 $title = get_the_title($post_id);
48 $url = get_permalink ($post_id);
49 $short = wp_ozh_yourls_get_new_short_url( $post_id, $url );
51 // Tweet title + short URL (only once, don't tweet on edit)
52 if ( !post_custom( 'yourls_tweeted' ) ) {
53 require_once( dirname(__FILE__) . '/inc/twitter.php' );
54 $tweet = wp_ozh_yourls_maketweet( $short, $title );
55 if ( wp_ozh_yourls_tweet_it(WPyourls_TWITTER_LOGIN, WPyourls_TWITTER_PWD, $tweet) ) {
56 update_post_meta($post_id, 'yourls_tweeted', 1);
61 // Get or create the short URL for a post. Return string(url)
62 function wp_ozh_yourls_geturl( $id ) {
63 $short = post_custom( 'yourls_shorturl' );
65 // short URL never was not created before, let's get it now
66 $short = wp_ozh_yourls_get_new_short_url( $id );
72 // Template tag: echo short URL for current post
73 function wp_ozh_yourls_url() {
75 $short = wp_ozh_yourls_geturl( $id );
77 echo "<a href=\"$short\" rel=\"alternate shorter\" rel=\"nofollow\" title=\"short URL\">$short</a>";
80 // Template tag: echo short URL alternate link in <head> for current post. See http://revcanonical.appspot.com/ && http://shorturl.appjet.net/
81 function wp_ozh_yourls_head_linkrel() {
82 //if ( (is_single() or is_page()) )
85 $short = wp_ozh_yourls_geturl( $id );
87 echo "<link rel=\"alternate short shorter shorturl\" href=\"$short\" />\n";
91 // The WP-PLC bridge function: get short URL of a WP post. Returns string(url)
92 function wp_ozh_yourls_get_new_short_url( $post_id, $url = '' ) {
93 global $yourls_reserved_URL;
94 require_once(WPyourls_yourls_INCLUDES_DIR . 'config.php');
97 $url = get_permalink ($post_id);
100 $yourls_db = new wpdb(yourls_DB_USER, yourls_DB_PASS, yourls_DB_NAME, yourls_DB_HOST);
101 $yourls_result = yourls_add_new_link($url, '', $yourls_db);
102 // Short URL is: $yourls_result['shorturl']
104 // Store short URL in a custom field
105 update_post_meta($post_id, 'yourls_shorturl', $yourls_result['shorturl']);
107 return $yourls_result['shorturl'];
110 // Parse the tweet template and make a 140 char string
111 function wp_ozh_yourls_maketweet( $url, $title ) {
112 // Replace %U with short url
113 $tweet = str_replace('%U', $url, WPyourls_TWITTER_MSG);
114 // Now replace %T with as many chars as possible to keep under 140
115 $maxlen = 140 - ( strlen( $tweet ) - 2); // 2 = "%T"
116 if (strlen($title) > $maxlen) {
117 $title = substr($title, 0, ($maxlen - 3)) . '...';
120 $tweet = str_replace('%T', $title, $tweet);
124 // Init plugin options
125 function wp_ozh_yourls_init(){
127 register_setting( 'wp_ozh_yourls_options', 'ozh_plc', 'wp_ozh_yourls_sanitize' );
128 $wp_ozh_plc = get_option('ozh_plc');