← All changes
|
inc/TemplateHooks/Order/AdminOrderListTemplate.php
+104
-5
4.3.7
→
4.4.8
View file →
| @@ -1,11 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace LearnPress\TemplateHooks\Order; |
| 4 | 4 | |
| 5 | +use LearnPress\Databases\PostDB; | |
| 6 | +use LearnPress\Filters\OrderPostFilter; | |
| 5 | 7 | use LearnPress\Helpers\Singleton; |
| 6 | 8 | use LearnPress\Helpers\Template; |
| 7 | 9 | use LP_Helper; |
| 10 | +use LP_Order; | |
| 11 | +use LP_Request; | |
| 12 | +use Throwable; | |
| 8 | 13 | |
| 9 | 14 | /** |
| 10 | 15 | * class AdminOrderListTemplate |
| 11 | 16 | * |
| @@ -16,12 +21,106 @@ | ||
| 16 | 21 | |
| 17 | 22 | use Singleton; |
| 18 | 23 | |
| 19 | 24 | public function init() { |
| 25 | + | |
| 20 | 26 | add_action( 'manage_posts_extra_tablenav', array( $this, 'add_export_order_button' ) ); |
| 27 | + add_filter( 'views_edit-' . LP_ORDER_CPT, array( $this, 'add_refund_requests_view' ) ); | |
| 21 | 28 | } |
| 22 | 29 | |
| 30 | + /** | |
| 31 | + * Add refund requests view to the admin order list. | |
| 32 | + * | |
| 33 | + * @param array $views | |
| 34 | + * | |
| 35 | + * @return array | |
| 36 | + * @since 4.3.9 | |
| 37 | + * @version 1.0.0 | |
| 38 | + */ | |
| 39 | + public function add_refund_requests_view( array $views ): array { | |
| 40 | + | |
| 41 | + $is_current = 'pending' === LP_Request::get_param( 'refund_request_status', '', 'key', 'get' ); | |
| 42 | + if ( $is_current ) { | |
| 43 | + foreach ( $views as $key => $view ) { | |
| 44 | + $views[ $key ] = str_replace( array( ' class="current"', " class='current'" ), '', $view ); | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + $count = self::get_refund_requests_count(); | |
| 49 | + $url = add_query_arg( | |
| 50 | + array( | |
| 51 | + 'post_type' => LP_ORDER_CPT, | |
| 52 | + 'refund_request_status' => 'pending', | |
| 53 | + ), | |
| 54 | + admin_url( 'edit.php' ) | |
| 55 | + ); | |
| 56 | + $view = sprintf( | |
| 57 | + '<a href="%1$s" class="%2$s">%3$s <span class="count">(%4$d)</span></a>', | |
| 58 | + esc_url( $url ), | |
| 59 | + $is_current ? 'current' : '', | |
| 60 | + esc_html__( 'Refund Requests', 'learnpress' ), | |
| 61 | + $count | |
| 62 | + ); | |
| 63 | + | |
| 64 | + return self::insert_refund_requests_view( $views, $view ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Insert refund requests view before terminal order statuses. | |
| 69 | + * | |
| 70 | + * @param array $views | |
| 71 | + * @param string $refund_requests_view | |
| 72 | + * | |
| 73 | + * @return array | |
| 74 | + */ | |
| 75 | + public static function insert_refund_requests_view( array $views, string $refund_requests_view ): array { | |
| 76 | + | |
| 77 | + $result = array(); | |
| 78 | + $inserted = false; | |
| 79 | + | |
| 80 | + foreach ( $views as $key => $view ) { | |
| 81 | + if ( ! $inserted && in_array( $key, array( LP_ORDER_CANCELLED_DB, LP_ORDER_REFUNDED_DB, 'trash' ), true ) ) { | |
| 82 | + $result['refund-requests'] = $refund_requests_view; | |
| 83 | + $inserted = true; | |
| 84 | + } | |
| 85 | + | |
| 86 | + $result[ $key ] = $view; | |
| 87 | + } | |
| 88 | + | |
| 89 | + if ( ! $inserted ) { | |
| 90 | + $result['refund-requests'] = $refund_requests_view; | |
| 91 | + } | |
| 92 | + | |
| 93 | + return $result; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Count pending refund requests. | |
| 98 | + * | |
| 99 | + * @return int | |
| 100 | + */ | |
| 101 | + public static function get_refund_requests_count(): int { | |
| 102 | + | |
| 103 | + try { | |
| 104 | + $filter = new OrderPostFilter(); | |
| 105 | + $filter->query_count = true; | |
| 106 | + LP_Order::handle_params_query_list_orders( | |
| 107 | + $filter, | |
| 108 | + array( | |
| 109 | + 'post_status' => 'all', | |
| 110 | + 'refund_request_status' => 'pending', | |
| 111 | + 'posts_per_page' => -1, | |
| 112 | + ) | |
| 113 | + ); | |
| 114 | + | |
| 115 | + return (int) PostDB::getInstance()->get_posts( $filter ); | |
| 116 | + } catch ( Throwable $e ) { | |
| 117 | + return 0; | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 23 | 121 | public function add_export_order_button( $which ) { |
| 122 | + | |
| 24 | 123 | if ( $which !== 'top' ) { |
| 25 | 124 | return; |
| 26 | 125 | } |
| 27 | 126 | |
| @@ -29,17 +128,17 @@ | ||
| 29 | 128 | if ( ! $screen || $screen->base !== 'edit' || $screen->post_type !== LP_ORDER_CPT ) { |
| 30 | 129 | return; |
| 31 | 130 | } |
| 32 | 131 | |
| 33 | - $order_data = [ | |
| 132 | + $order_data = array( | |
| 34 | 133 | 'action' => 'export_order_csv', |
| 35 | - 'export_id' => time() . '-' . uniqid(), | |
| 36 | - ]; | |
| 134 | + 'export_id' => wp_generate_password( 24, false ), | |
| 135 | + ); | |
| 37 | 136 | |
| 38 | 137 | $data_get = LP_Helper::sanitize_params_submitted( $_GET ); |
| 39 | 138 | $order_data = array_merge( $data_get, $order_data ); |
| 40 | 139 | |
| 41 | - $section = [ | |
| 140 | + $section = array( | |
| 42 | 141 | 'wrap-start' => '<div class="alignleft actions">', |
| 43 | 142 | 'btn-export-start' => sprintf( |
| 44 | 143 | '<button type="button" class="button lp-button lp-btn-export-order-to-csv" data-send="%s">', |
| 45 | 144 | esc_attr( Template::convert_data_to_json( $order_data ) ) |
| @@ -46,9 +145,9 @@ | ||
| 46 | 145 | ), |
| 47 | 146 | 'btn-text' => esc_html__( 'Export to CSV', 'learnpress' ), |
| 48 | 147 | 'btn-export-end' => '</button>', |
| 49 | 148 | 'wrap-end' => '</div>', |
| 50 | - ]; | |
| 149 | + ); | |
| 51 | 150 | |
| 52 | 151 | echo Template::combine_components( $section ); |
| 53 | 152 | } |
| 54 | 153 | } |