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

570 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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.3.3
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' ), 15 );
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( __DIR__ ) );
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 * Get the short URL types that shoud be redirected (types can be the same as local types).
163 *
164 * @uses apply_filters() Calls 'hum_redirect_types' with array of redirect types
165 *
166 * @return array redirect types
167 */
168 public function redirect_types() {
169 $redirect_types = array( 'i' );
170 return apply_filters( 'hum_redirect_types', $redirect_types );
171 }
172
173 /**
174 * Attempt to handle redirect for the current shortlink.
175 *
176 * This redirects shortlinks that are for content hosted directly within
177 * WordPress. The 'id' portion of these URLs is expected to be the
178 * sexagesimal post ID.
179 *
180 * This also allows for simple redirect rules for shortlink prefixes. Users
181 * can provide a filter to perform simple URL redirect for a given type
182 * prefix. For example, to redirect all /w/ shortlinks to your personal
183 * PBworks wiki, you could use:
184 *
185 * add_filter('hum_redirect_base_w', fn() => "http://willnorris.pbworks.com/");
186 *
187 * @uses apply_filters() Calls 'hum_redirect_{$type}' action
188 * @uses apply_filters() Calls 'hum_redirect_base_{$type}' filter on redirect base URL
189 *
190 * @param string $url the short URL
191 * @param string $type the content-type prefix
192 * @param string $id the requested post ID
193 */
194 public function redirect_request( $url, $type, $id ) {
195 // locally hosted content
196 $local_types = $this->local_types();
197 if ( in_array( $type, $local_types, true ) ) {
198 $p = sxg_to_num( $id );
199 if ( $p ) {
200 $url = get_permalink( $p );
201 }
202 }
203
204 // simple redirects for entire base type
205 if ( ! $url ) {
206 $url = apply_filters( "hum_redirect_base_{$type}", false );
207 if ( $url ) {
208 $url = trailingslashit( $url ) . $id;
209 }
210 }
211
212 $url = apply_filters( "hum_redirect_{$type}", $url, $id );
213 return $url;
214 }
215
216 /**
217 * Handles /i/ URLs that have ISBN or ASIN subpaths by redirecting to Amazon.
218 *
219 * @uses apply_filters() Calls 'hum_redirect_i_{$subtype}' action
220 * @uses apply_filters() Calls 'amazon_domain' filter
221 * @uses apply_filters() Calls 'amazon_affiliate_id' filter
222 *
223 * @param string $url the short URL
224 * @param string $path subpath of URL (after /i/)
225 */
226 public function redirect_request_i( $url, $path ) {
227 list( $subtype, $id ) = explode( '/', $path, 2 );
228
229 if ( $subtype ) {
230 switch ( $subtype ) {
231 case 'a':
232 case 'asin':
233 case 'i':
234 case 'isbn':
235 $amazon_domain = apply_filters( 'amazon_domain', 'www.amazon.com' );
236 $amazon_id = apply_filters( 'amazon_affiliate_id', false );
237 if ( $amazon_id ) {
238 // valid partner shortlink, checked by
239 // https://partnernet.amazon.de/gp/associates/network/tools/link-checker/main.html
240 $url = 'http://' . $amazon_domain . '/dp/product/' . $id . '?tag=' . $amazon_id;
241 } else {
242 $url = 'http://' . $amazon_domain . '/dp/product/' . $id;
243 }
244 break;
245 }
246 $url = apply_filters( "hum_redirect_i_{$subtype}", $url, $id );
247 }
248 return $url;
249 }
250
251 /**
252 * Add rewrite rules for hum shortlinks.
253 */
254 public function rewrite_rules() {
255 $local_types = $this->local_types();
256 $redirect_types = $this->redirect_types();
257
258 $types = array_merge( $local_types, $redirect_types );
259 $types = implode( '', array_unique( $types ) );
260
261 add_rewrite_rule( "([{$types}](\/.*)?$)", 'index.php?hum=$matches[1]', 'top' );
262 }
263
264 /**
265 * Add rewrite rules for hum shortlinks.
266 */
267 public function flush_rewrite_rules() {
268 $this->rewrite_rules();
269 flush_rewrite_rules();
270 }
271
272 /**
273 * Get the base URL for hum shortlinks. Defaults to the WordPress home url.
274 * Users can define HUM_SHORTLINK_BASE or provide a filter to use a custom
275 * domain for shortlinks.
276 *
277 * @uses apply_filters() Calls 'hum_shortlink_base' filter on base URL.
278 *
279 * @return string
280 */
281 public function shortlink_base() {
282 $base = get_option( 'hum_shortlink_base' );
283 if ( empty( $base ) ) {
284 $base = home_url();
285 }
286 return apply_filters( 'hum_shortlink_base', $base );
287 }
288
289 /**
290 * Allow the constant named 'HUM_SHORTLINK_BASE' to override the base URL for shortlinks.
291 *
292 * @param string $url The short URL.
293 */
294 public function config_shortlink_base( $url = '' ) {
295 if ( defined( 'HUM_SHORTLINK_BASE' ) ) {
296 return untrailingslashit( HUM_SHORTLINK_BASE );
297 }
298 return $url;
299 }
300
301 /**
302 * Get the shortlink for a post, page, attachment, or blog.
303 *
304 * @param int $id A post or site ID. Default is 0, which means the current post or site.
305 * @param string $context Whether the ID is a 'site' ID, 'post' ID, or 'media' ID. If 'post',
306 * the post_type of the post is consulted. If 'query', the current query is consulted
307 * to determine the ID and context. Default 'post'.
308 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how
309 * and whether to honor this. Default true.
310 * @return string
311 */
312 public function get_shortlink( $link, $id, $context, $allow_slugs ) {
313 $post_id = 0;
314 if ( 'query' === $context ) {
315 if ( is_front_page() ) {
316 $link = trailingslashit( $this->shortlink_base() );
317 } elseif ( is_singular() ) {
318 $post_id = get_queried_object_id();
319 }
320 } elseif ( 'post' === $context && (int) $id > 0 ) {
321 $post = get_post( $id );
322
323 if ( $post instanceof WP_Post ) {
324 $post_id = $post->ID;
325 }
326 }
327
328 if ( ! empty( $post_id ) ) {
329 $type = $this->type_prefix( $post_id );
330 $sxg_id = num_to_sxg( $post_id );
331 $link = trailingslashit( $this->shortlink_base() ) . $type . '/' . $sxg_id;
332 }
333
334 return $link;
335 }
336
337 /**
338 * Get the content-type prefix for the specified post.
339 *
340 * @see http://ttk.me/w/Whistle#design
341 * @uses apply_filters() Calls 'hum_type_prefix' on the content type prefix.
342 *
343 * @param int|object $post A post
344 * @return string The content type prefix for the post.
345 */
346 public function type_prefix( $post ) {
347 $prefix = 'b';
348
349 $post_type = get_post_type( $post );
350
351 if ( 'attachment' === $post_type ) {
352 // check if $post is a WP_Post or an ID
353 if ( is_numeric( $post ) ) {
354 $post_id = $post;
355 } else {
356 $post_id = $post->ID;
357 }
358
359 $mime_type = get_post_mime_type( $post_id );
360 $media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
361
362 switch ( $media_type ) {
363 case 'audio':
364 case 'video':
365 $prefix = 'a';
366 break;
367 case 'image':
368 $prefix = 'p';
369 break;
370 }
371
372 // @todo add support for slides
373 } else {
374 $post_format = get_post_format( $post );
375 switch ( $post_format ) {
376 case 'aside':
377 case 'status':
378 case 'link':
379 $prefix = 't';
380 break;
381 case 'audio':
382 case 'video':
383 $prefix = 'a';
384 break;
385 case 'photo':
386 case 'gallery':
387 case 'image':
388 $prefix = 'p';
389 break;
390 }
391 }
392
393 return apply_filters( 'hum_type_prefix', $prefix, $post );
394 }
395
396 /**
397 * Support redirects from legacy short URL schemes. This allows users to migrate from other
398 * shortlink generaters, but still have hum support the old URLs.
399 *
400 * @uses do_action() Calls 'hum_legacy_id' with the post ID and shortlink path.
401 */
402 public function legacy_redirect() {
403 if ( is_404() ) {
404 global $wp;
405 $post_id = apply_filters( 'hum_legacy_id', 0, $wp->request );
406 if ( $post_id ) {
407 $url = get_permalink( $post_id );
408 if ( $url ) {
409 $url = apply_filters( 'hum_legacy_redirect', $url );
410 wp_redirect( $url, 301 );
411 exit;
412 }
413 }
414 }
415 }
416
417 /**
418 * Handle shortlinks generated by Friendly Twitter Links, which take the form
419 * /{id}, where {id} can be the base10 or base32 post ID.
420 *
421 * @param int $id post ID to filter on.
422 * @param string $path URL path (without preceding slash) of the request.
423 *
424 * @return string ID of post to redirect to.
425 */
426 public function legacy_ftl_id( $id, $path ) {
427 if ( is_numeric( $path ) ) {
428 $post = get_post( $path );
429 } else {
430 $post_id = base_convert( preg_replace( '/[^0-9a-fA-F]/', '', $path ), 32, 10 );
431 $post = get_post( $post_id );
432 }
433
434 if ( $post ) {
435 $id = $post->ID;
436 }
437
438 return $id;
439 }
440
441
442 // Admin Settings
443
444 /**
445 * Register admin settings for Hum.
446 */
447 public function admin_init() {
448 register_setting( 'general', 'hum_shortlink_base' );
449 }
450
451 /**
452 * Add admin settings fields for Hum.
453 */
454 public function admin_menu() {
455 add_settings_field( 'hum_shortlink_base', __( 'Shortlink Base (URL)', 'hum' ), array( $this, 'admin_shortlink_base' ), 'general' );
456 }
457
458 /**
459 * Admin UI for setting the shortlink base URL.
460 */
461 public function admin_shortlink_base() {
462 ?>
463 <input name="hum_shortlink_base" type="text" id="hum_shortlink_base"
464 value="<?php form_option( 'hum_shortlink_base' ); ?>"
465 <?php disabled( defined( 'HUM_SHORTLINK_BASE' ) ); ?>
466 class="regular-text code<?php if ( defined( 'HUM_SHORTLINK_BASE' ) ) { echo ' disabled'; } // phpcs:ignore Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace ?>" />
467 <p class="description">
468 <?php _e( 'If you have a custom domain you want to use for shortlinks, enter the address here.', 'hum' ); ?>
469 </p>
470
471 <script type="text/javascript">
472 // move adjacent to other URL properties
473 jQuery('input#hum_shortlink_base').parents('tr').insertAfter( jQuery('input#home').parents('tr') );
474 </script>
475 <?php
476 }
477
478 /**
479 * Add shortlink <link /> to Atom-Entry.
480 */
481 public function shortlink_atom_entry() {
482 $shortlink = wp_get_shortlink();
483 if ( $shortlink ) {
484 echo "\t\t" . '<link rel="shortlink" href="' . esc_attr( $shortlink ) . '" />' . PHP_EOL;
485 }
486 }
487
488 /**
489 * Show shortlink column.
490 *
491 * @param array $columns The list of columns.
492 */
493 public function add_post_column( $columns ) {
494 $reorderes_columns = array();
495 foreach ( $columns as $key => $value ) {
496 if ( 'date' === $key ) {
497 $reorderes_columns['shortlink'] = esc_html__( 'Shortlink', 'hum' );
498 }
499 $reorderes_columns[ $key ] = $value;
500 }
501
502 return $reorderes_columns;
503 }
504
505 /**
506 * Generate shortlink column.
507 *
508 * @param string $column_name The culumn name.
509 * @param string $post_id The post id.
510 */
511 public function add_posts_custom_column( $column_name, $post_id ) {
512 if ( 'shortlink' === $column_name ) {
513 printf( '<small>%s</small>', wp_get_shortlink( $post_id ) );
514 }
515 }
516 }
517
518 new Hum();
519
520
521 // New Base 60 - see http://ttk.me/w/NewBase60
522 //
523 // slightly modified from Cassis Project (http://cassisproject.com/)
524 // Copyright 2010 Tantek Çelik, used with permission under CC0 license (http://git.io/tZ8fjw)
525 //
526 // @codingStandardsIgnoreStart
527 if ( ! function_exists( 'num_to_sxg' ) ) :
528 /**
529 * Convert base-10 number to sexagesimal.
530 */
531 function num_to_sxg($n) {
532 $s = "";
533 $m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
534 if ($n===null || $n===0) { return 0; }
535 while ($n>0) {
536 $d = $n % 60;
537 $s = $m[$d] . $s;
538 $n = ($n-$d)/60;
539 }
540 return $s;
541 }
542 endif;
543
544
545 if ( ! function_exists( 'sxg_to_num' ) ) :
546 /**
547 * Convert sexagesimal to base-10 number.
548 */
549 function sxg_to_num( $s ) {
550 $n = 0;
551 $j = strlen($s);
552 for ($i=0;$i<$j;$i++) { // iterate from first to last char of $s
553 $c = ord($s[$i]); // put current ASCII of char into $c
554 if ($c>=48 && $c<=57) { $c=$c-48; }
555 else if ($c>=65 && $c<=72) { $c-=55; }
556 else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
557 else if ($c>=74 && $c<=78) { $c-=56; }
558 else if ($c==79) { $c=0; } // error correct typo capital O to 0
559 else if ($c>=80 && $c<=90) { $c-=57; }
560 else if ($c==95) { $c=34; } // underscore
561 else if ($c>=97 && $c<=107) { $c-=62; }
562 else if ($c>=109 && $c<=122) { $c-=63; }
563 else { $c = 0; } // treat all other noise as 0
564 $n = 60*$n + $c;
565 }
566 return $n;
567 }
568 endif;
569 // @codingStandardsIgnoreEnd
570