|
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_authorize_url', array( self::$instance, 'add_subsidiary_id_as_query_arg' ) ); |
|
52
|
|
|
add_filter( 'jetpack_build_authorize_url', array( self::$instance, 'add_affiliate_code_as_query_arg' ) ); |
|
53
|
|
|
add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_subsidiary_id_as_query_arg' ) ); |
|
54
|
|
|
add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_affiliate_code_as_query_arg' ) ); |
|
55
|
|
|
|
|
56
|
|
|
add_filter( 'jetpack_register_request_body', array( self::$instance, 'add_subsidiary_id_to_params_array' ) ); |
|
57
|
|
|
add_filter( 'jetpack_register_request_body', array( self::$instance, 'add_affiliate_code_to_params_array' ) ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return self::$instance; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Adds the partner subsidiary code to the passed URL. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $url The URL. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function add_subsidiary_id_as_query_arg( $url ) { |
|
71
|
|
|
return $this->add_code_as_query_arg( self::SUBSIDIARY_CODE, $url ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Adds the affiliate code to the passed URL. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $url The URL. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function add_affiliate_code_as_query_arg( $url ) { |
|
82
|
|
|
return $this->add_code_as_query_arg( self::AFFILIATE_CODE, $url ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Adds the partner subsidiary code to the passed array. |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $params The parameters array. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
* @since 9.7.0 |
|
92
|
|
|
*/ |
|
93
|
|
|
public function add_subsidiary_id_to_params_array( $params ) { |
|
94
|
|
|
if ( ! is_array( $params ) ) { |
|
95
|
|
|
return $params; |
|
96
|
|
|
} |
|
97
|
|
|
return array_merge( $params, $this->get_code_as_array( self::SUBSIDIARY_CODE ) ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Adds the affiliate code to the passed array. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $params The parameters array. |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
* @since 9.7.0 |
|
107
|
|
|
*/ |
|
108
|
|
|
public function add_affiliate_code_to_params_array( $params ) { |
|
109
|
|
|
if ( ! is_array( $params ) ) { |
|
110
|
|
|
return $params; |
|
111
|
|
|
} |
|
112
|
|
|
return array_merge( $params, $this->get_code_as_array( self::AFFILIATE_CODE ) ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns the passed URL with the partner code added as a URL query arg. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $type The partner code. |
|
119
|
|
|
* @param string $url The URL where the partner subsidiary id will be added. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string The passed URL with the partner code added. |
|
122
|
|
|
* @since 8.1.0 |
|
123
|
|
|
*/ |
|
124
|
|
|
public function add_code_as_query_arg( $type, $url ) { |
|
125
|
|
|
return add_query_arg( $this->get_code_as_array( $type ), $url ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Gets the partner code in an associative array format |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $type The partner code. |
|
132
|
|
|
* @return array |
|
133
|
|
|
* @since 9.7.0 |
|
134
|
|
|
*/ |
|
135
|
|
|
private function get_code_as_array( $type ) { |
|
136
|
|
|
switch ( $type ) { |
|
137
|
|
|
case self::AFFILIATE_CODE: |
|
138
|
|
|
$query_arg_name = 'aff'; |
|
139
|
|
|
break; |
|
140
|
|
|
case self::SUBSIDIARY_CODE: |
|
141
|
|
|
$query_arg_name = 'subsidiaryId'; |
|
142
|
|
|
break; |
|
143
|
|
|
default: |
|
144
|
|
|
return array(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$code = $this->get_partner_code( $type ); |
|
148
|
|
|
|
|
149
|
|
|
if ( '' === $code ) { |
|
150
|
|
|
return array(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return array( $query_arg_name => $code ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns a partner code. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $type This can be either 'affiliate' or 'subsidiary'. Returns empty string when code is unknown. |
|
160
|
|
|
* |
|
161
|
|
|
* @return string The partner code. |
|
162
|
|
|
* @since 8.1.0 |
|
163
|
|
|
*/ |
|
164
|
|
|
public function get_partner_code( $type ) { |
|
165
|
|
|
switch ( $type ) { |
|
166
|
|
|
case self::AFFILIATE_CODE: |
|
167
|
|
|
/** |
|
168
|
|
|
* Allow to filter the affiliate code. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $affiliate_code The affiliate code, blank by default. |
|
171
|
|
|
* |
|
172
|
|
|
* @since 6.9.0 |
|
173
|
|
|
*/ |
|
174
|
|
|
return apply_filters( 'jetpack_affiliate_code', get_option( 'jetpack_affiliate_code', '' ) ); |
|
175
|
|
|
case self::SUBSIDIARY_CODE: |
|
176
|
|
|
/** |
|
177
|
|
|
* Allow to filter the partner subsidiary id. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $subsidiary_id The partner subsidiary id, blank by default. |
|
180
|
|
|
* |
|
181
|
|
|
* @since 8.1.0 |
|
182
|
|
|
*/ |
|
183
|
|
|
return apply_filters( |
|
184
|
|
|
'jetpack_partner_subsidiary_id', |
|
185
|
|
|
get_option( 'jetpack_partner_subsidiary_id', '' ) |
|
186
|
|
|
); |
|
187
|
|
|
default: |
|
188
|
|
|
return ''; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Resets the singleton for testing purposes. |
|
194
|
|
|
*/ |
|
195
|
|
|
public static function reset() { |
|
196
|
|
|
self::$instance = null; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|