| Total Complexity | 50 |
| Total Lines | 537 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ShippingZoneMethods often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ShippingZoneMethods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class ShippingZoneMethods extends AbstractShippingZonesController { |
||
| 20 | use SettingsTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Register the routes for Shipping Zone Methods. |
||
| 24 | */ |
||
| 25 | public function register_routes() { |
||
| 26 | register_rest_route( |
||
| 27 | $this->namespace, |
||
| 28 | '/' . $this->rest_base . '/(?P<zone_id>[\d]+)/methods', |
||
| 29 | array( |
||
| 30 | 'args' => array( |
||
| 31 | 'zone_id' => array( |
||
| 32 | 'description' => __( 'Unique ID for the zone.', 'woocommerce-rest-api' ), |
||
| 33 | 'type' => 'integer', |
||
| 34 | ), |
||
| 35 | ), |
||
| 36 | array( |
||
| 37 | 'methods' => \WP_REST_Server::READABLE, |
||
| 38 | 'callback' => array( $this, 'get_items' ), |
||
| 39 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
| 40 | ), |
||
| 41 | array( |
||
| 42 | 'methods' => \WP_REST_Server::CREATABLE, |
||
| 43 | 'callback' => array( $this, 'create_item' ), |
||
| 44 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
| 45 | 'args' => array_merge( |
||
| 46 | $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
||
| 47 | array( |
||
| 48 | 'method_id' => array( |
||
| 49 | 'required' => true, |
||
| 50 | 'readonly' => false, |
||
| 51 | 'description' => __( 'Shipping method ID.', 'woocommerce-rest-api' ), |
||
| 52 | ), |
||
| 53 | ) |
||
| 54 | ), |
||
| 55 | ), |
||
| 56 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
| 57 | ), |
||
| 58 | true |
||
| 59 | ); |
||
| 60 | |||
| 61 | register_rest_route( |
||
| 62 | $this->namespace, |
||
| 63 | '/' . $this->rest_base . '/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', |
||
| 64 | array( |
||
| 65 | 'args' => array( |
||
| 66 | 'zone_id' => array( |
||
| 67 | 'description' => __( 'Unique ID for the zone.', 'woocommerce-rest-api' ), |
||
| 68 | 'type' => 'integer', |
||
| 69 | ), |
||
| 70 | 'instance_id' => array( |
||
| 71 | 'description' => __( 'Unique ID for the instance.', 'woocommerce-rest-api' ), |
||
| 72 | 'type' => 'integer', |
||
| 73 | ), |
||
| 74 | ), |
||
| 75 | array( |
||
| 76 | 'methods' => \WP_REST_Server::READABLE, |
||
| 77 | 'callback' => array( $this, 'get_item' ), |
||
| 78 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
| 79 | ), |
||
| 80 | array( |
||
| 81 | 'methods' => \WP_REST_Server::EDITABLE, |
||
| 82 | 'callback' => array( $this, 'update_item' ), |
||
| 83 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
| 84 | 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
||
| 85 | ), |
||
| 86 | array( |
||
| 87 | 'methods' => \WP_REST_Server::DELETABLE, |
||
| 88 | 'callback' => array( $this, 'delete_item' ), |
||
| 89 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||
| 90 | 'args' => array( |
||
| 91 | 'force' => array( |
||
| 92 | 'default' => false, |
||
| 93 | 'type' => 'boolean', |
||
| 94 | 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce-rest-api' ), |
||
| 95 | ), |
||
| 96 | ), |
||
| 97 | ), |
||
| 98 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
| 99 | ), |
||
| 100 | true |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get a single Shipping Zone Method. |
||
| 106 | * |
||
| 107 | * @param \WP_REST_Request $request Request data. |
||
| 108 | * @return \WP_REST_Response|\WP_Error |
||
| 109 | */ |
||
| 110 | public function get_item( $request ) { |
||
| 111 | $zone = $this->get_zone( $request['zone_id'] ); |
||
| 112 | |||
| 113 | if ( is_wp_error( $zone ) ) { |
||
| 114 | return $zone; |
||
| 115 | } |
||
| 116 | |||
| 117 | $instance_id = (int) $request['instance_id']; |
||
| 118 | $methods = $zone->get_shipping_methods(); |
||
|
|
|||
| 119 | $method = false; |
||
| 120 | |||
| 121 | foreach ( $methods as $method_obj ) { |
||
| 122 | if ( $instance_id === $method_obj->instance_id ) { |
||
| 123 | $method = $method_obj; |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( false === $method ) { |
||
| 129 | return new \WP_Error( 'woocommerce_rest_shipping_zone_method_invalid', __( 'Resource does not exist.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 130 | } |
||
| 131 | |||
| 132 | $data = $this->prepare_item_for_response( $method, $request ); |
||
| 133 | |||
| 134 | return rest_ensure_response( $data ); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get all Shipping Zone Methods. |
||
| 139 | * |
||
| 140 | * @param \WP_REST_Request $request Request data. |
||
| 141 | * @return \WP_REST_Response|\WP_Error |
||
| 142 | */ |
||
| 143 | public function get_items( $request ) { |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Create a new shipping zone method instance. |
||
| 163 | * |
||
| 164 | * @param \WP_REST_Request $request Full details about the request. |
||
| 165 | * @return \WP_REST_Request|\WP_Error |
||
| 166 | */ |
||
| 167 | public function create_item( $request ) { |
||
| 168 | $method_id = $request['method_id']; |
||
| 169 | $zone = $this->get_zone( $request['zone_id'] ); |
||
| 170 | if ( is_wp_error( $zone ) ) { |
||
| 171 | return $zone; |
||
| 172 | } |
||
| 173 | |||
| 174 | $instance_id = $zone->add_shipping_method( $method_id ); |
||
| 175 | $methods = $zone->get_shipping_methods(); |
||
| 176 | $method = false; |
||
| 177 | foreach ( $methods as $method_obj ) { |
||
| 178 | if ( $instance_id === $method_obj->instance_id ) { |
||
| 179 | $method = $method_obj; |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | if ( false === $method ) { |
||
| 185 | return new \WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( 'Resource cannot be created.', 'woocommerce-rest-api' ), array( 'status' => 500 ) ); |
||
| 186 | } |
||
| 187 | |||
| 188 | $method = $this->update_fields( $instance_id, $method, $request ); |
||
| 189 | if ( is_wp_error( $method ) ) { |
||
| 190 | return $method; |
||
| 191 | } |
||
| 192 | |||
| 193 | $data = $this->prepare_item_for_response( $method, $request ); |
||
| 194 | return rest_ensure_response( $data ); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Delete a shipping method instance. |
||
| 199 | * |
||
| 200 | * @param \WP_REST_Request $request Full details about the request. |
||
| 201 | * @return \WP_Error|boolean |
||
| 202 | */ |
||
| 203 | public function delete_item( $request ) { |
||
| 204 | $zone = $this->get_zone( $request['zone_id'] ); |
||
| 205 | if ( is_wp_error( $zone ) ) { |
||
| 206 | return $zone; |
||
| 207 | } |
||
| 208 | |||
| 209 | $instance_id = (int) $request['instance_id']; |
||
| 210 | $force = $request['force']; |
||
| 211 | |||
| 212 | // We don't support trashing for this type, error out. |
||
| 213 | if ( ! $force ) { |
||
| 214 | return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Shipping methods do not support trashing.', 'woocommerce-rest-api' ), array( 'status' => 501 ) ); |
||
| 215 | } |
||
| 216 | |||
| 217 | $methods = $zone->get_shipping_methods(); |
||
| 218 | $method = false; |
||
| 219 | |||
| 220 | foreach ( $methods as $method_obj ) { |
||
| 221 | if ( $instance_id === $method_obj->instance_id ) { |
||
| 222 | $method = $method_obj; |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | if ( false === $method ) { |
||
| 228 | return new \WP_Error( 'woocommerce_rest_shipping_zone_method_invalid', __( 'Resource does not exist.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 229 | } |
||
| 230 | |||
| 231 | $method = $this->update_fields( $instance_id, $method, $request ); |
||
| 232 | if ( is_wp_error( $method ) ) { |
||
| 233 | return $method; |
||
| 234 | } |
||
| 235 | |||
| 236 | $request->set_param( 'context', 'view' ); |
||
| 237 | $previous = $this->prepare_item_for_response( $method, $request ); |
||
| 238 | |||
| 239 | // Actually delete. |
||
| 240 | $zone->delete_shipping_method( $instance_id ); |
||
| 241 | $response = new \WP_REST_Response(); |
||
| 242 | $response->set_data( |
||
| 243 | array( |
||
| 244 | 'deleted' => true, |
||
| 245 | 'previous' => $previous, |
||
| 246 | ) |
||
| 247 | ); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Fires after a method is deleted via the REST API. |
||
| 251 | * |
||
| 252 | * @param object $method |
||
| 253 | * @param \WP_REST_Response $response The response data. |
||
| 254 | * @param \WP_REST_Request $request The request sent to the API. |
||
| 255 | */ |
||
| 256 | do_action( 'woocommerce_rest_delete_shipping_zone_method', $method, $response, $request ); |
||
| 257 | |||
| 258 | return $response; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Update A Single Shipping Zone Method. |
||
| 263 | * |
||
| 264 | * @param \WP_REST_Request $request Request data. |
||
| 265 | * @return \WP_REST_Response|\WP_Error |
||
| 266 | */ |
||
| 267 | public function update_item( $request ) { |
||
| 268 | $zone = $this->get_zone( $request['zone_id'] ); |
||
| 269 | if ( is_wp_error( $zone ) ) { |
||
| 270 | return $zone; |
||
| 271 | } |
||
| 272 | |||
| 273 | $instance_id = (int) $request['instance_id']; |
||
| 274 | $methods = $zone->get_shipping_methods(); |
||
| 275 | $method = false; |
||
| 276 | |||
| 277 | foreach ( $methods as $method_obj ) { |
||
| 278 | if ( $instance_id === $method_obj->instance_id ) { |
||
| 279 | $method = $method_obj; |
||
| 280 | break; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | if ( false === $method ) { |
||
| 285 | return new \WP_Error( 'woocommerce_rest_shipping_zone_method_invalid', __( 'Resource does not exist.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 286 | } |
||
| 287 | |||
| 288 | $method = $this->update_fields( $instance_id, $method, $request ); |
||
| 289 | if ( is_wp_error( $method ) ) { |
||
| 290 | return $method; |
||
| 291 | } |
||
| 292 | |||
| 293 | $data = $this->prepare_item_for_response( $method, $request ); |
||
| 294 | return rest_ensure_response( $data ); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Updates settings, order, and enabled status on create. |
||
| 299 | * |
||
| 300 | * @param int $instance_id Instance ID. |
||
| 301 | * @param \WC_Shipping_Method $method Shipping method data. |
||
| 302 | * @param \WP_REST_Request $request Request data. |
||
| 303 | * |
||
| 304 | * @return WC_Shipping_Method |
||
| 305 | */ |
||
| 306 | public function update_fields( $instance_id, $method, $request ) { |
||
| 307 | global $wpdb; |
||
| 308 | |||
| 309 | // Update settings if present. |
||
| 310 | if ( isset( $request['settings'] ) ) { |
||
| 311 | $method->init_instance_settings(); |
||
| 312 | $instance_settings = $method->instance_settings; |
||
| 313 | $errors_found = false; |
||
| 314 | foreach ( $method->get_instance_form_fields() as $key => $field ) { |
||
| 315 | if ( isset( $request['settings'][ $key ] ) ) { |
||
| 316 | if ( is_callable( array( $this, 'validate_setting_' . $field['type'] . '_field' ) ) ) { |
||
| 317 | $value = $this->{'validate_setting_' . $field['type'] . '_field'}( $request['settings'][ $key ], $field ); |
||
| 318 | } else { |
||
| 319 | $value = $this->validate_setting_text_field( $request['settings'][ $key ], $field ); |
||
| 320 | } |
||
| 321 | if ( is_wp_error( $value ) ) { |
||
| 322 | $errors_found = true; |
||
| 323 | break; |
||
| 324 | } |
||
| 325 | $instance_settings[ $key ] = $value; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | if ( $errors_found ) { |
||
| 330 | return new \WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce-rest-api' ), array( 'status' => 400 ) ); |
||
| 331 | } |
||
| 332 | |||
| 333 | update_option( $method->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $method->id . '_instance_settings_values', $instance_settings, $method ) ); |
||
| 334 | } |
||
| 335 | |||
| 336 | // Update order. |
||
| 337 | if ( isset( $request['order'] ) ) { |
||
| 338 | $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'method_order' => absint( $request['order'] ) ), array( 'instance_id' => absint( $instance_id ) ) ); |
||
| 339 | $method->method_order = absint( $request['order'] ); |
||
| 340 | } |
||
| 341 | |||
| 342 | // Update if this method is enabled or not. |
||
| 343 | if ( isset( $request['enabled'] ) ) { |
||
| 344 | if ( $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'is_enabled' => $request['enabled'] ), array( 'instance_id' => absint( $instance_id ) ) ) ) { |
||
| 345 | do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method->id, $request['zone_id'], $request['enabled'] ); |
||
| 346 | $method->enabled = ( true === $request['enabled'] ? 'yes' : 'no' ); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | return $method; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get data for this object in the format of this endpoint's schema. |
||
| 355 | * |
||
| 356 | * @param array $object Object to prepare. |
||
| 357 | * @param \WP_REST_Request $request Request object. |
||
| 358 | * @return array Array of data in the correct format. |
||
| 359 | */ |
||
| 360 | protected function get_data_for_response( $object, $request ) { |
||
| 361 | return array( |
||
| 362 | 'id' => $object->instance_id, |
||
| 363 | 'instance_id' => $object->instance_id, |
||
| 364 | 'title' => $object->instance_settings['title'], |
||
| 365 | 'order' => $object->method_order, |
||
| 366 | 'enabled' => ( 'yes' === $object->enabled ), |
||
| 367 | 'method_id' => $object->id, |
||
| 368 | 'method_title' => $object->method_title, |
||
| 369 | 'method_description' => $object->method_description, |
||
| 370 | 'settings' => $this->get_settings( $object ), |
||
| 371 | ); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Prepare a single item for response. |
||
| 376 | * |
||
| 377 | * @param mixed $item Object used to create response. |
||
| 378 | * @param \WP_REST_Request $request Request object. |
||
| 379 | * @return \WP_REST_Response $response Response data. |
||
| 380 | */ |
||
| 381 | public function prepare_item_for_response( $item, $request ) { |
||
| 382 | $response = parent::prepare_item_for_response( $item, $request ); |
||
| 383 | $response = $this->prepare_response_for_collection( $response ); |
||
| 384 | return $response; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Return settings associated with this shipping zone method instance. |
||
| 389 | * |
||
| 390 | * @param WC_Shipping_Method $item Shipping method data. |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function get_settings( $item ) { |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Prepare links for the request. |
||
| 418 | * |
||
| 419 | * @param mixed $item Object to prepare. |
||
| 420 | * @param \WP_REST_Request $request Request object. |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | protected function prepare_links( $item, $request ) { |
||
| 424 | $base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $request['zone_id']; |
||
| 425 | $links = array( |
||
| 426 | 'self' => array( |
||
| 427 | 'href' => rest_url( $base . '/methods/' . $item->instance_id ), |
||
| 428 | ), |
||
| 429 | 'collection' => array( |
||
| 430 | 'href' => rest_url( $base . '/methods' ), |
||
| 431 | ), |
||
| 432 | 'describes' => array( |
||
| 433 | 'href' => rest_url( $base ), |
||
| 434 | ), |
||
| 435 | ); |
||
| 436 | |||
| 437 | return $links; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get the Shipping Zone Methods schema, conforming to JSON Schema |
||
| 442 | * |
||
| 443 | * @return array |
||
| 444 | */ |
||
| 445 | public function get_item_schema() { |
||
| 556 | } |
||
| 557 | } |
||
| 558 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.