diff options
Diffstat (limited to 'plugins/jetpack/modules/shortlinks.php')
-rw-r--r-- | plugins/jetpack/modules/shortlinks.php | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/plugins/jetpack/modules/shortlinks.php b/plugins/jetpack/modules/shortlinks.php index a34ed3c8..989271d7 100644 --- a/plugins/jetpack/modules/shortlinks.php +++ b/plugins/jetpack/modules/shortlinks.php @@ -1,7 +1,7 @@ <?php /** * Module Name: WP.me Shortlinks - * Module Description: Generates shorter links so you can have more space to write on social media sites. + * Module Description: Generates shorter links using the wp.me domain. * Sort Order: 8 * First Introduced: 1.1 * Requires Connection: Yes @@ -9,14 +9,23 @@ * Module Tags: Social * Feature: Writing * Additional Search Queries: shortlinks, wp.me + * + * @package automattic/jetpack */ add_filter( 'pre_get_shortlink', 'wpme_get_shortlink_handler', 1, 4 ); -if ( !function_exists( 'wpme_dec2sixtwo' ) ) { +if ( ! function_exists( 'wpme_dec2sixtwo' ) ) { + /** + * Converts number to base 62. + * + * @param int $num Number. + * + * @return string Value in base 62. + */ function wpme_dec2sixtwo( $num ) { $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - $out = ""; + $out = ''; if ( $num < 0 ) { $out = '-'; @@ -24,7 +33,7 @@ if ( !function_exists( 'wpme_dec2sixtwo' ) ) { } for ( $t = floor( log10( $num ) / log10( 62 ) ); $t >= 0; $t-- ) { - $a = floor( $num / pow( 62, $t ) ); + $a = floor( $num / pow( 62, $t ) ); $out = $out . substr( $index, $a, 1 ); $num = $num - ( $a * pow( 62, $t ) ); } @@ -33,14 +42,23 @@ if ( !function_exists( 'wpme_dec2sixtwo' ) ) { } } +/** + * Returns the WP.me shortlink. + * + * @param int $id Post ID, or 0 for the current post. + * @param string $context The context for the link. One of 'post' or 'query'. + * @param bool $allow_slugs Whether to allow post slugs in the shortlink. + * + * @return string + */ function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { global $wp_query; $blog_id = Jetpack_Options::get_option( 'id' ); - if ( 'query' == $context ) { + if ( 'query' === $context ) { if ( is_singular() ) { - $id = $wp_query->get_queried_object_id(); + $id = $wp_query->get_queried_object_id(); $context = 'post'; } elseif ( is_front_page() ) { $context = 'blog'; @@ -49,41 +67,57 @@ function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { } } - if ( 'blog' == $context ) { - if ( empty( $id ) ) + if ( 'blog' === $context ) { + if ( empty( $id ) ) { $id = $blog_id; + } return 'https://wp.me/' . wpme_dec2sixtwo( $id ); } $post = get_post( $id ); - if ( empty( $post ) ) + if ( empty( $post ) ) { return ''; + } $post_id = $post->ID; - $type = ''; + $type = ''; - if ( $allow_slugs && 'publish' == $post->post_status && 'post' == $post->post_type && strlen( $post->post_name ) <= 8 && false === strpos( $post->post_name, '%' ) + if ( $allow_slugs && 'publish' === $post->post_status && 'post' === $post->post_type && strlen( $post->post_name ) <= 8 && false === strpos( $post->post_name, '%' ) && false === strpos( $post->post_name, '-' ) ) { - $id = $post->post_name; + $id = $post->post_name; $type = 's'; } else { $id = wpme_dec2sixtwo( $post_id ); - if ( 'page' == $post->post_type ) + if ( 'page' === $post->post_type ) { $type = 'P'; - elseif ( 'post' == $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) ) - $type= 'p'; - elseif ( 'attachment' == $post->post_type ) + } elseif ( 'post' === $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) ) { + $type = 'p'; + } elseif ( 'attachment' === $post->post_type ) { $type = 'a'; + } } - if ( empty( $type ) ) + if ( empty( $type ) ) { return ''; + } return 'https://wp.me/' . $type . wpme_dec2sixtwo( $blog_id ) . '-' . $id; } +/** + * Get the shortlink handler. + * + * Used with the Core pre_get_shortlink hook. + * + * @param string $shortlink Shortlink value from the action. Ignored. + * @param int $id Post ID (0 for the current post). + * @param string $context The context for the link. One of 'post' or 'query'. + * @param bool $allow_slugs Whether to allow post slugs in the shortlink. + * + * @return string + */ function wpme_get_shortlink_handler( $shortlink, $id, $context, $allow_slugs ) { return wpme_get_shortlink( $id, $context, $allow_slugs ); } |