Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Redirect
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 2
5.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_url
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
4.00
1<?php
2/**
3 * Jetpack Redirect package.
4 *
5 * @package  automattic/jetpack-redirect
6 */
7
8namespace Automattic\Jetpack;
9
10/**
11 * Class Redirect
12 */
13class Redirect {
14    /**
15     * Constructor.
16     *
17     * Static-only class, so nothing here.
18     */
19    private function __construct() {}
20
21    /**
22     * Builds and returns an URL using the jetpack.com/redirect/ service
23     *
24     * If $source is a simple slug, it will be sent using the source query parameter. e.g. jetpack.com/redirect/?source=slug
25     *
26     * 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
27     *
28     * 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.
29     *
30     * @param string       $source The URL handler registered in the server or the full destination URL (starting with https://).
31     * @param array|string $args {
32     *    Optional. Additional arguments to build the url. This is not a complete list as any argument passed here will be sent to as a query parameter to the Redirect server. These parameters will not necessarily be passed over to the final destination URL. If you want to add a parameter to the final destination URL, use the `query` argument.
33     *
34     *    @type string  $site URL of the site; Default is current site.
35     *    @type string  $path Additional path to be appended to the URL.
36     *    @type string  $query Query parameters to be added to the final destination URL. should be in query string format (e.g. 'key=value&foo=bar').
37     *    @type string  $anchor Anchor to be added to the URL.
38     *    @type integer $u The user ID.
39     * }
40     *
41     * @return string The built URL
42     */
43    public static function get_url( $source, $args = array() ) {
44
45        $url         = 'https://jetpack.com/redirect/';
46        $site_suffix = ( new Status() )->get_site_suffix();
47        $args        = wp_parse_args( $args, array( 'site' => $site_suffix ) );
48
49        $source_key = 'source';
50
51        if ( \str_starts_with( $source, 'https://' ) ) {
52            $source_key = 'url';
53            $source_url = \wp_parse_url( $source );
54
55            // discard any query and fragments.
56            $source = 'https://' . $source_url['host'] . ( $source_url['path'] ?? '' );
57        }
58
59        $to_be_added = array(
60            $source_key => rawurlencode( $source ),
61        );
62
63        foreach ( $args as $arg_name => $arg_value ) {
64
65            if ( empty( $arg_value ) ) {
66                continue;
67            }
68
69            $to_be_added[ $arg_name ] = rawurlencode( $arg_value );
70
71        }
72
73        $url = add_query_arg( $to_be_added, $url );
74
75        return $url;
76    }
77}