Submission_Export_Handler

Class for handling submission export.

Description

Source

File: src/components/submission-export-handler.php

class Submission_Export_Handler extends Service {

	/**
	 * Submission manager instance.
	 *
	 * @since 1.0.0
	 * @var Submission_Manager
	 */
	protected $submission_manager;

	/**
	 * Submission export mode.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $modes = array();

	/**
	 * Nonce action to use.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $nonce_action = '';

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param string             $prefix             Instance prefix.
	 * @param Submission_Manager $submission_manager Submission manager instance.
	 */
	public function __construct( $prefix, $submission_manager ) {
		$this->set_prefix( $prefix );

		$this->submission_manager = $submission_manager;

		$this->modes = array(
			'xls' => new Submission_Export_XLS( $this ),
			'csv' => new Submission_Export_CSV( $this ),
		);

		$this->nonce_action = $this->get_prefix() . 'submission_export';
	}

	/**
	 * Gets the export admin action name.
	 *
	 * @since 1.0.0
	 *
	 * @return string Action name.
	 */
	public function get_export_action_name() {
		return $this->nonce_action;
	}

	/**
	 * Exports submissions for a form with a specific export mode.
	 *
	 * This method will terminate the current request.
	 *
	 * @since 1.0.0
	 *
	 * @param string $mode Export mode to use. Either 'xls' or 'csv'.
	 * @param Form   $form Form to export submissions for.
	 * @param array  $args Optional. Extra query arguments to pass to the submissions
	 *                     query.
	 */
	public function export_submissions( $mode, $form, $args = array() ) {
		if ( ! isset( $this->modes[ $mode ] ) ) {
			wp_die( __( 'Invalid submission export handler.', 'torro-forms' ) );
		}

		$this->modes[ $mode ]->export_submissions( $form, $args );
		exit;
	}

	/**
	 * Handles the export admin action.
	 *
	 * @since 1.0.0
	 */
	public function handle_export_action() {
		if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
			wp_die( __( 'Missing nonce.', 'torro-forms' ) );
		}

		if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], $this->nonce_action ) ) {
			wp_die( __( 'Invalid nonce.', 'torro-forms' ) );
		}

		if ( ! isset( $_REQUEST['form_id'] ) ) {
			wp_die( __( 'Missing form ID.', 'torro-forms' ) );
		}

		$capabilities = $this->submission_manager->capabilities();
		if ( ! $capabilities || ! $capabilities->user_can_read() ) {
			wp_die( __( 'Insufficient permissions.', 'torro-forms' ) );
		}

		$form = $this->submission_manager->get_parent_manager( 'forms' )->get( (int) $_REQUEST['form_id'] );
		if ( ! $form ) {
			wp_die( __( 'Invalid form ID.', 'torro-forms' ) );
		}

		if ( ! isset( $_REQUEST['mode'] ) ) {
			wp_die( __( 'Missing submission export handler.', 'torro-forms' ) );
		}

		$args = array();
		if ( isset( $_REQUEST['orderby'] ) && isset( $_REQUEST['order'] ) ) {
			$orderby = in_array( $_REQUEST['orderby'], array( 'id', 'timestamp' ), true ) ? $_REQUEST['orderby'] : 'id';
			$order   = in_array( $_REQUEST['order'], array( 'ASC', 'DESC' ), true ) ? $_REQUEST['order'] : 'ASC';

			$args['orderby'] = array( $orderby => $order );
		} else {
			$args['orderby'] = array( 'id' => 'ASC' );
		}

		$this->export_submissions( wp_unslash( $_REQUEST['mode'] ), $form, $args );
	}

	/**
	 * Renders the export form.
	 *
	 * @since 1.0.0
	 */
	public function render_export_form() {
		if ( ! isset( $_REQUEST['form_id'] ) ) {
			return;
		}

		?>
		<form class="torro-export-form" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="POST">
			<input type="hidden" name="action" value="<?php echo esc_attr( $this->get_export_action_name() ); ?>" />
			<input type="hidden" name="form_id" value="<?php echo absint( $_REQUEST['form_id'] ); ?>" />
			<?php wp_nonce_field( $this->nonce_action ); ?>

			<h3><?php _e( 'Export Submissions', 'torro-forms' ); ?></h3>

			<p class="description">
				<?php _e( 'Here you can export all completed submissions in a file format of your choice.', 'torro-forms' ); ?>
			</p>

			<label for="torro-export-mode"><?php _e( 'Export as', 'torro-forms' ); ?></label>
			<select id="torro-export-mode" name="mode" style="margin-right:15px;">
				<?php foreach ( $this->modes as $slug => $mode ) : ?>
					<option value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_html( $mode->get_title() ); ?></option>
				<?php endforeach; ?>
			</select>

			<label for="torro-export-orderby"><?php _e( 'Order by', 'torro-forms' ); ?></label>
			<select id="torro-export-orderby" name="orderby">
				<option value="id"><?php _e( 'ID', 'torro-forms' ); ?></option>
				<option value="timestamp"><?php _e( 'Date', 'torro-forms' ); ?></option>
			</select>

			<label for="torro-export-order" class="screen-reader-text"><?php _e( 'Order', 'torro-forms' ); ?></label>
			<select id="torro-export-order" name="order" style="margin-right:15px;">
				<option value="ASC"><?php _e( 'Ascending', 'torro-forms' ); ?></option>
				<option value="DESC"><?php _e( 'Descending', 'torro-forms' ); ?></option>
			</select>

			<button type="submit" class="button"><?php _e( 'Export', 'torro-forms' ); ?></button>
		</form>
		<?php
	}
}

Changelog

Changelog
Version Description
1.0.0 Introduced.

Methods