LLMS_Meta_Box_Voucher_Export::export()


Description Description


Source Source

File: includes/admin/post-types/meta-boxes/class.llms.meta.box.voucher.export.php

	public static function export() {

		if ( empty( $_POST['llms_generate_export'] ) || empty( $_POST['lifterlms_export_nonce'] ) || ! wp_verify_nonce( $_POST['lifterlms_export_nonce'], 'lifterlms_csv_export_data' ) ) {
			return false;
		}

		$type = ( isset( $_POST['llms_voucher_export_type'] ) ) ? $_POST['llms_voucher_export_type'] : false;
		if ( isset( $type ) && ! empty( $type ) ) {

			if ( 'vouchers' === $type || 'redeemed' === $type ) {

				// export CSV

				$csv = array();
				$file_name = '';

				global $post;
				$voucher = new LLMS_Voucher( $post->ID );

				switch ( $type ) {
					case 'vouchers':

						$voucher = new LLMS_Voucher( $post->ID );
						$codes = $voucher->get_voucher_codes( 'ARRAY_A' );

						if ( ! $codes ) {
							/**
							 * @todo  error handling here
							 */
							return;
						}

						foreach ( $codes as $k => $v ) {
							unset( $codes[ $k ]['id'] );
							unset( $codes[ $k ]['voucher_id'] );
							$codes[ $k ]['count'] = $codes[ $k ]['redemption_count'];
							$codes[ $k ]['used'] = $codes[ $k ]['used'];
							$codes[ $k ]['created'] = $codes[ $k ]['created_at'];
							$codes[ $k ]['updated'] = $codes[ $k ]['updated_at'];
							unset( $codes[ $k ]['redemption_count'] );
							unset( $codes[ $k ]['created_at'] );
							unset( $codes[ $k ]['updated_at'] );
							unset( $codes[ $k ]['is_deleted'] );

						}
						$csv = self::array_to_csv( $codes );

						$file_name = 'vouchers.csv';
					break;

					case 'redeemed':

						$redeemed_codes = $voucher->get_redeemed_codes( 'ARRAY_A' );

						if ( ! $redeemed_codes ) {
							/**
							 * @todo  error handling here
							 */
							return;
						}

						foreach ( $redeemed_codes as $k => $v ) {
							unset( $redeemed_codes[ $k ]['id'] );
							unset( $redeemed_codes[ $k ]['code_id'] );
							unset( $redeemed_codes[ $k ]['voucher_id'] );
							unset( $redeemed_codes[ $k ]['redemption_count'] );
							unset( $redeemed_codes[ $k ]['user_id'] );

						}

						$csv = self::array_to_csv( $redeemed_codes );

						$file_name = 'redeemed_codes.csv';

					break;
				}// End switch().

				$send_email = isset( $_POST['llms_voucher_export_send_email'] ) ? $_POST['llms_voucher_export_send_email'] : false;

				if ( isset( $send_email ) && ! empty( $send_email ) && true == $send_email ) {

					// send email
					$email_text = trim( $_POST['llms_voucher_export_email'] );
					if ( isset( $email_text ) && ! empty( $email_text ) ) {

						$emails = explode( ',', $email_text );

						if ( ! empty( $emails ) ) {

							$voucher = new LLMS_Voucher( $post->ID );

							self::send_email( $csv, $emails, $voucher->get_voucher_title() );
						}
					}

					return false;
				}

				self::download_csv( $csv, $file_name );
			}// End if().
		}// End if().

	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.