Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_JSON_API_JPS_WooCommerce_Connect_Endpoint
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 result
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
6
 validate_input
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * JPS WooCommerce connect endpoint.
9 *
10 * @phan-constructor-used-for-side-effects
11 */
12class Jetpack_JSON_API_JPS_WooCommerce_Connect_Endpoint extends Jetpack_JSON_API_Endpoint {
13
14    /**
15     * Needed capabilities.
16     *
17     * @var string
18     */
19    protected $needed_capabilities = 'manage_options';
20
21    /**
22     * The result.
23     *
24     * @return array|WP_Error
25     */
26    public function result() {
27        $input       = $this->input();
28        $helper_data = get_option( 'woocommerce_helper_data', array() );
29
30        if ( ! empty( $helper_data['auth'] ) ) {
31            return new WP_Error(
32                'already_configured',
33                __( 'WooCommerce auth data is already set.', 'jetpack' )
34            );
35        }
36
37        // Only update the auth field for `woocommerce_helper_data` instead of blowing out the entire option.
38        $helper_data['auth'] = array(
39            'user_id'             => $input['user_id'],
40            'site_id'             => $input['site_id'],
41            'updated'             => time(),
42            'access_token'        => $input['access_token'],
43            'access_token_secret' => $input['access_token_secret'],
44        );
45
46        $updated = update_option(
47            'woocommerce_helper_data',
48            $helper_data
49        );
50
51        return array(
52            'success' => $updated,
53        );
54    }
55
56    /**
57     * Validate input.
58     *
59     * @param object $object - the object we're validating.
60     *
61     * @return bool|WP_Error
62     */
63    public function validate_input( $object ) {
64        $input = $this->input();
65
66        if ( empty( $input['access_token'] ) ) {
67            return new WP_Error( 'input_error', __( 'access_token is required', 'jetpack' ) );
68        }
69
70        if ( empty( $input['access_token_secret'] ) ) {
71            return new WP_Error( 'input_error', __( 'access_token_secret is required', 'jetpack' ) );
72        }
73
74        if ( empty( $input['user_id'] ) ) {
75            return new WP_Error( 'input_error', __( 'user_id is required', 'jetpack' ) );
76        }
77
78        if ( empty( $input['site_id'] ) ) {
79            return new WP_Error( 'input_error', __( 'site_id is required', 'jetpack' ) );
80        }
81
82        return parent::validate_input( $object );
83    }
84}