@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | * @package Invoicing |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -defined( 'ABSPATH' ) || exit; |
|
| 9 | +defined('ABSPATH') || exit; |
|
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Are we supporting item quantities? |
@@ -20,35 +20,35 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wpinv_get_ip() { |
| 22 | 22 | |
| 23 | - if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) { |
|
| 24 | - return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) ); |
|
| 23 | + if (isset($_SERVER['HTTP_X_REAL_IP'])) { |
|
| 24 | + return sanitize_text_field(wp_unslash($_SERVER['HTTP_X_REAL_IP'])); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
|
| 27 | + if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
| 28 | 28 | // Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2 |
| 29 | 29 | // Make sure we always only send through the first IP in the list which should always be the client IP. |
| 30 | - return (string) rest_is_ip_address( trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) ); |
|
| 30 | + return (string) rest_is_ip_address(trim(current(preg_split('/,/', sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR'])))))); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
|
| 34 | - return sanitize_text_field( wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ) ); |
|
| 33 | + if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
|
| 34 | + return sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP'])); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
|
| 38 | - return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
|
| 37 | + if (isset($_SERVER['REMOTE_ADDR'])) { |
|
| 38 | + return sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | return ''; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | function wpinv_get_user_agent() { |
| 45 | - if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
|
| 46 | - $user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); |
|
| 45 | + if (!empty($_SERVER['HTTP_USER_AGENT'])) { |
|
| 46 | + $user_agent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']); |
|
| 47 | 47 | } else { |
| 48 | 48 | $user_agent = ''; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - return apply_filters( 'wpinv_get_user_agent', $user_agent ); |
|
| 51 | + return apply_filters('wpinv_get_user_agent', $user_agent); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -56,16 +56,16 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @param string $amount The amount to sanitize. |
| 58 | 58 | */ |
| 59 | -function wpinv_sanitize_amount( $amount ) { |
|
| 59 | +function wpinv_sanitize_amount($amount) { |
|
| 60 | 60 | |
| 61 | 61 | // Format decimals. |
| 62 | - $amount = str_replace( wpinv_decimal_separator(), '.', $amount ); |
|
| 62 | + $amount = str_replace(wpinv_decimal_separator(), '.', $amount); |
|
| 63 | 63 | |
| 64 | 64 | // Remove thousands. |
| 65 | - $amount = str_replace( wpinv_thousands_separator(), '', $amount ); |
|
| 65 | + $amount = str_replace(wpinv_thousands_separator(), '', $amount); |
|
| 66 | 66 | |
| 67 | 67 | // Cast the remaining to a float. |
| 68 | - return (float) preg_replace( '/[^0-9\.\-]/', '', $amount ); |
|
| 68 | + return (float) preg_replace('/[^0-9\.\-]/', '', $amount); |
|
| 69 | 69 | |
| 70 | 70 | } |
| 71 | 71 | |
@@ -75,15 +75,15 @@ discard block |
||
| 75 | 75 | * @param float $amount |
| 76 | 76 | * @param float|int|null $decimals |
| 77 | 77 | */ |
| 78 | -function wpinv_round_amount( $amount, $decimals = null ) { |
|
| 78 | +function wpinv_round_amount($amount, $decimals = null) { |
|
| 79 | 79 | |
| 80 | - if ( $decimals === null ) { |
|
| 80 | + if ($decimals === null) { |
|
| 81 | 81 | $decimals = wpinv_decimals(); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - $amount = round( (float) $amount, absint( $decimals ) ); |
|
| 84 | + $amount = round((float) $amount, absint($decimals)); |
|
| 85 | 85 | //$amount = (double) sprintf( "%.{$decimals}f", (float) $amount ); |
| 86 | - return apply_filters( 'wpinv_round_amount', $amount, $decimals ); |
|
| 86 | + return apply_filters('wpinv_round_amount', $amount, $decimals); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -95,32 +95,32 @@ discard block |
||
| 95 | 95 | * @param string|WPInv_Invoice $invoice The invoice object|post type|type |
| 96 | 96 | * @return array |
| 97 | 97 | */ |
| 98 | -function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
|
| 98 | +function wpinv_get_invoice_statuses($draft = false, $trashed = false, $invoice = false) { |
|
| 99 | 99 | |
| 100 | 100 | $invoice_statuses = array( |
| 101 | - 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
| 102 | - 'publish' => _x( 'Paid', 'Invoice status', 'invoicing' ), |
|
| 103 | - 'wpi-processing' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
|
| 104 | - 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
| 105 | - 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
| 106 | - 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
| 107 | - 'wpi-failed' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
|
| 108 | - 'wpi-renewal' => _x( 'Renewal Payment', 'Invoice status', 'invoicing' ), |
|
| 101 | + 'wpi-pending' => _x('Pending payment', 'Invoice status', 'invoicing'), |
|
| 102 | + 'publish' => _x('Paid', 'Invoice status', 'invoicing'), |
|
| 103 | + 'wpi-processing' => _x('Processing', 'Invoice status', 'invoicing'), |
|
| 104 | + 'wpi-onhold' => _x('On hold', 'Invoice status', 'invoicing'), |
|
| 105 | + 'wpi-cancelled' => _x('Cancelled', 'Invoice status', 'invoicing'), |
|
| 106 | + 'wpi-refunded' => _x('Refunded', 'Invoice status', 'invoicing'), |
|
| 107 | + 'wpi-failed' => _x('Failed', 'Invoice status', 'invoicing'), |
|
| 108 | + 'wpi-renewal' => _x('Renewal Payment', 'Invoice status', 'invoicing'), |
|
| 109 | 109 | ); |
| 110 | 110 | |
| 111 | - if ( $draft ) { |
|
| 112 | - $invoice_statuses['draft'] = __( 'Draft', 'invoicing' ); |
|
| 111 | + if ($draft) { |
|
| 112 | + $invoice_statuses['draft'] = __('Draft', 'invoicing'); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - if ( $trashed ) { |
|
| 116 | - $invoice_statuses['trash'] = __( 'Trash', 'invoicing' ); |
|
| 115 | + if ($trashed) { |
|
| 116 | + $invoice_statuses['trash'] = __('Trash', 'invoicing'); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - if ( $invoice instanceof WPInv_Invoice ) { |
|
| 119 | + if ($invoice instanceof WPInv_Invoice) { |
|
| 120 | 120 | $invoice = $invoice->get_post_type(); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 123 | + return apply_filters('wpinv_statuses', $invoice_statuses, $invoice); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
@@ -129,11 +129,11 @@ discard block |
||
| 129 | 129 | * @param string $status The raw status |
| 130 | 130 | * @param string|WPInv_Invoice $invoice The invoice object|post type|type |
| 131 | 131 | */ |
| 132 | -function wpinv_status_nicename( $status, $invoice = false ) { |
|
| 133 | - $statuses = wpinv_get_invoice_statuses( true, true, $invoice ); |
|
| 134 | - $status = isset( $statuses[$status] ) ? $statuses[$status] : $status; |
|
| 132 | +function wpinv_status_nicename($status, $invoice = false) { |
|
| 133 | + $statuses = wpinv_get_invoice_statuses(true, true, $invoice); |
|
| 134 | + $status = isset($statuses[$status]) ? $statuses[$status] : $status; |
|
| 135 | 135 | |
| 136 | - return sanitize_text_field( $status ); |
|
| 136 | + return sanitize_text_field($status); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | /** |
@@ -141,13 +141,13 @@ discard block |
||
| 141 | 141 | * |
| 142 | 142 | * @param string $current |
| 143 | 143 | */ |
| 144 | -function wpinv_get_currency( $current = '' ) { |
|
| 144 | +function wpinv_get_currency($current = '') { |
|
| 145 | 145 | |
| 146 | - if ( empty( $current ) ) { |
|
| 147 | - $current = apply_filters( 'wpinv_currency', wpinv_get_option( 'currency', 'USD' ) ); |
|
| 146 | + if (empty($current)) { |
|
| 147 | + $current = apply_filters('wpinv_currency', wpinv_get_option('currency', 'USD')); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - return trim( strtoupper( $current ) ); |
|
| 150 | + return trim(strtoupper($current)); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -155,25 +155,25 @@ discard block |
||
| 155 | 155 | * |
| 156 | 156 | * @param string|null $currency The currency code. Defaults to the default currency. |
| 157 | 157 | */ |
| 158 | -function wpinv_currency_symbol( $currency = null ) { |
|
| 158 | +function wpinv_currency_symbol($currency = null) { |
|
| 159 | 159 | |
| 160 | 160 | // Prepare the currency. |
| 161 | - $currency = empty( $currency ) ? wpinv_get_currency() : wpinv_clean( $currency ); |
|
| 161 | + $currency = empty($currency) ? wpinv_get_currency() : wpinv_clean($currency); |
|
| 162 | 162 | |
| 163 | 163 | // Fetch all symbols. |
| 164 | 164 | $symbols = wpinv_get_currency_symbols(); |
| 165 | 165 | |
| 166 | 166 | // Fetch this currencies symbol. |
| 167 | - $currency_symbol = isset( $symbols[$currency] ) ? $symbols[$currency] : $currency; |
|
| 167 | + $currency_symbol = isset($symbols[$currency]) ? $symbols[$currency] : $currency; |
|
| 168 | 168 | |
| 169 | 169 | // Filter the symbol. |
| 170 | - return apply_filters( 'wpinv_currency_symbol', $currency_symbol, $currency ); |
|
| 170 | + return apply_filters('wpinv_currency_symbol', $currency_symbol, $currency); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | function wpinv_currency_position() { |
| 174 | - $position = wpinv_get_option( 'currency_position', 'left' ); |
|
| 174 | + $position = wpinv_get_option('currency_position', 'left'); |
|
| 175 | 175 | |
| 176 | - return apply_filters( 'wpinv_currency_position', $position ); |
|
| 176 | + return apply_filters('wpinv_currency_position', $position); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
@@ -181,13 +181,13 @@ discard block |
||
| 181 | 181 | * |
| 182 | 182 | * @param $string|null $current |
| 183 | 183 | */ |
| 184 | -function wpinv_thousands_separator( $current = null ) { |
|
| 184 | +function wpinv_thousands_separator($current = null) { |
|
| 185 | 185 | |
| 186 | - if ( null == $current ) { |
|
| 187 | - $current = wpinv_get_option( 'thousands_separator', '.' ); |
|
| 186 | + if (null == $current) { |
|
| 187 | + $current = wpinv_get_option('thousands_separator', '.'); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - return trim( $current ); |
|
| 190 | + return trim($current); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | /** |
@@ -195,13 +195,13 @@ discard block |
||
| 195 | 195 | * |
| 196 | 196 | * @param $string|null $current |
| 197 | 197 | */ |
| 198 | -function wpinv_decimal_separator( $current = null ) { |
|
| 198 | +function wpinv_decimal_separator($current = null) { |
|
| 199 | 199 | |
| 200 | - if ( null == $current ) { |
|
| 201 | - $current = wpinv_get_option( 'decimal_separator', '.' ); |
|
| 200 | + if (null == $current) { |
|
| 201 | + $current = wpinv_get_option('decimal_separator', '.'); |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - return trim( $current ); |
|
| 204 | + return trim($current); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -209,27 +209,27 @@ discard block |
||
| 209 | 209 | * |
| 210 | 210 | * @param $string|null $current |
| 211 | 211 | */ |
| 212 | -function wpinv_decimals( $current = null ) { |
|
| 212 | +function wpinv_decimals($current = null) { |
|
| 213 | 213 | |
| 214 | - if ( null == $current ) { |
|
| 215 | - $current = wpinv_get_option( 'decimals', 2 ); |
|
| 214 | + if (null == $current) { |
|
| 215 | + $current = wpinv_get_option('decimals', 2); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - return absint( $current ); |
|
| 218 | + return absint($current); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
| 222 | 222 | * Retrieves a list of all supported currencies. |
| 223 | 223 | */ |
| 224 | 224 | function wpinv_get_currencies() { |
| 225 | - return apply_filters( 'wpinv_currencies', wpinv_get_data( 'currencies' ) ); |
|
| 225 | + return apply_filters('wpinv_currencies', wpinv_get_data('currencies')); |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | /** |
| 229 | 229 | * Retrieves a list of all currency symbols. |
| 230 | 230 | */ |
| 231 | 231 | function wpinv_get_currency_symbols() { |
| 232 | - return apply_filters( 'wpinv_currency_symbols', wpinv_get_data( 'currency-symbols' ) ); |
|
| 232 | + return apply_filters('wpinv_currency_symbols', wpinv_get_data('currency-symbols')); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | /** |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | $currency_pos = wpinv_currency_position(); |
| 242 | 242 | $format = '%1$s%2$s'; |
| 243 | 243 | |
| 244 | - switch ( $currency_pos ) { |
|
| 244 | + switch ($currency_pos) { |
|
| 245 | 245 | case 'left': |
| 246 | 246 | $format = '%1$s%2$s'; |
| 247 | 247 | break; |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | break; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - return apply_filters( 'getpaid_price_format', $format, $currency_pos ); |
|
| 259 | + return apply_filters('getpaid_price_format', $format, $currency_pos); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -266,25 +266,25 @@ discard block |
||
| 266 | 266 | * @param string $currency Currency. |
| 267 | 267 | * @return string |
| 268 | 268 | */ |
| 269 | -function wpinv_price( $amount = 0, $currency = '' ) { |
|
| 269 | +function wpinv_price($amount = 0, $currency = '') { |
|
| 270 | 270 | |
| 271 | 271 | // Backwards compatibility. |
| 272 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
| 272 | + $amount = floatval(wpinv_sanitize_amount($amount)); |
|
| 273 | 273 | |
| 274 | 274 | // Prepare variables. |
| 275 | - $currency = wpinv_get_currency( $currency ); |
|
| 275 | + $currency = wpinv_get_currency($currency); |
|
| 276 | 276 | $amount = (float) $amount; |
| 277 | 277 | $unformatted_amount = $amount; |
| 278 | 278 | $negative = $amount < 0; |
| 279 | - $amount = apply_filters( 'getpaid_raw_amount', floatval( $negative ? $amount * -1 : $amount ) ); |
|
| 280 | - $amount = wpinv_format_amount( $amount ); |
|
| 279 | + $amount = apply_filters('getpaid_raw_amount', floatval($negative ? $amount * -1 : $amount)); |
|
| 280 | + $amount = wpinv_format_amount($amount); |
|
| 281 | 281 | |
| 282 | 282 | // Format the amount. |
| 283 | 283 | $format = getpaid_get_price_format(); |
| 284 | - $formatted_amount = ( $negative ? '-' : '' ) . sprintf( $format, '<span class="getpaid-currency__symbol">' . wpinv_currency_symbol( $currency ) . '</span>', $amount ); |
|
| 284 | + $formatted_amount = ($negative ? '-' : '') . sprintf($format, '<span class="getpaid-currency__symbol">' . wpinv_currency_symbol($currency) . '</span>', $amount); |
|
| 285 | 285 | |
| 286 | 286 | // Filter the formatting. |
| 287 | - return apply_filters( 'wpinv_price', $formatted_amount, $amount, $currency, $unformatted_amount ); |
|
| 287 | + return apply_filters('wpinv_price', $formatted_amount, $amount, $currency, $unformatted_amount); |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | /** |
@@ -295,33 +295,33 @@ discard block |
||
| 295 | 295 | * @param bool $calculate Whether or not to apply separators. |
| 296 | 296 | * @return string |
| 297 | 297 | */ |
| 298 | -function wpinv_format_amount( $amount, $decimals = null, $calculate = false ) { |
|
| 298 | +function wpinv_format_amount($amount, $decimals = null, $calculate = false) { |
|
| 299 | 299 | $thousands_sep = wpinv_thousands_separator(); |
| 300 | 300 | $decimal_sep = wpinv_decimal_separator(); |
| 301 | - $decimals = wpinv_decimals( $decimals ); |
|
| 301 | + $decimals = wpinv_decimals($decimals); |
|
| 302 | 302 | |
| 303 | 303 | // Format decimals. |
| 304 | - $amount = str_replace( $decimal_sep, '.', $amount ); |
|
| 304 | + $amount = str_replace($decimal_sep, '.', $amount); |
|
| 305 | 305 | |
| 306 | 306 | // Remove thousands. |
| 307 | - $amount = str_replace( $thousands_sep, '', $amount ); |
|
| 307 | + $amount = str_replace($thousands_sep, '', $amount); |
|
| 308 | 308 | |
| 309 | 309 | // Cast the remaining to a float. |
| 310 | - $amount = floatval( $amount ); |
|
| 310 | + $amount = floatval($amount); |
|
| 311 | 311 | |
| 312 | - if ( $calculate ) { |
|
| 312 | + if ($calculate) { |
|
| 313 | 313 | return $amount; |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | // Fomart the amount. |
| 317 | - return number_format( $amount, $decimals, $decimal_sep, $thousands_sep ); |
|
| 317 | + return number_format($amount, $decimals, $decimal_sep, $thousands_sep); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | -function wpinv_sanitize_key( $key ) { |
|
| 320 | +function wpinv_sanitize_key($key) { |
|
| 321 | 321 | $raw_key = $key; |
| 322 | - $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key ); |
|
| 322 | + $key = preg_replace('/[^a-zA-Z0-9_\-\.\:\/]/', '', $key); |
|
| 323 | 323 | |
| 324 | - return apply_filters( 'wpinv_sanitize_key', $key, $raw_key ); |
|
| 324 | + return apply_filters('wpinv_sanitize_key', $key, $raw_key); |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | /** |
@@ -329,8 +329,8 @@ discard block |
||
| 329 | 329 | * |
| 330 | 330 | * @param $str the file whose extension should be retrieved. |
| 331 | 331 | */ |
| 332 | -function wpinv_get_file_extension( $str ) { |
|
| 333 | - $filetype = wp_check_filetype( $str ); |
|
| 332 | +function wpinv_get_file_extension($str) { |
|
| 333 | + $filetype = wp_check_filetype($str); |
|
| 334 | 334 | return $filetype['ext']; |
| 335 | 335 | } |
| 336 | 336 | |
@@ -339,16 +339,16 @@ discard block |
||
| 339 | 339 | * |
| 340 | 340 | * @param string $string |
| 341 | 341 | */ |
| 342 | -function wpinv_string_is_image_url( $string ) { |
|
| 343 | - $extension = strtolower( wpinv_get_file_extension( $string ) ); |
|
| 344 | - return in_array( $extension, array( 'jpeg', 'jpg', 'png', 'gif', 'ico' ), true ); |
|
| 342 | +function wpinv_string_is_image_url($string) { |
|
| 343 | + $extension = strtolower(wpinv_get_file_extension($string)); |
|
| 344 | + return in_array($extension, array('jpeg', 'jpg', 'png', 'gif', 'ico'), true); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /** |
| 348 | 348 | * Returns the current URL. |
| 349 | 349 | */ |
| 350 | 350 | function wpinv_get_current_page_url() { |
| 351 | - return ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
| 351 | + return (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | /** |
@@ -358,46 +358,46 @@ discard block |
||
| 358 | 358 | * @param string $name Constant name. |
| 359 | 359 | * @param mixed $value Value. |
| 360 | 360 | */ |
| 361 | -function getpaid_maybe_define_constant( $name, $value ) { |
|
| 362 | - if ( ! defined( $name ) ) { |
|
| 363 | - define( $name, $value ); |
|
| 361 | +function getpaid_maybe_define_constant($name, $value) { |
|
| 362 | + if (!defined($name)) { |
|
| 363 | + define($name, $value); |
|
| 364 | 364 | } |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | function wpinv_get_php_arg_separator_output() { |
| 368 | - return ini_get( 'arg_separator.output' ); |
|
| 368 | + return ini_get('arg_separator.output'); |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | -function wpinv_rgb_from_hex( $color ) { |
|
| 372 | - $color = str_replace( '#', '', $color ); |
|
| 371 | +function wpinv_rgb_from_hex($color) { |
|
| 372 | + $color = str_replace('#', '', $color); |
|
| 373 | 373 | |
| 374 | 374 | // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF" |
| 375 | - $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color ); |
|
| 376 | - if ( empty( $color ) ) { |
|
| 375 | + $color = preg_replace('~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color); |
|
| 376 | + if (empty($color)) { |
|
| 377 | 377 | return NULL; |
| 378 | 378 | } |
| 379 | 379 | |
| 380 | - $color = str_split( $color ); |
|
| 380 | + $color = str_split($color); |
|
| 381 | 381 | |
| 382 | 382 | $rgb = array(); |
| 383 | - $rgb['R'] = hexdec( $color[0] . $color[1] ); |
|
| 384 | - $rgb['G'] = hexdec( $color[2] . $color[3] ); |
|
| 385 | - $rgb['B'] = hexdec( $color[4] . $color[5] ); |
|
| 383 | + $rgb['R'] = hexdec($color[0] . $color[1]); |
|
| 384 | + $rgb['G'] = hexdec($color[2] . $color[3]); |
|
| 385 | + $rgb['B'] = hexdec($color[4] . $color[5]); |
|
| 386 | 386 | |
| 387 | 387 | return $rgb; |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | -function wpinv_hex_darker( $color, $factor = 30 ) { |
|
| 391 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 390 | +function wpinv_hex_darker($color, $factor = 30) { |
|
| 391 | + $base = wpinv_rgb_from_hex($color); |
|
| 392 | 392 | $color = '#'; |
| 393 | 393 | |
| 394 | - foreach ( $base as $k => $v ) { |
|
| 394 | + foreach ($base as $k => $v) { |
|
| 395 | 395 | $amount = $v / 100; |
| 396 | - $amount = round( $amount * $factor ); |
|
| 396 | + $amount = round($amount * $factor); |
|
| 397 | 397 | $new_decimal = $v - $amount; |
| 398 | 398 | |
| 399 | - $new_hex_component = dechex( $new_decimal ); |
|
| 400 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 399 | + $new_hex_component = dechex($new_decimal); |
|
| 400 | + if (strlen($new_hex_component) < 2) { |
|
| 401 | 401 | $new_hex_component = "0" . $new_hex_component; |
| 402 | 402 | } |
| 403 | 403 | $color .= $new_hex_component; |
@@ -406,18 +406,18 @@ discard block |
||
| 406 | 406 | return $color; |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | -function wpinv_hex_lighter( $color, $factor = 30 ) { |
|
| 410 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 409 | +function wpinv_hex_lighter($color, $factor = 30) { |
|
| 410 | + $base = wpinv_rgb_from_hex($color); |
|
| 411 | 411 | $color = '#'; |
| 412 | 412 | |
| 413 | - foreach ( $base as $k => $v ) { |
|
| 413 | + foreach ($base as $k => $v) { |
|
| 414 | 414 | $amount = 255 - $v; |
| 415 | 415 | $amount = $amount / 100; |
| 416 | - $amount = round( $amount * $factor ); |
|
| 416 | + $amount = round($amount * $factor); |
|
| 417 | 417 | $new_decimal = $v + $amount; |
| 418 | 418 | |
| 419 | - $new_hex_component = dechex( $new_decimal ); |
|
| 420 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 419 | + $new_hex_component = dechex($new_decimal); |
|
| 420 | + if (strlen($new_hex_component) < 2) { |
|
| 421 | 421 | $new_hex_component = "0" . $new_hex_component; |
| 422 | 422 | } |
| 423 | 423 | $color .= $new_hex_component; |
@@ -426,22 +426,22 @@ discard block |
||
| 426 | 426 | return $color; |
| 427 | 427 | } |
| 428 | 428 | |
| 429 | -function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
|
| 430 | - $hex = str_replace( '#', '', $color ); |
|
| 429 | +function wpinv_light_or_dark($color, $dark = '#000000', $light = '#FFFFFF') { |
|
| 430 | + $hex = str_replace('#', '', $color); |
|
| 431 | 431 | |
| 432 | - $c_r = hexdec( substr( $hex, 0, 2 ) ); |
|
| 433 | - $c_g = hexdec( substr( $hex, 2, 2 ) ); |
|
| 434 | - $c_b = hexdec( substr( $hex, 4, 2 ) ); |
|
| 432 | + $c_r = hexdec(substr($hex, 0, 2)); |
|
| 433 | + $c_g = hexdec(substr($hex, 2, 2)); |
|
| 434 | + $c_b = hexdec(substr($hex, 4, 2)); |
|
| 435 | 435 | |
| 436 | - $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
|
| 436 | + $brightness = (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000; |
|
| 437 | 437 | |
| 438 | 438 | return $brightness > 155 ? $dark : $light; |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | -function wpinv_format_hex( $hex ) { |
|
| 442 | - $hex = trim( str_replace( '#', '', $hex ) ); |
|
| 441 | +function wpinv_format_hex($hex) { |
|
| 442 | + $hex = trim(str_replace('#', '', $hex)); |
|
| 443 | 443 | |
| 444 | - if ( strlen( $hex ) == 3 ) { |
|
| 444 | + if (strlen($hex) == 3) { |
|
| 445 | 445 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
| 446 | 446 | } |
| 447 | 447 | |
@@ -461,12 +461,12 @@ discard block |
||
| 461 | 461 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 462 | 462 | * @return string |
| 463 | 463 | */ |
| 464 | -function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) { |
|
| 465 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 466 | - return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding ); |
|
| 464 | +function wpinv_utf8_strimwidth($str, $start, $width, $trimmaker = '', $encoding = 'UTF-8') { |
|
| 465 | + if (function_exists('mb_strimwidth')) { |
|
| 466 | + return mb_strimwidth($str, $start, $width, $trimmaker, $encoding); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker; |
|
| 469 | + return wpinv_utf8_substr($str, $start, $width, $encoding) . $trimmaker; |
|
| 470 | 470 | } |
| 471 | 471 | |
| 472 | 472 | /** |
@@ -478,28 +478,28 @@ discard block |
||
| 478 | 478 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 479 | 479 | * @return int Returns the number of characters in string. |
| 480 | 480 | */ |
| 481 | -function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) { |
|
| 482 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 483 | - return mb_strlen( $str, $encoding ); |
|
| 481 | +function wpinv_utf8_strlen($str, $encoding = 'UTF-8') { |
|
| 482 | + if (function_exists('mb_strlen')) { |
|
| 483 | + return mb_strlen($str, $encoding); |
|
| 484 | 484 | } |
| 485 | 485 | |
| 486 | - return strlen( $str ); |
|
| 486 | + return strlen($str); |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | -function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) { |
|
| 490 | - if ( function_exists( 'mb_strtolower' ) ) { |
|
| 491 | - return mb_strtolower( $str, $encoding ); |
|
| 489 | +function wpinv_utf8_strtolower($str, $encoding = 'UTF-8') { |
|
| 490 | + if (function_exists('mb_strtolower')) { |
|
| 491 | + return mb_strtolower($str, $encoding); |
|
| 492 | 492 | } |
| 493 | 493 | |
| 494 | - return strtolower( $str ); |
|
| 494 | + return strtolower($str); |
|
| 495 | 495 | } |
| 496 | 496 | |
| 497 | -function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) { |
|
| 498 | - if ( function_exists( 'mb_strtoupper' ) ) { |
|
| 499 | - return mb_strtoupper( $str, $encoding ); |
|
| 497 | +function wpinv_utf8_strtoupper($str, $encoding = 'UTF-8') { |
|
| 498 | + if (function_exists('mb_strtoupper')) { |
|
| 499 | + return mb_strtoupper($str, $encoding); |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | - return strtoupper( $str ); |
|
| 502 | + return strtoupper($str); |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | /** |
@@ -513,12 +513,12 @@ discard block |
||
| 513 | 513 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 514 | 514 | * @return int Returns the position of the first occurrence of search in the string. |
| 515 | 515 | */ |
| 516 | -function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 517 | - if ( function_exists( 'mb_strpos' ) ) { |
|
| 518 | - return mb_strpos( $str, $find, $offset, $encoding ); |
|
| 516 | +function wpinv_utf8_strpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 517 | + if (function_exists('mb_strpos')) { |
|
| 518 | + return mb_strpos($str, $find, $offset, $encoding); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | - return strpos( $str, $find, $offset ); |
|
| 521 | + return strpos($str, $find, $offset); |
|
| 522 | 522 | } |
| 523 | 523 | |
| 524 | 524 | /** |
@@ -532,12 +532,12 @@ discard block |
||
| 532 | 532 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 533 | 533 | * @return int Returns the position of the last occurrence of search. |
| 534 | 534 | */ |
| 535 | -function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 536 | - if ( function_exists( 'mb_strrpos' ) ) { |
|
| 537 | - return mb_strrpos( $str, $find, $offset, $encoding ); |
|
| 535 | +function wpinv_utf8_strrpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 536 | + if (function_exists('mb_strrpos')) { |
|
| 537 | + return mb_strrpos($str, $find, $offset, $encoding); |
|
| 538 | 538 | } |
| 539 | 539 | |
| 540 | - return strrpos( $str, $find, $offset ); |
|
| 540 | + return strrpos($str, $find, $offset); |
|
| 541 | 541 | } |
| 542 | 542 | |
| 543 | 543 | /** |
@@ -552,16 +552,16 @@ discard block |
||
| 552 | 552 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 553 | 553 | * @return string |
| 554 | 554 | */ |
| 555 | -function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) { |
|
| 556 | - if ( function_exists( 'mb_substr' ) ) { |
|
| 557 | - if ( $length === null ) { |
|
| 558 | - return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 555 | +function wpinv_utf8_substr($str, $start, $length = null, $encoding = 'UTF-8') { |
|
| 556 | + if (function_exists('mb_substr')) { |
|
| 557 | + if ($length === null) { |
|
| 558 | + return mb_substr($str, $start, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 559 | 559 | } else { |
| 560 | - return mb_substr( $str, $start, $length, $encoding ); |
|
| 560 | + return mb_substr($str, $start, $length, $encoding); |
|
| 561 | 561 | } |
| 562 | 562 | } |
| 563 | 563 | |
| 564 | - return substr( $str, $start, $length ); |
|
| 564 | + return substr($str, $start, $length); |
|
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | /** |
@@ -573,48 +573,48 @@ discard block |
||
| 573 | 573 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 574 | 574 | * @return string The width of string. |
| 575 | 575 | */ |
| 576 | -function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) { |
|
| 577 | - if ( function_exists( 'mb_strwidth' ) ) { |
|
| 578 | - return mb_strwidth( $str, $encoding ); |
|
| 576 | +function wpinv_utf8_strwidth($str, $encoding = 'UTF-8') { |
|
| 577 | + if (function_exists('mb_strwidth')) { |
|
| 578 | + return mb_strwidth($str, $encoding); |
|
| 579 | 579 | } |
| 580 | 580 | |
| 581 | - return wpinv_utf8_strlen( $str, $encoding ); |
|
| 581 | + return wpinv_utf8_strlen($str, $encoding); |
|
| 582 | 582 | } |
| 583 | 583 | |
| 584 | -function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) { |
|
| 585 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 586 | - $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding ); |
|
| 584 | +function wpinv_utf8_ucfirst($str, $lower_str_end = false, $encoding = 'UTF-8') { |
|
| 585 | + if (function_exists('mb_strlen')) { |
|
| 586 | + $first_letter = wpinv_utf8_strtoupper(wpinv_utf8_substr($str, 0, 1, $encoding), $encoding); |
|
| 587 | 587 | $str_end = ""; |
| 588 | 588 | |
| 589 | - if ( $lower_str_end ) { |
|
| 590 | - $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding ); |
|
| 589 | + if ($lower_str_end) { |
|
| 590 | + $str_end = wpinv_utf8_strtolower(wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding), $encoding); |
|
| 591 | 591 | } else { |
| 592 | - $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 592 | + $str_end = wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | return $first_letter . $str_end; |
| 596 | 596 | } |
| 597 | 597 | |
| 598 | - return ucfirst( $str ); |
|
| 598 | + return ucfirst($str); |
|
| 599 | 599 | } |
| 600 | 600 | |
| 601 | -function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) { |
|
| 602 | - if ( function_exists( 'mb_convert_case' ) ) { |
|
| 603 | - return mb_convert_case( $str, MB_CASE_TITLE, $encoding ); |
|
| 601 | +function wpinv_utf8_ucwords($str, $encoding = 'UTF-8') { |
|
| 602 | + if (function_exists('mb_convert_case')) { |
|
| 603 | + return mb_convert_case($str, MB_CASE_TITLE, $encoding); |
|
| 604 | 604 | } |
| 605 | 605 | |
| 606 | - return ucwords( $str ); |
|
| 606 | + return ucwords($str); |
|
| 607 | 607 | } |
| 608 | 608 | |
| 609 | -function wpinv_period_in_days( $period, $unit ) { |
|
| 610 | - $period = absint( $period ); |
|
| 609 | +function wpinv_period_in_days($period, $unit) { |
|
| 610 | + $period = absint($period); |
|
| 611 | 611 | |
| 612 | - if ( $period > 0 ) { |
|
| 613 | - if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) { |
|
| 612 | + if ($period > 0) { |
|
| 613 | + if (in_array(strtolower($unit), array('w', 'week', 'weeks'))) { |
|
| 614 | 614 | $period = $period * 7; |
| 615 | - } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) { |
|
| 615 | + } else if (in_array(strtolower($unit), array('m', 'month', 'months'))) { |
|
| 616 | 616 | $period = $period * 30; |
| 617 | - } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) { |
|
| 617 | + } else if (in_array(strtolower($unit), array('y', 'year', 'years'))) { |
|
| 618 | 618 | $period = $period * 365; |
| 619 | 619 | } |
| 620 | 620 | } |
@@ -622,14 +622,14 @@ discard block |
||
| 622 | 622 | return $period; |
| 623 | 623 | } |
| 624 | 624 | |
| 625 | -function wpinv_cal_days_in_month( $calendar, $month, $year ) { |
|
| 626 | - if ( function_exists( 'cal_days_in_month' ) ) { |
|
| 627 | - return cal_days_in_month( $calendar, $month, $year ); |
|
| 625 | +function wpinv_cal_days_in_month($calendar, $month, $year) { |
|
| 626 | + if (function_exists('cal_days_in_month')) { |
|
| 627 | + return cal_days_in_month($calendar, $month, $year); |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | // Fallback in case the calendar extension is not loaded in PHP |
| 631 | 631 | // Only supports Gregorian calendar |
| 632 | - return date( 't', mktime( 0, 0, 0, $month, 1, $year ) ); |
|
| 632 | + return date('t', mktime(0, 0, 0, $month, 1, $year)); |
|
| 633 | 633 | } |
| 634 | 634 | |
| 635 | 635 | /** |
@@ -640,12 +640,12 @@ discard block |
||
| 640 | 640 | * |
| 641 | 641 | * @return string |
| 642 | 642 | */ |
| 643 | -function wpi_help_tip( $tip, $allow_html = false ) { |
|
| 643 | +function wpi_help_tip($tip, $allow_html = false) { |
|
| 644 | 644 | |
| 645 | - if ( $allow_html ) { |
|
| 646 | - $tip = wpi_sanitize_tooltip( $tip ); |
|
| 645 | + if ($allow_html) { |
|
| 646 | + $tip = wpi_sanitize_tooltip($tip); |
|
| 647 | 647 | } else { |
| 648 | - $tip = esc_attr( $tip ); |
|
| 648 | + $tip = esc_attr($tip); |
|
| 649 | 649 | } |
| 650 | 650 | |
| 651 | 651 | return '<span class="wpi-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
@@ -659,8 +659,8 @@ discard block |
||
| 659 | 659 | * @param string $var |
| 660 | 660 | * @return string |
| 661 | 661 | */ |
| 662 | -function wpi_sanitize_tooltip( $var ) { |
|
| 663 | - return wp_kses( html_entity_decode( $var ), array( |
|
| 662 | +function wpi_sanitize_tooltip($var) { |
|
| 663 | + return wp_kses(html_entity_decode($var), array( |
|
| 664 | 664 | 'br' => array(), |
| 665 | 665 | 'em' => array(), |
| 666 | 666 | 'strong' => array(), |
@@ -671,7 +671,7 @@ discard block |
||
| 671 | 671 | 'li' => array(), |
| 672 | 672 | 'ol' => array(), |
| 673 | 673 | 'p' => array(), |
| 674 | - ) ); |
|
| 674 | + )); |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | /** |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | */ |
| 682 | 682 | function wpinv_get_screen_ids() { |
| 683 | 683 | |
| 684 | - $screen_id = sanitize_title( __( 'Invoicing', 'invoicing' ) ); |
|
| 684 | + $screen_id = sanitize_title(__('Invoicing', 'invoicing')); |
|
| 685 | 685 | |
| 686 | 686 | $screen_ids = array( |
| 687 | 687 | 'toplevel_page_' . $screen_id, |
@@ -699,7 +699,7 @@ discard block |
||
| 699 | 699 | 'invoicing_page_wpi-addons', |
| 700 | 700 | ); |
| 701 | 701 | |
| 702 | - return apply_filters( 'wpinv_screen_ids', $screen_ids ); |
|
| 702 | + return apply_filters('wpinv_screen_ids', $screen_ids); |
|
| 703 | 703 | } |
| 704 | 704 | |
| 705 | 705 | /** |
@@ -710,14 +710,14 @@ discard block |
||
| 710 | 710 | * @param array|string $list List of values. |
| 711 | 711 | * @return array Sanitized array of values. |
| 712 | 712 | */ |
| 713 | -function wpinv_parse_list( $list ) { |
|
| 713 | +function wpinv_parse_list($list) { |
|
| 714 | 714 | |
| 715 | - if ( empty( $list ) ) { |
|
| 715 | + if (empty($list)) { |
|
| 716 | 716 | $list = array(); |
| 717 | 717 | } |
| 718 | 718 | |
| 719 | - if ( ! is_array( $list ) ) { |
|
| 720 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 719 | + if (!is_array($list)) { |
|
| 720 | + return preg_split('/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY); |
|
| 721 | 721 | } |
| 722 | 722 | |
| 723 | 723 | return $list; |
@@ -731,16 +731,16 @@ discard block |
||
| 731 | 731 | * @param string $key Type of data to fetch. |
| 732 | 732 | * @return mixed Fetched data. |
| 733 | 733 | */ |
| 734 | -function wpinv_get_data( $key ) { |
|
| 734 | +function wpinv_get_data($key) { |
|
| 735 | 735 | |
| 736 | 736 | // Try fetching it from the cache. |
| 737 | - $data = wp_cache_get( "wpinv-data-$key", 'wpinv' ); |
|
| 738 | - if( $data ) { |
|
| 737 | + $data = wp_cache_get("wpinv-data-$key", 'wpinv'); |
|
| 738 | + if ($data) { |
|
| 739 | 739 | return $data; |
| 740 | 740 | } |
| 741 | 741 | |
| 742 | - $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
|
| 743 | - wp_cache_set( "wpinv-data-$key", $data, 'wpinv' ); |
|
| 742 | + $data = apply_filters("wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php"); |
|
| 743 | + wp_cache_set("wpinv-data-$key", $data, 'wpinv'); |
|
| 744 | 744 | |
| 745 | 745 | return $data; |
| 746 | 746 | } |
@@ -754,10 +754,10 @@ discard block |
||
| 754 | 754 | * @param bool $first_empty Whether or not the first item in the list should be empty |
| 755 | 755 | * @return mixed Fetched data. |
| 756 | 756 | */ |
| 757 | -function wpinv_maybe_add_empty_option( $options, $first_empty ) { |
|
| 757 | +function wpinv_maybe_add_empty_option($options, $first_empty) { |
|
| 758 | 758 | |
| 759 | - if ( ! empty( $options ) && $first_empty ) { |
|
| 760 | - return array_merge( array( '' => '' ), $options ); |
|
| 759 | + if (!empty($options) && $first_empty) { |
|
| 760 | + return array_merge(array('' => ''), $options); |
|
| 761 | 761 | } |
| 762 | 762 | return $options; |
| 763 | 763 | |
@@ -769,21 +769,21 @@ discard block |
||
| 769 | 769 | * @param mixed $var Data to sanitize. |
| 770 | 770 | * @return string|array |
| 771 | 771 | */ |
| 772 | -function wpinv_clean( $var ) { |
|
| 772 | +function wpinv_clean($var) { |
|
| 773 | 773 | |
| 774 | - if ( is_array( $var ) ) { |
|
| 775 | - return array_map( 'wpinv_clean', $var ); |
|
| 774 | + if (is_array($var)) { |
|
| 775 | + return array_map('wpinv_clean', $var); |
|
| 776 | 776 | } |
| 777 | 777 | |
| 778 | - if ( is_object( $var ) ) { |
|
| 779 | - $object_vars = get_object_vars( $var ); |
|
| 780 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
| 781 | - $var->$property_name = wpinv_clean( $property_value ); |
|
| 778 | + if (is_object($var)) { |
|
| 779 | + $object_vars = get_object_vars($var); |
|
| 780 | + foreach ($object_vars as $property_name => $property_value) { |
|
| 781 | + $var->$property_name = wpinv_clean($property_value); |
|
| 782 | 782 | } |
| 783 | 783 | return $var; |
| 784 | 784 | } |
| 785 | 785 | |
| 786 | - return is_string( $var ) ? sanitize_text_field( $var ) : $var; |
|
| 786 | + return is_string($var) ? sanitize_text_field($var) : $var; |
|
| 787 | 787 | } |
| 788 | 788 | |
| 789 | 789 | /** |
@@ -792,43 +792,43 @@ discard block |
||
| 792 | 792 | * @param string $str Data to convert. |
| 793 | 793 | * @return string|array |
| 794 | 794 | */ |
| 795 | -function getpaid_convert_price_string_to_options( $str ) { |
|
| 795 | +function getpaid_convert_price_string_to_options($str) { |
|
| 796 | 796 | |
| 797 | - $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
| 798 | - $options = array(); |
|
| 797 | + $raw_options = array_map('trim', explode(',', $str)); |
|
| 798 | + $options = array(); |
|
| 799 | 799 | |
| 800 | - foreach ( $raw_options as $option ) { |
|
| 800 | + foreach ($raw_options as $option) { |
|
| 801 | 801 | |
| 802 | - if ( '' == $option ) { |
|
| 802 | + if ('' == $option) { |
|
| 803 | 803 | continue; |
| 804 | 804 | } |
| 805 | 805 | |
| 806 | - $option = array_map( 'trim', explode( '|', $option ) ); |
|
| 806 | + $option = array_map('trim', explode('|', $option)); |
|
| 807 | 807 | |
| 808 | 808 | $price = null; |
| 809 | 809 | $label = null; |
| 810 | 810 | |
| 811 | - if ( isset( $option[0] ) && '' != $option[0] ) { |
|
| 812 | - $label = $option[0]; |
|
| 811 | + if (isset($option[0]) && '' != $option[0]) { |
|
| 812 | + $label = $option[0]; |
|
| 813 | 813 | } |
| 814 | 814 | |
| 815 | - if ( isset( $option[1] ) && '' != $option[1] ) { |
|
| 815 | + if (isset($option[1]) && '' != $option[1]) { |
|
| 816 | 816 | $price = $option[1]; |
| 817 | 817 | } |
| 818 | 818 | |
| 819 | - if ( ! isset( $price ) ) { |
|
| 819 | + if (!isset($price)) { |
|
| 820 | 820 | $price = $label; |
| 821 | 821 | } |
| 822 | 822 | |
| 823 | - if ( ! isset( $price ) || ! is_numeric( $price ) ) { |
|
| 823 | + if (!isset($price) || !is_numeric($price)) { |
|
| 824 | 824 | continue; |
| 825 | 825 | } |
| 826 | 826 | |
| 827 | - if ( ! isset( $label ) ) { |
|
| 827 | + if (!isset($label)) { |
|
| 828 | 828 | $label = $price; |
| 829 | 829 | } |
| 830 | 830 | |
| 831 | - $options[ $price ] = $label; |
|
| 831 | + $options[$price] = $label; |
|
| 832 | 832 | } |
| 833 | 833 | |
| 834 | 834 | return $options; |
@@ -837,22 +837,22 @@ discard block |
||
| 837 | 837 | /** |
| 838 | 838 | * Returns the help tip. |
| 839 | 839 | */ |
| 840 | -function getpaid_get_help_tip( $tip, $additional_classes = '' ) { |
|
| 841 | - $additional_classes = sanitize_html_class( $additional_classes ); |
|
| 842 | - $tip = esc_attr__( $tip ); |
|
| 840 | +function getpaid_get_help_tip($tip, $additional_classes = '') { |
|
| 841 | + $additional_classes = sanitize_html_class($additional_classes); |
|
| 842 | + $tip = esc_attr__($tip); |
|
| 843 | 843 | return "<span class='wpi-help-tip dashicons dashicons-editor-help $additional_classes' title='$tip'></span>"; |
| 844 | 844 | } |
| 845 | 845 | |
| 846 | 846 | /** |
| 847 | 847 | * Formats a date |
| 848 | 848 | */ |
| 849 | -function getpaid_format_date( $date ) { |
|
| 849 | +function getpaid_format_date($date) { |
|
| 850 | 850 | |
| 851 | - if ( empty( $date ) || $date == '0000-00-00 00:00:00' ) { |
|
| 851 | + if (empty($date) || $date == '0000-00-00 00:00:00') { |
|
| 852 | 852 | return ''; |
| 853 | 853 | } |
| 854 | 854 | |
| 855 | - return date_i18n( getpaid_date_format(), strtotime( $date ) ); |
|
| 855 | + return date_i18n(getpaid_date_format(), strtotime($date)); |
|
| 856 | 856 | |
| 857 | 857 | } |
| 858 | 858 | |
@@ -861,9 +861,9 @@ discard block |
||
| 861 | 861 | * |
| 862 | 862 | * @return string |
| 863 | 863 | */ |
| 864 | -function getpaid_format_date_value( $date, $default = "—" ) { |
|
| 865 | - $date = getpaid_format_date( $date ); |
|
| 866 | - return empty( $date ) ? $default : $date; |
|
| 864 | +function getpaid_format_date_value($date, $default = "—") { |
|
| 865 | + $date = getpaid_format_date($date); |
|
| 866 | + return empty($date) ? $default : $date; |
|
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | /** |
@@ -872,7 +872,7 @@ discard block |
||
| 872 | 872 | * @return string |
| 873 | 873 | */ |
| 874 | 874 | function getpaid_date_format() { |
| 875 | - return apply_filters( 'getpaid_date_format', get_option( 'date_format' ) ); |
|
| 875 | + return apply_filters('getpaid_date_format', get_option('date_format')); |
|
| 876 | 876 | } |
| 877 | 877 | |
| 878 | 878 | /** |
@@ -881,7 +881,7 @@ discard block |
||
| 881 | 881 | * @return string |
| 882 | 882 | */ |
| 883 | 883 | function getpaid_time_format() { |
| 884 | - return apply_filters( 'getpaid_time_format', get_option( 'time_format' ) ); |
|
| 884 | + return apply_filters('getpaid_time_format', get_option('time_format')); |
|
| 885 | 885 | } |
| 886 | 886 | |
| 887 | 887 | /** |
@@ -891,16 +891,16 @@ discard block |
||
| 891 | 891 | * @param integer $limit Limit size in characters. |
| 892 | 892 | * @return string |
| 893 | 893 | */ |
| 894 | -function getpaid_limit_length( $string, $limit ) { |
|
| 894 | +function getpaid_limit_length($string, $limit) { |
|
| 895 | 895 | $str_limit = $limit - 3; |
| 896 | 896 | |
| 897 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 898 | - if ( mb_strlen( $string ) > $limit ) { |
|
| 899 | - $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
|
| 897 | + if (function_exists('mb_strimwidth')) { |
|
| 898 | + if (mb_strlen($string) > $limit) { |
|
| 899 | + $string = mb_strimwidth($string, 0, $str_limit) . '...'; |
|
| 900 | 900 | } |
| 901 | 901 | } else { |
| 902 | - if ( strlen( $string ) > $limit ) { |
|
| 903 | - $string = substr( $string, 0, $str_limit ) . '...'; |
|
| 902 | + if (strlen($string) > $limit) { |
|
| 903 | + $string = substr($string, 0, $str_limit) . '...'; |
|
| 904 | 904 | } |
| 905 | 905 | } |
| 906 | 906 | return $string; |
@@ -914,7 +914,7 @@ discard block |
||
| 914 | 914 | * @since 1.0.19 |
| 915 | 915 | */ |
| 916 | 916 | function getpaid_api() { |
| 917 | - return getpaid()->get( 'api' ); |
|
| 917 | + return getpaid()->get('api'); |
|
| 918 | 918 | } |
| 919 | 919 | |
| 920 | 920 | /** |
@@ -924,7 +924,7 @@ discard block |
||
| 924 | 924 | * @since 1.0.19 |
| 925 | 925 | */ |
| 926 | 926 | function getpaid_post_types() { |
| 927 | - return getpaid()->get( 'post_types' ); |
|
| 927 | + return getpaid()->get('post_types'); |
|
| 928 | 928 | } |
| 929 | 929 | |
| 930 | 930 | /** |
@@ -934,7 +934,7 @@ discard block |
||
| 934 | 934 | * @since 1.0.19 |
| 935 | 935 | */ |
| 936 | 936 | function getpaid_session() { |
| 937 | - return getpaid()->get( 'session' ); |
|
| 937 | + return getpaid()->get('session'); |
|
| 938 | 938 | } |
| 939 | 939 | |
| 940 | 940 | /** |
@@ -944,7 +944,7 @@ discard block |
||
| 944 | 944 | * @since 1.0.19 |
| 945 | 945 | */ |
| 946 | 946 | function getpaid_notes() { |
| 947 | - return getpaid()->get( 'notes' ); |
|
| 947 | + return getpaid()->get('notes'); |
|
| 948 | 948 | } |
| 949 | 949 | |
| 950 | 950 | /** |
@@ -953,7 +953,7 @@ discard block |
||
| 953 | 953 | * @return GetPaid_Admin |
| 954 | 954 | */ |
| 955 | 955 | function getpaid_admin() { |
| 956 | - return getpaid()->get( 'admin' ); |
|
| 956 | + return getpaid()->get('admin'); |
|
| 957 | 957 | } |
| 958 | 958 | |
| 959 | 959 | /** |
@@ -963,8 +963,8 @@ discard block |
||
| 963 | 963 | * @param string $base the base url |
| 964 | 964 | * @return string |
| 965 | 965 | */ |
| 966 | -function getpaid_get_authenticated_action_url( $action, $base = false ) { |
|
| 967 | - return wp_nonce_url( add_query_arg( 'getpaid-action', $action, $base ), 'getpaid-nonce', 'getpaid-nonce' ); |
|
| 966 | +function getpaid_get_authenticated_action_url($action, $base = false) { |
|
| 967 | + return wp_nonce_url(add_query_arg('getpaid-action', $action, $base), 'getpaid-nonce', 'getpaid-nonce'); |
|
| 968 | 968 | } |
| 969 | 969 | |
| 970 | 970 | /** |
@@ -972,11 +972,11 @@ discard block |
||
| 972 | 972 | * |
| 973 | 973 | * @return string |
| 974 | 974 | */ |
| 975 | -function getpaid_get_post_type_label( $post_type, $plural = true ) { |
|
| 975 | +function getpaid_get_post_type_label($post_type, $plural = true) { |
|
| 976 | 976 | |
| 977 | - $post_type = get_post_type_object( $post_type ); |
|
| 977 | + $post_type = get_post_type_object($post_type); |
|
| 978 | 978 | |
| 979 | - if ( ! is_object( $post_type ) ) { |
|
| 979 | + if (!is_object($post_type)) { |
|
| 980 | 980 | return null; |
| 981 | 981 | } |
| 982 | 982 | |
@@ -989,18 +989,18 @@ discard block |
||
| 989 | 989 | * |
| 990 | 990 | * @return mixed|null |
| 991 | 991 | */ |
| 992 | -function getpaid_get_array_field( $array, $key, $secondary_key = null ) { |
|
| 992 | +function getpaid_get_array_field($array, $key, $secondary_key = null) { |
|
| 993 | 993 | |
| 994 | - if ( ! is_array( $array ) ) { |
|
| 994 | + if (!is_array($array)) { |
|
| 995 | 995 | return null; |
| 996 | 996 | } |
| 997 | 997 | |
| 998 | - if ( ! empty( $secondary_key ) ) { |
|
| 999 | - $array = isset( $array[ $secondary_key ] ) ? $array[ $secondary_key ] : array(); |
|
| 1000 | - return getpaid_get_array_field( $array, $key ); |
|
| 998 | + if (!empty($secondary_key)) { |
|
| 999 | + $array = isset($array[$secondary_key]) ? $array[$secondary_key] : array(); |
|
| 1000 | + return getpaid_get_array_field($array, $key); |
|
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | - return isset( $array[ $key ] ) ? $array[ $key ] : null; |
|
| 1003 | + return isset($array[$key]) ? $array[$key] : null; |
|
| 1004 | 1004 | |
| 1005 | 1005 | } |
| 1006 | 1006 | |
@@ -1009,12 +1009,12 @@ discard block |
||
| 1009 | 1009 | * |
| 1010 | 1010 | * @return array |
| 1011 | 1011 | */ |
| 1012 | -function getpaid_array_merge_if_empty( $args, $defaults ) { |
|
| 1012 | +function getpaid_array_merge_if_empty($args, $defaults) { |
|
| 1013 | 1013 | |
| 1014 | - foreach ( $defaults as $key => $value ) { |
|
| 1014 | + foreach ($defaults as $key => $value) { |
|
| 1015 | 1015 | |
| 1016 | - if ( array_key_exists( $key, $args ) && empty( $args[ $key ] ) ) { |
|
| 1017 | - $args[ $key ] = $value; |
|
| 1016 | + if (array_key_exists($key, $args) && empty($args[$key])) { |
|
| 1017 | + $args[$key] = $value; |
|
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | 1020 | } |
@@ -12,294 +12,294 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Reports_Helper { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Get report totals such as invoice totals and discount amounts. |
|
| 17 | - * |
|
| 18 | - * Data example: |
|
| 19 | - * |
|
| 20 | - * 'subtotal' => array( |
|
| 21 | - * 'type' => 'invoice_data', |
|
| 22 | - * 'function' => 'SUM', |
|
| 23 | - * 'name' => 'subtotal' |
|
| 24 | - * ) |
|
| 25 | - * |
|
| 26 | - * @param array $args |
|
| 27 | - * @return mixed depending on query_type |
|
| 28 | - */ |
|
| 29 | - public static function get_invoice_report_data( $args = array() ) { |
|
| 30 | - global $wpdb; |
|
| 31 | - |
|
| 32 | - $default_args = array( |
|
| 33 | - 'data' => array(), // The data to retrieve. |
|
| 34 | - 'where' => array(), // An array of where queries. |
|
| 35 | - 'query_type' => 'get_row', // wpdb query to run. |
|
| 36 | - 'group_by' => '', // What to group results by. |
|
| 37 | - 'order_by' => '', // What to order by. |
|
| 38 | - 'limit' => '', // Results limit. |
|
| 39 | - 'filter_range' => array(), // An array of before and after dates to limit results by. |
|
| 40 | - 'invoice_types' => array( 'wpi_invoice' ), // An array of post types to retrieve. |
|
| 41 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold' ), |
|
| 42 | - 'parent_invoice_status' => false, // Optionally filter by parent invoice status. |
|
| 43 | - ); |
|
| 44 | - |
|
| 45 | - $args = apply_filters( 'getpaid_reports_get_invoice_report_data_args', $args ); |
|
| 46 | - $args = wp_parse_args( $args, $default_args ); |
|
| 47 | - |
|
| 48 | - extract( $args ); |
|
| 49 | - |
|
| 50 | - if ( empty( $data ) ) { |
|
| 51 | - return ''; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $query = array(); |
|
| 55 | - $query['select'] = 'SELECT ' . implode( ',', self::prepare_invoice_data( $data ) ); |
|
| 56 | - $query['from'] = "FROM {$wpdb->posts} AS posts"; |
|
| 57 | - $query['join'] = implode( ' ', self::prepare_invoice_joins( $data + $where, ! empty( $parent_invoice_status ) ) ); |
|
| 58 | - |
|
| 59 | - $query['where'] = " |
|
| 15 | + /** |
|
| 16 | + * Get report totals such as invoice totals and discount amounts. |
|
| 17 | + * |
|
| 18 | + * Data example: |
|
| 19 | + * |
|
| 20 | + * 'subtotal' => array( |
|
| 21 | + * 'type' => 'invoice_data', |
|
| 22 | + * 'function' => 'SUM', |
|
| 23 | + * 'name' => 'subtotal' |
|
| 24 | + * ) |
|
| 25 | + * |
|
| 26 | + * @param array $args |
|
| 27 | + * @return mixed depending on query_type |
|
| 28 | + */ |
|
| 29 | + public static function get_invoice_report_data( $args = array() ) { |
|
| 30 | + global $wpdb; |
|
| 31 | + |
|
| 32 | + $default_args = array( |
|
| 33 | + 'data' => array(), // The data to retrieve. |
|
| 34 | + 'where' => array(), // An array of where queries. |
|
| 35 | + 'query_type' => 'get_row', // wpdb query to run. |
|
| 36 | + 'group_by' => '', // What to group results by. |
|
| 37 | + 'order_by' => '', // What to order by. |
|
| 38 | + 'limit' => '', // Results limit. |
|
| 39 | + 'filter_range' => array(), // An array of before and after dates to limit results by. |
|
| 40 | + 'invoice_types' => array( 'wpi_invoice' ), // An array of post types to retrieve. |
|
| 41 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold' ), |
|
| 42 | + 'parent_invoice_status' => false, // Optionally filter by parent invoice status. |
|
| 43 | + ); |
|
| 44 | + |
|
| 45 | + $args = apply_filters( 'getpaid_reports_get_invoice_report_data_args', $args ); |
|
| 46 | + $args = wp_parse_args( $args, $default_args ); |
|
| 47 | + |
|
| 48 | + extract( $args ); |
|
| 49 | + |
|
| 50 | + if ( empty( $data ) ) { |
|
| 51 | + return ''; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $query = array(); |
|
| 55 | + $query['select'] = 'SELECT ' . implode( ',', self::prepare_invoice_data( $data ) ); |
|
| 56 | + $query['from'] = "FROM {$wpdb->posts} AS posts"; |
|
| 57 | + $query['join'] = implode( ' ', self::prepare_invoice_joins( $data + $where, ! empty( $parent_invoice_status ) ) ); |
|
| 58 | + |
|
| 59 | + $query['where'] = " |
|
| 60 | 60 | WHERE posts.post_type IN ( '" . implode( "','", $invoice_types ) . "' ) |
| 61 | 61 | "; |
| 62 | 62 | |
| 63 | - if ( ! empty( $invoice_status ) ) { |
|
| 64 | - $query['where'] .= " |
|
| 63 | + if ( ! empty( $invoice_status ) ) { |
|
| 64 | + $query['where'] .= " |
|
| 65 | 65 | AND posts.post_status IN ( '" . implode( "','", $invoice_status ) . "' ) |
| 66 | 66 | "; |
| 67 | - } |
|
| 68 | - |
|
| 69 | - if ( ! empty( $parent_invoice_status ) ) { |
|
| 70 | - if ( ! empty( $invoice_status ) ) { |
|
| 71 | - $query['where'] .= " AND ( parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) OR parent.ID IS NULL ) "; |
|
| 72 | - } else { |
|
| 73 | - $query['where'] .= " AND parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) "; |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if ( ! empty( $filter_range['before'] ) ) { |
|
| 78 | - $query['where'] .= " |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if ( ! empty( $parent_invoice_status ) ) { |
|
| 70 | + if ( ! empty( $invoice_status ) ) { |
|
| 71 | + $query['where'] .= " AND ( parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) OR parent.ID IS NULL ) "; |
|
| 72 | + } else { |
|
| 73 | + $query['where'] .= " AND parent.post_status IN ( '" . implode( "','", $parent_invoice_status ) . "' ) "; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if ( ! empty( $filter_range['before'] ) ) { |
|
| 78 | + $query['where'] .= " |
|
| 79 | 79 | AND posts.post_date < '" . date( 'Y-m-d H:i:s', strtotime( $filter_range['before'] ) ) . "' |
| 80 | 80 | "; |
| 81 | - } |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - if ( ! empty( $filter_range['after'] ) ) { |
|
| 84 | - $query['where'] .= " |
|
| 83 | + if ( ! empty( $filter_range['after'] ) ) { |
|
| 84 | + $query['where'] .= " |
|
| 85 | 85 | AND posts.post_date > '" . date( 'Y-m-d H:i:s', strtotime( $filter_range['after'] ) ) . "' |
| 86 | 86 | "; |
| 87 | - } |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - if ( ! empty( $where ) ) { |
|
| 89 | + if ( ! empty( $where ) ) { |
|
| 90 | 90 | |
| 91 | - foreach ( $where as $value ) { |
|
| 91 | + foreach ( $where as $value ) { |
|
| 92 | 92 | |
| 93 | - if ( strtolower( $value['operator'] ) == 'in' || strtolower( $value['operator'] ) == 'not in' ) { |
|
| 94 | - |
|
| 95 | - if ( is_array( $value['value'] ) ) { |
|
| 96 | - $value['value'] = implode( "','", $value['value'] ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if ( ! empty( $value['value'] ) ) { |
|
| 100 | - $where_value = "{$value['operator']} ('{$value['value']}')"; |
|
| 101 | - } |
|
| 102 | - } else { |
|
| 103 | - $where_value = "{$value['operator']} '{$value['value']}'"; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if ( ! empty( $where_value ) ) { |
|
| 107 | - $query['where'] .= " AND {$value['key']} {$where_value}"; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ( $group_by ) { |
|
| 113 | - $query['group_by'] = "GROUP BY {$group_by}"; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - if ( $order_by ) { |
|
| 117 | - $query['order_by'] = "ORDER BY {$order_by}"; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - if ( $limit ) { |
|
| 121 | - $query['limit'] = "LIMIT {$limit}"; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $query = apply_filters( 'getpaid_reports_get_invoice_report_query', $query, $data ); |
|
| 125 | - $query = implode( ' ', $query ); |
|
| 126 | - |
|
| 127 | - return self::execute( $query_type, $query ); |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Prepares the data to select. |
|
| 133 | - * |
|
| 134 | - * |
|
| 135 | - * @param array $data |
|
| 136 | - * @return array |
|
| 137 | - */ |
|
| 138 | - public static function prepare_invoice_data( $data ) { |
|
| 139 | - |
|
| 140 | - $prepared = array(); |
|
| 141 | - |
|
| 142 | - foreach ( $data as $raw_key => $value ) { |
|
| 143 | - $key = sanitize_key( $raw_key ); |
|
| 144 | - $distinct = ''; |
|
| 145 | - |
|
| 146 | - if ( isset( $value['distinct'] ) ) { |
|
| 147 | - $distinct = 'DISTINCT'; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $get_key = self::get_invoice_table_key( $key, $value['type'] ); |
|
| 151 | - |
|
| 152 | - if ( false === $get_key ) { |
|
| 153 | - // Skip to the next foreach iteration else the query will be invalid. |
|
| 154 | - continue; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if ( ! empty( $value['function'] ) ) { |
|
| 158 | - $get = "{$value['function']}({$distinct} {$get_key})"; |
|
| 159 | - } else { |
|
| 160 | - $get = "{$distinct} {$get_key}"; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $prepared[] = "{$get} as {$value['name']}"; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return $prepared; |
|
| 167 | - |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Prepares the joins to use. |
|
| 172 | - * |
|
| 173 | - * |
|
| 174 | - * @param array $data |
|
| 175 | - * @param bool $with_parent |
|
| 176 | - * @return array |
|
| 177 | - */ |
|
| 178 | - public static function prepare_invoice_joins( $data, $with_parent ) { |
|
| 179 | - global $wpdb; |
|
| 180 | - |
|
| 181 | - $prepared = array(); |
|
| 182 | - |
|
| 183 | - foreach ( $data as $raw_key => $value ) { |
|
| 184 | - $join_type = isset( $value['join_type'] ) ? $value['join_type'] : 'INNER'; |
|
| 185 | - $type = isset( $value['type'] ) ? $value['type'] : false; |
|
| 186 | - $key = sanitize_key( $raw_key ); |
|
| 187 | - |
|
| 188 | - switch ( $type ) { |
|
| 189 | - case 'meta': |
|
| 190 | - $prepared[ "meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON ( posts.ID = meta_{$key}.post_id AND meta_{$key}.meta_key = '{$raw_key}' )"; |
|
| 191 | - break; |
|
| 192 | - case 'parent_meta': |
|
| 193 | - $prepared[ "parent_meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS parent_meta_{$key} ON (posts.post_parent = parent_meta_{$key}.post_id) AND (parent_meta_{$key}.meta_key = '{$raw_key}')"; |
|
| 194 | - break; |
|
| 195 | - case 'invoice_data': |
|
| 196 | - $prepared['invoices'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoices AS invoices ON posts.ID = invoices.post_id"; |
|
| 197 | - break; |
|
| 198 | - case 'invoice_item': |
|
| 199 | - $prepared['invoice_items'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoice_items AS invoice_items ON posts.ID = invoice_items.post_id"; |
|
| 200 | - break; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - if ( $with_parent ) { |
|
| 205 | - $prepared['parent'] = "LEFT JOIN {$wpdb->posts} AS parent ON posts.post_parent = parent.ID"; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return $prepared; |
|
| 209 | - |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Retrieves the appropriate table key to use. |
|
| 214 | - * |
|
| 215 | - * |
|
| 216 | - * @param string $key |
|
| 217 | - * @param string $table |
|
| 218 | - * @return string|false |
|
| 219 | - */ |
|
| 220 | - public static function get_invoice_table_key( $key, $table ) { |
|
| 221 | - |
|
| 222 | - $keys = array( |
|
| 223 | - 'meta' => "meta_{$key}.meta_value", |
|
| 224 | - 'parent_meta' => "parent_meta_{$key}.meta_value", |
|
| 225 | - 'post_data' => "posts.{$key}", |
|
| 226 | - 'invoice_data' => "invoices.{$key}", |
|
| 227 | - 'invoice_item' => "invoice_items.{$key}", |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - return isset( $keys[ $table ] ) ? $keys[ $table ] : false; |
|
| 231 | - |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Executes a query and caches the result for a minute. |
|
| 236 | - * |
|
| 237 | - * |
|
| 238 | - * @param string $query_type |
|
| 239 | - * @param string $query |
|
| 240 | - * @return mixed depending on query_type |
|
| 241 | - */ |
|
| 242 | - public static function execute( $query_type, $query ) { |
|
| 243 | - global $wpdb; |
|
| 244 | - |
|
| 245 | - $query_hash = md5( $query_type . $query ); |
|
| 246 | - $result = self::get_cached_query( $query_hash ); |
|
| 247 | - if ( $result === false ) { |
|
| 248 | - self::enable_big_selects(); |
|
| 249 | - |
|
| 250 | - $result = $wpdb->$query_type( $query ); |
|
| 251 | - self::set_cached_query( $query_hash, $result ); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $result; |
|
| 255 | - |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Enables big mysql selects for reports, just once for this session. |
|
| 260 | - */ |
|
| 261 | - protected static function enable_big_selects() { |
|
| 262 | - static $big_selects = false; |
|
| 263 | - |
|
| 264 | - global $wpdb; |
|
| 265 | - |
|
| 266 | - if ( ! $big_selects ) { |
|
| 267 | - $wpdb->query( 'SET SESSION SQL_BIG_SELECTS=1' ); |
|
| 268 | - $big_selects = true; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Get the cached query result or null if it's not in the cache. |
|
| 274 | - * |
|
| 275 | - * @param string $query_hash The query hash. |
|
| 276 | - * |
|
| 277 | - * @return mixed|false The cache contents on success, false on failure to retrieve contents. |
|
| 278 | - */ |
|
| 279 | - protected static function get_cached_query( $query_hash ) { |
|
| 280 | - |
|
| 281 | - return wp_cache_get( |
|
| 282 | - $query_hash, |
|
| 283 | - strtolower( __CLASS__ ) |
|
| 284 | - ); |
|
| 285 | - |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Set the cached query result. |
|
| 290 | - * |
|
| 291 | - * @param string $query_hash The query hash. |
|
| 292 | - * @param mixed $data The data to cache. |
|
| 293 | - */ |
|
| 294 | - protected static function set_cached_query( $query_hash, $data ) { |
|
| 295 | - |
|
| 296 | - wp_cache_set( |
|
| 297 | - $query_hash, |
|
| 298 | - $data, |
|
| 299 | - strtolower( __CLASS__ ), |
|
| 300 | - 5 * MINUTE_IN_SECONDS |
|
| 301 | - ); |
|
| 302 | - |
|
| 303 | - } |
|
| 93 | + if ( strtolower( $value['operator'] ) == 'in' || strtolower( $value['operator'] ) == 'not in' ) { |
|
| 94 | + |
|
| 95 | + if ( is_array( $value['value'] ) ) { |
|
| 96 | + $value['value'] = implode( "','", $value['value'] ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if ( ! empty( $value['value'] ) ) { |
|
| 100 | + $where_value = "{$value['operator']} ('{$value['value']}')"; |
|
| 101 | + } |
|
| 102 | + } else { |
|
| 103 | + $where_value = "{$value['operator']} '{$value['value']}'"; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if ( ! empty( $where_value ) ) { |
|
| 107 | + $query['where'] .= " AND {$value['key']} {$where_value}"; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ( $group_by ) { |
|
| 113 | + $query['group_by'] = "GROUP BY {$group_by}"; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + if ( $order_by ) { |
|
| 117 | + $query['order_by'] = "ORDER BY {$order_by}"; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + if ( $limit ) { |
|
| 121 | + $query['limit'] = "LIMIT {$limit}"; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $query = apply_filters( 'getpaid_reports_get_invoice_report_query', $query, $data ); |
|
| 125 | + $query = implode( ' ', $query ); |
|
| 126 | + |
|
| 127 | + return self::execute( $query_type, $query ); |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Prepares the data to select. |
|
| 133 | + * |
|
| 134 | + * |
|
| 135 | + * @param array $data |
|
| 136 | + * @return array |
|
| 137 | + */ |
|
| 138 | + public static function prepare_invoice_data( $data ) { |
|
| 139 | + |
|
| 140 | + $prepared = array(); |
|
| 141 | + |
|
| 142 | + foreach ( $data as $raw_key => $value ) { |
|
| 143 | + $key = sanitize_key( $raw_key ); |
|
| 144 | + $distinct = ''; |
|
| 145 | + |
|
| 146 | + if ( isset( $value['distinct'] ) ) { |
|
| 147 | + $distinct = 'DISTINCT'; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $get_key = self::get_invoice_table_key( $key, $value['type'] ); |
|
| 151 | + |
|
| 152 | + if ( false === $get_key ) { |
|
| 153 | + // Skip to the next foreach iteration else the query will be invalid. |
|
| 154 | + continue; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if ( ! empty( $value['function'] ) ) { |
|
| 158 | + $get = "{$value['function']}({$distinct} {$get_key})"; |
|
| 159 | + } else { |
|
| 160 | + $get = "{$distinct} {$get_key}"; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $prepared[] = "{$get} as {$value['name']}"; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return $prepared; |
|
| 167 | + |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Prepares the joins to use. |
|
| 172 | + * |
|
| 173 | + * |
|
| 174 | + * @param array $data |
|
| 175 | + * @param bool $with_parent |
|
| 176 | + * @return array |
|
| 177 | + */ |
|
| 178 | + public static function prepare_invoice_joins( $data, $with_parent ) { |
|
| 179 | + global $wpdb; |
|
| 180 | + |
|
| 181 | + $prepared = array(); |
|
| 182 | + |
|
| 183 | + foreach ( $data as $raw_key => $value ) { |
|
| 184 | + $join_type = isset( $value['join_type'] ) ? $value['join_type'] : 'INNER'; |
|
| 185 | + $type = isset( $value['type'] ) ? $value['type'] : false; |
|
| 186 | + $key = sanitize_key( $raw_key ); |
|
| 187 | + |
|
| 188 | + switch ( $type ) { |
|
| 189 | + case 'meta': |
|
| 190 | + $prepared[ "meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON ( posts.ID = meta_{$key}.post_id AND meta_{$key}.meta_key = '{$raw_key}' )"; |
|
| 191 | + break; |
|
| 192 | + case 'parent_meta': |
|
| 193 | + $prepared[ "parent_meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS parent_meta_{$key} ON (posts.post_parent = parent_meta_{$key}.post_id) AND (parent_meta_{$key}.meta_key = '{$raw_key}')"; |
|
| 194 | + break; |
|
| 195 | + case 'invoice_data': |
|
| 196 | + $prepared['invoices'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoices AS invoices ON posts.ID = invoices.post_id"; |
|
| 197 | + break; |
|
| 198 | + case 'invoice_item': |
|
| 199 | + $prepared['invoice_items'] = "{$join_type} JOIN {$wpdb->prefix}getpaid_invoice_items AS invoice_items ON posts.ID = invoice_items.post_id"; |
|
| 200 | + break; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + if ( $with_parent ) { |
|
| 205 | + $prepared['parent'] = "LEFT JOIN {$wpdb->posts} AS parent ON posts.post_parent = parent.ID"; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return $prepared; |
|
| 209 | + |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Retrieves the appropriate table key to use. |
|
| 214 | + * |
|
| 215 | + * |
|
| 216 | + * @param string $key |
|
| 217 | + * @param string $table |
|
| 218 | + * @return string|false |
|
| 219 | + */ |
|
| 220 | + public static function get_invoice_table_key( $key, $table ) { |
|
| 221 | + |
|
| 222 | + $keys = array( |
|
| 223 | + 'meta' => "meta_{$key}.meta_value", |
|
| 224 | + 'parent_meta' => "parent_meta_{$key}.meta_value", |
|
| 225 | + 'post_data' => "posts.{$key}", |
|
| 226 | + 'invoice_data' => "invoices.{$key}", |
|
| 227 | + 'invoice_item' => "invoice_items.{$key}", |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + return isset( $keys[ $table ] ) ? $keys[ $table ] : false; |
|
| 231 | + |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Executes a query and caches the result for a minute. |
|
| 236 | + * |
|
| 237 | + * |
|
| 238 | + * @param string $query_type |
|
| 239 | + * @param string $query |
|
| 240 | + * @return mixed depending on query_type |
|
| 241 | + */ |
|
| 242 | + public static function execute( $query_type, $query ) { |
|
| 243 | + global $wpdb; |
|
| 244 | + |
|
| 245 | + $query_hash = md5( $query_type . $query ); |
|
| 246 | + $result = self::get_cached_query( $query_hash ); |
|
| 247 | + if ( $result === false ) { |
|
| 248 | + self::enable_big_selects(); |
|
| 249 | + |
|
| 250 | + $result = $wpdb->$query_type( $query ); |
|
| 251 | + self::set_cached_query( $query_hash, $result ); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $result; |
|
| 255 | + |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Enables big mysql selects for reports, just once for this session. |
|
| 260 | + */ |
|
| 261 | + protected static function enable_big_selects() { |
|
| 262 | + static $big_selects = false; |
|
| 263 | + |
|
| 264 | + global $wpdb; |
|
| 265 | + |
|
| 266 | + if ( ! $big_selects ) { |
|
| 267 | + $wpdb->query( 'SET SESSION SQL_BIG_SELECTS=1' ); |
|
| 268 | + $big_selects = true; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Get the cached query result or null if it's not in the cache. |
|
| 274 | + * |
|
| 275 | + * @param string $query_hash The query hash. |
|
| 276 | + * |
|
| 277 | + * @return mixed|false The cache contents on success, false on failure to retrieve contents. |
|
| 278 | + */ |
|
| 279 | + protected static function get_cached_query( $query_hash ) { |
|
| 280 | + |
|
| 281 | + return wp_cache_get( |
|
| 282 | + $query_hash, |
|
| 283 | + strtolower( __CLASS__ ) |
|
| 284 | + ); |
|
| 285 | + |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Set the cached query result. |
|
| 290 | + * |
|
| 291 | + * @param string $query_hash The query hash. |
|
| 292 | + * @param mixed $data The data to cache. |
|
| 293 | + */ |
|
| 294 | + protected static function set_cached_query( $query_hash, $data ) { |
|
| 295 | + |
|
| 296 | + wp_cache_set( |
|
| 297 | + $query_hash, |
|
| 298 | + $data, |
|
| 299 | + strtolower( __CLASS__ ), |
|
| 300 | + 5 * MINUTE_IN_SECONDS |
|
| 301 | + ); |
|
| 302 | + |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | 305 | } |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | * @since 1.0.19 |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -defined( 'ABSPATH' ) || exit; |
|
| 9 | +defined('ABSPATH') || exit; |
|
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * The main API class |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | $this->top_sellers = new GetPaid_REST_Report_Top_Sellers_Controller(); |
| 81 | 81 | |
| 82 | 82 | // Fires after loading the rest api. |
| 83 | - do_action( 'getpaid_rest_api_loaded', $this ); |
|
| 83 | + do_action('getpaid_rest_api_loaded', $this); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | } |
@@ -18,652 +18,652 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class GetPaid_REST_Report_Sales_Controller extends GetPaid_REST_Date_Based_Controller { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Route base. |
|
| 23 | - * |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $rest_base = 'reports/sales'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The report data. |
|
| 30 | - * |
|
| 31 | - * @var stdClass |
|
| 32 | - */ |
|
| 33 | - protected $report_data; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The report range. |
|
| 37 | - * |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - protected $report_range; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Registers the routes for the objects of the controller. |
|
| 44 | - * |
|
| 45 | - * @since 2.0.0 |
|
| 46 | - * |
|
| 47 | - * @see register_rest_route() |
|
| 48 | - */ |
|
| 49 | - public function register_namespace_routes( $namespace ) { |
|
| 50 | - |
|
| 51 | - // Get sales report. |
|
| 52 | - register_rest_route( |
|
| 53 | - $namespace, |
|
| 54 | - $this->rest_base, |
|
| 55 | - array( |
|
| 56 | - array( |
|
| 57 | - 'methods' => WP_REST_Server::READABLE, |
|
| 58 | - 'callback' => array( $this, 'get_items' ), |
|
| 59 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 60 | - 'args' => $this->get_collection_params(), |
|
| 61 | - ), |
|
| 62 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 63 | - ) |
|
| 64 | - ); |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Makes sure the current user has access to READ the report APIs. |
|
| 70 | - * |
|
| 71 | - * @since 2.0.0 |
|
| 72 | - * @param WP_REST_Request $request Full data about the request. |
|
| 73 | - * @return WP_Error|boolean |
|
| 74 | - */ |
|
| 75 | - public function get_items_permissions_check( $request ) { |
|
| 76 | - |
|
| 77 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 78 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return true; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Get sales reports. |
|
| 86 | - * |
|
| 87 | - * @param WP_REST_Request $request |
|
| 88 | - * @return array|WP_Error |
|
| 89 | - */ |
|
| 90 | - public function get_items( $request ) { |
|
| 91 | - $data = array(); |
|
| 92 | - $item = $this->prepare_item_for_response( null, $request ); |
|
| 93 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
| 94 | - |
|
| 95 | - return rest_ensure_response( $data ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Prepare a report sales object for serialization. |
|
| 100 | - * |
|
| 101 | - * @param null $_ |
|
| 102 | - * @param WP_REST_Request $request Request object. |
|
| 103 | - * @return WP_REST_Response $response Response data. |
|
| 104 | - */ |
|
| 105 | - public function prepare_item_for_response( $_, $request ) { |
|
| 106 | - |
|
| 107 | - // Set report range. |
|
| 108 | - $this->report_range = $this->get_date_range( $request ); |
|
| 109 | - |
|
| 110 | - $report_data = $this->get_report_data(); |
|
| 111 | - $period_totals = array(); |
|
| 112 | - |
|
| 113 | - // Setup period totals by ensuring each period in the interval has data. |
|
| 114 | - $start_date = strtotime( $this->report_range['after'] ) + DAY_IN_SECONDS; |
|
| 115 | - |
|
| 116 | - if ( 'month' === $this->groupby ) { |
|
| 117 | - $start_date = strtotime( date( 'Y-m-01', $start_date ) ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - for ( $i = 0; $i < $this->interval; $i++ ) { |
|
| 121 | - |
|
| 122 | - switch ( $this->groupby ) { |
|
| 123 | - case 'day' : |
|
| 124 | - $time = date( 'Y-m-d', strtotime( "+{$i} DAY", $start_date ) ); |
|
| 125 | - break; |
|
| 126 | - default : |
|
| 127 | - $time = date( 'Y-m', strtotime( "+{$i} MONTH", $start_date ) ); |
|
| 128 | - break; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // Set the defaults for each period. |
|
| 132 | - $period_totals[ $time ] = array( |
|
| 133 | - 'sales' => wpinv_round_amount( 0.00 ), |
|
| 134 | - 'invoices' => 0, |
|
| 135 | - 'refunds' => wpinv_round_amount( 0.00 ), |
|
| 136 | - 'items' => 0, |
|
| 137 | - 'refunded_items' => 0, |
|
| 138 | - 'tax' => wpinv_round_amount( 0.00 ), |
|
| 139 | - 'refunded_tax' => wpinv_round_amount( 0.00 ), |
|
| 140 | - 'subtotal' => wpinv_round_amount( 0.00 ), |
|
| 141 | - 'refunded_subtotal' => wpinv_round_amount( 0.00 ), |
|
| 142 | - 'fees' => wpinv_round_amount( 0.00 ), |
|
| 143 | - 'refunded_fees' => wpinv_round_amount( 0.00 ), |
|
| 144 | - 'discount' => wpinv_round_amount( 0.00 ), |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // add total sales, total invoice count, total tax for each period |
|
| 150 | - $date_format = ( 'day' === $this->groupby ) ? 'Y-m-d' : 'Y-m'; |
|
| 151 | - foreach ( $report_data->invoices as $invoice ) { |
|
| 152 | - $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 153 | - |
|
| 154 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
| 155 | - continue; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $period_totals[ $time ]['sales'] = wpinv_round_amount( $invoice->total_sales ); |
|
| 159 | - $period_totals[ $time ]['tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
| 160 | - $period_totals[ $time ]['subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
| 161 | - $period_totals[ $time ]['fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - foreach ( $report_data->refunds as $invoice ) { |
|
| 166 | - $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 167 | - |
|
| 168 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
| 169 | - continue; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $period_totals[ $time ]['refunds'] = wpinv_round_amount( $invoice->total_sales ); |
|
| 173 | - $period_totals[ $time ]['refunded_tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
| 174 | - $period_totals[ $time ]['refunded_subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
| 175 | - $period_totals[ $time ]['refunded_fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
| 176 | - |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - foreach ( $report_data->invoice_counts as $invoice ) { |
|
| 180 | - $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 181 | - |
|
| 182 | - if ( isset( $period_totals[ $time ] ) ) { |
|
| 183 | - $period_totals[ $time ]['invoices'] = (int) $invoice->count; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - // Add total invoice items for each period. |
|
| 189 | - foreach ( $report_data->invoice_items as $invoice_item ) { |
|
| 190 | - $time = ( 'day' === $this->groupby ) ? date( 'Y-m-d', strtotime( $invoice_item->post_date ) ) : date( 'Y-m', strtotime( $invoice_item->post_date ) ); |
|
| 191 | - |
|
| 192 | - if ( isset( $period_totals[ $time ] ) ) { |
|
| 193 | - $period_totals[ $time ]['items'] = (int) $invoice_item->invoice_item_count; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - // Add total discount for each period. |
|
| 199 | - foreach ( $report_data->coupons as $discount ) { |
|
| 200 | - $time = ( 'day' === $this->groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) ); |
|
| 201 | - |
|
| 202 | - if ( isset( $period_totals[ $time ] ) ) { |
|
| 203 | - $period_totals[ $time ]['discount'] = wpinv_round_amount( $discount->discount_amount ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - $report_data->totals = $period_totals; |
|
| 209 | - $report_data->grouped_by = $this->groupby; |
|
| 210 | - $report_data->interval = max( $this->interval, 1 ); |
|
| 211 | - $report_data->currency = wpinv_get_currency(); |
|
| 212 | - $report_data->currency_symbol = wpinv_currency_symbol(); |
|
| 213 | - $report_data->currency_position = wpinv_currency_position(); |
|
| 214 | - $report_data->decimal_places = wpinv_decimals(); |
|
| 215 | - $report_data->thousands_sep = wpinv_thousands_separator(); |
|
| 216 | - $report_data->decimals_sep = wpinv_decimal_separator(); |
|
| 217 | - $report_data->start_date = getpaid_format_date( date( 'Y-m-d', strtotime( $this->report_range['after'] ) + DAY_IN_SECONDS ) ); |
|
| 218 | - $report_data->end_date = getpaid_format_date( date( 'Y-m-d', strtotime( $this->report_range['before'] ) - DAY_IN_SECONDS ) ); |
|
| 219 | - $report_data->decimals_sep = wpinv_decimal_separator(); |
|
| 220 | - |
|
| 221 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 222 | - $data = $report_data; |
|
| 223 | - unset( $data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items ); |
|
| 224 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 225 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 226 | - |
|
| 227 | - // Wrap the data in a response object. |
|
| 228 | - $response = rest_ensure_response( $data ); |
|
| 229 | - $response->add_links( array( |
|
| 230 | - 'about' => array( |
|
| 231 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
| 232 | - ), |
|
| 233 | - ) ); |
|
| 234 | - |
|
| 235 | - return apply_filters( 'getpaid_rest_prepare_report_sales', $response, $report_data, $request ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Get report data. |
|
| 240 | - * |
|
| 241 | - * @return stdClass |
|
| 242 | - */ |
|
| 243 | - public function get_report_data() { |
|
| 244 | - if ( empty( $this->report_data ) ) { |
|
| 245 | - $this->query_report_data(); |
|
| 246 | - } |
|
| 247 | - return $this->report_data; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get all data needed for this report and store in the class. |
|
| 252 | - */ |
|
| 253 | - protected function query_report_data() { |
|
| 254 | - |
|
| 255 | - // Prepare reports. |
|
| 256 | - $this->report_data = (object) array( |
|
| 257 | - 'invoice_counts' => $this->query_invoice_counts(),//count, post_date |
|
| 258 | - 'coupons' => $this->query_coupon_counts(), // discount_amount, post_date |
|
| 259 | - 'invoice_items' => $this->query_item_counts(), // invoice_item_count, post_date |
|
| 260 | - 'refunded_items' => $this->count_refunded_items(), // invoice_item_count, post_date |
|
| 261 | - 'invoices' => $this->query_invoice_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
| 262 | - 'refunds' => $this->query_refunded_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
| 263 | - ); |
|
| 264 | - |
|
| 265 | - // Calculated totals. |
|
| 266 | - $this->report_data->total_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_tax' ) ) ); |
|
| 267 | - $this->report_data->total_sales = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_sales' ) ) ); |
|
| 268 | - $this->report_data->total_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_discount' ) ) ); |
|
| 269 | - $this->report_data->total_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_fees' ) ) ); |
|
| 270 | - $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'subtotal' ) ) ); |
|
| 271 | - $this->report_data->net_sales = wpinv_round_amount( $this->report_data->total_sales - max( 0, $this->report_data->total_tax ) ); |
|
| 272 | - $this->report_data->total_refunded_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_tax' ) ) ); |
|
| 273 | - $this->report_data->total_refunds = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_sales' ) ) ); |
|
| 274 | - $this->report_data->refunded_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_discount' ) ) ); |
|
| 275 | - $this->report_data->refunded_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_fees' ) ) ); |
|
| 276 | - $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'subtotal' ) ) ); |
|
| 277 | - $this->report_data->net_refunds = wpinv_round_amount( $this->report_data->total_refunds + max( 0, $this->report_data->total_refunded_tax ) ); |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - // Calculate average based on net. |
|
| 281 | - $this->report_data->average_sales = wpinv_round_amount( $this->report_data->net_sales / max( $this->interval, 1 ), 2 ); |
|
| 282 | - $this->report_data->average_total_sales = wpinv_round_amount( $this->report_data->total_sales / max( $this->interval, 1 ), 2 ); |
|
| 283 | - |
|
| 284 | - // Total invoices in this period, even if refunded. |
|
| 285 | - $this->report_data->total_invoices = absint( array_sum( wp_list_pluck( $this->report_data->invoice_counts, 'count' ) ) ); |
|
| 286 | - |
|
| 287 | - // Items invoiced in this period, even if refunded. |
|
| 288 | - $this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->invoice_items, 'invoice_item_count' ) ) ); |
|
| 289 | - |
|
| 290 | - // 3rd party filtering of report data |
|
| 291 | - $this->report_data = apply_filters( 'getpaid_rest_api_filter_report_data', $this->report_data ); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Prepares invoice counts. |
|
| 296 | - * |
|
| 297 | - * @return array. |
|
| 298 | - */ |
|
| 299 | - protected function query_invoice_counts() { |
|
| 300 | - |
|
| 301 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 302 | - array( |
|
| 303 | - 'data' => array( |
|
| 304 | - 'ID' => array( |
|
| 305 | - 'type' => 'post_data', |
|
| 306 | - 'function' => 'COUNT', |
|
| 307 | - 'name' => 'count', |
|
| 308 | - 'distinct' => true, |
|
| 309 | - ), |
|
| 310 | - 'post_date' => array( |
|
| 311 | - 'type' => 'post_data', |
|
| 312 | - 'function' => '', |
|
| 313 | - 'name' => 'post_date', |
|
| 314 | - ), |
|
| 315 | - ), |
|
| 316 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 317 | - 'order_by' => 'post_date ASC', |
|
| 318 | - 'query_type' => 'get_results', |
|
| 319 | - 'filter_range' => $this->report_range, |
|
| 320 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 321 | - ) |
|
| 322 | - ); |
|
| 323 | - |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Prepares coupon counts. |
|
| 328 | - * |
|
| 329 | - * @return array. |
|
| 330 | - */ |
|
| 331 | - protected function query_coupon_counts() { |
|
| 332 | - |
|
| 333 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 334 | - array( |
|
| 335 | - 'data' => array( |
|
| 336 | - 'discount' => array( |
|
| 337 | - 'type' => 'invoice_data', |
|
| 338 | - 'function' => 'SUM', |
|
| 339 | - 'name' => 'discount_amount', |
|
| 340 | - ), |
|
| 341 | - 'post_date' => array( |
|
| 342 | - 'type' => 'post_data', |
|
| 343 | - 'function' => '', |
|
| 344 | - 'name' => 'post_date', |
|
| 345 | - ), |
|
| 346 | - ), |
|
| 347 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 348 | - 'order_by' => 'post_date ASC', |
|
| 349 | - 'query_type' => 'get_results', |
|
| 350 | - 'filter_range' => $this->report_range, |
|
| 351 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 352 | - ) |
|
| 353 | - ); |
|
| 354 | - |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Prepares item counts. |
|
| 359 | - * |
|
| 360 | - * @return array. |
|
| 361 | - */ |
|
| 362 | - protected function query_item_counts() { |
|
| 363 | - |
|
| 364 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 365 | - array( |
|
| 366 | - 'data' => array( |
|
| 367 | - 'quantity' => array( |
|
| 368 | - 'type' => 'invoice_item', |
|
| 369 | - 'function' => 'SUM', |
|
| 370 | - 'name' => 'invoice_item_count', |
|
| 371 | - ), |
|
| 372 | - 'post_date' => array( |
|
| 373 | - 'type' => 'post_data', |
|
| 374 | - 'function' => '', |
|
| 375 | - 'name' => 'post_date', |
|
| 376 | - ), |
|
| 377 | - ), |
|
| 378 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 379 | - 'order_by' => 'post_date ASC', |
|
| 380 | - 'query_type' => 'get_results', |
|
| 381 | - 'filter_range' => $this->report_range, |
|
| 382 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 383 | - ) |
|
| 384 | - ); |
|
| 385 | - |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Prepares refunded item counts. |
|
| 390 | - * |
|
| 391 | - * @return array. |
|
| 392 | - */ |
|
| 393 | - protected function count_refunded_items() { |
|
| 394 | - |
|
| 395 | - return (int) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 396 | - array( |
|
| 397 | - 'data' => array( |
|
| 398 | - 'quantity' => array( |
|
| 399 | - 'type' => 'invoice_item', |
|
| 400 | - 'function' => 'SUM', |
|
| 401 | - 'name' => 'invoice_item_count', |
|
| 402 | - ), |
|
| 403 | - ), |
|
| 404 | - 'query_type' => 'get_var', |
|
| 405 | - 'filter_range' => $this->report_range, |
|
| 406 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
| 407 | - ) |
|
| 408 | - ); |
|
| 409 | - |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Prepares daily invoice totals. |
|
| 414 | - * |
|
| 415 | - * @return array. |
|
| 416 | - */ |
|
| 417 | - protected function query_invoice_totals() { |
|
| 418 | - |
|
| 419 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 420 | - array( |
|
| 421 | - 'data' => array( |
|
| 422 | - 'total' => array( |
|
| 423 | - 'type' => 'invoice_data', |
|
| 424 | - 'function' => 'SUM', |
|
| 425 | - 'name' => 'total_sales', |
|
| 426 | - ), |
|
| 427 | - 'tax' => array( |
|
| 428 | - 'type' => 'invoice_data', |
|
| 429 | - 'function' => 'SUM', |
|
| 430 | - 'name' => 'total_tax', |
|
| 431 | - ), |
|
| 432 | - 'discount' => array( |
|
| 433 | - 'type' => 'invoice_data', |
|
| 434 | - 'function' => 'SUM', |
|
| 435 | - 'name' => 'total_discount', |
|
| 436 | - ), |
|
| 437 | - 'fees_total' => array( |
|
| 438 | - 'type' => 'invoice_data', |
|
| 439 | - 'function' => 'SUM', |
|
| 440 | - 'name' => 'total_fees', |
|
| 441 | - ), |
|
| 442 | - 'subtotal' => array( |
|
| 443 | - 'type' => 'invoice_data', |
|
| 444 | - 'function' => 'SUM', |
|
| 445 | - 'name' => 'subtotal', |
|
| 446 | - ), |
|
| 447 | - 'post_date' => array( |
|
| 448 | - 'type' => 'post_data', |
|
| 449 | - 'function' => '', |
|
| 450 | - 'name' => 'post_date', |
|
| 451 | - ), |
|
| 452 | - ), |
|
| 453 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 454 | - 'order_by' => 'post_date ASC', |
|
| 455 | - 'query_type' => 'get_results', |
|
| 456 | - 'filter_range' => $this->report_range, |
|
| 457 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold' ), |
|
| 458 | - ) |
|
| 459 | - ); |
|
| 460 | - |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Prepares daily invoice totals. |
|
| 465 | - * |
|
| 466 | - * @return array. |
|
| 467 | - */ |
|
| 468 | - protected function query_refunded_totals() { |
|
| 469 | - |
|
| 470 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 471 | - array( |
|
| 472 | - 'data' => array( |
|
| 473 | - 'total' => array( |
|
| 474 | - 'type' => 'invoice_data', |
|
| 475 | - 'function' => 'SUM', |
|
| 476 | - 'name' => 'total_sales', |
|
| 477 | - ), |
|
| 478 | - 'tax' => array( |
|
| 479 | - 'type' => 'invoice_data', |
|
| 480 | - 'function' => 'SUM', |
|
| 481 | - 'name' => 'total_tax', |
|
| 482 | - ), |
|
| 483 | - 'discount' => array( |
|
| 484 | - 'type' => 'invoice_data', |
|
| 485 | - 'function' => 'SUM', |
|
| 486 | - 'name' => 'total_discount', |
|
| 487 | - ), |
|
| 488 | - 'fees_total' => array( |
|
| 489 | - 'type' => 'invoice_data', |
|
| 490 | - 'function' => 'SUM', |
|
| 491 | - 'name' => 'total_fees', |
|
| 492 | - ), |
|
| 493 | - 'subtotal' => array( |
|
| 494 | - 'type' => 'invoice_data', |
|
| 495 | - 'function' => 'SUM', |
|
| 496 | - 'name' => 'subtotal', |
|
| 497 | - ), |
|
| 498 | - 'post_date' => array( |
|
| 499 | - 'type' => 'post_data', |
|
| 500 | - 'function' => '', |
|
| 501 | - 'name' => 'post_date', |
|
| 502 | - ), |
|
| 503 | - ), |
|
| 504 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 505 | - 'order_by' => 'post_date ASC', |
|
| 506 | - 'query_type' => 'get_results', |
|
| 507 | - 'filter_range' => $this->report_range, |
|
| 508 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
| 509 | - ) |
|
| 510 | - ); |
|
| 511 | - |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Get the Report's schema, conforming to JSON Schema. |
|
| 516 | - * |
|
| 517 | - * @return array |
|
| 518 | - */ |
|
| 519 | - public function get_item_schema() { |
|
| 520 | - |
|
| 521 | - $schema = array( |
|
| 522 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 523 | - 'title' => 'sales_report', |
|
| 524 | - 'type' => 'object', |
|
| 525 | - 'properties' => array( |
|
| 526 | - 'total_sales' => array( |
|
| 527 | - 'description' => __( 'Gross sales in the period.', 'invoicing' ), |
|
| 528 | - 'type' => 'string', |
|
| 529 | - 'context' => array( 'view' ), |
|
| 530 | - 'readonly' => true, |
|
| 531 | - ), |
|
| 532 | - 'net_sales' => array( |
|
| 533 | - 'description' => __( 'Net sales in the period.', 'invoicing' ), |
|
| 534 | - 'type' => 'string', |
|
| 535 | - 'context' => array( 'view' ), |
|
| 536 | - 'readonly' => true, |
|
| 537 | - ), |
|
| 538 | - 'average_sales' => array( |
|
| 539 | - 'description' => __( 'Average net daily sales.', 'invoicing' ), |
|
| 540 | - 'type' => 'string', |
|
| 541 | - 'context' => array( 'view' ), |
|
| 542 | - 'readonly' => true, |
|
| 543 | - ), |
|
| 544 | - 'average_total_sales' => array( |
|
| 545 | - 'description' => __( 'Average gross daily sales.', 'invoicing' ), |
|
| 546 | - 'type' => 'string', |
|
| 547 | - 'context' => array( 'view' ), |
|
| 548 | - 'readonly' => true, |
|
| 549 | - ), |
|
| 550 | - 'total_invoices' => array( |
|
| 551 | - 'description' => __( 'Number of paid invoices.', 'invoicing' ), |
|
| 552 | - 'type' => 'integer', |
|
| 553 | - 'context' => array( 'view' ), |
|
| 554 | - 'readonly' => true, |
|
| 555 | - ), |
|
| 556 | - 'total_items' => array( |
|
| 557 | - 'description' => __( 'Number of items purchased.', 'invoicing' ), |
|
| 558 | - 'type' => 'integer', |
|
| 559 | - 'context' => array( 'view' ), |
|
| 560 | - 'readonly' => true, |
|
| 561 | - ), |
|
| 562 | - 'refunded_items' => array( |
|
| 563 | - 'description' => __( 'Number of items refunded.', 'invoicing' ), |
|
| 564 | - 'type' => 'integer', |
|
| 565 | - 'context' => array( 'view' ), |
|
| 566 | - 'readonly' => true, |
|
| 567 | - ), |
|
| 568 | - 'total_tax' => array( |
|
| 569 | - 'description' => __( 'Total charged for taxes.', 'invoicing' ), |
|
| 570 | - 'type' => 'string', |
|
| 571 | - 'context' => array( 'view' ), |
|
| 572 | - 'readonly' => true, |
|
| 573 | - ), |
|
| 574 | - 'total_refunded_tax' => array( |
|
| 575 | - 'description' => __( 'Total refunded for taxes.', 'invoicing' ), |
|
| 576 | - 'type' => 'string', |
|
| 577 | - 'context' => array( 'view' ), |
|
| 578 | - 'readonly' => true, |
|
| 579 | - ), |
|
| 580 | - 'total_fees' => array( |
|
| 581 | - 'description' => __( 'Total fees charged.', 'invoicing' ), |
|
| 582 | - 'type' => 'string', |
|
| 583 | - 'context' => array( 'view' ), |
|
| 584 | - 'readonly' => true, |
|
| 585 | - ), |
|
| 586 | - 'total_refunds' => array( |
|
| 587 | - 'description' => __( 'Total of refunded invoices.', 'invoicing' ), |
|
| 588 | - 'type' => 'integer', |
|
| 589 | - 'context' => array( 'view' ), |
|
| 590 | - 'readonly' => true, |
|
| 591 | - ), |
|
| 592 | - 'net_refunds' => array( |
|
| 593 | - 'description' => __( 'Net of refunded invoices.', 'invoicing' ), |
|
| 594 | - 'type' => 'integer', |
|
| 595 | - 'context' => array( 'view' ), |
|
| 596 | - 'readonly' => true, |
|
| 597 | - ), |
|
| 598 | - 'total_discount' => array( |
|
| 599 | - 'description' => __( 'Total of discounts used.', 'invoicing' ), |
|
| 600 | - 'type' => 'integer', |
|
| 601 | - 'context' => array( 'view' ), |
|
| 602 | - 'readonly' => true, |
|
| 603 | - ), |
|
| 604 | - 'totals' => array( |
|
| 605 | - 'description' => __( 'Totals.', 'invoicing' ), |
|
| 606 | - 'type' => 'array', |
|
| 607 | - 'items' => array( |
|
| 608 | - 'type' => 'array', |
|
| 609 | - ), |
|
| 610 | - 'context' => array( 'view' ), |
|
| 611 | - 'readonly' => true, |
|
| 612 | - ), |
|
| 613 | - 'interval' => array( |
|
| 614 | - 'description' => __( 'Number of months/days in the report period.', 'invoicing' ), |
|
| 615 | - 'type' => 'integer', |
|
| 616 | - 'context' => array( 'view' ), |
|
| 617 | - 'readonly' => true, |
|
| 618 | - ), |
|
| 619 | - 'grouped_by' => array( |
|
| 620 | - 'description' => __( 'The period used to group the totals.', 'invoicing' ), |
|
| 621 | - 'type' => 'string', |
|
| 622 | - 'context' => array( 'view' ), |
|
| 623 | - 'enum' => array( 'day', 'month' ), |
|
| 624 | - 'readonly' => true, |
|
| 625 | - ), |
|
| 626 | - 'currency' => array( |
|
| 627 | - 'description' => __( 'The default store currency.', 'invoicing' ), |
|
| 628 | - 'type' => 'string', |
|
| 629 | - 'context' => array( 'view' ), |
|
| 630 | - 'readonly' => true, |
|
| 631 | - ), |
|
| 632 | - 'currency_symbol' => array( |
|
| 633 | - 'description' => __( 'The default store currency symbol.', 'invoicing' ), |
|
| 634 | - 'type' => 'string', |
|
| 635 | - 'context' => array( 'view' ), |
|
| 636 | - 'readonly' => true, |
|
| 637 | - ), |
|
| 638 | - 'currency_position' => array( |
|
| 639 | - 'description' => __( 'The default store currency position.', 'invoicing' ), |
|
| 640 | - 'type' => 'string', |
|
| 641 | - 'context' => array( 'view' ), |
|
| 642 | - 'readonly' => true, |
|
| 643 | - ), |
|
| 644 | - 'decimal_places' => array( |
|
| 645 | - 'description' => __( 'The default store decimal places.', 'invoicing' ), |
|
| 646 | - 'type' => 'string', |
|
| 647 | - 'context' => array( 'view' ), |
|
| 648 | - 'readonly' => true, |
|
| 649 | - ), |
|
| 650 | - 'thousands_sep' => array( |
|
| 651 | - 'description' => __( 'The default store thousands separator.', 'invoicing' ), |
|
| 652 | - 'type' => 'string', |
|
| 653 | - 'context' => array( 'view' ), |
|
| 654 | - 'readonly' => true, |
|
| 655 | - ), |
|
| 656 | - 'decimals_sep' => array( |
|
| 657 | - 'description' => __( 'The default store decimals separator.', 'invoicing' ), |
|
| 658 | - 'type' => 'string', |
|
| 659 | - 'context' => array( 'view' ), |
|
| 660 | - 'readonly' => true, |
|
| 661 | - ), |
|
| 662 | - ), |
|
| 663 | - ); |
|
| 664 | - |
|
| 665 | - return $this->add_additional_fields_schema( $schema ); |
|
| 666 | - |
|
| 667 | - } |
|
| 21 | + /** |
|
| 22 | + * Route base. |
|
| 23 | + * |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $rest_base = 'reports/sales'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The report data. |
|
| 30 | + * |
|
| 31 | + * @var stdClass |
|
| 32 | + */ |
|
| 33 | + protected $report_data; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The report range. |
|
| 37 | + * |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + protected $report_range; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Registers the routes for the objects of the controller. |
|
| 44 | + * |
|
| 45 | + * @since 2.0.0 |
|
| 46 | + * |
|
| 47 | + * @see register_rest_route() |
|
| 48 | + */ |
|
| 49 | + public function register_namespace_routes( $namespace ) { |
|
| 50 | + |
|
| 51 | + // Get sales report. |
|
| 52 | + register_rest_route( |
|
| 53 | + $namespace, |
|
| 54 | + $this->rest_base, |
|
| 55 | + array( |
|
| 56 | + array( |
|
| 57 | + 'methods' => WP_REST_Server::READABLE, |
|
| 58 | + 'callback' => array( $this, 'get_items' ), |
|
| 59 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 60 | + 'args' => $this->get_collection_params(), |
|
| 61 | + ), |
|
| 62 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 63 | + ) |
|
| 64 | + ); |
|
| 65 | + |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Makes sure the current user has access to READ the report APIs. |
|
| 70 | + * |
|
| 71 | + * @since 2.0.0 |
|
| 72 | + * @param WP_REST_Request $request Full data about the request. |
|
| 73 | + * @return WP_Error|boolean |
|
| 74 | + */ |
|
| 75 | + public function get_items_permissions_check( $request ) { |
|
| 76 | + |
|
| 77 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 78 | + return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return true; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Get sales reports. |
|
| 86 | + * |
|
| 87 | + * @param WP_REST_Request $request |
|
| 88 | + * @return array|WP_Error |
|
| 89 | + */ |
|
| 90 | + public function get_items( $request ) { |
|
| 91 | + $data = array(); |
|
| 92 | + $item = $this->prepare_item_for_response( null, $request ); |
|
| 93 | + $data[] = $this->prepare_response_for_collection( $item ); |
|
| 94 | + |
|
| 95 | + return rest_ensure_response( $data ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Prepare a report sales object for serialization. |
|
| 100 | + * |
|
| 101 | + * @param null $_ |
|
| 102 | + * @param WP_REST_Request $request Request object. |
|
| 103 | + * @return WP_REST_Response $response Response data. |
|
| 104 | + */ |
|
| 105 | + public function prepare_item_for_response( $_, $request ) { |
|
| 106 | + |
|
| 107 | + // Set report range. |
|
| 108 | + $this->report_range = $this->get_date_range( $request ); |
|
| 109 | + |
|
| 110 | + $report_data = $this->get_report_data(); |
|
| 111 | + $period_totals = array(); |
|
| 112 | + |
|
| 113 | + // Setup period totals by ensuring each period in the interval has data. |
|
| 114 | + $start_date = strtotime( $this->report_range['after'] ) + DAY_IN_SECONDS; |
|
| 115 | + |
|
| 116 | + if ( 'month' === $this->groupby ) { |
|
| 117 | + $start_date = strtotime( date( 'Y-m-01', $start_date ) ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + for ( $i = 0; $i < $this->interval; $i++ ) { |
|
| 121 | + |
|
| 122 | + switch ( $this->groupby ) { |
|
| 123 | + case 'day' : |
|
| 124 | + $time = date( 'Y-m-d', strtotime( "+{$i} DAY", $start_date ) ); |
|
| 125 | + break; |
|
| 126 | + default : |
|
| 127 | + $time = date( 'Y-m', strtotime( "+{$i} MONTH", $start_date ) ); |
|
| 128 | + break; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // Set the defaults for each period. |
|
| 132 | + $period_totals[ $time ] = array( |
|
| 133 | + 'sales' => wpinv_round_amount( 0.00 ), |
|
| 134 | + 'invoices' => 0, |
|
| 135 | + 'refunds' => wpinv_round_amount( 0.00 ), |
|
| 136 | + 'items' => 0, |
|
| 137 | + 'refunded_items' => 0, |
|
| 138 | + 'tax' => wpinv_round_amount( 0.00 ), |
|
| 139 | + 'refunded_tax' => wpinv_round_amount( 0.00 ), |
|
| 140 | + 'subtotal' => wpinv_round_amount( 0.00 ), |
|
| 141 | + 'refunded_subtotal' => wpinv_round_amount( 0.00 ), |
|
| 142 | + 'fees' => wpinv_round_amount( 0.00 ), |
|
| 143 | + 'refunded_fees' => wpinv_round_amount( 0.00 ), |
|
| 144 | + 'discount' => wpinv_round_amount( 0.00 ), |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // add total sales, total invoice count, total tax for each period |
|
| 150 | + $date_format = ( 'day' === $this->groupby ) ? 'Y-m-d' : 'Y-m'; |
|
| 151 | + foreach ( $report_data->invoices as $invoice ) { |
|
| 152 | + $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 153 | + |
|
| 154 | + if ( ! isset( $period_totals[ $time ] ) ) { |
|
| 155 | + continue; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $period_totals[ $time ]['sales'] = wpinv_round_amount( $invoice->total_sales ); |
|
| 159 | + $period_totals[ $time ]['tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
| 160 | + $period_totals[ $time ]['subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
| 161 | + $period_totals[ $time ]['fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + foreach ( $report_data->refunds as $invoice ) { |
|
| 166 | + $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 167 | + |
|
| 168 | + if ( ! isset( $period_totals[ $time ] ) ) { |
|
| 169 | + continue; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $period_totals[ $time ]['refunds'] = wpinv_round_amount( $invoice->total_sales ); |
|
| 173 | + $period_totals[ $time ]['refunded_tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
| 174 | + $period_totals[ $time ]['refunded_subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
| 175 | + $period_totals[ $time ]['refunded_fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
| 176 | + |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + foreach ( $report_data->invoice_counts as $invoice ) { |
|
| 180 | + $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 181 | + |
|
| 182 | + if ( isset( $period_totals[ $time ] ) ) { |
|
| 183 | + $period_totals[ $time ]['invoices'] = (int) $invoice->count; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // Add total invoice items for each period. |
|
| 189 | + foreach ( $report_data->invoice_items as $invoice_item ) { |
|
| 190 | + $time = ( 'day' === $this->groupby ) ? date( 'Y-m-d', strtotime( $invoice_item->post_date ) ) : date( 'Y-m', strtotime( $invoice_item->post_date ) ); |
|
| 191 | + |
|
| 192 | + if ( isset( $period_totals[ $time ] ) ) { |
|
| 193 | + $period_totals[ $time ]['items'] = (int) $invoice_item->invoice_item_count; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + // Add total discount for each period. |
|
| 199 | + foreach ( $report_data->coupons as $discount ) { |
|
| 200 | + $time = ( 'day' === $this->groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) ); |
|
| 201 | + |
|
| 202 | + if ( isset( $period_totals[ $time ] ) ) { |
|
| 203 | + $period_totals[ $time ]['discount'] = wpinv_round_amount( $discount->discount_amount ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + $report_data->totals = $period_totals; |
|
| 209 | + $report_data->grouped_by = $this->groupby; |
|
| 210 | + $report_data->interval = max( $this->interval, 1 ); |
|
| 211 | + $report_data->currency = wpinv_get_currency(); |
|
| 212 | + $report_data->currency_symbol = wpinv_currency_symbol(); |
|
| 213 | + $report_data->currency_position = wpinv_currency_position(); |
|
| 214 | + $report_data->decimal_places = wpinv_decimals(); |
|
| 215 | + $report_data->thousands_sep = wpinv_thousands_separator(); |
|
| 216 | + $report_data->decimals_sep = wpinv_decimal_separator(); |
|
| 217 | + $report_data->start_date = getpaid_format_date( date( 'Y-m-d', strtotime( $this->report_range['after'] ) + DAY_IN_SECONDS ) ); |
|
| 218 | + $report_data->end_date = getpaid_format_date( date( 'Y-m-d', strtotime( $this->report_range['before'] ) - DAY_IN_SECONDS ) ); |
|
| 219 | + $report_data->decimals_sep = wpinv_decimal_separator(); |
|
| 220 | + |
|
| 221 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 222 | + $data = $report_data; |
|
| 223 | + unset( $data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items ); |
|
| 224 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 225 | + $data = $this->filter_response_by_context( $data, $context ); |
|
| 226 | + |
|
| 227 | + // Wrap the data in a response object. |
|
| 228 | + $response = rest_ensure_response( $data ); |
|
| 229 | + $response->add_links( array( |
|
| 230 | + 'about' => array( |
|
| 231 | + 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
| 232 | + ), |
|
| 233 | + ) ); |
|
| 234 | + |
|
| 235 | + return apply_filters( 'getpaid_rest_prepare_report_sales', $response, $report_data, $request ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Get report data. |
|
| 240 | + * |
|
| 241 | + * @return stdClass |
|
| 242 | + */ |
|
| 243 | + public function get_report_data() { |
|
| 244 | + if ( empty( $this->report_data ) ) { |
|
| 245 | + $this->query_report_data(); |
|
| 246 | + } |
|
| 247 | + return $this->report_data; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get all data needed for this report and store in the class. |
|
| 252 | + */ |
|
| 253 | + protected function query_report_data() { |
|
| 254 | + |
|
| 255 | + // Prepare reports. |
|
| 256 | + $this->report_data = (object) array( |
|
| 257 | + 'invoice_counts' => $this->query_invoice_counts(),//count, post_date |
|
| 258 | + 'coupons' => $this->query_coupon_counts(), // discount_amount, post_date |
|
| 259 | + 'invoice_items' => $this->query_item_counts(), // invoice_item_count, post_date |
|
| 260 | + 'refunded_items' => $this->count_refunded_items(), // invoice_item_count, post_date |
|
| 261 | + 'invoices' => $this->query_invoice_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
| 262 | + 'refunds' => $this->query_refunded_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
| 263 | + ); |
|
| 264 | + |
|
| 265 | + // Calculated totals. |
|
| 266 | + $this->report_data->total_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_tax' ) ) ); |
|
| 267 | + $this->report_data->total_sales = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_sales' ) ) ); |
|
| 268 | + $this->report_data->total_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_discount' ) ) ); |
|
| 269 | + $this->report_data->total_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_fees' ) ) ); |
|
| 270 | + $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'subtotal' ) ) ); |
|
| 271 | + $this->report_data->net_sales = wpinv_round_amount( $this->report_data->total_sales - max( 0, $this->report_data->total_tax ) ); |
|
| 272 | + $this->report_data->total_refunded_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_tax' ) ) ); |
|
| 273 | + $this->report_data->total_refunds = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_sales' ) ) ); |
|
| 274 | + $this->report_data->refunded_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_discount' ) ) ); |
|
| 275 | + $this->report_data->refunded_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_fees' ) ) ); |
|
| 276 | + $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'subtotal' ) ) ); |
|
| 277 | + $this->report_data->net_refunds = wpinv_round_amount( $this->report_data->total_refunds + max( 0, $this->report_data->total_refunded_tax ) ); |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + // Calculate average based on net. |
|
| 281 | + $this->report_data->average_sales = wpinv_round_amount( $this->report_data->net_sales / max( $this->interval, 1 ), 2 ); |
|
| 282 | + $this->report_data->average_total_sales = wpinv_round_amount( $this->report_data->total_sales / max( $this->interval, 1 ), 2 ); |
|
| 283 | + |
|
| 284 | + // Total invoices in this period, even if refunded. |
|
| 285 | + $this->report_data->total_invoices = absint( array_sum( wp_list_pluck( $this->report_data->invoice_counts, 'count' ) ) ); |
|
| 286 | + |
|
| 287 | + // Items invoiced in this period, even if refunded. |
|
| 288 | + $this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->invoice_items, 'invoice_item_count' ) ) ); |
|
| 289 | + |
|
| 290 | + // 3rd party filtering of report data |
|
| 291 | + $this->report_data = apply_filters( 'getpaid_rest_api_filter_report_data', $this->report_data ); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Prepares invoice counts. |
|
| 296 | + * |
|
| 297 | + * @return array. |
|
| 298 | + */ |
|
| 299 | + protected function query_invoice_counts() { |
|
| 300 | + |
|
| 301 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 302 | + array( |
|
| 303 | + 'data' => array( |
|
| 304 | + 'ID' => array( |
|
| 305 | + 'type' => 'post_data', |
|
| 306 | + 'function' => 'COUNT', |
|
| 307 | + 'name' => 'count', |
|
| 308 | + 'distinct' => true, |
|
| 309 | + ), |
|
| 310 | + 'post_date' => array( |
|
| 311 | + 'type' => 'post_data', |
|
| 312 | + 'function' => '', |
|
| 313 | + 'name' => 'post_date', |
|
| 314 | + ), |
|
| 315 | + ), |
|
| 316 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 317 | + 'order_by' => 'post_date ASC', |
|
| 318 | + 'query_type' => 'get_results', |
|
| 319 | + 'filter_range' => $this->report_range, |
|
| 320 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 321 | + ) |
|
| 322 | + ); |
|
| 323 | + |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Prepares coupon counts. |
|
| 328 | + * |
|
| 329 | + * @return array. |
|
| 330 | + */ |
|
| 331 | + protected function query_coupon_counts() { |
|
| 332 | + |
|
| 333 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 334 | + array( |
|
| 335 | + 'data' => array( |
|
| 336 | + 'discount' => array( |
|
| 337 | + 'type' => 'invoice_data', |
|
| 338 | + 'function' => 'SUM', |
|
| 339 | + 'name' => 'discount_amount', |
|
| 340 | + ), |
|
| 341 | + 'post_date' => array( |
|
| 342 | + 'type' => 'post_data', |
|
| 343 | + 'function' => '', |
|
| 344 | + 'name' => 'post_date', |
|
| 345 | + ), |
|
| 346 | + ), |
|
| 347 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 348 | + 'order_by' => 'post_date ASC', |
|
| 349 | + 'query_type' => 'get_results', |
|
| 350 | + 'filter_range' => $this->report_range, |
|
| 351 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 352 | + ) |
|
| 353 | + ); |
|
| 354 | + |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Prepares item counts. |
|
| 359 | + * |
|
| 360 | + * @return array. |
|
| 361 | + */ |
|
| 362 | + protected function query_item_counts() { |
|
| 363 | + |
|
| 364 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 365 | + array( |
|
| 366 | + 'data' => array( |
|
| 367 | + 'quantity' => array( |
|
| 368 | + 'type' => 'invoice_item', |
|
| 369 | + 'function' => 'SUM', |
|
| 370 | + 'name' => 'invoice_item_count', |
|
| 371 | + ), |
|
| 372 | + 'post_date' => array( |
|
| 373 | + 'type' => 'post_data', |
|
| 374 | + 'function' => '', |
|
| 375 | + 'name' => 'post_date', |
|
| 376 | + ), |
|
| 377 | + ), |
|
| 378 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 379 | + 'order_by' => 'post_date ASC', |
|
| 380 | + 'query_type' => 'get_results', |
|
| 381 | + 'filter_range' => $this->report_range, |
|
| 382 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 383 | + ) |
|
| 384 | + ); |
|
| 385 | + |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Prepares refunded item counts. |
|
| 390 | + * |
|
| 391 | + * @return array. |
|
| 392 | + */ |
|
| 393 | + protected function count_refunded_items() { |
|
| 394 | + |
|
| 395 | + return (int) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 396 | + array( |
|
| 397 | + 'data' => array( |
|
| 398 | + 'quantity' => array( |
|
| 399 | + 'type' => 'invoice_item', |
|
| 400 | + 'function' => 'SUM', |
|
| 401 | + 'name' => 'invoice_item_count', |
|
| 402 | + ), |
|
| 403 | + ), |
|
| 404 | + 'query_type' => 'get_var', |
|
| 405 | + 'filter_range' => $this->report_range, |
|
| 406 | + 'invoice_status' => array( 'wpi-refunded' ), |
|
| 407 | + ) |
|
| 408 | + ); |
|
| 409 | + |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Prepares daily invoice totals. |
|
| 414 | + * |
|
| 415 | + * @return array. |
|
| 416 | + */ |
|
| 417 | + protected function query_invoice_totals() { |
|
| 418 | + |
|
| 419 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 420 | + array( |
|
| 421 | + 'data' => array( |
|
| 422 | + 'total' => array( |
|
| 423 | + 'type' => 'invoice_data', |
|
| 424 | + 'function' => 'SUM', |
|
| 425 | + 'name' => 'total_sales', |
|
| 426 | + ), |
|
| 427 | + 'tax' => array( |
|
| 428 | + 'type' => 'invoice_data', |
|
| 429 | + 'function' => 'SUM', |
|
| 430 | + 'name' => 'total_tax', |
|
| 431 | + ), |
|
| 432 | + 'discount' => array( |
|
| 433 | + 'type' => 'invoice_data', |
|
| 434 | + 'function' => 'SUM', |
|
| 435 | + 'name' => 'total_discount', |
|
| 436 | + ), |
|
| 437 | + 'fees_total' => array( |
|
| 438 | + 'type' => 'invoice_data', |
|
| 439 | + 'function' => 'SUM', |
|
| 440 | + 'name' => 'total_fees', |
|
| 441 | + ), |
|
| 442 | + 'subtotal' => array( |
|
| 443 | + 'type' => 'invoice_data', |
|
| 444 | + 'function' => 'SUM', |
|
| 445 | + 'name' => 'subtotal', |
|
| 446 | + ), |
|
| 447 | + 'post_date' => array( |
|
| 448 | + 'type' => 'post_data', |
|
| 449 | + 'function' => '', |
|
| 450 | + 'name' => 'post_date', |
|
| 451 | + ), |
|
| 452 | + ), |
|
| 453 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 454 | + 'order_by' => 'post_date ASC', |
|
| 455 | + 'query_type' => 'get_results', |
|
| 456 | + 'filter_range' => $this->report_range, |
|
| 457 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold' ), |
|
| 458 | + ) |
|
| 459 | + ); |
|
| 460 | + |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Prepares daily invoice totals. |
|
| 465 | + * |
|
| 466 | + * @return array. |
|
| 467 | + */ |
|
| 468 | + protected function query_refunded_totals() { |
|
| 469 | + |
|
| 470 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 471 | + array( |
|
| 472 | + 'data' => array( |
|
| 473 | + 'total' => array( |
|
| 474 | + 'type' => 'invoice_data', |
|
| 475 | + 'function' => 'SUM', |
|
| 476 | + 'name' => 'total_sales', |
|
| 477 | + ), |
|
| 478 | + 'tax' => array( |
|
| 479 | + 'type' => 'invoice_data', |
|
| 480 | + 'function' => 'SUM', |
|
| 481 | + 'name' => 'total_tax', |
|
| 482 | + ), |
|
| 483 | + 'discount' => array( |
|
| 484 | + 'type' => 'invoice_data', |
|
| 485 | + 'function' => 'SUM', |
|
| 486 | + 'name' => 'total_discount', |
|
| 487 | + ), |
|
| 488 | + 'fees_total' => array( |
|
| 489 | + 'type' => 'invoice_data', |
|
| 490 | + 'function' => 'SUM', |
|
| 491 | + 'name' => 'total_fees', |
|
| 492 | + ), |
|
| 493 | + 'subtotal' => array( |
|
| 494 | + 'type' => 'invoice_data', |
|
| 495 | + 'function' => 'SUM', |
|
| 496 | + 'name' => 'subtotal', |
|
| 497 | + ), |
|
| 498 | + 'post_date' => array( |
|
| 499 | + 'type' => 'post_data', |
|
| 500 | + 'function' => '', |
|
| 501 | + 'name' => 'post_date', |
|
| 502 | + ), |
|
| 503 | + ), |
|
| 504 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 505 | + 'order_by' => 'post_date ASC', |
|
| 506 | + 'query_type' => 'get_results', |
|
| 507 | + 'filter_range' => $this->report_range, |
|
| 508 | + 'invoice_status' => array( 'wpi-refunded' ), |
|
| 509 | + ) |
|
| 510 | + ); |
|
| 511 | + |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Get the Report's schema, conforming to JSON Schema. |
|
| 516 | + * |
|
| 517 | + * @return array |
|
| 518 | + */ |
|
| 519 | + public function get_item_schema() { |
|
| 520 | + |
|
| 521 | + $schema = array( |
|
| 522 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 523 | + 'title' => 'sales_report', |
|
| 524 | + 'type' => 'object', |
|
| 525 | + 'properties' => array( |
|
| 526 | + 'total_sales' => array( |
|
| 527 | + 'description' => __( 'Gross sales in the period.', 'invoicing' ), |
|
| 528 | + 'type' => 'string', |
|
| 529 | + 'context' => array( 'view' ), |
|
| 530 | + 'readonly' => true, |
|
| 531 | + ), |
|
| 532 | + 'net_sales' => array( |
|
| 533 | + 'description' => __( 'Net sales in the period.', 'invoicing' ), |
|
| 534 | + 'type' => 'string', |
|
| 535 | + 'context' => array( 'view' ), |
|
| 536 | + 'readonly' => true, |
|
| 537 | + ), |
|
| 538 | + 'average_sales' => array( |
|
| 539 | + 'description' => __( 'Average net daily sales.', 'invoicing' ), |
|
| 540 | + 'type' => 'string', |
|
| 541 | + 'context' => array( 'view' ), |
|
| 542 | + 'readonly' => true, |
|
| 543 | + ), |
|
| 544 | + 'average_total_sales' => array( |
|
| 545 | + 'description' => __( 'Average gross daily sales.', 'invoicing' ), |
|
| 546 | + 'type' => 'string', |
|
| 547 | + 'context' => array( 'view' ), |
|
| 548 | + 'readonly' => true, |
|
| 549 | + ), |
|
| 550 | + 'total_invoices' => array( |
|
| 551 | + 'description' => __( 'Number of paid invoices.', 'invoicing' ), |
|
| 552 | + 'type' => 'integer', |
|
| 553 | + 'context' => array( 'view' ), |
|
| 554 | + 'readonly' => true, |
|
| 555 | + ), |
|
| 556 | + 'total_items' => array( |
|
| 557 | + 'description' => __( 'Number of items purchased.', 'invoicing' ), |
|
| 558 | + 'type' => 'integer', |
|
| 559 | + 'context' => array( 'view' ), |
|
| 560 | + 'readonly' => true, |
|
| 561 | + ), |
|
| 562 | + 'refunded_items' => array( |
|
| 563 | + 'description' => __( 'Number of items refunded.', 'invoicing' ), |
|
| 564 | + 'type' => 'integer', |
|
| 565 | + 'context' => array( 'view' ), |
|
| 566 | + 'readonly' => true, |
|
| 567 | + ), |
|
| 568 | + 'total_tax' => array( |
|
| 569 | + 'description' => __( 'Total charged for taxes.', 'invoicing' ), |
|
| 570 | + 'type' => 'string', |
|
| 571 | + 'context' => array( 'view' ), |
|
| 572 | + 'readonly' => true, |
|
| 573 | + ), |
|
| 574 | + 'total_refunded_tax' => array( |
|
| 575 | + 'description' => __( 'Total refunded for taxes.', 'invoicing' ), |
|
| 576 | + 'type' => 'string', |
|
| 577 | + 'context' => array( 'view' ), |
|
| 578 | + 'readonly' => true, |
|
| 579 | + ), |
|
| 580 | + 'total_fees' => array( |
|
| 581 | + 'description' => __( 'Total fees charged.', 'invoicing' ), |
|
| 582 | + 'type' => 'string', |
|
| 583 | + 'context' => array( 'view' ), |
|
| 584 | + 'readonly' => true, |
|
| 585 | + ), |
|
| 586 | + 'total_refunds' => array( |
|
| 587 | + 'description' => __( 'Total of refunded invoices.', 'invoicing' ), |
|
| 588 | + 'type' => 'integer', |
|
| 589 | + 'context' => array( 'view' ), |
|
| 590 | + 'readonly' => true, |
|
| 591 | + ), |
|
| 592 | + 'net_refunds' => array( |
|
| 593 | + 'description' => __( 'Net of refunded invoices.', 'invoicing' ), |
|
| 594 | + 'type' => 'integer', |
|
| 595 | + 'context' => array( 'view' ), |
|
| 596 | + 'readonly' => true, |
|
| 597 | + ), |
|
| 598 | + 'total_discount' => array( |
|
| 599 | + 'description' => __( 'Total of discounts used.', 'invoicing' ), |
|
| 600 | + 'type' => 'integer', |
|
| 601 | + 'context' => array( 'view' ), |
|
| 602 | + 'readonly' => true, |
|
| 603 | + ), |
|
| 604 | + 'totals' => array( |
|
| 605 | + 'description' => __( 'Totals.', 'invoicing' ), |
|
| 606 | + 'type' => 'array', |
|
| 607 | + 'items' => array( |
|
| 608 | + 'type' => 'array', |
|
| 609 | + ), |
|
| 610 | + 'context' => array( 'view' ), |
|
| 611 | + 'readonly' => true, |
|
| 612 | + ), |
|
| 613 | + 'interval' => array( |
|
| 614 | + 'description' => __( 'Number of months/days in the report period.', 'invoicing' ), |
|
| 615 | + 'type' => 'integer', |
|
| 616 | + 'context' => array( 'view' ), |
|
| 617 | + 'readonly' => true, |
|
| 618 | + ), |
|
| 619 | + 'grouped_by' => array( |
|
| 620 | + 'description' => __( 'The period used to group the totals.', 'invoicing' ), |
|
| 621 | + 'type' => 'string', |
|
| 622 | + 'context' => array( 'view' ), |
|
| 623 | + 'enum' => array( 'day', 'month' ), |
|
| 624 | + 'readonly' => true, |
|
| 625 | + ), |
|
| 626 | + 'currency' => array( |
|
| 627 | + 'description' => __( 'The default store currency.', 'invoicing' ), |
|
| 628 | + 'type' => 'string', |
|
| 629 | + 'context' => array( 'view' ), |
|
| 630 | + 'readonly' => true, |
|
| 631 | + ), |
|
| 632 | + 'currency_symbol' => array( |
|
| 633 | + 'description' => __( 'The default store currency symbol.', 'invoicing' ), |
|
| 634 | + 'type' => 'string', |
|
| 635 | + 'context' => array( 'view' ), |
|
| 636 | + 'readonly' => true, |
|
| 637 | + ), |
|
| 638 | + 'currency_position' => array( |
|
| 639 | + 'description' => __( 'The default store currency position.', 'invoicing' ), |
|
| 640 | + 'type' => 'string', |
|
| 641 | + 'context' => array( 'view' ), |
|
| 642 | + 'readonly' => true, |
|
| 643 | + ), |
|
| 644 | + 'decimal_places' => array( |
|
| 645 | + 'description' => __( 'The default store decimal places.', 'invoicing' ), |
|
| 646 | + 'type' => 'string', |
|
| 647 | + 'context' => array( 'view' ), |
|
| 648 | + 'readonly' => true, |
|
| 649 | + ), |
|
| 650 | + 'thousands_sep' => array( |
|
| 651 | + 'description' => __( 'The default store thousands separator.', 'invoicing' ), |
|
| 652 | + 'type' => 'string', |
|
| 653 | + 'context' => array( 'view' ), |
|
| 654 | + 'readonly' => true, |
|
| 655 | + ), |
|
| 656 | + 'decimals_sep' => array( |
|
| 657 | + 'description' => __( 'The default store decimals separator.', 'invoicing' ), |
|
| 658 | + 'type' => 'string', |
|
| 659 | + 'context' => array( 'view' ), |
|
| 660 | + 'readonly' => true, |
|
| 661 | + ), |
|
| 662 | + ), |
|
| 663 | + ); |
|
| 664 | + |
|
| 665 | + return $this->add_additional_fields_schema( $schema ); |
|
| 666 | + |
|
| 667 | + } |
|
| 668 | 668 | |
| 669 | 669 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | * @since 2.0.0 |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | -defined( 'ABSPATH' ) || exit; |
|
| 12 | +defined('ABSPATH') || exit; |
|
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * GetPaid REST reports controller class. |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @see register_rest_route() |
| 48 | 48 | */ |
| 49 | - public function register_namespace_routes( $namespace ) { |
|
| 49 | + public function register_namespace_routes($namespace) { |
|
| 50 | 50 | |
| 51 | 51 | // Get sales report. |
| 52 | 52 | register_rest_route( |
@@ -55,11 +55,11 @@ discard block |
||
| 55 | 55 | array( |
| 56 | 56 | array( |
| 57 | 57 | 'methods' => WP_REST_Server::READABLE, |
| 58 | - 'callback' => array( $this, 'get_items' ), |
|
| 59 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 58 | + 'callback' => array($this, 'get_items'), |
|
| 59 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
| 60 | 60 | 'args' => $this->get_collection_params(), |
| 61 | 61 | ), |
| 62 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 62 | + 'schema' => array($this, 'get_public_item_schema'), |
|
| 63 | 63 | ) |
| 64 | 64 | ); |
| 65 | 65 | |
@@ -72,10 +72,10 @@ discard block |
||
| 72 | 72 | * @param WP_REST_Request $request Full data about the request. |
| 73 | 73 | * @return WP_Error|boolean |
| 74 | 74 | */ |
| 75 | - public function get_items_permissions_check( $request ) { |
|
| 75 | + public function get_items_permissions_check($request) { |
|
| 76 | 76 | |
| 77 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 78 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 77 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 78 | + return new WP_Error('rest_cannot_view', __('Sorry, you cannot list resources.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | return true; |
@@ -87,12 +87,12 @@ discard block |
||
| 87 | 87 | * @param WP_REST_Request $request |
| 88 | 88 | * @return array|WP_Error |
| 89 | 89 | */ |
| 90 | - public function get_items( $request ) { |
|
| 90 | + public function get_items($request) { |
|
| 91 | 91 | $data = array(); |
| 92 | - $item = $this->prepare_item_for_response( null, $request ); |
|
| 93 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
| 92 | + $item = $this->prepare_item_for_response(null, $request); |
|
| 93 | + $data[] = $this->prepare_response_for_collection($item); |
|
| 94 | 94 | |
| 95 | - return rest_ensure_response( $data ); |
|
| 95 | + return rest_ensure_response($data); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -102,137 +102,137 @@ discard block |
||
| 102 | 102 | * @param WP_REST_Request $request Request object. |
| 103 | 103 | * @return WP_REST_Response $response Response data. |
| 104 | 104 | */ |
| 105 | - public function prepare_item_for_response( $_, $request ) { |
|
| 105 | + public function prepare_item_for_response($_, $request) { |
|
| 106 | 106 | |
| 107 | 107 | // Set report range. |
| 108 | - $this->report_range = $this->get_date_range( $request ); |
|
| 108 | + $this->report_range = $this->get_date_range($request); |
|
| 109 | 109 | |
| 110 | 110 | $report_data = $this->get_report_data(); |
| 111 | 111 | $period_totals = array(); |
| 112 | 112 | |
| 113 | 113 | // Setup period totals by ensuring each period in the interval has data. |
| 114 | - $start_date = strtotime( $this->report_range['after'] ) + DAY_IN_SECONDS; |
|
| 114 | + $start_date = strtotime($this->report_range['after']) + DAY_IN_SECONDS; |
|
| 115 | 115 | |
| 116 | - if ( 'month' === $this->groupby ) { |
|
| 117 | - $start_date = strtotime( date( 'Y-m-01', $start_date ) ); |
|
| 116 | + if ('month' === $this->groupby) { |
|
| 117 | + $start_date = strtotime(date('Y-m-01', $start_date)); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - for ( $i = 0; $i < $this->interval; $i++ ) { |
|
| 120 | + for ($i = 0; $i < $this->interval; $i++) { |
|
| 121 | 121 | |
| 122 | - switch ( $this->groupby ) { |
|
| 122 | + switch ($this->groupby) { |
|
| 123 | 123 | case 'day' : |
| 124 | - $time = date( 'Y-m-d', strtotime( "+{$i} DAY", $start_date ) ); |
|
| 124 | + $time = date('Y-m-d', strtotime("+{$i} DAY", $start_date)); |
|
| 125 | 125 | break; |
| 126 | 126 | default : |
| 127 | - $time = date( 'Y-m', strtotime( "+{$i} MONTH", $start_date ) ); |
|
| 127 | + $time = date('Y-m', strtotime("+{$i} MONTH", $start_date)); |
|
| 128 | 128 | break; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // Set the defaults for each period. |
| 132 | - $period_totals[ $time ] = array( |
|
| 133 | - 'sales' => wpinv_round_amount( 0.00 ), |
|
| 132 | + $period_totals[$time] = array( |
|
| 133 | + 'sales' => wpinv_round_amount(0.00), |
|
| 134 | 134 | 'invoices' => 0, |
| 135 | - 'refunds' => wpinv_round_amount( 0.00 ), |
|
| 135 | + 'refunds' => wpinv_round_amount(0.00), |
|
| 136 | 136 | 'items' => 0, |
| 137 | 137 | 'refunded_items' => 0, |
| 138 | - 'tax' => wpinv_round_amount( 0.00 ), |
|
| 139 | - 'refunded_tax' => wpinv_round_amount( 0.00 ), |
|
| 140 | - 'subtotal' => wpinv_round_amount( 0.00 ), |
|
| 141 | - 'refunded_subtotal' => wpinv_round_amount( 0.00 ), |
|
| 142 | - 'fees' => wpinv_round_amount( 0.00 ), |
|
| 143 | - 'refunded_fees' => wpinv_round_amount( 0.00 ), |
|
| 144 | - 'discount' => wpinv_round_amount( 0.00 ), |
|
| 138 | + 'tax' => wpinv_round_amount(0.00), |
|
| 139 | + 'refunded_tax' => wpinv_round_amount(0.00), |
|
| 140 | + 'subtotal' => wpinv_round_amount(0.00), |
|
| 141 | + 'refunded_subtotal' => wpinv_round_amount(0.00), |
|
| 142 | + 'fees' => wpinv_round_amount(0.00), |
|
| 143 | + 'refunded_fees' => wpinv_round_amount(0.00), |
|
| 144 | + 'discount' => wpinv_round_amount(0.00), |
|
| 145 | 145 | ); |
| 146 | 146 | |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | // add total sales, total invoice count, total tax for each period |
| 150 | - $date_format = ( 'day' === $this->groupby ) ? 'Y-m-d' : 'Y-m'; |
|
| 151 | - foreach ( $report_data->invoices as $invoice ) { |
|
| 152 | - $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 150 | + $date_format = ('day' === $this->groupby) ? 'Y-m-d' : 'Y-m'; |
|
| 151 | + foreach ($report_data->invoices as $invoice) { |
|
| 152 | + $time = date($date_format, strtotime($invoice->post_date)); |
|
| 153 | 153 | |
| 154 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
| 154 | + if (!isset($period_totals[$time])) { |
|
| 155 | 155 | continue; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - $period_totals[ $time ]['sales'] = wpinv_round_amount( $invoice->total_sales ); |
|
| 159 | - $period_totals[ $time ]['tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
| 160 | - $period_totals[ $time ]['subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
| 161 | - $period_totals[ $time ]['fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
| 158 | + $period_totals[$time]['sales'] = wpinv_round_amount($invoice->total_sales); |
|
| 159 | + $period_totals[$time]['tax'] = wpinv_round_amount($invoice->total_tax); |
|
| 160 | + $period_totals[$time]['subtotal'] = wpinv_round_amount($invoice->subtotal); |
|
| 161 | + $period_totals[$time]['fees'] = wpinv_round_amount($invoice->total_fees); |
|
| 162 | 162 | |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - foreach ( $report_data->refunds as $invoice ) { |
|
| 166 | - $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 165 | + foreach ($report_data->refunds as $invoice) { |
|
| 166 | + $time = date($date_format, strtotime($invoice->post_date)); |
|
| 167 | 167 | |
| 168 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
| 168 | + if (!isset($period_totals[$time])) { |
|
| 169 | 169 | continue; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - $period_totals[ $time ]['refunds'] = wpinv_round_amount( $invoice->total_sales ); |
|
| 173 | - $period_totals[ $time ]['refunded_tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
| 174 | - $period_totals[ $time ]['refunded_subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
| 175 | - $period_totals[ $time ]['refunded_fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
| 172 | + $period_totals[$time]['refunds'] = wpinv_round_amount($invoice->total_sales); |
|
| 173 | + $period_totals[$time]['refunded_tax'] = wpinv_round_amount($invoice->total_tax); |
|
| 174 | + $period_totals[$time]['refunded_subtotal'] = wpinv_round_amount($invoice->subtotal); |
|
| 175 | + $period_totals[$time]['refunded_fees'] = wpinv_round_amount($invoice->total_fees); |
|
| 176 | 176 | |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - foreach ( $report_data->invoice_counts as $invoice ) { |
|
| 180 | - $time = date( $date_format, strtotime( $invoice->post_date ) ); |
|
| 179 | + foreach ($report_data->invoice_counts as $invoice) { |
|
| 180 | + $time = date($date_format, strtotime($invoice->post_date)); |
|
| 181 | 181 | |
| 182 | - if ( isset( $period_totals[ $time ] ) ) { |
|
| 183 | - $period_totals[ $time ]['invoices'] = (int) $invoice->count; |
|
| 182 | + if (isset($period_totals[$time])) { |
|
| 183 | + $period_totals[$time]['invoices'] = (int) $invoice->count; |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | // Add total invoice items for each period. |
| 189 | - foreach ( $report_data->invoice_items as $invoice_item ) { |
|
| 190 | - $time = ( 'day' === $this->groupby ) ? date( 'Y-m-d', strtotime( $invoice_item->post_date ) ) : date( 'Y-m', strtotime( $invoice_item->post_date ) ); |
|
| 189 | + foreach ($report_data->invoice_items as $invoice_item) { |
|
| 190 | + $time = ('day' === $this->groupby) ? date('Y-m-d', strtotime($invoice_item->post_date)) : date('Y-m', strtotime($invoice_item->post_date)); |
|
| 191 | 191 | |
| 192 | - if ( isset( $period_totals[ $time ] ) ) { |
|
| 193 | - $period_totals[ $time ]['items'] = (int) $invoice_item->invoice_item_count; |
|
| 192 | + if (isset($period_totals[$time])) { |
|
| 193 | + $period_totals[$time]['items'] = (int) $invoice_item->invoice_item_count; |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | // Add total discount for each period. |
| 199 | - foreach ( $report_data->coupons as $discount ) { |
|
| 200 | - $time = ( 'day' === $this->groupby ) ? date( 'Y-m-d', strtotime( $discount->post_date ) ) : date( 'Y-m', strtotime( $discount->post_date ) ); |
|
| 199 | + foreach ($report_data->coupons as $discount) { |
|
| 200 | + $time = ('day' === $this->groupby) ? date('Y-m-d', strtotime($discount->post_date)) : date('Y-m', strtotime($discount->post_date)); |
|
| 201 | 201 | |
| 202 | - if ( isset( $period_totals[ $time ] ) ) { |
|
| 203 | - $period_totals[ $time ]['discount'] = wpinv_round_amount( $discount->discount_amount ); |
|
| 202 | + if (isset($period_totals[$time])) { |
|
| 203 | + $period_totals[$time]['discount'] = wpinv_round_amount($discount->discount_amount); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | $report_data->totals = $period_totals; |
| 209 | 209 | $report_data->grouped_by = $this->groupby; |
| 210 | - $report_data->interval = max( $this->interval, 1 ); |
|
| 210 | + $report_data->interval = max($this->interval, 1); |
|
| 211 | 211 | $report_data->currency = wpinv_get_currency(); |
| 212 | 212 | $report_data->currency_symbol = wpinv_currency_symbol(); |
| 213 | 213 | $report_data->currency_position = wpinv_currency_position(); |
| 214 | 214 | $report_data->decimal_places = wpinv_decimals(); |
| 215 | 215 | $report_data->thousands_sep = wpinv_thousands_separator(); |
| 216 | 216 | $report_data->decimals_sep = wpinv_decimal_separator(); |
| 217 | - $report_data->start_date = getpaid_format_date( date( 'Y-m-d', strtotime( $this->report_range['after'] ) + DAY_IN_SECONDS ) ); |
|
| 218 | - $report_data->end_date = getpaid_format_date( date( 'Y-m-d', strtotime( $this->report_range['before'] ) - DAY_IN_SECONDS ) ); |
|
| 217 | + $report_data->start_date = getpaid_format_date(date('Y-m-d', strtotime($this->report_range['after']) + DAY_IN_SECONDS)); |
|
| 218 | + $report_data->end_date = getpaid_format_date(date('Y-m-d', strtotime($this->report_range['before']) - DAY_IN_SECONDS)); |
|
| 219 | 219 | $report_data->decimals_sep = wpinv_decimal_separator(); |
| 220 | 220 | |
| 221 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 221 | + $context = !empty($request['context']) ? $request['context'] : 'view'; |
|
| 222 | 222 | $data = $report_data; |
| 223 | - unset( $data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items ); |
|
| 224 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 225 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 223 | + unset($data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items); |
|
| 224 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
| 225 | + $data = $this->filter_response_by_context($data, $context); |
|
| 226 | 226 | |
| 227 | 227 | // Wrap the data in a response object. |
| 228 | - $response = rest_ensure_response( $data ); |
|
| 229 | - $response->add_links( array( |
|
| 228 | + $response = rest_ensure_response($data); |
|
| 229 | + $response->add_links(array( |
|
| 230 | 230 | 'about' => array( |
| 231 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
| 231 | + 'href' => rest_url(sprintf('%s/reports', $this->namespace)), |
|
| 232 | 232 | ), |
| 233 | - ) ); |
|
| 233 | + )); |
|
| 234 | 234 | |
| 235 | - return apply_filters( 'getpaid_rest_prepare_report_sales', $response, $report_data, $request ); |
|
| 235 | + return apply_filters('getpaid_rest_prepare_report_sales', $response, $report_data, $request); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | * @return stdClass |
| 242 | 242 | */ |
| 243 | 243 | public function get_report_data() { |
| 244 | - if ( empty( $this->report_data ) ) { |
|
| 244 | + if (empty($this->report_data)) { |
|
| 245 | 245 | $this->query_report_data(); |
| 246 | 246 | } |
| 247 | 247 | return $this->report_data; |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | |
| 255 | 255 | // Prepare reports. |
| 256 | 256 | $this->report_data = (object) array( |
| 257 | - 'invoice_counts' => $this->query_invoice_counts(),//count, post_date |
|
| 257 | + 'invoice_counts' => $this->query_invoice_counts(), //count, post_date |
|
| 258 | 258 | 'coupons' => $this->query_coupon_counts(), // discount_amount, post_date |
| 259 | 259 | 'invoice_items' => $this->query_item_counts(), // invoice_item_count, post_date |
| 260 | 260 | 'refunded_items' => $this->count_refunded_items(), // invoice_item_count, post_date |
@@ -263,32 +263,32 @@ discard block |
||
| 263 | 263 | ); |
| 264 | 264 | |
| 265 | 265 | // Calculated totals. |
| 266 | - $this->report_data->total_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_tax' ) ) ); |
|
| 267 | - $this->report_data->total_sales = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_sales' ) ) ); |
|
| 268 | - $this->report_data->total_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_discount' ) ) ); |
|
| 269 | - $this->report_data->total_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_fees' ) ) ); |
|
| 270 | - $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'subtotal' ) ) ); |
|
| 271 | - $this->report_data->net_sales = wpinv_round_amount( $this->report_data->total_sales - max( 0, $this->report_data->total_tax ) ); |
|
| 272 | - $this->report_data->total_refunded_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_tax' ) ) ); |
|
| 273 | - $this->report_data->total_refunds = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_sales' ) ) ); |
|
| 274 | - $this->report_data->refunded_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_discount' ) ) ); |
|
| 275 | - $this->report_data->refunded_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_fees' ) ) ); |
|
| 276 | - $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'subtotal' ) ) ); |
|
| 277 | - $this->report_data->net_refunds = wpinv_round_amount( $this->report_data->total_refunds + max( 0, $this->report_data->total_refunded_tax ) ); |
|
| 266 | + $this->report_data->total_tax = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_tax'))); |
|
| 267 | + $this->report_data->total_sales = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_sales'))); |
|
| 268 | + $this->report_data->total_discount = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_discount'))); |
|
| 269 | + $this->report_data->total_fees = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_fees'))); |
|
| 270 | + $this->report_data->subtotal = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'subtotal'))); |
|
| 271 | + $this->report_data->net_sales = wpinv_round_amount($this->report_data->total_sales - max(0, $this->report_data->total_tax)); |
|
| 272 | + $this->report_data->total_refunded_tax = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_tax'))); |
|
| 273 | + $this->report_data->total_refunds = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_sales'))); |
|
| 274 | + $this->report_data->refunded_discount = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_discount'))); |
|
| 275 | + $this->report_data->refunded_fees = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_fees'))); |
|
| 276 | + $this->report_data->subtotal = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'subtotal'))); |
|
| 277 | + $this->report_data->net_refunds = wpinv_round_amount($this->report_data->total_refunds + max(0, $this->report_data->total_refunded_tax)); |
|
| 278 | 278 | |
| 279 | 279 | |
| 280 | 280 | // Calculate average based on net. |
| 281 | - $this->report_data->average_sales = wpinv_round_amount( $this->report_data->net_sales / max( $this->interval, 1 ), 2 ); |
|
| 282 | - $this->report_data->average_total_sales = wpinv_round_amount( $this->report_data->total_sales / max( $this->interval, 1 ), 2 ); |
|
| 281 | + $this->report_data->average_sales = wpinv_round_amount($this->report_data->net_sales / max($this->interval, 1), 2); |
|
| 282 | + $this->report_data->average_total_sales = wpinv_round_amount($this->report_data->total_sales / max($this->interval, 1), 2); |
|
| 283 | 283 | |
| 284 | 284 | // Total invoices in this period, even if refunded. |
| 285 | - $this->report_data->total_invoices = absint( array_sum( wp_list_pluck( $this->report_data->invoice_counts, 'count' ) ) ); |
|
| 285 | + $this->report_data->total_invoices = absint(array_sum(wp_list_pluck($this->report_data->invoice_counts, 'count'))); |
|
| 286 | 286 | |
| 287 | 287 | // Items invoiced in this period, even if refunded. |
| 288 | - $this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->invoice_items, 'invoice_item_count' ) ) ); |
|
| 288 | + $this->report_data->total_items = absint(array_sum(wp_list_pluck($this->report_data->invoice_items, 'invoice_item_count'))); |
|
| 289 | 289 | |
| 290 | 290 | // 3rd party filtering of report data |
| 291 | - $this->report_data = apply_filters( 'getpaid_rest_api_filter_report_data', $this->report_data ); |
|
| 291 | + $this->report_data = apply_filters('getpaid_rest_api_filter_report_data', $this->report_data); |
|
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | /** |
@@ -313,11 +313,11 @@ discard block |
||
| 313 | 313 | 'name' => 'post_date', |
| 314 | 314 | ), |
| 315 | 315 | ), |
| 316 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 316 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
| 317 | 317 | 'order_by' => 'post_date ASC', |
| 318 | 318 | 'query_type' => 'get_results', |
| 319 | 319 | 'filter_range' => $this->report_range, |
| 320 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 320 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded'), |
|
| 321 | 321 | ) |
| 322 | 322 | ); |
| 323 | 323 | |
@@ -344,11 +344,11 @@ discard block |
||
| 344 | 344 | 'name' => 'post_date', |
| 345 | 345 | ), |
| 346 | 346 | ), |
| 347 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 347 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
| 348 | 348 | 'order_by' => 'post_date ASC', |
| 349 | 349 | 'query_type' => 'get_results', |
| 350 | 350 | 'filter_range' => $this->report_range, |
| 351 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 351 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded'), |
|
| 352 | 352 | ) |
| 353 | 353 | ); |
| 354 | 354 | |
@@ -375,11 +375,11 @@ discard block |
||
| 375 | 375 | 'name' => 'post_date', |
| 376 | 376 | ), |
| 377 | 377 | ), |
| 378 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 378 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
| 379 | 379 | 'order_by' => 'post_date ASC', |
| 380 | 380 | 'query_type' => 'get_results', |
| 381 | 381 | 'filter_range' => $this->report_range, |
| 382 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded' ), |
|
| 382 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded'), |
|
| 383 | 383 | ) |
| 384 | 384 | ); |
| 385 | 385 | |
@@ -403,7 +403,7 @@ discard block |
||
| 403 | 403 | ), |
| 404 | 404 | 'query_type' => 'get_var', |
| 405 | 405 | 'filter_range' => $this->report_range, |
| 406 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
| 406 | + 'invoice_status' => array('wpi-refunded'), |
|
| 407 | 407 | ) |
| 408 | 408 | ); |
| 409 | 409 | |
@@ -450,11 +450,11 @@ discard block |
||
| 450 | 450 | 'name' => 'post_date', |
| 451 | 451 | ), |
| 452 | 452 | ), |
| 453 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 453 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
| 454 | 454 | 'order_by' => 'post_date ASC', |
| 455 | 455 | 'query_type' => 'get_results', |
| 456 | 456 | 'filter_range' => $this->report_range, |
| 457 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold' ), |
|
| 457 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold'), |
|
| 458 | 458 | ) |
| 459 | 459 | ); |
| 460 | 460 | |
@@ -501,11 +501,11 @@ discard block |
||
| 501 | 501 | 'name' => 'post_date', |
| 502 | 502 | ), |
| 503 | 503 | ), |
| 504 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
| 504 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
| 505 | 505 | 'order_by' => 'post_date ASC', |
| 506 | 506 | 'query_type' => 'get_results', |
| 507 | 507 | 'filter_range' => $this->report_range, |
| 508 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
| 508 | + 'invoice_status' => array('wpi-refunded'), |
|
| 509 | 509 | ) |
| 510 | 510 | ); |
| 511 | 511 | |
@@ -524,145 +524,145 @@ discard block |
||
| 524 | 524 | 'type' => 'object', |
| 525 | 525 | 'properties' => array( |
| 526 | 526 | 'total_sales' => array( |
| 527 | - 'description' => __( 'Gross sales in the period.', 'invoicing' ), |
|
| 527 | + 'description' => __('Gross sales in the period.', 'invoicing'), |
|
| 528 | 528 | 'type' => 'string', |
| 529 | - 'context' => array( 'view' ), |
|
| 529 | + 'context' => array('view'), |
|
| 530 | 530 | 'readonly' => true, |
| 531 | 531 | ), |
| 532 | 532 | 'net_sales' => array( |
| 533 | - 'description' => __( 'Net sales in the period.', 'invoicing' ), |
|
| 533 | + 'description' => __('Net sales in the period.', 'invoicing'), |
|
| 534 | 534 | 'type' => 'string', |
| 535 | - 'context' => array( 'view' ), |
|
| 535 | + 'context' => array('view'), |
|
| 536 | 536 | 'readonly' => true, |
| 537 | 537 | ), |
| 538 | 538 | 'average_sales' => array( |
| 539 | - 'description' => __( 'Average net daily sales.', 'invoicing' ), |
|
| 539 | + 'description' => __('Average net daily sales.', 'invoicing'), |
|
| 540 | 540 | 'type' => 'string', |
| 541 | - 'context' => array( 'view' ), |
|
| 541 | + 'context' => array('view'), |
|
| 542 | 542 | 'readonly' => true, |
| 543 | 543 | ), |
| 544 | 544 | 'average_total_sales' => array( |
| 545 | - 'description' => __( 'Average gross daily sales.', 'invoicing' ), |
|
| 545 | + 'description' => __('Average gross daily sales.', 'invoicing'), |
|
| 546 | 546 | 'type' => 'string', |
| 547 | - 'context' => array( 'view' ), |
|
| 547 | + 'context' => array('view'), |
|
| 548 | 548 | 'readonly' => true, |
| 549 | 549 | ), |
| 550 | 550 | 'total_invoices' => array( |
| 551 | - 'description' => __( 'Number of paid invoices.', 'invoicing' ), |
|
| 551 | + 'description' => __('Number of paid invoices.', 'invoicing'), |
|
| 552 | 552 | 'type' => 'integer', |
| 553 | - 'context' => array( 'view' ), |
|
| 553 | + 'context' => array('view'), |
|
| 554 | 554 | 'readonly' => true, |
| 555 | 555 | ), |
| 556 | 556 | 'total_items' => array( |
| 557 | - 'description' => __( 'Number of items purchased.', 'invoicing' ), |
|
| 557 | + 'description' => __('Number of items purchased.', 'invoicing'), |
|
| 558 | 558 | 'type' => 'integer', |
| 559 | - 'context' => array( 'view' ), |
|
| 559 | + 'context' => array('view'), |
|
| 560 | 560 | 'readonly' => true, |
| 561 | 561 | ), |
| 562 | 562 | 'refunded_items' => array( |
| 563 | - 'description' => __( 'Number of items refunded.', 'invoicing' ), |
|
| 563 | + 'description' => __('Number of items refunded.', 'invoicing'), |
|
| 564 | 564 | 'type' => 'integer', |
| 565 | - 'context' => array( 'view' ), |
|
| 565 | + 'context' => array('view'), |
|
| 566 | 566 | 'readonly' => true, |
| 567 | 567 | ), |
| 568 | 568 | 'total_tax' => array( |
| 569 | - 'description' => __( 'Total charged for taxes.', 'invoicing' ), |
|
| 569 | + 'description' => __('Total charged for taxes.', 'invoicing'), |
|
| 570 | 570 | 'type' => 'string', |
| 571 | - 'context' => array( 'view' ), |
|
| 571 | + 'context' => array('view'), |
|
| 572 | 572 | 'readonly' => true, |
| 573 | 573 | ), |
| 574 | 574 | 'total_refunded_tax' => array( |
| 575 | - 'description' => __( 'Total refunded for taxes.', 'invoicing' ), |
|
| 575 | + 'description' => __('Total refunded for taxes.', 'invoicing'), |
|
| 576 | 576 | 'type' => 'string', |
| 577 | - 'context' => array( 'view' ), |
|
| 577 | + 'context' => array('view'), |
|
| 578 | 578 | 'readonly' => true, |
| 579 | 579 | ), |
| 580 | 580 | 'total_fees' => array( |
| 581 | - 'description' => __( 'Total fees charged.', 'invoicing' ), |
|
| 581 | + 'description' => __('Total fees charged.', 'invoicing'), |
|
| 582 | 582 | 'type' => 'string', |
| 583 | - 'context' => array( 'view' ), |
|
| 583 | + 'context' => array('view'), |
|
| 584 | 584 | 'readonly' => true, |
| 585 | 585 | ), |
| 586 | 586 | 'total_refunds' => array( |
| 587 | - 'description' => __( 'Total of refunded invoices.', 'invoicing' ), |
|
| 587 | + 'description' => __('Total of refunded invoices.', 'invoicing'), |
|
| 588 | 588 | 'type' => 'integer', |
| 589 | - 'context' => array( 'view' ), |
|
| 589 | + 'context' => array('view'), |
|
| 590 | 590 | 'readonly' => true, |
| 591 | 591 | ), |
| 592 | 592 | 'net_refunds' => array( |
| 593 | - 'description' => __( 'Net of refunded invoices.', 'invoicing' ), |
|
| 593 | + 'description' => __('Net of refunded invoices.', 'invoicing'), |
|
| 594 | 594 | 'type' => 'integer', |
| 595 | - 'context' => array( 'view' ), |
|
| 595 | + 'context' => array('view'), |
|
| 596 | 596 | 'readonly' => true, |
| 597 | 597 | ), |
| 598 | 598 | 'total_discount' => array( |
| 599 | - 'description' => __( 'Total of discounts used.', 'invoicing' ), |
|
| 599 | + 'description' => __('Total of discounts used.', 'invoicing'), |
|
| 600 | 600 | 'type' => 'integer', |
| 601 | - 'context' => array( 'view' ), |
|
| 601 | + 'context' => array('view'), |
|
| 602 | 602 | 'readonly' => true, |
| 603 | 603 | ), |
| 604 | 604 | 'totals' => array( |
| 605 | - 'description' => __( 'Totals.', 'invoicing' ), |
|
| 605 | + 'description' => __('Totals.', 'invoicing'), |
|
| 606 | 606 | 'type' => 'array', |
| 607 | 607 | 'items' => array( |
| 608 | 608 | 'type' => 'array', |
| 609 | 609 | ), |
| 610 | - 'context' => array( 'view' ), |
|
| 610 | + 'context' => array('view'), |
|
| 611 | 611 | 'readonly' => true, |
| 612 | 612 | ), |
| 613 | 613 | 'interval' => array( |
| 614 | - 'description' => __( 'Number of months/days in the report period.', 'invoicing' ), |
|
| 614 | + 'description' => __('Number of months/days in the report period.', 'invoicing'), |
|
| 615 | 615 | 'type' => 'integer', |
| 616 | - 'context' => array( 'view' ), |
|
| 616 | + 'context' => array('view'), |
|
| 617 | 617 | 'readonly' => true, |
| 618 | 618 | ), |
| 619 | 619 | 'grouped_by' => array( |
| 620 | - 'description' => __( 'The period used to group the totals.', 'invoicing' ), |
|
| 620 | + 'description' => __('The period used to group the totals.', 'invoicing'), |
|
| 621 | 621 | 'type' => 'string', |
| 622 | - 'context' => array( 'view' ), |
|
| 623 | - 'enum' => array( 'day', 'month' ), |
|
| 622 | + 'context' => array('view'), |
|
| 623 | + 'enum' => array('day', 'month'), |
|
| 624 | 624 | 'readonly' => true, |
| 625 | 625 | ), |
| 626 | 626 | 'currency' => array( |
| 627 | - 'description' => __( 'The default store currency.', 'invoicing' ), |
|
| 627 | + 'description' => __('The default store currency.', 'invoicing'), |
|
| 628 | 628 | 'type' => 'string', |
| 629 | - 'context' => array( 'view' ), |
|
| 629 | + 'context' => array('view'), |
|
| 630 | 630 | 'readonly' => true, |
| 631 | 631 | ), |
| 632 | 632 | 'currency_symbol' => array( |
| 633 | - 'description' => __( 'The default store currency symbol.', 'invoicing' ), |
|
| 633 | + 'description' => __('The default store currency symbol.', 'invoicing'), |
|
| 634 | 634 | 'type' => 'string', |
| 635 | - 'context' => array( 'view' ), |
|
| 635 | + 'context' => array('view'), |
|
| 636 | 636 | 'readonly' => true, |
| 637 | 637 | ), |
| 638 | 638 | 'currency_position' => array( |
| 639 | - 'description' => __( 'The default store currency position.', 'invoicing' ), |
|
| 639 | + 'description' => __('The default store currency position.', 'invoicing'), |
|
| 640 | 640 | 'type' => 'string', |
| 641 | - 'context' => array( 'view' ), |
|
| 641 | + 'context' => array('view'), |
|
| 642 | 642 | 'readonly' => true, |
| 643 | 643 | ), |
| 644 | 644 | 'decimal_places' => array( |
| 645 | - 'description' => __( 'The default store decimal places.', 'invoicing' ), |
|
| 645 | + 'description' => __('The default store decimal places.', 'invoicing'), |
|
| 646 | 646 | 'type' => 'string', |
| 647 | - 'context' => array( 'view' ), |
|
| 647 | + 'context' => array('view'), |
|
| 648 | 648 | 'readonly' => true, |
| 649 | 649 | ), |
| 650 | 650 | 'thousands_sep' => array( |
| 651 | - 'description' => __( 'The default store thousands separator.', 'invoicing' ), |
|
| 651 | + 'description' => __('The default store thousands separator.', 'invoicing'), |
|
| 652 | 652 | 'type' => 'string', |
| 653 | - 'context' => array( 'view' ), |
|
| 653 | + 'context' => array('view'), |
|
| 654 | 654 | 'readonly' => true, |
| 655 | 655 | ), |
| 656 | 656 | 'decimals_sep' => array( |
| 657 | - 'description' => __( 'The default store decimals separator.', 'invoicing' ), |
|
| 657 | + 'description' => __('The default store decimals separator.', 'invoicing'), |
|
| 658 | 658 | 'type' => 'string', |
| 659 | - 'context' => array( 'view' ), |
|
| 659 | + 'context' => array('view'), |
|
| 660 | 660 | 'readonly' => true, |
| 661 | 661 | ), |
| 662 | 662 | ), |
| 663 | 663 | ); |
| 664 | 664 | |
| 665 | - return $this->add_additional_fields_schema( $schema ); |
|
| 665 | + return $this->add_additional_fields_schema($schema); |
|
| 666 | 666 | |
| 667 | 667 | } |
| 668 | 668 | |
@@ -18,179 +18,179 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class GetPaid_REST_Report_Top_Sellers_Controller extends GetPaid_REST_Report_Sales_Controller { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Route base. |
|
| 23 | - * |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $rest_base = 'reports/top_sellers'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Get top sellers report. |
|
| 30 | - * |
|
| 31 | - * @param WP_REST_Request $request |
|
| 32 | - * @return array|WP_Error |
|
| 33 | - */ |
|
| 34 | - public function get_items( $request ) { |
|
| 35 | - |
|
| 36 | - // Prepare items. |
|
| 37 | - $this->report_range = $this->get_date_range( $request ); |
|
| 38 | - $report_data = $this->get_report_data(); |
|
| 39 | - |
|
| 40 | - $top_sellers = array(); |
|
| 41 | - |
|
| 42 | - foreach ( $report_data as $item ) { |
|
| 43 | - |
|
| 44 | - $item_obj = new WPInv_Item( $item ); |
|
| 45 | - $item_name = $item->invoice_item_name; |
|
| 46 | - $item_qty = absint( $item->invoice_item_qty ); |
|
| 47 | - $item_id = absint( $item->invoice_item_id ); |
|
| 48 | - $price = sanitize_text_field( wpinv_price( $item->invoice_item_price ) ); |
|
| 49 | - |
|
| 50 | - $item_obj = new WPInv_Item( $item_id ); |
|
| 51 | - |
|
| 52 | - if ( $item_obj->exists() ) { |
|
| 53 | - $item_name = $item_obj->get_name(); |
|
| 54 | - } else { |
|
| 55 | - $item_id = 0; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - $top_sellers[] = array( |
|
| 59 | - 'name' =>sanitize_text_field( $item_name ), |
|
| 60 | - 'item_id' => $item_id, |
|
| 61 | - 'quantity' => $item_qty, |
|
| 62 | - 'earnings' => wpinv_round_amount( $item->invoice_item_price ), |
|
| 63 | - 'earnings_formatted' => sanitize_text_field( wpinv_price( $price ) ), |
|
| 64 | - ); |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $data = array(); |
|
| 69 | - foreach ( $top_sellers as $top_seller ) { |
|
| 70 | - $item = $this->prepare_item_for_response( (object) $top_seller, $request ); |
|
| 71 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return rest_ensure_response( $data ); |
|
| 75 | - |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Prepare a report sales object for serialization. |
|
| 80 | - * |
|
| 81 | - * @param stdClass $top_seller |
|
| 82 | - * @param WP_REST_Request $request Request object. |
|
| 83 | - * @return WP_REST_Response $response Response data. |
|
| 84 | - */ |
|
| 85 | - public function prepare_item_for_response( $top_seller, $request ) { |
|
| 86 | - $data = (array) $top_seller; |
|
| 87 | - |
|
| 88 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 89 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 90 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 91 | - |
|
| 92 | - // Wrap the data in a response object. |
|
| 93 | - $response = rest_ensure_response( $data ); |
|
| 94 | - $links = array( |
|
| 95 | - 'about' => array( |
|
| 96 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
| 97 | - ), |
|
| 98 | - ); |
|
| 99 | - |
|
| 100 | - if ( ! empty( $top_seller->item_id ) ) { |
|
| 101 | - $links['item'] = array( |
|
| 102 | - 'href' => rest_url( sprintf( '/%s/items/%s', $this->namespace, $top_seller->item_id ) ), |
|
| 103 | - 'embeddable' => true, |
|
| 104 | - ); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $response->add_links( $links ); |
|
| 108 | - return apply_filters( 'getpaid_rest_prepare_report_top_sellers', $response, $top_seller, $request ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Get all data needed for this report and store in the class. |
|
| 113 | - */ |
|
| 114 | - protected function query_report_data() { |
|
| 115 | - |
|
| 116 | - $this->report_data = GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 117 | - array( |
|
| 118 | - 'data' => array( |
|
| 119 | - 'quantity' => array( |
|
| 120 | - 'type' => 'invoice_item', |
|
| 121 | - 'function' => 'SUM', |
|
| 122 | - 'name' => 'invoice_item_qty', |
|
| 123 | - ), |
|
| 124 | - 'item_id' => array( |
|
| 125 | - 'type' => 'invoice_item', |
|
| 126 | - 'function' => '', |
|
| 127 | - 'name' => 'invoice_item_id', |
|
| 128 | - ), |
|
| 129 | - 'item_name' => array( |
|
| 130 | - 'type' => 'invoice_item', |
|
| 131 | - 'function' => '', |
|
| 132 | - 'name' => 'invoice_item_name', |
|
| 133 | - ), |
|
| 134 | - 'price' => array( |
|
| 135 | - 'type' => 'invoice_item', |
|
| 136 | - 'function' => 'SUM', |
|
| 137 | - 'name' => 'invoice_item_price', |
|
| 138 | - ), |
|
| 139 | - ), |
|
| 140 | - 'group_by' => 'invoice_item_id', |
|
| 141 | - 'order_by' => 'invoice_item_qty DESC', |
|
| 142 | - 'query_type' => 'get_results', |
|
| 143 | - 'limit' => 10, |
|
| 144 | - 'filter_range' => $this->report_range, |
|
| 145 | - ) |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Get the Report's schema, conforming to JSON Schema. |
|
| 152 | - * |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public function get_item_schema() { |
|
| 156 | - $schema = array( |
|
| 157 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 158 | - 'title' => 'top_sellers_report', |
|
| 159 | - 'type' => 'object', |
|
| 160 | - 'properties' => array( |
|
| 161 | - 'name' => array( |
|
| 162 | - 'description' => __( 'Item name.', 'invoicing' ), |
|
| 163 | - 'type' => 'string', |
|
| 164 | - 'context' => array( 'view' ), |
|
| 165 | - 'readonly' => true, |
|
| 166 | - ), |
|
| 167 | - 'item_id' => array( |
|
| 168 | - 'description' => __( 'Item ID.', 'invoicing' ), |
|
| 169 | - 'type' => 'integer', |
|
| 170 | - 'context' => array( 'view' ), |
|
| 171 | - 'readonly' => true, |
|
| 172 | - ), |
|
| 173 | - 'quantity' => array( |
|
| 174 | - 'description' => __( 'Total number of purchases.', 'invoicing' ), |
|
| 175 | - 'type' => 'integer', |
|
| 176 | - 'context' => array( 'view' ), |
|
| 177 | - 'readonly' => true, |
|
| 178 | - ), |
|
| 179 | - 'earnings' => array( |
|
| 180 | - 'description' => __( 'Total earnings for the item.', 'invoicing' ), |
|
| 181 | - 'type' => 'double', |
|
| 182 | - 'context' => array( 'view' ), |
|
| 183 | - 'readonly' => true, |
|
| 184 | - ), |
|
| 185 | - 'earnings_formatted"' => array( |
|
| 186 | - 'description' => __( 'Total earnings (formatted) for the item.', 'invoicing' ), |
|
| 187 | - 'type' => 'string', |
|
| 188 | - 'context' => array( 'view' ), |
|
| 189 | - 'readonly' => true, |
|
| 190 | - ), |
|
| 191 | - ), |
|
| 192 | - ); |
|
| 193 | - |
|
| 194 | - return $this->add_additional_fields_schema( $schema ); |
|
| 195 | - } |
|
| 21 | + /** |
|
| 22 | + * Route base. |
|
| 23 | + * |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $rest_base = 'reports/top_sellers'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Get top sellers report. |
|
| 30 | + * |
|
| 31 | + * @param WP_REST_Request $request |
|
| 32 | + * @return array|WP_Error |
|
| 33 | + */ |
|
| 34 | + public function get_items( $request ) { |
|
| 35 | + |
|
| 36 | + // Prepare items. |
|
| 37 | + $this->report_range = $this->get_date_range( $request ); |
|
| 38 | + $report_data = $this->get_report_data(); |
|
| 39 | + |
|
| 40 | + $top_sellers = array(); |
|
| 41 | + |
|
| 42 | + foreach ( $report_data as $item ) { |
|
| 43 | + |
|
| 44 | + $item_obj = new WPInv_Item( $item ); |
|
| 45 | + $item_name = $item->invoice_item_name; |
|
| 46 | + $item_qty = absint( $item->invoice_item_qty ); |
|
| 47 | + $item_id = absint( $item->invoice_item_id ); |
|
| 48 | + $price = sanitize_text_field( wpinv_price( $item->invoice_item_price ) ); |
|
| 49 | + |
|
| 50 | + $item_obj = new WPInv_Item( $item_id ); |
|
| 51 | + |
|
| 52 | + if ( $item_obj->exists() ) { |
|
| 53 | + $item_name = $item_obj->get_name(); |
|
| 54 | + } else { |
|
| 55 | + $item_id = 0; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + $top_sellers[] = array( |
|
| 59 | + 'name' =>sanitize_text_field( $item_name ), |
|
| 60 | + 'item_id' => $item_id, |
|
| 61 | + 'quantity' => $item_qty, |
|
| 62 | + 'earnings' => wpinv_round_amount( $item->invoice_item_price ), |
|
| 63 | + 'earnings_formatted' => sanitize_text_field( wpinv_price( $price ) ), |
|
| 64 | + ); |
|
| 65 | + |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $data = array(); |
|
| 69 | + foreach ( $top_sellers as $top_seller ) { |
|
| 70 | + $item = $this->prepare_item_for_response( (object) $top_seller, $request ); |
|
| 71 | + $data[] = $this->prepare_response_for_collection( $item ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return rest_ensure_response( $data ); |
|
| 75 | + |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Prepare a report sales object for serialization. |
|
| 80 | + * |
|
| 81 | + * @param stdClass $top_seller |
|
| 82 | + * @param WP_REST_Request $request Request object. |
|
| 83 | + * @return WP_REST_Response $response Response data. |
|
| 84 | + */ |
|
| 85 | + public function prepare_item_for_response( $top_seller, $request ) { |
|
| 86 | + $data = (array) $top_seller; |
|
| 87 | + |
|
| 88 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 89 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 90 | + $data = $this->filter_response_by_context( $data, $context ); |
|
| 91 | + |
|
| 92 | + // Wrap the data in a response object. |
|
| 93 | + $response = rest_ensure_response( $data ); |
|
| 94 | + $links = array( |
|
| 95 | + 'about' => array( |
|
| 96 | + 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
| 97 | + ), |
|
| 98 | + ); |
|
| 99 | + |
|
| 100 | + if ( ! empty( $top_seller->item_id ) ) { |
|
| 101 | + $links['item'] = array( |
|
| 102 | + 'href' => rest_url( sprintf( '/%s/items/%s', $this->namespace, $top_seller->item_id ) ), |
|
| 103 | + 'embeddable' => true, |
|
| 104 | + ); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $response->add_links( $links ); |
|
| 108 | + return apply_filters( 'getpaid_rest_prepare_report_top_sellers', $response, $top_seller, $request ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Get all data needed for this report and store in the class. |
|
| 113 | + */ |
|
| 114 | + protected function query_report_data() { |
|
| 115 | + |
|
| 116 | + $this->report_data = GetPaid_Reports_Helper::get_invoice_report_data( |
|
| 117 | + array( |
|
| 118 | + 'data' => array( |
|
| 119 | + 'quantity' => array( |
|
| 120 | + 'type' => 'invoice_item', |
|
| 121 | + 'function' => 'SUM', |
|
| 122 | + 'name' => 'invoice_item_qty', |
|
| 123 | + ), |
|
| 124 | + 'item_id' => array( |
|
| 125 | + 'type' => 'invoice_item', |
|
| 126 | + 'function' => '', |
|
| 127 | + 'name' => 'invoice_item_id', |
|
| 128 | + ), |
|
| 129 | + 'item_name' => array( |
|
| 130 | + 'type' => 'invoice_item', |
|
| 131 | + 'function' => '', |
|
| 132 | + 'name' => 'invoice_item_name', |
|
| 133 | + ), |
|
| 134 | + 'price' => array( |
|
| 135 | + 'type' => 'invoice_item', |
|
| 136 | + 'function' => 'SUM', |
|
| 137 | + 'name' => 'invoice_item_price', |
|
| 138 | + ), |
|
| 139 | + ), |
|
| 140 | + 'group_by' => 'invoice_item_id', |
|
| 141 | + 'order_by' => 'invoice_item_qty DESC', |
|
| 142 | + 'query_type' => 'get_results', |
|
| 143 | + 'limit' => 10, |
|
| 144 | + 'filter_range' => $this->report_range, |
|
| 145 | + ) |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Get the Report's schema, conforming to JSON Schema. |
|
| 152 | + * |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public function get_item_schema() { |
|
| 156 | + $schema = array( |
|
| 157 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 158 | + 'title' => 'top_sellers_report', |
|
| 159 | + 'type' => 'object', |
|
| 160 | + 'properties' => array( |
|
| 161 | + 'name' => array( |
|
| 162 | + 'description' => __( 'Item name.', 'invoicing' ), |
|
| 163 | + 'type' => 'string', |
|
| 164 | + 'context' => array( 'view' ), |
|
| 165 | + 'readonly' => true, |
|
| 166 | + ), |
|
| 167 | + 'item_id' => array( |
|
| 168 | + 'description' => __( 'Item ID.', 'invoicing' ), |
|
| 169 | + 'type' => 'integer', |
|
| 170 | + 'context' => array( 'view' ), |
|
| 171 | + 'readonly' => true, |
|
| 172 | + ), |
|
| 173 | + 'quantity' => array( |
|
| 174 | + 'description' => __( 'Total number of purchases.', 'invoicing' ), |
|
| 175 | + 'type' => 'integer', |
|
| 176 | + 'context' => array( 'view' ), |
|
| 177 | + 'readonly' => true, |
|
| 178 | + ), |
|
| 179 | + 'earnings' => array( |
|
| 180 | + 'description' => __( 'Total earnings for the item.', 'invoicing' ), |
|
| 181 | + 'type' => 'double', |
|
| 182 | + 'context' => array( 'view' ), |
|
| 183 | + 'readonly' => true, |
|
| 184 | + ), |
|
| 185 | + 'earnings_formatted"' => array( |
|
| 186 | + 'description' => __( 'Total earnings (formatted) for the item.', 'invoicing' ), |
|
| 187 | + 'type' => 'string', |
|
| 188 | + 'context' => array( 'view' ), |
|
| 189 | + 'readonly' => true, |
|
| 190 | + ), |
|
| 191 | + ), |
|
| 192 | + ); |
|
| 193 | + |
|
| 194 | + return $this->add_additional_fields_schema( $schema ); |
|
| 195 | + } |
|
| 196 | 196 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | * @since 2.0.0 |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | -defined( 'ABSPATH' ) || exit; |
|
| 12 | +defined('ABSPATH') || exit; |
|
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * GetPaid REST top sellers controller class. |
@@ -31,47 +31,47 @@ discard block |
||
| 31 | 31 | * @param WP_REST_Request $request |
| 32 | 32 | * @return array|WP_Error |
| 33 | 33 | */ |
| 34 | - public function get_items( $request ) { |
|
| 34 | + public function get_items($request) { |
|
| 35 | 35 | |
| 36 | 36 | // Prepare items. |
| 37 | - $this->report_range = $this->get_date_range( $request ); |
|
| 37 | + $this->report_range = $this->get_date_range($request); |
|
| 38 | 38 | $report_data = $this->get_report_data(); |
| 39 | 39 | |
| 40 | 40 | $top_sellers = array(); |
| 41 | 41 | |
| 42 | - foreach ( $report_data as $item ) { |
|
| 42 | + foreach ($report_data as $item) { |
|
| 43 | 43 | |
| 44 | - $item_obj = new WPInv_Item( $item ); |
|
| 44 | + $item_obj = new WPInv_Item($item); |
|
| 45 | 45 | $item_name = $item->invoice_item_name; |
| 46 | - $item_qty = absint( $item->invoice_item_qty ); |
|
| 47 | - $item_id = absint( $item->invoice_item_id ); |
|
| 48 | - $price = sanitize_text_field( wpinv_price( $item->invoice_item_price ) ); |
|
| 46 | + $item_qty = absint($item->invoice_item_qty); |
|
| 47 | + $item_id = absint($item->invoice_item_id); |
|
| 48 | + $price = sanitize_text_field(wpinv_price($item->invoice_item_price)); |
|
| 49 | 49 | |
| 50 | - $item_obj = new WPInv_Item( $item_id ); |
|
| 50 | + $item_obj = new WPInv_Item($item_id); |
|
| 51 | 51 | |
| 52 | - if ( $item_obj->exists() ) { |
|
| 52 | + if ($item_obj->exists()) { |
|
| 53 | 53 | $item_name = $item_obj->get_name(); |
| 54 | 54 | } else { |
| 55 | 55 | $item_id = 0; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | $top_sellers[] = array( |
| 59 | - 'name' =>sanitize_text_field( $item_name ), |
|
| 59 | + 'name' =>sanitize_text_field($item_name), |
|
| 60 | 60 | 'item_id' => $item_id, |
| 61 | 61 | 'quantity' => $item_qty, |
| 62 | - 'earnings' => wpinv_round_amount( $item->invoice_item_price ), |
|
| 63 | - 'earnings_formatted' => sanitize_text_field( wpinv_price( $price ) ), |
|
| 62 | + 'earnings' => wpinv_round_amount($item->invoice_item_price), |
|
| 63 | + 'earnings_formatted' => sanitize_text_field(wpinv_price($price)), |
|
| 64 | 64 | ); |
| 65 | 65 | |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | $data = array(); |
| 69 | - foreach ( $top_sellers as $top_seller ) { |
|
| 70 | - $item = $this->prepare_item_for_response( (object) $top_seller, $request ); |
|
| 71 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
| 69 | + foreach ($top_sellers as $top_seller) { |
|
| 70 | + $item = $this->prepare_item_for_response((object) $top_seller, $request); |
|
| 71 | + $data[] = $this->prepare_response_for_collection($item); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - return rest_ensure_response( $data ); |
|
| 74 | + return rest_ensure_response($data); |
|
| 75 | 75 | |
| 76 | 76 | } |
| 77 | 77 | |
@@ -82,30 +82,30 @@ discard block |
||
| 82 | 82 | * @param WP_REST_Request $request Request object. |
| 83 | 83 | * @return WP_REST_Response $response Response data. |
| 84 | 84 | */ |
| 85 | - public function prepare_item_for_response( $top_seller, $request ) { |
|
| 85 | + public function prepare_item_for_response($top_seller, $request) { |
|
| 86 | 86 | $data = (array) $top_seller; |
| 87 | 87 | |
| 88 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 89 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 90 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 88 | + $context = !empty($request['context']) ? $request['context'] : 'view'; |
|
| 89 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
| 90 | + $data = $this->filter_response_by_context($data, $context); |
|
| 91 | 91 | |
| 92 | 92 | // Wrap the data in a response object. |
| 93 | - $response = rest_ensure_response( $data ); |
|
| 93 | + $response = rest_ensure_response($data); |
|
| 94 | 94 | $links = array( |
| 95 | 95 | 'about' => array( |
| 96 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
| 96 | + 'href' => rest_url(sprintf('%s/reports', $this->namespace)), |
|
| 97 | 97 | ), |
| 98 | 98 | ); |
| 99 | 99 | |
| 100 | - if ( ! empty( $top_seller->item_id ) ) { |
|
| 101 | - $links['item'] = array( |
|
| 102 | - 'href' => rest_url( sprintf( '/%s/items/%s', $this->namespace, $top_seller->item_id ) ), |
|
| 100 | + if (!empty($top_seller->item_id)) { |
|
| 101 | + $links['item'] = array( |
|
| 102 | + 'href' => rest_url(sprintf('/%s/items/%s', $this->namespace, $top_seller->item_id)), |
|
| 103 | 103 | 'embeddable' => true, |
| 104 | 104 | ); |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - $response->add_links( $links ); |
|
| 108 | - return apply_filters( 'getpaid_rest_prepare_report_top_sellers', $response, $top_seller, $request ); |
|
| 107 | + $response->add_links($links); |
|
| 108 | + return apply_filters('getpaid_rest_prepare_report_top_sellers', $response, $top_seller, $request); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -159,38 +159,38 @@ discard block |
||
| 159 | 159 | 'type' => 'object', |
| 160 | 160 | 'properties' => array( |
| 161 | 161 | 'name' => array( |
| 162 | - 'description' => __( 'Item name.', 'invoicing' ), |
|
| 162 | + 'description' => __('Item name.', 'invoicing'), |
|
| 163 | 163 | 'type' => 'string', |
| 164 | - 'context' => array( 'view' ), |
|
| 164 | + 'context' => array('view'), |
|
| 165 | 165 | 'readonly' => true, |
| 166 | 166 | ), |
| 167 | 167 | 'item_id' => array( |
| 168 | - 'description' => __( 'Item ID.', 'invoicing' ), |
|
| 168 | + 'description' => __('Item ID.', 'invoicing'), |
|
| 169 | 169 | 'type' => 'integer', |
| 170 | - 'context' => array( 'view' ), |
|
| 170 | + 'context' => array('view'), |
|
| 171 | 171 | 'readonly' => true, |
| 172 | 172 | ), |
| 173 | 173 | 'quantity' => array( |
| 174 | - 'description' => __( 'Total number of purchases.', 'invoicing' ), |
|
| 174 | + 'description' => __('Total number of purchases.', 'invoicing'), |
|
| 175 | 175 | 'type' => 'integer', |
| 176 | - 'context' => array( 'view' ), |
|
| 176 | + 'context' => array('view'), |
|
| 177 | 177 | 'readonly' => true, |
| 178 | 178 | ), |
| 179 | 179 | 'earnings' => array( |
| 180 | - 'description' => __( 'Total earnings for the item.', 'invoicing' ), |
|
| 180 | + 'description' => __('Total earnings for the item.', 'invoicing'), |
|
| 181 | 181 | 'type' => 'double', |
| 182 | - 'context' => array( 'view' ), |
|
| 182 | + 'context' => array('view'), |
|
| 183 | 183 | 'readonly' => true, |
| 184 | 184 | ), |
| 185 | 185 | 'earnings_formatted"' => array( |
| 186 | - 'description' => __( 'Total earnings (formatted) for the item.', 'invoicing' ), |
|
| 186 | + 'description' => __('Total earnings (formatted) for the item.', 'invoicing'), |
|
| 187 | 187 | 'type' => 'string', |
| 188 | - 'context' => array( 'view' ), |
|
| 188 | + 'context' => array('view'), |
|
| 189 | 189 | 'readonly' => true, |
| 190 | 190 | ), |
| 191 | 191 | ), |
| 192 | 192 | ); |
| 193 | 193 | |
| 194 | - return $this->add_additional_fields_schema( $schema ); |
|
| 194 | + return $this->add_additional_fields_schema($schema); |
|
| 195 | 195 | } |
| 196 | 196 | } |