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 3.9 3.9-beta 3.9.1 3.9.2 3.9.3 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1-beta 4.1.1 4.1.2 4.2 4.2-beta 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6 4.6.1 4.7 4.8 4.8.1 4.9 4.9.1 4.9.2 5.0 5.0.1
contact-form-7 / includes / integration.php
contact-form-7 / includes Last commit date
block-editor 4 years ago css 4 years ago js 4 years ago capabilities.php 7 years ago config-validator.php 4 years ago contact-form-functions.php 4 years ago contact-form-template.php 5 years ago contact-form.php 4 years ago controller.php 5 years ago file.php 4 years ago form-tag.php 4 years ago form-tags-manager.php 4 years ago formatting.php 4 years ago functions.php 4 years ago integration.php 4 years ago l10n.php 5 years ago mail.php 4 years ago pipe.php 4 years ago rest-api.php 4 years ago shortcodes.php 9 years ago special-mail-tags.php 5 years ago submission.php 4 years ago upgrade.php 7 years ago validation-functions.php 4 years ago validation.php 7 years ago
integration.php
355 lines
1 <?php
2
3 class WPCF7_Integration {
4
5 private static $instance;
6
7 private $services = array();
8 private $categories = array();
9
10 private function __construct() {}
11
12 public static function get_builtin_categories() {
13 return array(
14 'spam_protection' => __( 'Spam protection', 'contact-form-7' ),
15 'email_marketing' => __( 'Email marketing', 'contact-form-7' ),
16 'payments' => __( 'Payments', 'contact-form-7' ),
17 );
18 }
19
20 public static function get_instance() {
21 if ( empty( self::$instance ) ) {
22 self::$instance = new self;
23 self::$instance->categories = self::get_builtin_categories();
24 }
25
26 return self::$instance;
27 }
28
29 public function add_service( $name, WPCF7_Service $service ) {
30 $name = sanitize_key( $name );
31
32 if ( empty( $name )
33 or isset( $this->services[$name] ) ) {
34 return false;
35 }
36
37 $this->services[$name] = $service;
38 }
39
40 public function add_category( $name, $title ) {
41 $name = sanitize_key( $name );
42
43 if ( empty( $name )
44 or isset( $this->categories[$name] ) ) {
45 return false;
46 }
47
48 $this->categories[$name] = $title;
49 }
50
51 public function service_exists( $name = '' ) {
52 if ( '' == $name ) {
53 return (bool) count( $this->services );
54 } else {
55 return isset( $this->services[$name] );
56 }
57 }
58
59 public function get_service( $name ) {
60 if ( $this->service_exists( $name ) ) {
61 return $this->services[$name];
62 } else {
63 return false;
64 }
65 }
66
67 public function list_services( $args = '' ) {
68 $args = wp_parse_args( $args, array(
69 'include' => array(),
70 ) );
71
72 $singular = false;
73 $services = (array) $this->services;
74
75 if ( ! empty( $args['include'] ) ) {
76 $services = array_intersect_key( $services,
77 array_flip( (array) $args['include'] )
78 );
79
80 if ( 1 == count( $services ) ) {
81 $singular = true;
82 }
83 }
84
85 if ( empty( $services ) ) {
86 return;
87 }
88
89 $action = wpcf7_current_action();
90
91 foreach ( $services as $name => $service ) {
92 $cats = array_intersect_key( $this->categories,
93 array_flip( $service->get_categories() )
94 );
95 ?>
96 <div class="card<?php echo $service->is_active() ? ' active' : ''; ?>" id="<?php echo esc_attr( $name ); ?>">
97 <?php $service->icon(); ?>
98 <h2 class="title"><?php echo esc_html( $service->get_title() ); ?></h2>
99 <div class="infobox">
100 <?php echo esc_html( implode( ', ', $cats ) ); ?>
101 </div>
102 <br class="clear" />
103
104 <div class="inside">
105 <?php
106 if ( $singular ) {
107 $service->display( $action );
108 } else {
109 $service->display();
110 }
111 ?>
112 </div>
113 </div>
114 <?php
115 }
116 }
117
118 }
119
120 abstract class WPCF7_Service {
121
122 abstract public function get_title();
123 abstract public function is_active();
124
125 public function get_categories() {
126 return array();
127 }
128
129 public function icon() {
130 return '';
131 }
132
133 public function link() {
134 return '';
135 }
136
137 public function load( $action = '' ) {
138 }
139
140 public function display( $action = '' ) {
141 }
142
143 public function admin_notice( $message = '' ) {
144 }
145
146 }
147
148 class WPCF7_Service_OAuth2 extends WPCF7_Service {
149
150 protected $client_id = '';
151 protected $client_secret = '';
152 protected $access_token = '';
153 protected $refresh_token = '';
154 protected $authorization_endpoint = 'https://example.com/authorization';
155 protected $token_endpoint = 'https://example.com/token';
156
157 public function get_title() {
158 return '';
159 }
160
161 public function is_active() {
162 return ! empty( $this->refresh_token );
163 }
164
165 protected function save_data() {
166 }
167
168 protected function reset_data() {
169 }
170
171 protected function get_redirect_uri() {
172 return admin_url();
173 }
174
175 protected function menu_page_url( $args = '' ) {
176 return menu_page_url( 'wpcf7-integration', false );
177 }
178
179 public function load( $action = '' ) {
180 if ( 'auth_redirect' == $action ) {
181 $code = isset( $_GET['code'] ) ? $_GET['code'] : '';
182
183 if ( $code ) {
184 $this->request_token( $code );
185 }
186
187 if ( ! empty( $this->access_token ) ) {
188 $message = 'success';
189 } else {
190 $message = 'failed';
191 }
192
193 wp_safe_redirect( $this->menu_page_url(
194 array(
195 'action' => 'setup',
196 'message' => $message,
197 )
198 ) );
199
200 exit();
201 }
202 }
203
204 protected function authorize( $scope = '' ) {
205 $endpoint = add_query_arg(
206 array(
207 'response_type' => 'code',
208 'client_id' => $this->client_id,
209 'redirect_uri' => urlencode( $this->get_redirect_uri() ),
210 'scope' => $scope,
211 ),
212 $this->authorization_endpoint
213 );
214
215 if ( wp_redirect( esc_url_raw( $endpoint ) ) ) {
216 exit();
217 }
218 }
219
220 protected function get_http_authorization_header( $scheme = 'basic' ) {
221 $scheme = strtolower( trim( $scheme ) );
222
223 switch ( $scheme ) {
224 case 'bearer':
225 return sprintf( 'Bearer %s', $this->access_token );
226 case 'basic':
227 default:
228 return sprintf( 'Basic %s',
229 base64_encode( $this->client_id . ':' . $this->client_secret )
230 );
231 }
232 }
233
234 protected function request_token( $authorization_code ) {
235 $endpoint = add_query_arg(
236 array(
237 'code' => $authorization_code,
238 'redirect_uri' => urlencode( $this->get_redirect_uri() ),
239 'grant_type' => 'authorization_code',
240 ),
241 $this->token_endpoint
242 );
243
244 $request = array(
245 'headers' => array(
246 'Authorization' => $this->get_http_authorization_header( 'basic' ),
247 ),
248 );
249
250 $response = wp_remote_post( esc_url_raw( $endpoint ), $request );
251 $response_code = (int) wp_remote_retrieve_response_code( $response );
252 $response_body = wp_remote_retrieve_body( $response );
253 $response_body = json_decode( $response_body, true );
254
255 if ( WP_DEBUG and 400 <= $response_code ) {
256 $this->log( $endpoint, $request, $response );
257 }
258
259 if ( 401 == $response_code ) { // Unauthorized
260 $this->access_token = null;
261 $this->refresh_token = null;
262 } else {
263 if ( isset( $response_body['access_token'] ) ) {
264 $this->access_token = $response_body['access_token'];
265 } else {
266 $this->access_token = null;
267 }
268
269 if ( isset( $response_body['refresh_token'] ) ) {
270 $this->refresh_token = $response_body['refresh_token'];
271 } else {
272 $this->refresh_token = null;
273 }
274 }
275
276 $this->save_data();
277
278 return $response;
279 }
280
281 protected function refresh_token() {
282 $endpoint = add_query_arg(
283 array(
284 'refresh_token' => $this->refresh_token,
285 'grant_type' => 'refresh_token',
286 ),
287 $this->token_endpoint
288 );
289
290 $request = array(
291 'headers' => array(
292 'Authorization' => $this->get_http_authorization_header( 'basic' ),
293 ),
294 );
295
296 $response = wp_remote_post( esc_url_raw( $endpoint ), $request );
297 $response_code = (int) wp_remote_retrieve_response_code( $response );
298 $response_body = wp_remote_retrieve_body( $response );
299 $response_body = json_decode( $response_body, true );
300
301 if ( WP_DEBUG and 400 <= $response_code ) {
302 $this->log( $endpoint, $request, $response );
303 }
304
305 if ( 401 == $response_code ) { // Unauthorized
306 $this->access_token = null;
307 $this->refresh_token = null;
308 } else {
309 if ( isset( $response_body['access_token'] ) ) {
310 $this->access_token = $response_body['access_token'];
311 } else {
312 $this->access_token = null;
313 }
314
315 if ( isset( $response_body['refresh_token'] ) ) {
316 $this->refresh_token = $response_body['refresh_token'];
317 }
318 }
319
320 $this->save_data();
321
322 return $response;
323 }
324
325 protected function remote_request( $url, $request = array() ) {
326 static $refreshed = false;
327
328 $request = wp_parse_args( $request, array() );
329
330 $request['headers'] = array_merge(
331 $request['headers'],
332 array(
333 'Authorization' => $this->get_http_authorization_header( 'bearer' ),
334 )
335 );
336
337 $response = wp_remote_request( esc_url_raw( $url ), $request );
338
339 if ( 401 === wp_remote_retrieve_response_code( $response )
340 and ! $refreshed ) {
341 $this->refresh_token();
342 $refreshed = true;
343
344 $response = $this->remote_request( $url, $request );
345 }
346
347 return $response;
348 }
349
350 protected function log( $url, $request, $response ) {
351 wpcf7_log_remote_request( $url, $request, $response );
352 }
353
354 }
355