Completed
Push — update/jitm-test ( 03ced2...c48940 )
by
unknown
212:35 queued 203:56
created

shortlinks.php ➔ wpme_rest_get_shortlink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: WP.me Shortlinks
4
 * Module Description: Create short and simple links 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
 * 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 Post response.
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
		'post',
102
		'jetpack_shortlink',
103
		array(
104
			'get_callback'    => 'wpme_rest_get_shortlink',
105
			'update_callback' => null,
106
			'schema'          => null,
107
		)
108
	);
109
}
110
111
/**
112
 * Get the shortlink of a post.
113
 *
114
 * @since 6.9.0
115
 *
116
 * @param array $object Details of current post.
117
 *
118
 * @uses wpme_get_shortlink
119
 *
120
 * @return string
121
 */
122
function wpme_rest_get_shortlink( $object ) {
123
	return wpme_get_shortlink( $object['id'], array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
124
}
125
126
// Add shortlinks to the REST API Post response.
127
if ( function_exists( 'register_rest_field' ) ) {
128
	add_action( 'rest_api_init', 'wpme_rest_register_shortlinks' );
129
}
130
131
// Register Gutenberg plugin
132
jetpack_register_plugin( 'shortlinks' );
133