PluginProbe
Hum / 1.0
Hum v1.0
trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6
hum / hum.php

hum.php in Hum 1.0, at hum.php

222 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Hum
4 Plugin URI: http://github.com/willnorris/wordpress-hum
5 Description: Whistle inspired URL shortener for WordPress
6 Author: Will Norris
7 Author URI: http://willnorris.com/
8 Version: 1.0
9 License: Dual GPL (http://www.fsf.org/licensing/licenses/info/GPLv2.html) and Modified BSD (http://www.fsf.org/licensing/licenses/index_html#ModifiedBSD)
10 Text Domain: hum
11 */
12
13
14 /**
15 * Accept hum query variables.
16 */
17 function hum_query_vars( $vars ) {
18 $vars[] = 'hum';
19 return $vars;
20 }
21 add_action('query_vars', 'hum_query_vars');
22
23
24 /**
25 * Parse request for shortlink.
26 *
27 * @uses do_action() Calls 'hum_request_{$type}" action
28 * @uses do_action() Calls 'hum_request" action
29 */
30 function hum_parse_request( $wp ) {
31 if ( array_key_exists( 'hum', $wp->query_vars ) ) {
32 list($type, $id) = explode('/', $wp->query_vars['hum'], 2);
33 do_action("hum_request_{$type}", $id);
34 do_action('hum_request', $type, $id);
35 }
36 }
37 add_action('parse_request', 'hum_parse_request');
38
39
40 /**
41 * Redirect shortlinks that are for content hosted directly within WordPress.
42 * The 'id' portion of these URLs is expected to be the sexagesimal post ID.
43 *
44 * @param string $code the content-type prefix
45 */
46 function hum_redirect_local( $type, $id ) {
47 $local_types = array('b', 't');
48
49 if ( in_array($type, $local_types) ) {
50 $p = sxg_to_num( $id );
51 $redirect = add_query_arg( 'p', $p, home_url() );
52 wp_redirect( $redirect, 301 );
53 exit;
54 }
55 }
56 add_filter('hum_request', 'hum_redirect_local', 20, 2);
57
58
59 /**
60 * Allow for simple redirect rules for shortlink prefixes. Users can provide a
61 * filter to perform simple URL redirect for a given type prefix. For example,
62 * to redirect all /w/ shortlinks to your personal PBworks wiki, you could use:
63 *
64 * add_filter('hum_redirect_base_w',
65 * create_function('', 'return "http://willnorris.pbworks.com/";'));
66 *
67 * @uses apply_filters() Calls 'hum_redirect_base_{$type}' filter on redirect base URL
68 */
69 function hum_redirect_request( $type, $id ) {
70 $url = apply_filters("hum_redirect_base_{$type}", false);
71 if ( $url ) {
72 $url = trailingslashit($url) . $id;
73 wp_redirect( $url );
74 exit;
75 }
76 }
77 add_action('hum_request', 'hum_redirect_request', 30, 2);
78
79
80 /**
81 * Add rewrite rules for hum shortlinks.
82 *
83 * @param object $wp_rewrite
84 */
85 function hum_rewrite_rules( $wp_rewrite ) {
86 $hum_rules = array(
87 '([a-z]/.+)' => 'index.php?hum=$matches[1]',
88 );
89
90 $wp_rewrite->rules = $hum_rules + $wp_rewrite->rules;
91 }
92 add_action('generate_rewrite_rules', 'hum_rewrite_rules');
93
94
95 /**
96 * Flush wp_rewrite rules.
97 */
98 function hum_flush_rewrite_rules() {
99 global $wp_rewrite;
100 $wp_rewrite->flush_rules();
101 }
102 register_activation_hook(__FILE__, 'hum_flush_rewrite_rules');
103 register_deactivation_hook(__FILE__, 'hum_flush_rewrite_rules');
104
105
106 /**
107 * Get the base URL for hum shortlinks. Defaults to the WordPress home url.
108 * Users can provide a filter to use a custom domain for shortlinks.
109 *
110 * @uses apply_filters() Calls 'hum_shortlink_base' filter on base URL
111 * @return string
112 */
113 function hum_shortlink_base() {
114 return apply_filters( 'hum_shortlink_base', home_url() );
115 }
116
117
118 /**
119 * Get the shortlink for a post, page, attachment, or blog.
120 *
121 * @param string $link the current shortlink for the post
122 * @param int $id post ID
123 * @param string $context
124 * @param boolean $allow_alugs
125 * @return string
126 */
127 function hum_get_shortlink($link, $id, $context, $allow_slugs) {
128 $post_id = 0;
129 if ( 'query' == $context ) {
130 if ( is_front_page() ) {
131 $link = hum_shortlink_base();
132 } elseif ( is_singular() ) {
133 $post_id = get_queried_object_id();
134 }
135 } elseif ( 'post' == $context ) {
136 $post = get_post($id);
137 $post_id = $post->ID;
138 }
139
140 if ( !empty($post_id) ) {
141 $type = hum_type_prefix($post_id);
142 $sxg_id = num_to_sxg($post_id);
143 $link = trailingslashit( hum_shortlink_base() ) . $type . '/' . $sxg_id;
144 }
145
146 return $link;
147 }
148 add_filter('pre_get_shortlink', 'hum_get_shortlink', 10, 4);
149
150
151 /**
152 * Get the content-type prefix for the specified post.
153 *
154 * @see http://ttk.me/w/Whistle#design
155 * @uses apply_filters() Calls 'hum_type_prefix' on the content type prefix
156 *
157 * @param int|object $post A post
158 * @return string the content type prefix for the post
159 */
160 function hum_type_prefix( $post ) {
161 $prefix = 'b';
162
163 $post_format = get_post_format( $post );
164 switch($post_format) {
165 case 'aside':
166 $prefix = 't'; break;
167 case 'status':
168 $prefix = 't'; break;
169 }
170
171 return apply_filters('hum_type_prefix', $prefix, $post);
172 }
173
174
175 // New Base 60 - see http://ttk.me/w/NewBase60
176 //
177 // slightly modified from Cassis Project (http://cassisproject.com/)
178 // Copyright 2010 Tantek Çelik, released under Creative Commons by-sa 3.0
179
180 if ( !function_exists( 'num_to_sxg' ) ):
181 /**
182 * Convert base-10 number to sexagesimal.
183 */
184 function num_to_sxg($n) {
185 $s = "";
186 $m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
187 if ($n===null || $n===0) { return 0; }
188 while ($n>0) {
189 $d = $n % 60;
190 $s = $m[$d] . $s;
191 $n = ($n-$d)/60;
192 }
193 return $s;
194 }
195 endif;
196
197
198 if ( !function_exists( 'sxg_to_num' ) ):
199 /**
200 * Convert sexagesimal to base-10 number.
201 */
202 function sxg_to_num($s) {
203 $n = 0;
204 $j = strlen($s);
205 for ($i=0;$i<$j;$i++) { // iterate from first to last char of $s
206 $c = ord($s[$i]); // put current ASCII of char into $c
207 if ($c>=48 && $c<=57) { $c=$c-48; }
208 else if ($c>=65 && $c<=72) { $c-=55; }
209 else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
210 else if ($c>=74 && $c<=78) { $c-=56; }
211 else if ($c==79) { $c=0; } // error correct typo capital O to 0
212 else if ($c>=80 && $c<=90) { $c-=57; }
213 else if ($c==95) { $c=34; } // underscore
214 else if ($c>=97 && $c<=107) { $c-=62; }
215 else if ($c>=109 && $c<=122) { $c-=63; }
216 else { $c = 0; } // treat all other noise as 0
217 $n = 60*$n + $c;
218 }
219 return $n;
220 }
221 endif;
222