(no commit message)
[opensuse:news_o_o.git] / wp-content / plugins / tweetmeme / tweetmeme.php
1 <?php\r
2 /*\r
3 Plugin Name: TweetMeme Retweet Button\r
4 Plugin URI: http://tweetmeme.com/about/plugins\r
5 Description: Adds a button which easily lets you retweet your blog posts.\r
6 Version: 1.8.6\r
7 Author: TweetMeme\r
8 Author URI: http://tweetmeme.com\r
9 */\r
10 \r
11 function tm_options() {\r
12         add_menu_page('TweetMeme', 'TweetMeme', 8, basename(__FILE__), 'tm_options_page');\r
13         add_submenu_page(basename(__FILE__), 'Settings', 'Settings', 8, basename(__FILE__), 'tm_options_page');\r
14 }\r
15 \r
16 /**\r
17 * Build up all the params for the button\r
18 */\r
19 function tm_build_options() {\r
20         // get the post varibale (should be in the loop)\r
21         global $post;\r
22         // get the permalink\r
23     if (get_post_status($post->ID) == 'publish') {\r
24         $url = get_permalink();\r
25     }\r
26     $button = '?url=' . urlencode($url);\r
27 \r
28         // now build up the params, start with the source\r
29     if (get_option('tm_source')) {\r
30         $button .= '&amp;source=' . urlencode(get_option('tm_source'));\r
31     }\r
32 \r
33         // which style\r
34     if (get_option('tm_version') == 'compact') {\r
35         $button .= '&amp;style=compact';\r
36     } else {\r
37                 $button .= '&amp;style=normal';\r
38         }\r
39 \r
40         // what shortner to use\r
41         if (get_option('tm_url_shortner') && get_option('tm_url_shortner') != 'default') {\r
42         $button .= '&amp;service=' . urlencode(get_option('tm_url_shortner'));\r
43         }\r
44 \r
45         // does the shortner have an API key\r
46         if (get_option('tm_api_key')) {\r
47                 $button .= '&amp;service_api=' . urlencode(get_option('tm_api_key'));\r
48         }\r
49 \r
50         // how many spaces do we want to leave at the end\r
51         if (get_option('tm_space')) {\r
52                 $button .= '&amp;space=' . get_option('tm_space');\r
53         }\r
54 \r
55         // append the hashtags\r
56         if (get_option('tm_hashtags') == 'yes') {\r
57                 // first lets see if the post has the custom field\r
58                 if (($hashtags = get_post_meta($post->ID, 'tm_hashtags')) != false) {\r
59                         // first split them out\r
60                         $hashtags = explode(',', $hashtags[0]);\r
61                         // go through and urlencode\r
62                         foreach($hashtags as $row => $tag) {\r
63                                 $hashtags[$row] = urlencode(trim($tag));\r
64                         }\r
65                         // nope so lets use them\r
66                         $button .= '&amp;hashtags=' . implode(',', $hashtags);\r
67                 } else if (($tags = get_the_tags()) != false) {\r
68                         // ok, grab them off the post tags\r
69                         $hashtags = array();\r
70                         foreach ($tags as $tag) {\r
71                                 $hashtags[] = urlencode($tag->name);\r
72                         }\r
73                         $button .= '&amp;hashtags=' . implode(',', $hashtags);\r
74                 } else if (($hashtags = get_option('tm_hashtags_tags')) != false) {\r
75                         // first split them out\r
76                         $hashtags = explode(',', $hashtags);\r
77                         // go through and urlencode\r
78                         foreach($hashtags as $row => $tag) {\r
79                                 $hashtags[$row] = urlencode(trim($tag));\r
80                         }\r
81                         // add them all back together\r
82                         $button .= '&amp;hashtags=' . implode(',', $hashtags);\r
83                 }\r
84         }\r
85         // return all the params\r
86         return $button;\r
87 }\r
88 \r
89 /**\r
90 * Generate the iFrame render of the button\r
91 */\r
92 function tm_generate_button() {\r
93         // build up the outer style\r
94     $button = '<div class="tweetmeme_button" style="' . get_option('tm_style') . '">';\r
95     $button .= '<iframe src="http://api.tweetmeme.com/button.js' . tm_build_options() . '" ';\r
96 \r
97         // give it a height, dependant on style\r
98     if (get_option('tm_version') == 'compact') {\r
99         $button .= 'height="20" width="90"';\r
100     } else {\r
101                 $button .= 'height="61" width="50"';\r
102         }\r
103         // close off the iframe\r
104         $button .= ' frameborder="0" scrolling="no"></iframe></div>';\r
105         // return the iframe code\r
106     return $button;\r
107 }\r
108 \r
109 /**\r
110 * Generates the image button\r
111 */\r
112 function tm_generate_static_button() {\r
113         if (get_post_status($post->ID) == 'publish') {\r
114         $url = get_permalink();\r
115                 return\r
116                 '<div class="tweetmeme_button" style="' . get_option('tm_style') . '">\r
117                         <a href="http://api.tweetmeme.com/share?url=' . urlencode($url) . '">\r
118                                 <img src="http://api.tweetmeme.com/imagebutton.gif' . tm_build_options() . '" height="61" width="50" />\r
119                         </a>\r
120                 </div>';\r
121         } else {\r
122                 return;\r
123         }\r
124 }\r
125 \r
126 /**\r
127 * Gets run when the content is loaded in the loop\r
128 */\r
129 function tm_update($content) {\r
130 \r
131     global $post;\r
132 \r
133     // add the manual option, code added by kovshenin\r
134     if (get_option('tm_where') == 'manual') {\r
135         return $content;\r
136         }\r
137     // is it a page\r
138     if (get_option('tm_display_page') == null && is_page()) {\r
139         return $content;\r
140     }\r
141         // are we on the front page\r
142     if (get_option('tm_display_front') == null && is_home()) {\r
143         return $content;\r
144     }\r
145         // are we in a feed\r
146     if (is_feed()) {\r
147                 $button = tm_generate_static_button();\r
148                 $where = 'tm_rss_where';\r
149     } else {\r
150                 $button = tm_generate_button();\r
151                 $where = 'tm_where';\r
152         }\r
153         // are we displaying in a feed\r
154         if (is_feed() && get_option('tm_display_rss') == null) {\r
155                 return $content;\r
156         }\r
157 \r
158         // are we just using the shortcode\r
159         if (get_option($where) == 'shortcode') {\r
160                 return str_replace('[tweetmeme]', $button, $content);\r
161         } else {\r
162                 // if we have switched the button off\r
163                 if (get_post_meta($post->ID, 'tweetmeme') == null) {\r
164                         if (get_option($where) == 'beforeandafter') {\r
165                                 // adding it before and after\r
166                                 return $button . $content . $button;\r
167                         } else if (get_option($where) == 'before') {\r
168                                 // just before\r
169                                 return $button . $content;\r
170                         } else {\r
171                                 // just after\r
172                                 return $content . $button;\r
173                         }\r
174                 } else {\r
175                         // not at all\r
176                         return $content;\r
177                 }\r
178         }\r
179 }\r
180 \r
181 // Manual output\r
182 function tweetmeme() {\r
183     if (get_option('tm_where') == 'manual') {\r
184         return tm_generate_button();\r
185     } else {\r
186         return false;\r
187     }\r
188 }\r
189 \r
190 // Remove the filter excerpts\r
191 // Code added by Soccer Dad\r
192 function tm_remove_filter($content) {\r
193         if (!is_feed()) {\r
194         remove_action('the_content', 'tm_update');\r
195         }\r
196     return $content;\r
197 }\r
198 \r
199 /**\r
200 * Ping when tweetmeme when a post is updated, this makes sure the titles/desc are correct on tweetmeme\r
201 */\r
202 function tm_ping($post_id) {\r
203     // do we have curl\r
204     if ((get_option('tm_ping') != 'off') && function_exists('curl_init')) {\r
205         $url = get_permalink($post_id);\r
206         // create a new cURL resource\r
207         $ch = curl_init();\r
208         // set URL and other appropriate options\r
209         curl_setopt($ch, CURLOPT_URL, 'http://api.tweetmeme.com/ping.php?url=' . urlencode($url));\r
210         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);\r
211         curl_setopt($ch, CURLOPT_TIMEOUT, 3);\r
212         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r
213         // grab URL and pass it to the browser\r
214         curl_exec($ch);\r
215         // close cURL resource, and free up system resources\r
216         curl_close($ch);\r
217     }\r
218 }\r
219 \r
220 /**\r
221 * Adds a tweetmeme-title meta title, provides a much more accurate title for the button\r
222 */\r
223 function tm_head() {\r
224         // if its a post page\r
225         if (is_single()) {\r
226                 global $post;\r
227                 $title = get_the_title($post->ID);\r
228                 echo '<meta name="tweetmeme-title" content="' . strip_tags($title) . '" />';\r
229         }\r
230 }\r
231 \r
232 function tm_options_page() {\r
233 ?>\r
234     <div class="wrap">\r
235     <div class="icon32" id="icon-options-general"><br/></div><h2>Settings for Tweetmeme Integration</h2>\r
236     <p>This plugin will install the tweetmeme widget for each of your blog posts in both the content of your posts and the RSS feed.\r
237     It can be easily styles in your blog posts and is referenced by the id <code>tweetmeme_button</code>.\r
238     </p>\r
239     <form method="post" action="options.php">\r
240     <?php\r
241         // New way of setting the fields, for WP 2.7 and newer\r
242         if(function_exists('settings_fields')){\r
243             settings_fields('tm-options');\r
244         } else {\r
245             wp_nonce_field('update-options');\r
246             ?>\r
247             <input type="hidden" name="action" value="update" />\r
248             <input type="hidden" name="page_options" value="tm_ping,tm_where,tm_style,tm_version,tm_display_page,tm_display_front,tm_display_rss,tm_display_feed,tm_source,tm_url_shortner,tm_space,tm_hashtags,tm_hashtags_tags,tm_api_key" />\r
249             <?php\r
250         }\r
251     ?>\r
252         <table class="form-table">\r
253             <tr>\r
254                     <tr>\r
255                         <th scope="row" valign="top">\r
256                             Display\r
257                         </th>\r
258                         <td>\r
259                             <input type="checkbox" value="1" <?php if (get_option('tm_display_page') == '1') echo 'checked="checked"'; ?> name="tm_display_page" id="tm_display_page" group="tm_display"/>\r
260                             <label for="tm_display_page">Display the button on pages</label>\r
261                             <br/>\r
262                             <input type="checkbox" value="1" <?php if (get_option('tm_display_front') == '1') echo 'checked="checked"'; ?> name="tm_display_front" id="tm_display_front" group="tm_display"/>\r
263                             <label for="tm_display_front">Display the button on the front page (home)</label>\r
264                             <br/>\r
265                             <input type="checkbox" value="1" <?php if (get_option('tm_display_rss') == '1') echo 'checked="checked"'; ?> name="tm_display_rss" id="tm_display_rss" group="tm_display"/>\r
266                             <label for="tm_display_rss">Display the image button in your feed, only available as <strong>the normal size</strong> widget.</label>\r
267                         </td>\r
268                     </tr>\r
269                 <th scope="row" valign="top">\r
270                     Position\r
271                 </th>\r
272                 <td>\r
273                         <select name="tm_where">\r
274                                 <option <?php if (get_option('tm_where') == 'before') echo 'selected="selected"'; ?> value="before">Before</option>\r
275                                 <option <?php if (get_option('tm_where') == 'after') echo 'selected="selected"'; ?> value="after">After</option>\r
276                                 <option <?php if (get_option('tm_where') == 'beforeandafter') echo 'selected="selected"'; ?> value="beforeandafter">Before and After</option>\r
277                                 <option <?php if (get_option('tm_where') == 'shortcode') echo 'selected="selected"'; ?> value="shortcode">Shortcode [tweetmeme]</option>\r
278                                 <option <?php if (get_option('tm_where') == 'manual') echo 'selected="selected"'; ?> value="manual">Manual</option>\r
279                         </select>\r
280                 </td>\r
281             </tr>\r
282             <tr>\r
283                 <th scope="row" valign="top">\r
284                     RSS Position\r
285                 </th>\r
286                 <td>\r
287                         <select name="tm_rss_where">\r
288                                 <option <?php if (get_option('tm_rss_where') == 'before') echo 'selected="selected"'; ?> value="before">Before</option>\r
289                                 <option <?php if (get_option('tm_rss_where') == 'after') echo 'selected="selected"'; ?> value="after">After</option>\r
290                                 <option <?php if (get_option('tm_rss_where') == 'beforeandafter') echo 'selected="selected"'; ?> value="beforeandafter">Before and After</option>\r
291                                 <option <?php if (get_option('tm_where') == 'shortcode') echo 'selected="selected"'; ?> value="shortcode">Shortcode [tweetmeme]</option>\r
292                         </select>\r
293                 </td>\r
294             </tr>\r
295             <tr>\r
296                 <th scope="row" valign="top"><label for="tm_style">Styling</label></th>\r
297                 <td>\r
298                     <input type="text" value="<?php echo htmlspecialchars(get_option('tm_style')); ?>" name="tm_style" id="tm_style" />\r
299                     <span class="description">Add style to the div that surrounds the button E.g. <code>float: left; margin-right: 10px;</code></span>\r
300                 </td>\r
301             </tr>\r
302             <tr>\r
303                 <th scope="row" valign="top">\r
304                     Type\r
305                 </th>\r
306                 <td>\r
307                     <input type="radio" value="large" <?php if (get_option('tm_version') == 'large') echo 'checked="checked"'; ?> name="tm_version" id="tm_version_large" group="tm_version"/>\r
308                     <label for="tm_version_large">The normal size widget</label>\r
309                     <br/>\r
310                     <input type="radio" value="compact" <?php if (get_option('tm_version') == 'compact') echo 'checked="checked"'; ?> name="tm_version" id="tm_version_compact" group="tm_version" />\r
311                     <label for="tm_version_compact">The compact widget</label>\r
312                 </td>\r
313             </tr>\r
314             <tr>\r
315                 <th scope="row" valign="top">\r
316                     <label for="tm_source">Source</label>\r
317                 </th>\r
318                 <td>\r
319                     RT @<input type="text" value="<?php echo get_option('tm_source'); ?>" name="tm_source" id="tm_source" />\r
320                     <span class="description">Please use the format of 'yourname', not 'RT @yourname'. For more information please see the <a href="http://help.tweetmeme.com">help</a>.</span>\r
321                 </td>\r
322             </tr>\r
323             <tr>\r
324                 <th scope="row" valign="top">\r
325                     URL Shortner\r
326                 </th>\r
327                 <td>\r
328                     <select name="tm_url_shortner">\r
329                         <option <?php if (get_option('tm_url_shortner') == 'default') echo 'selected="selected"'; ?> value="default">Default</option>\r
330                         <option <?php if (get_option('tm_url_shortner') == 'bit.ly') echo 'selected="selected"'; ?> value="bit.ly">bit.ly</option>\r
331                         <option <?php if (get_option('tm_url_shortner') == 'awe.sm') echo 'selected="selected"'; ?> value="awe.sm">awe.sm</option>\r
332                         <option <?php if (get_option('tm_url_shortner') == 'cli.gs') echo 'selected="selected"'; ?> value="cli.gs">cligs</option>\r
333                         <option <?php if (get_option('tm_url_shortner') == 'is.gd') echo 'selected="selected"'; ?> value="is.gd">is.gd</option>\r
334                         <option <?php if (get_option('tm_url_shortner') == 'TinyURL.com') echo 'selected="selected"'; ?> value="TinyURL.com">TinyURL</option>\r
335                         <option <?php if (get_option('tm_url_shortner') == 'ow.ly') echo 'selected="selected"'; ?> value="ow.ly">Ow.ly</option>\r
336                         <option <?php if (get_option('tm_url_shortner') == 'retwt.me') echo 'selected="selected"'; ?> value="retwt.me">retwt.me</option>\r
337                     </select>\r
338                     <span class="description">If you use <strong>awe.sm</strong> an API key is required.</span>\r
339                 </td>\r
340             </tr>\r
341             <tr>\r
342                 <th scope="row" valign="top">\r
343                     <label for="tm_api_key">URL Shortener API Key</label>\r
344                 </th>\r
345                 <td>\r
346                     <input type="text" value="<?php echo get_option('tm_api_key'); ?>" name="tm_api_key" id="tm_api_key" />\r
347                     <span class="description">API Key for use with <strong>Bit.ly</strong>, <strong>awe.sm</strong>, <strong>cligs</strong>.\r
348                     Bit.ly Pro requires the API Key to be in the format 'user_name:api_key'. Where user_name is your username and api_key is your API Key.</span>\r
349                 </td>\r
350             </tr>\r
351             <tr>\r
352                 <th scope="row" valign="top">\r
353                     <label for="tm_space">Spaces</label>\r
354                 </th>\r
355                 <td>\r
356                     <input type="text" value="<?php echo get_option('tm_space'); ?>" name="tm_space" id="tm_space" />\r
357                     <span class="description">The amount of empty space to leave at the end of the tweet.</span>\r
358                 </td>\r
359             </tr>\r
360             <tr>\r
361                 <th scope="row" valigh="top">\r
362                         Hashtags\r
363                 </th>\r
364                 <td>\r
365                         <input type="radio" value="yes" name="tm_hashtags" group="tm_hashtags" id="tm_hashtags_on" <?php if (get_option('tm_hashtags') == 'yes') echo 'checked="checked"'; ?> />\r
366                         <label for="tm_hashtags_on">Take the top tags from the post and apply to the tweet</label>\r
367                         <br/>\r
368                         <input type="radio" value="no" name="tm_hashtags" group="tm_hashtags" id="tm_hashtags_off" <?php if (get_option('tm_hashtags') == 'no') echo 'checked="checked"'; ?> />\r
369                         <label for="tm_hashtags_off">Dont use hashtags</label>\r
370                         <br/>\r
371                         <label for="tm_hashtags_tags">Use these default tags if there are no tags on the post.</label>\r
372                         <input type="text" value="<?php echo get_option('tm_hashtags_tags'); ?>" name="tm_hashtags_tags" />\r
373                         <br/><br/>\r
374                         <span class="description">You can override any of these by specifying hashtags on a per post basis, by using the custom field tm_hashtags (seperated by ,).</span>\r
375                 </td>\r
376             </tr>\r
377             <tr>\r
378                 <th scope="row" valign="top">\r
379                     <label for="tm_ping">Ping Tweetmeme</label>\r
380                 </th>\r
381                 <td>\r
382                     <input type="radio" value="on" <?php if (get_option('tm_ping') == 'on') echo 'checked="checked"'; ?> name="tm_ping" id="tm_ping_on" group="tm_ping"/>\r
383                     <label for="tm_ping_on">Yes (Alert TweetMeme whenever a new post is published)</label>\r
384                     <br/>\r
385                     <input type="radio" value="off" <?php if (get_option('tm_ping') == 'off') echo 'checked="checked"'; ?> name="tm_ping" id="tm_ping_off" group="tm_ping" />\r
386                     <label for="tm_ping_off">No</label>\r
387                 </td>\r
388             </tr>\r
389         </table>\r
390         <p class="submit">\r
391             <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />\r
392         </p>\r
393     </form>\r
394     </div>\r
395 <?php\r
396 }\r
397 \r
398 \r
399 \r
400 // On access of the admin page, register these variables (required for WP 2.7 & newer)\r
401 function tm_init(){\r
402     if(function_exists('register_setting')){\r
403         register_setting('tm-options', 'tm_display_page');\r
404         register_setting('tm-options', 'tm_display_front');\r
405         register_setting('tm-options', 'tm_display_rss');\r
406         register_setting('tm-options', 'tm_source', 'tm_sanitize_username');\r
407         register_setting('tm-options', 'tm_style');\r
408         register_setting('tm-options', 'tm_version');\r
409         register_setting('tm-options', 'tm_where');\r
410         register_setting('tm-options', 'tm_rss_where');\r
411         register_setting('tm-options', 'tm_ping');\r
412         register_setting('tm-options', 'tm_url_shortner');\r
413         register_setting('tm-options', 'tm_space');\r
414         register_setting('tm-options', 'tm_hashtags');\r
415         register_setting('tm-options', 'tm_hashtags_tags');\r
416                 register_setting('tm-options', 'tm_api_key');\r
417     }\r
418 }\r
419 \r
420 function tm_sanitize_username($username){\r
421     return preg_replace('/[^A-Za-z0-9_]/','',$username);\r
422 }\r
423 \r
424 // Only all the admin options if the user is an admin\r
425 if(is_admin()){\r
426     add_action('admin_menu', 'tm_options');\r
427     add_action('admin_init', 'tm_init');\r
428 }\r
429 \r
430 // Set the default options when the plugin is activated\r
431 function tm_activate(){\r
432     add_option('tm_where', 'before');\r
433     add_option('tm_rss_where', 'before');\r
434     add_option('tm_source');\r
435     add_option('tm_style', 'float: right; margin-left: 10px;');\r
436     add_option('tm_version', 'large');\r
437     add_option('tm_display_page', '1');\r
438     add_option('tm_display_front', '1');\r
439     add_option('tm_display_rss', '1');\r
440     add_option('tm_ping', 'on');\r
441     add_option('tm_hashtags', 'on');\r
442 }\r
443 \r
444 add_filter('the_content', 'tm_update', 8);\r
445 add_filter('get_the_excerpt', 'tm_remove_filter', 9);\r
446 \r
447 add_action('publish_post', 'tm_ping', 9);\r
448 \r
449 add_action('wp_head', 'tm_head');\r
450 \r
451 register_activation_hook( __FILE__, 'tm_activate');\r