Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoreInfoController
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 register_routes
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 get_item_schema
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
2
 store_options
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace NewfoldLabs\WP\Module\ECommerce\RestApi;
4
5use NewfoldLabs\WP\Module\ECommerce\Permissions;
6
7/**
8 * Store info Rest API controller.
9 */
10class StoreInfoController extends \WP_REST_Controller {
11
12    /**
13     * The namespace of this controller's route.
14     *
15     * @var string
16     */
17    protected $namespace = 'newfold-ecommerce/v1';
18
19    /**
20     * The base of this controller's route.
21     *
22     * @var string
23     */
24    protected $rest_base = 'store-info';
25
26    /**
27     * Register the routes
28     */
29    public function register_routes() {
30
31        register_rest_route(
32            $this->namespace,
33            '/' . $this->rest_base,
34            array(
35                array(
36                    'methods'             => \WP_REST_Server::CREATABLE,
37                    'callback'            => array( $this, 'store_options' ),
38                    'permission_callback' => array( Permissions::class, 'rest_is_authorized_shop_manager' ),
39                ),
40                'schema' => array( $this, 'get_item_schema' ),
41            )
42        );
43    }
44
45    /**
46     * Get the Store Info API schema, conforming to JSON Schema.
47     *
48     * @return array
49     */
50    public function get_item_schema() {
51        return array(
52            '$schema'    => 'http://json-schema.org/draft-04/schema#',
53            'title'      => 'store-info',
54            'type'       => 'object',
55            'properties' => array(
56                'address'  => array(
57                    'description' => __( 'Store address info', 'wp-module-ecommerce' ),
58                    'type'        => 'string',
59                    'context'     => array( 'view', 'edit' ),
60                ),
61                'city'     => array(
62                    'description' => __( 'Store city info.', 'wp-module-ecommerce' ),
63                    'type'        => 'string',
64                    'context'     => array( 'view', 'edit' ),
65                ),
66                'postcode' => array(
67                    'description' => __( 'Store postcode info.', 'wp-module-ecommerce' ),
68                    'type'        => 'string',
69                    'context'     => array( 'view', 'edit' ),
70                ),
71                'state'    => array(
72                    'description' => __( 'Store state info.', 'wp-module-ecommerce' ),
73                    'type'        => 'string',
74                    'context'     => array( 'view', 'edit' ),
75                ),
76                'country'  => array(
77                    'description' => __( 'Store country info.', 'wp-module-ecommerce' ),
78                    'type'        => 'string',
79                    'context'     => array( 'view', 'edit' ),
80                ),
81                'industry' => array(
82                    'description' => __( 'Store industry info.', 'wp-module-ecommerce' ),
83                    'type'        => 'string',
84                    'context'     => array( 'view', 'edit' ),
85                ),
86            ),
87        );
88    }
89
90    /**
91     * Save store options
92     *
93     * @param \WP_REST_Request $request The request.
94     */
95    public function store_options( $request ) {
96        $updated = array();
97        $params  = $request->get_json_params();
98
99        // Handle special country option.
100        if ( isset( $params['country'] ) ) {
101            $value  = $params['country'];
102            $states = WC()->countries->get_states( $value );
103
104            if ( $states && isset( $params['state'] ) ) {
105                $value .= ':' . $params['state'];
106            }
107
108            $updated['country'] = update_option( 'woocommerce_default_country', $value );
109        }
110
111        // Handle option industry.
112        if ( isset( $params['industry'] ) ) {
113            $updated['industry'] = update_option( 'nfd_store_industry', $params['industry'] );
114        }
115
116        foreach ( array( 'address', 'city', 'postcode' ) as $key ) {
117            if ( ! isset( $params[ $key ] ) ) {
118                continue;
119            }
120
121            $updated[ $key ] = update_option( "woocommerce_store_{$key}", $params[ $key ] );
122        }
123
124        return $updated;
125    }
126}