PluginProbe
Hum / 1.2.8
Hum v1.2.8
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.8, at hum.php

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