AffiliationPayoutsController.php
2 years ago
AffiliationPayoutsListTable.php
1 year ago
AffiliationPayoutsScriptsController.php
2 years ago
AffiliationPayoutsController.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\AffiliationPayouts; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\AdminController; |
| 6 | use SureCart\Models\Payout; |
| 7 | |
| 8 | /** |
| 9 | * Handles affiliate payout admin routes. |
| 10 | */ |
| 11 | class AffiliationPayoutsController extends AdminController { |
| 12 | |
| 13 | /** |
| 14 | * Affiliate Payout index. |
| 15 | */ |
| 16 | public function index() { |
| 17 | $table = new AffiliationPayoutsListTable(); |
| 18 | $table->prepare_items(); |
| 19 | |
| 20 | $this->withHeader( |
| 21 | array( |
| 22 | 'breadcrumbs' => [ |
| 23 | 'affiliate_payouts' => [ |
| 24 | 'title' => __( 'Affiliate Payouts', 'surecart' ), |
| 25 | ], |
| 26 | ], |
| 27 | 'suffix' => '<sc-button href="' . esc_url( admin_url( 'admin.php?page=sc-affiliate-payouts&action=export' ) ) . '" type="primary">' . __( 'Export Payouts', 'surecart' ) . '</sc-button>', |
| 28 | ) |
| 29 | ); |
| 30 | |
| 31 | $this->withNotices( |
| 32 | array( |
| 33 | 'processing' => __( 'Affiliate payout marked processing.', 'surecart' ), |
| 34 | 'completed' => __( 'Affiliate payout marked completed.', 'surecart' ), |
| 35 | 'deleted' => __( 'Affiliate payout deleted.', 'surecart' ), |
| 36 | ) |
| 37 | ); |
| 38 | |
| 39 | return \SureCart::view( 'admin/affiliation-payouts/index' )->with( [ 'table' => $table ] ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Edit an affiliate payout. |
| 44 | * |
| 45 | * @param \SureCartCore\Http\Request $request Request object. |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | public function edit( $request ) { |
| 50 | // enqueue needed script. |
| 51 | add_action( 'admin_enqueue_scripts', \SureCart::closure()->method( AffiliationPayoutsScriptsController::class, 'enqueue' ) ); |
| 52 | |
| 53 | $this->preloadPaths( |
| 54 | [ |
| 55 | '/wp/v2/users/me', |
| 56 | '/wp/v2/types?context=view', |
| 57 | '/wp/v2/types?context=edit', |
| 58 | '/surecart/v1/affiliation_payouts/' . $request->query( 'id' ) . '?context=edit', |
| 59 | ] |
| 60 | ); |
| 61 | |
| 62 | // return view. |
| 63 | return '<div id="app"></div>'; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Export affiliate payouts. |
| 68 | * |
| 69 | * @param \SureCartCore\Http\Request $request Request object. |
| 70 | * |
| 71 | * @return string |
| 72 | */ |
| 73 | public function export( $request ) { |
| 74 | add_action( 'admin_enqueue_scripts', \SureCart::closure()->method( AffiliationPayoutsScriptsController::class, 'enqueue' ) ); |
| 75 | |
| 76 | $this->preloadPaths( |
| 77 | [ |
| 78 | '/wp/v2/users/me?context=edit', |
| 79 | '/wp/v2/types?context=view', |
| 80 | '/wp/v2/types?context=edit', |
| 81 | '/surecart/v1/affiliation_payouts/' . $request->query( 'id' ) . '?context=edit', |
| 82 | ] |
| 83 | ); |
| 84 | |
| 85 | // return view. |
| 86 | return '<div id="app"></div>'; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Delete an affiliate payout. |
| 91 | * |
| 92 | * @param \SureCartCore\Http\Request $request Request object. |
| 93 | * |
| 94 | * @return \SureCartCore\Responses\RedirectResponse |
| 95 | */ |
| 96 | public function delete( $request ) { |
| 97 | $deleted = Payout::delete( $request->query( 'id' ) ); |
| 98 | |
| 99 | if ( is_wp_error( $deleted ) ) { |
| 100 | wp_die( implode( ' ', array_map( 'esc_html', $deleted->get_error_messages() ) ) ); |
| 101 | } |
| 102 | |
| 103 | return \SureCart::redirect()->to( add_query_arg( array( 'deleted' => true ), \SureCart::getUrl()->index( 'affiliate-payouts' ) ) ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Complete an affiliate payout. |
| 108 | * |
| 109 | * @param \SureCartCore\Http\Request $request Request object. |
| 110 | * |
| 111 | * @return \SureCartCore\Responses\RedirectResponse |
| 112 | */ |
| 113 | public function complete( $request ) { |
| 114 | $completed = Payout::complete( $request->query( 'id' ) ); |
| 115 | |
| 116 | if ( is_wp_error( $completed ) ) { |
| 117 | wp_die( implode( ' ', array_map( 'esc_html', $completed->get_error_messages() ) ) ); |
| 118 | } |
| 119 | |
| 120 | return \SureCart::redirect()->to( add_query_arg( array( 'completed' => true ), \SureCart::getUrl()->index( 'affiliate-payouts' ) ) ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Make an affiliate payout processing. |
| 125 | * |
| 126 | * @param \SureCartCore\Http\Request $request Request object. |
| 127 | * |
| 128 | * @return \SureCartCore\Responses\RedirectResponse |
| 129 | */ |
| 130 | public function makeProcessing( $request ) { |
| 131 | $processed = Payout::make_processing( $request->query( 'id' ) ); |
| 132 | |
| 133 | if ( is_wp_error( $processed ) ) { |
| 134 | wp_die( implode( ' ', array_map( 'esc_html', $processed->get_error_messages() ) ) ); |
| 135 | } |
| 136 | |
| 137 | return \SureCart::redirect()->to( add_query_arg( array( 'processing' => true ), \SureCart::getUrl()->index( 'affiliate-payouts' ) ) ); |
| 138 | } |
| 139 | } |
| 140 |