PluginProbe
Hum / 1.2.6
Hum v1.2.6
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.2.6, at hum.php

513 lines 14.4 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: https://github.com/willnorris/wordpress-hum
5 * Description: Personal URL shortener for WordPress
6 * Author: Will Norris
7 * Author URI: https://willnorris.com/
8 * Version: 1.2.6
9 * License: MIT
10 * License URI: http://opensource.org/licenses/MIT
11 * Text Domain: hum
12 */
13
14 class Hum {
15
16 public function __construct() {
17 add_action( 'init', array( $this, 'init' ) );
18 add_action( 'init', array( $this, 'rewrite_rules' ) );
19
20 register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) );
21 register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
22 }
23
24 /**
25 * Initialize the plugin, registering WordPress hooks.
26 */
27 public function init() {
28 load_plugin_textdomain( 'hum', null, basename( dirname( __FILE__ ) ) );
29
30 // if you have hum installed, then you probably actually care about short
31 // links, so we'll add it to the admin menu bar.
32 add_action( 'admin_bar_menu', 'wp_admin_bar_shortlink_menu', 90 );
33
34 add_action( 'query_vars', array( $this, 'query_vars' ) );
35 add_action( 'parse_request', array( $this, 'parse_request' ) );
36 add_filter( 'hum_redirect', array( $this, 'redirect_request' ), 10, 3 );
37 add_filter( 'hum_redirect_i', array( $this, 'redirect_request_i' ), 10, 2 );
38 add_filter( 'hum_process_redirect', array( $this, 'process_redirect' ), 10, 2 );
39 add_filter( 'pre_option_hum_shortlink_base', array( $this, 'config_shortlink_base' ) );
40 add_filter( 'pre_get_shortlink', array( $this, 'get_shortlink' ), 10, 4 );
41 add_filter( 'template_redirect', array( $this, 'legacy_redirect' ) );
42 add_filter( 'hum_legacy_id', array( $this, 'legacy_ftl_id' ), 10, 2 );
43 add_action( 'atom_entry', array( $this, 'shortlink_atom_entry' ) );
44
45 // Admin Settings
46 add_action( 'admin_init', array( $this, 'admin_init' ) );
47 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
48 add_filter( 'manage_edit-post_columns', array( $this, 'add_post_column' ), 10, 1 );
49 add_filter( 'manage_edit-page_columns', array( $this, 'add_post_column' ), 10, 1 );
50 add_action( 'manage_posts_custom_column', array( $this, 'add_posts_custom_column' ), 10, 2 );
51 add_action( 'manage_pages_custom_column', array( $this, 'add_posts_custom_column' ), 10, 2 );
52 }
53
54 /**
55 * Accept hum query variables.
56 */
57 public function query_vars( $vars ) {
58 $vars[] = 'hum';
59 return $vars;
60 }
61
62 /**
63 * Parse request for shortlink. This is the main entry point for handling
64 * short URLs.
65 *
66 * @uses apply_filters() Calls 'hum_redirect' filter
67 * @uses apply_filters() Calls 'hum_process_redirect' filter
68 *
69 * @param WP $wp the WordPress environment for the request
70 */
71 public function parse_request( $wp ) {
72 if ( array_key_exists( 'hum', $wp->query_vars ) ) {
73 $hum_path = $wp->query_vars['hum'];
74 if ( strpos( $hum_path, '/' ) !== false ) {
75 list($type, $id) = explode( '/', $hum_path, 2 );
76 } else {
77 $type = $hum_path;
78 $id = null;
79 }
80
81 $url = apply_filters( 'hum_redirect', null, $type, $id );
82
83 // hum hasn't handled the request yet, so try again but strip common
84 // punctuation that might appear after a URL in written text: . , )
85 if ( ! $url ) {
86 $clean_id = preg_replace( '/[\.,\)]+$/', '', $id );
87 if ( $id !== $clean_id ) {
88 $url = apply_filters( 'hum_redirect', null, $type, $clean_id );
89 }
90 }
91
92 if ( $url ) {
93 do_action( 'hum_process_redirect', $url, $id );
94 }
95
96 // hum didn't handle request, so issue 404.
97 // manually setting query vars like this feels very fragile, but
98 // $wp_query->set_404() doesn't do what we need here.
99 $wp->query_vars['error'] = '404';
100 }
101 }
102
103 /**
104 * Process the redirect.
105 *
106 * @param string $url the permalink of the post
107 * @param string $id the requested post ID
108 */
109 public function process_redirect( $url, $id ) {
110 wp_redirect( $url, 301 );
111 exit;
112 }
113
114 /**
115 * Get the short URL types that are handled locally by WordPress.
116 *
117 * @uses apply_filters() Calls 'hum_local_types' with array of local types
118 *
119 * @return array local types
120 */
121 public function local_types() {
122 $local_types = array( 'b', 't', 'a', 'p' );
123 return apply_filters( 'hum_local_types', $local_types );
124 }
125
126 /**
127 * Attempt to handle redirect for the current shortlink.
128 *
129 * This redirects shortlinks that are for content hosted directly within
130 * WordPress. The 'id' portion of these URLs is expected to be the
131 * sexagesimal post ID.
132 *
133 * This also allows for simple redirect rules for shortlink prefixes. Users
134 * can provide a filter to perform simple URL redirect for a given type
135 * prefix. For example, to redirect all /w/ shortlinks to your personal
136 * PBworks wiki, you could use:
137 *
138 * add_filter('hum_redirect_base_w',
139 * create_function('', 'return "http://willnorris.pbworks.com/";'));
140 *
141 * @uses apply_filters() Calls 'hum_redirect_{$type}' action
142 * @uses apply_filters() Calls 'hum_redirect_base_{$type}' filter on redirect base URL
143 *
144 * @param string $url the short URL
145 * @param string $type the content-type prefix
146 * @param string $id the requested post ID
147 */
148 public function redirect_request( $url, $type, $id ) {
149 // locally hosted content
150 $local_types = $this->local_types();
151 if ( in_array( $type, $local_types, true ) ) {
152 $p = sxg_to_num( $id );
153 if ( $p ) {
154 $url = get_permalink( $p );
155 }
156 }
157
158 // simple redirects for entire base type
159 if ( ! $url ) {
160 $url = apply_filters( "hum_redirect_base_{$type}", false );
161 if ( $url ) {
162 $url = trailingslashit( $url ) . $id;
163 }
164 }
165
166 $url = apply_filters( "hum_redirect_{$type}", $url, $id );
167 return $url;
168 }
169
170 /**
171 * Handles /i/ URLs that have ISBN or ASIN subpaths by redirecting to Amazon.
172 *
173 * @uses apply_filters() Calls 'hum_redirect_i_{$subtype}' action
174 * @uses apply_filters() Calls 'amazon_domain' filter
175 * @uses apply_filters() Calls 'amazon_affiliate_id' filter
176 *
177 * @param string $url the short URL
178 * @param string $path subpath of URL (after /i/)
179 */
180 public function redirect_request_i( $url, $path ) {
181 list( $subtype, $id ) = explode( '/', $path, 2 );
182
183 if ( $subtype ) {
184 switch ( $subtype ) {
185 case 'a':
186 case 'asin':
187 case 'i':
188 case 'isbn':
189 $amazon_domain = apply_filters( 'amazon_domain', 'www.amazon.com' );
190 $amazon_id = apply_filters( 'amazon_affiliate_id', false );
191 if ( $amazon_id ) {
192 // valid partner shortlink, checked by
193 // https://partnernet.amazon.de/gp/associates/network/tools/link-checker/main.html
194 $url = 'http://' . $amazon_domain . '/dp/product/' . $id . '?tag=' . $amazon_id;
195 } else {
196 $url = 'http://' . $amazon_domain . '/dp/product/' . $id;
197 }
198 break;
199 }
200 $url = apply_filters( "hum_redirect_i_{$subtype}", $url, $id );
201 }
202 return $url;
203 }
204
205 /**
206 * Add rewrite rules for hum shortlinks.
207 */
208 public function rewrite_rules() {
209 add_rewrite_rule( '([a-z](\/.*)?$)', 'index.php?hum=$matches[1]', 'top' );
210 }
211
212 /**
213 * Add rewrite rules for hum shortlinks.
214 */
215 public function flush_rewrite_rules() {
216 $this->rewrite_rules();
217 flush_rewrite_rules();
218 }
219
220 /**
221 * Get the base URL for hum shortlinks. Defaults to the WordPress home url.
222 * Users can define HUM_SHORTLINK_BASE or provide a filter to use a custom
223 * domain for shortlinks.
224 *
225 * @uses apply_filters() Calls 'hum_shortlink_base' filter on base URL
226 *
227 * @return string
228 */
229 public function shortlink_base() {
230 $base = get_option( 'hum_shortlink_base' );
231 if ( empty( $base ) ) {
232 $base = home_url();
233 }
234 return apply_filters( 'hum_shortlink_base', $base );
235 }
236
237 /**
238 * Allow the constant named 'HUM_SHORTLINK_BASE' to override the base URL for shortlinks.
239 *
240 * @param string $url the short URL
241 */
242 public function config_shortlink_base( $url = '' ) {
243 if ( defined( 'HUM_SHORTLINK_BASE' ) ) {
244 return untrailingslashit( HUM_SHORTLINK_BASE );
245 }
246 return $url;
247 }
248
249 /**
250 * Get the shortlink for a post, page, attachment, or blog.
251 *
252 * @param string $link the current shortlink for the post
253 * @param int $id post ID
254 * @param string $context
255 * @param boolean $allow_slugs
256 * @return string
257 */
258 public function get_shortlink( $link, $id, $context, $allow_slugs ) {
259 $post_id = 0;
260 if ( 'query' === $context ) {
261 if ( is_front_page() ) {
262 $link = trailingslashit( $this->shortlink_base() );
263 } elseif ( is_singular() ) {
264 $post_id = get_queried_object_id();
265 }
266 } elseif ( 'post' === $context ) {
267 $post = get_post( $id );
268 $post_id = $post->ID;
269 }
270
271 if ( ! empty( $post_id ) ) {
272 $type = $this->type_prefix( $post_id );
273 $sxg_id = num_to_sxg( $post_id );
274 $link = trailingslashit( $this->shortlink_base() ) . $type . '/' . $sxg_id;
275 }
276
277 return $link;
278 }
279
280 /**
281 * Get the content-type prefix for the specified post.
282 *
283 * @see http://ttk.me/w/Whistle#design
284 * @uses apply_filters() Calls 'hum_type_prefix' on the content type prefix
285 *
286 * @param int|object $post A post
287 * @return string the content type prefix for the post
288 */
289 public function type_prefix( $post ) {
290 $prefix = 'b';
291
292 $post_type = get_post_type( $post );
293
294 if ( 'attachment' === $post_type ) {
295 // check if $post is a WP_Post or an ID
296 if ( is_numeric( $post ) ) {
297 $post_id = $post;
298 } else {
299 $post_id = $post->ID;
300 }
301
302 $mime_type = get_post_mime_type( $post_id );
303 $media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
304
305 switch ( $media_type ) {
306 case 'audio':
307 case 'video':
308 $prefix = 'a';
309 break;
310 case 'image':
311 $prefix = 'p';
312 break;
313 }
314
315 // @todo add support for slides
316 } else {
317 $post_format = get_post_format( $post );
318 switch ( $post_format ) {
319 case 'aside':
320 case 'status':
321 case 'link':
322 $prefix = 't';
323 break;
324 case 'audio':
325 case 'video':
326 $prefix = 'a';
327 break;
328 case 'photo':
329 case 'gallery':
330 case 'image':
331 $prefix = 'p';
332 break;
333 }
334 }
335
336 return apply_filters( 'hum_type_prefix', $prefix, $post );
337 }
338
339 /**
340 * Support redirects from legacy short URL schemes. This allows users to migrate from other
341 * shortlink generaters, but still have hum support the old URLs.
342 *
343 * @uses do_action() Calls 'hum_legacy_id' with the post ID and shortlink path.
344 */
345 public function legacy_redirect() {
346 if ( is_404() ) {
347 global $wp;
348 $post_id = apply_filters( 'hum_legacy_id', 0, $wp->request );
349 if ( $post_id ) {
350 $url = get_permalink( $post_id );
351 if ( $url ) {
352 $url = apply_filters( 'hum_legacy_redirect', $url );
353 wp_redirect( $url, 301 );
354 exit;
355 }
356 }
357 }
358 }
359
360 /**
361 * Handle shortlinks generated by Friendly Twitter Links, which take the form
362 * /{id}, where {id} can be the base10 or base32 post ID.
363 *
364 * @param int $id post ID to filter on
365 * @param string $path URL path (without preceding slash) of the request
366 *
367 * @return string ID of post to redirect to
368 */
369 public function legacy_ftl_id( $id, $path ) {
370 if ( is_numeric( $path ) ) {
371 $post = get_post( $path );
372 } else {
373 $post_id = base_convert( preg_replace( '/[^0-9a-fA-F]/', '', $path ), 32, 10 );
374 $post = get_post( $post_id );
375 }
376
377 if ( $post ) {
378 $id = $post->ID;
379 }
380
381 return $id;
382 }
383
384
385 // Admin Settings
386
387 /**
388 * Register admin settings for Hum.
389 */
390 public function admin_init() {
391 register_setting( 'general', 'hum_shortlink_base' );
392 }
393
394 /**
395 * Add admin settings fields for Hum.
396 */
397 public function admin_menu() {
398 add_settings_field( 'hum_shortlink_base', __( 'Shortlink Base (URL)', 'hum' ), array( $this, 'admin_shortlink_base' ), 'general' );
399 }
400
401 /**
402 * Admin UI for setting the shortlink base URL.
403 */
404 public function admin_shortlink_base() {
405 ?>
406 <input name="hum_shortlink_base" type="text" id="hum_shortlink_base"
407 value="<?php form_option( 'hum_shortlink_base' ); ?>"
408 <?php disabled( defined( 'HUM_SHORTLINK_BASE' ) ); ?>
409 class="regular-text code<?php if ( defined( 'HUM_SHORTLINK_BASE' ) ) { echo ' disabled'; } ?>" />
410 <p class="description">
411 <?php _e( 'If you have a custom domain you want to use for shortlinks, enter the address here.', 'hum' ); ?>
412 </p>
413
414 <script type="text/javascript">
415 // move adjacent to other URL properties
416 jQuery('input#hum_shortlink_base').parents('tr').insertAfter( jQuery('input#home').parents('tr') );
417 </script>
418 <?php
419 }
420
421 /**
422 * Add shortlink <link /> to Atom-Entry.
423 */
424 public function shortlink_atom_entry() {
425 $shortlink = wp_get_shortlink();
426 if ( $shortlink ) {
427 echo "\t\t" . '<link rel="shortlink" href="' . esc_attr( $shortlink ) . '" />' . PHP_EOL;
428 }
429 }
430
431 /**
432 * Show shortlink column.
433 *
434 * @param array $columns The list of columns.
435 */
436 public function add_post_column( $columns ) {
437 $reorderes_columns = array();
438 foreach ( $columns as $key => $value ) {
439 if ( 'date' === $key ) {
440 $reorderes_columns['shortlink'] = esc_html__( 'Shortlink', 'hum' );
441 }
442 $reorderes_columns[ $key ] = $value;
443 }
444
445 return $reorderes_columns;
446 }
447
448 /**
449 * Generate shortlink column.
450 *
451 * @param string $column_name The culumn name.
452 * @param string $post_id The post id.
453 */
454 public function add_posts_custom_column( $column_name, $post_id ) {
455 if ( 'shortlink' === $column_name ) {
456 printf( '<small>%s</small>', wp_get_shortlink( $post_id ) );
457 }
458 }
459 }
460
461 new Hum;
462
463
464 // New Base 60 - see http://ttk.me/w/NewBase60
465 //
466 // slightly modified from Cassis Project (http://cassisproject.com/)
467 // Copyright 2010 Tantek Çelik, used with permission under CC0 license (http://git.io/tZ8fjw)
468 //
469 // @codingStandardsIgnoreStart
470 if ( ! function_exists( 'num_to_sxg' ) ) :
471 /**
472 * Convert base-10 number to sexagesimal.
473 */
474 function num_to_sxg($n) {
475 $s = "";
476 $m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
477 if ($n===null || $n===0) { return 0; }
478 while ($n>0) {
479 $d = $n % 60;
480 $s = $m[$d] . $s;
481 $n = ($n-$d)/60;
482 }
483 return $s;
484 }
485 endif;
486
487
488 if ( ! function_exists( 'sxg_to_num' ) ) :
489 /**
490 * Convert sexagesimal to base-10 number.
491 */
492 function sxg_to_num( $s ) {
493 $n = 0;
494 $j = strlen($s);
495 for ($i=0;$i<$j;$i++) { // iterate from first to last char of $s
496 $c = ord($s[$i]); // put current ASCII of char into $c
497 if ($c>=48 && $c<=57) { $c=$c-48; }
498 else if ($c>=65 && $c<=72) { $c-=55; }
499 else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
500 else if ($c>=74 && $c<=78) { $c-=56; }
501 else if ($c==79) { $c=0; } // error correct typo capital O to 0
502 else if ($c>=80 && $c<=90) { $c-=57; }
503 else if ($c==95) { $c=34; } // underscore
504 else if ($c>=97 && $c<=107) { $c-=62; }
505 else if ($c>=109 && $c<=122) { $c-=63; }
506 else { $c = 0; } // treat all other noise as 0
507 $n = 60*$n + $c;
508 }
509 return $n;
510 }
511 endif;
512 // @codingStandardsIgnoreEnd
513