]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - wordpress-plugin/ozh-plc-bridge/plugin.php
Initial commit (importing from http://svn.planetozh.com/ozhin)
[Github/YOURLS.git] / wordpress-plugin / ozh-plc-bridge / plugin.php
1 <?php
2 /*
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
6 Author: Ozh
7 Author URI: http://planetozh.com/
8 */
9
10 /*********************** EDIT THIS **********************/
11
12 // Plugin behavior
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
15
16 // Twitter settings
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
20
21 // PLC location
22 define( 'WPyourls_yourls_INCLUDES_DIR', '/home/planetozh/ozh.in/includes/' ); // trailing slash plz
23
24 /********************* DO NOT EDIT *********************/
25
26 global $wp_ozh_plc;
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' );
32 if (is_admin()) {
33         //require_once(dirname(__FILE__).'/inc/options.php');
34         //add_action('admin_menu', 'wp_ozh_yourls_add_page');
35 }
36         
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 ) {
40         global $wp_ozh_plc;
41         $post_id = $post->ID;
42         $post = get_post($post_id);
43
44         if (  ( $post->post_type == 'post' && !WPyourls_DO_POSTS) || ( $post->post_type == 'page' && !WPyourls_DO_PAGES) )
45                 return;
46                 
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 );
50         
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);
57                 }
58         }
59 }
60
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' );
64         if (!$short) {
65                 // short URL never was not created before, let's get it now
66                 $short = wp_ozh_yourls_get_new_short_url( $id );
67         }
68         
69         return $short;
70 }
71
72 // Template tag: echo short URL for current post
73 function wp_ozh_yourls_url() {
74         global $id;
75         $short = wp_ozh_yourls_geturl( $id );
76         if ($short)
77                 echo "<a href=\"$short\" rel=\"alternate shorter\" rel=\"nofollow\" title=\"short URL\">$short</a>";
78 }
79
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()) )
83
84         global $id;
85         $short = wp_ozh_yourls_geturl( $id );
86         if ($short)
87                 echo "<link rel=\"alternate short shorter shorturl\" href=\"$short\" />\n";
88 }
89
90
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');
95         
96         if (!$url)
97                 $url = get_permalink ($post_id);
98
99         // Get/make new URL
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']
103         
104         // Store short URL in a custom field
105         update_post_meta($post_id, 'yourls_shorturl', $yourls_result['shorturl']);
106
107         return $yourls_result['shorturl'];
108 }
109
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)) . '...';
118         }
119         
120         $tweet = str_replace('%T', $title, $tweet);
121         return $tweet;
122 }
123
124 // Init plugin options
125 function wp_ozh_yourls_init(){
126         global $wp_ozh_plc;
127         register_setting( 'wp_ozh_yourls_options', 'ozh_plc', 'wp_ozh_yourls_sanitize' );
128         $wp_ozh_plc = get_option('ozh_plc');
129 }
130
131
132 ?>