Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuickAddProductController
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 register_routes
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 prepare_object_for_response_core
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 save_taxonomy_terms
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace NewfoldLabs\WP\Module\ECommerce\RestApi;
4
5use NewfoldLabs\WP\Module\ECommerce\Permissions;
6
7/**
8 * Quick Add Product Rest API controller.
9 */
10class QuickAddProductController extends \WC_REST_Products_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 = 'product';
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, 'create_item' ),
38                    'permission_callback' => array( Permissions::class, 'rest_is_authorized_shop_manager' ),
39                    'args'                => $this->get_endpoint_args_for_item_schema(),
40                ),
41                'schema' => array( $this, 'get_public_item_schema' ),
42            )
43        );
44    }
45
46    /**
47     * Core function to prepare a single product output for response
48     * (doesn't fire hooks, ensure_response, or add links).
49     *
50     * @param \WC_Data         $object_data Object data.
51     * @param \WP_REST_Request $request Request object.
52     * @param string           $context Request context.
53     * @return array Product data to be included in the response.
54     */
55    protected function prepare_object_for_response_core( $object_data, $request, $context ): array {
56        $data = parent::prepare_object_for_response_core( $object_data, $request, $context );
57
58        $data['edit_url'] = get_edit_post_link( $object_data->get_id(), 'view' );
59
60        return $data;
61    }
62
63    /**
64     * Save taxonomy terms.
65     *
66     * @param \WC_Product $product  Product instance.
67     * @param array       $terms    Terms data.
68     * @param string      $taxonomy Taxonomy name.
69     *
70     * @return \WC_Product
71     */
72    protected function save_taxonomy_terms( $product, $terms, $taxonomy = 'cat' ) {
73
74        // Go with default behaviour for tags.
75        if ( 'cat' !== $taxonomy ) {
76            return parent::save_taxonomy_terms( $product, $terms, $taxonomy );
77        }
78
79        $term_ids = array();
80
81        foreach ( $terms as $term ) {
82
83            // If term ID is zero, create term. Otherwise, collect.
84            if ( empty( $term['id'] ) ) {
85                $new_term = wp_insert_term( $term['name'], 'product_cat' );
86                if ( ! is_wp_error( $new_term ) ) {
87                    $term_ids[] = $new_term['term_id'];
88                }
89            } else {
90                $term_ids[] = $term['id'];
91            }
92
93            $product->set_category_ids( $term_ids );
94        }
95
96        return $product;
97    }
98}