@@ -20,31 +20,31 @@ discard block |
||
| 20 | 20 | class WPInv_REST_Invoice_Controller extends WP_REST_Posts_Controller { |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Post type. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $post_type = 'wpi_invoice'; |
|
| 23 | + * Post type. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $post_type = 'wpi_invoice'; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Cached results of get_item_schema. |
|
| 31 | - * |
|
| 32 | - * @since 1.0.13 |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - protected $schema; |
|
| 29 | + /** |
|
| 30 | + * Cached results of get_item_schema. |
|
| 31 | + * |
|
| 32 | + * @since 1.0.13 |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + protected $schema; |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | - * Constructor. |
|
| 39 | - * |
|
| 40 | - * @since 1.0.13 |
|
| 41 | - * |
|
| 42 | - * @param string $namespace Api Namespace |
|
| 43 | - */ |
|
| 44 | - public function __construct( $namespace ) { |
|
| 38 | + * Constructor. |
|
| 39 | + * |
|
| 40 | + * @since 1.0.13 |
|
| 41 | + * |
|
| 42 | + * @param string $namespace Api Namespace |
|
| 43 | + */ |
|
| 44 | + public function __construct( $namespace ) { |
|
| 45 | 45 | |
| 46 | 46 | // Set api namespace... |
| 47 | - $this->namespace = $namespace; |
|
| 47 | + $this->namespace = $namespace; |
|
| 48 | 48 | |
| 49 | 49 | // ... and the rest base |
| 50 | 50 | $this->rest_base = 'invoices'; |
@@ -52,39 +52,39 @@ discard block |
||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | - * Checks if a given request has access to read invoices. |
|
| 55 | + * Checks if a given request has access to read invoices. |
|
| 56 | 56 | * |
| 57 | - * |
|
| 58 | - * @since 1.0.13 |
|
| 59 | - * |
|
| 60 | - * @param WP_REST_Request $request Full details about the request. |
|
| 61 | - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
| 62 | - */ |
|
| 63 | - public function get_items_permissions_check( $request ) { |
|
| 57 | + * |
|
| 58 | + * @since 1.0.13 |
|
| 59 | + * |
|
| 60 | + * @param WP_REST_Request $request Full details about the request. |
|
| 61 | + * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
| 62 | + */ |
|
| 63 | + public function get_items_permissions_check( $request ) { |
|
| 64 | 64 | |
| 65 | 65 | $post_type = get_post_type_object( $this->post_type ); |
| 66 | 66 | |
| 67 | - if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) { |
|
| 68 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 69 | - } |
|
| 67 | + if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) { |
|
| 68 | + return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - // Read checks will be evaluated on a per invoice basis |
|
| 71 | + // Read checks will be evaluated on a per invoice basis |
|
| 72 | 72 | |
| 73 | - return true; |
|
| 73 | + return true; |
|
| 74 | 74 | |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | - * Retrieves a collection of invoices. |
|
| 79 | - * |
|
| 80 | - * @since 1.0.13 |
|
| 81 | - * |
|
| 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 ) { |
|
| 78 | + * Retrieves a collection of invoices. |
|
| 79 | + * |
|
| 80 | + * @since 1.0.13 |
|
| 81 | + * |
|
| 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 | 86 | |
| 87 | - // Retrieve the list of registered invoice query parameters. |
|
| 87 | + // Retrieve the list of registered invoice query parameters. |
|
| 88 | 88 | $registered = $this->get_collection_params(); |
| 89 | 89 | |
| 90 | 90 | $args = array(); |
@@ -97,54 +97,54 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 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 | - */ |
|
| 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 | 110 | $args = apply_filters( "wpinv_rest_get_invoices_arguments", $args, $request, $this ); |
| 111 | 111 | |
| 112 | - // Special args |
|
| 113 | - $args[ 'return' ] = 'objects'; |
|
| 114 | - $args[ 'paginate' ] = true; |
|
| 112 | + // Special args |
|
| 113 | + $args[ 'return' ] = 'objects'; |
|
| 114 | + $args[ 'paginate' ] = true; |
|
| 115 | 115 | |
| 116 | 116 | // Run the query. |
| 117 | - $query = wpinv_get_invoices( $args ); |
|
| 117 | + $query = wpinv_get_invoices( $args ); |
|
| 118 | 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. |
|
| 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 | 146 | * @param array $args Array of args used to retrieve the invoices |
| 147 | - */ |
|
| 147 | + */ |
|
| 148 | 148 | $response = apply_filters( "rest_wpinv_invoices_response", $response, $request, $args ); |
| 149 | 149 | |
| 150 | 150 | return rest_ensure_response( $response ); |
@@ -152,25 +152,25 @@ discard block |
||
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | - * Get the post, if the ID is valid. |
|
| 156 | - * |
|
| 157 | - * @since 1.0.13 |
|
| 158 | - * |
|
| 159 | - * @param int $invoice_id Supplied ID. |
|
| 160 | - * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise. |
|
| 161 | - */ |
|
| 162 | - protected function get_post( $invoice_id ) { |
|
| 155 | + * Get the post, if the ID is valid. |
|
| 156 | + * |
|
| 157 | + * @since 1.0.13 |
|
| 158 | + * |
|
| 159 | + * @param int $invoice_id Supplied ID. |
|
| 160 | + * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise. |
|
| 161 | + */ |
|
| 162 | + protected function get_post( $invoice_id ) { |
|
| 163 | 163 | |
| 164 | - $error = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 164 | + $error = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 165 | 165 | |
| 166 | 166 | // Ids start from 1 |
| 167 | 167 | if ( (int) $invoice_id <= 0 ) { |
| 168 | - return $error; |
|
| 169 | - } |
|
| 168 | + return $error; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - $invoice = wpinv_get_invoice( (int) $invoice_id ); |
|
| 172 | - if ( empty( $invoice ) ) { |
|
| 173 | - return $error; |
|
| 171 | + $invoice = wpinv_get_invoice( (int) $invoice_id ); |
|
| 172 | + if ( empty( $invoice ) ) { |
|
| 173 | + return $error; |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | return $invoice; |
@@ -178,88 +178,88 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | - * Checks if a given request has access to read an invoice. |
|
| 182 | - * |
|
| 183 | - * @since 1.0.13 |
|
| 184 | - * |
|
| 185 | - * @param WP_REST_Request $request Full details about the request. |
|
| 186 | - * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise. |
|
| 187 | - */ |
|
| 188 | - public function get_item_permissions_check( $request ) { |
|
| 181 | + * Checks if a given request has access to read an invoice. |
|
| 182 | + * |
|
| 183 | + * @since 1.0.13 |
|
| 184 | + * |
|
| 185 | + * @param WP_REST_Request $request Full details about the request. |
|
| 186 | + * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise. |
|
| 187 | + */ |
|
| 188 | + public function get_item_permissions_check( $request ) { |
|
| 189 | 189 | |
| 190 | 190 | // Retrieve the invoice object. |
| 191 | 191 | $invoice = $this->get_post( $request['id'] ); |
| 192 | 192 | |
| 193 | 193 | // Ensure it is valid. |
| 194 | - if ( is_wp_error( $invoice ) ) { |
|
| 195 | - return $invoice; |
|
| 196 | - } |
|
| 194 | + if ( is_wp_error( $invoice ) ) { |
|
| 195 | + return $invoice; |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - if ( $invoice ) { |
|
| 199 | - return $this->check_read_permission( $invoice ); |
|
| 200 | - } |
|
| 198 | + if ( $invoice ) { |
|
| 199 | + return $this->check_read_permission( $invoice ); |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - return true; |
|
| 202 | + return true; |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | /** |
| 206 | - * Checks if an invoice can be read. |
|
| 207 | - * |
|
| 208 | - * An invoice can be read by site admins and owners of the invoice |
|
| 209 | - * |
|
| 210 | - * |
|
| 211 | - * @since 1.0.13 |
|
| 212 | - * |
|
| 213 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 214 | - * @return bool Whether the post can be read. |
|
| 215 | - */ |
|
| 216 | - public function check_read_permission( $invoice ) { |
|
| 217 | - |
|
| 218 | - // An invoice can be read by an admin... |
|
| 219 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'view_invoices' ) ) { |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 206 | + * Checks if an invoice can be read. |
|
| 207 | + * |
|
| 208 | + * An invoice can be read by site admins and owners of the invoice |
|
| 209 | + * |
|
| 210 | + * |
|
| 211 | + * @since 1.0.13 |
|
| 212 | + * |
|
| 213 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 214 | + * @return bool Whether the post can be read. |
|
| 215 | + */ |
|
| 216 | + public function check_read_permission( $invoice ) { |
|
| 217 | + |
|
| 218 | + // An invoice can be read by an admin... |
|
| 219 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'view_invoices' ) ) { |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | 223 | // ... and the owner of the invoice |
| 224 | - if( get_current_user_id() ===(int) $invoice->get_user_id() ) { |
|
| 225 | - return true; |
|
| 226 | - } |
|
| 224 | + if( get_current_user_id() ===(int) $invoice->get_user_id() ) { |
|
| 225 | + return true; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - return false; |
|
| 228 | + return false; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | - * Retrieves a single invoice. |
|
| 233 | - * |
|
| 234 | - * @since 1.0.13 |
|
| 235 | - * |
|
| 236 | - * @param WP_REST_Request $request Full details about the request. |
|
| 237 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 238 | - */ |
|
| 239 | - public function get_item( $request ) { |
|
| 232 | + * Retrieves a single invoice. |
|
| 233 | + * |
|
| 234 | + * @since 1.0.13 |
|
| 235 | + * |
|
| 236 | + * @param WP_REST_Request $request Full details about the request. |
|
| 237 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 238 | + */ |
|
| 239 | + public function get_item( $request ) { |
|
| 240 | 240 | |
| 241 | 241 | // Fetch the invoice. |
| 242 | 242 | $invoice = $this->get_post( $request['id'] ); |
| 243 | 243 | |
| 244 | 244 | // Abort early if it does not exist |
| 245 | - if ( is_wp_error( $invoice ) ) { |
|
| 246 | - return $invoice; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - // Prepare the response |
|
| 250 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 251 | - $response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) ); |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Filters the responses for single invoice requests. |
|
| 255 | - * |
|
| 256 | - * |
|
| 257 | - * @since 1.0.13 |
|
| 258 | - * @var WP_HTTP_Response |
|
| 259 | - * |
|
| 260 | - * @param WP_HTTP_Response $response Response. |
|
| 261 | - * @param WP_REST_Request $request The request used. |
|
| 262 | - */ |
|
| 245 | + if ( is_wp_error( $invoice ) ) { |
|
| 246 | + return $invoice; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + // Prepare the response |
|
| 250 | + $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 251 | + $response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) ); |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Filters the responses for single invoice requests. |
|
| 255 | + * |
|
| 256 | + * |
|
| 257 | + * @since 1.0.13 |
|
| 258 | + * @var WP_HTTP_Response |
|
| 259 | + * |
|
| 260 | + * @param WP_HTTP_Response $response Response. |
|
| 261 | + * @param WP_REST_Request $request The request used. |
|
| 262 | + */ |
|
| 263 | 263 | $response = apply_filters( "rest_wpinv_get_invoice_response", $response, $request ); |
| 264 | 264 | |
| 265 | 265 | return rest_ensure_response( $response ); |
@@ -267,23 +267,23 @@ discard block |
||
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | - * Checks if a given request has access to create an invoice. |
|
| 271 | - * |
|
| 272 | - * @since 1.0.13 |
|
| 273 | - * |
|
| 274 | - * @param WP_REST_Request $request Full details about the request. |
|
| 275 | - * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
| 276 | - */ |
|
| 277 | - public function create_item_permissions_check( $request ) { |
|
| 270 | + * Checks if a given request has access to create an invoice. |
|
| 271 | + * |
|
| 272 | + * @since 1.0.13 |
|
| 273 | + * |
|
| 274 | + * @param WP_REST_Request $request Full details about the request. |
|
| 275 | + * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
| 276 | + */ |
|
| 277 | + public function create_item_permissions_check( $request ) { |
|
| 278 | 278 | |
| 279 | - if ( ! empty( $request['id'] ) ) { |
|
| 280 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 281 | - } |
|
| 279 | + if ( ! empty( $request['id'] ) ) { |
|
| 280 | + return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - $post_type = get_post_type_object( $this->post_type ); |
|
| 283 | + $post_type = get_post_type_object( $this->post_type ); |
|
| 284 | 284 | |
| 285 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
| 286 | - return new WP_Error( |
|
| 285 | + if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
| 286 | + return new WP_Error( |
|
| 287 | 287 | 'rest_cannot_create', |
| 288 | 288 | __( 'Sorry, you are not allowed to create invoices as this user.', 'invoicing' ), |
| 289 | 289 | array( |
@@ -292,88 +292,88 @@ discard block |
||
| 292 | 292 | ); |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - return true; |
|
| 295 | + return true; |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | /** |
| 299 | - * Creates a single invoice. |
|
| 300 | - * |
|
| 301 | - * @since 1.0.13 |
|
| 302 | - * |
|
| 303 | - * @param WP_REST_Request $request Full details about the request. |
|
| 304 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 305 | - */ |
|
| 306 | - public function create_item( $request ) { |
|
| 307 | - |
|
| 308 | - if ( ! empty( $request['id'] ) ) { |
|
| 309 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 310 | - } |
|
| 299 | + * Creates a single invoice. |
|
| 300 | + * |
|
| 301 | + * @since 1.0.13 |
|
| 302 | + * |
|
| 303 | + * @param WP_REST_Request $request Full details about the request. |
|
| 304 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 305 | + */ |
|
| 306 | + public function create_item( $request ) { |
|
| 307 | + |
|
| 308 | + if ( ! empty( $request['id'] ) ) { |
|
| 309 | + return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | - $request->set_param( 'context', 'edit' ); |
|
| 312 | + $request->set_param( 'context', 'edit' ); |
|
| 313 | 313 | |
| 314 | - // Prepare the updated data. |
|
| 315 | - $invoice_data = wp_unslash( $this->prepare_item_for_database( $request ) ); |
|
| 314 | + // Prepare the updated data. |
|
| 315 | + $invoice_data = wp_unslash( $this->prepare_item_for_database( $request ) ); |
|
| 316 | 316 | |
| 317 | - if ( is_wp_error( $invoice_data ) ) { |
|
| 318 | - return $invoice_data; |
|
| 319 | - } |
|
| 317 | + if ( is_wp_error( $invoice_data ) ) { |
|
| 318 | + return $invoice_data; |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - // Try creating the invoice |
|
| 321 | + // Try creating the invoice |
|
| 322 | 322 | $invoice = wpinv_insert_invoice( $invoice_data, true ); |
| 323 | 323 | |
| 324 | - if ( is_wp_error( $invoice ) ) { |
|
| 324 | + if ( is_wp_error( $invoice ) ) { |
|
| 325 | 325 | return $invoice; |
| 326 | - } |
|
| 327 | - |
|
| 328 | - // Prepare the response |
|
| 329 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Fires after a single invoice is created or updated via the REST API. |
|
| 333 | - * |
|
| 334 | - * @since 1.0.13 |
|
| 335 | - * |
|
| 336 | - * @param WPinv_Invoice $invoice Inserted or updated invoice object. |
|
| 337 | - * @param WP_REST_Request $request Request object. |
|
| 338 | - * @param bool $creating True when creating a post, false when updating. |
|
| 339 | - */ |
|
| 340 | - do_action( "wpinv_rest_insert_invoice", $invoice, $request, true ); |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Filters the responses for creating single invoice requests. |
|
| 344 | - * |
|
| 345 | - * |
|
| 346 | - * @since 1.0.13 |
|
| 347 | - * |
|
| 348 | - * |
|
| 349 | - * @param array $invoice_data Invoice properties. |
|
| 350 | - * @param WP_REST_Request $request The request used. |
|
| 351 | - */ |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + // Prepare the response |
|
| 329 | + $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Fires after a single invoice is created or updated via the REST API. |
|
| 333 | + * |
|
| 334 | + * @since 1.0.13 |
|
| 335 | + * |
|
| 336 | + * @param WPinv_Invoice $invoice Inserted or updated invoice object. |
|
| 337 | + * @param WP_REST_Request $request Request object. |
|
| 338 | + * @param bool $creating True when creating a post, false when updating. |
|
| 339 | + */ |
|
| 340 | + do_action( "wpinv_rest_insert_invoice", $invoice, $request, true ); |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Filters the responses for creating single invoice requests. |
|
| 344 | + * |
|
| 345 | + * |
|
| 346 | + * @since 1.0.13 |
|
| 347 | + * |
|
| 348 | + * |
|
| 349 | + * @param array $invoice_data Invoice properties. |
|
| 350 | + * @param WP_REST_Request $request The request used. |
|
| 351 | + */ |
|
| 352 | 352 | $response = apply_filters( "rest_wpinv_create_invoice_response", $response, $request ); |
| 353 | 353 | |
| 354 | 354 | return rest_ensure_response( $response ); |
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Checks if a given request has access to update an invoice. |
|
| 359 | - * |
|
| 360 | - * @since 1.0.13 |
|
| 361 | - * |
|
| 362 | - * @param WP_REST_Request $request Full details about the request. |
|
| 363 | - * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
| 364 | - */ |
|
| 365 | - public function update_item_permissions_check( $request ) { |
|
| 366 | - |
|
| 367 | - // Retrieve the invoice. |
|
| 368 | - $invoice = $this->get_post( $request['id'] ); |
|
| 369 | - if ( is_wp_error( $invoice ) ) { |
|
| 370 | - return $invoice; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - $post_type = get_post_type_object( $this->post_type ); |
|
| 374 | - |
|
| 375 | - if ( ! current_user_can( $post_type->cap->edit_post, $invoice->ID ) ) { |
|
| 376 | - return new WP_Error( |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Checks if a given request has access to update an invoice. |
|
| 359 | + * |
|
| 360 | + * @since 1.0.13 |
|
| 361 | + * |
|
| 362 | + * @param WP_REST_Request $request Full details about the request. |
|
| 363 | + * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
| 364 | + */ |
|
| 365 | + public function update_item_permissions_check( $request ) { |
|
| 366 | + |
|
| 367 | + // Retrieve the invoice. |
|
| 368 | + $invoice = $this->get_post( $request['id'] ); |
|
| 369 | + if ( is_wp_error( $invoice ) ) { |
|
| 370 | + return $invoice; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + $post_type = get_post_type_object( $this->post_type ); |
|
| 374 | + |
|
| 375 | + if ( ! current_user_can( $post_type->cap->edit_post, $invoice->ID ) ) { |
|
| 376 | + return new WP_Error( |
|
| 377 | 377 | 'rest_cannot_edit', |
| 378 | 378 | __( 'Sorry, you are not allowed to update this invoice.', 'invoicing' ), |
| 379 | 379 | array( |
@@ -382,162 +382,162 @@ discard block |
||
| 382 | 382 | ); |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | - return true; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Updates a single invoice. |
|
| 390 | - * |
|
| 391 | - * @since 1.0.13 |
|
| 392 | - * |
|
| 393 | - * @param WP_REST_Request $request Full details about the request. |
|
| 394 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 395 | - */ |
|
| 396 | - public function update_item( $request ) { |
|
| 385 | + return true; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Updates a single invoice. |
|
| 390 | + * |
|
| 391 | + * @since 1.0.13 |
|
| 392 | + * |
|
| 393 | + * @param WP_REST_Request $request Full details about the request. |
|
| 394 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 395 | + */ |
|
| 396 | + public function update_item( $request ) { |
|
| 397 | 397 | |
| 398 | - // Ensure the invoice exists. |
|
| 398 | + // Ensure the invoice exists. |
|
| 399 | 399 | $valid_check = $this->get_post( $request['id'] ); |
| 400 | 400 | |
| 401 | 401 | // Abort early if it does not exist |
| 402 | - if ( is_wp_error( $valid_check ) ) { |
|
| 403 | - return $valid_check; |
|
| 404 | - } |
|
| 402 | + if ( is_wp_error( $valid_check ) ) { |
|
| 403 | + return $valid_check; |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | - $request->set_param( 'context', 'edit' ); |
|
| 406 | + $request->set_param( 'context', 'edit' ); |
|
| 407 | 407 | |
| 408 | - // Prepare the updated data. |
|
| 409 | - $data_to_update = wp_unslash( $this->prepare_item_for_database( $request ) ); |
|
| 408 | + // Prepare the updated data. |
|
| 409 | + $data_to_update = wp_unslash( $this->prepare_item_for_database( $request ) ); |
|
| 410 | 410 | |
| 411 | - if ( is_wp_error( $data_to_update ) ) { |
|
| 412 | - return $data_to_update; |
|
| 413 | - } |
|
| 411 | + if ( is_wp_error( $data_to_update ) ) { |
|
| 412 | + return $data_to_update; |
|
| 413 | + } |
|
| 414 | 414 | |
| 415 | - // Abort if no invoice data is provided |
|
| 415 | + // Abort if no invoice data is provided |
|
| 416 | 416 | if( empty( $data_to_update ) ) { |
| 417 | 417 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - // Include the invoice ID |
|
| 421 | - $data_to_update['ID'] = $request['id']; |
|
| 422 | - |
|
| 423 | - // Update the invoice |
|
| 424 | - $updated_invoice = wpinv_update_invoice( $data_to_update, true ); |
|
| 425 | - |
|
| 426 | - // Incase the update operation failed... |
|
| 427 | - if ( is_wp_error( $updated_invoice ) ) { |
|
| 428 | - return $updated_invoice; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - // Prepare the response |
|
| 432 | - $response = $this->prepare_item_for_response( $updated_invoice, $request ); |
|
| 433 | - |
|
| 434 | - /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */ |
|
| 435 | - do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false ); |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Filters the responses for updating single invoice requests. |
|
| 439 | - * |
|
| 440 | - * |
|
| 441 | - * @since 1.0.13 |
|
| 442 | - * |
|
| 443 | - * |
|
| 444 | - * @param array $invoice_data Invoice properties. |
|
| 445 | - * @param WP_REST_Request $request The request used. |
|
| 446 | - */ |
|
| 420 | + // Include the invoice ID |
|
| 421 | + $data_to_update['ID'] = $request['id']; |
|
| 422 | + |
|
| 423 | + // Update the invoice |
|
| 424 | + $updated_invoice = wpinv_update_invoice( $data_to_update, true ); |
|
| 425 | + |
|
| 426 | + // Incase the update operation failed... |
|
| 427 | + if ( is_wp_error( $updated_invoice ) ) { |
|
| 428 | + return $updated_invoice; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + // Prepare the response |
|
| 432 | + $response = $this->prepare_item_for_response( $updated_invoice, $request ); |
|
| 433 | + |
|
| 434 | + /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */ |
|
| 435 | + do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false ); |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Filters the responses for updating single invoice requests. |
|
| 439 | + * |
|
| 440 | + * |
|
| 441 | + * @since 1.0.13 |
|
| 442 | + * |
|
| 443 | + * |
|
| 444 | + * @param array $invoice_data Invoice properties. |
|
| 445 | + * @param WP_REST_Request $request The request used. |
|
| 446 | + */ |
|
| 447 | 447 | $response = apply_filters( "wpinv_rest_update_invoice_response", $response, $request ); |
| 448 | 448 | |
| 449 | 449 | return rest_ensure_response( $response ); |
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Checks if a given request has access to delete an invoice. |
|
| 454 | - * |
|
| 455 | - * @since 1.0.13 |
|
| 456 | - * |
|
| 457 | - * @param WP_REST_Request $request Full details about the request. |
|
| 458 | - * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise. |
|
| 459 | - */ |
|
| 460 | - public function delete_item_permissions_check( $request ) { |
|
| 461 | - |
|
| 462 | - // Retrieve the invoice. |
|
| 463 | - $invoice = $this->get_post( $request['id'] ); |
|
| 464 | - if ( is_wp_error( $invoice ) ) { |
|
| 465 | - return $invoice; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - // Ensure the current user can delete invoices |
|
| 469 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'delete_invoices' ) ) { |
|
| 470 | - return new WP_Error( |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Checks if a given request has access to delete an invoice. |
|
| 454 | + * |
|
| 455 | + * @since 1.0.13 |
|
| 456 | + * |
|
| 457 | + * @param WP_REST_Request $request Full details about the request. |
|
| 458 | + * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise. |
|
| 459 | + */ |
|
| 460 | + public function delete_item_permissions_check( $request ) { |
|
| 461 | + |
|
| 462 | + // Retrieve the invoice. |
|
| 463 | + $invoice = $this->get_post( $request['id'] ); |
|
| 464 | + if ( is_wp_error( $invoice ) ) { |
|
| 465 | + return $invoice; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + // Ensure the current user can delete invoices |
|
| 469 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'delete_invoices' ) ) { |
|
| 470 | + return new WP_Error( |
|
| 471 | 471 | 'rest_cannot_delete', |
| 472 | 472 | __( 'Sorry, you are not allowed to delete this invoice.', 'invoicing' ), |
| 473 | 473 | array( |
| 474 | 474 | 'status' => rest_authorization_required_code(), |
| 475 | 475 | ) |
| 476 | 476 | ); |
| 477 | - } |
|
| 478 | - |
|
| 479 | - return true; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Deletes a single invoice. |
|
| 484 | - * |
|
| 485 | - * @since 1.0.13 |
|
| 486 | - * |
|
| 487 | - * @param WP_REST_Request $request Full details about the request. |
|
| 488 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 489 | - */ |
|
| 490 | - public function delete_item( $request ) { |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + return true; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Deletes a single invoice. |
|
| 484 | + * |
|
| 485 | + * @since 1.0.13 |
|
| 486 | + * |
|
| 487 | + * @param WP_REST_Request $request Full details about the request. |
|
| 488 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 489 | + */ |
|
| 490 | + public function delete_item( $request ) { |
|
| 491 | 491 | |
| 492 | - // Retrieve the invoice. |
|
| 493 | - $invoice = $this->get_post( $request['id'] ); |
|
| 494 | - if ( is_wp_error( $invoice ) ) { |
|
| 495 | - return $invoice; |
|
| 496 | - } |
|
| 492 | + // Retrieve the invoice. |
|
| 493 | + $invoice = $this->get_post( $request['id'] ); |
|
| 494 | + if ( is_wp_error( $invoice ) ) { |
|
| 495 | + return $invoice; |
|
| 496 | + } |
|
| 497 | 497 | |
| 498 | - $request->set_param( 'context', 'edit' ); |
|
| 498 | + $request->set_param( 'context', 'edit' ); |
|
| 499 | 499 | |
| 500 | - // Prepare the invoice id |
|
| 501 | - $id = $invoice->ID; |
|
| 500 | + // Prepare the invoice id |
|
| 501 | + $id = $invoice->ID; |
|
| 502 | 502 | |
| 503 | - // Prepare the response |
|
| 504 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 503 | + // Prepare the response |
|
| 504 | + $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 505 | 505 | |
| 506 | - // Check if the user wants to bypass the trash... |
|
| 507 | - $force_delete = (bool) $request['force']; |
|
| 506 | + // Check if the user wants to bypass the trash... |
|
| 507 | + $force_delete = (bool) $request['force']; |
|
| 508 | 508 | |
| 509 | - // Try deleting the invoice. |
|
| 510 | - $deleted = wp_delete_post( $id, $force_delete ); |
|
| 509 | + // Try deleting the invoice. |
|
| 510 | + $deleted = wp_delete_post( $id, $force_delete ); |
|
| 511 | 511 | |
| 512 | - // Abort early if we can't delete the invoice. |
|
| 513 | - if ( ! $deleted ) { |
|
| 514 | - return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
| 515 | - } |
|
| 512 | + // Abort early if we can't delete the invoice. |
|
| 513 | + if ( ! $deleted ) { |
|
| 514 | + return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
| 515 | + } |
|
| 516 | 516 | |
| 517 | - /** |
|
| 518 | - * Fires immediately after a single invoice is deleted or trashed via the REST API. |
|
| 519 | - * |
|
| 520 | - * |
|
| 521 | - * @since 1.0.13 |
|
| 522 | - * |
|
| 523 | - * @param WPInv_Invoice $invoice The deleted or trashed invoice. |
|
| 524 | - * @param WP_REST_Request $request The request sent to the API. |
|
| 525 | - */ |
|
| 526 | - do_action( "wpinv_rest_delete_invoice", $invoice, $request ); |
|
| 517 | + /** |
|
| 518 | + * Fires immediately after a single invoice is deleted or trashed via the REST API. |
|
| 519 | + * |
|
| 520 | + * |
|
| 521 | + * @since 1.0.13 |
|
| 522 | + * |
|
| 523 | + * @param WPInv_Invoice $invoice The deleted or trashed invoice. |
|
| 524 | + * @param WP_REST_Request $request The request sent to the API. |
|
| 525 | + */ |
|
| 526 | + do_action( "wpinv_rest_delete_invoice", $invoice, $request ); |
|
| 527 | 527 | |
| 528 | - return $response; |
|
| 528 | + return $response; |
|
| 529 | 529 | |
| 530 | - } |
|
| 530 | + } |
|
| 531 | 531 | |
| 532 | 532 | |
| 533 | 533 | /** |
| 534 | - * Retrieves the query params for the invoices collection. |
|
| 535 | - * |
|
| 536 | - * @since 1.0.13 |
|
| 537 | - * |
|
| 538 | - * @return array Collection parameters. |
|
| 539 | - */ |
|
| 540 | - public function get_collection_params() { |
|
| 534 | + * Retrieves the query params for the invoices collection. |
|
| 535 | + * |
|
| 536 | + * @since 1.0.13 |
|
| 537 | + * |
|
| 538 | + * @return array Collection parameters. |
|
| 539 | + */ |
|
| 540 | + public function get_collection_params() { |
|
| 541 | 541 | |
| 542 | 542 | $query_params = array( |
| 543 | 543 | |
@@ -555,22 +555,22 @@ discard block |
||
| 555 | 555 | |
| 556 | 556 | // User. |
| 557 | 557 | 'user' => array( |
| 558 | - 'description' => __( 'Limit result set to invoices for a specif user.', 'invoicing' ), |
|
| 559 | - 'type' => 'integer', |
|
| 558 | + 'description' => __( 'Limit result set to invoices for a specif user.', 'invoicing' ), |
|
| 559 | + 'type' => 'integer', |
|
| 560 | 560 | ), |
| 561 | 561 | |
| 562 | 562 | // Number of results per page |
| 563 | 563 | 'limit' => array( |
| 564 | - 'description' => __( 'Number of invoices to fetch.', 'invoicing' ), |
|
| 565 | - 'type' => 'integer', |
|
| 566 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
| 564 | + 'description' => __( 'Number of invoices to fetch.', 'invoicing' ), |
|
| 565 | + 'type' => 'integer', |
|
| 566 | + 'default' => (int) get_option( 'posts_per_page' ), |
|
| 567 | 567 | ), |
| 568 | 568 | |
| 569 | 569 | // Pagination |
| 570 | 570 | 'page' => array( |
| 571 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
| 572 | - 'type' => 'integer', |
|
| 573 | - 'default' => 1, |
|
| 571 | + 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
| 572 | + 'type' => 'integer', |
|
| 573 | + 'default' => 1, |
|
| 574 | 574 | ), |
| 575 | 575 | |
| 576 | 576 | // Exclude certain invoices |
@@ -606,790 +606,790 @@ discard block |
||
| 606 | 606 | ), |
| 607 | 607 | ); |
| 608 | 608 | |
| 609 | - /** |
|
| 610 | - * Filter collection parameters for the invoices controller. |
|
| 611 | - * |
|
| 612 | - * |
|
| 613 | - * @since 1.0.13 |
|
| 614 | - * |
|
| 615 | - * @param array $query_params JSON Schema-formatted collection parameters. |
|
| 616 | - * @param WP_Post_Type $post_type Post type object. |
|
| 617 | - */ |
|
| 618 | - return apply_filters( "rest_invoices_collection_params", $query_params, 'wpi-invoice' ); |
|
| 609 | + /** |
|
| 610 | + * Filter collection parameters for the invoices controller. |
|
| 611 | + * |
|
| 612 | + * |
|
| 613 | + * @since 1.0.13 |
|
| 614 | + * |
|
| 615 | + * @param array $query_params JSON Schema-formatted collection parameters. |
|
| 616 | + * @param WP_Post_Type $post_type Post type object. |
|
| 617 | + */ |
|
| 618 | + return apply_filters( "rest_invoices_collection_params", $query_params, 'wpi-invoice' ); |
|
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | /** |
| 622 | - * Checks if a given post type can be viewed or managed. |
|
| 623 | - * |
|
| 624 | - * @since 1.0.13 |
|
| 625 | - * |
|
| 626 | - * @param object|string $post_type Post type name or object. |
|
| 627 | - * @return bool Whether the post type is allowed in REST. |
|
| 628 | - */ |
|
| 629 | - protected function check_is_post_type_allowed( $post_type ) { |
|
| 630 | - return true; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * Prepares a single invoice for create or update. |
|
| 635 | - * |
|
| 636 | - * @since 1.0.13 |
|
| 637 | - * |
|
| 638 | - * @param WP_REST_Request $request Request object. |
|
| 639 | - * @return array|WP_Error Invoice Properties or WP_Error. |
|
| 640 | - */ |
|
| 641 | - protected function prepare_item_for_database( $request ) { |
|
| 642 | - $prepared_invoice = new stdClass(); |
|
| 643 | - |
|
| 644 | - // Post ID. |
|
| 645 | - if ( isset( $request['id'] ) ) { |
|
| 646 | - $existing_invoice = $this->get_post( $request['id'] ); |
|
| 647 | - if ( is_wp_error( $existing_invoice ) ) { |
|
| 648 | - return $existing_invoice; |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - $prepared_invoice->ID = $existing_invoice->ID; |
|
| 652 | - $prepared_invoice->invoice_id = $existing_invoice->ID; |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - $schema = $this->get_item_schema(); |
|
| 656 | - |
|
| 657 | - // Invoice owner. |
|
| 658 | - if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) { |
|
| 659 | - $prepared_invoice->user_id = (int) $request['user_id']; |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - // Cart details. |
|
| 663 | - if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) { |
|
| 664 | - $prepared_invoice->cart_details = (array) $request['cart_details']; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - // Invoice status. |
|
| 668 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
| 669 | - |
|
| 670 | - if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) { |
|
| 671 | - $prepared_invoice->status = $request['status']; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - // User info |
|
| 677 | - if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) { |
|
| 678 | - $prepared_invoice->user_info = array(); |
|
| 679 | - $user_info = (array) $request['user_info']; |
|
| 680 | - |
|
| 681 | - foreach( $user_info as $prop => $value ) { |
|
| 682 | - |
|
| 683 | - if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) { |
|
| 684 | - |
|
| 685 | - $prepared_invoice->user_info[$prop] = $value; |
|
| 622 | + * Checks if a given post type can be viewed or managed. |
|
| 623 | + * |
|
| 624 | + * @since 1.0.13 |
|
| 625 | + * |
|
| 626 | + * @param object|string $post_type Post type name or object. |
|
| 627 | + * @return bool Whether the post type is allowed in REST. |
|
| 628 | + */ |
|
| 629 | + protected function check_is_post_type_allowed( $post_type ) { |
|
| 630 | + return true; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * Prepares a single invoice for create or update. |
|
| 635 | + * |
|
| 636 | + * @since 1.0.13 |
|
| 637 | + * |
|
| 638 | + * @param WP_REST_Request $request Request object. |
|
| 639 | + * @return array|WP_Error Invoice Properties or WP_Error. |
|
| 640 | + */ |
|
| 641 | + protected function prepare_item_for_database( $request ) { |
|
| 642 | + $prepared_invoice = new stdClass(); |
|
| 643 | + |
|
| 644 | + // Post ID. |
|
| 645 | + if ( isset( $request['id'] ) ) { |
|
| 646 | + $existing_invoice = $this->get_post( $request['id'] ); |
|
| 647 | + if ( is_wp_error( $existing_invoice ) ) { |
|
| 648 | + return $existing_invoice; |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + $prepared_invoice->ID = $existing_invoice->ID; |
|
| 652 | + $prepared_invoice->invoice_id = $existing_invoice->ID; |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + $schema = $this->get_item_schema(); |
|
| 656 | + |
|
| 657 | + // Invoice owner. |
|
| 658 | + if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) { |
|
| 659 | + $prepared_invoice->user_id = (int) $request['user_id']; |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + // Cart details. |
|
| 663 | + if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) { |
|
| 664 | + $prepared_invoice->cart_details = (array) $request['cart_details']; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + // Invoice status. |
|
| 668 | + if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
| 669 | + |
|
| 670 | + if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) { |
|
| 671 | + $prepared_invoice->status = $request['status']; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + // User info |
|
| 677 | + if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) { |
|
| 678 | + $prepared_invoice->user_info = array(); |
|
| 679 | + $user_info = (array) $request['user_info']; |
|
| 680 | + |
|
| 681 | + foreach( $user_info as $prop => $value ) { |
|
| 682 | + |
|
| 683 | + if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) { |
|
| 684 | + |
|
| 685 | + $prepared_invoice->user_info[$prop] = $value; |
|
| 686 | 686 | |
| 687 | - } |
|
| 687 | + } |
|
| 688 | 688 | |
| 689 | - } |
|
| 689 | + } |
|
| 690 | 690 | |
| 691 | - } |
|
| 691 | + } |
|
| 692 | 692 | |
| 693 | - // IP |
|
| 694 | - if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) { |
|
| 695 | - $prepared_invoice->ip = $request['ip']; |
|
| 696 | - } |
|
| 693 | + // IP |
|
| 694 | + if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) { |
|
| 695 | + $prepared_invoice->ip = $request['ip']; |
|
| 696 | + } |
|
| 697 | 697 | |
| 698 | - // Payment details |
|
| 699 | - $prepared_invoice->payment_details = array(); |
|
| 698 | + // Payment details |
|
| 699 | + $prepared_invoice->payment_details = array(); |
|
| 700 | 700 | |
| 701 | - if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) { |
|
| 702 | - $prepared_invoice->payment_details['gateway'] = $request['gateway']; |
|
| 703 | - } |
|
| 701 | + if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) { |
|
| 702 | + $prepared_invoice->payment_details['gateway'] = $request['gateway']; |
|
| 703 | + } |
|
| 704 | 704 | |
| 705 | - if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) { |
|
| 706 | - $prepared_invoice->payment_details['gateway_title'] = $request['gateway_title']; |
|
| 707 | - } |
|
| 705 | + if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) { |
|
| 706 | + $prepared_invoice->payment_details['gateway_title'] = $request['gateway_title']; |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | - if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) { |
|
| 710 | - $prepared_invoice->payment_details['currency'] = $request['currency']; |
|
| 711 | - } |
|
| 709 | + if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) { |
|
| 710 | + $prepared_invoice->payment_details['currency'] = $request['currency']; |
|
| 711 | + } |
|
| 712 | 712 | |
| 713 | - if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) { |
|
| 714 | - $prepared_invoice->payment_details['transaction_id'] = $request['transaction_id']; |
|
| 715 | - } |
|
| 713 | + if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) { |
|
| 714 | + $prepared_invoice->payment_details['transaction_id'] = $request['transaction_id']; |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | - // Dates |
|
| 718 | - if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) { |
|
| 719 | - $post_date = rest_get_date_with_gmt( $request['date'] ); |
|
| 717 | + // Dates |
|
| 718 | + if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) { |
|
| 719 | + $post_date = rest_get_date_with_gmt( $request['date'] ); |
|
| 720 | 720 | |
| 721 | - if ( ! empty( $post_date ) ) { |
|
| 722 | - $prepared_invoice->post_date = $post_date[0]; |
|
| 723 | - } |
|
| 721 | + if ( ! empty( $post_date ) ) { |
|
| 722 | + $prepared_invoice->post_date = $post_date[0]; |
|
| 723 | + } |
|
| 724 | 724 | |
| 725 | - } |
|
| 725 | + } |
|
| 726 | 726 | |
| 727 | - if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) { |
|
| 728 | - $due_date = rest_get_date_with_gmt( $request['due_date'] ); |
|
| 727 | + if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) { |
|
| 728 | + $due_date = rest_get_date_with_gmt( $request['due_date'] ); |
|
| 729 | 729 | |
| 730 | - if ( ! empty( $due_date ) ) { |
|
| 731 | - $prepared_invoice->due_date = $due_date[0]; |
|
| 732 | - } |
|
| 730 | + if ( ! empty( $due_date ) ) { |
|
| 731 | + $prepared_invoice->due_date = $due_date[0]; |
|
| 732 | + } |
|
| 733 | 733 | |
| 734 | - } |
|
| 734 | + } |
|
| 735 | 735 | |
| 736 | - $invoice_data = (array) $prepared_invoice; |
|
| 736 | + $invoice_data = (array) $prepared_invoice; |
|
| 737 | 737 | |
| 738 | - /** |
|
| 739 | - * Filters an invoice before it is inserted via the REST API. |
|
| 740 | - * |
|
| 741 | - * @since 1.0.13 |
|
| 742 | - * |
|
| 743 | - * @param array $invoice_data An array of invoice data |
|
| 744 | - * @param WP_REST_Request $request Request object. |
|
| 745 | - */ |
|
| 746 | - return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request ); |
|
| 738 | + /** |
|
| 739 | + * Filters an invoice before it is inserted via the REST API. |
|
| 740 | + * |
|
| 741 | + * @since 1.0.13 |
|
| 742 | + * |
|
| 743 | + * @param array $invoice_data An array of invoice data |
|
| 744 | + * @param WP_REST_Request $request Request object. |
|
| 745 | + */ |
|
| 746 | + return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request ); |
|
| 747 | 747 | |
| 748 | - } |
|
| 748 | + } |
|
| 749 | 749 | |
| 750 | - /** |
|
| 751 | - * Prepares a single invoice output for response. |
|
| 752 | - * |
|
| 753 | - * @since 1.0.13 |
|
| 754 | - * |
|
| 755 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 756 | - * @param WP_REST_Request $request Request object. |
|
| 757 | - * @return WP_REST_Response Response object. |
|
| 758 | - */ |
|
| 759 | - public function prepare_item_for_response( $invoice, $request ) { |
|
| 750 | + /** |
|
| 751 | + * Prepares a single invoice output for response. |
|
| 752 | + * |
|
| 753 | + * @since 1.0.13 |
|
| 754 | + * |
|
| 755 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 756 | + * @param WP_REST_Request $request Request object. |
|
| 757 | + * @return WP_REST_Response Response object. |
|
| 758 | + */ |
|
| 759 | + public function prepare_item_for_response( $invoice, $request ) { |
|
| 760 | 760 | |
| 761 | - $GLOBALS['post'] = get_post( $invoice->ID ); |
|
| 761 | + $GLOBALS['post'] = get_post( $invoice->ID ); |
|
| 762 | 762 | |
| 763 | - setup_postdata( $invoice->ID ); |
|
| 763 | + setup_postdata( $invoice->ID ); |
|
| 764 | 764 | |
| 765 | - // Fetch the fields to include in this response. |
|
| 766 | - $fields = $this->get_fields_for_response( $request ); |
|
| 765 | + // Fetch the fields to include in this response. |
|
| 766 | + $fields = $this->get_fields_for_response( $request ); |
|
| 767 | 767 | |
| 768 | - // Base fields for every invoice. |
|
| 769 | - $data = array(); |
|
| 768 | + // Base fields for every invoice. |
|
| 769 | + $data = array(); |
|
| 770 | + |
|
| 771 | + // Set up ID |
|
| 772 | + if ( rest_is_field_included( 'id', $fields ) ) { |
|
| 773 | + $data['id'] = $invoice->ID; |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + |
|
| 777 | + // Basic properties |
|
| 778 | + $invoice_properties = array( |
|
| 779 | + 'title', 'email', 'ip', |
|
| 780 | + 'key', 'number', 'transaction_id', 'mode', |
|
| 781 | + 'gateway', 'gateway_title', |
|
| 782 | + 'total', 'discount', 'discount_code', |
|
| 783 | + 'tax', 'fees_total', 'subtotal', 'currency', |
|
| 784 | + 'status', 'status_nicename', 'post_type' |
|
| 785 | + ); |
|
| 786 | + |
|
| 787 | + foreach( $invoice_properties as $property ) { |
|
| 788 | + |
|
| 789 | + if ( rest_is_field_included( $property, $fields ) ) { |
|
| 790 | + $data[$property] = $invoice->get( $property ); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + // Cart details |
|
| 796 | + if ( rest_is_field_included( 'cart_details', $fields ) ) { |
|
| 797 | + $data['cart_details'] = $invoice->get( 'cart_details' ); |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + //Dates |
|
| 801 | + $invoice_properties = array( 'date', 'due_date', 'completed_date' ); |
|
| 802 | + |
|
| 803 | + foreach( $invoice_properties as $property ) { |
|
| 804 | + |
|
| 805 | + if ( rest_is_field_included( $property, $fields ) ) { |
|
| 806 | + $data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) ); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + // User id |
|
| 812 | + if ( rest_is_field_included( 'user_id', $fields ) ) { |
|
| 813 | + $data['user_id'] = (int) $invoice->get( 'user_id' ); |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + // User info |
|
| 817 | + $user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' ); |
|
| 818 | + |
|
| 819 | + foreach( $user_info as $property ) { |
|
| 820 | + |
|
| 821 | + if ( rest_is_field_included( "user_info.$property", $fields ) ) { |
|
| 822 | + $data['user_info'][$property] = $invoice->get( $property ); |
|
| 823 | + } |
|
| 824 | + |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + // Slug |
|
| 828 | + if ( rest_is_field_included( 'slug', $fields ) ) { |
|
| 829 | + $data['slug'] = $invoice->get( 'post_name' ); |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + // View invoice link |
|
| 833 | + if ( rest_is_field_included( 'link', $fields ) ) { |
|
| 834 | + $data['link'] = esc_url( $invoice->get_view_url() ); |
|
| 835 | + } |
|
| 770 | 836 | |
| 771 | - // Set up ID |
|
| 772 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
| 773 | - $data['id'] = $invoice->ID; |
|
| 774 | - } |
|
| 775 | 837 | |
| 838 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 839 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 840 | + $data = $this->filter_response_by_context( $data, $context ); |
|
| 776 | 841 | |
| 777 | - // Basic properties |
|
| 778 | - $invoice_properties = array( |
|
| 779 | - 'title', 'email', 'ip', |
|
| 780 | - 'key', 'number', 'transaction_id', 'mode', |
|
| 781 | - 'gateway', 'gateway_title', |
|
| 782 | - 'total', 'discount', 'discount_code', |
|
| 783 | - 'tax', 'fees_total', 'subtotal', 'currency', |
|
| 784 | - 'status', 'status_nicename', 'post_type' |
|
| 785 | - ); |
|
| 786 | - |
|
| 787 | - foreach( $invoice_properties as $property ) { |
|
| 788 | - |
|
| 789 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
| 790 | - $data[$property] = $invoice->get( $property ); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - // Cart details |
|
| 796 | - if ( rest_is_field_included( 'cart_details', $fields ) ) { |
|
| 797 | - $data['cart_details'] = $invoice->get( 'cart_details' ); |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - //Dates |
|
| 801 | - $invoice_properties = array( 'date', 'due_date', 'completed_date' ); |
|
| 802 | - |
|
| 803 | - foreach( $invoice_properties as $property ) { |
|
| 804 | - |
|
| 805 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
| 806 | - $data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) ); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - // User id |
|
| 812 | - if ( rest_is_field_included( 'user_id', $fields ) ) { |
|
| 813 | - $data['user_id'] = (int) $invoice->get( 'user_id' ); |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - // User info |
|
| 817 | - $user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' ); |
|
| 818 | - |
|
| 819 | - foreach( $user_info as $property ) { |
|
| 820 | - |
|
| 821 | - if ( rest_is_field_included( "user_info.$property", $fields ) ) { |
|
| 822 | - $data['user_info'][$property] = $invoice->get( $property ); |
|
| 823 | - } |
|
| 824 | - |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - // Slug |
|
| 828 | - if ( rest_is_field_included( 'slug', $fields ) ) { |
|
| 829 | - $data['slug'] = $invoice->get( 'post_name' ); |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - // View invoice link |
|
| 833 | - if ( rest_is_field_included( 'link', $fields ) ) { |
|
| 834 | - $data['link'] = esc_url( $invoice->get_view_url() ); |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - |
|
| 838 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 839 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 840 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 841 | - |
|
| 842 | - // Wrap the data in a response object. |
|
| 843 | - $response = rest_ensure_response( $data ); |
|
| 844 | - |
|
| 845 | - $links = $this->prepare_links( $invoice ); |
|
| 846 | - $response->add_links( $links ); |
|
| 847 | - |
|
| 848 | - if ( ! empty( $links['self']['href'] ) ) { |
|
| 849 | - $actions = $this->get_available_actions( $invoice, $request ); |
|
| 850 | - |
|
| 851 | - $self = $links['self']['href']; |
|
| 852 | - |
|
| 853 | - foreach ( $actions as $rel ) { |
|
| 854 | - $response->add_link( $rel, $self ); |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - /** |
|
| 859 | - * Filters the invoice data for a response. |
|
| 860 | - * |
|
| 861 | - * @since 1.0.13 |
|
| 862 | - * |
|
| 863 | - * @param WP_REST_Response $response The response object. |
|
| 864 | - * @param WPInv_Invoice $invoice The invoice object. |
|
| 865 | - * @param WP_REST_Request $request Request object. |
|
| 866 | - */ |
|
| 867 | - return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request ); |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - /** |
|
| 871 | - * Gets an array of fields to be included on the response. |
|
| 872 | - * |
|
| 873 | - * Included fields are based on item schema and `_fields=` request argument. |
|
| 874 | - * |
|
| 875 | - * @since 1.0.13 |
|
| 876 | - * |
|
| 877 | - * @param WP_REST_Request $request Full details about the request. |
|
| 878 | - * @return array Fields to be included in the response. |
|
| 879 | - */ |
|
| 880 | - public function get_fields_for_response( $request ) { |
|
| 881 | - $schema = $this->get_item_schema(); |
|
| 882 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 883 | - |
|
| 884 | - $additional_fields = $this->get_additional_fields(); |
|
| 885 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 886 | - // For back-compat, include any field with an empty schema |
|
| 887 | - // because it won't be present in $this->get_item_schema(). |
|
| 888 | - if ( is_null( $field_options['schema'] ) ) { |
|
| 889 | - $properties[ $field_name ] = $field_options; |
|
| 890 | - } |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - // Exclude fields that specify a different context than the request context. |
|
| 894 | - $context = $request['context']; |
|
| 895 | - if ( $context ) { |
|
| 896 | - foreach ( $properties as $name => $options ) { |
|
| 897 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 898 | - unset( $properties[ $name ] ); |
|
| 899 | - } |
|
| 900 | - } |
|
| 901 | - } |
|
| 902 | - |
|
| 903 | - $fields = array_keys( $properties ); |
|
| 904 | - |
|
| 905 | - if ( ! isset( $request['_fields'] ) ) { |
|
| 906 | - return $fields; |
|
| 907 | - } |
|
| 908 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 909 | - if ( 0 === count( $requested_fields ) ) { |
|
| 910 | - return $fields; |
|
| 911 | - } |
|
| 912 | - // Trim off outside whitespace from the comma delimited list. |
|
| 913 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 914 | - // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
| 915 | - if ( in_array( 'id', $fields, true ) ) { |
|
| 916 | - $requested_fields[] = 'id'; |
|
| 917 | - } |
|
| 918 | - // Return the list of all requested fields which appear in the schema. |
|
| 919 | - return array_reduce( |
|
| 920 | - $requested_fields, |
|
| 921 | - function( $response_fields, $field ) use ( $fields ) { |
|
| 922 | - if ( in_array( $field, $fields, true ) ) { |
|
| 923 | - $response_fields[] = $field; |
|
| 924 | - return $response_fields; |
|
| 925 | - } |
|
| 926 | - // Check for nested fields if $field is not a direct match. |
|
| 927 | - $nested_fields = explode( '.', $field ); |
|
| 928 | - // A nested field is included so long as its top-level property is |
|
| 929 | - // present in the schema. |
|
| 930 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 931 | - $response_fields[] = $field; |
|
| 932 | - } |
|
| 933 | - return $response_fields; |
|
| 934 | - }, |
|
| 935 | - array() |
|
| 936 | - ); |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - /** |
|
| 940 | - * Retrieves the invoice's schema, conforming to JSON Schema. |
|
| 941 | - * |
|
| 942 | - * @since 1.0.13 |
|
| 943 | - * |
|
| 944 | - * @return array Invoice schema data. |
|
| 945 | - */ |
|
| 946 | - public function get_item_schema() { |
|
| 947 | - |
|
| 948 | - // Maybe retrieve the schema from cache. |
|
| 949 | - if ( $this->schema ) { |
|
| 950 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - $schema = array( |
|
| 954 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 955 | - 'title' => $this->post_type, |
|
| 956 | - 'type' => 'object', |
|
| 957 | - |
|
| 958 | - // Base properties for every Invoice. |
|
| 959 | - 'properties' => array( |
|
| 960 | - |
|
| 961 | - 'title' => array( |
|
| 962 | - 'description' => __( 'The title for the invoice.', 'invoicing' ), |
|
| 963 | - 'type' => 'string', |
|
| 964 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 965 | - 'readonly' => true, |
|
| 966 | - ), |
|
| 967 | - |
|
| 968 | - 'user_id' => array( |
|
| 969 | - 'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ), |
|
| 970 | - 'type' => 'integer', |
|
| 971 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 972 | - ), |
|
| 973 | - |
|
| 974 | - 'email' => array( |
|
| 975 | - 'description' => __( 'The email of the owner of the invoice.', 'invoicing' ), |
|
| 976 | - 'type' => 'string', |
|
| 977 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 978 | - 'readonly' => true, |
|
| 979 | - ), |
|
| 980 | - |
|
| 981 | - 'ip' => array( |
|
| 982 | - 'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ), |
|
| 983 | - 'type' => 'string', |
|
| 984 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 985 | - ), |
|
| 986 | - |
|
| 987 | - 'user_info' => array( |
|
| 988 | - 'description' => __( 'Information about the owner of the invoice.', 'invoicing' ), |
|
| 989 | - 'type' => 'object', |
|
| 990 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 991 | - 'properties' => array( |
|
| 992 | - |
|
| 993 | - 'first_name' => array( |
|
| 994 | - 'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ), |
|
| 995 | - 'type' => 'string', |
|
| 996 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 997 | - ), |
|
| 998 | - |
|
| 999 | - 'last_name' => array( |
|
| 1000 | - 'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ), |
|
| 1001 | - 'type' => 'string', |
|
| 1002 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1003 | - ), |
|
| 1004 | - |
|
| 1005 | - 'company' => array( |
|
| 1006 | - 'description' => __( 'The company of the owner of the invoice.', 'invoicing' ), |
|
| 1007 | - 'type' => 'string', |
|
| 1008 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1009 | - ), |
|
| 1010 | - |
|
| 1011 | - 'vat_number' => array( |
|
| 1012 | - 'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ), |
|
| 1013 | - 'type' => 'string', |
|
| 1014 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1015 | - ), |
|
| 1016 | - |
|
| 1017 | - 'vat_rate' => array( |
|
| 1018 | - 'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ), |
|
| 1019 | - 'type' => 'string', |
|
| 1020 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1021 | - ), |
|
| 1022 | - |
|
| 1023 | - 'address' => array( |
|
| 1024 | - 'description' => __( 'The address of the invoice owner.', 'invoicing' ), |
|
| 1025 | - 'type' => 'string', |
|
| 1026 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1027 | - ), |
|
| 1028 | - |
|
| 1029 | - 'city' => array( |
|
| 1030 | - 'description' => __( 'The city of the invoice owner.', 'invoicing' ), |
|
| 1031 | - 'type' => 'string', |
|
| 1032 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1033 | - ), |
|
| 1034 | - |
|
| 1035 | - 'country' => array( |
|
| 1036 | - 'description' => __( 'The country of the invoice owner.', 'invoicing' ), |
|
| 1037 | - 'type' => 'string', |
|
| 1038 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1039 | - ), |
|
| 1040 | - |
|
| 1041 | - 'state' => array( |
|
| 1042 | - 'description' => __( 'The state of the invoice owner.', 'invoicing' ), |
|
| 1043 | - 'type' => 'string', |
|
| 1044 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1045 | - ), |
|
| 1046 | - |
|
| 1047 | - 'zip' => array( |
|
| 1048 | - 'description' => __( 'The zip code of the invoice owner.', 'invoicing' ), |
|
| 1049 | - 'type' => 'string', |
|
| 1050 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1051 | - ), |
|
| 1052 | - |
|
| 1053 | - 'phone' => array( |
|
| 1054 | - 'description' => __( 'The phone number of the invoice owner.', 'invoicing' ), |
|
| 1055 | - 'type' => 'string', |
|
| 1056 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1057 | - ), |
|
| 1058 | - ), |
|
| 1059 | - ), |
|
| 1060 | - |
|
| 1061 | - 'id' => array( |
|
| 1062 | - 'description' => __( 'Unique identifier for the invoice.', 'invoicing' ), |
|
| 1063 | - 'type' => 'integer', |
|
| 1064 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1065 | - 'readonly' => true, |
|
| 1066 | - ), |
|
| 1067 | - |
|
| 1068 | - 'key' => array( |
|
| 1069 | - 'description' => __( 'A unique key for the invoice.', 'invoicing' ), |
|
| 1070 | - 'type' => 'string', |
|
| 1071 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1072 | - 'readonly' => true, |
|
| 1073 | - ), |
|
| 1074 | - |
|
| 1075 | - 'number' => array( |
|
| 1076 | - 'description' => __( 'The invoice number.', 'invoicing' ), |
|
| 1077 | - 'type' => 'string', |
|
| 1078 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1079 | - 'readonly' => true, |
|
| 1080 | - ), |
|
| 1081 | - |
|
| 1082 | - 'transaction_id' => array( |
|
| 1083 | - 'description' => __( 'The transaction id of the invoice.', 'invoicing' ), |
|
| 1084 | - 'type' => 'string', |
|
| 1085 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1086 | - ), |
|
| 1087 | - |
|
| 1088 | - 'gateway' => array( |
|
| 1089 | - 'description' => __( 'The gateway used to process the invoice.', 'invoicing' ), |
|
| 1090 | - 'type' => 'string', |
|
| 1091 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1092 | - ), |
|
| 1093 | - |
|
| 1094 | - 'gateway_title' => array( |
|
| 1095 | - 'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ), |
|
| 1096 | - 'type' => 'string', |
|
| 1097 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1098 | - ), |
|
| 1099 | - |
|
| 1100 | - 'total' => array( |
|
| 1101 | - 'description' => __( 'The total amount of the invoice.', 'invoicing' ), |
|
| 1102 | - 'type' => 'number', |
|
| 1103 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1104 | - 'readonly' => true, |
|
| 1105 | - ), |
|
| 1106 | - |
|
| 1107 | - 'discount' => array( |
|
| 1108 | - 'description' => __( 'The discount applied to the invoice.', 'invoicing' ), |
|
| 1109 | - 'type' => 'number', |
|
| 1110 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1111 | - 'readonly' => true, |
|
| 1112 | - ), |
|
| 1113 | - |
|
| 1114 | - 'discount_code' => array( |
|
| 1115 | - 'description' => __( 'The discount code applied to the invoice.', 'invoicing' ), |
|
| 1116 | - 'type' => 'string', |
|
| 1117 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1118 | - 'readonly' => true, |
|
| 1119 | - ), |
|
| 1120 | - |
|
| 1121 | - 'tax' => array( |
|
| 1122 | - 'description' => __( 'The tax applied to the invoice.', 'invoicing' ), |
|
| 1123 | - 'type' => 'number', |
|
| 1124 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1125 | - 'readonly' => true, |
|
| 1126 | - ), |
|
| 1127 | - |
|
| 1128 | - 'fees_total' => array( |
|
| 1129 | - 'description' => __( 'The total fees applied to the invoice.', 'invoicing' ), |
|
| 1130 | - 'type' => 'number', |
|
| 1131 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1132 | - 'readonly' => true, |
|
| 1133 | - ), |
|
| 1134 | - |
|
| 1135 | - 'subtotal' => array( |
|
| 1136 | - 'description' => __( 'The sub-total for the invoice.', 'invoicing' ), |
|
| 1137 | - 'type' => 'number', |
|
| 1138 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1139 | - 'readonly' => true, |
|
| 1140 | - ), |
|
| 1141 | - |
|
| 1142 | - 'currency' => array( |
|
| 1143 | - 'description' => __( 'The currency used to process the invoice.', 'invoicing' ), |
|
| 1144 | - 'type' => 'string', |
|
| 1145 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1146 | - ), |
|
| 1147 | - |
|
| 1148 | - 'cart_details' => array( |
|
| 1149 | - 'description' => __( 'The cart details for invoice.', 'invoicing' ), |
|
| 1150 | - 'type' => 'array', |
|
| 1151 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1152 | - 'required' => true, |
|
| 1153 | - ), |
|
| 1154 | - |
|
| 1155 | - 'date' => array( |
|
| 1156 | - 'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ), |
|
| 1157 | - 'type' => array( 'string', 'null' ), |
|
| 1158 | - 'format' => 'date-time', |
|
| 1159 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1160 | - ), |
|
| 1161 | - |
|
| 1162 | - 'due_date' => array( |
|
| 1163 | - 'description' => __( 'The due date for the invoice.', 'invoicing' ), |
|
| 1164 | - 'type' => array( 'string', 'null' ), |
|
| 1165 | - 'format' => 'date-time', |
|
| 1166 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1167 | - ), |
|
| 1168 | - |
|
| 1169 | - 'completed_date' => array( |
|
| 1170 | - 'description' => __( 'The completed date for the invoice.', 'invoicing' ), |
|
| 1171 | - 'type' => array( 'string', 'null' ), |
|
| 1172 | - 'format' => 'date-time', |
|
| 1173 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1174 | - 'readonly' => true, |
|
| 1175 | - ), |
|
| 842 | + // Wrap the data in a response object. |
|
| 843 | + $response = rest_ensure_response( $data ); |
|
| 844 | + |
|
| 845 | + $links = $this->prepare_links( $invoice ); |
|
| 846 | + $response->add_links( $links ); |
|
| 847 | + |
|
| 848 | + if ( ! empty( $links['self']['href'] ) ) { |
|
| 849 | + $actions = $this->get_available_actions( $invoice, $request ); |
|
| 850 | + |
|
| 851 | + $self = $links['self']['href']; |
|
| 852 | + |
|
| 853 | + foreach ( $actions as $rel ) { |
|
| 854 | + $response->add_link( $rel, $self ); |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + /** |
|
| 859 | + * Filters the invoice data for a response. |
|
| 860 | + * |
|
| 861 | + * @since 1.0.13 |
|
| 862 | + * |
|
| 863 | + * @param WP_REST_Response $response The response object. |
|
| 864 | + * @param WPInv_Invoice $invoice The invoice object. |
|
| 865 | + * @param WP_REST_Request $request Request object. |
|
| 866 | + */ |
|
| 867 | + return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request ); |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + /** |
|
| 871 | + * Gets an array of fields to be included on the response. |
|
| 872 | + * |
|
| 873 | + * Included fields are based on item schema and `_fields=` request argument. |
|
| 874 | + * |
|
| 875 | + * @since 1.0.13 |
|
| 876 | + * |
|
| 877 | + * @param WP_REST_Request $request Full details about the request. |
|
| 878 | + * @return array Fields to be included in the response. |
|
| 879 | + */ |
|
| 880 | + public function get_fields_for_response( $request ) { |
|
| 881 | + $schema = $this->get_item_schema(); |
|
| 882 | + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 883 | + |
|
| 884 | + $additional_fields = $this->get_additional_fields(); |
|
| 885 | + foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 886 | + // For back-compat, include any field with an empty schema |
|
| 887 | + // because it won't be present in $this->get_item_schema(). |
|
| 888 | + if ( is_null( $field_options['schema'] ) ) { |
|
| 889 | + $properties[ $field_name ] = $field_options; |
|
| 890 | + } |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + // Exclude fields that specify a different context than the request context. |
|
| 894 | + $context = $request['context']; |
|
| 895 | + if ( $context ) { |
|
| 896 | + foreach ( $properties as $name => $options ) { |
|
| 897 | + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 898 | + unset( $properties[ $name ] ); |
|
| 899 | + } |
|
| 900 | + } |
|
| 901 | + } |
|
| 902 | + |
|
| 903 | + $fields = array_keys( $properties ); |
|
| 904 | + |
|
| 905 | + if ( ! isset( $request['_fields'] ) ) { |
|
| 906 | + return $fields; |
|
| 907 | + } |
|
| 908 | + $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 909 | + if ( 0 === count( $requested_fields ) ) { |
|
| 910 | + return $fields; |
|
| 911 | + } |
|
| 912 | + // Trim off outside whitespace from the comma delimited list. |
|
| 913 | + $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 914 | + // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
| 915 | + if ( in_array( 'id', $fields, true ) ) { |
|
| 916 | + $requested_fields[] = 'id'; |
|
| 917 | + } |
|
| 918 | + // Return the list of all requested fields which appear in the schema. |
|
| 919 | + return array_reduce( |
|
| 920 | + $requested_fields, |
|
| 921 | + function( $response_fields, $field ) use ( $fields ) { |
|
| 922 | + if ( in_array( $field, $fields, true ) ) { |
|
| 923 | + $response_fields[] = $field; |
|
| 924 | + return $response_fields; |
|
| 925 | + } |
|
| 926 | + // Check for nested fields if $field is not a direct match. |
|
| 927 | + $nested_fields = explode( '.', $field ); |
|
| 928 | + // A nested field is included so long as its top-level property is |
|
| 929 | + // present in the schema. |
|
| 930 | + if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 931 | + $response_fields[] = $field; |
|
| 932 | + } |
|
| 933 | + return $response_fields; |
|
| 934 | + }, |
|
| 935 | + array() |
|
| 936 | + ); |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + /** |
|
| 940 | + * Retrieves the invoice's schema, conforming to JSON Schema. |
|
| 941 | + * |
|
| 942 | + * @since 1.0.13 |
|
| 943 | + * |
|
| 944 | + * @return array Invoice schema data. |
|
| 945 | + */ |
|
| 946 | + public function get_item_schema() { |
|
| 947 | + |
|
| 948 | + // Maybe retrieve the schema from cache. |
|
| 949 | + if ( $this->schema ) { |
|
| 950 | + return $this->add_additional_fields_schema( $this->schema ); |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + $schema = array( |
|
| 954 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 955 | + 'title' => $this->post_type, |
|
| 956 | + 'type' => 'object', |
|
| 957 | + |
|
| 958 | + // Base properties for every Invoice. |
|
| 959 | + 'properties' => array( |
|
| 960 | + |
|
| 961 | + 'title' => array( |
|
| 962 | + 'description' => __( 'The title for the invoice.', 'invoicing' ), |
|
| 963 | + 'type' => 'string', |
|
| 964 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 965 | + 'readonly' => true, |
|
| 966 | + ), |
|
| 967 | + |
|
| 968 | + 'user_id' => array( |
|
| 969 | + 'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ), |
|
| 970 | + 'type' => 'integer', |
|
| 971 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 972 | + ), |
|
| 973 | + |
|
| 974 | + 'email' => array( |
|
| 975 | + 'description' => __( 'The email of the owner of the invoice.', 'invoicing' ), |
|
| 976 | + 'type' => 'string', |
|
| 977 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 978 | + 'readonly' => true, |
|
| 979 | + ), |
|
| 980 | + |
|
| 981 | + 'ip' => array( |
|
| 982 | + 'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ), |
|
| 983 | + 'type' => 'string', |
|
| 984 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 985 | + ), |
|
| 986 | + |
|
| 987 | + 'user_info' => array( |
|
| 988 | + 'description' => __( 'Information about the owner of the invoice.', 'invoicing' ), |
|
| 989 | + 'type' => 'object', |
|
| 990 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 991 | + 'properties' => array( |
|
| 992 | + |
|
| 993 | + 'first_name' => array( |
|
| 994 | + 'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ), |
|
| 995 | + 'type' => 'string', |
|
| 996 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 997 | + ), |
|
| 998 | + |
|
| 999 | + 'last_name' => array( |
|
| 1000 | + 'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ), |
|
| 1001 | + 'type' => 'string', |
|
| 1002 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1003 | + ), |
|
| 1004 | + |
|
| 1005 | + 'company' => array( |
|
| 1006 | + 'description' => __( 'The company of the owner of the invoice.', 'invoicing' ), |
|
| 1007 | + 'type' => 'string', |
|
| 1008 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1009 | + ), |
|
| 1010 | + |
|
| 1011 | + 'vat_number' => array( |
|
| 1012 | + 'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ), |
|
| 1013 | + 'type' => 'string', |
|
| 1014 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1015 | + ), |
|
| 1016 | + |
|
| 1017 | + 'vat_rate' => array( |
|
| 1018 | + 'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ), |
|
| 1019 | + 'type' => 'string', |
|
| 1020 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1021 | + ), |
|
| 1022 | + |
|
| 1023 | + 'address' => array( |
|
| 1024 | + 'description' => __( 'The address of the invoice owner.', 'invoicing' ), |
|
| 1025 | + 'type' => 'string', |
|
| 1026 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1027 | + ), |
|
| 1028 | + |
|
| 1029 | + 'city' => array( |
|
| 1030 | + 'description' => __( 'The city of the invoice owner.', 'invoicing' ), |
|
| 1031 | + 'type' => 'string', |
|
| 1032 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1033 | + ), |
|
| 1034 | + |
|
| 1035 | + 'country' => array( |
|
| 1036 | + 'description' => __( 'The country of the invoice owner.', 'invoicing' ), |
|
| 1037 | + 'type' => 'string', |
|
| 1038 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1039 | + ), |
|
| 1040 | + |
|
| 1041 | + 'state' => array( |
|
| 1042 | + 'description' => __( 'The state of the invoice owner.', 'invoicing' ), |
|
| 1043 | + 'type' => 'string', |
|
| 1044 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1045 | + ), |
|
| 1046 | + |
|
| 1047 | + 'zip' => array( |
|
| 1048 | + 'description' => __( 'The zip code of the invoice owner.', 'invoicing' ), |
|
| 1049 | + 'type' => 'string', |
|
| 1050 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1051 | + ), |
|
| 1052 | + |
|
| 1053 | + 'phone' => array( |
|
| 1054 | + 'description' => __( 'The phone number of the invoice owner.', 'invoicing' ), |
|
| 1055 | + 'type' => 'string', |
|
| 1056 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1057 | + ), |
|
| 1058 | + ), |
|
| 1059 | + ), |
|
| 1060 | + |
|
| 1061 | + 'id' => array( |
|
| 1062 | + 'description' => __( 'Unique identifier for the invoice.', 'invoicing' ), |
|
| 1063 | + 'type' => 'integer', |
|
| 1064 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1065 | + 'readonly' => true, |
|
| 1066 | + ), |
|
| 1067 | + |
|
| 1068 | + 'key' => array( |
|
| 1069 | + 'description' => __( 'A unique key for the invoice.', 'invoicing' ), |
|
| 1070 | + 'type' => 'string', |
|
| 1071 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1072 | + 'readonly' => true, |
|
| 1073 | + ), |
|
| 1074 | + |
|
| 1075 | + 'number' => array( |
|
| 1076 | + 'description' => __( 'The invoice number.', 'invoicing' ), |
|
| 1077 | + 'type' => 'string', |
|
| 1078 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1079 | + 'readonly' => true, |
|
| 1080 | + ), |
|
| 1081 | + |
|
| 1082 | + 'transaction_id' => array( |
|
| 1083 | + 'description' => __( 'The transaction id of the invoice.', 'invoicing' ), |
|
| 1084 | + 'type' => 'string', |
|
| 1085 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1086 | + ), |
|
| 1087 | + |
|
| 1088 | + 'gateway' => array( |
|
| 1089 | + 'description' => __( 'The gateway used to process the invoice.', 'invoicing' ), |
|
| 1090 | + 'type' => 'string', |
|
| 1091 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1092 | + ), |
|
| 1093 | + |
|
| 1094 | + 'gateway_title' => array( |
|
| 1095 | + 'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ), |
|
| 1096 | + 'type' => 'string', |
|
| 1097 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1098 | + ), |
|
| 1099 | + |
|
| 1100 | + 'total' => array( |
|
| 1101 | + 'description' => __( 'The total amount of the invoice.', 'invoicing' ), |
|
| 1102 | + 'type' => 'number', |
|
| 1103 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1104 | + 'readonly' => true, |
|
| 1105 | + ), |
|
| 1106 | + |
|
| 1107 | + 'discount' => array( |
|
| 1108 | + 'description' => __( 'The discount applied to the invoice.', 'invoicing' ), |
|
| 1109 | + 'type' => 'number', |
|
| 1110 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1111 | + 'readonly' => true, |
|
| 1112 | + ), |
|
| 1113 | + |
|
| 1114 | + 'discount_code' => array( |
|
| 1115 | + 'description' => __( 'The discount code applied to the invoice.', 'invoicing' ), |
|
| 1116 | + 'type' => 'string', |
|
| 1117 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1118 | + 'readonly' => true, |
|
| 1119 | + ), |
|
| 1120 | + |
|
| 1121 | + 'tax' => array( |
|
| 1122 | + 'description' => __( 'The tax applied to the invoice.', 'invoicing' ), |
|
| 1123 | + 'type' => 'number', |
|
| 1124 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1125 | + 'readonly' => true, |
|
| 1126 | + ), |
|
| 1127 | + |
|
| 1128 | + 'fees_total' => array( |
|
| 1129 | + 'description' => __( 'The total fees applied to the invoice.', 'invoicing' ), |
|
| 1130 | + 'type' => 'number', |
|
| 1131 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1132 | + 'readonly' => true, |
|
| 1133 | + ), |
|
| 1134 | + |
|
| 1135 | + 'subtotal' => array( |
|
| 1136 | + 'description' => __( 'The sub-total for the invoice.', 'invoicing' ), |
|
| 1137 | + 'type' => 'number', |
|
| 1138 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1139 | + 'readonly' => true, |
|
| 1140 | + ), |
|
| 1141 | + |
|
| 1142 | + 'currency' => array( |
|
| 1143 | + 'description' => __( 'The currency used to process the invoice.', 'invoicing' ), |
|
| 1144 | + 'type' => 'string', |
|
| 1145 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1146 | + ), |
|
| 1147 | + |
|
| 1148 | + 'cart_details' => array( |
|
| 1149 | + 'description' => __( 'The cart details for invoice.', 'invoicing' ), |
|
| 1150 | + 'type' => 'array', |
|
| 1151 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1152 | + 'required' => true, |
|
| 1153 | + ), |
|
| 1154 | + |
|
| 1155 | + 'date' => array( |
|
| 1156 | + 'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ), |
|
| 1157 | + 'type' => array( 'string', 'null' ), |
|
| 1158 | + 'format' => 'date-time', |
|
| 1159 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1160 | + ), |
|
| 1161 | + |
|
| 1162 | + 'due_date' => array( |
|
| 1163 | + 'description' => __( 'The due date for the invoice.', 'invoicing' ), |
|
| 1164 | + 'type' => array( 'string', 'null' ), |
|
| 1165 | + 'format' => 'date-time', |
|
| 1166 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1167 | + ), |
|
| 1168 | + |
|
| 1169 | + 'completed_date' => array( |
|
| 1170 | + 'description' => __( 'The completed date for the invoice.', 'invoicing' ), |
|
| 1171 | + 'type' => array( 'string', 'null' ), |
|
| 1172 | + 'format' => 'date-time', |
|
| 1173 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1174 | + 'readonly' => true, |
|
| 1175 | + ), |
|
| 1176 | 1176 | |
| 1177 | - 'link' => array( |
|
| 1178 | - 'description' => __( 'URL to the invoice.', 'invoicing' ), |
|
| 1179 | - 'type' => 'string', |
|
| 1180 | - 'format' => 'uri', |
|
| 1181 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1182 | - 'readonly' => true, |
|
| 1183 | - ), |
|
| 1184 | - |
|
| 1185 | - 'mode' => array( |
|
| 1186 | - 'description' => __( 'The mode used to process the invoice.', 'invoicing' ), |
|
| 1187 | - 'type' => 'string', |
|
| 1188 | - 'enum' => array( 'live', 'test' ), |
|
| 1189 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1190 | - 'readonly' => true, |
|
| 1191 | - ), |
|
| 1192 | - |
|
| 1193 | - 'slug' => array( |
|
| 1194 | - 'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ), |
|
| 1195 | - 'type' => 'string', |
|
| 1196 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1197 | - 'arg_options' => array( |
|
| 1198 | - 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
|
| 1199 | - ), |
|
| 1200 | - 'readonly' => true, |
|
| 1201 | - ), |
|
| 1202 | - |
|
| 1203 | - 'status' => array( |
|
| 1204 | - 'description' => __( 'A named status for the invoice.', 'invoicing' ), |
|
| 1205 | - 'type' => 'string', |
|
| 1206 | - 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
| 1207 | - 'context' => array( 'view', 'edit' ), |
|
| 1208 | - 'default' => 'wpi-pending', |
|
| 1209 | - ), |
|
| 1210 | - |
|
| 1211 | - 'status_nicename' => array( |
|
| 1212 | - 'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ), |
|
| 1213 | - 'type' => 'string', |
|
| 1214 | - 'context' => array( 'view', 'embed' ), |
|
| 1215 | - 'readonly' => true, |
|
| 1216 | - ), |
|
| 1217 | - |
|
| 1218 | - 'post_type' => array( |
|
| 1219 | - 'description' => __( 'The post type for the invoice.', 'invoicing' ), |
|
| 1220 | - 'type' => 'string', |
|
| 1221 | - 'context' => array( 'view' ), |
|
| 1222 | - 'readonly' => true, |
|
| 1223 | - ), |
|
| 1224 | - ), |
|
| 1225 | - ); |
|
| 1226 | - |
|
| 1227 | - // Add helpful links to the invoice schem. |
|
| 1228 | - $schema['links'] = $this->get_schema_links(); |
|
| 1229 | - |
|
| 1230 | - /** |
|
| 1231 | - * Filters the invoice schema for the REST API. |
|
| 1232 | - * |
|
| 1233 | - * Enables adding extra properties to invoices. |
|
| 1234 | - * |
|
| 1235 | - * @since 1.0.13 |
|
| 1236 | - * |
|
| 1237 | - * @param array $schema The invoice schema. |
|
| 1238 | - */ |
|
| 1177 | + 'link' => array( |
|
| 1178 | + 'description' => __( 'URL to the invoice.', 'invoicing' ), |
|
| 1179 | + 'type' => 'string', |
|
| 1180 | + 'format' => 'uri', |
|
| 1181 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1182 | + 'readonly' => true, |
|
| 1183 | + ), |
|
| 1184 | + |
|
| 1185 | + 'mode' => array( |
|
| 1186 | + 'description' => __( 'The mode used to process the invoice.', 'invoicing' ), |
|
| 1187 | + 'type' => 'string', |
|
| 1188 | + 'enum' => array( 'live', 'test' ), |
|
| 1189 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1190 | + 'readonly' => true, |
|
| 1191 | + ), |
|
| 1192 | + |
|
| 1193 | + 'slug' => array( |
|
| 1194 | + 'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ), |
|
| 1195 | + 'type' => 'string', |
|
| 1196 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1197 | + 'arg_options' => array( |
|
| 1198 | + 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
|
| 1199 | + ), |
|
| 1200 | + 'readonly' => true, |
|
| 1201 | + ), |
|
| 1202 | + |
|
| 1203 | + 'status' => array( |
|
| 1204 | + 'description' => __( 'A named status for the invoice.', 'invoicing' ), |
|
| 1205 | + 'type' => 'string', |
|
| 1206 | + 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
| 1207 | + 'context' => array( 'view', 'edit' ), |
|
| 1208 | + 'default' => 'wpi-pending', |
|
| 1209 | + ), |
|
| 1210 | + |
|
| 1211 | + 'status_nicename' => array( |
|
| 1212 | + 'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ), |
|
| 1213 | + 'type' => 'string', |
|
| 1214 | + 'context' => array( 'view', 'embed' ), |
|
| 1215 | + 'readonly' => true, |
|
| 1216 | + ), |
|
| 1217 | + |
|
| 1218 | + 'post_type' => array( |
|
| 1219 | + 'description' => __( 'The post type for the invoice.', 'invoicing' ), |
|
| 1220 | + 'type' => 'string', |
|
| 1221 | + 'context' => array( 'view' ), |
|
| 1222 | + 'readonly' => true, |
|
| 1223 | + ), |
|
| 1224 | + ), |
|
| 1225 | + ); |
|
| 1226 | + |
|
| 1227 | + // Add helpful links to the invoice schem. |
|
| 1228 | + $schema['links'] = $this->get_schema_links(); |
|
| 1229 | + |
|
| 1230 | + /** |
|
| 1231 | + * Filters the invoice schema for the REST API. |
|
| 1232 | + * |
|
| 1233 | + * Enables adding extra properties to invoices. |
|
| 1234 | + * |
|
| 1235 | + * @since 1.0.13 |
|
| 1236 | + * |
|
| 1237 | + * @param array $schema The invoice schema. |
|
| 1238 | + */ |
|
| 1239 | 1239 | $schema = apply_filters( "wpinv_rest_invoice_schema", $schema ); |
| 1240 | 1240 | |
| 1241 | - // Cache the invoice schema. |
|
| 1242 | - $this->schema = $schema; |
|
| 1241 | + // Cache the invoice schema. |
|
| 1242 | + $this->schema = $schema; |
|
| 1243 | 1243 | |
| 1244 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 1245 | - } |
|
| 1246 | - |
|
| 1247 | - /** |
|
| 1248 | - * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
| 1249 | - * |
|
| 1250 | - * @since 1.0.13 |
|
| 1251 | - * |
|
| 1252 | - * @return array |
|
| 1253 | - */ |
|
| 1254 | - protected function get_schema_links() { |
|
| 1255 | - |
|
| 1256 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
| 1257 | - |
|
| 1258 | - $links = array(); |
|
| 1259 | - |
|
| 1260 | - $links[] = array( |
|
| 1261 | - 'rel' => 'https://api.w.org/action-publish', |
|
| 1262 | - 'title' => __( 'The current user can mark this invoice as completed.', 'invoicing' ), |
|
| 1263 | - 'href' => $href, |
|
| 1264 | - 'targetSchema' => array( |
|
| 1265 | - 'type' => 'object', |
|
| 1266 | - 'properties' => array( |
|
| 1267 | - 'status' => array( |
|
| 1268 | - 'type' => 'string', |
|
| 1269 | - 'enum' => array( 'publish', 'wpi-renewal' ), |
|
| 1270 | - ), |
|
| 1271 | - ), |
|
| 1272 | - ), |
|
| 1273 | - ); |
|
| 1274 | - |
|
| 1275 | - $links[] = array( |
|
| 1276 | - 'rel' => 'https://api.w.org/action-assign-author', |
|
| 1277 | - 'title' => __( 'The current user can change the owner of this invoice.', 'invoicing' ), |
|
| 1278 | - 'href' => $href, |
|
| 1279 | - 'targetSchema' => array( |
|
| 1280 | - 'type' => 'object', |
|
| 1281 | - 'properties' => array( |
|
| 1282 | - 'user_id' => array( |
|
| 1283 | - 'type' => 'integer', |
|
| 1284 | - ), |
|
| 1285 | - ), |
|
| 1286 | - ), |
|
| 1287 | - ); |
|
| 1288 | - |
|
| 1289 | - return $links; |
|
| 1290 | - } |
|
| 1291 | - |
|
| 1292 | - /** |
|
| 1293 | - * Prepares links for the request. |
|
| 1294 | - * |
|
| 1295 | - * @since 1.0.13 |
|
| 1296 | - * |
|
| 1297 | - * @param WPInv_Invoice $invoice Invoice Object. |
|
| 1298 | - * @return array Links for the given invoice. |
|
| 1299 | - */ |
|
| 1300 | - protected function prepare_links( $invoice ) { |
|
| 1301 | - |
|
| 1302 | - // Prepare the base REST API endpoint for invoices. |
|
| 1303 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
| 1304 | - |
|
| 1305 | - // Entity meta. |
|
| 1306 | - $links = array( |
|
| 1307 | - 'self' => array( |
|
| 1308 | - 'href' => rest_url( trailingslashit( $base ) . $invoice->ID ), |
|
| 1309 | - ), |
|
| 1310 | - 'collection' => array( |
|
| 1311 | - 'href' => rest_url( $base ), |
|
| 1312 | - ), |
|
| 1313 | - ); |
|
| 1314 | - |
|
| 1315 | - if ( ! empty( $invoice->user_id ) ) { |
|
| 1316 | - $links['user'] = array( |
|
| 1317 | - 'href' => rest_url( 'wp/v2/users/' . $invoice->user_id ), |
|
| 1318 | - 'embeddable' => true, |
|
| 1319 | - ); |
|
| 1320 | - } |
|
| 1321 | - |
|
| 1322 | - /** |
|
| 1323 | - * Filters the returned invoice links for the REST API. |
|
| 1324 | - * |
|
| 1325 | - * Enables adding extra links to invoice API responses. |
|
| 1326 | - * |
|
| 1327 | - * @since 1.0.13 |
|
| 1328 | - * |
|
| 1329 | - * @param array $links Rest links. |
|
| 1330 | - */ |
|
| 1331 | - return apply_filters( "wpinv_invoice_rest_links", $links ); |
|
| 1332 | - |
|
| 1333 | - } |
|
| 1334 | - |
|
| 1335 | - /** |
|
| 1336 | - * Get the link relations available for the post and current user. |
|
| 1337 | - * |
|
| 1338 | - * @since 1.0.13 |
|
| 1339 | - * |
|
| 1340 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 1341 | - * @param WP_REST_Request $request Request object. |
|
| 1342 | - * @return array List of link relations. |
|
| 1343 | - */ |
|
| 1344 | - protected function get_available_actions( $invoice, $request ) { |
|
| 1345 | - |
|
| 1346 | - if ( 'edit' !== $request['context'] ) { |
|
| 1347 | - return array(); |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - $rels = array(); |
|
| 1351 | - |
|
| 1352 | - // Retrieve the post type object. |
|
| 1353 | - $post_type = get_post_type_object( $invoice->post_type ); |
|
| 1354 | - |
|
| 1355 | - // Mark invoice as completed. |
|
| 1356 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
| 1357 | - $rels[] = 'https://api.w.org/action-publish'; |
|
| 1358 | - } |
|
| 1359 | - |
|
| 1360 | - // Change the owner of the invoice. |
|
| 1361 | - if ( current_user_can( $post_type->cap->edit_others_posts ) ) { |
|
| 1362 | - $rels[] = 'https://api.w.org/action-assign-author'; |
|
| 1363 | - } |
|
| 1364 | - |
|
| 1365 | - /** |
|
| 1366 | - * Filters the available invoice link relations for the REST API. |
|
| 1367 | - * |
|
| 1368 | - * Enables adding extra link relation for the current user and request to invoice responses. |
|
| 1369 | - * |
|
| 1370 | - * @since 1.0.13 |
|
| 1371 | - * |
|
| 1372 | - * @param array $rels Available link relations. |
|
| 1373 | - */ |
|
| 1374 | - return apply_filters( "wpinv_invoice_rest_link_relations", $rels ); |
|
| 1375 | - } |
|
| 1376 | - |
|
| 1377 | - /** |
|
| 1378 | - * Sanitizes and validates the list of post statuses. |
|
| 1379 | - * |
|
| 1380 | - * @since 1.0.13 |
|
| 1381 | - * |
|
| 1382 | - * @param string|array $statuses One or more post statuses. |
|
| 1383 | - * @param WP_REST_Request $request Full details about the request. |
|
| 1384 | - * @param string $parameter Additional parameter to pass to validation. |
|
| 1385 | - * @return array|WP_Error A list of valid statuses, otherwise WP_Error object. |
|
| 1386 | - */ |
|
| 1387 | - public function sanitize_post_statuses( $statuses, $request, $parameter ) { |
|
| 1388 | - |
|
| 1389 | - $statuses = wp_parse_slug_list( $statuses ); |
|
| 1390 | - $valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
| 1391 | - return array_intersect( $statuses, $valid_statuses ); |
|
| 1244 | + return $this->add_additional_fields_schema( $this->schema ); |
|
| 1245 | + } |
|
| 1246 | + |
|
| 1247 | + /** |
|
| 1248 | + * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
| 1249 | + * |
|
| 1250 | + * @since 1.0.13 |
|
| 1251 | + * |
|
| 1252 | + * @return array |
|
| 1253 | + */ |
|
| 1254 | + protected function get_schema_links() { |
|
| 1255 | + |
|
| 1256 | + $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
| 1257 | + |
|
| 1258 | + $links = array(); |
|
| 1259 | + |
|
| 1260 | + $links[] = array( |
|
| 1261 | + 'rel' => 'https://api.w.org/action-publish', |
|
| 1262 | + 'title' => __( 'The current user can mark this invoice as completed.', 'invoicing' ), |
|
| 1263 | + 'href' => $href, |
|
| 1264 | + 'targetSchema' => array( |
|
| 1265 | + 'type' => 'object', |
|
| 1266 | + 'properties' => array( |
|
| 1267 | + 'status' => array( |
|
| 1268 | + 'type' => 'string', |
|
| 1269 | + 'enum' => array( 'publish', 'wpi-renewal' ), |
|
| 1270 | + ), |
|
| 1271 | + ), |
|
| 1272 | + ), |
|
| 1273 | + ); |
|
| 1274 | + |
|
| 1275 | + $links[] = array( |
|
| 1276 | + 'rel' => 'https://api.w.org/action-assign-author', |
|
| 1277 | + 'title' => __( 'The current user can change the owner of this invoice.', 'invoicing' ), |
|
| 1278 | + 'href' => $href, |
|
| 1279 | + 'targetSchema' => array( |
|
| 1280 | + 'type' => 'object', |
|
| 1281 | + 'properties' => array( |
|
| 1282 | + 'user_id' => array( |
|
| 1283 | + 'type' => 'integer', |
|
| 1284 | + ), |
|
| 1285 | + ), |
|
| 1286 | + ), |
|
| 1287 | + ); |
|
| 1288 | + |
|
| 1289 | + return $links; |
|
| 1290 | + } |
|
| 1291 | + |
|
| 1292 | + /** |
|
| 1293 | + * Prepares links for the request. |
|
| 1294 | + * |
|
| 1295 | + * @since 1.0.13 |
|
| 1296 | + * |
|
| 1297 | + * @param WPInv_Invoice $invoice Invoice Object. |
|
| 1298 | + * @return array Links for the given invoice. |
|
| 1299 | + */ |
|
| 1300 | + protected function prepare_links( $invoice ) { |
|
| 1301 | + |
|
| 1302 | + // Prepare the base REST API endpoint for invoices. |
|
| 1303 | + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
| 1304 | + |
|
| 1305 | + // Entity meta. |
|
| 1306 | + $links = array( |
|
| 1307 | + 'self' => array( |
|
| 1308 | + 'href' => rest_url( trailingslashit( $base ) . $invoice->ID ), |
|
| 1309 | + ), |
|
| 1310 | + 'collection' => array( |
|
| 1311 | + 'href' => rest_url( $base ), |
|
| 1312 | + ), |
|
| 1313 | + ); |
|
| 1314 | + |
|
| 1315 | + if ( ! empty( $invoice->user_id ) ) { |
|
| 1316 | + $links['user'] = array( |
|
| 1317 | + 'href' => rest_url( 'wp/v2/users/' . $invoice->user_id ), |
|
| 1318 | + 'embeddable' => true, |
|
| 1319 | + ); |
|
| 1320 | + } |
|
| 1321 | + |
|
| 1322 | + /** |
|
| 1323 | + * Filters the returned invoice links for the REST API. |
|
| 1324 | + * |
|
| 1325 | + * Enables adding extra links to invoice API responses. |
|
| 1326 | + * |
|
| 1327 | + * @since 1.0.13 |
|
| 1328 | + * |
|
| 1329 | + * @param array $links Rest links. |
|
| 1330 | + */ |
|
| 1331 | + return apply_filters( "wpinv_invoice_rest_links", $links ); |
|
| 1332 | + |
|
| 1333 | + } |
|
| 1334 | + |
|
| 1335 | + /** |
|
| 1336 | + * Get the link relations available for the post and current user. |
|
| 1337 | + * |
|
| 1338 | + * @since 1.0.13 |
|
| 1339 | + * |
|
| 1340 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 1341 | + * @param WP_REST_Request $request Request object. |
|
| 1342 | + * @return array List of link relations. |
|
| 1343 | + */ |
|
| 1344 | + protected function get_available_actions( $invoice, $request ) { |
|
| 1345 | + |
|
| 1346 | + if ( 'edit' !== $request['context'] ) { |
|
| 1347 | + return array(); |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + $rels = array(); |
|
| 1351 | + |
|
| 1352 | + // Retrieve the post type object. |
|
| 1353 | + $post_type = get_post_type_object( $invoice->post_type ); |
|
| 1354 | + |
|
| 1355 | + // Mark invoice as completed. |
|
| 1356 | + if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
| 1357 | + $rels[] = 'https://api.w.org/action-publish'; |
|
| 1358 | + } |
|
| 1359 | + |
|
| 1360 | + // Change the owner of the invoice. |
|
| 1361 | + if ( current_user_can( $post_type->cap->edit_others_posts ) ) { |
|
| 1362 | + $rels[] = 'https://api.w.org/action-assign-author'; |
|
| 1363 | + } |
|
| 1364 | + |
|
| 1365 | + /** |
|
| 1366 | + * Filters the available invoice link relations for the REST API. |
|
| 1367 | + * |
|
| 1368 | + * Enables adding extra link relation for the current user and request to invoice responses. |
|
| 1369 | + * |
|
| 1370 | + * @since 1.0.13 |
|
| 1371 | + * |
|
| 1372 | + * @param array $rels Available link relations. |
|
| 1373 | + */ |
|
| 1374 | + return apply_filters( "wpinv_invoice_rest_link_relations", $rels ); |
|
| 1375 | + } |
|
| 1376 | + |
|
| 1377 | + /** |
|
| 1378 | + * Sanitizes and validates the list of post statuses. |
|
| 1379 | + * |
|
| 1380 | + * @since 1.0.13 |
|
| 1381 | + * |
|
| 1382 | + * @param string|array $statuses One or more post statuses. |
|
| 1383 | + * @param WP_REST_Request $request Full details about the request. |
|
| 1384 | + * @param string $parameter Additional parameter to pass to validation. |
|
| 1385 | + * @return array|WP_Error A list of valid statuses, otherwise WP_Error object. |
|
| 1386 | + */ |
|
| 1387 | + public function sanitize_post_statuses( $statuses, $request, $parameter ) { |
|
| 1388 | + |
|
| 1389 | + $statuses = wp_parse_slug_list( $statuses ); |
|
| 1390 | + $valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
| 1391 | + return array_intersect( $statuses, $valid_statuses ); |
|
| 1392 | 1392 | |
| 1393 | - } |
|
| 1393 | + } |
|
| 1394 | 1394 | |
| 1395 | 1395 | } |
| 1396 | 1396 | \ No newline at end of file |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | * @since 1.0.13 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -if ( !defined( 'WPINC' ) ) { |
|
| 11 | +if (!defined('WPINC')) { |
|
| 12 | 12 | exit; |
| 13 | 13 | } |
| 14 | 14 | |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @param string $namespace Api Namespace |
| 43 | 43 | */ |
| 44 | - public function __construct( $namespace ) { |
|
| 44 | + public function __construct($namespace) { |
|
| 45 | 45 | |
| 46 | 46 | // Set api namespace... |
| 47 | 47 | $this->namespace = $namespace; |
@@ -60,12 +60,12 @@ discard block |
||
| 60 | 60 | * @param WP_REST_Request $request Full details about the request. |
| 61 | 61 | * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 62 | 62 | */ |
| 63 | - public function get_items_permissions_check( $request ) { |
|
| 63 | + public function get_items_permissions_check($request) { |
|
| 64 | 64 | |
| 65 | - $post_type = get_post_type_object( $this->post_type ); |
|
| 65 | + $post_type = get_post_type_object($this->post_type); |
|
| 66 | 66 | |
| 67 | - if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) { |
|
| 68 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 67 | + if ('edit' === $request['context'] && !current_user_can($post_type->cap->edit_posts)) { |
|
| 68 | + return new WP_Error('rest_forbidden_context', __('Sorry, you are not allowed to edit invoices.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Read checks will be evaluated on a per invoice basis |
@@ -82,17 +82,17 @@ discard block |
||
| 82 | 82 | * @param WP_REST_Request $request Full details about the request. |
| 83 | 83 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 84 | 84 | */ |
| 85 | - public function get_items( $request ) { |
|
| 85 | + public function get_items($request) { |
|
| 86 | 86 | |
| 87 | 87 | // Retrieve the list of registered invoice query parameters. |
| 88 | 88 | $registered = $this->get_collection_params(); |
| 89 | 89 | |
| 90 | 90 | $args = array(); |
| 91 | 91 | |
| 92 | - foreach( array_keys( $registered ) as $key ) { |
|
| 92 | + foreach (array_keys($registered) as $key) { |
|
| 93 | 93 | |
| 94 | - if( isset( $request[ $key] ) ) { |
|
| 95 | - $args[ $key ] = $request[ $key]; |
|
| 94 | + if (isset($request[$key])) { |
|
| 95 | + $args[$key] = $request[$key]; |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | } |
@@ -107,32 +107,32 @@ discard block |
||
| 107 | 107 | * @param array $args Key value array of query var to query value. |
| 108 | 108 | * @param WP_REST_Request $request The request used. |
| 109 | 109 | */ |
| 110 | - $args = apply_filters( "wpinv_rest_get_invoices_arguments", $args, $request, $this ); |
|
| 110 | + $args = apply_filters("wpinv_rest_get_invoices_arguments", $args, $request, $this); |
|
| 111 | 111 | |
| 112 | 112 | // Special args |
| 113 | - $args[ 'return' ] = 'objects'; |
|
| 114 | - $args[ 'paginate' ] = true; |
|
| 113 | + $args['return'] = 'objects'; |
|
| 114 | + $args['paginate'] = true; |
|
| 115 | 115 | |
| 116 | 116 | // Run the query. |
| 117 | - $query = wpinv_get_invoices( $args ); |
|
| 117 | + $query = wpinv_get_invoices($args); |
|
| 118 | 118 | |
| 119 | 119 | // Prepare the retrieved invoices |
| 120 | 120 | $invoices = array(); |
| 121 | - foreach( $query->invoices as $invoice ) { |
|
| 121 | + foreach ($query->invoices as $invoice) { |
|
| 122 | 122 | |
| 123 | - if ( ! $this->check_read_permission( $invoice ) ) { |
|
| 123 | + if (!$this->check_read_permission($invoice)) { |
|
| 124 | 124 | continue; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - $data = $this->prepare_item_for_response( $invoice, $request ); |
|
| 128 | - $invoices[] = $this->prepare_response_for_collection( $data ); |
|
| 127 | + $data = $this->prepare_item_for_response($invoice, $request); |
|
| 128 | + $invoices[] = $this->prepare_response_for_collection($data); |
|
| 129 | 129 | |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 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 ); |
|
| 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 | 136 | |
| 137 | 137 | /** |
| 138 | 138 | * Filters the responses for invoices requests. |
@@ -145,9 +145,9 @@ discard block |
||
| 145 | 145 | * @param WP_REST_Request $request The request used. |
| 146 | 146 | * @param array $args Array of args used to retrieve the invoices |
| 147 | 147 | */ |
| 148 | - $response = apply_filters( "rest_wpinv_invoices_response", $response, $request, $args ); |
|
| 148 | + $response = apply_filters("rest_wpinv_invoices_response", $response, $request, $args); |
|
| 149 | 149 | |
| 150 | - return rest_ensure_response( $response ); |
|
| 150 | + return rest_ensure_response($response); |
|
| 151 | 151 | |
| 152 | 152 | } |
| 153 | 153 | |
@@ -159,17 +159,17 @@ discard block |
||
| 159 | 159 | * @param int $invoice_id Supplied ID. |
| 160 | 160 | * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise. |
| 161 | 161 | */ |
| 162 | - protected function get_post( $invoice_id ) { |
|
| 162 | + protected function get_post($invoice_id) { |
|
| 163 | 163 | |
| 164 | - $error = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 164 | + $error = new WP_Error('rest_invoice_invalid_id', __('Invalid invoice ID.', 'invoicing'), array('status' => 404)); |
|
| 165 | 165 | |
| 166 | 166 | // Ids start from 1 |
| 167 | - if ( (int) $invoice_id <= 0 ) { |
|
| 167 | + if ((int)$invoice_id <= 0) { |
|
| 168 | 168 | return $error; |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - $invoice = wpinv_get_invoice( (int) $invoice_id ); |
|
| 172 | - if ( empty( $invoice ) ) { |
|
| 171 | + $invoice = wpinv_get_invoice((int)$invoice_id); |
|
| 172 | + if (empty($invoice)) { |
|
| 173 | 173 | return $error; |
| 174 | 174 | } |
| 175 | 175 | |
@@ -185,18 +185,18 @@ discard block |
||
| 185 | 185 | * @param WP_REST_Request $request Full details about the request. |
| 186 | 186 | * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise. |
| 187 | 187 | */ |
| 188 | - public function get_item_permissions_check( $request ) { |
|
| 188 | + public function get_item_permissions_check($request) { |
|
| 189 | 189 | |
| 190 | 190 | // Retrieve the invoice object. |
| 191 | - $invoice = $this->get_post( $request['id'] ); |
|
| 191 | + $invoice = $this->get_post($request['id']); |
|
| 192 | 192 | |
| 193 | 193 | // Ensure it is valid. |
| 194 | - if ( is_wp_error( $invoice ) ) { |
|
| 194 | + if (is_wp_error($invoice)) { |
|
| 195 | 195 | return $invoice; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - if ( $invoice ) { |
|
| 199 | - return $this->check_read_permission( $invoice ); |
|
| 198 | + if ($invoice) { |
|
| 199 | + return $this->check_read_permission($invoice); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | return true; |
@@ -213,15 +213,15 @@ discard block |
||
| 213 | 213 | * @param WPInv_Invoice $invoice WPInv_Invoice object. |
| 214 | 214 | * @return bool Whether the post can be read. |
| 215 | 215 | */ |
| 216 | - public function check_read_permission( $invoice ) { |
|
| 216 | + public function check_read_permission($invoice) { |
|
| 217 | 217 | |
| 218 | 218 | // An invoice can be read by an admin... |
| 219 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'view_invoices' ) ) { |
|
| 219 | + if (current_user_can('manage_options') || current_user_can('view_invoices')) { |
|
| 220 | 220 | return true; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | // ... and the owner of the invoice |
| 224 | - if( get_current_user_id() ===(int) $invoice->get_user_id() ) { |
|
| 224 | + if (get_current_user_id() === (int)$invoice->get_user_id()) { |
|
| 225 | 225 | return true; |
| 226 | 226 | } |
| 227 | 227 | |
@@ -236,19 +236,19 @@ discard block |
||
| 236 | 236 | * @param WP_REST_Request $request Full details about the request. |
| 237 | 237 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 238 | 238 | */ |
| 239 | - public function get_item( $request ) { |
|
| 239 | + public function get_item($request) { |
|
| 240 | 240 | |
| 241 | 241 | // Fetch the invoice. |
| 242 | - $invoice = $this->get_post( $request['id'] ); |
|
| 242 | + $invoice = $this->get_post($request['id']); |
|
| 243 | 243 | |
| 244 | 244 | // Abort early if it does not exist |
| 245 | - if ( is_wp_error( $invoice ) ) { |
|
| 245 | + if (is_wp_error($invoice)) { |
|
| 246 | 246 | return $invoice; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | // Prepare the response |
| 250 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 251 | - $response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) ); |
|
| 250 | + $response = $this->prepare_item_for_response($invoice, $request); |
|
| 251 | + $response->link_header('alternate', esc_url($invoice->get_view_url()), array('type' => 'text/html')); |
|
| 252 | 252 | |
| 253 | 253 | /** |
| 254 | 254 | * Filters the responses for single invoice requests. |
@@ -260,9 +260,9 @@ discard block |
||
| 260 | 260 | * @param WP_HTTP_Response $response Response. |
| 261 | 261 | * @param WP_REST_Request $request The request used. |
| 262 | 262 | */ |
| 263 | - $response = apply_filters( "rest_wpinv_get_invoice_response", $response, $request ); |
|
| 263 | + $response = apply_filters("rest_wpinv_get_invoice_response", $response, $request); |
|
| 264 | 264 | |
| 265 | - return rest_ensure_response( $response ); |
|
| 265 | + return rest_ensure_response($response); |
|
| 266 | 266 | |
| 267 | 267 | } |
| 268 | 268 | |
@@ -274,18 +274,18 @@ discard block |
||
| 274 | 274 | * @param WP_REST_Request $request Full details about the request. |
| 275 | 275 | * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 276 | 276 | */ |
| 277 | - public function create_item_permissions_check( $request ) { |
|
| 277 | + public function create_item_permissions_check($request) { |
|
| 278 | 278 | |
| 279 | - if ( ! empty( $request['id'] ) ) { |
|
| 280 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 279 | + if (!empty($request['id'])) { |
|
| 280 | + return new WP_Error('rest_invoice_exists', __('Cannot create existing invoice.', 'invoicing'), array('status' => 400)); |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - $post_type = get_post_type_object( $this->post_type ); |
|
| 283 | + $post_type = get_post_type_object($this->post_type); |
|
| 284 | 284 | |
| 285 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
| 285 | + if (!current_user_can($post_type->cap->create_posts)) { |
|
| 286 | 286 | return new WP_Error( |
| 287 | 287 | 'rest_cannot_create', |
| 288 | - __( 'Sorry, you are not allowed to create invoices as this user.', 'invoicing' ), |
|
| 288 | + __('Sorry, you are not allowed to create invoices as this user.', 'invoicing'), |
|
| 289 | 289 | array( |
| 290 | 290 | 'status' => rest_authorization_required_code(), |
| 291 | 291 | ) |
@@ -303,30 +303,30 @@ discard block |
||
| 303 | 303 | * @param WP_REST_Request $request Full details about the request. |
| 304 | 304 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 305 | 305 | */ |
| 306 | - public function create_item( $request ) { |
|
| 306 | + public function create_item($request) { |
|
| 307 | 307 | |
| 308 | - if ( ! empty( $request['id'] ) ) { |
|
| 309 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 308 | + if (!empty($request['id'])) { |
|
| 309 | + return new WP_Error('rest_invoice_exists', __('Cannot create existing invoice.', 'invoicing'), array('status' => 400)); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - $request->set_param( 'context', 'edit' ); |
|
| 312 | + $request->set_param('context', 'edit'); |
|
| 313 | 313 | |
| 314 | 314 | // Prepare the updated data. |
| 315 | - $invoice_data = wp_unslash( $this->prepare_item_for_database( $request ) ); |
|
| 315 | + $invoice_data = wp_unslash($this->prepare_item_for_database($request)); |
|
| 316 | 316 | |
| 317 | - if ( is_wp_error( $invoice_data ) ) { |
|
| 317 | + if (is_wp_error($invoice_data)) { |
|
| 318 | 318 | return $invoice_data; |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | // Try creating the invoice |
| 322 | - $invoice = wpinv_insert_invoice( $invoice_data, true ); |
|
| 322 | + $invoice = wpinv_insert_invoice($invoice_data, true); |
|
| 323 | 323 | |
| 324 | - if ( is_wp_error( $invoice ) ) { |
|
| 324 | + if (is_wp_error($invoice)) { |
|
| 325 | 325 | return $invoice; |
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | // Prepare the response |
| 329 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 329 | + $response = $this->prepare_item_for_response($invoice, $request); |
|
| 330 | 330 | |
| 331 | 331 | /** |
| 332 | 332 | * Fires after a single invoice is created or updated via the REST API. |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | * @param WP_REST_Request $request Request object. |
| 338 | 338 | * @param bool $creating True when creating a post, false when updating. |
| 339 | 339 | */ |
| 340 | - do_action( "wpinv_rest_insert_invoice", $invoice, $request, true ); |
|
| 340 | + do_action("wpinv_rest_insert_invoice", $invoice, $request, true); |
|
| 341 | 341 | |
| 342 | 342 | /** |
| 343 | 343 | * Filters the responses for creating single invoice requests. |
@@ -349,9 +349,9 @@ discard block |
||
| 349 | 349 | * @param array $invoice_data Invoice properties. |
| 350 | 350 | * @param WP_REST_Request $request The request used. |
| 351 | 351 | */ |
| 352 | - $response = apply_filters( "rest_wpinv_create_invoice_response", $response, $request ); |
|
| 352 | + $response = apply_filters("rest_wpinv_create_invoice_response", $response, $request); |
|
| 353 | 353 | |
| 354 | - return rest_ensure_response( $response ); |
|
| 354 | + return rest_ensure_response($response); |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | /** |
@@ -362,20 +362,20 @@ discard block |
||
| 362 | 362 | * @param WP_REST_Request $request Full details about the request. |
| 363 | 363 | * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
| 364 | 364 | */ |
| 365 | - public function update_item_permissions_check( $request ) { |
|
| 365 | + public function update_item_permissions_check($request) { |
|
| 366 | 366 | |
| 367 | 367 | // Retrieve the invoice. |
| 368 | - $invoice = $this->get_post( $request['id'] ); |
|
| 369 | - if ( is_wp_error( $invoice ) ) { |
|
| 368 | + $invoice = $this->get_post($request['id']); |
|
| 369 | + if (is_wp_error($invoice)) { |
|
| 370 | 370 | return $invoice; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - $post_type = get_post_type_object( $this->post_type ); |
|
| 373 | + $post_type = get_post_type_object($this->post_type); |
|
| 374 | 374 | |
| 375 | - if ( ! current_user_can( $post_type->cap->edit_post, $invoice->ID ) ) { |
|
| 375 | + if (!current_user_can($post_type->cap->edit_post, $invoice->ID)) { |
|
| 376 | 376 | return new WP_Error( |
| 377 | 377 | 'rest_cannot_edit', |
| 378 | - __( 'Sorry, you are not allowed to update this invoice.', 'invoicing' ), |
|
| 378 | + __('Sorry, you are not allowed to update this invoice.', 'invoicing'), |
|
| 379 | 379 | array( |
| 380 | 380 | 'status' => rest_authorization_required_code(), |
| 381 | 381 | ) |
@@ -393,46 +393,46 @@ discard block |
||
| 393 | 393 | * @param WP_REST_Request $request Full details about the request. |
| 394 | 394 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 395 | 395 | */ |
| 396 | - public function update_item( $request ) { |
|
| 396 | + public function update_item($request) { |
|
| 397 | 397 | |
| 398 | 398 | // Ensure the invoice exists. |
| 399 | - $valid_check = $this->get_post( $request['id'] ); |
|
| 399 | + $valid_check = $this->get_post($request['id']); |
|
| 400 | 400 | |
| 401 | 401 | // Abort early if it does not exist |
| 402 | - if ( is_wp_error( $valid_check ) ) { |
|
| 402 | + if (is_wp_error($valid_check)) { |
|
| 403 | 403 | return $valid_check; |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | - $request->set_param( 'context', 'edit' ); |
|
| 406 | + $request->set_param('context', 'edit'); |
|
| 407 | 407 | |
| 408 | 408 | // Prepare the updated data. |
| 409 | - $data_to_update = wp_unslash( $this->prepare_item_for_database( $request ) ); |
|
| 409 | + $data_to_update = wp_unslash($this->prepare_item_for_database($request)); |
|
| 410 | 410 | |
| 411 | - if ( is_wp_error( $data_to_update ) ) { |
|
| 411 | + if (is_wp_error($data_to_update)) { |
|
| 412 | 412 | return $data_to_update; |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | // Abort if no invoice data is provided |
| 416 | - if( empty( $data_to_update ) ) { |
|
| 417 | - return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
|
| 416 | + if (empty($data_to_update)) { |
|
| 417 | + return new WP_Error('missing_data', __('An update request cannot be empty.', 'invoicing')); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | // Include the invoice ID |
| 421 | 421 | $data_to_update['ID'] = $request['id']; |
| 422 | 422 | |
| 423 | 423 | // Update the invoice |
| 424 | - $updated_invoice = wpinv_update_invoice( $data_to_update, true ); |
|
| 424 | + $updated_invoice = wpinv_update_invoice($data_to_update, true); |
|
| 425 | 425 | |
| 426 | 426 | // Incase the update operation failed... |
| 427 | - if ( is_wp_error( $updated_invoice ) ) { |
|
| 427 | + if (is_wp_error($updated_invoice)) { |
|
| 428 | 428 | return $updated_invoice; |
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | // Prepare the response |
| 432 | - $response = $this->prepare_item_for_response( $updated_invoice, $request ); |
|
| 432 | + $response = $this->prepare_item_for_response($updated_invoice, $request); |
|
| 433 | 433 | |
| 434 | 434 | /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */ |
| 435 | - do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false ); |
|
| 435 | + do_action("wpinv_rest_insert_invoice", $updated_invoice, $request, false); |
|
| 436 | 436 | |
| 437 | 437 | /** |
| 438 | 438 | * Filters the responses for updating single invoice requests. |
@@ -444,9 +444,9 @@ discard block |
||
| 444 | 444 | * @param array $invoice_data Invoice properties. |
| 445 | 445 | * @param WP_REST_Request $request The request used. |
| 446 | 446 | */ |
| 447 | - $response = apply_filters( "wpinv_rest_update_invoice_response", $response, $request ); |
|
| 447 | + $response = apply_filters("wpinv_rest_update_invoice_response", $response, $request); |
|
| 448 | 448 | |
| 449 | - return rest_ensure_response( $response ); |
|
| 449 | + return rest_ensure_response($response); |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | /** |
@@ -457,19 +457,19 @@ discard block |
||
| 457 | 457 | * @param WP_REST_Request $request Full details about the request. |
| 458 | 458 | * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise. |
| 459 | 459 | */ |
| 460 | - public function delete_item_permissions_check( $request ) { |
|
| 460 | + public function delete_item_permissions_check($request) { |
|
| 461 | 461 | |
| 462 | 462 | // Retrieve the invoice. |
| 463 | - $invoice = $this->get_post( $request['id'] ); |
|
| 464 | - if ( is_wp_error( $invoice ) ) { |
|
| 463 | + $invoice = $this->get_post($request['id']); |
|
| 464 | + if (is_wp_error($invoice)) { |
|
| 465 | 465 | return $invoice; |
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | // Ensure the current user can delete invoices |
| 469 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'delete_invoices' ) ) { |
|
| 469 | + if (current_user_can('manage_options') || current_user_can('delete_invoices')) { |
|
| 470 | 470 | return new WP_Error( |
| 471 | 471 | 'rest_cannot_delete', |
| 472 | - __( 'Sorry, you are not allowed to delete this invoice.', 'invoicing' ), |
|
| 472 | + __('Sorry, you are not allowed to delete this invoice.', 'invoicing'), |
|
| 473 | 473 | array( |
| 474 | 474 | 'status' => rest_authorization_required_code(), |
| 475 | 475 | ) |
@@ -487,31 +487,31 @@ discard block |
||
| 487 | 487 | * @param WP_REST_Request $request Full details about the request. |
| 488 | 488 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 489 | 489 | */ |
| 490 | - public function delete_item( $request ) { |
|
| 490 | + public function delete_item($request) { |
|
| 491 | 491 | |
| 492 | 492 | // Retrieve the invoice. |
| 493 | - $invoice = $this->get_post( $request['id'] ); |
|
| 494 | - if ( is_wp_error( $invoice ) ) { |
|
| 493 | + $invoice = $this->get_post($request['id']); |
|
| 494 | + if (is_wp_error($invoice)) { |
|
| 495 | 495 | return $invoice; |
| 496 | 496 | } |
| 497 | 497 | |
| 498 | - $request->set_param( 'context', 'edit' ); |
|
| 498 | + $request->set_param('context', 'edit'); |
|
| 499 | 499 | |
| 500 | 500 | // Prepare the invoice id |
| 501 | - $id = $invoice->ID; |
|
| 501 | + $id = $invoice->ID; |
|
| 502 | 502 | |
| 503 | 503 | // Prepare the response |
| 504 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 504 | + $response = $this->prepare_item_for_response($invoice, $request); |
|
| 505 | 505 | |
| 506 | 506 | // Check if the user wants to bypass the trash... |
| 507 | - $force_delete = (bool) $request['force']; |
|
| 507 | + $force_delete = (bool)$request['force']; |
|
| 508 | 508 | |
| 509 | 509 | // Try deleting the invoice. |
| 510 | - $deleted = wp_delete_post( $id, $force_delete ); |
|
| 510 | + $deleted = wp_delete_post($id, $force_delete); |
|
| 511 | 511 | |
| 512 | 512 | // Abort early if we can't delete the invoice. |
| 513 | - if ( ! $deleted ) { |
|
| 514 | - return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
| 513 | + if (!$deleted) { |
|
| 514 | + return new WP_Error('rest_cannot_delete', __('The invoice cannot be deleted.', 'invoicing'), array('status' => 500)); |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | /** |
@@ -523,7 +523,7 @@ discard block |
||
| 523 | 523 | * @param WPInv_Invoice $invoice The deleted or trashed invoice. |
| 524 | 524 | * @param WP_REST_Request $request The request sent to the API. |
| 525 | 525 | */ |
| 526 | - do_action( "wpinv_rest_delete_invoice", $invoice, $request ); |
|
| 526 | + do_action("wpinv_rest_delete_invoice", $invoice, $request); |
|
| 527 | 527 | |
| 528 | 528 | return $response; |
| 529 | 529 | |
@@ -539,43 +539,43 @@ discard block |
||
| 539 | 539 | */ |
| 540 | 540 | public function get_collection_params() { |
| 541 | 541 | |
| 542 | - $query_params = array( |
|
| 542 | + $query_params = array( |
|
| 543 | 543 | |
| 544 | 544 | // Invoice status. |
| 545 | 545 | 'status' => array( |
| 546 | 546 | 'default' => 'publish', |
| 547 | - 'description' => __( 'Limit result set to invoices assigned one or more statuses.', 'invoicing' ), |
|
| 547 | + 'description' => __('Limit result set to invoices assigned one or more statuses.', 'invoicing'), |
|
| 548 | 548 | 'type' => 'array', |
| 549 | 549 | 'items' => array( |
| 550 | - 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
| 550 | + 'enum' => array_keys(wpinv_get_invoice_statuses(true, true)), |
|
| 551 | 551 | 'type' => 'string', |
| 552 | 552 | ), |
| 553 | - 'sanitize_callback' => array( $this, 'sanitize_post_statuses' ), |
|
| 553 | + 'sanitize_callback' => array($this, 'sanitize_post_statuses'), |
|
| 554 | 554 | ), |
| 555 | 555 | |
| 556 | 556 | // User. |
| 557 | 557 | 'user' => array( |
| 558 | - 'description' => __( 'Limit result set to invoices for a specif user.', 'invoicing' ), |
|
| 558 | + 'description' => __('Limit result set to invoices for a specif user.', 'invoicing'), |
|
| 559 | 559 | 'type' => 'integer', |
| 560 | 560 | ), |
| 561 | 561 | |
| 562 | 562 | // Number of results per page |
| 563 | 563 | 'limit' => array( |
| 564 | - 'description' => __( 'Number of invoices to fetch.', 'invoicing' ), |
|
| 564 | + 'description' => __('Number of invoices to fetch.', 'invoicing'), |
|
| 565 | 565 | 'type' => 'integer', |
| 566 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
| 566 | + 'default' => (int)get_option('posts_per_page'), |
|
| 567 | 567 | ), |
| 568 | 568 | |
| 569 | 569 | // Pagination |
| 570 | 570 | 'page' => array( |
| 571 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
| 571 | + 'description' => __('Current page to fetch.', 'invoicing'), |
|
| 572 | 572 | 'type' => 'integer', |
| 573 | 573 | 'default' => 1, |
| 574 | 574 | ), |
| 575 | 575 | |
| 576 | 576 | // Exclude certain invoices |
| 577 | 577 | 'exclude' => array( |
| 578 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'invoicing' ), |
|
| 578 | + 'description' => __('Ensure result set excludes specific IDs.', 'invoicing'), |
|
| 579 | 579 | 'type' => 'array', |
| 580 | 580 | 'items' => array( |
| 581 | 581 | 'type' => 'integer', |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | |
| 586 | 586 | // Order invoices by |
| 587 | 587 | 'orderby' => array( |
| 588 | - 'description' => __( 'Sort invoices by object attribute.', 'invoicing' ), |
|
| 588 | + 'description' => __('Sort invoices by object attribute.', 'invoicing'), |
|
| 589 | 589 | 'type' => 'string', |
| 590 | 590 | 'default' => 'date', |
| 591 | 591 | 'enum' => array( |
@@ -599,10 +599,10 @@ discard block |
||
| 599 | 599 | |
| 600 | 600 | // How to order |
| 601 | 601 | 'order' => array( |
| 602 | - 'description' => __( 'Order sort attribute ascending or descending.', 'invoicing' ), |
|
| 602 | + 'description' => __('Order sort attribute ascending or descending.', 'invoicing'), |
|
| 603 | 603 | 'type' => 'string', |
| 604 | 604 | 'default' => 'DESC', |
| 605 | - 'enum' => array( 'ASC', 'DESC' ), |
|
| 605 | + 'enum' => array('ASC', 'DESC'), |
|
| 606 | 606 | ), |
| 607 | 607 | ); |
| 608 | 608 | |
@@ -615,7 +615,7 @@ discard block |
||
| 615 | 615 | * @param array $query_params JSON Schema-formatted collection parameters. |
| 616 | 616 | * @param WP_Post_Type $post_type Post type object. |
| 617 | 617 | */ |
| 618 | - return apply_filters( "rest_invoices_collection_params", $query_params, 'wpi-invoice' ); |
|
| 618 | + return apply_filters("rest_invoices_collection_params", $query_params, 'wpi-invoice'); |
|
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | /** |
@@ -626,7 +626,7 @@ discard block |
||
| 626 | 626 | * @param object|string $post_type Post type name or object. |
| 627 | 627 | * @return bool Whether the post type is allowed in REST. |
| 628 | 628 | */ |
| 629 | - protected function check_is_post_type_allowed( $post_type ) { |
|
| 629 | + protected function check_is_post_type_allowed($post_type) { |
|
| 630 | 630 | return true; |
| 631 | 631 | } |
| 632 | 632 | |
@@ -638,49 +638,49 @@ discard block |
||
| 638 | 638 | * @param WP_REST_Request $request Request object. |
| 639 | 639 | * @return array|WP_Error Invoice Properties or WP_Error. |
| 640 | 640 | */ |
| 641 | - protected function prepare_item_for_database( $request ) { |
|
| 641 | + protected function prepare_item_for_database($request) { |
|
| 642 | 642 | $prepared_invoice = new stdClass(); |
| 643 | 643 | |
| 644 | 644 | // Post ID. |
| 645 | - if ( isset( $request['id'] ) ) { |
|
| 646 | - $existing_invoice = $this->get_post( $request['id'] ); |
|
| 647 | - if ( is_wp_error( $existing_invoice ) ) { |
|
| 645 | + if (isset($request['id'])) { |
|
| 646 | + $existing_invoice = $this->get_post($request['id']); |
|
| 647 | + if (is_wp_error($existing_invoice)) { |
|
| 648 | 648 | return $existing_invoice; |
| 649 | 649 | } |
| 650 | 650 | |
| 651 | - $prepared_invoice->ID = $existing_invoice->ID; |
|
| 651 | + $prepared_invoice->ID = $existing_invoice->ID; |
|
| 652 | 652 | $prepared_invoice->invoice_id = $existing_invoice->ID; |
| 653 | 653 | } |
| 654 | 654 | |
| 655 | 655 | $schema = $this->get_item_schema(); |
| 656 | 656 | |
| 657 | 657 | // Invoice owner. |
| 658 | - if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) { |
|
| 659 | - $prepared_invoice->user_id = (int) $request['user_id']; |
|
| 658 | + if (!empty($schema['properties']['user_id']) && isset($request['user_id'])) { |
|
| 659 | + $prepared_invoice->user_id = (int)$request['user_id']; |
|
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | // Cart details. |
| 663 | - if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) { |
|
| 664 | - $prepared_invoice->cart_details = (array) $request['cart_details']; |
|
| 663 | + if (!empty($schema['properties']['cart_details']) && isset($request['cart_details'])) { |
|
| 664 | + $prepared_invoice->cart_details = (array)$request['cart_details']; |
|
| 665 | 665 | } |
| 666 | 666 | |
| 667 | 667 | // Invoice status. |
| 668 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
| 668 | + if (!empty($schema['properties']['status']) && isset($request['status'])) { |
|
| 669 | 669 | |
| 670 | - if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) { |
|
| 670 | + if (in_array($request['status'], array_keys(wpinv_get_invoice_statuses(true, true)), true)) { |
|
| 671 | 671 | $prepared_invoice->status = $request['status']; |
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | } |
| 675 | 675 | |
| 676 | 676 | // User info |
| 677 | - if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) { |
|
| 677 | + if (!empty($schema['properties']['user_info']) && isset($request['user_info'])) { |
|
| 678 | 678 | $prepared_invoice->user_info = array(); |
| 679 | - $user_info = (array) $request['user_info']; |
|
| 679 | + $user_info = (array)$request['user_info']; |
|
| 680 | 680 | |
| 681 | - foreach( $user_info as $prop => $value ) { |
|
| 681 | + foreach ($user_info as $prop => $value) { |
|
| 682 | 682 | |
| 683 | - if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) { |
|
| 683 | + if (!empty($schema['properties']['user_info']['properties'][$prop])) { |
|
| 684 | 684 | |
| 685 | 685 | $prepared_invoice->user_info[$prop] = $value; |
| 686 | 686 | |
@@ -691,49 +691,49 @@ discard block |
||
| 691 | 691 | } |
| 692 | 692 | |
| 693 | 693 | // IP |
| 694 | - if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) { |
|
| 694 | + if (!empty($schema['properties']['ip']) && isset($request['ip'])) { |
|
| 695 | 695 | $prepared_invoice->ip = $request['ip']; |
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | // Payment details |
| 699 | 699 | $prepared_invoice->payment_details = array(); |
| 700 | 700 | |
| 701 | - if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) { |
|
| 701 | + if (!empty($schema['properties']['gateway']) && isset($request['gateway'])) { |
|
| 702 | 702 | $prepared_invoice->payment_details['gateway'] = $request['gateway']; |
| 703 | 703 | } |
| 704 | 704 | |
| 705 | - if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) { |
|
| 705 | + if (!empty($schema['properties']['gateway_title']) && isset($request['gateway_title'])) { |
|
| 706 | 706 | $prepared_invoice->payment_details['gateway_title'] = $request['gateway_title']; |
| 707 | 707 | } |
| 708 | 708 | |
| 709 | - if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) { |
|
| 709 | + if (!empty($schema['properties']['currency']) && isset($request['currency'])) { |
|
| 710 | 710 | $prepared_invoice->payment_details['currency'] = $request['currency']; |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | - if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) { |
|
| 713 | + if (!empty($schema['properties']['transaction_id']) && isset($request['transaction_id'])) { |
|
| 714 | 714 | $prepared_invoice->payment_details['transaction_id'] = $request['transaction_id']; |
| 715 | 715 | } |
| 716 | 716 | |
| 717 | 717 | // Dates |
| 718 | - if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) { |
|
| 719 | - $post_date = rest_get_date_with_gmt( $request['date'] ); |
|
| 718 | + if (!empty($schema['properties']['date']) && isset($request['date'])) { |
|
| 719 | + $post_date = rest_get_date_with_gmt($request['date']); |
|
| 720 | 720 | |
| 721 | - if ( ! empty( $post_date ) ) { |
|
| 721 | + if (!empty($post_date)) { |
|
| 722 | 722 | $prepared_invoice->post_date = $post_date[0]; |
| 723 | 723 | } |
| 724 | 724 | |
| 725 | 725 | } |
| 726 | 726 | |
| 727 | - if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) { |
|
| 728 | - $due_date = rest_get_date_with_gmt( $request['due_date'] ); |
|
| 727 | + if (!empty($schema['properties']['due_date']) && isset($request['due_date'])) { |
|
| 728 | + $due_date = rest_get_date_with_gmt($request['due_date']); |
|
| 729 | 729 | |
| 730 | - if ( ! empty( $due_date ) ) { |
|
| 730 | + if (!empty($due_date)) { |
|
| 731 | 731 | $prepared_invoice->due_date = $due_date[0]; |
| 732 | 732 | } |
| 733 | 733 | |
| 734 | 734 | } |
| 735 | 735 | |
| 736 | - $invoice_data = (array) $prepared_invoice; |
|
| 736 | + $invoice_data = (array)$prepared_invoice; |
|
| 737 | 737 | |
| 738 | 738 | /** |
| 739 | 739 | * Filters an invoice before it is inserted via the REST API. |
@@ -743,7 +743,7 @@ discard block |
||
| 743 | 743 | * @param array $invoice_data An array of invoice data |
| 744 | 744 | * @param WP_REST_Request $request Request object. |
| 745 | 745 | */ |
| 746 | - return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request ); |
|
| 746 | + return apply_filters("wpinv_rest_pre_insert_invoice", $invoice_data, $request); |
|
| 747 | 747 | |
| 748 | 748 | } |
| 749 | 749 | |
@@ -756,20 +756,20 @@ discard block |
||
| 756 | 756 | * @param WP_REST_Request $request Request object. |
| 757 | 757 | * @return WP_REST_Response Response object. |
| 758 | 758 | */ |
| 759 | - public function prepare_item_for_response( $invoice, $request ) { |
|
| 759 | + public function prepare_item_for_response($invoice, $request) { |
|
| 760 | 760 | |
| 761 | - $GLOBALS['post'] = get_post( $invoice->ID ); |
|
| 761 | + $GLOBALS['post'] = get_post($invoice->ID); |
|
| 762 | 762 | |
| 763 | - setup_postdata( $invoice->ID ); |
|
| 763 | + setup_postdata($invoice->ID); |
|
| 764 | 764 | |
| 765 | 765 | // Fetch the fields to include in this response. |
| 766 | - $fields = $this->get_fields_for_response( $request ); |
|
| 766 | + $fields = $this->get_fields_for_response($request); |
|
| 767 | 767 | |
| 768 | 768 | // Base fields for every invoice. |
| 769 | 769 | $data = array(); |
| 770 | 770 | |
| 771 | 771 | // Set up ID |
| 772 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
| 772 | + if (rest_is_field_included('id', $fields)) { |
|
| 773 | 773 | $data['id'] = $invoice->ID; |
| 774 | 774 | } |
| 775 | 775 | |
@@ -784,74 +784,74 @@ discard block |
||
| 784 | 784 | 'status', 'status_nicename', 'post_type' |
| 785 | 785 | ); |
| 786 | 786 | |
| 787 | - foreach( $invoice_properties as $property ) { |
|
| 787 | + foreach ($invoice_properties as $property) { |
|
| 788 | 788 | |
| 789 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
| 790 | - $data[$property] = $invoice->get( $property ); |
|
| 789 | + if (rest_is_field_included($property, $fields)) { |
|
| 790 | + $data[$property] = $invoice->get($property); |
|
| 791 | 791 | } |
| 792 | 792 | |
| 793 | 793 | } |
| 794 | 794 | |
| 795 | 795 | // Cart details |
| 796 | - if ( rest_is_field_included( 'cart_details', $fields ) ) { |
|
| 797 | - $data['cart_details'] = $invoice->get( 'cart_details' ); |
|
| 796 | + if (rest_is_field_included('cart_details', $fields)) { |
|
| 797 | + $data['cart_details'] = $invoice->get('cart_details'); |
|
| 798 | 798 | } |
| 799 | 799 | |
| 800 | 800 | //Dates |
| 801 | - $invoice_properties = array( 'date', 'due_date', 'completed_date' ); |
|
| 801 | + $invoice_properties = array('date', 'due_date', 'completed_date'); |
|
| 802 | 802 | |
| 803 | - foreach( $invoice_properties as $property ) { |
|
| 803 | + foreach ($invoice_properties as $property) { |
|
| 804 | 804 | |
| 805 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
| 806 | - $data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) ); |
|
| 805 | + if (rest_is_field_included($property, $fields)) { |
|
| 806 | + $data[$property] = $this->prepare_date_response('0000-00-00 00:00:00', $invoice->get($property)); |
|
| 807 | 807 | } |
| 808 | 808 | |
| 809 | 809 | } |
| 810 | 810 | |
| 811 | 811 | // User id |
| 812 | - if ( rest_is_field_included( 'user_id', $fields ) ) { |
|
| 813 | - $data['user_id'] = (int) $invoice->get( 'user_id' ); |
|
| 812 | + if (rest_is_field_included('user_id', $fields)) { |
|
| 813 | + $data['user_id'] = (int)$invoice->get('user_id'); |
|
| 814 | 814 | } |
| 815 | 815 | |
| 816 | 816 | // User info |
| 817 | - $user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' ); |
|
| 817 | + $user_info = array('first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone'); |
|
| 818 | 818 | |
| 819 | - foreach( $user_info as $property ) { |
|
| 819 | + foreach ($user_info as $property) { |
|
| 820 | 820 | |
| 821 | - if ( rest_is_field_included( "user_info.$property", $fields ) ) { |
|
| 822 | - $data['user_info'][$property] = $invoice->get( $property ); |
|
| 821 | + if (rest_is_field_included("user_info.$property", $fields)) { |
|
| 822 | + $data['user_info'][$property] = $invoice->get($property); |
|
| 823 | 823 | } |
| 824 | 824 | |
| 825 | 825 | } |
| 826 | 826 | |
| 827 | 827 | // Slug |
| 828 | - if ( rest_is_field_included( 'slug', $fields ) ) { |
|
| 829 | - $data['slug'] = $invoice->get( 'post_name' ); |
|
| 828 | + if (rest_is_field_included('slug', $fields)) { |
|
| 829 | + $data['slug'] = $invoice->get('post_name'); |
|
| 830 | 830 | } |
| 831 | 831 | |
| 832 | 832 | // View invoice link |
| 833 | - if ( rest_is_field_included( 'link', $fields ) ) { |
|
| 834 | - $data['link'] = esc_url( $invoice->get_view_url() ); |
|
| 833 | + if (rest_is_field_included('link', $fields)) { |
|
| 834 | + $data['link'] = esc_url($invoice->get_view_url()); |
|
| 835 | 835 | } |
| 836 | 836 | |
| 837 | 837 | |
| 838 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 839 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 840 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 838 | + $context = !empty($request['context']) ? $request['context'] : 'view'; |
|
| 839 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
| 840 | + $data = $this->filter_response_by_context($data, $context); |
|
| 841 | 841 | |
| 842 | 842 | // Wrap the data in a response object. |
| 843 | - $response = rest_ensure_response( $data ); |
|
| 843 | + $response = rest_ensure_response($data); |
|
| 844 | 844 | |
| 845 | - $links = $this->prepare_links( $invoice ); |
|
| 846 | - $response->add_links( $links ); |
|
| 845 | + $links = $this->prepare_links($invoice); |
|
| 846 | + $response->add_links($links); |
|
| 847 | 847 | |
| 848 | - if ( ! empty( $links['self']['href'] ) ) { |
|
| 849 | - $actions = $this->get_available_actions( $invoice, $request ); |
|
| 848 | + if (!empty($links['self']['href'])) { |
|
| 849 | + $actions = $this->get_available_actions($invoice, $request); |
|
| 850 | 850 | |
| 851 | 851 | $self = $links['self']['href']; |
| 852 | 852 | |
| 853 | - foreach ( $actions as $rel ) { |
|
| 854 | - $response->add_link( $rel, $self ); |
|
| 853 | + foreach ($actions as $rel) { |
|
| 854 | + $response->add_link($rel, $self); |
|
| 855 | 855 | } |
| 856 | 856 | } |
| 857 | 857 | |
@@ -864,7 +864,7 @@ discard block |
||
| 864 | 864 | * @param WPInv_Invoice $invoice The invoice object. |
| 865 | 865 | * @param WP_REST_Request $request Request object. |
| 866 | 866 | */ |
| 867 | - return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request ); |
|
| 867 | + return apply_filters("wpinv_rest_prepare_invoice", $response, $invoice, $request); |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | /** |
@@ -877,57 +877,57 @@ discard block |
||
| 877 | 877 | * @param WP_REST_Request $request Full details about the request. |
| 878 | 878 | * @return array Fields to be included in the response. |
| 879 | 879 | */ |
| 880 | - public function get_fields_for_response( $request ) { |
|
| 880 | + public function get_fields_for_response($request) { |
|
| 881 | 881 | $schema = $this->get_item_schema(); |
| 882 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 882 | + $properties = isset($schema['properties']) ? $schema['properties'] : array(); |
|
| 883 | 883 | |
| 884 | 884 | $additional_fields = $this->get_additional_fields(); |
| 885 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 885 | + foreach ($additional_fields as $field_name => $field_options) { |
|
| 886 | 886 | // For back-compat, include any field with an empty schema |
| 887 | 887 | // because it won't be present in $this->get_item_schema(). |
| 888 | - if ( is_null( $field_options['schema'] ) ) { |
|
| 889 | - $properties[ $field_name ] = $field_options; |
|
| 888 | + if (is_null($field_options['schema'])) { |
|
| 889 | + $properties[$field_name] = $field_options; |
|
| 890 | 890 | } |
| 891 | 891 | } |
| 892 | 892 | |
| 893 | 893 | // Exclude fields that specify a different context than the request context. |
| 894 | 894 | $context = $request['context']; |
| 895 | - if ( $context ) { |
|
| 896 | - foreach ( $properties as $name => $options ) { |
|
| 897 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 898 | - unset( $properties[ $name ] ); |
|
| 895 | + if ($context) { |
|
| 896 | + foreach ($properties as $name => $options) { |
|
| 897 | + if (!empty($options['context']) && !in_array($context, $options['context'], true)) { |
|
| 898 | + unset($properties[$name]); |
|
| 899 | 899 | } |
| 900 | 900 | } |
| 901 | 901 | } |
| 902 | 902 | |
| 903 | - $fields = array_keys( $properties ); |
|
| 903 | + $fields = array_keys($properties); |
|
| 904 | 904 | |
| 905 | - if ( ! isset( $request['_fields'] ) ) { |
|
| 905 | + if (!isset($request['_fields'])) { |
|
| 906 | 906 | return $fields; |
| 907 | 907 | } |
| 908 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 909 | - if ( 0 === count( $requested_fields ) ) { |
|
| 908 | + $requested_fields = wpinv_parse_list($request['_fields']); |
|
| 909 | + if (0 === count($requested_fields)) { |
|
| 910 | 910 | return $fields; |
| 911 | 911 | } |
| 912 | 912 | // Trim off outside whitespace from the comma delimited list. |
| 913 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 913 | + $requested_fields = array_map('trim', $requested_fields); |
|
| 914 | 914 | // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
| 915 | - if ( in_array( 'id', $fields, true ) ) { |
|
| 915 | + if (in_array('id', $fields, true)) { |
|
| 916 | 916 | $requested_fields[] = 'id'; |
| 917 | 917 | } |
| 918 | 918 | // Return the list of all requested fields which appear in the schema. |
| 919 | 919 | return array_reduce( |
| 920 | 920 | $requested_fields, |
| 921 | - function( $response_fields, $field ) use ( $fields ) { |
|
| 922 | - if ( in_array( $field, $fields, true ) ) { |
|
| 921 | + function($response_fields, $field) use ($fields) { |
|
| 922 | + if (in_array($field, $fields, true)) { |
|
| 923 | 923 | $response_fields[] = $field; |
| 924 | 924 | return $response_fields; |
| 925 | 925 | } |
| 926 | 926 | // Check for nested fields if $field is not a direct match. |
| 927 | - $nested_fields = explode( '.', $field ); |
|
| 927 | + $nested_fields = explode('.', $field); |
|
| 928 | 928 | // A nested field is included so long as its top-level property is |
| 929 | 929 | // present in the schema. |
| 930 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 930 | + if (in_array($nested_fields[0], $fields, true)) { |
|
| 931 | 931 | $response_fields[] = $field; |
| 932 | 932 | } |
| 933 | 933 | return $response_fields; |
@@ -946,8 +946,8 @@ discard block |
||
| 946 | 946 | public function get_item_schema() { |
| 947 | 947 | |
| 948 | 948 | // Maybe retrieve the schema from cache. |
| 949 | - if ( $this->schema ) { |
|
| 950 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 949 | + if ($this->schema) { |
|
| 950 | + return $this->add_additional_fields_schema($this->schema); |
|
| 951 | 951 | } |
| 952 | 952 | |
| 953 | 953 | $schema = array( |
@@ -959,266 +959,266 @@ discard block |
||
| 959 | 959 | 'properties' => array( |
| 960 | 960 | |
| 961 | 961 | 'title' => array( |
| 962 | - 'description' => __( 'The title for the invoice.', 'invoicing' ), |
|
| 962 | + 'description' => __('The title for the invoice.', 'invoicing'), |
|
| 963 | 963 | 'type' => 'string', |
| 964 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 964 | + 'context' => array('view', 'edit', 'embed'), |
|
| 965 | 965 | 'readonly' => true, |
| 966 | 966 | ), |
| 967 | 967 | |
| 968 | 968 | 'user_id' => array( |
| 969 | - 'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ), |
|
| 969 | + 'description' => __('The ID of the owner of the invoice.', 'invoicing'), |
|
| 970 | 970 | 'type' => 'integer', |
| 971 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 971 | + 'context' => array('view', 'edit', 'embed'), |
|
| 972 | 972 | ), |
| 973 | 973 | |
| 974 | 974 | 'email' => array( |
| 975 | - 'description' => __( 'The email of the owner of the invoice.', 'invoicing' ), |
|
| 975 | + 'description' => __('The email of the owner of the invoice.', 'invoicing'), |
|
| 976 | 976 | 'type' => 'string', |
| 977 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 977 | + 'context' => array('view', 'edit', 'embed'), |
|
| 978 | 978 | 'readonly' => true, |
| 979 | 979 | ), |
| 980 | 980 | |
| 981 | 981 | 'ip' => array( |
| 982 | - 'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ), |
|
| 982 | + 'description' => __('The IP of the owner of the invoice.', 'invoicing'), |
|
| 983 | 983 | 'type' => 'string', |
| 984 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 984 | + 'context' => array('view', 'edit', 'embed'), |
|
| 985 | 985 | ), |
| 986 | 986 | |
| 987 | 987 | 'user_info' => array( |
| 988 | - 'description' => __( 'Information about the owner of the invoice.', 'invoicing' ), |
|
| 988 | + 'description' => __('Information about the owner of the invoice.', 'invoicing'), |
|
| 989 | 989 | 'type' => 'object', |
| 990 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 990 | + 'context' => array('view', 'edit', 'embed'), |
|
| 991 | 991 | 'properties' => array( |
| 992 | 992 | |
| 993 | 993 | 'first_name' => array( |
| 994 | - 'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ), |
|
| 994 | + 'description' => __('The first name of the owner of the invoice.', 'invoicing'), |
|
| 995 | 995 | 'type' => 'string', |
| 996 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 996 | + 'context' => array('view', 'edit', 'embed'), |
|
| 997 | 997 | ), |
| 998 | 998 | |
| 999 | 999 | 'last_name' => array( |
| 1000 | - 'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ), |
|
| 1000 | + 'description' => __('The last name of the owner of the invoice.', 'invoicing'), |
|
| 1001 | 1001 | 'type' => 'string', |
| 1002 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1002 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1003 | 1003 | ), |
| 1004 | 1004 | |
| 1005 | 1005 | 'company' => array( |
| 1006 | - 'description' => __( 'The company of the owner of the invoice.', 'invoicing' ), |
|
| 1006 | + 'description' => __('The company of the owner of the invoice.', 'invoicing'), |
|
| 1007 | 1007 | 'type' => 'string', |
| 1008 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1008 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1009 | 1009 | ), |
| 1010 | 1010 | |
| 1011 | 1011 | 'vat_number' => array( |
| 1012 | - 'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ), |
|
| 1012 | + 'description' => __('The VAT number of the owner of the invoice.', 'invoicing'), |
|
| 1013 | 1013 | 'type' => 'string', |
| 1014 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1014 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1015 | 1015 | ), |
| 1016 | 1016 | |
| 1017 | 1017 | 'vat_rate' => array( |
| 1018 | - 'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ), |
|
| 1018 | + 'description' => __('The VAT rate applied on the invoice.', 'invoicing'), |
|
| 1019 | 1019 | 'type' => 'string', |
| 1020 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1020 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1021 | 1021 | ), |
| 1022 | 1022 | |
| 1023 | 1023 | 'address' => array( |
| 1024 | - 'description' => __( 'The address of the invoice owner.', 'invoicing' ), |
|
| 1024 | + 'description' => __('The address of the invoice owner.', 'invoicing'), |
|
| 1025 | 1025 | 'type' => 'string', |
| 1026 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1026 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1027 | 1027 | ), |
| 1028 | 1028 | |
| 1029 | 1029 | 'city' => array( |
| 1030 | - 'description' => __( 'The city of the invoice owner.', 'invoicing' ), |
|
| 1030 | + 'description' => __('The city of the invoice owner.', 'invoicing'), |
|
| 1031 | 1031 | 'type' => 'string', |
| 1032 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1032 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1033 | 1033 | ), |
| 1034 | 1034 | |
| 1035 | 1035 | 'country' => array( |
| 1036 | - 'description' => __( 'The country of the invoice owner.', 'invoicing' ), |
|
| 1036 | + 'description' => __('The country of the invoice owner.', 'invoicing'), |
|
| 1037 | 1037 | 'type' => 'string', |
| 1038 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1038 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1039 | 1039 | ), |
| 1040 | 1040 | |
| 1041 | 1041 | 'state' => array( |
| 1042 | - 'description' => __( 'The state of the invoice owner.', 'invoicing' ), |
|
| 1042 | + 'description' => __('The state of the invoice owner.', 'invoicing'), |
|
| 1043 | 1043 | 'type' => 'string', |
| 1044 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1044 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1045 | 1045 | ), |
| 1046 | 1046 | |
| 1047 | 1047 | 'zip' => array( |
| 1048 | - 'description' => __( 'The zip code of the invoice owner.', 'invoicing' ), |
|
| 1048 | + 'description' => __('The zip code of the invoice owner.', 'invoicing'), |
|
| 1049 | 1049 | 'type' => 'string', |
| 1050 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1050 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1051 | 1051 | ), |
| 1052 | 1052 | |
| 1053 | 1053 | 'phone' => array( |
| 1054 | - 'description' => __( 'The phone number of the invoice owner.', 'invoicing' ), |
|
| 1054 | + 'description' => __('The phone number of the invoice owner.', 'invoicing'), |
|
| 1055 | 1055 | 'type' => 'string', |
| 1056 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1056 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1057 | 1057 | ), |
| 1058 | 1058 | ), |
| 1059 | 1059 | ), |
| 1060 | 1060 | |
| 1061 | 1061 | 'id' => array( |
| 1062 | - 'description' => __( 'Unique identifier for the invoice.', 'invoicing' ), |
|
| 1062 | + 'description' => __('Unique identifier for the invoice.', 'invoicing'), |
|
| 1063 | 1063 | 'type' => 'integer', |
| 1064 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1064 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1065 | 1065 | 'readonly' => true, |
| 1066 | 1066 | ), |
| 1067 | 1067 | |
| 1068 | 1068 | 'key' => array( |
| 1069 | - 'description' => __( 'A unique key for the invoice.', 'invoicing' ), |
|
| 1069 | + 'description' => __('A unique key for the invoice.', 'invoicing'), |
|
| 1070 | 1070 | 'type' => 'string', |
| 1071 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1071 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1072 | 1072 | 'readonly' => true, |
| 1073 | 1073 | ), |
| 1074 | 1074 | |
| 1075 | 1075 | 'number' => array( |
| 1076 | - 'description' => __( 'The invoice number.', 'invoicing' ), |
|
| 1076 | + 'description' => __('The invoice number.', 'invoicing'), |
|
| 1077 | 1077 | 'type' => 'string', |
| 1078 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1078 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1079 | 1079 | 'readonly' => true, |
| 1080 | 1080 | ), |
| 1081 | 1081 | |
| 1082 | 1082 | 'transaction_id' => array( |
| 1083 | - 'description' => __( 'The transaction id of the invoice.', 'invoicing' ), |
|
| 1083 | + 'description' => __('The transaction id of the invoice.', 'invoicing'), |
|
| 1084 | 1084 | 'type' => 'string', |
| 1085 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1085 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1086 | 1086 | ), |
| 1087 | 1087 | |
| 1088 | 1088 | 'gateway' => array( |
| 1089 | - 'description' => __( 'The gateway used to process the invoice.', 'invoicing' ), |
|
| 1089 | + 'description' => __('The gateway used to process the invoice.', 'invoicing'), |
|
| 1090 | 1090 | 'type' => 'string', |
| 1091 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1091 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1092 | 1092 | ), |
| 1093 | 1093 | |
| 1094 | 1094 | 'gateway_title' => array( |
| 1095 | - 'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ), |
|
| 1095 | + 'description' => __('The title of the gateway used to process the invoice.', 'invoicing'), |
|
| 1096 | 1096 | 'type' => 'string', |
| 1097 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1097 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1098 | 1098 | ), |
| 1099 | 1099 | |
| 1100 | 1100 | 'total' => array( |
| 1101 | - 'description' => __( 'The total amount of the invoice.', 'invoicing' ), |
|
| 1101 | + 'description' => __('The total amount of the invoice.', 'invoicing'), |
|
| 1102 | 1102 | 'type' => 'number', |
| 1103 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1103 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1104 | 1104 | 'readonly' => true, |
| 1105 | 1105 | ), |
| 1106 | 1106 | |
| 1107 | 1107 | 'discount' => array( |
| 1108 | - 'description' => __( 'The discount applied to the invoice.', 'invoicing' ), |
|
| 1108 | + 'description' => __('The discount applied to the invoice.', 'invoicing'), |
|
| 1109 | 1109 | 'type' => 'number', |
| 1110 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1110 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1111 | 1111 | 'readonly' => true, |
| 1112 | 1112 | ), |
| 1113 | 1113 | |
| 1114 | 1114 | 'discount_code' => array( |
| 1115 | - 'description' => __( 'The discount code applied to the invoice.', 'invoicing' ), |
|
| 1115 | + 'description' => __('The discount code applied to the invoice.', 'invoicing'), |
|
| 1116 | 1116 | 'type' => 'string', |
| 1117 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1117 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1118 | 1118 | 'readonly' => true, |
| 1119 | 1119 | ), |
| 1120 | 1120 | |
| 1121 | 1121 | 'tax' => array( |
| 1122 | - 'description' => __( 'The tax applied to the invoice.', 'invoicing' ), |
|
| 1122 | + 'description' => __('The tax applied to the invoice.', 'invoicing'), |
|
| 1123 | 1123 | 'type' => 'number', |
| 1124 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1124 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1125 | 1125 | 'readonly' => true, |
| 1126 | 1126 | ), |
| 1127 | 1127 | |
| 1128 | 1128 | 'fees_total' => array( |
| 1129 | - 'description' => __( 'The total fees applied to the invoice.', 'invoicing' ), |
|
| 1129 | + 'description' => __('The total fees applied to the invoice.', 'invoicing'), |
|
| 1130 | 1130 | 'type' => 'number', |
| 1131 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1131 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1132 | 1132 | 'readonly' => true, |
| 1133 | 1133 | ), |
| 1134 | 1134 | |
| 1135 | 1135 | 'subtotal' => array( |
| 1136 | - 'description' => __( 'The sub-total for the invoice.', 'invoicing' ), |
|
| 1136 | + 'description' => __('The sub-total for the invoice.', 'invoicing'), |
|
| 1137 | 1137 | 'type' => 'number', |
| 1138 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1138 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1139 | 1139 | 'readonly' => true, |
| 1140 | 1140 | ), |
| 1141 | 1141 | |
| 1142 | 1142 | 'currency' => array( |
| 1143 | - 'description' => __( 'The currency used to process the invoice.', 'invoicing' ), |
|
| 1143 | + 'description' => __('The currency used to process the invoice.', 'invoicing'), |
|
| 1144 | 1144 | 'type' => 'string', |
| 1145 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1145 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1146 | 1146 | ), |
| 1147 | 1147 | |
| 1148 | 1148 | 'cart_details' => array( |
| 1149 | - 'description' => __( 'The cart details for invoice.', 'invoicing' ), |
|
| 1149 | + 'description' => __('The cart details for invoice.', 'invoicing'), |
|
| 1150 | 1150 | 'type' => 'array', |
| 1151 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1151 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1152 | 1152 | 'required' => true, |
| 1153 | 1153 | ), |
| 1154 | 1154 | |
| 1155 | 1155 | 'date' => array( |
| 1156 | - 'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ), |
|
| 1157 | - 'type' => array( 'string', 'null' ), |
|
| 1156 | + 'description' => __("The date the invoice was published, in the site's timezone.", 'invoicing'), |
|
| 1157 | + 'type' => array('string', 'null'), |
|
| 1158 | 1158 | 'format' => 'date-time', |
| 1159 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1159 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1160 | 1160 | ), |
| 1161 | 1161 | |
| 1162 | 1162 | 'due_date' => array( |
| 1163 | - 'description' => __( 'The due date for the invoice.', 'invoicing' ), |
|
| 1164 | - 'type' => array( 'string', 'null' ), |
|
| 1163 | + 'description' => __('The due date for the invoice.', 'invoicing'), |
|
| 1164 | + 'type' => array('string', 'null'), |
|
| 1165 | 1165 | 'format' => 'date-time', |
| 1166 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1166 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1167 | 1167 | ), |
| 1168 | 1168 | |
| 1169 | 1169 | 'completed_date' => array( |
| 1170 | - 'description' => __( 'The completed date for the invoice.', 'invoicing' ), |
|
| 1171 | - 'type' => array( 'string', 'null' ), |
|
| 1170 | + 'description' => __('The completed date for the invoice.', 'invoicing'), |
|
| 1171 | + 'type' => array('string', 'null'), |
|
| 1172 | 1172 | 'format' => 'date-time', |
| 1173 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1173 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1174 | 1174 | 'readonly' => true, |
| 1175 | 1175 | ), |
| 1176 | 1176 | |
| 1177 | 1177 | 'link' => array( |
| 1178 | - 'description' => __( 'URL to the invoice.', 'invoicing' ), |
|
| 1178 | + 'description' => __('URL to the invoice.', 'invoicing'), |
|
| 1179 | 1179 | 'type' => 'string', |
| 1180 | 1180 | 'format' => 'uri', |
| 1181 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1181 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1182 | 1182 | 'readonly' => true, |
| 1183 | 1183 | ), |
| 1184 | 1184 | |
| 1185 | 1185 | 'mode' => array( |
| 1186 | - 'description' => __( 'The mode used to process the invoice.', 'invoicing' ), |
|
| 1186 | + 'description' => __('The mode used to process the invoice.', 'invoicing'), |
|
| 1187 | 1187 | 'type' => 'string', |
| 1188 | - 'enum' => array( 'live', 'test' ), |
|
| 1189 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1188 | + 'enum' => array('live', 'test'), |
|
| 1189 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1190 | 1190 | 'readonly' => true, |
| 1191 | 1191 | ), |
| 1192 | 1192 | |
| 1193 | 1193 | 'slug' => array( |
| 1194 | - 'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ), |
|
| 1194 | + 'description' => __('An alphanumeric identifier for the invoice.', 'invoicing'), |
|
| 1195 | 1195 | 'type' => 'string', |
| 1196 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1196 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1197 | 1197 | 'arg_options' => array( |
| 1198 | - 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
|
| 1198 | + 'sanitize_callback' => array($this, 'sanitize_slug'), |
|
| 1199 | 1199 | ), |
| 1200 | 1200 | 'readonly' => true, |
| 1201 | 1201 | ), |
| 1202 | 1202 | |
| 1203 | 1203 | 'status' => array( |
| 1204 | - 'description' => __( 'A named status for the invoice.', 'invoicing' ), |
|
| 1204 | + 'description' => __('A named status for the invoice.', 'invoicing'), |
|
| 1205 | 1205 | 'type' => 'string', |
| 1206 | - 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
| 1207 | - 'context' => array( 'view', 'edit' ), |
|
| 1206 | + 'enum' => array_keys(wpinv_get_invoice_statuses(true, true)), |
|
| 1207 | + 'context' => array('view', 'edit'), |
|
| 1208 | 1208 | 'default' => 'wpi-pending', |
| 1209 | 1209 | ), |
| 1210 | 1210 | |
| 1211 | 1211 | 'status_nicename' => array( |
| 1212 | - 'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ), |
|
| 1212 | + 'description' => __('A human-readable status name for the invoice.', 'invoicing'), |
|
| 1213 | 1213 | 'type' => 'string', |
| 1214 | - 'context' => array( 'view', 'embed' ), |
|
| 1214 | + 'context' => array('view', 'embed'), |
|
| 1215 | 1215 | 'readonly' => true, |
| 1216 | 1216 | ), |
| 1217 | 1217 | |
| 1218 | 1218 | 'post_type' => array( |
| 1219 | - 'description' => __( 'The post type for the invoice.', 'invoicing' ), |
|
| 1219 | + 'description' => __('The post type for the invoice.', 'invoicing'), |
|
| 1220 | 1220 | 'type' => 'string', |
| 1221 | - 'context' => array( 'view' ), |
|
| 1221 | + 'context' => array('view'), |
|
| 1222 | 1222 | 'readonly' => true, |
| 1223 | 1223 | ), |
| 1224 | 1224 | ), |
@@ -1236,12 +1236,12 @@ discard block |
||
| 1236 | 1236 | * |
| 1237 | 1237 | * @param array $schema The invoice schema. |
| 1238 | 1238 | */ |
| 1239 | - $schema = apply_filters( "wpinv_rest_invoice_schema", $schema ); |
|
| 1239 | + $schema = apply_filters("wpinv_rest_invoice_schema", $schema); |
|
| 1240 | 1240 | |
| 1241 | 1241 | // Cache the invoice schema. |
| 1242 | 1242 | $this->schema = $schema; |
| 1243 | 1243 | |
| 1244 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 1244 | + return $this->add_additional_fields_schema($this->schema); |
|
| 1245 | 1245 | } |
| 1246 | 1246 | |
| 1247 | 1247 | /** |
@@ -1253,20 +1253,20 @@ discard block |
||
| 1253 | 1253 | */ |
| 1254 | 1254 | protected function get_schema_links() { |
| 1255 | 1255 | |
| 1256 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
| 1256 | + $href = rest_url("{$this->namespace}/{$this->rest_base}/{id}"); |
|
| 1257 | 1257 | |
| 1258 | 1258 | $links = array(); |
| 1259 | 1259 | |
| 1260 | 1260 | $links[] = array( |
| 1261 | 1261 | 'rel' => 'https://api.w.org/action-publish', |
| 1262 | - 'title' => __( 'The current user can mark this invoice as completed.', 'invoicing' ), |
|
| 1262 | + 'title' => __('The current user can mark this invoice as completed.', 'invoicing'), |
|
| 1263 | 1263 | 'href' => $href, |
| 1264 | 1264 | 'targetSchema' => array( |
| 1265 | 1265 | 'type' => 'object', |
| 1266 | 1266 | 'properties' => array( |
| 1267 | 1267 | 'status' => array( |
| 1268 | 1268 | 'type' => 'string', |
| 1269 | - 'enum' => array( 'publish', 'wpi-renewal' ), |
|
| 1269 | + 'enum' => array('publish', 'wpi-renewal'), |
|
| 1270 | 1270 | ), |
| 1271 | 1271 | ), |
| 1272 | 1272 | ), |
@@ -1274,7 +1274,7 @@ discard block |
||
| 1274 | 1274 | |
| 1275 | 1275 | $links[] = array( |
| 1276 | 1276 | 'rel' => 'https://api.w.org/action-assign-author', |
| 1277 | - 'title' => __( 'The current user can change the owner of this invoice.', 'invoicing' ), |
|
| 1277 | + 'title' => __('The current user can change the owner of this invoice.', 'invoicing'), |
|
| 1278 | 1278 | 'href' => $href, |
| 1279 | 1279 | 'targetSchema' => array( |
| 1280 | 1280 | 'type' => 'object', |
@@ -1297,24 +1297,24 @@ discard block |
||
| 1297 | 1297 | * @param WPInv_Invoice $invoice Invoice Object. |
| 1298 | 1298 | * @return array Links for the given invoice. |
| 1299 | 1299 | */ |
| 1300 | - protected function prepare_links( $invoice ) { |
|
| 1300 | + protected function prepare_links($invoice) { |
|
| 1301 | 1301 | |
| 1302 | 1302 | // Prepare the base REST API endpoint for invoices. |
| 1303 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
| 1303 | + $base = sprintf('%s/%s', $this->namespace, $this->rest_base); |
|
| 1304 | 1304 | |
| 1305 | 1305 | // Entity meta. |
| 1306 | 1306 | $links = array( |
| 1307 | 1307 | 'self' => array( |
| 1308 | - 'href' => rest_url( trailingslashit( $base ) . $invoice->ID ), |
|
| 1308 | + 'href' => rest_url(trailingslashit($base) . $invoice->ID), |
|
| 1309 | 1309 | ), |
| 1310 | 1310 | 'collection' => array( |
| 1311 | - 'href' => rest_url( $base ), |
|
| 1311 | + 'href' => rest_url($base), |
|
| 1312 | 1312 | ), |
| 1313 | 1313 | ); |
| 1314 | 1314 | |
| 1315 | - if ( ! empty( $invoice->user_id ) ) { |
|
| 1315 | + if (!empty($invoice->user_id)) { |
|
| 1316 | 1316 | $links['user'] = array( |
| 1317 | - 'href' => rest_url( 'wp/v2/users/' . $invoice->user_id ), |
|
| 1317 | + 'href' => rest_url('wp/v2/users/' . $invoice->user_id), |
|
| 1318 | 1318 | 'embeddable' => true, |
| 1319 | 1319 | ); |
| 1320 | 1320 | } |
@@ -1328,7 +1328,7 @@ discard block |
||
| 1328 | 1328 | * |
| 1329 | 1329 | * @param array $links Rest links. |
| 1330 | 1330 | */ |
| 1331 | - return apply_filters( "wpinv_invoice_rest_links", $links ); |
|
| 1331 | + return apply_filters("wpinv_invoice_rest_links", $links); |
|
| 1332 | 1332 | |
| 1333 | 1333 | } |
| 1334 | 1334 | |
@@ -1341,24 +1341,24 @@ discard block |
||
| 1341 | 1341 | * @param WP_REST_Request $request Request object. |
| 1342 | 1342 | * @return array List of link relations. |
| 1343 | 1343 | */ |
| 1344 | - protected function get_available_actions( $invoice, $request ) { |
|
| 1344 | + protected function get_available_actions($invoice, $request) { |
|
| 1345 | 1345 | |
| 1346 | - if ( 'edit' !== $request['context'] ) { |
|
| 1346 | + if ('edit' !== $request['context']) { |
|
| 1347 | 1347 | return array(); |
| 1348 | 1348 | } |
| 1349 | 1349 | |
| 1350 | 1350 | $rels = array(); |
| 1351 | 1351 | |
| 1352 | 1352 | // Retrieve the post type object. |
| 1353 | - $post_type = get_post_type_object( $invoice->post_type ); |
|
| 1353 | + $post_type = get_post_type_object($invoice->post_type); |
|
| 1354 | 1354 | |
| 1355 | 1355 | // Mark invoice as completed. |
| 1356 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
| 1356 | + if (current_user_can($post_type->cap->publish_posts)) { |
|
| 1357 | 1357 | $rels[] = 'https://api.w.org/action-publish'; |
| 1358 | 1358 | } |
| 1359 | 1359 | |
| 1360 | 1360 | // Change the owner of the invoice. |
| 1361 | - if ( current_user_can( $post_type->cap->edit_others_posts ) ) { |
|
| 1361 | + if (current_user_can($post_type->cap->edit_others_posts)) { |
|
| 1362 | 1362 | $rels[] = 'https://api.w.org/action-assign-author'; |
| 1363 | 1363 | } |
| 1364 | 1364 | |
@@ -1371,7 +1371,7 @@ discard block |
||
| 1371 | 1371 | * |
| 1372 | 1372 | * @param array $rels Available link relations. |
| 1373 | 1373 | */ |
| 1374 | - return apply_filters( "wpinv_invoice_rest_link_relations", $rels ); |
|
| 1374 | + return apply_filters("wpinv_invoice_rest_link_relations", $rels); |
|
| 1375 | 1375 | } |
| 1376 | 1376 | |
| 1377 | 1377 | /** |
@@ -1384,11 +1384,11 @@ discard block |
||
| 1384 | 1384 | * @param string $parameter Additional parameter to pass to validation. |
| 1385 | 1385 | * @return array|WP_Error A list of valid statuses, otherwise WP_Error object. |
| 1386 | 1386 | */ |
| 1387 | - public function sanitize_post_statuses( $statuses, $request, $parameter ) { |
|
| 1387 | + public function sanitize_post_statuses($statuses, $request, $parameter) { |
|
| 1388 | 1388 | |
| 1389 | - $statuses = wp_parse_slug_list( $statuses ); |
|
| 1390 | - $valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
| 1391 | - return array_intersect( $statuses, $valid_statuses ); |
|
| 1389 | + $statuses = wp_parse_slug_list($statuses); |
|
| 1390 | + $valid_statuses = array_keys(wpinv_get_invoice_statuses(true, true)); |
|
| 1391 | + return array_intersect($statuses, $valid_statuses); |
|
| 1392 | 1392 | |
| 1393 | 1393 | } |
| 1394 | 1394 | |
@@ -700,7 +700,7 @@ discard block |
||
| 700 | 700 | } |
| 701 | 701 | |
| 702 | 702 | function wpinv_get_php_arg_separator_output() { |
| 703 | - return ini_get( 'arg_separator.output' ); |
|
| 703 | + return ini_get( 'arg_separator.output' ); |
|
| 704 | 704 | } |
| 705 | 705 | |
| 706 | 706 | function wpinv_rgb_from_hex( $color ) { |
@@ -1038,9 +1038,9 @@ discard block |
||
| 1038 | 1038 | * @return array Sanitized array of values. |
| 1039 | 1039 | */ |
| 1040 | 1040 | function wpinv_parse_list( $list ) { |
| 1041 | - if ( ! is_array( $list ) ) { |
|
| 1042 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1043 | - } |
|
| 1041 | + if ( ! is_array( $list ) ) { |
|
| 1042 | + return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1043 | + } |
|
| 1044 | 1044 | |
| 1045 | - return $list; |
|
| 1045 | + return $list; |
|
| 1046 | 1046 | } |
| 1047 | 1047 | \ No newline at end of file |
@@ -7,132 +7,132 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | // MUST have WordPress. |
| 10 | -if ( !defined( 'WPINC' ) ) { |
|
| 11 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
| 10 | +if (!defined('WPINC')) { |
|
| 11 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | function wpinv_item_quantities_enabled() { |
| 15 | - $ret = wpinv_get_option( 'item_quantities', true ); |
|
| 15 | + $ret = wpinv_get_option('item_quantities', true); |
|
| 16 | 16 | |
| 17 | - return (bool) apply_filters( 'wpinv_item_quantities_enabled', $ret ); |
|
| 17 | + return (bool)apply_filters('wpinv_item_quantities_enabled', $ret); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | function wpinv_get_ip() { |
| 21 | 21 | $ip = '127.0.0.1'; |
| 22 | 22 | |
| 23 | - if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
|
| 24 | - $ip = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); |
|
| 25 | - } elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
|
| 26 | - $ip = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); |
|
| 27 | - } elseif( !empty( $_SERVER['REMOTE_ADDR'] ) ) { |
|
| 28 | - $ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); |
|
| 23 | + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
|
| 24 | + $ip = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']); |
|
| 25 | + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
| 26 | + $ip = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']); |
|
| 27 | + } elseif (!empty($_SERVER['REMOTE_ADDR'])) { |
|
| 28 | + $ip = sanitize_text_field($_SERVER['REMOTE_ADDR']); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - return apply_filters( 'wpinv_get_ip', $ip ); |
|
| 31 | + return apply_filters('wpinv_get_ip', $ip); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | function wpinv_get_user_agent() { |
| 35 | - if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
|
| 36 | - $user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); |
|
| 35 | + if (!empty($_SERVER['HTTP_USER_AGENT'])) { |
|
| 36 | + $user_agent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']); |
|
| 37 | 37 | } else { |
| 38 | 38 | $user_agent = ''; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - return apply_filters( 'wpinv_get_user_agent', $user_agent ); |
|
| 41 | + return apply_filters('wpinv_get_user_agent', $user_agent); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | -function wpinv_sanitize_amount( $amount, $decimals = NULL ) { |
|
| 44 | +function wpinv_sanitize_amount($amount, $decimals = NULL) { |
|
| 45 | 45 | $is_negative = false; |
| 46 | 46 | $thousands_sep = wpinv_thousands_separator(); |
| 47 | 47 | $decimal_sep = wpinv_decimal_separator(); |
| 48 | - if ( $decimals === NULL ) { |
|
| 48 | + if ($decimals === NULL) { |
|
| 49 | 49 | $decimals = wpinv_decimals(); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | // Sanitize the amount |
| 53 | - if ( $decimal_sep == ',' && false !== ( $found = strpos( $amount, $decimal_sep ) ) ) { |
|
| 54 | - if ( ( $thousands_sep == '.' || $thousands_sep == ' ' ) && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 55 | - $amount = str_replace( $thousands_sep, '', $amount ); |
|
| 56 | - } elseif( empty( $thousands_sep ) && false !== ( $found = strpos( $amount, '.' ) ) ) { |
|
| 57 | - $amount = str_replace( '.', '', $amount ); |
|
| 53 | + if ($decimal_sep == ',' && false !== ($found = strpos($amount, $decimal_sep))) { |
|
| 54 | + if (($thousands_sep == '.' || $thousands_sep == ' ') && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 55 | + $amount = str_replace($thousands_sep, '', $amount); |
|
| 56 | + } elseif (empty($thousands_sep) && false !== ($found = strpos($amount, '.'))) { |
|
| 57 | + $amount = str_replace('.', '', $amount); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - $amount = str_replace( $decimal_sep, '.', $amount ); |
|
| 61 | - } elseif( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 62 | - $amount = str_replace( $thousands_sep, '', $amount ); |
|
| 60 | + $amount = str_replace($decimal_sep, '.', $amount); |
|
| 61 | + } elseif ($thousands_sep == ',' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 62 | + $amount = str_replace($thousands_sep, '', $amount); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if( $amount < 0 ) { |
|
| 65 | + if ($amount < 0) { |
|
| 66 | 66 | $is_negative = true; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - $amount = preg_replace( '/[^0-9\.]/', '', $amount ); |
|
| 69 | + $amount = preg_replace('/[^0-9\.]/', '', $amount); |
|
| 70 | 70 | |
| 71 | - $decimals = apply_filters( 'wpinv_sanitize_amount_decimals', absint( $decimals ), $amount ); |
|
| 72 | - $amount = number_format( (double) $amount, absint( $decimals ), '.', '' ); |
|
| 71 | + $decimals = apply_filters('wpinv_sanitize_amount_decimals', absint($decimals), $amount); |
|
| 72 | + $amount = number_format((double)$amount, absint($decimals), '.', ''); |
|
| 73 | 73 | |
| 74 | - if( $is_negative ) { |
|
| 74 | + if ($is_negative) { |
|
| 75 | 75 | $amount *= -1; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - return apply_filters( 'wpinv_sanitize_amount', $amount, $decimals ); |
|
| 78 | + return apply_filters('wpinv_sanitize_amount', $amount, $decimals); |
|
| 79 | 79 | } |
| 80 | -add_filter( 'wpinv_sanitize_amount_decimals', 'wpinv_currency_decimal_filter', 10, 1 ); |
|
| 80 | +add_filter('wpinv_sanitize_amount_decimals', 'wpinv_currency_decimal_filter', 10, 1); |
|
| 81 | 81 | |
| 82 | -function wpinv_round_amount( $amount, $decimals = NULL ) { |
|
| 83 | - if ( $decimals === NULL ) { |
|
| 82 | +function wpinv_round_amount($amount, $decimals = NULL) { |
|
| 83 | + if ($decimals === NULL) { |
|
| 84 | 84 | $decimals = wpinv_decimals(); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - $amount = round( (double)$amount, wpinv_currency_decimal_filter( absint( $decimals ) ) ); |
|
| 87 | + $amount = round((double)$amount, wpinv_currency_decimal_filter(absint($decimals))); |
|
| 88 | 88 | |
| 89 | - return apply_filters( 'wpinv_round_amount', $amount, $decimals ); |
|
| 89 | + return apply_filters('wpinv_round_amount', $amount, $decimals); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | -function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
|
| 92 | +function wpinv_get_invoice_statuses($draft = false, $trashed = false, $invoice = false) { |
|
| 93 | 93 | global $post; |
| 94 | 94 | |
| 95 | 95 | $invoice_statuses = array( |
| 96 | - 'wpi-pending' => __( 'Pending Payment', 'invoicing' ), |
|
| 97 | - 'publish' => __( 'Paid', 'invoicing'), |
|
| 98 | - 'wpi-processing' => __( 'Processing', 'invoicing' ), |
|
| 99 | - 'wpi-onhold' => __( 'On Hold', 'invoicing' ), |
|
| 100 | - 'wpi-refunded' => __( 'Refunded', 'invoicing' ), |
|
| 101 | - 'wpi-cancelled' => __( 'Cancelled', 'invoicing' ), |
|
| 102 | - 'wpi-failed' => __( 'Failed', 'invoicing' ), |
|
| 103 | - 'wpi-renewal' => __( 'Renewal Payment', 'invoicing' ) |
|
| 96 | + 'wpi-pending' => __('Pending Payment', 'invoicing'), |
|
| 97 | + 'publish' => __('Paid', 'invoicing'), |
|
| 98 | + 'wpi-processing' => __('Processing', 'invoicing'), |
|
| 99 | + 'wpi-onhold' => __('On Hold', 'invoicing'), |
|
| 100 | + 'wpi-refunded' => __('Refunded', 'invoicing'), |
|
| 101 | + 'wpi-cancelled' => __('Cancelled', 'invoicing'), |
|
| 102 | + 'wpi-failed' => __('Failed', 'invoicing'), |
|
| 103 | + 'wpi-renewal' => __('Renewal Payment', 'invoicing') |
|
| 104 | 104 | ); |
| 105 | 105 | |
| 106 | - if ( $draft ) { |
|
| 107 | - $invoice_statuses['draft'] = __( 'Draft', 'invoicing' ); |
|
| 106 | + if ($draft) { |
|
| 107 | + $invoice_statuses['draft'] = __('Draft', 'invoicing'); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if ( $trashed ) { |
|
| 111 | - $invoice_statuses['trash'] = __( 'Trash', 'invoicing' ); |
|
| 110 | + if ($trashed) { |
|
| 111 | + $invoice_statuses['trash'] = __('Trash', 'invoicing'); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 114 | + return apply_filters('wpinv_statuses', $invoice_statuses, $invoice); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | -function wpinv_status_nicename( $status ) { |
|
| 118 | - $statuses = wpinv_get_invoice_statuses( true, true ); |
|
| 119 | - $status = isset( $statuses[$status] ) ? $statuses[$status] : __( $status, 'invoicing' ); |
|
| 117 | +function wpinv_status_nicename($status) { |
|
| 118 | + $statuses = wpinv_get_invoice_statuses(true, true); |
|
| 119 | + $status = isset($statuses[$status]) ? $statuses[$status] : __($status, 'invoicing'); |
|
| 120 | 120 | |
| 121 | 121 | return $status; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | function wpinv_get_currency() { |
| 125 | - $currency = wpinv_get_option( 'currency', 'USD' ); |
|
| 125 | + $currency = wpinv_get_option('currency', 'USD'); |
|
| 126 | 126 | |
| 127 | - return apply_filters( 'wpinv_currency', $currency ); |
|
| 127 | + return apply_filters('wpinv_currency', $currency); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | -function wpinv_currency_symbol( $currency = '' ) { |
|
| 131 | - if ( empty( $currency ) ) { |
|
| 130 | +function wpinv_currency_symbol($currency = '') { |
|
| 131 | + if (empty($currency)) { |
|
| 132 | 132 | $currency = wpinv_get_currency(); |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - $symbols = apply_filters( 'wpinv_currency_symbols', array( |
|
| 135 | + $symbols = apply_filters('wpinv_currency_symbols', array( |
|
| 136 | 136 | 'AED' => 'د.إ', |
| 137 | 137 | 'AFN' => '؋', |
| 138 | 138 | 'ALL' => 'L', |
@@ -295,209 +295,209 @@ discard block |
||
| 295 | 295 | 'YER' => '﷼', |
| 296 | 296 | 'ZAR' => 'R', |
| 297 | 297 | 'ZMW' => 'ZK', |
| 298 | - ) ); |
|
| 298 | + )); |
|
| 299 | 299 | |
| 300 | - $currency_symbol = isset( $symbols[$currency] ) ? $symbols[$currency] : $currency; |
|
| 300 | + $currency_symbol = isset($symbols[$currency]) ? $symbols[$currency] : $currency; |
|
| 301 | 301 | |
| 302 | - return apply_filters( 'wpinv_currency_symbol', $currency_symbol, $currency ); |
|
| 302 | + return apply_filters('wpinv_currency_symbol', $currency_symbol, $currency); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | function wpinv_currency_position() { |
| 306 | - $position = wpinv_get_option( 'currency_position', 'left' ); |
|
| 306 | + $position = wpinv_get_option('currency_position', 'left'); |
|
| 307 | 307 | |
| 308 | - return apply_filters( 'wpinv_currency_position', $position ); |
|
| 308 | + return apply_filters('wpinv_currency_position', $position); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | function wpinv_thousands_separator() { |
| 312 | - $thousand_sep = wpinv_get_option( 'thousands_separator', ',' ); |
|
| 312 | + $thousand_sep = wpinv_get_option('thousands_separator', ','); |
|
| 313 | 313 | |
| 314 | - return apply_filters( 'wpinv_thousands_separator', $thousand_sep ); |
|
| 314 | + return apply_filters('wpinv_thousands_separator', $thousand_sep); |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | function wpinv_decimal_separator() { |
| 318 | - $decimal_sep = wpinv_get_option( 'decimal_separator', '.' ); |
|
| 318 | + $decimal_sep = wpinv_get_option('decimal_separator', '.'); |
|
| 319 | 319 | |
| 320 | - return apply_filters( 'wpinv_decimal_separator', $decimal_sep ); |
|
| 320 | + return apply_filters('wpinv_decimal_separator', $decimal_sep); |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | function wpinv_decimals() { |
| 324 | - $decimals = apply_filters( 'wpinv_decimals', wpinv_get_option( 'decimals', 2 ) ); |
|
| 324 | + $decimals = apply_filters('wpinv_decimals', wpinv_get_option('decimals', 2)); |
|
| 325 | 325 | |
| 326 | - return absint( $decimals ); |
|
| 326 | + return absint($decimals); |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | function wpinv_get_currencies() { |
| 330 | 330 | $currencies = array( |
| 331 | - 'USD' => __( 'US Dollar', 'invoicing' ), |
|
| 332 | - 'EUR' => __( 'Euro', 'invoicing' ), |
|
| 333 | - 'GBP' => __( 'Pound Sterling', 'invoicing' ), |
|
| 334 | - 'AED' => __( 'United Arab Emirates', 'invoicing' ), |
|
| 335 | - 'AFN' => __( 'Afghan Afghani', 'invoicing' ), |
|
| 336 | - 'ALL' => __( 'Albanian Lek', 'invoicing' ), |
|
| 337 | - 'AMD' => __( 'Armenian Dram', 'invoicing' ), |
|
| 338 | - 'ANG' => __( 'Netherlands Antillean Guilder', 'invoicing' ), |
|
| 339 | - 'AOA' => __( 'Angolan Kwanza', 'invoicing' ), |
|
| 340 | - 'ARS' => __( 'Argentine Peso', 'invoicing' ), |
|
| 341 | - 'AUD' => __( 'Australian Dollar', 'invoicing' ), |
|
| 342 | - 'AWG' => __( 'Aruban Florin', 'invoicing' ), |
|
| 343 | - 'AZN' => __( 'Azerbaijani Manat', 'invoicing' ), |
|
| 344 | - 'BAM' => __( 'Bosnia and Herzegovina Convertible Marka', 'invoicing' ), |
|
| 345 | - 'BBD' => __( 'Barbadian Dollar', 'invoicing' ), |
|
| 346 | - 'BDT' => __( 'Bangladeshi Taka', 'invoicing' ), |
|
| 347 | - 'BGN' => __( 'Bulgarian Lev', 'invoicing' ), |
|
| 348 | - 'BHD' => __( 'Bahraini Dinar', 'invoicing' ), |
|
| 349 | - 'BIF' => __( 'Burundian Franc', 'invoicing' ), |
|
| 350 | - 'BMD' => __( 'Bermudian Dollar', 'invoicing' ), |
|
| 351 | - 'BND' => __( 'Brunei Dollar', 'invoicing' ), |
|
| 352 | - 'BOB' => __( 'Bolivian Boliviano', 'invoicing' ), |
|
| 353 | - 'BRL' => __( 'Brazilian Real', 'invoicing' ), |
|
| 354 | - 'BSD' => __( 'Bahamian Dollar', 'invoicing' ), |
|
| 355 | - 'BTC' => __( 'Bitcoin', 'invoicing' ), |
|
| 356 | - 'BTN' => __( 'Bhutanese Ngultrum', 'invoicing' ), |
|
| 357 | - 'BWP' => __( 'Botswana Pula', 'invoicing' ), |
|
| 358 | - 'BYN' => __( 'Belarusian Ruble', 'invoicing' ), |
|
| 359 | - 'BZD' => __( 'Belize Dollar', 'invoicing' ), |
|
| 360 | - 'CAD' => __( 'Canadian Dollar', 'invoicing' ), |
|
| 361 | - 'CDF' => __( 'Congolese Franc', 'invoicing' ), |
|
| 362 | - 'CHF' => __( 'Swiss Franc', 'invoicing' ), |
|
| 363 | - 'CLP' => __( 'Chilean Peso', 'invoicing' ), |
|
| 364 | - 'CNY' => __( 'Chinese Yuan', 'invoicing' ), |
|
| 365 | - 'COP' => __( 'Colombian Peso', 'invoicing' ), |
|
| 366 | - 'CRC' => __( 'Costa Rican Colon', 'invoicing' ), |
|
| 367 | - 'CUC' => __( 'Cuban Convertible Peso', 'invoicing' ), |
|
| 368 | - 'CUP' => __( 'Cuban Peso', 'invoicing' ), |
|
| 369 | - 'CVE' => __( 'Cape Verdean escudo', 'invoicing' ), |
|
| 370 | - 'CZK' => __( 'Czech Koruna', 'invoicing' ), |
|
| 371 | - 'DJF' => __( 'Djiboutian Franc', 'invoicing' ), |
|
| 372 | - 'DKK' => __( 'Danish Krone', 'invoicing' ), |
|
| 373 | - 'DOP' => __( 'Dominican Peso', 'invoicing' ), |
|
| 374 | - 'DZD' => __( 'Algerian Dinar', 'invoicing' ), |
|
| 375 | - 'EGP' => __( 'Egyptian Pound', 'invoicing' ), |
|
| 376 | - 'ERN' => __( 'Eritrean Nakfa', 'invoicing' ), |
|
| 377 | - 'ETB' => __( 'Ethiopian Irr', 'invoicing' ), |
|
| 378 | - 'FJD' => __( 'Fijian Dollar', 'invoicing' ), |
|
| 379 | - 'FKP' => __( 'Falkland Islands Pound', 'invoicing' ), |
|
| 380 | - 'GEL' => __( 'Georgian Lari', 'invoicing' ), |
|
| 381 | - 'GGP' => __( 'Guernsey Pound', 'invoicing' ), |
|
| 382 | - 'GHS' => __( 'Ghana Cedi', 'invoicing' ), |
|
| 383 | - 'GIP' => __( 'Gibraltar Pound', 'invoicing' ), |
|
| 384 | - 'GMD' => __( 'Gambian Dalasi', 'invoicing' ), |
|
| 385 | - 'GNF' => __( 'Guinean Franc', 'invoicing' ), |
|
| 386 | - 'GTQ' => __( 'Guatemalan Quetzal', 'invoicing' ), |
|
| 387 | - 'GYD' => __( 'Guyanese Dollar', 'invoicing' ), |
|
| 388 | - 'HKD' => __( 'Hong Kong Dollar', 'invoicing' ), |
|
| 389 | - 'HNL' => __( 'Honduran Lempira', 'invoicing' ), |
|
| 390 | - 'HRK' => __( 'Croatian Kuna', 'invoicing' ), |
|
| 391 | - 'HTG' => __( 'Haitian Gourde', 'invoicing' ), |
|
| 392 | - 'HUF' => __( 'Hungarian Forint', 'invoicing' ), |
|
| 393 | - 'IDR' => __( 'Indonesian Rupiah', 'invoicing' ), |
|
| 394 | - 'ILS' => __( 'Israeli New Shekel', 'invoicing' ), |
|
| 395 | - 'IMP' => __( 'Manx Pound', 'invoicing' ), |
|
| 396 | - 'INR' => __( 'Indian Rupee', 'invoicing' ), |
|
| 397 | - 'IQD' => __( 'Iraqi Dinar', 'invoicing' ), |
|
| 398 | - 'IRR' => __( 'Iranian Rial', 'invoicing' ), |
|
| 399 | - 'IRT' => __( 'Iranian Toman', 'invoicing' ), |
|
| 400 | - 'ISK' => __( 'Icelandic Krona', 'invoicing' ), |
|
| 401 | - 'JEP' => __( 'Jersey Pound', 'invoicing' ), |
|
| 402 | - 'JMD' => __( 'Jamaican Dollar', 'invoicing' ), |
|
| 403 | - 'JOD' => __( 'Jordanian Dinar', 'invoicing' ), |
|
| 404 | - 'JPY' => __( 'Japanese Yen', 'invoicing' ), |
|
| 405 | - 'KES' => __( 'Kenyan Shilling', 'invoicing' ), |
|
| 406 | - 'KGS' => __( 'Kyrgyzstani Som', 'invoicing' ), |
|
| 407 | - 'KHR' => __( 'Cambodian Riel', 'invoicing' ), |
|
| 408 | - 'KMF' => __( 'Comorian Franc', 'invoicing' ), |
|
| 409 | - 'KPW' => __( 'North Korean Won', 'invoicing' ), |
|
| 410 | - 'KRW' => __( 'South Korean Won', 'invoicing' ), |
|
| 411 | - 'KWD' => __( 'Kuwaiti Dinar', 'invoicing' ), |
|
| 412 | - 'KYD' => __( 'Cayman Islands Dollar', 'invoicing' ), |
|
| 413 | - 'KZT' => __( 'Kazakhstani Tenge', 'invoicing' ), |
|
| 414 | - 'LAK' => __( 'Lao Kip', 'invoicing' ), |
|
| 415 | - 'LBP' => __( 'Lebanese Pound', 'invoicing' ), |
|
| 416 | - 'LKR' => __( 'Sri Lankan Rupee', 'invoicing' ), |
|
| 417 | - 'LRD' => __( 'Liberian Dollar', 'invoicing' ), |
|
| 418 | - 'LSL' => __( 'Lesotho Loti', 'invoicing' ), |
|
| 419 | - 'LYD' => __( 'Libyan Dinar', 'invoicing' ), |
|
| 420 | - 'MAD' => __( 'Moroccan Dirham', 'invoicing' ), |
|
| 421 | - 'MDL' => __( 'Moldovan Leu', 'invoicing' ), |
|
| 422 | - 'MGA' => __( 'Malagasy Ariary', 'invoicing' ), |
|
| 423 | - 'MKD' => __( 'Macedonian Denar', 'invoicing' ), |
|
| 424 | - 'MMK' => __( 'Burmese Kyat', 'invoicing' ), |
|
| 425 | - 'MNT' => __( 'Mongolian Tughrik', 'invoicing' ), |
|
| 426 | - 'MOP' => __( 'Macanese Pataca', 'invoicing' ), |
|
| 427 | - 'MRO' => __( 'Mauritanian Ouguiya', 'invoicing' ), |
|
| 428 | - 'MUR' => __( 'Mauritian Rupee', 'invoicing' ), |
|
| 429 | - 'MVR' => __( 'Maldivian Rufiyaa', 'invoicing' ), |
|
| 430 | - 'MWK' => __( 'Malawian Kwacha', 'invoicing' ), |
|
| 431 | - 'MXN' => __( 'Mexican Peso', 'invoicing' ), |
|
| 432 | - 'MYR' => __( 'Malaysian Ringgit', 'invoicing' ), |
|
| 433 | - 'MZN' => __( 'Mozambican Metical', 'invoicing' ), |
|
| 434 | - 'NAD' => __( 'Namibian Dollar', 'invoicing' ), |
|
| 435 | - 'NGN' => __( 'Nigerian Naira', 'invoicing' ), |
|
| 436 | - 'NIO' => __( 'Nicaraguan Cordoba', 'invoicing' ), |
|
| 437 | - 'NOK' => __( 'Norwegian Krone', 'invoicing' ), |
|
| 438 | - 'NPR' => __( 'Nepalese Rupee', 'invoicing' ), |
|
| 439 | - 'NZD' => __( 'New Zealand Dollar', 'invoicing' ), |
|
| 440 | - 'OMR' => __( 'Omani Rial', 'invoicing' ), |
|
| 441 | - 'PAB' => __( 'Panamanian Balboa', 'invoicing' ), |
|
| 442 | - 'PEN' => __( 'Peruvian Nuevo Sol', 'invoicing' ), |
|
| 443 | - 'PGK' => __( 'Papua New Guinean Kina', 'invoicing' ), |
|
| 444 | - 'PHP' => __( 'Philippine Peso', 'invoicing' ), |
|
| 445 | - 'PKR' => __( 'Pakistani Rupee', 'invoicing' ), |
|
| 446 | - 'PLN' => __( 'Polish Zloty', 'invoicing' ), |
|
| 447 | - 'PRB' => __( 'Transnistrian Ruble', 'invoicing' ), |
|
| 448 | - 'PYG' => __( 'Paraguayan Guarani', 'invoicing' ), |
|
| 449 | - 'QAR' => __( 'Qatari Riyal', 'invoicing' ), |
|
| 450 | - 'RON' => __( 'Romanian Leu', 'invoicing' ), |
|
| 451 | - 'RSD' => __( 'Serbian Dinar', 'invoicing' ), |
|
| 452 | - 'RUB' => __( 'Russian Ruble', 'invoicing' ), |
|
| 453 | - 'RWF' => __( 'Rwandan Franc', 'invoicing' ), |
|
| 454 | - 'SAR' => __( 'Saudi Riyal', 'invoicing' ), |
|
| 455 | - 'SBD' => __( 'Solomon Islands Dollar', 'invoicing' ), |
|
| 456 | - 'SCR' => __( 'Seychellois Rupee', 'invoicing' ), |
|
| 457 | - 'SDG' => __( 'Sudanese Pound', 'invoicing' ), |
|
| 458 | - 'SEK' => __( 'Swedish Krona', 'invoicing' ), |
|
| 459 | - 'SGD' => __( 'Singapore Dollar', 'invoicing' ), |
|
| 460 | - 'SHP' => __( 'Saint Helena Pound', 'invoicing' ), |
|
| 461 | - 'SLL' => __( 'Sierra Leonean Leone', 'invoicing' ), |
|
| 462 | - 'SOS' => __( 'Somali Shilling', 'invoicing' ), |
|
| 463 | - 'SRD' => __( 'Surinamese Dollar', 'invoicing' ), |
|
| 464 | - 'SSP' => __( 'South Sudanese Pound', 'invoicing' ), |
|
| 465 | - 'STD' => __( 'Sao Tomean Dobra', 'invoicing' ), |
|
| 466 | - 'SYP' => __( 'Syrian Pound', 'invoicing' ), |
|
| 467 | - 'SZL' => __( 'Swazi Lilangeni', 'invoicing' ), |
|
| 468 | - 'THB' => __( 'Thai Baht', 'invoicing' ), |
|
| 469 | - 'TJS' => __( 'Tajikistani Somoni', 'invoicing' ), |
|
| 470 | - 'TMT' => __( 'Turkmenistan Manat', 'invoicing' ), |
|
| 471 | - 'TND' => __( 'Tunisian Dinar', 'invoicing' ), |
|
| 472 | - 'TOP' => __( 'Tongan Paʻanga', 'invoicing' ), |
|
| 473 | - 'TRY' => __( 'Turkish Lira', 'invoicing' ), |
|
| 474 | - 'TTD' => __( 'Trinidad and Tobago Dollar', 'invoicing' ), |
|
| 475 | - 'TWD' => __( 'New Taiwan Dollar', 'invoicing' ), |
|
| 476 | - 'TZS' => __( 'Tanzanian Shilling', 'invoicing' ), |
|
| 477 | - 'UAH' => __( 'Ukrainian Hryvnia', 'invoicing' ), |
|
| 478 | - 'UGX' => __( 'Ugandan Shilling', 'invoicing' ), |
|
| 479 | - 'UYU' => __( 'Uruguayan Peso', 'invoicing' ), |
|
| 480 | - 'UZS' => __( 'Uzbekistani Som', 'invoicing' ), |
|
| 481 | - 'VEF' => __( 'Venezuelan Bolívar', 'invoicing' ), |
|
| 482 | - 'VND' => __( 'Vietnamese Dong', 'invoicing' ), |
|
| 483 | - 'VUV' => __( 'Vanuatu Vatu', 'invoicing' ), |
|
| 484 | - 'WST' => __( 'Samoan Tala', 'invoicing' ), |
|
| 485 | - 'XAF' => __( 'Central African CFA Franc', 'invoicing' ), |
|
| 486 | - 'XCD' => __( 'East Caribbean Dollar', 'invoicing' ), |
|
| 487 | - 'XOF' => __( 'West African CFA Franc', 'invoicing' ), |
|
| 488 | - 'XPF' => __( 'CFP Franc', 'invoicing' ), |
|
| 489 | - 'YER' => __( 'Yemeni Rial', 'invoicing' ), |
|
| 490 | - 'ZAR' => __( 'South African Rand', 'invoicing' ), |
|
| 491 | - 'ZMW' => __( 'Zambian Kwacha', 'invoicing' ), |
|
| 331 | + 'USD' => __('US Dollar', 'invoicing'), |
|
| 332 | + 'EUR' => __('Euro', 'invoicing'), |
|
| 333 | + 'GBP' => __('Pound Sterling', 'invoicing'), |
|
| 334 | + 'AED' => __('United Arab Emirates', 'invoicing'), |
|
| 335 | + 'AFN' => __('Afghan Afghani', 'invoicing'), |
|
| 336 | + 'ALL' => __('Albanian Lek', 'invoicing'), |
|
| 337 | + 'AMD' => __('Armenian Dram', 'invoicing'), |
|
| 338 | + 'ANG' => __('Netherlands Antillean Guilder', 'invoicing'), |
|
| 339 | + 'AOA' => __('Angolan Kwanza', 'invoicing'), |
|
| 340 | + 'ARS' => __('Argentine Peso', 'invoicing'), |
|
| 341 | + 'AUD' => __('Australian Dollar', 'invoicing'), |
|
| 342 | + 'AWG' => __('Aruban Florin', 'invoicing'), |
|
| 343 | + 'AZN' => __('Azerbaijani Manat', 'invoicing'), |
|
| 344 | + 'BAM' => __('Bosnia and Herzegovina Convertible Marka', 'invoicing'), |
|
| 345 | + 'BBD' => __('Barbadian Dollar', 'invoicing'), |
|
| 346 | + 'BDT' => __('Bangladeshi Taka', 'invoicing'), |
|
| 347 | + 'BGN' => __('Bulgarian Lev', 'invoicing'), |
|
| 348 | + 'BHD' => __('Bahraini Dinar', 'invoicing'), |
|
| 349 | + 'BIF' => __('Burundian Franc', 'invoicing'), |
|
| 350 | + 'BMD' => __('Bermudian Dollar', 'invoicing'), |
|
| 351 | + 'BND' => __('Brunei Dollar', 'invoicing'), |
|
| 352 | + 'BOB' => __('Bolivian Boliviano', 'invoicing'), |
|
| 353 | + 'BRL' => __('Brazilian Real', 'invoicing'), |
|
| 354 | + 'BSD' => __('Bahamian Dollar', 'invoicing'), |
|
| 355 | + 'BTC' => __('Bitcoin', 'invoicing'), |
|
| 356 | + 'BTN' => __('Bhutanese Ngultrum', 'invoicing'), |
|
| 357 | + 'BWP' => __('Botswana Pula', 'invoicing'), |
|
| 358 | + 'BYN' => __('Belarusian Ruble', 'invoicing'), |
|
| 359 | + 'BZD' => __('Belize Dollar', 'invoicing'), |
|
| 360 | + 'CAD' => __('Canadian Dollar', 'invoicing'), |
|
| 361 | + 'CDF' => __('Congolese Franc', 'invoicing'), |
|
| 362 | + 'CHF' => __('Swiss Franc', 'invoicing'), |
|
| 363 | + 'CLP' => __('Chilean Peso', 'invoicing'), |
|
| 364 | + 'CNY' => __('Chinese Yuan', 'invoicing'), |
|
| 365 | + 'COP' => __('Colombian Peso', 'invoicing'), |
|
| 366 | + 'CRC' => __('Costa Rican Colon', 'invoicing'), |
|
| 367 | + 'CUC' => __('Cuban Convertible Peso', 'invoicing'), |
|
| 368 | + 'CUP' => __('Cuban Peso', 'invoicing'), |
|
| 369 | + 'CVE' => __('Cape Verdean escudo', 'invoicing'), |
|
| 370 | + 'CZK' => __('Czech Koruna', 'invoicing'), |
|
| 371 | + 'DJF' => __('Djiboutian Franc', 'invoicing'), |
|
| 372 | + 'DKK' => __('Danish Krone', 'invoicing'), |
|
| 373 | + 'DOP' => __('Dominican Peso', 'invoicing'), |
|
| 374 | + 'DZD' => __('Algerian Dinar', 'invoicing'), |
|
| 375 | + 'EGP' => __('Egyptian Pound', 'invoicing'), |
|
| 376 | + 'ERN' => __('Eritrean Nakfa', 'invoicing'), |
|
| 377 | + 'ETB' => __('Ethiopian Irr', 'invoicing'), |
|
| 378 | + 'FJD' => __('Fijian Dollar', 'invoicing'), |
|
| 379 | + 'FKP' => __('Falkland Islands Pound', 'invoicing'), |
|
| 380 | + 'GEL' => __('Georgian Lari', 'invoicing'), |
|
| 381 | + 'GGP' => __('Guernsey Pound', 'invoicing'), |
|
| 382 | + 'GHS' => __('Ghana Cedi', 'invoicing'), |
|
| 383 | + 'GIP' => __('Gibraltar Pound', 'invoicing'), |
|
| 384 | + 'GMD' => __('Gambian Dalasi', 'invoicing'), |
|
| 385 | + 'GNF' => __('Guinean Franc', 'invoicing'), |
|
| 386 | + 'GTQ' => __('Guatemalan Quetzal', 'invoicing'), |
|
| 387 | + 'GYD' => __('Guyanese Dollar', 'invoicing'), |
|
| 388 | + 'HKD' => __('Hong Kong Dollar', 'invoicing'), |
|
| 389 | + 'HNL' => __('Honduran Lempira', 'invoicing'), |
|
| 390 | + 'HRK' => __('Croatian Kuna', 'invoicing'), |
|
| 391 | + 'HTG' => __('Haitian Gourde', 'invoicing'), |
|
| 392 | + 'HUF' => __('Hungarian Forint', 'invoicing'), |
|
| 393 | + 'IDR' => __('Indonesian Rupiah', 'invoicing'), |
|
| 394 | + 'ILS' => __('Israeli New Shekel', 'invoicing'), |
|
| 395 | + 'IMP' => __('Manx Pound', 'invoicing'), |
|
| 396 | + 'INR' => __('Indian Rupee', 'invoicing'), |
|
| 397 | + 'IQD' => __('Iraqi Dinar', 'invoicing'), |
|
| 398 | + 'IRR' => __('Iranian Rial', 'invoicing'), |
|
| 399 | + 'IRT' => __('Iranian Toman', 'invoicing'), |
|
| 400 | + 'ISK' => __('Icelandic Krona', 'invoicing'), |
|
| 401 | + 'JEP' => __('Jersey Pound', 'invoicing'), |
|
| 402 | + 'JMD' => __('Jamaican Dollar', 'invoicing'), |
|
| 403 | + 'JOD' => __('Jordanian Dinar', 'invoicing'), |
|
| 404 | + 'JPY' => __('Japanese Yen', 'invoicing'), |
|
| 405 | + 'KES' => __('Kenyan Shilling', 'invoicing'), |
|
| 406 | + 'KGS' => __('Kyrgyzstani Som', 'invoicing'), |
|
| 407 | + 'KHR' => __('Cambodian Riel', 'invoicing'), |
|
| 408 | + 'KMF' => __('Comorian Franc', 'invoicing'), |
|
| 409 | + 'KPW' => __('North Korean Won', 'invoicing'), |
|
| 410 | + 'KRW' => __('South Korean Won', 'invoicing'), |
|
| 411 | + 'KWD' => __('Kuwaiti Dinar', 'invoicing'), |
|
| 412 | + 'KYD' => __('Cayman Islands Dollar', 'invoicing'), |
|
| 413 | + 'KZT' => __('Kazakhstani Tenge', 'invoicing'), |
|
| 414 | + 'LAK' => __('Lao Kip', 'invoicing'), |
|
| 415 | + 'LBP' => __('Lebanese Pound', 'invoicing'), |
|
| 416 | + 'LKR' => __('Sri Lankan Rupee', 'invoicing'), |
|
| 417 | + 'LRD' => __('Liberian Dollar', 'invoicing'), |
|
| 418 | + 'LSL' => __('Lesotho Loti', 'invoicing'), |
|
| 419 | + 'LYD' => __('Libyan Dinar', 'invoicing'), |
|
| 420 | + 'MAD' => __('Moroccan Dirham', 'invoicing'), |
|
| 421 | + 'MDL' => __('Moldovan Leu', 'invoicing'), |
|
| 422 | + 'MGA' => __('Malagasy Ariary', 'invoicing'), |
|
| 423 | + 'MKD' => __('Macedonian Denar', 'invoicing'), |
|
| 424 | + 'MMK' => __('Burmese Kyat', 'invoicing'), |
|
| 425 | + 'MNT' => __('Mongolian Tughrik', 'invoicing'), |
|
| 426 | + 'MOP' => __('Macanese Pataca', 'invoicing'), |
|
| 427 | + 'MRO' => __('Mauritanian Ouguiya', 'invoicing'), |
|
| 428 | + 'MUR' => __('Mauritian Rupee', 'invoicing'), |
|
| 429 | + 'MVR' => __('Maldivian Rufiyaa', 'invoicing'), |
|
| 430 | + 'MWK' => __('Malawian Kwacha', 'invoicing'), |
|
| 431 | + 'MXN' => __('Mexican Peso', 'invoicing'), |
|
| 432 | + 'MYR' => __('Malaysian Ringgit', 'invoicing'), |
|
| 433 | + 'MZN' => __('Mozambican Metical', 'invoicing'), |
|
| 434 | + 'NAD' => __('Namibian Dollar', 'invoicing'), |
|
| 435 | + 'NGN' => __('Nigerian Naira', 'invoicing'), |
|
| 436 | + 'NIO' => __('Nicaraguan Cordoba', 'invoicing'), |
|
| 437 | + 'NOK' => __('Norwegian Krone', 'invoicing'), |
|
| 438 | + 'NPR' => __('Nepalese Rupee', 'invoicing'), |
|
| 439 | + 'NZD' => __('New Zealand Dollar', 'invoicing'), |
|
| 440 | + 'OMR' => __('Omani Rial', 'invoicing'), |
|
| 441 | + 'PAB' => __('Panamanian Balboa', 'invoicing'), |
|
| 442 | + 'PEN' => __('Peruvian Nuevo Sol', 'invoicing'), |
|
| 443 | + 'PGK' => __('Papua New Guinean Kina', 'invoicing'), |
|
| 444 | + 'PHP' => __('Philippine Peso', 'invoicing'), |
|
| 445 | + 'PKR' => __('Pakistani Rupee', 'invoicing'), |
|
| 446 | + 'PLN' => __('Polish Zloty', 'invoicing'), |
|
| 447 | + 'PRB' => __('Transnistrian Ruble', 'invoicing'), |
|
| 448 | + 'PYG' => __('Paraguayan Guarani', 'invoicing'), |
|
| 449 | + 'QAR' => __('Qatari Riyal', 'invoicing'), |
|
| 450 | + 'RON' => __('Romanian Leu', 'invoicing'), |
|
| 451 | + 'RSD' => __('Serbian Dinar', 'invoicing'), |
|
| 452 | + 'RUB' => __('Russian Ruble', 'invoicing'), |
|
| 453 | + 'RWF' => __('Rwandan Franc', 'invoicing'), |
|
| 454 | + 'SAR' => __('Saudi Riyal', 'invoicing'), |
|
| 455 | + 'SBD' => __('Solomon Islands Dollar', 'invoicing'), |
|
| 456 | + 'SCR' => __('Seychellois Rupee', 'invoicing'), |
|
| 457 | + 'SDG' => __('Sudanese Pound', 'invoicing'), |
|
| 458 | + 'SEK' => __('Swedish Krona', 'invoicing'), |
|
| 459 | + 'SGD' => __('Singapore Dollar', 'invoicing'), |
|
| 460 | + 'SHP' => __('Saint Helena Pound', 'invoicing'), |
|
| 461 | + 'SLL' => __('Sierra Leonean Leone', 'invoicing'), |
|
| 462 | + 'SOS' => __('Somali Shilling', 'invoicing'), |
|
| 463 | + 'SRD' => __('Surinamese Dollar', 'invoicing'), |
|
| 464 | + 'SSP' => __('South Sudanese Pound', 'invoicing'), |
|
| 465 | + 'STD' => __('Sao Tomean Dobra', 'invoicing'), |
|
| 466 | + 'SYP' => __('Syrian Pound', 'invoicing'), |
|
| 467 | + 'SZL' => __('Swazi Lilangeni', 'invoicing'), |
|
| 468 | + 'THB' => __('Thai Baht', 'invoicing'), |
|
| 469 | + 'TJS' => __('Tajikistani Somoni', 'invoicing'), |
|
| 470 | + 'TMT' => __('Turkmenistan Manat', 'invoicing'), |
|
| 471 | + 'TND' => __('Tunisian Dinar', 'invoicing'), |
|
| 472 | + 'TOP' => __('Tongan Paʻanga', 'invoicing'), |
|
| 473 | + 'TRY' => __('Turkish Lira', 'invoicing'), |
|
| 474 | + 'TTD' => __('Trinidad and Tobago Dollar', 'invoicing'), |
|
| 475 | + 'TWD' => __('New Taiwan Dollar', 'invoicing'), |
|
| 476 | + 'TZS' => __('Tanzanian Shilling', 'invoicing'), |
|
| 477 | + 'UAH' => __('Ukrainian Hryvnia', 'invoicing'), |
|
| 478 | + 'UGX' => __('Ugandan Shilling', 'invoicing'), |
|
| 479 | + 'UYU' => __('Uruguayan Peso', 'invoicing'), |
|
| 480 | + 'UZS' => __('Uzbekistani Som', 'invoicing'), |
|
| 481 | + 'VEF' => __('Venezuelan Bolívar', 'invoicing'), |
|
| 482 | + 'VND' => __('Vietnamese Dong', 'invoicing'), |
|
| 483 | + 'VUV' => __('Vanuatu Vatu', 'invoicing'), |
|
| 484 | + 'WST' => __('Samoan Tala', 'invoicing'), |
|
| 485 | + 'XAF' => __('Central African CFA Franc', 'invoicing'), |
|
| 486 | + 'XCD' => __('East Caribbean Dollar', 'invoicing'), |
|
| 487 | + 'XOF' => __('West African CFA Franc', 'invoicing'), |
|
| 488 | + 'XPF' => __('CFP Franc', 'invoicing'), |
|
| 489 | + 'YER' => __('Yemeni Rial', 'invoicing'), |
|
| 490 | + 'ZAR' => __('South African Rand', 'invoicing'), |
|
| 491 | + 'ZMW' => __('Zambian Kwacha', 'invoicing'), |
|
| 492 | 492 | ); |
| 493 | 493 | |
| 494 | 494 | //asort( $currencies ); // this |
| 495 | 495 | |
| 496 | - return apply_filters( 'wpinv_currencies', $currencies ); |
|
| 496 | + return apply_filters('wpinv_currencies', $currencies); |
|
| 497 | 497 | } |
| 498 | 498 | |
| 499 | -function wpinv_price( $amount = '', $currency = '' ) { |
|
| 500 | - if( empty( $currency ) ) { |
|
| 499 | +function wpinv_price($amount = '', $currency = '') { |
|
| 500 | + if (empty($currency)) { |
|
| 501 | 501 | $currency = wpinv_get_currency(); |
| 502 | 502 | } |
| 503 | 503 | |
@@ -505,14 +505,14 @@ discard block |
||
| 505 | 505 | |
| 506 | 506 | $negative = $amount < 0; |
| 507 | 507 | |
| 508 | - if ( $negative ) { |
|
| 509 | - $amount = substr( $amount, 1 ); |
|
| 508 | + if ($negative) { |
|
| 509 | + $amount = substr($amount, 1); |
|
| 510 | 510 | } |
| 511 | 511 | |
| 512 | - $symbol = wpinv_currency_symbol( $currency ); |
|
| 512 | + $symbol = wpinv_currency_symbol($currency); |
|
| 513 | 513 | |
| 514 | - if ( $position == 'left' || $position == 'left_space' ) { |
|
| 515 | - switch ( $currency ) { |
|
| 514 | + if ($position == 'left' || $position == 'left_space') { |
|
| 515 | + switch ($currency) { |
|
| 516 | 516 | case "GBP" : |
| 517 | 517 | case "BRL" : |
| 518 | 518 | case "EUR" : |
@@ -524,15 +524,15 @@ discard block |
||
| 524 | 524 | case "NZD" : |
| 525 | 525 | case "SGD" : |
| 526 | 526 | case "JPY" : |
| 527 | - $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 527 | + $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 528 | 528 | break; |
| 529 | 529 | default : |
| 530 | 530 | //$price = $currency . ' ' . $amount; |
| 531 | - $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 531 | + $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 532 | 532 | break; |
| 533 | 533 | } |
| 534 | 534 | } else { |
| 535 | - switch ( $currency ) { |
|
| 535 | + switch ($currency) { |
|
| 536 | 536 | case "GBP" : |
| 537 | 537 | case "BRL" : |
| 538 | 538 | case "EUR" : |
@@ -543,83 +543,83 @@ discard block |
||
| 543 | 543 | case "MXN" : |
| 544 | 544 | case "SGD" : |
| 545 | 545 | case "JPY" : |
| 546 | - $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 546 | + $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 547 | 547 | break; |
| 548 | 548 | default : |
| 549 | 549 | //$price = $amount . ' ' . $currency; |
| 550 | - $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 550 | + $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 551 | 551 | break; |
| 552 | 552 | } |
| 553 | 553 | } |
| 554 | 554 | |
| 555 | - if ( $negative ) { |
|
| 555 | + if ($negative) { |
|
| 556 | 556 | $price = '-' . $price; |
| 557 | 557 | } |
| 558 | 558 | |
| 559 | - $price = apply_filters( 'wpinv_' . strtolower( $currency ) . '_currency_filter_' . $position, $price, $currency, $amount ); |
|
| 559 | + $price = apply_filters('wpinv_' . strtolower($currency) . '_currency_filter_' . $position, $price, $currency, $amount); |
|
| 560 | 560 | |
| 561 | 561 | return $price; |
| 562 | 562 | } |
| 563 | 563 | |
| 564 | -function wpinv_format_amount( $amount, $decimals = NULL, $calculate = false ) { |
|
| 564 | +function wpinv_format_amount($amount, $decimals = NULL, $calculate = false) { |
|
| 565 | 565 | $thousands_sep = wpinv_thousands_separator(); |
| 566 | 566 | $decimal_sep = wpinv_decimal_separator(); |
| 567 | 567 | |
| 568 | - if ( $decimals === NULL ) { |
|
| 568 | + if ($decimals === NULL) { |
|
| 569 | 569 | $decimals = wpinv_decimals(); |
| 570 | 570 | } |
| 571 | 571 | |
| 572 | - if ( $decimal_sep == ',' && false !== ( $sep_found = strpos( $amount, $decimal_sep ) ) ) { |
|
| 573 | - $whole = substr( $amount, 0, $sep_found ); |
|
| 574 | - $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) ); |
|
| 572 | + if ($decimal_sep == ',' && false !== ($sep_found = strpos($amount, $decimal_sep))) { |
|
| 573 | + $whole = substr($amount, 0, $sep_found); |
|
| 574 | + $part = substr($amount, $sep_found + 1, (strlen($amount) - 1)); |
|
| 575 | 575 | $amount = $whole . '.' . $part; |
| 576 | 576 | } |
| 577 | 577 | |
| 578 | - if ( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 579 | - $amount = str_replace( ',', '', $amount ); |
|
| 578 | + if ($thousands_sep == ',' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 579 | + $amount = str_replace(',', '', $amount); |
|
| 580 | 580 | } |
| 581 | 581 | |
| 582 | - if ( $thousands_sep == ' ' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 583 | - $amount = str_replace( ' ', '', $amount ); |
|
| 582 | + if ($thousands_sep == ' ' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 583 | + $amount = str_replace(' ', '', $amount); |
|
| 584 | 584 | } |
| 585 | 585 | |
| 586 | - if ( empty( $amount ) ) { |
|
| 586 | + if (empty($amount)) { |
|
| 587 | 587 | $amount = 0; |
| 588 | 588 | } |
| 589 | 589 | |
| 590 | - $decimals = apply_filters( 'wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate ); |
|
| 591 | - $formatted = number_format( (float)$amount, $decimals, $decimal_sep, $thousands_sep ); |
|
| 590 | + $decimals = apply_filters('wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate); |
|
| 591 | + $formatted = number_format((float)$amount, $decimals, $decimal_sep, $thousands_sep); |
|
| 592 | 592 | |
| 593 | - if ( $calculate ) { |
|
| 594 | - if ( $thousands_sep === "," ) { |
|
| 595 | - $formatted = str_replace( ",", "", $formatted ); |
|
| 593 | + if ($calculate) { |
|
| 594 | + if ($thousands_sep === ",") { |
|
| 595 | + $formatted = str_replace(",", "", $formatted); |
|
| 596 | 596 | } |
| 597 | 597 | |
| 598 | - if ( $decimal_sep === "," ) { |
|
| 599 | - $formatted = str_replace( ",", ".", $formatted ); |
|
| 598 | + if ($decimal_sep === ",") { |
|
| 599 | + $formatted = str_replace(",", ".", $formatted); |
|
| 600 | 600 | } |
| 601 | 601 | } |
| 602 | 602 | |
| 603 | - return apply_filters( 'wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate ); |
|
| 603 | + return apply_filters('wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate); |
|
| 604 | 604 | } |
| 605 | -add_filter( 'wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1 ); |
|
| 605 | +add_filter('wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1); |
|
| 606 | 606 | |
| 607 | -function wpinv_sanitize_key( $key ) { |
|
| 607 | +function wpinv_sanitize_key($key) { |
|
| 608 | 608 | $raw_key = $key; |
| 609 | - $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key ); |
|
| 609 | + $key = preg_replace('/[^a-zA-Z0-9_\-\.\:\/]/', '', $key); |
|
| 610 | 610 | |
| 611 | - return apply_filters( 'wpinv_sanitize_key', $key, $raw_key ); |
|
| 611 | + return apply_filters('wpinv_sanitize_key', $key, $raw_key); |
|
| 612 | 612 | } |
| 613 | 613 | |
| 614 | -function wpinv_get_file_extension( $str ) { |
|
| 615 | - $parts = explode( '.', $str ); |
|
| 616 | - return end( $parts ); |
|
| 614 | +function wpinv_get_file_extension($str) { |
|
| 615 | + $parts = explode('.', $str); |
|
| 616 | + return end($parts); |
|
| 617 | 617 | } |
| 618 | 618 | |
| 619 | -function wpinv_string_is_image_url( $str ) { |
|
| 620 | - $ext = wpinv_get_file_extension( $str ); |
|
| 619 | +function wpinv_string_is_image_url($str) { |
|
| 620 | + $ext = wpinv_get_file_extension($str); |
|
| 621 | 621 | |
| 622 | - switch ( strtolower( $ext ) ) { |
|
| 622 | + switch (strtolower($ext)) { |
|
| 623 | 623 | case 'jpeg'; |
| 624 | 624 | case 'jpg'; |
| 625 | 625 | $return = true; |
@@ -635,32 +635,32 @@ discard block |
||
| 635 | 635 | break; |
| 636 | 636 | } |
| 637 | 637 | |
| 638 | - return (bool)apply_filters( 'wpinv_string_is_image', $return, $str ); |
|
| 638 | + return (bool)apply_filters('wpinv_string_is_image', $return, $str); |
|
| 639 | 639 | } |
| 640 | 640 | |
| 641 | -function wpinv_error_log( $log, $title = '', $file = '', $line = '', $exit = false ) { |
|
| 642 | - $should_log = apply_filters( 'wpinv_log_errors', WP_DEBUG ); |
|
| 641 | +function wpinv_error_log($log, $title = '', $file = '', $line = '', $exit = false) { |
|
| 642 | + $should_log = apply_filters('wpinv_log_errors', WP_DEBUG); |
|
| 643 | 643 | |
| 644 | - if ( true === $should_log ) { |
|
| 644 | + if (true === $should_log) { |
|
| 645 | 645 | $label = ''; |
| 646 | - if ( $file && $file !== '' ) { |
|
| 647 | - $label .= basename( $file ) . ( $line ? '(' . $line . ')' : '' ); |
|
| 646 | + if ($file && $file !== '') { |
|
| 647 | + $label .= basename($file) . ($line ? '(' . $line . ')' : ''); |
|
| 648 | 648 | } |
| 649 | 649 | |
| 650 | - if ( $title && $title !== '' ) { |
|
| 650 | + if ($title && $title !== '') { |
|
| 651 | 651 | $label = $label !== '' ? $label . ' ' : ''; |
| 652 | 652 | $label .= $title . ' '; |
| 653 | 653 | } |
| 654 | 654 | |
| 655 | - $label = $label !== '' ? trim( $label ) . ' : ' : ''; |
|
| 655 | + $label = $label !== '' ? trim($label) . ' : ' : ''; |
|
| 656 | 656 | |
| 657 | - if ( is_array( $log ) || is_object( $log ) ) { |
|
| 658 | - error_log( $label . print_r( $log, true ) ); |
|
| 657 | + if (is_array($log) || is_object($log)) { |
|
| 658 | + error_log($label . print_r($log, true)); |
|
| 659 | 659 | } else { |
| 660 | - error_log( $label . $log ); |
|
| 660 | + error_log($label . $log); |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | - if ( $exit ) { |
|
| 663 | + if ($exit) { |
|
| 664 | 664 | exit; |
| 665 | 665 | } |
| 666 | 666 | } |
@@ -668,65 +668,65 @@ discard block |
||
| 668 | 668 | |
| 669 | 669 | function wpinv_is_ajax_disabled() { |
| 670 | 670 | $retval = false; |
| 671 | - return apply_filters( 'wpinv_is_ajax_disabled', $retval ); |
|
| 671 | + return apply_filters('wpinv_is_ajax_disabled', $retval); |
|
| 672 | 672 | } |
| 673 | 673 | |
| 674 | -function wpinv_get_current_page_url( $nocache = false ) { |
|
| 674 | +function wpinv_get_current_page_url($nocache = false) { |
|
| 675 | 675 | global $wp; |
| 676 | 676 | |
| 677 | - if ( get_option( 'permalink_structure' ) ) { |
|
| 678 | - $base = trailingslashit( home_url( $wp->request ) ); |
|
| 677 | + if (get_option('permalink_structure')) { |
|
| 678 | + $base = trailingslashit(home_url($wp->request)); |
|
| 679 | 679 | } else { |
| 680 | - $base = add_query_arg( $wp->query_string, '', trailingslashit( home_url( $wp->request ) ) ); |
|
| 681 | - $base = remove_query_arg( array( 'post_type', 'name' ), $base ); |
|
| 680 | + $base = add_query_arg($wp->query_string, '', trailingslashit(home_url($wp->request))); |
|
| 681 | + $base = remove_query_arg(array('post_type', 'name'), $base); |
|
| 682 | 682 | } |
| 683 | 683 | |
| 684 | 684 | $scheme = is_ssl() ? 'https' : 'http'; |
| 685 | - $uri = set_url_scheme( $base, $scheme ); |
|
| 685 | + $uri = set_url_scheme($base, $scheme); |
|
| 686 | 686 | |
| 687 | - if ( is_front_page() ) { |
|
| 688 | - $uri = home_url( '/' ); |
|
| 689 | - } elseif ( wpinv_is_checkout( array(), false ) ) { |
|
| 687 | + if (is_front_page()) { |
|
| 688 | + $uri = home_url('/'); |
|
| 689 | + } elseif (wpinv_is_checkout(array(), false)) { |
|
| 690 | 690 | $uri = wpinv_get_checkout_uri(); |
| 691 | 691 | } |
| 692 | 692 | |
| 693 | - $uri = apply_filters( 'wpinv_get_current_page_url', $uri ); |
|
| 693 | + $uri = apply_filters('wpinv_get_current_page_url', $uri); |
|
| 694 | 694 | |
| 695 | - if ( $nocache ) { |
|
| 696 | - $uri = wpinv_add_cache_busting( $uri ); |
|
| 695 | + if ($nocache) { |
|
| 696 | + $uri = wpinv_add_cache_busting($uri); |
|
| 697 | 697 | } |
| 698 | 698 | |
| 699 | 699 | return $uri; |
| 700 | 700 | } |
| 701 | 701 | |
| 702 | 702 | function wpinv_get_php_arg_separator_output() { |
| 703 | - return ini_get( 'arg_separator.output' ); |
|
| 703 | + return ini_get('arg_separator.output'); |
|
| 704 | 704 | } |
| 705 | 705 | |
| 706 | -function wpinv_rgb_from_hex( $color ) { |
|
| 707 | - $color = str_replace( '#', '', $color ); |
|
| 706 | +function wpinv_rgb_from_hex($color) { |
|
| 707 | + $color = str_replace('#', '', $color); |
|
| 708 | 708 | // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF" |
| 709 | - $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color ); |
|
| 709 | + $color = preg_replace('~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color); |
|
| 710 | 710 | |
| 711 | 711 | $rgb = array(); |
| 712 | - $rgb['R'] = hexdec( $color{0}.$color{1} ); |
|
| 713 | - $rgb['G'] = hexdec( $color{2}.$color{3} ); |
|
| 714 | - $rgb['B'] = hexdec( $color{4}.$color{5} ); |
|
| 712 | + $rgb['R'] = hexdec($color{0} . $color{1} ); |
|
| 713 | + $rgb['G'] = hexdec($color{2} . $color{3} ); |
|
| 714 | + $rgb['B'] = hexdec($color{4} . $color{5} ); |
|
| 715 | 715 | |
| 716 | 716 | return $rgb; |
| 717 | 717 | } |
| 718 | 718 | |
| 719 | -function wpinv_hex_darker( $color, $factor = 30 ) { |
|
| 720 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 719 | +function wpinv_hex_darker($color, $factor = 30) { |
|
| 720 | + $base = wpinv_rgb_from_hex($color); |
|
| 721 | 721 | $color = '#'; |
| 722 | 722 | |
| 723 | - foreach ( $base as $k => $v ) { |
|
| 723 | + foreach ($base as $k => $v) { |
|
| 724 | 724 | $amount = $v / 100; |
| 725 | - $amount = round( $amount * $factor ); |
|
| 725 | + $amount = round($amount * $factor); |
|
| 726 | 726 | $new_decimal = $v - $amount; |
| 727 | 727 | |
| 728 | - $new_hex_component = dechex( $new_decimal ); |
|
| 729 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 728 | + $new_hex_component = dechex($new_decimal); |
|
| 729 | + if (strlen($new_hex_component) < 2) { |
|
| 730 | 730 | $new_hex_component = "0" . $new_hex_component; |
| 731 | 731 | } |
| 732 | 732 | $color .= $new_hex_component; |
@@ -735,18 +735,18 @@ discard block |
||
| 735 | 735 | return $color; |
| 736 | 736 | } |
| 737 | 737 | |
| 738 | -function wpinv_hex_lighter( $color, $factor = 30 ) { |
|
| 739 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 738 | +function wpinv_hex_lighter($color, $factor = 30) { |
|
| 739 | + $base = wpinv_rgb_from_hex($color); |
|
| 740 | 740 | $color = '#'; |
| 741 | 741 | |
| 742 | - foreach ( $base as $k => $v ) { |
|
| 742 | + foreach ($base as $k => $v) { |
|
| 743 | 743 | $amount = 255 - $v; |
| 744 | 744 | $amount = $amount / 100; |
| 745 | - $amount = round( $amount * $factor ); |
|
| 745 | + $amount = round($amount * $factor); |
|
| 746 | 746 | $new_decimal = $v + $amount; |
| 747 | 747 | |
| 748 | - $new_hex_component = dechex( $new_decimal ); |
|
| 749 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 748 | + $new_hex_component = dechex($new_decimal); |
|
| 749 | + if (strlen($new_hex_component) < 2) { |
|
| 750 | 750 | $new_hex_component = "0" . $new_hex_component; |
| 751 | 751 | } |
| 752 | 752 | $color .= $new_hex_component; |
@@ -755,22 +755,22 @@ discard block |
||
| 755 | 755 | return $color; |
| 756 | 756 | } |
| 757 | 757 | |
| 758 | -function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
|
| 759 | - $hex = str_replace( '#', '', $color ); |
|
| 758 | +function wpinv_light_or_dark($color, $dark = '#000000', $light = '#FFFFFF') { |
|
| 759 | + $hex = str_replace('#', '', $color); |
|
| 760 | 760 | |
| 761 | - $c_r = hexdec( substr( $hex, 0, 2 ) ); |
|
| 762 | - $c_g = hexdec( substr( $hex, 2, 2 ) ); |
|
| 763 | - $c_b = hexdec( substr( $hex, 4, 2 ) ); |
|
| 761 | + $c_r = hexdec(substr($hex, 0, 2)); |
|
| 762 | + $c_g = hexdec(substr($hex, 2, 2)); |
|
| 763 | + $c_b = hexdec(substr($hex, 4, 2)); |
|
| 764 | 764 | |
| 765 | - $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
|
| 765 | + $brightness = (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000; |
|
| 766 | 766 | |
| 767 | 767 | return $brightness > 155 ? $dark : $light; |
| 768 | 768 | } |
| 769 | 769 | |
| 770 | -function wpinv_format_hex( $hex ) { |
|
| 771 | - $hex = trim( str_replace( '#', '', $hex ) ); |
|
| 770 | +function wpinv_format_hex($hex) { |
|
| 771 | + $hex = trim(str_replace('#', '', $hex)); |
|
| 772 | 772 | |
| 773 | - if ( strlen( $hex ) == 3 ) { |
|
| 773 | + if (strlen($hex) == 3) { |
|
| 774 | 774 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
| 775 | 775 | } |
| 776 | 776 | |
@@ -790,12 +790,12 @@ discard block |
||
| 790 | 790 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 791 | 791 | * @return string |
| 792 | 792 | */ |
| 793 | -function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) { |
|
| 794 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 795 | - return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding ); |
|
| 793 | +function wpinv_utf8_strimwidth($str, $start, $width, $trimmaker = '', $encoding = 'UTF-8') { |
|
| 794 | + if (function_exists('mb_strimwidth')) { |
|
| 795 | + return mb_strimwidth($str, $start, $width, $trimmaker, $encoding); |
|
| 796 | 796 | } |
| 797 | 797 | |
| 798 | - return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker; |
|
| 798 | + return wpinv_utf8_substr($str, $start, $width, $encoding) . $trimmaker; |
|
| 799 | 799 | } |
| 800 | 800 | |
| 801 | 801 | /** |
@@ -807,28 +807,28 @@ discard block |
||
| 807 | 807 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 808 | 808 | * @return int Returns the number of characters in string. |
| 809 | 809 | */ |
| 810 | -function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) { |
|
| 811 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 812 | - return mb_strlen( $str, $encoding ); |
|
| 810 | +function wpinv_utf8_strlen($str, $encoding = 'UTF-8') { |
|
| 811 | + if (function_exists('mb_strlen')) { |
|
| 812 | + return mb_strlen($str, $encoding); |
|
| 813 | 813 | } |
| 814 | 814 | |
| 815 | - return strlen( $str ); |
|
| 815 | + return strlen($str); |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | -function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) { |
|
| 819 | - if ( function_exists( 'mb_strtolower' ) ) { |
|
| 820 | - return mb_strtolower( $str, $encoding ); |
|
| 818 | +function wpinv_utf8_strtolower($str, $encoding = 'UTF-8') { |
|
| 819 | + if (function_exists('mb_strtolower')) { |
|
| 820 | + return mb_strtolower($str, $encoding); |
|
| 821 | 821 | } |
| 822 | 822 | |
| 823 | - return strtolower( $str ); |
|
| 823 | + return strtolower($str); |
|
| 824 | 824 | } |
| 825 | 825 | |
| 826 | -function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) { |
|
| 827 | - if ( function_exists( 'mb_strtoupper' ) ) { |
|
| 828 | - return mb_strtoupper( $str, $encoding ); |
|
| 826 | +function wpinv_utf8_strtoupper($str, $encoding = 'UTF-8') { |
|
| 827 | + if (function_exists('mb_strtoupper')) { |
|
| 828 | + return mb_strtoupper($str, $encoding); |
|
| 829 | 829 | } |
| 830 | 830 | |
| 831 | - return strtoupper( $str ); |
|
| 831 | + return strtoupper($str); |
|
| 832 | 832 | } |
| 833 | 833 | |
| 834 | 834 | /** |
@@ -842,12 +842,12 @@ discard block |
||
| 842 | 842 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 843 | 843 | * @return int Returns the position of the first occurrence of search in the string. |
| 844 | 844 | */ |
| 845 | -function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 846 | - if ( function_exists( 'mb_strpos' ) ) { |
|
| 847 | - return mb_strpos( $str, $find, $offset, $encoding ); |
|
| 845 | +function wpinv_utf8_strpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 846 | + if (function_exists('mb_strpos')) { |
|
| 847 | + return mb_strpos($str, $find, $offset, $encoding); |
|
| 848 | 848 | } |
| 849 | 849 | |
| 850 | - return strpos( $str, $find, $offset ); |
|
| 850 | + return strpos($str, $find, $offset); |
|
| 851 | 851 | } |
| 852 | 852 | |
| 853 | 853 | /** |
@@ -861,12 +861,12 @@ discard block |
||
| 861 | 861 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 862 | 862 | * @return int Returns the position of the last occurrence of search. |
| 863 | 863 | */ |
| 864 | -function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 865 | - if ( function_exists( 'mb_strrpos' ) ) { |
|
| 866 | - return mb_strrpos( $str, $find, $offset, $encoding ); |
|
| 864 | +function wpinv_utf8_strrpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 865 | + if (function_exists('mb_strrpos')) { |
|
| 866 | + return mb_strrpos($str, $find, $offset, $encoding); |
|
| 867 | 867 | } |
| 868 | 868 | |
| 869 | - return strrpos( $str, $find, $offset ); |
|
| 869 | + return strrpos($str, $find, $offset); |
|
| 870 | 870 | } |
| 871 | 871 | |
| 872 | 872 | /** |
@@ -881,16 +881,16 @@ discard block |
||
| 881 | 881 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 882 | 882 | * @return string |
| 883 | 883 | */ |
| 884 | -function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) { |
|
| 885 | - if ( function_exists( 'mb_substr' ) ) { |
|
| 886 | - if ( $length === null ) { |
|
| 887 | - return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 884 | +function wpinv_utf8_substr($str, $start, $length = null, $encoding = 'UTF-8') { |
|
| 885 | + if (function_exists('mb_substr')) { |
|
| 886 | + if ($length === null) { |
|
| 887 | + return mb_substr($str, $start, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 888 | 888 | } else { |
| 889 | - return mb_substr( $str, $start, $length, $encoding ); |
|
| 889 | + return mb_substr($str, $start, $length, $encoding); |
|
| 890 | 890 | } |
| 891 | 891 | } |
| 892 | 892 | |
| 893 | - return substr( $str, $start, $length ); |
|
| 893 | + return substr($str, $start, $length); |
|
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | /** |
@@ -902,48 +902,48 @@ discard block |
||
| 902 | 902 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 903 | 903 | * @return string The width of string. |
| 904 | 904 | */ |
| 905 | -function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) { |
|
| 906 | - if ( function_exists( 'mb_strwidth' ) ) { |
|
| 907 | - return mb_strwidth( $str, $encoding ); |
|
| 905 | +function wpinv_utf8_strwidth($str, $encoding = 'UTF-8') { |
|
| 906 | + if (function_exists('mb_strwidth')) { |
|
| 907 | + return mb_strwidth($str, $encoding); |
|
| 908 | 908 | } |
| 909 | 909 | |
| 910 | - return wpinv_utf8_strlen( $str, $encoding ); |
|
| 910 | + return wpinv_utf8_strlen($str, $encoding); |
|
| 911 | 911 | } |
| 912 | 912 | |
| 913 | -function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) { |
|
| 914 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 915 | - $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding ); |
|
| 913 | +function wpinv_utf8_ucfirst($str, $lower_str_end = false, $encoding = 'UTF-8') { |
|
| 914 | + if (function_exists('mb_strlen')) { |
|
| 915 | + $first_letter = wpinv_utf8_strtoupper(wpinv_utf8_substr($str, 0, 1, $encoding), $encoding); |
|
| 916 | 916 | $str_end = ""; |
| 917 | 917 | |
| 918 | - if ( $lower_str_end ) { |
|
| 919 | - $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding ); |
|
| 918 | + if ($lower_str_end) { |
|
| 919 | + $str_end = wpinv_utf8_strtolower(wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding), $encoding); |
|
| 920 | 920 | } else { |
| 921 | - $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 921 | + $str_end = wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 922 | 922 | } |
| 923 | 923 | |
| 924 | 924 | return $first_letter . $str_end; |
| 925 | 925 | } |
| 926 | 926 | |
| 927 | - return ucfirst( $str ); |
|
| 927 | + return ucfirst($str); |
|
| 928 | 928 | } |
| 929 | 929 | |
| 930 | -function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) { |
|
| 931 | - if ( function_exists( 'mb_convert_case' ) ) { |
|
| 932 | - return mb_convert_case( $str, MB_CASE_TITLE, $encoding ); |
|
| 930 | +function wpinv_utf8_ucwords($str, $encoding = 'UTF-8') { |
|
| 931 | + if (function_exists('mb_convert_case')) { |
|
| 932 | + return mb_convert_case($str, MB_CASE_TITLE, $encoding); |
|
| 933 | 933 | } |
| 934 | 934 | |
| 935 | - return ucwords( $str ); |
|
| 935 | + return ucwords($str); |
|
| 936 | 936 | } |
| 937 | 937 | |
| 938 | -function wpinv_period_in_days( $period, $unit ) { |
|
| 939 | - $period = absint( $period ); |
|
| 938 | +function wpinv_period_in_days($period, $unit) { |
|
| 939 | + $period = absint($period); |
|
| 940 | 940 | |
| 941 | - if ( $period > 0 ) { |
|
| 942 | - if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) { |
|
| 941 | + if ($period > 0) { |
|
| 942 | + if (in_array(strtolower($unit), array('w', 'week', 'weeks'))) { |
|
| 943 | 943 | $period = $period * 7; |
| 944 | - } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) { |
|
| 944 | + } else if (in_array(strtolower($unit), array('m', 'month', 'months'))) { |
|
| 945 | 945 | $period = $period * 30; |
| 946 | - } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) { |
|
| 946 | + } else if (in_array(strtolower($unit), array('y', 'year', 'years'))) { |
|
| 947 | 947 | $period = $period * 365; |
| 948 | 948 | } |
| 949 | 949 | } |
@@ -951,14 +951,14 @@ discard block |
||
| 951 | 951 | return $period; |
| 952 | 952 | } |
| 953 | 953 | |
| 954 | -function wpinv_cal_days_in_month( $calendar, $month, $year ) { |
|
| 955 | - if ( function_exists( 'cal_days_in_month' ) ) { |
|
| 956 | - return cal_days_in_month( $calendar, $month, $year ); |
|
| 954 | +function wpinv_cal_days_in_month($calendar, $month, $year) { |
|
| 955 | + if (function_exists('cal_days_in_month')) { |
|
| 956 | + return cal_days_in_month($calendar, $month, $year); |
|
| 957 | 957 | } |
| 958 | 958 | |
| 959 | 959 | // Fallback in case the calendar extension is not loaded in PHP |
| 960 | 960 | // Only supports Gregorian calendar |
| 961 | - return date( 't', mktime( 0, 0, 0, $month, 1, $year ) ); |
|
| 961 | + return date('t', mktime(0, 0, 0, $month, 1, $year)); |
|
| 962 | 962 | } |
| 963 | 963 | |
| 964 | 964 | /** |
@@ -969,11 +969,11 @@ discard block |
||
| 969 | 969 | * |
| 970 | 970 | * @return string |
| 971 | 971 | */ |
| 972 | -function wpi_help_tip( $tip, $allow_html = false ) { |
|
| 973 | - if ( $allow_html ) { |
|
| 974 | - $tip = wpi_sanitize_tooltip( $tip ); |
|
| 972 | +function wpi_help_tip($tip, $allow_html = false) { |
|
| 973 | + if ($allow_html) { |
|
| 974 | + $tip = wpi_sanitize_tooltip($tip); |
|
| 975 | 975 | } else { |
| 976 | - $tip = esc_attr( $tip ); |
|
| 976 | + $tip = esc_attr($tip); |
|
| 977 | 977 | } |
| 978 | 978 | |
| 979 | 979 | return '<span class="wpi-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
@@ -987,8 +987,8 @@ discard block |
||
| 987 | 987 | * @param string $var |
| 988 | 988 | * @return string |
| 989 | 989 | */ |
| 990 | -function wpi_sanitize_tooltip( $var ) { |
|
| 991 | - return htmlspecialchars( wp_kses( html_entity_decode( $var ), array( |
|
| 990 | +function wpi_sanitize_tooltip($var) { |
|
| 991 | + return htmlspecialchars(wp_kses(html_entity_decode($var), array( |
|
| 992 | 992 | 'br' => array(), |
| 993 | 993 | 'em' => array(), |
| 994 | 994 | 'strong' => array(), |
@@ -998,7 +998,7 @@ discard block |
||
| 998 | 998 | 'li' => array(), |
| 999 | 999 | 'ol' => array(), |
| 1000 | 1000 | 'p' => array(), |
| 1001 | - ) ) ); |
|
| 1001 | + ))); |
|
| 1002 | 1002 | } |
| 1003 | 1003 | |
| 1004 | 1004 | /** |
@@ -1008,7 +1008,7 @@ discard block |
||
| 1008 | 1008 | */ |
| 1009 | 1009 | function wpinv_get_screen_ids() { |
| 1010 | 1010 | |
| 1011 | - $screen_id = sanitize_title( __( 'Invoicing', 'invoicing' ) ); |
|
| 1011 | + $screen_id = sanitize_title(__('Invoicing', 'invoicing')); |
|
| 1012 | 1012 | |
| 1013 | 1013 | $screen_ids = array( |
| 1014 | 1014 | 'toplevel_page_' . $screen_id, |
@@ -1026,7 +1026,7 @@ discard block |
||
| 1026 | 1026 | 'invoicing_page_wpi-addons', |
| 1027 | 1027 | ); |
| 1028 | 1028 | |
| 1029 | - return apply_filters( 'wpinv_screen_ids', $screen_ids ); |
|
| 1029 | + return apply_filters('wpinv_screen_ids', $screen_ids); |
|
| 1030 | 1030 | } |
| 1031 | 1031 | |
| 1032 | 1032 | /** |
@@ -1037,9 +1037,9 @@ discard block |
||
| 1037 | 1037 | * @param array|string $list List of values. |
| 1038 | 1038 | * @return array Sanitized array of values. |
| 1039 | 1039 | */ |
| 1040 | -function wpinv_parse_list( $list ) { |
|
| 1041 | - if ( ! is_array( $list ) ) { |
|
| 1042 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1040 | +function wpinv_parse_list($list) { |
|
| 1041 | + if (!is_array($list)) { |
|
| 1042 | + return preg_split('/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY); |
|
| 1043 | 1043 | } |
| 1044 | 1044 | |
| 1045 | 1045 | return $list; |