LLMS_Meta_Box_Voucher_Export::export()

Export vouchers.


Return Return

([type]) [description]


Top ↑

Source Source

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

	public static function export() {

		if ( empty( llms_filter_input( INPUT_POST, 'llms_generate_export' ) ) || ! llms_verify_nonce( 'lifterlms_export_nonce', 'lifterlms_csv_export_data' ) ) {
			return false;
		}

		$type = llms_filter_input( INPUT_POST, 'llms_voucher_export_type' );
		if ( ! 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;
				}

				$send_email = llms_parse_bool( llms_filter_input( INPUT_POST, 'llms_voucher_export_send_email' ) );
				if ( $send_email ) {

					// Send email.
					$email_text = trim( llms_filter_input_sanitize_string( INPUT_POST, 'llms_voucher_export_email' ) );
					if ( ! empty( $email_text ) ) {

						$emails = array_filter( array_map( 'is_email', array_map( 'trim', 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 ↑

Changelog Changelog

Changelog
Version Description
5.9.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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