summaryrefslogtreecommitdiff
blob: bec501bcc3f0263c7cf3a447e4d8a8462972120e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * Jetpack Partner package.
 *
 * @package  automattic/jetpack-partner
 */

namespace Automattic\Jetpack;

/**
 * This class introduces functionality used by Jetpack hosting partners.
 *
 * @since 1.0.0
 */
class Partner {

	/**
	 * Affiliate code.
	 */
	const AFFILIATE_CODE = 'affiliate';

	/**
	 * Subsidiary id code.
	 */
	const SUBSIDIARY_CODE = 'subsidiary';

	/**
	 * Singleton instance.
	 *
	 * @since 1.0.0
	 *
	 * @var Partner This class instance.
	 */
	private static $instance = null;

	/**
	 * Partner constructor.
	 */
	private function __construct() {
	}

	/**
	 * Initializes the class or returns the singleton.
	 *
	 * @return Partner | false
	 * @since 1.0.0
	 */
	public static function init() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new Partner();
			add_filter( 'jetpack_build_authorize_url', array( self::$instance, 'add_subsidiary_id_as_query_arg' ) );
			add_filter( 'jetpack_build_authorize_url', array( self::$instance, 'add_affiliate_code_as_query_arg' ) );
			add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_subsidiary_id_as_query_arg' ) );
			add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_affiliate_code_as_query_arg' ) );

			add_filter( 'jetpack_register_request_body', array( self::$instance, 'add_subsidiary_id_to_params_array' ) );
			add_filter( 'jetpack_register_request_body', array( self::$instance, 'add_affiliate_code_to_params_array' ) );
		}

		return self::$instance;
	}

	/**
	 * Adds the partner subsidiary code to the passed URL.
	 *
	 * @param string $url The URL.
	 *
	 * @return string
	 */
	public function add_subsidiary_id_as_query_arg( $url ) {
		return $this->add_code_as_query_arg( self::SUBSIDIARY_CODE, $url );
	}

	/**
	 * Adds the affiliate code to the passed URL.
	 *
	 * @param string $url The URL.
	 *
	 * @return string
	 */
	public function add_affiliate_code_as_query_arg( $url ) {
		return $this->add_code_as_query_arg( self::AFFILIATE_CODE, $url );
	}

	/**
	 * Adds the partner subsidiary code to the passed array.
	 *
	 * @param array $params The parameters array.
	 *
	 * @return array
	 * @since 1.5.0
	 */
	public function add_subsidiary_id_to_params_array( $params ) {
		if ( ! is_array( $params ) ) {
			return $params;
		}
		return array_merge( $params, $this->get_code_as_array( self::SUBSIDIARY_CODE ) );
	}

	/**
	 * Adds the affiliate code to the passed array.
	 *
	 * @param array $params The parameters array.
	 *
	 * @return array
	 * @since 1.5.0
	 */
	public function add_affiliate_code_to_params_array( $params ) {
		if ( ! is_array( $params ) ) {
			return $params;
		}
		return array_merge( $params, $this->get_code_as_array( self::AFFILIATE_CODE ) );
	}

	/**
	 * Returns the passed URL with the partner code added as a URL query arg.
	 *
	 * @param string $type The partner code.
	 * @param string $url The URL where the partner subsidiary id will be added.
	 *
	 * @return string The passed URL with the partner code added.
	 * @since 1.0.0
	 */
	public function add_code_as_query_arg( $type, $url ) {
		return add_query_arg( $this->get_code_as_array( $type ), $url );
	}

	/**
	 * Gets the partner code in an associative array format
	 *
	 * @param string $type The partner code.
	 * @return array
	 * @since 1.5.0
	 */
	private function get_code_as_array( $type ) {
		switch ( $type ) {
			case self::AFFILIATE_CODE:
				$query_arg_name = 'aff';
				break;
			case self::SUBSIDIARY_CODE:
				$query_arg_name = 'subsidiaryId';
				break;
			default:
				return array();
		}

		$code = $this->get_partner_code( $type );

		if ( '' === $code ) {
			return array();
		}

		return array( $query_arg_name => $code );
	}

	/**
	 * Returns a partner code.
	 *
	 * @param string $type This can be either 'affiliate' or 'subsidiary'. Returns empty string when code is unknown.
	 *
	 * @return string The partner code.
	 * @since 1.0.0
	 */
	public function get_partner_code( $type ) {
		switch ( $type ) {
			case self::AFFILIATE_CODE:
				/**
				 * Allow to filter the affiliate code.
				 *
				 * @param string $affiliate_code The affiliate code, blank by default.
				 *
				 * @since 1.0.0
				 * @since-jetpack 6.9.0
				 */
				return apply_filters( 'jetpack_affiliate_code', get_option( 'jetpack_affiliate_code', '' ) );
			case self::SUBSIDIARY_CODE:
				/**
				 * Allow to filter the partner subsidiary id.
				 *
				 * @param string $subsidiary_id The partner subsidiary id, blank by default.
				 *
				 * @since 1.0.0
				 */
				return apply_filters(
					'jetpack_partner_subsidiary_id',
					get_option( 'jetpack_partner_subsidiary_id', '' )
				);
			default:
				return '';
		}
	}

	/**
	 * Resets the singleton for testing purposes.
	 */
	public static function reset() {
		self::$instance = null;
	}
}