Code Duplication    Length = 81-84 lines in 3 locations

includes/api/legacy/v1/class-wc-api-server.php 1 location

@@ 295-375 (lines=81) @@
292
	 * @since 2.1
293
	 * @return mixed The value returned by the callback, or a WP_Error instance
294
	 */
295
	public function dispatch() {
296
297
		switch ( $this->method ) {
298
299
			case 'HEAD':
300
			case 'GET':
301
				$method = self::METHOD_GET;
302
				break;
303
304
			case 'POST':
305
				$method = self::METHOD_POST;
306
				break;
307
308
			case 'PUT':
309
				$method = self::METHOD_PUT;
310
				break;
311
312
			case 'PATCH':
313
				$method = self::METHOD_PATCH;
314
				break;
315
316
			case 'DELETE':
317
				$method = self::METHOD_DELETE;
318
				break;
319
320
			default:
321
				return new WP_Error( 'woocommerce_api_unsupported_method', __( 'Unsupported request method', 'woocommerce' ), array( 'status' => 400 ) );
322
		}
323
324
		foreach ( $this->get_routes() as $route => $handlers ) {
325
			foreach ( $handlers as $handler ) {
326
				$callback = $handler[0];
327
				$supported = isset( $handler[1] ) ? $handler[1] : self::METHOD_GET;
328
329
				if ( !( $supported & $method ) )
330
					continue;
331
332
				$match = preg_match( '@^' . $route . '$@i', urldecode( $this->path ), $args );
333
334
				if ( !$match )
335
					continue;
336
337
				if ( ! is_callable( $callback ) )
338
					return new WP_Error( 'woocommerce_api_invalid_handler', __( 'The handler for the route is invalid', 'woocommerce' ), array( 'status' => 500 ) );
339
340
				$args = array_merge( $args, $this->params['GET'] );
341
				if ( $method & self::METHOD_POST ) {
342
					$args = array_merge( $args, $this->params['POST'] );
343
				}
344
				if ( $supported & self::ACCEPT_DATA ) {
345
					$data = $this->handler->parse_body( $this->get_raw_data() );
346
					$args = array_merge( $args, array( 'data' => $data ) );
347
				}
348
				elseif ( $supported & self::ACCEPT_RAW_DATA ) {
349
					$data = $this->get_raw_data();
350
					$args = array_merge( $args, array( 'data' => $data ) );
351
				}
352
353
				$args['_method']  = $method;
354
				$args['_route']   = $route;
355
				$args['_path']    = $this->path;
356
				$args['_headers'] = $this->headers;
357
				$args['_files']   = $this->files;
358
359
				$args = apply_filters( 'woocommerce_api_dispatch_args', $args, $callback );
360
361
				// Allow plugins to halt the request via this filter
362
				if ( is_wp_error( $args ) ) {
363
					return $args;
364
				}
365
366
				$params = $this->sort_callback_params( $callback, $args );
367
				if ( is_wp_error( $params ) )
368
					return $params;
369
370
				return call_user_func_array( $callback, $params );
371
			}
372
		}
373
374
		return new WP_Error( 'woocommerce_api_no_route', __( 'No route was found matching the URL and request method', 'woocommerce' ), array( 'status' => 404 ) );
375
	}
376
377
	/**
378
	 * Sort parameters by order specified in method declaration

includes/api/legacy/v2/class-wc-api-server.php 1 location

@@ 292-375 (lines=84) @@
289
	 * @since 2.1
290
	 * @return mixed The value returned by the callback, or a WP_Error instance
291
	 */
292
	public function dispatch() {
293
294
		switch ( $this->method ) {
295
296
			case 'HEAD' :
297
			case 'GET' :
298
				$method = self::METHOD_GET;
299
				break;
300
301
			case 'POST' :
302
				$method = self::METHOD_POST;
303
				break;
304
305
			case 'PUT' :
306
				$method = self::METHOD_PUT;
307
				break;
308
309
			case 'PATCH' :
310
				$method = self::METHOD_PATCH;
311
				break;
312
313
			case 'DELETE' :
314
				$method = self::METHOD_DELETE;
315
				break;
316
317
			default :
318
				return new WP_Error( 'woocommerce_api_unsupported_method', __( 'Unsupported request method', 'woocommerce' ), array( 'status' => 400 ) );
319
		}
320
321
		foreach ( $this->get_routes() as $route => $handlers ) {
322
			foreach ( $handlers as $handler ) {
323
				$callback  = $handler[0];
324
				$supported = isset( $handler[1] ) ? $handler[1] : self::METHOD_GET;
325
326
				if ( ! ( $supported & $method ) ) {
327
					continue;
328
				}
329
330
				$match = preg_match( '@^' . $route . '$@i', urldecode( $this->path ), $args );
331
332
				if ( ! $match ) {
333
					continue;
334
				}
335
336
				if ( ! is_callable( $callback ) ) {
337
					return new WP_Error( 'woocommerce_api_invalid_handler', __( 'The handler for the route is invalid', 'woocommerce' ), array( 'status' => 500 ) );
338
				}
339
340
				$args = array_merge( $args, $this->params['GET'] );
341
				if ( $method & self::METHOD_POST ) {
342
					$args = array_merge( $args, $this->params['POST'] );
343
				}
344
				if ( $supported & self::ACCEPT_DATA ) {
345
					$data = $this->handler->parse_body( $this->get_raw_data() );
346
					$args = array_merge( $args, array( 'data' => $data ) );
347
				} elseif ( $supported & self::ACCEPT_RAW_DATA ) {
348
					$data = $this->get_raw_data();
349
					$args = array_merge( $args, array( 'data' => $data ) );
350
				}
351
352
				$args['_method']  = $method;
353
				$args['_route']   = $route;
354
				$args['_path']    = $this->path;
355
				$args['_headers'] = $this->headers;
356
				$args['_files']   = $this->files;
357
358
				$args = apply_filters( 'woocommerce_api_dispatch_args', $args, $callback );
359
360
				// Allow plugins to halt the request via this filter
361
				if ( is_wp_error( $args ) ) {
362
					return $args;
363
				}
364
365
				$params = $this->sort_callback_params( $callback, $args );
366
				if ( is_wp_error( $params ) ) {
367
					return $params;
368
				}
369
370
				return call_user_func_array( $callback, $params );
371
			}
372
		}
373
374
		return new WP_Error( 'woocommerce_api_no_route', __( 'No route was found matching the URL and request method', 'woocommerce' ), array( 'status' => 404 ) );
375
	}
376
377
	/**
378
	 * urldecode deep.

includes/api/legacy/v3/class-wc-api-server.php 1 location

@@ 292-375 (lines=84) @@
289
	 * @since 2.1
290
	 * @return mixed The value returned by the callback, or a WP_Error instance
291
	 */
292
	public function dispatch() {
293
294
		switch ( $this->method ) {
295
296
			case 'HEAD' :
297
			case 'GET' :
298
				$method = self::METHOD_GET;
299
				break;
300
301
			case 'POST' :
302
				$method = self::METHOD_POST;
303
				break;
304
305
			case 'PUT' :
306
				$method = self::METHOD_PUT;
307
				break;
308
309
			case 'PATCH' :
310
				$method = self::METHOD_PATCH;
311
				break;
312
313
			case 'DELETE' :
314
				$method = self::METHOD_DELETE;
315
				break;
316
317
			default :
318
				return new WP_Error( 'woocommerce_api_unsupported_method', __( 'Unsupported request method', 'woocommerce' ), array( 'status' => 400 ) );
319
		}
320
321
		foreach ( $this->get_routes() as $route => $handlers ) {
322
			foreach ( $handlers as $handler ) {
323
				$callback  = $handler[0];
324
				$supported = isset( $handler[1] ) ? $handler[1] : self::METHOD_GET;
325
326
				if ( ! ( $supported & $method ) ) {
327
					continue;
328
				}
329
330
				$match = preg_match( '@^' . $route . '$@i', urldecode( $this->path ), $args );
331
332
				if ( ! $match ) {
333
					continue;
334
				}
335
336
				if ( ! is_callable( $callback ) ) {
337
					return new WP_Error( 'woocommerce_api_invalid_handler', __( 'The handler for the route is invalid', 'woocommerce' ), array( 'status' => 500 ) );
338
				}
339
340
				$args = array_merge( $args, $this->params['GET'] );
341
				if ( $method & self::METHOD_POST ) {
342
					$args = array_merge( $args, $this->params['POST'] );
343
				}
344
				if ( $supported & self::ACCEPT_DATA ) {
345
					$data = $this->handler->parse_body( $this->get_raw_data() );
346
					$args = array_merge( $args, array( 'data' => $data ) );
347
				} elseif ( $supported & self::ACCEPT_RAW_DATA ) {
348
					$data = $this->get_raw_data();
349
					$args = array_merge( $args, array( 'data' => $data ) );
350
				}
351
352
				$args['_method']  = $method;
353
				$args['_route']   = $route;
354
				$args['_path']    = $this->path;
355
				$args['_headers'] = $this->headers;
356
				$args['_files']   = $this->files;
357
358
				$args = apply_filters( 'woocommerce_api_dispatch_args', $args, $callback );
359
360
				// Allow plugins to halt the request via this filter
361
				if ( is_wp_error( $args ) ) {
362
					return $args;
363
				}
364
365
				$params = $this->sort_callback_params( $callback, $args );
366
				if ( is_wp_error( $params ) ) {
367
					return $params;
368
				}
369
370
				return call_user_func_array( $callback, $params );
371
			}
372
		}
373
374
		return new WP_Error( 'woocommerce_api_no_route', __( 'No route was found matching the URL and request method', 'woocommerce' ), array( 'status' => 404 ) );
375
	}
376
377
	/**
378
	 * urldecode deep.