summaryrefslogtreecommitdiff
blob: 75a3b04dbb2c11d30808b8717ff500a1117b8279 (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
<?php

class Jetpack_JSON_API_JPS_WooCommerce_Connect_Endpoint extends Jetpack_JSON_API_Endpoint {

	protected $needed_capabilities = 'manage_options';

	function result() {
		$input       = $this->input();
		$helper_data = get_option( 'woocommerce_helper_data', array() );

		if ( ! empty( $helper_data['auth'] ) ) {
			return new WP_Error(
				'already_configured',
				__( 'WooCommerce auth data is already set.', 'jetpack' )
			);
		}

		// Only update the auth field for `woocommerce_helper_data` instead of blowing out the entire option.
		$helper_data['auth'] = array(
			'user_id'             => $input['user_id'],
			'site_id'             => $input['site_id'],
			'updated'             => time(),
			'access_token'        => $input['access_token'],
			'access_token_secret' => $input['access_token_secret'],
		);

		$updated = update_option(
			'woocommerce_helper_data',
			$helper_data
		);

		return array(
			'success' => $updated,
		);
	}

	function validate_input( $object ) {
		$input = $this->input();

		if ( empty( $input['access_token'] ) ) {
			return new WP_Error( 'input_error', __( 'access_token is required', 'jetpack' ) );
		}

		if ( empty( $input['access_token_secret'] ) ) {
			return new WP_Error( 'input_error', __( 'access_token_secret is required', 'jetpack' ) );
		}

		if ( empty( $input['user_id'] ) ) {
			return new WP_Error( 'input_error', __( 'user_id is required', 'jetpack' ) );
		}

		if ( empty( $input['site_id'] ) ) {
			return new WP_Error( 'input_error', __( 'site_id is required', 'jetpack' ) );
		}

		return parent::validate_input( $object );
	}
}