Completed
Push — instant-search-master ( 8be3b4...336413 )
by
unknown
06:37 queued 10s
created

Partner::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack Partner package.
4
 *
5
 * @package  automattic/jetpack-partner
6
 */
7
8
namespace Automattic\Jetpack;
9
10
/**
11
 * This class introduces functionality used by Jetpack hosting partners.
12
 *
13
 * @since 8.1.0
14
 */
15
class Partner {
16
17
	/**
18
	 * Affiliate code.
19
	 */
20
	const AFFILIATE_CODE = 'affiliate';
21
22
	/**
23
	 * Subsidiary id code.
24
	 */
25
	const SUBSIDIARY_CODE = 'subsidiary';
26
27
	/**
28
	 * Singleton instance.
29
	 *
30
	 * @since 8.1.0
31
	 *
32
	 * @var Partner This class instance.
33
	 */
34
	private static $instance = null;
35
36
	/**
37
	 * Partner constructor.
38
	 */
39
	private function __construct() {
40
	}
41
42
	/**
43
	 * Initializes the class or returns the singleton.
44
	 *
45
	 * @return Partner | false
46
	 * @since 8.1.0
47
	 */
48
	public static function init() {
49
		if ( is_null( self::$instance ) ) {
50
			self::$instance = new Partner();
51
			add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_subsidiary_id_as_query_arg' ) );
52
			add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_affiliate_code_as_query_arg' ) );
53
		}
54
55
		return self::$instance;
56
	}
57
58
	/**
59
	 * Adds the partner subsidiary code to the passed URL.
60
	 *
61
	 * @param string $url The URL.
62
	 *
63
	 * @return string
64
	 */
65
	public function add_subsidiary_id_as_query_arg( $url ) {
66
		return $this->add_code_as_query_arg( self::SUBSIDIARY_CODE, $url );
67
	}
68
69
	/**
70
	 * Adds the affiliate code to the passed URL.
71
	 *
72
	 * @param string $url The URL.
73
	 *
74
	 * @return string
75
	 */
76
	public function add_affiliate_code_as_query_arg( $url ) {
77
		return $this->add_code_as_query_arg( self::AFFILIATE_CODE, $url );
78
	}
79
80
	/**
81
	 * Returns the passed URL with the partner code added as a URL query arg.
82
	 *
83
	 * @param string $type The partner code.
84
	 * @param string $url The URL where the partner subsidiary id will be added.
85
	 *
86
	 * @return string The passed URL with the partner code added.
87
	 * @since 8.1.0
88
	 */
89
	public function add_code_as_query_arg( $type, $url ) {
90
		switch ( $type ) {
91
			case self::AFFILIATE_CODE:
92
				$query_arg_name = 'aff';
93
				break;
94
			case self::SUBSIDIARY_CODE:
95
				$query_arg_name = 'subsidiaryId';
96
				break;
97
			default:
98
				return $url;
99
		}
100
101
		$code = $this->get_partner_code( $type );
102
103
		if ( '' === $code ) {
104
			return $url;
105
		}
106
107
		return add_query_arg( $query_arg_name, $code, $url );
108
	}
109
110
	/**
111
	 * Returns a partner code.
112
	 *
113
	 * @param string $type This can be either 'affiliate' or 'subsidiary'. Returns empty string when code is unknown.
114
	 *
115
	 * @return string The partner code.
116
	 * @since 8.1.0
117
	 */
118
	public function get_partner_code( $type ) {
119
		switch ( $type ) {
120
			case self::AFFILIATE_CODE:
121
				/**
122
				 * Allow to filter the affiliate code.
123
				 *
124
				 * @param string $affiliate_code The affiliate code, blank by default.
125
				 *
126
				 * @since 6.9.0
127
				 */
128
				return apply_filters( 'jetpack_affiliate_code', get_option( 'jetpack_affiliate_code', '' ) );
129
			case self::SUBSIDIARY_CODE:
130
				/**
131
				 * Allow to filter the partner subsidiary id.
132
				 *
133
				 * @param string $subsidiary_id The partner subsidiary id, blank by default.
134
				 *
135
				 * @since 8.1.0
136
				 */
137
				return apply_filters(
138
					'jetpack_partner_subsidiary_id',
139
					get_option( 'jetpack_partner_subsidiary_id', '' )
140
				);
141
			default:
142
				return '';
143
		}
144
	}
145
146
	/**
147
	 * Resets the singleton for testing purposes.
148
	 */
149
	public static function reset() {
150
		self::$instance = null;
151
	}
152
}
153