PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-connection / src / class-partner.php

class-partner.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.7, at jetpack_vendor/automattic/jetpack-connection/src/class-partner.php

216 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Jetpack Partner utilities.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8 namespace Automattic\Jetpack;
9
10 /**
11 * This class introduces functionality used by Jetpack hosting partners.
12 *
13 * @since partner-1.0.0
14 * @since 2.0.0
15 */
16 class Partner {
17
18 /**
19 * Affiliate code.
20 */
21 const AFFILIATE_CODE = 'affiliate';
22
23 /**
24 * Subsidiary id code.
25 */
26 const SUBSIDIARY_CODE = 'subsidiary';
27
28 /**
29 * Singleton instance.
30 *
31 * @since partner-1.0.0
32 * @since 2.0.0
33 *
34 * @var Partner This class instance.
35 */
36 private static $instance = null;
37
38 /**
39 * Partner constructor.
40 */
41 private function __construct() {
42 }
43
44 /**
45 * Initializes the class or returns the singleton.
46 *
47 * @since partner-1.0.0
48 * @since 2.0.0
49 *
50 * @return Partner | false
51 */
52 public static function init() {
53 if ( self::$instance === null ) {
54 self::$instance = new Partner();
55 add_filter( 'jetpack_build_authorize_url', array( self::$instance, 'add_subsidiary_id_as_query_arg' ) );
56 add_filter( 'jetpack_build_authorize_url', array( self::$instance, 'add_affiliate_code_as_query_arg' ) );
57 add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_subsidiary_id_as_query_arg' ) );
58 add_filter( 'jetpack_build_connection_url', array( self::$instance, 'add_affiliate_code_as_query_arg' ) );
59
60 add_filter( 'jetpack_register_request_body', array( self::$instance, 'add_subsidiary_id_to_params_array' ) );
61 add_filter( 'jetpack_register_request_body', array( self::$instance, 'add_affiliate_code_to_params_array' ) );
62 }
63
64 return self::$instance;
65 }
66
67 /**
68 * Adds the partner subsidiary code to the passed URL.
69 *
70 * @param string $url The URL.
71 *
72 * @return string
73 */
74 public function add_subsidiary_id_as_query_arg( $url ) {
75 return $this->add_code_as_query_arg( self::SUBSIDIARY_CODE, $url );
76 }
77
78 /**
79 * Adds the affiliate code to the passed URL.
80 *
81 * @param string $url The URL.
82 *
83 * @return string
84 */
85 public function add_affiliate_code_as_query_arg( $url ) {
86 return $this->add_code_as_query_arg( self::AFFILIATE_CODE, $url );
87 }
88
89 /**
90 * Adds the partner subsidiary code to the passed array.
91 *
92 * @since partner-1.5.0
93 * @since 2.0.0
94 *
95 * @param array $params The parameters array.
96 *
97 * @return array
98 */
99 public function add_subsidiary_id_to_params_array( $params ) {
100 if ( ! is_array( $params ) ) {
101 return $params;
102 }
103 return array_merge( $params, $this->get_code_as_array( self::SUBSIDIARY_CODE ) );
104 }
105
106 /**
107 * Adds the affiliate code to the passed array.
108 *
109 * @since partner-1.5.0
110 * @since 2.0.0
111 *
112 * @param array $params The parameters array.
113 *
114 * @return array
115 */
116 public function add_affiliate_code_to_params_array( $params ) {
117 if ( ! is_array( $params ) ) {
118 return $params;
119 }
120 return array_merge( $params, $this->get_code_as_array( self::AFFILIATE_CODE ) );
121 }
122
123 /**
124 * Returns the passed URL with the partner code added as a URL query arg.
125 *
126 * @since partner-1.0.0
127 * @since 2.0.0
128 *
129 * @param string $type The partner code.
130 * @param string $url The URL where the partner subsidiary id will be added.
131 *
132 * @return string The passed URL with the partner code added.
133 */
134 public function add_code_as_query_arg( $type, $url ) {
135 return add_query_arg( $this->get_code_as_array( $type ), $url );
136 }
137
138 /**
139 * Gets the partner code in an associative array format
140 *
141 * @since partner-1.5.0
142 * @since 2.0.0
143 *
144 * @param string $type The partner code.
145 * @return array
146 */
147 private function get_code_as_array( $type ) {
148 switch ( $type ) {
149 case self::AFFILIATE_CODE:
150 $query_arg_name = 'aff';
151 break;
152 case self::SUBSIDIARY_CODE:
153 $query_arg_name = 'subsidiaryId';
154 break;
155 default:
156 return array();
157 }
158
159 $code = $this->get_partner_code( $type );
160
161 if ( '' === $code ) {
162 return array();
163 }
164
165 return array( $query_arg_name => $code );
166 }
167
168 /**
169 * Returns a partner code.
170 *
171 * @since partner-1.0.0
172 * @since 2.0.0
173 *
174 * @param string $type This can be either 'affiliate' or 'subsidiary'. Returns empty string when code is unknown.
175 *
176 * @return string The partner code.
177 */
178 public function get_partner_code( $type ) {
179 switch ( $type ) {
180 case self::AFFILIATE_CODE:
181 /**
182 * Allow to filter the affiliate code.
183 *
184 * @since partner-1.0.0
185 * @since-jetpack 6.9.0
186 * @since 2.0.0
187 *
188 * @param string $affiliate_code The affiliate code, blank by default.
189 */
190 return apply_filters( 'jetpack_affiliate_code', get_option( 'jetpack_affiliate_code', '' ) );
191 case self::SUBSIDIARY_CODE:
192 /**
193 * Allow to filter the partner subsidiary id.
194 *
195 * @since partner-1.0.0
196 * @since 2.0.0
197 *
198 * @param string $subsidiary_id The partner subsidiary id, blank by default.
199 */
200 return apply_filters(
201 'jetpack_partner_subsidiary_id',
202 get_option( 'jetpack_partner_subsidiary_id', '' )
203 );
204 default:
205 return '';
206 }
207 }
208
209 /**
210 * Resets the singleton for testing purposes.
211 */
212 public static function reset() {
213 self::$instance = null;
214 }
215 }
216