PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.1.5
Jetpack – WP Security, Backup, Speed, & Growth v3.1.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortlinks.php

shortlinks.php in Jetpack – WP Security, Backup, Speed, & Growth 3.1.5, at modules/shortlinks.php

87 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Name: WP.me Shortlinks
4 * Module Description: Enable WP.me-powered shortlinks for all posts and pages.
5 * Sort Order: 8
6 * First Introduced: 1.1
7 * Requires Connection: Yes
8 * Auto Activate: Yes
9 * Module Tags: Social
10 */
11
12 add_filter( 'get_shortlink', 'wpme_get_shortlink_handler', 1, 4 );
13
14 if ( !function_exists( 'wpme_dec2sixtwo' ) ) {
15 function wpme_dec2sixtwo( $num ) {
16 $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
17 $out = "";
18
19 if ( $num < 0 ) {
20 $out = '-';
21 $num = abs( $num );
22 }
23
24 for ( $t = floor( log10( $num ) / log10( 62 ) ); $t >= 0; $t-- ) {
25 $a = floor( $num / pow( 62, $t ) );
26 $out = $out . substr( $index, $a, 1 );
27 $num = $num - ( $a * pow( 62, $t ) );
28 }
29
30 return $out;
31 }
32 }
33
34 function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
35 global $wp_query;
36
37 $blog_id = Jetpack_Options::get_option( 'id' );
38
39 if ( 'query' == $context ) {
40 if ( is_singular() ) {
41 $id = $wp_query->get_queried_object_id();
42 $context = 'post';
43 } elseif ( is_front_page() ) {
44 $context = 'blog';
45 } else {
46 return '';
47 }
48 }
49
50 if ( 'blog' == $context ) {
51 if ( empty( $id ) )
52 $id = $blog_id;
53 return 'http://wp.me/' . wpme_dec2sixtwo( $id );
54 }
55
56 $post = get_post( $id );
57
58 if ( empty( $post ) )
59 return '';
60
61 $post_id = $post->ID;
62 $type = '';
63
64 if ( $allow_slugs && 'publish' == $post->post_status && 'post' == $post->post_type && strlen( $post->post_name ) <= 8 && false === strpos( $post->post_name, '%' )
65 && false === strpos( $post->post_name, '-' ) ) {
66 $id = $post->post_name;
67 $type = 's';
68 } else {
69 $id = wpme_dec2sixtwo( $post_id );
70 if ( 'page' == $post->post_type )
71 $type = 'P';
72 elseif ( 'post' == $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) )
73 $type= 'p';
74 elseif ( 'attachment' == $post->post_type )
75 $type = 'a';
76 }
77
78 if ( empty( $type ) )
79 return '';
80
81 return 'http://wp.me/' . $type . wpme_dec2sixtwo( $blog_id ) . '-' . $id;
82 }
83
84 function wpme_get_shortlink_handler( $shortlink, $id, $context, $allow_slugs ) {
85 return wpme_get_shortlink( $id, $context, $allow_slugs );
86 }
87