This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * REST API Reports controller |
||
4 | * |
||
5 | * Handles requests to the reports/top_sellers endpoint. |
||
6 | * |
||
7 | * @author WooThemes |
||
8 | * @category API |
||
9 | * @package WooCommerce/API |
||
10 | * @since 2.6.0 |
||
11 | */ |
||
12 | |||
13 | if ( ! defined( 'ABSPATH' ) ) { |
||
14 | exit; |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * REST API Report Top Sellers controller class. |
||
19 | * |
||
20 | * @package WooCommerce/API |
||
21 | * @extends WC_REST_Report_Sales_Controller |
||
22 | */ |
||
23 | class WC_REST_Report_Top_Sellers_Controller extends WC_REST_Report_Sales_Controller { |
||
24 | |||
25 | /** |
||
26 | * Endpoint namespace. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $namespace = 'wc/v1'; |
||
31 | |||
32 | /** |
||
33 | * Route base. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $rest_base = 'reports/top_sellers'; |
||
38 | |||
39 | /** |
||
40 | * Get sales reports. |
||
41 | * |
||
42 | * @param WP_REST_Request $request |
||
43 | * @return array|WP_Error |
||
44 | */ |
||
45 | public function get_items( $request ) { |
||
46 | // Set date filtering. |
||
47 | $filter = array( |
||
48 | 'period' => $request['period'], |
||
49 | 'date_min' => $request['date_min'], |
||
50 | 'date_max' => $request['date_max'], |
||
51 | ); |
||
52 | $this->setup_report( $filter ); |
||
53 | |||
54 | $report_data = $this->report->get_order_report_data( array( |
||
55 | 'data' => array( |
||
56 | '_product_id' => array( |
||
57 | 'type' => 'order_item_meta', |
||
58 | 'order_item_type' => 'line_item', |
||
59 | 'function' => '', |
||
60 | 'name' => 'product_id', |
||
61 | ), |
||
62 | '_qty' => array( |
||
63 | 'type' => 'order_item_meta', |
||
64 | 'order_item_type' => 'line_item', |
||
65 | 'function' => 'SUM', |
||
66 | 'name' => 'order_item_qty', |
||
67 | ) |
||
68 | ), |
||
69 | 'order_by' => 'order_item_qty DESC', |
||
70 | 'group_by' => 'product_id', |
||
71 | 'limit' => isset( $filter['limit'] ) ? absint( $filter['limit'] ) : 12, |
||
72 | 'query_type' => 'get_results', |
||
73 | 'filter_range' => true, |
||
74 | ) ); |
||
75 | |||
76 | $top_sellers = array(); |
||
77 | |||
78 | View Code Duplication | foreach ( $report_data as $item ) { |
|
0 ignored issues
–
show
|
|||
79 | $product = wc_get_product( $item->product_id ); |
||
80 | |||
81 | if ( $product ) { |
||
82 | $top_sellers[] = array( |
||
83 | 'name' => $product->get_title(), |
||
84 | 'product_id' => (int) $item->product_id, |
||
85 | 'quantity' => wc_stock_amount( $item->order_item_qty ), |
||
86 | ); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | $data = array(); |
||
91 | foreach ( $top_sellers as $top_seller ) { |
||
92 | $item = $this->prepare_item_for_response( (object) $top_seller, $request ); |
||
93 | $data[] = $this->prepare_response_for_collection( $item ); |
||
94 | } |
||
95 | |||
96 | return rest_ensure_response( $data ); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Prepare a report sales object for serialization. |
||
101 | * |
||
102 | * @param stdClass $top_seller |
||
103 | * @param WP_REST_Request $request Request object. |
||
104 | * @return WP_REST_Response $response Response data. |
||
105 | */ |
||
106 | View Code Duplication | public function prepare_item_for_response( $top_seller, $request ) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
107 | $data = array( |
||
108 | 'name' => $top_seller->name, |
||
109 | 'product_id' => $top_seller->product_id, |
||
110 | 'quantity' => $top_seller->quantity, |
||
111 | ); |
||
112 | |||
113 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
||
114 | $data = $this->add_additional_fields_to_object( $data, $request ); |
||
115 | $data = $this->filter_response_by_context( $data, $context ); |
||
116 | |||
117 | // Wrap the data in a response object. |
||
118 | $response = rest_ensure_response( $data ); |
||
119 | $response->add_links( array( |
||
120 | 'about' => array( |
||
121 | 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
||
122 | ), |
||
123 | 'product' => array( |
||
124 | 'href' => rest_url( sprintf( '/%s/products/%s', $this->namespace, $top_seller->product_id ) ), |
||
125 | ), |
||
126 | ) ); |
||
127 | |||
128 | /** |
||
129 | * Filter a report top sellers returned from the API. |
||
130 | * |
||
131 | * Allows modification of the report top sellers data right before it is returned. |
||
132 | * |
||
133 | * @param WP_REST_Response $response The response object. |
||
134 | * @param stdClass $top_seller The original report object. |
||
135 | * @param WP_REST_Request $request Request used to generate the response. |
||
136 | */ |
||
137 | return apply_filters( 'woocommerce_rest_prepare_report_top_sellers', $response, $top_seller, $request ); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get the Report's schema, conforming to JSON Schema. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function get_item_schema() { |
||
146 | $schema = array( |
||
147 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||
148 | 'title' => 'top_sellers_report', |
||
149 | 'type' => 'object', |
||
150 | 'properties' => array( |
||
151 | 'name' => array( |
||
152 | 'description' => __( 'Product name.', 'woocommerce' ), |
||
153 | 'type' => 'string', |
||
154 | 'context' => array( 'view' ), |
||
155 | 'readonly' => true, |
||
156 | ), |
||
157 | 'product_id' => array( |
||
158 | 'description' => __( 'Product ID.', 'woocommerce' ), |
||
159 | 'type' => 'integer', |
||
160 | 'context' => array( 'view' ), |
||
161 | 'readonly' => true, |
||
162 | ), |
||
163 | 'quantity' => array( |
||
164 | 'description' => __( 'Total number of purchases.', 'woocommerce' ), |
||
165 | 'type' => 'integer', |
||
166 | 'context' => array( 'view' ), |
||
167 | 'readonly' => true, |
||
168 | ), |
||
169 | ), |
||
170 | ); |
||
171 | |||
172 | return $this->add_additional_fields_schema( $schema ); |
||
173 | } |
||
174 | } |
||
175 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.