1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jetpack Redirect package. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-redirect |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Redirect |
12
|
|
|
*/ |
13
|
|
|
class Redirect { |
14
|
|
|
/** |
15
|
|
|
* Constructor. |
16
|
|
|
* |
17
|
|
|
* Static-only class, so nothing here. |
18
|
|
|
*/ |
19
|
|
|
private function __construct() {} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Strip http:// or https:// from a url, replaces forward slash with ::, |
23
|
|
|
* so we can bring them directly to their site in calypso. |
24
|
|
|
* |
25
|
|
|
* @param string $url the full URL. |
26
|
|
|
* @return string $url without the guff |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
private static function build_raw_urls( $url ) { |
29
|
|
|
$strip_http = '/.*?:\/\//i'; |
30
|
|
|
$url = preg_replace( $strip_http, '', $url ); |
31
|
|
|
$url = str_replace( '/', '::', $url ); |
32
|
|
|
return $url; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Builds and returns an URL using the jetpack.com/redirect service |
37
|
|
|
* |
38
|
|
|
* If $source is a simple slug, it will be sent using the source query parameter. e.g. jetpack.com/redirect?source=slug |
39
|
|
|
* |
40
|
|
|
* If $source is a full URL, starting with https://, it will be sent using the url query parameter. e.g. jetpack.com/redirect?url=https://wordpress.com |
41
|
|
|
* |
42
|
|
|
* Note: if using full URL, query parameters and anchor must be passed in $args. Any querystring of url fragment in the URL will be discarded. |
43
|
|
|
* |
44
|
|
|
* @param string $source The URL handler registered in the server or the full destination URL (starting with https://). |
45
|
|
|
* @param array|string $args { |
46
|
|
|
* Optional. Additional arguments to build the url. |
47
|
|
|
* |
48
|
|
|
* @type string $site URL of the site; Default is current site. |
49
|
|
|
* @type string $path Additional path to be appended to the URL. |
50
|
|
|
* @type string $query Query parameters to be added to the URL. |
51
|
|
|
* @type string $anchor Anchor to be added to the URL. |
52
|
|
|
* } |
53
|
|
|
* |
54
|
|
|
* @return string The built URL |
55
|
|
|
*/ |
56
|
|
|
public static function get_url( $source, $args = array() ) { |
57
|
|
|
|
58
|
|
|
$url = 'https://jetpack.com/redirect'; |
59
|
|
|
$args = wp_parse_args( $args, array( 'site' => self::build_raw_urls( get_home_url() ) ) ); |
|
|
|
|
60
|
|
|
$accepted_args = array( 'site', 'path', 'query', 'anchor' ); |
61
|
|
|
|
62
|
|
|
$source_key = 'source'; |
63
|
|
|
|
64
|
|
|
if ( 0 === strpos( $source, 'https://' ) ) { |
65
|
|
|
$source_key = 'url'; |
66
|
|
|
$source_url = \wp_parse_url( $source ); |
67
|
|
|
|
68
|
|
|
// discard any query and fragments. |
69
|
|
|
$source = 'https://' . $source_url['host'] . ( isset( $source_url['path'] ) ? $source_url['path'] : '' ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$to_be_added = array( |
73
|
|
|
$source_key => rawurlencode( $source ), |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
foreach ( $args as $arg_name => $arg_value ) { |
|
|
|
|
77
|
|
|
|
78
|
|
|
if ( ! in_array( $arg_name, $accepted_args, true ) || empty( $arg_value ) ) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$to_be_added[ $arg_name ] = rawurlencode( $arg_value ); |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ( ! empty( $to_be_added ) ) { |
87
|
|
|
$url = add_query_arg( $to_be_added, $url ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $url; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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: