Code Duplication    Length = 58-59 lines in 4 locations

includes/api/class-wc-api-coupons.php 1 location

@@ 522-580 (lines=59) @@
519
	 * @param array $data
520
	 * @return array
521
	 */
522
	public function bulk( $data ) {
523
524
		try {
525
			if ( ! isset( $data['coupons'] ) ) {
526
				throw new WC_API_Exception( 'woocommerce_api_missing_coupons_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'coupons' ), 400 );
527
			}
528
529
			$data  = $data['coupons'];
530
			$limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'coupons' );
531
532
			// Limit bulk operation
533
			if ( count( $data ) > $limit ) {
534
				throw new WC_API_Exception( 'woocommerce_api_coupons_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request', 'woocommerce' ), $limit ), 413 );
535
			}
536
537
			$coupons = array();
538
539
			foreach ( $data as $_coupon ) {
540
				$coupon_id = 0;
541
542
				// Try to get the coupon ID
543
				if ( isset( $_coupon['id'] ) ) {
544
					$coupon_id = intval( $_coupon['id'] );
545
				}
546
547
				// Coupon exists / edit coupon
548
				if ( $coupon_id ) {
549
					$edit = $this->edit_coupon( $coupon_id, array( 'coupon' => $_coupon ) );
550
551
					if ( is_wp_error( $edit ) ) {
552
						$coupons[] = array(
553
							'id'    => $coupon_id,
554
							'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() )
555
						);
556
					} else {
557
						$coupons[] = $edit['coupon'];
558
					}
559
				}
560
561
				// Coupon don't exists / create coupon
562
				else {
563
					$new = $this->create_coupon( array( 'coupon' => $_coupon ) );
564
565
					if ( is_wp_error( $new ) ) {
566
						$coupons[] = array(
567
							'id'    => $coupon_id,
568
							'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() )
569
						);
570
					} else {
571
						$coupons[] = $new['coupon'];
572
					}
573
				}
574
			}
575
576
			return array( 'coupons' => apply_filters( 'woocommerce_api_coupons_bulk_response', $coupons, $this ) );
577
		} catch ( WC_API_Exception $e ) {
578
			return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
579
		}
580
	}
581
}
582

includes/api/class-wc-api-taxes.php 1 location

@@ 454-511 (lines=58) @@
451
	 *
452
	 * @return array
453
	 */
454
	public function bulk( $data ) {
455
		try {
456
			if ( ! isset( $data['taxes'] ) ) {
457
				throw new WC_API_Exception( 'woocommerce_api_missing_taxes_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'taxes' ), 400 );
458
			}
459
460
			$data  = $data['taxes'];
461
			$limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'taxes' );
462
463
			// Limit bulk operation
464
			if ( count( $data ) > $limit ) {
465
				throw new WC_API_Exception( 'woocommerce_api_taxes_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request', 'woocommerce' ), $limit ), 413 );
466
			}
467
468
			$taxes = array();
469
470
			foreach ( $data as $_tax ) {
471
				$tax_id = 0;
472
473
				// Try to get the tax rate ID
474
				if ( isset( $_tax['id'] ) ) {
475
					$tax_id = intval( $_tax['id'] );
476
				}
477
478
				// Tax rate exists / edit tax rate
479
				if ( $tax_id ) {
480
					$edit = $this->edit_tax( $tax_id, array( 'tax' => $_tax ) );
481
482
					if ( is_wp_error( $edit ) ) {
483
						$taxes[] = array(
484
							'id'    => $tax_id,
485
							'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() )
486
						);
487
					} else {
488
						$taxes[] = $edit['tax'];
489
					}
490
				}
491
492
				// Tax rate don't exists / create tax rate
493
				else {
494
					$new = $this->create_tax( array( 'tax' => $_tax ) );
495
496
					if ( is_wp_error( $new ) ) {
497
						$taxes[] = array(
498
							'id'    => $tax_id,
499
							'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() )
500
						);
501
					} else {
502
						$taxes[] = $new['tax'];
503
					}
504
				}
505
			}
506
507
			return array( 'taxes' => apply_filters( 'woocommerce_api_taxes_bulk_response', $taxes, $this ) );
508
		} catch ( WC_API_Exception $e ) {
509
			return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
510
		}
511
	}
512
513
	/**
514
	 * Get all tax classes

includes/api/class-wc-api-customers.php 1 location

@@ 774-832 (lines=59) @@
771
	 * @param array $data
772
	 * @return array
773
	 */
774
	public function bulk( $data ) {
775
776
		try {
777
			if ( ! isset( $data['customers'] ) ) {
778
				throw new WC_API_Exception( 'woocommerce_api_missing_customers_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'customers' ), 400 );
779
			}
780
781
			$data  = $data['customers'];
782
			$limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'customers' );
783
784
			// Limit bulk operation
785
			if ( count( $data ) > $limit ) {
786
				throw new WC_API_Exception( 'woocommerce_api_customers_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request', 'woocommerce' ), $limit ), 413 );
787
			}
788
789
			$customers = array();
790
791
			foreach ( $data as $_customer ) {
792
				$customer_id = 0;
793
794
				// Try to get the customer ID
795
				if ( isset( $_customer['id'] ) ) {
796
					$customer_id = intval( $_customer['id'] );
797
				}
798
799
				// Customer exists / edit customer
800
				if ( $customer_id ) {
801
					$edit = $this->edit_customer( $customer_id, array( 'customer' => $_customer ) );
802
803
					if ( is_wp_error( $edit ) ) {
804
						$customers[] = array(
805
							'id'    => $customer_id,
806
							'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() )
807
						);
808
					} else {
809
						$customers[] = $edit['customer'];
810
					}
811
				}
812
813
				// Customer don't exists / create customer
814
				else {
815
					$new = $this->create_customer( array( 'customer' => $_customer ) );
816
817
					if ( is_wp_error( $new ) ) {
818
						$customers[] = array(
819
							'id'    => $customer_id,
820
							'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() )
821
						);
822
					} else {
823
						$customers[] = $new['customer'];
824
					}
825
				}
826
			}
827
828
			return array( 'customers' => apply_filters( 'woocommerce_api_customers_bulk_response', $customers, $this ) );
829
		} catch ( WC_API_Exception $e ) {
830
			return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
831
		}
832
	}
833
}
834

includes/api/class-wc-api-orders.php 1 location

@@ 1804-1862 (lines=59) @@
1801
	 * @param array $data
1802
	 * @return array
1803
	 */
1804
	public function bulk( $data ) {
1805
1806
		try {
1807
			if ( ! isset( $data['orders'] ) ) {
1808
				throw new WC_API_Exception( 'woocommerce_api_missing_orders_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'orders' ), 400 );
1809
			}
1810
1811
			$data  = $data['orders'];
1812
			$limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'orders' );
1813
1814
			// Limit bulk operation
1815
			if ( count( $data ) > $limit ) {
1816
				throw new WC_API_Exception( 'woocommerce_api_orders_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request', 'woocommerce' ), $limit ), 413 );
1817
			}
1818
1819
			$orders = array();
1820
1821
			foreach ( $data as $_order ) {
1822
				$order_id = 0;
1823
1824
				// Try to get the order ID
1825
				if ( isset( $_order['id'] ) ) {
1826
					$order_id = intval( $_order['id'] );
1827
				}
1828
1829
				// Order exists / edit order
1830
				if ( $order_id ) {
1831
					$edit = $this->edit_order( $order_id, array( 'order' => $_order ) );
1832
1833
					if ( is_wp_error( $edit ) ) {
1834
						$orders[] = array(
1835
							'id'    => $order_id,
1836
							'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() )
1837
						);
1838
					} else {
1839
						$orders[] = $edit['order'];
1840
					}
1841
				}
1842
1843
				// Order don't exists / create order
1844
				else {
1845
					$new = $this->create_order( array( 'order' => $_order ) );
1846
1847
					if ( is_wp_error( $new ) ) {
1848
						$orders[] = array(
1849
							'id'    => $order_id,
1850
							'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() )
1851
						);
1852
					} else {
1853
						$orders[] = $new['order'];
1854
					}
1855
				}
1856
			}
1857
1858
			return array( 'orders' => apply_filters( 'woocommerce_api_orders_bulk_response', $orders, $this ) );
1859
		} catch ( WC_API_Exception $e ) {
1860
			return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
1861
		}
1862
	}
1863
}
1864