| @@ -11,9 +11,9 @@ | ||
| 11 | 11 | /** |
| 12 | 12 | * @return array |
| 13 | 13 | */ |
| 14 | 14 | public function get_defaults() { |
| 15 | - $values = array( | |
| 15 | + return array( | |
| 16 | 16 | 'receipt_id' => array( |
| 17 | 17 | 'sanitize' => 'sanitize_text_field', |
| 18 | 18 | 'default' => '', |
| 19 | 19 | ), |
| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | 'default' => '', |
| 27 | 27 | ), |
| 28 | 28 | 'item_id' => array( |
| 29 | 29 | 'sanitize' => 'absint', |
| 30 | - 'default' => '', | |
| 30 | + 'default' => '', | |
| 31 | 31 | ), |
| 32 | 32 | 'amount' => array( |
| 33 | 33 | 'sanitize' => 'float', |
| 34 | 34 | 'default' => '', |
| @@ -60,8 +60,92 @@ | ||
| 60 | 60 | 'meta_value' => array( |
| 61 | 61 | 'sanitize' => 'maybe_serialize', |
| 62 | 62 | 'default' => '', |
| 63 | 63 | ), |
| 64 | + 'test' => array( | |
| 65 | + 'sanitize' => 'sanitize_text_field', | |
| 66 | + 'default' => null, | |
| 67 | + ), | |
| 64 | 68 | ); |
| 65 | - return $values; | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Gets payments statistic data. | |
| 73 | + * | |
| 74 | + * @since 6.7 | |
| 75 | + * | |
| 76 | + * @param string|null $from_date From date. | |
| 77 | + * @param string|null $to_date To date. | |
| 78 | + * | |
| 79 | + * @return array Contains `count` and `total`. | |
| 80 | + */ | |
| 81 | + public function get_payments_stats( $from_date = null, $to_date = null ) { | |
| 82 | + $data = array( | |
| 83 | + 'count' => 0, | |
| 84 | + 'total' => array(), | |
| 85 | + ); | |
| 86 | + | |
| 87 | + if ( ! FrmTransLiteAppHelper::payments_table_exists() ) { | |
| 88 | + return $data; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $where = array(); | |
| 92 | + | |
| 93 | + if ( null !== $from_date ) { | |
| 94 | + $where['created_at >'] = $from_date; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( null !== $to_date ) { | |
| 98 | + $where['created_at <'] = $to_date . ' 23:59:59'; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $db_version = get_option( $this->db_opt_name ); | |
| 102 | + | |
| 103 | + // Do not collect test payment, this is a new feature of Stripe lite. | |
| 104 | + if ( 6 <= $db_version ) { | |
| 105 | + $where['test'] = array( null, 0 ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // If only Paypal is used, and the DB isn't migrated (to version 4), use `completed` column instead of `status`. | |
| 109 | + if ( 4 > $db_version && FrmDb::db_column_exists( $this->table_name, 'completed' ) ) { | |
| 110 | + $where['completed'] = 1; | |
| 111 | + } else { | |
| 112 | + $where['status'] = 'complete'; | |
| 113 | + } | |
| 114 | + | |
| 115 | + $payments = FrmDb::get_results( $this->table_name, $where, 'action_id,amount' ); | |
| 116 | + | |
| 117 | + if ( ! $payments ) { | |
| 118 | + return $data; | |
| 119 | + } | |
| 120 | + | |
| 121 | + $data['count'] = count( $payments ); | |
| 122 | + $data['total'] = $this->get_payment_total_data( $payments ); | |
| 123 | + | |
| 124 | + return $data; | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Gets payment total data. | |
| 129 | + * | |
| 130 | + * @since 6.7 | |
| 131 | + * | |
| 132 | + * @param object[] $payments Array of payment objects. | |
| 133 | + * | |
| 134 | + * @return array Return array of total amount for each currency. | |
| 135 | + */ | |
| 136 | + private function get_payment_total_data( $payments ) { | |
| 137 | + $data = array(); | |
| 138 | + | |
| 139 | + foreach ( $payments as $payment ) { | |
| 140 | + list( $amount, $currency ) = FrmTransLiteAppHelper::get_amount_and_currency_from_payment( $payment ); | |
| 141 | + | |
| 142 | + if ( ! isset( $data[ $currency ] ) ) { | |
| 143 | + $data[ $currency ] = 0; | |
| 144 | + } | |
| 145 | + | |
| 146 | + $data[ $currency ] += floatval( $amount ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + return $data; | |
| 66 | 150 | } |
| 67 | 151 | } |