| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Models\WPTables; |
| 4 |
|
| 5 |
use LearnPress\Helpers\LPDateTime; |
| 6 |
use LearnPress\Helpers\Response; |
| 7 |
use LearnPress\Helpers\Template; |
| 8 |
use LearnPress\Models\OrderPostModel; |
| 9 |
use LP_Order; |
| 10 |
use Throwable; |
| 11 |
use WP_Post; |
| 12 |
use WP_Posts_List_Table; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* LearnPress Orders Table class. |
| 18 |
* |
| 19 |
* @since 4.4.8 |
| 20 |
* @version 1.0.0 |
| 21 |
*/ |
| 22 |
class OrdersTable extends WP_Posts_List_Table { |
| 23 |
/** |
| 24 |
* Get the table columns. |
| 25 |
* |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
public function get_columns() { |
| 29 |
$existing = parent::get_columns(); |
| 30 |
|
| 31 |
$columns = array( |
| 32 |
'cb' => '<input type="checkbox" />', |
| 33 |
'title' => esc_html__( 'Order', 'learnpress' ), |
| 34 |
'order_student' => esc_html__( 'Student', 'learnpress' ), |
| 35 |
'order_items' => esc_html__( 'Purchased', 'learnpress' ), |
| 36 |
'order_total' => esc_html__( 'Total', 'learnpress' ), |
| 37 |
'order_date' => esc_html__( 'Date', 'learnpress' ), |
| 38 |
'order_status' => '<span class="status_head tips" data-tip="' . esc_attr__( 'Status', 'learnpress' ) . '">' . esc_attr__( 'Status', 'learnpress' ) . '</span>', |
| 39 |
); |
| 40 |
|
| 41 |
$columns = array_merge( $columns, $existing ); |
| 42 |
|
| 43 |
// Unset date column default of WP |
| 44 |
unset( $columns['date'] ); |
| 45 |
|
| 46 |
return $columns; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Set columns can be sortable. |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function get_sortable_columns() { |
| 55 |
$sortable_columns = parent::get_sortable_columns(); |
| 56 |
$sortable_columns['order_date'] = array( 'date', 'asc' ); |
| 57 |
$sortable_columns['order_total'] = array( 'order_total', 'asc' ); |
| 58 |
|
| 59 |
return $sortable_columns; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Column order student. |
| 64 |
* |
| 65 |
* @param WP_Post $post |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function column_order_student( $post ) { |
| 70 |
try { |
| 71 |
$orderPostModel = OrderPostModel::find_by_id( $post->ID, true ); |
| 72 |
if ( ! $orderPostModel ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$lp_order = learn_press_get_order( $post->ID ); |
| 77 |
$user_ids = $lp_order->get_users(); |
| 78 |
|
| 79 |
if ( $user_ids ) { |
| 80 |
$outputs = array(); |
| 81 |
foreach ( $user_ids as $user_id ) { |
| 82 |
if ( get_user_by( 'id', $user_id ) ) { |
| 83 |
$user = learn_press_get_user( $user_id ); |
| 84 |
$outputs[] = sprintf( |
| 85 |
'<a href="user-edit.php?user_id=%d">%s (%s)</a><span>%s</span>', |
| 86 |
$user_id, |
| 87 |
$user->get_data( 'user_login' ), |
| 88 |
$user->get_data( 'display_name' ), |
| 89 |
$user->get_data( 'user_email' ) |
| 90 |
); |
| 91 |
} elseif ( sizeof( $user_ids ) === 1 ) { |
| 92 |
$outputs[] = $lp_order->get_customer_name(); |
| 93 |
} |
| 94 |
} |
| 95 |
echo join( ', ', $outputs ); |
| 96 |
} else { |
| 97 |
esc_html_e( '(Guest)', 'learnpress' ); |
| 98 |
} |
| 99 |
} catch ( Throwable $e ) { |
| 100 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Column order items. |
| 106 |
* |
| 107 |
* @param WP_Post $post |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function column_order_items( $post ) { |
| 112 |
try { |
| 113 |
$lp_order = learn_press_get_order( $post->ID ); |
| 114 |
do_action( 'learn-press/admin/order-items/layout', $lp_order ); |
| 115 |
} catch ( Throwable $e ) { |
| 116 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Column order date. |
| 122 |
* |
| 123 |
* @param WP_Post $post |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function column_order_date( $post ) { |
| 128 |
try { |
| 129 |
$orderPostModel = OrderPostModel::find_by_id( $post->ID, true ); |
| 130 |
if ( ! $orderPostModel ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// Check column post_date_gmt is default value. |
| 135 |
if ( '0000-00-00 00:00:00' === $orderPostModel->post_date_gmt ) { |
| 136 |
$convert_post_date_gmt = get_gmt_from_date( $orderPostModel->post_date ); |
| 137 |
$orderPostModel->post_date_gmt = $convert_post_date_gmt; |
| 138 |
} |
| 139 |
|
| 140 |
$time = strtotime( $orderPostModel->post_date_gmt ); |
| 141 |
$time_diff = time() - $time; |
| 142 |
|
| 143 |
$lpDateTimeGMT = new LPDateTime( $orderPostModel->post_date_gmt ); |
| 144 |
$time_local_format_mysql = $lpDateTimeGMT->format( LPDateTime::FORMAT_MYSQL, 'gmt_to_local' ); |
| 145 |
|
| 146 |
if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) { |
| 147 |
$time_display = $lpDateTimeGMT->format( |
| 148 |
LPDateTime::FORMAT_HUMAN, |
| 149 |
'gmt_to_local', |
| 150 |
true |
| 151 |
); |
| 152 |
} else { |
| 153 |
$time_display = $lpDateTimeGMT->format( |
| 154 |
LPDateTime::FORMAT_I18N_DATE_TIME, |
| 155 |
'gmt_to_local', |
| 156 |
true |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
printf( |
| 161 |
'<abbr title="%s">%s</abbr>', |
| 162 |
esc_attr( $time_local_format_mysql ), |
| 163 |
esc_html( $time_display ) |
| 164 |
); |
| 165 |
} catch ( Throwable $e ) { |
| 166 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Column order total. |
| 172 |
* |
| 173 |
* @param WP_Post $post |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function column_order_total( $post ) { |
| 178 |
try { |
| 179 |
$orderPostModel = OrderPostModel::find_by_id( $post->ID, true ); |
| 180 |
if ( ! $orderPostModel ) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$lp_order = learn_press_get_order( $post->ID ); |
| 185 |
echo wp_kses_post( $lp_order->get_formatted_order_total() ); |
| 186 |
|
| 187 |
$method_title = $lp_order->get_payment_method_title(); |
| 188 |
$method_title = apply_filters( 'learn-press/order-payment-method-title', $method_title, $lp_order ); |
| 189 |
|
| 190 |
if ( ! empty( $method_title ) ) { |
| 191 |
$method_title_html = sprintf( |
| 192 |
/* translators: %s: payment method title */ |
| 193 |
__( 'Pay via <strong>%s</strong>', 'learnpress' ), |
| 194 |
$method_title |
| 195 |
); |
| 196 |
?> |
| 197 |
<div class="payment-method-title"> |
| 198 |
<?php echo wp_kses_post( $method_title_html ); ?> |
| 199 |
</div> |
| 200 |
<?php |
| 201 |
} |
| 202 |
} catch ( Throwable $e ) { |
| 203 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Column order status. |
| 209 |
* |
| 210 |
* @param WP_Post $post |
| 211 |
* |
| 212 |
* @return void |
| 213 |
*/ |
| 214 |
public function column_order_status( $post ) { |
| 215 |
try { |
| 216 |
$orderPostModel = OrderPostModel::find_by_id( $post->ID, true ); |
| 217 |
if ( ! $orderPostModel ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
$lp_order = learn_press_get_order( $post->ID ); |
| 222 |
$lp_order_icons = LP_Order::get_icons_status(); |
| 223 |
$icon = $lp_order_icons[ $lp_order->get_status() ] ?? ''; |
| 224 |
$badge_html = ''; |
| 225 |
|
| 226 |
$refund_request_status = $lp_order->get_refund_request(); |
| 227 |
if ( 'pending' === $refund_request_status ) { |
| 228 |
$badge_html = sprintf( |
| 229 |
'<span class="lp-order-refund-request-badge">%s</span>', |
| 230 |
esc_html__( 'Refund Requested', 'learnpress' ) |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
printf( |
| 235 |
'<span class="lp-order-status %1$s">%2$s%3$s</span>%4$s', |
| 236 |
esc_attr( $lp_order->get_status() ), |
| 237 |
$icon, |
| 238 |
esc_html( LP_Order::get_status_label( $lp_order->get_status() ) ), |
| 239 |
$badge_html |
| 240 |
); |
| 241 |
} catch ( Throwable $e ) { |
| 242 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|