Completed
Push — try/slideshow ( 30e91a...3c332f )
by Jeremy
28:00 queued 18:58
created

result()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_JSON_API_JPS_WooCommerce_Connect_Endpoint extends Jetpack_JSON_API_Endpoint {
4
5
	protected $needed_capabilities = 'manage_options';
6
7
	function result() {
8
		$input       = $this->input();
9
		$helper_data = get_option( 'woocommerce_helper_data', array() );
10
11
		if ( ! empty( $helper_data['auth'] ) ) {
12
			return new WP_Error(
13
				'already_configured',
14
				__( 'WooCommerce auth data is already set.', 'jetpack' )
15
			);
16
		}
17
18
		// Only update the auth field for `woocommerce_helper_data` instead of blowing out the entire option.
19
		$helper_data['auth'] = array(
20
			'user_id'             => $input['user_id'],
21
			'site_id'             => $input['site_id'],
22
			'updated'             => time(),
23
			'access_token'        => $input['access_token'],
24
			'access_token_secret' => $input['access_token_secret'],
25
		);
26
27
		$updated = update_option(
28
			'woocommerce_helper_data',
29
			$helper_data
30
		);
31
32
		return array(
33
			'success' => $updated,
34
		);
35
	}
36
37
	function validate_input( $object ) {
38
		$input = $this->input();
39
40
		if ( empty( $input['access_token'] ) ) {
41
			return new WP_Error( 'input_error', __( 'access_token is required', 'jetpack' ) );
42
		}
43
44
		if ( empty( $input['access_token_secret'] ) ) {
45
			return new WP_Error( 'input_error', __( 'access_token_secret is required', 'jetpack' ) );
46
		}
47
48
		if ( empty( $input['user_id'] ) ) {
49
			return new WP_Error( 'input_error', __( 'user_id is required', 'jetpack' ) );
50
		}
51
52
		if ( empty( $input['site_id'] ) ) {
53
			return new WP_Error( 'input_error', __( 'site_id is required', 'jetpack' ) );
54
		}
55
56
		return parent::validate_input( $object );
57
	}
58
}
59