base-export-controller.php
2 years ago
multiple-controller.php
2 years ago
single-controller.php
2 years ago
multiple-controller.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Gateways\Export; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 7 | use JFB_Modules\Gateways\Query_Views\Payment_For_Export_View; |
| 8 | |
| 9 | // If this file is called directly, abort. |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class Multiple_Controller extends Base_Export_Controller { |
| 15 | |
| 16 | /** |
| 17 | * @throws \Exception |
| 18 | */ |
| 19 | public function do_export() { |
| 20 | $this->modify_columns(); |
| 21 | $this->modify_record_columns(); |
| 22 | $this->modify_payer_columns(); |
| 23 | $this->modify_shipping_columns(); |
| 24 | |
| 25 | if ( ! $this->columns && |
| 26 | ! $this->payers_columns && |
| 27 | ! $this->shipping_columns && |
| 28 | ! $this->record_columns |
| 29 | ) { |
| 30 | throw new \Exception( |
| 31 | esc_html__( 'General or additional columns must be specified', 'jet-form-builder' ) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | $this->get_exporter()->set_title( __( 'JFB Payments', 'jet-form-builder' ) ); |
| 36 | $this->get_exporter()->open(); |
| 37 | |
| 38 | // headings |
| 39 | $this->get_exporter()->add_row( |
| 40 | $this->prepare_row( |
| 41 | $this->columns, |
| 42 | $this->record_columns, |
| 43 | $this->payers_columns, |
| 44 | $this->shipping_columns |
| 45 | ) |
| 46 | ); |
| 47 | |
| 48 | $this->add_rows(); |
| 49 | $this->get_exporter()->close(); |
| 50 | die; |
| 51 | } |
| 52 | |
| 53 | protected function add_rows() { |
| 54 | try { |
| 55 | $payments = $this->generate_payments(); |
| 56 | } catch ( Query_Builder_Exception $exception ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if ( ! $this->payers_columns && ! $this->shipping_columns && ! $this->record_columns ) { |
| 61 | foreach ( $payments as $payment ) { |
| 62 | /** |
| 63 | * @see https://github.com/Crocoblock/issues-tracker/issues/3112 |
| 64 | */ |
| 65 | if ( empty( $this->columns['id'] ) ) { |
| 66 | unset( $payment->id ); |
| 67 | } |
| 68 | $this->get_exporter()->add_row( |
| 69 | $this->prepare_row( |
| 70 | $payment, |
| 71 | array(), |
| 72 | array(), |
| 73 | array() |
| 74 | ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | foreach ( $payments as $payment ) { |
| 82 | $this->add_row( $payment ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @throws Query_Builder_Exception |
| 88 | */ |
| 89 | protected function generate_payments(): \Generator { |
| 90 | $view = ( new Payment_For_Export_View() )->set_filters( |
| 91 | // phpcs:ignore WordPress.Security |
| 92 | (array) ( $_GET['filters'] ?? array() ) |
| 93 | ); |
| 94 | |
| 95 | $columns = $this->columns; |
| 96 | |
| 97 | /** |
| 98 | * @see https://github.com/Crocoblock/issues-tracker/issues/3112 |
| 99 | */ |
| 100 | if ( empty( $columns['id'] ) ) { |
| 101 | $columns['id'] = true; |
| 102 | } |
| 103 | |
| 104 | $view->set_select( array_keys( $columns ) ); |
| 105 | |
| 106 | return $view->query()->generate_all(); |
| 107 | } |
| 108 | |
| 109 | protected function modify_columns() { |
| 110 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 111 | $columns = array_map( 'sanitize_key', (array) ( $_GET['columns'] ?? array() ) ); |
| 112 | |
| 113 | foreach ( $this->columns as $column_name => $label ) { |
| 114 | if ( ! in_array( $column_name, $columns, true ) ) { |
| 115 | unset( $this->columns[ $column_name ] ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | protected function modify_record_columns() { |
| 121 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 122 | $columns = array_map( 'sanitize_key', (array) ( $_GET['columns'] ?? array() ) ); |
| 123 | |
| 124 | foreach ( $this->record_columns as $column_name => $label ) { |
| 125 | if ( ! in_array( $column_name, $columns, true ) ) { |
| 126 | unset( $this->record_columns[ $column_name ] ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | $this->update_record_empty_columns(); |
| 131 | } |
| 132 | |
| 133 | protected function modify_payer_columns() { |
| 134 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 135 | $columns = array_map( 'sanitize_key', (array) ( $_GET['payerColumns'] ?? array() ) ); |
| 136 | |
| 137 | foreach ( $this->payers_columns as $column_name => $label ) { |
| 138 | if ( ! in_array( $column_name, $columns, true ) ) { |
| 139 | unset( $this->payers_columns[ $column_name ] ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | $this->update_payer_empty_columns(); |
| 144 | } |
| 145 | |
| 146 | protected function modify_shipping_columns() { |
| 147 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 148 | $columns = array_map( 'sanitize_key', (array) ( $_GET['shippingColumns'] ?? array() ) ); |
| 149 | |
| 150 | foreach ( $this->shipping_columns as $column_name => $label ) { |
| 151 | if ( ! in_array( $column_name, $columns, true ) ) { |
| 152 | unset( $this->shipping_columns[ $column_name ] ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | $this->update_shipping_empty_columns(); |
| 157 | } |
| 158 | } |
| 159 |