Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules
File: shortlinks.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Module Name: WP.me Shortlinks
[2] Fix | Delete
* Module Description: Share short, easy-to-remember links to your posts and pages.
[3] Fix | Delete
* Sort Order: 8
[4] Fix | Delete
* First Introduced: 1.1
[5] Fix | Delete
* Requires Connection: Yes
[6] Fix | Delete
* Auto Activate: No
[7] Fix | Delete
* Module Tags: Social
[8] Fix | Delete
* Feature: Writing
[9] Fix | Delete
* Additional Search Queries: shortlinks, wp.me
[10] Fix | Delete
*
[11] Fix | Delete
* @package automattic/jetpack
[12] Fix | Delete
*/
[13] Fix | Delete
[14] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[15] Fix | Delete
exit( 0 );
[16] Fix | Delete
}
[17] Fix | Delete
[18] Fix | Delete
add_filter( 'pre_get_shortlink', 'wpme_get_shortlink_handler', 1, 4 );
[19] Fix | Delete
[20] Fix | Delete
if ( ! function_exists( 'wpme_dec2sixtwo' ) ) {
[21] Fix | Delete
/**
[22] Fix | Delete
* Converts number to base 62.
[23] Fix | Delete
*
[24] Fix | Delete
* @param int $num Number.
[25] Fix | Delete
*
[26] Fix | Delete
* @return string Value in base 62.
[27] Fix | Delete
*/
[28] Fix | Delete
function wpme_dec2sixtwo( $num ) {
[29] Fix | Delete
$index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
[30] Fix | Delete
$out = '';
[31] Fix | Delete
[32] Fix | Delete
if ( $num < 0 ) {
[33] Fix | Delete
$out = '-';
[34] Fix | Delete
$num = abs( $num );
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
for ( $t = floor( log10( $num ) / log10( 62 ) ); $t >= 0; $t-- ) {
[38] Fix | Delete
$a = floor( $num / pow( 62, $t ) );
[39] Fix | Delete
$out = $out . substr( $index, $a, 1 );
[40] Fix | Delete
$num = $num - ( $a * pow( 62, $t ) );
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
return $out;
[44] Fix | Delete
}
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Returns the WP.me shortlink.
[49] Fix | Delete
*
[50] Fix | Delete
* @param int $id Post ID, or 0 for the current post.
[51] Fix | Delete
* @param string $context The context for the link. One of 'post' or 'query'.
[52] Fix | Delete
* @param bool $allow_slugs Whether to allow post slugs in the shortlink.
[53] Fix | Delete
*
[54] Fix | Delete
* @return string
[55] Fix | Delete
*/
[56] Fix | Delete
function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
[57] Fix | Delete
global $wp_query;
[58] Fix | Delete
[59] Fix | Delete
$blog_id = Jetpack_Options::get_option( 'id' );
[60] Fix | Delete
[61] Fix | Delete
if ( 'query' === $context ) {
[62] Fix | Delete
if ( is_singular() ) {
[63] Fix | Delete
$id = $wp_query->get_queried_object_id();
[64] Fix | Delete
$context = 'post';
[65] Fix | Delete
} elseif ( is_front_page() ) {
[66] Fix | Delete
$context = 'blog';
[67] Fix | Delete
} else {
[68] Fix | Delete
return '';
[69] Fix | Delete
}
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
if ( 'blog' === $context ) {
[73] Fix | Delete
if ( empty( $id ) ) {
[74] Fix | Delete
$id = $blog_id;
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
return 'https://wp.me/' . wpme_dec2sixtwo( $id );
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
$post = get_post( $id );
[81] Fix | Delete
[82] Fix | Delete
if ( empty( $post ) ) {
[83] Fix | Delete
return '';
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
$post_id = $post->ID;
[87] Fix | Delete
$type = '';
[88] Fix | Delete
[89] Fix | Delete
if ( $allow_slugs && 'publish' === $post->post_status && 'post' === $post->post_type && strlen( $post->post_name ) <= 8 && ! str_contains( $post->post_name, '%' )
[90] Fix | Delete
&& ! str_contains( $post->post_name, '-' ) ) {
[91] Fix | Delete
$id = $post->post_name;
[92] Fix | Delete
$type = 's';
[93] Fix | Delete
} else {
[94] Fix | Delete
$id = wpme_dec2sixtwo( $post_id );
[95] Fix | Delete
if ( 'page' === $post->post_type ) {
[96] Fix | Delete
$type = 'P';
[97] Fix | Delete
} elseif ( 'post' === $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) ) {
[98] Fix | Delete
$type = 'p';
[99] Fix | Delete
} elseif ( 'attachment' === $post->post_type ) {
[100] Fix | Delete
$type = 'a';
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
if ( empty( $type ) ) {
[105] Fix | Delete
return '';
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
return 'https://wp.me/' . $type . wpme_dec2sixtwo( $blog_id ) . '-' . $id;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Get the shortlink handler.
[113] Fix | Delete
*
[114] Fix | Delete
* Used with the Core pre_get_shortlink hook.
[115] Fix | Delete
*
[116] Fix | Delete
* @param string $shortlink Shortlink value from the action. Ignored.
[117] Fix | Delete
* @param int $id Post ID (0 for the current post).
[118] Fix | Delete
* @param string $context The context for the link. One of 'post' or 'query'.
[119] Fix | Delete
* @param bool $allow_slugs Whether to allow post slugs in the shortlink.
[120] Fix | Delete
*
[121] Fix | Delete
* @return string
[122] Fix | Delete
*/
[123] Fix | Delete
function wpme_get_shortlink_handler( $shortlink, $id, $context, $allow_slugs ) {
[124] Fix | Delete
return wpme_get_shortlink( $id, $context, $allow_slugs );
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Add Shortlinks to the REST API responses.
[129] Fix | Delete
*
[130] Fix | Delete
* @since 6.9.0
[131] Fix | Delete
*
[132] Fix | Delete
* @action rest_api_init
[133] Fix | Delete
* @uses register_rest_field, wpme_rest_get_shortlink
[134] Fix | Delete
*/
[135] Fix | Delete
function wpme_rest_register_shortlinks() {
[136] Fix | Delete
// Post types that support shortlinks by default.
[137] Fix | Delete
$supported_post_types = array(
[138] Fix | Delete
'attachment',
[139] Fix | Delete
'page',
[140] Fix | Delete
'post',
[141] Fix | Delete
);
[142] Fix | Delete
[143] Fix | Delete
// Add any CPT that may have declared support for shortlinks.
[144] Fix | Delete
foreach ( get_post_types() as $post_type ) {
[145] Fix | Delete
if (
[146] Fix | Delete
post_type_supports( $post_type, 'shortlinks' )
[147] Fix | Delete
&& post_type_supports( $post_type, 'editor' )
[148] Fix | Delete
) {
[149] Fix | Delete
$supported_post_types[] = $post_type;
[150] Fix | Delete
}
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
register_rest_field(
[154] Fix | Delete
$supported_post_types,
[155] Fix | Delete
'jetpack_shortlink',
[156] Fix | Delete
array(
[157] Fix | Delete
'get_callback' => 'wpme_rest_get_shortlink',
[158] Fix | Delete
'update_callback' => null,
[159] Fix | Delete
'schema' => null,
[160] Fix | Delete
)
[161] Fix | Delete
);
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Get the shortlink of a post.
[166] Fix | Delete
*
[167] Fix | Delete
* @since 6.9.0
[168] Fix | Delete
*
[169] Fix | Delete
* @param array $object Details of current post.
[170] Fix | Delete
*
[171] Fix | Delete
* @uses wpme_get_shortlink
[172] Fix | Delete
*
[173] Fix | Delete
* @return string
[174] Fix | Delete
*/
[175] Fix | Delete
function wpme_rest_get_shortlink( $object ) {
[176] Fix | Delete
$object_id = $object['id'] ?? 0;
[177] Fix | Delete
return wpme_get_shortlink( $object_id, array() );
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
// Add shortlinks to the REST API Post response.
[181] Fix | Delete
add_action( 'rest_api_init', 'wpme_rest_register_shortlinks' );
[182] Fix | Delete
[183] Fix | Delete
/**
[184] Fix | Delete
* Set the Shortlink Gutenberg extension as available.
[185] Fix | Delete
*/
[186] Fix | Delete
function wpme_set_extension_available() {
[187] Fix | Delete
Jetpack_Gutenberg::set_extension_available( 'shortlinks' );
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
add_action( 'init', 'wpme_set_extension_available' );
[191] Fix | Delete
[192] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function