Code Duplication    Length = 68-68 lines in 2 locations

includes/api/class-wpinv-rest-invoice-controller.php 1 location

@@ 85-152 (lines=68) @@
82
	 * @param WP_REST_Request $request Full details about the request.
83
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
84
	 */
85
	public function get_items( $request ) {
86
		
87
		// Retrieve the list of registered invoice query parameters.
88
        $registered = $this->get_collection_params();
89
        
90
        $args       = array();
91
92
        foreach( array_keys( $registered ) as $key ) {
93
94
            if( isset( $request[ $key] ) ) {
95
                $args[ $key ] = $request[ $key];
96
            }
97
98
        }
99
100
		/**
101
		 * Filters the wpinv_get_invoices arguments for invoices requests.
102
		 *
103
		 *
104
		 * @since 1.0.13
105
		 *
106
		 *
107
		 * @param array           $args    Key value array of query var to query value.
108
		 * @param WP_REST_Request $request The request used.
109
		 */
110
        $args       = apply_filters( "wpinv_rest_get_invoices_arguments", $args, $request, $this );
111
		
112
		// Special args
113
		$args[ 'return' ]   = 'objects';
114
		$args[ 'paginate' ] = true;
115
116
        // Run the query.
117
		$query = wpinv_get_invoices( $args );
118
		
119
		// Prepare the retrieved invoices
120
		$invoices = array();
121
		foreach( $query->invoices as $invoice ) {
122
123
			if ( ! $this->check_read_permission( $invoice ) ) {
124
				continue;
125
			}
126
127
			$data       = $this->prepare_item_for_response( $invoice, $request );
128
			$invoices[] = $this->prepare_response_for_collection( $data );
129
130
		}
131
132
		// Prepare the response.
133
		$response = rest_ensure_response( $invoices );
134
		$response->header( 'X-WP-Total', (int) $query->total );
135
		$response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
136
137
		/**
138
		 * Filters the responses for invoices requests.
139
		 *
140
		 *
141
		 * @since 1.0.13
142
		 *
143
		 *
144
		 * @param arrWP_REST_Response $response    Response object.
145
		 * @param WP_REST_Request     $request The request used.
146
         * @param array               $args Array of args used to retrieve the invoices
147
		 */
148
        $response       = apply_filters( "wpinv_rest_invoices_response", $response, $request, $args );
149
150
        return rest_ensure_response( $response );
151
        
152
    }
153
154
    /**
155
	 * Get the post, if the ID is valid.

includes/api/class-wpinv-rest-items-controller.php 1 location

@@ 105-172 (lines=68) @@
102
	 * @param WP_REST_Request $request Full details about the request.
103
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
104
	 */
105
	public function get_items( $request ) {
106
		
107
		// Retrieve the list of registered item query parameters.
108
        $registered = $this->get_collection_params();
109
        
110
        $args       = array();
111
112
        foreach( array_keys( $registered ) as $key ) {
113
114
            if( isset( $request[ $key] ) ) {
115
                $args[ $key ] = $request[ $key];
116
            }
117
118
        }
119
120
		/**
121
		 * Filters the wpinv_get_items arguments for items rest requests.
122
		 *
123
		 *
124
		 * @since 1.0.13
125
		 *
126
		 *
127
		 * @param array           $args    Key value array of query var to query value.
128
		 * @param WP_REST_Request $request The request used.
129
		 */
130
        $args       = apply_filters( "wpinv_rest_get_items_arguments", $args, $request, $this );
131
		
132
		// Special args
133
		$args[ 'return' ]   = 'objects';
134
		$args[ 'paginate' ] = true;
135
136
        // Run the query.
137
		$query = wpinv_get_all_items( $args );
138
		
139
		// Prepare the retrieved items
140
		$items = array();
141
		foreach( $query->items as $item ) {
142
143
			if ( ! $this->check_read_permission( $item ) ) {
144
				continue;
145
			}
146
147
			$data       = $this->prepare_item_for_response( $item, $request );
148
			$items[]    = $this->prepare_response_for_collection( $data );
149
150
		}
151
152
		// Prepare the response.
153
		$response = rest_ensure_response( $items );
154
		$response->header( 'X-WP-Total', (int) $query->total );
155
		$response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
156
157
		/**
158
		 * Filters the responses for item requests.
159
		 *
160
		 *
161
		 * @since 1.0.13
162
		 *
163
		 *
164
		 * @param arrWP_REST_Response $response    Response object.
165
		 * @param WP_REST_Request     $request The request used.
166
         * @param array               $args Array of args used to retrieve the items
167
		 */
168
        $response       = apply_filters( "wpinv_rest_items_response", $response, $request, $args );
169
170
        return rest_ensure_response( $response );
171
        
172
    }
173
174
    /**
175
	 * Get the post, if the ID is valid.