kollapsminoriteten/wp-content/plugins/jetpack/modules/shortlinks.php

193 lines
4.5 KiB
PHP
Raw Normal View History

2019-11-15 23:26:29 +01:00
<?php
/**
* Module Name: WP.me Shortlinks
2025-07-27 19:58:08 +02:00
* Module Description: Share short, easy-to-remember links to your posts and pages.
2019-11-15 23:26:29 +01:00
* Sort Order: 8
* First Introduced: 1.1
* Requires Connection: Yes
* Auto Activate: No
* Module Tags: Social
* Feature: Writing
* Additional Search Queries: shortlinks, wp.me
2022-04-02 10:26:41 +02:00
*
* @package automattic/jetpack
2019-11-15 23:26:29 +01:00
*/
2025-12-12 13:13:07 +01:00
if ( ! defined( 'ABSPATH' ) ) {
exit( 0 );
}
2019-11-15 23:26:29 +01:00
add_filter( 'pre_get_shortlink', 'wpme_get_shortlink_handler', 1, 4 );
2022-04-02 10:26:41 +02:00
if ( ! function_exists( 'wpme_dec2sixtwo' ) ) {
/**
* Converts number to base 62.
*
* @param int $num Number.
*
* @return string Value in base 62.
*/
2019-11-15 23:26:29 +01:00
function wpme_dec2sixtwo( $num ) {
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2022-04-02 10:26:41 +02:00
$out = '';
2019-11-15 23:26:29 +01:00
if ( $num < 0 ) {
$out = '-';
$num = abs( $num );
}
for ( $t = floor( log10( $num ) / log10( 62 ) ); $t >= 0; $t-- ) {
2025-12-12 13:13:07 +01:00
$a = (int) floor( $num / pow( 62, $t ) );
2019-11-15 23:26:29 +01:00
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( 62, $t ) );
}
return $out;
}
}
2022-04-02 10:26:41 +02:00
/**
* 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
*/
2019-11-15 23:26:29 +01:00
function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
global $wp_query;
$blog_id = Jetpack_Options::get_option( 'id' );
2022-04-02 10:26:41 +02:00
if ( 'query' === $context ) {
2019-11-15 23:26:29 +01:00
if ( is_singular() ) {
2022-04-02 10:26:41 +02:00
$id = $wp_query->get_queried_object_id();
2019-11-15 23:26:29 +01:00
$context = 'post';
} elseif ( is_front_page() ) {
$context = 'blog';
} else {
return '';
}
}
2022-04-02 10:26:41 +02:00
if ( 'blog' === $context ) {
if ( empty( $id ) ) {
2019-11-15 23:26:29 +01:00
$id = $blog_id;
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
return 'https://wp.me/' . wpme_dec2sixtwo( $id );
}
$post = get_post( $id );
2022-04-02 10:26:41 +02:00
if ( empty( $post ) ) {
2019-11-15 23:26:29 +01:00
return '';
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
$post_id = $post->ID;
2022-04-02 10:26:41 +02:00
$type = '';
2019-11-15 23:26:29 +01:00
2023-12-07 09:44:11 +01:00
if ( $allow_slugs && 'publish' === $post->post_status && 'post' === $post->post_type && strlen( $post->post_name ) <= 8 && ! str_contains( $post->post_name, '%' )
&& ! str_contains( $post->post_name, '-' ) ) {
2022-04-02 10:26:41 +02:00
$id = $post->post_name;
2019-11-15 23:26:29 +01:00
$type = 's';
} else {
$id = wpme_dec2sixtwo( $post_id );
2022-04-02 10:26:41 +02:00
if ( 'page' === $post->post_type ) {
2019-11-15 23:26:29 +01:00
$type = 'P';
2022-04-02 10:26:41 +02:00
} elseif ( 'post' === $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) ) {
$type = 'p';
} elseif ( 'attachment' === $post->post_type ) {
2019-11-15 23:26:29 +01:00
$type = 'a';
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
}
2022-04-02 10:26:41 +02:00
if ( empty( $type ) ) {
2019-11-15 23:26:29 +01:00
return '';
2022-04-02 10:26:41 +02:00
}
2019-11-15 23:26:29 +01:00
return 'https://wp.me/' . $type . wpme_dec2sixtwo( $blog_id ) . '-' . $id;
}
2022-04-02 10:26:41 +02:00
/**
* 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
*/
2019-11-15 23:26:29 +01:00
function wpme_get_shortlink_handler( $shortlink, $id, $context, $allow_slugs ) {
return wpme_get_shortlink( $id, $context, $allow_slugs );
}
/**
* Add Shortlinks to the REST API responses.
*
* @since 6.9.0
*
* @action rest_api_init
* @uses register_rest_field, wpme_rest_get_shortlink
*/
function wpme_rest_register_shortlinks() {
2022-06-16 14:01:47 +02:00
// Post types that support shortlinks by default.
$supported_post_types = array(
'attachment',
'page',
'post',
);
// Add any CPT that may have declared support for shortlinks.
foreach ( get_post_types() as $post_type ) {
if (
post_type_supports( $post_type, 'shortlinks' )
&& post_type_supports( $post_type, 'editor' )
) {
$supported_post_types[] = $post_type;
}
}
2019-11-15 23:26:29 +01:00
register_rest_field(
2022-06-16 14:01:47 +02:00
$supported_post_types,
2019-11-15 23:26:29 +01:00
'jetpack_shortlink',
array(
'get_callback' => 'wpme_rest_get_shortlink',
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Get the shortlink of a post.
*
* @since 6.9.0
*
* @param array $object Details of current post.
*
* @uses wpme_get_shortlink
*
* @return string
*/
function wpme_rest_get_shortlink( $object ) {
2024-04-17 11:32:24 +02:00
$object_id = $object['id'] ?? 0;
return wpme_get_shortlink( $object_id, array() );
2019-11-15 23:26:29 +01:00
}
// Add shortlinks to the REST API Post response.
add_action( 'rest_api_init', 'wpme_rest_register_shortlinks' );
/**
* Set the Shortlink Gutenberg extension as available.
*/
function wpme_set_extension_available() {
2025-02-28 08:42:11 +01:00
Jetpack_Gutenberg::set_extension_available( 'shortlinks' );
2019-11-15 23:26:29 +01:00
}
add_action( 'init', 'wpme_set_extension_available' );