PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.9.4
Jetpack – WP Security, Backup, Speed, & Growth v8.9.4
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 14.4.2 All 500 releases
jetpack / modules / shortlinks.php
shortlinks.php
141 lines 3.2 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: Generates shorter links using the wp.me domain.
5 * Sort Order: 8
6 * First Introduced: 1.1
7 * Requires Connection: Yes
8 * Auto Activate: No
9 * Module Tags: Social
10 * Feature: Writing
11 * Additional Search Queries: shortlinks, wp.me
12 */
13
14 add_filter( 'pre_get_shortlink', 'wpme_get_shortlink_handler', 1, 4 );
15
16 if ( !function_exists( 'wpme_dec2sixtwo' ) ) {
17 function wpme_dec2sixtwo( $num ) {
18 $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
19 $out = "";
20
21 if ( $num < 0 ) {
22 $out = '-';
23 $num = abs( $num );
24 }
25
26 for ( $t = floor( log10( $num ) / log10( 62 ) ); $t >= 0; $t-- ) {
27 $a = floor( $num / pow( 62, $t ) );
28 $out = $out . substr( $index, $a, 1 );
29 $num = $num - ( $a * pow( 62, $t ) );
30 }
31
32 return $out;
33 }
34 }
35
36 function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
37 global $wp_query;
38
39 $blog_id = Jetpack_Options::get_option( 'id' );
40
41 if ( 'query' == $context ) {
42 if ( is_singular() ) {
43 $id = $wp_query->get_queried_object_id();
44 $context = 'post';
45 } elseif ( is_front_page() ) {
46 $context = 'blog';
47 } else {
48 return '';
49 }
50 }
51
52 if ( 'blog' == $context ) {
53 if ( empty( $id ) )
54 $id = $blog_id;
55
56 return 'https://wp.me/' . wpme_dec2sixtwo( $id );
57 }
58
59 $post = get_post( $id );
60
61 if ( empty( $post ) )
62 return '';
63
64 $post_id = $post->ID;
65 $type = '';
66
67 if ( $allow_slugs && 'publish' == $post->post_status && 'post' == $post->post_type && strlen( $post->post_name ) <= 8 && false === strpos( $post->post_name, '%' )
68 && false === strpos( $post->post_name, '-' ) ) {
69 $id = $post->post_name;
70 $type = 's';
71 } else {
72 $id = wpme_dec2sixtwo( $post_id );
73 if ( 'page' == $post->post_type )
74 $type = 'P';
75 elseif ( 'post' == $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) )
76 $type= 'p';
77 elseif ( 'attachment' == $post->post_type )
78 $type = 'a';
79 }
80
81 if ( empty( $type ) )
82 return '';
83
84 return 'https://wp.me/' . $type . wpme_dec2sixtwo( $blog_id ) . '-' . $id;
85 }
86
87 function wpme_get_shortlink_handler( $shortlink, $id, $context, $allow_slugs ) {
88 return wpme_get_shortlink( $id, $context, $allow_slugs );
89 }
90
91 /**
92 * Add Shortlinks to the REST API responses.
93 *
94 * @since 6.9.0
95 *
96 * @action rest_api_init
97 * @uses register_rest_field, wpme_rest_get_shortlink
98 */
99 function wpme_rest_register_shortlinks() {
100 register_rest_field(
101 array(
102 'attachment',
103 'page',
104 'post',
105 ),
106 'jetpack_shortlink',
107 array(
108 'get_callback' => 'wpme_rest_get_shortlink',
109 'update_callback' => null,
110 'schema' => null,
111 )
112 );
113 }
114
115 /**
116 * Get the shortlink of a post.
117 *
118 * @since 6.9.0
119 *
120 * @param array $object Details of current post.
121 *
122 * @uses wpme_get_shortlink
123 *
124 * @return string
125 */
126 function wpme_rest_get_shortlink( $object ) {
127 return wpme_get_shortlink( $object['id'], array() );
128 }
129
130 // Add shortlinks to the REST API Post response.
131 add_action( 'rest_api_init', 'wpme_rest_register_shortlinks' );
132
133 /**
134 * Set the Shortlink Gutenberg extension as available.
135 */
136 function wpme_set_extension_available() {
137 Jetpack_Gutenberg::set_extension_available( 'jetpack/shortlinks' );
138 }
139
140 add_action( 'init', 'wpme_set_extension_available' );
141