| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Ajax\Order; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Ajax\AbstractAjax; |
| 7 |
use LearnPress\Databases\PostDB; |
| 8 |
use LearnPress\Filters\OrderPostFilter; |
| 9 |
use LearnPress\Models\UserModel; |
| 10 |
use LP_Datetime; |
| 11 |
use LP_Helper; |
| 12 |
use LP_Order; |
| 13 |
use LP_Request; |
| 14 |
use LP_REST_Response; |
| 15 |
use Throwable; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class ExportOrderCSVAjax |
| 21 |
* |
| 22 |
* @author ThimPress |
| 23 |
* @package LearnPress\Ajax |
| 24 |
* @since 4.3.2.8 |
| 25 |
* @version 1.0.1 |
| 26 |
*/ |
| 27 |
class ExportOrderCSVAjax extends AbstractAjax { |
| 28 |
/** |
| 29 |
* Export orders to CSV file, action call from js file: export-order.js |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function export_order_csv() { |
| 34 |
$response = new LP_REST_Response(); |
| 35 |
try { |
| 36 |
// Check permission |
| 37 |
if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) { |
| 38 |
throw new Exception( __( 'You do not have permission to perform this action.', 'learnpress' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
$data_str = LP_Request::get_param( 'data' ); |
| 42 |
$params = LP_Helper::json_decode( $data_str, true ); |
| 43 |
|
| 44 |
$export_id = sanitize_key( $params['export_id'] ?? '' ); |
| 45 |
if ( ! $export_id ) { |
| 46 |
throw new Exception( __( 'Missing export id', 'learnpress' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
$paged = max( 1, intval( $params['paged'] ?? 1 ) ); |
| 50 |
|
| 51 |
$post_db = PostDB::getInstance(); |
| 52 |
$filter = new OrderPostFilter(); |
| 53 |
$filter->post_type = LP_ORDER_CPT; |
| 54 |
LP_Order::handle_params_query_list_orders( $filter, $params ); |
| 55 |
$total_rows = 0; |
| 56 |
$lp_orders = $post_db->get_posts( $filter, $total_rows ); |
| 57 |
|
| 58 |
if ( count( $lp_orders ) > 0 ) { |
| 59 |
$file = self::get_export_csv_path( $export_id ); |
| 60 |
$handle = fopen( $file, $paged === 1 ? 'w' : 'a' ); |
| 61 |
|
| 62 |
if ( $paged === 1 ) { |
| 63 |
fprintf( $handle, "\xEF\xBB\xBF" ); |
| 64 |
fputcsv( |
| 65 |
$handle, |
| 66 |
[ |
| 67 |
__( 'Order', 'learnpress' ), |
| 68 |
__( 'Student', 'learnpress' ), |
| 69 |
__( 'Purchased', 'learnpress' ), |
| 70 |
__( 'Date', 'learnpress' ), |
| 71 |
__( 'Total', 'learnpress' ), |
| 72 |
__( 'Status', 'learnpress' ), |
| 73 |
], |
| 74 |
',', |
| 75 |
'"', |
| 76 |
'\\', |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
foreach ( $lp_orders as $order ) { |
| 81 |
$order_id = $order->ID; |
| 82 |
$order = learn_press_get_order( $order_id ); |
| 83 |
if ( ! $order ) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
fputcsv( |
| 87 |
$handle, |
| 88 |
[ |
| 89 |
$order->get_order_number(), |
| 90 |
$this->get_order_users( $order ), |
| 91 |
$this->get_order_items( $order ), |
| 92 |
$order->get_order_date( 'edit' )->format( LP_Datetime::I18N_FORMAT_HAS_TIME ), |
| 93 |
$order->get_formatted_order_total(), |
| 94 |
LP_Order::get_status_label( $order->get_status() ), |
| 95 |
], |
| 96 |
',', |
| 97 |
'"', |
| 98 |
'\\', |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
fclose( $handle ); |
| 103 |
} else { |
| 104 |
if ( intval( $paged ) === 1 ) { |
| 105 |
throw new Exception( __( 'There are no orders.', 'learnpress' ) ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
$data = []; |
| 110 |
$max_page = $post_db::get_total_pages( $filter->limit, $total_rows ); |
| 111 |
$data['max_page'] = $max_page; |
| 112 |
|
| 113 |
if ( $paged < $max_page ) { |
| 114 |
$data['next_page'] = $paged + 1; |
| 115 |
} else { |
| 116 |
$data['download_url'] = add_query_arg( |
| 117 |
[ |
| 118 |
'lp_download_order' => 1, |
| 119 |
'export_id' => $export_id, |
| 120 |
], |
| 121 |
home_url( '/' ) |
| 122 |
); |
| 123 |
$data['done'] = true; |
| 124 |
$response->message = esc_html__( 'Orders export successfully!', 'learnpress' ); |
| 125 |
} |
| 126 |
|
| 127 |
$response->data = $data; |
| 128 |
$response->status = 'success'; |
| 129 |
} catch ( Throwable $e ) { |
| 130 |
$response->message = $e->getMessage(); |
| 131 |
} |
| 132 |
|
| 133 |
wp_send_json( $response ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get export CSV file path |
| 138 |
* |
| 139 |
* @param string $export_id |
| 140 |
* |
| 141 |
* @return string |
| 142 |
* @since 4.3.2.8 |
| 143 |
* @version 1.0.0 |
| 144 |
*/ |
| 145 |
public static function get_export_csv_path( string $export_id = '' ): string { |
| 146 |
$upload = wp_upload_dir(); |
| 147 |
$dir = $upload['basedir'] . '/lp-order-export'; |
| 148 |
|
| 149 |
if ( ! file_exists( $dir ) ) { |
| 150 |
wp_mkdir_p( $dir ); |
| 151 |
} |
| 152 |
|
| 153 |
$index_file = $dir . '/index.php'; |
| 154 |
if ( ! file_exists( $index_file ) ) { |
| 155 |
file_put_contents( $index_file, "<?php\n// Silence is golden.\n" ); |
| 156 |
} |
| 157 |
|
| 158 |
return $dir . "/lp-orders-{$export_id}.csv"; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param LP_Order $order |
| 163 |
* |
| 164 |
* @return string|null |
| 165 |
*/ |
| 166 |
public function get_order_users( $order ) { |
| 167 |
$user_ids = $order->get_users(); |
| 168 |
$checkout_email = $order->get_checkout_email(); |
| 169 |
|
| 170 |
if ( $order->is_manual() && empty( $user_ids ) ) { |
| 171 |
return __( '(No customer)', 'learnpress' ); |
| 172 |
} else { |
| 173 |
$outputs = array(); |
| 174 |
foreach ( $user_ids as $user_id ) { |
| 175 |
$userModel = UserModel::find( $user_id, true ); |
| 176 |
if ( $userModel ) { |
| 177 |
$outputs[] = sprintf( |
| 178 |
'%s (#%d)', |
| 179 |
$userModel->get_display_name(), |
| 180 |
$user_id, |
| 181 |
); |
| 182 |
} elseif ( $user_id > 0 ) { |
| 183 |
$outputs[] = sprintf( |
| 184 |
__( 'User #%d (Deleted)', 'learnpress' ), |
| 185 |
$user_id, |
| 186 |
); |
| 187 |
} elseif ( ! empty( $checkout_email ) ) { |
| 188 |
$outputs[] = sprintf( |
| 189 |
'%s', |
| 190 |
$order->get_checkout_email(), |
| 191 |
); |
| 192 |
} else { |
| 193 |
$outputs[] = __( '(Guest)', 'learnpress' ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return join( ', ', $outputs ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @param LP_Order $order |
| 203 |
* |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
private function get_order_items( $order ): string { |
| 207 |
$order_item_str = ''; |
| 208 |
$items = $order->get_all_items(); |
| 209 |
$total_items = count( $items ); |
| 210 |
|
| 211 |
if ( $total_items === 0 ) { |
| 212 |
return __( '(No item)', 'learnpress' ); |
| 213 |
} else { |
| 214 |
foreach ( $items as $i => $itemObj ) { |
| 215 |
$item_type = $itemObj['item_type'] ?? ''; |
| 216 |
$item_id = $itemObj['item_id'] ?? 0; |
| 217 |
$item_name = $itemObj['order_item_name'] ?? ''; |
| 218 |
$course_id_meta = learn_press_get_order_item_meta( $itemObj['order_item_id'], '_course_id' ); |
| 219 |
// For old data, the data not migrated yet to new column. |
| 220 |
if ( ( empty( $item_type ) || empty( $item_id ) ) |
| 221 |
&& ! empty( $course_id_meta ) ) { |
| 222 |
$item_id = $course_id_meta; |
| 223 |
$item_type = LP_COURSE_CPT; |
| 224 |
} |
| 225 |
|
| 226 |
$order_item_str .= sprintf( |
| 227 |
'%s (#%d)', |
| 228 |
esc_html( $item_name ), |
| 229 |
$item_id |
| 230 |
); |
| 231 |
|
| 232 |
if ( $i < $total_items && $total_items > 1 ) { |
| 233 |
$order_item_str .= '|'; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return $order_item_str; |
| 239 |
} |
| 240 |
} |
| 241 |
|