← All changes
|
inc/TemplateHooks/Order/AdminOrderListTemplate.php
+153
-54
4.3.8
→
4.4.8
View file →
| @@ -1,54 +1,153 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace LearnPress\TemplateHooks\Order; | |
| 4 | - | |
| 5 | -use LearnPress\Helpers\Singleton; | |
| 6 | -use LearnPress\Helpers\Template; | |
| 7 | -use LP_Helper; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * class AdminOrderListTemplate | |
| 11 | - * | |
| 12 | - * @since 4.3.2.8 | |
| 13 | - * @version 1.0.0 | |
| 14 | - */ | |
| 15 | -class AdminOrderListTemplate { | |
| 16 | - | |
| 17 | - use Singleton; | |
| 18 | - | |
| 19 | - public function init() { | |
| 20 | - add_action( 'manage_posts_extra_tablenav', array( $this, 'add_export_order_button' ) ); | |
| 21 | - } | |
| 22 | - | |
| 23 | - public function add_export_order_button( $which ) { | |
| 24 | - if ( $which !== 'top' ) { | |
| 25 | - return; | |
| 26 | - } | |
| 27 | - | |
| 28 | - $screen = get_current_screen(); | |
| 29 | - if ( ! $screen || $screen->base !== 'edit' || $screen->post_type !== LP_ORDER_CPT ) { | |
| 30 | - return; | |
| 31 | - } | |
| 32 | - | |
| 33 | - $order_data = [ | |
| 34 | - 'action' => 'export_order_csv', | |
| 35 | - 'export_id' => time() . '-' . uniqid(), | |
| 36 | - ]; | |
| 37 | - | |
| 38 | - $data_get = LP_Helper::sanitize_params_submitted( $_GET ); | |
| 39 | - $order_data = array_merge( $data_get, $order_data ); | |
| 40 | - | |
| 41 | - $section = [ | |
| 42 | - 'wrap-start' => '<div class="alignleft actions">', | |
| 43 | - 'btn-export-start' => sprintf( | |
| 44 | - '<button type="button" class="button lp-button lp-btn-export-order-to-csv" data-send="%s">', | |
| 45 | - esc_attr( Template::convert_data_to_json( $order_data ) ) | |
| 46 | - ), | |
| 47 | - 'btn-text' => esc_html__( 'Export to CSV', 'learnpress' ), | |
| 48 | - 'btn-export-end' => '</button>', | |
| 49 | - 'wrap-end' => '</div>', | |
| 50 | - ]; | |
| 51 | - | |
| 52 | - echo Template::combine_components( $section ); | |
| 53 | - } | |
| 54 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace LearnPress\TemplateHooks\Order; | |
| 4 | + | |
| 5 | +use LearnPress\Databases\PostDB; | |
| 6 | +use LearnPress\Filters\OrderPostFilter; | |
| 7 | +use LearnPress\Helpers\Singleton; | |
| 8 | +use LearnPress\Helpers\Template; | |
| 9 | +use LP_Helper; | |
| 10 | +use LP_Order; | |
| 11 | +use LP_Request; | |
| 12 | +use Throwable; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * class AdminOrderListTemplate | |
| 16 | + * | |
| 17 | + * @since 4.3.2.8 | |
| 18 | + * @version 1.0.0 | |
| 19 | + */ | |
| 20 | +class AdminOrderListTemplate { | |
| 21 | + | |
| 22 | + use Singleton; | |
| 23 | + | |
| 24 | + public function init() { | |
| 25 | + | |
| 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' ) ); | |
| 28 | + } | |
| 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 | + | |
| 121 | + public function add_export_order_button( $which ) { | |
| 122 | + | |
| 123 | + if ( $which !== 'top' ) { | |
| 124 | + return; | |
| 125 | + } | |
| 126 | + | |
| 127 | + $screen = get_current_screen(); | |
| 128 | + if ( ! $screen || $screen->base !== 'edit' || $screen->post_type !== LP_ORDER_CPT ) { | |
| 129 | + return; | |
| 130 | + } | |
| 131 | + | |
| 132 | + $order_data = array( | |
| 133 | + 'action' => 'export_order_csv', | |
| 134 | + 'export_id' => wp_generate_password( 24, false ), | |
| 135 | + ); | |
| 136 | + | |
| 137 | + $data_get = LP_Helper::sanitize_params_submitted( $_GET ); | |
| 138 | + $order_data = array_merge( $data_get, $order_data ); | |
| 139 | + | |
| 140 | + $section = array( | |
| 141 | + 'wrap-start' => '<div class="alignleft actions">', | |
| 142 | + 'btn-export-start' => sprintf( | |
| 143 | + '<button type="button" class="button lp-button lp-btn-export-order-to-csv" data-send="%s">', | |
| 144 | + esc_attr( Template::convert_data_to_json( $order_data ) ) | |
| 145 | + ), | |
| 146 | + 'btn-text' => esc_html__( 'Export to CSV', 'learnpress' ), | |
| 147 | + 'btn-export-end' => '</button>', | |
| 148 | + 'wrap-end' => '</div>', | |
| 149 | + ); | |
| 150 | + | |
| 151 | + echo Template::combine_components( $section ); | |
| 152 | + } | |
| 153 | +} | |